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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [nindy-share/] [nindy.c] - Diff between revs 107 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 107 Rev 1765
/* This file is part of GDB.
/* This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 
/* This started out life as code shared between the nindy monitor and
/* This started out life as code shared between the nindy monitor and
   GDB.  For various reasons, this is no longer true.  Eventually, it
   GDB.  For various reasons, this is no longer true.  Eventually, it
   probably should be merged into remote-nindy.c.  */
   probably should be merged into remote-nindy.c.  */
 
 
/******************************************************************************
/******************************************************************************
 *
 *
 *                      NINDY INTERFACE ROUTINES
 *                      NINDY INTERFACE ROUTINES
 *
 *
 * The caller of these routines should be aware that:
 * The caller of these routines should be aware that:
 *
 *
 * (1) ninConnect() should be called to open communications with the
 * (1) ninConnect() should be called to open communications with the
 *     remote NINDY board before any of the other routines are invoked.
 *     remote NINDY board before any of the other routines are invoked.
 *
 *
 * (2) almost all interactions are driven by the host: nindy sends information
 * (2) almost all interactions are driven by the host: nindy sends information
 *     in response to host commands.
 *     in response to host commands.
 *
 *
 * (3) the lone exception to (2) is the single character DLE (^P, 0x10).
 * (3) the lone exception to (2) is the single character DLE (^P, 0x10).
 *     Receipt of a DLE from NINDY indicates that the application program
 *     Receipt of a DLE from NINDY indicates that the application program
 *     running under NINDY has stopped execution and that NINDY is now
 *     running under NINDY has stopped execution and that NINDY is now
 *     available to talk to the host (all other communication received after
 *     available to talk to the host (all other communication received after
 *     the application has been started should be presumed to come from the
 *     the application has been started should be presumed to come from the
 *     application and should be passed on by the host to stdout).
 *     application and should be passed on by the host to stdout).
 *
 *
 * (4) the reason the application program stopped can be determined with the
 * (4) the reason the application program stopped can be determined with the
 *     ninStopWhy() function.  There are three classes of stop reasons:
 *     ninStopWhy() function.  There are three classes of stop reasons:
 *
 *
 *      (a) the application has terminated execution.
 *      (a) the application has terminated execution.
 *          The host should take appropriate action.
 *          The host should take appropriate action.
 *
 *
 *      (b) the application had a fault or trace event.
 *      (b) the application had a fault or trace event.
 *          The host should take appropriate action.
 *          The host should take appropriate action.
 *
 *
 *      (c) the application wishes to make a service request (srq) of the host;
 *      (c) the application wishes to make a service request (srq) of the host;
 *          e.g., to open/close a file, read/write a file, etc.  The ninSrq()
 *          e.g., to open/close a file, read/write a file, etc.  The ninSrq()
 *          function should be called to determine the nature of the request
 *          function should be called to determine the nature of the request
 *          and process it.
 *          and process it.
 */
 */
 
 
#include <stdio.h>
#include <stdio.h>
#include "defs.h"
#include "defs.h"
#include "serial.h"
#include "serial.h"
#ifdef ANSI_PROTOTYPES
#ifdef ANSI_PROTOTYPES
#include <stdarg.h>
#include <stdarg.h>
#else
#else
#include <varargs.h>
#include <varargs.h>
#endif
#endif
 
 
#if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
#if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
#define HAVE_SGTTY
#define HAVE_SGTTY
#endif
#endif
 
 
#ifdef HAVE_SGTTY
#ifdef HAVE_SGTTY
#include <sys/ioctl.h>
#include <sys/ioctl.h>
#endif
#endif
 
 
#include <sys/types.h>  /* Needed by file.h on Sys V */
#include <sys/types.h>  /* Needed by file.h on Sys V */
#include <sys/file.h>
#include <sys/file.h>
#include <signal.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/stat.h>
 
 
#if 0
#if 0
#include "ttycntl.h"
#include "ttycntl.h"
#endif
#endif
#include "block_io.h"
#include "block_io.h"
#include "gdb_wait.h"
#include "gdb_wait.h"
#include "env.h"
#include "env.h"
 
 
#define DLE     0x10    /* ^P */
#define DLE     0x10    /* ^P */
#define XON     0x11    /* ^Q */
#define XON     0x11    /* ^Q */
#define XOFF    0x13    /* ^S */
#define XOFF    0x13    /* ^S */
#define ESC     0x1b
#define ESC     0x1b
 
 
#define TIMEOUT         -1
#define TIMEOUT         -1
 
 
int quiet = 0;   /* 1 => stifle unnecessary messages */
int quiet = 0;   /* 1 => stifle unnecessary messages */
serial_t nindy_serial;
serial_t nindy_serial;
 
 
static int old_nindy = 0; /* 1 => use old (hex) communication protocol */
static int old_nindy = 0; /* 1 => use old (hex) communication protocol */
static ninStrGet();
static ninStrGet();


                /****************************
                /****************************
                 *                          *
                 *                          *
                 *  MISCELLANEOUS UTILTIES  *
                 *  MISCELLANEOUS UTILTIES  *
                 *                          *
                 *                          *
                 ****************************/
                 ****************************/
 
 
/******************************************************************************
/******************************************************************************
 * say:
 * say:
 *      This is a printf that takes at most two arguments (in addition to the
 *      This is a printf that takes at most two arguments (in addition to the
 *      format string) and that outputs nothing if verbose output has been
 *      format string) and that outputs nothing if verbose output has been
 *      suppressed.
 *      suppressed.
 *****************************************************************************/
 *****************************************************************************/
 
 
/* VARARGS */
/* VARARGS */
static void
static void
#ifdef ANSI_PROTOTYPES
#ifdef ANSI_PROTOTYPES
say (char *fmt, ...)
say (char *fmt, ...)
#else
#else
say (va_alist)
say (va_alist)
     va_dcl
     va_dcl
#endif
#endif
{
{
  va_list args;
  va_list args;
#ifdef ANSI_PROTOTYPES
#ifdef ANSI_PROTOTYPES
  va_start(args, fmt);
  va_start(args, fmt);
#else
#else
  char *fmt;
  char *fmt;
 
 
  va_start (args);
  va_start (args);
  fmt = va_arg (args, char *);
  fmt = va_arg (args, char *);
#endif
#endif
 
 
  if (!quiet)
  if (!quiet)
    {
    {
      vfprintf_unfiltered (gdb_stdout, fmt, args);
      vfprintf_unfiltered (gdb_stdout, fmt, args);
      gdb_flush (gdb_stdout);
      gdb_flush (gdb_stdout);
    }
    }
  va_end (args);
  va_end (args);
}
}
 
 
/******************************************************************************
/******************************************************************************
 * exists:
 * exists:
 *      Creates a full pathname by concatenating up to three name components
 *      Creates a full pathname by concatenating up to three name components
 *      onto a specified base name; optionally looks up the base name as a
 *      onto a specified base name; optionally looks up the base name as a
 *      runtime environment variable;  and checks to see if the file or
 *      runtime environment variable;  and checks to see if the file or
 *      directory specified by the pathname actually exists.
 *      directory specified by the pathname actually exists.
 *
 *
 *      Returns:  the full pathname if it exists, NULL otherwise.
 *      Returns:  the full pathname if it exists, NULL otherwise.
 *              (returned pathname is in malloc'd memory and must be freed
 *              (returned pathname is in malloc'd memory and must be freed
 *              by caller).
 *              by caller).
 *****************************************************************************/
 *****************************************************************************/
