aboutsummaryrefslogtreecommitdiff
path: root/src/target/esirisc.c
blob: c928445ccf240f1a250851de5fbb5716fde45324 (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
/***************************************************************************
 *   Copyright (C) 2018 by Square, Inc.                                    *
 *   Steven Stallion <stallion@squareup.com>                               *
 *   James Zhao <hjz@squareup.com>                                         *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

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

#include <helper/binarybuffer.h>
#include <helper/command.h>
#include <helper/log.h>
#include <helper/time_support.h>
#include <helper/types.h>
#include <jtag/interface.h>
#include <target/breakpoints.h>
#include <target/register.h>
#include <target/target.h>
#include <target/target_type.h>

#include "esirisc.h"

#define RESET_TIMEOUT			5000	/* 5s */
#define STEP_TIMEOUT			1000	/* 1s */

/*
 * eSi-RISC targets support a configurable number of interrupts;
 * up to 32 interrupts are supported.
 */
static const char * const esirisc_exception_strings[] = {
	[EID_RESET]					= "Reset",
	[EID_HARDWARE_FAILURE]		= "HardwareFailure",
	[EID_NMI]					= "NMI",
	[EID_INST_BREAKPOINT]		= "InstBreakpoint",
	[EID_DATA_BREAKPOINT]		= "DataBreakpoint",
	[EID_UNSUPPORTED]			= "Unsupported",
	[EID_PRIVILEGE_VIOLATION]	= "PrivilegeViolation",
	[EID_INST_BUS_ERROR]		= "InstBusError",
	[EID_DATA_BUS_ERROR]		= "DataBusError",
	[EID_ALIGNMENT_ERROR]		= "AlignmentError",
	[EID_ARITHMETIC_ERROR]		= "ArithmeticError",
	[EID_SYSTEM_CALL]			= "SystemCall",
	[EID_MEMORY_MANAGEMENT]		= "MemoryManagement",
	[EID_UNRECOVERABLE]			= "Unrecoverable",
	[EID_INTERRUPTn+0]			= "Interrupt0",
	[EID_INTERRUPTn+1]			= "Interrupt1",
	[EID_INTERRUPTn+2]			= "Interrupt2",
	[EID_INTERRUPTn+3]			= "Interrupt3",
	[EID_INTERRUPTn+4]			= "Interrupt4",
	[EID_INTERRUPTn+5]			= "Interrupt5",
	[EID_INTERRUPTn+6]			= "Interrupt6",
	[EID_INTERRUPTn+7]			= "Interrupt7",
	[EID_INTERRUPTn+8]			= "Interrupt8",
	[EID_INTERRUPTn+9]			= "Interrupt9",
	[EID_INTERRUPTn+10]			= "Interrupt10",
	[EID_INTERRUPTn+11]			= "Interrupt11",
	[EID_INTERRUPTn+12]			= "Interrupt12",
	[EID_INTERRUPTn+13]			= "Interrupt13",
	[EID_INTERRUPTn+14]			= "Interrupt14",
	[EID_INTERRUPTn+15]			= "Interrupt15",
	[EID_INTERRUPTn+16]			= "Interrupt16",
	[EID_INTERRUPTn+17]			= "Interrupt17",
	[EID_INTERRUPTn+18]			= "Interrupt18",
	[EID_INTERRUPTn+19]			= "Interrupt19",
	[EID_INTERRUPTn+20]			= "Interrupt20",
	[EID_INTERRUPTn+21]			= "Interrupt21",
	[EID_INTERRUPTn+22]			= "Interrupt22",
	[EID_INTERRUPTn+23]			= "Interrupt23",
	[EID_INTERRUPTn+24]			= "Interrupt24",
	[EID_INTERRUPTn+25]			= "Interrupt25",
	[EID_INTERRUPTn+26]			= "Interrupt26",
	[EID_INTERRUPTn+27]			= "Interrupt27",
	[EID_INTERRUPTn+28]			= "Interrupt28",
	[EID_INTERRUPTn+29]			= "Interrupt29",
	[EID_INTERRUPTn+30]			= "Interrupt30",
	[EID_INTERRUPTn+31]			= "Interrupt31",
};

/*
 * eSi-RISC targets support a configurable number of general purpose
 * registers; 8, 16, and 32 registers are supported.
 */
static const struct {
	enum esirisc_reg_num number;
	const char *name;
	enum reg_type type;
	const char *group;
} esirisc_regs[] = {
	{ ESIRISC_SP, "sp", REG_TYPE_DATA_PTR, "general" },
	{ ESIRISC_RA, "ra", REG_TYPE_INT, "general" },
	{ ESIRISC_R2, "r2", REG_TYPE_INT, "general" },
	{ ESIRISC_R3, "r3", REG_TYPE_INT, "general" },
	{ ESIRISC_R4, "r4", REG_TYPE_INT, "general" },
	{ ESIRISC_R5, "r5", REG_TYPE_INT, "general" },
	{ ESIRISC_R6, "r6", REG_TYPE_INT, "general" },
	{ ESIRISC_R7, "r7", REG_TYPE_INT, "general" },
	{ ESIRISC_R8, "r8", REG_TYPE_INT, "general" },
	{ ESIRISC_R9, "r9", REG_TYPE_INT, "general" },
	{ ESIRISC_R10, "r10", REG_TYPE_INT, "general" },
	{ ESIRISC_R11, "r11", REG_TYPE_INT, "general" },
	{ ESIRISC_R12, "r12", REG_TYPE_INT, "general" },
	{ ESIRISC_R13, "r13", REG_TYPE_INT, "general" },
	{ ESIRISC_R14, "r14", REG_TYPE_INT, "general" },
	{ ESIRISC_R15, "r15", REG_TYPE_INT, "general" },
	{ ESIRISC_R16, "r16", REG_TYPE_INT, "general" },
	{ ESIRISC_R17, "r17", REG_TYPE_INT, "general" },
	{ ESIRISC_R18, "r18", REG_TYPE_INT, "general" },
	{ ESIRISC_R19, "r19", REG_TYPE_INT, "general" },
	{ ESIRISC_R20, "r20", REG_TYPE_INT, "general" },
	{ ESIRISC_R21, "r21", REG_TYPE_INT, "general" },
	{ ESIRISC_R22, "r22", REG_TYPE_INT, "general" },
	{ ESIRISC_R23, "r23", REG_TYPE_INT, "general" },
	{ ESIRISC_R24, "r24", REG_TYPE_INT, "general" },
	{ ESIRISC_R25, "r25", REG_TYPE_INT, "general" },
	{ ESIRISC_R26, "r26", REG_TYPE_INT, "general" },
	{ ESIRISC_R27, "r27", REG_TYPE_INT, "general" },
	{ ESIRISC_R28, "r28", REG_TYPE_INT, "general" },
	{ ESIRISC_R29, "r29", REG_TYPE_INT, "general" },
	{ ESIRISC_R30, "r30", REG_TYPE_INT, "general" },
	{ ESIRISC_R31, "r31", REG_TYPE_INT, "general" },
};

/*
 * Control and Status Registers (CSRs) are largely defined as belonging
 * to the system register group. The exception to this rule are the PC
 * and CAS registers, which belong to the general group. While debug is
 * active, EPC, ECAS, and ETC must be used to read and write the PC,
 * CAS, and TC CSRs, respectively.
 */
static const struct {
	enum esirisc_reg_num number;
	uint8_t bank;
	uint8_t csr;
	const char *name;
	enum reg_type type;
	const char *group;
} esirisc_csrs[] = {
	{ ESIRISC_PC, CSR_THREAD, CSR_THREAD_EPC, "PC", REG_TYPE_CODE_PTR, "general" },	/*  PC -> EPC  */
	{ ESIRISC_CAS, CSR_THREAD, CSR_THREAD_ECAS, "CAS", REG_TYPE_INT, "general" },	/* CAS -> ECAS */
	{ ESIRISC_TC, CSR_THREAD, CSR_THREAD_ETC, "TC", REG_TYPE_INT, "system" },		/*  TC -> ETC  */
	{ ESIRISC_ETA, CSR_THREAD, CSR_THREAD_ETA, "ETA", REG_TYPE_INT, "system" },
	{ ESIRISC_ETC, CSR_THREAD, CSR_THREAD_ETC, "ETC", REG_TYPE_INT, "system" },
	{ ESIRISC_EPC, CSR_THREAD, CSR_THREAD_EPC, "EPC", REG_TYPE_CODE_PTR, "system" },
	{ ESIRISC_ECAS, CSR_THREAD, CSR_THREAD_ECAS, "ECAS", REG_TYPE_INT, "system" },
	{ ESIRISC_EID, CSR_THREAD, CSR_THREAD_EID, "EID", REG_TYPE_INT, "system" },
	{ ESIRISC_ED, CSR_THREAD, CSR_THREAD_ED, "ED", REG_TYPE_INT, "system" },
	{ ESIRISC_IP, CSR_INTERRUPT, CSR_INTERRUPT_IP, "IP", REG_TYPE_INT, "system"},
	{ ESIRISC_IM, CSR_INTERRUPT, CSR_INTERRUPT_IM, "IM", REG_TYPE_INT, "system"},
	{ ESIRISC_IS, CSR_INTERRUPT, CSR_INTERRUPT_IS, "IS", REG_TYPE_INT, "system"},
	{ ESIRISC_IT, CSR_INTERRUPT, CSR_INTERRUPT_IT, "IT", REG_TYPE_INT, "system"},
};

static int esirisc_disable_interrupts(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t etc;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
		return retval;
	}

	etc &= ~(1<<0);		/* TC.I */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

#if 0
static int esirisc_enable_interrupts(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t etc;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, &etc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
		return retval;
	}

	etc |= (1<<0);		/* TC.I */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC, etc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}
