aboutsummaryrefslogtreecommitdiff
path: root/gdb/rdi-share/ardi.c
blob: f7a391e52a1282d4d7dcdd427b0b650382c287da (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
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
/* 
 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
 * 
 * This software may be freely used, copied, modified, and distributed
 * provided that the above copyright notice is preserved in all copies of the
 * software.
 */

/*
 * ARDI.c
 * Angel Remote Debug Interface
 *
 *
 * $Revision$
 *     $Date$
 *
 * This file is based on /plg/pisd/rdi.c, but instead of using RDP it uses
 * ADP messages.
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define uint HIDE_HPs_uint
#include <signal.h>
#undef uint


#include "angel_endian.h"
#include "ardi.h"
#include "buffers.h"
#include "channels.h"
#include "hostchan.h"
#include "host.h"
#include "angel_bytesex.h"
#include "dbg_cp.h"
#include "adp.h"
#include "hsys.h"
#include "logging.h"
#include "msgbuild.h"
#include "rxtx.h"
#include "devsw.h"
#include "params.h"

#ifdef COMPILING_ON_WINDOWS
#  define IGNORE(x) (x = x)   /* must go after #includes to work on Windows */
#endif
#define NOT(x) (!(x))

#define ADP_INITIAL_TIMEOUT_PERIOD 5

static volatile int executing;
static int rdi_log = 0 ; /* debugging  ? */

/* we need a starting point for our first buffers, this is a safe one */
int Armsd_BufferSize = ADP_BUFFER_MIN_SIZE;
int Armsd_LongBufSize = ADP_BUFFER_MIN_SIZE;

#ifdef WIN32
  extern int interrupted;
  extern int swiprocessing;
#endif

static char dummycline = 0;
char *ardi_commandline = &dummycline ; /* exported in ardi.h */

extern unsigned int heartbeat_enabled;

static unsigned char *cpwords[16];

typedef struct stoppedProcListElement {
  struct stoppedProcListElement *next;
  angel_RDI_TargetStoppedProc *fn;
  void *arg;
} stoppedProcListElement;

static stoppedProcListElement *stopped_proc_list=NULL;

const struct Dbg_HostosInterface *angel_hostif;
static hsys_state *hstate;

static void angel_DebugPrint(const char *format, ...)
{ va_list ap;
  va_start(ap, format);
  angel_hostif->dbgprint(angel_hostif->dbgarg, format, ap);
  va_end(ap);
}

#ifdef RDI_VERBOSE
#define TracePrint(s) \
  if (rdi_log & 2) angel_DebugPrint("\n"); \
  if (rdi_log & 1) angel_DebugPrint s
#else
#define TracePrint(s)
#endif

typedef struct receive_dbgmsg_state {
  volatile int received;
  Packet *packet;
} receive_dbgmsg_state;

static receive_dbgmsg_state dbgmsg_state;

static void receive_debug_packet(Packet *packet, void *stateptr)
{
  receive_dbgmsg_state *state = stateptr;

  state->packet = packet;
  state->received = 1;
}

static int register_debug_message_handler(void)
{
  int err;
  dbgmsg_state.received = 0;

  err = Adp_ChannelRegisterRead(CI_HADP, receive_debug_packet, &dbgmsg_state);
#ifdef DEBUG
  if (err!=adp_ok) angel_DebugPrint("register_debug_message_handler failed %i\n", err);
#endif
  return err;
}


static int wait_for_debug_message(int *rcode, int *debugID,
                                  int *OSinfo1, int *OSinfo2,
                                  int *status, Packet **packet)
{
  unsigned int reason;

#ifdef DEBUG
  angel_DebugPrint("wait_for_debug_message waiting for %X\n", *rcode);
#endif

  for ( ; dbgmsg_state.received == 0 ; )
    Adp_AsynchronousProcessing(async_block_on_read);

#ifdef DEBUG
  angel_DebugPrint("wait_for_debug_message got packet\n");
#endif

  *packet = dbgmsg_state.packet;

  Adp_ChannelRegisterRead(CI_HADP, NULL, NULL);

  /*
   * TODO:
   * If ADP_Unrecognised return error.
   * If ADP_Acknowledge - handle appropriately.
   * If expected message read arguments and return RDIError_NoError.
   * Note: if RDIError occurs then the data values returned are junk
   */

  unpack_message(BUFFERDATA((*packet)->pk_buffer), "%w%w%w%w%w", &reason, debugID,
                 OSinfo1, OSinfo2, status);
  if (reason&0xffffff == ADP_HADPUnrecognised)
    return RDIError_UnimplementedMessage;
  if (reason != (unsigned ) *rcode) {
    if((reason&0xffffff) == ADP_HADPUnrecognised)
      return RDIError_UnimplementedMessage;
    else {
      angel_DebugPrint("ARDI ERROR: Expected reasoncode %x got reasoncode %x.\n",
             *rcode, reason);
      return RDIError_Error;
    }
  }
  else
    return RDIError_NoError;
  return RDIError_Error;    /* stop a pesky ANSI compiler warning */
}


/*
 * Handler and registration for logging messages from target
 */
static void TargetLogCallback( Packet *packet, void *state )
{
    p_Buffer     reply = BUFFERDATA(packet->pk_buffer);
    unsigned int len   = packet->pk_length;
    IGNORE(state);
    angel_hostif->write(angel_hostif->hostosarg,
                        (char *)reply, len - CHAN_HEADER_SIZE);
    DevSW_FreePacket(packet);

    packet = DevSW_AllocatePacket(4); /* better not ask for 0 */
    /* the reply is the ACK - any contents are ignored */
    if (packet != NULL)
       Adp_ChannelWrite( CI_TLOG, packet );
}

static void TargetLogInit( void )
{
    AdpErrs err = Adp_ChannelRegisterRead( CI_TLOG, TargetLogCallback, NULL );

#ifdef DEBUG
    if (err != adp_ok)
       angel_DebugPrint("CI_TLOG RegisterRead failed %d\n", err);
#else
    IGNORE(err);
#endif
}

/*----------------------------------------------------------------------*/
/*----angel_RDI_open-----------------------------------------------------*/
/*----------------------------------------------------------------------*/

typedef struct NegotiateState {
      bool             negotiate_resp;
      bool             negotiate_ack;
      bool             link_check_resp;
      ParameterConfig *accepted_config;
} NegotiateState;

static void receive_negotiate(Packet *packet, void *stateptr)
{
    unsigned reason, debugID, OSinfo1, OSinfo2, status;
    NegotiateState *n_state = (NegotiateState *)stateptr;
    p_Buffer reply = BUFFERDATA(packet->pk_buffer);

    unpack_message( reply, "%w%w%w%w",
                    &reason, &debugID, &OSinfo1, &OSinfo2 );
    reply += ADP_DEFAULT_HEADER_SIZE;

#ifdef DEBUG
    angel_DebugPrint( "receive_negotiate: reason %x\n", reason );
#endif

    switch ( reason )
    {
        case ADP_ParamNegotiate | TtoH:
        {
            n_state->negotiate_resp = TRUE;

            status = GET32LE( reply );
            reply += sizeof(word);
#ifdef DEBUG
            angel_DebugPrint( "ParamNegotiate status %u\n", status );
#endif
            if ( status == RDIError_NoError )
            {
                if ( Angel_ReadParamConfigMessage(
                         reply, n_state->accepted_config ) )
                   n_state->negotiate_ack = TRUE;
            }
            break;
        }

        case ADP_LinkCheck | TtoH:
        {
#ifdef DEBUG
            angel_DebugPrint( "PONG!\n" );
#endif
            n_state->link_check_resp = TRUE;
            break;
        }

        default:
        {
#ifdef DEBUG
            angel_DebugPrint( "Unexpected!\n" );
#endif
            break;
        }
    }
    DevSW_FreePacket( packet );
}

# include <sys/types.h>
#ifdef __unix
# include <sys/time.h>
#else
# include <time.h>
#endif

/*
 * convert a config into a single-valued options list
 */
static ParameterOptions *config_to_options( const ParameterConfig *config )
{
    unsigned int        num_params;
    size_t              size;
    ParameterOptions   *base_p;

    num_params  = config->num_parameters;
    size        =
        sizeof(ParameterOptions)
        + num_params*(sizeof(ParameterList) + sizeof(unsigned int));
    base_p      = malloc( size );

    if ( base_p != NULL )
    {
        unsigned int    u;
        ParameterList  *list_p          =
            (ParameterList *)((char *)base_p + sizeof(ParameterOptions));
        unsigned int   *option_p        =
            (unsigned int *)(list_p + num_params);

        base_p->num_param_lists = num_params;
        base_p->param_list = list_p;

        for ( u = 0; u < num_params; ++u )
        {
            option_p[u]                 = config->param[u].value;
            list_p[u].type              = config->param[u].type;
            list_p[u].num_options       = 1;
            list_p[u].option            = &option_p[u];
        }
    }

    return base_p;
}

static AdpErrs negotiate_params( const ParameterOptions *user_options )
{
    Packet                    *packet;
    unsigned int               count;
    static Parameter           params[AP_NUM_PARAMS];
    static ParameterConfig     accepted_config = { AP_NUM_PARAMS, params };

    time_t t;

    static volatile NegotiateState    n_state = {
        FALSE, FALSE, FALSE, &accepted_config };

#ifdef DEBUG
    angel_DebugPrint( "negotiate_params\n" );
#endif

    Adp_ChannelRegisterRead( CI_HBOOT, receive_negotiate, (void *)&n_state );

    packet = (Packet *)DevSW_AllocatePacket(Armsd_BufferSize);
    count = msgbuild( BUFFERDATA(packet->pk_buffer), "%w%w%w%w",
                      ADP_ParamNegotiate | HtoT, 0,
                      ADP_HandleUnknown, ADP_HandleUnknown );
    count += Angel_BuildParamOptionsMessage(
        BUFFERDATA(packet->pk_buffer) + count, user_options );
    packet->pk_length = count;
    Adp_ChannelWriteAsync( CI_HBOOT, packet );

#ifdef DEBUG
    angel_DebugPrint( "sent negotiate packet\n" );
#endif

    t=time(NULL);

    do {
      Adp_AsynchronousProcessing(async_block_on_nothing);

      if ((time(NULL)-t) > ADP_INITIAL_TIMEOUT_PERIOD) {
        return adp_timeout_on_open;
      }
    } while ( ! n_state.negotiate_resp );

    if ( n_state.negotiate_ack )
    {
        /* select accepted config */
        Adp_Ioctl( DC_SET_PARAMS, (void *)n_state.accepted_config );

        /*
         * 960430 KWelton
         *
         * There is a race in the renegotiation protocol: the
         * target has to have had time to load new config before
         * we send the link check packet - insert a deliberate
         * pause (100ms) to give the target some time
         */
        Adp_delay(100000);

        /* do link check */
        msgsend( CI_HBOOT, "%w%w%w%w", ADP_LinkCheck | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown );
#ifdef DEBUG
        angel_DebugPrint("sent link check\n");
#endif

        do {
            Adp_AsynchronousProcessing(async_block_on_read);
        } while ( ! n_state.link_check_resp );
        Adp_initSeq();
    }
    return adp_ok;
}

static int late_booted = FALSE;
static bool ardi_handler_installed = FALSE;

#ifdef __unix
static struct sigaction old_action;
#else
static void (*old_handler)();
#endif

static bool boot_interrupted = FALSE;

static void ardi_sigint_handler(int sig) {
#ifdef DEBUG
    if (sig != SIGINT)
       angel_DebugPrint("Expecting SIGINT got %d.\n", sig);
#else
    IGNORE(sig);
#endif
    boot_interrupted = TRUE;
#ifndef __unix
    signal(SIGINT, ardi_sigint_handler);
#endif
}

static void install_ardi_handler( void ) {
  if (!ardi_handler_installed) {
    /* install a new Ctrl-C handler so we can abandon waiting */
#ifdef __unix
    struct sigaction new_action;
    sigemptyset(&new_action.sa_mask);
    new_action.sa_handler = ardi_sigint_handler;
    new_action.sa_flags = 0;
    sigaction(SIGINT, &new_action, &old_action);
#else
    old_handler = signal(SIGINT, ardi_sigint_handler);
#endif
    ardi_handler_installed = TRUE;
  }
}

static int angel_RDI_errmess(char *buf, int blen, int errnum);

static void receive_reset_acknowledge(Packet *packet, void *stateptr) {
  unsigned reason, debugID, OSinfo1, OSinfo2, status;
  IGNORE(stateptr);

  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason, &debugID,
                 &OSinfo1, &OSinfo2, &status);
  if (reason==(ADP_Reset | TtoH) && status==AB_NORMAL_ACK) {
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Successfully received normal reset acknowledgement\n");
    late_booted = FALSE;
#endif
  } else if (reason==(ADP_Reset | TtoH) && status==AB_LATE_ACK) {
    char late_msg[AdpMessLen_LateStartup];
    int  late_len;
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Successfully received LATE reset acknowledgement\n");
#endif
    late_booted = TRUE;
    install_ardi_handler();
    late_len = angel_RDI_errmess(late_msg,
                                 AdpMessLen_LateStartup, adp_late_startup);
    angel_hostif->write(angel_hostif->hostosarg, late_msg, late_len);
  } else {
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Bad reset ack: reason=%8X, status=%8X\n", reason, status);
#endif
  }
  DevSW_FreePacket(packet);
}

