File: udi2mtip.c

package info (click to toggle)
pm3 1.1.13-11
  • links: PTS
  • area: main
  • in suites: potato
  • size: 174,164 kB
  • ctags: 133,819
  • sloc: ansic: 982,617; modula3: 548,483; cpp: 57,119; exp: 21,673; sh: 17,053; lisp: 13,693; makefile: 13,492; asm: 11,795; yacc: 8,575; sed: 1,100; objc: 476; csh: 254; awk: 223; pascal: 95; fortran: 5
file content (3593 lines) | stat: -rw-r--r-- 106,269 bytes parent folder | download | duplicates (8)
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
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
static char     _[] = "@(#)udi2mtip.c	5.31 93/11/03 08:34:07, Srini, AMD.";
/******************************************************************************
 * Copyright 1991 Advanced Micro Devices, Inc.
 *
 * This software is the property of Advanced Micro Devices, Inc  (AMD)  which
 * specifically  grants the user the right to modify, use and distribute this
 * software provided this notice is not removed or altered.  All other rights
 * are reserved by AMD.
 *
 * AMD MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
 * SOFTWARE.  IN NO EVENT SHALL AMD BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL
 * DAMAGES IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR
 * USE OF THIS SOFTWARE.
 *
 * So that all may benefit from your experience, please report  any  problems
 * or  suggestions about this software to the 29K Technical Support Center at
 * 800-29-29-AMD (800-292-9263) in the USA, or 0800-89-1131  in  the  UK,  or
 * 0031-11-1129 in Japan, toll free.  The direct dial number is 512-462-4118.
 *
 * Advanced Micro Devices, Inc.
 * 29K Support Products
 * Mail Stop 573
 * 5900 E. Ben White Blvd.
 * Austin, TX 78741
 * 800-292-9263
 *****************************************************************************
 *      Engineers: Srini Subramanian.
 *****************************************************************************
 * This module implements the UDI procedural interface routines of MONTIP
 * for both the Dos and Unix environments.
 *****************************************************************************
 */
#include <stdio.h>
#include  <signal.h>

#ifdef	MSDOS
#include <stdlib.h>
#include <conio.h>
#else
#include <malloc.h>
#endif

#include <string.h>
#include "coff.h"
#include "messages.h"
#include "memspcs.h"
#include "macros.h"
#include "udiproc.h"
#include "udiids.h"
#include "udiext.h"
#include "mtip.h"
#include "hif.h"
#include "versions.h"

/* 
 * MsgCode halt1, halt2, halt3, halt4 are variables defined as INT32 *
 * inside the macro block *
 */


#define	CLEAR_PENDING_STOP	StopFlag=0;

/* Stop signal handler / macro */
#define	STOP_SIG_HDLR	{\
			INT32   MsgCode;\
			INT32   halt1, halt2, halt3, halt4;\
			StopFlag=0;\
			Mini_build_break_msg();\
			if (Mini_msg_send() != SUCCESS)\
			  return((-1) * MONErrCantSendMsg);\
			SIGINT_POLL	\
			MsgCode = Wait_For_Ack();\
			if (MsgCode == ABORT_FAILURE)\
			   return ((-1) * MONErrAbortAborted);\
			if (MsgCode == FAILURE)\
			   return ((-1) * MONErrNoAck);\
			else if (MsgCode != HALT)\
			   return ((-1) * MONErrCantRecvMsg);\
			Mini_unpack_halt_msg(&halt1, &halt2, &halt3, &halt4);\
			};

#define	SEND_AND_WAIT_ACK(x)	{\
				INT32	MsgCode;\
				if (Mini_msg_send() != SUCCESS)\
				   return((-1)*MONErrCantSendMsg);\
				SIGINT_POLL	\
				MsgCode = Wait_For_Ack();\
				if (MsgCode == ABORT_FAILURE)\
				  return (UDIErrorAborted);\
				else if (MsgCode == FAILURE)\
				  return ((-1) * MONErrNoAck);\
				else if (MsgCode == ERROR)\
				  ReturnedError = 1; \
				else if (MsgCode != (INT32) (x))\
				  return ((-1) * MONErrCantRecvMsg);\
				};

#define	MONUDISession		1

static int      AllSections=(STYP_ABS|STYP_TEXT|STYP_LIT|STYP_DATA|STYP_BSS);
static UDIPId   CurrentPID = (UDIPId) UDIProcessProcessor;
static UDIUInt32 PreviousProcessorState;
static UDIUInt32 ProcessorState;
static int      TipAlive = 0;
static	int	NumberOfProcesses=0;
static int      ContinuingSession = 0;
static char    *TargetType;
static char    *SecondTarget;
static char    *CoreFile;
static int      CoreLoaded;
static BreakIdType LastBreakId = 0;
static UDIBool  SupervisorMode;
static UDIBool  RealMode;
static UDIBool  ProtectedMode;
static UDIBool  VirtualMode;
static int      BreaksInPlace;	/* EB29K */
static int      StepCmdGiven = 0;
static int	ReturnedError=0;
static	int	StopFlag=0;
static	int	Interrupted=0;
static	int	RemoteTarget=0;
static	int	NoStepReqd=0;
static	int	NoChan1Ack=0;
static	int	SendACKFirst=0;
static	INT32	MsgAlreadyInBuffer = 0;
static	INT32	MsgAlreadyReceived = 0;
static	int	Channel0Busy=0;
unsigned long	TimeOut;
int		MessageRetries;
int		BlockCount;
int		DelayFactor;
unsigned int	MaxMsgBufSize;
extern int		lpt_initialize;	/* global */
extern int		use_parport;		/* global */
static	UDISizeT	ErrCntRemaining=(UDISizeT) 0;
static	FILE	*coff_in;
static	char	buffer[LOAD_BUFFER_SIZE];

/* used in input/output routines */
#define	TIP_IO_BUFSIZE		1024
static 	char     	channel0_buffer[TIP_IO_BUFSIZE];
static 	UDISizeT   	Channel0_count=0;
static 	char     	channel1_buffer[TIP_IO_BUFSIZE];
static 	UDISizeT   	Channel1_count=0;
static	char		channel2_buffer[TIP_IO_BUFSIZE];
static 	UDISizeT   	Channel2_count=0;
static 	UDIUInt32 	Lr4_count;
static 	UDIUInt32 	TotalDone=(UDIUInt32) 0;
static 	CPUOffset 	Lr3_addr;

#define	TIP_COOKED	0		/* default */
#define	TIP_RAW		1
#define	TIP_CBREAK	2
#define	TIP_ECHO	4
#define	TIP_ASYNC	8
#define	TIP_NBLOCK	0x10
static	UDIUInt32	PgmStdinMode=TIP_COOKED;	/* default */
static	UDIUInt32	StdinCharsNeeded=0;

/* Cache register values */
static UDIUInt32 Glob_Regs[128],
                Loc_Regs[128];
static int      RefreshRegs = 1;
static int	  exitstat;
static	char	ConnectString[512];
static	char		TempArgString[1024];

static	struct tip_break_table  *bp_table=NULL;

/* Global variables */
TIP_TARGET_CONFIG tip_target_config;
TIP_TARGET_STATUS tip_target_status;
TIP_CONFIG      tip_config;
char           *Msg_Logfile;
FILE           *MsgFile;

/* ------------- Minimon TIP Specific Error Codes ------------ */
#define	MONNoError		0
#define	MONErrCantSendMsg	1
#define	MONErrCantRecvMsg	2
#define	MONErrCantLoadROMfile	3
#define	MONErrCantInitMsgSystem	4
#define	MONErrCantBreakInROM	5
#define	MONErrCantResetComm	6
#define	MONErrCantAllocBufs	7
#define	MONErrUnknownBreakType	8
#define	MONErrNoAck		9
#define	MONErrNoSynch		10
#define	MONErrCantOpenCoff	11
#define	MONErrCantWriteToMem	12
#define	MONErrAbortAborted	13
#define	MONErrNullConfigString	14
#define	MONErrNoTargetType	15
#define	MONErrOutofMemory	16
#define	MONErrErrorInit		17
#define	MONErrErrorRead		18
#define	MONErrErrorWrite	19
#define	MONErrErrorCopy		20
#define	MONErrErrorSetBreak	21
#define	MONErrErrorStatBreak	22
#define	MONErrErrorRmBreak	23
#define	MONErrConfigInterrupt	24
#define	MONErrNoConfig		25
#define	MONErrMsgInBuf		26
#define	MONErrUnknownTIPCmd	27
#define	MAX_MONERR		28

static char    *monerr_tip[] = {
   /* 0 */ "No Error.",
   /* 1 */ "Could not send message to target.",
   /* 2 */ "Did not receive the correct ACK from target.",
   /* 3 */ "Cant load ROM file.",
   /* 4 */ "Cant initialize the message system.",
   /* 5 */ "Cant set breakpoint in ROM.",
   /* 6 */ "Cant reset communication channel.",
   /* 7 */ "Cant reallocate message buffers.",
   /* 8 */ "Breakpoint type requested is not recognized.",
   /* 9 */ "No ACK from target - timed out.",
   /* 10 */ "Timed out synching. No response from target.",
   /* 11 */ "Cannot open ROM file.",
   /* 12 */ "Cannot write to memory while downloading ROM file.",
   /* 13 */ "Ctrl-C aborted previous Ctrl-C processing.",
   /* 14 */ "Null configuration string specified for connection.",
   /* 15 */ "No Target type specified for connection.",
   /* 16 */ "Out of memory.",
   /* 17 */ "Error on target - trying to initialize process.",
   /* 18 */ "Error on target - trying to read.",
   /* 19 */ "Error on target - trying to write.",
   /* 20 */ "Error on target - trying to do copy.",
   /* 21 */ "Error on target - trying to set breakpoint.",
   /* 22 */ "Error on target - trying to query breakpoint.",
   /* 23 */ "Error on target - trying to remove breakpoint.",
   /* 24 */ "User interrupt signal received - Aborting synch.",
   /* 25 */ "Couldn't get target config after reset. Try again.",
   /* 26 */ "Message received from target waiting in buffer.",
   /* 27 */ "Unknown Montip command, Exiting TIP mode."
};

#define	MAX_MONERR_SIZE		80

/* ---------------- Error Codes -------------------------------- */

/* Function declarations */

extern	void	IntHandler PARAMS((int num));
extern	void	print_recv_bytes PARAMS((void));
extern	void	set_lpt PARAMS((void));
extern	void	unset_lpt PARAMS((void));
static	FILE	*FindFile PARAMS((char *));
static	char	*GetTargetType PARAMS((char *, char *));
static	INT32	SendConfigWait PARAMS((void));
extern	void	SendACK PARAMS((void));
static 	int 	parse_string PARAMS((char *string));
static 	int 	write_args PARAMS((char *argstr, ADDR32 argstart,
						     ADDR32 * datahigh));
static 	int 	write_argv PARAMS((int arg_count, char *arg_ptr[], 
				   ADDR32 argstart, ADDR32 * hi_data));
static	INT32 	SpaceMap_udi2mm PARAMS((CPUSpace space));
static	CPUSpace	SpaceMap_mm2udi PARAMS((INT32 space));
static	int 	Reset_Processor PARAMS((void));
static	INT32 	Wait_For_Ack PARAMS((void));
static	void 	process_target_msg PARAMS((INT32 msgcode));
static	void 	process_HALT_msg PARAMS((void));
static	INT32 	process_chan0_ack PARAMS((void));
static	void 	process_CHAN1_msg PARAMS((void));
static	void 	process_CHAN2_msg PARAMS((void));
static	void 	process_stdin_needed_req PARAMS((void));
static	void 	set_stdin_mode PARAMS((void));
static	void 	process_ERR_msg PARAMS((void));
static	void 	process_HIF_msg PARAMS((void));
static 	int 	PutAllBreakpoints PARAMS((void));
static 	int 	ResetAllBreakpoints PARAMS((void));
static 	int 	Write_Glob_Reg PARAMS((INT32 RegVal, int RegNum));

/*
 * these three functions are called from HIF/IO handlers to do terminal
 * input/output.
 */
extern	void 	set_stdin_needed PARAMS((ADDR32 offset, UDICount count));
extern	void 	set_stderr_ready PARAMS((ADDR32 offset, UDICount count));
extern	void 	set_stdout_ready PARAMS((ADDR32 offset, UDICount count));


static	INT32  	Mini_load_coff PARAMS((char *fname,
			       INT32 space, 
			       INT32 sym,
			       INT32 sects,
			       int   msg));
static	int 	update_breakpt_at PARAMS((INT32 space, ADDR32 addr, ADDR32 Inst));
static	int 	is_breakpt_at PARAMS((INT32 space, ADDR32 addr));
static	void 	add_to_bp_table PARAMS((BreakIdType * id, INT32 space,
		ADDR32 offset, INT32 count, INT32 type, ADDR32 inst));
static	int 	get_from_bp_table PARAMS((BreakIdType id, INT32 * space,
				ADDR32 * offset, INT32 * count,
				INT32 * type, ADDR32 * inst));
static	int 	remove_from_bp_table PARAMS((BreakIdType id));

extern	INT32 	CheckForMsg PARAMS((INT32 time));

extern	int 	service_HIF PARAMS((UINT32 svcnm, UINT32 lr2, UINT32 lr3, 
		   UINT32 lr4, UINT32 * gr96, UINT32 * gr97, UINT32 * gr121));

/* ================================================================= */
/* UDI Procedure definitions  */