#endif

static int esirisc_save_interrupts(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
			&esirisc->etc_save);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Thread CSR: ETC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_restore_interrupts(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETC,
			esirisc->etc_save);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Thread CSR: ETC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

#if 0
static int esirisc_save_hwdc(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
			&esirisc->hwdc_save);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Thread CSR: HWDC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}
#endif

static int esirisc_restore_hwdc(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_HWDC,
			esirisc->hwdc_save);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: HWDC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_save_context(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);

	LOG_DEBUG("-");

	for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
		struct reg *reg = esirisc->reg_cache->reg_list + i;
		struct esirisc_reg *reg_info = reg->arch_info;

		if (reg->exist && !reg->valid)
			reg_info->read(reg);
	}

	return ERROR_OK;
}

static int esirisc_restore_context(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);

	LOG_DEBUG("-");

	for (unsigned i = 0; i < esirisc->reg_cache->num_regs; ++i) {
		struct reg *reg = esirisc->reg_cache->reg_list + i;
		struct esirisc_reg *reg_info = reg->arch_info;

		if (reg->exist && reg->dirty)
			reg_info->write(reg);
	}

	return ERROR_OK;
}

static int esirisc_flush_caches(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

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

	int retval = esirisc_jtag_flush_caches(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to flush caches", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_wait_debug_active(struct esirisc_common *esirisc, int ms)
{
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int64_t t;

	LOG_DEBUG("-");

	t = timeval_ms();
	for (;;) {
		int retval = esirisc_jtag_enable_debug(jtag_info);
		if (retval == ERROR_OK && esirisc_jtag_is_debug_active(jtag_info))
			return retval;

		if ((timeval_ms() - t) > ms)
			return ERROR_TARGET_TIMEOUT;

		alive_sleep(100);
	}
}

static int esirisc_read_memory(struct target *target, target_addr_t address,
		uint32_t size, uint32_t count, uint8_t *buffer)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	int num_bits = 8 * size;
	for (uint32_t i = 0; i < count; ++i) {
		union esirisc_memory value;
		void *value_p;

		switch (size) {
			case sizeof(value.word):
				value_p = &value.word;
				retval = esirisc_jtag_read_word(jtag_info, address, value_p);
				break;

			case sizeof(value.hword):
				value_p = &value.hword;
				retval = esirisc_jtag_read_hword(jtag_info, address, value_p);
				break;

			case sizeof(value.byte):
				value_p = &value.byte;
				retval = esirisc_jtag_read_byte(jtag_info, address, value_p);
				break;

			default:
				LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
				return ERROR_FAIL;
		}

		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
					address);
			return retval;
		}

		buf_cpy(value_p, buffer, num_bits);
		address += size;
		buffer += size;
	}

	return ERROR_OK;
}

