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