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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [ppc/] [sim_calls.c] - Diff between revs 834 and 842

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

Rev 834 Rev 842
/*  This file is part of the program psim.
/*  This file is part of the program psim.
 
 
    Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
    Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
 
 
    This program is free software; you can redistribute it and/or modify
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    (at your option) any later version.
 
 
    This program is distributed in the hope that it will be useful,
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    GNU General Public License for more details.
 
 
    You should have received a copy of the GNU General Public License
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 
    */
    */
 
 
 
 
#include <signal.h> /* FIXME - should be machine dependant version */
#include <signal.h> /* FIXME - should be machine dependant version */
#include <stdarg.h>
#include <stdarg.h>
#include <ctype.h>
#include <ctype.h>
 
 
#include "psim.h"
#include "psim.h"
#include "options.h"
#include "options.h"
 
 
#undef printf_filtered /* blow away the mapping */
#undef printf_filtered /* blow away the mapping */
 
 
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.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
 
 
#include "libiberty.h"
#include "libiberty.h"
#include "bfd.h"
#include "bfd.h"
#include "gdb/callback.h"
#include "gdb/callback.h"
#include "gdb/remote-sim.h"
#include "gdb/remote-sim.h"
#include "gdb/signals.h"
#include "gdb/signals.h"
 
 
/* Define the rate at which the simulator should poll the host
/* Define the rate at which the simulator should poll the host
   for a quit. */
   for a quit. */
