aboutsummaryrefslogtreecommitdiff
path: root/src/target/dsp563xx.c
blob: b9c8b4dc27d5bb0c63d2c7635f39e6916521820e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
/***************************************************************************
 *   Copyright (C) 2009-2011 by Mathias Kuester                            *
 *   mkdorg@users.sourceforge.net                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <jim.h>

#include "target.h"
#include "target_type.h"
#include "algorithm.h"
#include "register.h"
#include "dsp563xx.h"
#include "dsp563xx_once.h"

#define ASM_REG_W_R0    0x60F400
#define ASM_REG_W_R1    0x61F400
#define ASM_REG_W_R2    0x62F400
#define ASM_REG_W_R3    0x63F400
#define ASM_REG_W_R4    0x64F400
#define ASM_REG_W_R5    0x65F400
#define ASM_REG_W_R6    0x66F400
#define ASM_REG_W_R7    0x67F400

#define ASM_REG_W_N0    0x70F400
#define ASM_REG_W_N1    0x71F400
#define ASM_REG_W_N2    0x72F400
#define ASM_REG_W_N3    0x73F400
#define ASM_REG_W_N4    0x74F400
#define ASM_REG_W_N5    0x75F400
#define ASM_REG_W_N6    0x76F400
#define ASM_REG_W_N7    0x77F400

#define ASM_REG_W_M0    0x05F420
#define ASM_REG_W_M1    0x05F421
#define ASM_REG_W_M2    0x05F422
#define ASM_REG_W_M3    0x05F423
#define ASM_REG_W_M4    0x05F424
#define ASM_REG_W_M5    0x05F425
#define ASM_REG_W_M6    0x05F426
#define ASM_REG_W_M7    0x05F427

#define ASM_REG_W_X0    0x44F400
#define ASM_REG_W_X1    0x45F400

#define ASM_REG_W_Y0    0x46F400
#define ASM_REG_W_Y1    0x47F400

#define ASM_REG_W_A0    0x50F400
#define ASM_REG_W_A1    0x54F400
#define ASM_REG_W_A2    0x52F400

#define ASM_REG_W_B0    0x51F400
#define ASM_REG_W_B1    0x55F400
#define ASM_REG_W_B2    0x53F400

#define ASM_REG_W_VBA   0x05F430
#define ASM_REG_W_OMR   0x05F43A
#define ASM_REG_W_EP    0x05F42A
#define ASM_REG_W_SC    0x05F431
#define ASM_REG_W_SZ    0x05F438
#define ASM_REG_W_SR    0x05F439
#define ASM_REG_W_SP    0x05F43B
#define ASM_REG_W_SSH   0x05F43C
#define ASM_REG_W_SSL   0x05F43D
#define ASM_REG_W_LA    0x05F43E
#define ASM_REG_W_LC    0x05F43F
#define ASM_REG_W_PC    0x000000
#define ASM_REG_W_IPRC  0xFFFFFF
#define ASM_REG_W_IPRP  0xFFFFFE

#define ASM_REG_W_BCR   0xFFFFFB
#define ASM_REG_W_DCR   0xFFFFFA
#define ASM_REG_W_AAR0  0xFFFFF9
#define ASM_REG_W_AAR1  0xFFFFF8
#define ASM_REG_W_AAR2  0xFFFFF7
#define ASM_REG_W_AAR3  0xFFFFF6

enum once_reg_idx {
	ONCE_REG_IDX_OSCR = 0,
	ONCE_REG_IDX_OMBC = 1,
	ONCE_REG_IDX_OBCR = 2,
	ONCE_REG_IDX_OMLR0 = 3,
	ONCE_REG_IDX_OMLR1 = 4,
	ONCE_REG_IDX_OGDBR = 5,
	ONCE_REG_IDX_OPDBR = 6,
	ONCE_REG_IDX_OPILR = 7,
	ONCE_REG_IDX_PDB = 8,
	ONCE_REG_IDX_OTC = 9,
	ONCE_REG_IDX_OPABFR = 10,
	ONCE_REG_IDX_OPABDR = 11,
	ONCE_REG_IDX_OPABEX = 12,
	ONCE_REG_IDX_OPABF0 = 13,
	ONCE_REG_IDX_OPABF1 = 14,
	ONCE_REG_IDX_OPABF2 = 15,
	ONCE_REG_IDX_OPABF3 = 16,
	ONCE_REG_IDX_OPABF4 = 17,
	ONCE_REG_IDX_OPABF5 = 18,
	ONCE_REG_IDX_OPABF6 = 19,
	ONCE_REG_IDX_OPABF7 = 20,
	ONCE_REG_IDX_OPABF8 = 21,
	ONCE_REG_IDX_OPABF9 = 22,
	ONCE_REG_IDX_OPABF10 = 23,
	ONCE_REG_IDX_OPABF11 = 24,
};

static struct once_reg once_regs[] = {
	{ONCE_REG_IDX_OSCR,    DSP563XX_ONCE_OSCR,    24, "OSCR",    0},
	{ONCE_REG_IDX_OMBC,    DSP563XX_ONCE_OMBC,    24, "OMBC",    0},
	{ONCE_REG_IDX_OBCR,    DSP563XX_ONCE_OBCR,    24, "OBCR",    0},
	{ONCE_REG_IDX_OMLR0,   DSP563XX_ONCE_OMLR0,   24, "OMLR0",   0},
	{ONCE_REG_IDX_OMLR1,   DSP563XX_ONCE_OMLR1,   24, "OMLR1",   0},
	{ONCE_REG_IDX_OGDBR,   DSP563XX_ONCE_OGDBR,   24, "OGDBR",   0},
	{ONCE_REG_IDX_OPDBR,   DSP563XX_ONCE_OPDBR,   24, "OPDBR",   0},
	{ONCE_REG_IDX_OPILR,   DSP563XX_ONCE_OPILR,   24, "OPILR",   0},
	{ONCE_REG_IDX_PDB,     DSP563XX_ONCE_PDBGOTO, 24, "PDB",     0},
	{ONCE_REG_IDX_OTC,     DSP563XX_ONCE_OTC,     24, "OTC",     0},
	{ONCE_REG_IDX_OPABFR,  DSP563XX_ONCE_OPABFR,  24, "OPABFR",  0},
	{ONCE_REG_IDX_OPABDR,  DSP563XX_ONCE_OPABDR,  24, "OPABDR",  0},
	{ONCE_REG_IDX_OPABEX,  DSP563XX_ONCE_OPABEX,  24, "OPABEX",  0},
	{ONCE_REG_IDX_OPABF0,  DSP563XX_ONCE_OPABF11, 25, "OPABF0",  0},
	{ONCE_REG_IDX_OPABF1,  DSP563XX_ONCE_OPABF11, 25, "OPABF1",  0},
	{ONCE_REG_IDX_OPABF2,  DSP563XX_ONCE_OPABF11, 25, "OPABF2",  0},
	{ONCE_REG_IDX_OPABF3,  DSP563XX_ONCE_OPABF11, 25, "OPABF3",  0},
	{ONCE_REG_IDX_OPABF4,  DSP563XX_ONCE_OPABF11, 25, "OPABF4",  0},
	{ONCE_REG_IDX_OPABF5,  DSP563XX_ONCE_OPABF11, 25, "OPABF5",  0},
	{ONCE_REG_IDX_OPABF6,  DSP563XX_ONCE_OPABF11, 25, "OPABF6",  0},
	{ONCE_REG_IDX_OPABF7,  DSP563XX_ONCE_OPABF11, 25, "OPABF7",  0},
	{ONCE_REG_IDX_OPABF8,  DSP563XX_ONCE_OPABF11, 25, "OPABF8",  0},
	{ONCE_REG_IDX_OPABF9,  DSP563XX_ONCE_OPABF11, 25, "OPABF9",  0},
	{ONCE_REG_IDX_OPABF10, DSP563XX_ONCE_OPABF11, 25, "OPABF10", 0},
	{ONCE_REG_IDX_OPABF11, DSP563XX_ONCE_OPABF11, 25, "OPABF11", 0},
/*      {25,0x1f,24,"NRSEL",0}, */
};

