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