UDIError 
UDIConnect(string, Session)
  char           *string;
  UDISessionId   *Session;
{
  INT32           MsgCode;
  int		 retval;

  if (TipAlive) {	/* already connected */
    /* If same TargetType requested, return ConnectionUnavailable */
    SecondTarget = NULL;
    if ((SecondTarget = GetTargetType (SecondTarget, string)) == NULL)
       return (UDIErrorInvalidTIPOption);
    if (strcmp (SecondTarget, TargetType) == 0)
       return (UDIErrorConnectionUnavailable);
    else
       return (UDIErrorTryAnotherTIP);
  } else {
    if (ContinuingSession) {
      ContinuingSession=0; /* reset */
      *Session = (UDISessionId) MONUDISession;
      if ((int) (ProcessorState & 0xFF) != UDINotExecuting) {/* none active */
        CurrentPID = (UDIPId) (UDIProcessProcessor+1);
        NumberOfProcesses=1;
      };
      TipAlive = 1;
      return (UDINoError);
    }
    /* Initialize variables */
    /* Take control of Ctrl-C until connect time */
    signal (SIGINT, IntHandler);
    CoreFile = NULL;
    TargetType = NULL;
    Msg_Logfile = NULL;
    CoreLoaded = 0;
    CurrentPID = (UDIPId) UDIProcessProcessor;
    SupervisorMode = 0;
    RealMode = 0;
    ProtectedMode = 1;	/* default */
    VirtualMode = 0;
    BreaksInPlace = 0;	/* EB29K */
    TimeOut = (unsigned long) 10000;
    MessageRetries = (int) 1000;
#ifdef	MSDOS
    BlockCount = (int) 1000;
#else
    BlockCount = (int) 40000;
#endif
    DelayFactor = (int) 0;
    MaxMsgBufSize = (unsigned int) 0;
    Channel0_count = 0;
    Channel1_count = 0;
    Channel2_count = 0;
    Channel0Busy=0;
    *Session = (UDISessionId) MONUDISession;
    TipAlive = 1; /* no more positive error codes */

    /* TIP_CONFIG initialization */
    tip_config.PC_port_base = (INT32) - 1;	/* default */
    tip_config.PC_mem_seg = (INT32) - 1;	/* default */
    (void) strcpy(tip_config.baud_rate, DEFAULT_BAUD_RATE);
    (void) strcpy(tip_config.comm_port, DEFAULT_COMM_PORT);
    (void) strcpy(tip_config.par_port, DEFAULT_PAR_PORT);
    /* Get the CFG register value to find out 29kEndian */
    tip_target_config.P29KEndian = BIG;	/* default */

    if ((retval = parse_string(string)) != (int) 0)
      return ((UDIError) retval);	
    if (TargetType == NULL)
       return ((-1) * MONErrNoTargetType);

    /* Open Msg_Logfile if any */
    if (Msg_Logfile) {
      if ((MsgFile = fopen(Msg_Logfile, "w")) == NULL)
	Msg_Logfile = NULL;
    }
    /* Initialize message system */
    if (Mini_msg_init(TargetType) != SUCCESS) {
     *Session = (UDISessionId) MONUDISession;
      TipAlive = 1;
      return ((-1) * MONErrCantInitMsgSystem);
    }
    /* Reset communication channel */
    Mini_reset_comm();

    /*
     * Should we have different TIPS: one for shared memory & another for
     * serial connections?
     */
    if (CoreFile) {
      if ((MsgCode = Mini_load_coff(CoreFile, (INT32) D_MEM, (INT32) 1, (INT32) AllSections, 0)) != SUCCESS) {
       *Session = (UDISessionId) MONUDISession;
	TipAlive = 1;
	return ((UDIError) MsgCode);
      }
      CoreLoaded = 1;	/* True */
      /* Reset communication channel */
      Mini_reset_comm();
      Mini_go_target();	/* RESET Target Processor */
    }

    /* Define TIPs endianess */

#ifdef	MSDOS
    tip_target_config.TipEndian = LITTLE;
#else
    tip_target_config.TipEndian = BIG;
#endif

   if (strcmp(TargetType, "serial") &&
       strcmp(TargetType, "paral_1") &&
       strcmp(TargetType, "pcserver")) { /* non-serial targets */
     RemoteTarget = 0;	/* not a remote target */
   } else {
     RemoteTarget = 1;	/* remote target */
     SendACKFirst = 1; /* safe to send always */
   }

   if (RemoteTarget == 0) {	/* shared memory */
      MsgCode = Wait_For_Ack();
      if (MsgCode == ABORT_FAILURE) 
	 return (UDIErrorAborted);
      else if (MsgCode == FAILURE) 
	return((-1) * MONErrNoSynch);
    }

    if (SendACKFirst)
	 SendACK();
    /* send a config msg to sync with target */
    do {
       MsgCode = SendConfigWait();
    } while (MsgCode == HALT);
    if (MsgCode == ABORT_FAILURE)
       return (UDIErrorAborted);
    else if (MsgCode == FAILURE)
	return ((-1) * MONErrNoSynch);
    else if (MsgCode != CONFIG)
	return ((-1) * MONErrCantRecvMsg);
    Mini_unpack_config_msg(&tip_target_config);

    /* Reallocate message buffers to the smallest, if necessary */
    if ((MaxMsgBufSize != (unsigned int) 0) && 
	    (MaxMsgBufSize < (unsigned int) tip_target_config.max_msg_size))
       tip_target_config.max_msg_size = (INT32) MaxMsgBufSize;

    if (Mini_alloc_msgbuf((int) tip_target_config.max_msg_size) != SUCCESS)
      return ((-1) * MONErrCantAllocBufs);

     ProcessorState = (UDIUInt32) UDINotExecuting;
     PreviousProcessorState = (UDIUInt32) UDINotExecuting;

    return (UDINoError);
  }
}

UDIError 
UDIDisconnect(Session, Terminate)
  UDISessionId    Session;
  UDIBool	Terminate;
{
  if (Session != (UDISessionId) MONUDISession)
    return (UDIErrorNoSuchConnection);

  if (Terminate == (UDIBool) UDITerminateSession) {
    if (CoreFile)
      (void) free((char *) CoreFile);
    if (TargetType)
      (void) free((char *) TargetType);
    Mini_msg_exit();	/* clean up message buffers */
    if (Msg_Logfile)
      (void) fclose(MsgFile);
    ProcessorState = (UDIUInt32) UDINotExecuting;
    PreviousProcessorState = (UDIUInt32) UDINotExecuting;
  } else {
    ContinuingSession=1;
  };
  if ((int) (ProcessorState & 0xFF) == UDINotExecuting) {/* none active */
     CurrentPID = (UDIPId) UDIProcessProcessor;
     NumberOfProcesses=0;
  }
  TipAlive = 0;
  return (UDINoError);
}

UDIError 
UDISetCurrentConnection(Session)
  UDISessionId    Session;
{
  if (Session != (UDISessionId) MONUDISession)
    return (UDIErrorNoSuchConnection);

  return (UDINoError);
}

UDIError 
UDICapabilities ( TIPId, TargetId, DFEId, DFE, TIP, DFEIPCId, TIPIPCId, TIPString) 
  UDIUInt32	*TIPId;			/* Out */
  UDIUInt32	*TargetId;		/* Out */
  UDIUInt32	DFEId;			/* In */
  UDIUInt32	DFE;			/* In */
  UDIUInt32	*TIP;			/* Out */
  UDIUInt32	*DFEIPCId;		/* Out */
  UDIUInt32	*TIPIPCId;		/* Out */
  char		*TIPString;		/* Out */
{
  *TIPId = (UDIUInt32) UDIID (UDIProductCode_Montip, MONTIPRev, MONTIPSubRev, MONTIPSubSubRev);
  *TargetId = (UDIUInt32) UDIID (UDIProductCode_Montip, MONTIPRev, MONTIPSubRev, MONTIPSubSubRev);
  if ((int) (DFE & 0x00000FFF) > (int) (MONTIPUDIVers)) {
     *TIP = (UDIUInt32) 0;
  } else if ((int) (DFE & 0x00000FFF) == (int) MONTIPUDIVers) {
     *TIP = (UDIUInt32) DFE;
  } else {
     *TIP = (UDIUInt32) MONTIPUDIVers;
  }
  *DFEIPCId = (UDIUInt32) 0;
  *TIPIPCId = (UDIUInt32) 0;
  (void) strcpy (TIPString, "UDI 1.2 Conformant Montip for 29K targets\0");
  return (UDINoError);
}

UDIError 
UDIGetErrorMsg (ErrorCode, MsgSize, Msg, CountDone)
  UDIError	ErrorCode;		/* In */
  UDISizeT	MsgSize;		/* In */
  char		*Msg;			/* Out */
  UDISizeT	*CountDone;		/* Out */
{
  int		index;

  /* Continue Previous Error Message */
  if (ErrCntRemaining != (UDISizeT) 0) {
    index = (int) (strlen(monerr_tip[-ErrorCode]) + 1 - ErrCntRemaining);
    if (MsgSize < (UDISizeT) ErrCntRemaining) {
      (void) strncpy((char *) Msg, (char *) (monerr_tip[-ErrorCode]+index), MsgSize);
      *CountDone = MsgSize;
      ErrCntRemaining = ErrCntRemaining - MsgSize;
    } else {
      (void) strcpy((char *) Msg, (char *) (monerr_tip[-ErrorCode]+index));
      *CountDone = (UDISizeT) strlen(Msg) + 1;
      ErrCntRemaining = (UDISizeT) 0;
    }
    return (UDINoError);
  };
  /* A New ErrorCode */
  if ((ErrorCode <= 0) && (ErrorCode > (-1) * MAX_MONERR)) {
    if (MsgSize < (UDISizeT) MAX_MONERR_SIZE) {
      (void) strncpy((char *) Msg, monerr_tip[-ErrorCode], MsgSize);
      *CountDone = MsgSize;
      ErrCntRemaining = (UDISizeT) (strlen(monerr_tip[-ErrorCode])+1) - MsgSize;
    } else {
      (void) strcpy((char *) Msg, monerr_tip[-ErrorCode]);
      *CountDone = (UDISizeT) strlen(Msg) + 1;
      ErrCntRemaining = (UDISizeT) 0;
    }
    return (UDINoError);
  } else {
    return (UDIErrorUnknownError);
  };
}

UDIError 
UDIGetTargetConfig(KnownMemory, NumberOfRanges, ChipVersions, NumberOfChips) 
  UDIMemoryRange KnownMemory[];		/* Out */
  UDIInt	*NumberOfRanges;	/* In/Out */
  UDIUInt32	ChipVersions[];		/* Out */
  UDIInt	*NumberOfChips;		/* In/Out */
{
  UDIInt	InRanges, InChips;
  int		Incomplete;

  Incomplete = 0;

  InRanges = *NumberOfRanges;
  InChips = *NumberOfChips;

  if ((InRanges < (UDIInt) MONMaxMemRanges) || (InChips < (UDIInt) MONMaxChips))
     Incomplete = 1;
   
  *NumberOfRanges = (UDIInt) 0;
  switch ((int) InRanges) {
    default:
    case	3 /* MONMaxMemRanges */:
     if (*NumberOfRanges == (UDIInt) 0)
	 *NumberOfRanges = (UDIInt) 3;
     KnownMemory[2].Space = (CPUSpace) UDI29KIRAMSpace;
     KnownMemory[2].Offset = (CPUOffset) tip_target_config.I_mem_start;
     KnownMemory[2].Size = (CPUSizeT) tip_target_config.I_mem_size;
    case	2:
     if (*NumberOfRanges == (UDIInt) 0)
	 *NumberOfRanges = (UDIInt) 2;
     KnownMemory[1].Space = (CPUSpace) UDI29KDRAMSpace;
     KnownMemory[1].Offset = (CPUOffset) tip_target_config.D_mem_start;
     KnownMemory[1].Size = (CPUSizeT) tip_target_config.D_mem_size;
    case	1:
     if (*NumberOfRanges == (UDIInt) 0)
	 *NumberOfRanges = (UDIInt) 1;
     KnownMemory[0].Space = (CPUSpace) UDI29KIROMSpace;
     KnownMemory[0].Offset = (CPUOffset) tip_target_config.ROM_start;
     KnownMemory[0].Size = (CPUSizeT) tip_target_config.ROM_size;
	break;
    case	0:
        *NumberOfRanges = (UDIInt) 0;
	break;
  }

  *NumberOfChips = (UDIInt) 0;
  switch ((int) InChips) {
     default:
     case	2: /* MONMaxChips */
       if (*NumberOfChips == (UDIInt) 0) 
	   *NumberOfChips = (UDIInt) 2;
       if (tip_target_config.coprocessor == (UINT32) -1)
          ChipVersions[1] = (UDIUInt32) UDI29KChipNotPresent;
       else
          ChipVersions[1] = (UDIUInt32) tip_target_config.coprocessor;
     case	1:
       if (*NumberOfChips == (UDIInt) 0) 
	   *NumberOfChips = (UDIInt) 1;
        ChipVersions[0] = (UDIUInt32) tip_target_config.processor_id;
	break;
     case	0:
	*NumberOfChips = (UDIInt) 0;
	break;
  }
  if (Incomplete)
     return (UDIErrorIncomplete);
  else
     return (UDINoError);
}

UDIError 
UDICreateProcess(pid)
  UDIPId         *pid;
{
  if (CurrentPID == (UDIPId) (UDIProcessProcessor + 1))
    return (UDIErrorCantCreateProcess);
  CurrentPID = (UDIPId) (UDIProcessProcessor + 1);
  NumberOfProcesses=1;
  *pid = (UDIPId) CurrentPID;
  return (UDINoError);
}

UDIError 
UDISetCurrentProcess(pid)
  UDIPId          pid;
{
  if ((pid > (UDIPId) (UDIProcessProcessor + 1)) ||
	 (pid < (UDIPId) (UDIProcessProcessor)))
    return (UDIErrorNoSuchProcess);
  if ((NumberOfProcesses == (int) 0) && (pid != (UDIPId) UDIProcessProcessor))
    return (UDIErrorNoSuchProcess);
  CurrentPID = pid;
  return (UDINoError);
}

UDIError 
UDIDestroyProcess(pid)
  UDIPId          pid;
{
  if ((pid > (UDIPId) (UDIProcessProcessor + 1)) ||
	 (pid < (UDIPId) (UDIProcessProcessor)))
    return (UDIErrorNoSuchProcess);
  CurrentPID = (UDIPId) UDIProcessProcessor;
  ProcessorState = (UDIUInt32) UDINotExecuting;
  PreviousProcessorState = (UDIUInt32) UDINotExecuting;
  NumberOfProcesses=0;
  return (UDINoError);
}

UDIError 
UDIInitializeProcess (ProcessMemory, NumberOfRanges, EntryPoint, StackSizes, NumberOfStacks, ArgString)
  UDIMemoryRange ProcessMemory[];	/* In */
  UDIInt	NumberOfRanges;		/* In */
  UDIResource	EntryPoint;		/* In */
  CPUSizeT	StackSizes[];		/* In */
  UDIInt	NumberOfStacks;		/* In */
  char		*ArgString;		/* In */
{
  UDIError        ErrCode;
  UDIRange	  text_addr, data_addr;
  CPUSizeT	  mem_stack_size, reg_stack_size;

  ADDR32          arg_start;
  ADDR32          data_high;
  ADDR32          highmem;
  INT32           os_control;
  INT32		MsgCode;
  UDIInt	i;


  exitstat = 0;	/* reset */

  PgmStdinMode=TIP_COOKED;	/* revert to default mode */

  CLEAR_PENDING_STOP

  if (CurrentPID == (UDIPId) UDIProcessProcessor) {
    if ((MsgCode = Reset_Processor()) != SUCCESS)
       return ((UDIError) MsgCode);
    do {
       MsgCode = SendConfigWait();
    } while (MsgCode == HALT);
    if (MsgCode == ABORT_FAILURE)
       return (UDIErrorAborted);
    else if (MsgCode == FAILURE)
	return ((-1) * MONErrNoSynch);
    else if (MsgCode != CONFIG)
	return ((-1) * MONErrCantRecvMsg);
    Mini_unpack_config_msg(&tip_target_config);
    /* Reallocate message buffers */
    if (Mini_alloc_msgbuf((int) tip_target_config.max_msg_size) != SUCCESS)
      return ((-1) * MONErrCantAllocBufs);
    ProcessorState = (UDIUInt32) UDINotExecuting;
    Channel0_count = 0;
    Channel1_count = 0;
    Channel2_count = 0;
    Channel0Busy = 0;
    PreviousProcessorState = (UDIUInt32) UDINotExecuting;
    return (UDINoError);
  };


  /* For other processes */
  /* Set Default Values */
  mem_stack_size = (CPUSizeT) MONDefaultMemStackSize;
  reg_stack_size = (CPUSizeT) MONDefaultRegStackSize;
  text_addr.Low = (CPUOffset) tip_target_config.I_mem_start;
  text_addr.High = (CPUOffset) tip_target_config.I_mem_start +
		   (CPUOffset) tip_target_config.I_mem_size - 1;
  data_addr.Low = (CPUOffset) tip_target_config.D_mem_start;
  data_addr.High = (CPUOffset) tip_target_config.D_mem_start +
		   (CPUOffset) tip_target_config.D_mem_size -
		   (CPUOffset) (mem_stack_size + reg_stack_size + 16) - 1;

  /* Get Memory Ranges */
  if (NumberOfRanges != (UDIInt) 0) {
     for (;NumberOfRanges--;) {
       switch ((int) ProcessMemory[NumberOfRanges].Space) {
	  case	UDI29KIRAMSpace:
		text_addr.Low = ProcessMemory[NumberOfRanges].Offset;
		text_addr.High = ProcessMemory[NumberOfRanges].Offset +
			(CPUOffset) ProcessMemory[NumberOfRanges].Size;
		break;
	  case	UDI29KDRAMSpace:
		data_addr.Low = ProcessMemory[NumberOfRanges].Offset;
		data_addr.High = ProcessMemory[NumberOfRanges].Offset +
			(CPUOffset) ProcessMemory[NumberOfRanges].Size;
		break;
	  default: /* don't care */
		break;
       } /* switch */
     } /* for */
  }
  /* Get Stack Sizes */
  for (i = (UDIInt) 0; i < NumberOfStacks; i=i+(UDIInt)1) {
     switch ((int) i) {
	case	0:  /* register stack size */
		if (StackSizes[0] != (CPUSizeT) 0)
		  reg_stack_size = StackSizes[0];
		break;
	case	1: /* memory stack size */
		if (StackSizes[1] != (CPUSizeT) 0)
		  mem_stack_size = StackSizes[1];
		break;
	default: /* don't care */
		break;
     }
  }

  if ((CPUOffset) text_addr.High > (CPUOffset) data_addr.High)
     data_addr.High = text_addr.High;  /* when no data sections */
  arg_start = (data_addr.High + 7) & ~0x7;	/* word boundary */

  if ((ErrCode = write_args(ArgString, 
			    arg_start, &data_high)) != UDINoError)
    return (ErrCode);

  data_addr.High = (data_high + 7) & ~0x7;	/* double word bdry */

  highmem = (ADDR32) 0;

  /* User programs run mode */
  if (SupervisorMode)
    os_control = (INT32) 0x10000000;	/* set bit 28 only */
  else if (VirtualMode || ProtectedMode)
    os_control = (INT32) 0;
  else
    os_control = (INT32) 0x80000000;

  Mini_build_init_msg((ADDR32) text_addr.Low, (ADDR32) text_addr.High,
		      (ADDR32) data_addr.Low, (ADDR32) data_addr.High,
		      (ADDR32) EntryPoint.Offset,
		      (INT32) mem_stack_size, (INT32) reg_stack_size,
		      (ADDR32) highmem,
		      (ADDR32) arg_start,
		      (INT32) os_control);
  SEND_AND_WAIT_ACK(INIT_ACK);
  if (ReturnedError == (int) 1) {
    ReturnedError = 0;
    return ((-1) * MONErrErrorInit);
  }
  Mini_unpack_init_ack_msg();

  ProcessorState = (UDIUInt32) UDINotExecuting;
  PreviousProcessorState = (UDIUInt32) UDINotExecuting;

  return (UDINoError);
}