enum dsp563xx_reg_idx {
	DSP563XX_REG_IDX_R0 = 0,
	DSP563XX_REG_IDX_R1 = 1,
	DSP563XX_REG_IDX_R2 = 2,
	DSP563XX_REG_IDX_R3 = 3,
	DSP563XX_REG_IDX_R4 = 4,
	DSP563XX_REG_IDX_R5 = 5,
	DSP563XX_REG_IDX_R6 = 6,
	DSP563XX_REG_IDX_R7 = 7,
	DSP563XX_REG_IDX_N0 = 8,
	DSP563XX_REG_IDX_N1 = 9,
	DSP563XX_REG_IDX_N2 = 10,
	DSP563XX_REG_IDX_N3 = 11,
	DSP563XX_REG_IDX_N4 = 12,
	DSP563XX_REG_IDX_N5 = 13,
	DSP563XX_REG_IDX_N6 = 14,
	DSP563XX_REG_IDX_N7 = 15,
	DSP563XX_REG_IDX_M0 = 16,
	DSP563XX_REG_IDX_M1 = 17,
	DSP563XX_REG_IDX_M2 = 18,
	DSP563XX_REG_IDX_M3 = 19,
	DSP563XX_REG_IDX_M4 = 20,
	DSP563XX_REG_IDX_M5 = 21,
	DSP563XX_REG_IDX_M6 = 22,
	DSP563XX_REG_IDX_M7 = 23,
	DSP563XX_REG_IDX_X0 = 24,
	DSP563XX_REG_IDX_X1 = 25,
	DSP563XX_REG_IDX_Y0 = 26,
	DSP563XX_REG_IDX_Y1 = 27,
	DSP563XX_REG_IDX_A0 = 28,
	DSP563XX_REG_IDX_A1 = 29,
	DSP563XX_REG_IDX_A2 = 30,
	DSP563XX_REG_IDX_B0 = 31,
	DSP563XX_REG_IDX_B1 = 32,
	DSP563XX_REG_IDX_B2 = 33,
	DSP563XX_REG_IDX_SSH = 34,
	DSP563XX_REG_IDX_SSL = 35,
	DSP563XX_REG_IDX_SP = 36,
	DSP563XX_REG_IDX_EP = 37,
	DSP563XX_REG_IDX_SZ = 38,
	DSP563XX_REG_IDX_SC = 39,
	DSP563XX_REG_IDX_PC = 40,
	DSP563XX_REG_IDX_SR = 41,
	DSP563XX_REG_IDX_OMR = 42,
	DSP563XX_REG_IDX_LA = 43,
	DSP563XX_REG_IDX_LC = 44,
	DSP563XX_REG_IDX_VBA = 45,
	DSP563XX_REG_IDX_IPRC = 46,
	DSP563XX_REG_IDX_IPRP = 47,
	DSP563XX_REG_IDX_BCR = 48,
	DSP563XX_REG_IDX_DCR = 49,
	DSP563XX_REG_IDX_AAR0 = 50,
	DSP563XX_REG_IDX_AAR1 = 51,
	DSP563XX_REG_IDX_AAR2 = 52,
	DSP563XX_REG_IDX_AAR3 = 53,
};

static const struct {
	unsigned id;
	const char *name;
	unsigned bits;
	/* effective addressing mode encoding */
	uint8_t eame;
	uint32_t instr_mask;
} dsp563xx_regs[] = {
	/* *INDENT-OFF* */
	/* address registers */
	{DSP563XX_REG_IDX_R0, "r0", 24, 0x10, ASM_REG_W_R0},
	{DSP563XX_REG_IDX_R1, "r1", 24, 0x11, ASM_REG_W_R1},
	{DSP563XX_REG_IDX_R2, "r2", 24, 0x12, ASM_REG_W_R2},
	{DSP563XX_REG_IDX_R3, "r3", 24, 0x13, ASM_REG_W_R3},
	{DSP563XX_REG_IDX_R4, "r4", 24, 0x14, ASM_REG_W_R4},
	{DSP563XX_REG_IDX_R5, "r5", 24, 0x15, ASM_REG_W_R5},
	{DSP563XX_REG_IDX_R6, "r6", 24, 0x16, ASM_REG_W_R6},
	{DSP563XX_REG_IDX_R7, "r7", 24, 0x17, ASM_REG_W_R7},
	/* offset registers */
	{DSP563XX_REG_IDX_N0, "n0", 24, 0x18, ASM_REG_W_N0},
	{DSP563XX_REG_IDX_N1, "n1", 24, 0x19, ASM_REG_W_N1},
	{DSP563XX_REG_IDX_N2, "n2", 24, 0x1a, ASM_REG_W_N2},
	{DSP563XX_REG_IDX_N3, "n3", 24, 0x1b, ASM_REG_W_N3},
	{DSP563XX_REG_IDX_N4, "n4", 24, 0x1c, ASM_REG_W_N4},
	{DSP563XX_REG_IDX_N5, "n5", 24, 0x1d, ASM_REG_W_N5},
	{DSP563XX_REG_IDX_N6, "n6", 24, 0x1e, ASM_REG_W_N6},
	{DSP563XX_REG_IDX_N7, "n7", 24, 0x1f, ASM_REG_W_N7},
	/* modifier registers */
	{DSP563XX_REG_IDX_M0, "m0", 24, 0x20, ASM_REG_W_M0},
	{DSP563XX_REG_IDX_M1, "m1", 24, 0x21, ASM_REG_W_M1},
	{DSP563XX_REG_IDX_M2, "m2", 24, 0x22, ASM_REG_W_M2},
	{DSP563XX_REG_IDX_M3, "m3", 24, 0x23, ASM_REG_W_M3},
	{DSP563XX_REG_IDX_M4, "m4", 24, 0x24, ASM_REG_W_M4},
	{DSP563XX_REG_IDX_M5, "m5", 24, 0x25, ASM_REG_W_M5},
	{DSP563XX_REG_IDX_M6, "m6", 24, 0x26, ASM_REG_W_M6},
	{DSP563XX_REG_IDX_M7, "m7", 24, 0x27, ASM_REG_W_M7},
	/* data alu input register */
	{DSP563XX_REG_IDX_X0, "x0", 24, 0x04, ASM_REG_W_X0},
	{DSP563XX_REG_IDX_X1, "x1", 24, 0x05, ASM_REG_W_X1},
	{DSP563XX_REG_IDX_Y0, "y0", 24, 0x06, ASM_REG_W_Y0},
	{DSP563XX_REG_IDX_Y1, "y1", 24, 0x07, ASM_REG_W_Y1},
	/* data alu accumulator register */
	{DSP563XX_REG_IDX_A0, "a0", 24, 0x08, ASM_REG_W_A0},
	{DSP563XX_REG_IDX_A1, "a1", 24, 0x0c, ASM_REG_W_A1},
	{DSP563XX_REG_IDX_A2, "a2", 8, 0x0a, ASM_REG_W_A2},
	{DSP563XX_REG_IDX_B0, "b0", 24, 0x09, ASM_REG_W_B0},
	{DSP563XX_REG_IDX_B1, "b1", 24, 0x0d, ASM_REG_W_B1},
	{DSP563XX_REG_IDX_B2, "b2", 8, 0x0b, ASM_REG_W_B2},
	/* stack */
	{DSP563XX_REG_IDX_SSH, "ssh", 24, 0x3c, ASM_REG_W_SSH},
	{DSP563XX_REG_IDX_SSL, "ssl", 24, 0x3d, ASM_REG_W_SSL},
	{DSP563XX_REG_IDX_SP, "sp", 24, 0x3b, ASM_REG_W_SP},
	{DSP563XX_REG_IDX_EP, "ep", 24, 0x2a, ASM_REG_W_EP},
	{DSP563XX_REG_IDX_SZ, "sz", 24, 0x38, ASM_REG_W_SZ},
	{DSP563XX_REG_IDX_SC, "sc", 24, 0x31, ASM_REG_W_SC},
	/* system */
	{DSP563XX_REG_IDX_PC, "pc", 24, 0x00, ASM_REG_W_PC},
	{DSP563XX_REG_IDX_SR, "sr", 24, 0x39, ASM_REG_W_SR},
	{DSP563XX_REG_IDX_OMR, "omr", 24, 0x3a, ASM_REG_W_OMR},
	{DSP563XX_REG_IDX_LA, "la", 24, 0x3e, ASM_REG_W_LA},
	{DSP563XX_REG_IDX_LC, "lc", 24, 0x3f, ASM_REG_W_LC},
	/* interrupt */
	{DSP563XX_REG_IDX_VBA, "vba", 24, 0x30, ASM_REG_W_VBA},
	{DSP563XX_REG_IDX_IPRC, "iprc", 24, 0x00, ASM_REG_W_IPRC},
	{DSP563XX_REG_IDX_IPRP, "iprp", 24, 0x00, ASM_REG_W_IPRP},
	/* port a */
	{DSP563XX_REG_IDX_BCR, "bcr", 24, 0x00, ASM_REG_W_BCR},
	{DSP563XX_REG_IDX_DCR, "dcr", 24, 0x00, ASM_REG_W_DCR},
	{DSP563XX_REG_IDX_AAR0, "aar0", 24, 0x00, ASM_REG_W_AAR0},
	{DSP563XX_REG_IDX_AAR1, "aar1", 24, 0x00, ASM_REG_W_AAR1},
	{DSP563XX_REG_IDX_AAR2, "aar2", 24, 0x00, ASM_REG_W_AAR2},
	{DSP563XX_REG_IDX_AAR3, "aar3", 24, 0x00, ASM_REG_W_AAR3},
	/* *INDENT-ON* */
};