static int booted_not_received;
static unsigned int angel_version;
static unsigned int adp_version;
static unsigned int arch_info;
static unsigned int cpu_info;
static unsigned int hw_status;

static void receive_booted(Packet *packet, void *stateptr) {
  unsigned reason, debugID, OSinfo1, OSinfo2, banner_length, bufsiz, longsiz;
  unsigned i, count;

  IGNORE(stateptr);

  count = unpack_message(BUFFERDATA(packet->pk_buffer),
                         "%w%w%w%w%w%w%w%w%w%w%w%w",
                 &reason, &debugID, &OSinfo1, &OSinfo2, &bufsiz, &longsiz,
                 &angel_version, &adp_version,
                 &arch_info, &cpu_info, &hw_status, &banner_length);
  if (reason==(ADP_Booted | TtoH)) {
#ifdef MONITOR_DOWNLOAD_PACKETS
    angel_DebugPrint("DEBUG: Successfully received Booted\n");
    angel_DebugPrint("       cpu_info=%8X, hw_status=%8X, bufsiz=%d, longsiz=%d\n",
           cpu_info, hw_status, bufsiz, longsiz);
#endif
    /* Get the banner from the booted message */
    for (i=0; i<banner_length; i++)
      angel_hostif->writec(angel_hostif->hostosarg,
                          (BUFFERDATA(packet->pk_buffer)+count)[i]);

    booted_not_received=0;
#ifndef NO_HEARTBEAT
    heartbeat_enabled = TRUE;
#endif
    Armsd_BufferSize = bufsiz + CHAN_HEADER_SIZE;
    Armsd_LongBufSize = longsiz + CHAN_HEADER_SIZE;
  } else {
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Bad Booted msg: reason=%8X\n", reason);
#endif
  }
  DevSW_FreePacket(packet);
}


/* forward declaration */
static int angel_negotiate_defaults( void );

/* Open communications. */
int angel_RDI_open(
    unsigned type, Dbg_ConfigBlock const *config,
    Dbg_HostosInterface const *hostif, struct Dbg_MCState *dbg_state)
{
  Packet *packet;
  int status, reasoncode, debugID, OSinfo1, OSinfo2, err;
  ParameterOptions *user_options = NULL;

  time_t t;

  IGNORE( dbg_state );

  if ((type & 1) == 0) {
    /* cold start */
    if (hostif != NULL) {
      angel_hostif = hostif;
      err = HostSysInit(hostif, &ardi_commandline, &hstate);
      if (err != RDIError_NoError) {
#ifdef DEBUG
        angel_DebugPrint("DEBUG: HostSysInit error %i\n",err);
#endif
        return err;
      }
    }
    TargetLogInit();
  }

#ifdef DEBUG
  angel_DebugPrint("DEBUG: Buffer allocated in angel_RDI_open(type=%i).\n",type);
#endif

  if ((type & 1) == 0) {
    /* cold start */
    unsigned endian;
    Adp_Ioctl( DC_GET_USER_PARAMS, (void *)&user_options );
    if ( user_options != NULL ) {
      err = negotiate_params( user_options );
      if (err != adp_ok) return err;
    }
    else {
      ParameterConfig *default_config = NULL;
      Adp_Ioctl( DC_GET_DEFAULT_PARAMS, (void *)&default_config );
      if ( default_config != NULL ) {
        ParameterOptions *default_options = config_to_options(default_config);
        err = negotiate_params( default_options );
        if (err != adp_ok) return err;
      }
    }

    /* Register handlers before sending any messages */
    booted_not_received=1;
    Adp_ChannelRegisterRead(CI_HBOOT, receive_reset_acknowledge, NULL);
    Adp_ChannelRegisterRead(CI_TBOOT, receive_booted, NULL);
    endian = 0;
    if (config!=NULL) {
      if (config->bytesex & RDISex_Little) endian |= ADP_BootHostFeature_LittleEnd;
      if (config->bytesex & RDISex_Big) endian |= ADP_BootHostFeature_BigEnd;
    }
    msgsend(CI_HBOOT,"%w%w%w%w%w", ADP_Reset | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown, endian);
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Transmitted Reset message in angel_RDI_open.\n");
#endif

    /* We will now either get an acknowledgement for the Reset message
     * or if the target was started after the host, we will get a
     * rebooted message first.
     */

#ifdef DEBUG
    angel_DebugPrint("DEBUG: waiting for a booted message\n");
#endif

    {
      boot_interrupted = FALSE;

      if (late_booted)
        install_ardi_handler();

      t=time(NULL);

      do {
        Adp_AsynchronousProcessing(async_block_on_nothing);
        if ((time(NULL)-t) > ADP_INITIAL_TIMEOUT_PERIOD && !late_booted) {
          return adp_timeout_on_open;
        }
      } while (booted_not_received && !boot_interrupted);

      if (ardi_handler_installed)
      {
        /* uninstall our Ctrl-C handler */
#ifdef __unix
        sigaction(SIGINT, &old_action, NULL);
#else
        signal(SIGINT, old_handler);
#endif
      }

      if (boot_interrupted) {
        angel_negotiate_defaults();
        return adp_abandon_boot_wait;
      }
    }

    booted_not_received=1;
    Adp_ChannelRegisterRead(CI_HBOOT, NULL, NULL);

    /* Leave the booted handler installed */
    msgsend(CI_TBOOT, "%w%w%w%w%w", ADP_Booted | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown, 0);
    Adp_initSeq();
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Transmitted ADP_Booted acknowledgement.\n");
    angel_DebugPrint("DEBUG: Boot sequence completed, leaving angel_RDI_open.\n");
#endif

    return (hw_status & ADP_CPU_BigEndian )? RDIError_BigEndian :
      RDIError_LittleEndian;
  }
  else {
    /* warm start */
    register_debug_message_handler();

    msgsend(CI_HADP, "%w%w%w%w",
            ADP_InitialiseApplication | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown);
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Transmitted Initialise Application\n");
#endif
    reasoncode=ADP_InitialiseApplication | TtoH;
    err = wait_for_debug_message(&reasoncode, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet);
    if (err != RDIError_NoError) return err;
    return status;
  }
  return -1;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_close----------------------------------------------------*/
/*----------------------------------------------------------------------*/

static int angel_negotiate_defaults( void ) {
    int err = adp_ok;
    ParameterConfig *default_config = NULL;
    Adp_Ioctl( DC_GET_DEFAULT_PARAMS, (void *)&default_config );
    if ( default_config != NULL ) {
        ParameterOptions *default_options = config_to_options(default_config);
        err = negotiate_params( default_options );
        free( default_options );
    }
    return err;
}

int angel_RDI_close(void) {
/*Angel host exit */
  int err;
  int status,debugID, OSinfo1,OSinfo2;
  int reason;
  Packet *packet = NULL;;
#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_Close.\n");
#endif

  register_debug_message_handler();

  heartbeat_enabled = FALSE;

  err = msgsend(CI_HADP,"%w%w%w%w",ADP_End | HtoT,0,
          ADP_HandleUnknown, ADP_HandleUnknown);
  if (err != RDIError_NoError) return err;
  reason = ADP_End | TtoH;
  err =  wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                  &status, &packet);
  DevSW_FreePacket(packet);
  if (err != RDIError_NoError) return err;
  if (status == RDIError_NoError) {
    err = angel_negotiate_defaults();
    if (err != adp_ok) return err;
    Adp_Ioctl( DC_RESET, NULL ); /* just to be safe */
    return HostSysExit(hstate);
  }
  else
      return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_read-----------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Read memory contents from target to host: use ADP_Read */
int angel_RDI_read(ARMword source, void *dest, unsigned *nbytes)
{
  Packet *packet=NULL;
  int len;                               /* Integer to hold message length. */
  unsigned int nbtogo = *nbytes, nbinpacket, nbdone=0;
  int rnbytes = 0, status, reason, debugID, OSinfo1, OSinfo2, err;
  unsigned int maxlen = Armsd_BufferSize-CHAN_HEADER_SIZE-ADP_ReadHeaderSize;

  /* Print debug trace information, this is just copied straight from rdi.c
     and I can see no reason why it should have to be changed. */
  TracePrint(("angel_RDI_read: source=%.8lx dest=%p nbytes=%.8x\n",
                (unsigned long)source, dest, *nbytes));
  if (*nbytes == 0) return RDIError_NoError;       /* Read nothing - easy! */
  /* check the buffer size */
  while (nbtogo >0) {
    register_debug_message_handler();

    nbinpacket = (nbtogo <= maxlen) ? nbtogo : maxlen;
    len = msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Read | HtoT, 0,
                  ADP_HandleUnknown, ADP_HandleUnknown, source+nbdone,
                  nbinpacket);
    reason=ADP_Read | TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet);
    TracePrint(("angel_RDI_read: nbinpacket =%d status=%08x err = %d\n",
                nbinpacket,status,err));
    if (err != RDIError_NoError) return err;       /* Was there an error? */
    if (status == RDIError_NoError){
      rnbytes += PREAD(LE,(unsigned int *)(BUFFERDATA(packet->pk_buffer)+20));
      TracePrint(("angel_RDI_read: rnbytes = %d\n",rnbytes));
      memcpy(((unsigned char *)dest)+nbdone, BUFFERDATA(packet->pk_buffer)+24,
             nbinpacket);
    }
    nbdone += nbinpacket;
    nbtogo -= nbinpacket;
  }
  *nbytes -= rnbytes;
  return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_write----------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Transfer memory block from host to target.  Use ADP_Write>. */
