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