home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / remote-e7000.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  33.3 KB  |  1,759 lines

  1. /* Remote debugging interface for Hitachi E7000 ICE, for GDB
  2.    Copyright 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support. 
  4.  
  5.    Written by Steve Chamberlain for Cygnus Support.
  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
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23.  
  24. #include "defs.h"
  25. #include "gdbcore.h"
  26. #include "target.h"
  27. #include "wait.h"
  28. #include <varargs.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include "serial.h"
  33.  
  34.  
  35. /* The E7000 is an in-circuit emulator for the Hitachi H8/300-H and
  36. Hitachi-SH processor.  It has serial port and a lan port.  
  37.  
  38. The monitor command set makes it difficult to load large ammounts of
  39. data over the lan without using ftp - so try not to issue load
  40. commands when communicating over ethernet; use the ftpload command.
  41.  
  42. The monitor pauses for a second when dumping srecords to the serial
  43. line too, so we use a slower per byte mechanism but without the
  44. startup overhead.  Even so, it's pretty slow... */
  45.  
  46. int using_tcp; /* nonzero if using the tcp serial driver */
  47. int using_pc; /* nonzero if using the pc isa card */
  48.  
  49. extern struct target_ops e7000_ops;    /* Forward declaration */
  50. #define CTRLC 0x03
  51. #define ENQ  0x05
  52. #define ACK  0x06
  53. #define CTRLZ 0x1a
  54.  
  55. char *ENQSTRING = "\005";
  56.  
  57. /* Nonzero if some routine (as opposed to the user) wants echoing.
  58.    FIXME: Do this reentrantly with an extra parameter.  */
  59. static int echo;
  60.  
  61. int ctrl_c;
  62. static void e7000_close ();
  63. static void e7000_fetch_register ();
  64. static void e7000_store_register ();
  65.  
  66. static int timeout = 5;
  67.  
  68. static void expect PARAMS ((char *));
  69. static void expect_full_prompt PARAMS (());
  70. static void expect_prompt PARAMS (());
  71. static serial_t e7000_desc;
  72.  
  73.  
  74. /* Send data to e7000debug.  Works just like printf. */
  75. #if 0
  76. static void
  77. printf_e7000debug (va_alist)
  78.      va_dcl
  79. {
  80.   va_list args;
  81.   char *pattern;
  82.   char buf[200];
  83.  
  84.   va_start (args);
  85.  
  86.   pattern = va_arg (args, char *);
  87.  
  88.   vsprintf (buf, pattern, args);
  89. #else
  90.  
  91. static void
  92. printf_e7000debug(a,b,c,d,e,f)
  93.   {
  94.     char buf[200];
  95.     sprintf(buf, a,b,c,d,e,f);
  96. #endif
  97. if (!e7000_desc)
  98.   {
  99.     error ("use \"target e7000 ...\" first.");
  100.   }
  101.     if (remote_debug )
  102.       printf("Sending %s\n", buf);
  103.  
  104.   if (SERIAL_WRITE (e7000_desc, buf, strlen (buf)))
  105.     fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
  106.  
  107.   /* And expect to see it echoed, unless using the pc interface */
  108. /*    if (!using_pc)*/
  109.       expect (buf);
  110. }
  111.  
  112. static void
  113. putchar_e7000 (x)
  114. {
  115.   char b[1];
  116.   b[0] = x;
  117.   SERIAL_WRITE (e7000_desc, b, 1);
  118. }
  119.  
  120. static void
  121. write_e7000 (s)
  122.      char *s;
  123. {
  124.   SERIAL_WRITE (e7000_desc, s, strlen (s));
  125. }
  126.  
  127. /* Read a character from the remote system, doing all the fancy timeout
  128.    stuff.  */
  129.  
  130. static int
  131. readchar (timeout)
  132.      int timeout;
  133. {
  134.   int c;
  135.   do
  136.     {
  137.       c = SERIAL_READCHAR (e7000_desc, timeout);
  138.     }
  139.   while (c > 127);
  140.   if (c == SERIAL_TIMEOUT)
  141.     {
  142.       if (timeout == 0)
  143.     return -1;
  144.       echo = 0;
  145.       error ("Timeout reading from remote system.");
  146.     }
  147.   if(remote_debug) 
  148.     {
  149.       putchar (c);
  150.       fflush(stdout);
  151.     }
  152.  
  153.   return normal(c);
  154. }
  155.  
  156. static normal(x) 
  157. {
  158.   if (x == '\n') return '\r';
  159.   return x;
  160. }
  161. char *tl(x)
  162. {
  163.   static char b[8][10];
  164.   static int p;
  165.   p++;
  166.   p &= 7;
  167.   if (x >= ' ') 
  168.     { 
  169.       b[p][0] = x;
  170.       b[p][1] = 0;
  171.     }
  172. else {
  173.   sprintf(b[p],"<%d>", x);
  174. }
  175.   return b[p];
  176. }
  177.  
  178. /* Scan input from the remote system, until STRING is found.  If DISCARD is
  179.    non-zero, then discard non-matching input, else print it out.
  180.    Let the user break out immediately.  */
  181. static void
  182. expect (string)
  183.      char *string;
  184. {
  185.   char *p = string;
  186.   int c;
  187.  
  188.   while (1)
  189.     {
  190.       c = readchar (timeout);
  191.       notice_quit ();
  192.       if (quit_flag == 1) 
  193.     {
  194.       if (ctrl_c) {
  195.         putchar_e7000(CTRLC);
  196.         ctrl_c -- ;
  197.       }
  198.       else 
  199.         {
  200.           quit();
  201.         }
  202.     }
  203.       
  204.       if (c == SERIAL_ERROR)
  205.     {
  206.       error ("Serial communication error");
  207.     }
  208.       if (echo || remote_debug)
  209.     {
  210.       if (c != '\r')
  211.         putchar (c);
  212.       fflush (stdout);
  213.     }
  214.       if (normal(c) == normal(*p++))
  215.     {
  216.       if (*p == '\0')
  217.         {
  218.           return;
  219.         }
  220.     }
  221.       else
  222.     {
  223.       p = string ;
  224.  
  225.       if (normal(c) == normal(string[0]))
  226.         p++;
  227.     }
  228.     }
  229. }
  230.  
  231. /* Keep discarding input until we see the e7000 prompt.
  232.  
  233.    The convention for dealing with the prompt is that you
  234.    o give your command
  235.    o *then* wait for the prompt.
  236.  
  237.    Thus the last thing that a procedure does with the serial line
  238.    will be an expect_prompt().  Exception:  e7000_resume does not
  239.    wait for the prompt, because the terminal is being handed over
  240.    to the inferior.  However, the next thing which happens after that
  241.    is a e7000_wait which does wait for the prompt.
  242.    Note that this includes abnormal exit, e.g. error().  This is
  243.    necessary to prevent getting into states from which we can't
  244.    recover.  */
  245. static void
  246. expect_prompt ()
  247. {
  248.     expect (":");
  249. }
  250. static void
  251. expect_full_prompt ()
  252. {
  253.  
  254.     expect ("\r:");
  255.  
  256. }
  257.  
  258. static int
  259. get_hex_digit (ch)
  260. {
  261.   if (ch >= '0' && ch <= '9')
  262.     return ch - '0';
  263.   else if (ch >= 'A' && ch <= 'F')
  264.     return ch - 'A' + 10;
  265.   else if (ch >= 'a' && ch <= 'f')
  266.     return ch - 'a' + 10;
  267.   return -1;
  268.  
  269. }
  270.  
  271.  
  272.  
  273. static int
  274. get_hex (start)
  275.      int *start;
  276. {
  277.   int value = get_hex_digit (*start);
  278.   int try;
  279.  
  280.   *start = readchar (timeout);
  281.   while ((try = get_hex_digit (*start)) >= 0)
  282.     {
  283.       value <<= 4;
  284.       value += try;
  285.       *start = readchar (timeout);
  286.     }
  287.   return value;
  288. }
  289.  
  290. /* Get N 32-bit words from remote, each preceded by a space,
  291.    and put them in registers starting at REGNO.  */
  292.  
  293. static void
  294. get_hex_regs (n, regno)
  295.      int n;
  296.      int regno;
  297. {
  298.   long val;
  299.   int i;
  300.  
  301.   for (i = 0; i < n; i++)
  302.     {
  303.       int j;
  304.  
  305.       val = 0;
  306.       for (j = 0; j < 8; j++)
  307.     val = (val << 4) + get_hex_digit (j == 0);
  308.       supply_register (regno++, (char *) &val);
  309.     }
  310. }
  311.  
  312. /* This is called not only when we first attach, but also when the
  313.    user types "run" after having attached.  */
  314. static void
  315. e7000_create_inferior (execfile, args, env)
  316.      char *execfile;
  317.      char *args;
  318.      char **env;
  319. {
  320.   int entry_pt;
  321.  
  322.   if (args && *args)
  323.     error ("Can't pass arguments to remote E7000DEBUG process");
  324.  
  325.   if (execfile == 0 || exec_bfd == 0)
  326.     error ("No exec file specified");
  327.  
  328.   entry_pt = (int) bfd_get_start_address (exec_bfd);
  329.  
  330. #ifdef CREATE_INFERIOR_HOOK
  331.   CREATE_INFERIOR_HOOK (0);    /* No process-ID */
  332. #endif
  333.  
  334.   /* The "process" (board) is already stopped awaiting our commands, and
  335.      the program is already downloaded.  We just set its PC and go.  */
  336.  
  337.   clear_proceed_status ();
  338.  
  339.   /* Tell wait_for_inferior that we've started a new process.  */
  340.   init_wait_for_inferior ();
  341.  
  342.   /* Set up the "saved terminal modes" of the inferior
  343.      based on what modes we are starting it with.  */
  344.   target_terminal_init ();
  345.  
  346.   /* Install inferior's terminal modes.  */
  347.   target_terminal_inferior ();
  348.  
  349.   /* insert_step_breakpoint ();  FIXME, do we need this?  */
  350.   proceed ((CORE_ADDR) entry_pt, -1, 0);    /* Let 'er rip... */
  351. }
  352.  
  353. /* Open a connection to a remote debugger.
  354.    NAME is the filename used for communication.  */
  355.  
  356. static int baudrate = 9600;
  357. static char dev_name[100];
  358.  
  359. static char *machine = "";
  360. static char *user = "";
  361. static char *passwd = "";
  362. static char *dir = "";
  363.  
  364. /* Grab the next token and buy some space for it */
  365. static char *
  366. next (ptr)
  367.      char **ptr;
  368. {
  369.   char *p = *ptr;
  370.   char *s;
  371.   char *r;
  372.   int l = 0;
  373.   while (*p && *p == ' ')
  374.     {
  375.       p++;
  376.     }
  377.   s = p;
  378.   while (*p && (*p != ' ' && *p != '\t'))
  379.     {
  380.       l++;
  381.       p++;
  382.     }
  383.   r = xmalloc (l + 1);
  384.   memcpy (r, s, l);
  385.   r[l] = 0;
  386.   *ptr = p;
  387.   return r;
  388. }
  389.  
  390. static
  391. e7000_login (args, from_tty)
  392.      char *args;
  393.      int from_tty;
  394. {
  395.   if (args)
  396.     {
  397.       machine = next (&args);
  398.       user = next (&args);
  399.       passwd = next (&args);
  400.       dir = next (&args);
  401.       if (from_tty)
  402.     {
  403.       printf ("Set info to %s %s %s %s\n", machine, user, passwd, dir);
  404.     }
  405.     }
  406.   else
  407.     {
  408.       error ("Syntax is ftplogin <machine> <user> <passwd> <directory>");
  409.     }
  410. }
  411.  
  412. /* Start an ftp transfer from the E7000 to a host */
  413.  
  414. static
  415. e7000_ftp (args, from_tty)
  416.      char *args;
  417.      int from_tty;
  418. {
  419.   int oldtimeout = timeout;
  420.   timeout = 10;
  421.   printf_e7000debug ("ftp %s\r", machine);
  422.   expect (" Username : ");
  423.   printf_e7000debug ("%s\r", user);
  424.   expect (" Password : ");
  425.   write_e7000 (passwd);
  426.   write_e7000 ("\r");
  427.   expect ("success\r");
  428.   expect ("FTP>");
  429.   printf_e7000debug ("cd %s\r", dir);
  430.   expect ("FTP>");
  431.   printf_e7000debug ("ll 0;s:%s\r", args);
  432.   expect ("FTP>");
  433.   printf_e7000debug ("bye\r");
  434.   expect (":");
  435.   timeout = oldtimeout;
  436. }
  437.  
  438. static void
  439. e7000_open (args, from_tty)
  440.      char *args;
  441.      int from_tty;
  442. {
  443.   int n;
  444.   int loop;
  445.   char junk[100];
  446.   int sync;
  447.   target_preopen (from_tty);
  448.  
  449.   n = 0;
  450.   if (args && strcasecmp (args,"pc") == 0)
  451.     {
  452.       strcpy (dev_name, args);
  453.     }
  454.   else 
  455.     {
  456.     if (args) 
  457.       {
  458.     n = sscanf (args, " %s %d %s", dev_name, &baudrate, junk);
  459.       }
  460.  
  461.     if (n != 1 && n != 2)
  462.       {
  463.     error ("Bad arguments.  Usage:\ttarget e7000 <device> <speed>\n\
  464. or \t\ttarget e7000 <host>[:<port>]\n\
  465. or \t\ttarget e7000 pc\n");
  466.       }
  467.  
  468. #ifndef __GO32__
  469.     if (n == 1 && strchr (dev_name, ':') == 0)
  470.       {
  471.     /* Default to normal telnet port */
  472.     strcat (dev_name, ":23");
  473.       }
  474. #endif
  475.  
  476.   }
  477.  
  478.   push_target (&e7000_ops);
  479.  
  480.   e7000_desc = SERIAL_OPEN (dev_name);
  481.  
  482.   if (!e7000_desc)
  483.     perror_with_name (dev_name);
  484.  
  485.   using_tcp = strcmp (e7000_desc->ops->name, "tcp") == 0;
  486.   using_pc = strcmp (e7000_desc->ops->name, "pc") == 0;
  487.  
  488.   SERIAL_SETBAUDRATE (e7000_desc, baudrate);
  489.   SERIAL_RAW (e7000_desc);
  490.  
  491.   /* Hello?  Are you there?  */
  492.   sync = 0;
  493.   loop =  0;
  494.   putchar_e7000 (CTRLC);
  495.   while (!sync)
  496.     {
  497.       int c;
  498.       int k = 0;
  499.       if (from_tty)
  500.     printf_unfiltered ("[waiting for e7000...]\n");
  501.       write_e7000 ("\r");
  502.       c = SERIAL_READCHAR (e7000_desc, 1);
  503.       while (c != SERIAL_TIMEOUT)
  504.     {
  505.       /* Dont echo cr's */
  506.       if (from_tty && c != '\r')
  507.         {
  508.           putchar (c);
  509.           fflush (stdout);
  510.         }
  511.       if (c == ':')
  512.         {
  513.           sync = 1;
  514.         }
  515.  
  516.       if (loop++ == 20) 
  517.         {
  518.           putchar_e7000 (CTRLC);
  519.           loop = 0;
  520.         }
  521.  
  522.       QUIT ;
  523.  
  524.  
  525.       if (quit_flag)
  526.         {
  527.           putchar_e7000 (CTRLC);
  528.           quit_flag = 0;
  529.         }
  530.       c = SERIAL_READCHAR (e7000_desc, 1);
  531.     }
  532.     }
  533.   printf_e7000debug ("\r");
  534.  
  535.   expect_prompt ();
  536.  
  537.   if (from_tty)
  538.     printf_filtered ("Remote %s connected to %s\n", target_shortname,
  539.              dev_name);
  540.  
  541. #ifdef GDB_TARGET_IS_H8300
  542.   h8300hmode = 1;
  543. #endif
  544. }
  545.  
  546. /* Close out all files and local state before this target loses control. */
  547.  
  548. static void
  549. e7000_close (quitting)
  550.      int quitting;
  551. {
  552.   if (e7000_desc)
  553.     {
  554.       SERIAL_CLOSE (e7000_desc);
  555.       e7000_desc = 0;
  556.     }
  557. }
  558.  
  559. /* Terminate the open connection to the remote debugger.
  560.    Use this when you want to detach and do something else
  561.    with your gdb.  */
  562. static void
  563. e7000_detach (from_tty)
  564.      int from_tty;
  565. {
  566.   pop_target ();        /* calls e7000_close to do the real work */
  567.   if (from_tty)
  568.     printf ("Ending remote %s debugging\n", target_shortname);
  569. }
  570.  
  571. /* Tell the remote machine to resume.  */
  572.  
  573. static void
  574. e7000_resume (pid, step, sig)
  575.      int pid, step, sig;
  576. {
  577.   if (step)
  578.     {
  579.       printf_e7000debug ("S\r");
  580.     }
  581.   else
  582.     {
  583.       printf_e7000debug ("G\r");
  584.     }
  585. }
  586.  
  587. /* Read the remote registers into the block REGS.  
  588.  
  589.    For the H8/300 a register dump looks like:
  590.  
  591.  
  592.  PC=00021A  CCR=80:I*******
  593.  ER0 - ER3  0000000A 0000002E 0000002E 00000000
  594.  ER4 - ER7  00000000 00000000 00000000 00FFEFF6
  595.  000218           MOV.B     R1L,R2L
  596.  STEP NORMAL END or
  597.  BREAK POINT
  598.  */
  599.  
  600. #ifdef GDB_TARGET_IS_H8300
  601. char *want = "PC=%p CCR=%c\n\
  602.  ER0 - ER3  %0 %1 %2 %3\n\
  603.  ER4 - ER7  %4 %5 %6 %7\n";
  604.  
  605. char *want_nopc = "%p CCR=%c\n\
  606.  ER0 - ER3  %0 %1 %2 %3\n\
  607.  ER4 - ER7  %4 %5 %6 %7";
  608.  
  609.  
  610. #endif
  611. #ifdef GDB_TARGET_IS_SH
  612. char *want = "PC=%16 SR=%22\n\
  613. PR=%17 GBR=%18 VBR=%19\n\
  614. MACH=%20 MACL=%21\n\
  615. R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
  616. R8-15 %8 %9 %10 %11 %12 %13 %14 %15\n";
  617.  
  618. char *want_nopc = "%16 SR=%22\n\
  619.  PR=%17 GBR=%18 VBR=%19\n\
  620.  MACH=%20 MACL=%21\n\
  621.  R0-7  %0 %1 %2 %3 %4 %5 %6 %7\n\
  622.  R8-15 %8 %9 %10 %11 %12 %13 %14 %15";
  623.  
  624. #endif
  625.  
  626. static
  627. int
  628. gch ()
  629. {
  630.   int c = readchar (timeout);
  631.   if (remote_debug)
  632.     {
  633.       if (c >= ' ')
  634.     printf ("%c", c);
  635.       else if (c == '\n')
  636.     printf ("\n", c);
  637.  
  638.     }
  639.   return c;
  640. }
  641.  
  642.  
  643. static
  644. unsigned int
  645. gbyte ()
  646. {
  647.   int high = get_hex_digit (gch ());
  648.   int low = get_hex_digit (gch ());
  649.   return (high << 4) + low;
  650. }
  651.  
  652. void
  653. fetch_regs_from_dump (nextchar, want)
  654.      int (*nextchar)();
  655.      char *want;
  656. {
  657.   int regno;
  658.   char buf[MAX_REGISTER_RAW_SIZE];
  659.  
  660.   int  thischar = nextchar();
  661.  
  662.   while (*want)
  663.     {
  664.       switch (*want)
  665.     {
  666.     case '\n':
  667.       /* Skip to end of line and then eat all new line type stuff */
  668.       while (thischar != '\n' && thischar != '\r') 
  669.         {
  670.           thischar = nextchar();
  671.         }
  672.  
  673.       while (thischar == '\n' || thischar == '\r') 
  674.         {
  675.           thischar = nextchar();
  676.         }
  677.       want++;
  678.       break;
  679.  
  680.     case ' ':
  681.       while (thischar == ' ' || thischar == '\t' || thischar == '\r' || thischar == '\n')
  682.         thischar = nextchar();
  683.       want++;
  684.       break;
  685.       
  686.     default:
  687.       if (*want == thischar)
  688.         {
  689.           want++;
  690.           if (*want)
  691.         thischar = nextchar();
  692.           
  693.         }
  694.       else if (thischar == ' ' || thischar == '\n' || thischar == '\r')
  695.         {
  696.           thischar = nextchar();
  697.         }
  698.       else {
  699.         error("out of sync in fetch registers wanted <%s>, got <%c 0x%x>", want, thischar, thischar);
  700.       }
  701.     
  702.       break;
  703.     case '%':
  704.       /* Got a register command */
  705.       want++;
  706.       switch (*want)
  707.         {
  708. #ifdef PC_REGNUM
  709.         case 'p':
  710.           regno = PC_REGNUM;
  711.           want++;
  712.           break;
  713. #endif
  714. #ifdef CCR_REGNUM
  715.         case 'c':
  716.           regno = CCR_REGNUM;
  717.           want++;
  718.           break;
  719. #endif
  720. #ifdef SP_REGNUM
  721.         case 's':
  722.           regno = SP_REGNUM;
  723.           want++;
  724.           break;
  725. #endif
  726. #ifdef FP_REGNUM
  727.         case 'f':
  728.           regno = FP_REGNUM;
  729.           want++;
  730.           break;
  731. #endif
  732.  
  733.  
  734.         default:
  735.           if (isdigit(want[0])) 
  736.         {
  737.           if (isdigit(want[1]))
  738.             {
  739.               regno = (want[0] - '0') * 10 + want[1] - '0';
  740.               want+=2;
  741.             }
  742.           else 
  743.             {
  744.               regno = want[0] - '0';
  745.               want++;
  746.             }
  747.         }
  748.           
  749.           else
  750.         abort();
  751.         }
  752.       store_signed_integer (buf,
  753.                 REGISTER_RAW_SIZE(regno),
  754.                 (LONGEST)get_hex(&thischar, nextchar));
  755.       supply_register (regno, buf);
  756.       break;
  757.     }
  758.     }
  759. }
  760.  
  761. static void
  762. e7000_fetch_registers ()
  763. {
  764.   int regno;
  765.  
  766.   printf_e7000debug ("R\r");
  767.   fetch_regs_from_dump (gch, want);
  768.  
  769.   /* And supply the extra ones the simulator uses */
  770.   for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
  771.     {
  772.       int buf = 0;
  773.       supply_register (regno, (char *) (&buf));
  774.     }
  775. }
  776.  
  777. /* Fetch register REGNO, or all registers if REGNO is -1.
  778.    Returns errno value.  */
  779.  
  780. static
  781. void
  782. e7000_fetch_register (regno)
  783.      int regno;
  784. {
  785.   e7000_fetch_registers ();
  786. }
  787.  
  788. /* Store the remote registers from the contents of the block REGS.  */
  789.  
  790. static void
  791. e7000_store_registers ()
  792. {
  793.   int regno;
  794.  
  795.   for (regno = 0; regno < NUM_REALREGS; regno++)
  796.     e7000_store_register (regno);
  797.  
  798.   registers_changed ();
  799. }
  800.  
  801. /* Store register REGNO, or all if REGNO == 0.
  802.    Return errno value.  */
  803. static void
  804. e7000_store_register (regno)
  805.      int regno;
  806. {
  807.   if (regno == -1)
  808.     {
  809.       e7000_store_registers ();
  810.       return;
  811.     }
  812. #ifdef GDB_TARGET_IS_H8300
  813.   if (regno <= 7)
  814.     {
  815.       printf_e7000debug (".ER%d %x\r", regno,
  816.              read_register (regno));
  817.  
  818.     }
  819.   else if (regno == PC_REGNUM)
  820.     {
  821.       printf_e7000debug (".PC %x\r",
  822.              read_register (regno));
  823.     }
  824.   else if (regno == CCR_REGNUM)
  825.     {
  826.       printf_e7000debug (".CCR %x\r",
  827.              read_register (regno));
  828.     }
  829. #endif
  830.  
  831. #ifdef  GDB_TARGET_IS_SH
  832.   switch (regno)
  833.     {
  834.     default:
  835.       printf_e7000debug (".R%d %x\r", regno,
  836.              read_register (regno));
  837.  
  838.       break;
  839.     case PC_REGNUM:
  840.       printf_e7000debug (".PC %x\r",
  841.              read_register (regno));
  842.       break;
  843.     case SR_REGNUM:
  844.       printf_e7000debug (".SR %x\r",
  845.              read_register (regno));
  846.       break;
  847.  
  848.     case PR_REGNUM:
  849.       printf_e7000debug (".PR %x\r",
  850.              read_register (regno));
  851.       break;
  852.  
  853.     case GBR_REGNUM:
  854.       printf_e7000debug (".GBR %x\r",
  855.              read_register (regno));
  856.       break;
  857.  
  858.     case VBR_REGNUM:
  859.       printf_e7000debug (".VBR %x\r",
  860.              read_register (regno));
  861.       break;
  862.  
  863.     case MACH_REGNUM:
  864.       printf_e7000debug (".MACH %x\r",
  865.              read_register (regno));
  866.       break;
  867.  
  868.     case MACL_REGNUM:
  869.       printf_e7000debug (".MACL %x\r",
  870.              read_register (regno));
  871.       break;
  872.     }
  873.  
  874. #endif
  875.   expect_prompt ();
  876. }
  877.  
  878. /* Get ready to modify the registers array.  On machines which store
  879.    individual registers, this doesn't need to do anything.  On machines
  880.    which store all the registers in one fell swoop, this makes sure
  881.    that registers contains all the registers from the program being
  882.    debugged.  */
  883.  
  884. static void
  885. e7000_prepare_to_store ()
  886. {
  887.   /* Do nothing, since we can store individual regs */
  888. }
  889.  
  890. static void
  891. e7000_files_info ()
  892. {
  893.   printf ("\tAttached to %s at %d baud.\n",
  894.       dev_name, baudrate);
  895. }
  896.  
  897. static
  898. int
  899. stickbyte (where, what)
  900.      char *where;
  901.      unsigned int what;
  902. {
  903.   static CONST char digs[] = "0123456789ABCDEF";
  904.   where[0] = digs[(what >> 4) & 0xf];
  905.   where[1] = digs[(what & 0xf) & 0xf];
  906.   return what;
  907. }
  908.  
  909. /* Write a small ammount of memory */
  910. static int
  911. write_small (memaddr, myaddr, len)
  912.      CORE_ADDR memaddr;
  913.      unsigned char *myaddr;
  914.      int len;
  915. {
  916.   int i;
  917.   for (i = 0; i < len; i++)
  918.     {
  919.       if (((memaddr + i) & 3) == 0
  920.       && (i + 3 < len))
  921.     {
  922.       /* Can be done with a long word */
  923.       printf_e7000debug ("m %x %x%02x%02x%02x;l\r",
  924.                  memaddr + i,
  925.                  myaddr[i],
  926.                  myaddr[i + 1],
  927.                  myaddr[i + 2],
  928.                  myaddr[i + 3]);
  929.       i += 3;
  930.     }
  931.       else
  932.     {
  933.       printf_e7000debug ("m %x %x\r", memaddr + i, myaddr[i]);
  934.     }
  935.     }
  936.   expect_prompt ();
  937.   return len;
  938. }
  939. /* Write a large ammount of memory, this only works with the serial mode enabled.
  940.    Command is sent as
  941.     il ;s:s\r     ->
  942.             <- il ;s:s\r
  943.             <-      ENQ
  944.     ACK        ->
  945.             <- LO s\r
  946.     Srecords...
  947.     ^Z        ->
  948.             <-    ENQ
  949.     ACK        ->  
  950.             <-    :       
  951.  */
  952.  
  953. static int
  954. write_large (memaddr, myaddr, len)
  955.      CORE_ADDR memaddr;
  956.      unsigned char *myaddr;
  957.      int len;
  958. {
  959.   int i;
  960.   int c;
  961. #define maxstride  128
  962.   int stride;
  963.  
  964.   printf_e7000debug ("IL ;S:FK\r");
  965.   expect (ENQSTRING);
  966.   putchar_e7000 (ACK);
  967.   expect ("LO FK\r");
  968.   for (i = 0; i < len; i += stride)
  969.     {
  970.       char compose[maxstride * 2 + 50];
  971.       int address = i + memaddr;
  972.       int j;
  973.       int check_sum;
  974.       int where = 0;
  975.       int alen;
  976.       stride = len - i;
  977.       if (stride > maxstride)
  978.     stride = maxstride;
  979.  
  980.       compose[where++] = 'S';
  981.       check_sum = 0;
  982.       if (address >= 0xffffff)
  983.     {
  984.       alen = 4;
  985.     }
  986.       else if (address >= 0xffff)
  987.     {
  988.       alen = 3;
  989.     }
  990.       else
  991.     alen = 2;
  992.       compose[where++] = alen - 1 + '0'; /* insert type */
  993.       check_sum += stickbyte (compose + where, alen + stride + 1); /* Insert length */
  994.       where += 2;
  995.       while (alen > 0)
  996.     {
  997.       alen--;
  998.       check_sum += stickbyte (compose + where, address >> (8 * (alen)));
  999.       where += 2;
  1000.     }
  1001.  
  1002.       for (j = 0; j < stride; j++)
  1003.     {
  1004.       check_sum += stickbyte (compose + where, myaddr[i + j]);
  1005.       where += 2;
  1006.     }
  1007.  
  1008.       stickbyte (compose + where, ~check_sum);
  1009.  
  1010.       where += 2;
  1011.       compose[where++] = '\r';
  1012.       compose[where++] = '\n';
  1013.       compose[where++] = 0;
  1014.       {
  1015.     char *z;
  1016.     for (z = compose; *z; z++) ;
  1017.     {
  1018.       SERIAL_WRITE (e7000_desc, compose, where);
  1019.       j = SERIAL_READCHAR (e7000_desc, 0);
  1020.       if (j == SERIAL_TIMEOUT)
  1021.         {
  1022.           /* This is ok - nothing there */
  1023.         }
  1024.       else if (j == ENQ)
  1025.         {
  1026.           /* Hmm, it's trying to tell us something */
  1027.           expect (":");
  1028.           error ("Error writing memory");
  1029.         }
  1030.       else
  1031.         {
  1032.           printf ("@%d}@", j);
  1033.           while ((j = SERIAL_READCHAR(e7000_desc,0)) > 0) 
  1034.         {
  1035.           printf ("@{%d}@",j);
  1036.         }
  1037.         }
  1038.     }
  1039.       }
  1040.     }
  1041.   /* Send the trailer record */
  1042.   write_e7000 ("S70500000000FA\r");
  1043.   putchar_e7000 (CTRLZ);
  1044.   expect (ENQSTRING);
  1045.   putchar_e7000 (ACK);
  1046.   expect (":");
  1047.   return len;
  1048. }
  1049.  
  1050. /* Copy LEN bytes of data from debugger memory at MYADDR
  1051.    to inferior's memory at MEMADDR.  Returns length moved.  
  1052.  
  1053.    Can't use the Srecord load over ethernet, so dont use 
  1054.    fast method then.
  1055.  */
  1056. static int
  1057. e7000_write_inferior_memory (memaddr, myaddr, len)
  1058.      CORE_ADDR memaddr;
  1059.      unsigned char *myaddr;
  1060.      int len;
  1061. {
  1062.   if (len < 16 || using_tcp || using_pc)
  1063.     {
  1064.       return write_small (memaddr, myaddr, len);
  1065.     }
  1066.   else
  1067.     {
  1068.       return write_large (memaddr, myaddr, len);
  1069.     }
  1070. }
  1071.  
  1072.  
  1073. /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
  1074.    at debugger address MYADDR.  Returns length moved. 
  1075.  
  1076.  
  1077.   Small transactions we send
  1078.   m <addr>;l
  1079.   and receive
  1080.     00000000 12345678 ?
  1081.  
  1082.  */
  1083.  
  1084. static int
  1085. e7000_read_inferior_memory (memaddr, myaddr, len)
  1086.      CORE_ADDR memaddr;
  1087.      unsigned char *myaddr;
  1088.      int len;
  1089. {
  1090.   int count;
  1091.   int c;
  1092.   int i;
  1093.   /* Starting address of this pass.  */
  1094.  
  1095. /*  printf("READ INF %x %x %d\n", memaddr, myaddr, len);*/
  1096.   if (((memaddr - 1) + len) < memaddr)
  1097.     {
  1098.       errno = EIO;
  1099.       return 0;
  1100.     }
  1101.  
  1102.   printf_e7000debug ("m %x;l\r", memaddr);
  1103.  
  1104.   for (count = 0; count < len; count += 4) 
  1105.     {
  1106.       /* Suck away the address */
  1107.       c = gch();    
  1108.       while (c != ' ')
  1109.     c = gch();    
  1110.       c = gch();
  1111.       if (c == '*') 
  1112.     {            /* Some kind of error */
  1113.       expect_prompt();
  1114.       return -1;
  1115.     }
  1116.       while (c != ' ')
  1117.     c = gch();    
  1118.  
  1119.       /* Now read in the data */
  1120.       for (i = 0; i < 4; i++) 
  1121.     {
  1122.       int b = gbyte();
  1123.       if (count + i < len) {
  1124.         myaddr[count + i] = b;
  1125.       }
  1126.     }
  1127.  
  1128.       /* Skip the trailing ? and send a . to end and a cr for more */
  1129.       gch();    
  1130.       gch();
  1131.       if (count + 4 >= len)
  1132.     printf_e7000debug(".\r");
  1133.       else
  1134.     printf_e7000debug("\r");
  1135.  
  1136.     }
  1137.   expect_prompt();
  1138.   return len;
  1139. }
  1140.  
  1141.  
  1142. #if 0
  1143. /*
  1144.   For large transfers we used to send
  1145.  
  1146.  
  1147.   d <addr> <endaddr>\r
  1148.  
  1149.   and receive
  1150.    <ADDR>              <    D   A   T   A    >               <   ASCII CODE   >
  1151.    000000  5F FD FD FF DF 7F DF FF  01 00 01 00 02 00 08 04  "_..............."
  1152.    000010  FF D7 FF 7F D7 F1 7F FF  00 05 00 00 08 00 40 00  "..............@."
  1153.    000020  7F FD FF F7 7F FF FF F7  00 00 00 00 00 00 00 00  "................"
  1154.  
  1155.   A cost in chars for each transaction of 80 + 5*n-bytes. 
  1156.  
  1157.  
  1158.   Large transactions could be done with the srecord load code, but
  1159.   there is a pause for a second before dumping starts, which slows the
  1160.   average rate down!
  1161. */
  1162.  
  1163. static int
  1164. e7000_read_inferior_memory (memaddr, myaddr, len)
  1165.      CORE_ADDR memaddr;
  1166.      unsigned char *myaddr;
  1167.      int len;
  1168. {
  1169.   int count;
  1170.   int c;
  1171.  
  1172.   /* Starting address of this pass.  */
  1173.  
  1174.   if (((memaddr - 1) + len) < memaddr)
  1175.     {
  1176.       errno = EIO;
  1177.       return 0;
  1178.     }
  1179.  
  1180.   printf_e7000debug ("d %x %x\r", memaddr, memaddr + len - 1);
  1181.  
  1182.   count = 0;
  1183.   c = gch ();
  1184.  
  1185.   /* First skip the command */
  1186.   while (c == '\n')
  1187.     c = gch ();
  1188.  
  1189.   while (c == ' ')
  1190.     c = gch ();
  1191.   if (c == '*')
  1192.     {
  1193.       expect ("\r");
  1194.       return -1;
  1195.     }
  1196.  
  1197.   /* Skip the title line */
  1198.   while (c != '\n')
  1199.     c = gch ();
  1200.   c = gch ();
  1201.   while (count < len)
  1202.     {
  1203.       /* Skip the address */
  1204.       while (c <= ' ')
  1205.     c = gch ();
  1206.  
  1207.       get_hex (&c);
  1208.  
  1209.       /* read in the bytes on the line */
  1210.       while (c != '"' && count < len)
  1211.     {
  1212.       if (c == ' ')
  1213.         c = gch ();
  1214.       else
  1215.         {
  1216.           myaddr[count++] = get_hex (&c);
  1217.         }
  1218.     }
  1219.  
  1220.       while (c != '\n')
  1221.     c = gch ();
  1222.     }
  1223.  
  1224.   while (c != ':')
  1225.     c = gch ();
  1226.  
  1227.   return len;
  1228. }
  1229.  
  1230. static int
  1231. fast_but_for_the_pause_e7000_read_inferior_memory (memaddr, myaddr, len)
  1232.      CORE_ADDR memaddr;
  1233.      char *myaddr;
  1234.      int len;
  1235. {
  1236.   int loop;
  1237.   int c;
  1238.  
  1239.   if (((memaddr - 1) + len) < memaddr)
  1240.     {
  1241.       errno = EIO;
  1242.       return 0;
  1243.     }
  1244.  
  1245.   printf_e7000debug ("is %x@%x:s\r", memaddr, len);
  1246.   gch ();
  1247.   c = gch ();
  1248.   if (c != ENQ)
  1249.     {
  1250.       /* Got an error */
  1251.       error ("Memory read error");
  1252.     }
  1253.   putchar_e7000 (ACK);
  1254.   expect ("SV s");
  1255.   loop = 1;
  1256.   while (loop)
  1257.     {
  1258.       int type;
  1259.       int length;
  1260.       int addr;
  1261.       int i;
  1262.       c = gch ();
  1263.       switch (c)
  1264.     {
  1265.     case ENQ:        /* ENQ, at the end */
  1266.       loop = 0;
  1267.       break;
  1268.     case 'S':
  1269.       /* Start of an Srecord */
  1270.       type = gch ();
  1271.       length = gbyte ();
  1272.       switch (type)
  1273.         {
  1274.         case '7':        /* Termination record, ignore */
  1275.         case '0':
  1276.         case '8':
  1277.         case '9':
  1278.           /* Header record - ignore it */
  1279.           while (length--)
  1280.         {
  1281.           gbyte ();
  1282.         }
  1283.           break;
  1284.         case '1':
  1285.         case '2':
  1286.         case '3':
  1287.           {
  1288.         int alen;
  1289.         alen = type - '0' + 1;
  1290.         addr = 0;
  1291.         while (alen--)
  1292.           {
  1293.             addr = (addr << 8) + gbyte ();
  1294.             length--;
  1295.           }
  1296.  
  1297.         for (i = 0; i < length - 1; i++)
  1298.           {
  1299.             myaddr[i + addr - memaddr] = gbyte ();
  1300.           }
  1301.         gbyte ();    /* Ignore checksum */
  1302.           }
  1303.         }
  1304.     }
  1305.     }
  1306.   putchar_e7000 (ACK);
  1307.   expect ("TOP ADDRESS =");
  1308.   expect ("END ADDRESS =");
  1309.   expect (":");
  1310.  
  1311.   return len;
  1312. }
  1313.  
  1314. #endif
  1315.  
  1316. static int
  1317. e7000_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  1318.      CORE_ADDR memaddr;
  1319.      unsigned char *myaddr;
  1320.      int len;
  1321.      int write;
  1322.      struct target_ops *target;    /* ignored */
  1323. {
  1324.   if (write)
  1325.     {
  1326.       return e7000_write_inferior_memory( memaddr, myaddr, len);
  1327.     }
  1328.   else
  1329.     {
  1330.       return e7000_read_inferior_memory( memaddr, myaddr, len);
  1331.     }
  1332. }
  1333.  
  1334. static void
  1335. e7000_kill (args, from_tty)
  1336.      char *args;
  1337.      int from_tty;
  1338. {
  1339.  
  1340. }
  1341.  
  1342. /* Clean up when a program exits.
  1343.  
  1344.    The program actually lives on in the remote processor's RAM, and may be
  1345.    run again without a download.  Don't leave it full of breakpoint
  1346.    instructions.  */
  1347.  
  1348. static void
  1349. e7000_mourn_inferior ()
  1350. {
  1351.   remove_breakpoints ();
  1352.   unpush_target (&e7000_ops);
  1353.   generic_mourn_inferior ();    /* Do all the proper things now */
  1354. }
  1355.  
  1356. #define MAX_E7000DEBUG_BREAKPOINTS 200
  1357.  
  1358. extern int memory_breakpoint_size;
  1359. static CORE_ADDR breakaddr[MAX_E7000DEBUG_BREAKPOINTS] =
  1360. {0};
  1361.  
  1362. static int
  1363. e7000_insert_breakpoint (addr, shadow)
  1364.      CORE_ADDR addr;
  1365.      unsigned char *shadow;
  1366. {
  1367.   int i;
  1368.   static char nop[2] = NOP;
  1369.  
  1370.   for (i = 0; i <= MAX_E7000DEBUG_BREAKPOINTS; i++)
  1371.     if (breakaddr[i] == 0)
  1372.       {
  1373.     breakaddr[i] = addr;
  1374.     /* Save old contents, and insert a nop in the space */
  1375.     e7000_read_inferior_memory (addr, shadow, 2);
  1376.     e7000_write_inferior_memory (addr, nop, 2);
  1377.     printf_e7000debug ("B %x\r", addr);
  1378.     expect_prompt ();
  1379.     return 0;
  1380.       }
  1381.  
  1382.   error("Too many breakpoints ( > %d) for the E7000\n", MAX_E7000DEBUG_BREAKPOINTS);
  1383.   return 1;
  1384. }
  1385.  
  1386. static int
  1387. e7000_remove_breakpoint (addr, shadow)
  1388.      CORE_ADDR addr;
  1389.      unsigned char *shadow;
  1390. {
  1391.   int i;
  1392.  
  1393.   for (i = 0; i < MAX_E7000DEBUG_BREAKPOINTS; i++)
  1394.     if (breakaddr[i] == addr)
  1395.       {
  1396.     breakaddr[i] = 0;
  1397.     printf_e7000debug ("B - %x\r", addr);
  1398.     expect_prompt ();
  1399.     /* Replace the insn under the break */
  1400.     e7000_write_inferior_memory (addr, shadow, 2);
  1401.     return 0;
  1402.       }
  1403.  
  1404.   fprintf (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
  1405.   return 1;
  1406. }
  1407.  
  1408.  
  1409. /* Put a command string, in args, out to STDBUG.  Output from STDBUG is placed
  1410.    on the users terminal until the prompt is seen. */
  1411.  
  1412. static void
  1413. e7000_command (args, fromtty)
  1414.      char *args;
  1415.      int fromtty;
  1416. {
  1417.   echo = 0;
  1418.   if (!e7000_desc)
  1419.     error ("e7000 target not open.");
  1420.   if (!args)
  1421.     {
  1422.       printf_e7000debug ("\r");
  1423.     }
  1424.   else
  1425.     {
  1426.       printf_e7000debug ("%s\r", args);
  1427.     }
  1428.   echo++;
  1429.   ctrl_c = 2;
  1430.   expect_full_prompt ();
  1431.   echo--;
  1432.   ctrl_c = 0;
  1433.   printf_unfiltered ("\n");
  1434. }
  1435.  
  1436. static void
  1437. e7000_load (args, fromtty)
  1438.      char *args;
  1439.      int fromtty;
  1440. {
  1441.   gr_load_image (args, fromtty);
  1442. }
  1443.  
  1444. static void
  1445. e7000_drain (args, fromtty)
  1446.      char *args;
  1447.      int fromtty;
  1448.  
  1449. {
  1450.   int c;
  1451.   printf_e7000debug("end\r");
  1452.   putchar_e7000 (CTRLC);
  1453.   while ((c = SERIAL_READCHAR (e7000_desc, 1) != SERIAL_TIMEOUT))
  1454.     {
  1455.       if(quit_flag)
  1456.     {
  1457.       putchar_e7000(CTRLC);
  1458.       quit_flag = 0;
  1459.     }
  1460.       if (c > ' ' && c < 127)
  1461.     printf ("%c", c & 0xff);
  1462.       else
  1463.     printf ("<%x>", c & 0xff);
  1464.     }
  1465. }
  1466.  
  1467. #define NITEMS 3
  1468. static int
  1469. why_stop ()
  1470. {
  1471.   static  char *strings[NITEMS] = 
  1472.     {
  1473.       "STEP NORMAL",
  1474.       "BREAK POINT",
  1475.       "BREAK KEY",
  1476.     };
  1477.   char *p[NITEMS];
  1478.   int c;
  1479.   p[0] = strings[0];
  1480.   p[1] = strings[1];
  1481.   p[2] = strings[2];
  1482.   
  1483.   c = gch();
  1484.   while (1)
  1485.     {
  1486.       int i;
  1487.       for (i = 0; i < NITEMS; i++)
  1488.     {
  1489.       if (c == *(p[i])) 
  1490.         {
  1491.           p[i]++;
  1492.           if (*(p[i]) == 0) 
  1493.         { 
  1494.           /* found one of the choices */
  1495.           return i;
  1496.         }
  1497.         }
  1498.       else {
  1499.         p[i] = strings[i];
  1500.       }
  1501.     }
  1502.  
  1503.       c = gch();
  1504.     }
  1505. }
  1506. /* Suck characters, if a string match, then return the strings index
  1507.    otherwise echo them */
  1508. int
  1509. expect_n ( strings)
  1510. char **strings;
  1511. {
  1512.   char *(ptr[10]);
  1513.   int n; 
  1514.   int c;
  1515.   char saveaway[100];
  1516.   char *buffer = saveaway;
  1517.   /* Count number of expect strings  */
  1518.  
  1519.   for (n =0; strings[n]; n++) 
  1520.     {
  1521.       ptr[n] = strings[n];
  1522.     }
  1523.  
  1524.   while (1) {
  1525.     int i;
  1526.     int gotone = 0;
  1527.  
  1528.     c = SERIAL_READCHAR (e7000_desc, 1);
  1529.     if (c == SERIAL_TIMEOUT) {
  1530.       printf_unfiltered ("[waiting for e7000...]\n");
  1531.     }
  1532. #ifdef __GO32__
  1533.     if (kbhit())
  1534.       {
  1535.     int k = getkey();
  1536.     if (k == 1)
  1537.       quit_flag = 1;
  1538.       }
  1539. #endif
  1540.  
  1541.     if (quit_flag)
  1542.       {
  1543.     putchar_e7000 (CTRLC);    /* interrupt the running program */
  1544.     quit_flag = 0;
  1545.       }
  1546.  
  1547.     for (i = 0; i < n; i++)
  1548.       {
  1549.     if (c == ptr[i][0]) 
  1550.       {
  1551.         ptr[i]++;
  1552.         if (ptr[i][0] == 0)
  1553.           {
  1554.         /* Gone all the way */
  1555.         return i;
  1556.           }
  1557.         gotone = 1;
  1558.       }
  1559.     else 
  1560.       {
  1561.         ptr[i] = strings[i];
  1562.       }
  1563.       }
  1564.  
  1565.     
  1566.     if (gotone)
  1567.       {
  1568.     /* Save it up incase we find that there was no match */
  1569.     *buffer ++ = c;
  1570.       }
  1571.     else
  1572.       {
  1573.     if (buffer != saveaway) 
  1574.       {
  1575.         *buffer++ = 0;
  1576.         printf(buffer);
  1577.         buffer = saveaway;
  1578.       }
  1579.     if (c != SERIAL_TIMEOUT) {
  1580.       putchar (c);
  1581.       fflush(stdout);
  1582.     }
  1583.       }
  1584.   }
  1585. }
  1586.  
  1587. /* We subtract two from the pc here rather than use DECR_PC_AFTER_BREAK
  1588.    since the e7000 doesn't always add two to the pc, and the simulators never do. */
  1589.  
  1590. static void
  1591. sub2_from_pc()
  1592. {
  1593.   char buf[4];
  1594.   store_signed_integer (buf,
  1595.             REGISTER_RAW_SIZE(PC_REGNUM), 
  1596.             read_register (PC_REGNUM) -2);
  1597.   supply_register (PC_REGNUM, buf);
  1598.   printf_e7000debug (".PC %x\r", read_register (PC_REGNUM));
  1599. }
  1600. #define WAS_SLEEP 0
  1601. #define WAS_INT 1
  1602. #define WAS_RUNNING 2
  1603. #define WAS_OTHER 3
  1604. static char *estrings[] = { "** SLEEP", "BREAK !", "** PC", "PC", 0};
  1605.  
  1606. /* Wait until the remote machine stops, then return,
  1607.    storing status in STATUS just as `wait' would.  */
  1608.  
  1609. static int
  1610. e7000_wait (pid, status)
  1611.      int pid;
  1612.      struct target_waitstatus *status;
  1613. {
  1614.   int c;
  1615.   int reset_pc;
  1616.   int regno;
  1617.   int running_count = 0;
  1618.   int had_sleep = 0;
  1619.   int loop = 1;
  1620.   char *reg;
  1621.   int time = 0;
  1622.   /* Then echo chars until PC= string seen */
  1623.   gch ();            /* Drop cr */
  1624.   gch ();            /* and space */
  1625.   while (loop)
  1626.     {
  1627.       switch (expect_n(estrings))
  1628.     {     
  1629.     case WAS_OTHER:
  1630.       /* how did this happen ? */
  1631.       loop =0;
  1632.       break;
  1633.     case WAS_SLEEP:
  1634.       had_sleep = 1;
  1635.       putchar_e7000 (CTRLC);
  1636.       loop = 0;
  1637.       break;
  1638.     case WAS_INT:
  1639.       loop = 0;
  1640.       break;
  1641.     case WAS_RUNNING:
  1642.       running_count++;
  1643.       if (running_count == 20)
  1644.         {
  1645.           printf_unfiltered ("[running...]\n");
  1646.           running_count = 0;
  1647.         }
  1648.       break;
  1649.     }
  1650.     }
  1651.   /* Skip till the PC=*/
  1652.   expect("=");
  1653.   fetch_regs_from_dump (gch, want_nopc);
  1654.  
  1655.   /* And supply the extra ones the simulator uses */
  1656.   for (regno = NUM_REALREGS; regno < NUM_REGS; regno++)
  1657.     {
  1658.       int buf = 0;
  1659.       supply_register (regno, (char *) &buf);
  1660.     }
  1661.  
  1662.   reset_pc = why_stop ();
  1663.   expect_full_prompt ();
  1664.  
  1665.   status->kind = TARGET_WAITKIND_STOPPED;
  1666.   status->value.sig = TARGET_SIGNAL_TRAP;
  1667.  
  1668.   switch (reset_pc)
  1669.     {
  1670.     case 1:            /* Breakpoint */
  1671.       status->value.sig = TARGET_SIGNAL_TRAP;      
  1672.       break;
  1673.     case 0:
  1674.       /* Single step */
  1675.       status->value.sig = TARGET_SIGNAL_TRAP;      
  1676.       break;
  1677.     case 2:
  1678.       /* Interrupt */
  1679.       if (had_sleep)
  1680.     {
  1681.       status->value.sig = TARGET_SIGNAL_TRAP;      
  1682.       sub2_from_pc();
  1683.     }
  1684.       else
  1685.     {
  1686.       status->value.sig = TARGET_SIGNAL_INT;      
  1687.     }
  1688.       break;
  1689.     }
  1690.   return 0;
  1691. }
  1692.  
  1693. /* Define the target subroutine names */
  1694.  
  1695. struct target_ops e7000_ops =
  1696. {
  1697.   "e7000",
  1698.   "Remote Hitachi e7000 target",
  1699.   "Use a remote Hitachi e7000 ICE connected by a serial line,\n\
  1700. or a network connection.\n\
  1701. Arguments are the name of the device for the serial line,\n\
  1702. the speed to connect at in bits per second.\n\
  1703. eg\n\
  1704. target e7000 /dev/ttya 9600\n\
  1705. target e7000 foobar",
  1706.   e7000_open,
  1707.   e7000_close,
  1708.   0,
  1709.   e7000_detach,
  1710.   e7000_resume,
  1711.   e7000_wait,
  1712.   e7000_fetch_register,
  1713.   e7000_store_register,
  1714.   e7000_prepare_to_store,
  1715.   e7000_xfer_inferior_memory,
  1716.   e7000_files_info,
  1717. 0,0,/*  e7000_insert_breakpoint,
  1718.   e7000_remove_breakpoint,    /* Breakpoints */
  1719.   0,
  1720.   0,
  1721.   0,
  1722.   0,
  1723.   0,                /* Terminal handling */
  1724.   e7000_kill,
  1725.   e7000_load,            /* load */
  1726.   0,                /* lookup_symbol */
  1727.   e7000_create_inferior,
  1728.   e7000_mourn_inferior,
  1729.   0,                /* can_run */
  1730.   0,                /* notice_signals */
  1731.   process_stratum,
  1732.   0,                /* next */
  1733.   1,
  1734.   1,
  1735.   1,
  1736.   1,
  1737.   1,                /* all mem, mem, stack, regs, exec */
  1738.   0,
  1739.   0,                /* Section pointers */
  1740.   OPS_MAGIC,            /* Always the last thing */
  1741. };
  1742.  
  1743. void
  1744. _initialize_remote_e7000 ()
  1745. {
  1746.   add_target (&e7000_ops);
  1747.   add_com ("e7000 <command>", class_obscure, e7000_command,
  1748.        "Send a command to the e7000 monitor.");
  1749.  
  1750.   add_com ("ftplogin <machine> <name> <passwd> <dir>", class_obscure, e7000_login,
  1751.        "Login to machine and change to directory.");
  1752.  
  1753.   add_com ("ftpload <file>", class_obscure, e7000_ftp,
  1754.        "Fetch and load a file from previously described place.");
  1755.  
  1756.   add_com ("drain", class_obscure, e7000_drain,
  1757.        "Drain pending e7000 text buffers.");
  1758. }
  1759.