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