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

   Written by Steve Chamberlain for Cygnus Support.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* The E7000 is an in-circuit emulator for the Hitachi H8/300-H and
   Hitachi-SH processor.  It has serial port and a lan port.  

   The monitor command set makes it difficult to load large ammounts of
   data over the lan without using ftp - so try not to issue load
   commands when communicating over ethernet; use the ftpload command.

   The monitor pauses for a second when dumping srecords to the serial
   line too, so we use a slower per byte mechanism but without the
   startup overhead.  Even so, it's pretty slow... */

#include "defs.h"
#include "gdbcore.h"
#include "inferior.h"
#include "target.h"
#include "wait.h"
#include "value.h"
#include "command.h"
#include <signal.h>
#include "gdb_string.h"
#include "gdbcmd.h"
#include <sys/types.h>
#include "serial.h"
#include "remote-utils.h"
#include "symfile.h"
#include <time.h>

#if 1
#define HARD_BREAKPOINTS	/* Now handled by set option. */
#define BC_BREAKPOINTS use_hard_breakpoints
#endif

#define CTRLC 0x03
#define ENQ  0x05
#define ACK  0x06
#define CTRLZ 0x1a

extern void report_transfer_performance PARAMS ((unsigned long,
						 time_t, time_t));

extern char *sh_processor_type;

/* Local function declarations.  */

static void e7000_close PARAMS ((int));

static void e7000_fetch_register PARAMS ((int));

static void e7000_store_register PARAMS ((int));

static void e7000_command PARAMS ((char *, int));

static void e7000_login_command PARAMS ((char *, int));

static void e7000_ftp_command PARAMS ((char *, int));

static void e7000_drain_command PARAMS ((char *, int));

static void expect PARAMS ((char *));

static void expect_full_prompt PARAMS ((void));

static void expect_prompt PARAMS ((void));

/* Variables. */

static serial_t e7000_desc;

/* Allow user to chose between using hardware breakpoints or memory. */
static int use_hard_breakpoints = 0; /* use sw breakpoints by default */

/* Nonzero if using the tcp serial driver.  */

static int using_tcp;	/* direct tcp connection to target */
static int using_tcp_remote;	/* indirect connection to target 
				   via tcp to controller */

/* Nonzero if using the pc isa card.  */

static int using_pc;

extern struct target_ops e7000_ops;	/* Forward declaration */

char *ENQSTRING = "\005";

/* Nonzero if some routine (as opposed to the user) wants echoing.
   FIXME: Do this reentrantly with an extra parameter.  */

static int echo;

static int ctrl_c;

static int timeout = 20;

/* Send data to e7000debug.  */

static void
puts_e7000debug (buf)
     char *buf;
{
  if (!e7000_desc)
    error ("Use \"target e7000 ...\" first.");

  if (remote_debug)
    printf("Sending %s\n", buf);

  if (SERIAL_WRITE (e7000_desc, buf, strlen (buf)))
    fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));

  /* And expect to see it echoed, unless using the pc interface */
#if 0
  if (!using_pc)
#endif
    expect (buf);
}

static void
putchar_e7000 (x)
     int x;
{
  char b[1];

  b[0] = x;
  SERIAL_WRITE (e7000_desc, b, 1);
}

static void
write_e7000 (s)
     char *s;
{
  SERIAL_WRITE (e7000_desc, s, strlen (s));
}

static int
normal (x)
     int x;
{
  if (x == '\n')
    return '\r';
  return x;
}

/* Read a character from the remote system, doing all the fancy timeout
   stuff.  */

static int
readchar (timeout)
     int timeout;
{
  int c;

  do
    {
      c = SERIAL_READCHAR (e7000_desc, timeout);
    }
  while (c > 127);

  if (c == SERIAL_TIMEOUT)
    {
      if (timeout == 0)
	return -1;
      echo = 0;
      error ("Timeout reading from remote system.");
    }
  if (remote_debug) 
    {
      putchar (c);
      fflush (stdout);
    }

  return normal (c);
}

#if 0
char *
tl (x)
{
  static char b[8][10];
  static int p;

  p++;
  p &= 7;
  if (x >= ' ') 
    { 
      b[p][0] = x;
      b[p][1] = 0;
    }
  else
    {
      sprintf(b[p], "<%d>", x);
    }

  return b[p];
}
#endif

/* Scan input from the remote system, until STRING is found.  If
   DISCARD is non-zero, then discard non-matching input, else print it
   out.  Let the user break out immediately.  */

static void
expect (string)
     char *string;
{
  char *p = string;
  int c;
  int nl = 0;

  while (1)
    {
      c = readchar (timeout);
      notice_quit ();
      if (quit_flag == 1) 
	{
	  if (ctrl_c)
	    {
	      putchar_e7000(CTRLC);
	      --ctrl_c;
	    }
	  else 
	    {
	      quit ();
	    }
	}
      
      if (c == SERIAL_ERROR)
	{
	  error ("Serial communication error");
	}
      if (echo || remote_debug)
	{
	  if (c == '\r' || c == '\n')
	    {
	      if (!nl)
		putchar ('\n');
	      nl = 1;
	    }
	  else
	    {
	      nl = 0;
	      putchar (c);
	    }
	  fflush (stdout);
	}
      if (normal (c) == normal (*p++))
	{
	  if (*p == '\0')
	    return;
	}
      else
	{
	  p = string;

	  if (normal (c) == normal (string[0]))
	    p++;
	}
    }
}

/* Keep discarding input until we see the e7000 prompt.

   The convention for dealing with the prompt is that you
   o give your command
   o *then* wait for the prompt.

   Thus the last thing that a procedure does with the serial line will
   be an expect_prompt().  Exception: e7000_resume does not wait for
   the prompt, because the terminal is being handed over to the
   inferior.  However, the next thing which happens after that is a
   e7000_wait which does wait for the prompt.  Note that this includes
   abnormal exit, e.g. error().  This is necessary to prevent getting
   into states from which we can't recover.  */

static void
expect_prompt ()
{
  expect (":");
}

static void
expect_full_prompt ()
{
  expect ("\r:");
}

static int
convert_hex_digit (ch)
     int ch;
{
  if (ch >= '0' && ch <= '9')
    return ch - '0';
  else if (ch >= 'A' && ch <= 'F')
    return ch - 'A' + 10;
  else if (ch >= 'a' && ch <= 'f')
    return ch - 'a' + 10;
  return -1;
}