static int esirisc_write_memory(struct target *target, target_addr_t address,
		uint32_t size, uint32_t count, const uint8_t *buffer)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	int num_bits = 8 * size;
	for (uint32_t i = 0; i < count; ++i) {
		union esirisc_memory value;

		switch (size) {
			case sizeof(value.word):
				value.word = buf_get_u32(buffer, 0, num_bits);
				retval = esirisc_jtag_write_word(jtag_info, address, value.word);
				break;

			case sizeof(value.hword):
				value.hword = buf_get_u32(buffer, 0, num_bits);
				retval = esirisc_jtag_write_hword(jtag_info, address, value.hword);
				break;

			case sizeof(value.byte):
				value.byte = buf_get_u32(buffer, 0, num_bits);
				retval = esirisc_jtag_write_byte(jtag_info, address, value.byte);
				break;

			default:
				LOG_ERROR("%s: unsupported size: %" PRIu32, target_name(target), size);
				return ERROR_FAIL;
		}

		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to write address: 0x%" TARGET_PRIxADDR, target_name(target),
					address);
			return retval;
		}

		address += size;
		buffer += size;
	}

	return ERROR_OK;
}

static int esirisc_checksum_memory(struct target *target, target_addr_t address,
		uint32_t count, uint32_t *checksum)
{
	return ERROR_FAIL;	/* not supported */
}

static int esirisc_next_breakpoint(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct breakpoint **breakpoints_p = esirisc->breakpoints_p;
	struct breakpoint **breakpoints_e = breakpoints_p + esirisc->num_breakpoints;

	LOG_DEBUG("-");

	for (int bp_index = 0; breakpoints_p < breakpoints_e; ++breakpoints_p, ++bp_index)
		if (*breakpoints_p == NULL)
			return bp_index;

	return -1;
}

static int esirisc_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int bp_index;
	uint32_t ibc;
	int retval;

	LOG_DEBUG("-");

	/*
	 * The default linker scripts provided by the eSi-RISC toolchain do
	 * not specify attributes on memory regions, which results in
	 * incorrect application of software breakpoints by GDB. Targets
	 * must be configured with `gdb_breakpoint_override hard` as
	 * software breakpoints are not supported.
	 */
	if (breakpoint->type != BKPT_HARD)
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;

	bp_index = esirisc_next_breakpoint(target);
	if (bp_index < 0) {
		LOG_ERROR("%s: out of hardware breakpoints", target_name(target));
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}

	breakpoint->set = bp_index + 1;
	esirisc->breakpoints_p[bp_index] = breakpoint;

	/* specify instruction breakpoint address */
	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBAn + bp_index,
			breakpoint->address);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: IBA", target_name(target));
		return retval;
	}

	/* enable instruction breakpoint */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
		return retval;
	}

	ibc |= (1 << bp_index);		/* IBC.In */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_add_breakpoints(struct target *target)
{
	struct breakpoint *breakpoint = target->breakpoints;

	LOG_DEBUG("-");

	while (breakpoint != NULL) {
		if (breakpoint->set == 0)
			esirisc_add_breakpoint(target, breakpoint);

		breakpoint = breakpoint->next;
	}

	return ERROR_OK;
}

static int esirisc_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int bp_index = breakpoint->set - 1;
	uint32_t ibc;
	int retval;

	LOG_DEBUG("-");

	/* disable instruction breakpoint */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, &ibc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: IBC", target_name(target));
		return retval;
	}

	ibc &= ~(1 << bp_index);	/* IBC.In */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, ibc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
		return retval;
	}

	esirisc->breakpoints_p[bp_index] = NULL;
	breakpoint->set = 0;

	return ERROR_OK;
}

static int esirisc_remove_breakpoints(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	/* clear instruction breakpoints */
	int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_IBC, 0);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: IBC", target_name(target));
		return retval;
	}

	memset(esirisc->breakpoints_p, 0, sizeof(esirisc->breakpoints_p));

	return ERROR_OK;
}

static int esirisc_next_watchpoint(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct watchpoint **watchpoints_p = esirisc->watchpoints_p;
	struct watchpoint **watchpoints_e = watchpoints_p + esirisc->num_watchpoints;

	LOG_DEBUG("-");

	for (int wp_index = 0; watchpoints_p < watchpoints_e; ++watchpoints_p, ++wp_index)
		if (*watchpoints_p == NULL)
			return wp_index;

	return -1;
}