static char *
static char *
exists( base, c1, c2, c3, env )
exists( base, c1, c2, c3, env )
    char *base;         /* Base directory of path */
    char *base;         /* Base directory of path */
    char *c1, *c2, *c3; /* Components (subdirectories and/or file name) to be
    char *c1, *c2, *c3; /* Components (subdirectories and/or file name) to be
                         *      appended onto the base directory name.  One or
                         *      appended onto the base directory name.  One or
                         *      more may be omitted by passing NULL pointers.
                         *      more may be omitted by passing NULL pointers.
                         */
                         */
    int env;            /* If 1, '*base' is the name of an environment variable
    int env;            /* If 1, '*base' is the name of an environment variable
                         *      to be examined for the base directory name;
                         *      to be examined for the base directory name;
                         *      otherwise, '*base' is the actual name of the
                         *      otherwise, '*base' is the actual name of the
                         *      base directory.
                         *      base directory.
                         */
                         */
{
{
        struct stat buf;/* For call to 'stat' -- never examined */
        struct stat buf;/* For call to 'stat' -- never examined */
        char *path;     /* Pointer to full pathname (malloc'd memory) */
        char *path;     /* Pointer to full pathname (malloc'd memory) */
        int len;        /* Length of full pathname (incl. terminator) */
        int len;        /* Length of full pathname (incl. terminator) */
        extern char *getenv();
        extern char *getenv();
 
 
 
 
        if ( env ){
        if ( env ){
                base = getenv( base );
                base = getenv( base );
                if ( base == NULL ){
                if ( base == NULL ){
                        return NULL;
                        return NULL;
                }
                }
        }
        }
 
 
        len = strlen(base) + 4;
        len = strlen(base) + 4;
                        /* +4 for terminator and "/" before each component */
                        /* +4 for terminator and "/" before each component */
        if ( c1 != NULL ){
        if ( c1 != NULL ){
                len += strlen(c1);
                len += strlen(c1);
        }
        }
        if ( c2 != NULL ){
        if ( c2 != NULL ){
                len += strlen(c2);
                len += strlen(c2);
        }
        }
        if ( c3 != NULL ){
        if ( c3 != NULL ){
                len += strlen(c3);
                len += strlen(c3);
        }
        }
 
 
        path = xmalloc (len);
        path = xmalloc (len);
 
 
        strcpy( path, base );
        strcpy( path, base );
        if ( c1 != NULL ){
        if ( c1 != NULL ){
                strcat( path, "/" );
                strcat( path, "/" );
                strcat( path, c1 );
                strcat( path, c1 );
                if ( c2 != NULL ){
                if ( c2 != NULL ){
                        strcat( path, "/" );
                        strcat( path, "/" );
                        strcat( path, c2 );
                        strcat( path, c2 );
                        if ( c3 != NULL ){
                        if ( c3 != NULL ){
                                strcat( path, "/" );
                                strcat( path, "/" );
                                strcat( path, c3 );
                                strcat( path, c3 );
                        }
                        }
                }
                }
        }
        }
 
 
        if ( stat(path,&buf) != 0 ){
        if ( stat(path,&buf) != 0 ){
                free( path );
                free( path );
                path = NULL;
                path = NULL;
        }
        }
        return path;
        return path;
}
}


                /*****************************
                /*****************************
                 *                           *
                 *                           *
                 *  LOW-LEVEL COMMUNICATION  *
                 *  LOW-LEVEL COMMUNICATION  *
                 *                           *
                 *                           *
                 *****************************/
                 *****************************/
 
 
/* Read *exactly* N characters from the NINDY tty, and put them in
/* Read *exactly* N characters from the NINDY tty, and put them in
   *BUF.  Translate escape sequences into single characters, counting
   *BUF.  Translate escape sequences into single characters, counting
   each such sequence as 1 character.
   each such sequence as 1 character.
 
 
   An escape sequence consists of ESC and a following character.  The
   An escape sequence consists of ESC and a following character.  The
   ESC is discarded and the other character gets bit 0x40 cleared --
   ESC is discarded and the other character gets bit 0x40 cleared --
   thus ESC P == ^P, ESC S == ^S, ESC [ == ESC, etc.
   thus ESC P == ^P, ESC S == ^S, ESC [ == ESC, etc.
 
 
   Return 1 if successful, 0 if more than TIMEOUT seconds pass without
   Return 1 if successful, 0 if more than TIMEOUT seconds pass without
   any input.  */
   any input.  */
 
 