static int
get_hex (start)
     int *start;
{
  int value = convert_hex_digit (*start);
  int try;

  *start = readchar (timeout);
  while ((try = convert_hex_digit (*start)) >= 0)
    {
      value <<= 4;
      value += try;
      *start = readchar (timeout);
    }
  return value;
}

#if 0
/* Get N 32-bit words from remote, each preceded by a space, and put
   them in registers starting at REGNO.  */

static void
get_hex_regs (n, regno)
     int n;
     int regno;
{
  long val;
  int i;

  for (i = 0; i < n; i++)
    {
      int j;

      val = 0;
      for (j = 0; j < 8; j++)
	val = (val << 4) + get_hex_digit (j == 0);
      supply_register (regno++, (char *) &val);
    }
}
#endif

/* This is called not only when we first attach, but also when the
   user types "run" after having attached.  */

static void
e7000_create_inferior (execfile, args, env)
     char *execfile;
     char *args;
     char **env;
{
  int entry_pt;

  if (args && *args)
    error ("Can't pass arguments to remote E7000DEBUG process");

  if (execfile == 0 || exec_bfd == 0)
    error ("No exec file specified");

  entry_pt = (int) bfd_get_start_address (exec_bfd);

#ifdef CREATE_INFERIOR_HOOK
  CREATE_INFERIOR_HOOK (0);	/* No process-ID */
#endif

  /* The "process" (board) is already stopped awaiting our commands, and
     the program is already downloaded.  We just set its PC and go.  */

  clear_proceed_status ();

  /* Tell wait_for_inferior that we've started a new process.  */
  init_wait_for_inferior ();

  /* Set up the "saved terminal modes" of the inferior
     based on what modes we are starting it with.  */
  target_terminal_init ();

  /* Install inferior's terminal modes.  */
  target_terminal_inferior ();

  /* insert_step_breakpoint ();  FIXME, do we need this?  */
  proceed ((CORE_ADDR) entry_pt, -1, 0);	/* Let 'er rip... */
}

/* Open a connection to a remote debugger.  NAME is the filename used
   for communication.  */

static int baudrate = 9600;
static char dev_name[100];

static char *machine = "";
static char *user = "";
static char *passwd = "";
static char *dir = "";

/* Grab the next token and buy some space for it */

static char *
next (ptr)
     char **ptr;
{
  char *p = *ptr;
  char *s;
  char *r;
  int l = 0;

  while (*p && *p == ' ')
    p++;
  s = p;
  while (*p && (*p != ' ' && *p != '\t'))
    {
      l++;
      p++;
    }
  r = xmalloc (l + 1);
  memcpy (r, s, l);
  r[l] = 0;
  *ptr = p;
  return r;
}

static void
e7000_login_command (args, from_tty)
     char *args;
     int from_tty;
{
  if (args)
    {
      machine = next (&args);
      user = next (&args);
      passwd = next (&args);
      dir = next (&args);
      if (from_tty)
	{
	  printf ("Set info to %s %s %s %s\n", machine, user, passwd, dir);
	}
    }
  else
    {
      error ("Syntax is ftplogin <machine> <user> <passwd> <directory>");
    }
}

/* Start an ftp transfer from the E7000 to a host */

static void
e7000_ftp_command (args, from_tty)
     char *args;
     int from_tty;
{
  /* FIXME: arbitrary limit on machine names and such.  */
  char buf[200];

  int oldtimeout = timeout;
  timeout = remote_timeout;

  sprintf (buf, "ftp %s\r", machine);
  puts_e7000debug (buf);
  expect (" Username : ");
  sprintf (buf, "%s\r", user);
  puts_e7000debug (buf);
  expect (" Password : ");
  write_e7000 (passwd);
  write_e7000 ("\r");
  expect ("success\r");
  expect ("FTP>");
  sprintf (buf, "cd %s\r", dir);
  puts_e7000debug (buf);
  expect ("FTP>");
  sprintf (buf, "ll 0;s:%s\r", args);
  puts_e7000debug (buf);
  expect ("FTP>");
  puts_e7000debug ("bye\r");
  expect (":");
  timeout = oldtimeout;
}

static int 
e7000_parse_device(args,dev_name,serial_flag,baudrate) 
    char *args;
    char *dev_name;
    int serial_flag;
    int baudrate;
{
  char junk[128];
  int n = 0;
  if (args && strcasecmp (args, "pc") == 0)
    {
      strcpy (dev_name, args);
      using_pc = 1;
    }
  else 
    {
      /* FIXME! temp hack to allow use with port master -
	     target tcp_remote <device> */
      if (args && strncmp (args, "tcp_remote", 10) == 0) 
        {
	  char com_type[128];
	  n = sscanf (args, " %s %s %d %s", com_type, dev_name, &baudrate, junk);
	  using_tcp_remote=1;
	  n--;
        }
      else if (args) 
	{
	  n = sscanf (args, " %s %d %s", dev_name, &baudrate, junk);
	}

      if (n != 1 && n != 2)
	{
	  error ("Bad arguments.  Usage:\ttarget e7000 <device> <speed>\n\
or \t\ttarget e7000 <host>[:<port>]\n\
or \t\ttarget e7000 tcp_remote <host>[:<port>]\n\
or \t\ttarget e7000 pc\n");
	}

#if !defined(__GO32__) && !defined(_WIN32)
      /* FIXME!  test for ':' is ambiguous */
      if (n == 1 && strchr (dev_name, ':') == 0)
	{
	  /* Default to normal telnet port */
	  /* serial_open will use this to determine tcp communication */
	  strcat (dev_name, ":23");
	}
#endif
      if (!using_tcp_remote && strchr (dev_name, ':'))
        using_tcp = 1;
    }

  return n;
}

