OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gdb-6.8/] [pre-binutils-2.20.1-sync/] [sim/] [cris/] [dv-rv.c] - Diff between revs 157 and 223

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

Rev 157 Rev 223
/* The remote-virtual-component simulator framework
/* The remote-virtual-component simulator framework
   for GDB, the GNU Debugger.
   for GDB, the GNU Debugger.
 
 
   Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
   Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
 
 
   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 3 of the License, or
   the Free Software Foundation; either version 3 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, see <http://www.gnu.org/licenses/>.  */
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 
 
#include "sim-main.h"
#include "sim-main.h"
#include "hw-main.h"
#include "hw-main.h"
 
 
#include "hw-tree.h"
#include "hw-tree.h"
 
 
#include <ctype.h>
#include <ctype.h>
 
 
#ifdef HAVE_ERRNO_H
#ifdef HAVE_ERRNO_H
#include <errno.h>
#include <errno.h>
#endif
#endif
 
 
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#else
#else
#ifdef HAVE_STRINGS_H
#ifdef HAVE_STRINGS_H
#include <strings.h>
#include <strings.h>
#endif
#endif
#endif
#endif
 
 
#ifdef HAVE_UNISTD_H
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <unistd.h>
#endif
#endif
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.h>
#endif
#endif
 
 
#ifdef HAVE_SYS_TYPES_H
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#include <sys/types.h>
#endif
#endif
 
 
#ifdef HAVE_SYS_TIME_H
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#include <sys/time.h>
#endif
#endif
 
 
#ifdef HAVE_SYS_SELECT_H
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#include <sys/select.h>
#endif
#endif
 
 
/* Not guarded in dv-sockser.c, so why here.  */
/* Not guarded in dv-sockser.c, so why here.  */
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/socket.h>
 
 
 
 
/* DEVICE
/* DEVICE
 
 
 
 
   rv - Remote Virtual component
   rv - Remote Virtual component
 
 
 
 
   DESCRIPTION
   DESCRIPTION
 
 
 
 
   Socket connection to a remote simulator component, for example one
   Socket connection to a remote simulator component, for example one
   for testing a verilog construction.  Protocol defined below.
   for testing a verilog construction.  Protocol defined below.
 
 
   There is a set of 32-bit I/O ports, with a mapping from local to
   There is a set of 32-bit I/O ports, with a mapping from local to
   remote addresses.  There is a set of interrupts expressed as a
   remote addresses.  There is a set of interrupts expressed as a
   bit-mask, with a mapping from remote to local.  There is a set of
   bit-mask, with a mapping from remote to local.  There is a set of
   memory ranges (actual memory defined elsewhere), also with a
   memory ranges (actual memory defined elsewhere), also with a
   mapping from remote to local addresses, that is expected to be
   mapping from remote to local addresses, that is expected to be
   accessible to the remote simulator in 32-byte chunks (simulating
   accessible to the remote simulator in 32-byte chunks (simulating
   DMA).  There is a mapping from remote cycles (or an appropriate
   DMA).  There is a mapping from remote cycles (or an appropriate
   elsewhere defined time-slice) to local cycles.
   elsewhere defined time-slice) to local cycles.
 
 
   PROPERTIES
   PROPERTIES
 
 
   reg = <address> <size>
   reg = <address> <size>
   The address (within the parent bus) that this device is to
   The address (within the parent bus) that this device is to
   be located.
   be located.
 
 
   remote-reg = <remote-address>
   remote-reg = <remote-address>
   The address of reg on the remote side.  Defaults to 0.
   The address of reg on the remote side.  Defaults to 0.
 
 
   mem = <address> <size>
   mem = <address> <size>
   Specify an address-range (within the parent bus) that the remote
   Specify an address-range (within the parent bus) that the remote
   device can access.  The memory is assumed to be already defined.
   device can access.  The memory is assumed to be already defined.
   If there's no memory defined but the remote side asks for a memory
   If there's no memory defined but the remote side asks for a memory
   access, the simulation is aborted.
   access, the simulation is aborted.
 
 
   remote-mem = <remote-address>
   remote-mem = <remote-address>
   The address of mem on the remote side.  Defaults to 0.
   The address of mem on the remote side.  Defaults to 0.
 
 
   mbox = <address>
   mbox = <address>
   Address of the mailbox interface.  Writes to this address with the
   Address of the mailbox interface.  Writes to this address with the
   local address of a mailbox command, a complete packet with length
   local address of a mailbox command, a complete packet with length
   and command; (4 or 6)) invokes the mailbox interface.  Reads are
   and command; (4 or 6)) invokes the mailbox interface.  Reads are
   invalid.  Replies are written to the same address.  Address space
   invalid.  Replies are written to the same address.  Address space
   from <address> up-to-and-including <address>+3 is allocated.
   from <address> up-to-and-including <address>+3 is allocated.
 
 
   max-poll-ticks = <local-count>
   max-poll-ticks = <local-count>
   Sets the maximum interval between polling the external component,
   Sets the maximum interval between polling the external component,
   expressed in internal cycles.  Defaults to 10000.
   expressed in internal cycles.  Defaults to 10000.
 
 
   watchdog-interval = <seconds>
   watchdog-interval = <seconds>
   Sets the wallclock seconds between watchdog packets sent to the
   Sets the wallclock seconds between watchdog packets sent to the
   remote side (may be larger if there's no rv activity in that time).
   remote side (may be larger if there's no rv activity in that time).
   Defaults to 30.  If set to 0, no watchdog packets are sent.
   Defaults to 30.  If set to 0, no watchdog packets are sent.
 
 
   intnum = <local-int-0> <local-int-1> ... <local-int-31>
   intnum = <local-int-0> <local-int-1> ... <local-int-31>
   Defines a map from remote bit numbers to local values to be emitted
   Defines a map from remote bit numbers to local values to be emitted
   on the "int" port, with the external bit number as the ordinal - 1
   on the "int" port, with the external bit number as the ordinal - 1
   of the local translation.  E.g. 43 121 would mean map external
   of the local translation.  E.g. 43 121 would mean map external
   (1<<0) to internal 43 and external (1<<1) to internal 121.  The
   (1<<0) to internal 43 and external (1<<1) to internal 121.  The
   default is unity; no translation.  If more than one bit is set in
   default is unity; no translation.  If more than one bit is set in
   the remote interrupt word, the intmultiple property can be used to
   the remote interrupt word, the intmultiple property can be used to
   control the translation.
   control the translation.
 
 
   intmultiple = <intvalue>
   intmultiple = <intvalue>
   When more than one bit is set in the remote interrupt word, you may
   When more than one bit is set in the remote interrupt word, you may
   want to map this situation to a separate interrupt value.  If this
   want to map this situation to a separate interrupt value.  If this
   property is non-zero, it is used as that value.  If it is zero, the
   property is non-zero, it is used as that value.  If it is zero, the
   local value for the "int" port is the bitwise-or of the translated
   local value for the "int" port is the bitwise-or of the translated
   local values.
   local values.
 
 
   host = <hostid>
   host = <hostid>
   The hostname or address where the simulator to be used listens.
   The hostname or address where the simulator to be used listens.
   Defaults to "127.0.0.1"
   Defaults to "127.0.0.1"
 
 
   port = <portnumber>
   port = <portnumber>
   The hostname or address where the simulator to be used listens.
   The hostname or address where the simulator to be used listens.
   Defaults to 10000.
   Defaults to 10000.
 
 
   dummy = <value>
   dummy = <value>
    or
    or
   dummy = <filename>
   dummy = <filename>
   Don't connect to a remote side; use initial dummy contents from
   Don't connect to a remote side; use initial dummy contents from
   <filename> (which has to be at least as big as the <size> argument
   <filename> (which has to be at least as big as the <size> argument
   of reg above) or filled with byte-value <value>.  Mailboxes are not
   of reg above) or filled with byte-value <value>.  Mailboxes are not
   supported (can be defined but can not be used) and remote-memory
   supported (can be defined but can not be used) and remote-memory
   accesses don't apply.  The main purpose for this property is to
   accesses don't apply.  The main purpose for this property is to
   simplify use of configuration and simulated hardware that is
   simplify use of configuration and simulated hardware that is
   e.g. only trivially initialized but not actually used.
   e.g. only trivially initialized but not actually used.
 
 
 
 
   PORTS
   PORTS
 
 
   int (output)
   int (output)
   Driven as a result of a remote interrupt request.  The value is a
   Driven as a result of a remote interrupt request.  The value is a
   32-bit bitset of active interrupts.
   32-bit bitset of active interrupts.
 
 
 
 
   BUGS
   BUGS
 
 
   All and none.
   All and none.
 
 
 
 
   PROTOCOL
   PROTOCOL
 
 
   This is version 1.0 of this protocol, defining packet format and
   This is version 1.0 of this protocol, defining packet format and
   actions in a supposedly upward-compatible manner where client and
   actions in a supposedly upward-compatible manner where client and
   servers of different versions are expected to interoperate; the
   servers of different versions are expected to interoperate; the
   format and the definitions below are hopefully generic enough to
   format and the definitions below are hopefully generic enough to
   allow this.
   allow this.
 
 
   Each connection has a server and a client (this code); the roles
   Each connection has a server and a client (this code); the roles
   are known beforehand.  The client usually corresponds to a CPU and
   are known beforehand.  The client usually corresponds to a CPU and
   memory system and the server corresponds to a memory-mapped
   memory system and the server corresponds to a memory-mapped
   register hardware interface and/or a DMA controller.  They
   register hardware interface and/or a DMA controller.  They
   communicate using packets with specific commands, of which some
   communicate using packets with specific commands, of which some
   require replies from the other side; most are intiated by the
   require replies from the other side; most are intiated by the
   client with one exception.  A reply uses the same format as the
   client with one exception.  A reply uses the same format as the
   command.
   command.
 
 
   Packets are at least three bytes long, where the first two bytes
   Packets are at least three bytes long, where the first two bytes
   form a header, a 16-bit little-endian number that is the total
   form a header, a 16-bit little-endian number that is the total
   length of the packet including the header.  There is also a
   length of the packet including the header.  There is also a
   one-byte command.  The payload is optional, depending on the
   one-byte command.  The payload is optional, depending on the
   command.
   command.
 
 
   [[16-bit-low-byte-of-length] [16-bit-high-byte-of-length]
   [[16-bit-low-byte-of-length] [16-bit-high-byte-of-length]
    [command/reply] [payload byte 0] [payload byte 1]
    [command/reply] [payload byte 0] [payload byte 1]
    ... [payload byte (length-of-packet - 3)]]
    ... [payload byte (length-of-packet - 3)]]
 
 
   Commands:
   Commands:
 
 
   A client or server that reads an undocumented command may exit with
   A client or server that reads an undocumented command may exit with
   a hard error.  Payload not defined or disallowed below is ignored.
   a hard error.  Payload not defined or disallowed below is ignored.
 
 
   It is expected that future client versions find out the version of
   It is expected that future client versions find out the version of
   the server side by polling with base commands, assuming earlier
   the server side by polling with base commands, assuming earlier
   versions if a certain reply isn't seen, with newly defined payload
   versions if a certain reply isn't seen, with newly defined payload
   parts where earlier versions left it undefined.  New commands and
   parts where earlier versions left it undefined.  New commands and
   formats are sent only to the other side after the client and server
   formats are sent only to the other side after the client and server
   has found out each others version.  Not all servers support all
   has found out each others version.  Not all servers support all
   commands; the type of server and supported set of commands is
   commands; the type of server and supported set of commands is
   expected to be known beforehand.
   expected to be known beforehand.
 
 
   RV_READ_CMD = 0
   RV_READ_CMD = 0
   Initiated by the client, requires a reply from the server.  The
   Initiated by the client, requires a reply from the server.  The
   payload from the client is at least 4 bytes, forming a 4-byte
   payload from the client is at least 4 bytes, forming a 4-byte
   little-endian address, the rest being undefined.  The reply from
   little-endian address, the rest being undefined.  The reply from
   the server is at least 8 bytes, forming the same address data as in
   the server is at least 8 bytes, forming the same address data as in
   the request and the second 4-byte data being the little-endian
   the request and the second 4-byte data being the little-endian
   contents.
   contents.
 
 
   RV_WRITE_CMD = 1
   RV_WRITE_CMD = 1
   Initiated by the client, requires a reply from the server.  Payload
   Initiated by the client, requires a reply from the server.  Payload
   from the client is at least 8 bytes, forming a 4-byte little-endian
   from the client is at least 8 bytes, forming a 4-byte little-endian
   word being the address, the rest being the little-endian contents
   word being the address, the rest being the little-endian contents
   to write.  The reply from the server is 8 bytes unless elsewhere
   to write.  The reply from the server is 8 bytes unless elsewhere
   agreed otherwise, forming the same address and data as in the
   agreed otherwise, forming the same address and data as in the
   request.  The data sent back may have been altered to correspond to
   request.  The data sent back may have been altered to correspond to
   defined parts but can safely be discarded.
   defined parts but can safely be discarded.
 
 
   RV_IRQ_CMD = 2
   RV_IRQ_CMD = 2
   Initiated by the server, no reply.  The payload is 4 bytes, forming
   Initiated by the server, no reply.  The payload is 4 bytes, forming
   a little-endian word with bits numbers corresponding to currently
   a little-endian word with bits numbers corresponding to currently
   active interrupt sources; value (1<<N) indicating interrupt source
   active interrupt sources; value (1<<N) indicating interrupt source
   N being active.
   N being active.
 
 
   RV_MEM_RD_CMD = 3
   RV_MEM_RD_CMD = 3
   Initiated by the server, requires a reply.  A client must know
   Initiated by the server, requires a reply.  A client must know
   beforehand when (in command sequence or constant) the server can
   beforehand when (in command sequence or constant) the server can
   send this command and if so must then not send any commands of its
   send this command and if so must then not send any commands of its
   own (including watchdog commands); the server is allowed to assume
   own (including watchdog commands); the server is allowed to assume
   that incoming data is only replies to this command.  The format is
   that incoming data is only replies to this command.  The format is
   8 bytes of data; 4 bytes of little-endian address followed by a
   8 bytes of data; 4 bytes of little-endian address followed by a
   32-bit little endian word with the number of bytes to read.  The
   32-bit little endian word with the number of bytes to read.  The
   reply is the same address and number of bytes, followed by the data
   reply is the same address and number of bytes, followed by the data
   that had been read.
   that had been read.
 
 
   RV_MEM_WR_CMD = 4
   RV_MEM_WR_CMD = 4
   Initiated by the server, no reply.  The format is the same as a
   Initiated by the server, no reply.  The format is the same as a
   reply to RV_MEM_RD_CMD; a 32-bit little-endian address, followed by
   reply to RV_MEM_RD_CMD; a 32-bit little-endian address, followed by
   the 32-bit little-endian number of bytes to write (redundant
   the 32-bit little-endian number of bytes to write (redundant
   information but must be consistent with the packet header).
   information but must be consistent with the packet header).
 
 
   RV_MBOX_HANDLE_CMD = 5
   RV_MBOX_HANDLE_CMD = 5
   Initiated by the client, requires a reply.  The payload is 4
   Initiated by the client, requires a reply.  The payload is 4
   undefined bytes followed by an binary blob, the size of the
   undefined bytes followed by an binary blob, the size of the
   blob given by the packet header.  The reply is a 32-bit little
   blob given by the packet header.  The reply is a 32-bit little
   endian number at the same index as the undefined bytes.  Actual
   endian number at the same index as the undefined bytes.  Actual
   semantics are application-specific.
   semantics are application-specific.
 
 
   RV_MBOX_PUT_CMD = 6
   RV_MBOX_PUT_CMD = 6
   Initiated by the client, requires a reply, with the reply using the
   Initiated by the client, requires a reply, with the reply using the
   RV_MBOX_HANDLE_CMD reply format (i.e. *both* that command and
   RV_MBOX_HANDLE_CMD reply format (i.e. *both* that command and
   32-bit little-endian number).  The payload is a 32-bit little
   32-bit little-endian number).  The payload is a 32-bit little
   endian number followed by an undefined payload, at most 20 bytes
   endian number followed by an undefined payload, at most 20 bytes
   long.  The reply is a 32-bit little endian number.  Actual
   long.  The reply is a 32-bit little endian number.  Actual
   semantics are application-specific.
   semantics are application-specific.
 
 
   RV_WATCHDOG_CMD = 7
   RV_WATCHDOG_CMD = 7
   Initiated by the client, no reply.  A version 1.0 client sends no
   Initiated by the client, no reply.  A version 1.0 client sends no
   payload; a version 1.0 server should ignore any such payload.  A
   payload; a version 1.0 server should ignore any such payload.  A
   version 1.0 server must not send a reply.
   version 1.0 server must not send a reply.
 
 
 
 
   Possible future enhancements:
   Possible future enhancements:
 
 
   Synchronization; server and client reports the number of elapsed
   Synchronization; server and client reports the number of elapsed
   cycles (unit to-be-defined) at each request or notification.
   cycles (unit to-be-defined) at each request or notification.
   Pretty much the top-of-the-todo-list item.
   Pretty much the top-of-the-todo-list item.
 
 
   Large addresses; 1.0 being restricted to 32-bit addresses.
   Large addresses; 1.0 being restricted to 32-bit addresses.
 
 
   Variable-size data; currently restricted to 32-bit register
   Variable-size data; currently restricted to 32-bit register
   accesses.
   accesses.
 
 
   Specified data endianness (not the packet header) perhaps as part
   Specified data endianness (not the packet header) perhaps as part
   of an initial format request; currently little-endian only.
   of an initial format request; currently little-endian only.
 
 
 
 
   Usage notes:
   Usage notes:
   When used with servers sending RV_MEM_RD_CMD but being
   When used with servers sending RV_MEM_RD_CMD but being
   narrow-minded about indata, set watchdog-interval to 0.  Use
   narrow-minded about indata, set watchdog-interval to 0.  Use
   multiple rv instances when there are e.g. separate register and
   multiple rv instances when there are e.g. separate register and
   memory servers.  Alway log, setting "/rv/trace? true", at the
   memory servers.  Alway log, setting "/rv/trace? true", at the
   development phase.  Borrow from the test-suite.
   development phase.  Borrow from the test-suite.
   */
   */
 
 
