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