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