enum memory_type {
	MEM_X = 0,
	MEM_Y = 1,
	MEM_P = 2,
	MEM_L = 3,
};

#define INSTR_JUMP      0x0AF080
/* Effective Addressing Mode Encoding */
#define EAME_R0         0x10
/* instrcution encoder */
/* movep
 * s - peripheral space X/Y (X=0,Y=1)
 * w - write/read
 * d - source/destination register
 * p - IO short address
 */
#define INSTR_MOVEP_REG_HIO(s, w, d, p) (0x084000 | \
	((s & 1) << 16) | ((w & 1) << 15) | ((d & 0x3f) << 8) | (p & 0x3f))

/* the gdb register list is send in this order */
uint8_t gdb_reg_list_idx[] = {
	DSP563XX_REG_IDX_X1, DSP563XX_REG_IDX_X0, DSP563XX_REG_IDX_Y1, DSP563XX_REG_IDX_Y0,
	DSP563XX_REG_IDX_A2, DSP563XX_REG_IDX_A1, DSP563XX_REG_IDX_A0, DSP563XX_REG_IDX_B2,
	DSP563XX_REG_IDX_B1, DSP563XX_REG_IDX_B0, DSP563XX_REG_IDX_PC, DSP563XX_REG_IDX_SR,
	DSP563XX_REG_IDX_OMR, DSP563XX_REG_IDX_LA, DSP563XX_REG_IDX_LC, DSP563XX_REG_IDX_SSH,
	DSP563XX_REG_IDX_SSL, DSP563XX_REG_IDX_SP, DSP563XX_REG_IDX_EP, DSP563XX_REG_IDX_SZ,
	DSP563XX_REG_IDX_SC, DSP563XX_REG_IDX_VBA, DSP563XX_REG_IDX_IPRC, DSP563XX_REG_IDX_IPRP,
	DSP563XX_REG_IDX_BCR, DSP563XX_REG_IDX_DCR, DSP563XX_REG_IDX_AAR0, DSP563XX_REG_IDX_AAR1,
	DSP563XX_REG_IDX_AAR2, DSP563XX_REG_IDX_AAR3, DSP563XX_REG_IDX_R0, DSP563XX_REG_IDX_R1,
	DSP563XX_REG_IDX_R2, DSP563XX_REG_IDX_R3, DSP563XX_REG_IDX_R4, DSP563XX_REG_IDX_R5,
	DSP563XX_REG_IDX_R6, DSP563XX_REG_IDX_R7, DSP563XX_REG_IDX_N0, DSP563XX_REG_IDX_N1,
	DSP563XX_REG_IDX_N2, DSP563XX_REG_IDX_N3, DSP563XX_REG_IDX_N4, DSP563XX_REG_IDX_N5,
	DSP563XX_REG_IDX_N6, DSP563XX_REG_IDX_N7, DSP563XX_REG_IDX_M0, DSP563XX_REG_IDX_M1,
	DSP563XX_REG_IDX_M2, DSP563XX_REG_IDX_M3, DSP563XX_REG_IDX_M4, DSP563XX_REG_IDX_M5,
	DSP563XX_REG_IDX_M6, DSP563XX_REG_IDX_M7,
};

static int dsp563xx_get_gdb_reg_list(struct target *target,
	struct reg **reg_list[],
	int *reg_list_size)
{
	int i;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if (target->state != TARGET_HALTED)
		return ERROR_TARGET_NOT_HALTED;

	*reg_list_size = DSP563XX_NUMCOREREGS;
	*reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));

	if (!*reg_list)
		return ERROR_COMMAND_SYNTAX_ERROR;

	for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
		(*reg_list)[i] = &dsp563xx->core_cache->reg_list[gdb_reg_list_idx[i]];

	return ERROR_OK;

}

static int dsp563xx_read_core_reg(struct target *target, int num)
{
	uint32_t reg_value;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
		return ERROR_COMMAND_SYNTAX_ERROR;

	reg_value = dsp563xx->core_regs[num];
	buf_set_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32, reg_value);
	dsp563xx->core_cache->reg_list[num].valid = 1;
	dsp563xx->core_cache->reg_list[num].dirty = 0;

	return ERROR_OK;
}

static int dsp563xx_write_core_reg(struct target *target, int num)
{
	uint32_t reg_value;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
		return ERROR_COMMAND_SYNTAX_ERROR;

	reg_value = buf_get_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32);
	dsp563xx->core_regs[num] = reg_value;
	dsp563xx->core_cache->reg_list[num].valid = 1;
	dsp563xx->core_cache->reg_list[num].dirty = 0;

	return ERROR_OK;
}

static int dsp563xx_get_core_reg(struct reg *reg)
{
	struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
	struct target *target = dsp563xx_reg->target;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	LOG_DEBUG("%s", __func__);

	if (target->state != TARGET_HALTED)
		return ERROR_TARGET_NOT_HALTED;

	return dsp563xx->read_core_reg(target, dsp563xx_reg->num);
}

static int dsp563xx_set_core_reg(struct reg *reg, uint8_t *buf)
{
	LOG_DEBUG("%s", __func__);

	struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
	struct target *target = dsp563xx_reg->target;
	uint32_t value = buf_get_u32(buf, 0, 32);

	if (target->state != TARGET_HALTED)
		return ERROR_TARGET_NOT_HALTED;

	buf_set_u32(reg->value, 0, reg->size, value);
	reg->dirty = 1;
	reg->valid = 1;

	return ERROR_OK;
}

static const struct reg_arch_type dsp563xx_reg_type = {
	.get = dsp563xx_get_core_reg,
	.set = dsp563xx_set_core_reg,
};