#define RV_FAMILY_NAME "rv"
#define RV_FAMILY_NAME "rv"
 
 
enum rv_command {
enum rv_command {
  RV_READ_CMD = 0,
  RV_READ_CMD = 0,
  RV_WRITE_CMD = 1,
  RV_WRITE_CMD = 1,
  RV_IRQ_CMD = 2,
  RV_IRQ_CMD = 2,
  RV_MEM_RD_CMD = 3,
  RV_MEM_RD_CMD = 3,
  RV_MEM_WR_CMD = 4,
  RV_MEM_WR_CMD = 4,
  RV_MBOX_HANDLE_CMD = 5,
  RV_MBOX_HANDLE_CMD = 5,
  RV_MBOX_PUT_CMD = 6,
  RV_MBOX_PUT_CMD = 6,
  RV_WATCHDOG_CMD = 7
  RV_WATCHDOG_CMD = 7
};
};
 
 
 
 
typedef struct _hw_rv_device
typedef struct _hw_rv_device
{
{
  /* Mapping of remote interrupt bit-numbers to local ones.  */
  /* Mapping of remote interrupt bit-numbers to local ones.  */
  unsigned32 remote_to_local_int[32];
  unsigned32 remote_to_local_int[32];
 
 
  /* When multiple bits are set, a non-zero value here indicates that
  /* When multiple bits are set, a non-zero value here indicates that
     this value should be used instead.  */
     this value should be used instead.  */
  unsigned32 intmultiple;
  unsigned32 intmultiple;
 
 
  /* Local address of registers.  */
  /* Local address of registers.  */
  unsigned32 reg_address;
  unsigned32 reg_address;
 
 
  /* Size of register bank in bytes.  */
  /* Size of register bank in bytes.  */
  unsigned32 reg_size;
  unsigned32 reg_size;
 
 
  /* Remote address of registers.  */
  /* Remote address of registers.  */
  unsigned32 remote_reg_address;
  unsigned32 remote_reg_address;
 
 
  /* Local address of DMA:able memory.  */
  /* Local address of DMA:able memory.  */
  unsigned32 mem_address;
  unsigned32 mem_address;
 
 
  /* Size of DMA:able memory in bytes.  */
  /* Size of DMA:able memory in bytes.  */
  unsigned32 mem_size;
  unsigned32 mem_size;
 
 
  /* Bitmask for valid DMA request size.  */
  /* Bitmask for valid DMA request size.  */
  unsigned32 mem_burst_mask;
  unsigned32 mem_burst_mask;
 
 
  /* Remote address of DMA:able memory.  */
  /* Remote address of DMA:able memory.  */
  unsigned32 remote_mem_address;
  unsigned32 remote_mem_address;
 
 
  /* (Local) address of mbox; where to put a pointer to the mbox to be
  /* (Local) address of mbox; where to put a pointer to the mbox to be
     sent.  */
     sent.  */
  unsigned32 mbox_address;
  unsigned32 mbox_address;
 
 
  /* Probably not 127.0.0.1:10000.  */
  /* Probably not 127.0.0.1:10000.  */
  const char *host;
  const char *host;
  int port;
  int port;
 
 
  /* If non-NULL, points to memory to use instead of connection.  */
  /* If non-NULL, points to memory to use instead of connection.  */
  unsigned8 *dummy;
  unsigned8 *dummy;
 
 
  /* File descriptor for the socket.  Set to -1 when error.  Only one
  /* File descriptor for the socket.  Set to -1 when error.  Only one
     of dummy and this is active.  */
     of dummy and this is active.  */
  int fd;
  int fd;
 
 
  /* Stashed errno, as we don't emit an error right away.  */
  /* Stashed errno, as we don't emit an error right away.  */
  int saved_errno;
  int saved_errno;
 
 
  /* This, plus latency because the CPU might not be checking until a
  /* This, plus latency because the CPU might not be checking until a
     CTI insn (usually a branch or a jump) is the interval in cycles
     CTI insn (usually a branch or a jump) is the interval in cycles
     between the rv is polled for e.g. DMA requests.  */
     between the rv is polled for e.g. DMA requests.  */
  unsigned32 max_tick_poll_interval;
  unsigned32 max_tick_poll_interval;
 
 
  /* Running counter for exponential backoff up to
  /* Running counter for exponential backoff up to
     max_tick_poll_interval to avoid polling the connection
     max_tick_poll_interval to avoid polling the connection
     unnecessarily often.  Set to 1 when rv activity (read/write
     unnecessarily often.  Set to 1 when rv activity (read/write
     register, DMA request) is detected.  */
     register, DMA request) is detected.  */
  unsigned32 next_period;
  unsigned32 next_period;
 
 
  /* This is the interval in wall-clock seconds between watchdog
  /* This is the interval in wall-clock seconds between watchdog
     packets are sent to the remote side.  Zero means no watchdog
     packets are sent to the remote side.  Zero means no watchdog
     packets. */
     packets. */
  unsigned32 watchdog_interval;
  unsigned32 watchdog_interval;
 
 
  /* Last time we sent a watchdog packet.  */
  /* Last time we sent a watchdog packet.  */
  struct timeval last_wdog_time;
  struct timeval last_wdog_time;
 
 
  /* Mostly used as a kludge for knowing which rv:s have poll events
  /* Mostly used as a kludge for knowing which rv:s have poll events
     active.  */
     active.  */
  struct hw_event *poll_callback;
  struct hw_event *poll_callback;
} hw_rv_device;
} hw_rv_device;
 
 
 
 
/* We might add ports in the future, so keep this an enumeration.  */
/* We might add ports in the future, so keep this an enumeration.  */
enum
enum
  {
  {
    INT_PORT
    INT_PORT
  };
  };
 
 
