* monitor.c (#include "gdb_wait.h"): Removed.
[platform/upstream/binutils.git] / gdb / remote-e7000.c
1 /* Remote debugging interface for Hitachi E7000 ICE, for GDB
2    Copyright 1993, 1994, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Cygnus Support. 
4
5    Written by Steve Chamberlain for Cygnus Support.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 /* The E7000 is an in-circuit emulator for the Hitachi H8/300-H and
25    Hitachi-SH processor.  It has serial port and a lan port.  
26
27    The monitor command set makes it difficult to load large ammounts of
28    data over the lan without using ftp - so try not to issue load
29    commands when communicating over ethernet; use the ftpload command.
30
31    The monitor pauses for a second when dumping srecords to the serial
32    line too, so we use a slower per byte mechanism but without the
33    startup overhead.  Even so, it's pretty slow... */
34
35 #include "defs.h"
36 #include "gdbcore.h"
37 #include "gdbarch.h"
38 #include "inferior.h"
39 #include "target.h"
40 #include "value.h"
41 #include "command.h"
42 #include <signal.h>
43 #include "gdb_string.h"
44 #include "gdbcmd.h"
45 #include <sys/types.h>
46 #include "serial.h"
47 #include "remote-utils.h"
48 #include "symfile.h"
49 #include <time.h>
50 #include <ctype.h>
51
52
53 #if 1
54 #define HARD_BREAKPOINTS        /* Now handled by set option. */
55 #define BC_BREAKPOINTS use_hard_breakpoints
56 #endif
57
58 #define CTRLC 0x03
59 #define ENQ  0x05
60 #define ACK  0x06
61 #define CTRLZ 0x1a
62
63 /* This file is used by 2 different targets, sh-elf and h8300. The
64    h8300 is not multiarched and doesn't use the registers defined in
65    tm-sh.h. To avoid using a macro GDB_TARGET_IS_SH, we do runtime check
66    of the target, which requires that these namse below are always
67    defined also in the h8300 case. */
68
69 #if !defined (PR_REGNUM)
70 #define PR_REGNUM       -1
71 #endif
72 #if !defined (GBR_REGNUM)
73 #define GBR_REGNUM      -1
74 #endif
75 #if !defined (VBR_REGNUM)
76 #define VBR_REGNUM      -1
77 #endif
78 #if !defined (MACH_REGNUM)
79 #define MACH_REGNUM     -1
80 #endif
81 #if !defined (MACL_REGNUM)
82 #define MACL_REGNUM     -1
83 #endif
84 #if !defined (SR_REGNUM)
85 #define SR_REGNUM       -1
86 #endif
87
88 extern void notice_quit (void);
89
90 extern void report_transfer_performance (unsigned long, time_t, time_t);
91
92 extern char *sh_processor_type;
93
94 /* Local function declarations.  */
95
96 static void e7000_close (int);
97
98 static void e7000_fetch_register (int);
99
100 static void e7000_store_register (int);
101
102 static void e7000_command (char *, int);
103
104 static void e7000_login_command (char *, int);
105
106 static void e7000_ftp_command (char *, int);
107
108 static void e7000_drain_command (char *, int);
109
110 static void expect (char *);
111
112 static void expect_full_prompt (void);
113
114 static void expect_prompt (void);
115
116 static int e7000_parse_device (char *args, char *dev_name, int baudrate);
117 /* Variables. */
118
119 static serial_t e7000_desc;
120
121 /* Allow user to chose between using hardware breakpoints or memory. */
122 static int use_hard_breakpoints = 0;    /* use sw breakpoints by default */
123
124 /* Nonzero if using the tcp serial driver.  */
125
126 static int using_tcp;           /* direct tcp connection to target */
127 static int using_tcp_remote;    /* indirect connection to target 
128                                    via tcp to controller */
129
130 /* Nonzero if using the pc isa card.  */
131
132 static int using_pc;
133
134 extern struct target_ops e7000_ops;     /* Forward declaration */
135
136 char *ENQSTRING = "\005";
137
138 /* Nonzero if some routine (as opposed to the user) wants echoing.
139    FIXME: Do this reentrantly with an extra parameter.  */
140
141 static int echo;
142
143 static int ctrl_c;
144
145 static int timeout = 20;
146
147 /* Send data to e7000debug.  */
148
149 static void
150 puts_e7000debug (char *buf)
151 {
152   if (!e7000_desc)
153     error ("Use \"target e7000 ...\" first.");
154
155   if (remote_debug)
156     printf_unfiltered ("Sending %s\n", buf);
157
158   if (SERIAL_WRITE (e7000_desc, buf, strlen (buf)))
159     fprintf_unfiltered (gdb_stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
160
161   /* And expect to see it echoed, unless using the pc interface */
162 #if 0
163   if (!using_pc)
164 #endif
165     expect (buf);
166 }
167
168 static void
169 putchar_e7000 (int x)
170 {
171   char b[1];
172
173   b[0] = x;
174   SERIAL_WRITE (e7000_desc, b, 1);
175 }
176
177 static void
178 write_e7000 (char *s)
179 {
180   SERIAL_WRITE (e7000_desc, s, strlen (s));
181 }
182
183 static int
184 normal (int x)
185 {
186   if (x == '\n')
187     return '\r';
188   return x;
189 }
190
191 /* Read a character from the remote system, doing all the fancy timeout
192    stuff.  Handles serial errors and EOF.  If TIMEOUT == 0, and no chars,
193    returns -1, else returns next char.  Discards chars > 127.  */
194
195 static int
196 readchar (int timeout)
197 {
198   int c;
199
200   do
201     {
202       c = SERIAL_READCHAR (e7000_desc, timeout);
203     }
204   while (c > 127);
205
206   if (c == SERIAL_TIMEOUT)
207     {
208       if (timeout == 0)
209         return -1;
210       echo = 0;
211       error ("Timeout reading from remote system.");
212     }
213   else if (c < 0)
214     error ("Serial communication error");
215
216   if (remote_debug)
217     {
218       putchar_unfiltered (c);
219       gdb_flush (gdb_stdout);
220     }
221
222   return normal (c);
223 }
224
225 #if 0
226 char *
227 tl (int x)
228 {
229   static char b[8][10];
230   static int p;
231
232   p++;
233   p &= 7;
234   if (x >= ' ')
235     {
236       b[p][0] = x;
237       b[p][1] = 0;
238     }
239   else
240     {
241       sprintf (b[p], "<%d>", x);
242     }
243
244   return b[p];
245 }
246 #endif
247
248 /* Scan input from the remote system, until STRING is found.  If
249    DISCARD is non-zero, then discard non-matching input, else print it
250    out.  Let the user break out immediately.  */
251
252 static void
253 expect (char *string)
254 {
255   char *p = string;
256   int c;
257   int nl = 0;
258
259   while (1)
260     {
261       c = readchar (timeout);
262 #if 0
263       notice_quit ();
264       if (quit_flag == 1)
265         {
266           if (ctrl_c)
267             {
268               putchar_e7000 (CTRLC);
269               --ctrl_c;
270             }
271           else
272             {
273               quit ();
274             }
275         }
276 #endif
277
278       if (echo)
279         {
280           if (c == '\r' || c == '\n')
281             {
282               if (!nl)
283                 putchar_unfiltered ('\n');
284               nl = 1;
285             }
286           else
287             {
288               nl = 0;
289               putchar_unfiltered (c);
290             }
291           gdb_flush (gdb_stdout);
292         }
293       if (normal (c) == normal (*p++))
294         {
295           if (*p == '\0')
296             return;
297         }
298       else
299         {
300           p = string;
301
302           if (normal (c) == normal (string[0]))
303             p++;
304         }
305     }
306 }
307
308 /* Keep discarding input until we see the e7000 prompt.
309
310    The convention for dealing with the prompt is that you
311    o give your command
312    o *then* wait for the prompt.
313
314    Thus the last thing that a procedure does with the serial line will
315    be an expect_prompt().  Exception: e7000_resume does not wait for
316    the prompt, because the terminal is being handed over to the
317    inferior.  However, the next thing which happens after that is a
318    e7000_wait which does wait for the prompt.  Note that this includes
319    abnormal exit, e.g. error().  This is necessary to prevent getting
320    into states from which we can't recover.  */
321
322 static void
323 expect_prompt (void)
324 {
325   expect (":");
326 }
327
328 static void
329 expect_full_prompt (void)
330 {
331   expect ("\r:");
332 }
333
334 static int
335 convert_hex_digit (int ch)
336 {
337   if (ch >= '0' && ch <= '9')
338     return ch - '0';
339   else if (ch >= 'A' && ch <= 'F')
340     return ch - 'A' + 10;
341   else if (ch >= 'a' && ch <= 'f')
342     return ch - 'a' + 10;
343   return -1;
344 }
345
346 static int
347 get_hex (int *start)
348 {
349   int value = convert_hex_digit (*start);
350   int try;
351
352   *start = readchar (timeout);
353   while ((try = convert_hex_digit (*start)) >= 0)
354     {
355       value <<= 4;
356       value += try;
357       *start = readchar (timeout);
358     }
359   return value;
360 }
361
362 #if 0
363 /* Get N 32-bit words from remote, each preceded by a space, and put
364    them in registers starting at REGNO.  */
365
366 static void
367 get_hex_regs (int n, int regno)
368 {
369   long val;
370   int i;
371
372   for (i = 0; i < n; i++)
373     {
374       int j;
375
376       val = 0;
377       for (j = 0; j < 8; j++)
378         val = (val << 4) + get_hex_digit (j == 0);
379       supply_register (regno++, (char *) &val);
380     }
381 }
382 #endif
383
384 /* This is called not only when we first attach, but also when the
385    user types "run" after having attached.  */
386
387 static void
388 e7000_create_inferior (char *execfile, char *args, char **env)
389 {
390   int entry_pt;
391
392   if (args && *args)
393     error ("Can't pass arguments to remote E7000DEBUG process");
394
395   if (execfile == 0 || exec_bfd == 0)
396     error ("No executable file specified");
397
398   entry_pt = (int) bfd_get_start_address (exec_bfd);
399
400 #ifdef CREATE_INFERIOR_HOOK
401   CREATE_INFERIOR_HOOK (0);     /* No process-ID */
402 #endif
403
404   /* The "process" (board) is already stopped awaiting our commands, and
405      the program is already downloaded.  We just set its PC and go.  */
406
407   clear_proceed_status ();
408
409   /* Tell wait_for_inferior that we've started a new process.  */
410   init_wait_for_inferior ();
411
412   /* Set up the "saved terminal modes" of the inferior
413      based on what modes we are starting it with.  */
414   target_terminal_init ();
415
416   /* Install inferior's terminal modes.  */
417   target_terminal_inferior ();
418
419   /* insert_step_breakpoint ();  FIXME, do we need this?  */
420   proceed ((CORE_ADDR) entry_pt, -1, 0);        /* Let 'er rip... */
421 }
422
423 /* Open a connection to a remote debugger.  NAME is the filename used
424    for communication.  */
425
426 static int baudrate = 9600;
427 static char dev_name[100];
428
429 static char *machine = "";
430 static char *user = "";
431 static char *passwd = "";
432 static char *dir = "";
433
434 /* Grab the next token and buy some space for it */
435
436 static char *
437 next (char **ptr)
438 {
439   char *p = *ptr;
440   char *s;
441   char *r;
442   int l = 0;
443
444   while (*p && *p == ' ')
445     p++;
446   s = p;
447   while (*p && (*p != ' ' && *p != '\t'))
448     {
449       l++;
450       p++;
451     }
452   r = xmalloc (l + 1);
453   memcpy (r, s, l);
454   r[l] = 0;
455   *ptr = p;
456   return r;
457 }
458
459 static void
460 e7000_login_command (char *args, int from_tty)
461 {
462   if (args)
463     {
464       machine = next (&args);
465       user = next (&args);
466       passwd = next (&args);
467       dir = next (&args);
468       if (from_tty)
469         {
470           printf_unfiltered ("Set info to %s %s %s %s\n", machine, user, passwd, dir);
471         }
472     }
473   else
474     {
475       error ("Syntax is ftplogin <machine> <user> <passwd> <directory>");
476     }
477 }
478
479 /* Start an ftp transfer from the E7000 to a host */
480
481 static void
482 e7000_ftp_command (char *args, int from_tty)
483 {
484   /* FIXME: arbitrary limit on machine names and such.  */
485   char buf[200];
486
487   int oldtimeout = timeout;
488   timeout = remote_timeout;
489
490   sprintf (buf, "ftp %s\r", machine);
491   puts_e7000debug (buf);
492   expect (" Username : ");
493   sprintf (buf, "%s\r", user);
494   puts_e7000debug (buf);
495   expect (" Password : ");
496   write_e7000 (passwd);
497   write_e7000 ("\r");
498   expect ("success\r");
499   expect ("FTP>");
500   sprintf (buf, "cd %s\r", dir);
501   puts_e7000debug (buf);
502   expect ("FTP>");
503   sprintf (buf, "ll 0;s:%s\r", args);
504   puts_e7000debug (buf);
505   expect ("FTP>");
506   puts_e7000debug ("bye\r");
507   expect (":");
508   timeout = oldtimeout;
509 }
510
511 static int
512 e7000_parse_device (char *args, char *dev_name, int baudrate)
513 {
514   char junk[128];
515   int n = 0;
516   if (args && strcasecmp (args, "pc") == 0)
517     {
518       strcpy (dev_name, args);
519       using_pc = 1;
520     }
521   else
522     {
523       /* FIXME! temp hack to allow use with port master -
524          target tcp_remote <device> */
525       if (args && strncmp (args, "tcp", 10) == 0)
526         {
527           char com_type[128];
528           n = sscanf (args, " %s %s %d %s", com_type, dev_name, &baudrate, junk);
529           using_tcp_remote = 1;
530           n--;
531         }
532       else if (args)
533         {
534           n = sscanf (args, " %s %d %s", dev_name, &baudrate, junk);
535         }
536
537       if (n != 1 && n != 2)
538         {
539           error ("Bad arguments.  Usage:\ttarget e7000 <device> <speed>\n\
540 or \t\ttarget e7000 <host>[:<port>]\n\
541 or \t\ttarget e7000 tcp_remote <host>[:<port>]\n\
542 or \t\ttarget e7000 pc\n");
543         }
544
545 #if !defined(__GO32__) && !defined(_WIN32)
546       /* FIXME!  test for ':' is ambiguous */
547       if (n == 1 && strchr (dev_name, ':') == 0)
548         {
549           /* Default to normal telnet port */
550           /* serial_open will use this to determine tcp communication */
551           strcat (dev_name, ":23");
552         }
553 #endif
554       if (!using_tcp_remote && strchr (dev_name, ':'))
555         using_tcp = 1;
556     }
557
558   return n;
559 }
560
561 /* Stub for catch_errors.  */
562
563 static int
564 e7000_start_remote (void *dummy)
565 {
566   int loop;
567   int sync;
568   int try;
569   int quit_trying;
570
571   immediate_quit++;             /* Allow user to interrupt it */
572
573   /* Hello?  Are you there?  */
574   sync = 0;
575   loop = 0;
576   try = 0;
577   quit_trying = 20;
578   putchar_e7000 (CTRLC);
579   while (!sync && ++try <= quit_trying)
580     {
581       int c;
582
583       printf_unfiltered ("[waiting for e7000...]\n");
584
585       write_e7000 ("\r");
586       c = readchar (1);
587
588       /* FIXME!  this didn't seem right->  while (c != SERIAL_TIMEOUT)
589        * we get stuck in this loop ...
590        * We may never timeout, and never sync up :-(
591        */
592       while (!sync && c != -1)
593         {
594           /* Dont echo cr's */
595           if (c != '\r')
596             {
597               putchar_unfiltered (c);
598               gdb_flush (gdb_stdout);
599             }
600           /* Shouldn't we either break here, or check for sync in inner loop? */
601           if (c == ':')
602             sync = 1;
603
604           if (loop++ == 20)
605             {
606               putchar_e7000 (CTRLC);
607               loop = 0;
608             }
609
610           QUIT;
611
612           if (quit_flag)
613             {
614               putchar_e7000 (CTRLC);
615               /* Was-> quit_flag = 0; */
616               c = -1;
617               quit_trying = try + 1;    /* we don't want to try anymore */
618             }
619           else
620             {
621               c = readchar (1);
622             }
623         }
624     }
625
626   if (!sync)
627     {
628       fprintf_unfiltered (gdb_stderr, "Giving up after %d tries...\n", try);
629       error ("Unable to synchronize with target.\n");
630     }
631
632   puts_e7000debug ("\r");
633   expect_prompt ();
634   puts_e7000debug ("b -\r");    /* Clear breakpoints */
635   expect_prompt ();
636
637   immediate_quit--;
638
639 /* This is really the job of start_remote however, that makes an assumption
640    that the target is about to print out a status message of some sort.  That
641    doesn't happen here. */
642
643   flush_cached_frames ();
644   registers_changed ();
645   stop_pc = read_pc ();
646   set_current_frame (create_new_frame (read_fp (), stop_pc));
647   select_frame (get_current_frame (), 0);
648   print_stack_frame (selected_frame, -1, 1);
649
650   return 1;
651 }
652
653 static void
654 e7000_open (char *args, int from_tty)
655 {
656   int n;
657
658   target_preopen (from_tty);
659
660   n = e7000_parse_device (args, dev_name, baudrate);
661
662   push_target (&e7000_ops);
663
664   e7000_desc = SERIAL_OPEN (dev_name);
665
666   if (!e7000_desc)
667     perror_with_name (dev_name);
668
669   SERIAL_SETBAUDRATE (e7000_desc, baudrate);
670   SERIAL_RAW (e7000_desc);
671
672 #ifdef GDB_TARGET_IS_H8300
673   h8300hmode = 1;
674 #endif
675
676   /* Start the remote connection; if error (0), discard this target.
677      In particular, if the user quits, be sure to discard it
678      (we'd be in an inconsistent state otherwise).  */
679   if (!catch_errors (e7000_start_remote, (char *) 0,
680        "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
681     if (from_tty)
682       printf_filtered ("Remote target %s connected to %s\n", target_shortname,
683                        dev_name);
684 }
685
686 /* Close out all files and local state before this target loses control. */
687
688 static void
689 e7000_close (int quitting)
690 {
691   if (e7000_desc)
692     {
693       SERIAL_CLOSE (e7000_desc);
694       e7000_desc = 0;
695     }
696 }
697
698 /* Terminate the open connection to the remote debugger.  Use this
699    when you want to detach and do something else with your gdb.  */
700
701 static void
702 e7000_detach (char *arg, int from_tty)
703 {
704   pop_target ();                /* calls e7000_close to do the real work */
705   if (from_tty)
706     printf_unfiltered ("Ending remote %s debugging\n", target_shortname);
707 }
708
709 /* Tell the remote machine to resume.  */
710
711 static void
712 e7000_resume (int pid, int step, enum target_signal sigal)
713 {
714   if (step)
715     puts_e7000debug ("S\r");
716   else
717     puts_e7000debug ("G\r");
718 }
719
720 /* Read the remote registers into the block REGS.  
721
722    For the H8/300 a register dump looks like:
723
724    PC=00021A  CCR=80:I*******
725    ER0 - ER3  0000000A 0000002E 0000002E 00000000
726    ER4 - ER7  00000000 00000000 00000000 00FFEFF6
727    000218           MOV.B     R1L,R2L
728    STEP NORMAL END or
729    BREAK POINT
730  */
731
732 char *want_h8300h = "PC=%p CCR=%c\n\
733  ER0 - ER3  %0 %1 %2 %3\n\
734  ER4 - ER7  %4 %5 %6 %7\n";
735
736 char *want_nopc_h8300h = "%p CCR=%c\n\
737  ER0 - ER3  %0 %1 %2 %3\n\
738  ER4 - ER7  %4 %5 %6 %7";
739
740 char *want_h8300s = "PC=%p CCR=%c\n\
741  MACH=\n\
742  ER0 - ER3  %0 %1 %2 %3\n\
743  ER4 - ER7  %4 %5 %6 %7\n";
744
745 char *want_nopc_h8300s = "%p CCR=%c EXR=%9\n\
746  ER0 - ER3  %0 %1 %2 %3\n\
747  ER4 - ER7  %4 %5 %6 %7";
748
749 char *want_sh = "PC=%16 SR=%22\n\
750 PR=%17 GBR=%18 VBR=%19\n\
751 MACH=%20 MACL=%21\n\
752 R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
753 R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n";
754
755 char *want_nopc_sh = "%16 SR=%22\n\
756  PR=%17 GBR=%18 VBR=%19\n\
757  MACH=%20 MACL=%21\n\
758  R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
759  R8-15 %8 %9 %10 %11 %12 %13 %14 %15";
760
761 char *want_sh3 = "PC=%16 SR=%22\n\
762 PR=%17 GBR=%18 VBR=%19\n\
763 MACH=%20 MACL=%21 SSR=%23 SPC=%24\n\
764 R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
765 R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
766 R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
767 R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
768 R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
769 R4_BANK1-R7_BANK1 %37 %38 %39 %40";
770
771 char *want_nopc_sh3 = "%16 SR=%22\n\
772  PR=%17 GBR=%18 VBR=%19\n\
773  MACH=%20 MACL=%21 SSR=%22 SPC=%23\n\
774  R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
775  R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n\
776  R0_BANK0-R3_BANK0 %25 %26 %27 %28\n\
777  R4_BANK0-R7_BANK0 %29 %30 %31 %32\n\
778  R0_BANK1-R3_BANK1 %33 %34 %35 %36\n\
779  R4_BANK1-R7_BANK1 %37 %38 %39 %40";
780
781 static int
782 gch (void)
783 {
784   return readchar (timeout);
785 }
786
787 static unsigned int
788 gbyte (void)
789 {
790   int high = convert_hex_digit (gch ());
791   int low = convert_hex_digit (gch ());
792
793   return (high << 4) + low;
794 }
795
796 void
797 fetch_regs_from_dump (int (*nextchar) (), char *want)
798 {
799   int regno;
800   char buf[MAX_REGISTER_RAW_SIZE];
801
802   int thischar = nextchar ();
803
804   while (*want)
805     {
806       switch (*want)
807         {
808         case '\n':
809           /* Skip to end of line and then eat all new line type stuff */
810           while (thischar != '\n' && thischar != '\r')
811             thischar = nextchar ();
812           while (thischar == '\n' || thischar == '\r')
813             thischar = nextchar ();
814           want++;
815           break;
816
817         case ' ':
818           while (thischar == ' '
819                  || thischar == '\t'
820                  || thischar == '\r'
821                  || thischar == '\n')
822             thischar = nextchar ();
823           want++;
824           break;
825
826         default:
827           if (*want == thischar)
828             {
829               want++;
830               if (*want)
831                 thischar = nextchar ();
832
833             }
834           else if (thischar == ' ' || thischar == '\n' || thischar == '\r')
835             {
836               thischar = nextchar ();
837             }
838           else
839             {
840               error ("out of sync in fetch registers wanted <%s>, got <%c 0x%x>",
841                      want, thischar, thischar);
842             }
843
844           break;
845         case '%':
846           /* Got a register command */
847           want++;
848           switch (*want)
849             {
850 #ifdef PC_REGNUM
851             case 'p':
852               regno = PC_REGNUM;
853               want++;
854               break;
855 #endif
856 #ifdef CCR_REGNUM
857             case 'c':
858               regno = CCR_REGNUM;
859               want++;
860               break;
861 #endif
862 #ifdef SP_REGNUM
863             case 's':
864               regno = SP_REGNUM;
865               want++;
866               break;
867 #endif
868 #ifdef FP_REGNUM
869             case 'f':
870               regno = FP_REGNUM;
871               want++;
872               break;
873 #endif
874
875             default:
876               if (isdigit (want[0]))
877                 {
878                   if (isdigit (want[1]))
879                     {
880                       regno = (want[0] - '0') * 10 + want[1] - '0';
881                       want += 2;
882                     }
883                   else
884                     {
885                       regno = want[0] - '0';
886                       want++;
887                     }
888                 }
889
890               else
891                 abort ();
892             }
893           store_signed_integer (buf,
894                                 REGISTER_RAW_SIZE (regno),
895                                 (LONGEST) get_hex (&thischar));
896           supply_register (regno, buf);
897           break;
898         }
899     }
900 }
901
902 static void
903 e7000_fetch_registers (void)
904 {
905   int regno;
906   char *wanted;
907
908   puts_e7000debug ("R\r");
909
910   if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
911     {
912       wanted = want_sh;
913       switch (TARGET_ARCHITECTURE->mach)
914         {
915         case bfd_mach_sh3:
916         case bfd_mach_sh3e:
917         case bfd_mach_sh4:
918           wanted = want_sh3;
919         }
920     }
921 #ifdef GDB_TARGET_IS_H8300
922   if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
923     {
924       if (h8300smode)
925         wanted = want_h8300s;
926       else
927         wanted = want_h8300h;
928     }
929 #endif
930
931   fetch_regs_from_dump (gch, wanted);
932
933   /* And supply the extra ones the simulator uses */
934   for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
935     {
936       int buf = 0;
937
938       supply_register (regno, (char *) (&buf));
939     }
940 }
941
942 /* Fetch register REGNO, or all registers if REGNO is -1.  Returns
943    errno value.  */
944
945 static void
946 e7000_fetch_register (int regno)
947 {
948   e7000_fetch_registers ();
949 }
950
951 /* Store the remote registers from the contents of the block REGS.  */
952
953 static void
954 e7000_store_registers (void)
955 {
956   int regno;
957
958   for (regno = 0; regno < NUM_REALREGS; regno++)
959     e7000_store_register (regno);
960
961   registers_changed ();
962 }
963
964 /* Store register REGNO, or all if REGNO == 0.  Return errno value.  */
965
966 static void
967 e7000_store_register (int regno)
968 {
969   char buf[200];
970
971   if (regno == -1)
972     {
973       e7000_store_registers ();
974       return;
975     }
976
977   if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
978     {
979       if (regno <= 7)
980         {
981           sprintf (buf, ".ER%d %lx\r", regno, read_register (regno));
982           puts_e7000debug (buf);
983         }
984       else if (regno == PC_REGNUM)
985         {
986           sprintf (buf, ".PC %lx\r", read_register (regno));
987           puts_e7000debug (buf);
988         }
989 #ifdef CCR_REGNUM
990       else if (regno == CCR_REGNUM)
991         {
992           sprintf (buf, ".CCR %lx\r", read_register (regno));
993           puts_e7000debug (buf);
994         }
995 #endif
996     }
997
998   else if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
999     {
1000       if (regno == PC_REGNUM)
1001         {
1002           sprintf (buf, ".PC %lx\r", read_register (regno));
1003           puts_e7000debug (buf);
1004         }
1005
1006       else if (regno == SR_REGNUM)
1007         {
1008           sprintf (buf, ".SR %lx\r", read_register (regno));
1009           puts_e7000debug (buf);
1010         }
1011
1012       else if (regno ==  PR_REGNUM)
1013         {
1014           sprintf (buf, ".PR %lx\r", read_register (regno));
1015           puts_e7000debug (buf);
1016         }
1017
1018       else if (regno == GBR_REGNUM)
1019         {
1020           sprintf (buf, ".GBR %lx\r", read_register (regno));
1021           puts_e7000debug (buf);
1022         }
1023
1024       else if (regno == VBR_REGNUM)
1025         {
1026           sprintf (buf, ".VBR %lx\r", read_register (regno));
1027           puts_e7000debug (buf);
1028         }
1029
1030       else if (regno == MACH_REGNUM)
1031         {
1032           sprintf (buf, ".MACH %lx\r", read_register (regno));
1033           puts_e7000debug (buf);
1034         }
1035
1036       else if (regno == MACL_REGNUM)
1037         {
1038           sprintf (buf, ".MACL %lx\r", read_register (regno));
1039           puts_e7000debug (buf);
1040         }
1041       else
1042         {
1043           sprintf (buf, ".R%d %lx\r", regno, read_register (regno));
1044           puts_e7000debug (buf);
1045         }
1046     }
1047
1048   expect_prompt ();
1049 }
1050
1051 /* Get ready to modify the registers array.  On machines which store
1052    individual registers, this doesn't need to do anything.  On machines
1053    which store all the registers in one fell swoop, this makes sure
1054    that registers contains all the registers from the program being
1055    debugged.  */
1056
1057 static void
1058 e7000_prepare_to_store (void)
1059 {
1060   /* Do nothing, since we can store individual regs */
1061 }
1062
1063 static void
1064 e7000_files_info (struct target_ops *ops)
1065 {
1066   printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baudrate);
1067 }
1068
1069 static int
1070 stickbyte (char *where, unsigned int what)
1071 {
1072   static CONST char digs[] = "0123456789ABCDEF";
1073
1074   where[0] = digs[(what >> 4) & 0xf];
1075   where[1] = digs[(what & 0xf) & 0xf];
1076
1077   return what;
1078 }
1079
1080 /* Write a small ammount of memory. */
1081
1082 static int
1083 write_small (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1084 {
1085   int i;
1086   char buf[200];
1087
1088   for (i = 0; i < len; i++)
1089     {
1090       if (((memaddr + i) & 3) == 0 && (i + 3 < len))
1091         {
1092           /* Can be done with a long word */
1093           sprintf (buf, "m %lx %x%02x%02x%02x;l\r",
1094                    memaddr + i,
1095                    myaddr[i], myaddr[i + 1], myaddr[i + 2], myaddr[i + 3]);
1096           puts_e7000debug (buf);
1097           i += 3;
1098         }
1099       else
1100         {
1101           sprintf (buf, "m %lx %x\r", memaddr + i, myaddr[i]);
1102           puts_e7000debug (buf);
1103         }
1104     }
1105
1106   expect_prompt ();
1107
1108   return len;
1109 }
1110
1111 /* Write a large ammount of memory, this only works with the serial
1112    mode enabled.  Command is sent as
1113
1114    il ;s:s\r     ->
1115    <- il ;s:s\r
1116    <-   ENQ
1117    ACK          ->
1118    <- LO s\r
1119    Srecords...
1120    ^Z           ->
1121    <-   ENQ
1122    ACK          ->  
1123    <-   :       
1124  */
1125
1126 static int
1127 write_large (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1128 {
1129   int i;
1130 #define maxstride  128
1131   int stride;
1132
1133   puts_e7000debug ("IL ;S:FK\r");
1134   expect (ENQSTRING);
1135   putchar_e7000 (ACK);
1136   expect ("LO FK\r");
1137
1138   for (i = 0; i < len; i += stride)
1139     {
1140       char compose[maxstride * 2 + 50];
1141       int address = i + memaddr;
1142       int j;
1143       int check_sum;
1144       int where = 0;
1145       int alen;
1146
1147       stride = len - i;
1148       if (stride > maxstride)
1149         stride = maxstride;
1150
1151       compose[where++] = 'S';
1152       check_sum = 0;
1153       if (address >= 0xffffff)
1154         alen = 4;
1155       else if (address >= 0xffff)
1156         alen = 3;
1157       else
1158         alen = 2;
1159       /* Insert type. */
1160       compose[where++] = alen - 1 + '0';
1161       /* Insert length. */
1162       check_sum += stickbyte (compose + where, alen + stride + 1);
1163       where += 2;
1164       while (alen > 0)
1165         {
1166           alen--;
1167           check_sum += stickbyte (compose + where, address >> (8 * (alen)));
1168           where += 2;
1169         }
1170
1171       for (j = 0; j < stride; j++)
1172         {
1173           check_sum += stickbyte (compose + where, myaddr[i + j]);
1174           where += 2;
1175         }
1176       stickbyte (compose + where, ~check_sum);
1177       where += 2;
1178       compose[where++] = '\r';
1179       compose[where++] = '\n';
1180       compose[where++] = 0;
1181
1182       SERIAL_WRITE (e7000_desc, compose, where);
1183       j = readchar (0);
1184       if (j == -1)
1185         {
1186           /* This is ok - nothing there */
1187         }
1188       else if (j == ENQ)
1189         {
1190           /* Hmm, it's trying to tell us something */
1191           expect (":");
1192           error ("Error writing memory");
1193         }
1194       else
1195         {
1196           printf_unfiltered ("@%d}@", j);
1197           while ((j = readchar (0)) > 0)
1198             {
1199               printf_unfiltered ("@{%d}@", j);
1200             }
1201         }
1202     }
1203
1204   /* Send the trailer record */
1205   write_e7000 ("S70500000000FA\r");
1206   putchar_e7000 (CTRLZ);
1207   expect (ENQSTRING);
1208   putchar_e7000 (ACK);
1209   expect (":");
1210
1211   return len;
1212 }
1213
1214 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
1215    memory at MEMADDR.  Returns length moved.
1216
1217    Can't use the Srecord load over ethernet, so don't use fast method
1218    then.  */
1219
1220 static int
1221 e7000_write_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1222 {
1223   if (len < 16 || using_tcp || using_pc)
1224     return write_small (memaddr, myaddr, len);
1225   else
1226     return write_large (memaddr, myaddr, len);
1227 }
1228
1229 /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
1230    at debugger address MYADDR.  Returns length moved. 
1231
1232    Small transactions we send
1233    m <addr>;l
1234    and receive
1235    00000000 12345678 ?
1236  */
1237
1238 static int
1239 e7000_read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
1240 {
1241   int count;
1242   int c;
1243   int i;
1244   char buf[200];
1245   /* Starting address of this pass.  */
1246
1247 /*  printf("READ INF %x %x %d\n", memaddr, myaddr, len); */
1248   if (((memaddr - 1) + len) < memaddr)
1249     {
1250       errno = EIO;
1251       return 0;
1252     }
1253
1254   sprintf (buf, "m %lx;l\r", memaddr);
1255   puts_e7000debug (buf);
1256
1257   for (count = 0; count < len; count += 4)
1258     {
1259       /* Suck away the address */
1260       c = gch ();
1261       while (c != ' ')
1262         c = gch ();
1263       c = gch ();
1264       if (c == '*')
1265         {                       /* Some kind of error */
1266           puts_e7000debug (".\r");      /* Some errors leave us in memory input mode */
1267           expect_full_prompt ();
1268           return -1;
1269         }
1270       while (c != ' ')
1271         c = gch ();
1272
1273       /* Now read in the data */
1274       for (i = 0; i < 4; i++)
1275         {
1276           int b = gbyte ();
1277           if (count + i < len)
1278             {
1279               myaddr[count + i] = b;
1280             }
1281         }
1282
1283       /* Skip the trailing ? and send a . to end and a cr for more */
1284       gch ();
1285       gch ();
1286       if (count + 4 >= len)
1287         puts_e7000debug (".\r");
1288       else
1289         puts_e7000debug ("\r");
1290
1291     }
1292   expect_prompt ();
1293   return len;
1294 }
1295
1296
1297
1298 /*
1299    For large transfers we used to send
1300
1301
1302    d <addr> <endaddr>\r
1303
1304    and receive
1305    <ADDRESS>           <    D   A   T   A    >               <   ASCII CODE   >
1306    00000000 5F FD FD FF DF 7F DF FF  01 00 01 00 02 00 08 04  "_..............."
1307    00000010 FF D7 FF 7F D7 F1 7F FF  00 05 00 00 08 00 40 00  "..............@."
1308    00000020 7F FD FF F7 7F FF FF F7  00 00 00 00 00 00 00 00  "................"
1309
1310    A cost in chars for each transaction of 80 + 5*n-bytes. 
1311
1312    Large transactions could be done with the srecord load code, but
1313    there is a pause for a second before dumping starts, which slows the
1314    average rate down!
1315  */
1316
1317 static int
1318 e7000_read_inferior_memory_large (CORE_ADDR memaddr, unsigned char *myaddr,
1319                                   int len)
1320 {
1321   int count;
1322   int c;
1323   char buf[200];
1324
1325   /* Starting address of this pass.  */
1326
1327   if (((memaddr - 1) + len) < memaddr)
1328     {
1329       errno = EIO;
1330       return 0;
1331     }
1332
1333   sprintf (buf, "d %lx %lx\r", memaddr, memaddr + len - 1);
1334   puts_e7000debug (buf);
1335
1336   count = 0;
1337   c = gch ();
1338
1339   /* skip down to the first ">" */
1340   while (c != '>')
1341     c = gch ();
1342   /* now skip to the end of that line */
1343   while (c != '\r')
1344     c = gch ();
1345   c = gch ();
1346
1347   while (count < len)
1348     {
1349       /* get rid of any white space before the address */
1350       while (c <= ' ')
1351         c = gch ();
1352
1353       /* Skip the address */
1354       get_hex (&c);
1355
1356       /* read in the bytes on the line */
1357       while (c != '"' && count < len)
1358         {
1359           if (c == ' ')
1360             c = gch ();
1361           else
1362             {
1363               myaddr[count++] = get_hex (&c);
1364             }
1365         }
1366       /* throw out the rest of the line */
1367       while (c != '\r')
1368         c = gch ();
1369     }
1370
1371   /* wait for the ":" prompt */
1372   while (c != ':')
1373     c = gch ();
1374
1375   return len;
1376 }
1377
1378 #if 0
1379
1380 static int
1381 fast_but_for_the_pause_e7000_read_inferior_memory (CORE_ADDR memaddr,
1382                                                    char *myaddr, int len)
1383 {
1384   int loop;
1385   int c;
1386   char buf[200];
1387
1388   if (((memaddr - 1) + len) < memaddr)
1389     {
1390       errno = EIO;
1391       return 0;
1392     }
1393
1394   sprintf (buf, "is %x@%x:s\r", memaddr, len);
1395   puts_e7000debug (buf);
1396   gch ();
1397   c = gch ();
1398   if (c != ENQ)
1399     {
1400       /* Got an error */
1401       error ("Memory read error");
1402     }
1403   putchar_e7000 (ACK);
1404   expect ("SV s");
1405   loop = 1;
1406   while (loop)
1407     {
1408       int type;
1409       int length;
1410       int addr;
1411       int i;
1412
1413       c = gch ();
1414       switch (c)
1415         {
1416         case ENQ:               /* ENQ, at the end */
1417           loop = 0;
1418           break;
1419         case 'S':
1420           /* Start of an Srecord */
1421           type = gch ();
1422           length = gbyte ();
1423           switch (type)
1424             {
1425             case '7':           /* Termination record, ignore */
1426             case '0':
1427             case '8':
1428             case '9':
1429               /* Header record - ignore it */
1430               while (length--)
1431                 {
1432                   gbyte ();
1433                 }
1434               break;
1435             case '1':
1436             case '2':
1437             case '3':
1438               {
1439                 int alen;
1440
1441                 alen = type - '0' + 1;
1442                 addr = 0;
1443                 while (alen--)
1444                   {
1445                     addr = (addr << 8) + gbyte ();
1446                     length--;
1447                   }
1448
1449                 for (i = 0; i < length - 1; i++)
1450                   myaddr[i + addr - memaddr] = gbyte ();
1451
1452                 gbyte ();       /* Ignore checksum */
1453               }
1454             }
1455         }
1456     }
1457
1458   putchar_e7000 (ACK);
1459   expect ("TOP ADDRESS =");
1460   expect ("END ADDRESS =");
1461   expect (":");
1462
1463   return len;
1464 }
1465
1466 #endif
1467
1468 /* Transfer LEN bytes between GDB address MYADDR and target address
1469    MEMADDR.  If WRITE is non-zero, transfer them to the target,
1470    otherwise transfer them from the target.  TARGET is unused.
1471
1472    Returns the number of bytes transferred. */
1473
1474 static int
1475 e7000_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr,
1476                             int len, int write, 
1477                             struct mem_attrib *attrib ATTRIBUTE_UNUSED,
1478                             struct target_ops *target ATTRIBUTE_UNUSED)
1479 {
1480   if (write)
1481     return e7000_write_inferior_memory (memaddr, myaddr, len);
1482   else if (len < 16)
1483     return e7000_read_inferior_memory (memaddr, myaddr, len);
1484   else
1485     return e7000_read_inferior_memory_large (memaddr, myaddr, len);
1486 }
1487
1488 static void
1489 e7000_kill (void)
1490 {
1491 }
1492
1493 static void
1494 e7000_load (char *args, int from_tty)
1495 {
1496   struct cleanup *old_chain;
1497   asection *section;
1498   bfd *pbfd;
1499   bfd_vma entry;
1500 #define WRITESIZE 0x1000
1501   char buf[2 + 4 + 4 + WRITESIZE];      /* `DT' + <addr> + <len> + <data> */
1502   char *filename;
1503   int quiet;
1504   int nostart;
1505   time_t start_time, end_time;  /* Start and end times of download */
1506   unsigned long data_count;     /* Number of bytes transferred to memory */
1507   int oldtimeout = timeout;
1508
1509   timeout = remote_timeout;
1510
1511
1512   /* FIXME! change test to test for type of download */
1513   if (!using_tcp)
1514     {
1515       generic_load (args, from_tty);
1516       return;
1517     }
1518
1519   /* for direct tcp connections, we can do a fast binary download */
1520   buf[0] = 'D';
1521   buf[1] = 'T';
1522   quiet = 0;
1523   nostart = 0;
1524   filename = NULL;
1525
1526   while (*args != '\000')
1527     {
1528       char *arg;
1529
1530       while (isspace (*args))
1531         args++;
1532
1533       arg = args;
1534
1535       while ((*args != '\000') && !isspace (*args))
1536         args++;
1537
1538       if (*args != '\000')
1539         *args++ = '\000';
1540
1541       if (*arg != '-')
1542         filename = arg;
1543       else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1544         quiet = 1;
1545       else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1546         nostart = 1;
1547       else
1548         error ("unknown option `%s'", arg);
1549     }
1550
1551   if (!filename)
1552     filename = get_exec_file (1);
1553
1554   pbfd = bfd_openr (filename, gnutarget);
1555   if (pbfd == NULL)
1556     {
1557       perror_with_name (filename);
1558       return;
1559     }
1560   old_chain = make_cleanup_bfd_close (pbfd);
1561
1562   if (!bfd_check_format (pbfd, bfd_object))
1563     error ("\"%s\" is not an object file: %s", filename,
1564            bfd_errmsg (bfd_get_error ()));
1565
1566   start_time = time (NULL);
1567   data_count = 0;
1568
1569   puts_e7000debug ("mw\r");
1570
1571   expect ("\nOK");
1572
1573   for (section = pbfd->sections; section; section = section->next)
1574     {
1575       if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1576         {
1577           bfd_vma section_address;
1578           bfd_size_type section_size;
1579           file_ptr fptr;
1580
1581           section_address = bfd_get_section_vma (pbfd, section);
1582           section_size = bfd_get_section_size_before_reloc (section);
1583
1584           if (!quiet)
1585             printf_filtered ("[Loading section %s at 0x%x (%ud bytes)]\n",
1586                              bfd_get_section_name (pbfd, section),
1587                              section_address,
1588                              section_size);
1589
1590           fptr = 0;
1591
1592           data_count += section_size;
1593
1594           while (section_size > 0)
1595             {
1596               int count;
1597               static char inds[] = "|/-\\";
1598               static int k = 0;
1599
1600               QUIT;
1601
1602               count = min (section_size, WRITESIZE);
1603
1604               buf[2] = section_address >> 24;
1605               buf[3] = section_address >> 16;
1606               buf[4] = section_address >> 8;
1607               buf[5] = section_address;
1608
1609               buf[6] = count >> 24;
1610               buf[7] = count >> 16;
1611               buf[8] = count >> 8;
1612               buf[9] = count;
1613
1614               bfd_get_section_contents (pbfd, section, buf + 10, fptr, count);
1615
1616               if (SERIAL_WRITE (e7000_desc, buf, count + 10))
1617                 fprintf_unfiltered (gdb_stderr,
1618                                     "e7000_load: SERIAL_WRITE failed: %s\n",
1619                                     safe_strerror (errno));
1620
1621               expect ("OK");
1622
1623               if (!quiet)
1624                 {
1625                   printf_unfiltered ("\r%c", inds[k++ % 4]);
1626                   gdb_flush (gdb_stdout);
1627                 }
1628
1629               section_address += count;
1630               fptr += count;
1631               section_size -= count;
1632             }
1633         }
1634     }
1635
1636   write_e7000 ("ED");
1637
1638   expect_prompt ();
1639
1640   end_time = time (NULL);
1641
1642 /* Finally, make the PC point at the start address */
1643
1644   if (exec_bfd)
1645     write_pc (bfd_get_start_address (exec_bfd));
1646
1647   inferior_pid = 0;             /* No process now */
1648
1649 /* This is necessary because many things were based on the PC at the time that
1650    we attached to the monitor, which is no longer valid now that we have loaded
1651    new code (and just changed the PC).  Another way to do this might be to call
1652    normal_stop, except that the stack may not be valid, and things would get
1653    horribly confused... */
1654
1655   clear_symtab_users ();
1656
1657   if (!nostart)
1658     {
1659       entry = bfd_get_start_address (pbfd);
1660
1661       if (!quiet)
1662         printf_unfiltered ("[Starting %s at 0x%x]\n", filename, entry);
1663
1664 /*      start_routine (entry); */
1665     }
1666
1667   report_transfer_performance (data_count, start_time, end_time);
1668
1669   do_cleanups (old_chain);
1670   timeout = oldtimeout;
1671 }
1672
1673 /* Clean up when a program exits.
1674
1675    The program actually lives on in the remote processor's RAM, and may be
1676    run again without a download.  Don't leave it full of breakpoint
1677    instructions.  */
1678
1679 static void
1680 e7000_mourn_inferior (void)
1681 {
1682   remove_breakpoints ();
1683   unpush_target (&e7000_ops);
1684   generic_mourn_inferior ();    /* Do all the proper things now */
1685 }
1686
1687 #define MAX_BREAKPOINTS 200
1688 #ifdef  HARD_BREAKPOINTS
1689 #define MAX_E7000DEBUG_BREAKPOINTS (BC_BREAKPOINTS ? 5 :  MAX_BREAKPOINTS)
1690 #else
1691 #define MAX_E7000DEBUG_BREAKPOINTS MAX_BREAKPOINTS
1692 #endif
1693
1694 /* Since we can change to soft breakpoints dynamically, we must define 
1695    more than enough.  Was breakaddr[MAX_E7000DEBUG_BREAKPOINTS]. */
1696 static CORE_ADDR breakaddr[MAX_BREAKPOINTS] =
1697 {0};
1698
1699 static int
1700 e7000_insert_breakpoint (CORE_ADDR addr, char *shadow)
1701 {
1702   int i;
1703   char buf[200];
1704 #if 0
1705   static char nop[2] = NOP;
1706 #endif
1707
1708   for (i = 0; i <= MAX_E7000DEBUG_BREAKPOINTS; i++)
1709     if (breakaddr[i] == 0)
1710       {
1711         breakaddr[i] = addr;
1712         /* Save old contents, and insert a nop in the space */
1713 #ifdef HARD_BREAKPOINTS
1714         if (BC_BREAKPOINTS)
1715           {
1716             sprintf (buf, "BC%d A=%lx\r", i + 1, addr);
1717             puts_e7000debug (buf);
1718           }
1719         else
1720           {
1721             sprintf (buf, "B %lx\r", addr);
1722             puts_e7000debug (buf);
1723           }
1724 #else
1725 #if 0
1726         e7000_read_inferior_memory (addr, shadow, 2);
1727         e7000_write_inferior_memory (addr, nop, 2);
1728 #endif
1729
1730         sprintf (buf, "B %x\r", addr);
1731         puts_e7000debug (buf);
1732 #endif
1733         expect_prompt ();
1734         return 0;
1735       }
1736
1737   error ("Too many breakpoints ( > %d) for the E7000\n",
1738          MAX_E7000DEBUG_BREAKPOINTS);
1739   return 1;
1740 }
1741
1742 static int
1743 e7000_remove_breakpoint (CORE_ADDR addr, char *shadow)
1744 {
1745   int i;
1746   char buf[200];
1747
1748   for (i = 0; i < MAX_E7000DEBUG_BREAKPOINTS; i++)
1749     if (breakaddr[i] == addr)
1750       {
1751         breakaddr[i] = 0;
1752 #ifdef HARD_BREAKPOINTS
1753         if (BC_BREAKPOINTS)
1754           {
1755             sprintf (buf, "BC%d - \r", i + 1);
1756             puts_e7000debug (buf);
1757           }
1758         else
1759           {
1760             sprintf (buf, "B - %lx\r", addr);
1761             puts_e7000debug (buf);
1762           }
1763         expect_prompt ();
1764 #else
1765         sprintf (buf, "B - %lx\r", addr);
1766         puts_e7000debug (buf);
1767         expect_prompt ();
1768
1769 #if 0
1770         /* Replace the insn under the break */
1771         e7000_write_inferior_memory (addr, shadow, 2);
1772 #endif
1773 #endif
1774
1775         return 0;
1776       }
1777
1778   warning ("Can't find breakpoint associated with 0x%lx\n", addr);
1779   return 1;
1780 }
1781
1782 /* Put a command string, in args, out to STDBUG.  Output from STDBUG
1783    is placed on the users terminal until the prompt is seen. */
1784
1785 static void
1786 e7000_command (char *args, int fromtty)
1787 {
1788   /* FIXME: arbitrary limit on length of args.  */
1789   char buf[200];
1790
1791   echo = 0;
1792
1793   if (!e7000_desc)
1794     error ("e7000 target not open.");
1795   if (!args)
1796     {
1797       puts_e7000debug ("\r");
1798     }
1799   else
1800     {
1801       sprintf (buf, "%s\r", args);
1802       puts_e7000debug (buf);
1803     }
1804
1805   echo++;
1806   ctrl_c = 2;
1807   expect_full_prompt ();
1808   echo--;
1809   ctrl_c = 0;
1810   printf_unfiltered ("\n");
1811
1812   /* Who knows what the command did... */
1813   registers_changed ();
1814 }
1815
1816
1817 static void
1818 e7000_drain_command (char *args, int fromtty)
1819 {
1820   int c;
1821
1822   puts_e7000debug ("end\r");
1823   putchar_e7000 (CTRLC);
1824
1825   while ((c = readchar (1) != -1))
1826     {
1827       if (quit_flag)
1828         {
1829           putchar_e7000 (CTRLC);
1830           quit_flag = 0;
1831         }
1832       if (c > ' ' && c < 127)
1833         printf_unfiltered ("%c", c & 0xff);
1834       else
1835         printf_unfiltered ("<%x>", c & 0xff);
1836     }
1837 }
1838
1839 #define NITEMS 7
1840
1841 static int
1842 why_stop (void)
1843 {
1844   static char *strings[NITEMS] =
1845   {
1846     "STEP NORMAL",
1847     "BREAK POINT",
1848     "BREAK KEY",
1849     "BREAK CONDI",
1850     "CYCLE ACCESS",
1851     "ILLEGAL INSTRUCTION",
1852     "WRITE PROTECT",
1853   };
1854   char *p[NITEMS];
1855   int c;
1856   int i;
1857
1858   for (i = 0; i < NITEMS; ++i)
1859     p[i] = strings[i];
1860
1861   c = gch ();
1862   while (1)
1863     {
1864       for (i = 0; i < NITEMS; i++)
1865         {
1866           if (c == *(p[i]))
1867             {
1868               p[i]++;
1869               if (*(p[i]) == 0)
1870                 {
1871                   /* found one of the choices */
1872                   return i;
1873                 }
1874             }
1875           else
1876             p[i] = strings[i];
1877         }
1878
1879       c = gch ();
1880     }
1881 }
1882
1883 /* Suck characters, if a string match, then return the strings index
1884    otherwise echo them.  */
1885
1886 int
1887 expect_n (char **strings)
1888 {
1889   char *(ptr[10]);
1890   int n;
1891   int c;
1892   char saveaway[100];
1893   char *buffer = saveaway;
1894   /* Count number of expect strings  */
1895
1896   for (n = 0; strings[n]; n++)
1897     {
1898       ptr[n] = strings[n];
1899     }
1900
1901   while (1)
1902     {
1903       int i;
1904       int gotone = 0;
1905
1906       c = readchar (1);
1907       if (c == -1)
1908         {
1909           printf_unfiltered ("[waiting for e7000...]\n");
1910         }
1911 #ifdef __GO32__
1912       if (kbhit ())
1913         {
1914           int k = getkey ();
1915
1916           if (k == 1)
1917             quit_flag = 1;
1918         }
1919 #endif
1920       if (quit_flag)
1921         {
1922           putchar_e7000 (CTRLC);        /* interrupt the running program */
1923           quit_flag = 0;
1924         }
1925
1926       for (i = 0; i < n; i++)
1927         {
1928           if (c == ptr[i][0])
1929             {
1930               ptr[i]++;
1931               if (ptr[i][0] == 0)
1932                 {
1933                   /* Gone all the way */
1934                   return i;
1935                 }
1936               gotone = 1;
1937             }
1938           else
1939             {
1940               ptr[i] = strings[i];
1941             }
1942         }
1943
1944       if (gotone)
1945         {
1946           /* Save it up incase we find that there was no match */
1947           *buffer++ = c;
1948         }
1949       else
1950         {
1951           if (buffer != saveaway)
1952             {
1953               *buffer++ = 0;
1954               printf_unfiltered ("%s", buffer);
1955               buffer = saveaway;
1956             }
1957           if (c != -1)
1958             {
1959               putchar_unfiltered (c);
1960               gdb_flush (gdb_stdout);
1961             }
1962         }
1963     }
1964 }
1965
1966 /* We subtract two from the pc here rather than use
1967    DECR_PC_AFTER_BREAK since the e7000 doesn't always add two to the
1968    pc, and the simulators never do. */
1969
1970 static void
1971 sub2_from_pc (void)
1972 {
1973   char buf[4];
1974   char buf2[200];
1975
1976   store_signed_integer (buf,
1977                         REGISTER_RAW_SIZE (PC_REGNUM),
1978                         read_register (PC_REGNUM) - 2);
1979   supply_register (PC_REGNUM, buf);
1980   sprintf (buf2, ".PC %lx\r", read_register (PC_REGNUM));
1981   puts_e7000debug (buf2);
1982 }
1983
1984 #define WAS_SLEEP 0
1985 #define WAS_INT 1
1986 #define WAS_RUNNING 2
1987 #define WAS_OTHER 3
1988
1989 static char *estrings[] =
1990 {
1991   "** SLEEP",
1992   "BREAK !",
1993   "** PC",
1994   "PC",
1995   NULL
1996 };
1997
1998 /* Wait until the remote machine stops, then return, storing status in
1999    STATUS just as `wait' would.  */
2000
2001 static int
2002 e7000_wait (int pid, struct target_waitstatus *status)
2003 {
2004   int stop_reason;
2005   int regno;
2006   int running_count = 0;
2007   int had_sleep = 0;
2008   int loop = 1;
2009   char *wanted_nopc;
2010
2011   /* Then echo chars until PC= string seen */
2012   gch ();                       /* Drop cr */
2013   gch ();                       /* and space */
2014
2015   while (loop)
2016     {
2017       switch (expect_n (estrings))
2018         {
2019         case WAS_OTHER:
2020           /* how did this happen ? */
2021           loop = 0;
2022           break;
2023         case WAS_SLEEP:
2024           had_sleep = 1;
2025           putchar_e7000 (CTRLC);
2026           loop = 0;
2027           break;
2028         case WAS_INT:
2029           loop = 0;
2030           break;
2031         case WAS_RUNNING:
2032           running_count++;
2033           if (running_count == 20)
2034             {
2035               printf_unfiltered ("[running...]\n");
2036               running_count = 0;
2037             }
2038           break;
2039         default:
2040           /* error? */
2041           break;
2042         }
2043     }
2044
2045   /* Skip till the PC= */
2046   expect ("=");
2047
2048   if (TARGET_ARCHITECTURE->arch == bfd_arch_sh)
2049     {
2050       wanted_nopc = want_nopc_sh;
2051       switch (TARGET_ARCHITECTURE->mach)
2052         {
2053         case bfd_mach_sh3:
2054         case bfd_mach_sh3e:
2055         case bfd_mach_sh4:
2056           wanted_nopc = want_nopc_sh3;
2057         }
2058     }
2059 #ifdef GDB_TARGET_IS_H8300
2060   if (TARGET_ARCHITECTURE->arch == bfd_arch_h8300)
2061     {
2062       if (h8300smode)
2063         wanted_nopc = want_nopc_h8300s;
2064       else
2065         wanted_nopc = want_nopc_h8300h;
2066     }
2067 #endif
2068   fetch_regs_from_dump (gch, wanted_nopc);
2069
2070   /* And supply the extra ones the simulator uses */
2071   for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
2072     {
2073       int buf = 0;
2074       supply_register (regno, (char *) &buf);
2075     }
2076
2077   stop_reason = why_stop ();
2078   expect_full_prompt ();
2079
2080   status->kind = TARGET_WAITKIND_STOPPED;
2081   status->value.sig = TARGET_SIGNAL_TRAP;
2082
2083   switch (stop_reason)
2084     {
2085     case 1:                     /* Breakpoint */
2086       write_pc (read_pc ());    /* PC is always off by 2 for breakpoints */
2087       status->value.sig = TARGET_SIGNAL_TRAP;
2088       break;
2089     case 0:                     /* Single step */
2090       status->value.sig = TARGET_SIGNAL_TRAP;
2091       break;
2092     case 2:                     /* Interrupt */
2093       if (had_sleep)
2094         {
2095           status->value.sig = TARGET_SIGNAL_TRAP;
2096           sub2_from_pc ();
2097         }
2098       else
2099         {
2100           status->value.sig = TARGET_SIGNAL_INT;
2101         }
2102       break;
2103     case 3:
2104       break;
2105     case 4:
2106       printf_unfiltered ("a cycle address error?\n");
2107       status->value.sig = TARGET_SIGNAL_UNKNOWN;
2108       break;
2109     case 5:
2110       status->value.sig = TARGET_SIGNAL_ILL;
2111       break;
2112     case 6:
2113       status->value.sig = TARGET_SIGNAL_SEGV;
2114       break;
2115     case 7:                     /* Anything else (NITEMS + 1) */
2116       printf_unfiltered ("a write protect error?\n");
2117       status->value.sig = TARGET_SIGNAL_UNKNOWN;
2118       break;
2119     default:
2120       /* Get the user's attention - this should never happen. */
2121       abort ();
2122     }
2123
2124   return 0;
2125 }
2126
2127 /* Stop the running program.  */
2128
2129 static void
2130 e7000_stop (void)
2131 {
2132   /* Sending a ^C is supposed to stop the running program.  */
2133   putchar_e7000 (CTRLC);
2134 }
2135
2136 /* Define the target subroutine names. */
2137
2138 struct target_ops e7000_ops;
2139
2140 static void
2141 init_e7000_ops (void)
2142 {
2143   e7000_ops.to_shortname = "e7000";
2144   e7000_ops.to_longname = "Remote Hitachi e7000 target";
2145   e7000_ops.to_doc = "Use a remote Hitachi e7000 ICE connected by a serial line;\n\
2146 or a network connection.\n\
2147 Arguments are the name of the device for the serial line,\n\
2148 the speed to connect at in bits per second.\n\
2149 eg\n\
2150 target e7000 /dev/ttya 9600\n\
2151 target e7000 foobar";
2152   e7000_ops.to_open = e7000_open;
2153   e7000_ops.to_close = e7000_close;
2154   e7000_ops.to_attach = 0;
2155   e7000_ops.to_post_attach = NULL;
2156   e7000_ops.to_require_attach = NULL;
2157   e7000_ops.to_detach = e7000_detach;
2158   e7000_ops.to_require_detach = NULL;
2159   e7000_ops.to_resume = e7000_resume;
2160   e7000_ops.to_wait = e7000_wait;
2161   e7000_ops.to_post_wait = NULL;
2162   e7000_ops.to_fetch_registers = e7000_fetch_register;
2163   e7000_ops.to_store_registers = e7000_store_register;
2164   e7000_ops.to_prepare_to_store = e7000_prepare_to_store;
2165   e7000_ops.to_xfer_memory = e7000_xfer_inferior_memory;
2166   e7000_ops.to_files_info = e7000_files_info;
2167   e7000_ops.to_insert_breakpoint = e7000_insert_breakpoint;
2168   e7000_ops.to_remove_breakpoint = e7000_remove_breakpoint;
2169   e7000_ops.to_terminal_init = 0;
2170   e7000_ops.to_terminal_inferior = 0;
2171   e7000_ops.to_terminal_ours_for_output = 0;
2172   e7000_ops.to_terminal_ours = 0;
2173   e7000_ops.to_terminal_info = 0;
2174   e7000_ops.to_kill = e7000_kill;
2175   e7000_ops.to_load = e7000_load;
2176   e7000_ops.to_lookup_symbol = 0;
2177   e7000_ops.to_create_inferior = e7000_create_inferior;
2178   e7000_ops.to_post_startup_inferior = NULL;
2179   e7000_ops.to_acknowledge_created_inferior = NULL;
2180   e7000_ops.to_clone_and_follow_inferior = NULL;
2181   e7000_ops.to_post_follow_inferior_by_clone = NULL;
2182   e7000_ops.to_insert_fork_catchpoint = NULL;
2183   e7000_ops.to_remove_fork_catchpoint = NULL;
2184   e7000_ops.to_insert_vfork_catchpoint = NULL;
2185   e7000_ops.to_remove_vfork_catchpoint = NULL;
2186   e7000_ops.to_has_forked = NULL;
2187   e7000_ops.to_has_vforked = NULL;
2188   e7000_ops.to_can_follow_vfork_prior_to_exec = NULL;
2189   e7000_ops.to_post_follow_vfork = NULL;
2190   e7000_ops.to_insert_exec_catchpoint = NULL;
2191   e7000_ops.to_remove_exec_catchpoint = NULL;
2192   e7000_ops.to_has_execd = NULL;
2193   e7000_ops.to_reported_exec_events_per_exec_call = NULL;
2194   e7000_ops.to_has_exited = NULL;
2195   e7000_ops.to_mourn_inferior = e7000_mourn_inferior;
2196   e7000_ops.to_can_run = 0;
2197   e7000_ops.to_notice_signals = 0;
2198   e7000_ops.to_thread_alive = 0;
2199   e7000_ops.to_stop = e7000_stop;
2200   e7000_ops.to_pid_to_exec_file = NULL;
2201   e7000_ops.to_core_file_to_sym_file = NULL;
2202   e7000_ops.to_stratum = process_stratum;
2203   e7000_ops.DONT_USE = 0;
2204   e7000_ops.to_has_all_memory = 1;
2205   e7000_ops.to_has_memory = 1;
2206   e7000_ops.to_has_stack = 1;
2207   e7000_ops.to_has_registers = 1;
2208   e7000_ops.to_has_execution = 1;
2209   e7000_ops.to_sections = 0;
2210   e7000_ops.to_sections_end = 0;
2211   e7000_ops.to_magic = OPS_MAGIC;
2212 };
2213
2214 void
2215 _initialize_remote_e7000 (void)
2216 {
2217   init_e7000_ops ();
2218   add_target (&e7000_ops);
2219
2220   add_com ("e7000", class_obscure, e7000_command,
2221            "Send a command to the e7000 monitor.");
2222
2223   add_com ("ftplogin", class_obscure, e7000_login_command,
2224            "Login to machine and change to directory.");
2225
2226   add_com ("ftpload", class_obscure, e7000_ftp_command,
2227            "Fetch and load a file from previously described place.");
2228
2229   add_com ("drain", class_obscure, e7000_drain_command,
2230            "Drain pending e7000 text buffers.");
2231
2232   add_show_from_set (add_set_cmd ("usehardbreakpoints", no_class,
2233                                 var_integer, (char *) &use_hard_breakpoints,
2234         "Set use of hardware breakpoints for all breakpoints.\n", &setlist),
2235                      &showlist);
2236 }