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