UDIError 
UDIRead(from, to, count, size, count_done, host_endian)
  UDIResource     from;
  UDIHostMemPtr   to;
  UDICount        count;
  UDISizeT          size;
  UDICount       *count_done;
  UDIBool         host_endian;
{
  INT32           space = SpaceMap_udi2mm(from.Space);
  INT32           done;
  INT32           ttl_count;
  INT32           msg_count;
  INT32           overhead;

  ADDR32          ack_addr;
  INT32		  ack_space;
  BYTE           *output;
  UDIError        UDIretval;

  UDICount        i;
  INT32          *Version;

  int             Gr1_val;
  int             Lrnum;
  int             j;
  UDIResource     temp_from;
  UDICount        temp_done;
  UDIUInt32       start_offset,
                  end_offset;
  BYTE           *reg_data;


  CLEAR_PENDING_STOP

  if (count <= (UDICount) 0) {
    *count_done = (UDICount) 0;
    return (UDINoError);
  }

  if (space == (INT32) VERSION_SPACE) {	/* minimon ver cmd */
    Version = (INT32 *) to;
    *Version = (INT32) tip_target_config.version;
     *(Version+1) = (INT32) tip_target_config.os_version; 
    /*  TIPVERSION must be 11 chars or less  */
    strcpy((char *) (Version+2),TIPVERSION); 
    /*  TIPDATE must be 11 chars or less  */
    strcpy((char *) (Version+5),TIPDATE); 
    /* max msg size */
    *(Version + 8) = tip_target_config.max_msg_size;
    /* max bkpts */
    *(Version + 9) = tip_target_config.max_bkpts;
    if ((host_endian) && (tip_target_config.TipEndian != tip_target_config.P29KEndian)) {
      output = (BYTE *) to;
      for (i = 0; i < count; i++) {
        if (size == 4)
	  tip_convert32(output);
        else if (size == 2)
	  tip_convert16(output);
        output = output + size;
      }
    }	/* hostendian */
    *count_done = (UDICount) count;
    return (UDINoError);
  };

  if (space < (INT32) 0) {
    *count_done = (UDICount) 0;
    return (UDIErrorUnknownResourceSpace);
  }

  output = (BYTE *) to;

  switch (from.Space) {
   case UDI29KPC:
    from.Offset = 1;	/* PC1 */
    break;
   case UDI29KGlobalRegs:
    break;
   case UDI29KRealRegs:
    /* REAL REGS BEGIN */
    /* get global and local reg values from target if target exec'ed */
    if (RefreshRegs) {
      RefreshRegs = 0;	/* reset */
      temp_from.Offset = (CPUOffset) 0;
      temp_from.Space = UDI29KGlobalRegs;
      if ((UDIretval = UDIRead(temp_from,
			       (UDIHostMemPtr) &Glob_Regs[0],
			       (UDICount) 2,
			       (UDISizeT) 4,
			       (UDICount *) &temp_done,
			       (UDIBool) TRUE)) != UDINoError)	/* gr0, gr1 */
	return (UDIretval);
      /* UDIRead ();  gr64 to gr 127 */
      temp_from.Offset = (CPUOffset) 64;
      temp_from.Space = UDI29KGlobalRegs;
      if ((UDIretval = UDIRead(temp_from,
			       (UDIHostMemPtr) &Glob_Regs[64],
			       (UDICount) 64,
			       (UDISizeT) 4,
			       (UDICount *) &temp_done,
			       (UDIBool) TRUE)) != UDINoError)	/* gr0, gr1 */
	return (UDIretval);
      /* UDIRead ();   lr0 to lr127 */
      temp_from.Offset = (CPUOffset) 0;
      temp_from.Space = UDI29KLocalRegs;
      if ((UDIretval = UDIRead(temp_from,
			       (UDIHostMemPtr) &Loc_Regs[0],
			       (UDICount) 128,
			       (UDISizeT) 4,
			       (UDICount *) &temp_done,
			       (UDIBool) TRUE)) != UDINoError)	/* gr0, gr1 */
	return (UDIretval);
    };

    start_offset = from.Offset;
    end_offset = start_offset + count;
    output = (BYTE *) to;
    while (start_offset < end_offset) {	/* do only if count is non zero */
      if (start_offset <= (UDIUInt32) 127) {
	reg_data = (BYTE *) &Glob_Regs[(int) start_offset];
	for (j = 0; j < 4 /* sizeof (UDIUInt32) */ ; j++)
	  *output++ = *reg_data++;
      } else if ((start_offset >= (UDIUInt32) 128) && (start_offset <= (UDIUInt32) 255)) {
	Gr1_val = (int) (Glob_Regs[1] & 0x000001FC) >> 2;	/* bits 2 to 8 */
	Lrnum = (int) ((int) start_offset - Gr1_val) % 128;
	reg_data = (BYTE *) & Loc_Regs[(int) Lrnum];
	for (j = 0; j < 4 /* sizeof (UDIUInt32) */ ; j++)
	  *output++ = *reg_data++;
      } else
	return (UDIErrorUnknownResourceSpace);
      start_offset = start_offset + (UDIUInt32) 1;
    }	/* end while */
    *count_done = (UDICount) count;
    return (UDINoError);
    /* REAL REGS END */
   default:
    break;
  }

  output = (BYTE *) to;
  if ( (RemoteTarget == 0) &&
	((from.Space == UDI29KDRAMSpace) ||
	 (from.Space == UDI29KIRAMSpace) ||
	 (from.Space == UDI29KIROMSpace))) { /* shared memory board */
      Mini_read_memory(space, from.Offset, count * size, (BYTE *) output);
  } else {
     /* overhead = checksum + header + size rounding + bfr rounding + ? */
     overhead = 32;
     ttl_count = count;
     output = (BYTE *) to;
     while (ttl_count > 0) {	
       /* Check for user interrupt */
       if (StopFlag) {
          STOP_SIG_HDLR
          ProcessorState = (UDIUInt32) UDIStopped;
          PreviousProcessorState = (UDIUInt32) UDIStopped;
          return (UDIErrorAborted);
       }; 
       /* Check possible buffer overflow */
       if ((ttl_count * size) + overhead > 
#ifdef MSDOS
		   tip_target_config.max_msg_size) {
         msg_count = (tip_target_config.max_msg_size-overhead) >> (size >> 1);  
#else
		   (INT32) 256) { /* SunOS has problems with higher numbers */
         msg_count = (256 - overhead) >> (size >> 1);  
#endif
         ttl_count = ttl_count - msg_count;
       } else {
         msg_count = ttl_count;
         ttl_count = ttl_count - msg_count;
       }
       Mini_build_read_req_msg(space, (ADDR32) from.Offset, msg_count, size);
       SEND_AND_WAIT_ACK(READ_ACK);
       if (ReturnedError == (int) 1) {
	 ReturnedError = 0;
	 return ((-1) * MONErrErrorRead);
       }
       Mini_unpack_read_ack_msg((INT32 *) &ack_space, (ADDR32 *) &ack_addr,
			     (INT32 *) &done, (BYTE *) output);
       output = output + (msg_count * size);
       if (ISMEM(space))
          from.Offset = from.Offset + (CPUOffset) (msg_count * size);
       else
          from.Offset = from.Offset + (CPUOffset) msg_count;
     }
  } /* end while */

  if ((host_endian) && 
	(tip_target_config.TipEndian != tip_target_config.P29KEndian)) {
    output = (BYTE *) to;
    for (i = 0; i < count; i++) {
      if (size == 4)
	tip_convert32(output);
      else if (size == 2)
	tip_convert16(output);
      output = output + size;
    }
  }	/* hostendian */

  *count_done = (UDICount) count;
  return (UDINoError);
}

UDIError 
UDIWrite(from, to, count, size, count_done, HostEndian)
  UDIHostMemPtr   from;
  UDIResource     to;
  UDICount        count;
  UDISizeT          size;
  UDICount       *count_done;
  UDIBool         HostEndian;
{
  INT32           space = SpaceMap_udi2mm(to.Space);
  INT32           done;
  INT32           ttl_count;
  INT32           msg_count;
  INT32           overhead;
  ADDR32          ack_addr;
  INT32		  ack_space;
  BYTE           *input;
  UDIError        UDIretval;
  UDIUInt32       tmpbuf[2];
  UDICount		i;

  /* REAL REGS BEGIN */
  UDIResource     temp_to;
  UDICount        temp_done;
  CPUOffset       start_offset,
                  end_offset;
  UDIUInt32       Gr1_val;

  /* REAL REGS END */

  CLEAR_PENDING_STOP

  if (space < (INT32) 0) {
    *count_done = (UDICount) 0;
    return (UDIErrorUnknownResourceSpace);
  }

  if (count <= (UDICount) 0) {
    *count_done = (UDICount) 0;
    return (UDINoError);
  }

  if (to.Space == UDI29KPC) {
    /* when writing UDI29KPC, set both PC1 and PC0 */
    /* NOTE: this assumes we are not in freeze mode */
    /* this must all be done before doing the endian conversion below */
    to.Offset = 0;	/* start at PC0 */
    count = (UDIInt32) 2;	/* writing 2 4-byte quantities */
    tmpbuf[1] = *((UDIUInt32 *) from);	/* PC1 = PC */
    if (!HostEndian && (tip_target_config.TipEndian != tip_target_config.P29KEndian)) {
	tmpbuf[0] = tmpbuf[1];
	tip_convert32((BYTE *) &tmpbuf[0]);
        tmpbuf[0] = tmpbuf[0] + 4;	/* PC0 = PC + 4 */
	tip_convert32((BYTE *) &tmpbuf[0]);
    } else {
        tmpbuf[0] = tmpbuf[1] + 4;	/* PC0 = PC + 4 */
    }
    from = (UDIHostMemPtr) tmpbuf;	/* set pointer to temporary (8-byte)
					 * buffer */
  }

  switch (to.Space) {
   case UDI29KLocalRegs:
    RefreshRegs = 1;
    break;
   case UDI29KPC:	/* PC causes special regs(PC0,PC1) space */
    break;
   case UDI29KGlobalRegs:
    RefreshRegs = 1;
    break;
   case UDI29KRealRegs:
    RefreshRegs = 1;
    /* REAL REGS BEGIN */
    start_offset = to.Offset;
    end_offset = start_offset + count - 1;
    if ((end_offset <= 127)) {	/* all globals asked */
      temp_to.Offset = to.Offset;
      temp_to.Space = UDI29KGlobalRegs;
      if ((UDIretval = UDIWrite(from,
				temp_to,
				count,
				size,
				&temp_done,
				HostEndian)) != UDINoError)
	return (UDIretval);
    } else if (start_offset > 127) {	/* all local regs */
      /* read gr1 */
      temp_to.Offset = (CPUOffset) 1;
      temp_to.Space = UDI29KGlobalRegs;
      if ((UDIretval = UDIRead(temp_to,
			       (UDIHostMemPtr) &Gr1_val,
			       (UDICount) 1,
			       (UDISizeT) 4,
			       (UDICount *) &temp_done,
			       (UDIBool) TRUE)) != UDINoError)	/* gr1 */
	return (UDIretval);
      /* recompute start_offset and end_offset */
      Gr1_val = (Gr1_val & 0x01FC) >> 2;
      start_offset = (start_offset - Gr1_val) % 128;
      end_offset = (end_offset - Gr1_val) % 128;
      input = (BYTE *) from;
      if (start_offset > end_offset) {	/* wrap around */
	temp_to.Offset = start_offset;
	temp_to.Space = UDI29KLocalRegs;
	if ((UDIretval = UDIWrite(input,
				  temp_to,
				  (UDICount) (128 - start_offset),
				  size,
				  &temp_done,
				  HostEndian)) != UDINoError)
	  return (UDIretval);
	input = input + (int) ((128 - start_offset) * size);
	temp_to.Offset = (CPUOffset) 0;	/* from LR0 */
	temp_to.Space = UDI29KLocalRegs;
	if ((UDIretval = UDIWrite(input,
				  temp_to,
				  (UDICount) (end_offset + 1 ),
				  size,
				  &temp_done,
				  HostEndian)) != UDINoError)
	  return (UDIretval);
      } else {	/* no wrapping */
	temp_to.Offset = start_offset;
	temp_to.Space = UDI29KLocalRegs;
	if ((UDIretval = UDIWrite(input,
				  temp_to,
				  count,
				  size,
				  &temp_done,
				  HostEndian)) != UDINoError)
	  return (UDIretval);
      }
    } else {	/* overlap */
      input = (BYTE *) from;
      /* write globals */
      temp_to.Offset = start_offset;
      temp_to.Space = UDI29KGlobalRegs;
      if ((UDIretval = UDIWrite(input,
				temp_to,
				((UDICount) 128 - (UDICount) start_offset),
				size,
				&temp_done,
				HostEndian)) != UDINoError)
	return (UDIretval);
      input = input + (int) (size) * ((UDICount) 128 - (UDICount) start_offset);
      /* write locals */
      temp_to.Offset = (CPUOffset) 128;
      temp_to.Space = UDI29KRealRegs;
      if ((UDIretval = UDIWrite(input,
				temp_to,
				(UDICount) (count - 128 + start_offset),
				size,
				&temp_done,
				HostEndian)) != UDINoError)
	return (UDIretval);
    }
    *count_done = (UDICount) count;
    return (UDINoError);
    /* REAL REGS END */
   default:
    break;
  }

  if (HostEndian && 
	      (tip_target_config.TipEndian != tip_target_config.P29KEndian)) {
    input = (BYTE *) from;
    for (i = 0; i < count; i++) {
      if (size == 4)
	tip_convert32(input);
      else if (size == 2)
	tip_convert16(input);
      input = input + size;
    }
  }; /* endian conversion done */

  input = (BYTE *) from;
  if ((RemoteTarget == 0) &&
	((to.Space == UDI29KDRAMSpace) ||
	 (to.Space == UDI29KIRAMSpace) ||
	 (to.Space == UDI29KIROMSpace))) {
      Mini_write_memory(space, to.Offset, count * size, (BYTE *) input);
      *count_done = (UDICount) count;
      return (UDINoError);
  } else {  /* remote */
     /* overhead = checksum + header + size rounding + bfr rounding + ? */
     overhead = 32;
     ttl_count = count;
     input = (BYTE *) from;
     while (ttl_count > 0) {	
       /* Check for user interrupt */
       if (StopFlag) {
          STOP_SIG_HDLR
          ProcessorState = (UDIUInt32) UDIStopped;
          PreviousProcessorState = (UDIUInt32) UDIStopped;
          return (UDIErrorAborted);
       }; 
       /* Check possible buffer overflow */
       if ((ttl_count * size) + overhead > 
		   tip_target_config.max_msg_size) {
         msg_count = (tip_target_config.max_msg_size-overhead) >> (size >> 1);  
         ttl_count = ttl_count - msg_count;
       } else {
         msg_count = ttl_count;
         ttl_count = ttl_count - msg_count;
       }
       Mini_build_write_req_msg(space, (ADDR32) to.Offset,
			      msg_count, size, (BYTE *) input);
       SEND_AND_WAIT_ACK(WRITE_ACK);
       if (ReturnedError == (int) 1) {
	 ReturnedError = 0;
	 return ((-1) * MONErrErrorWrite);
       }
       Mini_unpack_write_ack_msg((INT32 *) &ack_space,
			      (ADDR32 *) &ack_addr,
			      (INT32 *) &done);
       input = input + (msg_count * size);
       if (ISMEM(space))
          to.Offset = to.Offset + (CPUOffset) (msg_count * size);
       else
          to.Offset = to.Offset + (CPUOffset) msg_count;
     }	/* while */
  } /* end remote */
  *count_done = (to.Space == UDI29KPC) ? (UDICount) 1 : (UDICount) count;
  return (UDINoError);
}