static int esirisc_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int wp_index;
	uint32_t dbs, dbc;
	int retval;

	LOG_DEBUG("-");

	wp_index = esirisc_next_watchpoint(target);
	if (wp_index < 0) {
		LOG_ERROR("%s: out of hardware watchpoints", target_name(target));
		return ERROR_FAIL;
	}

	watchpoint->set = wp_index + 1;
	esirisc->watchpoints_p[wp_index] = watchpoint;

	/* specify data breakpoint address */
	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBAn + wp_index,
			watchpoint->address);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DBA", target_name(target));
		return retval;
	}

	/* specify data breakpoint size */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, &dbs);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: DBS", target_name(target));
		return retval;
	}

	uint32_t sn;
	switch (watchpoint->length) {
		case sizeof(uint64_t):
			sn = 0x3;
			break;
		case sizeof(uint32_t):
			sn = 0x2;
			break;

		case sizeof(uint16_t):
			sn = 0x1;
			break;

		case sizeof(uint8_t):
			sn = 0x0;
			break;

		default:
			LOG_ERROR("%s: unsupported length: %" PRIu32, target_name(target),
					watchpoint->length);
			return ERROR_FAIL;
	}

	dbs |= (sn << (2 * wp_index));		/* DBS.Sn */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBS, dbs);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DBS", target_name(target));
		return retval;
	}

	/* enable data breakpoint */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
		return retval;
	}

	uint32_t dn;
	switch (watchpoint->rw) {
		case WPT_READ:
			dn = 0x1;
			break;

		case WPT_WRITE:
			dn = 0x2;
			break;

		case WPT_ACCESS:
			dn = 0x3;
			break;

		default:
			LOG_ERROR("%s: unsupported rw: %" PRId32, target_name(target),
					watchpoint->rw);
			return ERROR_FAIL;
	}

	dbc |= (dn << (2 * wp_index));		/* DBC.Dn */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_add_watchpoints(struct target *target)
{
	struct watchpoint *watchpoint = target->watchpoints;

	LOG_DEBUG("-");

	while (watchpoint != NULL) {
		if (watchpoint->set == 0)
			esirisc_add_watchpoint(target, watchpoint);

		watchpoint = watchpoint->next;
	}

	return ERROR_OK;
}

static int esirisc_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int wp_index = watchpoint->set - 1;
	uint32_t dbc;
	int retval;

	LOG_DEBUG("-");

	/* disable data breakpoint */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, &dbc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: DBC", target_name(target));
		return retval;
	}

	dbc &= ~(0x3 << (2 * wp_index));	/* DBC.Dn */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, dbc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
		return retval;
	}

	esirisc->watchpoints_p[wp_index] = NULL;
	watchpoint->set = 0;

	return ERROR_OK;
}

static int esirisc_remove_watchpoints(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	/* clear data breakpoints */
	int retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DBC, 0);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DBC", target_name(target));
		return retval;
	}

	memset(esirisc->watchpoints_p, 0, sizeof(esirisc->watchpoints_p));

	return ERROR_OK;
}

static int esirisc_halt(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;

	LOG_DEBUG("-");

	if (target->state == TARGET_HALTED)
		return ERROR_OK;

	int retval = esirisc_jtag_break(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to halt target", target_name(target));
		return retval;
	}

	target->debug_reason = DBG_REASON_DBGRQ;

	return ERROR_OK;
}

static int esirisc_disable_step(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t dc;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
		return retval;
	}

	dc &= ~(1<<0);	/* DC.S */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_enable_step(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t dc;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_read_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, &dc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Debug CSR: DC", target_name(target));
		return retval;
	}

	dc |= (1<<0);	/* DC.S */

	retval = esirisc_jtag_write_csr(jtag_info, CSR_DEBUG, CSR_DEBUG_DC, dc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Debug CSR: DC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_resume_or_step(struct target *target, int current, target_addr_t address,
		int handle_breakpoints, int debug_execution, bool step)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	struct breakpoint *breakpoint = NULL;
	int retval;

	LOG_DEBUG("-");

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

	if (!debug_execution) {
		target_free_all_working_areas(target);
		esirisc_add_breakpoints(target);
		esirisc_add_watchpoints(target);
	}

	if (current)
		address = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
	else {
		buf_set_u32(esirisc->epc->value, 0, esirisc->epc->size, address);
		esirisc->epc->dirty = true;
		esirisc->epc->valid = true;
	}

	esirisc_restore_context(target);

	if (esirisc_has_cache(esirisc))
		esirisc_flush_caches(target);

	if (handle_breakpoints) {
		breakpoint = breakpoint_find(target, address);
		if (breakpoint != NULL)
			esirisc_remove_breakpoint(target, breakpoint);
	}

	if (step) {
		esirisc_disable_interrupts(target);
		esirisc_enable_step(target);
		target->debug_reason = DBG_REASON_SINGLESTEP;
	} else {
		esirisc_disable_step(target);
		esirisc_restore_interrupts(target);
		target->debug_reason = DBG_REASON_NOTHALTED;
	}

	esirisc_restore_hwdc(target);

	retval = esirisc_jtag_continue(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to resume target", target_name(target));
		return retval;
	}

	register_cache_invalidate(esirisc->reg_cache);

	if (!debug_execution) {
		target->state = TARGET_RUNNING;
		target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
	} else {
		target->state = TARGET_DEBUG_RUNNING;
		target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
	}

	return ERROR_OK;
}

static int esirisc_resume(struct target *target, int current, target_addr_t address,
		int handle_breakpoints, int debug_execution)
{
	LOG_DEBUG("-");

	return esirisc_resume_or_step(target, current, address,
			handle_breakpoints, debug_execution, false);
}

static int esirisc_step(struct target *target, int current, target_addr_t address,
		int handle_breakpoints)
{
	LOG_DEBUG("-");

	return esirisc_resume_or_step(target, current, address,
			handle_breakpoints, 0, true);
}

static int esirisc_debug_step(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	esirisc_disable_interrupts(target);
	esirisc_enable_step(target);

	retval = esirisc_jtag_continue(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to resume target", target_name(target));
		return retval;
	}

	retval = esirisc_wait_debug_active(esirisc, STEP_TIMEOUT);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: step timed out", target_name(target));
		return retval;
	}

	esirisc_disable_step(target);
	esirisc_restore_interrupts(target);

	return ERROR_OK;
}

