* gdbtk.tcl (create_command_window): If command window's buffer
[deliverable/binutils-gdb.git] / gdb / remote-e7000.c
CommitLineData
edd01519 1/* Remote debugging interface for Hitachi E7000 ICE, for GDB
b5eccf74 2 Copyright 1993, 1994, 1996 Free Software Foundation, Inc.
edd01519
SC
3 Contributed by Cygnus Support.
4
5 Written by Steve Chamberlain for Cygnus Support.
863099f4
SC
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
0fe1522a 21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
863099f4 22
2b576293
C
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... */
863099f4
SC
33
34#include "defs.h"
35#include "gdbcore.h"
2b576293 36#include "inferior.h"
863099f4
SC
37#include "target.h"
38#include "wait.h"
2b576293
C
39#include "value.h"
40#include "command.h"
863099f4 41#include <signal.h>
2b576293 42#include "gdb_string.h"
863099f4
SC
43#include <sys/types.h>
44#include "serial.h"
2b576293 45#include "remote-utils.h"
0fe1522a 46#include "symfile.h"
72158e71
SS
47#include <time.h>
48
2b576293
C
49#if 0
50#define HARD_BREAKPOINTS
51#define BC_BREAKPOINTS 0
52#endif
863099f4 53
863099f4
SC
54#define CTRLC 0x03
55#define ENQ 0x05
56#define ACK 0x06
57#define CTRLZ 0x1a
58
2b576293 59extern void notice_quit PARAMS ((void));
863099f4 60
72158e71
SS
61extern void report_transfer_performance PARAMS ((unsigned long,
62 time_t, time_t));
63
2b576293 64/* Local function declarations. */
863099f4 65
2b576293
C
66static void e7000_close PARAMS ((int));
67
68static void e7000_fetch_register PARAMS ((int));
69
70static void e7000_store_register PARAMS ((int));
71
72static void e7000_command PARAMS ((char *, int));
73
74static void e7000_login_command PARAMS ((char *, int));
75
76static void e7000_ftp_command PARAMS ((char *, int));
77
78static void e7000_drain_command PARAMS ((char *, int));
863099f4
SC
79
80static void expect PARAMS ((char *));
2b576293
C
81
82static void expect_full_prompt PARAMS ((void));
83
84static void expect_prompt PARAMS ((void));
85
86/* Variables. */
87
863099f4
SC
88static serial_t e7000_desc;
89
2b576293 90/* Nonzero if using the tcp serial driver. */
863099f4 91
2b576293 92static int using_tcp;
863099f4 93
2b576293 94/* Nonzero if using the pc isa card. */
863099f4 95
2b576293 96static int using_pc;
863099f4 97
2b576293
C
98extern struct target_ops e7000_ops; /* Forward declaration */
99
100char *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
105static int echo;
106
107static int ctrl_c;
108
109static int timeout = 5;
110
111/* Send data to e7000debug. */
edd01519
SC
112
113static void
2b576293
C
114puts_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
863099f4
SC
123 if (SERIAL_WRITE (e7000_desc, buf, strlen (buf)))
124 fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
125
2b576293
C
126 /* And expect to see it echoed, unless using the pc interface */
127#if 0
128 if (!using_pc)
129#endif
130 expect (buf);
863099f4
SC
131}
132
133static void
134putchar_e7000 (x)
2b576293 135 int x;
863099f4
SC
136{
137 char b[1];
2b576293 138
863099f4
SC
139 b[0] = x;
140 SERIAL_WRITE (e7000_desc, b, 1);
141}
142
143static void
144write_e7000 (s)
145 char *s;
146{
147 SERIAL_WRITE (e7000_desc, s, strlen (s));
148}
149
2b576293
C
150static int
151normal (x)
152 int x;
153{
154 if (x == '\n')
155 return '\r';
156 return x;
157}
158
863099f4
SC
159/* Read a character from the remote system, doing all the fancy timeout
160 stuff. */
161
162static int
163readchar (timeout)
164 int timeout;
165{
166 int c;
2b576293 167
863099f4
SC
168 do
169 {
170 c = SERIAL_READCHAR (e7000_desc, timeout);
171 }
172 while (c > 127);
2b576293 173
863099f4
SC
174 if (c == SERIAL_TIMEOUT)
175 {
176 if (timeout == 0)
2b576293
C
177 return -1;
178 echo = 0;
863099f4
SC
179 error ("Timeout reading from remote system.");
180 }
2b576293
C
181 if (remote_debug)
182 {
183 putchar (c);
184 fflush (stdout);
185 }
186
187 return normal (c);
863099f4
SC
188}
189
2b576293
C
190#if 0
191char *
192tl (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. */
edd01519 216
863099f4
SC
217static void
218expect (string)
219 char *string;
220{
221 char *p = string;
222 int c;
2b576293 223 int nl = 0;
863099f4 224
863099f4 225 while (1)
863099f4
SC
226 {
227 c = readchar (timeout);
edd01519
SC
228 notice_quit ();
229 if (quit_flag == 1)
230 {
2b576293
C
231 if (ctrl_c)
232 {
233 putchar_e7000(CTRLC);
234 --ctrl_c;
235 }
edd01519
SC
236 else
237 {
2b576293 238 quit ();
edd01519
SC
239 }
240 }
241
863099f4
SC
242 if (c == SERIAL_ERROR)
243 {
244 error ("Serial communication error");
245 }
2b576293 246 if (echo || remote_debug)
863099f4 247 {
2b576293
C
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 }
863099f4
SC
259 fflush (stdout);
260 }
2b576293 261 if (normal (c) == normal (*p++))
863099f4
SC
262 {
263 if (*p == '\0')
2b576293 264 return;
863099f4
SC
265 }
266 else
267 {
268 p = string;
2b576293
C
269
270 if (normal (c) == normal (string[0]))
271 p++;
863099f4
SC
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
2b576293
C
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
863099f4
SC
290static void
291expect_prompt ()
292{
863099f4
SC
293 expect (":");
294}
2b576293 295
863099f4
SC
296static void
297expect_full_prompt ()
298{
2b576293 299 expect ("\r:");
863099f4
SC
300}
301
302static int
2b576293
C
303convert_hex_digit (ch)
304 int ch;
863099f4
SC
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;
863099f4
SC
313}
314
863099f4
SC
315static int
316get_hex (start)
317 int *start;
318{
2b576293 319 int value = convert_hex_digit (*start);
863099f4
SC
320 int try;
321
322 *start = readchar (timeout);
2b576293 323 while ((try = convert_hex_digit (*start)) >= 0)
863099f4
SC
324 {
325 value <<= 4;
326 value += try;
327 *start = readchar (timeout);
328 }
329 return value;
330}
331
2b576293
C
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. */
863099f4
SC
335
336static void
337get_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}
2b576293 354#endif
863099f4
SC
355
356/* This is called not only when we first attach, but also when the
357 user types "run" after having attached. */
2b576293 358
863099f4
SC
359static void
360e7000_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
2b576293
C
398/* Open a connection to a remote debugger. NAME is the filename used
399 for communication. */
863099f4
SC
400
401static int baudrate = 9600;
402static char dev_name[100];
403
404static char *machine = "";
405static char *user = "";
406static char *passwd = "";
407static char *dir = "";
408
409/* Grab the next token and buy some space for it */
2b576293 410
863099f4
SC
411static char *
412next (ptr)
413 char **ptr;
414{
415 char *p = *ptr;
416 char *s;
417 char *r;
418 int l = 0;
2b576293 419
863099f4 420 while (*p && *p == ' ')
2b576293 421 p++;
863099f4
SC
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
2b576293
C
435static void
436e7000_login_command (args, from_tty)
863099f4
SC
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
2b576293
C
459static void
460e7000_ftp_command (args, from_tty)
863099f4
SC
461 char *args;
462 int from_tty;
463{
2b576293
C
464 /* FIXME: arbitrary limit on machine names and such. */
465 char buf[200];
466
863099f4
SC
467 int oldtimeout = timeout;
468 timeout = 10;
2b576293
C
469
470 sprintf (buf, "ftp %s\r", machine);
471 puts_e7000debug (buf);
863099f4 472 expect (" Username : ");
2b576293
C
473 sprintf (buf, "%s\r", user);
474 puts_e7000debug (buf);
863099f4
SC
475 expect (" Password : ");
476 write_e7000 (passwd);
477 write_e7000 ("\r");
478 expect ("success\r");
479 expect ("FTP>");
2b576293
C
480 sprintf (buf, "cd %s\r", dir);
481 puts_e7000debug (buf);
863099f4 482 expect ("FTP>");
2b576293
C
483 sprintf (buf, "ll 0;s:%s\r", args);
484 puts_e7000debug (buf);
863099f4 485 expect ("FTP>");
2b576293 486 puts_e7000debug ("bye\r");
863099f4 487 expect (":");
863099f4
SC
488 timeout = oldtimeout;
489}
490
491static void
492e7000_open (args, from_tty)
493 char *args;
494 int from_tty;
495{
496 int n;
2b576293 497 int loop;
863099f4
SC
498 char junk[100];
499 int sync;
500 target_preopen (from_tty);
501
2b576293
C
502 n = 0;
503 if (args && strcasecmp (args, "pc") == 0)
504 {
505 strcpy (dev_name, args);
506 }
507 else
863099f4 508 {
2b576293
C
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\
517or \t\ttarget e7000 <host>[:<port>]\n\
518or \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
863099f4
SC
528 }
529
530 push_target (&e7000_ops);
863099f4 531
2b576293 532 e7000_desc = SERIAL_OPEN (dev_name);
863099f4
SC
533
534 if (!e7000_desc)
535 perror_with_name (dev_name);
536
537 using_tcp = strcmp (e7000_desc->ops->name, "tcp") == 0;
2b576293 538 using_pc = strcmp (e7000_desc->ops->name, "pc") == 0;
863099f4
SC
539
540 SERIAL_SETBAUDRATE (e7000_desc, baudrate);
541 SERIAL_RAW (e7000_desc);
542
543 /* Hello? Are you there? */
544 sync = 0;
2b576293 545 loop = 0;
edd01519 546 putchar_e7000 (CTRLC);
863099f4
SC
547 while (!sync)
548 {
549 int c;
2b576293 550
863099f4
SC
551 if (from_tty)
552 printf_unfiltered ("[waiting for e7000...]\n");
2b576293
C
553
554 write_e7000 ("\r");
555 c = SERIAL_READCHAR (e7000_desc, 1);
863099f4
SC
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 == ':')
2b576293
C
565 sync = 1;
566
567 if (loop++ == 20)
863099f4 568 {
2b576293
C
569 putchar_e7000 (CTRLC);
570 loop = 0;
863099f4 571 }
2b576293
C
572
573 QUIT ;
574
575
863099f4
SC
576 if (quit_flag)
577 {
578 putchar_e7000 (CTRLC);
579 quit_flag = 0;
580 }
2b576293 581 c = SERIAL_READCHAR (e7000_desc, 1);
863099f4
SC
582 }
583 }
2b576293
C
584 puts_e7000debug ("\r");
585
863099f4
SC
586 expect_prompt ();
587
72158e71
SS
588 puts_e7000debug ("b -\r");
589
590 expect_prompt ();
591
863099f4 592 if (from_tty)
72158e71 593 printf_filtered ("Remote target %s connected to %s\n", target_shortname,
863099f4
SC
594 dev_name);
595
edd01519
SC
596#ifdef GDB_TARGET_IS_H8300
597 h8300hmode = 1;
598#endif
863099f4
SC
599}
600
601/* Close out all files and local state before this target loses control. */
602
603static void
604e7000_close (quitting)
605 int quitting;
606{
607 if (e7000_desc)
608 {
609 SERIAL_CLOSE (e7000_desc);
610 e7000_desc = 0;
611 }
612}
613
2b576293
C
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
863099f4
SC
617static void
618e7000_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
628static void
629e7000_resume (pid, step, sig)
630 int pid, step, sig;
631{
632 if (step)
2b576293 633 puts_e7000debug ("S\r");
863099f4 634 else
2b576293 635 puts_e7000debug ("G\r");
863099f4
SC
636}
637
638/* Read the remote registers into the block REGS.
639
edd01519 640 For the H8/300 a register dump looks like:
863099f4 641
2b576293
C
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 */
863099f4
SC
649
650#ifdef GDB_TARGET_IS_H8300
2b576293
C
651
652char *want = "PC=%p CCR=%c\n\
863099f4 653 ER0 - ER3 %0 %1 %2 %3\n\
edd01519
SC
654 ER4 - ER7 %4 %5 %6 %7\n";
655
656char *want_nopc = "%p CCR=%c\n\
657 ER0 - ER3 %0 %1 %2 %3\n\
658 ER4 - ER7 %4 %5 %6 %7";
659
863099f4 660#endif
2b576293 661
863099f4 662#ifdef GDB_TARGET_IS_SH
2b576293
C
663
664char *want = "PC=%16 SR=%22\n\
665PR=%17 GBR=%18 VBR=%19\n\
666MACH=%20 MACL=%21\n\
667R0-7 %0 %1 %2 %3 %4 %5 %6 %7\n\
668R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n";
863099f4
SC
669
670char *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
863099f4
SC
676#endif
677
2b576293 678static int
863099f4
SC
679gch ()
680{
681 int c = readchar (timeout);
2b576293
C
682
683 if (remote_debug)
863099f4
SC
684 {
685 if (c >= ' ')
686 printf ("%c", c);
687 else if (c == '\n')
2b576293 688 printf ("\n");
863099f4
SC
689 }
690 return c;
691}
692
2b576293 693static unsigned int
863099f4
SC
694gbyte ()
695{
2b576293
C
696 int high = convert_hex_digit (gch ());
697 int low = convert_hex_digit (gch ());
698
863099f4
SC
699 return (high << 4) + low;
700}
701
702void
703fetch_regs_from_dump (nextchar, want)
704 int (*nextchar)();
705 char *want;
706{
707 int regno;
708 char buf[MAX_REGISTER_RAW_SIZE];
709
2b576293
C
710 int thischar = nextchar ();
711
863099f4
SC
712 while (*want)
713 {
714 switch (*want)
715 {
716 case '\n':
2b576293
C
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 ();
863099f4
SC
722 want++;
723 break;
724
725 case ' ':
2b576293
C
726 while (thischar == ' '
727 || thischar == '\t'
728 || thischar == '\r'
729 || thischar == '\n')
730 thischar = nextchar ();
863099f4
SC
731 want++;
732 break;
733
734 default:
735 if (*want == thischar)
736 {
737 want++;
738 if (*want)
2b576293 739 thischar = nextchar ();
863099f4
SC
740
741 }
2b576293 742 else if (thischar == ' ' || thischar == '\n' || thischar == '\r')
863099f4 743 {
2b576293 744 thischar = nextchar ();
863099f4
SC
745 }
746 else {
2b576293
C
747 error ("out of sync in fetch registers wanted <%s>, got <%c 0x%x>",
748 want, thischar, thischar);
863099f4
SC
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
863099f4 782 default:
2b576293 783 if (isdigit (want[0]))
863099f4 784 {
2b576293 785 if (isdigit (want[1]))
863099f4
SC
786 {
787 regno = (want[0] - '0') * 10 + want[1] - '0';
2b576293 788 want += 2;
863099f4
SC
789 }
790 else
791 {
792 regno = want[0] - '0';
793 want++;
794 }
795 }
796
797 else
2b576293 798 abort ();
863099f4
SC
799 }
800 store_signed_integer (buf,
801 REGISTER_RAW_SIZE(regno),
2b576293 802 (LONGEST) get_hex (&thischar, nextchar));
863099f4
SC
803 supply_register (regno, buf);
804 break;
805 }
806 }
807}
808
809static void
810e7000_fetch_registers ()
811{
812 int regno;
813
2b576293 814 puts_e7000debug ("R\r");
863099f4
SC
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;
2b576293 821
863099f4
SC
822 supply_register (regno, (char *) (&buf));
823 }
824}
825
2b576293
C
826/* Fetch register REGNO, or all registers if REGNO is -1. Returns
827 errno value. */
863099f4 828
2b576293 829static void
863099f4
SC
830e7000_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
838static void
839e7000_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
2b576293
C
849/* Store register REGNO, or all if REGNO == 0. Return errno value. */
850
863099f4
SC
851static void
852e7000_store_register (regno)
853 int regno;
854{
2b576293
C
855 char buf[200];
856
863099f4
SC
857 if (regno == -1)
858 {
859 e7000_store_registers ();
860 return;
861 }
2b576293 862
863099f4
SC
863#ifdef GDB_TARGET_IS_H8300
864 if (regno <= 7)
865 {
2b576293
C
866 sprintf (buf, ".ER%d %x\r", regno, read_register (regno));
867 puts_e7000debug (buf);
863099f4
SC
868 }
869 else if (regno == PC_REGNUM)
870 {
2b576293
C
871 sprintf (buf, ".PC %x\r", read_register (regno));
872 puts_e7000debug (buf);
863099f4
SC
873 }
874 else if (regno == CCR_REGNUM)
875 {
2b576293
C
876 sprintf (buf, ".CCR %x\r", read_register (regno));
877 puts_e7000debug (buf);
863099f4 878 }
2b576293 879#endif /* GDB_TARGET_IS_H8300 */
863099f4
SC
880
881#ifdef GDB_TARGET_IS_SH
882 switch (regno)
883 {
884 default:
2b576293
C
885 sprintf (buf, ".R%d %x\r", regno, read_register (regno));
886 puts_e7000debug (buf);
863099f4 887 break;
2b576293 888
863099f4 889 case PC_REGNUM:
2b576293
C
890 sprintf (buf, ".PC %x\r", read_register (regno));
891 puts_e7000debug (buf);
863099f4 892 break;
2b576293 893
863099f4 894 case SR_REGNUM:
2b576293
C
895 sprintf (buf, ".SR %x\r", read_register (regno));
896 puts_e7000debug (buf);
863099f4
SC
897 break;
898
899 case PR_REGNUM:
2b576293
C
900 sprintf (buf, ".PR %x\r", read_register (regno));
901 puts_e7000debug (buf);
863099f4
SC
902 break;
903
904 case GBR_REGNUM:
2b576293
C
905 sprintf (buf, ".GBR %x\r", read_register (regno));
906 puts_e7000debug (buf);
863099f4
SC
907 break;
908
909 case VBR_REGNUM:
2b576293
C
910 sprintf (buf, ".VBR %x\r", read_register (regno));
911 puts_e7000debug (buf);
863099f4
SC
912 break;
913
914 case MACH_REGNUM:
2b576293
C
915 sprintf (buf, ".MACH %x\r", read_register (regno));
916 puts_e7000debug (buf);
863099f4
SC
917 break;
918
919 case MACL_REGNUM:
2b576293
C
920 sprintf (buf, ".MACL %x\r", read_register (regno));
921 puts_e7000debug (buf);
863099f4
SC
922 break;
923 }
924
2b576293
C
925#endif /* GDB_TARGET_IS_SH */
926
863099f4
SC
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
936static void
937e7000_prepare_to_store ()
938{
939 /* Do nothing, since we can store individual regs */
940}
941
942static void
943e7000_files_info ()
944{
2b576293 945 printf ("\tAttached to %s at %d baud.\n", dev_name, baudrate);
863099f4
SC
946}
947
2b576293 948static int
863099f4
SC
949stickbyte (where, what)
950 char *where;
951 unsigned int what;
952{
953 static CONST char digs[] = "0123456789ABCDEF";
2b576293 954
863099f4
SC
955 where[0] = digs[(what >> 4) & 0xf];
956 where[1] = digs[(what & 0xf) & 0xf];
2b576293 957
863099f4
SC
958 return what;
959}
960
2b576293
C
961/* Write a small ammount of memory. */
962
863099f4
SC
963static int
964write_small (memaddr, myaddr, len)
965 CORE_ADDR memaddr;
966 unsigned char *myaddr;
967 int len;
968{
969 int i;
2b576293
C
970 char buf[200];
971
863099f4
SC
972 for (i = 0; i < len; i++)
973 {
2b576293 974 if (((memaddr + i) & 3) == 0 && (i + 3 < len))
863099f4
SC
975 {
976 /* Can be done with a long word */
2b576293
C
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);
863099f4
SC
981 i += 3;
982 }
983 else
984 {
2b576293
C
985 sprintf (buf, "m %x %x\r", memaddr + i, myaddr[i]);
986 puts_e7000debug (buf);
863099f4
SC
987 }
988 }
2b576293 989
863099f4 990 expect_prompt ();
2b576293 991
863099f4
SC
992 return len;
993}
2b576293
C
994
995/* Write a large ammount of memory, this only works with the serial
996 mode enabled. Command is sent as
997
863099f4
SC
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 <- :
2b576293 1008 */
863099f4
SC
1009
1010static int
1011write_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
2b576293 1020 puts_e7000debug ("IL ;S:FK\r");
863099f4
SC
1021 expect (ENQSTRING);
1022 putchar_e7000 (ACK);
edd01519 1023 expect ("LO FK\r");
2b576293 1024
863099f4
SC
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;
2b576293 1033
863099f4
SC
1034 stride = len - i;
1035 if (stride > maxstride)
1036 stride = maxstride;
1037
1038 compose[where++] = 'S';
1039 check_sum = 0;
1040 if (address >= 0xffffff)
2b576293 1041 alen = 4;
863099f4 1042 else if (address >= 0xffff)
2b576293 1043 alen = 3;
863099f4
SC
1044 else
1045 alen = 2;
2b576293
C
1046 /* Insert type. */
1047 compose[where++] = alen - 1 + '0';
1048 /* Insert length. */
1049 check_sum += stickbyte (compose + where, alen + stride + 1);
863099f4
SC
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 }
863099f4 1063 stickbyte (compose + where, ~check_sum);
863099f4
SC
1064 where += 2;
1065 compose[where++] = '\r';
edd01519
SC
1066 compose[where++] = '\n';
1067 compose[where++] = 0;
2b576293
C
1068
1069 SERIAL_WRITE (e7000_desc, compose, where);
1070 j = SERIAL_READCHAR (e7000_desc, 0);
1071 if (j == SERIAL_TIMEOUT)
863099f4 1072 {
2b576293
C
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)
edd01519 1085 {
2b576293 1086 printf ("@{%d}@",j);
edd01519 1087 }
863099f4 1088 }
863099f4 1089 }
2b576293 1090
863099f4
SC
1091 /* Send the trailer record */
1092 write_e7000 ("S70500000000FA\r");
1093 putchar_e7000 (CTRLZ);
1094 expect (ENQSTRING);
1095 putchar_e7000 (ACK);
1096 expect (":");
2b576293 1097
863099f4
SC
1098 return len;
1099}
1100
2b576293
C
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. */
863099f4 1106
863099f4
SC
1107static int
1108e7000_write_inferior_memory (memaddr, myaddr, len)
1109 CORE_ADDR memaddr;
1110 unsigned char *myaddr;
1111 int len;
1112{
2b576293
C
1113 if (len < 16 || using_tcp || using_pc)
1114 return write_small (memaddr, myaddr, len);
863099f4 1115 else
2b576293 1116 return write_large (memaddr, myaddr, len);
863099f4
SC
1117}
1118
863099f4
SC
1119/* Read LEN bytes from inferior memory at MEMADDR. Put the result
1120 at debugger address MYADDR. Returns length moved.
1121
863099f4
SC
1122 Small transactions we send
1123 m <addr>;l
1124 and receive
1125 00000000 12345678 ?
863099f4
SC
1126 */
1127
1128static int
1129e7000_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;
2b576293 1137 char buf[200];
863099f4
SC
1138 /* Starting address of this pass. */
1139
2b576293 1140/* printf("READ INF %x %x %d\n", memaddr, myaddr, len);*/
863099f4
SC
1141 if (((memaddr - 1) + len) < memaddr)
1142 {
1143 errno = EIO;
1144 return 0;
1145 }
1146
2b576293
C
1147 sprintf (buf, "m %x;l\r", memaddr);
1148 puts_e7000debug (buf);
863099f4
SC
1149
1150 for (count = 0; count < len; count += 4)
1151 {
1152 /* Suck away the address */
2b576293 1153 c = gch ();
863099f4 1154 while (c != ' ')
2b576293
C
1155 c = gch ();
1156 c = gch ();
863099f4
SC
1157 if (c == '*')
1158 { /* Some kind of error */
1159 expect_prompt();
1160 return -1;
1161 }
1162 while (c != ' ')
2b576293 1163 c = gch ();
863099f4
SC
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 */
2b576293
C
1175 gch ();
1176 gch ();
863099f4 1177 if (count + 4 >= len)
2b576293 1178 puts_e7000debug(".\r");
863099f4 1179 else
2b576293
C
1180 puts_e7000debug("\r");
1181
863099f4
SC
1182 }
1183 expect_prompt();
2b576293 1184 return len;
863099f4
SC
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
863099f4
SC
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
1208static int
1209e7000_read_inferior_memory (memaddr, myaddr, len)
1210 CORE_ADDR memaddr;
1211 unsigned char *myaddr;
1212 int len;
1213{
1214 int count;
1215 int c;
2b576293 1216 char buf[200];
863099f4
SC
1217
1218 /* Starting address of this pass. */
1219
1220 if (((memaddr - 1) + len) < memaddr)
1221 {
1222 errno = EIO;
1223 return 0;
1224 }
1225
2b576293
C
1226 sprintf (buf, "d %x %x\r", memaddr, memaddr + len - 1);
1227 puts_e7000debug (buf);
863099f4
SC
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
1277static int
1278fast_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;
2b576293 1285 char buf[200];
863099f4
SC
1286
1287 if (((memaddr - 1) + len) < memaddr)
1288 {
1289 errno = EIO;
1290 return 0;
1291 }
1292
2b576293
C
1293 sprintf (buf, "is %x@%x:s\r", memaddr, len);
1294 puts_e7000debug (buf);
863099f4
SC
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;
2b576293 1311
863099f4
SC
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;
2b576293 1339
863099f4
SC
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++)
2b576293
C
1349 myaddr[i + addr - memaddr] = gbyte ();
1350
863099f4
SC
1351 gbyte (); /* Ignore checksum */
1352 }
1353 }
1354 }
1355 }
2b576293 1356
863099f4
SC
1357 putchar_e7000 (ACK);
1358 expect ("TOP ADDRESS =");
1359 expect ("END ADDRESS =");
1360 expect (":");
1361
1362 return len;
1363}
1364
1365#endif
1366
1367static int
1368e7000_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)
2b576293 1376 return e7000_write_inferior_memory( memaddr, myaddr, len);
863099f4 1377 else
2b576293 1378 return e7000_read_inferior_memory( memaddr, myaddr, len);
863099f4
SC
1379}
1380
1381static void
1382e7000_kill (args, from_tty)
1383 char *args;
1384 int from_tty;
1385{
863099f4
SC
1386}
1387
b5eccf74
SG
1388static void
1389e7000_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;
72158e71
SS
1403 time_t start_time, end_time; /* Start and end times of download */
1404 unsigned long data_count; /* Number of bytes transferred to memory */
b5eccf74
SG
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
72158e71
SS
1456 start_time = time (NULL);
1457 data_count = 0;
1458
b5eccf74
SG
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
72158e71
SS
1482 data_count += section_size;
1483
b5eccf74
SG
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))
72158e71
SS
1507 fprintf_unfiltered (gdb_stderr,
1508 "e7000_load: SERIAL_WRITE failed: %s\n",
1509 safe_strerror(errno));
b5eccf74
SG
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
72158e71
SS
1530 end_time = time (NULL);
1531
b5eccf74
SG
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
72158e71
SS
1557 report_transfer_performance (data_count, start_time, end_time);
1558
b5eccf74
SG
1559 do_cleanups (old_chain);
1560}
1561
863099f4
SC
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
1568static void
1569e7000_mourn_inferior ()
1570{
1571 remove_breakpoints ();
1572 unpush_target (&e7000_ops);
1573 generic_mourn_inferior (); /* Do all the proper things now */
1574}
1575
2b576293
C
1576#ifdef HARD_BREAKPOINTS
1577#define MAX_E7000DEBUG_BREAKPOINTS (BC_BREAKPOINTS ? 5 : 200)
1578#else
863099f4 1579#define MAX_E7000DEBUG_BREAKPOINTS 200
2b576293 1580#endif
863099f4
SC
1581
1582extern int memory_breakpoint_size;
2b576293
C
1583
1584static CORE_ADDR breakaddr[MAX_E7000DEBUG_BREAKPOINTS] = {0};
863099f4
SC
1585
1586static int
1587e7000_insert_breakpoint (addr, shadow)
1588 CORE_ADDR addr;
1589 unsigned char *shadow;
1590{
1591 int i;
2b576293 1592 char buf[200];
edd01519 1593 static char nop[2] = NOP;
863099f4
SC
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 */
2b576293
C
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
0fe1522a 1612#if 0
863099f4
SC
1613 e7000_read_inferior_memory (addr, shadow, 2);
1614 e7000_write_inferior_memory (addr, nop, 2);
0fe1522a 1615#endif
2b576293
C
1616
1617 sprintf (buf, "B %x\r", addr);
1618 puts_e7000debug (buf);
1619#endif
863099f4
SC
1620 expect_prompt ();
1621 return 0;
1622 }
1623
2b576293
C
1624 error ("Too many breakpoints ( > %d) for the E7000\n",
1625 MAX_E7000DEBUG_BREAKPOINTS);
863099f4
SC
1626 return 1;
1627}
1628
1629static int
1630e7000_remove_breakpoint (addr, shadow)
1631 CORE_ADDR addr;
1632 unsigned char *shadow;
1633{
1634 int i;
2b576293 1635 char buf[200];
863099f4
SC
1636
1637 for (i = 0; i < MAX_E7000DEBUG_BREAKPOINTS; i++)
1638 if (breakaddr[i] == addr)
1639 {
1640 breakaddr[i] = 0;
2b576293
C
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 }
863099f4 1652 expect_prompt ();
2b576293
C
1653#else
1654 sprintf (buf, "B - %x\r", addr);
1655 puts_e7000debug (buf);
1656 expect_prompt ();
1657
0fe1522a 1658#if 0
863099f4
SC
1659 /* Replace the insn under the break */
1660 e7000_write_inferior_memory (addr, shadow, 2);
0fe1522a 1661#endif
2b576293
C
1662#endif
1663
863099f4
SC
1664 return 0;
1665 }
1666
2b576293 1667 warning ("Can't find breakpoint associated with 0x%x\n", addr);
863099f4
SC
1668 return 1;
1669}
1670
2b576293
C
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. */
863099f4
SC
1673
1674static void
1675e7000_command (args, fromtty)
1676 char *args;
1677 int fromtty;
1678{
2b576293
C
1679 /* FIXME: arbitrary limit on length of args. */
1680 char buf[200];
1681
1682 echo = 0;
863099f4
SC
1683
1684 if (!e7000_desc)
1685 error ("e7000 target not open.");
1686 if (!args)
1687 {
2b576293 1688 puts_e7000debug ("\r");
863099f4
SC
1689 }
1690 else
1691 {
2b576293
C
1692 sprintf (buf, "%s\r", args);
1693 puts_e7000debug (buf);
863099f4 1694 }
2b576293 1695
edd01519
SC
1696 echo++;
1697 ctrl_c = 2;
863099f4 1698 expect_full_prompt ();
edd01519
SC
1699 echo--;
1700 ctrl_c = 0;
863099f4 1701 printf_unfiltered ("\n");
2b576293
C
1702
1703 /* Who knows what the command did... */
1704 registers_changed ();
863099f4
SC
1705}
1706
863099f4
SC
1707
1708static void
2b576293 1709e7000_drain_command (args, fromtty)
863099f4
SC
1710 char *args;
1711 int fromtty;
1712
1713{
1714 int c;
2b576293
C
1715
1716 puts_e7000debug("end\r");
edd01519 1717 putchar_e7000 (CTRLC);
2b576293 1718
edd01519 1719 while ((c = SERIAL_READCHAR (e7000_desc, 1) != SERIAL_TIMEOUT))
863099f4 1720 {
2b576293 1721 if (quit_flag)
edd01519
SC
1722 {
1723 putchar_e7000(CTRLC);
1724 quit_flag = 0;
1725 }
863099f4
SC
1726 if (c > ' ' && c < 127)
1727 printf ("%c", c & 0xff);
1728 else
1729 printf ("<%x>", c & 0xff);
1730 }
1731}
1732
2b576293 1733#define NITEMS 7
edd01519 1734
edd01519
SC
1735static int
1736why_stop ()
1737{
2b576293
C
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 };
edd01519
SC
1747 char *p[NITEMS];
1748 int c;
2b576293
C
1749 int i;
1750
1751 for (i = 0; i < NITEMS; ++i)
1752 p[i] = strings[i];
edd01519 1753
2b576293 1754 c = gch ();
edd01519
SC
1755 while (1)
1756 {
edd01519
SC
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 }
2b576293 1768 else
edd01519 1769 p[i] = strings[i];
edd01519
SC
1770 }
1771
2b576293 1772 c = gch ();
edd01519
SC
1773 }
1774}
2b576293 1775
edd01519 1776/* Suck characters, if a string match, then return the strings index
2b576293
C
1777 otherwise echo them. */
1778
edd01519 1779int
2b576293 1780expect_n (strings)
edd01519
SC
1781char **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
2b576293 1790 for (n = 0; strings[n]; n++)
edd01519
SC
1791 {
1792 ptr[n] = strings[n];
1793 }
1794
2b576293
C
1795 while (1)
1796 {
1797 int i;
1798 int gotone = 0;
edd01519 1799
2b576293
C
1800 c = SERIAL_READCHAR (e7000_desc, 1);
1801 if (c == SERIAL_TIMEOUT)
1802 {
1803 printf_unfiltered ("[waiting for e7000...]\n");
1804 }
edd01519 1805#ifdef __GO32__
2b576293
C
1806 if (kbhit ())
1807 {
1808 int k = getkey();
edd01519 1809
2b576293
C
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 }
edd01519 1819
2b576293
C
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 }
edd01519 1837
2b576293
C
1838 if (gotone)
1839 {
1840 /* Save it up incase we find that there was no match */
1841 *buffer ++ = c;
edd01519 1842 }
2b576293
C
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 }
863099f4
SC
1858}
1859
2b576293
C
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. */
edd01519
SC
1863
1864static void
2b576293 1865sub2_from_pc ()
edd01519
SC
1866{
1867 char buf[4];
2b576293
C
1868 char buf2[200];
1869
edd01519
SC
1870 store_signed_integer (buf,
1871 REGISTER_RAW_SIZE(PC_REGNUM),
1872 read_register (PC_REGNUM) -2);
1873 supply_register (PC_REGNUM, buf);
2b576293
C
1874 sprintf (buf2, ".PC %x\r", read_register (PC_REGNUM));
1875 puts_e7000debug (buf2);
edd01519 1876}
2b576293 1877
edd01519
SC
1878#define WAS_SLEEP 0
1879#define WAS_INT 1
1880#define WAS_RUNNING 2
1881#define WAS_OTHER 3
edd01519 1882
2b576293
C
1883static 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. */
863099f4
SC
1893
1894static int
1895e7000_wait (pid, status)
1896 int pid;
2b576293 1897 struct target_waitstatus *status;
863099f4 1898{
2b576293 1899 int stop_reason;
863099f4 1900 int regno;
edd01519
SC
1901 int running_count = 0;
1902 int had_sleep = 0;
863099f4 1903 int loop = 1;
2b576293 1904
863099f4
SC
1905 /* Then echo chars until PC= string seen */
1906 gch (); /* Drop cr */
1907 gch (); /* and space */
2b576293 1908
863099f4
SC
1909 while (loop)
1910 {
2b576293 1911 switch (expect_n (estrings))
edd01519
SC
1912 {
1913 case WAS_OTHER:
1914 /* how did this happen ? */
2b576293 1915 loop = 0;
edd01519
SC
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)
863099f4 1928 {
edd01519
SC
1929 printf_unfiltered ("[running...]\n");
1930 running_count = 0;
863099f4 1931 }
edd01519 1932 break;
2b576293
C
1933 default:
1934 /* error? */
1935 break;
863099f4
SC
1936 }
1937 }
2b576293
C
1938
1939 /* Skip till the PC= */
1940 expect ("=");
863099f4
SC
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
2b576293 1950 stop_reason = why_stop ();
863099f4 1951 expect_full_prompt ();
863099f4 1952
2b576293
C
1953 status->kind = TARGET_WAITKIND_STOPPED;
1954 status->value.sig = TARGET_SIGNAL_TRAP;
1955
1956 switch (stop_reason)
edd01519
SC
1957 {
1958 case 1: /* Breakpoint */
b5eccf74 1959 write_pc (read_pc ()); /* PC is always off by 2 for breakpoints */
2b576293 1960 status->value.sig = TARGET_SIGNAL_TRAP;
edd01519 1961 break;
2b576293
C
1962 case 0: /* Single step */
1963 status->value.sig = TARGET_SIGNAL_TRAP;
edd01519 1964 break;
2b576293 1965 case 2: /* Interrupt */
edd01519
SC
1966 if (had_sleep)
1967 {
2b576293
C
1968 status->value.sig = TARGET_SIGNAL_TRAP;
1969 sub2_from_pc ();
edd01519
SC
1970 }
1971 else
1972 {
2b576293 1973 status->value.sig = TARGET_SIGNAL_INT;
edd01519
SC
1974 }
1975 break;
2b576293
C
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 ();
edd01519 1995 }
2b576293 1996
863099f4
SC
1997 return 0;
1998}
1999
2b576293 2000/* Define the target subroutine names. */
863099f4
SC
2001
2002struct 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\
2007or a network connection.\n\
2008Arguments are the name of the device for the serial line,\n\
2009the speed to connect at in bits per second.\n\
2010eg\n\
2011target e7000 /dev/ttya 9600\n\
2012target e7000 foobar",
2b576293
C
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 */
2b576293
C
2024 e7000_insert_breakpoint, /* to_insert_breakpoint */
2025 e7000_remove_breakpoint, /* to_remove_breakpoint */
2b576293
C
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 */
b5eccf74 2032 e7000_load, /* to_load */
2b576293
C
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 */
863099f4
SC
2049 OPS_MAGIC, /* Always the last thing */
2050};
2051
2052void
2053_initialize_remote_e7000 ()
2054{
2055 add_target (&e7000_ops);
2b576293 2056
863099f4
SC
2057 add_com ("e7000 <command>", class_obscure, e7000_command,
2058 "Send a command to the e7000 monitor.");
2059
2b576293 2060 add_com ("ftplogin <machine> <name> <passwd> <dir>", class_obscure, e7000_login_command,
863099f4
SC
2061 "Login to machine and change to directory.");
2062
2b576293 2063 add_com ("ftpload <file>", class_obscure, e7000_ftp_command,
863099f4
SC
2064 "Fetch and load a file from previously described place.");
2065
2b576293 2066 add_com ("drain", class_obscure, e7000_drain_command,
863099f4 2067 "Drain pending e7000 text buffers.");
863099f4 2068}
This page took 0.226113 seconds and 4 git commands to generate.