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