home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / nindy-share / nindy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-01  |  30.7 KB  |  1,135 lines

  1. /* This file is part of GDB.
  2.  
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. /* This started out life as code shared between the nindy monitor and
  18.    GDB.  For various reasons, this is no longer true.  Eventually, it
  19.    probably should be merged into remote-nindy.c.  */
  20.  
  21. /******************************************************************************
  22.  *
  23.  *             NINDY INTERFACE ROUTINES
  24.  *
  25.  * The caller of these routines should be aware that:
  26.  *
  27.  * (1) ninConnect() should be called to open communications with the
  28.  *     remote NINDY board before any of the other routines are invoked.
  29.  *
  30.  * (2) almost all interactions are driven by the host: nindy sends information
  31.  *     in response to host commands.
  32.  *
  33.  * (3) the lone exception to (2) is the single character DLE (^P, 0x10).
  34.  *     Receipt of a DLE from NINDY indicates that the application program
  35.  *     running under NINDY has stopped execution and that NINDY is now
  36.  *     available to talk to the host (all other communication received after
  37.  *     the application has been started should be presumed to come from the
  38.  *     application and should be passed on by the host to stdout).
  39.  *
  40.  * (4) the reason the application program stopped can be determined with the
  41.  *     ninStopWhy() function.  There are three classes of stop reasons:
  42.  *
  43.  *    (a) the application has terminated execution.
  44.  *        The host should take appropriate action.
  45.  *
  46.  *    (b) the application had a fault or trace event.
  47.  *        The host should take appropriate action.
  48.  *
  49.  *    (c) the application wishes to make a service request (srq) of the host;
  50.  *        e.g., to open/close a file, read/write a file, etc.  The ninSrq()
  51.  *        function should be called to determine the nature of the request
  52.  *        and process it.
  53.  */
  54.  
  55. #include <stdio.h>
  56. #include "defs.h"
  57. #include "serial.h"
  58. #include <varargs.h>
  59.  
  60. #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
  61. #define HAVE_SGTTY
  62. #endif
  63.  
  64. #ifdef HAVE_SGTTY
  65. #include <sys/ioctl.h>
  66. #endif
  67.  
  68. #include <sys/types.h>    /* Needed by file.h on Sys V */
  69. #include <sys/file.h>
  70. #include <signal.h>
  71. #include <sys/stat.h>
  72.  
  73. #if 0
  74. #include "ttycntl.h"
  75. #endif
  76. #include "block_io.h"
  77. #include "wait.h"
  78. #include "env.h"
  79.  
  80. #define DLE    0x10    /* ^P */
  81. #define XON    0x11    /* ^Q */
  82. #define XOFF    0x13    /* ^S */
  83. #define ESC    0x1b
  84.  
  85. #define TIMEOUT        -1
  86.  
  87. int quiet = 0;    /* 1 => stifle unnecessary messages */
  88. serial_t nindy_serial;
  89.  
  90. static int old_nindy = 0; /* 1 => use old (hex) communication protocol */
  91. static ninStrGet();
  92.  
  93.         /****************************
  94.          *                          *
  95.          *  MISCELLANEOUS UTILTIES  *
  96.          *                          *
  97.          ****************************/
  98.  
  99. /******************************************************************************
  100.  * say:
  101.  *    This is a printf that takes at most two arguments (in addition to the
  102.  *    format string) and that outputs nothing if verbose output has been
  103.  *    suppressed.
  104.  *****************************************************************************/
  105.  
  106. static void
  107. say (va_alist)
  108.      va_dcl
  109. {
  110.   va_list args;
  111.   char *fmt;
  112.  
  113.   va_start (args);
  114.   fmt = va_arg (args, char *);
  115.   if (!quiet)
  116.     {
  117.       vfprintf_unfiltered (gdb_stdout, fmt, args);
  118.       gdb_flush (gdb_stdout);
  119.     }
  120.   va_end (args);
  121. }
  122.  
  123. /******************************************************************************
  124.  * exists:
  125.  *    Creates a full pathname by concatenating up to three name components
  126.  *    onto a specified base name; optionally looks up the base name as a
  127.  *    runtime environment variable;  and checks to see if the file or
  128.  *    directory specified by the pathname actually exists.
  129.  *
  130.  *    Returns:  the full pathname if it exists, NULL otherwise.
  131.  *        (returned pathname is in malloc'd memory and must be freed
  132.  *        by caller).
  133.  *****************************************************************************/
  134. static char *
  135. exists( base, c1, c2, c3, env )
  136.     char *base;        /* Base directory of path */
  137.     char *c1, *c2, *c3;    /* Components (subdirectories and/or file name) to be
  138.              *    appended onto the base directory name.  One or
  139.              *    more may be omitted by passing NULL pointers.
  140.              */
  141.     int env;        /* If 1, '*base' is the name of an environment variable
  142.              *    to be examined for the base directory name;
  143.              *    otherwise, '*base' is the actual name of the
  144.              *    base directory.
  145.              */
  146. {
  147.     struct stat buf;/* For call to 'stat' -- never examined */
  148.     char *path;    /* Pointer to full pathname (malloc'd memory) */
  149.     int len;    /* Length of full pathname (incl. terminator) */
  150.     extern char *getenv();
  151.  
  152.  
  153.     if ( env ){
  154.         base = getenv( base );
  155.         if ( base == NULL ){
  156.             return NULL;
  157.         }
  158.     }
  159.  
  160.     len = strlen(base) + 4;
  161.             /* +4 for terminator and "/" before each component */
  162.     if ( c1 != NULL ){
  163.         len += strlen(c1);
  164.     }
  165.     if ( c2 != NULL ){
  166.         len += strlen(c2);
  167.     }
  168.     if ( c3 != NULL ){
  169.         len += strlen(c3);
  170.     }
  171.  
  172.     path = xmalloc (len);
  173.  
  174.     strcpy( path, base );
  175.     if ( c1 != NULL ){
  176.         strcat( path, "/" );
  177.         strcat( path, c1 );
  178.         if ( c2 != NULL ){
  179.             strcat( path, "/" );
  180.             strcat( path, c2 );
  181.             if ( c3 != NULL ){
  182.                 strcat( path, "/" );
  183.                 strcat( path, c3 );
  184.             }
  185.         }
  186.     }
  187.  
  188.     if ( stat(path,&buf) != 0 ){
  189.         free( path );
  190.         path = NULL;
  191.     }
  192.     return path;
  193. }
  194.  
  195.         /*****************************
  196.          *                           *
  197.          *  LOW-LEVEL COMMUNICATION  *
  198.          *                           *
  199.          *****************************/
  200.  
  201. /* Read *exactly* N characters from the NINDY tty, and put them in
  202.    *BUF.  Translate escape sequences into single characters, counting
  203.    each such sequence as 1 character.
  204.  
  205.    An escape sequence consists of ESC and a following character.  The
  206.    ESC is discarded and the other character gets bit 0x40 cleared --
  207.    thus ESC P == ^P, ESC S == ^S, ESC [ == ESC, etc.
  208.  
  209.    Return 1 if successful, 0 if more than TIMEOUT seconds pass without
  210.    any input.  */
  211.  
  212. static int
  213. rdnin (buf,n,timeout)
  214.     unsigned char * buf;    /* Where to place characters read    */
  215.     int n;            /* Number of characters to read        */
  216.     int timeout;        /* Timeout, in seconds            */
  217. {
  218.   int escape_seen;    /* 1 => last character of a read was an ESC */
  219.   int c;
  220.  
  221.   escape_seen = 0;
  222.   while (n)
  223.     {
  224.       c = SERIAL_READCHAR (nindy_serial, timeout);
  225.       switch (c)
  226.     {
  227.     case SERIAL_ERROR:
  228.     case SERIAL_TIMEOUT:
  229.     case SERIAL_EOF:
  230.       return 0;
  231.  
  232.     case ESC:
  233.       escape_seen = 1;
  234.       break;
  235.  
  236.     default:
  237.       if (escape_seen)
  238.         {
  239.           escape_seen = 0;
  240.           c &= ~0x40;
  241.         }
  242.       *buf++ = c;
  243.       --n;
  244.       break;
  245.     }
  246.     }
  247.   return 1;
  248. }
  249.  
  250.  
  251. /******************************************************************************
  252.  * getpkt:
  253.  *    Read a packet from a remote NINDY, with error checking, into the
  254.  *    indicated buffer.
  255.  *
  256.  *    Return packet status byte on success, TIMEOUT on failure.
  257.  ******************************************************************************/
  258. static
  259. int
  260. getpkt(buf)
  261.      unsigned char *buf;
  262. {
  263.     int i;
  264.     unsigned char hdr[3];    /* Packet header:
  265.                  *    hdr[0] = low byte of message length
  266.                  *    hdr[1] = high byte of message length
  267.                  *    hdr[2] = message status
  268.                  */
  269.     int cnt;        /* Message length (status byte + data)    */
  270.     unsigned char cs_calc;    /* Checksum calculated            */
  271.     unsigned char cs_recv;    /* Checksum received            */
  272.     static char errfmt[] =
  273.             "Bad checksum (recv=0x%02x; calc=0x%02x); retrying\r\n";
  274.  
  275.     while (1){
  276.         if ( !rdnin(hdr,3,5) ){
  277.             return TIMEOUT;
  278.         }
  279.         cnt = (hdr[1]<<8) + hdr[0] - 1;
  280.                     /* -1 for status byte (already read) */
  281.  
  282.         /* Caller's buffer may only be big enough for message body,
  283.          * without status byte and checksum, so make sure to read
  284.          * checksum into a separate buffer.
  285.          */
  286.         if ( !rdnin(buf,cnt,5) || !rdnin(&cs_recv,1,5) ){
  287.             return TIMEOUT;
  288.         }
  289.  
  290.         /* Calculate checksum
  291.          */
  292.         cs_calc = hdr[0] + hdr[1] + hdr[2];
  293.         for ( i = 0; i < cnt; i++ ){
  294.             cs_calc += buf[i];
  295.         }
  296.         if ( cs_calc == cs_recv ){
  297.             SERIAL_WRITE (nindy_serial, "+", 1);
  298.             return hdr[2];
  299.         }
  300.     
  301.         /* Bad checksum: report, send NAK, and re-receive
  302.          */
  303.         fprintf(stderr, errfmt, cs_recv, cs_calc );
  304.         SERIAL_WRITE (nindy_serial, "-", 1);
  305.     }
  306. }
  307.  
  308.  
  309. /******************************************************************************
  310.  * putpkt:
  311.  *    Send a packet to NINDY, checksumming it and converting special
  312.  *    characters to escape sequences.
  313.  ******************************************************************************/
  314.  
  315. /* This macro puts the character 'c' into the buffer pointed at by 'p',
  316.  * and increments the pointer.  If 'c' is one of the 4 special characters
  317.  * in the transmission protocol, it is converted into a 2-character
  318.  * escape sequence.
  319.  */
  320. #define PUTBUF(c,p)                        \
  321.     if ( c == DLE || c == ESC || c == XON || c == XOFF ){    \
  322.         *p++ = ESC;                    \
  323.         *p++ = c | 0x40;                \
  324.     } else {                        \
  325.         *p++ = c;                    \
  326.     }
  327.  
  328. static
  329. putpkt( msg, len )
  330.     unsigned char *msg;    /* Command to be sent, without lead ^P (\020) or checksum */
  331.     int len;    /* Number of bytes in message            */
  332. {
  333.     static char *buf = NULL;/* Local buffer -- build packet here    */
  334.     static int maxbuf = 0;    /* Current length of buffer        */
  335.     unsigned char ack;    /* Response received from NINDY        */
  336.     unsigned char checksum;    /* Packet checksum            */
  337.     char *p;        /* Pointer into buffer            */
  338.     int lenhi, lenlo;     /* High and low bytes of message length    */
  339.     int i;
  340.  
  341.  
  342.     /* Make sure local buffer is big enough.  Must include space for
  343.      * packet length, message body, and checksum.  And in the worst
  344.      * case, each character would expand into a 2-character escape
  345.      * sequence.
  346.      */
  347.     if ( maxbuf < ((2*len)+10) ){
  348.         if ( buf ){
  349.             free( buf );
  350.         }
  351.         buf = xmalloc( maxbuf=((2*len)+10) );
  352.     }
  353.  
  354.     /* Attention, NINDY!
  355.      */
  356.     SERIAL_WRITE (nindy_serial, "\020", 1);
  357.  
  358.  
  359.     lenlo = len & 0xff;
  360.     lenhi = (len>>8) & 0xff;
  361.     checksum = lenlo + lenhi;
  362.     p = buf;
  363.  
  364.     PUTBUF( lenlo, p );
  365.     PUTBUF( lenhi, p );
  366.  
  367.     for ( i=0; i<len; i++ ){
  368.         PUTBUF( msg[i], p );
  369.         checksum += msg[i];
  370.     }
  371.  
  372.     PUTBUF( checksum, p );
  373.  
  374.     /* Send checksummed message over and over until we get a positive ack
  375.      */
  376.     SERIAL_WRITE (nindy_serial, buf, p - buf);
  377.     while (1){
  378.         if ( !rdnin(&ack,1,5) ){
  379.             /* timed out */
  380.             fprintf(stderr,"ACK timed out; resending\r\n");
  381.             /* Attention, NINDY! */
  382.             SERIAL_WRITE (nindy_serial, "\020", 1);
  383.             SERIAL_WRITE (nindy_serial, buf, p - buf);
  384.         } else if ( ack == '+' ){
  385.             return;
  386.         } else if ( ack == '-' ){
  387.             fprintf( stderr, "Remote NAK; resending\r\n" );
  388.             SERIAL_WRITE (nindy_serial, buf, p - buf);
  389.         } else {
  390.             fprintf( stderr, "Bad ACK, ignored: <%c>\r\n", ack );
  391.         }
  392.     }
  393. }
  394.  
  395.  
  396.  
  397. /******************************************************************************
  398.  * send:
  399.  *    Send a message to a remote NINDY.  Check message status byte
  400.  *    for error responses.  If no error, return NINDY reponse (if any).
  401.  ******************************************************************************/
  402. static
  403. send( out, len, in )
  404.     unsigned char *out;    /* Message to be sent to NINDY            */
  405.     int len;        /* Number of meaningful bytes in out buffer    */
  406.     unsigned char *in;    /* Where to put response received from NINDY    */
  407. {
  408.     char *fmt;
  409.     int status;
  410.     static char *errmsg[] = {
  411.         "",                        /* 0 */
  412.         "Buffer overflow",                /* 1 */
  413.         "Unknown command",                /* 2 */
  414.         "Wrong amount of data to load register(s)",    /* 3 */
  415.         "Missing command argument(s)",            /* 4 */
  416.         "Odd number of digits sent to load memory",    /* 5 */
  417.         "Unknown register name",            /* 6 */
  418.         "No such memory segment",            /* 7 */
  419.         "No breakpoint available",            /* 8 */
  420.         "Can't set requested baud rate",        /* 9 */
  421.     };
  422. #    define NUMERRS    ( sizeof(errmsg) / sizeof(errmsg[0]) )
  423.  
  424.     static char err1[] = "Unknown error response from NINDY: #%d\r\n";
  425.     static char err2[] = "Error response #%d from NINDY: %s\r\n";
  426.  
  427.     while (1){
  428.         putpkt(out,len);
  429.         status = getpkt(in);
  430.         if ( status == TIMEOUT ){
  431.             fprintf( stderr, "Response timed out; resending\r\n" );
  432.         } else {
  433.             break;
  434.         }
  435.     }
  436.  
  437.     if ( status ){
  438.         fmt =  status > NUMERRS ? err1 : err2;
  439.         fprintf( stderr, fmt, status, errmsg[status] );
  440.         abort();
  441.     }
  442. }
  443.  
  444.         /************************
  445.          *                      *
  446.          *  BAUD RATE ROUTINES  *
  447.          *                      *
  448.          ************************/
  449.  
  450. /* Table of baudrates known to be acceptable to NINDY.  Each baud rate
  451.  * appears both as character string and as a Unix baud rate constant.
  452.  */
  453. struct baudrate {
  454.     char *string;
  455.     int rate;
  456. };
  457.  
  458. static struct baudrate baudtab[] = {
  459.      "1200", 1200,
  460.      "2400", 2400,
  461.      "4800", 4800,
  462.      "9600", 9600,
  463.     "19200", 19200,
  464.     "38400", 38400,
  465.     NULL,    0        /* End of table */
  466. };
  467.  
  468. /******************************************************************************
  469.  * parse_baudrate:
  470.  *    Look up the passed baud rate in the baudrate table.  If found, change
  471.  *    our internal record of the current baud rate, but don't do anything
  472.  *    about the tty just now.
  473.  *
  474.  *    Return pointer to baudrate structure on success, NULL on failure.
  475.  ******************************************************************************/
  476. static
  477. struct baudrate *
  478. parse_baudrate(s)
  479.     char *s;    /* Desired baud rate, as an ASCII (decimal) string */
  480. {
  481.     int i;
  482.  
  483.     for ( i=0; baudtab[i].string != NULL; i++ ){
  484.         if ( !strcmp(baudtab[i].string,s) ){
  485.             return &baudtab[i];
  486.         }
  487.     }
  488.     return NULL;
  489. }
  490.  
  491. /******************************************************************************
  492.  * try_baudrate:
  493.  *    Try speaking to NINDY via the specified file descriptor at the
  494.  *    specified baudrate.  Assume success if we can send an empty command
  495.  *    with a bogus checksum and receive a NAK (response of '-') back within
  496.  *    one second.
  497.  *
  498.  *    Return 1 on success, 0 on failure.
  499.  ***************************************************************************/
  500.  
  501. static int
  502. try_baudrate (serial, brp)
  503.      serial_t serial;
  504.      struct baudrate *brp;
  505. {
  506.   unsigned char c;
  507.  
  508.   /* Set specified baud rate and flush all pending input */
  509.   SERIAL_SETBAUDRATE (serial, brp->rate);
  510.   tty_flush (serial);
  511.  
  512.   /* Send empty command with bad checksum, hope for NAK ('-') response */
  513.   SERIAL_WRITE (serial, "\020\0\0\001", 4);
  514.  
  515.   /* Anything but a quick '-', including error, eof, or timeout, means that
  516.      this baudrate doesn't work.  */
  517.   return SERIAL_READCHAR (serial, 1) == '-';
  518. }
  519.  
  520. /******************************************************************************
  521.  * autobaud:
  522.  *    Get NINDY talking over the specified file descriptor at the specified
  523.  *    baud rate.  First see if NINDY's already talking at 'baudrate'.  If
  524.  *    not, run through all the legal baudrates in 'baudtab' until one works,
  525.  *    and then tell NINDY to talk at 'baudrate' instead.
  526.  ******************************************************************************/
  527. static
  528. autobaud( serial, brp )
  529.      serial_t serial;
  530.      struct baudrate *brp;
  531. {
  532.   int i;
  533.   int failures;
  534.  
  535.   say("NINDY at wrong baud rate? Trying to autobaud...\n");
  536.   failures = i = 0;
  537.   while (1)
  538.     {
  539.       say( "\r%s...   ", baudtab[i].string );
  540.       if (try_baudrate(serial, &baudtab[i]))
  541.     {
  542.       break;
  543.     }
  544.       if (baudtab[++i].string == NULL)
  545.     {
  546.       /* End of table -- wraparound */
  547.       i = 0;
  548.       if ( failures++ )
  549.         {
  550.           say("\nAutobaud failed again.  Giving up.\n");
  551.           exit(1);
  552.         }
  553.       else
  554.         {
  555.           say("\nAutobaud failed. Trying again...\n");
  556.         }
  557.     }
  558.     }
  559.  
  560.   /* Found NINDY's current baud rate; now change it.  */
  561.   say("Changing NINDY baudrate to %s\n", brp->string);
  562.   ninBaud (brp->string);
  563.  
  564.   /* Change our baud rate back to rate to which we just set NINDY.  */
  565.   SERIAL_SETBAUDRATE (serial, brp->rate);
  566. }
  567.  
  568.         /**********************************
  569.          *                  *
  570.          *   NINDY INTERFACE ROUTINES      *
  571.          *                                  *
  572.          * ninConnect *MUST* be the first *
  573.          * one of these routines called.  *
  574.          **********************************/
  575.  
  576.  
  577. /******************************************************************************
  578.  * ninBaud:
  579.  *    Ask NINDY to change the baud rate on its serial port.
  580.  *    Assumes we know the baud rate at which NINDY's currently talking.
  581.  ******************************************************************************/
  582. ninBaud( baudrate )
  583.     char *baudrate;    /* Desired baud rate, as a string of ASCII decimal
  584.              * digits.
  585.              */
  586. {
  587.   unsigned char msg[100];
  588.  
  589.   tty_flush (nindy_serial);
  590.  
  591.   if (old_nindy)
  592.     {
  593.       char *p;        /* Pointer into buffer    */
  594.       unsigned char csum;    /* Calculated checksum    */
  595.  
  596.       /* Can't use putpkt() because after the baudrate change NINDY's
  597.      ack/nak will look like gibberish.  */
  598.  
  599.       for (p=baudrate, csum=020+'z'; *p; p++)
  600.     {
  601.       csum += *p;
  602.     }
  603.       sprintf (msg, "\020z%s#%02x", baudrate, csum);
  604.       SERIAL_WRITE (nindy_serial, msg, strlen (msg));
  605.     }
  606.   else
  607.     {
  608.       /* Can't use "send" because NINDY reply will be unreadable after
  609.      baud rate change.  */
  610.       sprintf( msg, "z%s", baudrate );
  611.       putpkt( msg, strlen(msg)+1 );    /* "+1" to send terminator too */
  612.     }
  613. }
  614.  
  615. /******************************************************************************
  616.  * ninBptDel:
  617.  *    Ask NINDY to delete the specified type of *hardware* breakpoint at
  618.  *    the specified address.  If the 'addr' is -1, all breakpoints of
  619.  *    the specified type are deleted.
  620.  ***************************************************************************/
  621. ninBptDel( addr, type )
  622.     long addr;    /* Address in 960 memory    */
  623.     char type;    /* 'd' => data bkpt, 'i' => instruction breakpoint */
  624. {
  625.     unsigned char buf[10];
  626.  
  627.     if ( old_nindy ){
  628.         OninBptDel( addr, type == 'd' ? 1 : 0 );
  629.         return;
  630.     }
  631.  
  632.     buf[0] = 'b';
  633.     buf[1] = type;
  634.  
  635.     if ( addr == -1 ){
  636.         send( buf, 2, NULL );
  637.     } else {
  638.         store_unsigned_integer (&buf[2], 4, addr);
  639.         send( buf, 6, NULL );
  640.     }
  641. }
  642.  
  643.  
  644. /******************************************************************************
  645.  * ninBptSet:
  646.  *    Ask NINDY to set the specified type of *hardware* breakpoint at
  647.  *    the specified address.
  648.  ******************************************************************************/
  649. ninBptSet( addr, type )
  650.     long addr;    /* Address in 960 memory    */
  651.     char type;    /* 'd' => data bkpt, 'i' => instruction breakpoint */
  652. {
  653.     unsigned char buf[10];
  654.  
  655.     if ( old_nindy ){
  656.         OninBptSet( addr, type == 'd' ? 1 : 0 );
  657.         return;
  658.     }
  659.  
  660.  
  661.     buf[0] = 'B';
  662.     buf[1] = type;
  663.     store_unsigned_integer (&buf[2], 4, addr);
  664.     send( buf, 6, NULL );
  665. }
  666.  
  667.  
  668. /******************************************************************************
  669.  * ninConnect:
  670.  *    Open the specified tty.  Get communications working at the specified
  671.  *    baud rate.  Flush any pending I/O on the tty.
  672.  *
  673.  *    Return the file descriptor, or -1 on failure.
  674.  ******************************************************************************/
  675. int
  676. ninConnect( name, baudrate, brk, silent, old_protocol )
  677.     char *name;        /* "/dev/ttyXX" to be opened            */
  678.     char *baudrate;/* baud rate: a string of ascii decimal digits (eg,"9600")*/
  679.     int brk;        /* 1 => send break to tty first thing after opening it*/
  680.     int silent;        /* 1 => stifle unnecessary messages when talking to 
  681.              *    this tty.
  682.              */
  683.     int old_protocol;
  684. {
  685.     int i;
  686.     char *p;
  687.     struct baudrate *brp;
  688.  
  689.     /* We will try each of the following paths when trying to open the tty
  690.      */
  691.     static char *prefix[] = { "", "/dev/", "/dev/tty", NULL };
  692.  
  693.     if ( old_protocol ){
  694.         old_nindy = 1;
  695.     }
  696.  
  697.     quiet = silent;        /* Make global to this file */
  698.  
  699.     for ( i=0; prefix[i] != NULL; i++ ){
  700.         p = xmalloc(strlen(prefix[i]) + strlen(name) + 1 );
  701.         strcpy( p, prefix[i] );
  702.         strcat( p, name );
  703.         nindy_serial = SERIAL_OPEN (p);
  704.         if (nindy_serial != NULL) {
  705. #ifdef TIOCEXCL
  706.             /* Exclusive use mode (hp9000 does not support it) */
  707.             ioctl(nindy_serial->fd,TIOCEXCL,NULL);
  708. #endif
  709.             SERIAL_RAW (nindy_serial);
  710.  
  711.             if (brk)
  712.               {
  713.                 SERIAL_SEND_BREAK (nindy_serial);
  714.               }
  715.  
  716.             brp = parse_baudrate( baudrate );
  717.             if ( brp == NULL ){
  718.                 say("Illegal baudrate %s ignored; using 9600\n",
  719.                                 baudrate);
  720.                 brp = parse_baudrate( "9600" );
  721.             }
  722.  
  723.             if ( !try_baudrate(nindy_serial, brp) ){
  724.                 autobaud(nindy_serial, brp);
  725.             }
  726.             tty_flush (nindy_serial);
  727.             say( "Connected to %s\n", p );
  728.             free(p);
  729.             break;
  730.         }
  731.         free(p);
  732.     }
  733.     return 0;
  734. }
  735.  
  736. #if 0
  737.  
  738. /* Currently unused; shouldn't we be doing this on target_kill and
  739. perhaps target_mourn?  FIXME.  */
  740.  
  741. /******************************************************************************
  742.  * ninGdbExit:
  743.  *    Ask NINDY to leave GDB mode and print a NINDY prompt.
  744.  ****************************************************************************/
  745. ninGdbExit()
  746. {
  747.     if ( old_nindy ){
  748.         OninGdbExit();
  749.         return;
  750.     }
  751.         putpkt((unsigned char *) "E", 1 );
  752. }
  753. #endif
  754.  
  755. /******************************************************************************
  756.  * ninGo:
  757.  *    Ask NINDY to start or continue execution of an application program
  758.  *    in it's memory at the current ip.
  759.  ******************************************************************************/
  760. ninGo( step_flag )
  761.     int step_flag;    /* 1 => run in single-step mode */
  762. {
  763.     if ( old_nindy ){
  764.         OninGo( step_flag );
  765.         return;
  766.     }
  767.     putpkt((unsigned char *) (step_flag ? "s" : "c"), 1 );
  768. }
  769.  
  770.  
  771. /******************************************************************************
  772.  * ninMemGet:
  773.  *    Read a string of bytes from NINDY's address space (960 memory).
  774.  ******************************************************************************/
  775. ninMemGet(ninaddr, hostaddr, len)
  776.      long ninaddr;    /* Source address, in the 960 memory space    */
  777.      unsigned char *hostaddr;    /* Destination address, in our memory space */
  778.      int len;        /* Number of bytes to read            */
  779. {
  780.     unsigned char buf[BUFSIZE+20];
  781.     int cnt;        /* Number of bytes in next transfer    */
  782.  
  783.     if ( old_nindy ){
  784.         OninMemGet(ninaddr, hostaddr, len);
  785.         return;
  786.     }
  787.  
  788.     for ( ; len > 0; len -= BUFSIZE ){
  789.         cnt = len > BUFSIZE ? BUFSIZE : len;
  790.  
  791.         buf[0] = 'm';
  792.         store_unsigned_integer (&buf[1], 4, ninaddr);
  793.         buf[5] = cnt & 0xff;
  794.         buf[6] = (cnt>>8) & 0xff;
  795.  
  796.         send( buf, 7, hostaddr );
  797.  
  798.         ninaddr += cnt;
  799.         hostaddr += cnt;
  800.     }
  801. }
  802.  
  803.  
  804. /******************************************************************************
  805.  * ninMemPut:
  806.  *    Write a string of bytes into NINDY's address space (960 memory).
  807.  ******************************************************************************/
  808. ninMemPut( ninaddr, hostaddr, len )
  809.      long ninaddr;    /* Destination address, in NINDY memory space    */
  810.      unsigned char *hostaddr;    /* Source address, in our memory space    */
  811.      int len;        /* Number of bytes to write            */
  812. {
  813.     unsigned char buf[BUFSIZE+20];
  814.     int cnt;        /* Number of bytes in next transfer    */
  815.  
  816.     if ( old_nindy ){
  817.         OninMemPut( ninaddr, hostaddr, len );
  818.         return;
  819.     }
  820.     for ( ; len > 0; len -= BUFSIZE ){
  821.         cnt = len > BUFSIZE ? BUFSIZE : len;
  822.  
  823.         buf[0] = 'M';
  824.         store_unsigned_integer (&buf[1], 4, ninaddr);
  825.         memcpy(buf + 5, hostaddr, cnt);
  826.         send( buf, cnt+5, NULL );
  827.  
  828.         ninaddr += cnt;
  829.         hostaddr += cnt;
  830.     }
  831. }
  832.  
  833. /******************************************************************************
  834.  * ninRegGet:
  835.  *    Retrieve the contents of a 960 register, and return them as a long
  836.  *    in host byte order.
  837.  *
  838.  *    THIS ROUTINE CAN ONLY BE USED TO READ THE LOCAL, GLOBAL, AND
  839.  *    ip/ac/pc/tc REGISTERS.
  840.  *
  841.  ******************************************************************************/
  842. long
  843. ninRegGet( regname )
  844.     char *regname;    /* Register name recognized by NINDY, subject to the
  845.              * above limitations.
  846.              */
  847. {
  848.     unsigned char outbuf[10];
  849.     unsigned char inbuf[20];
  850.  
  851.     if ( old_nindy ){
  852.         return OninRegGet( regname );
  853.     }
  854.  
  855.     sprintf( outbuf, "u%s:", regname );
  856.     send( outbuf, strlen(outbuf), inbuf );
  857.     return extract_unsigned_integer (inbuf, 4);
  858. }
  859.  
  860. /******************************************************************************
  861.  * ninRegPut:
  862.  *    Set the contents of a 960 register.
  863.  *
  864.  *    THIS ROUTINE CAN ONLY BE USED TO SET THE LOCAL, GLOBAL, AND
  865.  *    ip/ac/pc/tc REGISTERS.
  866.  *
  867.  ******************************************************************************/
  868. ninRegPut( regname, val )
  869.     char *regname;    /* Register name recognized by NINDY, subject to the
  870.              * above limitations.
  871.              */
  872.     long val;        /* New contents of register, in host byte-order    */
  873. {
  874.     unsigned char buf[20];
  875.     int len;
  876.  
  877.     if ( old_nindy ){
  878.         OninRegPut( regname, val );
  879.         return;
  880.     }
  881.  
  882.     sprintf( buf, "U%s:", regname );
  883.     len = strlen(buf);
  884.     store_unsigned_integer (&buf[len], 4, val);
  885.     send( buf, len+4, NULL );
  886. }
  887.  
  888. /******************************************************************************
  889.  * ninRegsGet:
  890.  *    Get a dump of the contents of the entire 960 register set.  The
  891.  *    individual registers appear in the dump in the following order:
  892.  *
  893.  *        pfp  sp   rip  r3   r4   r5   r6   r7 
  894.  *        r8   r9   r10  r11  r12  r13  r14  r15 
  895.  *        g0   g1   g2   g3   g4   g5   g6   g7 
  896.  *        g8   g9   g10  g11  g12  g13  g14  fp 
  897.  *        pc   ac   ip   tc   fp0  fp1  fp2  fp3
  898.  *
  899.  *    Each individual register comprises exactly 4 bytes, except for
  900.  *    fp0-fp3, which are 8 bytes.  All register values are in 960
  901.  *    (little-endian) byte order.
  902.  *
  903.  ******************************************************************************/
  904. ninRegsGet( regp )
  905.     unsigned char *regp;        /* Where to place the register dump */
  906. {
  907.     if ( old_nindy ){
  908.         OninRegsGet( regp );
  909.         return;
  910.     }
  911.     send( (unsigned char *) "r", 1, regp );
  912. }
  913.  
  914.  
  915. /******************************************************************************
  916.  * ninRegsPut:
  917.  *    Initialize the entire 960 register set to a specified set of values.
  918.  *    The format of the register value data should be the same as that
  919.  *    returned by ninRegsGet.
  920.  *
  921.  * WARNING:
  922.  *    All register values must be in 960 (little-endian) byte order.
  923.  *
  924.  ******************************************************************************/
  925. ninRegsPut( regp )
  926.     char *regp;        /* Pointer to desired values of registers */
  927. {
  928. /* Number of bytes that we send to nindy.  I believe this is defined by
  929.    the protocol (it does not agree with REGISTER_BYTES).  */
  930. #define NINDY_REGISTER_BYTES    ((36*4) + (4*8))
  931.     unsigned char buf[NINDY_REGISTER_BYTES+10];
  932.  
  933.     if ( old_nindy ){
  934.         OninRegsPut( regp );
  935.         return;
  936.     }
  937.  
  938.     buf[0] = 'R';
  939.     memcpy(buf+1,  regp, NINDY_REGISTER_BYTES );
  940.     send( buf, NINDY_REGISTER_BYTES+1, NULL );
  941. }
  942.  
  943.  
  944. /******************************************************************************
  945.  * ninReset:
  946.  *      Ask NINDY to perform a soft reset; wait for the reset to complete.
  947.  *
  948.  ******************************************************************************/
  949. ninReset()
  950. {
  951.     unsigned char ack;
  952.  
  953.     if ( old_nindy ){
  954.         OninReset();
  955.         return;
  956.     }
  957.  
  958.     while (1){
  959.         putpkt((unsigned char *) "X", 1 );
  960.         while (1){
  961.             if ( !rdnin(&ack,1,5) ){
  962.                 /* Timed out */
  963.                 break;        /* Resend */
  964.             }
  965.             if ( ack == '+' ){
  966.                 return;
  967.             }
  968.         }
  969.     }
  970. }
  971.  
  972.  
  973. /******************************************************************************
  974.  * ninSrq:
  975.  *    Assume NINDY has stopped execution of the 960 application program in
  976.  *    order to process a host service request (srq).  Ask NINDY for the
  977.  *    srq arguments, perform the requested service, and send an "srq
  978.  *    complete" message so NINDY will return control to the application.
  979.  *
  980.  ******************************************************************************/
  981. ninSrq()
  982. {
  983.   /* FIXME: Imposes arbitrary limits on lengths of pathnames and such.  */
  984.     unsigned char buf[BUFSIZE];
  985.     int retcode;
  986.     unsigned char srqnum;
  987.     int i;
  988.     int offset;
  989.     int arg[MAX_SRQ_ARGS];
  990.  
  991.     if ( old_nindy ){
  992.         OninSrq();
  993.         return;
  994.     }
  995.  
  996.  
  997.     /* Get srq number and arguments
  998.      */
  999.     send((unsigned char *) "!", 1, buf );
  1000.  
  1001.     srqnum = buf[0];
  1002.     for  ( i=0, offset=1; i < MAX_SRQ_ARGS; i++, offset+=4 ){
  1003.         arg[i] = extract_unsigned_integer (&buf[offset], 4);
  1004.     }
  1005.  
  1006.     /* Process Srq
  1007.      */
  1008.     switch( srqnum ){
  1009.     case BS_CLOSE:
  1010.         /* args: file descriptor */
  1011.         if ( arg[0] > 2 ){
  1012.             retcode = close( arg[0] );
  1013.         } else {
  1014.             retcode = 0;
  1015.         }
  1016.         break;
  1017.     case BS_CREAT:
  1018.         /* args: filename, mode */
  1019.         ninStrGet( arg[0], buf );
  1020.         retcode = creat(buf,arg[1]);
  1021.         break;
  1022.     case BS_OPEN:
  1023.         /* args: filename, flags, mode */
  1024.         ninStrGet( arg[0], buf );
  1025.         retcode = open(buf,arg[1],arg[2]);
  1026.         break;
  1027.     case BS_READ:
  1028.         /* args: file descriptor, buffer, count */
  1029.         retcode = read(arg[0],buf,arg[2]);
  1030.         if ( retcode > 0 ){
  1031.             ninMemPut( arg[1], buf, retcode );
  1032.         }
  1033.         break;
  1034.     case BS_SEEK:
  1035.         /* args: file descriptor, offset, whence */
  1036.         retcode = lseek(arg[0],arg[1],arg[2]);
  1037.         break;
  1038.     case BS_WRITE:
  1039.         /* args: file descriptor, buffer, count */
  1040.         ninMemGet( arg[1], buf, arg[2] );
  1041.         retcode = write(arg[0],buf,arg[2]);
  1042.         break;
  1043.     default:
  1044.         retcode = -1;
  1045.         break;
  1046.     }
  1047.  
  1048.     /* Send request termination status to NINDY
  1049.      */
  1050.     buf[0] = 'e';
  1051.     store_unsigned_integer (&buf[1], 4, retcode);
  1052.     send( buf, 5, NULL );
  1053. }
  1054.  
  1055.  
  1056. /******************************************************************************
  1057.  * ninStopWhy:
  1058.  *    Assume the application program has stopped (i.e., a DLE was received
  1059.  *    from NINDY).  Ask NINDY for status information describing the
  1060.  *    reason for the halt.
  1061.  *
  1062.  *    Returns a non-zero value if the user program has exited, 0 otherwise.
  1063.  *    Also returns the following information, through passed pointers:
  1064.  *           - why: an exit code if program the exited; otherwise the reason
  1065.  *            why the program halted (see stop.h for values).
  1066.  *        - contents of register ip (little-endian byte order)
  1067.  *        - contents of register sp (little-endian byte order)
  1068.  *        - contents of register fp (little-endian byte order)
  1069.  ******************************************************************************/
  1070. char
  1071. ninStopWhy( whyp, ipp, fpp, spp )
  1072.     unsigned char *whyp; /* Return the 'why' code through this pointer    */
  1073.     long *ipp;    /* Return contents of register ip through this pointer    */
  1074.     long *fpp;    /* Return contents of register fp through this pointer    */
  1075.     long *spp;    /* Return contents of register sp through this pointer    */
  1076. {
  1077.     unsigned char buf[30];
  1078.     extern char OninStopWhy ();
  1079.  
  1080.     if ( old_nindy ){
  1081.         return OninStopWhy( whyp, ipp, fpp, spp );
  1082.     }
  1083.     send((unsigned char *) "?", 1, buf );
  1084.  
  1085.     *whyp = buf[1];
  1086.     memcpy ((char *)ipp, &buf[2],  sizeof (*ipp));
  1087.     memcpy ((char *)fpp, &buf[6],  sizeof (*ipp));
  1088.     memcpy ((char *)spp, &buf[10], sizeof (*ipp));
  1089.     return buf[0];
  1090. }
  1091.  
  1092. /******************************************************************************
  1093.  * ninStrGet:
  1094.  *    Read a '\0'-terminated string of data out of the 960 memory space.
  1095.  *
  1096.  ******************************************************************************/
  1097. static
  1098. ninStrGet( ninaddr, hostaddr )
  1099.      unsigned long ninaddr;    /* Address of string in NINDY memory space */
  1100.      unsigned char *hostaddr;    /* Address of the buffer to which string should
  1101.                  *    be copied.
  1102.                  */
  1103. {
  1104.     unsigned char cmd[5];
  1105.  
  1106.     cmd[0] = '"';
  1107.     store_unsigned_integer (&cmd[1], 4, ninaddr);
  1108.     send( cmd, 5, hostaddr );
  1109. }
  1110.  
  1111. #if 0
  1112. /* Not used.  */
  1113.  
  1114. /******************************************************************************
  1115.  * ninVersion:
  1116.  *    Ask NINDY for version information about itself.
  1117.  *    The information is sent as an ascii string in the form "x.xx,<arch>",
  1118.  *    where,
  1119.  *        x.xx    is the version number
  1120.  *        <arch>    is the processor architecture: "KA", "KB", "MC", "CA" *
  1121.  *
  1122.  ******************************************************************************/
  1123. int
  1124. ninVersion( p )
  1125.      unsigned char *p;        /* Where to place version string */
  1126. {
  1127.  
  1128.     if ( old_nindy ){
  1129.         return OninVersion( p );
  1130.     }
  1131.     send((unsigned char *) "v", 1, p );
  1132.     return strlen(p);
  1133. }
  1134. #endif /* 0 */
  1135.