int angel_RDI_write(const void *source, ARMword dest, unsigned *nbytes)
{
  Packet *packet;/* Message buffers. */
  unsigned int len, nbtogo = *nbytes, nboffset = 0, nbinpacket;
  int status, reason, debugID, OSinfo1, OSinfo2, err;
  unsigned int maxlen = Armsd_LongBufSize-CHAN_HEADER_SIZE-ADP_WriteHeaderSize;

  TracePrint(("angel_RDI_write: source=%p dest=%.8lx nbytes=%.8x\n",
                 source, (unsigned long)dest, *nbytes));

  if (*nbytes == 0) return RDIError_NoError;

  *nbytes = 0;
  while (nbtogo > 0) {
    packet = (Packet *) DevSW_AllocatePacket(Armsd_LongBufSize);
    nbinpacket = (nbtogo <= maxlen) ? nbtogo : maxlen;
    len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
                   ADP_Write | HtoT, 0, ADP_HandleUnknown,
                   ADP_HandleUnknown, dest+nboffset, nbinpacket);
    /* Copy the data into the packet. */

    memcpy(BUFFERDATA(packet->pk_buffer)+len,
           ((const unsigned char *) source)+nboffset, nbinpacket);
    nboffset += nbinpacket;
    packet->pk_length = nbinpacket+len;

#ifdef MONITOR_DOWNLOAD_PACKETS
    angel_DebugPrint("angel_RDI_write packet size=%i, bytes done=%i\n",
            nbinpacket, nboffset);
#endif

    register_debug_message_handler();
    Adp_ChannelWrite(CI_HADP, packet);
    reason=ADP_Write | TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet);
    nbtogo -= nbinpacket;
    if (err != RDIError_NoError) return err;
    if (status == RDIError_NoError)
      *nbytes += nbinpacket;

    DevSW_FreePacket(packet);
  }
  return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_CPUread--------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Reads the values of registers in the CPU, uses ADP_CPUwrite. */
int angel_RDI_CPUread(unsigned mode, unsigned long mask, ARMword *buffer)
{
  unsigned int i, j;
  Packet *packet = NULL;
  int err, status, reason, debugID, OSinfo1, OSinfo2;
#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_CPUread.\n");
#endif
  for (i=0, j=0 ; i < RDINumCPURegs ; i++)
    if (mask & (1L << i)) j++;            /* Count the number of registers. */

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%c%w", ADP_CPUread | HtoT, 0,
          ADP_HandleUnknown, ADP_HandleUnknown, mode, mask);
  reason = ADP_CPUread | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;
  }
  if(status == RDIError_NoError) {
    for (i=0; i<j; i++)
      buffer[i] = GET32LE(BUFFERDATA(packet->pk_buffer)+20+(i*4));
    TracePrint(("angel_RDI_CPUread: mode=%.8x mask=%.8lx", mode, mask));
    DevSW_FreePacket(packet);
#ifdef RDI_VERBOSE
    if (rdi_log & 1) {
      unsigned k;
      for (k = 0, j = 0 ; j <= 20 ; j++)
        if (mask & (1L << j)) {
          angel_DebugPrint("%c%.8lx",k%4==0?'\n':' ',
                           (unsigned long)buffer[k]);
          k++ ;
        }
      angel_DebugPrint("\n") ;
    }
#endif

  }
  return status;
}

/*----------------------------------------------------------------------*/
/*----angel_RDI_CPUwrite-------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Write CPU registers: use ADP_CPUwrite. */
int angel_RDI_CPUwrite(unsigned mode, unsigned long mask,
                      ARMword const *buffer){

  unsigned i, j, c;
  Packet *packet;
  int status, reason, debugID, OSinfo1, OSinfo2, err, len;

  TracePrint(("angel_RDI_CPUwrite: mode=%.8x mask=%.8lx", mode, mask));
#ifdef RDI_VERBOSE
 if (rdi_log & 1) {
    for (j = 0, i = 0 ; i <= 20 ; i++)
       if (mask & (1L << i)) {
          angel_DebugPrint("%c%.8lx",j%4==0?'\n':' ',
                           (unsigned long)buffer[j]);
          j++ ;
          }
    angel_DebugPrint("\n") ;
    }
#endif
 packet = (Packet *)DevSW_AllocatePacket(Armsd_BufferSize);
 for (i=0, j=0; i < RDINumCPURegs ; i++)
   if (mask & (1L << i)) j++; /* count the number of registers */

 len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%b%w",
                ADP_CPUwrite | HtoT, 0,
                ADP_HandleUnknown, ADP_HandleUnknown, mode, mask);
 for(c=0; c<j; c++)
   PUT32LE(BUFFERDATA(packet->pk_buffer)+len+(c*4), buffer[c]);
 packet->pk_length = len+(j*4);
 register_debug_message_handler();

 Adp_ChannelWrite(CI_HADP, packet);
 reason = ADP_CPUwrite | TtoH;
 err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                             &status, &packet);
 unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", &reason, &debugID,
                &OSinfo1, &OSinfo2, &status);
 DevSW_FreePacket(packet);
 if (err != RDIError_NoError)
   return err;      /* Was there an error? */
 else
   return status;
 }


/*----------------------------------------------------------------------*/
/*----angel_RDI_CPread---------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Read coprocessor's internal state.  See dbg_cp.h for help.
 * Use ADP_CPRead.
 * It would appear that the correct behaviour at this point is to leave
 * the unpacking to a the caller and to simply copy the stream of data
 * words into the buffer
 */

int angel_RDI_CPread(unsigned CPnum, unsigned long mask, ARMword *buffer){
  Packet *packet = NULL;
  int i, j, status, reasoncode, OSinfo1, OSinfo2, err, debugID;
  unsigned char *rmap = cpwords[CPnum];
  int n;
#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_CPread.\n");
#endif
  if (rmap == NULL) return RDIError_UnknownCoPro;

  register_debug_message_handler();
  n = rmap[-1];
  msgsend(CI_HADP, "%w%w%w%w%b%w", ADP_CPread | HtoT, 0,
          ADP_HandleUnknown, ADP_HandleUnknown, CPnum, mask);
  reasoncode=ADP_CPread | TtoH;
  err = wait_for_debug_message(&reasoncode, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;          /* Was there an error? */
  }
  for (j=i=0; i < n ; i++) /* count the number of registers */
    if (mask & (1L << i)) {
      j++;
    }
  for (i=0; i<j; i++)
    buffer[i] = PREAD32(LE, BUFFERDATA(packet->pk_buffer) + 20 + (i*4));
  DevSW_FreePacket(packet);
  TracePrint(("angel_RDI_CPread: CPnum=%.8x mask=%.8lx\n", CPnum, mask));
#ifdef RDI_VERBOSE
  if (rdi_log & 1) {
    for (i = 0, j = 0; j < n ; j++) {
      if (mask & (1L << j)) {
        int nw = rmap[j];
        angel_DebugPrint("%2d ", j);
        while (--nw > 0)
          angel_DebugPrint("%.8lx ", (unsigned long)buffer[i++]);
        angel_DebugPrint("%.8lx\n", (unsigned long)buffer[i++]);
      }
    }
  }
#endif
  return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_CPwrite--------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Write coprocessor's internal state.  See dbg_cp.h for help. Use
 * ADP_CPwrite.
 */

int angel_RDI_CPwrite(unsigned CPnum, unsigned long mask,
                      ARMword const *buffer)
{
  Packet *packet = NULL;
  int i, j, len, status, reason, OSinfo1, OSinfo2, err, debugID;
  unsigned char *rmap = cpwords[CPnum];
  int n;

  if (rmap == NULL) return RDIError_UnknownCoPro;
  n = rmap[-1];

  TracePrint(("angel_RDI_CPwrite: CPnum=%d mask=%.8lx\n", CPnum, mask));

#ifdef RDI_VERBOSE
 if (rdi_log & 1) {
    for (i = 0, j = 0; j < n ; j++)
       if (mask & (1L << j)) {
          int nw = rmap[j];
          angel_DebugPrint("%2d ", j);
          while (--nw > 0)
             angel_DebugPrint("%.8lx ", (unsigned long)buffer[i++]);
          angel_DebugPrint("%.8lx\n", (unsigned long)buffer[i++]);
       }
 }
#endif

  for (j=i=0; i < n ; i++) /* Count the number of registers. */
    if (mask & (1L << i)) j++;
  packet = DevSW_AllocatePacket(Armsd_BufferSize);
  len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%c%w",
                 ADP_CPwrite | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown, CPnum, mask);
  for(i=0;  i<j; i++)
    len+=msgbuild(BUFFERDATA(packet->pk_buffer) + len, "%w", buffer[i]);
  packet->pk_length = len;
  register_debug_message_handler();
  Adp_ChannelWrite(CI_HADP, packet);    /* Transmit message. */
  reason=ADP_CPwrite | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  DevSW_FreePacket(packet);
  if (err != RDIError_NoError)
    return err;
  else
    return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_pointinq-------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Do test calls to ADP_SetBreak/ADP_SetWatch to see if resources exist to
   carry out request. */
int angel_RDI_pointinq(ARMword *address, unsigned type, unsigned datatype,
                       ARMword *bound)
{
  Packet *packet = NULL;
  int len, status, reason, OSinfo1, OSinfo2, err=RDIError_NoError;
       /* stop a compiler warning */
  int debugID, pointhandle;
  TracePrint(
      ("angel_RDI_pointinq: address=%.8lx type=%d datatype=%d bound=%.8lx ",
      (unsigned long)*address, type, datatype, (unsigned long)*bound));
       /* for a buffer.  */
  packet = DevSW_AllocatePacket(Armsd_BufferSize);
  len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%b",
                 ((datatype == 0) ? ADP_SetBreak : ADP_SetWatch) | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown, address, type);
  if (datatype == 0)
    len += msgbuild(BUFFERDATA(packet->pk_buffer) + 21, "%w", bound);
  else
    len += msgbuild(BUFFERDATA(packet->pk_buffer) + 21, "%b%w", datatype, bound);

  register_debug_message_handler();
  packet->pk_length = len;
  Adp_ChannelWrite(CI_HADP, packet);
  reason = ((datatype == 0) ? ADP_SetBreak : ADP_SetWatch | TtoH);
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;        /* Was there an error? */
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
                 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
                 &pointhandle, &address, &bound);
  DevSW_FreePacket(packet);
  return err;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_setbreak-------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Set a breakpoint: Use ADP_SetBreak */
