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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include "device.h" /* FIXME: psim should provide the interface */
31 #include "events.h" /* FIXME: psim should provide the interface */
51 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
53 #define WITH_STDIO DO_USE_STDIO
57 extern char **environ;
59 static psim *simulation = NULL;
63 sim_io_poll_quit (void)
69 sim_io_printf_filtered(const char *msg, ...)
78 error (const char *msg, ...)
86 /* any final clean up */
87 if (ppc_trace[trace_print_info] && simulation != NULL)
88 psim_print_info (simulation, ppc_trace[trace_print_info]);
94 sim_io_write_stdout(const char *buf,
97 switch (CURRENT_STDIO) {
101 for (i = 0; i < sizeof_buf; i++) {
108 return write(1, buf, sizeof_buf);
111 error("sim_io_write_stdout: invalid switch\n");
117 sim_io_write_stderr(const char *buf,
120 switch (CURRENT_STDIO) {
124 for (i = 0; i < sizeof_buf; i++) {
125 fputc(buf[i], stderr);
131 return write(2, buf, sizeof_buf);
134 error("sim_io_write_stdout: invalid switch\n");
140 sim_io_read_stdin(char *buf,
143 switch (CURRENT_STDIO) {
145 if (sizeof_buf > 1) {
146 if (fgets(buf, sizeof_buf, stdin) != NULL)
149 else if (sizeof_buf == 1) {
151 if (fgets(b, sizeof(b), stdin) != NULL) {
152 memcpy(buf, b, strlen(b));
156 else if (sizeof_buf == 0)
161 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
163 /* check for input */
168 /* get the old status */
169 flags = fcntl(0, F_GETFL, 0);
171 perror("sim_io_read_stdin");
174 /* temp, disable blocking IO */
175 status = fcntl(0, F_SETFL, flags | O_NDELAY);
177 perror("sim_io_read_stdin");
181 nr_read = read(0, buf, sizeof_buf);
183 || (nr_read == 0 && sizeof_buf == 0))
185 else if (nr_read == 0)
187 else { /* nr_read < 0 */
189 result = sim_io_not_ready;
193 /* return to regular vewing */
194 status = fcntl(0, F_SETFL, flags);
196 perror("sim_io_read_stdin");
204 error("sim_io_read_stdin: invalid switch\n");
211 sim_io_flush_stdoutput(void)
213 switch (CURRENT_STDIO) {
220 error("sim_io_flush_stdoutput: invalid switch\n");
229 void *memory = malloc(size);
231 error("zmalloc failed\n");
232 memset(memory, 0, size);
242 /* When a CNTRL-C occures, queue an event to shut down the simulation */
247 psim_stop (simulation);
252 main(int argc, char **argv)
254 const char *name_of_file;
257 device *root = psim_tree();
259 /* parse the arguments */
260 argv = psim_options(root, argv + 1);
261 if (argv[0] == NULL) {
262 if (ppc_trace[trace_opts]) {
269 name_of_file = argv[0];
271 if (ppc_trace[trace_opts])
274 /* create the simulator */
275 simulation = psim_create(name_of_file, root);
277 /* fudge the environment so that _=prog-name */
278 arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
280 strcat(arg_, argv[0]);
284 psim_init(simulation);
285 psim_stack(simulation, argv, environ);
288 RETSIGTYPE (*prev) ();
289 prev = signal(SIGINT, cntrl_c);
290 psim_run(simulation);
291 signal(SIGINT, prev);
294 /* any final clean up */
295 if (ppc_trace[trace_print_info])
296 psim_print_info (simulation, ppc_trace[trace_print_info]);
298 /* why did we stop */
299 status = psim_get_status(simulation);
300 switch (status.reason) {
302 error("psim: continuing while stoped!\n");
305 error("psim: no trap insn\n");
308 return status.signal;
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;
315 error("unknown halt condition\n");