static int
static int
rdnin (buf,n,timeout)
rdnin (buf,n,timeout)
    unsigned char * buf;        /* Where to place characters read       */
    unsigned char * buf;        /* Where to place characters read       */
    int n;                      /* Number of characters to read         */
    int n;                      /* Number of characters to read         */
    int timeout;                /* Timeout, in seconds                  */
    int timeout;                /* Timeout, in seconds                  */
{
{
  int escape_seen;      /* 1 => last character of a read was an ESC */
  int escape_seen;      /* 1 => last character of a read was an ESC */
  int c;
  int c;
 
 
  escape_seen = 0;
  escape_seen = 0;
  while (n)
  while (n)
    {
    {
      c = SERIAL_READCHAR (nindy_serial, timeout);
      c = SERIAL_READCHAR (nindy_serial, timeout);
      switch (c)
      switch (c)
        {
        {
        case SERIAL_ERROR:
        case SERIAL_ERROR:
        case SERIAL_TIMEOUT:
        case SERIAL_TIMEOUT:
        case SERIAL_EOF:
        case SERIAL_EOF:
          return 0;
          return 0;
 
 
        case ESC:
        case ESC:
          escape_seen = 1;
          escape_seen = 1;
          break;
          break;
 
 
        default:
        default:
          if (escape_seen)
          if (escape_seen)
            {
            {
              escape_seen = 0;
              escape_seen = 0;
              c &= ~0x40;
              c &= ~0x40;
            }
            }
          *buf++ = c;
          *buf++ = c;
          --n;
          --n;
          break;
          break;
        }
        }
    }
    }
  return 1;
  return 1;
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * getpkt:
 * getpkt:
 *      Read a packet from a remote NINDY, with error checking, into the
 *      Read a packet from a remote NINDY, with error checking, into the
 *      indicated buffer.
 *      indicated buffer.
 *
 *
 *      Return packet status byte on success, TIMEOUT on failure.
 *      Return packet status byte on success, TIMEOUT on failure.
 ******************************************************************************/
 ******************************************************************************/
static
static
int
int
getpkt(buf)
getpkt(buf)
     unsigned char *buf;
     unsigned char *buf;
{
{
        int i;
        int i;
        unsigned char hdr[3];   /* Packet header:
        unsigned char hdr[3];   /* Packet header:
                                 *      hdr[0] = low byte of message length
                                 *      hdr[0] = low byte of message length
                                 *      hdr[1] = high byte of message length
                                 *      hdr[1] = high byte of message length
                                 *      hdr[2] = message status
                                 *      hdr[2] = message status
                                 */
                                 */
        int cnt;                /* Message length (status byte + data)  */
        int cnt;                /* Message length (status byte + data)  */
        unsigned char cs_calc;  /* Checksum calculated                  */
        unsigned char cs_calc;  /* Checksum calculated                  */
        unsigned char cs_recv;  /* Checksum received                    */
        unsigned char cs_recv;  /* Checksum received                    */
        static char errfmt[] =
        static char errfmt[] =
                        "Bad checksum (recv=0x%02x; calc=0x%02x); retrying\r\n";
                        "Bad checksum (recv=0x%02x; calc=0x%02x); retrying\r\n";
 
 
        while (1){
        while (1){
                if ( !rdnin(hdr,3,5) ){
                if ( !rdnin(hdr,3,5) ){
                        return TIMEOUT;
                        return TIMEOUT;
                }
                }
                cnt = (hdr[1]<<8) + hdr[0] - 1;
                cnt = (hdr[1]<<8) + hdr[0] - 1;
                                        /* -1 for status byte (already read) */
                                        /* -1 for status byte (already read) */
 
 
                /* Caller's buffer may only be big enough for message body,
                /* Caller's buffer may only be big enough for message body,
                 * without status byte and checksum, so make sure to read
                 * without status byte and checksum, so make sure to read
                 * checksum into a separate buffer.
                 * checksum into a separate buffer.
                 */
                 */
                if ( !rdnin(buf,cnt,5) || !rdnin(&cs_recv,1,5) ){
                if ( !rdnin(buf,cnt,5) || !rdnin(&cs_recv,1,5) ){
                        return TIMEOUT;
                        return TIMEOUT;
                }
                }
 
 
                /* Calculate checksum
                /* Calculate checksum
                 */
                 */
                cs_calc = hdr[0] + hdr[1] + hdr[2];
                cs_calc = hdr[0] + hdr[1] + hdr[2];
                for ( i = 0; i < cnt; i++ ){
                for ( i = 0; i < cnt; i++ ){
                        cs_calc += buf[i];
                        cs_calc += buf[i];
                }
                }
                if ( cs_calc == cs_recv ){
                if ( cs_calc == cs_recv ){
                        SERIAL_WRITE (nindy_serial, "+", 1);
                        SERIAL_WRITE (nindy_serial, "+", 1);
                        return hdr[2];
                        return hdr[2];
                }
                }
 
 
                /* Bad checksum: report, send NAK, and re-receive
                /* Bad checksum: report, send NAK, and re-receive
                 */
                 */
                fprintf(stderr, errfmt, cs_recv, cs_calc );
                fprintf(stderr, errfmt, cs_recv, cs_calc );
                SERIAL_WRITE (nindy_serial, "-", 1);
                SERIAL_WRITE (nindy_serial, "-", 1);
        }
        }
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * putpkt:
 * putpkt:
 *      Send a packet to NINDY, checksumming it and converting special
 *      Send a packet to NINDY, checksumming it and converting special
 *      characters to escape sequences.
 *      characters to escape sequences.
 ******************************************************************************/
 ******************************************************************************/
 
 
/* This macro puts the character 'c' into the buffer pointed at by 'p',
/* This macro puts the character 'c' into the buffer pointed at by 'p',
 * and increments the pointer.  If 'c' is one of the 4 special characters
 * and increments the pointer.  If 'c' is one of the 4 special characters
 * in the transmission protocol, it is converted into a 2-character
 * in the transmission protocol, it is converted into a 2-character
 * escape sequence.
 * escape sequence.
 */
 */
#define PUTBUF(c,p)                                             \
#define PUTBUF(c,p)                                             \
        if ( c == DLE || c == ESC || c == XON || c == XOFF ){   \
        if ( c == DLE || c == ESC || c == XON || c == XOFF ){   \
                *p++ = ESC;                                     \
                *p++ = ESC;                                     \
                *p++ = c | 0x40;                                \
                *p++ = c | 0x40;                                \
        } else {                                                \
        } else {                                                \
                *p++ = c;                                       \
                *p++ = c;                                       \
        }
        }
 
 
static
static
putpkt( msg, len )
putpkt( msg, len )
    unsigned char *msg; /* Command to be sent, without lead ^P (\020) or checksum */
    unsigned char *msg; /* Command to be sent, without lead ^P (\020) or checksum */
    int len;    /* Number of bytes in message                   */
    int len;    /* Number of bytes in message                   */
{
{
        static char *buf = NULL;/* Local buffer -- build packet here    */
        static char *buf = NULL;/* Local buffer -- build packet here    */
        static int maxbuf = 0;   /* Current length of buffer             */
        static int maxbuf = 0;   /* Current length of buffer             */
        unsigned char ack;      /* Response received from NINDY         */
        unsigned char ack;      /* Response received from NINDY         */
        unsigned char checksum; /* Packet checksum                      */
        unsigned char checksum; /* Packet checksum                      */
        char *p;                /* Pointer into buffer                  */
        char *p;                /* Pointer into buffer                  */
        int lenhi, lenlo;       /* High and low bytes of message length */
        int lenhi, lenlo;       /* High and low bytes of message length */
        int i;
        int i;
 
 
 
 
        /* Make sure local buffer is big enough.  Must include space for
        /* Make sure local buffer is big enough.  Must include space for
         * packet length, message body, and checksum.  And in the worst
         * packet length, message body, and checksum.  And in the worst
         * case, each character would expand into a 2-character escape
         * case, each character would expand into a 2-character escape
         * sequence.
         * sequence.
         */
         */
        if ( maxbuf < ((2*len)+10) ){
        if ( maxbuf < ((2*len)+10) ){
                if ( buf ){
                if ( buf ){
                        free( buf );
                        free( buf );
                }
                }
                buf = xmalloc( maxbuf=((2*len)+10) );
                buf = xmalloc( maxbuf=((2*len)+10) );
        }
        }
 
 
        /* Attention, NINDY!
        /* Attention, NINDY!
         */
         */
        SERIAL_WRITE (nindy_serial, "\020", 1);
        SERIAL_WRITE (nindy_serial, "\020", 1);
 
 
 
 
        lenlo = len & 0xff;
        lenlo = len & 0xff;
        lenhi = (len>>8) & 0xff;
        lenhi = (len>>8) & 0xff;
        checksum = lenlo + lenhi;
        checksum = lenlo + lenhi;
        p = buf;
        p = buf;
 
 
        PUTBUF( lenlo, p );
        PUTBUF( lenlo, p );
        PUTBUF( lenhi, p );
        PUTBUF( lenhi, p );
 
 
        for ( i=0; i<len; i++ ){
        for ( i=0; i<len; i++ ){
                PUTBUF( msg[i], p );
                PUTBUF( msg[i], p );
                checksum += msg[i];
                checksum += msg[i];
        }
        }
 
 
        PUTBUF( checksum, p );
        PUTBUF( checksum, p );
 
 
        /* Send checksummed message over and over until we get a positive ack
        /* Send checksummed message over and over until we get a positive ack
         */
         */
        SERIAL_WRITE (nindy_serial, buf, p - buf);
        SERIAL_WRITE (nindy_serial, buf, p - buf);
        while (1){
        while (1){
                if ( !rdnin(&ack,1,5) ){
                if ( !rdnin(&ack,1,5) ){
                        /* timed out */
                        /* timed out */
                        fprintf(stderr,"ACK timed out; resending\r\n");
                        fprintf(stderr,"ACK timed out; resending\r\n");
                        /* Attention, NINDY! */
                        /* Attention, NINDY! */
                        SERIAL_WRITE (nindy_serial, "\020", 1);
                        SERIAL_WRITE (nindy_serial, "\020", 1);
                        SERIAL_WRITE (nindy_serial, buf, p - buf);
                        SERIAL_WRITE (nindy_serial, buf, p - buf);
                } else if ( ack == '+' ){
                } else if ( ack == '+' ){
                        return;
                        return;
                } else if ( ack == '-' ){
                } else if ( ack == '-' ){
                        fprintf( stderr, "Remote NAK; resending\r\n" );
                        fprintf( stderr, "Remote NAK; resending\r\n" );
                        SERIAL_WRITE (nindy_serial, buf, p - buf);
                        SERIAL_WRITE (nindy_serial, buf, p - buf);
                } else {
                } else {
                        fprintf( stderr, "Bad ACK, ignored: <%c>\r\n", ack );
                        fprintf( stderr, "Bad ACK, ignored: <%c>\r\n", ack );
                }
                }
        }
        }
}
}
 
 
 
 
 
 
/******************************************************************************
/******************************************************************************
 * send:
 * send:
 *      Send a message to a remote NINDY.  Check message status byte
 *      Send a message to a remote NINDY.  Check message status byte
 *      for error responses.  If no error, return NINDY reponse (if any).
 *      for error responses.  If no error, return NINDY reponse (if any).
 ******************************************************************************/
 ******************************************************************************/
static
static
send( out, len, in )
send( out, len, in )
    unsigned char *out; /* Message to be sent to NINDY                  */
    unsigned char *out; /* Message to be sent to NINDY                  */
    int len;            /* Number of meaningful bytes in out buffer     */
    int len;            /* Number of meaningful bytes in out buffer     */
    unsigned char *in;  /* Where to put response received from NINDY    */
    unsigned char *in;  /* Where to put response received from NINDY    */
{
{
        char *fmt;
        char *fmt;
        int status;
        int status;
        static char *errmsg[] = {
        static char *errmsg[] = {
                "",                                             /* 0 */
                "",                                             /* 0 */
                "Buffer overflow",                              /* 1 */
                "Buffer overflow",                              /* 1 */
                "Unknown command",                              /* 2 */
                "Unknown command",                              /* 2 */
                "Wrong amount of data to load register(s)",     /* 3 */
                "Wrong amount of data to load register(s)",     /* 3 */
                "Missing command argument(s)",                  /* 4 */
                "Missing command argument(s)",                  /* 4 */
                "Odd number of digits sent to load memory",     /* 5 */
                "Odd number of digits sent to load memory",     /* 5 */
                "Unknown register name",                        /* 6 */
                "Unknown register name",                        /* 6 */
                "No such memory segment",                       /* 7 */
                "No such memory segment",                       /* 7 */
                "No breakpoint available",                      /* 8 */
                "No breakpoint available",                      /* 8 */
                "Can't set requested baud rate",                /* 9 */
                "Can't set requested baud rate",                /* 9 */
        };
        };
#       define NUMERRS  ( sizeof(errmsg) / sizeof(errmsg[0]) )
#       define NUMERRS  ( sizeof(errmsg) / sizeof(errmsg[0]) )
 
 
        static char err1[] = "Unknown error response from NINDY: #%d\r\n";
        static char err1[] = "Unknown error response from NINDY: #%d\r\n";
        static char err2[] = "Error response #%d from NINDY: %s\r\n";
        static char err2[] = "Error response #%d from NINDY: %s\r\n";
 
 
        while (1){
        while (1){
                putpkt(out,len);
                putpkt(out,len);
                status = getpkt(in);
                status = getpkt(in);
                if ( status == TIMEOUT ){
                if ( status == TIMEOUT ){
                        fprintf( stderr, "Response timed out; resending\r\n" );
                        fprintf( stderr, "Response timed out; resending\r\n" );
                } else {
                } else {
                        break;
                        break;
                }
                }
        }
        }
 
 
        if ( status ){
        if ( status ){
                fmt =  status > NUMERRS ? err1 : err2;
                fmt =  status > NUMERRS ? err1 : err2;
                fprintf( stderr, fmt, status, errmsg[status] );
                fprintf( stderr, fmt, status, errmsg[status] );
                abort();
                abort();
        }
        }
}
}


                /************************
                /************************
                 *                      *
                 *                      *
                 *  BAUD RATE ROUTINES  *
                 *  BAUD RATE ROUTINES  *
                 *                      *
                 *                      *
                 ************************/
                 ************************/
 
 
/* Table of baudrates known to be acceptable to NINDY.  Each baud rate
/* Table of baudrates known to be acceptable to NINDY.  Each baud rate
 * appears both as character string and as a Unix baud rate constant.
 * appears both as character string and as a Unix baud rate constant.
 */
 */
struct baudrate {
struct baudrate {
        char *string;
        char *string;
        int rate;
        int rate;
};
};
 
 
static struct baudrate baudtab[] = {
static struct baudrate baudtab[] = {
         "1200", 1200,
         "1200", 1200,
         "2400", 2400,
         "2400", 2400,
         "4800", 4800,
         "4800", 4800,
         "9600", 9600,
         "9600", 9600,
        "19200", 19200,
        "19200", 19200,
        "38400", 38400,
        "38400", 38400,
        NULL,    0               /* End of table */
        NULL,    0               /* End of table */
};
};
 
 
/******************************************************************************
/******************************************************************************
 * parse_baudrate:
 * parse_baudrate:
 *      Look up the passed baud rate in the baudrate table.  If found, change
 *      Look up the passed baud rate in the baudrate table.  If found, change
 *      our internal record of the current baud rate, but don't do anything
 *      our internal record of the current baud rate, but don't do anything
 *      about the tty just now.
 *      about the tty just now.
 *
 *
 *      Return pointer to baudrate structure on success, NULL on failure.
 *      Return pointer to baudrate structure on success, NULL on failure.
 ******************************************************************************/
 ******************************************************************************/
static
static
struct baudrate *
struct baudrate *
parse_baudrate(s)
parse_baudrate(s)
    char *s;    /* Desired baud rate, as an ASCII (decimal) string */
    char *s;    /* Desired baud rate, as an ASCII (decimal) string */
{
{
        int i;
        int i;
 
 
        for ( i=0; baudtab[i].string != NULL; i++ ){
        for ( i=0; baudtab[i].string != NULL; i++ ){
                if ( !strcmp(baudtab[i].string,s) ){
                if ( !strcmp(baudtab[i].string,s) ){
                        return &baudtab[i];
                        return &baudtab[i];
                }
                }
        }
        }
        return NULL;
        return NULL;
}
}
 
 
/******************************************************************************
/******************************************************************************
 * try_baudrate:
 * try_baudrate:
 *      Try speaking to NINDY via the specified file descriptor at the
 *      Try speaking to NINDY via the specified file descriptor at the
 *      specified baudrate.  Assume success if we can send an empty command
 *      specified baudrate.  Assume success if we can send an empty command
 *      with a bogus checksum and receive a NAK (response of '-') back within
 *      with a bogus checksum and receive a NAK (response of '-') back within
 *      one second.
 *      one second.
 *
 *
 *      Return 1 on success, 0 on failure.
 *      Return 1 on success, 0 on failure.
 ***************************************************************************/
 ***************************************************************************/
 
 
static int
static int
try_baudrate (serial, brp)
try_baudrate (serial, brp)
     serial_t serial;
     serial_t serial;
     struct baudrate *brp;
     struct baudrate *brp;
{
{
  unsigned char c;
  unsigned char c;
 
 
  /* Set specified baud rate and flush all pending input */
  /* Set specified baud rate and flush all pending input */
  SERIAL_SETBAUDRATE (serial, brp->rate);
  SERIAL_SETBAUDRATE (serial, brp->rate);
  tty_flush (serial);
  tty_flush (serial);
 
 
  /* Send empty command with bad checksum, hope for NAK ('-') response */
  /* Send empty command with bad checksum, hope for NAK ('-') response */
  SERIAL_WRITE (serial, "\020\0\0\001", 4);
  SERIAL_WRITE (serial, "\020\0\0\001", 4);
 
 
  /* Anything but a quick '-', including error, eof, or timeout, means that
  /* Anything but a quick '-', including error, eof, or timeout, means that
     this baudrate doesn't work.  */
     this baudrate doesn't work.  */
  return SERIAL_READCHAR (serial, 1) == '-';
  return SERIAL_READCHAR (serial, 1) == '-';
}
}
 
 
/******************************************************************************
/******************************************************************************
 * autobaud:
 * autobaud:
 *      Get NINDY talking over the specified file descriptor at the specified
 *      Get NINDY talking over the specified file descriptor at the specified
 *      baud rate.  First see if NINDY's already talking at 'baudrate'.  If
 *      baud rate.  First see if NINDY's already talking at 'baudrate'.  If
 *      not, run through all the legal baudrates in 'baudtab' until one works,
 *      not, run through all the legal baudrates in 'baudtab' until one works,
 *      and then tell NINDY to talk at 'baudrate' instead.
 *      and then tell NINDY to talk at 'baudrate' instead.
 ******************************************************************************/
 ******************************************************************************/
static
static
autobaud( serial, brp )
autobaud( serial, brp )
     serial_t serial;
     serial_t serial;
     struct baudrate *brp;
     struct baudrate *brp;
{
{
  int i;
  int i;
  int failures;
  int failures;
 
 
  say("NINDY at wrong baud rate? Trying to autobaud...\n");
  say("NINDY at wrong baud rate? Trying to autobaud...\n");
  failures = i = 0;
  failures = i = 0;
  while (1)
  while (1)
    {
    {
      say( "\r%s...   ", baudtab[i].string );
      say( "\r%s...   ", baudtab[i].string );
      if (try_baudrate(serial, &baudtab[i]))
      if (try_baudrate(serial, &baudtab[i]))
        {
        {
          break;
          break;
        }
        }
      if (baudtab[++i].string == NULL)
      if (baudtab[++i].string == NULL)
        {
        {
          /* End of table -- wraparound */
          /* End of table -- wraparound */
          i = 0;
          i = 0;
          if ( failures++ )
          if ( failures++ )
            {
            {
              say("\nAutobaud failed again.  Giving up.\n");
              say("\nAutobaud failed again.  Giving up.\n");
              exit(1);
              exit(1);
            }
            }
          else
          else
            {
            {
              say("\nAutobaud failed. Trying again...\n");
              say("\nAutobaud failed. Trying again...\n");
            }
            }
        }
        }
    }
    }
 
 
  /* Found NINDY's current baud rate; now change it.  */
  /* Found NINDY's current baud rate; now change it.  */
  say("Changing NINDY baudrate to %s\n", brp->string);
  say("Changing NINDY baudrate to %s\n", brp->string);
  ninBaud (brp->string);
  ninBaud (brp->string);
 
 
  /* Change our baud rate back to rate to which we just set NINDY.  */
  /* Change our baud rate back to rate to which we just set NINDY.  */
  SERIAL_SETBAUDRATE (serial, brp->rate);
  SERIAL_SETBAUDRATE (serial, brp->rate);
}
}


                /**********************************
                /**********************************
                 *                                *
                 *                                *
                 *   NINDY INTERFACE ROUTINES     *
                 *   NINDY INTERFACE ROUTINES     *
                 *                                *
                 *                                *
                 * ninConnect *MUST* be the first *
                 * ninConnect *MUST* be the first *
                 * one of these routines called.  *
                 * one of these routines called.  *
                 **********************************/
                 **********************************/
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninBaud:
 * ninBaud:
 *      Ask NINDY to change the baud rate on its serial port.
 *      Ask NINDY to change the baud rate on its serial port.
 *      Assumes we know the baud rate at which NINDY's currently talking.
 *      Assumes we know the baud rate at which NINDY's currently talking.
 ******************************************************************************/
 ******************************************************************************/
ninBaud( baudrate )
ninBaud( baudrate )
    char *baudrate;     /* Desired baud rate, as a string of ASCII decimal
    char *baudrate;     /* Desired baud rate, as a string of ASCII decimal
                         * digits.
                         * digits.
                         */
                         */
{
{
  unsigned char msg[100];
  unsigned char msg[100];
 
 
  tty_flush (nindy_serial);
  tty_flush (nindy_serial);
 
 
  if (old_nindy)
  if (old_nindy)
    {
    {
      char *p;          /* Pointer into buffer  */
      char *p;          /* Pointer into buffer  */
      unsigned char csum;       /* Calculated checksum  */
      unsigned char csum;       /* Calculated checksum  */
 
 
      /* Can't use putpkt() because after the baudrate change NINDY's
      /* Can't use putpkt() because after the baudrate change NINDY's
         ack/nak will look like gibberish.  */
         ack/nak will look like gibberish.  */
 
 
      for (p=baudrate, csum=020+'z'; *p; p++)
      for (p=baudrate, csum=020+'z'; *p; p++)
        {
        {
          csum += *p;
          csum += *p;
        }
        }
      sprintf (msg, "\020z%s#%02x", baudrate, csum);
      sprintf (msg, "\020z%s#%02x", baudrate, csum);
      SERIAL_WRITE (nindy_serial, msg, strlen (msg));
      SERIAL_WRITE (nindy_serial, msg, strlen (msg));
    }
    }
  else
  else
    {
    {
      /* Can't use "send" because NINDY reply will be unreadable after
      /* Can't use "send" because NINDY reply will be unreadable after
         baud rate change.  */
         baud rate change.  */
      sprintf( msg, "z%s", baudrate );
      sprintf( msg, "z%s", baudrate );
      putpkt( msg, strlen(msg)+1 );     /* "+1" to send terminator too */
      putpkt( msg, strlen(msg)+1 );     /* "+1" to send terminator too */
    }
    }
}
}
 
 
/******************************************************************************
/******************************************************************************
 * ninBptDel:
 * ninBptDel:
 *      Ask NINDY to delete the specified type of *hardware* breakpoint at
 *      Ask NINDY to delete the specified type of *hardware* breakpoint at
 *      the specified address.  If the 'addr' is -1, all breakpoints of
 *      the specified address.  If the 'addr' is -1, all breakpoints of
 *      the specified type are deleted.
 *      the specified type are deleted.
 ***************************************************************************/
 ***************************************************************************/
ninBptDel( addr, type )
ninBptDel( addr, type )
    long addr;  /* Address in 960 memory        */
    long addr;  /* Address in 960 memory        */
    char type;  /* 'd' => data bkpt, 'i' => instruction breakpoint */
    char type;  /* 'd' => data bkpt, 'i' => instruction breakpoint */
{
{
        unsigned char buf[10];
        unsigned char buf[10];
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninBptDel( addr, type == 'd' ? 1 : 0 );
                OninBptDel( addr, type == 'd' ? 1 : 0 );
                return;
                return;
        }
        }
 
 
        buf[0] = 'b';
        buf[0] = 'b';
        buf[1] = type;
        buf[1] = type;
 
 
        if ( addr == -1 ){
        if ( addr == -1 ){
                send( buf, 2, NULL );
                send( buf, 2, NULL );
        } else {
        } else {
                store_unsigned_integer (&buf[2], 4, addr);
                store_unsigned_integer (&buf[2], 4, addr);
                send( buf, 6, NULL );
                send( buf, 6, NULL );
        }
        }
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninBptSet:
 * ninBptSet:
 *      Ask NINDY to set the specified type of *hardware* breakpoint at
 *      Ask NINDY to set the specified type of *hardware* breakpoint at
 *      the specified address.
 *      the specified address.
 ******************************************************************************/
 ******************************************************************************/
ninBptSet( addr, type )
ninBptSet( addr, type )
    long addr;  /* Address in 960 memory        */
    long addr;  /* Address in 960 memory        */
    char type;  /* 'd' => data bkpt, 'i' => instruction breakpoint */
    char type;  /* 'd' => data bkpt, 'i' => instruction breakpoint */
{
{
        unsigned char buf[10];
        unsigned char buf[10];
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninBptSet( addr, type == 'd' ? 1 : 0 );
                OninBptSet( addr, type == 'd' ? 1 : 0 );
                return;
                return;
        }
        }
 
 
 
 
        buf[0] = 'B';
        buf[0] = 'B';
        buf[1] = type;
        buf[1] = type;
        store_unsigned_integer (&buf[2], 4, addr);
        store_unsigned_integer (&buf[2], 4, addr);
        send( buf, 6, NULL );
        send( buf, 6, NULL );
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninConnect:
 * ninConnect:
 *      Open the specified tty.  Get communications working at the specified
 *      Open the specified tty.  Get communications working at the specified
 *      baud rate.  Flush any pending I/O on the tty.
 *      baud rate.  Flush any pending I/O on the tty.
 *
 *
 *      Return the file descriptor, or -1 on failure.
 *      Return the file descriptor, or -1 on failure.
 ******************************************************************************/
 ******************************************************************************/
int
int
ninConnect( name, baudrate, brk, silent, old_protocol )
ninConnect( name, baudrate, brk, silent, old_protocol )
    char *name;         /* "/dev/ttyXX" to be opened                    */
    char *name;         /* "/dev/ttyXX" to be opened                    */
    char *baudrate;/* baud rate: a string of ascii decimal digits (eg,"9600")*/
    char *baudrate;/* baud rate: a string of ascii decimal digits (eg,"9600")*/
    int brk;            /* 1 => send break to tty first thing after opening it*/
    int brk;            /* 1 => send break to tty first thing after opening it*/
    int silent;         /* 1 => stifle unnecessary messages when talking to
    int silent;         /* 1 => stifle unnecessary messages when talking to
                         *      this tty.
                         *      this tty.
                         */
                         */
    int old_protocol;
    int old_protocol;
{
{
        int i;
        int i;
        char *p;
        char *p;
        struct baudrate *brp;
        struct baudrate *brp;
 
 
        /* We will try each of the following paths when trying to open the tty
        /* We will try each of the following paths when trying to open the tty
         */
         */
        static char *prefix[] = { "", "/dev/", "/dev/tty", NULL };
        static char *prefix[] = { "", "/dev/", "/dev/tty", NULL };
 
 
        if ( old_protocol ){
        if ( old_protocol ){
                old_nindy = 1;
                old_nindy = 1;
        }
        }
 
 
        quiet = silent;         /* Make global to this file */
        quiet = silent;         /* Make global to this file */
 
 
        for ( i=0; prefix[i] != NULL; i++ ){
        for ( i=0; prefix[i] != NULL; i++ ){
                p = xmalloc(strlen(prefix[i]) + strlen(name) + 1 );
                p = xmalloc(strlen(prefix[i]) + strlen(name) + 1 );
                strcpy( p, prefix[i] );
                strcpy( p, prefix[i] );
                strcat( p, name );
                strcat( p, name );
                nindy_serial = SERIAL_OPEN (p);
                nindy_serial = SERIAL_OPEN (p);
                if (nindy_serial != NULL) {
                if (nindy_serial != NULL) {
#ifdef TIOCEXCL
#ifdef TIOCEXCL
                        /* Exclusive use mode (hp9000 does not support it) */
                        /* Exclusive use mode (hp9000 does not support it) */
                        ioctl(nindy_serial->fd,TIOCEXCL,NULL);
                        ioctl(nindy_serial->fd,TIOCEXCL,NULL);
#endif
#endif
                        SERIAL_RAW (nindy_serial);
                        SERIAL_RAW (nindy_serial);
 
 
                        if (brk)
                        if (brk)
                          {
                          {
                            SERIAL_SEND_BREAK (nindy_serial);
                            SERIAL_SEND_BREAK (nindy_serial);
                          }
                          }
 
 
                        brp = parse_baudrate( baudrate );
                        brp = parse_baudrate( baudrate );
                        if ( brp == NULL ){
                        if ( brp == NULL ){
                                say("Illegal baudrate %s ignored; using 9600\n",
                                say("Illegal baudrate %s ignored; using 9600\n",
                                                                baudrate);
                                                                baudrate);
                                brp = parse_baudrate( "9600" );
                                brp = parse_baudrate( "9600" );
                        }
                        }
 
 
                        if ( !try_baudrate(nindy_serial, brp) ){
                        if ( !try_baudrate(nindy_serial, brp) ){
                                autobaud(nindy_serial, brp);
                                autobaud(nindy_serial, brp);
                        }
                        }
                        tty_flush (nindy_serial);
                        tty_flush (nindy_serial);
                        say( "Connected to %s\n", p );
                        say( "Connected to %s\n", p );
                        free(p);
                        free(p);
                        break;
                        break;
                }
                }
                free(p);
                free(p);
        }
        }
        return 0;
        return 0;
}
}
 
 
#if 0
#if 0
 
 
/* Currently unused; shouldn't we be doing this on target_kill and
/* Currently unused; shouldn't we be doing this on target_kill and
perhaps target_mourn?  FIXME.  */
perhaps target_mourn?  FIXME.  */
 
 
/******************************************************************************
/******************************************************************************
 * ninGdbExit:
 * ninGdbExit:
 *      Ask NINDY to leave GDB mode and print a NINDY prompt.
 *      Ask NINDY to leave GDB mode and print a NINDY prompt.
 ****************************************************************************/
 ****************************************************************************/
ninGdbExit()
ninGdbExit()
{
{
        if ( old_nindy ){
        if ( old_nindy ){
                OninGdbExit();
                OninGdbExit();
                return;
                return;
        }
        }
        putpkt((unsigned char *) "E", 1 );
        putpkt((unsigned char *) "E", 1 );
}
}
#endif
#endif
 
 
/******************************************************************************
/******************************************************************************
 * ninGo:
 * ninGo:
 *      Ask NINDY to start or continue execution of an application program
 *      Ask NINDY to start or continue execution of an application program
 *      in it's memory at the current ip.
 *      in it's memory at the current ip.
 ******************************************************************************/
 ******************************************************************************/
ninGo( step_flag )
ninGo( step_flag )
    int step_flag;      /* 1 => run in single-step mode */
    int step_flag;      /* 1 => run in single-step mode */
{
{
        if ( old_nindy ){
        if ( old_nindy ){
                OninGo( step_flag );
                OninGo( step_flag );
                return;
                return;
        }
        }
        putpkt((unsigned char *) (step_flag ? "s" : "c"), 1 );
        putpkt((unsigned char *) (step_flag ? "s" : "c"), 1 );
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninMemGet:
 * ninMemGet:
 *      Read a string of bytes from NINDY's address space (960 memory).
 *      Read a string of bytes from NINDY's address space (960 memory).
 ******************************************************************************/
 ******************************************************************************/
int
int
ninMemGet(ninaddr, hostaddr, len)
ninMemGet(ninaddr, hostaddr, len)
     long ninaddr;      /* Source address, in the 960 memory space      */
     long ninaddr;      /* Source address, in the 960 memory space      */
     unsigned char *hostaddr;   /* Destination address, in our memory space */
     unsigned char *hostaddr;   /* Destination address, in our memory space */
     int len;           /* Number of bytes to read                      */
     int len;           /* Number of bytes to read                      */
{
{
        unsigned char buf[BUFSIZE+20];
        unsigned char buf[BUFSIZE+20];
        int cnt;                /* Number of bytes in next transfer     */
        int cnt;                /* Number of bytes in next transfer     */
        int origlen = len;
        int origlen = len;
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninMemGet(ninaddr, hostaddr, len);
                OninMemGet(ninaddr, hostaddr, len);
                return;
                return;
        }
        }
 
 
        for ( ; len > 0; len -= BUFSIZE ){
        for ( ; len > 0; len -= BUFSIZE ){
                cnt = len > BUFSIZE ? BUFSIZE : len;
                cnt = len > BUFSIZE ? BUFSIZE : len;
 
 
                buf[0] = 'm';
                buf[0] = 'm';
                store_unsigned_integer (&buf[1], 4, ninaddr);
                store_unsigned_integer (&buf[1], 4, ninaddr);
                buf[5] = cnt & 0xff;
                buf[5] = cnt & 0xff;
                buf[6] = (cnt>>8) & 0xff;
                buf[6] = (cnt>>8) & 0xff;
 
 
                send( buf, 7, hostaddr );
                send( buf, 7, hostaddr );
 
 
                ninaddr += cnt;
                ninaddr += cnt;
                hostaddr += cnt;
                hostaddr += cnt;
        }
        }
        return origlen;
        return origlen;
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninMemPut:
 * ninMemPut:
 *      Write a string of bytes into NINDY's address space (960 memory).
 *      Write a string of bytes into NINDY's address space (960 memory).
 ******************************************************************************/
 ******************************************************************************/
int
int
ninMemPut( ninaddr, hostaddr, len )
ninMemPut( ninaddr, hostaddr, len )
     long ninaddr;      /* Destination address, in NINDY memory space   */
     long ninaddr;      /* Destination address, in NINDY memory space   */
     unsigned char *hostaddr;   /* Source address, in our memory space  */
     unsigned char *hostaddr;   /* Source address, in our memory space  */
     int len;           /* Number of bytes to write                     */
     int len;           /* Number of bytes to write                     */
{
{
        unsigned char buf[BUFSIZE+20];
        unsigned char buf[BUFSIZE+20];
        int cnt;                /* Number of bytes in next transfer     */
        int cnt;                /* Number of bytes in next transfer     */
        int origlen = len;
        int origlen = len;
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninMemPut( ninaddr, hostaddr, len );
                OninMemPut( ninaddr, hostaddr, len );
                return;
                return;
        }
        }
        for ( ; len > 0; len -= BUFSIZE ){
        for ( ; len > 0; len -= BUFSIZE ){
                cnt = len > BUFSIZE ? BUFSIZE : len;
                cnt = len > BUFSIZE ? BUFSIZE : len;
 
 
                buf[0] = 'M';
                buf[0] = 'M';
                store_unsigned_integer (&buf[1], 4, ninaddr);
                store_unsigned_integer (&buf[1], 4, ninaddr);
                memcpy(buf + 5, hostaddr, cnt);
                memcpy(buf + 5, hostaddr, cnt);
                send( buf, cnt+5, NULL );
                send( buf, cnt+5, NULL );
 
 
                ninaddr += cnt;
                ninaddr += cnt;
                hostaddr += cnt;
                hostaddr += cnt;
        }
        }
        return origlen;
        return origlen;
}
}
 
 
/******************************************************************************
/******************************************************************************
 * ninRegGet:
 * ninRegGet:
 *      Retrieve the contents of a 960 register, and return them as a long
 *      Retrieve the contents of a 960 register, and return them as a long
 *      in host byte order.
 *      in host byte order.
 *
 *
 *      THIS ROUTINE CAN ONLY BE USED TO READ THE LOCAL, GLOBAL, AND
 *      THIS ROUTINE CAN ONLY BE USED TO READ THE LOCAL, GLOBAL, AND
 *      ip/ac/pc/tc REGISTERS.
 *      ip/ac/pc/tc REGISTERS.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
long
long
ninRegGet( regname )
ninRegGet( regname )
    char *regname;      /* Register name recognized by NINDY, subject to the
    char *regname;      /* Register name recognized by NINDY, subject to the
                         * above limitations.
                         * above limitations.
                         */
                         */
{
{
        unsigned char outbuf[10];
        unsigned char outbuf[10];
        unsigned char inbuf[20];
        unsigned char inbuf[20];
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                return OninRegGet( regname );
                return OninRegGet( regname );
        }
        }
 
 
        sprintf( outbuf, "u%s:", regname );
        sprintf( outbuf, "u%s:", regname );
        send( outbuf, strlen(outbuf), inbuf );
        send( outbuf, strlen(outbuf), inbuf );
        return extract_unsigned_integer (inbuf, 4);
        return extract_unsigned_integer (inbuf, 4);
}
}
 
 
/******************************************************************************
/******************************************************************************
 * ninRegPut:
 * ninRegPut:
 *      Set the contents of a 960 register.
 *      Set the contents of a 960 register.
 *
 *
 *      THIS ROUTINE CAN ONLY BE USED TO SET THE LOCAL, GLOBAL, AND
 *      THIS ROUTINE CAN ONLY BE USED TO SET THE LOCAL, GLOBAL, AND
 *      ip/ac/pc/tc REGISTERS.
 *      ip/ac/pc/tc REGISTERS.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
ninRegPut( regname, val )
ninRegPut( regname, val )
    char *regname;      /* Register name recognized by NINDY, subject to the
    char *regname;      /* Register name recognized by NINDY, subject to the
                         * above limitations.
                         * above limitations.
                         */
                         */
    long val;           /* New contents of register, in host byte-order */
    long val;           /* New contents of register, in host byte-order */
{
{
        unsigned char buf[20];
        unsigned char buf[20];
        int len;
        int len;
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninRegPut( regname, val );
                OninRegPut( regname, val );
                return;
                return;
        }
        }
 
 
        sprintf( buf, "U%s:", regname );
        sprintf( buf, "U%s:", regname );
        len = strlen(buf);
        len = strlen(buf);
        store_unsigned_integer (&buf[len], 4, val);
        store_unsigned_integer (&buf[len], 4, val);
        send( buf, len+4, NULL );
        send( buf, len+4, NULL );
}
}
 
 
/******************************************************************************
/******************************************************************************
 * ninRegsGet:
 * ninRegsGet:
 *      Get a dump of the contents of the entire 960 register set.  The
 *      Get a dump of the contents of the entire 960 register set.  The
 *      individual registers appear in the dump in the following order:
 *      individual registers appear in the dump in the following order:
 *
 *
 *              pfp  sp   rip  r3   r4   r5   r6   r7
 *              pfp  sp   rip  r3   r4   r5   r6   r7
 *              r8   r9   r10  r11  r12  r13  r14  r15
 *              r8   r9   r10  r11  r12  r13  r14  r15
 *              g0   g1   g2   g3   g4   g5   g6   g7
 *              g0   g1   g2   g3   g4   g5   g6   g7
 *              g8   g9   g10  g11  g12  g13  g14  fp
 *              g8   g9   g10  g11  g12  g13  g14  fp
 *              pc   ac   ip   tc   fp0  fp1  fp2  fp3
 *              pc   ac   ip   tc   fp0  fp1  fp2  fp3
 *
 *
 *      Each individual register comprises exactly 4 bytes, except for
 *      Each individual register comprises exactly 4 bytes, except for
 *      fp0-fp3, which are 8 bytes.  All register values are in 960
 *      fp0-fp3, which are 8 bytes.  All register values are in 960
 *      (little-endian) byte order.
 *      (little-endian) byte order.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
ninRegsGet( regp )
ninRegsGet( regp )
    unsigned char *regp;                /* Where to place the register dump */
    unsigned char *regp;                /* Where to place the register dump */
{
{
        if ( old_nindy ){
        if ( old_nindy ){
                OninRegsGet( regp );
                OninRegsGet( regp );
                return;
                return;
        }
        }
        send( (unsigned char *) "r", 1, regp );
        send( (unsigned char *) "r", 1, regp );
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninRegsPut:
 * ninRegsPut:
 *      Initialize the entire 960 register set to a specified set of values.
 *      Initialize the entire 960 register set to a specified set of values.
 *      The format of the register value data should be the same as that
 *      The format of the register value data should be the same as that
 *      returned by ninRegsGet.
 *      returned by ninRegsGet.
 *
 *
 * WARNING:
 * WARNING:
 *      All register values must be in 960 (little-endian) byte order.
 *      All register values must be in 960 (little-endian) byte order.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
ninRegsPut( regp )
ninRegsPut( regp )
    char *regp;         /* Pointer to desired values of registers */
    char *regp;         /* Pointer to desired values of registers */
{
{
/* Number of bytes that we send to nindy.  I believe this is defined by
/* Number of bytes that we send to nindy.  I believe this is defined by
   the protocol (it does not agree with REGISTER_BYTES).  */
   the protocol (it does not agree with REGISTER_BYTES).  */
#define NINDY_REGISTER_BYTES    ((36*4) + (4*8))
#define NINDY_REGISTER_BYTES    ((36*4) + (4*8))
        unsigned char buf[NINDY_REGISTER_BYTES+10];
        unsigned char buf[NINDY_REGISTER_BYTES+10];
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninRegsPut( regp );
                OninRegsPut( regp );
                return;
                return;
        }
        }
 
 
        buf[0] = 'R';
        buf[0] = 'R';
        memcpy(buf+1,  regp, NINDY_REGISTER_BYTES );
        memcpy(buf+1,  regp, NINDY_REGISTER_BYTES );
        send( buf, NINDY_REGISTER_BYTES+1, NULL );
        send( buf, NINDY_REGISTER_BYTES+1, NULL );
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninReset:
 * ninReset:
 *      Ask NINDY to perform a soft reset; wait for the reset to complete.
 *      Ask NINDY to perform a soft reset; wait for the reset to complete.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
ninReset()
ninReset()
{
{
        unsigned char ack;
        unsigned char ack;
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninReset();
                OninReset();
                return;
                return;
        }
        }
 
 
        while (1){
        while (1){
                putpkt((unsigned char *) "X", 1 );
                putpkt((unsigned char *) "X", 1 );
                while (1){
                while (1){
                        if ( !rdnin(&ack,1,5) ){
                        if ( !rdnin(&ack,1,5) ){
                                /* Timed out */
                                /* Timed out */
                                break;          /* Resend */
                                break;          /* Resend */
                        }
                        }
                        if ( ack == '+' ){
                        if ( ack == '+' ){
                                return;
                                return;
                        }
                        }
                }
                }
        }
        }
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninSrq:
 * ninSrq:
 *      Assume NINDY has stopped execution of the 960 application program in
 *      Assume NINDY has stopped execution of the 960 application program in
 *      order to process a host service request (srq).  Ask NINDY for the
 *      order to process a host service request (srq).  Ask NINDY for the
 *      srq arguments, perform the requested service, and send an "srq
 *      srq arguments, perform the requested service, and send an "srq
 *      complete" message so NINDY will return control to the application.
 *      complete" message so NINDY will return control to the application.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
ninSrq()
ninSrq()
{
{
  /* FIXME: Imposes arbitrary limits on lengths of pathnames and such.  */
  /* FIXME: Imposes arbitrary limits on lengths of pathnames and such.  */
        unsigned char buf[BUFSIZE];
        unsigned char buf[BUFSIZE];
        int retcode;
        int retcode;
        unsigned char srqnum;
        unsigned char srqnum;
        int i;
        int i;
        int offset;
        int offset;
        int arg[MAX_SRQ_ARGS];
        int arg[MAX_SRQ_ARGS];
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                OninSrq();
                OninSrq();
                return;
                return;
        }
        }
 
 
 
 
        /* Get srq number and arguments
        /* Get srq number and arguments
         */
         */
        send((unsigned char *) "!", 1, buf );
        send((unsigned char *) "!", 1, buf );
 
 
        srqnum = buf[0];
        srqnum = buf[0];
        for  ( i=0, offset=1; i < MAX_SRQ_ARGS; i++, offset+=4 ){
        for  ( i=0, offset=1; i < MAX_SRQ_ARGS; i++, offset+=4 ){
                arg[i] = extract_unsigned_integer (&buf[offset], 4);
                arg[i] = extract_unsigned_integer (&buf[offset], 4);
        }
        }
 
 
        /* Process Srq
        /* Process Srq
         */
         */
        switch( srqnum ){
        switch( srqnum ){
        case BS_CLOSE:
        case BS_CLOSE:
                /* args: file descriptor */
                /* args: file descriptor */
                if ( arg[0] > 2 ){
                if ( arg[0] > 2 ){
                        retcode = close( arg[0] );
                        retcode = close( arg[0] );
                } else {
                } else {
                        retcode = 0;
                        retcode = 0;
                }
                }
                break;
                break;
        case BS_CREAT:
        case BS_CREAT:
                /* args: filename, mode */
                /* args: filename, mode */
                ninStrGet( arg[0], buf );
                ninStrGet( arg[0], buf );
                retcode = creat(buf,arg[1]);
                retcode = creat(buf,arg[1]);
                break;
                break;
        case BS_OPEN:
        case BS_OPEN:
                /* args: filename, flags, mode */
                /* args: filename, flags, mode */
                ninStrGet( arg[0], buf );
                ninStrGet( arg[0], buf );
                retcode = open(buf,arg[1],arg[2]);
                retcode = open(buf,arg[1],arg[2]);
                break;
                break;
        case BS_READ:
        case BS_READ:
                /* args: file descriptor, buffer, count */
                /* args: file descriptor, buffer, count */
                retcode = read(arg[0],buf,arg[2]);
                retcode = read(arg[0],buf,arg[2]);
                if ( retcode > 0 ){
                if ( retcode > 0 ){
                        ninMemPut( arg[1], buf, retcode );
                        ninMemPut( arg[1], buf, retcode );
                }
                }
                break;
                break;
        case BS_SEEK:
        case BS_SEEK:
                /* args: file descriptor, offset, whence */
                /* args: file descriptor, offset, whence */
                retcode = lseek(arg[0],arg[1],arg[2]);
                retcode = lseek(arg[0],arg[1],arg[2]);
                break;
                break;
        case BS_WRITE:
        case BS_WRITE:
                /* args: file descriptor, buffer, count */
                /* args: file descriptor, buffer, count */
                ninMemGet( arg[1], buf, arg[2] );
                ninMemGet( arg[1], buf, arg[2] );
                retcode = write(arg[0],buf,arg[2]);
                retcode = write(arg[0],buf,arg[2]);
                break;
                break;
        default:
        default:
                retcode = -1;
                retcode = -1;
                break;
                break;
        }
        }
 
 
        /* Send request termination status to NINDY
        /* Send request termination status to NINDY
         */
         */
        buf[0] = 'e';
        buf[0] = 'e';
        store_unsigned_integer (&buf[1], 4, retcode);
        store_unsigned_integer (&buf[1], 4, retcode);
        send( buf, 5, NULL );
        send( buf, 5, NULL );
}
}
 
 
 
 
/******************************************************************************
/******************************************************************************
 * ninStopWhy:
 * ninStopWhy:
 *      Assume the application program has stopped (i.e., a DLE was received
 *      Assume the application program has stopped (i.e., a DLE was received
 *      from NINDY).  Ask NINDY for status information describing the
 *      from NINDY).  Ask NINDY for status information describing the
 *      reason for the halt.
 *      reason for the halt.
 *
 *
 *      Returns a non-zero value if the user program has exited, 0 otherwise.
 *      Returns a non-zero value if the user program has exited, 0 otherwise.
 *      Also returns the following information, through passed pointers:
 *      Also returns the following information, through passed pointers:
 *           - why: an exit code if program the exited; otherwise the reason
 *           - why: an exit code if program the exited; otherwise the reason
 *                      why the program halted (see stop.h for values).
 *                      why the program halted (see stop.h for values).
 *          - contents of register ip (little-endian byte order)
 *          - contents of register ip (little-endian byte order)
 *          - contents of register sp (little-endian byte order)
 *          - contents of register sp (little-endian byte order)
 *          - contents of register fp (little-endian byte order)
 *          - contents of register fp (little-endian byte order)
 ******************************************************************************/
 ******************************************************************************/
