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