#ifndef POLL_QUIT_INTERVAL
#ifndef POLL_QUIT_INTERVAL
#define POLL_QUIT_INTERVAL 0x20
#define POLL_QUIT_INTERVAL 0x20
#endif
#endif
 
 
static int poll_quit_count = POLL_QUIT_INTERVAL;
static int poll_quit_count = POLL_QUIT_INTERVAL;
 
 
/* Structures used by the simulator, for gdb just have static structures */
/* Structures used by the simulator, for gdb just have static structures */
 
 
psim *simulator;
psim *simulator;
static device *root_device;
static device *root_device;
static host_callback *callbacks;
static host_callback *callbacks;
 
 
SIM_DESC
SIM_DESC
sim_open (SIM_OPEN_KIND kind,
sim_open (SIM_OPEN_KIND kind,
          host_callback *callback,
          host_callback *callback,
          struct bfd *abfd,
          struct bfd *abfd,
          char **argv)
          char **argv)
{
{
  callbacks = callback;
  callbacks = callback;
 
 
  /* Note: The simulation is not created by sim_open() because
  /* Note: The simulation is not created by sim_open() because
     complete information is not yet available */
     complete information is not yet available */
  /* trace the call */
  /* trace the call */
  TRACE(trace_gdb, ("sim_open called\n"));
  TRACE(trace_gdb, ("sim_open called\n"));
 
 
  if (root_device != NULL)
  if (root_device != NULL)
    sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
    sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
  root_device = psim_tree();
  root_device = psim_tree();
  simulator = NULL;
  simulator = NULL;
 
 
  psim_options(root_device, argv + 1);
  psim_options(root_device, argv + 1);
 
 
  if (ppc_trace[trace_opts])
  if (ppc_trace[trace_opts])
    print_options ();
    print_options ();
 
 
  /* fudge our descriptor for now */
  /* fudge our descriptor for now */
  return (SIM_DESC) 1;
  return (SIM_DESC) 1;
}
}
 
 
 
 
void
void
sim_close (SIM_DESC sd, int quitting)
sim_close (SIM_DESC sd, int quitting)
{
{
  TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
  TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
  if (ppc_trace[trace_print_info] && simulator != NULL)
  if (ppc_trace[trace_print_info] && simulator != NULL)
    psim_print_info (simulator, ppc_trace[trace_print_info]);
    psim_print_info (simulator, ppc_trace[trace_print_info]);
}
}
 
 
 
 
SIM_RC
SIM_RC
sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
{
{
  TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
  TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
                    prog, from_tty));
                    prog, from_tty));
  ASSERT(prog != NULL);
  ASSERT(prog != NULL);
 
 
  /* create the simulator */
  /* create the simulator */
  TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
  TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
  simulator = psim_create(prog, root_device);
  simulator = psim_create(prog, root_device);
 
 
  /* bring in all the data section */
  /* bring in all the data section */
  psim_init(simulator);
  psim_init(simulator);
 
 
  /* get the start address */
  /* get the start address */
  if (abfd == NULL)
  if (abfd == NULL)
    {
    {
      abfd = bfd_openr (prog, 0);
      abfd = bfd_openr (prog, 0);
      if (abfd == NULL)
      if (abfd == NULL)
        error ("psim: can't open \"%s\": %s\n",
        error ("psim: can't open \"%s\": %s\n",
               prog, bfd_errmsg (bfd_get_error ()));
               prog, bfd_errmsg (bfd_get_error ()));
      if (!bfd_check_format (abfd, bfd_object))
      if (!bfd_check_format (abfd, bfd_object))
        {
        {
          const char *errmsg = bfd_errmsg (bfd_get_error ());
          const char *errmsg = bfd_errmsg (bfd_get_error ());
          bfd_close (abfd);
          bfd_close (abfd);
          error ("psim: \"%s\" is not an object file: %s\n",
          error ("psim: \"%s\" is not an object file: %s\n",
                 prog, errmsg);
                 prog, errmsg);
        }
        }
      bfd_close (abfd);
      bfd_close (abfd);
    }
    }
 
 
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
 
 
int
int
sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
{
{
  int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
  int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
                                buf, mem, length);
                                buf, mem, length);
  TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
  TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
                    (long)mem, (long)buf, length, result));
                    (long)mem, (long)buf, length, result));
  return result;
  return result;
}
}
 
 
 
 
int
int
sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
{
{
  int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
  int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
                                 buf, mem, length,
                                 buf, mem, length,
                                 1/*violate_ro*/);
                                 1/*violate_ro*/);
  TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
  TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
                    (long)mem, (long)buf, length, result));
                    (long)mem, (long)buf, length, result));
  return result;
  return result;
}
}
 
 
void
void
sim_info (SIM_DESC sd, int verbose)
sim_info (SIM_DESC sd, int verbose)
{
{
  TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
  TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
  psim_print_info (simulator, verbose);
  psim_print_info (simulator, verbose);
}
}
 
 
 
 
SIM_RC
SIM_RC
sim_create_inferior (SIM_DESC sd,
sim_create_inferior (SIM_DESC sd,
                     struct bfd *abfd,
                     struct bfd *abfd,
                     char **argv,
                     char **argv,
                     char **envp)
                     char **envp)
{
{
  unsigned_word entry_point;
  unsigned_word entry_point;
  TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
  TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
                    entry_point));
                    entry_point));
 
 
  if (simulator == NULL)
  if (simulator == NULL)
    error ("No program loaded");
    error ("No program loaded");
 
 
  if (abfd != NULL)
  if (abfd != NULL)
    entry_point = bfd_get_start_address (abfd);
    entry_point = bfd_get_start_address (abfd);
  else
  else
    entry_point = 0xfff00000; /* ??? */
    entry_point = 0xfff00000; /* ??? */
 
 
  psim_init(simulator);
  psim_init(simulator);
  psim_stack(simulator, argv, envp);
  psim_stack(simulator, argv, envp);
 
 
  ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
  ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
                              &entry_point, "pc", cooked_transfer) > 0);
                              &entry_point, "pc", cooked_transfer) > 0);
  return SIM_RC_OK;
  return SIM_RC_OK;
}
}
 
 
 
 
void
void
sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
{
{
  psim_status status = psim_get_status(simulator);
  psim_status status = psim_get_status(simulator);
 
 
  switch (status.reason) {
  switch (status.reason) {
  case was_continuing:
  case was_continuing:
    *reason = sim_stopped;
    *reason = sim_stopped;
    if (status.signal == 0)
    if (status.signal == 0)
      *sigrc = TARGET_SIGNAL_TRAP;
      *sigrc = TARGET_SIGNAL_TRAP;
    else
    else
      *sigrc = status.signal;
      *sigrc = status.signal;
    break;
    break;
  case was_trap:
  case was_trap:
    *reason = sim_stopped;
    *reason = sim_stopped;
    *sigrc = TARGET_SIGNAL_TRAP;
    *sigrc = TARGET_SIGNAL_TRAP;
    break;
    break;
  case was_exited:
  case was_exited:
    *reason = sim_exited;
    *reason = sim_exited;
    *sigrc = status.signal;
    *sigrc = status.signal;
    break;
    break;
  case was_signalled:
  case was_signalled:
    *reason = sim_signalled;
    *reason = sim_signalled;
    *sigrc = status.signal;
    *sigrc = status.signal;
    break;
    break;
  }
  }
 
 
  TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
  TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
                    (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
                    (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
}
}
 
 
 
 
 
 
/* Run (or resume) the program.  */
/* Run (or resume) the program.  */
 
 
int
int
sim_stop (SIM_DESC sd)
sim_stop (SIM_DESC sd)
{
{
  psim_stop (simulator);
  psim_stop (simulator);
  return 1;
  return 1;
}
}
 
 
void
void
sim_resume (SIM_DESC sd, int step, int siggnal)
sim_resume (SIM_DESC sd, int step, int siggnal)
{
{
  TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
  TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
                    step, siggnal));
                    step, siggnal));
 
 
  if (step)
  if (step)
    {
    {
      psim_step (simulator);
      psim_step (simulator);
    }
    }
  else
  else
    {
    {
      psim_run (simulator);
      psim_run (simulator);
    }
    }
}
}
 
 
void
void
sim_do_command (SIM_DESC sd, char *cmd)
sim_do_command (SIM_DESC sd, char *cmd)
{
{
  TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
  TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
                    cmd ? cmd : "(null)"));
                    cmd ? cmd : "(null)"));
  if (cmd != NULL) {
  if (cmd != NULL) {
    char **argv = buildargv(cmd);
    char **argv = buildargv(cmd);
    psim_command(root_device, argv);
    psim_command(root_device, argv);
    freeargv(argv);
    freeargv(argv);
  }
  }
}
}
 
 
 
 
/* Polling, if required */
/* Polling, if required */
 
 
void
void
sim_io_poll_quit (void)
sim_io_poll_quit (void)
{
{
  if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
  if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
    {
    {
      poll_quit_count = POLL_QUIT_INTERVAL;
      poll_quit_count = POLL_QUIT_INTERVAL;
      if (callbacks->poll_quit (callbacks))
      if (callbacks->poll_quit (callbacks))
        psim_stop (simulator);
        psim_stop (simulator);
    }
    }
}
}
 
 
 
 
 
 
/* Map simulator IO operations onto the corresponding GDB I/O
/* Map simulator IO operations onto the corresponding GDB I/O
   functions.
   functions.
 
 
   NB: Only a limited subset of operations are mapped across.  More
   NB: Only a limited subset of operations are mapped across.  More
   advanced operations (such as dup or write) must either be mapped to
   advanced operations (such as dup or write) must either be mapped to
   one of the below calls or handled internally */
   one of the below calls or handled internally */
 
 
