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