static int esirisc_debug_reset(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_assert_reset(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to assert reset", target_name(target));
		return retval;
	}

	retval = esirisc_jtag_deassert_reset(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to deassert reset", target_name(target));
		return retval;
	}

	retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: reset timed out", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_debug_enable(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_enable_debug(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to enable debug mode", target_name(target));
		return retval;
	}

	/*
	 * The debug clock is inactive until the first command is sent.
	 * If the target is stopped, we must first issue a reset before
	 * attempting further communication. This also handles unpowered
	 * targets, which will respond with all ones and appear active.
	 */
	if (esirisc_jtag_is_stopped(jtag_info)) {
		LOG_INFO("%s: debug clock inactive; attempting debug reset", target_name(target));
		retval = esirisc_debug_reset(target);
		if (retval != ERROR_OK)
			return retval;

		if (esirisc_jtag_is_stopped(jtag_info)) {
			LOG_ERROR("%s: target unresponsive; giving up", target_name(target));
			return ERROR_FAIL;
		}
	}

	return ERROR_OK;
}

static int esirisc_debug_entry(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct breakpoint *breakpoint;

	LOG_DEBUG("-");

	esirisc_save_context(target);

	if (esirisc_has_cache(esirisc))
		esirisc_flush_caches(target);

	if (target->debug_reason != DBG_REASON_SINGLESTEP) {
		esirisc_save_interrupts(target);

		uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
		switch (eid) {
			/*
			 * InstBreakpoint exceptions are also raised when a core is
			 * halted for debugging. The following is required to
			 * determine if a breakpoint was encountered.
			 */
			case EID_INST_BREAKPOINT:
				breakpoint = breakpoint_find(target,
						buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size));
				target->debug_reason = (breakpoint != NULL) ?
						DBG_REASON_BREAKPOINT : DBG_REASON_DBGRQ;
				break;

			/*
			 * eSi-RISC treats watchpoints similarly to breakpoints,
			 * however GDB will not request to step over the current
			 * instruction when a watchpoint fires. The following is
			 * required to resume the target.
			 */
			case EID_DATA_BREAKPOINT:
				esirisc_remove_watchpoints(target);
				esirisc_debug_step(target);
				esirisc_add_watchpoints(target);
				target->debug_reason = DBG_REASON_WATCHPOINT;
				break;

			default:
				target->debug_reason = DBG_REASON_DBGRQ;
		}
	}

	return ERROR_OK;
}

static int esirisc_poll(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	retval = esirisc_jtag_enable_debug(jtag_info);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to poll target", target_name(target));
		return retval;
	}

	if (esirisc_jtag_is_stopped(jtag_info)) {
		LOG_ERROR("%s: target has stopped; reset required", target_name(target));
		target->state = TARGET_UNKNOWN;
		return ERROR_TARGET_FAILURE;
	}

	if (esirisc_jtag_is_debug_active(jtag_info)) {
		if (target->state == TARGET_RUNNING || target->state == TARGET_RESET) {
			target->state = TARGET_HALTED;

			retval = esirisc_debug_entry(target);
			if (retval != ERROR_OK)
				return retval;

			target_call_event_callbacks(target, TARGET_EVENT_HALTED);
		}

	} else if (target->state == TARGET_HALTED || target->state == TARGET_RESET) {
		target->state = TARGET_RUNNING;
		target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
	}

	return ERROR_OK;
}

static int esirisc_assert_reset(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	if (jtag_get_reset_config() & RESET_HAS_SRST) {
		jtag_add_reset(1, 1);
		if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) == 0)
			jtag_add_reset(0, 1);
	} else {
		esirisc_remove_breakpoints(target);
		esirisc_remove_watchpoints(target);

		retval = esirisc_jtag_assert_reset(jtag_info);
		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to assert reset", target_name(target));
			return retval;
		}
	}

	target->state = TARGET_RESET;

	register_cache_invalidate(esirisc->reg_cache);

	return ERROR_OK;
}

static int esirisc_reset_entry(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t eta, epc;
	int retval;

	LOG_DEBUG("-");

	/* read exception table address */
	retval = esirisc_jtag_read_csr(jtag_info, CSR_THREAD, CSR_THREAD_ETA, &eta);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Thread CSR: ETA", target_name(target));
		return retval;
	}

	/* read reset entry point */
	retval = esirisc_jtag_read_word(jtag_info, eta + ENTRY_RESET, &epc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read address: 0x%" TARGET_PRIxADDR, target_name(target),
				(target_addr_t)epc);
		return retval;
	}

	/* write reset entry point */
	retval = esirisc_jtag_write_csr(jtag_info, CSR_THREAD, CSR_THREAD_EPC, epc);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write Thread CSR: EPC", target_name(target));
		return retval;
	}

	return ERROR_OK;
}

static int esirisc_deassert_reset(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	if (jtag_get_reset_config() & RESET_HAS_SRST) {
		jtag_add_reset(0, 0);

		retval = esirisc_debug_enable(target);
		if (retval != ERROR_OK)
			return retval;

		retval = esirisc_debug_reset(target);
		if (retval != ERROR_OK)
			return retval;

	} else {
		retval = esirisc_jtag_deassert_reset(jtag_info);
		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to deassert reset", target_name(target));
			return retval;
		}
	}

	retval = esirisc_wait_debug_active(esirisc, RESET_TIMEOUT);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: reset timed out", target_name(target));
		return retval;
	}

	retval = esirisc_reset_entry(target);
	if (retval != ERROR_OK)
		return retval;

	esirisc_add_breakpoints(target);
	esirisc_add_watchpoints(target);

	esirisc_restore_hwdc(target);

	if (!target->reset_halt) {
		retval = esirisc_jtag_continue(jtag_info);
		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to resume target", target_name(target));
			return retval;
		}
	}

	return ERROR_OK;
}