int
int
sim_io_read_stdin(char *buf,
sim_io_read_stdin(char *buf,
                  int sizeof_buf)
                  int sizeof_buf)
{
{
  switch (CURRENT_STDIO) {
  switch (CURRENT_STDIO) {
  case DO_USE_STDIO:
  case DO_USE_STDIO:
    return callbacks->read_stdin(callbacks, buf, sizeof_buf);
    return callbacks->read_stdin(callbacks, buf, sizeof_buf);
    break;
    break;
  case DONT_USE_STDIO:
  case DONT_USE_STDIO:
    return callbacks->read(callbacks, 0, buf, sizeof_buf);
    return callbacks->read(callbacks, 0, buf, sizeof_buf);
    break;
    break;
  default:
  default:
    error("sim_io_read_stdin: unaccounted switch\n");
    error("sim_io_read_stdin: unaccounted switch\n");
    break;
    break;
  }
  }
  return 0;
  return 0;
}
}
 
 
int
int
sim_io_write_stdout(const char *buf,
sim_io_write_stdout(const char *buf,
                    int sizeof_buf)
                    int sizeof_buf)
{
{
  switch (CURRENT_STDIO) {
  switch (CURRENT_STDIO) {
  case DO_USE_STDIO:
  case DO_USE_STDIO:
    return callbacks->write_stdout(callbacks, buf, sizeof_buf);
    return callbacks->write_stdout(callbacks, buf, sizeof_buf);
    break;
    break;
  case DONT_USE_STDIO:
  case DONT_USE_STDIO:
    return callbacks->write(callbacks, 1, buf, sizeof_buf);
    return callbacks->write(callbacks, 1, buf, sizeof_buf);
    break;
    break;
  default:
  default:
    error("sim_io_write_stdout: unaccounted switch\n");
    error("sim_io_write_stdout: unaccounted switch\n");
    break;
    break;
  }
  }
  return 0;
  return 0;
}
}
 
 
int
int
sim_io_write_stderr(const char *buf,
sim_io_write_stderr(const char *buf,
                    int sizeof_buf)
                    int sizeof_buf)
{
{
  switch (CURRENT_STDIO) {
  switch (CURRENT_STDIO) {
  case DO_USE_STDIO:
  case DO_USE_STDIO:
    /* NB: I think there should be an explicit write_stderr callback */
    /* NB: I think there should be an explicit write_stderr callback */
    return callbacks->write(callbacks, 3, buf, sizeof_buf);
    return callbacks->write(callbacks, 3, buf, sizeof_buf);
    break;
    break;
  case DONT_USE_STDIO:
  case DONT_USE_STDIO:
    return callbacks->write(callbacks, 3, buf, sizeof_buf);
    return callbacks->write(callbacks, 3, buf, sizeof_buf);
    break;
    break;
  default:
  default:
    error("sim_io_write_stderr: unaccounted switch\n");
    error("sim_io_write_stderr: unaccounted switch\n");
    break;
    break;
  }
  }
  return 0;
  return 0;
}
}
 
 
 
 
void
void
sim_io_printf_filtered(const char *fmt,
sim_io_printf_filtered(const char *fmt,
                       ...)
                       ...)
{
{
  char message[1024];
  char message[1024];
  va_list ap;
  va_list ap;
  /* format the message */
  /* format the message */
  va_start(ap, fmt);
  va_start(ap, fmt);
  vsprintf(message, fmt, ap);
  vsprintf(message, fmt, ap);
  va_end(ap);
  va_end(ap);
  /* sanity check */
  /* sanity check */
  if (strlen(message) >= sizeof(message))
  if (strlen(message) >= sizeof(message))
    error("sim_io_printf_filtered: buffer overflow\n");
    error("sim_io_printf_filtered: buffer overflow\n");
  callbacks->printf_filtered(callbacks, "%s", message);
  callbacks->printf_filtered(callbacks, "%s", message);
}
}
 
 
void
void
sim_io_flush_stdoutput(void)
sim_io_flush_stdoutput(void)
{
{
  switch (CURRENT_STDIO) {
  switch (CURRENT_STDIO) {
  case DO_USE_STDIO:
  case DO_USE_STDIO:
    callbacks->flush_stdout (callbacks);
    callbacks->flush_stdout (callbacks);
    break;
    break;
  case DONT_USE_STDIO:
  case DONT_USE_STDIO:
    break;
    break;
  default:
  default:
    error("sim_io_read_stdin: unaccounted switch\n");
    error("sim_io_read_stdin: unaccounted switch\n");
    break;
    break;
  }
  }
}
}
 
 
void
void
sim_io_error (SIM_DESC sd, const char *fmt, ...)
sim_io_error (SIM_DESC sd, const char *fmt, ...)
{
{
  va_list ap;
  va_list ap;
  va_start(ap, fmt);
  va_start(ap, fmt);
  callbacks->evprintf_filtered (callbacks, fmt, ap);
  callbacks->evprintf_filtered (callbacks, fmt, ap);
  va_end(ap);
  va_end(ap);
  callbacks->error (callbacks, "");
  callbacks->error (callbacks, "");
}
}
 
 
/****/
/****/
 
 
void *
void *
zalloc(long size)
zalloc(long size)
{
{
  void *memory = (void*)xmalloc(size);
  void *memory = (void*)xmalloc(size);
  if (memory == NULL)
  if (memory == NULL)
    error("xmalloc failed\n");
    error("xmalloc failed\n");
  memset(memory, 0, size);
  memset(memory, 0, size);
  return memory;
  return memory;
}
}
 
 
void zfree(void *data)
void zfree(void *data)
{
{
  free(data);
  free(data);
}
}
 
 

powered by: WebSVN 2.1.0

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