UDIError 
UDICopy(from, to, count, size, count_done, direction)
  UDIResource     from;
  UDIResource     to;
  UDICount        count;
  UDISizeT          size;
  UDICount       *count_done;
  UDIBool         direction;
{
  INT32           f_space = SpaceMap_udi2mm(from.Space);
  INT32           t_space = SpaceMap_udi2mm(to.Space);

  UDICount	  counter, maxcount,curcount;
  INT32	  	fill_count, fill_size;
  
  INT32           done;
  ADDR32          ack_saddr,
                  ack_daddr;

  CLEAR_PENDING_STOP

  if ((t_space < 0) || (f_space < 0)) {
    *count_done = (UDICount) 0;
    return (UDIErrorUnknownResourceSpace);
  }

  if (count <= (UDICount) 0) {
    *count_done = (UDICount) 0;
    return (UDINoError);
  }

  RefreshRegs = 1;

  /* Split the copy to smaller copies based on the message size */
  maxcount = (UDICount) (tip_target_config.max_msg_size / size);
  counter = (UDICount) count;

  while (counter > (UDICount) 0) {
    /* Check for user interrupt */
    if (StopFlag) {
       STOP_SIG_HDLR
       ProcessorState = (UDIUInt32) UDIStopped;
       PreviousProcessorState = (UDIUInt32) UDIStopped;
       return (UDIErrorAborted);
    }; 
    curcount = (maxcount < counter) ? maxcount : counter;
    counter = counter - curcount;
    if ((size > (UDISizeT) 4) && (t_space == (INT32) I_MEM)) { 
       /* reduce it to  4, must be a multiple also for I_MEM */
       fill_count = (INT32) (curcount * (size/4));
       fill_size = (INT32) size;
    } else if ((size > (UDISizeT) 4) && (t_space != (INT32) I_MEM)) { 
       /* copy as bytes */
       fill_count = (INT32) (curcount * size);
       fill_size = (INT32) 1; /* bytes */
    } else {
       fill_count = (INT32) curcount;
       fill_size = (INT32) size;
    };
    Mini_build_copy_msg(f_space, (ADDR32) from.Offset,
		      t_space, (ADDR32) to.Offset,
		      fill_count, fill_size);
    SEND_AND_WAIT_ACK(COPY_ACK);
    if (ReturnedError == (int) 1) {
      ReturnedError = 0;
      return ((-1) * MONErrErrorCopy);
    }
    Mini_unpack_copy_ack_msg(&f_space, &ack_saddr,
			   &t_space, &ack_daddr, &done);
    from.Offset = from.Offset + (CPUOffset) (curcount * size);
    to.Offset = to.Offset + (CPUOffset) (curcount * size);
  }; /* end while */

  *count_done = (UDICount) count;
  return (UDINoError);
}

UDIError 
UDIExecute()
{
  INT32           MsgCode;

    CLEAR_PENDING_STOP

  if (!NoStepReqd) {
    if (!StepCmdGiven) {
      /* Execute one instruction */
      Mini_build_step_msg((INT32) 1);

      if (Mini_msg_send() != SUCCESS)
	return ((-1) * MONErrCantSendMsg);
      /* process message received from target */
      MsgCode = Wait_For_Ack();
      if (MsgCode == ABORT_FAILURE)
	return (UDIErrorAborted);
      else if (MsgCode == FAILURE)
	return ((-1) * MONErrNoAck);

      process_target_msg(MsgCode);

      /* if processor state is stepped, set breakpoints, issue a GO */
      if (ProcessorState != (UDIUInt32) UDIStepped) {
	RefreshRegs = 1;
	return (UDINoError);
      }
      PutAllBreakpoints();
      BreaksInPlace = 1;
    }
  }

  Mini_build_go_msg();

  if (Mini_msg_send() != SUCCESS)
    return ((-1) * MONErrCantSendMsg);

  RefreshRegs = 1;
  ProcessorState = (UDIUInt32) UDIRunning;
  PreviousProcessorState = (UDIUInt32) UDIRunning;

  return (UDINoError);
}

/*
 * Stepping will NOT cause any breakpoints to be installed. It will step the
 * number requested.
 */

UDIError 
UDIStep(steps, steptype, range)
  UDIUInt32       steps;
  UDIStepType     steptype;
  UDIRange        range;
{
  CLEAR_PENDING_STOP

  if (steps == (UDIUInt32) 0)
    return (UDINoError);

  if ((steptype & UDIStepOverCalls) || (steptype & UDIStepOverTraps) ||
       (steptype & UDIStepInRange))
       return (UDIErrorUnsupportedStepType);

  StepCmdGiven = 1;
  Mini_build_step_msg(steps);
  if (Mini_msg_send() != SUCCESS)
    return ((-1) * MONErrCantSendMsg);
  RefreshRegs = 1;
  ProcessorState = (UDIUInt32) UDIRunning;
  PreviousProcessorState = (UDIUInt32) UDIRunning;

  return (UDINoError);
}

UDIVoid 
UDIStop()
{
  int	GrossState;

  GrossState = (int) (ProcessorState & 0xFF);
  if ((GrossState == UDINotExecuting) || (GrossState == UDIRunning) ||
      (GrossState == UDIStdoutReady) || (GrossState == UDIStderrReady) ||
      (GrossState == UDIStdinNeeded) ) {
      StopFlag = 1; /* This will be reset after its handled */
  }
  /* Else ignored */
  return; 
}

UDIError 
UDIWait(maxtime, pid, stop_reason)
  UDIInt32        maxtime;
  UDIPId         *pid;
  UDIUInt32      *stop_reason;
{
  INT32           MsgCode;

  *pid = (UDIPId) CurrentPID;

  if (ProcessorState == (UDIUInt32) UDIRunning) {
    while (1) { /* handle messages as long as they are coming */
      if (MsgAlreadyInBuffer==1) {
	MsgCode = MsgAlreadyReceived;
	MsgAlreadyInBuffer=0;
      } else {
        MsgCode = CheckForMsg(maxtime);
      }
#if 0
      MsgCode = CheckForMsg(maxtime);
#endif
      if ((MsgCode == FAILURE) || (MsgCode == ABORT_FAILURE)) { /* no news */
        *stop_reason = ProcessorState;	
        return (UDINoError);
      } else {	/* a message from target has arrived */
        *stop_reason = ProcessorState;
	if (MsgCode == CHANNEL0_ACK) {
	   process_chan0_ack();
	   return (UDINoError);
	}
        (void) process_target_msg(MsgCode);
        if (ProcessorState != (UDIUInt32) UDIRunning) 
          return (UDINoError);
      };
    }
  } else {
    *stop_reason = ProcessorState;
    return (UDINoError);
  }
}

UDIError 
UDISetBreakpoint(addr, pass_count, bk_type, break_id)
  UDIResource     addr;
  UDIInt32        pass_count;
  UDIBreakType    bk_type;
  BreakIdType    *break_id;
{
  INT32           space = SpaceMap_udi2mm(addr.Space);
  ADDR32          ack_addr;
  INT32           set_count,
                  set_type;
  BreakIdType     newid;
  UDIUInt32       BreakInst;	/* EB29K */
  UDIError        UDIretval;	/* EB29K */

  CLEAR_PENDING_STOP

  if (space < 0)
    return (UDIErrorUnknownResourceSpace);

  /*
   * Minimon currently supports only two types of breakpoints * BKPT_29000
   * and BKPT_29050 *
   */
  if (bk_type & MONBreakFlagHardware) {
    if (bk_type & MONBreakTranslationEnabled)
      bk_type = BKPT_29050_BTE_1;
    else
      bk_type = BKPT_29050_BTE_0;	/* default */
  } else if ((bk_type & UDIBreakFlagRead) || (bk_type & UDIBreakFlagWrite))
    return ((-1) * MONErrUnknownBreakType);
  else if (bk_type & UDIBreakFlagExecute)
    bk_type = BKPT_29000;	/* Minimon uses this */

  if (pass_count == (UDIInt32) 0)
     pass_count = (UDIInt32) -1; /* make it temporary */
  Mini_build_bkpt_set_msg(space,
			  (ADDR32) addr.Offset,
			  (INT32) pass_count,
			  (INT32) bk_type);
  SEND_AND_WAIT_ACK (BKPT_SET_ACK);
  if (ReturnedError == (int) 1) {
    ReturnedError = 0;
    return ((-1) * MONErrErrorSetBreak);
  }
  Mini_unpack_bkpt_set_ack_msg((INT32 *) &space,
			       (ADDR32 *) &ack_addr,
			       (INT32 *) &set_count,
			       (INT32 *) &set_type);

  BreakInst = (ADDR32) - 1;

  if (!strcmp(TargetType, "eb29k")) {	/* For EB29K */
    /* UDIRead(); read instruction */
    if ((UDIretval = UDIRead(addr,
			     (UDIHostMemPtr) &BreakInst,
			     (UDICount) 4,
			     (UDISizeT) 1,
			     (UDICount *) &ack_addr,
			     (UDIBool) FALSE)) != UDINoError)	/* 29K endian */
      return (UDIretval);
  };

  add_to_bp_table(&newid, space, addr.Offset, set_count, set_type, BreakInst);
  *break_id = (BreakIdType) newid;
  LastBreakId = newid + 1;	/* ??? */
  return (UDINoError);
}

UDIError 
UDIQueryBreakpoint(break_id, addr, pass_count,
		   bk_type, current_count)
  BreakIdType     break_id;
  UDIResource    *addr;
  UDIInt32       *pass_count;
  UDIBreakType   *bk_type;
  UDIInt32       *current_count;
{
  INT32           space;
  ADDR32          offset;
  INT32           pcount;
  INT32           type;
  INT32           ccount;
  ADDR32          Inst;

  CLEAR_PENDING_STOP

  if (break_id >= LastBreakId)
    return (UDIErrorNoMoreBreakIds);

  if (get_from_bp_table(break_id, &space, &offset, &pcount, &type, &Inst) != 0)
    return (UDIErrorInvalidBreakId);

  Mini_build_bkpt_stat_msg(space, offset);
  SEND_AND_WAIT_ACK (BKPT_STAT_ACK);
  if (ReturnedError == (int) 1) {
    ReturnedError = 0;
    return ((-1) * MONErrErrorStatBreak);
  }
  Mini_unpack_bkpt_stat_ack_msg((INT32 *) &space,
				(ADDR32 *) &offset,
				(INT32 *) &ccount,
				(INT32 *) &type);

  addr->Space = SpaceMap_mm2udi(space);
  addr->Offset = (CPUOffset) offset;
  *pass_count = (UDIInt32) pcount;

  if (type == (INT32) BKPT_29000)
    type = UDIBreakFlagExecute;
  else if (type == BKPT_29050_BTE_0)
    type = (MONBreakFlagHardware | UDIBreakFlagExecute);
  else if (type == BKPT_29050_BTE_1)
    type = (MONBreakTranslationEnabled | MONBreakFlagHardware | UDIBreakFlagExecute);
  *bk_type = (UDIBreakType) type;

  *current_count = (UDIInt32) ccount;

  return (UDINoError);
}

UDIError 
UDIClearBreakpoint(break_id)
  BreakIdType     break_id;
{
  INT32           space;
  ADDR32          offset;
  INT32           count;
  INT32           type;
  ADDR32          Inst;
  UDIResource     addr;	/* EB29K */
  UDIError        UDIretval;	/* EB29K */

  CLEAR_PENDING_STOP

  /* should bkpt be removed from linked list ?? */
  if (get_from_bp_table(break_id, &space, &offset, &count, &type, &Inst) != 0)
    return (UDIErrorInvalidBreakId);

  Mini_build_bkpt_rm_msg(space, offset);
  SEND_AND_WAIT_ACK (BKPT_RM_ACK);
  if (ReturnedError == (int) 1) {
    ReturnedError = 0;
    return ((-1) * MONErrErrorRmBreak);
  }
  Mini_unpack_bkpt_rm_ack_msg(&space, &offset);

  if (!strcmp(TargetType, "eb29k")) {	/* For EB29K */
    /* Write back the original instruction * UDIWrite(Inst); */
    addr.Offset = offset;
    addr.Space = SpaceMap_mm2udi(space);
    if ((UDIretval = UDIWrite((UDIHostMemPtr) &Inst,
			      addr,
			      (UDICount) 4,
			      (UDISizeT) 1,
			      &offset,
			      FALSE)) != UDINoError)
      return (UDIretval);
  };

  remove_from_bp_table(break_id);

  return (UDINoError);
}