/* Our ports.  */
/* Our ports.  */
static const struct hw_port_descriptor hw_rv_ports[] = {
static const struct hw_port_descriptor hw_rv_ports[] = {
  { "int", INT_PORT, 0, output_port },
  { "int", INT_PORT, 0, output_port },
  { NULL }
  { NULL }
};
};
 
 
/* Send LEN bytes of data from BUF to the socket.  Abort on
/* Send LEN bytes of data from BUF to the socket.  Abort on
   errors.  */
   errors.  */
 
 
static void
static void
hw_rv_write (struct hw *me,
hw_rv_write (struct hw *me,
             void *buf,
             void *buf,
             unsigned int len)
             unsigned int len)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned8 *bufp = buf;
  unsigned8 *bufp = buf;
 
 
  /* If we don't have a valid fd here, it's because we got an error
  /* If we don't have a valid fd here, it's because we got an error
     initially, and we suppressed that error.  */
     initially, and we suppressed that error.  */
  if (rv->fd < 0)
  if (rv->fd < 0)
    hw_abort (me, "couldn't open a connection to %s:%d because: %s",
    hw_abort (me, "couldn't open a connection to %s:%d because: %s",
              rv->host, rv->port, strerror (rv->saved_errno));
              rv->host, rv->port, strerror (rv->saved_errno));
 
 
  while (len > 0)
  while (len > 0)
    {
    {
      ssize_t ret = write (rv->fd, bufp, len);
      ssize_t ret = write (rv->fd, bufp, len);
      if (ret < 0)
      if (ret < 0)
        /* FIXME: More graceful exit.  */
        /* FIXME: More graceful exit.  */
        hw_abort (me, "write to %s:%d failed: %s\n", rv->host, rv->port,
        hw_abort (me, "write to %s:%d failed: %s\n", rv->host, rv->port,
                  strerror (errno));
                  strerror (errno));
 
 
      len -= ret;
      len -= ret;
      bufp += ret;
      bufp += ret;
    }
    }
}
}
 
 
/* Read LEN bytes of data into BUF from the socket.  Set the file
/* Read LEN bytes of data into BUF from the socket.  Set the file
   descriptor to -1 if there's an error.  */
   descriptor to -1 if there's an error.  */
 
 
static void
static void
hw_rv_read (struct hw *me,
hw_rv_read (struct hw *me,
            void *buf,
            void *buf,
            unsigned int len)
            unsigned int len)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned8 *bufp = buf;
  unsigned8 *bufp = buf;
 
 
  while (len > 0)
  while (len > 0)
    {
    {
      ssize_t ret = read (rv->fd, bufp, len);
      ssize_t ret = read (rv->fd, bufp, len);
 
 
      /* We get all zero if the remote end quits, but no error
      /* We get all zero if the remote end quits, but no error
         indication; even select says there's data active.  */
         indication; even select says there's data active.  */
      if (ret <= 0)
      if (ret <= 0)
        {
        {
          if (close (rv->fd) != 0)
          if (close (rv->fd) != 0)
            /* FIXME: More graceful exit.  */
            /* FIXME: More graceful exit.  */
            hw_abort (me, "read from %s:%d failed: %d\n", rv->host, rv->port, errno);
            hw_abort (me, "read from %s:%d failed: %d\n", rv->host, rv->port, errno);
          rv->fd = -1;
          rv->fd = -1;
          return;
          return;
        }
        }
 
 
      len -= ret;
      len -= ret;
      bufp += ret;
      bufp += ret;
    }
    }
}
}
 
 
/* Construct and send a packet of data of type CMD and len
/* Construct and send a packet of data of type CMD and len
   LEN_NOHEADER (not counting the header...).  */
   LEN_NOHEADER (not counting the header...).  */
 
 
static void
static void
hw_rv_send (struct hw *me,
hw_rv_send (struct hw *me,
            unsigned int cmd,
            unsigned int cmd,
            void *msg,
            void *msg,
            unsigned int len_noheader)
            unsigned int len_noheader)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned8 buf[32+3];
  unsigned8 buf[32+3];
  unsigned8 *bufp;
  unsigned8 *bufp;
  unsigned int len = len_noheader + 3;
  unsigned int len = len_noheader + 3;
  int ret;
  int ret;
 
 
  buf[0] = len & 255;
  buf[0] = len & 255;
  buf[1] = (len >> 8) & 255;
  buf[1] = (len >> 8) & 255;
  buf[2] = cmd;
  buf[2] = cmd;
 
 
  if (len > sizeof (buf))
  if (len > sizeof (buf))
    {
    {
      hw_rv_write (me, buf, 3);
      hw_rv_write (me, buf, 3);
      len = len_noheader;
      len = len_noheader;
      bufp = msg;
      bufp = msg;
    }
    }
  else
  else
    {
    {
      memcpy (buf + 3, msg, len_noheader);
      memcpy (buf + 3, msg, len_noheader);
      bufp = buf;
      bufp = buf;
    }
    }
 
 
  hw_rv_write (me, bufp, len);
  hw_rv_write (me, bufp, len);
}
}
 
 
/* Handle incoming DMA requests as per the RV_MEM_RD_CMD packet.
/* Handle incoming DMA requests as per the RV_MEM_RD_CMD packet.
   Abort on errors.  */
   Abort on errors.  */
 
 