static void dsp563xx_build_reg_cache(struct target *target)
{
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
	struct reg_cache *cache = malloc(sizeof(struct reg_cache));
	struct reg *reg_list = malloc(sizeof(struct reg) * DSP563XX_NUMCOREREGS);
	struct dsp563xx_core_reg *arch_info = malloc(
			sizeof(struct dsp563xx_core_reg) * DSP563XX_NUMCOREREGS);
	int i;

	/* Build the process context cache */
	cache->name = "dsp563xx registers";
	cache->next = NULL;
	cache->reg_list = reg_list;
	cache->num_regs = DSP563XX_NUMCOREREGS;
	(*cache_p) = cache;
	dsp563xx->core_cache = cache;

	for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
		arch_info[i].num = dsp563xx_regs[i].id;
		arch_info[i].name = dsp563xx_regs[i].name;
		arch_info[i].size = dsp563xx_regs[i].bits;
		arch_info[i].eame = dsp563xx_regs[i].eame;
		arch_info[i].instr_mask = dsp563xx_regs[i].instr_mask;
		arch_info[i].target = target;
		arch_info[i].dsp563xx_common = dsp563xx;
		reg_list[i].name = dsp563xx_regs[i].name;
		reg_list[i].size = 32;	/* dsp563xx_regs[i].bits; */
		reg_list[i].value = calloc(1, 4);
		reg_list[i].dirty = 0;
		reg_list[i].valid = 0;
		reg_list[i].type = &dsp563xx_reg_type;
		reg_list[i].arch_info = &arch_info[i];
	}
}

static int dsp563xx_read_register(struct target *target, int num, int force);
static int dsp563xx_write_register(struct target *target, int num, int force);

static int dsp563xx_reg_read_high_io(struct target *target, uint32_t instr_mask, uint32_t *data)
{
	int err;
	uint32_t instr;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	/* we use r0 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);

	/* move source memory to r0 */
	instr = INSTR_MOVEP_REG_HIO(MEM_X, 0, EAME_R0, instr_mask);
	err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr);
	if (err != ERROR_OK)
		return err;
	/* move r0 to debug register */
	instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, 0xfffffc);
	err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr);
	if (err != ERROR_OK)
		return err;
	/* read debug register */
	err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data);
	if (err != ERROR_OK)
		return err;
	/* r0 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;

	return ERROR_OK;
}

static int dsp563xx_reg_write_high_io(struct target *target, uint32_t instr_mask, uint32_t data)
{
	int err;
	uint32_t instr;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	/* we use r0 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);

	/* move data to r0 */
	err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x60F400, data);
	if (err != ERROR_OK)
		return err;
	/* move r0 to destination memory */
	instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, instr_mask);
	err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr);
	if (err != ERROR_OK)
		return err;

	/* r0 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;

	return ERROR_OK;
}

static int dsp563xx_reg_read(struct target *target, uint32_t eame, uint32_t *data)
{
	int err;
	uint32_t instr;

	instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, eame, 0xfffffc);
	err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr);
	if (err != ERROR_OK)
		return err;
	/* nop */
	err = dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000);
	if (err != ERROR_OK)
		return err;
	/* read debug register */
	return dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data);
}

static int dsp563xx_reg_write(struct target *target, uint32_t instr_mask, uint32_t data)
{
	int err;

	err = dsp563xx_once_execute_dw_ir(target->tap, 0, instr_mask, data);
	if (err != ERROR_OK)
		return err;
	/* nop */
	return dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000);
}

static int dsp563xx_reg_pc_read(struct target *target)
{
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	/* pc was changed, nothing todo */
	if (dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty)
		return ERROR_OK;

	/* conditional branch check */
	if (once_regs[ONCE_REG_IDX_OPABDR].reg == once_regs[ONCE_REG_IDX_OPABEX].reg) {
		if ((once_regs[ONCE_REG_IDX_OPABF11].reg & 1) == 0) {
			LOG_DEBUG("%s conditional branch not supported yet (0x%x 0x%x 0x%x)",
				__func__,
				(once_regs[ONCE_REG_IDX_OPABF11].reg >> 1),
				once_regs[ONCE_REG_IDX_OPABDR].reg,
				once_regs[ONCE_REG_IDX_OPABEX].reg);

			/* TODO: use disassembly to set correct pc offset
			 * read 2 words from OPABF11 and disasm the instruction
			 */
			dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
				(once_regs[ONCE_REG_IDX_OPABF11].reg >> 1) & 0x00FFFFFF;
		} else {
			if (once_regs[ONCE_REG_IDX_OPABEX].reg ==
				once_regs[ONCE_REG_IDX_OPABFR].reg)
				dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
					once_regs[ONCE_REG_IDX_OPABEX].reg;
			else
				dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
					once_regs[ONCE_REG_IDX_OPABEX].reg - 1;
		}
	} else
		dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = once_regs[ONCE_REG_IDX_OPABEX].reg;

	dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_PC);

	return ERROR_OK;
}

static int dsp563xx_reg_ssh_read(struct target *target)
{
	int err;
	uint32_t sp;
	struct dsp563xx_core_reg *arch_info;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;

	/* get a valid stack pointer */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
	if (err != ERROR_OK)
		return err;
	sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
	err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 0);
	if (err != ERROR_OK)
		return err;

	/* get a valid stack count */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SC, 0);
	if (err != ERROR_OK)
		return err;

	err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 0);
	if (err != ERROR_OK)
		return err;

	/* get a valid extended pointer */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_EP, 0);
	if (err != ERROR_OK)
		return err;

	err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 0);
	if (err != ERROR_OK)
		return err;

	if (!sp)
		sp = 0x00FFFFFF;
	else {
		err = dsp563xx_reg_read(target, arch_info->eame, &sp);
		if (err != ERROR_OK)
			return err;

		err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 1);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 1);
		if (err != ERROR_OK)
			return err;
	}

	dsp563xx->core_regs[DSP563XX_REG_IDX_SSH] = sp;
	dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSH);

	return ERROR_OK;
}

static int dsp563xx_reg_ssh_write(struct target *target)
{
	int err;
	uint32_t sp;
	struct dsp563xx_core_reg *arch_info;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;

	/* get a valid stack pointer */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
	if (err != ERROR_OK)
		return err;
	sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];

	if (sp) {
		sp--;
		/* write new stackpointer */
		dsp563xx->core_regs[DSP563XX_REG_IDX_SP] = sp;
		err = dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SP);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1);
		if (err != ERROR_OK)
			return err;

		err = dsp563xx_reg_write(target, arch_info->instr_mask,
				dsp563xx->core_regs[DSP563XX_REG_IDX_SSH]);
		if (err != ERROR_OK)
			return err;

		err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 1);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SSH, 1);
		if (err != ERROR_OK)
			return err;
	}

	return ERROR_OK;
}

static int dsp563xx_reg_ssl_read(struct target *target)
{
	int err;
	uint32_t sp;
	struct dsp563xx_core_reg *arch_info;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].arch_info;

	/* get a valid stack pointer */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
	if (err != ERROR_OK)
		return err;
	sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];

	if (!sp)
		sp = 0x00FFFFFF;
	else {
		err = dsp563xx_reg_read(target, arch_info->eame, &sp);
		if (err != ERROR_OK)
			return err;
	}

	dsp563xx->core_regs[DSP563XX_REG_IDX_SSL] = sp;
	dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSL);

	return ERROR_OK;
}

static int dsp563xx_read_register(struct target *target, int num, int force)
{
	int err = ERROR_OK;
	uint32_t data = 0;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	struct dsp563xx_core_reg *arch_info;

	if (force)
		dsp563xx->core_cache->reg_list[num].valid = 0;

	if (!dsp563xx->core_cache->reg_list[num].valid) {
		arch_info = dsp563xx->core_cache->reg_list[num].arch_info;

		switch (arch_info->num) {
			case DSP563XX_REG_IDX_SSH:
				err = dsp563xx_reg_ssh_read(target);
				break;
			case DSP563XX_REG_IDX_SSL:
				err = dsp563xx_reg_ssl_read(target);
				break;
			case DSP563XX_REG_IDX_PC:
				err = dsp563xx_reg_pc_read(target);
				break;
			case DSP563XX_REG_IDX_IPRC:
			case DSP563XX_REG_IDX_IPRP:
			case DSP563XX_REG_IDX_BCR:
			case DSP563XX_REG_IDX_DCR:
			case DSP563XX_REG_IDX_AAR0:
			case DSP563XX_REG_IDX_AAR1:
			case DSP563XX_REG_IDX_AAR2:
			case DSP563XX_REG_IDX_AAR3:
				err = dsp563xx_reg_read_high_io(target,
						arch_info->instr_mask, &data);
				if (err == ERROR_OK) {
					dsp563xx->core_regs[num] = data;
					dsp563xx->read_core_reg(target, num);
				}
				break;
			default:
				err = dsp563xx_reg_read(target, arch_info->eame, &data);
				if (err == ERROR_OK) {
					dsp563xx->core_regs[num] = data;
					dsp563xx->read_core_reg(target, num);
				}
				break;
		}
	}

	return err;
}