static int esirisc_arch_state(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	uint32_t epc = buf_get_u32(esirisc->epc->value, 0, esirisc->epc->size);
	uint32_t ecas = buf_get_u32(esirisc->ecas->value, 0, esirisc->ecas->size);
	uint32_t eid = buf_get_u32(esirisc->eid->value, 0, esirisc->eid->size);
	uint32_t ed = buf_get_u32(esirisc->ed->value, 0, esirisc->ed->size);

	LOG_USER("target halted due to %s, exception: %s\n"
			"EPC: 0x%" PRIx32 ", ECAS: 0x%" PRIx32 ", EID: 0x%" PRIx32 ", ED: 0x%" PRIx32,
			debug_reason_name(target), esirisc_exception_strings[eid], epc, ecas, eid, ed);

	return ERROR_OK;
}

static const char *esirisc_get_gdb_arch(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);

	LOG_DEBUG("-");

	/*
	 * Targets with the UNIFIED_ADDRESS_SPACE option disabled employ a
	 * Harvard architecture. This option is not exposed in a CSR, which
	 * requires additional configuration to properly interact with these
	 * targets in GDB (also see: `esirisc cache_arch`).
	 */
	if (esirisc->gdb_arch == NULL && target_was_examined(target))
		esirisc->gdb_arch = alloc_printf("esirisc:%d_bit_%d_reg_%s",
				esirisc->num_bits, esirisc->num_regs, esirisc_cache_arch_name(esirisc));

	return esirisc->gdb_arch;
}

static int esirisc_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
		int *reg_list_size, enum target_register_class reg_class)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);

	LOG_DEBUG("-");

	*reg_list_size = ESIRISC_NUM_REGS;

	*reg_list = calloc(*reg_list_size, sizeof(struct reg *));
	if (*reg_list == NULL)
		return ERROR_FAIL;

	if (reg_class == REG_CLASS_ALL)
		for (int i = 0; i < *reg_list_size; ++i)
			(*reg_list)[i] = esirisc->reg_cache->reg_list + i;
	else {
		for (int i = 0; i < esirisc->num_regs; ++i)
			(*reg_list)[i] = esirisc->reg_cache->reg_list + i;

		(*reg_list)[ESIRISC_PC] = esirisc->reg_cache->reg_list + ESIRISC_PC;
		(*reg_list)[ESIRISC_CAS] = esirisc->reg_cache->reg_list + ESIRISC_CAS;
	}

	return ERROR_OK;
}

static int esirisc_read_reg(struct reg *reg)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	struct target *target = esirisc->target;
	uint32_t data;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_read_reg(jtag_info, reg->number, &data);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read register: %s", target_name(target), reg->name);
		return retval;
	}

	buf_set_u32(reg->value, 0, reg->size, data);
	reg->dirty = false;
	reg->valid = true;

	return ERROR_OK;
}

static int esirisc_write_reg(struct reg *reg)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	struct target *target = esirisc->target;
	uint32_t data = buf_get_u32(reg->value, 0, reg->size);

	LOG_DEBUG("-");

	int retval = esirisc_jtag_write_reg(jtag_info, reg->number, data);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write register: %s", target_name(target), reg->name);
		return retval;
	}

	reg->dirty = false;
	reg->valid = true;

	return ERROR_OK;
}

static int esirisc_read_csr(struct reg *reg)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	struct target *target = esirisc->target;
	uint32_t data;

	LOG_DEBUG("-");

	int retval = esirisc_jtag_read_csr(jtag_info, reg_info->bank, reg_info->csr, &data);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read CSR: %s", target_name(target), reg->name);
		return retval;
	}

	buf_set_u32(reg->value, 0, reg->size, data);
	reg->dirty = false;
	reg->valid = true;

	return ERROR_OK;
}

static int esirisc_write_csr(struct reg *reg)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	struct target *target = esirisc->target;
	uint32_t data = buf_get_u32(reg->value, 0, reg->size);

	LOG_DEBUG("-");

	int retval = esirisc_jtag_write_csr(jtag_info, reg_info->bank, reg_info->csr, data);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to write CSR: %s", target_name(target), reg->name);
		return retval;
	}

	reg->dirty = false;
	reg->valid = true;

	return ERROR_OK;
}

static int esirisc_get_reg(struct reg *reg)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct target *target = esirisc->target;

	LOG_DEBUG("-");

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

	return reg_info->read(reg);
}

static int esirisc_set_reg(struct reg *reg, uint8_t *buf)
{
	struct esirisc_reg *reg_info = reg->arch_info;
	struct esirisc_common *esirisc = reg_info->esirisc;
	struct target *target = esirisc->target;
	uint32_t value = buf_get_u32(buf, 0, reg->size);

	LOG_DEBUG("-");

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

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

	return ERROR_OK;
}

static const struct reg_arch_type esirisc_reg_type = {
	.get = esirisc_get_reg,
	.set = esirisc_set_reg,
};