UDIError 
UDIGetStdout(buf, bufsize, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          bufsize;
  UDISizeT         *count_done;
{
  static int      chan1_indx = 0;
  UDISizeT          mincount;
  UDISizeT          i;
  char           *temp;
  UDIUInt32	  reg_val;
  UDIError	  UDIretval;


  if ((int) (ProcessorState & 0xFF) != (int) UDIStdoutReady) {
     *count_done = (UDISizeT) 0;
      return (UDINoError);
  };

  temp = (char *) buf;	/* used for copying */
  i = (UDISizeT) chan1_indx;
  if (Channel1_count) {
    mincount = (Channel1_count < (UDISizeT) bufsize) ? Channel1_count :
	(UDISizeT) bufsize;
    for (i = 0; i < mincount; i++) {
      (char) *temp++ = (char) channel1_buffer[chan1_indx];
      chan1_indx = (chan1_indx + 1) % TIP_IO_BUFSIZE;	/* circular buffer */
    }
    *count_done = (UDISizeT) mincount;
    Channel1_count = Channel1_count - mincount;
    TotalDone = TotalDone + (UDIUInt32) mincount;
    if (Channel1_count <= (UDISizeT) 0) {
	/*
	 * The HIF kernel from MiniMON29K release 2.1 expects MONTIP
	 * to send a HIF_CALL_RTN response for a HIF_CALL message, and
	 * a CHANNEL1_ACK response for a CHANNEL1 message, and 
	 * a CHANNEL2_ACK response for a CHANNEL2 message, and
	 * a CHANNEL0 message for a asynchronous input.
	 * The HIF kernel version numbers 0x05 and above support these
	 * features.
	 */
     if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
       if (!NoChan1Ack) {
          Mini_build_channel1_ack_msg(TotalDone); /* send gr96 value */
          if (Mini_msg_send() != SUCCESS)
            return ((-1) * MONErrCantSendMsg);
       }
     } else { /* old HIF kernel */
       if ((UDIretval = Write_Glob_Reg(TotalDone, (int) 96)) != UDINoError)
         return (UDIretval);
       reg_val = (UDIUInt32) 0x80000000;
       if ((UDIretval = Write_Glob_Reg(reg_val, (int) 121)) != UDINoError)
         return (UDIretval);
     }
      TotalDone = (UDIUInt32) 0;
      Channel1_count = (UDISizeT) 0;
      chan1_indx = 0;
    } else {
      return (UDINoError);
    }
  } else {
    *count_done = (UDISizeT) 0;
    TotalDone = (UDIUInt32) 0;
    Channel1_count = (UDISizeT) 0;
    chan1_indx = 0;
	/*
	 * The HIF kernel from MiniMON29K release 2.1 expects MONTIP
	 * to send a HIF_CALL_RTN response for a HIF_CALL message, and
	 * a CHANNEL1_ACK response for a CHANNEL1 message, and 
	 * a CHANNEL2_ACK response for a CHANNEL2 message, and
	 * a CHANNEL0 message for a asynchronous input.
	 * The HIF kernel version numbers 0x05 and above support these
	 * features.
	 */
     if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
       Mini_build_channel1_ack_msg(TotalDone); /* send gr96 value */
       if (Mini_msg_send() != SUCCESS)
         return ((-1) * MONErrCantSendMsg);
     } else { /* old HIF kernel */
       if ((UDIretval = Write_Glob_Reg(TotalDone, (int) 96)) != UDINoError)
         return (UDIretval);
       reg_val = (UDIUInt32) 0x80000000;
       if ((UDIretval = Write_Glob_Reg(reg_val, (int) 121)) != UDINoError)
         return (UDIretval);
     }
  }
  if (StepCmdGiven) {
    ProcessorState = UDIStepped;
    PreviousProcessorState = UDIStepped;
    StepCmdGiven = 0;
  } else {
      if (!BreaksInPlace) {
	PutAllBreakpoints();
	BreaksInPlace = 1;
      }
	/*
	 * The HIF kernel from MiniMON29K release 2.1 expects MONTIP
	 * to send a HIF_CALL_RTN response for a HIF_CALL message, and
	 * a CHANNEL1_ACK response for a CHANNEL1 message, and 
	 * a CHANNEL2_ACK response for a CHANNEL2 message, and
	 * a CHANNEL0 message for a asynchronous input.
	 * The HIF kernel version numbers 0x05 and above support these
	 * features.
	 */
     if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
       ProcessorState = (UDIUInt32) UDIRunning;
       PreviousProcessorState = (UDIUInt32) UDIRunning;
     } else { /* old HIF kernel */
       UDIExecute();	/* sends a GO to the Debugger to start application */
     }
  }
  return (UDINoError);
}

#ifdef	MSDOS
UDIError OldHIFGetStderr( UDIHostMemPtr buf,UDISizeT bufsize,UDISizeT *count_done);
#else
UDIError OldHIFGetStderr();
#endif

UDIError 
UDIGetStderr(buf, bufsize, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          bufsize;
  UDISizeT         *count_done;
{
  static int      chan2_indx = 0;
  UDISizeT          mincount;
  UDISizeT          i;
  char           *temp;

  if ((int) (ProcessorState & 0xFF) != (int) UDIStderrReady) {
    *count_done = (UDISizeT) 0;
    return (UDINoError);
  };
	/*
	 * The HIF kernel from MiniMON29K release 2.1 expects MONTIP
	 * to send a HIF_CALL_RTN response for a HIF_CALL message, and
	 * a CHANNEL1_ACK response for a CHANNEL1 message, and 
	 * a CHANNEL2_ACK response for a CHANNEL2 message, and
	 * a CHANNEL0 message for a asynchronous input.
	 * The HIF kernel version numbers 0x05 and above support these
	 * features.
	 */
     if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
	/*
	 * From MiniMON29K release 2.1 all interactions with 
	 * stdin, stdout, stderr by the application is handled without
	 * invoking the Debugger on the target. Thus, a write to stderr
	 * is implemented by a CHANNEL2 message similar to the CHANNEL1
	 * message for stdout.
	 */
       temp = (char *) buf;	/* used for copying */
       i = (UDISizeT) chan2_indx;
       if (Channel2_count) {
         mincount = (Channel2_count < (UDISizeT) bufsize) ? Channel2_count :
	     (UDISizeT) bufsize;
         for (i = 0; i < mincount; i++) {
           (char) *temp++ = (char) channel2_buffer[chan2_indx];
           chan2_indx = (chan2_indx + 1) % TIP_IO_BUFSIZE;/* circular buffer */
         }
         *count_done = (UDISizeT) mincount;
         Channel2_count = Channel2_count - mincount;
         TotalDone = TotalDone + (UDIUInt32) mincount;
         if (Channel2_count <= (UDISizeT) 0) {
            Mini_build_channel2_ack_msg(TotalDone); /* send gr96 value */
            if (Mini_msg_send() != SUCCESS)
              return ((-1) * MONErrCantSendMsg);
            TotalDone = (UDIUInt32) 0;
            Channel2_count = (UDISizeT) 0;
            chan2_indx = 0;
          } else {
            return (UDINoError);
          }
       } else {
          *count_done = (UDISizeT) 0;
          TotalDone = (UDIUInt32) 0;
          Channel2_count = (UDISizeT) 0;
          chan2_indx = 0;
          Mini_build_channel2_ack_msg(TotalDone); /* send gr96 value */
          if (Mini_msg_send() != SUCCESS)
            return ((-1) * MONErrCantSendMsg);
      }
      if (StepCmdGiven) {
        ProcessorState = UDIStepped;
        PreviousProcessorState = UDIStepped;
        StepCmdGiven = 0;
      } else {
          if (!BreaksInPlace) {
             PutAllBreakpoints();
             BreaksInPlace = 1;
          }
        ProcessorState = (UDIUInt32) UDIRunning;
        PreviousProcessorState = (UDIUInt32) UDIRunning;
      }
     } else { /* old HIF kernel code */
	return (OldHIFGetStderr(buf, bufsize, count_done));
     }	/* old kernel code */

  return (UDINoError);
}

