Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* This file is part of the program psim. |
2 | ||
3 | Copyright (C) 1994-1997, 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 | #include <fcntl.h> | |
25 | ||
26 | #include <signal.h> | |
27 | ||
28 | #include "psim.h" | |
29 | #include "options.h" | |
30 | #include "device.h" /* FIXME: psim should provide the interface */ | |
31 | #include "events.h" /* FIXME: psim should provide the interface */ | |
32 | ||
33 | #ifdef HAVE_STDLIB_H | |
34 | #include <stdlib.h> | |
35 | #endif | |
36 | ||
37 | #ifdef HAVE_UNISTD_H | |
38 | #include <unistd.h> | |
39 | #endif | |
40 | ||
41 | #ifdef HAVE_STRING_H | |
42 | #include <string.h> | |
43 | #else | |
44 | #ifdef HAVE_STRINGS_H | |
45 | #include <strings.h> | |
46 | #endif | |
47 | #endif | |
48 | ||
49 | #include <errno.h> | |
50 | ||
51 | #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL) | |
52 | #undef WITH_STDIO | |
53 | #define WITH_STDIO DO_USE_STDIO | |
54 | #endif | |
55 | ||
56 | ||
57 | extern char **environ; | |
58 | ||
59 | static psim *simulation = NULL; | |
60 | ||
61 | ||
62 | void | |
63 | sim_io_poll_quit (void) | |
64 | { | |
65 | /* nothing to do */ | |
66 | } | |
67 | ||
68 | void | |
69 | sim_io_printf_filtered(const char *msg, ...) | |
70 | { | |
71 | va_list ap; | |
72 | va_start(ap, msg); | |
73 | vprintf(msg, ap); | |
74 | va_end(ap); | |
75 | } | |
76 | ||
77 | void | |
78 | error (const char *msg, ...) | |
79 | { | |
80 | va_list ap; | |
81 | va_start(ap, msg); | |
82 | vprintf(msg, ap); | |
83 | printf("\n"); | |
84 | va_end(ap); | |
85 | ||
86 | /* any final clean up */ | |
87 | if (ppc_trace[trace_print_info] && simulation != NULL) | |
88 | psim_print_info (simulation, ppc_trace[trace_print_info]); | |
89 | ||
90 | exit (1); | |
91 | } | |
92 | ||
93 | int | |
94 | sim_io_write_stdout(const char *buf, | |
95 | int sizeof_buf) | |
96 | { | |
97 | switch (CURRENT_STDIO) { | |
98 | case DO_USE_STDIO: | |
99 | { | |
100 | int i; | |
101 | for (i = 0; i < sizeof_buf; i++) { | |
102 | putchar(buf[i]); | |
103 | } | |
104 | return i; | |
105 | } | |
106 | break; | |
107 | case DONT_USE_STDIO: | |
108 | return write(1, buf, sizeof_buf); | |
109 | break; | |
110 | default: | |
111 | error("sim_io_write_stdout: invalid switch\n"); | |
112 | } | |
113 | return 0; | |
114 | } | |
115 | ||
116 | int | |
117 | sim_io_write_stderr(const char *buf, | |
118 | int sizeof_buf) | |
119 | { | |
120 | switch (CURRENT_STDIO) { | |
121 | case DO_USE_STDIO: | |
122 | { | |
123 | int i; | |
124 | for (i = 0; i < sizeof_buf; i++) { | |
125 | fputc(buf[i], stderr); | |
126 | } | |
127 | return i; | |
128 | } | |
129 | break; | |
130 | case DONT_USE_STDIO: | |
131 | return write(2, buf, sizeof_buf); | |
132 | break; | |
133 | default: | |
134 | error("sim_io_write_stdout: invalid switch\n"); | |
135 | } | |
136 | return 0; | |
137 | } | |
138 | ||
139 | int | |
140 | sim_io_read_stdin(char *buf, | |
141 | int sizeof_buf) | |
142 | { | |
143 | switch (CURRENT_STDIO) { | |
144 | case DO_USE_STDIO: | |
145 | if (sizeof_buf > 1) { | |
146 | if (fgets(buf, sizeof_buf, stdin) != NULL) | |
147 | return strlen(buf); | |
148 | } | |
149 | else if (sizeof_buf == 1) { | |
150 | char b[2]; | |
151 | if (fgets(b, sizeof(b), stdin) != NULL) { | |
152 | memcpy(buf, b, strlen(b)); | |
153 | return strlen(b); | |
154 | } | |
155 | } | |
156 | else if (sizeof_buf == 0) | |
157 | return 0; | |
158 | return sim_io_eof; | |
159 | break; | |
160 | case DONT_USE_STDIO: | |
161 | #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL) | |
162 | { | |
163 | /* check for input */ | |
164 | int flags; | |
165 | int status; | |
166 | int nr_read; | |
167 | int result; | |
168 | /* get the old status */ | |
169 | flags = fcntl(0, F_GETFL, 0); | |
170 | if (flags == -1) { | |
171 | perror("sim_io_read_stdin"); | |
172 | return sim_io_eof; | |
173 | } | |
174 | /* temp, disable blocking IO */ | |
175 | status = fcntl(0, F_SETFL, flags | O_NDELAY); | |
176 | if (status == -1) { | |
177 | perror("sim_io_read_stdin"); | |
178 | return sim_io_eof; | |
179 | } | |
180 | /* try for input */ | |
181 | nr_read = read(0, buf, sizeof_buf); | |
182 | if (nr_read > 0 | |
183 | || (nr_read == 0 && sizeof_buf == 0)) | |
184 | result = nr_read; | |
185 | else if (nr_read == 0) | |
186 | result = sim_io_eof; | |
187 | else { /* nr_read < 0 */ | |
188 | if (errno == EAGAIN) | |
189 | result = sim_io_not_ready; | |
190 | else | |
191 | result = sim_io_eof; | |
192 | } | |
193 | /* return to regular vewing */ | |
194 | status = fcntl(0, F_SETFL, flags); | |
195 | if (status == -1) { | |
196 | perror("sim_io_read_stdin"); | |
197 | return sim_io_eof; | |
198 | } | |
199 | return result; | |
200 | } | |
201 | break; | |
202 | #endif | |
203 | default: | |
204 | error("sim_io_read_stdin: invalid switch\n"); | |
205 | break; | |
206 | } | |
207 | return 0; | |
208 | } | |
209 | ||
210 | void | |
211 | sim_io_flush_stdoutput(void) | |
212 | { | |
213 | switch (CURRENT_STDIO) { | |
214 | case DO_USE_STDIO: | |
215 | fflush (stdout); | |
216 | break; | |
217 | case DONT_USE_STDIO: | |
218 | break; | |
219 | default: | |
220 | error("sim_io_flush_stdoutput: invalid switch\n"); | |
221 | break; | |
222 | } | |
223 | } | |
224 | ||
225 | ||
226 | void * | |
227 | zalloc(long size) | |
228 | { | |
229 | void *memory = malloc(size); | |
230 | if (memory == NULL) | |
231 | error("zmalloc failed\n"); | |
232 | memset(memory, 0, size); | |
233 | return memory; | |
234 | } | |
235 | ||
236 | void | |
237 | zfree(void *chunk) | |
238 | { | |
239 | free(chunk); | |
240 | } | |
241 | ||
242 | /* When a CNTRL-C occures, queue an event to shut down the simulation */ | |
243 | ||
244 | static RETSIGTYPE | |
245 | cntrl_c(int sig) | |
246 | { | |
247 | psim_stop (simulation); | |
248 | } | |
249 | ||
250 | ||
251 | int | |
252 | main(int argc, char **argv) | |
253 | { | |
254 | const char *name_of_file; | |
255 | char *arg_; | |
256 | psim_status status; | |
257 | device *root = psim_tree(); | |
258 | ||
259 | /* parse the arguments */ | |
260 | argv = psim_options(root, argv + 1); | |
261 | if (argv[0] == NULL) { | |
262 | if (ppc_trace[trace_opts]) { | |
263 | print_options (); | |
264 | return 0; | |
265 | } else { | |
266 | psim_usage(0); | |
267 | } | |
268 | } | |
269 | name_of_file = argv[0]; | |
270 | ||
271 | if (ppc_trace[trace_opts]) | |
272 | print_options (); | |
273 | ||
274 | /* create the simulator */ | |
275 | simulation = psim_create(name_of_file, root); | |
276 | ||
277 | /* fudge the environment so that _=prog-name */ | |
278 | arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1); | |
279 | strcpy(arg_, "_="); | |
280 | strcat(arg_, argv[0]); | |
281 | putenv(arg_); | |
282 | ||
283 | /* initialize it */ | |
284 | psim_init(simulation); | |
285 | psim_stack(simulation, argv, environ); | |
286 | ||
287 | { | |
288 | RETSIGTYPE (*prev) (); | |
289 | prev = signal(SIGINT, cntrl_c); | |
290 | psim_run(simulation); | |
291 | signal(SIGINT, prev); | |
292 | } | |
293 | ||
294 | /* any final clean up */ | |
295 | if (ppc_trace[trace_print_info]) | |
296 | psim_print_info (simulation, ppc_trace[trace_print_info]); | |
297 | ||
298 | /* why did we stop */ | |
299 | status = psim_get_status(simulation); | |
300 | switch (status.reason) { | |
301 | case was_continuing: | |
302 | error("psim: continuing while stoped!\n"); | |
303 | return 0; | |
304 | case was_trap: | |
305 | error("psim: no trap insn\n"); | |
306 | return 0; | |
307 | case was_exited: | |
308 | return status.signal; | |
309 | case was_signalled: | |
310 | printf ("%s: Caught signal %d at address 0x%lx\n", | |
311 | name_of_file, (int)status.signal, | |
312 | (long)status.program_counter); | |
313 | return status.signal; | |
314 | default: | |
315 | error("unknown halt condition\n"); | |
316 | return 0; | |
317 | } | |
318 | } |