static struct reg_cache *esirisc_build_reg_cache(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(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 = calloc(ESIRISC_NUM_REGS, sizeof(struct reg));

	LOG_DEBUG("-");

	cache->name = "eSi-RISC registers";
	cache->next = NULL;
	cache->reg_list = reg_list;
	cache->num_regs = ESIRISC_NUM_REGS;
	(*cache_p) = cache;

	esirisc->reg_cache = cache;
	esirisc->epc = reg_list + ESIRISC_EPC;
	esirisc->ecas = reg_list + ESIRISC_ECAS;
	esirisc->eid = reg_list + ESIRISC_EID;
	esirisc->ed = reg_list + ESIRISC_ED;

	for (int i = 0; i < esirisc->num_regs; ++i) {
		struct reg *reg = reg_list + esirisc_regs[i].number;
		struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));

		reg->name = esirisc_regs[i].name;
		reg->number = esirisc_regs[i].number;
		reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
		reg->size = esirisc->num_bits;
		reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
		reg->reg_data_type->type = esirisc_regs[i].type;
		reg->group = esirisc_regs[i].group;
		reg_info->esirisc = esirisc;
		reg_info->read = esirisc_read_reg;
		reg_info->write = esirisc_write_reg;
		reg->arch_info = reg_info;
		reg->type = &esirisc_reg_type;
		reg->exist = true;
	}

	for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
		struct reg *reg = reg_list + esirisc_csrs[i].number;
		struct esirisc_reg *reg_info = calloc(1, sizeof(struct esirisc_reg));

		reg->name = esirisc_csrs[i].name;
		reg->number = esirisc_csrs[i].number;
		reg->value = calloc(1, DIV_ROUND_UP(esirisc->num_bits, 8));
		reg->size = esirisc->num_bits;
		reg->reg_data_type = calloc(1, sizeof(struct reg_data_type));
		reg->reg_data_type->type = esirisc_csrs[i].type;
		reg->group = esirisc_csrs[i].group;
		reg_info->esirisc = esirisc;
		reg_info->bank = esirisc_csrs[i].bank;
		reg_info->csr = esirisc_csrs[i].csr;
		reg_info->read = esirisc_read_csr;
		reg_info->write = esirisc_write_csr;
		reg->arch_info = reg_info;
		reg->type = &esirisc_reg_type;
		reg->exist = true;
	}

	return cache;
}

static int esirisc_identify(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	uint32_t csr;
	int retval;

	LOG_DEBUG("-");

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_ARCH0, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: ARCH0", target_name(target));
		return retval;
	}

	esirisc->num_bits = (csr >> 0) & 0x3f;			/* ARCH0.B */
	esirisc->num_regs = (csr >> 10) & 0x3f;			/* ARCH0.R */

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_MEM, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: MEM", target_name(target));
		return retval;
	}

	target->endianness = (csr & 1<<0) ?				/* MEM.E */
			TARGET_BIG_ENDIAN : TARGET_LITTLE_ENDIAN;

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_IC, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: IC", target_name(target));
		return retval;
	}

	esirisc->has_icache = !!(csr & 1<<0);			/* IC.E */

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DC, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: DC", target_name(target));
		return retval;
	}

	esirisc->has_dcache = !!(csr & 1<<0);			/* DC.E */

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_DBG, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: DBG", target_name(target));
		return retval;
	}

	esirisc->num_breakpoints = (csr >> 7) & 0xf;	/* DBG.BP */
	esirisc->num_watchpoints = (csr >> 12) & 0xf;	/* DBG.WP */

	retval = esirisc_jtag_read_csr(jtag_info, CSR_CONFIG, CSR_CONFIG_TRACE, &csr);
	if (retval != ERROR_OK) {
		LOG_ERROR("%s: failed to read Configuration CSR: TRACE", target_name(target));
		return retval;
	}

	esirisc->has_trace = !!(csr & 1<<0);			/* TRACE.T */

	return ERROR_OK;
}

static int esirisc_target_create(struct target *target, Jim_Interp *interp)
{
	struct jtag_tap *tap = target->tap;
	struct esirisc_common *esirisc;

	if (tap == NULL)
		return ERROR_FAIL;

	if (tap->ir_length != INSTR_LENGTH) {
		LOG_ERROR("%s: invalid IR length; expected %d", target_name(target),
				INSTR_LENGTH);
		return ERROR_FAIL;
	}

	esirisc = calloc(1, sizeof(struct esirisc_common));
	if (esirisc == NULL)
		return ERROR_FAIL;

	esirisc->target = target;
	esirisc->jtag_info.tap = tap;
	target->arch_info = esirisc;

	return ERROR_OK;
}

static int esirisc_init_target(struct command_context *cmd_ctx, struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);

	/* trap reset, error, and debug exceptions */
	esirisc->hwdc_save = HWDC_R | HWDC_E | HWDC_D;

	return ERROR_OK;
}

static int esirisc_examine(struct target *target)
{
	struct esirisc_common *esirisc = target_to_esirisc(target);
	struct esirisc_jtag *jtag_info = &esirisc->jtag_info;
	int retval;

	LOG_DEBUG("-");

	if (!target_was_examined(target)) {
		retval = esirisc_debug_enable(target);
		if (retval != ERROR_OK)
			return retval;

		/*
		 * In order to identify the target we must first halt the core.
		 * We quietly resume once identification has completed for those
		 * targets that were running when target_examine was called.
		 */
		if (esirisc_jtag_is_debug_active(jtag_info)) {
			if (target->state == TARGET_UNKNOWN)
				target->debug_reason = DBG_REASON_DBGRQ;

			target->state = TARGET_HALTED;
		} else {
			retval = esirisc_jtag_break(jtag_info);
			if (retval != ERROR_OK) {
				LOG_ERROR("%s: failed to halt target", target_name(target));
				return retval;
			}

			target->state = TARGET_RUNNING;
		}

		retval = esirisc_identify(target);
		if (retval != ERROR_OK) {
			LOG_ERROR("%s: failed to identify target", target_name(target));
			return retval;
		}

		esirisc_build_reg_cache(target);

		esirisc_remove_breakpoints(target);
		esirisc_remove_watchpoints(target);

		esirisc_disable_step(target);
		esirisc_restore_hwdc(target);

		if (target->state == TARGET_HALTED)
			esirisc_save_interrupts(target);
		else {
			retval = esirisc_jtag_continue(jtag_info);
			if (retval != ERROR_OK) {
				LOG_ERROR("%s: failed to resume target", target_name(target));
				return retval;
			}
		}

		target_set_examined(target);

		LOG_INFO("%s: %d bit, %d registers, %s%s%s", target_name(target),
				esirisc->num_bits, esirisc->num_regs,
				target_endianness(target),
				esirisc->has_icache ? ", icache" : "",
				esirisc->has_dcache ? ", dcache" : "");

		LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints%s", target_name(target),
				esirisc->num_breakpoints, esirisc->num_watchpoints,
				esirisc->has_trace ? ", trace" : "");
	}

	return ERROR_OK;
}