UDIError
OldHIFGetStderr(buf, bufsize, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          bufsize;
  UDISizeT         *count_done;
/*
 * For HIF kernel version 0x04 and lower.
 */
{
  UDIUInt32       count;
  UDIUInt32       done;
  UDIResource     from;
  UDIBool         host_endian;
  UDISizeT          size;
  UDIError        UDIretval;
  UDIUInt32       reg_val;

       /* Lr4_count gives the bytes to be written */
       /* Lr3_addr gives the address in the target */
       if (Lr4_count > (UDIUInt32) 0) {
         count = (Lr4_count < (UDIUInt32) bufsize) ? Lr4_count : (UDIUInt32) bufsize;
         /* read count bytes from Lr3_addr */
         from.Offset = Lr3_addr;
         from.Space = UDI29KDRAMSpace;
         size = 1;
         host_endian = FALSE;
         if ((UDIretval = UDIRead(from,
			          buf,
			          count,
			          size,
			          &done,
			          host_endian)) != UDINoError) {
           return (UDIretval);
         }
         *count_done = (UDISizeT) count;
         Lr4_count = Lr4_count - count;
         Lr3_addr = Lr3_addr + count;
         TotalDone = TotalDone + (UDIUInt32) count;
         if (Lr4_count <= (UDISizeT) 0) {
             if ((UDIretval = Write_Glob_Reg(TotalDone, (int) 96)) != UDINoError)
                  return (UDIretval);
             reg_val = (UDIUInt32) 0x80000000;
             if ((UDIretval = Write_Glob_Reg(reg_val, (int) 121)) != UDINoError)
                  return (UDIretval);
             TotalDone = (UDIUInt32) 0;
	     Lr4_count = (UDIUInt32) 0;
         } else {
           return (UDINoError);
         }
       } else {
         *count_done = (UDISizeT) 0;
         TotalDone = (UDIUInt32) 0;
         if ((UDIretval = Write_Glob_Reg(TotalDone, (int) 96)) != UDINoError)
              return (UDIretval);
         reg_val = (UDIUInt32) 0x80000000;
         if ((UDIretval = Write_Glob_Reg(reg_val, (int) 121)) != UDINoError)
              return (UDIretval);
         Lr4_count = (UDIUInt32) 0;
       };
     
       /* Resume execution  UDIExecute()? */
       if (StepCmdGiven) {
         ProcessorState = UDIStepped;
         PreviousProcessorState = UDIStepped;
         StepCmdGiven = 0;
       } else {
           if (!BreaksInPlace) {
	     PutAllBreakpoints();
	     BreaksInPlace = 1;
           }
         UDIExecute();
       }
  return (UDINoError);
}

#ifdef MSDOS
UDIError OldHIFPutStdin( UDIHostMemPtr buf,UDISizeT count,UDISizeT *count_done);
#else
UDIError OldHIFPutStdin();
#endif

UDIError 
UDIPutStdin(buf, count, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          count;
  UDISizeT         *count_done;
{
     char	*CharPtr;
     UINT32	MinCnt;
     INT32	Code;

     if ((tip_target_config.os_version & 0xf) > 0x6) { /* MiniMON29K 3.0 */
       if ((int) (ProcessorState & 0xFF) != (int) UDIStdinNeeded) {
           /* non-blocking mode asynchronous mode */
	   /* 
	    if asynchronous mode, we sent a channel0 message for
	    every character sent by DFE in this call. 
	    */
	    if (PgmStdinMode & TIP_NBLOCK) { 
               if ((Code = Mini_msg_recv(NONBLOCK)) != FAILURE) {
		   MsgAlreadyReceived=Code;
		   MsgAlreadyInBuffer = 1;
	       } 
               CharPtr = buf;
	       if (!Channel0Busy) {
	         /* send one character and return to DFE */
	         Mini_build_channel0_msg(CharPtr, (INT32) 1);
	         /*
		  * Just send the message here, and wait for the ack later
	         SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	         */
	         if (Mini_msg_send() != SUCCESS) 
		    return((-1) * MONErrCantSendMsg);
#if 0
	         Channel0Busy = 1;	/* never set */
#endif
	       } else {
		 /* save it in channel0_buffer */
	         channel0_buffer[Channel0_count] = (char) *CharPtr;
	         Channel0_count=Channel0_count+1;
	       }
	       *count_done = (UDISizeT) 1;
	       return (UDINoError);
	    } else  if (PgmStdinMode & TIP_ASYNC) {
               if ((Code = Mini_msg_recv(NONBLOCK)) != FAILURE) {
		   MsgAlreadyReceived=Code;
		   MsgAlreadyInBuffer = 1; /* check in UDIWait */
	       } 
               CharPtr = buf;
	       *count_done = (UDISizeT) 0;
	       for ( ; count > 0; count--) {
	            Mini_build_channel0_msg(CharPtr, (INT32) 1);
	           /*
		    * Just send the message here, and wait for the ack later
	           SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	           */
		    if (Mini_msg_send() != SUCCESS)
		      return((-1)*MONErrCantSendMsg);
	            *count_done = (UDISizeT) (*count_done + (UDISizeT) 1);
	            CharPtr++;
	       }
    	       return (UDINoError);
	    }
       } else { /* synchronous mode */
	  /*
	   in synchronous mode, we send all the characters received using
	   stdin_needed_ack_msg, when the processorstate becomes stdinneeded.
	   This is line-buffered mode. So we clear variables
	   after we send.
	   What do we do when DFE sends more characters than we need now?
	   The count_done return value gives number accepted. But who keeps
	   the rest.
	   Otherwise, what do we do???????
	   */
	  if (PgmStdinMode & TIP_NBLOCK) { 
	     /* send one character and return to DFE */
             CharPtr = buf;
             if ((Code = Mini_msg_recv(NONBLOCK)) != FAILURE) {
		   MsgAlreadyReceived=Code;
		   MsgAlreadyInBuffer = 1;
	     }
	     *count_done = (UDISizeT) 1;
	     Mini_build_channel0_msg(CharPtr, (INT32) 1);
	     /*
	      * Send the message now and wait for the ack later.
	     SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	      */
	     if (Mini_msg_send() != SUCCESS)
	       return ((-1)*MONErrCantSendMsg);
	     return (UDINoError);
	  }; 

	  MinCnt = ((UINT32) StdinCharsNeeded > (UINT32) count) ?
		      (UINT32) count : (UINT32) StdinCharsNeeded;
	  Mini_build_stdin_needed_ack_msg (MinCnt, buf);
          if (Mini_msg_send() != SUCCESS)
            return ((-1) * MONErrCantSendMsg);
	  *count_done = (UDISizeT) MinCnt;
	  StdinCharsNeeded = 0;		/* reset to zero ?? */
          if (StepCmdGiven) {
            ProcessorState = UDIStepped;
            PreviousProcessorState = UDIStepped;
            StepCmdGiven = 0;
          } else {
            if (!BreaksInPlace) {
	      PutAllBreakpoints();
	      BreaksInPlace = 1;
            }
	    ProcessorState = UDIRunning;
	    PreviousProcessorState = UDIRunning;
	  }
	  return (UDINoError);
       }
     } else if ((tip_target_config.os_version & 0xf) > 4) { /* pre-release */
	/*
	 * The HIF kernel from MiniMON29K release 2.1 expects MONTIP
	 * to send a HIF_CALL_RTN response for a HIF_CALL message, and
	 * a CHANNEL1_ACK response for a CHANNEL1 message, and 
	 * a CHANNEL2_ACK response for a CHANNEL2 message, and
	 * a CHANNEL0 message for a asynchronous input.
	 * The HIF kernel version numbers 0x05 and above support these
	 * features.
	 */
       /* Send CHANNEL0 message depending on StdinMode. */
       CharPtr = buf;
       if (PgmStdinMode == TIP_COOKED) { /* default line buffered */
	 /*
	  * send a line of input using channel0 
	  * Check for '\n' sent from DFE.
	  */
	  if ((int) *CharPtr == (int) 8) {/* backspace */
	    Channel0_count=Channel0_count-1;
	  } else if ((int) *CharPtr == (int) 127) {/* delete */
	    Channel0_count=Channel0_count-1;
#ifdef MSDOS
	  } else if ((int) *CharPtr == (int) 10) {/* \n */
	    /* simply return, no change. already padded. */
	    *count_done = count;
	    return (UDINoError);
	  } else if ((int) *CharPtr == (int) 13) {/* end of line */
	    channel0_buffer[Channel0_count] = (char) *CharPtr;
	    Channel0_count=Channel0_count+1;
	    channel0_buffer[Channel0_count] = (char) 10; /* add \n */
	    Channel0_count=Channel0_count+1;
	    Mini_build_channel0_msg(channel0_buffer, Channel0_count);
	    SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	    Channel0_count = 0;	/* reset */
	    *count_done = count;
#else	/* MSDOS */
	  } else if ((int) *CharPtr == (int) 13) {/* end of line */
	    /* simply return, added on \n */
	    *count_done = count;
	    return (UDINoError);
	  } else if ((int) *CharPtr == (int) 10) {/* \n */
	    channel0_buffer[Channel0_count] = (char) 13; /* add \r */
	    Channel0_count=Channel0_count+1;
	    channel0_buffer[Channel0_count] = (char) *CharPtr;
	    Channel0_count=Channel0_count+1;
	    Mini_build_channel0_msg(channel0_buffer, Channel0_count);
	    SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	    Channel0_count = 0;	/* reset */
#endif /* MSDOS */
	  } else { /* store it in buffer here */
	    channel0_buffer[Channel0_count] = (char) *CharPtr;
	    Channel0_count=Channel0_count+1;
	    *count_done = count;
	  }
	  return (UDINoError);
       } else if (PgmStdinMode == TIP_RAW) { /* for other modes of input */
	    channel0_buffer[Channel0_count] = (char) *CharPtr;
	    Channel0_count=Channel0_count+1;
	    Mini_build_channel0_msg(channel0_buffer, Channel0_count);
	    SEND_AND_WAIT_ACK(CHANNEL0_ACK);
	    Channel0_count = 0;	/* reset */
	    *count_done = count;
	    return (UDINoError);
       } else { /* for other modes of input */
	 /* NOT IMPLEMENTED */
	    return (UDINoError);
       }
     } else { /* old HIF kernel */
       return(OldHIFPutStdin(buf, count, count_done));
     }
}

UDIError
OldHIFPutStdin(buf, count, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          count;
  UDISizeT         *count_done;
{
  UDIResource     to;
  UDIError        retval;
  UDIBool         hostendian;
  UDICount        mincount,
                  bytes_ret;
  UDIUInt32       reg_val;
  UDISizeT          size;

  if ((int) (ProcessorState & 0xFF) != (int) UDIStdinNeeded) {
    *count_done = (UDISizeT) 0;
    return (UDINoError);
  };
  /* Lr4_count has count requested */
  /* Lr3_addr has the destination */
  if (Lr4_count > (UDIUInt32) 0) {
    mincount = ((UDICount) count < (UDICount) Lr4_count) ?
	(UDICount) count :
	(UDICount) Lr4_count;
    to.Space = (CPUSpace) UDI29KDRAMSpace;
    to.Offset = (CPUOffset) Lr3_addr;
    size = (UDISizeT) 1;
    hostendian = FALSE;
    if ((retval = UDIWrite(buf,
			   to,
			   mincount,
			   size,
			   &bytes_ret,
			   hostendian)) != UDINoError) {
      return ((UDIError) retval);
    };
    Lr4_count = (UDIUInt32) 0;
    *count_done = (UDISizeT) bytes_ret;
  } else {
    Lr4_count = (UDIUInt32) 0;
    *count_done = (UDISizeT) 0;
  };

  /*
   * ASSUMPTION: It's always a non-blocking read & this function is called
   * only when app. needs data. So, write the number of bytes read to gr96 on
   * the target.
   */
  /* Write gr96  set above */
  /* gr96 */
  reg_val = (UDIUInt32) * count_done;	/* same as mincount */
  if ((retval = Write_Glob_Reg(reg_val, (int) 96)) != UDINoError)
    return (retval);

  /* Write Gr121 */
  reg_val = (UDIUInt32) 0x80000000;
  if ((retval = Write_Glob_Reg(reg_val, (int) 121)) != UDINoError)
    return (retval);

  if (StopFlag) {
     STOP_SIG_HDLR
     ProcessorState = (UDIUInt32) UDIStopped;
     PreviousProcessorState = (UDIUInt32) UDIStopped;
     return (UDINoError);
  }; 
  /* Resume execution UDIExecute()? */
  if (StepCmdGiven) {
    ProcessorState = UDIStepped;
    PreviousProcessorState = UDIStepped;
    StepCmdGiven = 0;
  } else {
      if (!BreaksInPlace) {
	PutAllBreakpoints();
	BreaksInPlace = 1;
      }
    UDIExecute();
  }

  return (UDINoError);
}

UDIError 
UDIStdinMode(mode)
 UDIMode   *mode;
{
  *mode = (UDIMode) (PgmStdinMode);
  /* restore ProcessorState from saved value in PreviousState */
  ProcessorState = PreviousProcessorState;
  return (UDINoError);
}

UDIError 
UDIPutTrans(buf, count, count_done)
  UDIHostMemPtr   buf;
  UDISizeT          count;
  UDISizeT         *count_done;
{
  char	*tip_token;

  tip_token = strtok ((char *) buf, " \t,;\n\r");
  if (tip_token == NULL)
        return ((-1) * MONErrUnknownTIPCmd);

  if (strcmp (tip_token, "tip") == 0)  {
    tip_token = strtok ((char *) 0, " \t,;\n\r");
    if (tip_token == NULL)
       return ((-1) * MONErrUnknownTIPCmd);
    else {
      if (strcmp(tip_token, "lpt=1") == 0) {
#ifdef MSDOS
	set_lpt();
#endif
	use_parport = 1;
      } else if (strcmp(tip_token, "lpt=0") == 0) {
#ifdef MSDOS
	unset_lpt();
#endif
	use_parport = 0;
      } else
       return ((-1) * MONErrUnknownTIPCmd);
    }
    return (UDINoError);
  } else {
    return ((-1) * MONErrUnknownTIPCmd);
  }

}

UDIError 
UDIGetTrans(buf, bufsize, count)
  UDIHostMemPtr   buf;
  UDISizeT          bufsize;
  UDISizeT         *count;
{
  CLEAR_PENDING_STOP
  return (UDIErrorUnsupportedService);
}

UDIError 
UDITransMode(mode)
  UDIMode   *mode;
{
  CLEAR_PENDING_STOP
  return (UDIErrorUnsupportedService);
}

/* Map Space conversion functions */

static INT32 
SpaceMap_udi2mm(space)
  CPUSpace        space;
{
  switch (space) {
   case UDI29KDRAMSpace:
    return ((INT32) D_MEM);
   case UDI29KIOSpace:
    return ((INT32) I_O);
   case UDI29KCPSpace0:
    return ((INT32) SPECIAL_REG);
   case UDI29KCPSpace1:
    return ((INT32) SPECIAL_REG);
   case UDI29KIROMSpace:
    return ((INT32) I_ROM);
   case UDI29KIRAMSpace:
    return ((INT32) I_MEM);
   case UDI29KLocalRegs:
    return ((INT32) LOCAL_REG);
   case UDI29KGlobalRegs:
    return ((INT32) GLOBAL_REG);
   case UDI29KRealRegs:
    return ((INT32) GLOBAL_REG);
   case UDI29KSpecialRegs:
    return ((INT32) SPECIAL_REG);
   case UDI29KTLBRegs:
    return ((INT32) TLB_REG);
   case UDI29KACCRegs:
    return ((INT32) SPECIAL_REG);
   case UDI29KICacheSpace:
    return ((INT32) I_CACHE);
   case UDI29KAm29027Regs:
    return ((INT32) COPROC_REG);
   case UDI29KPC:
    return ((INT32) PC_SPACE);
   case UDI29KDCacheSpace:
    return ((INT32) D_CACHE);
   case VERSION_SPACE:
    return ((INT32) VERSION_SPACE);
   default:
    return (FAILURE);
  };
}

static CPUSpace 
SpaceMap_mm2udi(space)
  INT32           space;
{
  switch (space) {
   case LOCAL_REG:
    return ((CPUSpace) UDI29KLocalRegs);
   case ABSOLUTE_REG:
    return ((CPUSpace) UDI29KGlobalRegs);
   case GLOBAL_REG:
    return ((CPUSpace) UDI29KGlobalRegs);
   case SPECIAL_REG:
    return ((CPUSpace) UDI29KSpecialRegs);
   case TLB_REG:
    return ((CPUSpace) UDI29KTLBRegs);
   case COPROC_REG:
    return ((CPUSpace) UDI29KAm29027Regs);
   case I_MEM:
    return ((CPUSpace) UDI29KIRAMSpace);
   case D_MEM:
    return ((CPUSpace) UDI29KDRAMSpace);
   case I_ROM:
    return ((CPUSpace) UDI29KIROMSpace);
   case D_ROM:
    return ((CPUSpace) UDI29KIROMSpace);
   case I_O:
    return ((CPUSpace) UDI29KIOSpace);
   case I_CACHE:
    return ((CPUSpace) UDI29KICacheSpace);
   case D_CACHE:
    return ((CPUSpace) UDI29KDCacheSpace);
   case PC_SPACE:
    return ((CPUSpace) UDI29KPC);
   case A_SPCL_REG:
    return ((CPUSpace) UDI29KSpecialRegs);
   default:
    return (FAILURE);
  }
}

/* Miscellaneous UDI support functions */

static int
Reset_Processor()
{
  INT32           MsgCode;
  BreakIdType		i;

  CLEAR_PENDING_STOP

  Mini_build_reset_msg();
  if (Mini_msg_send() != SUCCESS)
    return ((-1) * MONErrCantSendMsg);

  MsgCode = Wait_For_Ack();
  if (MsgCode == ABORT_FAILURE)
     return (UDIErrorAborted);
  else if (MsgCode == FAILURE)
	return ((-1) * MONErrNoSynch);

  RefreshRegs = 1;
  /* Clear all breakpoints */
  BreaksInPlace = 0;
  for (i = 1; i < LastBreakId; i++)
     remove_from_bp_table(i);

  return (SUCCESS);
}

static int 
parse_string(string)
  char           *string;
{
  char           *s;

  if ((string == NULL) || (strcmp(string,"") == 0))
    return ((-1) * MONErrNullConfigString);

  (void) strcpy (&ConnectString[0], string); /* to preserve the original */

  s = strtok(ConnectString, " ");

  while (s != NULL) {
    if ((s[0] == '-') && (s[1] != '\0') && (s[2] == '\0')) {	/* single letter options */
      switch (s[1]) {
       case 'S':	/* -Supervisor Mode */
	SupervisorMode = 1;	/* always in real mode */
	RealMode = 1;
	ProtectedMode = 0;
	VirtualMode = 0;
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 'R':	/* -Real Mode */
	RealMode = 1;
	ProtectedMode = 0;
	VirtualMode = 0;
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 'P':	/* _Protected mode */
	SupervisorMode = 0;	/* SM mode not supported */
	RealMode = 0;
	VirtualMode = 0;
	ProtectedMode = 1;
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 'V':	/* -Virtual mode */
	SupervisorMode = 0;	/* SM mode not supported */
	RealMode = 0;
	ProtectedMode = 0;
	VirtualMode = 1;
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 'r':	/* core file */
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if ((CoreFile = (char *) malloc(strlen(s) + 1)) == NULL) {
	    return ((-1) * MONErrOutofMemory);	/* EMALLOC ? */
	  };
	  (void) strcpy(CoreFile, s);
	};
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 't':	/* target type */
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if ((TargetType = (char *) malloc(strlen(s) + 1))
	      == NULL) {
	    return ((-1) * MONErrOutofMemory);	/* EMALLOC ? */
	  };
	  (void) strcpy(TargetType, s);
	};
	s = strtok(NULL, " ");	/* get next string */
	break;
       case 'm':	/* message log file */
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if ((Msg_Logfile = (char *) malloc(strlen(s) + 1)) == NULL) {
	    return ((-1) * MONErrOutofMemory);	/* EMALLOC ? */
	  };
	  (void) strcpy(Msg_Logfile, s);
	};
	s = strtok(NULL, " ");	/* get next string */
	break;
       default:	/* unknown */
	return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
      };	/* end switch */
    } else {	/* multiple letter options */
      if (strcmp(s, "-com") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {
	  return (UDIErrorInvalidTIPOption);
	} else {
	  (void) strcpy(tip_config.comm_port, s);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-af") == 0) {
	SendACKFirst = 1;
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-par") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {
	  return (UDIErrorInvalidTIPOption);
	} else {
	  (void) strcpy(tip_config.par_port, s);
	  lpt_initialize = 1;
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-le") == 0) { /* little endian target */
	tip_target_config.P29KEndian = LITTLE;
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-re") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%d", &MessageRetries) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-na") == 0) { /* no need to ack channel1 msg */
	NoChan1Ack = 1;
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-nt") == 0) {
	NoStepReqd = 1;
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-mbuf") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%d", &MaxMsgBufSize) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-del") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%d", &DelayFactor) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-bl") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%d", &BlockCount) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-to") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%ld", &TimeOut) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-seg") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%lx", &tip_config.PC_mem_seg) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-nblock") == 0) { /* specify NBLOCK Stdin Mode */
	PgmStdinMode = TIP_NBLOCK;
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-port") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  if (sscanf(s, "%lx", &tip_config.PC_port_base) != 1)
	    return (UDIErrorInvalidTIPOption);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else if (strcmp(s, "-baud") == 0) {
	s = strtok(NULL, " ");
	if (s == NULL) {	/* error */
	  return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
	} else {
	  (void) strcpy(tip_config.baud_rate, s);
	};
	s = strtok(NULL, " ");	/* get next string */
      } else	/* unknown option */
	return (UDIErrorInvalidTIPOption);	/* UNKNOWN */
    }
  };	/* end while */
  return ((int) 0); /* SUCCESS */
}

static int
write_args(argstring, argstart, datahigh)
  char           *argstring;
  ADDR32          argstart;
  ADDR32         *datahigh;
{
  char           *argvstring[25];
  int             i;
  char           *s;

  i = 0;
  if (argstring == NULL) {
    s = strtok(argstring, " \t\n\r");
    argvstring[i] = s;
  } else {
     (void) strcpy (&TempArgString[0], argstring);
     s = strtok (&TempArgString[0], " \t\n\r");
     argvstring[i] = s;
     if (s != NULL) {
        while ((s = strtok((char *) 0, " \t\n\r"))) {
          i++;
          argvstring[i] = s;
          (void) strcpy(argvstring[i], s);
        };
        /* add the final NULL */ /* i is the argc count */
        argvstring[++i] = NULL;
     }
  }
  return (write_argv(i, argvstring, argstart, datahigh));
}

static int
write_argv(arg_count, arg_ptr, argstart, hi_data)
  int             arg_count;
  char           *arg_ptr[];
ADDR32          argstart;
ADDR32         *hi_data;

{

  int             i;

  UDIError        retval;
  UDIResource     to;
  UDIBool         hostendian;
  UDICount        count,
                  bytes_ret;
  UDISizeT          size;
  UDIUInt32       dataend;	/* start address for heap */
  UDIUInt32       tmp_dataend;	/* start address for heap */

  /*
   * * Write args to target
   */

  /* Set init.data_end to start of arg string space */
  /* (saving room for the array of pointers) */
  dataend = argstart + (arg_count + 1) * sizeof(CPUOffset);

  for (i = 0; i < arg_count; i = i + 1) {

    /* Write arg_ptr[i] pointer (Am29000 address) to target */
    tmp_dataend = dataend;
    /* We might have to change the endian of the address */
    if (tip_target_config.P29KEndian != tip_target_config.TipEndian) {
      tip_convert32((BYTE *) &(tmp_dataend));
    }
    to.Offset = argstart + (i * sizeof(CPUOffset));
    to.Space = UDI29KDRAMSpace;
    count = (UDICount) 1;
    size = (UDISizeT) sizeof(CPUOffset);
    hostendian = FALSE;	/* ???? */
    if ((retval = UDIWrite((UDIHostMemPtr) &tmp_dataend,
			   to,
			   count,
			   size,
			   &bytes_ret,
			   hostendian)) != UDINoError) {
      return (retval);
    };
    /* Continue if SUCCESSful */
    /* Write arg_ptr[i] to target */
    to.Offset = dataend;
    to.Space = UDI29KDRAMSpace;
    count = (UDICount) strlen(arg_ptr[i]) + 1;
    size = (UDISizeT) 1;
    hostendian = FALSE;
    if ((retval = UDIWrite(arg_ptr[i],
			   to,
			   count,
			   size,
			   &bytes_ret,
			   hostendian)) != UDINoError) {
      return (retval);
    };

    dataend = dataend + strlen(arg_ptr[i]) + 1;

  }	/* end for loop */

  /* return dataend */
  *hi_data = dataend;
  /* Write NULL pointer at end of argv array */
  to.Offset = argstart + arg_count * sizeof(CPUOffset);
  to.Space = UDI29KDRAMSpace;
  count = (UDICount) sizeof(CPUOffset);
  size = (UDISizeT) 1;
  hostendian = FALSE;

  if ((retval = UDIWrite("\0\0\0",
			 to,
			 count,
			 size,
			 &bytes_ret,
			 hostendian)) != UDINoError) {
    return (retval);
  };
  return (UDINoError);
}