char
char
ninStopWhy( whyp, ipp, fpp, spp )
ninStopWhy( whyp, ipp, fpp, spp )
    unsigned char *whyp; /* Return the 'why' code through this pointer  */
    unsigned char *whyp; /* Return the 'why' code through this pointer  */
    long *ipp;  /* Return contents of register ip through this pointer  */
    long *ipp;  /* Return contents of register ip through this pointer  */
    long *fpp;  /* Return contents of register fp through this pointer  */
    long *fpp;  /* Return contents of register fp through this pointer  */
    long *spp;  /* Return contents of register sp through this pointer  */
    long *spp;  /* Return contents of register sp through this pointer  */
{
{
        unsigned char buf[30];
        unsigned char buf[30];
        extern char OninStopWhy ();
        extern char OninStopWhy ();
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                return OninStopWhy( whyp, ipp, fpp, spp );
                return OninStopWhy( whyp, ipp, fpp, spp );
        }
        }
        send((unsigned char *) "?", 1, buf );
        send((unsigned char *) "?", 1, buf );
 
 
        *whyp = buf[1];
        *whyp = buf[1];
        memcpy ((char *)ipp, &buf[2],  sizeof (*ipp));
        memcpy ((char *)ipp, &buf[2],  sizeof (*ipp));
        memcpy ((char *)fpp, &buf[6],  sizeof (*ipp));
        memcpy ((char *)fpp, &buf[6],  sizeof (*ipp));
        memcpy ((char *)spp, &buf[10], sizeof (*ipp));
        memcpy ((char *)spp, &buf[10], sizeof (*ipp));
        return buf[0];
        return buf[0];
}
}
 
 
/******************************************************************************
/******************************************************************************
 * ninStrGet:
 * ninStrGet:
 *      Read a '\0'-terminated string of data out of the 960 memory space.
 *      Read a '\0'-terminated string of data out of the 960 memory space.
 *
 *
 ******************************************************************************/
 ******************************************************************************/