static int dsp563xx_write_register(struct target *target, int num, int force)
{
	int err = ERROR_OK;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	struct dsp563xx_core_reg *arch_info;

	if (force)
		dsp563xx->core_cache->reg_list[num].dirty = 1;

	if (dsp563xx->core_cache->reg_list[num].dirty) {
		arch_info = dsp563xx->core_cache->reg_list[num].arch_info;

		dsp563xx->write_core_reg(target, num);

		switch (arch_info->num) {
			case DSP563XX_REG_IDX_SSH:
				err = dsp563xx_reg_ssh_write(target);
				break;
			case DSP563XX_REG_IDX_PC:
				/* pc is updated on resume, no need to write it here */
				break;
			case DSP563XX_REG_IDX_IPRC:
			case DSP563XX_REG_IDX_IPRP:
			case DSP563XX_REG_IDX_BCR:
			case DSP563XX_REG_IDX_DCR:
			case DSP563XX_REG_IDX_AAR0:
			case DSP563XX_REG_IDX_AAR1:
			case DSP563XX_REG_IDX_AAR2:
			case DSP563XX_REG_IDX_AAR3:
				err = dsp563xx_reg_write_high_io(target,
					arch_info->instr_mask,
					dsp563xx->core_regs[num]);
				break;
			default:
				err = dsp563xx_reg_write(target,
					arch_info->instr_mask,
					dsp563xx->core_regs[num]);

				if ((err == ERROR_OK) && (arch_info->num == DSP563XX_REG_IDX_SP)) {
					dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].valid =
						0;
					dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].valid =
						0;
				}

				break;
		}
	}

	return err;
}

static int dsp563xx_save_context(struct target *target)
{
	int i, err = ERROR_OK;

	for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
		err = dsp563xx_read_register(target, i, 0);
		if (err != ERROR_OK)
			break;
	}

	return err;
}

static int dsp563xx_restore_context(struct target *target)
{
	int i, err = ERROR_OK;

	for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
		err = dsp563xx_write_register(target, i, 0);
		if (err != ERROR_OK)
			break;
	}

	return err;
}

static void dsp563xx_invalidate_x_context(struct target *target,
	uint32_t addr_start,
	uint32_t addr_end)
{
	int i;
	struct dsp563xx_core_reg *arch_info;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if (addr_start > ASM_REG_W_IPRC)
		return;
	if (addr_start < ASM_REG_W_AAR3)
		return;

	for (i = DSP563XX_REG_IDX_IPRC; i < DSP563XX_NUMCOREREGS; i++) {
		arch_info = dsp563xx->core_cache->reg_list[i].arch_info;

		if ((arch_info->instr_mask >= addr_start) &&
			(arch_info->instr_mask <= addr_end)) {
			dsp563xx->core_cache->reg_list[i].valid = 0;
			dsp563xx->core_cache->reg_list[i].dirty = 0;
		}
	}
}

static int dsp563xx_target_create(struct target *target, Jim_Interp *interp)
{
	struct dsp563xx_common *dsp563xx = calloc(1, sizeof(struct dsp563xx_common));

	if (!dsp563xx)
		return ERROR_COMMAND_SYNTAX_ERROR;

	dsp563xx->jtag_info.tap = target->tap;
	target->arch_info = dsp563xx;
	dsp563xx->read_core_reg = dsp563xx_read_core_reg;
	dsp563xx->write_core_reg = dsp563xx_write_core_reg;

	return ERROR_OK;
}

static int dsp563xx_init_target(struct command_context *cmd_ctx, struct target *target)
{
	LOG_DEBUG("%s", __func__);

	dsp563xx_build_reg_cache(target);

	return ERROR_OK;
}

static int dsp563xx_examine(struct target *target)
{
	uint32_t chip;

	if (target->tap->hasidcode == false) {
		LOG_ERROR("no IDCODE present on device");
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	if (!target_was_examined(target)) {
		target_set_examined(target);

		/* examine core and chip derivate number */
		chip = (target->tap->idcode>>12) & 0x3ff;
		/* core number 0 means DSP563XX */
		if (((chip>>5)&0x1f) == 0)
			chip += 300;

		LOG_INFO("DSP56%03d device found", chip);
	}

	return ERROR_OK;
}

static int dsp563xx_arch_state(struct target *target)
{
	LOG_DEBUG("%s", __func__);
	return ERROR_OK;
}

#define DSP563XX_SR_SA (1<<17)
#define DSP563XX_SR_SC (1<<13)

static int dsp563xx_debug_once_init(struct target *target)
{
	return dsp563xx_once_read_register(target->tap, 1, once_regs, DSP563XX_NUMONCEREGS);
}

static int dsp563xx_debug_init(struct target *target)
{
	int err;
	uint32_t sr;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	struct dsp563xx_core_reg *arch_info;

	err = dsp563xx_debug_once_init(target);
	if (err != ERROR_OK)
		return err;

	arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].arch_info;

	/* check 24bit mode */
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SR, 0);
	if (err != ERROR_OK)
		return err;

	sr = dsp563xx->core_regs[DSP563XX_REG_IDX_SR];

	if (sr & (DSP563XX_SR_SA | DSP563XX_SR_SC)) {
		sr &= ~(DSP563XX_SR_SA | DSP563XX_SR_SC);

		err = dsp563xx_once_execute_dw_ir(target->tap, 1, arch_info->instr_mask, sr);
		if (err != ERROR_OK)
			return err;
		dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].dirty = 1;
	}

	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N0, 0);
	if (err != ERROR_OK)
		return err;
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N1, 0);
	if (err != ERROR_OK)
		return err;
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M0, 0);
	if (err != ERROR_OK)
		return err;
	err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M1, 0);
	if (err != ERROR_OK)
		return err;

	if (dsp563xx->core_regs[DSP563XX_REG_IDX_N0] != 0x000000) {
		arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].arch_info;
		err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000);
		if (err != ERROR_OK)
			return err;
	}
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].dirty = 1;

	if (dsp563xx->core_regs[DSP563XX_REG_IDX_N1] != 0x000000) {
		arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].arch_info;
		err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000);
		if (err != ERROR_OK)
			return err;
	}
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].dirty = 1;

	if (dsp563xx->core_regs[DSP563XX_REG_IDX_M0] != 0xffffff) {
		arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].arch_info;
		err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff);
		if (err != ERROR_OK)
			return err;
	}
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].dirty = 1;

	if (dsp563xx->core_regs[DSP563XX_REG_IDX_M1] != 0xffffff) {
		arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].arch_info;
		err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff);
		if (err != ERROR_OK)
			return err;
	}
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].dirty = 1;

	err = dsp563xx_save_context(target);
	if (err != ERROR_OK)
		return err;

	return ERROR_OK;
}

static int dsp563xx_jtag_debug_request(struct target *target)
{
	return dsp563xx_once_request_debug(target->tap, target->state == TARGET_RESET);
}

static int dsp563xx_poll(struct target *target)
{
	int err;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	uint32_t once_status = 0;
	int state;

	state = dsp563xx_once_target_status(target->tap);

	if (state == TARGET_UNKNOWN) {
		target->state = state;
		LOG_ERROR("jtag status contains invalid mode value - communication failure");
		return ERROR_TARGET_FAILURE;
	}

	err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status);
	if (err != ERROR_OK)
		return err;

	if ((once_status & DSP563XX_ONCE_OSCR_DEBUG_M) == DSP563XX_ONCE_OSCR_DEBUG_M) {
		if (target->state != TARGET_HALTED) {
			target->state = TARGET_HALTED;

			err = dsp563xx_debug_init(target);
			if (err != ERROR_OK)
				return err;

			if (once_status & (DSP563XX_ONCE_OSCR_MBO|DSP563XX_ONCE_OSCR_SWO))
				target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
			else
				target_call_event_callbacks(target, TARGET_EVENT_HALTED);

			LOG_DEBUG("target->state: %s (%x)", target_state_name(target), once_status);
			LOG_INFO("halted: PC: 0x%x", dsp563xx->core_regs[DSP563XX_REG_IDX_PC]);
		}
	}

	return ERROR_OK;
}