static void
static void
hw_rv_read_mem (struct hw *me, unsigned int len)
hw_rv_read_mem (struct hw *me, unsigned int len)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  /* If you change this size, please adjust the mem2 testcase.  */
  /* If you change this size, please adjust the mem2 testcase.  */
  unsigned8 buf[32+8];
  unsigned8 buf[32+8];
  unsigned8 *bufp = buf;
  unsigned8 *bufp = buf;
  unsigned32 leaddr;
  unsigned32 leaddr;
  unsigned32 addr;
  unsigned32 addr;
  unsigned32 lelen;
  unsigned32 lelen;
  unsigned32 i;
  unsigned32 i;
 
 
  if (len != 8)
  if (len != 8)
    hw_abort (me, "expected DMA read request len 8+3, got %d+3", len);
    hw_abort (me, "expected DMA read request len 8+3, got %d+3", len);
 
 
  hw_rv_read (me, &leaddr, 4);
  hw_rv_read (me, &leaddr, 4);
  hw_rv_read (me, &lelen, 4);
  hw_rv_read (me, &lelen, 4);
  len = LE2H_4 (lelen);
  len = LE2H_4 (lelen);
  addr = LE2H_4 (leaddr);
  addr = LE2H_4 (leaddr);
 
 
  if (addr < rv->remote_mem_address
  if (addr < rv->remote_mem_address
      || addr >= rv->remote_mem_address + rv->mem_size)
      || addr >= rv->remote_mem_address + rv->mem_size)
    hw_abort (me, "DMA read at remote 0x%x; outside [0x%x..0x%x-1]",
    hw_abort (me, "DMA read at remote 0x%x; outside [0x%x..0x%x-1]",
              (unsigned) addr, (unsigned) rv->remote_mem_address,
              (unsigned) addr, (unsigned) rv->remote_mem_address,
              (unsigned) (rv->remote_mem_address + rv->mem_size));
              (unsigned) (rv->remote_mem_address + rv->mem_size));
  addr = addr - rv->remote_mem_address + rv->mem_address;
  addr = addr - rv->remote_mem_address + rv->mem_address;
 
 
  if (len == 0)
  if (len == 0)
    hw_abort (me, "DMA read request for 0 bytes isn't supported");
    hw_abort (me, "DMA read request for 0 bytes isn't supported");
 
 
  if (len & ~rv->mem_burst_mask)
  if (len & ~rv->mem_burst_mask)
    hw_abort (me, "DMA trying to read %d bytes; not matching mask of 0x%x",
    hw_abort (me, "DMA trying to read %d bytes; not matching mask of 0x%x",
              len, rv->mem_burst_mask);
              len, rv->mem_burst_mask);
  if (len + 8 > sizeof (buf))
  if (len + 8 > sizeof (buf))
    bufp = hw_malloc (me, len + 8);
    bufp = hw_malloc (me, len + 8);
 
 
  HW_TRACE ((me, "DMA R 0x%x..0x%x", addr, addr + len -1));
  HW_TRACE ((me, "DMA R 0x%x..0x%x", addr, addr + len -1));
  hw_dma_read_buffer (me, bufp + 8, 0, addr, len);
  hw_dma_read_buffer (me, bufp + 8, 0, addr, len);
  if (hw_trace_p (me))
  if (hw_trace_p (me))
    for (i = 0; i < len; i += 4)
    for (i = 0; i < len; i += 4)
      HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
      HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
                 addr + i,
                 addr + i,
                 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
                 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
 
 
  memcpy (bufp, &leaddr, 4);
  memcpy (bufp, &leaddr, 4);
  memcpy (bufp + 4, &lelen, 4);
  memcpy (bufp + 4, &lelen, 4);
  hw_rv_send (me, RV_MEM_RD_CMD, bufp, len + 8);
  hw_rv_send (me, RV_MEM_RD_CMD, bufp, len + 8);
  if (bufp != buf)
  if (bufp != buf)
    hw_free (me, bufp);
    hw_free (me, bufp);
}
}
 
 
/* Handle incoming DMA requests as per the RV_MEM_WR_CMD packet.
/* Handle incoming DMA requests as per the RV_MEM_WR_CMD packet.
   Abort on errors.  */
   Abort on errors.  */
 
 
static void
static void
hw_rv_write_mem (struct hw *me, unsigned int plen)
hw_rv_write_mem (struct hw *me, unsigned int plen)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  /* If you change this size, please adjust the mem2 testcase.  */
  /* If you change this size, please adjust the mem2 testcase.  */
  unsigned8 buf[32+8];
  unsigned8 buf[32+8];
  unsigned8 *bufp = buf;
  unsigned8 *bufp = buf;
  unsigned32 leaddr;
  unsigned32 leaddr;
  unsigned32 addr;
  unsigned32 addr;
  unsigned32 lelen;
  unsigned32 lelen;
  unsigned32 len;
  unsigned32 len;
  unsigned32 i;
  unsigned32 i;
 
 
  hw_rv_read (me, &leaddr, 4);
  hw_rv_read (me, &leaddr, 4);
  hw_rv_read (me, &lelen, 4);
  hw_rv_read (me, &lelen, 4);
  len = LE2H_4 (lelen);
  len = LE2H_4 (lelen);
  addr = LE2H_4 (leaddr);
  addr = LE2H_4 (leaddr);
 
 
  if (len != plen - 8)
  if (len != plen - 8)
    hw_abort (me,
    hw_abort (me,
              "inconsistency in DMA write request packet: "
              "inconsistency in DMA write request packet: "
              "envelope %d+3, inner %d bytes", plen, len);
              "envelope %d+3, inner %d bytes", plen, len);
 
 
  if (addr < rv->remote_mem_address
  if (addr < rv->remote_mem_address
      || addr >= rv->remote_mem_address + rv->mem_size)
      || addr >= rv->remote_mem_address + rv->mem_size)
    hw_abort (me, "DMA write at remote 0x%x; outside [0x%x..0x%x-1]",
    hw_abort (me, "DMA write at remote 0x%x; outside [0x%x..0x%x-1]",
              (unsigned) addr, (unsigned) rv->remote_mem_address,
              (unsigned) addr, (unsigned) rv->remote_mem_address,
              (unsigned) (rv->remote_mem_address + rv->mem_size));
              (unsigned) (rv->remote_mem_address + rv->mem_size));
 
 
  addr = addr - rv->remote_mem_address + rv->mem_address;
  addr = addr - rv->remote_mem_address + rv->mem_address;
  if (len == 0)
  if (len == 0)
    hw_abort (me, "DMA write request for 0 bytes isn't supported");
    hw_abort (me, "DMA write request for 0 bytes isn't supported");
 
 
  if (len & ~rv->mem_burst_mask)
  if (len & ~rv->mem_burst_mask)
    hw_abort (me, "DMA trying to write %d bytes; not matching mask of 0x%x",
    hw_abort (me, "DMA trying to write %d bytes; not matching mask of 0x%x",
              len, rv->mem_burst_mask);
              len, rv->mem_burst_mask);
  if (len + 8 > sizeof (buf))
  if (len + 8 > sizeof (buf))
    bufp = hw_malloc (me, len + 8);
    bufp = hw_malloc (me, len + 8);
 
 
  hw_rv_read (me, bufp + 8, len);
  hw_rv_read (me, bufp + 8, len);
  HW_TRACE ((me, "DMA W 0x%x..0x%x", addr, addr + len - 1));
  HW_TRACE ((me, "DMA W 0x%x..0x%x", addr, addr + len - 1));
  hw_dma_write_buffer (me, bufp + 8, 0, addr, len, 0);
  hw_dma_write_buffer (me, bufp + 8, 0, addr, len, 0);
  if (hw_trace_p (me))
  if (hw_trace_p (me))
    for (i = 0; i < len; i += 4)
    for (i = 0; i < len; i += 4)
      HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
      HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
                 addr + i,
                 addr + i,
                 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
                 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
  if (bufp != buf)
  if (bufp != buf)
    hw_free (me, bufp);
    hw_free (me, bufp);
}
}
 
 
static void
static void
hw_rv_irq (struct hw *me, unsigned int len)
hw_rv_irq (struct hw *me, unsigned int len)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned32 intbitsle;
  unsigned32 intbitsle;
  unsigned32 intbits_ext;
  unsigned32 intbits_ext;
  unsigned32 intval = 0;
  unsigned32 intval = 0;
  int i;
  int i;
 
 
  if (len != 4)
  if (len != 4)
    hw_abort (me, "IRQ with %d data not supported", len);
    hw_abort (me, "IRQ with %d data not supported", len);
 
 
  hw_rv_read (me, &intbitsle, 4);
  hw_rv_read (me, &intbitsle, 4);
  intbits_ext = LE2H_4 (intbitsle);
  intbits_ext = LE2H_4 (intbitsle);
  for (i = 0; i < 32; i++)
  for (i = 0; i < 32; i++)
    if ((intbits_ext & (1 << i)) != 0)
    if ((intbits_ext & (1 << i)) != 0)
      intval |= rv->remote_to_local_int[i];
      intval |= rv->remote_to_local_int[i];
  if ((intbits_ext & ~(intbits_ext - 1)) != intbits_ext
  if ((intbits_ext & ~(intbits_ext - 1)) != intbits_ext
      && rv->intmultiple != 0)
      && rv->intmultiple != 0)
    intval = rv->intmultiple;
    intval = rv->intmultiple;
 
 
  HW_TRACE ((me, "IRQ 0x%x", intval));
  HW_TRACE ((me, "IRQ 0x%x", intval));
  hw_port_event (me, INT_PORT, intval);
  hw_port_event (me, INT_PORT, intval);
}
}
 
 
/* Handle incoming interrupt notifications as per the RV_IRQ_CMD
/* Handle incoming interrupt notifications as per the RV_IRQ_CMD
   packet.  Abort on errors.  */
   packet.  Abort on errors.  */
 
 