int angel_RDI_setbreak(ARMword address, unsigned type, ARMword bound,
                      PointHandle *handle)
{
  int status, reason, OSinfo1, OSinfo2, err, debugID;
  int tmpval, tmpaddr, tmpbnd;
  Packet *packet;
  TracePrint(("angel_RDI_setbreak address=%.8lx type=%d bound=%.8lx \n",
              (unsigned long)address, type, (unsigned long)bound));

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%b%w",
          ADP_SetBreak| HtoT, 0,  ADP_HandleUnknown,
          ADP_HandleUnknown, address, type, bound);
  reason = ADP_SetBreak |TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;         /* Was there an error? */
  }
  /* Work around varargs problem... -sts */
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
                 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
                 &tmpval, &tmpaddr, &tmpbnd);
  *handle = tmpval;
  address = tmpaddr;
  bound = tmpbnd;
  DevSW_FreePacket(packet);
  if (status != RDIError_NoError) return status;
  TracePrint(("returns handle %.8lx\n", (unsigned long)*handle));
  return RDIError_NoError;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_clearbreak-----------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Clear a breakpoint: Use ADP_ClearBreak. */
int angel_RDI_clearbreak(PointHandle handle)
{
  Packet *packet = NULL;
  int status, reason, OSinfo1, OSinfo2, err, debugID;

  TracePrint(("angel_RDI_clearbreak: handle=%.8lx\n", (unsigned long)handle));

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w",
          ADP_ClearBreak| HtoT, 0,  ADP_HandleUnknown,
          ADP_HandleUnknown, handle);
  reason = ADP_ClearBreak|TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    angel_DebugPrint("***RECEIVE DEBUG MESSAGE RETURNED ERR = %d.\n", err);
    return err;          /* Was there an error? */
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w",  &reason,
                 &debugID, &OSinfo1, &OSinfo2, &status);
  DevSW_FreePacket(packet);
#ifdef DEBUG
  angel_DebugPrint("DEBUG: Clear Break completed OK.\n");
#endif
  return RDIError_NoError;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_setwatch-------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Set a watchpoint: use ADP_SetWatch. */
int angel_RDI_setwatch(ARMword address, unsigned type, unsigned datatype,
                       ARMword bound, PointHandle *handle)
{
  Packet *packet = NULL;
  int status, reason, OSinfo1, OSinfo2, err, debugID;

  TracePrint(("angel_RDI_setwatch: address=%.8lx type=%d bound=%.8lx ",
              (unsigned long)address, type, (unsigned long)bound));

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%b%b%w",
          ADP_SetWatch| HtoT, 0,  ADP_HandleUnknown,
          ADP_HandleUnknown, address, type, datatype, bound);

  reason = ADP_SetWatch | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;        /* Was there an error? */
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
                 &reason, &debugID, &OSinfo1, &OSinfo2, &status,
                 handle, &address, &bound);
  DevSW_FreePacket(packet);
  TracePrint(("returns handle %.8lx\n", (unsigned long)*handle));
  return RDIError_NoError;
}

/*----------------------------------------------------------------------*/
/*----angel_RDI_clearwatch-----------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Clear a watchpoint: use ADP_ClearWatch. */
int angel_RDI_clearwatch(PointHandle handle) {

  int status, reason, OSinfo1, OSinfo2, err, debugID;
  Packet *packet = NULL;

  TracePrint(("angel_RDI_clearwatch: handle=%.8lx\n", (unsigned long)handle));

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w",
          ADP_ClearWatch| HtoT, 0,  ADP_HandleUnknown,
          ADP_HandleUnknown, handle);
  reason = ADP_ClearWatch|TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;        /* Was there an error? */
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w",  &reason, &debugID,
                 &OSinfo1, &OSinfo2, &status);
  DevSW_FreePacket(packet);
  return RDIError_NoError;
}

typedef struct {
  unsigned stopped_reason;
  int stopped_status;
  int data;
} adp_stopped_struct;


int angel_RDI_OnTargetStopping(angel_RDI_TargetStoppedProc *fn,
                               void *arg)
{
  stoppedProcListElement **lptr = &stopped_proc_list;

  /* Find the address of the NULL ptr at the end of the list */
  for (; *lptr!=NULL ; lptr = &((*lptr)->next))
    ; /* Do nothing */

  *lptr = (stoppedProcListElement *) malloc(sizeof(stoppedProcListElement));
  if (*lptr == NULL) return RDIError_OutOfStore;
  (*lptr)->fn = fn;
  (*lptr)->arg = arg;

  return RDIError_NoError;
}

static int CallStoppedProcs(unsigned reason)
{
  stoppedProcListElement *p = stopped_proc_list;
  int err=RDIError_NoError;
  
  for (; p!=NULL ; p=p->next) {
    int local_err = p->fn(reason, p->arg);
    if (local_err != RDIError_NoError) err=local_err;
  }

  return err;
}

/*----------------------------------------------------------------------*/
/*----angel_RDI_execute--------------------------------------------------*/
/*----------------------------------------------------------------------*/

static int HandleStoppedMessage(Packet *packet, void *stateptr) {
  unsigned int err,  reason, debugID, OSinfo1, OSinfo2, count;
  adp_stopped_struct *stopped_info;
  stopped_info = (adp_stopped_struct *) stateptr;
  IGNORE(stateptr);
  count = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
                         &reason, &debugID,
                         &OSinfo1, &OSinfo2,
                         &stopped_info->stopped_reason, &stopped_info->data);
  DevSW_FreePacket(packet);

  if (reason != (ADP_Stopped | TtoH)) {
#ifdef DEBUG
    angel_DebugPrint("Expecting stopped message, got %x", reason);
#endif
    return RDIError_Error;
  }
  else {
    executing = FALSE;
#ifdef DEBUG
    angel_DebugPrint("Received stopped message.\n");
#endif
  }

  err = msgsend(CI_TADP,  "%w%w%w%w%w", (ADP_Stopped | HtoT), 0,
                ADP_HandleUnknown, ADP_HandleUnknown, RDIError_NoError);
#ifdef DEBUG
  angel_DebugPrint("Transmiting stopped acknowledge.\n");
#endif
  if (err != RDIError_NoError) angel_DebugPrint("Transmit failed.\n");
#ifdef DEBUG
  angel_DebugPrint("DEBUG: Stopped reason : %x\n", stopped_info->stopped_reason);
#endif
  switch (stopped_info->stopped_reason) {
  case ADP_Stopped_BranchThroughZero:
    stopped_info->stopped_status = RDIError_BranchThrough0;
    break;
  case ADP_Stopped_UndefinedInstr:
    stopped_info->stopped_status = RDIError_UndefinedInstruction;
    break;
  case ADP_Stopped_SoftwareInterrupt:
    stopped_info->stopped_status = RDIError_SoftwareInterrupt;
    break;
  case ADP_Stopped_PrefetchAbort:
    stopped_info->stopped_status = RDIError_PrefetchAbort;
    break;
  case ADP_Stopped_DataAbort:
    stopped_info->stopped_status = RDIError_DataAbort;
    break;
  case ADP_Stopped_AddressException:
    stopped_info->stopped_status = RDIError_AddressException;
    break;
  case ADP_Stopped_IRQ:
    stopped_info->stopped_status = RDIError_IRQ;
    break;
  case ADP_Stopped_BreakPoint:
    stopped_info->stopped_status = RDIError_BreakpointReached;
    break;
  case ADP_Stopped_WatchPoint:
    stopped_info->stopped_status = RDIError_WatchpointAccessed;
    break;
  case ADP_Stopped_StepComplete:
    stopped_info->stopped_status = RDIError_ProgramFinishedInStep;
    break;
  case ADP_Stopped_RunTimeErrorUnknown:
  case ADP_Stopped_StackOverflow:
  case ADP_Stopped_DivisionByZero:
    stopped_info->stopped_status = RDIError_Error;
    break;
  case ADP_Stopped_FIQ:
    stopped_info->stopped_status = RDIError_FIQ;
    break;
  case ADP_Stopped_UserInterruption:
  case ADP_Stopped_OSSpecific:
    stopped_info->stopped_status = RDIError_UserInterrupt;
    break;
  case ADP_Stopped_ApplicationExit:
    stopped_info->stopped_status = RDIError_NoError;
    break;
  default:
    stopped_info->stopped_status = RDIError_Error;
    break;
  }
  return RDIError_NoError;
}

