OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [remote-e7000.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.