static void
static void
hw_rv_handle_incoming (struct hw *me,
hw_rv_handle_incoming (struct hw *me,
                       int expected_type,
                       int expected_type,
                       unsigned8 *buf,
                       unsigned8 *buf,
                       unsigned int *return_len)
                       unsigned int *return_len)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned8 cbuf[32];
  unsigned8 cbuf[32];
  unsigned int len;
  unsigned int len;
  unsigned int cmd;
  unsigned int cmd;
 
 
  while (1)
  while (1)
    {
    {
      hw_rv_read (me, cbuf, 3);
      hw_rv_read (me, cbuf, 3);
 
 
      if (rv->fd < 0)
      if (rv->fd < 0)
        return;
        return;
 
 
      len = cbuf[0] + cbuf[1] * 256 - 3;
      len = cbuf[0] + cbuf[1] * 256 - 3;
      cmd = cbuf[2];
      cmd = cbuf[2];
 
 
      /* These come in "asynchronously"; not as a reply.  */
      /* These come in "asynchronously"; not as a reply.  */
      switch (cmd)
      switch (cmd)
        {
        {
        case RV_IRQ_CMD:
        case RV_IRQ_CMD:
          hw_rv_irq (me, len);
          hw_rv_irq (me, len);
          break;
          break;
 
 
        case RV_MEM_RD_CMD:
        case RV_MEM_RD_CMD:
          hw_rv_read_mem (me, len);
          hw_rv_read_mem (me, len);
          break;
          break;
 
 
        case RV_MEM_WR_CMD:
        case RV_MEM_WR_CMD:
          hw_rv_write_mem (me, len);
          hw_rv_write_mem (me, len);
          break;
          break;
        }
        }
 
 
      /* Something is incoming from the other side, so tighten up all
      /* Something is incoming from the other side, so tighten up all
         slack at the next wait.  */
         slack at the next wait.  */
      rv->next_period = 1;
      rv->next_period = 1;
 
 
      switch (cmd)
      switch (cmd)
        {
        {
        case RV_MEM_RD_CMD:
        case RV_MEM_RD_CMD:
        case RV_MEM_WR_CMD:
        case RV_MEM_WR_CMD:
        case RV_IRQ_CMD:
        case RV_IRQ_CMD:
          /* Don't try to handle more than one of these if we were'nt
          /* Don't try to handle more than one of these if we were'nt
             expecting a reply.  */
             expecting a reply.  */
          if (expected_type == -1)
          if (expected_type == -1)
            return;
            return;
          continue;
          continue;
        }
        }
 
 
      /* Require a match between this supposed-reply and the command
      /* Require a match between this supposed-reply and the command
         for the rest.  */
         for the rest.  */
      if (cmd != expected_type)
      if (cmd != expected_type)
        hw_abort (me, "unexpected reply, expected command %d, got %d",
        hw_abort (me, "unexpected reply, expected command %d, got %d",
                  expected_type, cmd);
                  expected_type, cmd);
 
 
      switch (cmd)
      switch (cmd)
        {
        {
        case RV_MBOX_PUT_CMD:
        case RV_MBOX_PUT_CMD:
        case RV_MBOX_HANDLE_CMD:
        case RV_MBOX_HANDLE_CMD:
        case RV_WRITE_CMD:
        case RV_WRITE_CMD:
        case RV_READ_CMD:
        case RV_READ_CMD:
          hw_rv_read (me, buf, len <= *return_len ? len : *return_len);
          hw_rv_read (me, buf, len <= *return_len ? len : *return_len);
          *return_len = len;
          *return_len = len;
          break;
          break;
        }
        }
      break;
      break;
    }
    }
}
}
 
 
/* Send a watchdog packet.  Make a note of wallclock time.  */
/* Send a watchdog packet.  Make a note of wallclock time.  */
 
 
static void
static void
hw_rv_send_wdog (struct hw *me)
hw_rv_send_wdog (struct hw *me)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  HW_TRACE ((me, "WD"));
  HW_TRACE ((me, "WD"));
  gettimeofday (&rv->last_wdog_time, NULL);
  gettimeofday (&rv->last_wdog_time, NULL);
  hw_rv_send (me, RV_WATCHDOG_CMD, "", 0);
  hw_rv_send (me, RV_WATCHDOG_CMD, "", 0);
}
}
 
 
/* Poll the remote side: see if there's any incoming traffic; handle a
/* Poll the remote side: see if there's any incoming traffic; handle a
   packet if so.  Send a watchdog packet if it's time to do so.
   packet if so.  Send a watchdog packet if it's time to do so.
   Beware that the Linux select call indicates traffic for a socket
   Beware that the Linux select call indicates traffic for a socket
   that the remote side has closed (which may be because it was
   that the remote side has closed (which may be because it was
   finished; don't hork until we need to write something just because
   finished; don't hork until we need to write something just because
   we're polling).  */
   we're polling).  */
 
 
static void
static void
hw_rv_poll_once (struct hw *me)
hw_rv_poll_once (struct hw *me)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  fd_set rfds;
  fd_set rfds;
  fd_set efds;
  fd_set efds;
  struct timeval now;
  struct timeval now;
  int ret;
  int ret;
  struct timeval tv;
  struct timeval tv;
 
 
  if (rv->fd < 0)
  if (rv->fd < 0)
    /* Connection has died or was never initiated.  */
    /* Connection has died or was never initiated.  */
    return;
    return;
 
 
  FD_ZERO (&rfds);
  FD_ZERO (&rfds);
  FD_SET (rv->fd, &rfds);
  FD_SET (rv->fd, &rfds);
  FD_ZERO (&efds);
  FD_ZERO (&efds);
  FD_SET (rv->fd, &efds);
  FD_SET (rv->fd, &efds);
  tv.tv_sec = 0;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  tv.tv_usec = 0;
 
 
  ret = select (rv->fd + 1, &rfds, NULL, &efds, &tv);
  ret = select (rv->fd + 1, &rfds, NULL, &efds, &tv);
  gettimeofday (&now, NULL);
  gettimeofday (&now, NULL);
 
 
  if (ret < 0)
  if (ret < 0)
    hw_abort (me, "select failed: %d\n", errno);
    hw_abort (me, "select failed: %d\n", errno);
 
 
  if (rv->watchdog_interval != 0
  if (rv->watchdog_interval != 0
      && now.tv_sec - rv->last_wdog_time.tv_sec >= rv->watchdog_interval)
      && now.tv_sec - rv->last_wdog_time.tv_sec >= rv->watchdog_interval)
    hw_rv_send_wdog (me);
    hw_rv_send_wdog (me);
 
 
  if (FD_ISSET (rv->fd, &rfds))
  if (FD_ISSET (rv->fd, &rfds))
    hw_rv_handle_incoming (me, -1, NULL, NULL);
    hw_rv_handle_incoming (me, -1, NULL, NULL);
}
}
 
 
/* Initialize mapping of remote-to-local interrupt data.  */
/* Initialize mapping of remote-to-local interrupt data.  */
 
 
static void
static void
hw_rv_map_ints (struct hw *me)
hw_rv_map_ints (struct hw *me)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  int i;
  int i;
 
 
  for (i = 0; i < 32; i++)
  for (i = 0; i < 32; i++)
    rv->remote_to_local_int[i] = 1 << i;
    rv->remote_to_local_int[i] = 1 << i;
 
 
  if (hw_find_property (me, "intnum") != NULL)
  if (hw_find_property (me, "intnum") != NULL)
    for (i = 0; i < 32; i++)
    for (i = 0; i < 32; i++)
      {
      {
        signed_cell val = -1;
        signed_cell val = -1;
        if (hw_find_integer_array_property (me, "intnum", i, &val) > 0)
        if (hw_find_integer_array_property (me, "intnum", i, &val) > 0)
          {
          {
            if (val > 0)
            if (val > 0)
              rv->remote_to_local_int[i] = val;
              rv->remote_to_local_int[i] = val;
            else
            else
              hw_abort (me, "property \"intnum@%d\" must be > 0; is %d",
              hw_abort (me, "property \"intnum@%d\" must be > 0; is %d",
                        i, (int) val);
                        i, (int) val);
          }
          }
      }
      }
}
}
 
 
/* Handle the after-N-ticks "poll event", calling the poll-the-fd
/* Handle the after-N-ticks "poll event", calling the poll-the-fd
   method.  Update the period.  */
   method.  Update the period.  */
 
 
