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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [sim/] [or32/] [wrapper.c] - Diff between revs 216 and 225

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 216 Rev 225
Line 30... Line 30...
 
 
#include <errno.h>
#include <errno.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <signal.h>
#include <signal.h>
 
#include <string.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/un.h>
#include <unistd.h>
#include <unistd.h>
 
 
Line 44... Line 45...
#include "targ-vals.h"
#include "targ-vals.h"
 
 
#include "or1ksim.h"
#include "or1ksim.h"
#include "or32sim.h"
#include "or32sim.h"
 
 
/*!A global record of the simulator description */
 
static SIM_DESC  global_sd = NULL;
 
 
 
/*!The last reason we stopped */
 
enum sim_stop  last_reason = sim_running;
 
 
 
/*!The last return code from a resume/step call */
 
static unsigned long int  last_rc = 0;
 
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/*!Create a fully initialized simulator instance.
/*!Create a fully initialized simulator instance.
 
 
   This function is called when the simulator is selected from the gdb command
   This function is called when the simulator is selected from the gdb command
Line 72... Line 64...
   memory reads/writes, register fetch/stores and a resume).
   memory reads/writes, register fetch/stores and a resume).
 
 
   For a process simulator, the process is not created until a call to
   For a process simulator, the process is not created until a call to
   sim_create_inferior.
   sim_create_inferior.
 
 
   This function creates a socket pair, which will be used to communicate to
   We do the following on a first call.
   and from the process model, then forks off a process to run the SystemC
   - parse the options
   model. Communication is via RSP packets across the socket pair, but with
   -
   simplified hand-shaking.
   @todo Eventually we should use the option parser built into the GDB
 
         simulator (see common/sim-options.h). However since this is minimally
 
         documented, and we have only the one option, for now we do it
 
         ourselves.
 
 
   @note We seem to capable of being called twice. We use the static
   @note We seem to capable of being called twice. We use the static
         "global_sd" variable to keep track of this. Second and subsequent
         "global_sd" variable to keep track of this. Second and subsequent
         calls do nothing, but return the previously opened simulator
         calls do nothing, but return the previously opened simulator
         description.
         description.
Line 110... Line 105...
sim_open (SIM_OPEN_KIND                kind,
sim_open (SIM_OPEN_KIND                kind,
          struct host_callback_struct *callback,
          struct host_callback_struct *callback,
          struct bfd                  *abfd,
          struct bfd                  *abfd,
          char                        *argv[])
          char                        *argv[])
{
{
  int   argc;
  /*!A global record of the simulator description */
  char *config_file;
  static SIM_DESC  static_sd = NULL;
  char *image_file;
 
 
 
  /* If the global_sd is currently set, we just return it. */
  int    local_argc;                    /* Our local argv with extra args */
  if (NULL != global_sd)
  char **local_argv;
 
 
 
  /* If static_sd is not yet allocated, we allocate it and mark the simulator
 
     as not yet open. */
 
  if (NULL == static_sd)
    {
    {
      return  global_sd;
      static_sd = (SIM_DESC) malloc (sizeof (*static_sd));
 
      static_sd->sim_open = 0;
    }
    }
 
 
  /* Eventually we should use the option parser built into the GDB simulator
  /* If this is a second call, we cannot take any new configuration
     (see common/sim-options.h). However since this is minimally documented,
     arguments. We silently ignore them. */
     and we have only the one option, for now we do it ourselves. */
  if (!static_sd->sim_open)
 
    {
  /* Count the number of arguments */
      int    argc;                      /* How many args originally */
  for (argc = 0; NULL != argv[argc]; argc++)
      int    i;                         /* For local argv */
 
      int    mem_defined_p = 0;          /* Have we requested a memory size? */
 
 
 
      /* Count the number of arguments and see if we have specified either a
 
         config file or a memory size. */
 
      for (argc = 1; NULL != argv[argc]; argc++)
    {
    {
      /* printf ("argv[%d] = %s\n", argc, argv[argc]); */
      /* printf ("argv[%d] = %s\n", argc, argv[argc]); */
    }
 
 
 
  /* Configuration file may be passed using the -f <filename> */
          if ((0 == strcmp (argv[argc], "-f"))    ||
  if ((argc > 2) && (0 == strcmp (argv[1], "-f")))
              (0 == strcmp (argv[argc], "-file")) ||
 
              (0 == strcmp (argv[argc], "-m"))    ||
 
              (0 == strcmp (argv[argc], "-memory")))
    {
    {
      config_file = argv[2];
              mem_defined_p = 1;
      image_file  = argc > 3 ? argv[3] : NULL;
 
    }
    }
  else
 
    {
 
      config_file = NULL;
 
      image_file  = argc > 1 ? argv[1] : NULL;
 
    }
    }
 
 
  /* Initialize the simulator. No class image nor upcalls */
      /* If we have no memory defined, we give it a default 8MB. We also always
  if (0 == or1ksim_init (config_file, image_file, NULL, NULL, NULL))
         run quiet. So we must define our own argument vector */
 
      local_argc = mem_defined_p ? argc + 1 : argc + 3;
 
      local_argv = malloc ((local_argc + 1) * sizeof (char *));
 
 
 
      for (i = 0 ; i < argc; i++)
    {
    {
      global_sd = (SIM_DESC) malloc (sizeof (*global_sd));
          local_argv[i] = argv[i];
 
        }
 
 
 
      local_argv[i++] = "--quiet";
 
 
      global_sd->callback = callback;
      if (!mem_defined_p)
      global_sd->is_debug = (kind == SIM_OPEN_DEBUG);
        {
      global_sd->myname   = (char *)xstrdup (argv[0]);
          local_argv[i++] = "--memory";
 
          local_argv[i++] = "8M";
    }
    }
 
 
  return  global_sd;
      local_argv[i] = NULL;
 
    }
 
 
 
  /* We just pass the arguments to the simulator initialization. No class
 
     image nor upcalls. Having initialized, stall the processor, free the
 
     argument vector and return the SD (or NULL on failure) */
 
  if (0 == or1ksim_init (local_argc, local_argv, NULL, NULL, NULL))
 
    {
 
 
 
      static_sd->callback    = callback;
 
      static_sd->is_debug    = (kind == SIM_OPEN_DEBUG);
 
      static_sd->myname      = (char *)xstrdup (argv[0]);
 
      static_sd->sim_open    = 1;
 
      static_sd->last_reason = sim_running;
 
      static_sd->last_rc     = TARGET_SIGNAL_NONE;
 
      static_sd->entry_point = OR32_RESET_EXCEPTION;
 
      static_sd->resume_npc  = OR32_RESET_EXCEPTION;
 
 
 
      or1ksim_set_stall_state (0);
 
      free (local_argv);
 
      return  static_sd;
 
    }
 
  else
 
    {
 
      /* On failure return a NULL sd */
 
      free (local_argv);
 
      return  NULL;
 
    }
}       /* sim_open () */
}       /* sim_open () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/*!Destroy a simulator instance.
/*!Destroy a simulator instance.
 
 
   This may involve freeing target memory and closing any open files and
   We only have one instance, but we mark it as closed, so it can be reused.
   mmap'd areas.
 
 
 
   We cannot assume sim_kill () has already been called.
 
 
 
   @param[in] sd        Simulation descriptor from sim_open ().
   @param[in] sd        Simulation descriptor from sim_open ().
   @param[in] quitting  Non-zero if we cannot hang on errors.                */
   @param[in] quitting  Non-zero if we cannot hang on errors.                */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void
