Fix typo in last change.
[deliverable/binutils-gdb.git] / sim / ppc / sim_calls.c
index 6fdca6a3aa6887afb7d99a4a56964c40a8c6c39e..15df3c50d01e495a1e280d29a9c17e02cf0434d7 100644 (file)
@@ -1,6 +1,6 @@
 /*  This file is part of the program psim.
 
-    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
+    Copyright (C) 1994-1996,1998, Andrew Cagney <cagney@highland.com.au>
 
     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
@@ -26,6 +26,8 @@
 #include "psim.h"
 #include "options.h"
 
+#undef printf_filtered /* blow away the mapping */
+
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
 #endif
 #endif
 
-#include "../../gdb/defs.h"
+#include "defs.h"
+#include "bfd.h"
+#include "callback.h"
+#include "remote-sim.h"
 
-#include "../../gdb/remote-sim.h"
-#include "../../gdb/callback.h"
+/* Define the rate at which the simulator should poll the host
+   for a quit. */
+#ifndef POLL_QUIT_INTERVAL
+#define POLL_QUIT_INTERVAL 0x20
+#endif
 
+static int poll_quit_count = POLL_QUIT_INTERVAL;
 
 /* Structures used by the simulator, for gdb just have static structures */
 
 static psim *simulator;
 static device *root_device;
-static const char *register_names[] = REGISTER_NAMES;
-
-void
-sim_open (char *args)
+static host_callback *callbacks;
+
+/* We use GDB's reg_names array to map GDB register numbers onto
+   names, which we can then look up in the register table.
+
+   We used to just use the REGISTER_NAMES macro, from GDB's
+   target-dependent header files.  That was kind of nice, because it
+   meant that libsim.a had only a compile-time dependency on GDB;
+   using reg_names directly means that there are now link-time and
+   run-time dependencies too.
+
+   However, the GDB PPC back-end now modifies the reg_names array when
+   the user runs the `set processor' command, which affects the
+   meanings of the register numbers.  So the sim needs to see the
+   register names GDB is actually using.
+
+   Perhaps the host_callback structure could contain a pointer to the
+   register name table; that would be cleaner.  */
+
+SIM_DESC
+sim_open (SIM_OPEN_KIND kind,
+         host_callback *callback,
+         struct _bfd *abfd,
+         char **argv)
 {
+  callbacks = callback;
+
   /* Note: The simulation is not created by sim_open() because
      complete information is not yet available */
   /* trace the call */
-  TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
+  TRACE(trace_gdb, ("sim_open called\n"));
 
   if (root_device != NULL)
-    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();
   simulator = NULL;
 
-  if (args) {
-    char **argv = buildargv(args);
-    psim_options(root_device, argv);
-    freeargv(argv);
-  }
+  psim_options(root_device, argv + 1);
 
   if (ppc_trace[trace_opts])
     print_options ();
+
+  /* fudge our descriptor for now */
+  return (SIM_DESC) 1;
 }
 
 
 void
-sim_close (int quitting)
+sim_close (SIM_DESC sd, int quitting)
 {
   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
   if (ppc_trace[trace_print_info] && simulator != NULL)
@@ -83,43 +113,43 @@ sim_close (int quitting)
 }
 
 
-int
-sim_load (char *prog, int from_tty)
+SIM_RC
+sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
 {
-  char **argv;
   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
                    prog, from_tty));
   ASSERT(prog != NULL);
 
-  /* parse the arguments, assume that the file is argument 0 */
-  argv = buildargv(prog);
-  ASSERT(argv != NULL && argv[0] != NULL);
-
   /* create the simulator */
   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
-  simulator = psim_create(argv[0], root_device);
+  simulator = psim_create(prog, root_device);
 
   /* bring in all the data section */
   psim_init(simulator);
 
-  /* release the arguments */
-  freeargv(argv);
-
-  /* `I did it my way' */
-  return 0;
-}
-
+  /* get the start address */
+  if (abfd == NULL)
+    {
+      abfd = bfd_openr (prog, 0);
+      if (abfd == NULL)
+       error ("psim: can't open \"%s\": %s\n", 
+              prog, bfd_errmsg (bfd_get_error ()));
+      if (!bfd_check_format (abfd, bfd_object)) 
+       {
+         const char *errmsg = bfd_errmsg (bfd_get_error ());
+         bfd_close (abfd);
+         error ("psim: \"%s\" is not an object file: %s\n",
+                prog, errmsg);
+       }
+      bfd_close (abfd);
+    }
 