static void
static void
do_poll_event (struct hw *me, void *data)
do_poll_event (struct hw *me, void *data)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned32 new_period;
  unsigned32 new_period;
 
 
  if (rv->dummy != NULL)
  if (rv->dummy != NULL)
    return;
    return;
 
 
  hw_rv_poll_once (me);
  hw_rv_poll_once (me);
  if (rv->fd >= 0)
  if (rv->fd >= 0)
    rv->poll_callback
    rv->poll_callback
      = hw_event_queue_schedule (me, rv->next_period, do_poll_event, NULL);
      = hw_event_queue_schedule (me, rv->next_period, do_poll_event, NULL);
 
 
  new_period = rv->next_period * 2;
  new_period = rv->next_period * 2;
  if (new_period <= rv->max_tick_poll_interval)
  if (new_period <= rv->max_tick_poll_interval)
    rv->next_period = new_period;
    rv->next_period = new_period;
}
}
 
 
/* HW tree traverse function for hw_rv_add_init.  */
/* HW tree traverse function for hw_rv_add_init.  */
 
 
static void
static void
hw_rv_add_poller (struct hw *me, void *data)
hw_rv_add_poller (struct hw *me, void *data)
{
{
  hw_rv_device *rv;
  hw_rv_device *rv;
 
 
  if (hw_family (me) == NULL
  if (hw_family (me) == NULL
      || strcmp (hw_family (me), RV_FAMILY_NAME) != 0)
      || strcmp (hw_family (me), RV_FAMILY_NAME) != 0)
    return;
    return;
 
 
  rv = (hw_rv_device *) hw_data (me);
  rv = (hw_rv_device *) hw_data (me);
  if (rv->poll_callback != NULL)
  if (rv->poll_callback != NULL)
    return;
    return;
 
 
  rv->poll_callback
  rv->poll_callback
    = hw_event_queue_schedule (me, 1, do_poll_event, NULL);
    = hw_event_queue_schedule (me, 1, do_poll_event, NULL);
}
}
 
 
/* Simulator module init function for hw_rv_add_init.  */
/* Simulator module init function for hw_rv_add_init.  */
 
 
/* FIXME: For the call so hw_tree_traverse, we need to know that the
/* FIXME: For the call so hw_tree_traverse, we need to know that the
   first member of struct sim_hw is the struct hw *root, but there's
   first member of struct sim_hw is the struct hw *root, but there's
   no accessor method and struct sim_hw is defined in sim-hw.c only.
   no accessor method and struct sim_hw is defined in sim-hw.c only.
   Hence this hack, until an accessor is added, or there's a traverse
   Hence this hack, until an accessor is added, or there's a traverse
   function that takes a SIM_DESC argument.  */
   function that takes a SIM_DESC argument.  */