static volatile bool interrupt_request = FALSE;

static void interrupt_target( void )
{
    Packet *packet = NULL;
    int err;
    int reason, debugID, OSinfo1, OSinfo2, status;

#ifdef DEBUG
    angel_DebugPrint("DEBUG: interrupt_target.\n");
#endif

    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w", ADP_InterruptRequest | HtoT, 0,
                   ADP_HandleUnknown, ADP_HandleUnknown);

    reason = ADP_InterruptRequest |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    DevSW_FreePacket(packet);
#ifdef DEBUG
    angel_DebugPrint("DEBUG: got interrupt ack ok err = %d, status=%i\n",
                     err, status);
#endif

    return;
}

#ifdef TEST_DC_APPL
  extern void test_dc_appl_handler( const DeviceDescr *device,
                                    Packet *packet );
#endif

/* Core functionality for execute and step */
static int angel_RDI_ExecuteOrStep(PointHandle *handle, word type, 
                                   unsigned ninstr)
{
  int err;
  adp_stopped_struct stopped_info;
  void* stateptr = (void *)&stopped_info;
  ChannelCallback HandleStoppedMessageFPtr=(ChannelCallback) HandleStoppedMessage;
  int status, reasoncode, debugID, OSinfo1, OSinfo2;
  Packet *packet = NULL;

  TracePrint(("angel_RDI_ExecuteOrStep\n"));

  err = Adp_ChannelRegisterRead(CI_TADP,
                                HandleStoppedMessageFPtr, stateptr);
  if (err != RDIError_NoError) {
#ifdef DEBUG
    angel_DebugPrint("TADP Register failed.\n");
#endif
    return err;
  }
  /* Set executing TRUE here, as it must be set up before the target has
   * had any chance at all to execute, or it may send its stopped message
   * before we get round to setting executing = TRUE !!!
   */
  executing = TRUE;

  register_debug_message_handler();

#ifdef TEST_DC_APPL
  Adp_Install_DC_Appl_Handler( test_dc_appl_handler );
#endif

#ifdef DEBUG
  angel_DebugPrint("Transmiting %s message.\n",
                   type == ADP_Execute ? "execute": "step");
#endif

  register_debug_message_handler();
  /* Extra ninstr parameter for execute message will simply be ignored */
  err = msgsend(CI_HADP,"%w%w%w%w%w", type | HtoT, 0,
                ADP_HandleUnknown, ADP_HandleUnknown, ninstr);
#if DEBUG
  if (err != RDIError_NoError) angel_DebugPrint("Transmit failed.\n");
#endif

  reasoncode = type | TtoH;
  err = wait_for_debug_message( &reasoncode, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet );
  if (err != RDIError_NoError)
     return err;
  else if (status != RDIError_NoError)
     return status;

#ifdef DEBUG
  angel_DebugPrint("Waiting for program to finish...\n");
#endif

  while( executing )
  {
      if (interrupt_request)
      {
        interrupt_target();
        interrupt_request = FALSE;
      }
      Adp_AsynchronousProcessing( async_block_on_nothing );
  }

#ifdef TEST_DC_APPL
  Adp_Install_DC_Appl_Handler( NULL );
#endif

  (void)Adp_ChannelRegisterRead(CI_TADP, NULL, NULL);

  *handle = (PointHandle)stopped_info.data;

  CallStoppedProcs(stopped_info.stopped_reason);

  return stopped_info.stopped_status;
}

/* Request that the target starts executing from the stored CPU state: use
   ADP_Execute. */
int angel_RDI_execute(PointHandle *handle)
{
    return angel_RDI_ExecuteOrStep(handle, ADP_Execute, 0);
}

#ifdef __WATCOMC__
typedef void handlertype(int);

static int interrupted=0;

static void myhandler(int sig) {
  IGNORE(sig);
  interrupted=1;
  signal(SIGINT, myhandler);
}
#endif

/*----------------------------------------------------------------------*/
/*----angel_RDI_step-----------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Step 'ninstr' through the code: use ADP_Step. */
int angel_RDI_step(unsigned ninstr, PointHandle *handle)
{
    int err = angel_RDI_ExecuteOrStep(handle, ADP_Step, ninstr);
    if (err == RDIError_ProgramFinishedInStep)
       return RDIError_NoError;
    else
       return err;
}


static void SetCPWords(int cpnum, struct Dbg_CoProDesc const *cpd) {
  int i, rmax = 0;
  for (i = 0; i < cpd->entries; i++)
    if (cpd->regdesc[i].rmax > rmax)
      rmax = cpd->regdesc[i].rmax;

  { unsigned char *rmap = (unsigned char *)malloc(rmax + 2);
    *rmap++ = rmax + 1;
    for (i = 0; i < cpd->entries; i++) {
      int r;
      for (r = cpd->regdesc[i].rmin; r <= cpd->regdesc[i].rmax; r++)
        rmap[r] = (cpd->regdesc[i].nbytes+3) / 4;
      }
/*    if (cpwords[cpnum] != NULL) free(cpwords[cpnum]); */
    cpwords[cpnum] = rmap;
  }
}

/*----------------------------------------------------------------------*/
/*----angel_RDI_info-----------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Use ADP_Info, ADP_Ctrl and ADP_Profile calls to implement these,
   see adp.h for more details. */

static int angel_cc_exists( void )
{
  Packet *packet = NULL;
  int err;
  int reason, debugID, OSinfo1, OSinfo2, subreason, status;

#ifdef DEBUG
  angel_DebugPrint("DEBUG: ADP_ICEB_CC_Exists.\n");
#endif

  if ( angel_RDI_info( RDIInfo_Icebreaker, NULL, NULL ) == RDIError_NoError ) {
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w", ADP_ICEbreakerHADP | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown,
            ADP_ICEB_CC_Exists );
    reason = ADP_ICEbreakerHADP |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                   &debugID, &OSinfo1, &OSinfo2,  &subreason, &status);
    if (subreason !=  ADP_ICEB_CC_Exists) {
      DevSW_FreePacket(packet);
      return RDIError_Error;
    }
    else
      return status;
  }
  else
    return RDIError_UnimplementedMessage;
}

typedef struct {
  RDICCProc_ToHost *tohost; void *tohostarg;
  RDICCProc_FromHost *fromhost; void *fromhostarg;
  bool registered;
} CCState;
static CCState ccstate = { NULL, NULL, NULL, NULL, FALSE };

static void HandleDCCMessage( Packet *packet, void *stateptr )
{
  unsigned int reason, debugID, OSinfo1, OSinfo2;
  int count;
  CCState *ccstate_p = (CCState *)stateptr;

  count = unpack_message( BUFFERDATA(packet->pk_buffer), "%w%w%w%w",
                          &reason, &debugID, &OSinfo1, &OSinfo2 );
  switch ( reason )
  {
      case ADP_TDCC_ToHost | TtoH:
      {
           /* only handles a single word of data, for now */

          unsigned int nbytes, data;

          unpack_message( BUFFERDATA(packet->pk_buffer)+count, "%w%w",
                          &nbytes, &data );
#ifdef DEBUG
          angel_DebugPrint( "DEBUG: received CC_ToHost message: nbytes %d data %08x.\n",
                  nbytes, data );
#endif
          ccstate_p->tohost( ccstate_p->tohostarg, data );
          msgsend(CI_TTDCC, "%w%w%w%w%w",
                  ADP_TDCC_ToHost | HtoT, debugID, OSinfo1, OSinfo2,
                  RDIError_NoError );
          break;
      }

      case ADP_TDCC_FromHost | TtoH:
      {
           /* only handles a single word of data, for now */

          int valid;
          ARMword data;

          ccstate_p->fromhost( ccstate_p->fromhostarg, &data, &valid );
#ifdef DEBUG
          angel_DebugPrint( "DEBUG: received CC_FromHost message, returning: %08x %s.\n",
                  data, valid ? "VALID" : "INvalid" );
#endif
          msgsend(CI_TTDCC, "%w%w%w%w%w%w%w",
                  ADP_TDCC_FromHost | HtoT, debugID, OSinfo1, OSinfo2,
                  RDIError_NoError, valid ? 1 : 0, data );
          break;
      }

      default:
#ifdef DEBUG
      angel_DebugPrint( "Unexpected TDCC message %08x received\n", reason );
#endif
      break;
  }
  DevSW_FreePacket(packet);
  return;
}

static void angel_check_DCC_handler( CCState *ccstate_p )
{
    int err;

    if ( ccstate_p->tohost != NULL || ccstate_p->fromhost != NULL )
    {
        /* doing DCC, so need a handler */
        if ( ! ccstate_p->registered )
        {
#ifdef DEBUG
            angel_DebugPrint( "Registering handler for TTDCC channel.\n" );
#endif
            err = Adp_ChannelRegisterRead( CI_TTDCC, HandleDCCMessage,
                                           ccstate_p );
            if ( err == adp_ok )
               ccstate_p->registered = TRUE;
#ifdef DEBUG
            else
               angel_DebugPrint( "angel_check_DCC_handler: register failed!\n" );
#endif
        }
    }
    else
    {
        /* not doing DCC, so don't need a handler */
        if ( ccstate_p->registered )
        {
#ifdef DEBUG
            angel_DebugPrint( "Unregistering handler for TTDCC channel.\n" );
#endif
            err = Adp_ChannelRegisterRead( CI_TTDCC, NULL, NULL );
            if ( err == adp_ok )
               ccstate_p->registered = FALSE;
#ifdef DEBUG
            else
               angel_DebugPrint( "angel_check_DCC_handler: unregister failed!\n" );
#endif
        }
    }
}


static int CheckSubMessageReply(int reason, int subreason) {
  Packet *packet = NULL;
  int status, debugID, OSinfo1, OSinfo2;
  int err = RDIError_NoError;
  reason |= TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                               &status, &packet);
  if (err != RDIError_NoError) {
    status = err;
  } else {
    int sr;
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                   &OSinfo1, &OSinfo2, &sr, &status);
    if (subreason != sr) status = RDIError_Error;
  }
  DevSW_FreePacket(packet);
  return status;
}