void
set_stdin_needed(hif_lr3, hif_lr4)
  ADDR32          hif_lr3;
  UDICount        hif_lr4;
{
  Lr3_addr = (CPUOffset) hif_lr3;
  Lr4_count = (UDIUInt32) hif_lr4;
  ProcessorState = (UDIUInt32) UDIStdinNeeded;
  PreviousProcessorState = (UDIUInt32) UDIStdinNeeded;
}

void
set_stdout_ready(hif_lr3, hif_lr4)
  ADDR32          hif_lr3;
  UDICount        hif_lr4;
{
  Lr3_addr = (CPUOffset) hif_lr3;
  Lr4_count = (UDIUInt32) hif_lr4;
  ProcessorState = (UDIUInt32) UDIStdoutReady;
  PreviousProcessorState = (UDIUInt32) UDIStdoutReady;
}

void
set_stderr_ready(hif_lr3, hif_lr4)
  ADDR32          hif_lr3;
  UDICount        hif_lr4;
{
  Lr3_addr = (CPUOffset) hif_lr3;
  Lr4_count = (UDIUInt32) hif_lr4;
  ProcessorState = (UDIUInt32) UDIStderrReady;
  PreviousProcessorState = (UDIUInt32) UDIStderrReady;
}

static  INT32
Wait_For_Ack( /* retries */ )
{
  INT32           code;
  UINT32		  count;

  count=(UINT32) 1;

  code = FAILURE;
  while ((code == FAILURE) && (count < TimeOut)) {
    code = Mini_msg_recv(BLOCK);
    count = count + 10;
    /* Check for user interrupt */
    SIGINT_POLL
    if (StopFlag) {
       STOP_SIG_HDLR
       ProcessorState = (UDIUInt32) UDIStopped;
       PreviousProcessorState = (UDIUInt32) UDIStopped;
       return (ABORT_FAILURE);
    }; 
  };
  return (code);
}

static  INT32
CheckForMsg(time)
  INT32           time;
{
  INT32             i;
  INT32           Code;
  int             ForEver;


  ForEver = 0;
  if (time == (UDIInt32) UDIWaitForever) 
    ForEver = 1;
  else 
   if (RemoteTarget == 1)	/* remote targets */
#ifdef MSDOS
       time = time*100;
#else
       time = time;
#endif

  i = 0;
  while ((i <= time) || ForEver) {
    /* Check for user interrupt */
    SIGINT_POLL
    if (StopFlag) {
       STOP_SIG_HDLR
       ProcessorState = (UDIUInt32) UDIStopped;
       PreviousProcessorState = (UDIUInt32) UDIStopped;
       return (ABORT_FAILURE);
    }; 
    if ((Code = Mini_msg_recv(NONBLOCK)) != FAILURE)
      return (Code);
    i = i + 1;
  }
  return (FAILURE);
}

static  void
process_target_msg(msgcode)
  INT32           msgcode;
{
  switch (msgcode) {
   case HALT:
    if (BreaksInPlace)
      ResetAllBreakpoints();
    BreaksInPlace = 0;
    process_HALT_msg();
    break;
   case CHANNEL1:
    process_CHAN1_msg();
    break;
   case CHANNEL2:
    process_CHAN2_msg();
    break;
   case CHANNEL0_ACK:
    (void) process_chan0_ack();
    break;
   case HIF_CALL:
    (void) process_HIF_msg();
    break;
   case STDIN_NEEDED_REQ:
    (void) process_stdin_needed_req();
    break;
   case STDIN_MODE_REQ:
    (void) set_stdin_mode();
    break;
   case ERROR:
    if (BreaksInPlace)
      ResetAllBreakpoints();
    BreaksInPlace = 0;
    process_ERR_msg();
    break;
   default:
    if (BreaksInPlace)
      ResetAllBreakpoints();
    BreaksInPlace = 0;
    ProcessorState = (UDIUInt32) UDIHalted;
    PreviousProcessorState = (UDIUInt32) UDIHalted;
    fprintf(stderr, "FATAL: a unknown msg 0x%lx\n", msgcode);
    (void)print_recv_bytes();
    break;
  };
}

static INT32
process_chan0_ack()
{
  char	nextchar;

  Channel0Busy = 0;
  if (Channel0_count > 0) {
    Channel0_count = Channel0_count - 1;
    nextchar = channel0_buffer[Channel0_count];
    Mini_build_channel0_msg(&nextchar, (INT32) 1);
    if (Mini_msg_send() != SUCCESS) {
        return((-1) * MONErrCantSendMsg);
    }
#if 0
    Channel0Busy=1;	/* never set */
#endif
  }
  return (UDINoError);
}

static  void
process_HALT_msg()
{
  INT32           mspace;
  ADDR32          pc0,
                  pc1;
  INT32           trap_number;
  INT32		type;
  ADDR32	Inst;
  BreakIdType	break_id;
  INT32		count;


  Mini_unpack_halt_msg(&mspace, &pc0, &pc1, &trap_number);
  if (trap_number == (INT32) 0) {
    if ((break_id = (BreakIdType) is_breakpt_at(mspace, pc1)) 
						> (BreakIdType) 0) {
      ProcessorState = (UDIUInt32) UDIBreak;
      PreviousProcessorState = (UDIUInt32) UDIBreak;
      if ((get_from_bp_table(break_id, &mspace, &pc1, 
				     &count, &type, &Inst) == 0) &&
				     (count < (INT32) 0))
         remove_from_bp_table(break_id);
    } else
      ProcessorState = (UDIUInt32) UDITrapped;
      PreviousProcessorState = (UDIUInt32) UDITrapped;
  } else if (trap_number == 15) {	/* Trace */
    ProcessorState = (UDIUInt32) UDIStepped;
    PreviousProcessorState = (UDIUInt32) UDIStepped;
    StepCmdGiven = 0;	/* step complete */
  } else if (trap_number == 75) {
    ProcessorState = (UDIUInt32) UDIBreak;
    PreviousProcessorState = (UDIUInt32) UDIBreak;
  } else if (trap_number & 0x1000) { /* HIF specific reason */
    if ((trap_number & 0xffff) == 0x1000) { /* HIF exit */
       ProcessorState = (UDIUInt32) (UDIExited | 
					((trap_number & 0xffff0000)>>8));
       PreviousProcessorState = ProcessorState;
    } else { /* HIF error */
       ProcessorState = (UDIUInt32) (UDIHalted | (trap_number & 0xffff0000));
       PreviousProcessorState = ProcessorState;
    }
  } else {
    ProcessorState = (UDIUInt32) (UDITrapped | (trap_number << 8));
    PreviousProcessorState = ProcessorState;
  }
}

static	void
set_stdin_mode()
{
  INT32	mode;

  Mini_unpack_stdin_mode_msg(&mode);
  Mini_build_stdin_mode_ack_msg(PgmStdinMode);
  PgmStdinMode = mode;
  PreviousProcessorState = ProcessorState;	/* save current state */
  ProcessorState = (UDIUInt32) (UDIStdinModeX);
  if (Mini_msg_send() != SUCCESS) {
      ProcessorState = (UDIUInt32) UDIHalted;
  }
  return;
}

static	void
process_stdin_needed_req()
{
  Mini_unpack_stdin_needed_msg(&StdinCharsNeeded);
  /* upper 24 bits gives number needed */
  ProcessorState = (UDIUInt32) (UDIStdinNeeded | (StdinCharsNeeded << 8));
  PreviousProcessorState = ProcessorState;
}

static  void
process_CHAN1_msg()
{
  INT32           count;

  Mini_unpack_channel1_msg((BYTE *) channel1_buffer, &count);
  Channel1_count = (UDISizeT) count;
  /* upper 24 bits gives number to output */
  ProcessorState = (UDIUInt32) (UDIStdoutReady | (Channel1_count << 8));
  PreviousProcessorState = ProcessorState;
}

static  void
process_CHAN2_msg()
{
  INT32           count;

  Mini_unpack_channel2_msg((BYTE *) channel2_buffer, &count);
  Channel2_count = (UDISizeT) count;
  /* upper 24 bits gives number to output */
  ProcessorState = (UDIUInt32) (UDIStderrReady | (Channel2_count << 8));
  PreviousProcessorState = ProcessorState;
}

static  void
process_HIF_msg()
{
  INT32           svc_nm,
                  lr2,
                  lr3,
                  lr4,
                  gr96,
                  gr97,
                  gr121;
  int             retval;
  INT32		MsgCode;

  Mini_unpack_hif_msg(&svc_nm, &lr2, &lr3, &lr4);
  if ((svc_nm == (INT32) HIF_read) && (lr2 == (INT32) 0)) {	/* stdin */
    set_stdin_needed(lr3, lr4);
    return;
  } else if ((svc_nm == (INT32) HIF_write) && (lr2 == (INT32) 2)) {	/* stderr */
    set_stderr_ready(lr3, lr4);
    return;
  } else {
    retval = service_HIF(svc_nm, lr2, lr3, lr4, &gr96, &gr97, &gr121);
    if (retval == (INT32) -1) { /* service failed */
      ProcessorState = (UDIUInt32) UDIHalted;
      PreviousProcessorState = ProcessorState;
      return;
    } ;
    if (svc_nm == HIF_exit) {
	if (BreaksInPlace) {	/* For EB29K */
	  BreaksInPlace = 0;
	  ResetAllBreakpoints();
	};
       if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
	  if ((tip_target_config.os_version & 0xf) > 0x6) { /* send hif rtn */
	    Mini_build_hif_rtn_msg(svc_nm, gr121, gr96, gr97);
            if (Mini_msg_send() != SUCCESS) {
	      ProcessorState = (UDIUInt32) UDIHalted;
	      PreviousProcessorState = ProcessorState;
	      return;
	    }
      	    MsgCode = Wait_For_Ack();	/* debug core sends a HALT msg */
      	    if (MsgCode == ABORT_FAILURE) {
	      ProcessorState = (UDIUInt32) UDIHalted;
	      PreviousProcessorState = ProcessorState;
      	    } else if (MsgCode == FAILURE) {
	      ProcessorState = (UDIUInt32) UDIHalted;
	      PreviousProcessorState = ProcessorState;
	    } else {
	      ProcessorState = (UDIUInt32) (UDIExited | (lr2 << 8));
	      PreviousProcessorState = ProcessorState;
	    }
	    return;
	  } else {
	    ProcessorState = (UDIUInt32) (UDIExited | (lr2 << 8));
	    PreviousProcessorState = ProcessorState;
            return;
	  }
       } else { /* old HIF kernel */
	  exitstat = (int) lr2;
          if (Write_Glob_Reg(gr121, (int) 121) != UDINoError) {
	      ProcessorState = (UDIUInt32) UDIHalted;
	      PreviousProcessorState = ProcessorState;
	      return;
          }
	  ProcessorState = (UDIUInt32) (UDIExited | (exitstat << 8));
	  PreviousProcessorState = ProcessorState;
          return;
       }
    } else {	/* not a HIF exit */
       if ((tip_target_config.os_version & 0xf) > 4) { /* new HIF kernel */
	  Mini_build_hif_rtn_msg(svc_nm, gr121, gr96, gr97);
          if (Mini_msg_send() != SUCCESS) {
	    ProcessorState = (UDIUInt32) UDIHalted;
	    PreviousProcessorState = ProcessorState;
	    return;
	  }
	  ProcessorState = (UDIUInt32) UDIRunning;
	  PreviousProcessorState = ProcessorState;
	  return;
       } else { /* old HIF kernel */
          if (Write_Glob_Reg(gr96, (int) 96) != UDINoError) {
	    ProcessorState = (UDIUInt32) UDIHalted;
	    PreviousProcessorState = ProcessorState;
          }
          if (svc_nm == (INT32) HIF_gettz) {
	    if (Write_Glob_Reg(gr97, (int) 97) != UDINoError) {
	      ProcessorState = (UDIUInt32) UDIHalted;
	      PreviousProcessorState = ProcessorState;
	    }
          }
          if (Write_Glob_Reg(gr121, (int) 121) != UDINoError) {
	    ProcessorState = (UDIUInt32) UDIHalted;
	    PreviousProcessorState = ProcessorState;
	  }
          /* UDIExecute()? */
          if (StepCmdGiven) {
	    ProcessorState = UDIStepped;
	    PreviousProcessorState = ProcessorState;
          } else {
	      if (!BreaksInPlace) {
	        PutAllBreakpoints();
	        BreaksInPlace = 1;
	      }
	      UDIExecute();
          }
       };
      return;
    }
  }
}

static  void
process_ERR_msg()
{
  ProcessorState = (UDIUInt32) UDIStopped;
  PreviousProcessorState = ProcessorState;
}

static int
Write_Glob_Reg(RegVal, RegNum)
  INT32           RegVal;
  int             RegNum;
{
  UDIResource     to;
  UDISizeT          size;
  UDICount        mincount;
  UDIBool         hostendian;
  UDICount        bytes_ret;
  UDIError        retval;

  to.Space = (CPUSpace) UDI29KGlobalRegs;
  to.Offset = (CPUOffset) RegNum;
  size = (UDISizeT) 4;
  mincount = (UDICount) 1;
  hostendian = FALSE;
  if (tip_target_config.TipEndian != tip_target_config.P29KEndian) {
    tip_convert32((BYTE *) &RegVal);
  }
  if ((retval = UDIWrite((UDIHostMemPtr) &RegVal,
			 to,
			 mincount,
			 size,
			 &bytes_ret,
			 hostendian)) != UDINoError) {
    return ((UDIError) retval);
  };
  return (UDINoError);
}

static int
PutAllBreakpoints()
{
  UDIResource     addr;
  UDIError        UDIretval;
  UDICount        count_done;
  BreakIdType     break_id;
  ADDR32          offset;
  INT32           space;
  INT32           pcount;
  INT32           type;
  ADDR32          Inst;
  UDIUInt32       IllOp;

  if (strcmp (TargetType, "eb29k") == 0) { /* if EB29K */
    for (break_id = 1; break_id < LastBreakId; break_id++) {
      if (get_from_bp_table(break_id, &space, &offset,
			    &pcount, &type, &Inst) == 0) {
        addr.Offset = offset;
        addr.Space = SpaceMap_mm2udi(space);
	/* Read Instruction into Breaktable */
        if (!strcmp(TargetType, "eb29k")) {	
          if ((UDIretval = UDIRead(addr,
			     (UDIHostMemPtr) &Inst,
			     (UDICount) 1,
			     (UDISizeT) 4,
			     (UDICount *) &count_done,
			     (UDIBool) FALSE)) != UDINoError)	/* 29K endian */
            return (UDIretval);
        };
	(void) update_breakpt_at(space, offset, Inst);
        /* UDIWrite(Illop); write illegal opcode instruction */
        IllOp = (UDIUInt32) 0x00ccbbaa;	/* Illegal opcode */
        if ((UDIretval = UDIWrite((UDIHostMemPtr) &IllOp,
				  addr,
				  (UDICount) 1,
				  (UDISizeT) 4,
				  &count_done,
				  TRUE)) != UDINoError)
	  return (UDIretval);
      }
    }
  }
  return (0);
}