-void
-sim_kill (void)
-{
-  TRACE(trace_gdb, ("sim_kill(void) called\n"));
-  /* do nothing, nothing to do */
+  return SIM_RC_OK;
 }
 
 
 int
-sim_read (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,
                                buf, mem, length);
@@ -130,7 +160,7 @@ sim_read (SIM_ADDR mem, unsigned char *buf, int length)
 
 
 int
-sim_write (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,
                                 buf, mem, length,
@@ -141,99 +171,115 @@ sim_write (SIM_ADDR mem, unsigned char *buf, int length)
 }
 
 
-void
-sim_fetch_register (int regno, unsigned char *buf)
+int
+sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
 {
+  char *regname;
+
   if (simulator == NULL) {
-    return;
+    return 0;
   }
+
+  /* GDB will sometimes ask for the contents of a register named "";
+     we ignore such requests, and leave garbage in *BUF.  In
+     REG_NAMES, the empty string means "the register with this
+     number is not present in the currently selected architecture
+     variant."  That's following the kludge we're using for the MIPS
+     processors.  But there are loops that just walk through the
+     entire list of names and try to get everything.  */
+  regname = REGISTER_NAME (regno);
+  if (! regname || regname[0] == '\0')
+    return -1;
+
   TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
-                   regno, register_names[regno], (long)buf));
+                   regno, regname, (long)buf));
   psim_read_register(simulator, MAX_NR_PROCESSORS,
-                    buf, register_names[regno],
-                    raw_transfer);
+                    buf, regname, raw_transfer);
+  return -1;
 }
 
 
-void
-sim_store_register (int regno, unsigned char *buf)
+int
+sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
 {
+  char *regname;
+
   if (simulator == NULL)
-    return;
+    return 0;
+
+  /* See comments in sim_fetch_register, above.  */
+  regname = REGISTER_NAME (regno);
+  if (! regname || regname[0] == '\0')
+    return -1;
+
   TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
-                   regno, register_names[regno], (long)buf));
+                   regno, regname, (long)buf));
   psim_write_register(simulator, MAX_NR_PROCESSORS,
-                     buf, register_names[regno],
-                     raw_transfer);
+                     buf, regname, raw_transfer);
+  return -1;
 }
 
 
 void
-sim_info (int verbose)
+sim_info (SIM_DESC sd, int verbose)
 {
   TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
   psim_print_info (simulator, verbose);
 }
 
 
-void
-sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
+SIM_RC
+sim_create_inferior (SIM_DESC sd,
+                    struct _bfd *abfd,
+                    char **argv,
+                    char **envp)
 {
-  unsigned_word entry_point = start_address;
-
+  unsigned_word entry_point;
   TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
-                   start_address));
+                   entry_point));
+
+  if (simulator == NULL)
+    error ("No program loaded");
+
+  if (abfd != NULL)
+    entry_point = bfd_get_start_address (abfd);
+  else
+    entry_point = 0xfff00000; /* ??? */
 
   psim_init(simulator);
   psim_stack(simulator, argv, envp);
 
   psim_write_register(simulator, -1 /* all start at same PC */,
                      &entry_point, "pc", cooked_transfer);
+  return SIM_RC_OK;
 }
 
 
-static volatile int sim_should_run;
-
 void
-sim_stop_reason (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);
 
-  switch (CURRENT_ENVIRONMENT) {
-
-  case USER_ENVIRONMENT:
-  case VIRTUAL_ENVIRONMENT:
-    switch (status.reason) {
-    case was_continuing:
-      *reason = sim_stopped;
-      *sigrc = SIGTRAP;
-      if (sim_should_run) {
-       error("sim_stop_reason() unknown reason for halt\n");
-      }
-      break;
-    case was_trap:
-      *reason = sim_stopped;
+  switch (status.reason) {
+  case was_continuing:
+    *reason = sim_stopped;
+    if (status.signal == 0)
       *sigrc = SIGTRAP;
-      break;
-    case was_exited:
-      *reason = sim_exited;
-      *sigrc = 0;
-      break;
-    case was_signalled:
-      *reason = sim_signalled;
+    else
       *sigrc = status.signal;
-      break;
-    }
     break;
-
-  case OPERATING_ENVIRONMENT:
+  case was_trap:
     *reason = sim_stopped;
     *sigrc = SIGTRAP;
     break;
-
-  default:
-    error("sim_stop_reason() - unknown environment\n");
-  
+  case was_exited:
+    *reason = sim_exited;
+    *sigrc = status.signal;
+    break;
+  case was_signalled:
+    *reason = sim_signalled;
+    *sigrc = status.signal;
+    break;
   }
 
   TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
@@ -243,47 +289,150 @@ sim_stop_reason (enum sim_stop *reason, int *sigrc)
 
 
 /* Run (or resume) the program.  */
-static void
-sim_ctrl_c()
+
+int
+sim_stop (SIM_DESC sd)
 {
-  sim_should_run = 0;
+  psim_stop (simulator);
+  return 1;
 }
 
 void
-sim_resume (int step, int siggnal)
+sim_resume (SIM_DESC sd, int step, int siggnal)
 {
-  void (*prev) ();
-
   TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
                    step, siggnal));
 