static int SendSubMessageAndCheckReply(int reason, int subreason) {
  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w", reason | HtoT, 0,
          ADP_HandleUnknown, ADP_HandleUnknown,
          subreason);
  return CheckSubMessageReply(reason, subreason);
}

static int SendSubMessageWordAndCheckReply(int reason, int subreason, ARMword word) {
  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%w", reason | HtoT, 0,
          ADP_HandleUnknown, ADP_HandleUnknown,
          subreason, word);
  return CheckSubMessageReply(reason, subreason);
}

static int SendSubMessageGetWordAndCheckReply(int reason, int subreason, ARMword *resp) {
  Packet *packet = NULL;
  int status, debugID, OSinfo1, OSinfo2;
  int err = RDIError_NoError;

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w", reason | HtoT, 0,
          ADP_HandleUnknown, ADP_HandleUnknown,
          subreason);
  reason |= TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                               &status, &packet);
  if (err != RDIError_NoError) {
    status = err;
  } else {
    int sr;
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w", &reason, &debugID,
                   &OSinfo1, &OSinfo2,  &sr, &status, resp);
    if (subreason != sr) status = RDIError_Error;
  }
  DevSW_FreePacket(packet);
  return status;
}

static int const hostsex = 1;

int angel_RDI_info(unsigned type, ARMword *arg1, ARMword *arg2) {
  Packet *packet = NULL;
  int len, status, c, reason, subreason, debugID, OSinfo1, OSinfo2;
  int err=RDIError_NoError, cpnum=0;
  struct Dbg_CoProDesc *cpd;
  int count, i;
  unsigned char *bp;

#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_info.\n");
#endif
  switch (type) {
  case RDIInfo_Target:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_Target.\n");
#endif

    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown, ADP_Info_Target);
    reason = ADP_Info |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
                   &debugID, &OSinfo1, &OSinfo2,  &subreason, &status,
                   arg1, arg2);
    DevSW_FreePacket(packet);

    if (subreason !=  ADP_Info_Target)
      return RDIError_Error;
    else
      return status;

  case RDISignal_Stop:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDISignal_Stop.\n");
    if (interrupt_request)
       angel_DebugPrint("       STILL WAITING to send previous interrupt request\n");
#endif
    interrupt_request = TRUE;
    return RDIError_NoError;

  case RDIInfo_Points:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_Points.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_Points, arg1);

  case RDIInfo_Step:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_Step.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_Step, arg1);

  case RDISet_Cmdline:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDISet_Cmdline.\n");
#endif
    if (ardi_commandline != &dummycline)
      free(ardi_commandline);
    ardi_commandline = (char *)malloc(strlen((char*)arg1) + 1) ;
    (void)strcpy(ardi_commandline, (char *)arg1) ;
    return RDIError_NoError;

  case RDIInfo_SetLog:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_SetLog.\n");
#endif
    rdi_log = (int) *arg1;
    return RDIError_NoError;

  case RDIInfo_Log:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_Log.\n");
#endif
    *arg1 = rdi_log;
    return RDIError_NoError;


  case RDIInfo_MMU:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_MMU.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Info, ADP_Info_MMU, arg1);

  case RDIInfo_SemiHosting:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_SemiHosting.\n");
#endif
    return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_SemiHosting);

  case RDIInfo_CoPro:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_CoPro.\n");
#endif
    return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_CoPro);

  case RDICycles:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDICycles.\n");
#endif
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown, ADP_Info_Cycles);
    reason = ADP_Info |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                 &OSinfo1, &OSinfo2,  &subreason, &status);
    DevSW_FreePacket(packet);
    if (subreason !=  ADP_Info_Cycles)
      return RDIError_Error;
    if (status != RDIError_NoError) return status;
    for (c=0; c<12; c++)
      arg1[c]=GET32LE(BUFFERDATA(packet->pk_buffer)+24+(c*4));
    return status;

  case RDIInfo_DescribeCoPro:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_DescribeCoPro.\n");
#endif
    cpnum = *(int *)arg1;
    cpd = (struct Dbg_CoProDesc *)arg2;
    packet = DevSW_AllocatePacket(Armsd_BufferSize);
    if (angel_RDI_info(ADP_Info_CoPro, NULL, NULL) != RDIError_NoError)
      return RDIError_Error;
    len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", ADP_Info | HtoT, 0,
                   ADP_HandleUnknown, ADP_HandleUnknown,
                   ADP_Info_DescribeCoPro);
    len +=msgbuild(BUFFERDATA(packet->pk_buffer)+20, "%b%b%b%b%b", cpnum,
                   cpd->regdesc[cpnum].rmin, cpd->regdesc[cpnum].rmax,
                   cpd->regdesc[cpnum].nbytes, cpd->regdesc[cpnum].access);
    if (cpd->regdesc[cpnum].access&0x3 == 0x3){
      len += msgbuild(BUFFERDATA(packet->pk_buffer)+25, "%b%b%b%b%b",
                      cpd->regdesc[cpnum].accessinst.cprt.read_b0,
                      cpd->regdesc[cpnum].accessinst.cprt.read_b1,
                      cpd->regdesc[cpnum].accessinst.cprt.write_b0,
                      cpd->regdesc[cpnum].accessinst.cprt.write_b1, 0xff);
    }
    else {
      len += msgbuild(BUFFERDATA(packet->pk_buffer)+25, "%b%b%b%b%b%",
                      cpd->regdesc[cpnum].accessinst.cpdt.rdbits,
                      cpd->regdesc[cpnum].accessinst.cpdt.nbit,0,0, 0xff);
    }
    register_debug_message_handler();
    packet->pk_length = len;
    Adp_ChannelWrite(CI_HADP, packet); /* Transmit message. */
    reason = ADP_Info |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                   &OSinfo1, &OSinfo2, &subreason, &status);
    DevSW_FreePacket(packet);
    if (subreason != ADP_Info_DescribeCoPro)
      return RDIError_Error;
    else
      return status;

  case RDIInfo_RequestCoProDesc:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: RDIInfo_RequestCoProDesc.\n");
#endif
    cpnum = *(int *)arg1;
    cpd = (struct Dbg_CoProDesc *)arg2;
    packet = DevSW_AllocatePacket(Armsd_BufferSize);
    len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w", ADP_Info | HtoT, 0,
                   ADP_HandleUnknown, ADP_HandleUnknown,
                   ADP_Info_RequestCoProDesc);
    len += msgbuild(BUFFERDATA(packet->pk_buffer)+20, "%b", *(int *)arg1);
    packet->pk_length = len;
    register_debug_message_handler();
    Adp_ChannelWrite(CI_HADP, packet); /* Transmit message. */
    reason = ADP_Info |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    count = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                           &debugID, &OSinfo1, &OSinfo2,  &subreason, &status);
    if (subreason !=  ADP_Info_RequestCoProDesc) {
      DevSW_FreePacket(packet);
      return RDIError_Error;
    } else if ( status != RDIError_NoError ) {
      DevSW_FreePacket(packet);
      return status;
    } else {
      bp = BUFFERDATA(packet->pk_buffer)+count;
      for ( i = 0; *bp != 0xFF && i < cpd->entries; ++i ) {
        cpd->regdesc[i].rmin = *bp++;
        cpd->regdesc[i].rmax = *bp++;
        cpd->regdesc[i].nbytes = *bp++;
        cpd->regdesc[i].access = *bp++;
      }
      cpd->entries = i;
      if ( *bp != 0xFF )
        status = RDIError_BufferFull;
      else
        SetCPWords( cpnum, cpd );
      DevSW_FreePacket(packet);
      return status;
    }

  case RDIInfo_GetLoadSize:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Info_AngelBufferSize.\n");
#endif
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w", ADP_Info | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown,
            ADP_Info_AngelBufferSize);
    reason = ADP_Info |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                   &debugID, &OSinfo1, &OSinfo2,  &subreason, &status);
    if (subreason !=  ADP_Info_AngelBufferSize) {
      DevSW_FreePacket(packet);
      return RDIError_Error;
    }
    else {
      word defaultsize, longsize;
      unpack_message(BUFFERDATA(packet->pk_buffer)+24, "%w%w",
                     &defaultsize, &longsize);
      *arg1 = longsize - ADP_WriteHeaderSize;   /* space for ADP header */
#ifdef MONITOR_DOWNLOAD_PACKETS
      angel_DebugPrint("DEBUG: ADP_Info_AngelBufferSize: got (%d, %d), returning %d.\n",
             defaultsize, longsize, *arg1);
#endif
      DevSW_FreePacket(packet);
      return status;
    }

  case RDIVector_Catch:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_VectorCatch %lx.\n", *arg1);
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_VectorCatch, *arg1);

  case RDISemiHosting_SetState:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetState %lx.\n", *arg1);
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetState, *arg1);

  case RDISemiHosting_GetState:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetState.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetState, arg1);

  case RDISemiHosting_SetVector:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetVector %lx.\n", *arg1);
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetVector, *arg1);

  case RDISemiHosting_GetVector:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetVector.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetVector, arg1);

  case RDISemiHosting_SetARMSWI:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetARMSWI.\n");
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetARMSWI, *arg1);

  case RDISemiHosting_GetARMSWI:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetARMSWI.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetARMSWI, arg1);

  case RDISemiHosting_SetThumbSWI:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_SetThumbSWI.\n");
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_SetThumbSWI, *arg1);

  case RDISemiHosting_GetThumbSWI:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SemiHosting_GetThumbSWI.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_Control, ADP_Ctrl_SemiHosting_GetThumbSWI, arg1);

  case RDIInfo_SetTopMem:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_SetTopMem.\n");
#endif
    return SendSubMessageWordAndCheckReply(ADP_Control, ADP_Ctrl_SetTopMem, *arg1);

  case RDIPointStatus_Watch:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_PointStatus_Watch.\n");
#endif
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown,
            ADP_Ctrl_PointStatus_Watch, *arg1 );
    reason = ADP_Control |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
                   &debugID, &OSinfo1, &OSinfo2,  &subreason, &status,
                   arg1, arg2);
    if (subreason !=  ADP_Ctrl_PointStatus_Watch) {
      DevSW_FreePacket(packet);
      return RDIError_Error;
    }
    else
      return status;

  case RDIPointStatus_Break:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_PointStatus_Break.\n");
