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