static int dsp563xx_halt(struct target *target)
{
	int err;

	LOG_DEBUG("%s", __func__);

	if (target->state == TARGET_HALTED) {
		LOG_DEBUG("target was already halted");
		return ERROR_OK;
	}

	if (target->state == TARGET_UNKNOWN)
		LOG_WARNING("target was in unknown state when halt was requested");

	err = dsp563xx_jtag_debug_request(target);
	if (err != ERROR_OK)
		return err;

	target->debug_reason = DBG_REASON_DBGRQ;

	return ERROR_OK;
}

static int dsp563xx_resume(struct target *target,
	int current,
	uint32_t address,
	int handle_breakpoints,
	int debug_execution)
{
	int err;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	/* check if pc was changed and resume want to execute the next address
	 * if pc was changed from gdb or other interface we will
	 * jump to this address and don't execute the next address
	 * this will not affect the resume command with an address argument
	 * because current is set to zero then
	 */
	if (current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty) {
		dsp563xx_write_core_reg(target, DSP563XX_REG_IDX_PC);
		address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
		current = 0;
	}

	LOG_DEBUG("%s %08X %08X", __func__, current, (unsigned) address);

	err = dsp563xx_restore_context(target);
	if (err != ERROR_OK)
		return err;
	register_cache_invalidate(dsp563xx->core_cache);

	if (current) {
		/* restore pipeline registers and go */
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR,
				once_regs[ONCE_REG_IDX_OPILR].reg);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR |
				DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
				once_regs[ONCE_REG_IDX_OPDBR].reg);
		if (err != ERROR_OK)
			return err;
	} else {
		/* set to go register and jump */
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR,	INSTR_JUMP);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO |
				DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO, address);
		if (err != ERROR_OK)
			return err;
	}

	target->state = TARGET_RUNNING;

	target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);

	return ERROR_OK;
}

static int dsp563xx_step_ex(struct target *target,
	int current,
	uint32_t address,
	int handle_breakpoints,
	int steps)
{
	int err;
	uint32_t once_status;
	uint32_t dr_in, cnt;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if (target->state != TARGET_HALTED) {
		LOG_DEBUG("target was not halted");
		return ERROR_OK;
	}

	/* check if pc was changed and step want to execute the next address
	 * if pc was changed from gdb or other interface we will
	 * jump to this address and don't execute the next address
	 * this will not affect the step command with an address argument
	 * because current is set to zero then
	 */
	if (current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty) {
		dsp563xx_write_core_reg(target, DSP563XX_REG_IDX_PC);
		address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
		current = 0;
	}

	LOG_DEBUG("%s %08X %08X", __func__, current, (unsigned) address);

	err = dsp563xx_jtag_debug_request(target);
	if (err != ERROR_OK)
		return err;
	err = dsp563xx_restore_context(target);
	if (err != ERROR_OK)
		return err;

	/* reset trace mode */
	err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000);
	if (err != ERROR_OK)
		return err;
	/* enable trace mode */
	err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, DSP563XX_ONCE_OSCR_TME);
	if (err != ERROR_OK)
		return err;

	cnt = steps;

	/* on JUMP we need one extra cycle */
	if (!current)
		cnt++;

	/* load step counter with N-1 */
	err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OTC, cnt);
	if (err != ERROR_OK)
		return err;

	if (current) {
		/* restore pipeline registers and go */
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR,
				once_regs[ONCE_REG_IDX_OPILR].reg);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR |
				DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
				once_regs[ONCE_REG_IDX_OPDBR].reg);
		if (err != ERROR_OK)
			return err;
	} else {
		/* set to go register and jump */
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, INSTR_JUMP);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO |
				DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
				address);
		if (err != ERROR_OK)
			return err;
	}

	while (1) {
		err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status);
		if (err != ERROR_OK)
			return err;

		if (once_status & DSP563XX_ONCE_OSCR_TO) {
			err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABFR, &dr_in);
			if (err != ERROR_OK)
				return err;
			LOG_DEBUG("fetch: %08X", (unsigned) dr_in&0x00ffffff);
			err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABDR, &dr_in);
			if (err != ERROR_OK)
				return err;
			LOG_DEBUG("decode: %08X", (unsigned) dr_in&0x00ffffff);
			err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABEX, &dr_in);
			if (err != ERROR_OK)
				return err;
			LOG_DEBUG("execute: %08X", (unsigned) dr_in&0x00ffffff);

			/* reset trace mode */
			err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000);
			if (err != ERROR_OK)
				return err;

			register_cache_invalidate(dsp563xx->core_cache);
			err = dsp563xx_debug_init(target);
			if (err != ERROR_OK)
				return err;

			break;
		}
	}

	return ERROR_OK;
}

static int dsp563xx_step(struct target *target,
	int current,
	uint32_t address,
	int handle_breakpoints)
{
	int err;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	err = dsp563xx_step_ex(target, current, address, handle_breakpoints, 0);
	if (err != ERROR_OK)
		return err;

	target->debug_reason = DBG_REASON_SINGLESTEP;
	target_call_event_callbacks(target, TARGET_EVENT_HALTED);

	LOG_INFO("halted: PC: 0x%x", dsp563xx->core_regs[DSP563XX_REG_IDX_PC]);

	return err;
}

static int dsp563xx_assert_reset(struct target *target)
{
	int retval = 0;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	enum reset_types jtag_reset_config = jtag_get_reset_config();

	if (jtag_reset_config & RESET_HAS_SRST) {
		/* default to asserting srst */
		if (jtag_reset_config & RESET_SRST_PULLS_TRST)
			jtag_add_reset(1, 1);
		else
			jtag_add_reset(0, 1);
	}

	target->state = TARGET_RESET;
	jtag_add_sleep(5000);

	/* registers are now invalid */
	register_cache_invalidate(dsp563xx->core_cache);

	if (target->reset_halt) {
		retval = target_halt(target);
		if (retval != ERROR_OK)
			return retval;
	}

	LOG_DEBUG("%s", __func__);
	return ERROR_OK;
}

static int dsp563xx_deassert_reset(struct target *target)
{
	int err;

	/* deassert reset lines */
	jtag_add_reset(0, 0);

	err = dsp563xx_poll(target);
	if (err != ERROR_OK)
		return err;

	if (target->reset_halt) {
		if (target->state == TARGET_HALTED) {
			/* after a reset the cpu jmp to the
			 * reset vector and need 2 cycles to fill
			 * the cache (fetch,decode,excecute)
			 */
			err = dsp563xx_step_ex(target, 1, 0, 1, 1);
			if (err != ERROR_OK)
				return err;
		}
	} else
		target->state = TARGET_RUNNING;

	LOG_DEBUG("%s", __func__);
	return ERROR_OK;
}

static int dsp563xx_soft_reset_halt(struct target *target)
{
	LOG_DEBUG("%s", __func__);
	return ERROR_OK;
}