COMMAND_HANDLER(handle_esirisc_cache_arch_command)
{
	struct target *target = get_current_target(CMD_CTX);
	struct esirisc_common *esirisc = target_to_esirisc(target);

	if (CMD_ARGC > 0) {
		if (strcmp(*CMD_ARGV, "harvard") == 0)
			esirisc->cache_arch = ESIRISC_CACHE_HARVARD;
		else if (strcmp(*CMD_ARGV, "von_neumann") == 0)
			esirisc->cache_arch = ESIRISC_CACHE_VON_NEUMANN;
		else {
			LOG_ERROR("invalid cache_arch: %s", *CMD_ARGV);
			return ERROR_COMMAND_SYNTAX_ERROR;
		}
	}

	command_print(CMD, "esirisc cache_arch %s", esirisc_cache_arch_name(esirisc));

	return ERROR_OK;
}

COMMAND_HANDLER(handle_esirisc_flush_caches_command)
{
	struct target *target = get_current_target(CMD_CTX);
	struct esirisc_common *esirisc = target_to_esirisc(target);
	int retval;

	if (!esirisc_has_cache(esirisc)) {
		LOG_ERROR("target does not support caching");
		return ERROR_FAIL;
	}

	retval = esirisc_flush_caches(target);

	command_print(CMD, "cache flush %s",
			(retval == ERROR_OK) ? "successful" : "failed");

	return retval;
}

static const struct {
	const char *name;
	int mask;
} esirisc_hwdc_masks[] = {
	{ "reset",		HWDC_R },
	{ "interrupt",	HWDC_I },
	{ "syscall",	HWDC_S },
	{ "error",		HWDC_E },
	{ "debug",		HWDC_D },
};

static int esirisc_find_hwdc_mask(const char *name)
{
	for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
		if (strcmp(esirisc_hwdc_masks[i].name, name) == 0)
			return esirisc_hwdc_masks[i].mask;

	return -1;
}

COMMAND_HANDLER(handle_esirisc_hwdc_command)
{
	struct target *target = get_current_target(CMD_CTX);
	struct esirisc_common *esirisc = target_to_esirisc(target);

	if (CMD_ARGC > 0) {
		if (strcmp(CMD_ARGV[0], "all") == 0)
			esirisc->hwdc_save = HWDC_R | HWDC_I | HWDC_S | HWDC_E | HWDC_D;
		else {
			esirisc->hwdc_save = 0;
			if (strcmp(CMD_ARGV[0], "none") != 0) {
				while (CMD_ARGC-- > 0) {
					int mask = esirisc_find_hwdc_mask(CMD_ARGV[CMD_ARGC]);
					if (mask < 0) {
						LOG_ERROR("invalid mask: %s", CMD_ARGV[CMD_ARGC]);
						return ERROR_COMMAND_SYNTAX_ERROR;
					}
					esirisc->hwdc_save |= mask;
				}
			}
		}
	}

	for (size_t i = 0; i < ARRAY_SIZE(esirisc_hwdc_masks); ++i)
		command_print(CMD, "%9s: %s", esirisc_hwdc_masks[i].name,
				(esirisc->hwdc_save & esirisc_hwdc_masks[i].mask) ? "enabled" : "disabled");

	return ERROR_OK;
}

static const struct command_registration esirisc_exec_command_handlers[] = {
	{
		.name = "flush_caches",
		.handler = handle_esirisc_flush_caches_command,
		.mode = COMMAND_EXEC,
		.help = "flush instruction and data caches",
		.usage = "",
	},
	COMMAND_REGISTRATION_DONE
};

static const struct command_registration esirisc_any_command_handlers[] = {
	{
		.name = "cache_arch",
		.handler = handle_esirisc_cache_arch_command,
		.mode = COMMAND_ANY,
		.help = "configure cache architecture",
		.usage = "['harvard'|'von_neumann']",
	},
	{
		.name = "hwdc",
		.handler = handle_esirisc_hwdc_command,
		.mode = COMMAND_ANY,
		.help = "configure hardware debug control",
		.usage = "['all'|'none'|mask ...]",
	},
	{
		.chain = esirisc_exec_command_handlers
	},
	{
		.chain = esirisc_trace_command_handlers
	},
	COMMAND_REGISTRATION_DONE
};

static const struct command_registration esirisc_command_handlers[] = {
	{
		.name = "esirisc",
		.mode = COMMAND_ANY,
		.help = "eSi-RISC command group",
		.usage = "",
		.chain = esirisc_any_command_handlers,
	},
	COMMAND_REGISTRATION_DONE
};

struct target_type esirisc_target = {
	.name = "esirisc",

	.poll = esirisc_poll,
	.arch_state = esirisc_arch_state,

	.halt = esirisc_halt,
	.resume = esirisc_resume,
	.step = esirisc_step,

	.assert_reset = esirisc_assert_reset,
	.deassert_reset = esirisc_deassert_reset,

	.get_gdb_arch = esirisc_get_gdb_arch,
	.get_gdb_reg_list = esirisc_get_gdb_reg_list,

	.read_memory = esirisc_read_memory,
	.write_memory = esirisc_write_memory,
	.checksum_memory = esirisc_checksum_memory,

	.add_breakpoint = esirisc_add_breakpoint,
	.remove_breakpoint = esirisc_remove_breakpoint,
	.add_watchpoint = esirisc_add_watchpoint,
	.remove_watchpoint = esirisc_remove_watchpoint,

	.commands = esirisc_command_handlers,

	.target_create = esirisc_target_create,
	.init_target = esirisc_init_target,
	.examine = esirisc_examine,
};