#endif
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown,
            ADP_Ctrl_PointStatus_Break, *arg1 );
    reason = ADP_Control |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    }
    unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w", &reason,
                   &debugID, &OSinfo1, &OSinfo2,  &subreason, &status,
                   arg1, arg2);
    if (subreason !=  ADP_Ctrl_PointStatus_Break) {
      DevSW_FreePacket(packet);
      return RDIError_Error;
    }
    else
      return status;

  case RDIInfo_DownLoad:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Ctrl_Download_Supported.\n");
#endif
    return SendSubMessageAndCheckReply(ADP_Control, ADP_Ctrl_Download_Supported);

  case RDIConfig_Count:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEM_ConfigCount.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_ICEman, ADP_ICEM_ConfigCount, arg1);

  case RDIConfig_Nth:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEM_ConfigNth.\n");
#endif
    register_debug_message_handler();
    msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_ICEman | HtoT, 0,
            ADP_HandleUnknown, ADP_HandleUnknown,
            ADP_ICEM_ConfigNth, *arg1 );
    reason = ADP_ICEman |TtoH;
    err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
    if (err != RDIError_NoError) {
      DevSW_FreePacket(packet);
      return err;
    } else {
      RDI_ConfigDesc *cd = (RDI_ConfigDesc *)arg2;
      unsigned char n;
      len = unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%b",
                           &reason, &debugID,
                           &OSinfo1, &OSinfo2,  &subreason, &status,
                           &cd->version, &n);
      if (subreason !=  ADP_ICEM_ConfigNth) {
        DevSW_FreePacket(packet);
        return RDIError_Error;
      }
      else {
        memcpy( cd->name, BUFFERDATA(packet->pk_buffer)+len, n+1 );
        cd->name[n] = 0;
        return status;
      }
    }

  case RDIInfo_Icebreaker:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEB_Exists.\n");
#endif
    return SendSubMessageAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_Exists);

  case RDIIcebreaker_GetLocks:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEB_GetLocks.\n");
#endif
    return SendSubMessageGetWordAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_GetLocks, arg1);

  case RDIIcebreaker_SetLocks:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEB_SetLocks.\n");
#endif
    return SendSubMessageWordAndCheckReply(ADP_ICEbreakerHADP, ADP_ICEB_SetLocks, *arg1);

  case RDICommsChannel_ToHost:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEB_CC_Connect_ToHost.\n");
#endif
    if ( angel_cc_exists() == RDIError_NoError ) {

    /*
     * The following three lines of code have to be removed in order to get
     * the Windows Angel Channel Viewer working with the Thumb comms channel.
     * At the moment it allows the ARMSD command line to register a CCIN/CCOUT
     * callback which stops the ACV working!
     */
#ifdef __unix
      ccstate.tohost = (RDICCProc_ToHost *)arg1;
      ccstate.tohostarg = arg2;
      angel_check_DCC_handler( &ccstate );
#endif
#ifdef _WIN32
      
#endif

      register_debug_message_handler();
      msgsend(CI_HADP, "%w%w%w%w%w%b", ADP_ICEbreakerHADP | HtoT, 0,
              ADP_HandleUnknown, ADP_HandleUnknown,
              ADP_ICEB_CC_Connect_ToHost, (arg1 != NULL) );
      return CheckSubMessageReply(ADP_ICEbreakerHADP, ADP_ICEB_CC_Connect_ToHost);
    } else {
      return RDIError_UnimplementedMessage;
    }

  case RDICommsChannel_FromHost:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_ICEB_CC_Connect_FromHost.\n");
#endif
    if ( angel_cc_exists() == RDIError_NoError ) {

      ccstate.fromhost = (RDICCProc_FromHost *)arg1;
      ccstate.fromhostarg = arg2;
      angel_check_DCC_handler( &ccstate );

      register_debug_message_handler();
      msgsend(CI_HADP, "%w%w%w%w%w%b", ADP_ICEbreakerHADP | HtoT, 0,
              ADP_HandleUnknown, ADP_HandleUnknown,
              ADP_ICEB_CC_Connect_FromHost, (arg1 != NULL) );
      return CheckSubMessageReply(ADP_ICEbreakerHADP, ADP_ICEB_CC_Connect_FromHost);
    } else {
      return RDIError_UnimplementedMessage;
    }

  case RDIProfile_Stop:
    return SendSubMessageAndCheckReply(ADP_Profile, ADP_Profile_Stop);

  case RDIProfile_ClearCounts:
    return SendSubMessageAndCheckReply(ADP_Profile, ADP_Profile_ClearCounts);

  case RDIProfile_Start:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: ADP_Profile_Start %ld.\n", (long)*arg1);
#endif
    return SendSubMessageWordAndCheckReply(ADP_Profile, ADP_Profile_Start, *arg1);

  case RDIProfile_WriteMap:
    { RDI_ProfileMap *map = (RDI_ProfileMap *)arg1;
      int32 maplen = map->len,
            offset,
            size;
      int32 chunk = (Armsd_LongBufSize-CHAN_HEADER_SIZE-ADP_ProfileWriteHeaderSize) / sizeof(ARMword);
                     /* Maximum number of words sendable in one message */
      int oldrev = bytesex_reversing();
      int host_little = *(uint8 const *)&hostsex;
#ifdef DEBUG
      angel_DebugPrint("DEBUG: ADP_Profile_WriteMap %ld.\n", maplen);
#endif
      status = RDIError_NoError;
      if (!host_little) {
        bytesex_reverse(1);
        for (offset = 0; offset < maplen; offset++)
          map->map[offset] = bytesex_hostval(map->map[offset]);
      }
      for (offset = 0; offset < maplen; offset += size) {
        unsigned hdrlen;
        size = maplen - offset;
        packet = (Packet *)DevSW_AllocatePacket(Armsd_LongBufSize);
        if (size > chunk) size = chunk;
        hdrlen = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w%w",
                          ADP_Profile | HtoT, 0, ADP_HandleUnknown,
                          ADP_HandleUnknown, ADP_Profile_WriteMap,
                          maplen, size, offset);

        /* Copy the data into the packet. */
        memcpy(BUFFERDATA(packet->pk_buffer)+hdrlen,
               &map->map[offset], (size_t)size * sizeof(ARMword));
        packet->pk_length = size * sizeof(ARMword) + hdrlen;
        register_debug_message_handler();
        Adp_ChannelWrite(CI_HADP, packet);
        reason = ADP_Profile | TtoH;
        err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                     &status, &packet);
        if (err == RDIError_NoError) {
          unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                         &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
          if (subreason !=  ADP_Profile_WriteMap) {
            err = RDIError_Error;
          }
          DevSW_FreePacket(packet);
        }
        if (err != RDIError_NoError) { status = err; break; }
      }
      if (!host_little) {
        for (offset = 0; offset < maplen; offset++)
          map->map[offset] = bytesex_hostval(map->map[offset]);
        bytesex_reverse(oldrev);
      }
      return status;
    }

  case RDIProfile_ReadMap:
    { int32 maplen = *(int32 *)arg1,
            offset = 0,
            size;
      int32 chunk = (Armsd_BufferSize-CHAN_HEADER_SIZE-ADP_ProfileReadHeaderSize) / sizeof(ARMword);
#ifdef DEBUG
      angel_DebugPrint("DEBUG: ADP_Profile_ReadMap %ld.\n", maplen);
#endif
      status = RDIError_NoError;
      for (offset = 0; offset < maplen; offset += size) {
        size = maplen - offset;
        if (size > chunk) size = chunk;
        register_debug_message_handler();
        msgsend(CI_HADP, "%w%w%w%w%w%w%w", ADP_Profile | HtoT, 0,
                ADP_HandleUnknown, ADP_HandleUnknown,
                ADP_Profile_ReadMap, offset, size);
        reason = ADP_Profile | TtoH;
        err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                                     &status, &packet);
        if (err != RDIError_NoError) return err;
        unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                       &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
        memcpy(&arg2[offset], BUFFERDATA(packet->pk_buffer)+ADP_ProfileReadHeaderSize,
               size * sizeof(ARMword));
        DevSW_FreePacket(packet);
        if (status != RDIError_NoError) break;
      }
      { int oldrev = bytesex_reversing();
        int host_little = *(uint8 const *)&hostsex;
        if (!host_little) {
          bytesex_reverse(1);
          for (offset = 0; offset < maplen; offset++)
            arg2[offset] = bytesex_hostval(arg2[offset]);
        }
        bytesex_reverse(oldrev);
      }
      return status;
    }

  case RDIInfo_CanTargetExecute:
#ifdef DEBUG
    printf("DEBUG: RDIInfo_CanTargetExecute.\n");
#endif
    return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_CanTargetExecute);

  case RDIInfo_AgentEndianess:
    return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_AgentEndianess);

  default:
#ifdef DEBUG
    angel_DebugPrint("DEBUG: Fell through ADP_Info, default case taken.\n");
    angel_DebugPrint("DEBUG: type = 0x%x.\n", type);
#endif
    if (type & RDIInfo_CapabilityRequest) {
      switch (type & ~RDIInfo_CapabilityRequest) {
        case RDISemiHosting_SetARMSWI:
          return SendSubMessageAndCheckReply(ADP_Info, ADP_Info_ChangeableSHSWI);
        default:
#ifdef DEBUG
          angel_DebugPrint(
          "DEBUG: ADP_Info - Capability Request(%d) - reporting unimplemented \n",
                 type & ~RDIInfo_CapabilityRequest);
#endif
          break;
      }
    }
    return RDIError_UnimplementedMessage;
  }
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_AddConfig------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Add a configuration: use ADP_ICEM_AddConfig. */
int angel_RDI_AddConfig(unsigned long nbytes) {
  Packet *packet = NULL;
  int status, reason, subreason, debugID, OSinfo1, OSinfo2, err;

#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_AddConfig.\n");
#endif
  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_ICEman | HtoT,
          0, ADP_HandleUnknown, ADP_HandleUnknown,
          ADP_ICEM_AddConfig, nbytes);
  reason=ADP_ICEman | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return -1;
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                 &OSinfo1, &OSinfo2, &subreason, &status);
  DevSW_FreePacket(packet);
  if ( subreason != ADP_ICEM_AddConfig )
    return RDIError_Error;
  else
    return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_LoadConfigData-------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Load configuration data: use ADP_Ctrl_Download_Data. */