static int dsp563xx_run_algorithm(struct target *target,
	int num_mem_params, struct mem_param *mem_params,
	int num_reg_params, struct reg_param *reg_params,
	uint32_t entry_point, uint32_t exit_point,
	int timeout_ms, void *arch_info)
{
	int i;
	int retval = ERROR_OK;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);

	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	for (i = 0; i < num_mem_params; i++) {
		retval = target_write_buffer(target, mem_params[i].address,
				mem_params[i].size, mem_params[i].value);
		if (retval != ERROR_OK)
			return retval;
	}

	for (i = 0; i < num_reg_params; i++) {
		struct reg *reg = register_get_by_name(dsp563xx->core_cache,
				reg_params[i].reg_name,
				0);

		if (!reg) {
			LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
			continue;
		}

		if (reg->size != reg_params[i].size) {
			LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
				reg_params[i].reg_name);
			continue;
		}

		retval = dsp563xx_set_core_reg(reg, reg_params[i].value);
		if (retval != ERROR_OK)
			return retval;
	}

	/* exec */
	retval = target_resume(target, 0, entry_point, 1, 1);
	if (retval != ERROR_OK)
		return retval;

	retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
	if (retval != ERROR_OK)
		return retval;

	for (i = 0; i < num_mem_params; i++) {
		if (mem_params[i].direction != PARAM_OUT)
			retval = target_read_buffer(target,
					mem_params[i].address,
					mem_params[i].size,
					mem_params[i].value);
		if (retval != ERROR_OK)
			return retval;
	}

	for (i = 0; i < num_reg_params; i++) {
		if (reg_params[i].direction != PARAM_OUT) {

			struct reg *reg = register_get_by_name(dsp563xx->core_cache,
					reg_params[i].reg_name,
					0);
			if (!reg) {
				LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
				continue;
			}

			if (reg->size != reg_params[i].size) {
				LOG_ERROR(
					"BUG: register '%s' size doesn't match reg_params[i].size",
					reg_params[i].reg_name);
				continue;
			}

			buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
		}
	}

	return ERROR_OK;
}

/* global command context from openocd.c */
extern struct command_context *global_cmd_ctx;

static int dsp563xx_get_default_memory(void)
{
	Jim_Interp *interp;
	Jim_Obj *memspace;
	char *c;

	if (!global_cmd_ctx)
		return MEM_P;

	interp = global_cmd_ctx->interp;

	if (!interp)
		return MEM_P;

	memspace = Jim_GetGlobalVariableStr(interp, "memspace", JIM_NONE);

	if (!memspace)
		return MEM_P;

	c = (char *)Jim_GetString(memspace, NULL);

	if (!c)
		return MEM_P;

	switch (c[0]) {
		case '1':
			return MEM_X;
		case '2':
			return MEM_Y;
		case '3':
			return MEM_L;
		default:
			break;
	}

	return MEM_P;
}

static int dsp563xx_read_memory_core(struct target *target,
	int mem_type,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	uint8_t *buffer)
{
	int err;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	uint32_t i, x;
	uint32_t data, move_cmd = 0;
	uint8_t *b;

	LOG_DEBUG(
		"memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
		mem_type,
		address,
		size,
		count);

	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	switch (mem_type) {
		case MEM_X:
			/* TODO: mark effected queued registers */
			move_cmd = 0x61d800;
			break;
		case MEM_Y:
			move_cmd = 0x69d800;
			break;
		case MEM_P:
			move_cmd = 0x07d891;
			break;
		default:
			return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* we use r0 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
	/* we use r1 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);

	/* r0 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
	/* r1 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;

	x = count;
	b = buffer;

	err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address);
	if (err != ERROR_OK)
		return err;

	for (i = 0; i < x; i++) {
		err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_execute_sw_ir(target->tap, 0, 0x08D13C);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_reg_read(target->tap, 0,
				DSP563XX_ONCE_OGDBR, (uint32_t *)(void *)b);
		if (err != ERROR_OK)
			return err;
		b += 4;
	}

	/* flush the jtag queue */
	err = jtag_execute_queue();
	if (err != ERROR_OK)
		return err;

	/* walk over the buffer and fix target endianness */
	b = buffer;

	for (i = 0; i < x; i++) {
		data = buf_get_u32(b, 0, 32) & 0x00FFFFFF;
/*		LOG_DEBUG("R: %08X", *((uint32_t*)b)); */
		target_buffer_set_u32(target, b, data);
		b += 4;
	}

	return ERROR_OK;
}

static int dsp563xx_read_memory(struct target *target,
	int mem_type,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	uint8_t *buffer)
{
	int err;
	uint32_t i, i1;
	uint8_t *buffer_y, *buffer_x;

	/* if size equals zero we are called from target read memory
	 * and have to handle the parameter here */
	if ((size == 0) && (count != 0)) {
		size = count % 4;

		if (size)
			LOG_DEBUG("size is not aligned to 4 byte");

		count = (count - size) / 4;
		size = 4;
	}

	/* we only support 4 byte aligned data */
	if ((size != 4) || (!count))
		return ERROR_COMMAND_SYNTAX_ERROR;

	if (mem_type != MEM_L)
		return dsp563xx_read_memory_core(target, mem_type, address, size, count, buffer);

	buffer_y = malloc(size * count);
	if (!buffer_y)
		return ERROR_COMMAND_SYNTAX_ERROR;

	buffer_x = malloc(size * count);
	if (!buffer_x) {
		free(buffer_y);
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	err = dsp563xx_read_memory_core(target, MEM_Y, address, size, count / 2, buffer_y);

	if (err != ERROR_OK) {
		free(buffer_y);
		free(buffer_x);
		return err;
	}

	err = dsp563xx_read_memory_core(target, MEM_X, address, size, count / 2, buffer_x);

	if (err != ERROR_OK) {
		free(buffer_y);
		free(buffer_x);
		return err;
	}

	for (i = 0, i1 = 0; i < count; i += 2, i1++) {
		buf_set_u32(buffer + i*sizeof(uint32_t), 0, 32,
			buf_get_u32(buffer_y + i1 * sizeof(uint32_t), 0, 32));
			buf_set_u32(buffer + (i + 1) * sizeof(uint32_t), 0, 32,
			buf_get_u32(buffer_x + i1 * sizeof(uint32_t), 0, 32));
	}

	free(buffer_y);
	free(buffer_x);

	return ERROR_OK;
}

static int dsp563xx_read_memory_default(struct target *target,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	uint8_t *buffer)
{

	return dsp563xx_read_memory(target,
			dsp563xx_get_default_memory(), address, size, count, buffer);
}

static int dsp563xx_read_buffer_default(struct target *target,
	uint32_t address,
	uint32_t size,
	uint8_t *buffer)
{

	return dsp563xx_read_memory(target, dsp563xx_get_default_memory(), address, size, 0,
			buffer);
}

static int dsp563xx_write_memory_core(struct target *target,
	int mem_type,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	const uint8_t *buffer)
{
	int err;
	struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
	uint32_t i, x;
	uint32_t data, move_cmd = 0;
	const uint8_t *b;

	LOG_DEBUG(
		"memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
		mem_type,
		address,
		size,
		count);

	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	switch (mem_type) {
		case MEM_X:
			/* invalidate affected x registers */
			dsp563xx_invalidate_x_context(target, address, address + count - 1);
			move_cmd = 0x615800;
			break;
		case MEM_Y:
			move_cmd = 0x695800;
			break;
		case MEM_P:
			move_cmd = 0x075891;
			break;
		default:
			return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* we use r0 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
	/* we use r1 to store temporary data */
	if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
		dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);

	/* r0 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
	/* r1 is no longer valid on target */
	dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;

	x = count;
	b = buffer;

	err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address);
	if (err != ERROR_OK)
		return err;

	for (i = 0; i < x; i++) {
		data = target_buffer_get_u32(target, b);

/*		LOG_DEBUG("W: %08X", data); */

		data &= 0x00ffffff;

		err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x61F400, data);
		if (err != ERROR_OK)
			return err;
		err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd);
		if (err != ERROR_OK)
			return err;
		b += 4;
	}

	/* flush the jtag queue */
	err = jtag_execute_queue();
	if (err != ERROR_OK)
		return err;

	return ERROR_OK;
}

