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