int angel_RDI_LoadConfigData(unsigned long nbytes, char const *data) {
  Packet *packet = NULL;
  int len, status, reason, subreason, debugID, OSinfo1, OSinfo2, err;

#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_LoadConfigData (%d bytes)\n", nbytes);
#endif
#if 0
  if (err = angel_RDI_AddConfig(nbytes) != RDIError_NoError)
    return err;
#endif
  packet = DevSW_AllocatePacket(Armsd_LongBufSize);
  len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w",
                 ADP_Control | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown,
                 ADP_Ctrl_Download_Data, nbytes);
  memcpy(BUFFERDATA(packet->pk_buffer)+len, data, nbytes);
  len += nbytes;
  packet->pk_length = len;
#ifdef DEBUG
  angel_DebugPrint("DEBUG: packet len %d.\n", len);
#endif
  register_debug_message_handler();
  Adp_ChannelWrite(CI_HADP, packet);
  reason=ADP_Control | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return -1;
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                 &OSinfo1, &OSinfo2, &subreason, &status);
  DevSW_FreePacket(packet);
  if ( subreason != ADP_Ctrl_Download_Data )
    return RDIError_Error;
  else
    return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_SelectConfig---------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Select a configuration: use ADP_ICEM_SelecConfig.*/
int angel_RDI_SelectConfig(RDI_ConfigAspect aspect, char const *name,
                           RDI_ConfigMatchType matchtype, unsigned versionreq,
                            unsigned *versionp)
{
  Packet *packet = NULL;
  int len, status, reason, subreason, debugID, OSinfo1, OSinfo2, err;

#ifdef DEBUG
  angel_DebugPrint("DEBUG: Entered angel_RDI_SelectConfig.\n");
#endif
  packet = DevSW_AllocatePacket(Armsd_BufferSize);
  len = msgbuild(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%b%b%b%w",
                 ADP_ICEman | HtoT, 0,
                 ADP_HandleUnknown, ADP_HandleUnknown,
                 ADP_ICEM_SelectConfig, aspect, strlen(name),
                 matchtype, versionreq);
  /* copy the name into the buffer */
  memcpy(BUFFERDATA(packet->pk_buffer)+len, name, strlen(name));
  len += strlen(name);
  packet->pk_length = len;
  register_debug_message_handler();
  Adp_ChannelWrite(CI_HADP, packet);
  reason=ADP_ICEman | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return err;
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w%w",
                 &reason, &debugID, &OSinfo1, &OSinfo2,
                 &subreason, &status, versionp);
  DevSW_FreePacket(packet);
  if ( subreason != ADP_ICEM_SelectConfig )
    return RDIError_Error;
  else
    return status;
}


/*----------------------------------------------------------------------*/
/*----angel_RDI_LoadAgent------------------------------------------------*/
/*----------------------------------------------------------------------*/

/* Load a new debug agent: use ADP_Ctrl_Download_Agent. */
int angel_RDI_LoadAgent(ARMword dest, unsigned long size,
                       getbufferproc *getb, void *getbarg)
{
  Packet *packet = NULL;
  int  status, reason, subreason, debugID, OSinfo1, OSinfo2, err;
  time_t t;

#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
  angel_DebugPrint("DEBUG: Entered angel_RDI_LoadAgent.\n");
#endif
  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%w%w", ADP_Control | HtoT,
          0, ADP_HandleUnknown, ADP_HandleUnknown,
          ADP_Ctrl_Download_Agent, dest, size);
  reason=ADP_Control | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return -1;
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason, &debugID,
                 &OSinfo1, &OSinfo2, &subreason, &status);
    DevSW_FreePacket(packet);
  if ( subreason != ADP_Ctrl_Download_Agent )
    return RDIError_Error;
  if ( status != RDIError_NoError )
    return status;

#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
  angel_DebugPrint("DEBUG: starting agent data download.\n");
#endif
  { unsigned long pos = 0, segsize;
    for (; pos < size; pos += segsize) {
      char *b = getb(getbarg, &segsize);
      if (b == NULL) return RDIError_NoError;
      err = angel_RDI_LoadConfigData( segsize, b );
      if (err != RDIError_NoError) return err;
    }
  }
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
  angel_DebugPrint("DEBUG: finished downloading new agent.\n");
#endif

  /* renegotiate back down */
  err = angel_negotiate_defaults();
  if (err != adp_ok)
     return err;

  /* Output a message to tell the user what is going on.  This is vital
   * when switching from ADP EICE to ADP over JTAG, as then the user
   * has to reset the target board !
   */
  { char msg[256];
    int len=angel_RDI_errmess(msg, 256, adp_new_agent_starting);
    angel_hostif->write(angel_hostif->hostosarg, msg, len);
  }

  /* get new image started */
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
  angel_DebugPrint("DEBUG: sending start message for new agent.\n");
#endif

  register_debug_message_handler();
  msgsend(CI_HADP, "%w%w%w%w%w%w", ADP_Control | HtoT,
          0, ADP_HandleUnknown, ADP_HandleUnknown,
          ADP_Ctrl_Start_Agent, dest);
  reason=ADP_Control | TtoH;
  err = wait_for_debug_message(&reason, &debugID, &OSinfo1, &OSinfo2,
                              &status, &packet);
  if (err != RDIError_NoError) {
    DevSW_FreePacket(packet);
    return -1;
  }
  unpack_message(BUFFERDATA(packet->pk_buffer), "%w%w%w%w%w%w", &reason,
                 &debugID, &OSinfo1, &OSinfo2, &subreason, &status);
    DevSW_FreePacket(packet);
  if ( subreason != ADP_Ctrl_Start_Agent )
    return RDIError_Error;
  if ( status != RDIError_NoError )
    return status;

  /* wait for image to start up */
  heartbeat_enabled = FALSE;
  t=time(NULL);
  do {
    Adp_AsynchronousProcessing(async_block_on_nothing);
    if ((time(NULL)-t) > 2) {
#ifdef DEBUG
      angel_DebugPrint("DEBUG: no booted message from new image yet.\n");
#endif
      break;
    }
  } while (booted_not_received);
  booted_not_received=1;

  /* Give device driver a chance to do any necessary resyncing with new agent.
   * Only used by etherdrv.c at the moment.
   */
  (void)Adp_Ioctl( DC_RESYNC, NULL );

#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
  angel_DebugPrint("DEBUG: reopening to new agent.\n");
#endif
  err = angel_RDI_open(0, NULL, NULL, NULL);
  switch ( err )
  {
      case RDIError_NoError:
      {
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
          angel_DebugPrint( "LoadAgent: Open returned RDIError_NoError\n" );
#endif
          break;
      }

      case RDIError_LittleEndian:
      {
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
          angel_DebugPrint( "LoadAgent: Open returned RDIError_LittleEndian (OK)\n" );
#endif
          err = RDIError_NoError;
          break;
      }

      case RDIError_BigEndian:
      {
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
          angel_DebugPrint( "LoadAgent: Open returned RDIError_BigEndian (OK)\n" );
#endif
          err = RDIError_NoError;
          break;
      }

      default:
      {
#if defined(DEBUG) || defined(DEBUG_LOADAGENT)
          angel_DebugPrint( "LoadAgent: Open returned %d - unexpected!\n", err );
#endif
          break;
      }
  }
#ifndef NO_HEARTBEAT
  heartbeat_enabled = TRUE;
#endif
  return err;
}

static int angel_RDI_errmess(char *buf, int blen, int errnum) {
  char *s=NULL;
  int n;

  switch (errnum) {
    case adp_malloc_failure:
      s=AdpMess_MallocFailed; break;
    case adp_illegal_args:
      s=AdpMess_IllegalArgs; break;
    case adp_device_not_found:
      s=AdpMess_DeviceNotFound; break;
    case adp_device_open_failed:
      s=AdpMess_DeviceOpenFailed; break;
    case adp_device_already_open:
      s=AdpMess_DeviceAlreadyOpen; break;
    case adp_device_not_open:
      s=AdpMess_DeviceNotOpen; break;
    case adp_bad_channel_id:
      s=AdpMess_BadChannelId; break;
    case adp_callback_already_registered:
      s=AdpMess_CBAlreadyRegd; break;
    case adp_write_busy:
      s=AdpMess_WriteBusy; break;
    case adp_bad_packet:
      s=AdpMess_BadPacket; break;
    case adp_seq_high:
      s=AdpMess_SeqHigh; break;
    case adp_seq_low:
      s=AdpMess_SeqLow; break;
    case adp_timeout_on_open:
      s=AdpMess_TimeoutOnOpen; break;
    case adp_failed:
      s=AdpMess_Failed; break;
    case adp_abandon_boot_wait:
      s=AdpMess_AbandonBootWait; break;
    case adp_late_startup:
      s=AdpMess_LateStartup; break;
    case adp_new_agent_starting:
      s=AdpMess_NewAgentStarting; break;
    default: return 0;
  }
  n=strlen(s);
  if (n>blen-1) n=blen-1;
  memcpy(buf, s, n);
  buf[n++]=0;
  return n;
}

extern const struct RDIProcVec angel_rdi;
const struct RDIProcVec angel_rdi = {
    "ADP",
    angel_RDI_open,
    angel_RDI_close,
    angel_RDI_read,
    angel_RDI_write,
    angel_RDI_CPUread,
    angel_RDI_CPUwrite,
    angel_RDI_CPread,
    angel_RDI_CPwrite,
    angel_RDI_setbreak,
    angel_RDI_clearbreak,
    angel_RDI_setwatch,
    angel_RDI_clearwatch,
    angel_RDI_execute,
    angel_RDI_step,
    angel_RDI_info,
    angel_RDI_pointinq,

    angel_RDI_AddConfig,
    angel_RDI_LoadConfigData,
    angel_RDI_SelectConfig,

    0, /*angel_RDI_drivernames,*/
    0,   /* cpunames */

    angel_RDI_errmess,

    angel_RDI_LoadAgent
};

/* EOF ardi.c */

/* Not strictly necessary, but allows linking this code into armsd. */

struct foo {
    char *name;
    int (*action)();
    char *syntax;
    char **helpmessage;
    int doafterend;
    int dobeforestart;
    int doinmidline;
} hostappl_CmdTable[1] = {{"", NULL}};

void
hostappl_Init()
{
}

int
hostappl_Backstop()
{
  return -30;
}