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