static
static
ninStrGet( ninaddr, hostaddr )
ninStrGet( ninaddr, hostaddr )
     unsigned long ninaddr;     /* Address of string in NINDY memory space */
     unsigned long ninaddr;     /* Address of string in NINDY memory space */
     unsigned char *hostaddr;   /* Address of the buffer to which string should
     unsigned char *hostaddr;   /* Address of the buffer to which string should
                                 *      be copied.
                                 *      be copied.
                                 */
                                 */
{
{
        unsigned char cmd[5];
        unsigned char cmd[5];
 
 
        cmd[0] = '"';
        cmd[0] = '"';
        store_unsigned_integer (&cmd[1], 4, ninaddr);
        store_unsigned_integer (&cmd[1], 4, ninaddr);
        send( cmd, 5, hostaddr );
        send( cmd, 5, hostaddr );
}
}
 
 
#if 0
#if 0
/* Not used.  */
/* Not used.  */
 
 
/******************************************************************************
/******************************************************************************
 * ninVersion:
 * ninVersion:
 *      Ask NINDY for version information about itself.
 *      Ask NINDY for version information about itself.
 *      The information is sent as an ascii string in the form "x.xx,<arch>",
 *      The information is sent as an ascii string in the form "x.xx,<arch>",
 *      where,
 *      where,
 *              x.xx    is the version number
 *              x.xx    is the version number
 *              <arch>  is the processor architecture: "KA", "KB", "MC", "CA" *
 *              <arch>  is the processor architecture: "KA", "KB", "MC", "CA" *
 *
 *
 ******************************************************************************/
 ******************************************************************************/
int
int
ninVersion( p )
ninVersion( p )
     unsigned char *p;          /* Where to place version string */
     unsigned char *p;          /* Where to place version string */
{
{
 
 
        if ( old_nindy ){
        if ( old_nindy ){
                return OninVersion( p );
                return OninVersion( p );
        }
        }
        send((unsigned char *) "v", 1, p );
        send((unsigned char *) "v", 1, p );
        return strlen(p);
        return strlen(p);
}
}
#endif /* 0 */
#endif /* 0 */
 
 

powered by: WebSVN 2.1.0

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