static void
e7000_open (args, from_tty)
     char *args;
     int from_tty;
{
  int n;
  int loop;
  int sync;
  int serial_flag;

  target_preopen (from_tty);

  n = e7000_parse_device(args,dev_name,serial_flag,baudrate);

  push_target (&e7000_ops);

  e7000_desc = SERIAL_OPEN (dev_name);

  if (!e7000_desc)
    perror_with_name (dev_name);

  SERIAL_SETBAUDRATE (e7000_desc, baudrate);
  SERIAL_RAW (e7000_desc);

  /* Hello?  Are you there?  */
  sync = 0;
  loop =  0;
  putchar_e7000 (CTRLC);
  while (!sync)
    {
      int c;

      if (from_tty)
	printf_unfiltered ("[waiting for e7000...]\n");

      write_e7000 ("\r");
      c = SERIAL_READCHAR (e7000_desc, 1);
      while (c != SERIAL_TIMEOUT)
	{
	  /* Dont echo cr's */
	  if (from_tty && c != '\r')
	    {
	      putchar (c);
	      fflush (stdout);
	    }
	  if (c == ':')
	    sync = 1;

	  if (loop++ == 20) 
	    {
	      putchar_e7000 (CTRLC);
	      loop = 0;
	    }

	  QUIT ;


	  if (quit_flag)
	    {
	      putchar_e7000 (CTRLC);
	      quit_flag = 0;
	    }
	  c = SERIAL_READCHAR (e7000_desc, 1);
	}
    }
  puts_e7000debug ("\r");

  expect_prompt ();

  puts_e7000debug ("b -\r");

  expect_prompt ();

  if (from_tty)
    printf_filtered ("Remote target %s connected to %s\n", target_shortname,
		     dev_name);

#ifdef GDB_TARGET_IS_H8300
  h8300hmode = 1;
#endif
}

/* Close out all files and local state before this target loses control. */

static void
e7000_close (quitting)
     int quitting;
{
  if (e7000_desc)
    {
      SERIAL_CLOSE (e7000_desc);
      e7000_desc = 0;
    }
}

/* Terminate the open connection to the remote debugger.  Use this
   when you want to detach and do something else with your gdb.  */

static void
e7000_detach (from_tty)
     int from_tty;
{
  pop_target ();		/* calls e7000_close to do the real work */
  if (from_tty)
    printf ("Ending remote %s debugging\n", target_shortname);
}

/* Tell the remote machine to resume.  */

static void
e7000_resume (pid, step, sig)
     int pid, step, sig;
{
  if (step)
    puts_e7000debug ("S\r");
  else
    puts_e7000debug ("G\r");
}

/* Read the remote registers into the block REGS.  

   For the H8/300 a register dump looks like:

   PC=00021A  CCR=80:I*******
   ER0 - ER3  0000000A 0000002E 0000002E 00000000
   ER4 - ER7  00000000 00000000 00000000 00FFEFF6
   000218           MOV.B     R1L,R2L
   STEP NORMAL END or
   BREAK POINT
   */

#ifdef GDB_TARGET_IS_H8300

char *want = "PC=%p CCR=%c\n\
 ER0 - ER3  %0 %1 %2 %3\n\
 ER4 - ER7  %4 %5 %6 %7\n";

char *want_nopc = "%p CCR=%c\n\
 ER0 - ER3  %0 %1 %2 %3\n\
 ER4 - ER7  %4 %5 %6 %7";

#endif

#ifdef GDB_TARGET_IS_SH

char *want = "PC=%16 SR=%22\n\
PR=%17 GBR=%18 VBR=%19\n\
MACH=%20 MACL=%21\n\
R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n";

char *want_nopc = "%16 SR=%22\n\
 PR=%17 GBR=%18 VBR=%19\n\
 MACH=%20 MACL=%21\n\
 R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
 R8-15 %8 %9 %10 %11 %12 %13 %14 %15";

char *want_sh3 = "PC=%16 SR=%22\n\
PR=%17 GBR=%18 VBR=%19\n\
MACH=%20 MACL=%21 SSR=%23 SPC=%24\n\
R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
R4_BANK1-R7_BANK1 %37 %38 %39 %40";

char *want_sh3_nopc = "%16 SR=%22\n\
 PR=%17 GBR=%18 VBR=%19\n\
 MACH=%20 MACL=%21 SSR=%22 SPC=%23\n\
 R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
 R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
 R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
 R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
 R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
 R4_BANK1-R7_BANK1 %37 %38 %39 %40";

#endif

static int
gch ()
{
  int c = readchar (timeout);

  if (remote_debug)
    {
      if (c >= ' ')
	printf ("%c", c);
      else if (c == '\n')
	printf ("\n");
    }
  return c;
}

static unsigned int
gbyte ()
{
  int high = convert_hex_digit (gch ());
  int low = convert_hex_digit (gch ());

  return (high << 4) + low;
}

void
fetch_regs_from_dump (nextchar, want)
     int (*nextchar)();
     char *want;
{
  int regno;
  char buf[MAX_REGISTER_RAW_SIZE];

  int thischar = nextchar ();

  while (*want)
    {
      switch (*want)
	{
	case '\n':
	  /* Skip to end of line and then eat all new line type stuff */
	  while (thischar != '\n' && thischar != '\r') 
	    thischar = nextchar ();
	  while (thischar == '\n' || thischar == '\r') 
	    thischar = nextchar ();
	  want++;
	  break;

	case ' ':
	  while (thischar == ' '
		 || thischar == '\t'
		 || thischar == '\r'
		 || thischar == '\n')
	    thischar = nextchar ();
	  want++;
	  break;
	  
	default:
	  if (*want == thischar)
	    {
	      want++;
	      if (*want)
		thischar = nextchar ();
	      
	    }
	  else if (thischar == ' ' || thischar == '\n' || thischar == '\r')
	    {
	      thischar = nextchar ();
	    }
	  else {
	    error ("out of sync in fetch registers wanted <%s>, got <%c 0x%x>",
		   want, thischar, thischar);
	  }
    
	  break;
	case '%':
	  /* Got a register command */
	  want++;
	  switch (*want)
	    {
#ifdef PC_REGNUM
	    case 'p':
	      regno = PC_REGNUM;
	      want++;
	      break;
#endif
#ifdef CCR_REGNUM
	    case 'c':
	      regno = CCR_REGNUM;
	      want++;
	      break;
#endif
#ifdef SP_REGNUM
	    case 's':
	      regno = SP_REGNUM;
	      want++;
	      break;
#endif
#ifdef FP_REGNUM
	    case 'f':
	      regno = FP_REGNUM;
	      want++;
	      break;
#endif

	    default:
	      if (isdigit (want[0])) 
		{
		  if (isdigit (want[1]))
		    {
		      regno = (want[0] - '0') * 10 + want[1] - '0';
		      want += 2;
		    }
		  else 
		    {
		      regno = want[0] - '0';
		      want++;
		    }
		}
	      
	      else
		abort ();
	    }
	  store_signed_integer (buf,
				REGISTER_RAW_SIZE(regno),
				(LONGEST) get_hex (&thischar, nextchar));
	  supply_register (regno, buf);
	  break;
	}
    }
}

