Add input support; at end of user writes, call fflush
[external/binutils.git] / sim / ppc / main.c
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 void
48 printf_filtered(const char *msg, ...)
49 {
50   va_list ap;
51   va_start(ap, msg);
52   vprintf(msg, ap);
53   va_end(ap);
54 }
55
56 void
57 flush_stdoutput(void)
58 {
59   fflush (stdout);
60 }
61
62 void
63 error (char *msg, ...)
64 {
65   va_list ap;
66   va_start(ap, msg);
67   vprintf(msg, ap);
68   va_end(ap);
69   exit (1);
70 }
71
72 void *
73 zalloc(long size)
74 {
75   void *memory = malloc(size);
76   if (memory == NULL)
77     error("zmalloc failed\n");
78   memset(memory, 0, size);
79   return memory;
80 }
81
82 void
83 zfree(void *chunk)
84 {
85   free(chunk);
86 }
87
88 int
89 main(int argc, char **argv)
90 {
91   psim *system;
92   const char *name_of_file;
93   char *arg_;
94   psim_status status;
95   device *root = psim_tree();
96
97   /* parse the arguments */
98   argv = psim_options(root, argv + 1);
99   if (argv[0] == NULL)
100     psim_usage(0);
101   name_of_file = argv[0];
102
103   if (ppc_trace[trace_opts])
104     print_options ();
105
106   /* create the simulator */
107   system = psim_create(name_of_file, root);
108
109   /* fudge the environment so that _=prog-name */
110   arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
111   strcpy(arg_, "_=");
112   strcat(arg_, argv[0]);
113   putenv(arg_);
114
115   /* initialize it */
116   psim_init(system);
117   psim_stack(system, argv, environ);
118
119   psim_run(system);
120
121   /* any final clean up */
122   if (ppc_trace[trace_print_info])
123     psim_print_info (system, ppc_trace[trace_print_info]);
124
125   /* why did we stop */
126   status = psim_get_status(system);
127   switch (status.reason) {
128   case was_continuing:
129     error("psim: continuing while stoped!\n");
130     return 0;
131   case was_trap:
132     error("psim: no trap insn\n");
133     return 0;
134   case was_exited:
135     return status.signal;
136   case was_signalled:
137     printf ("%s: Caught signal %d at address 0x%lx\n",
138             name_of_file, (int)status.signal,
139             (long)status.program_counter);
140     return status.signal;
141   default:
142     error("unknown halt condition\n");
143     return 0;
144   }
145 }