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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [sim/] [arm/] [communicate.c] - Diff between revs 827 and 840

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

Rev 827 Rev 840
/*  communicate.c -- ARMulator RDP comms code:  ARM6 Instruction Emulator.
/*  communicate.c -- ARMulator RDP comms code:  ARM6 Instruction Emulator.
    Copyright (C) 1994 Advanced RISC Machines Ltd.
    Copyright (C) 1994 Advanced RISC Machines Ltd.
 
 
    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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
 
 
/**************************************************************************/
/**************************************************************************/
/* Functions to read and write characters or groups of characters         */
/* Functions to read and write characters or groups of characters         */
/* down sockets or pipes.  Those that return a value return -1 on failure */
/* down sockets or pipes.  Those that return a value return -1 on failure */
/* and 0 on success.                                                      */
/* and 0 on success.                                                      */
/**************************************************************************/
/**************************************************************************/
 
 
#include <sys/time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
 
 
#include "armdefs.h"
#include "armdefs.h"
 
 
/* The socket to the debugger */
/* The socket to the debugger */
int debugsock;
int debugsock;
 
 
/* The maximum number of file descriptors */
/* The maximum number of file descriptors */
extern int nfds;
extern int nfds;
 
 
/* The socket handle */
/* The socket handle */
extern int sockethandle;
extern int sockethandle;
 
 
/* Read and Write routines down a pipe or socket */
/* Read and Write routines down a pipe or socket */
 
 
/****************************************************************/
/****************************************************************/
/* Read an individual character.                                */
/* Read an individual character.                                */
/* All other read functions rely on this one.                   */
/* All other read functions rely on this one.                   */
/* It waits 15 seconds until there is a character available: if */
/* It waits 15 seconds until there is a character available: if */
/* no character is available, then it timeouts and returns -1.  */
/* no character is available, then it timeouts and returns -1.  */
/****************************************************************/
/****************************************************************/
int
int
MYread_char (int sock, unsigned char *c)
MYread_char (int sock, unsigned char *c)
{
{
  int i;
  int i;
  fd_set readfds;
  fd_set readfds;
  struct timeval timeout = { 15, 0 };
  struct timeval timeout = { 15, 0 };
  struct sockaddr_in isa;
  struct sockaddr_in isa;
 
 
retry:
retry:
 
 
  FD_ZERO (&readfds);
  FD_ZERO (&readfds);
  FD_SET (sock, &readfds);
  FD_SET (sock, &readfds);
 
 
  i = select (nfds, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
  i = select (nfds, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
 
 
  if (i < 0)
  if (i < 0)
    {
    {
      perror ("select");
      perror ("select");
      exit (1);
      exit (1);
    }
    }
 
 
  if (!i)
  if (!i)
    {
    {
      fprintf (stderr, "read: Timeout\n");
      fprintf (stderr, "read: Timeout\n");
      return -1;
      return -1;
    }
    }
 
 
  if ((i = read (sock, c, 1)) < 1)
  if ((i = read (sock, c, 1)) < 1)
    {
    {
      if (!i && sock == debugsock)
      if (!i && sock == debugsock)
        {
        {
          fprintf (stderr, "Connection with debugger severed.\n");
          fprintf (stderr, "Connection with debugger severed.\n");
          /* This shouldn't be necessary for a detached armulator, but
          /* This shouldn't be necessary for a detached armulator, but
             the armulator cannot be cold started a second time, so
             the armulator cannot be cold started a second time, so
             this is probably preferable to locking up.  */
             this is probably preferable to locking up.  */
          return -1;
          return -1;
          fprintf (stderr, "Waiting for connection from debugger...");
          fprintf (stderr, "Waiting for connection from debugger...");
          debugsock = accept (sockethandle, &isa, &i);
          debugsock = accept (sockethandle, &isa, &i);
          if (debugsock < 0)
          if (debugsock < 0)
            {                   /* Now we are in serious trouble... */
            {                   /* Now we are in serious trouble... */
              perror ("accept");
              perror ("accept");
              return -1;
              return -1;
            }
            }
          fprintf (stderr, " done.\nConnection Established.\n");
          fprintf (stderr, " done.\nConnection Established.\n");
          sock = debugsock;
          sock = debugsock;
          goto retry;
          goto retry;
        }
        }
      perror ("read");
      perror ("read");
      return -1;
      return -1;
    }
    }
 
 
#ifdef DEBUG
#ifdef DEBUG
  if (sock == debugsock)
  if (sock == debugsock)
    fprintf (stderr, "<%02x ", *c);
    fprintf (stderr, "<%02x ", *c);
#endif
#endif
 
 
  return 0;
  return 0;
}
}
 
 
/****************************************************************/
/****************************************************************/
/* Read an individual character.                                */
/* Read an individual character.                                */
/* It waits until there is a character available. Returns -1 if */
/* It waits until there is a character available. Returns -1 if */
/* an error occurs.                                             */
/* an error occurs.                                             */
/****************************************************************/
/****************************************************************/
int
int
MYread_charwait (int sock, unsigned char *c)
MYread_charwait (int sock, unsigned char *c)
{
{
  int i;
  int i;
  fd_set readfds;
  fd_set readfds;
  struct sockaddr_in isa;
  struct sockaddr_in isa;
 
 
retry:
retry:
 
 
  FD_ZERO (&readfds);
  FD_ZERO (&readfds);
  FD_SET (sock, &readfds);
  FD_SET (sock, &readfds);
 
 
  i = select (nfds, &readfds,
  i = select (nfds, &readfds,
              (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
              (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
 
 
  if (i < 0)
  if (i < 0)
    {
    {
      perror ("select");
      perror ("select");
      exit (-1);
      exit (-1);
    }
    }
 
 
  if ((i = read (sock, c, 1)) < 1)
  if ((i = read (sock, c, 1)) < 1)
    {
    {
      if (!i && sock == debugsock)
      if (!i && sock == debugsock)
        {
        {
          fprintf (stderr, "Connection with debugger severed.\n");
          fprintf (stderr, "Connection with debugger severed.\n");
          return -1;
          return -1;
          fprintf (stderr, "Waiting for connection from debugger...");
          fprintf (stderr, "Waiting for connection from debugger...");
          debugsock = accept (sockethandle, &isa, &i);
          debugsock = accept (sockethandle, &isa, &i);
          if (debugsock < 0)
          if (debugsock < 0)
            {                   /* Now we are in serious trouble... */
            {                   /* Now we are in serious trouble... */
              perror ("accept");
              perror ("accept");
              return -1;
              return -1;
            }
            }
          fprintf (stderr, " done.\nConnection Established.\n");
          fprintf (stderr, " done.\nConnection Established.\n");
          sock = debugsock;
          sock = debugsock;
          goto retry;
          goto retry;
        }
        }
      perror ("read");
      perror ("read");
      return -1;
      return -1;
    }
    }
 
 
#ifdef DEBUG
#ifdef DEBUG
  if (sock == debugsock)
  if (sock == debugsock)
    fprintf (stderr, "<%02x ", *c);
    fprintf (stderr, "<%02x ", *c);
#endif
#endif
 
 
  return 0;
  return 0;
}
}
 
 
void
void
MYwrite_char (int sock, unsigned char c)
MYwrite_char (int sock, unsigned char c)
{
{
 
 
  if (write (sock, &c, 1) < 1)
  if (write (sock, &c, 1) < 1)
    perror ("write");
    perror ("write");
#ifdef DEBUG
#ifdef DEBUG
  if (sock == debugsock)
  if (sock == debugsock)
    fprintf (stderr, ">%02x ", c);
    fprintf (stderr, ">%02x ", c);
#endif
#endif
}
}
 
 
int
int
MYread_word (int sock, ARMword * here)
MYread_word (int sock, ARMword * here)
{
{
  unsigned char a, b, c, d;
  unsigned char a, b, c, d;
 
 
  if (MYread_char (sock, &a) < 0)
  if (MYread_char (sock, &a) < 0)
    return -1;
    return -1;
  if (MYread_char (sock, &b) < 0)
  if (MYread_char (sock, &b) < 0)
    return -1;
    return -1;
  if (MYread_char (sock, &c) < 0)
  if (MYread_char (sock, &c) < 0)
    return -1;
    return -1;
  if (MYread_char (sock, &d) < 0)
  if (MYread_char (sock, &d) < 0)
    return -1;
    return -1;
  *here = a | b << 8 | c << 16 | d << 24;
  *here = a | b << 8 | c << 16 | d << 24;
  return 0;
  return 0;
}
}
 
 
void
void
MYwrite_word (int sock, ARMword i)
MYwrite_word (int sock, ARMword i)
{
{
  MYwrite_char (sock, i & 0xff);
  MYwrite_char (sock, i & 0xff);
  MYwrite_char (sock, (i & 0xff00) >> 8);
  MYwrite_char (sock, (i & 0xff00) >> 8);
  MYwrite_char (sock, (i & 0xff0000) >> 16);
  MYwrite_char (sock, (i & 0xff0000) >> 16);
  MYwrite_char (sock, (i & 0xff000000) >> 24);
  MYwrite_char (sock, (i & 0xff000000) >> 24);
}
}
 
 
void
void
MYwrite_string (int sock, char *s)
MYwrite_string (int sock, char *s)
{
{
  int i;
  int i;
  for (i = 0; MYwrite_char (sock, s[i]), s[i]; i++);
  for (i = 0; MYwrite_char (sock, s[i]), s[i]; i++);
}
}
 
 
int
int
MYread_FPword (int sock, char *putinhere)
MYread_FPword (int sock, char *putinhere)
{
{
  int i;
  int i;
  for (i = 0; i < 16; i++)
  for (i = 0; i < 16; i++)
    if (MYread_char (sock, &putinhere[i]) < 0)
    if (MYread_char (sock, &putinhere[i]) < 0)
      return -1;
      return -1;
  return 0;
  return 0;
}
}
 
 
void
void
MYwrite_FPword (int sock, char *fromhere)
MYwrite_FPword (int sock, char *fromhere)
{
{
  int i;
  int i;
  for (i = 0; i < 16; i++)
  for (i = 0; i < 16; i++)
    MYwrite_char (sock, fromhere[i]);
    MYwrite_char (sock, fromhere[i]);
}
}
 
 
/* Takes n bytes from source and those n bytes */
/* Takes n bytes from source and those n bytes */
/* down to dest */
/* down to dest */
int
int
passon (int source, int dest, int n)
passon (int source, int dest, int n)
{
{
  char *p;
  char *p;
  int i;
  int i;
 
 
  p = (char *) malloc (n);
  p = (char *) malloc (n);
  if (!p)
  if (!p)
    {
    {
      perror ("Out of memory\n");
      perror ("Out of memory\n");
      exit (1);
      exit (1);
    }
    }
  if (n)
  if (n)
    {
    {
      for (i = 0; i < n; i++)
      for (i = 0; i < n; i++)
        if (MYread_char (source, &p[i]) < 0)
        if (MYread_char (source, &p[i]) < 0)
          return -1;
          return -1;
 
 
#ifdef DEBUG
#ifdef DEBUG
      if (dest == debugsock)
      if (dest == debugsock)
        for (i = 0; i < n; i++)
        for (i = 0; i < n; i++)
          fprintf (stderr, ")%02x ", (unsigned char) p[i]);
          fprintf (stderr, ")%02x ", (unsigned char) p[i]);
#endif
#endif
 
 
      write (dest, p, n);
      write (dest, p, n);
    }
    }
  free (p);
  free (p);
  return 0;
  return 0;
}
}
 
 

powered by: WebSVN 2.1.0

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