home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / sim / ppc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  3.1 KB  |  153 lines

  1. /*  This file is part of the program psim.
  2.  
  3.     Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19.     */
  20.  
  21.  
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24.  
  25. #include "psim.h"
  26. #include "options.h"
  27. #include "device.h" /* FIXME: psim should provide the interface */
  28.  
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #endif
  32.  
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36.  
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #else
  40. #ifdef HAVE_STRINGS_H
  41. #include <strings.h>
  42. #endif
  43. #endif
  44.  
  45. extern char **environ;
  46.  
  47. static psim *simulation;
  48.  
  49.  
  50. void
  51. printf_filtered(const char *msg, ...)
  52. {
  53.   va_list ap;
  54.   va_start(ap, msg);
  55.   vprintf(msg, ap);
  56.   va_end(ap);
  57. }
  58.  
  59. void
  60. flush_stdoutput(void)
  61. {
  62.   fflush (stdout);
  63. }
  64.  
  65. void
  66. error (char *msg, ...)
  67. {
  68.   va_list ap;
  69.   va_start(ap, msg);
  70.   vprintf(msg, ap);
  71.   va_end(ap);
  72.  
  73.   /* any final clean up */
  74.   if (ppc_trace[trace_print_info])
  75.     psim_print_info (simulation, ppc_trace[trace_print_info]);
  76.  
  77.   exit (1);
  78. }
  79.  
  80. void *
  81. zalloc(long size)
  82. {
  83.   void *memory = malloc(size);
  84.   if (memory == NULL)
  85.     error("zmalloc failed\n");
  86.   memset(memory, 0, size);
  87.   return memory;
  88. }
  89.  
  90. void
  91. zfree(void *chunk)
  92. {
  93.   free(chunk);
  94. }
  95.  
  96. int
  97. main(int argc, char **argv)
  98. {
  99.   const char *name_of_file;
  100.   char *arg_;
  101.   psim_status status;
  102.   device *root = psim_tree();
  103.  
  104.   /* parse the arguments */
  105.   argv = psim_options(root, argv + 1);
  106.   if (argv[0] == NULL)
  107.     psim_usage(0);
  108.   name_of_file = argv[0];
  109.  
  110.   if (ppc_trace[trace_opts])
  111.     print_options ();
  112.  
  113.   /* create the simulator */
  114.   simulation = psim_create(name_of_file, root);
  115.  
  116.   /* fudge the environment so that _=prog-name */
  117.   arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
  118.   strcpy(arg_, "_=");
  119.   strcat(arg_, argv[0]);
  120.   putenv(arg_);
  121.  
  122.   /* initialize it */
  123.   psim_init(simulation);
  124.   psim_stack(simulation, argv, environ);
  125.  
  126.   psim_run(simulation);
  127.  
  128.   /* any final clean up */
  129.   if (ppc_trace[trace_print_info])
  130.     psim_print_info (simulation, ppc_trace[trace_print_info]);
  131.  
  132.   /* why did we stop */
  133.   status = psim_get_status(simulation);
  134.   switch (status.reason) {
  135.   case was_continuing:
  136.     error("psim: continuing while stoped!\n");
  137.     return 0;
  138.   case was_trap:
  139.     error("psim: no trap insn\n");
  140.     return 0;
  141.   case was_exited:
  142.     return status.signal;
  143.   case was_signalled:
  144.     printf ("%s: Caught signal %d at address 0x%lx\n",
  145.          name_of_file, (int)status.signal,
  146.          (long)status.program_counter);
  147.     return status.signal;
  148.   default:
  149.     error("unknown halt condition\n");
  150.     return 0;
  151.   }
  152. }
  153.