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