1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* This must come before any other includes. */
31 #include "device.h" /* FIXME: psim should provide the interface */
32 #include "events.h" /* FIXME: psim should provide the interface */
35 #include "sim/callback.h"
45 #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL)
47 #define WITH_STDIO DO_USE_STDIO
51 static psim
*simulation
= NULL
;
55 sim_io_poll_quit (void)
61 sim_io_printf_filtered(const char *msg
, ...)
70 error (const char *msg
, ...)
78 /* any final clean up */
79 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
80 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
86 sim_io_write_stdout(const char *buf
,
89 switch (CURRENT_STDIO
) {
93 for (i
= 0; i
< sizeof_buf
; i
++) {
100 return write(1, buf
, sizeof_buf
);
103 error("sim_io_write_stdout: invalid switch\n");
109 sim_io_write_stderr(const char *buf
,
112 switch (CURRENT_STDIO
) {
116 for (i
= 0; i
< sizeof_buf
; i
++) {
117 fputc(buf
[i
], stderr
);
123 return write(2, buf
, sizeof_buf
);
126 error("sim_io_write_stdout: invalid switch\n");
132 sim_io_read_stdin(char *buf
,
135 switch (CURRENT_STDIO
) {
137 if (sizeof_buf
> 1) {
138 if (fgets(buf
, sizeof_buf
, stdin
) != NULL
)
141 else if (sizeof_buf
== 1) {
143 if (fgets(b
, sizeof(b
), stdin
) != NULL
) {
144 memcpy(buf
, b
, strlen(b
));
148 else if (sizeof_buf
== 0)
153 #if defined(O_NONBLOCK) && defined(F_GETFL) && defined(F_SETFL)
155 /* check for input */
160 /* get the old status */
161 flags
= fcntl(0, F_GETFL
, 0);
163 perror("sim_io_read_stdin");
166 /* temp, disable blocking IO */
167 status
= fcntl(0, F_SETFL
, flags
| O_NONBLOCK
);
169 perror("sim_io_read_stdin");
173 nr_read
= read(0, buf
, sizeof_buf
);
175 || (nr_read
== 0 && sizeof_buf
== 0))
177 else if (nr_read
== 0)
179 else { /* nr_read < 0 */
181 result
= sim_io_not_ready
;
185 /* return to regular vewing */
186 status
= fcntl(0, F_SETFL
, flags
);
188 perror("sim_io_read_stdin");
196 error("sim_io_read_stdin: invalid switch\n");
203 sim_io_flush_stdoutput(void)
205 switch (CURRENT_STDIO
) {
212 error("sim_io_flush_stdoutput: invalid switch\n");
217 /* Glue to use sim-fpu module. */
220 sim_io_error (SIM_DESC sd
, const char *msg
, ...)
228 /* any final clean up */
229 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
230 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
239 void *memory
= malloc(size
);
241 error("zalloc failed\n");
242 memset(memory
, 0, size
);
246 /* When a CNTRL-C occures, queue an event to shut down the simulation */
251 psim_stop (simulation
);
256 main(int argc
, char * const *argv
)
258 const char *name_of_file
;
261 device
*root
= psim_tree();
263 /* parse the arguments */
264 argv
= psim_options (root
, argv
+ 1, SIM_OPEN_STANDALONE
);
265 if (argv
[0] == NULL
) {
266 if (ppc_trace
[trace_opts
]) {
270 psim_usage (0, 0, SIM_OPEN_STANDALONE
);
273 name_of_file
= argv
[0];
275 if (ppc_trace
[trace_opts
])
278 /* create the simulator */
279 simulation
= psim_create(name_of_file
, root
);
281 /* fudge the environment so that _=prog-name */
282 arg_
= (char*)zalloc(strlen(argv
[0]) + strlen("_=") + 1);
284 strcat(arg_
, argv
[0]);
288 psim_init(simulation
);
289 psim_stack(simulation
, argv
, environ
);
292 RETSIGTYPE (*prev
) ();
293 prev
= signal(SIGINT
, cntrl_c
);
294 psim_run(simulation
);
295 signal(SIGINT
, prev
);
298 /* any final clean up */
299 if (ppc_trace
[trace_print_info
])
300 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
302 /* why did we stop */
303 status
= psim_get_status(simulation
);
304 switch (status
.reason
) {
306 error("psim: continuing while stopped!\n");
309 error("psim: no trap insn\n");
312 return status
.signal
;
314 printf ("%s: Caught signal %d at address 0x%lx\n",
315 name_of_file
, (int)status
.signal
,
316 (long)status
.program_counter
);
317 return status
.signal
;
319 error("unknown halt condition\n");