struct sim_hw { struct hw *tree; };
struct sim_hw { struct hw *tree; };
 
 
static SIM_RC
static SIM_RC
hw_rv_add_rv_pollers (SIM_DESC sd)
hw_rv_add_rv_pollers (SIM_DESC sd)
{
{
  hw_tree_traverse (STATE_HW (sd)->tree, hw_rv_add_poller, NULL, NULL);
  hw_tree_traverse (STATE_HW (sd)->tree, hw_rv_add_poller, NULL, NULL);
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
/* We need to add events for polling, but we can't add one from the
/* We need to add events for polling, but we can't add one from the
   finish-function, and there are no other call points, at least for
   finish-function, and there are no other call points, at least for
   instances without "reg" (when there are just DMA requests from the
   instances without "reg" (when there are just DMA requests from the
   remote end; no locally initiated activity).  Therefore we add a
   remote end; no locally initiated activity).  Therefore we add a
   simulator module init function, but those don't have private
   simulator module init function, but those don't have private
   payload arguments; just a SD argument.  We cope by parsing the HW
   payload arguments; just a SD argument.  We cope by parsing the HW
   root and making sure *all* "rv":s have poll callbacks installed.
   root and making sure *all* "rv":s have poll callbacks installed.
   Luckily, this is just an initialization step, and not many
   Luckily, this is just an initialization step, and not many
   simultaneous instances of rv are expected: we get a N**2 complexity
   simultaneous instances of rv are expected: we get a N**2 complexity
   for visits to each rv node by this method.  */
   for visits to each rv node by this method.  */
 
 
static void
static void
hw_rv_add_init (struct hw *me)
hw_rv_add_init (struct hw *me)
{
{
  sim_module_add_init_fn (hw_system (me), hw_rv_add_rv_pollers);
  sim_module_add_init_fn (hw_system (me), hw_rv_add_rv_pollers);
}
}
 
 
/* Open up a connection to the other side.  Abort on errors.  */
/* Open up a connection to the other side.  Abort on errors.  */
 
 
static void
static void
hw_rv_init_socket (struct hw *me)
hw_rv_init_socket (struct hw *me)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  int sock;
  int sock;
  struct sockaddr_in server;
  struct sockaddr_in server;
 
 
  rv->fd = -1;
  rv->fd = -1;
 
 
  if (rv->dummy != NULL)
  if (rv->dummy != NULL)
    return;
    return;
 
 
  memset (&server, 0, sizeof (server));
  memset (&server, 0, sizeof (server));
  server.sin_family = AF_INET;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr (rv->host);
  server.sin_addr.s_addr = inet_addr (rv->host);
 
 
  /* Solaris 2.7 lacks this macro.  */
  /* Solaris 2.7 lacks this macro.  */
#ifndef INADDR_NONE
#ifndef INADDR_NONE
#define INADDR_NONE -1
#define INADDR_NONE -1
#endif
#endif
 
 
  if (server.sin_addr.s_addr == INADDR_NONE)
  if (server.sin_addr.s_addr == INADDR_NONE)
    {
    {
      struct hostent *h;
      struct hostent *h;
      h = gethostbyname (rv->host);
      h = gethostbyname (rv->host);
      if (h != NULL)
      if (h != NULL)
        {
        {
          memcpy (&server.sin_addr, h->h_addr, h->h_length);
          memcpy (&server.sin_addr, h->h_addr, h->h_length);
          server.sin_family = h->h_addrtype;
          server.sin_family = h->h_addrtype;
        }
        }
      else
      else
        hw_abort (me, "can't resolve host %s", rv->host);
        hw_abort (me, "can't resolve host %s", rv->host);
    }
    }
 
 
  server.sin_port = htons (rv->port);
  server.sin_port = htons (rv->port);
  sock = socket (AF_INET, SOCK_STREAM, 0);
  sock = socket (AF_INET, SOCK_STREAM, 0);
 
 
  if (sock < 0)
  if (sock < 0)
    hw_abort (me, "can't get a socket for %s:%d connection",
    hw_abort (me, "can't get a socket for %s:%d connection",
              rv->host, rv->port);
              rv->host, rv->port);
 
 
  if (connect (sock, (struct sockaddr *) &server, sizeof server) >= 0)
  if (connect (sock, (struct sockaddr *) &server, sizeof server) >= 0)
    {
    {
      rv->fd = sock;
      rv->fd = sock;
 
 
      /* FIXME: init packet here.  Maybe start packet too.  */
      /* FIXME: init packet here.  Maybe start packet too.  */
      if (rv->watchdog_interval != 0)
      if (rv->watchdog_interval != 0)
        hw_rv_send_wdog (me);
        hw_rv_send_wdog (me);
    }
    }
  else
  else
    /* Stash the errno for later display, if some connection activity
    /* Stash the errno for later display, if some connection activity
       is requested.  Don't emit an error here; we might have been
       is requested.  Don't emit an error here; we might have been
       called just for test purposes.  */
       called just for test purposes.  */
    rv->saved_errno = errno;
    rv->saved_errno = errno;
}
}
 
 
/* Local rv register reads end up here.  */
/* Local rv register reads end up here.  */
 
 
static unsigned int
static unsigned int
hw_rv_reg_read (struct hw *me,
hw_rv_reg_read (struct hw *me,
                void *dest,
                void *dest,
                int space,
                int space,
                unsigned_word addr,
                unsigned_word addr,
                unsigned int nr_bytes)
                unsigned int nr_bytes)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  unsigned8 addr_data[8] = "";
  unsigned8 addr_data[8] = "";
  unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
  unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
  unsigned int len = 8;
  unsigned int len = 8;
 
 
  if (nr_bytes != 4)
  if (nr_bytes != 4)
    hw_abort (me, "must be four byte read");
    hw_abort (me, "must be four byte read");
 
 
  if (addr == rv->mbox_address)
  if (addr == rv->mbox_address)
    hw_abort (me, "invalid read of mbox address 0x%x",
    hw_abort (me, "invalid read of mbox address 0x%x",
              (unsigned) rv->mbox_address);
              (unsigned) rv->mbox_address);
 
 
  memcpy (addr_data, &a_l, 4);
  memcpy (addr_data, &a_l, 4);
  HW_TRACE ((me, "REG R 0x%x", addr));
  HW_TRACE ((me, "REG R 0x%x", addr));
  if (rv->dummy != NULL)
  if (rv->dummy != NULL)
    {
    {
      len = 8;
      len = 8;
      memcpy (addr_data + 4, rv->dummy + addr - rv->reg_address, 4);
      memcpy (addr_data + 4, rv->dummy + addr - rv->reg_address, 4);
    }
    }
  else
  else
    {
    {
      hw_rv_send (me, RV_READ_CMD, addr_data, len);
      hw_rv_send (me, RV_READ_CMD, addr_data, len);
      hw_rv_handle_incoming (me, RV_READ_CMD, addr_data, &len);
      hw_rv_handle_incoming (me, RV_READ_CMD, addr_data, &len);
    }
    }
 
 
  if (len != 8)
  if (len != 8)
    hw_abort (me, "read %d != 8 bytes returned", len);
    hw_abort (me, "read %d != 8 bytes returned", len);
  HW_TRACE ((me, ":= 0x%02x%02x%02x%02x",
  HW_TRACE ((me, ":= 0x%02x%02x%02x%02x",
             addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
             addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
  memcpy (dest, addr_data + 4, 4);
  memcpy (dest, addr_data + 4, 4);
  return nr_bytes;
  return nr_bytes;
}
}
 
 
/* Local rv mbox requests (handle or put) end up here.  */
/* Local rv mbox requests (handle or put) end up here.  */
 
 
static void
static void
hw_rv_mbox (struct hw *me, unsigned_word address)
hw_rv_mbox (struct hw *me, unsigned_word address)
{
{
  unsigned8 buf[256+3];
  unsigned8 buf[256+3];
  unsigned int cmd;
  unsigned int cmd;
  unsigned int rlen;
  unsigned int rlen;
  unsigned32 i;
  unsigned32 i;
  unsigned int len
  unsigned int len
    = hw_dma_read_buffer (me, buf, 0, address, 3);
    = hw_dma_read_buffer (me, buf, 0, address, 3);
 
 
  if (len != 3)
  if (len != 3)
    hw_abort (me, "mbox read %d != 3 bytes returned", len);
    hw_abort (me, "mbox read %d != 3 bytes returned", len);
 
 
  cmd = buf[2];
  cmd = buf[2];
  if (cmd != RV_MBOX_HANDLE_CMD && cmd != RV_MBOX_PUT_CMD)
  if (cmd != RV_MBOX_HANDLE_CMD && cmd != RV_MBOX_PUT_CMD)
    hw_abort (me, "unsupported mbox command %d", cmd);
    hw_abort (me, "unsupported mbox command %d", cmd);
 
 
  len = buf[0] + buf[1]*256;
  len = buf[0] + buf[1]*256;
 
 
  if (len > sizeof (buf))
  if (len > sizeof (buf))
    hw_abort (me, "mbox cmd %d send size %d unsupported", cmd, len);
    hw_abort (me, "mbox cmd %d send size %d unsupported", cmd, len);
 
 
  rlen = hw_dma_read_buffer (me, buf + 3, 0, address + 3, len - 3);
  rlen = hw_dma_read_buffer (me, buf + 3, 0, address + 3, len - 3);
  if (rlen != len - 3)
  if (rlen != len - 3)
    hw_abort (me, "mbox read %d != %d bytes returned", rlen, len - 3);
    hw_abort (me, "mbox read %d != %d bytes returned", rlen, len - 3);
 
 
  HW_TRACE ((me, "MBOX %s 0x%x..0x%x",
  HW_TRACE ((me, "MBOX %s 0x%x..0x%x",
             cmd == RV_MBOX_HANDLE_CMD ? "H" : "P",
             cmd == RV_MBOX_HANDLE_CMD ? "H" : "P",
             address, address + len - 1));
             address, address + len - 1));
  for (i = 0; i < rlen; i += 8)
  for (i = 0; i < rlen; i += 8)
    HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
    HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
               address + 3 + i,
               address + 3 + i,
               buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
               buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
               buf[9+i], buf[10+i]));
               buf[9+i], buf[10+i]));
 
 
  len -= 3;
  len -= 3;
  hw_rv_send (me, cmd, buf + 3, len);
  hw_rv_send (me, cmd, buf + 3, len);
 
 
  /* Note: both ..._PUT and ..._HANDLE get the ..._HANDLE reply.  */
  /* Note: both ..._PUT and ..._HANDLE get the ..._HANDLE reply.  */
  hw_rv_handle_incoming (me, RV_MBOX_HANDLE_CMD, buf + 3, &len);
  hw_rv_handle_incoming (me, RV_MBOX_HANDLE_CMD, buf + 3, &len);
  if (len > sizeof (buf))
  if (len > sizeof (buf))
    hw_abort (me, "mbox cmd %d receive size %d unsupported", cmd, len);
    hw_abort (me, "mbox cmd %d receive size %d unsupported", cmd, len);
  HW_TRACE ((me, "-> 0x%x..0x%x", address, address + len + 3 - 1));
  HW_TRACE ((me, "-> 0x%x..0x%x", address, address + len + 3 - 1));
  for (i = 0; i < len; i += 8)
  for (i = 0; i < len; i += 8)
    HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
    HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
               address + 3 + i,
               address + 3 + i,
               buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
               buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
               buf[9+i], buf[10+i]));
               buf[9+i], buf[10+i]));
 
 
  len += 3;
  len += 3;
  buf[0] = len & 255;
  buf[0] = len & 255;
  buf[1] = len / 256;
  buf[1] = len / 256;
  rlen = hw_dma_write_buffer (me, buf, 0, address, len, 0);
  rlen = hw_dma_write_buffer (me, buf, 0, address, len, 0);
  if (rlen != len)
  if (rlen != len)
    hw_abort (me, "mbox write %d != %d bytes", rlen, len);
    hw_abort (me, "mbox write %d != %d bytes", rlen, len);
}
}
 
 
/* Local rv register writes end up here.  */
/* Local rv register writes end up here.  */
 
 
static unsigned int
static unsigned int
hw_rv_reg_write (struct hw *me,
hw_rv_reg_write (struct hw *me,
                 const void *source,
                 const void *source,
                 int space,
                 int space,
                 unsigned_word addr,
                 unsigned_word addr,
                 unsigned int nr_bytes)
                 unsigned int nr_bytes)
{
{
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
  hw_rv_device *rv = (hw_rv_device *) hw_data (me);
 
 
  unsigned8 addr_data[8] = "";
  unsigned8 addr_data[8] = "";
  unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
  unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
  unsigned int len = 8;
  unsigned int len = 8;
 
 
  if (nr_bytes != 4)
  if (nr_bytes != 4)
    hw_abort (me, "must be four byte write");
    hw_abort (me, "must be four byte write");
 
 
  memcpy (addr_data, &a_l, 4);
  memcpy (addr_data, &a_l, 4);
  memcpy (addr_data + 4, source, 4);
  memcpy (addr_data + 4, source, 4);
 
 
  if (addr == rv->mbox_address)
  if (addr == rv->mbox_address)
    {
    {
      unsigned32 mbox_addr_le;
      unsigned32 mbox_addr_le;
      if (rv->dummy != NULL)
      if (rv->dummy != NULL)
        hw_abort (me, "mbox not supported for a dummy instance");
        hw_abort (me, "mbox not supported for a dummy instance");
      memcpy (&mbox_addr_le, source, 4);
      memcpy (&mbox_addr_le, source, 4);
      hw_rv_mbox (me, LE2H_4 (mbox_addr_le));
      hw_rv_mbox (me, LE2H_4 (mbox_addr_le));
      return nr_bytes;
      return nr_bytes;
    }
    }
 
 
  HW_TRACE ((me, "REG W 0x%x := 0x%02x%02x%02x%02x", addr,
  HW_TRACE ((me, "REG W 0x%x := 0x%02x%02x%02x%02x", addr,
             addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
             addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
  if (rv->dummy != NULL)
  if (rv->dummy != NULL)
    {
    {
      len = 8;
      len = 8;
      memcpy (rv->dummy + addr - rv->reg_address, addr_data + 4, 4);
      memcpy (rv->dummy + addr - rv->reg_address, addr_data + 4, 4);
    }
    }
  else
  else
    {
    {
      hw_rv_send (me, RV_WRITE_CMD, addr_data, len);
      hw_rv_send (me, RV_WRITE_CMD, addr_data, len);
      hw_rv_handle_incoming (me, RV_WRITE_CMD, addr_data, &len);
      hw_rv_handle_incoming (me, RV_WRITE_CMD, addr_data, &len);
    }
    }
 
 
  if (len != 8)
  if (len != 8)
    hw_abort (me, "read %d != 8 bytes returned", len);
    hw_abort (me, "read %d != 8 bytes returned", len);
 
 
  /* We had an access: tighten up all slack.  */
  /* We had an access: tighten up all slack.  */
  rv->next_period = 1;
  rv->next_period = 1;
 
 
  return nr_bytes;
  return nr_bytes;
}
}
 
 
/* Instance initializer function.  */
/* Instance initializer function.  */
 
 
static void
static void
hw_rv_finish (struct hw *me)
hw_rv_finish (struct hw *me)
{
{
  hw_rv_device *rv = HW_ZALLOC (me, hw_rv_device);
  hw_rv_device *rv = HW_ZALLOC (me, hw_rv_device);
  int i;
  int i;
  const struct hw_property *mem_prop;
  const struct hw_property *mem_prop;
  const struct hw_property *dummy_prop;
  const struct hw_property *dummy_prop;
  const struct hw_property *mbox_prop;
  const struct hw_property *mbox_prop;
 
 
  set_hw_data (me, rv);
  set_hw_data (me, rv);
 
 
#undef RV_GET_IPROP
#undef RV_GET_IPROP
#undef RV_GET_PROP
#undef RV_GET_PROP
#define RV_GET_PROP(T, N, M, D)                         \
#define RV_GET_PROP(T, N, M, D)                         \
  do                                                    \
  do                                                    \
    {                                                   \
    {                                                   \
      if (hw_find_property (me, N) != NULL)             \
      if (hw_find_property (me, N) != NULL)             \
        rv->M = hw_find_ ## T ## _property (me, N);     \
        rv->M = hw_find_ ## T ## _property (me, N);     \
      else                                              \
      else                                              \
        rv->M = (D);                                    \
        rv->M = (D);                                    \
    }                                                   \
    }                                                   \
  while (0)
  while (0)
#define RV_GET_IPROP(N, M, D) RV_GET_PROP (integer, N, M, D)
#define RV_GET_IPROP(N, M, D) RV_GET_PROP (integer, N, M, D)
 
 
  RV_GET_PROP (string, "host", host, "127.0.0.1");
  RV_GET_PROP (string, "host", host, "127.0.0.1");
  RV_GET_IPROP ("port", port, 10000);
  RV_GET_IPROP ("port", port, 10000);
  RV_GET_IPROP ("remote-reg", remote_reg_address, 0);
  RV_GET_IPROP ("remote-reg", remote_reg_address, 0);
  RV_GET_IPROP ("max-poll-ticks", max_tick_poll_interval, 10000);
  RV_GET_IPROP ("max-poll-ticks", max_tick_poll_interval, 10000);
  RV_GET_IPROP ("watchdog-interval", watchdog_interval, 30);
  RV_GET_IPROP ("watchdog-interval", watchdog_interval, 30);
  RV_GET_IPROP ("remote-mem", remote_mem_address, 0);
  RV_GET_IPROP ("remote-mem", remote_mem_address, 0);
  RV_GET_IPROP ("mem-burst-mask", mem_burst_mask, 0xffff);
  RV_GET_IPROP ("mem-burst-mask", mem_burst_mask, 0xffff);
  RV_GET_IPROP ("intmultiple", intmultiple, 0);
  RV_GET_IPROP ("intmultiple", intmultiple, 0);
 
 
  set_hw_io_read_buffer (me, hw_rv_reg_read);
  set_hw_io_read_buffer (me, hw_rv_reg_read);
  set_hw_io_write_buffer (me, hw_rv_reg_write);
  set_hw_io_write_buffer (me, hw_rv_reg_write);
  set_hw_ports (me, hw_rv_ports);
  set_hw_ports (me, hw_rv_ports);
  rv->next_period = 1;
  rv->next_period = 1;
 
 
  /* FIXME: We only support zero or one reg and zero or one mem area.  */
  /* FIXME: We only support zero or one reg and zero or one mem area.  */
  if (hw_find_property (me, "reg") != NULL)
  if (hw_find_property (me, "reg") != NULL)
    {
    {
      reg_property_spec reg;
      reg_property_spec reg;
      if (hw_find_reg_array_property (me, "reg", 0, &reg))
      if (hw_find_reg_array_property (me, "reg", 0, &reg))
        {
        {
          unsigned_word attach_address;
          unsigned_word attach_address;
          int attach_space;
          int attach_space;
          unsigned int attach_size;
          unsigned int attach_size;
 
 
          hw_unit_address_to_attach_address (hw_parent (me),
          hw_unit_address_to_attach_address (hw_parent (me),
                                             &reg.address,
                                             &reg.address,
                                             &attach_space,
                                             &attach_space,
                                             &attach_address,
                                             &attach_address,
                                             me);
                                             me);
          rv->reg_address = attach_address;
          rv->reg_address = attach_address;
          hw_unit_size_to_attach_size (hw_parent (me),
          hw_unit_size_to_attach_size (hw_parent (me),
                                       &reg.size,
                                       &reg.size,
                                       &attach_size, me);
                                       &attach_size, me);
          rv->reg_size = attach_size;
          rv->reg_size = attach_size;
          if ((attach_address & 3) != 0)
          if ((attach_address & 3) != 0)
            hw_abort (me, "register block must be 4 byte aligned");
            hw_abort (me, "register block must be 4 byte aligned");
          hw_attach_address (hw_parent (me),
          hw_attach_address (hw_parent (me),
                             0,
                             0,
                             attach_space, attach_address, attach_size,
                             attach_space, attach_address, attach_size,
                             me);
                             me);
        }
        }
      else
      else
        hw_abort (me, "property \"reg\" has the wrong type");
        hw_abort (me, "property \"reg\" has the wrong type");
    }
    }
 
 
  dummy_prop = hw_find_property (me, "dummy");
  dummy_prop = hw_find_property (me, "dummy");
  if (dummy_prop != NULL)
  if (dummy_prop != NULL)
    {
    {
      if (rv->reg_size == 0)
      if (rv->reg_size == 0)
        hw_abort (me, "dummy argument requires a \"reg\" property");
        hw_abort (me, "dummy argument requires a \"reg\" property");
 
 
      if (hw_property_type (dummy_prop) == integer_property)
      if (hw_property_type (dummy_prop) == integer_property)
        {
        {
          unsigned32 dummyfill = hw_find_integer_property (me, "dummy");
          unsigned32 dummyfill = hw_find_integer_property (me, "dummy");
          unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
          unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
          memset (dummymem, dummyfill, rv->reg_size);
          memset (dummymem, dummyfill, rv->reg_size);
          rv->dummy = dummymem;
          rv->dummy = dummymem;
        }
        }
      else
      else
        {
        {
          const char *dummyarg = hw_find_string_property (me, "dummy");
          const char *dummyarg = hw_find_string_property (me, "dummy");
          unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
          unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
          FILE *f = fopen (dummyarg, "rb");
          FILE *f = fopen (dummyarg, "rb");
 
 
          if (f == NULL)
          if (f == NULL)
            hw_abort (me, "opening dummy-file \"%s\": %s",
            hw_abort (me, "opening dummy-file \"%s\": %s",
                      dummyarg, strerror (errno));
                      dummyarg, strerror (errno));
          if (fread (dummymem, 1, rv->reg_size, f) != rv->reg_size)
          if (fread (dummymem, 1, rv->reg_size, f) != rv->reg_size)
            hw_abort (me, "reading dummy-file \"%s\": %s",
            hw_abort (me, "reading dummy-file \"%s\": %s",
                      dummyarg, strerror (errno));
                      dummyarg, strerror (errno));
          fclose (f);
          fclose (f);
          rv->dummy = dummymem;
          rv->dummy = dummymem;
        }
        }
    }
    }
 
 
  mbox_prop = hw_find_property (me, "mbox");
  mbox_prop = hw_find_property (me, "mbox");
  if (mbox_prop != NULL)
  if (mbox_prop != NULL)
    {
    {
      if (hw_property_type (mbox_prop) == integer_property)
      if (hw_property_type (mbox_prop) == integer_property)
        {
        {
          signed_cell attach_address_sc
          signed_cell attach_address_sc
            = hw_find_integer_property (me, "mbox");
            = hw_find_integer_property (me, "mbox");
 
 
          rv->mbox_address = (unsigned32) attach_address_sc;
          rv->mbox_address = (unsigned32) attach_address_sc;
          hw_attach_address (hw_parent (me),
          hw_attach_address (hw_parent (me),
                             0,
                             0,
                             0, (unsigned32) attach_address_sc, 4, me);
                             0, (unsigned32) attach_address_sc, 4, me);
        }
        }
      else
      else
        hw_abort (me, "property \"mbox\" has the wrong type");
        hw_abort (me, "property \"mbox\" has the wrong type");
    }
    }
 
 
  mem_prop = hw_find_property (me, "mem");
  mem_prop = hw_find_property (me, "mem");
  if (mem_prop != NULL)
  if (mem_prop != NULL)
    {
    {
      signed_cell attach_address_sc;
      signed_cell attach_address_sc;
      signed_cell attach_size_sc;
      signed_cell attach_size_sc;
 
 
      /* Only specific names are reg_array_properties, the rest are
      /* Only specific names are reg_array_properties, the rest are
         array_properties.  */
         array_properties.  */
      if (hw_property_type (mem_prop) == array_property
      if (hw_property_type (mem_prop) == array_property
          && hw_property_sizeof_array (mem_prop) == 2 * sizeof (attach_address_sc)
          && hw_property_sizeof_array (mem_prop) == 2 * sizeof (attach_address_sc)
          && hw_find_integer_array_property (me, "mem", 0, &attach_address_sc)
          && hw_find_integer_array_property (me, "mem", 0, &attach_address_sc)
          && hw_find_integer_array_property (me, "mem", 1, &attach_size_sc))
          && hw_find_integer_array_property (me, "mem", 1, &attach_size_sc))
        {
        {
          /* Unfortunate choice of types forces us to dance around a bit.  */
          /* Unfortunate choice of types forces us to dance around a bit.  */
          rv->mem_address = (unsigned32) attach_address_sc;
          rv->mem_address = (unsigned32) attach_address_sc;
          rv->mem_size = (unsigned32) attach_size_sc;
          rv->mem_size = (unsigned32) attach_size_sc;
          if ((attach_address_sc & 3) != 0)
          if ((attach_address_sc & 3) != 0)
            hw_abort (me, "memory block must be 4 byte aligned");
            hw_abort (me, "memory block must be 4 byte aligned");
        }
        }
      else
      else
        hw_abort (me, "property \"mem\" has the wrong type");
        hw_abort (me, "property \"mem\" has the wrong type");
    }
    }
 
 
  hw_rv_map_ints (me);
  hw_rv_map_ints (me);
 
 
  hw_rv_init_socket (me);
  hw_rv_init_socket (me);
 
 
  /* We need an extra initialization pass, after all others currently
  /* We need an extra initialization pass, after all others currently
     scheduled (mostly, after the simulation events machinery has been
     scheduled (mostly, after the simulation events machinery has been
     initialized so the events we want don't get thrown out).  */
     initialized so the events we want don't get thrown out).  */
  hw_rv_add_init (me);
  hw_rv_add_init (me);
}
}
 
 
/* Our root structure; see dv-* build machinery for usage.  */
/* Our root structure; see dv-* build machinery for usage.  */
 
 
const struct hw_descriptor dv_rv_descriptor[] = {
const struct hw_descriptor dv_rv_descriptor[] = {
  { RV_FAMILY_NAME, hw_rv_finish },
  { RV_FAMILY_NAME, hw_rv_finish },
  { NULL }
  { NULL }
};
};
 
 

powered by: WebSVN 2.1.0

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