static int dsp563xx_write_memory(struct target *target,
	int mem_type,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	const uint8_t *buffer)
{
	int err;
	uint32_t i, i1;
	uint8_t *buffer_y, *buffer_x;

	/* if size equals zero we are called from target write memory
	 * and have to handle the parameter here */
	if ((size == 0) && (count != 0)) {
		size = count % 4;

		if (size)
			LOG_DEBUG("size is not aligned to 4 byte");

		count = (count - size) / 4;
		size = 4;
	}

	/* we only support 4 byte aligned data */
	if ((size != 4) || (!count))
		return ERROR_COMMAND_SYNTAX_ERROR;

	if (mem_type != MEM_L)
		return dsp563xx_write_memory_core(target, mem_type, address, size, count, buffer);

	buffer_y = malloc(size * count);
	if (!buffer_y)
		return ERROR_COMMAND_SYNTAX_ERROR;

	buffer_x = malloc(size * count);
	if (!buffer_x) {
		free(buffer_y);
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	for (i = 0, i1 = 0; i < count; i += 2, i1++) {
		buf_set_u32(buffer_y + i1 * sizeof(uint32_t), 0, 32,
		buf_get_u32(buffer + i * sizeof(uint32_t), 0, 32));
		buf_set_u32(buffer_x + i1 * sizeof(uint32_t), 0, 32,
		buf_get_u32(buffer + (i + 1) * sizeof(uint32_t), 0, 32));
	}

	err = dsp563xx_write_memory_core(target, MEM_Y, address, size, count / 2, buffer_y);

	if (err != ERROR_OK) {
		free(buffer_y);
		free(buffer_x);
		return err;
	}

	err = dsp563xx_write_memory_core(target, MEM_X, address, size, count / 2, buffer_x);

	if (err != ERROR_OK) {
		free(buffer_y);
		free(buffer_x);
		return err;
	}

	free(buffer_y);
	free(buffer_x);

	return ERROR_OK;
}

static int dsp563xx_write_memory_default(struct target *target,
	uint32_t address,
	uint32_t size,
	uint32_t count,
	const uint8_t *buffer)
{
	return dsp563xx_write_memory(target,
			dsp563xx_get_default_memory(), address, size, count, buffer);
}

static int dsp563xx_write_buffer_default(struct target *target,
	uint32_t address,
	uint32_t size,
	const uint8_t *buffer)
{
	return dsp563xx_write_memory(target, dsp563xx_get_default_memory(), address, size, 0,
			buffer);
}

static int dsp563xx_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
{
	return ERROR_OK;
}

static int dsp563xx_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
{
	return ERROR_OK;
}

static int dsp563xx_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
{
	return ERROR_OK;
}

static int dsp563xx_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
{
	return ERROR_OK;
}

static void handle_md_output(struct command_context *cmd_ctx,
	struct target *target,
	uint32_t address,
	unsigned size,
	unsigned count,
	const uint8_t *buffer)
{
	const unsigned line_bytecnt = 32;
	unsigned line_modulo = line_bytecnt / size;

	char output[line_bytecnt * 4 + 1];
	unsigned output_len = 0;

	const char *value_fmt;
	switch (size) {
		case 4:
			value_fmt = "%8.8x ";
			break;
		case 2:
			value_fmt = "%4.4x ";
			break;
		case 1:
			value_fmt = "%2.2x ";
			break;
		default:
			/* "can't happen", caller checked */
			LOG_ERROR("invalid memory read size: %u", size);
			return;
	}

	for (unsigned i = 0; i < count; i++) {
		if (i % line_modulo == 0)
			output_len += snprintf(output + output_len,
					sizeof(output) - output_len,
					"0x%8.8x: ",
					(unsigned) (address + i));

		uint32_t value = 0;
		const uint8_t *value_ptr = buffer + i * size;
		switch (size) {
			case 4:
				value = target_buffer_get_u32(target, value_ptr);
				break;
			case 2:
				value = target_buffer_get_u16(target, value_ptr);
				break;
			case 1:
				value = *value_ptr;
		}
		output_len += snprintf(output + output_len,
				sizeof(output) - output_len,
				value_fmt,
				value);

		if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
			command_print(cmd_ctx, "%s", output);
			output_len = 0;
		}
	}
}

COMMAND_HANDLER(dsp563xx_mem_command)
{
	struct target *target = get_current_target(CMD_CTX);
	int err = ERROR_OK;
	int read_mem;
	uint32_t address = 0;
	uint32_t count = 1, i;
	uint32_t pattern = 0;
	uint32_t mem_type;
	uint8_t *buffer, *b;

	switch (CMD_NAME[1]) {
		case 'w':
			read_mem = 0;
			break;
		case 'd':
			read_mem = 1;
			break;
		default:
			return ERROR_COMMAND_SYNTAX_ERROR;
	}

	switch (CMD_NAME[3]) {
		case 'x':
			mem_type = MEM_X;
			break;
		case 'y':
			mem_type = MEM_Y;
			break;
		case 'p':
			mem_type = MEM_P;
			break;
		default:
			return ERROR_COMMAND_SYNTAX_ERROR;
	}

	if (CMD_ARGC > 0)
		COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);

	if (read_mem == 0) {
		if (CMD_ARGC < 2)
			return ERROR_COMMAND_SYNTAX_ERROR;
		if (CMD_ARGC > 1)
			COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
		if (CMD_ARGC > 2)
			COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
	}

	if (read_mem == 1) {
		if (CMD_ARGC < 1)
			return ERROR_COMMAND_SYNTAX_ERROR;
		if (CMD_ARGC > 1)
			COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], count);
	}

	buffer = calloc(count, sizeof(uint32_t));

	if (read_mem == 1) {
		err = dsp563xx_read_memory(target, mem_type, address, sizeof(uint32_t),
				count, buffer);
		if (err == ERROR_OK)
			handle_md_output(CMD_CTX, target, address, sizeof(uint32_t), count, buffer);

	} else {
		b = buffer;

		for (i = 0; i < count; i++) {
			target_buffer_set_u32(target, b, pattern);
			b += 4;
		}

		err = dsp563xx_write_memory(target,
				mem_type,
				address,
				sizeof(uint32_t),
				count,
				buffer);
	}

	free(buffer);

	return err;
}

static const struct command_registration dsp563xx_command_handlers[] = {
	{
		.name = "mwwx",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "write x memory words",
		.usage = "mwwx address value [count]",
	},
	{
		.name = "mwwy",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "write y memory words",
		.usage = "mwwy address value [count]",
	},
	{
		.name = "mwwp",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "write p memory words",
		.usage = "mwwp address value [count]",
	},
	{
		.name = "mdwx",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "display x memory words",
		.usage = "mdwx address [count]",
	},
	{
		.name = "mdwy",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "display y memory words",
		.usage = "mdwy address [count]",
	},
	{
		.name = "mdwp",
		.handler = dsp563xx_mem_command,
		.mode = COMMAND_EXEC,
		.help = "display p memory words",
		.usage = "mdwp address [count]",
	},
	COMMAND_REGISTRATION_DONE
};

/** Holds methods for DSP563XX targets. */
struct target_type dsp563xx_target = {
	.name = "dsp563xx",

	.poll = dsp563xx_poll,
	.arch_state = dsp563xx_arch_state,

	.target_request_data = NULL,

	.get_gdb_reg_list = dsp563xx_get_gdb_reg_list,

	.halt = dsp563xx_halt,
	.resume = dsp563xx_resume,
	.step = dsp563xx_step,

	.assert_reset = dsp563xx_assert_reset,
	.deassert_reset = dsp563xx_deassert_reset,
	.soft_reset_halt = dsp563xx_soft_reset_halt,

	.read_memory = dsp563xx_read_memory_default,
	.write_memory = dsp563xx_write_memory_default,

	.read_buffer = dsp563xx_read_buffer_default,
	.write_buffer = dsp563xx_write_buffer_default,

	.run_algorithm = dsp563xx_run_algorithm,

	.add_breakpoint = dsp563xx_add_breakpoint,
	.remove_breakpoint = dsp563xx_remove_breakpoint,
	.add_watchpoint = dsp563xx_add_watchpoint,
	.remove_watchpoint = dsp563xx_remove_watchpoint,

	.commands = dsp563xx_command_handlers,
	.target_create = dsp563xx_target_create,
	.init_target = dsp563xx_init_target,
	.examine = dsp563xx_examine,
};