New revision from Andrew
[deliverable/binutils-gdb.git] / sim / ppc / main.c
CommitLineData
4f35cbff
MM
1/* This file is part of the program psim.
2
262faa54 3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4f35cbff
MM
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>
4f35cbff 23#include <stdio.h>
2e913166 24#include <fcntl.h>
4f35cbff
MM
25
26#include "psim.h"
28816f45 27#include "options.h"
262faa54 28#include "device.h" /* FIXME: psim should provide the interface */
4f35cbff 29
c494cadd
MM
30#ifdef HAVE_STDLIB_H
31#include <stdlib.h>
32#endif
33
73c4941b
MM
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
37
c494cadd
MM
38#ifdef HAVE_STRING_H
39#include <string.h>
40#else
41#ifdef HAVE_STRINGS_H
42#include <strings.h>
43#endif
44#endif
45
2e913166
MM
46#if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
47#undef WITH_STDIO
48#define WITH_STDIO DO_USE_STDIO
49#endif
50
51
4f35cbff 52extern char **environ;
4f35cbff 53
2e913166
MM
54static psim *simulation = NULL;
55
56
4f35cbff 57void
2e913166 58sim_io_printf_filtered(const char *msg, ...)
4f35cbff
MM
59{
60 va_list ap;
61 va_start(ap, msg);
62 vprintf(msg, ap);
22ddef46 63 va_end(ap);
4f35cbff
MM
64}
65
66void
67error (char *msg, ...)
68{
69 va_list ap;
70 va_start(ap, msg);
71 vprintf(msg, ap);
22ddef46 72 va_end(ap);
2e913166
MM
73
74 /* any final clean up */
75 if (ppc_trace[trace_print_info] && simulation != NULL)
76 psim_print_info (simulation, ppc_trace[trace_print_info]);
77
4f35cbff
MM
78 exit (1);
79}
80
2e913166
MM
81int
82sim_io_write_stdout(const char *buf,
83 int sizeof_buf)
84{
85 switch (CURRENT_STDIO) {
86 case DO_USE_STDIO:
87 {
88 int i;
89 for (i = 0; i < sizeof_buf; i++) {
90 putchar(buf[i]);
91 }
92 return i;
93 }
94 break;
95 case DONT_USE_STDIO:
96 return write(1, buf, sizeof_buf);
97 break;
98 default:
99 error("sim_io_write_stdout: invalid switch\n");
100 }
101}
102
103int
104sim_io_write_stderr(const char *buf,
105 int sizeof_buf)
106{
107 switch (CURRENT_STDIO) {
108 case DO_USE_STDIO:
109 {
110 int i;
111 for (i = 0; i < sizeof_buf; i++) {
112 fputc(buf[i], stderr);
113 }
114 return i;
115 }
116 break;
117 case DONT_USE_STDIO:
118 return write(2, buf, sizeof_buf);
119 break;
120 default:
121 error("sim_io_write_stdout: invalid switch\n");
122 }
123}
124
125int
126sim_io_read_stdin(char *buf,
127 int sizeof_buf)
128{
129 switch (CURRENT_STDIO) {
130 case DO_USE_STDIO:
131 if (fgets(buf, sizeof_buf, stdin) == NULL)
132 return sim_io_eof;
133 else
134 return strlen(buf);
135 break;
136 case DONT_USE_STDIO:
137 {
138 /* check for input */
139 int flags;
140 int status;
141 int nr_read;
142 /* get the old status */
143 flags = fcntl(0, F_GETFL, 0);
144 if (flags == -1) {
145 perror("sim_io_read_stdin");
146 return sim_io_eof;
147 }
148 /* temp, disable blocking IO */
149 status = fcntl(0, F_SETFL, flags | O_NDELAY);
150 if (status == -1) {
151 perror("sim_io_read_stdin");
152 return sim_io_eof;
153 }
154 /* try for input */
155 nr_read = read(0, &buf, sizeof_buf);
156 /* return to regular vewing */
157 status = fcntl(0, F_SETFL, flags);
158 if (status == -1) {
159 perror("sim_io_read_stdin");
160 return sim_io_eof;
161 }
162 if (status > 0)
163 return 1;
164 else if (status < 0)
165 return sim_io_eof;
166 else
167 return sim_io_not_ready;
168 }
169 break;
170 default:
171 error("sim_io_read_stdin: invalid switch\n");
172 break;
173 }
174}
175
176void
177sim_io_flush_stdoutput(void)
178{
179 switch (CURRENT_STDIO) {
180 case DO_USE_STDIO:
181 fflush (stdout);
182 break;
183 case DONT_USE_STDIO:
184 break;
185 default:
186 error("sim_io_flush_stdoutput: invalid switch\n");
187 break;
188 }
189}
190
191
4f35cbff
MM
192void *
193zalloc(long size)
194{
195 void *memory = malloc(size);
196 if (memory == NULL)
197 error("zmalloc failed\n");
1dc7c0ed 198 memset(memory, 0, size);
4f35cbff
MM
199 return memory;
200}
201
202void
203zfree(void *chunk)
204{
205 free(chunk);
206}
207
4f35cbff
MM
208int
209main(int argc, char **argv)
210{
4f35cbff
MM
211 const char *name_of_file;
212 char *arg_;
4f35cbff 213 psim_status status;
262faa54
MM
214 device *root = psim_tree();
215
216 /* parse the arguments */
217 argv = psim_options(root, argv + 1);
218 if (argv[0] == NULL)
219 psim_usage(0);
220 name_of_file = argv[0];
4f35cbff 221
28816f45
MM
222 if (ppc_trace[trace_opts])
223 print_options ();
224
4f35cbff 225 /* create the simulator */
2e913166 226 simulation = psim_create(name_of_file, root);
4f35cbff
MM
227
228 /* fudge the environment so that _=prog-name */
262faa54 229 arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
4f35cbff 230 strcpy(arg_, "_=");
262faa54 231 strcat(arg_, argv[0]);
4f35cbff
MM
232 putenv(arg_);
233
234 /* initialize it */
2e913166
MM
235 psim_init(simulation);
236 psim_stack(simulation, argv, environ);
4f35cbff 237
2e913166 238 psim_run(simulation);
4f35cbff 239
22ddef46 240 /* any final clean up */
262faa54 241 if (ppc_trace[trace_print_info])
2e913166 242 psim_print_info (simulation, ppc_trace[trace_print_info]);
83d96c6e 243
4f35cbff 244 /* why did we stop */
2e913166 245 status = psim_get_status(simulation);
4f35cbff
MM
246 switch (status.reason) {
247 case was_continuing:
248 error("psim: continuing while stoped!\n");
249 return 0;
250 case was_trap:
251 error("psim: no trap insn\n");
252 return 0;
253 case was_exited:
254 return status.signal;
255 case was_signalled:
22ddef46 256 printf ("%s: Caught signal %d at address 0x%lx\n",
a983c8f0
MM
257 name_of_file, (int)status.signal,
258 (long)status.program_counter);
4f35cbff
MM
259 return status.signal;
260 default:
261 error("unknown halt condition\n");
262 return 0;
263 }
264}
This page took 0.06869 seconds and 4 git commands to generate.