-  prev = signal(SIGINT, sim_ctrl_c);
-  sim_should_run = 1;
-
   if (step)
-    psim_step(simulator);
+    {
+      psim_step (simulator);
+    }
   else
-    psim_run_until_stop(simulator, &sim_should_run);
-
-  signal(SIGINT, prev);
+    {
+      psim_run (simulator);
+    }
 }
 
 void
-sim_do_command (char *cmd)
+sim_do_command (SIM_DESC sd, char *cmd)
 {
   TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
                    cmd ? cmd : "(null)"));
-  if (cmd) {
+  if (cmd != NULL) {
     char **argv = buildargv(cmd);
-    psim_options(root_device, argv);
+    psim_command(root_device, argv);
     freeargv(argv);
   }
 }
 
+
+/* Polling, if required */
+
 void
-sim_set_callbacks (host_callback *callback)
+sim_io_poll_quit (void)
 {
-  TRACE(trace_gdb, ("sim_set_callbacks called\n"));
+  if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
+    {
+      poll_quit_count = POLL_QUIT_INTERVAL;
+      if (callbacks->poll_quit (callbacks))
+       psim_stop (simulator);
+    }
+}
+
+
+
+/* Map simulator IO operations onto the corresponding GDB I/O
+   functions.
+   
+   NB: Only a limited subset of operations are mapped across.  More
+   advanced operations (such as dup or write) must either be mapped to
+   one of the below calls or handled internally */
+
+int
+sim_io_read_stdin(char *buf,
+                 int sizeof_buf)
+{
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    return callbacks->read_stdin(callbacks, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->read(callbacks, 0, buf, sizeof_buf);
+    break;
+  default:
+    error("sim_io_read_stdin: unaccounted switch\n");
+    break;
+  }
+  return 0;
+}
+
+int
+sim_io_write_stdout(const char *buf,
+                   int sizeof_buf)
+{
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    return callbacks->write_stdout(callbacks, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->write(callbacks, 1, buf, sizeof_buf);
+    break;
+  default:
+    error("sim_io_write_stdout: unaccounted switch\n");
+    break;
+  }
+  return 0;
+}
+
+int
+sim_io_write_stderr(const char *buf,
+                   int sizeof_buf)
+{
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    /* NB: I think there should be an explicit write_stderr callback */
+    return callbacks->write(callbacks, 3, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->write(callbacks, 3, buf, sizeof_buf);
+    break;
+  default:
+    error("sim_io_write_stderr: unaccounted switch\n");
+    break;
+  }
+  return 0;
+}
+
+
+void
+sim_io_printf_filtered(const char *fmt,
+                      ...)
+{
+  char message[1024];
+  va_list ap;
+  /* format the message */
+  va_start(ap, fmt);
+  vsprintf(message, fmt, ap);
+  va_end(ap);
+  /* sanity check */
+  if (strlen(message) >= sizeof(message))
+    error("sim_io_printf_filtered: buffer overflow\n");
+  callbacks->printf_filtered(callbacks, "%s", message);
+}
+
+void
+sim_io_flush_stdoutput(void)
+{
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    callbacks->flush_stdout (callbacks);
+    break;
+  case DONT_USE_STDIO:
+    break;
+  default:
+    error("sim_io_read_stdin: unaccounted switch\n");
+    break;
+  }
 }
 
 /****/
@@ -300,10 +449,5 @@ zalloc(long size)
 
 void zfree(void *data)
 {
-  mfree(NULL, data);
-}
-
-void flush_stdoutput(void)
-{
-  gdb_flush (gdb_stdout);
+  free(data);
 }
This page took 0.028352 seconds and 4 git commands to generate.