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