void
sim_close (SIM_DESC  sd,
sim_close (SIM_DESC  sd,
           int       quitting)
           int       quitting)
{
{
  if (NULL != sd)
  if (NULL == sd)
 
    {
 
      fprintf (stderr,
 
               "Warning: Attempt to close non-open simulation: ignored.\n");
 
    }
 
  else
    {
    {
      free (sd->myname);
      free (sd->myname);
      free (sd);
      sd->sim_open = 0;
    }
    }
}       /* sim_close () */
}       /* sim_close () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
Line 255... Line 291...
   counter and stack pointer set accordingly.
   counter and stack pointer set accordingly.
 
 
   ABFD, if not NULL, provides initial processor state information.
   ABFD, if not NULL, provides initial processor state information.
   ARGV and ENV, if non NULL, are NULL terminated lists of pointers.
   ARGV and ENV, if non NULL, are NULL terminated lists of pointers.
 
 
 
   We perform the following steps:
 
   - stall the processor
 
   - set the entry point to the entry point in the BFD, or the reset
 
     vector if the BFD is not available.
 
   - set the resumption NPC to the reset vector. We always do this, to ensure
 
     the library is initialized.
 
 
   @param[in] sd    Simulation descriptor from sim_open ().
   @param[in] sd    Simulation descriptor from sim_open ().
   @param[in] abfd  If non-NULL provides initial processor state information.
   @param[in] abfd  If non-NULL provides initial processor state information.
   @param[in] argv  Vector of arguments to the program. We don't use this
   @param[in] argv  Vector of arguments to the program. We don't use this
   @param[in] env   Vector of environment data. We don't use this.
   @param[in] env   Vector of environment data. We don't use this.
 
 
Line 268... Line 311...
sim_create_inferior (SIM_DESC     sd,
sim_create_inferior (SIM_DESC     sd,
                     struct bfd  *abfd,
                     struct bfd  *abfd,
                     char       **argv  ATTRIBUTE_UNUSED,
                     char       **argv  ATTRIBUTE_UNUSED,
                     char       **env   ATTRIBUTE_UNUSED)
                     char       **env   ATTRIBUTE_UNUSED)
{
{
  /* We set the starting PC if we have that data */
  or1ksim_set_stall_state (1);
  unsigned long int  pc     = (NULL == abfd) ? 0 : bfd_get_start_address (abfd);
  sd->entry_point = (NULL == abfd) ? OR32_RESET_EXCEPTION :
  unsigned char     *pc_ptr = (unsigned char *)(&pc);
                                    bfd_get_start_address (abfd);
 
  sd->resume_npc  = OR32_RESET_EXCEPTION;
 
 
  sim_store_register (sd, OR32_NPC_REGNUM, pc_ptr, sizeof (pc));
 
  return  SIM_RC_OK;
  return  SIM_RC_OK;
 
 
}       /* sim_create_inferior () */
}       /* sim_create_inferior () */
 
 
 
 
Line 295... Line 338...
sim_read (SIM_DESC       sd  ATTRIBUTE_UNUSED,
sim_read (SIM_DESC       sd  ATTRIBUTE_UNUSED,
          SIM_ADDR       mem,
          SIM_ADDR       mem,
          unsigned char *buf,
          unsigned char *buf,
          int            len)
          int            len)
{
{
  return  or1ksim_read_mem (buf, (unsigned int) mem, len);
  int res = or1ksim_read_mem (mem, buf, len);
 
 
 
  /* printf ("Reading %d bytes from 0x%08p\n", len, mem); */
 
 
 
  return  res;
 
 
}      /* sim_read () */
}      /* sim_read () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
Line 317... Line 364...
sim_write (SIM_DESC       sd  ATTRIBUTE_UNUSED,
sim_write (SIM_DESC       sd  ATTRIBUTE_UNUSED,
           SIM_ADDR       mem,
           SIM_ADDR       mem,
           unsigned char *buf,
           unsigned char *buf,
           int            len)
           int            len)
{
{
  return  or1ksim_write_mem (buf, (unsigned int) mem, len);
  /* printf ("Writing %d bytes to 0x%08p\n", len, mem); */
 
 
 
  return  or1ksim_write_mem ((unsigned int) mem, buf, len);
 
 
}       /* sim_write () */
}       /* sim_write () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/*!Fetch a register from the simulation
/*!Fetch a register from the simulation
 
 
 
   We get the register back as a 32-bit value. However we must convert it to a
 
   character array <em>in target endian order</em>.
 
 
 
   The exception is if the register is the NPC, which is only written just
 
   before resumption, to avoid pipeline confusion. It is fetched from the SD.
 
 
   @param[in]  sd     Simulation descriptor from sim_open (). We don't use
   @param[in]  sd     Simulation descriptor from sim_open (). We don't use
                      this.
                      this.
   @param[in]  regno  The register to fetch
   @param[in]  regno  The register to fetch
   @param[out] buf    Buffer of length bytes to store the result. Data is
   @param[out] buf    Buffer of length bytes to store the result. Data is
                      only transferred if length matches the register length
                      only transferred if length matches the register length
Line 338... Line 393...
 
 
   @return  The actual size of the register, or zero if regno is not
   @return  The actual size of the register, or zero if regno is not
            applicable. Legacy implementations return -1.
            applicable. Legacy implementations return -1.
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
int
int
sim_fetch_register (SIM_DESC       sd  ATTRIBUTE_UNUSED,
sim_fetch_register (SIM_DESC       sd,
                    int            regno,
                    int            regno,
                    unsigned char *buf,
                    unsigned char *buf,
                    int            len)
                    int            len)
{
{
  return  or1ksim_read_reg (buf, regno, len);
  unsigned int  regval;
 
  int           res;
 
 
 
  if (4 != len)
 
    {
 
      fprintf (stderr, "Invalid register length %d\n");
 
      return  0;
 
    }
 
 
 
  if (OR32_NPC_REGNUM == regno)
 
    {
 
      regval = sd->resume_npc;
 
      res    = 4;
 
    }
 
  else
 
    {
 
      int res = or1ksim_read_reg (regno, &regval) ? 4 : 0;
 
    }
 
 
 
  /* Convert to target (big) endian */
 
  if (res)
 
    {
 
      buf[0] = (regval >> 24) & 0xff;
 
      buf[1] = (regval >> 16) & 0xff;
 
      buf[2] = (regval >>  8) & 0xff;
 
      buf[3] =  regval        & 0xff;
 
    }
 
 
 
  /* printf ("Read register 0x%02x, value 0x%08x\n", regno, regval); */
 
  return  res;
 
 
}       /* sim_fetch_register () */
}       /* sim_fetch_register () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/*!Store a register to the simulation
/*!Store a register to the simulation
 
 
 
   We write the register back as a 32-bit value. However we must convert it from
 
   a character array <em>in target endian order</em>.
 
 
 
   The exception is if the register is the NPC, which is only written just
 
   before resumption, to avoid pipeline confusion. It is saved in the SD.
 
 
   @param[in] sd     Simulation descriptor from sim_open (). We don't use
   @param[in] sd     Simulation descriptor from sim_open (). We don't use
                     this.
                     this.
   @param[in] regno  The register to store
   @param[in] regno  The register to store
   @param[in] buf    Buffer of length bytes with the data to store. Data is
   @param[in] buf    Buffer of length bytes with the data to store. Data is
                     only transferred if length matches the register length
                     only transferred if length matches the register length
Line 364... Line 454...
 
 
   @return  The actual size of the register, or zero if regno is not
   @return  The actual size of the register, or zero if regno is not
            applicable. Legacy implementations return -1.
            applicable. Legacy implementations return -1.
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
int
int
sim_store_register (SIM_DESC       sd  ATTRIBUTE_UNUSED,
sim_store_register (SIM_DESC       sd,
                    int            regno,
                    int            regno,
                    unsigned char *buf,
                    unsigned char *buf,
                    int            len)
                    int            len)
{
{
  return  or1ksim_write_reg (buf, regno, len);
  unsigned int  regval;
 
 
 
  if (4 != len)
 
    {
 
      fprintf (stderr, "Invalid register length %d\n");
 
      return  0;
 
    }
 
 
 
  /* Convert from target (big) endian */
 
  regval = (((unsigned int) buf[0]) << 24) |
 
           (((unsigned int) buf[1]) << 16) |
 
           (((unsigned int) buf[2]) <<  8) |
 
           (((unsigned int) buf[3])      );
 
 
 
  /* printf ("Writing register 0x%02x, value 0x%08x\n", regno, regval); */
 
 
 
  if (OR32_NPC_REGNUM == regno)
 
    {
 
      sd->resume_npc = regval;
 
      return  4;                        /* Reg length in bytes */
 
    }
 
  else
 
    {
 
      return  or1ksim_write_reg (regno, regval) ? 4 : 0;
 
    }
}       /* sim_store_register () */
}       /* sim_store_register () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* Print whatever statistics the simulator has collected.
/* Print whatever statistics the simulator has collected.
Line 404... Line 517...
   Process simulator: If SIGRC is non-zero then the corresponding
   Process simulator: If SIGRC is non-zero then the corresponding
   signal is delivered to the simulated program and execution is then
   signal is delivered to the simulated program and execution is then
   continued.  A zero SIGRC value indicates that the program should
   continued.  A zero SIGRC value indicates that the program should
   continue as normal.
   continue as normal.
 
 
 
   We carry out the following
 
   - Clear the debug reason register
 
   - Clear watchpoing break generation in debug mode register 2
 
   - Set the debug unit to handle TRAP exceptions
 
   - If stepping, set the single step trigger in debug mode register 1
 
   - Write the resume_npc if it differs from the actual NPC.
 
   - Unstall the processor
 
   - Run the processor.
 
 
 
   On execution completion, we determine the reason for the halt. If it is a
 
   breakpoint, we mark the resumption NPC to be the PPC (so we redo the NPC
 
   location).
 
 
   @param[in] sd       Simulation descriptor from sim_open ().
   @param[in] sd       Simulation descriptor from sim_open ().
   @param[in] step     When non-zero indicates that only a single simulator
   @param[in] step     When non-zero indicates that only a single simulator
                       cycle should be emulated.
                       cycle should be emulated.
   @param[in] siggnal  If non-zero is a (HOST) SIGRC value indicating the type
   @param[in] siggnal  If non-zero is a (HOST) SIGRC value indicating the type
                       of event (hardware interrupt, signal) to be delivered
                       of event (hardware interrupt, signal) to be delivered
Line 416... Line 542...
void
void
sim_resume (SIM_DESC  sd,
sim_resume (SIM_DESC  sd,
            int       step,
            int       step,
            int       siggnal)
            int       siggnal)
{
{
  int  res;
  unsigned int  npc;                    /* Next Program Counter */
 
  unsigned int  drr;                    /* Debug Reason Register */
 
  unsigned int  dsr;                    /* Debug Stop Register */
 
  unsigned int  dmr1;                   /* Debug Mode Register 1*/
 
  unsigned int  dmr2;                   /* Debug Mode Register 2*/
 
 
 
  int                res;               /* Result of a run. */
 
 
 
  /* Clear Debug Reason Register and watchpoint break generation in Debug Mode
 
     Register 2 */
 
  (void) or1ksim_write_spr (OR32_SPR_DRR, 0);
 
 
 
  (void) or1ksim_read_spr (OR32_SPR_DMR2, &dmr2);
 
  dmr2 &= ~OR32_SPR_DMR2_WGB;
 
  (void) or1ksim_write_spr (OR32_SPR_DMR2, dmr2);
 
 
 
  /* Set debug unit to handle TRAP exceptions */
 
  (void) or1ksim_read_spr (OR32_SPR_DSR, &dsr);
 
  dsr |= OR32_SPR_DSR_TE;
 
  (void) or1ksim_write_spr (OR32_SPR_DSR, dsr);
 
 
 
  /* Set the single step trigger in Debug Mode Register 1 if we are stepping. */
 
  if (step)
 
    {
 
      (void) or1ksim_read_spr (OR32_SPR_DMR1, &dmr1);
 
      dmr1 |= OR32_SPR_DMR1_ST;
 
      (void) or1ksim_write_spr (OR32_SPR_DMR1, dmr1);
 
    }
 
 
  res = step ? or1ksim_step () : or1ksim_run (-1.0);
  /* Set the NPC if it has changed */
 
  (void) or1ksim_read_reg (OR32_NPC_REGNUM, &npc);
 
 
 
  if (npc != sd->resume_npc)
 
    {
 
      (void) or1ksim_write_reg (OR32_NPC_REGNUM, sd->resume_npc);
 
    }
 
 
 
  /* Unstall and run */
 
  or1ksim_set_stall_state (0);
 
  res = or1ksim_run (-1.0);
 
 
 
  /* Determine the reason for stopping. If we hit a breakpoint, then the
 
     resumption NPC must be set to the PPC to allow re-execution of the
 
     trapped instruction. */
  switch (res)
  switch (res)
    {
    {
    case OR1KSIM_RC_HALTED:
    case OR1KSIM_RC_HALTED:
      last_reason = sim_exited;
      sd->last_reason = sim_exited;
      (void) sim_fetch_register (sd, OR32_FIRST_ARG_REGNUM,
      (void) or1ksim_read_reg (OR32_FIRST_ARG_REGNUM, &(sd->last_rc));
                                 (unsigned char *) &last_rc, 4);
      sd->resume_npc  = OR32_RESET_EXCEPTION;
      break;
      break;
 
 
    case OR1KSIM_RC_BRKPT:
    case OR1KSIM_RC_BRKPT:
      last_reason = sim_stopped;
      sd->last_reason = sim_stopped;
      last_rc     = TARGET_SIGNAL_TRAP;
      sd->last_rc     = TARGET_SIGNAL_TRAP;
 
      (void) or1ksim_read_reg (OR32_PPC_REGNUM, &(sd->resume_npc));
 
      break;
 
 
 
    case OR1KSIM_RC_OK:
 
      /* Should not happen */
 
      fprintf (stderr, "Ooops. Didn't expect OK return from Or1ksim.\n");
 
 
 
      sd->last_reason = sim_running;            /* Should trigger an error! */
 
      sd->last_rc     = TARGET_SIGNAL_NONE;
 
      (void) or1ksim_read_reg (OR32_NPC_REGNUM, &(sd->resume_npc));
      break;
      break;
    }
    }
}       /* sim_resume () */
}       /* sim_resume () */
 
 
 
 
Line 481... Line 657...
 
 
   - sim_running:
   - sim_running:
   - sim_polling:   The return of one of these values indicates a problem
   - sim_polling:   The return of one of these values indicates a problem
                    internal to the simulator.
                    internal to the simulator.
 
 
   @param[in]  sd      Simulation descriptor from sim_open (). We don't use
   @param[in]  sd      Simulation descriptor from sim_open ().
                       this.
 
   @param[out] reason  The reason for stopping
   @param[out] reason  The reason for stopping
   @param[out] sigrc   Supplementary information for some values of reason.  */
   @param[out] sigrc   Supplementary information for some values of reason.  */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void
void
sim_stop_reason (SIM_DESC       sd      ATTRIBUTE_UNUSED,
sim_stop_reason (SIM_DESC       sd,
                 enum sim_stop *reason,
                 enum sim_stop *reason,
                 int           *sigrc)
                 int           *sigrc)
 {
 {
   *reason = last_reason;
   *reason = sd->last_reason;
   *sigrc  = last_rc;
   *sigrc  = sd->last_rc;
 
 
}       /* sim_stop_reason () */
}       /* sim_stop_reason () */
 
 
 
 
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */

powered by: WebSVN 2.1.0

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