static void
e7000_fetch_registers ()
{
  int regno;

  puts_e7000debug ("R\r");

#ifdef GDB_TARGET_IS_SH
  if  ((sh_processor_type != NULL) && (*(sh_processor_type+2) == '3')) 
     fetch_regs_from_dump (gch, want_sh3);
  else
     fetch_regs_from_dump (gch, want);
#else
  fetch_regs_from_dump (gch, want);
#endif


  /* And supply the extra ones the simulator uses */
  for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
    {
      int buf = 0;

      supply_register (regno, (char *) (&buf));
    }
}

/* Fetch register REGNO, or all registers if REGNO is -1.  Returns
   errno value.  */

static void
e7000_fetch_register (regno)
     int regno;
{
  e7000_fetch_registers ();
}

/* Store the remote registers from the contents of the block REGS.  */

static void
e7000_store_registers ()
{
  int regno;

  for (regno = 0; regno < NUM_REALREGS; regno++)
    e7000_store_register (regno);

  registers_changed ();
}

/* Store register REGNO, or all if REGNO == 0.  Return errno value.  */

static void
e7000_store_register (regno)
     int regno;
{
  char buf[200];

  if (regno == -1)
    {
      e7000_store_registers ();
      return;
    }

#ifdef GDB_TARGET_IS_H8300
  if (regno <= 7)
    {
      sprintf (buf, ".ER%d %x\r", regno, read_register (regno));
      puts_e7000debug (buf);
    }
  else if (regno == PC_REGNUM)
    {
      sprintf (buf, ".PC %x\r", read_register (regno));
      puts_e7000debug (buf);
    }
  else if (regno == CCR_REGNUM)
    {
      sprintf (buf, ".CCR %x\r", read_register (regno));
      puts_e7000debug (buf);
    }
#endif /* GDB_TARGET_IS_H8300 */

#ifdef  GDB_TARGET_IS_SH
  switch (regno)
    {
    default:
      sprintf (buf, ".R%d %x\r", regno, read_register (regno));
      puts_e7000debug (buf);
      break;

    case PC_REGNUM:
      sprintf (buf, ".PC %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case SR_REGNUM:
      sprintf (buf, ".SR %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case PR_REGNUM:
      sprintf (buf, ".PR %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case GBR_REGNUM:
      sprintf (buf, ".GBR %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case VBR_REGNUM:
      sprintf (buf, ".VBR %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case MACH_REGNUM:
      sprintf (buf, ".MACH %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;

    case MACL_REGNUM:
      sprintf (buf, ".MACL %x\r", read_register (regno));
      puts_e7000debug (buf);
      break;
    }

#endif /* GDB_TARGET_IS_SH */

  expect_prompt ();
}

/* Get ready to modify the registers array.  On machines which store
   individual registers, this doesn't need to do anything.  On machines
   which store all the registers in one fell swoop, this makes sure
   that registers contains all the registers from the program being
   debugged.  */

static void
e7000_prepare_to_store ()
{
  /* Do nothing, since we can store individual regs */
}

static void
e7000_files_info ()
{
  printf ("\tAttached to %s at %d baud.\n", dev_name, baudrate);
}

static int
stickbyte (where, what)
     char *where;
     unsigned int what;
{
  static CONST char digs[] = "0123456789ABCDEF";

  where[0] = digs[(what >> 4) & 0xf];
  where[1] = digs[(what & 0xf) & 0xf];

  return what;
}

/* Write a small ammount of memory. */

static int
write_small (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
{
  int i;
  char buf[200];

  for (i = 0; i < len; i++)
    {
      if (((memaddr + i) & 3) == 0 && (i + 3 < len))
	{
	  /* Can be done with a long word */
	  sprintf (buf, "m %x %x%02x%02x%02x;l\r",
		   memaddr + i,
		   myaddr[i], myaddr[i + 1], myaddr[i + 2], myaddr[i + 3]);
	  puts_e7000debug (buf);
	  i += 3;
	}
      else
	{
	  sprintf (buf, "m %x %x\r", memaddr + i, myaddr[i]);
	  puts_e7000debug (buf);
	}
    }

  expect_prompt ();

  return len;
}

/* Write a large ammount of memory, this only works with the serial
   mode enabled.  Command is sent as

	il ;s:s\r	 ->
			<- il ;s:s\r
			<-  	ENQ
	ACK		->
			<- LO s\r
	Srecords...
	^Z		->
			<-	ENQ
	ACK		->  
			<-	:   	
  */

static int
write_large (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
{
  int i;
#define maxstride  128
  int stride;

  puts_e7000debug ("IL ;S:FK\r");
  expect (ENQSTRING);
  putchar_e7000 (ACK);
  expect ("LO FK\r");

  for (i = 0; i < len; i += stride)
    {
      char compose[maxstride * 2 + 50];
      int address = i + memaddr;
      int j;
      int check_sum;
      int where = 0;
      int alen;

      stride = len - i;
      if (stride > maxstride)
	stride = maxstride;

      compose[where++] = 'S';
      check_sum = 0;
      if (address >= 0xffffff)
	alen = 4;
      else if (address >= 0xffff)
	alen = 3;
      else
	alen = 2;
      /* Insert type. */
      compose[where++] = alen - 1 + '0';
      /* Insert length. */
      check_sum += stickbyte (compose + where, alen + stride + 1);
      where += 2;
      while (alen > 0)
	{
	  alen--;
	  check_sum += stickbyte (compose + where, address >> (8 * (alen)));
	  where += 2;
	}

      for (j = 0; j < stride; j++)
	{
	  check_sum += stickbyte (compose + where, myaddr[i + j]);
	  where += 2;
	}
      stickbyte (compose + where, ~check_sum);
      where += 2;
      compose[where++] = '\r';
      compose[where++] = '\n';
      compose[where++] = 0;

      SERIAL_WRITE (e7000_desc, compose, where);
      j = SERIAL_READCHAR (e7000_desc, 0);
      if (j == SERIAL_TIMEOUT)
	{
	  /* This is ok - nothing there */
	}
      else if (j == ENQ)
	{
	  /* Hmm, it's trying to tell us something */
	  expect (":");
	  error ("Error writing memory");
	}
      else
	{
	  printf ("@%d}@", j);
	  while ((j = SERIAL_READCHAR(e7000_desc,0)) > 0) 
	    {
	      printf ("@{%d}@",j);
	    }
	}
    }

  /* Send the trailer record */
  write_e7000 ("S70500000000FA\r");
  putchar_e7000 (CTRLZ);
  expect (ENQSTRING);
  putchar_e7000 (ACK);
  expect (":");

  return len;
}

/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
   memory at MEMADDR.  Returns length moved.

   Can't use the Srecord load over ethernet, so don't use fast method
   then.  */

static int
e7000_write_inferior_memory (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
{
  if (len < 16 || using_tcp || using_pc)
    return write_small (memaddr, myaddr, len);
  else
    return write_large (memaddr, myaddr, len);
}

/* Read LEN bytes from inferior memory at MEMADDR.  Put the result
   at debugger address MYADDR.  Returns length moved. 

  Small transactions we send
  m <addr>;l
  and receive
    00000000 12345678 ?
 */

static int
e7000_read_inferior_memory (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
{
  int count;
  int c;
  int i;
  char buf[200];
  /* Starting address of this pass.  */

/*  printf("READ INF %x %x %d\n", memaddr, myaddr, len);*/
  if (((memaddr - 1) + len) < memaddr)
    {
      errno = EIO;
      return 0;
    }

  sprintf (buf, "m %x;l\r", memaddr);
  puts_e7000debug (buf);

  for (count = 0; count < len; count += 4) 
    {
      /* Suck away the address */
      c = gch ();	
      while (c != ' ')
	c = gch ();	
      c = gch ();
      if (c == '*') 
	{			/* Some kind of error */
	  expect_prompt();
	  return -1;
	}
      while (c != ' ')
	c = gch ();	

      /* Now read in the data */
      for (i = 0; i < 4; i++) 
	{
	  int b = gbyte();
	  if (count + i < len) {
	    myaddr[count + i] = b;
	  }
	}

      /* Skip the trailing ? and send a . to end and a cr for more */
      gch ();	
      gch ();
      if (count + 4 >= len)
	puts_e7000debug(".\r");
      else
	puts_e7000debug("\r");

    }
  expect_prompt();
  return len;
}



/*
  For large transfers we used to send


  d <addr> <endaddr>\r

  and receive
   <ADDRESS>           <    D   A   T   A    >               <   ASCII CODE   >
   00000000 5F FD FD FF DF 7F DF FF  01 00 01 00 02 00 08 04  "_..............."
   00000010 FF D7 FF 7F D7 F1 7F FF  00 05 00 00 08 00 40 00  "..............@."
   00000020 7F FD FF F7 7F FF FF F7  00 00 00 00 00 00 00 00  "................"

  A cost in chars for each transaction of 80 + 5*n-bytes. 

  Large transactions could be done with the srecord load code, but
  there is a pause for a second before dumping starts, which slows the
  average rate down!
*/

static int
e7000_read_inferior_memory_large (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
{
  int count;
  int c;
  char buf[200];

  /* Starting address of this pass.  */

  if (((memaddr - 1) + len) < memaddr)
    {
      errno = EIO;
      return 0;
    }

  sprintf (buf, "d %x %x\r", memaddr, memaddr + len - 1);
  puts_e7000debug (buf);

  count = 0;
  c = gch ();
  
  /* skip down to the first ">" */
  while( c != '>' )
    c = gch ();
  /* now skip to the end of that line */
  while( c != '\r' )
    c = gch ();
  c = gch ();

  while (count < len)
    {
      /* get rid of any white space before the address */
      while (c <= ' ')
	c = gch ();

      /* Skip the address */
      get_hex (&c);

      /* read in the bytes on the line */
      while (c != '"' && count < len)
	{
	  if (c == ' ')
	    c = gch ();
	  else
	    {
	      myaddr[count++] = get_hex (&c);
	    }
	}
      /* throw out the rest of the line */
      while( c != '\r' )
	c = gch ();
    }

  /* wait for the ":" prompt */
  while (c != ':')
    c = gch ();

  return len;
}

#if 0

static int
fast_but_for_the_pause_e7000_read_inferior_memory (memaddr, myaddr, len)
     CORE_ADDR memaddr;
     char *myaddr;
     int len;
{
  int loop;
  int c;
  char buf[200];

  if (((memaddr - 1) + len) < memaddr)
    {
      errno = EIO;
      return 0;
    }

  sprintf (buf, "is %x@%x:s\r", memaddr, len);
  puts_e7000debug (buf);
  gch ();
  c = gch ();
  if (c != ENQ)
    {
      /* Got an error */
      error ("Memory read error");
    }
  putchar_e7000 (ACK);
  expect ("SV s");
  loop = 1;
  while (loop)
    {
      int type;
      int length;
      int addr;
      int i;

      c = gch ();
      switch (c)
	{
	case ENQ:		/* ENQ, at the end */
	  loop = 0;
	  break;
	case 'S':
	  /* Start of an Srecord */
	  type = gch ();
	  length = gbyte ();
	  switch (type)
	    {
	    case '7':		/* Termination record, ignore */
	    case '0':
	    case '8':
	    case '9':
	      /* Header record - ignore it */
	      while (length--)
		{
		  gbyte ();
		}
	      break;
	    case '1':
	    case '2':
	    case '3':
	      {
		int alen;

		alen = type - '0' + 1;
		addr = 0;
		while (alen--)
		  {
		    addr = (addr << 8) + gbyte ();
		    length--;
		  }

		for (i = 0; i < length - 1; i++)
		  myaddr[i + addr - memaddr] = gbyte ();

		gbyte ();	/* Ignore checksum */
	      }
	    }
	}
    }

  putchar_e7000 (ACK);
  expect ("TOP ADDRESS =");
  expect ("END ADDRESS =");
  expect (":");

  return len;
}

#endif

static int
e7000_xfer_inferior_memory (memaddr, myaddr, len, write, target)
     CORE_ADDR memaddr;
     unsigned char *myaddr;
     int len;
     int write;
     struct target_ops *target;	/* ignored */
{
  if (write)
    return e7000_write_inferior_memory( memaddr, myaddr, len);
  else 
    if( len < 16 )
      return e7000_read_inferior_memory( memaddr, myaddr, len);
    else
      return e7000_read_inferior_memory_large( memaddr, myaddr, len);
}

static void
e7000_kill (args, from_tty)
     char *args;
     int from_tty;
{
}

static void
e7000_load (args, from_tty)
     char *args;
     int from_tty;
{
  struct cleanup *old_chain;
  asection *section;
  bfd *pbfd;
  bfd_vma entry;
  int i;
#define WRITESIZE 0x1000
  char buf[2 + 4 + 4 + WRITESIZE]; /* `DT' + <addr> + <len> + <data> */
  char *filename;
  int quiet;
  int nostart;
  time_t start_time, end_time;	/* Start and end times of download */
  unsigned long data_count;	/* Number of bytes transferred to memory */
  int oldtimeout = timeout;	

  timeout = remote_timeout;


  /* FIXME! change test to test for type of download */
  if (!using_tcp)
    {
      generic_load (args, from_tty);
      return;
    }

  /* for direct tcp connections, we can do a fast binary download */
  buf[0] = 'D';
  buf[1] = 'T';
  quiet = 0;
  nostart = 0;
  filename = NULL;

  while (*args != '\000')
    {
      char *arg;

      while (isspace (*args)) args++;

      arg = args;

      while ((*args != '\000') && !isspace (*args)) args++;

      if (*args != '\000')
	*args++ = '\000';

      if (*arg != '-')
	filename = arg;
      else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
	quiet = 1;
      else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
	nostart = 1;
      else
	error ("unknown option `%s'", arg);
    }

  if (!filename)
    filename = get_exec_file (1);

  pbfd = bfd_openr (filename, gnutarget);
  if (pbfd == NULL)
    {
      perror_with_name (filename);
      return;
    }
  old_chain = make_cleanup (bfd_close, pbfd);

  if (!bfd_check_format (pbfd, bfd_object)) 
    error ("\"%s\" is not an object file: %s", filename,
	   bfd_errmsg (bfd_get_error ()));

  start_time = time (NULL);
  data_count = 0;

  puts_e7000debug ("mw\r");

  expect ("\nOK");

  for (section = pbfd->sections; section; section = section->next) 
    {
      if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
	{
	  bfd_vma section_address;
	  bfd_size_type section_size;
	  file_ptr fptr;

	  section_address = bfd_get_section_vma (pbfd, section);
	  section_size = bfd_get_section_size_before_reloc (section);

	  if (!quiet)
	    printf_filtered ("[Loading section %s at 0x%x (%d bytes)]\n",
			     bfd_get_section_name (pbfd, section),
			     section_address,
			     section_size);

	  fptr = 0;
	  
	  data_count += section_size;

	  while (section_size > 0)
	    {
	      int count;
	      static char inds[] = "|/-\\";
	      static int k = 0;

	      QUIT;

	      count = min (section_size, WRITESIZE);

	      buf[2] = section_address >> 24;
	      buf[3] = section_address >> 16;
	      buf[4] = section_address >> 8;
	      buf[5] = section_address;

	      buf[6] = count >> 24;
	      buf[7] = count >> 16;
	      buf[8] = count >> 8;
	      buf[9] = count;

	      bfd_get_section_contents (pbfd, section, buf + 10, fptr, count);

	      if (SERIAL_WRITE (e7000_desc, buf, count + 10))
		fprintf_unfiltered (gdb_stderr,
				    "e7000_load: SERIAL_WRITE failed: %s\n",
				    safe_strerror(errno));

	      expect ("OK");

	      if (!quiet)
		{
		  printf_unfiltered ("\r%c", inds[k++ % 4]);
		  gdb_flush (gdb_stdout);
		}

	      section_address += count;
	      fptr += count;
	      section_size -= count;
	    }
	}
    }

  write_e7000 ("ED");

  expect_prompt ();

  end_time = time (NULL);

/* Finally, make the PC point at the start address */

  if (exec_bfd)
    write_pc (bfd_get_start_address (exec_bfd));

  inferior_pid = 0;		/* No process now */

/* This is necessary because many things were based on the PC at the time that
   we attached to the monitor, which is no longer valid now that we have loaded
   new code (and just changed the PC).  Another way to do this might be to call
   normal_stop, except that the stack may not be valid, and things would get
   horribly confused... */

  clear_symtab_users ();

  if (!nostart)
    {
      entry = bfd_get_start_address (pbfd);

      if (!quiet)
	printf_unfiltered ("[Starting %s at 0x%x]\n", filename, entry);

/*      start_routine (entry);*/
    }

  report_transfer_performance (data_count, start_time, end_time);

  do_cleanups (old_chain);
  timeout = oldtimeout;
}

/* Clean up when a program exits.

   The program actually lives on in the remote processor's RAM, and may be
   run again without a download.  Don't leave it full of breakpoint
   instructions.  */

static void
e7000_mourn_inferior ()
{
  remove_breakpoints ();
  unpush_target (&e7000_ops);
  generic_mourn_inferior ();	/* Do all the proper things now */
}

#define MAX_BREAKPOINTS 200
#ifdef  HARD_BREAKPOINTS
#define MAX_E7000DEBUG_BREAKPOINTS (BC_BREAKPOINTS ? 5 :  MAX_BREAKPOINTS)
#else
#define MAX_E7000DEBUG_BREAKPOINTS MAX_BREAKPOINTS
#endif

extern int memory_breakpoint_size;

/* Since we can change to soft breakpoints dynamically, we must define 
   more than enough.  Was breakaddr[MAX_E7000DEBUG_BREAKPOINTS]. */
static CORE_ADDR breakaddr[MAX_BREAKPOINTS] = {0};

static int
e7000_insert_breakpoint (addr, shadow)
     CORE_ADDR addr;
     unsigned char *shadow;
{
  int i;
  char buf[200];
  static char nop[2] = NOP;

  for (i = 0; i <= MAX_E7000DEBUG_BREAKPOINTS; i++)
    if (breakaddr[i] == 0)
      {
	breakaddr[i] = addr;
	/* Save old contents, and insert a nop in the space */
#ifdef HARD_BREAKPOINTS
	if (BC_BREAKPOINTS) 
	  {
	    sprintf (buf, "BC%d A=%x\r", i+1, addr);
	    puts_e7000debug (buf);
	  }
	else 
	  {
	    sprintf (buf, "B %x\r", addr);
	    puts_e7000debug (buf);
	  }
#else
#if 0
	e7000_read_inferior_memory (addr, shadow, 2);
	e7000_write_inferior_memory (addr, nop, 2);
#endif

	sprintf (buf, "B %x\r", addr);
	puts_e7000debug (buf);
#endif
	expect_prompt ();
	return 0;
      }

  error ("Too many breakpoints ( > %d) for the E7000\n",
	 MAX_E7000DEBUG_BREAKPOINTS);
  return 1;
}

static int
e7000_remove_breakpoint (addr, shadow)
     CORE_ADDR addr;
     unsigned char *shadow;
{
  int i;
  char buf[200];

  for (i = 0; i < MAX_E7000DEBUG_BREAKPOINTS; i++)
    if (breakaddr[i] == addr)
      {
	breakaddr[i] = 0;
#ifdef HARD_BREAKPOINTS
	if (BC_BREAKPOINTS) 
	  {
	    sprintf (buf, "BC%d - \r",  i+1);
	    puts_e7000debug (buf);
	  }
	else 
	  {
	    sprintf (buf, "B - %x\r",  addr);
	    puts_e7000debug (buf);
	  }
	expect_prompt ();
#else
	sprintf (buf, "B - %x\r", addr);
	puts_e7000debug (buf);
	expect_prompt ();

#if 0
	/* Replace the insn under the break */
	e7000_write_inferior_memory (addr, shadow, 2);
#endif
#endif

	return 0;
      }

  warning ("Can't find breakpoint associated with 0x%x\n", addr);
  return 1;
}

/* Put a command string, in args, out to STDBUG.  Output from STDBUG
   is placed on the users terminal until the prompt is seen. */

static void
e7000_command (args, fromtty)
     char *args;
     int fromtty;
{
  /* FIXME: arbitrary limit on length of args.  */
  char buf[200];

  echo = 0;

  if (!e7000_desc)
    error ("e7000 target not open.");
  if (!args)
    {
      puts_e7000debug ("\r");
    }
  else
    {
      sprintf (buf, "%s\r", args);
      puts_e7000debug (buf);
    }

  echo++;
  ctrl_c = 2;
  expect_full_prompt ();
  echo--;
  ctrl_c = 0;
  printf_unfiltered ("\n");

  /* Who knows what the command did... */
  registers_changed ();
}


static void
e7000_drain_command (args, fromtty)
     char *args;
     int fromtty;

{
  int c;

  puts_e7000debug("end\r");
  putchar_e7000 (CTRLC);

  while ((c = SERIAL_READCHAR (e7000_desc, 1) != SERIAL_TIMEOUT))
    {
      if (quit_flag)
	{
	  putchar_e7000(CTRLC);
	  quit_flag = 0;
	}
      if (c > ' ' && c < 127)
	printf ("%c", c & 0xff);
      else
	printf ("<%x>", c & 0xff);
    }
}

#define NITEMS 7

static int
why_stop ()
{
  static  char *strings[NITEMS] = {
    "STEP NORMAL",
    "BREAK POINT",
    "BREAK KEY",
    "BREAK CONDI",
    "CYCLE ACCESS",
    "ILLEGAL INSTRUCTION",
    "WRITE PROTECT",
  };
  char *p[NITEMS];
  int c;
  int i;

  for (i = 0; i < NITEMS; ++i)
    p[i] = strings[i];
  
  c = gch ();
  while (1)
    {
      for (i = 0; i < NITEMS; i++)
	{
	  if (c == *(p[i])) 
	    {
	      p[i]++;
	      if (*(p[i]) == 0) 
		{ 
		  /* found one of the choices */
		  return i;
		}
	    }
	  else
	    p[i] = strings[i];
	}

      c = gch ();
    }
}

/* Suck characters, if a string match, then return the strings index
   otherwise echo them.  */

int
expect_n (strings)
char **strings;
{
  char *(ptr[10]);
  int n; 
  int c;
  char saveaway[100];
  char *buffer = saveaway;
  /* Count number of expect strings  */

  for (n = 0; strings[n]; n++) 
    {
      ptr[n] = strings[n];
    }

  while (1)
    {
      int i;
      int gotone = 0;

      c = SERIAL_READCHAR (e7000_desc, 1);
      if (c == SERIAL_TIMEOUT)
	{
	  printf_unfiltered ("[waiting for e7000...]\n");
	}
#ifdef __GO32__
      if (kbhit ())
	{
	  int k = getkey();

	  if (k == 1)
	    quit_flag = 1;
	}
#endif
      if (quit_flag)
	{
	  putchar_e7000 (CTRLC);	/* interrupt the running program */
	  quit_flag = 0;
	}

      for (i = 0; i < n; i++)
	{
	  if (c == ptr[i][0]) 
	    {
	      ptr[i]++;
	      if (ptr[i][0] == 0)
		{
		  /* Gone all the way */
		  return i;
		}
	      gotone = 1;
	    }
	  else
	    {
	      ptr[i] = strings[i];
	    }
	}
    
      if (gotone)
	{
	  /* Save it up incase we find that there was no match */
	  *buffer ++ = c;
	}
      else
	{
	  if (buffer != saveaway) 
	    {
	      *buffer++ = 0;
	      printf ("%s", buffer);
	      buffer = saveaway;
	    }
	  if (c != SERIAL_TIMEOUT)
	    {
	      putchar (c);
	      fflush (stdout);
	    }
	}
    }
}

/* We subtract two from the pc here rather than use
   DECR_PC_AFTER_BREAK since the e7000 doesn't always add two to the
   pc, and the simulators never do. */

static void
sub2_from_pc ()
{
  char buf[4];
  char buf2[200];

  store_signed_integer (buf,
			REGISTER_RAW_SIZE(PC_REGNUM), 
			read_register (PC_REGNUM) -2);
  supply_register (PC_REGNUM, buf);
  sprintf (buf2, ".PC %x\r", read_register (PC_REGNUM));
  puts_e7000debug (buf2);
}

#define WAS_SLEEP 0
#define WAS_INT 1
#define WAS_RUNNING 2
#define WAS_OTHER 3

static char *estrings[] = {
  "** SLEEP",
  "BREAK !",
  "** PC",
  "PC",
  NULL
};

/* Wait until the remote machine stops, then return, storing status in
   STATUS just as `wait' would.  */

static int
e7000_wait (pid, status)
     int pid;
     struct target_waitstatus *status;
{
  int stop_reason;
  int regno;
  int running_count = 0;
  int had_sleep = 0;
  int loop = 1;

  /* Then echo chars until PC= string seen */
  gch ();			/* Drop cr */
  gch ();			/* and space */

  while (loop)
    {
      switch (expect_n (estrings))
	{	 
	case WAS_OTHER:
	  /* how did this happen ? */
	  loop = 0;
	  break;
	case WAS_SLEEP:
	  had_sleep = 1;
	  putchar_e7000 (CTRLC);
	  loop = 0;
	  break;
	case WAS_INT:
	  loop = 0;
	  break;
	case WAS_RUNNING:
	  running_count++;
	  if (running_count == 20)
	    {
	      printf_unfiltered ("[running...]\n");
	      running_count = 0;
	    }
	  break;
	default:
	  /* error? */
	  break;
	}
    }

  /* Skip till the PC= */
  expect ("=");

#ifdef GDB_TARGET_IS_SH
  if  ((sh_processor_type != NULL) && (*(sh_processor_type+2) == '3')) 
     fetch_regs_from_dump (gch, want_sh3_nopc);
  else
     fetch_regs_from_dump (gch, want_nopc);
#else
  fetch_regs_from_dump (gch, want_nopc);
#endif

  /* And supply the extra ones the simulator uses */
  for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
    {
      int buf = 0;
      supply_register (regno, (char *) &buf);
    }

  stop_reason = why_stop ();
  expect_full_prompt ();

  status->kind = TARGET_WAITKIND_STOPPED;
  status->value.sig = TARGET_SIGNAL_TRAP;

  switch (stop_reason)
    {
    case 1:			/* Breakpoint */
      write_pc (read_pc ()); /* PC is always off by 2 for breakpoints */
      status->value.sig = TARGET_SIGNAL_TRAP;      
      break;
    case 0:			/* Single step */
      status->value.sig = TARGET_SIGNAL_TRAP;      
      break;
    case 2:			/* Interrupt */
      if (had_sleep)
	{
	  status->value.sig = TARGET_SIGNAL_TRAP;      
	  sub2_from_pc ();
	}
      else
	{
	  status->value.sig = TARGET_SIGNAL_INT;      
	}
      break;
    case 3:
      break;
    case 4:
      printf_unfiltered ("a cycle address error?\n");
      status->value.sig = TARGET_SIGNAL_UNKNOWN;      
      break;
    case 5:
      status->value.sig = TARGET_SIGNAL_ILL;      
      break;
    case 6:
      status->value.sig = TARGET_SIGNAL_SEGV;      
      break;
    case 7:			/* Anything else (NITEMS + 1) */
      printf_unfiltered ("a write protect error?\n");
      status->value.sig = TARGET_SIGNAL_UNKNOWN;      
      break;
    default:
      /* Get the user's attention - this should never happen. */
      abort ();
    }

  return 0;
}

/* Stop the running program.  */

static void
e7000_stop ()  
{
  /* Sending a ^C is supposed to stop the running program.  */
  putchar_e7000 (CTRLC);
}

/* Define the target subroutine names. */

struct target_ops e7000_ops =
{
  "e7000",
  "Remote Hitachi e7000 target",
  "Use a remote Hitachi e7000 ICE connected by a serial line,\n\
or a network connection.\n\
Arguments are the name of the device for the serial line,\n\
the speed to connect at in bits per second.\n\
eg\n\
target e7000 /dev/ttya 9600\n\
target e7000 foobar",
  e7000_open,			/* to_open */
  e7000_close,			/* to_close */
  0,				/* to_attach */
  e7000_detach,			/* to_detach */
  e7000_resume,			/* to_resume */
  e7000_wait,			/* to_wait */
  e7000_fetch_register,		/* to_fetch_registers */
  e7000_store_register,		/* to_store_registers */
  e7000_prepare_to_store,	/* to_prepare_to_store */
  e7000_xfer_inferior_memory,	/* to_xfer_memory */
  e7000_files_info,		/* to_files_info */
  e7000_insert_breakpoint,	/* to_insert_breakpoint */
  e7000_remove_breakpoint,	/* to_remove_breakpoint */
  0,				/* to_terminal_init */
  0,				/* to_terminal_inferior */
  0,				/* to_terminal_ours_for_output */
  0,				/* to_terminal_ours */
  0,				/* to_terminal_info */
  e7000_kill,			/* to_kill */
  e7000_load,			/* to_load */
  0,				/* to_lookup_symbol */
  e7000_create_inferior,	/* to_create_inferior */
  e7000_mourn_inferior,		/* to_mourn_inferior */
  0,				/* to_can_run */
  0,				/* to_notice_signals */
  0,				/* to_thread_alive */
  e7000_stop,			/* to_stop */
  process_stratum,		/* to_stratum */
  0,				/* next (unused) */
  1,				/* to_has_all_memory */
  1,				/* to_has_memory */
  1,				/* to_has_stack */
  1,				/* to_has_registers */
  1,				/* to_has_execution */
  0,				/* to_sections */
  0,				/* to_sections_end */
  OPS_MAGIC,			/* Always the last thing */
};

void
_initialize_remote_e7000 ()
{
  add_target (&e7000_ops);

  add_com ("e7000 <command>", class_obscure, e7000_command,
	   "Send a command to the e7000 monitor.");

  add_com ("ftplogin <machine> <name> <passwd> <dir>", class_obscure, e7000_login_command,
	   "Login to machine and change to directory.");

  add_com ("ftpload <file>", class_obscure, e7000_ftp_command,
	   "Fetch and load a file from previously described place.");

  add_com ("drain", class_obscure, e7000_drain_command,
	   "Drain pending e7000 text buffers.");

  add_show_from_set (add_set_cmd ("usehardbreakpoints", no_class,
				  var_integer, (char *)&use_hard_breakpoints,
				  "Set use of hardware breakpoints for all breakpoints.\n", &setlist),
		     &showlist);
}