static int
ResetAllBreakpoints()
{
  UDIResource     addr;
  UDIError        UDIretval;
  UDICount        count_done;
  BreakIdType     break_id;
  ADDR32          offset;
  INT32           space;
  INT32           pcount;
  INT32           type;
  ADDR32          Inst;

  if (strcmp (TargetType, "eb29k") == 0) { /* if EB29K */
    for (break_id = 1; break_id < LastBreakId; break_id++) {
      if (get_from_bp_table(break_id, &space, &offset,
			    &pcount, &type, &Inst) == 0) {
        /* UDIWrite(); write original instruction */
        addr.Offset = offset;
        addr.Space = SpaceMap_mm2udi(space);
        if ((UDIretval = UDIWrite((UDIHostMemPtr) &Inst,
				  addr,
				  (UDICount) 1,
				  (UDISizeT) 4,
				  &count_done,
				  FALSE)) != UDINoError)
	  return (UDIretval);
      }
    }
  }
  return (0);
}

void
TIPPrintUsage(s)
  char           *s;
{
  fprintf(stderr, "Minimon (UDI 1.2) TIP Version: %s Date: %s\n", TIPVERSION, TIPDATE);
  fprintf(stderr, "List of Valid MONTIP options are:\n");
  fprintf(stderr, "-t <target_if_type> - the target interface type MUST be specified\n");
  fprintf(stderr, "[-r <Minimon_OS_linked_object>] - ROM file to be downloaded, if any\n");
  fprintf(stderr, "[-m <msg_log_filename>] - file to log messages between TIP and target\n");
  fprintf(stderr, "[-com <serial_comm_port>] -communication port to use, ex: -com com1:\n");
  fprintf(stderr, "[-par <paralle_port>] -parallel port for download , ex: -par lpt1:\n");
  fprintf(stderr, "[-re <retries_for_a_msg>] - number of retries\n");
  fprintf(stderr, "[-le] - specifies target is little endian\n");
  fprintf(stderr, "[-mbuf <msg_bufsize_to_use>] - maximum message buffer size\n");
  fprintf(stderr, "[-bl <msg_block_loopcount>] - block count while receiving\n");
  fprintf(stderr, "[-to <timeout_loopcount>] - timeout while receiving\n");
  fprintf(stderr, "[-seg <PC_mem_seg_addr_in_hex>] - PC memory segment base address, ex: -seg D800\n");
  fprintf(stderr, "[-port <PC_port_base_addr_in_hex>] - PC i/o port base address, ex: -port 2A0\n");
  fprintf(stderr, "[-baud <baudrate>] - baud rate for serial communications, ex: -baud 38400\n");
  fprintf(stderr, "[-R/P] - physical or protected mode (ONLY with OSBOOT supplied)\n");
  fprintf(stderr, "[-S] - run in supervisor mode (ONLY with OSBOOT supplied)\n");
  exit(1);
}

static INT32
SendConfigWait()
{
    INT32	MsgCode;
    int	count;
    unsigned long	time;

    count = (UINT32) 0;
    MsgCode = (INT32) FAILURE;
    do {
#ifdef	MSDOS
        if (RemoteTarget == 1)
           Mini_reset_comm();  /* reset communications channel */
#endif
      Mini_build_config_req_msg();
      if (Mini_msg_send() != SUCCESS) {
        return ((-1) * MONErrCantSendMsg);
      }
      count = count + 1;
      time = (unsigned long) 1;
      do {
	time = time + 10;
        MsgCode = Mini_msg_recv(BLOCK);
        SIGINT_POLL
        if (Interrupted) {
	   Interrupted = 0;
	   return ((INT32) ABORT_FAILURE);
        }
#ifndef	MSDOS
        if ((MsgCode == FAILURE) && 
	    (RemoteTarget == 1))
           Mini_reset_comm();  /* reset communications channel */
#endif
      } while ((MsgCode == FAILURE) && (time < TimeOut));
    } while ((MsgCode == FAILURE) && (count < MessageRetries)); 
    return (MsgCode);
}

static char *
GetTargetType( Target, FromString)
char	*Target;
char	*FromString;
{
  char		*s;

  if (FromString == NULL)
     return (NULL);

  (void) strcpy (&ConnectString[0], FromString); /* to preserve the original */
  s = strtok(ConnectString, " ");
  while (s != NULL) {
    if ((s[0] == '-') && (s[1] == 't') && (s[2] == '\0')) { /* -t option */
       s = strtok (NULL, " "); /* continue search */
       if (s == NULL)
	  return (NULL);
       else {
	  Target = s;
	  return (s);
       }
    };
    s = strtok (NULL, " "); /* continue search */
  }; /* while */
  return (NULL);
}


static INT32
Mini_load_coff(filename, mspace, sym, Section, quietmode)
   char *filename;
   int   quietmode;
   INT32	sym;
   INT32	Section;
   INT32	mspace;
   {
   unsigned short  COFF_sections;
   INT32  flags;
   INT32  memory_space;
   INT32  address;
   INT32  byte_count;
   INT32  write_count;
   INT32  temp_byte_count;

   struct  filehdr      COFF_header;
   struct  aouthdr      COFF_aout_header;
   struct  scnhdr      COFF_section_header;

   /* Open the COFF input file (if we can) */
   if ((coff_in = FindFile(filename)) == NULL)
      return ((-1) * MONErrCantOpenCoff);

   /* Read in COFF header information */
   if (fread((char *)&COFF_header, sizeof(struct filehdr), 1, coff_in) != 1) {
      fclose(coff_in); return ((-1) * MONErrCantLoadROMfile);
   };


   /* Is it an Am29000 COFF File? */
   if ((COFF_header.f_magic != 0x17a) && (COFF_header.f_magic != 0x7a01) &&
       (COFF_header.f_magic != 0x17b) && (COFF_header.f_magic != 0x7b01)) {
      fclose(coff_in); return ((-1) * MONErrCantLoadROMfile);
   }

   /* Get number of COFF sections */
   if ((COFF_header.f_magic != 0x17a) && (COFF_header.f_magic != 0x017b))
      tip_convert16((BYTE *) &COFF_header.f_nscns);
   COFF_sections = (unsigned short) COFF_header.f_nscns;

   /* Read in COFF a.out header information (if we can) */
   if (COFF_header.f_opthdr > 0) {
      if (fread((char *)&COFF_aout_header, sizeof(struct aouthdr), 
						   1, coff_in) != 1) {
         fclose(coff_in); return ((-1) * MONErrCantLoadROMfile);
      };
      if ((COFF_header.f_magic != 0x17a) && (COFF_header.f_magic != 0x017b)) {
         tip_convert16((BYTE *) &COFF_header.f_opthdr);
      }
   }


   /*
   ** Process COFF section headers
   */

   /* Process all sections */
   while ((int) COFF_sections--) {

      fseek (coff_in, (long) (FILHSZ+(int)COFF_header.f_opthdr+
			      SCNHSZ*(COFF_header.f_nscns-COFF_sections-1)), 
			      FROM_BEGINNING);

      if (fread(&COFF_section_header, 1, SCNHSZ, coff_in) != SCNHSZ) {
          fclose(coff_in); return ((-1) * MONErrCantLoadROMfile);
      }

      if ((COFF_header.f_magic != 0x17a) && (COFF_header.f_magic != 0x017b)) {
         tip_convert32((BYTE *) &(COFF_section_header.s_paddr));
         tip_convert32((BYTE *) &(COFF_section_header.s_scnptr));
         tip_convert32((BYTE *) &(COFF_section_header.s_size));
         tip_convert32((BYTE *) &(COFF_section_header.s_flags));
       }

      address = COFF_section_header.s_paddr;
      byte_count = COFF_section_header.s_size;
      flags = COFF_section_header.s_flags;

      /* Print downloading messages (if necessary) */
      if ((flags == (INT32) STYP_TEXT) || (flags == (INT32) (STYP_TEXT | STYP_ABS))) {
	 memory_space = I_MEM;
      } else if ((flags == (INT32) STYP_DATA) || (flags == (INT32) (STYP_DATA | STYP_ABS)) ||
          (flags == (INT32) STYP_LIT) || (flags == (INT32) (STYP_LIT | STYP_ABS)) ||
          (flags == (INT32) STYP_BSS) || (flags == (INT32) (STYP_BSS | STYP_ABS))) {
	 memory_space = D_MEM;
      } else {
	 flags = (INT32) 0;
      }

      if ((flags == (INT32) STYP_BSS) || (flags == (INT32) (STYP_BSS | STYP_ABS))) {
      /* Clear BSS section */
   	if (flags & Section) {
	   (void) memset ((char *) buffer, (int) '\0', sizeof(buffer));
	   while (byte_count > 0) {
	     write_count = (byte_count < (INT32) sizeof(buffer)) ?
				byte_count : (INT32) sizeof (buffer);
	     if(Mini_write_memory ((INT32) memory_space,
			      (ADDR32) address,
			      (INT32) write_count,
			      (BYTE *) buffer) != SUCCESS) {
   		(void) fclose(coff_in);
		return((-1) * MONErrCantWriteToMem);
	     }
	     address = address + write_count;
	     byte_count = byte_count - write_count;
	   }
	}
      } else if (flags & Section) { /* not a BSS or COmment */
	 if (flags == (INT32) (flags & Section)) {
	   fseek (coff_in, COFF_section_header.s_scnptr, FROM_BEGINNING);
           while (byte_count > 0) {
             temp_byte_count = MIN((INT32) byte_count, (INT32) sizeof(buffer));
             if (fread((char *) buffer, (int) temp_byte_count, 1, coff_in) != 1) {
                fclose(coff_in); return ((-1) * MONErrCantLoadROMfile);
	     };
             /* Write to 29K memory*/
             if (Mini_write_memory ((INT32)  memory_space,
                            	(ADDR32) address,
                            	(INT32)  temp_byte_count,
                            	(BYTE *) buffer) != SUCCESS) {
   	       (void) fclose(coff_in);
	       return((-1) * MONErrCantWriteToMem);
	     };
             address = address + temp_byte_count;
             byte_count = byte_count - temp_byte_count;
           };
	 };
      }
   }  /* end while */

   (void) fclose(coff_in);
   return (SUCCESS);

   }   /* end Mini_loadcoff() */

/* 
** Breakpoint code 
*/

static void
add_to_bp_table(id, space, offset, count, type, inst)
BreakIdType	*id;
INT32	space;
ADDR32	offset;
INT32 	count;
INT32	type;
ADDR32	inst;
{
  static BreakIdType	current_break_id=1;
  struct tip_break_table  	*temp, *temp2;

  if (bp_table == NULL) { /* first element */
    bp_table = (struct tip_break_table *) malloc (sizeof(struct tip_break_table));
    bp_table->id = current_break_id;
    bp_table->offset = offset;
    bp_table->space = space;
    bp_table->count = count;
    bp_table->type = type;
    bp_table->BreakInst = inst;
    bp_table->next = NULL;
  } else {
    temp2 = bp_table;
    temp = (struct tip_break_table *) malloc (sizeof(struct tip_break_table));
    temp->id = current_break_id;
    temp->offset = offset;
    temp->space = space;
    temp->count = count;
    temp->type = type;
    temp->BreakInst = inst;
    temp->next = NULL;
    while (temp2->next != NULL)
      temp2 = temp2->next;
    temp2->next = temp;
  };
  *id = current_break_id;
  current_break_id++;
}

static int 
get_from_bp_table(id, space, offset, count, type, inst)
BreakIdType	id;
INT32	*space;
ADDR32	*offset;
INT32 	*count;
INT32	*type;
ADDR32	*inst;
{
  struct tip_break_table  *temp;

  temp = bp_table;

  while (temp != NULL) {
    if (temp->id == id) {
       *offset = temp->offset;
       *space = temp->space;
       *count = temp->count;
       *type = temp->type;
       *inst = temp->BreakInst;
       return(0);
    } else {
      temp = temp->next;
    };
  }
  return(-1);
}

static int
remove_from_bp_table(id)
BreakIdType	id;
{
  struct  tip_break_table	*temp, *temp2;

  if (bp_table == NULL)
     return (-1);
  else {
    temp = bp_table;
    if (temp->id == id) { /* head of list */
       bp_table = bp_table->next;
       (void) free (temp);
       return (0); /* success */
    } else {
       while (temp->next != NULL) {
          if (temp->next->id == id) {
	     temp2 = temp->next;
	     temp->next = temp->next->next;
	     (void) free (temp2);
	     return (0); /* success */
          } else {
            temp = temp->next;
          }
       };
    }
  };
  return (-1);  /* failed */
}

static int 
update_breakpt_at(space, offset, Inst)
INT32	space;
ADDR32	offset;
ADDR32	Inst;
{
  struct tip_break_table  *temp;

  temp = bp_table;

  while (temp != NULL) {
    if ((temp->space == space) && (temp->offset == offset)) {
       temp->BreakInst = Inst;
       return(0);
    } else {
      temp = temp->next;
    };
  }
  return (-1);
}

static int 
is_breakpt_at(space, offset)
INT32	space;
ADDR32	offset;
{
  struct tip_break_table  *temp;

  temp = bp_table;

  while (temp != NULL) {
    if ((temp->space == space) && (temp->offset == offset)) {
       return((int) temp->id); /* TRUE */
    } else {
      temp = temp->next;
    };
  }
  return(0); /* FALSE */
}

#ifdef	MSDOS
#define	PATH_DELIM	";"
#define	DIR_SEP_CHAR	(char) '\\'
#define	APPEND_PATH	"\\lib\\"
#else
#define	PATH_DELIM	":"
#define	DIR_SEP_CHAR		(char) '/'
#define	APPEND_PATH	"/lib/"
#endif
static FILE  *
FindFile(filename)
char	*filename;
{
  char	*path, *pathptr;
  char	*trypath, *at;
  char	*pathbuf;
  FILE	*filep;

  /* is the filename given already complete? */
  if ((filep = fopen(filename, FILE_OPEN_FLAG)) != NULL) {
      return(filep);
  };

  /* get PATH */
  if ((pathptr = (char *) getenv ("PATH")) == NULL)
     return ((FILE *) NULL);
  if ((path = (char *) malloc ((unsigned int)strlen(pathptr)+1))==NULL)
     return ((FILE *) NULL);
  (void) strcpy(path,pathptr); /* local copy */
  /* alloc buffer */
  if ((pathbuf = (char *) malloc((unsigned int) strlen(path)+
		   strlen("../lib/ ")+strlen(filename)+3)) == NULL)
     return ((FILE *) NULL);

  /* get first item */
  if ((trypath = strtok(path, PATH_DELIM)) == NULL) { /* one item */
     (void) strcpy(pathbuf,path);
     if ((at = strrchr (pathbuf, DIR_SEP_CHAR)) != NULL) {
         (void) strcpy (at, APPEND_PATH);
	 (void) strcat (pathbuf, filename);
         if ((filep = fopen(pathbuf, FILE_OPEN_FLAG)) != NULL) 
	   return (filep);
     } else { /* just append filename */
         (void) strcat (pathbuf, APPEND_PATH);
	 (void) strcat (pathbuf, filename);
         if ((filep = fopen(pathbuf, FILE_OPEN_FLAG)) != NULL) 
	   return (filep);
     };
     return ((FILE *) NULL);
  };

  /* try all items */
  while (trypath != NULL) {
      (void) strcpy (pathbuf, trypath);
      if ((at = strrchr (pathbuf, DIR_SEP_CHAR)) != NULL) {
         (void) strcpy (at, APPEND_PATH);
	 (void) strcat (pathbuf, filename);
         if ((filep = fopen(pathbuf, FILE_OPEN_FLAG)) != NULL) 
	   return (filep);
      } else { /* just append filename */
         (void) strcat (pathbuf, APPEND_PATH);
	 (void) strcat (pathbuf, filename);
         if ((filep = fopen(pathbuf, FILE_OPEN_FLAG)) != NULL) 
	   return (filep);
      };
      trypath = strtok((char *) 0, PATH_DELIM);
  }

  /* didn't succeed */
  return ((FILE *) NULL);
}

void
IntHandler(num)
int	num;
{
  Interrupted = 1;
  return;
}