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 */
34 #include "gdb/callback.h"
35 #include "gdb/remote-sim.h"
55 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
57 #define WITH_STDIO DO_USE_STDIO
61 extern char **environ;
63 static psim *simulation = NULL;
67 sim_io_poll_quit (void)
73 sim_io_printf_filtered(const char *msg, ...)
82 error (const char *msg, ...)
90 /* any final clean up */
91 if (ppc_trace[trace_print_info] && simulation != NULL)
92 psim_print_info (simulation, ppc_trace[trace_print_info]);
98 sim_io_write_stdout(const char *buf,
101 switch (CURRENT_STDIO) {
105 for (i = 0; i < sizeof_buf; i++) {
112 return write(1, buf, sizeof_buf);
115 error("sim_io_write_stdout: invalid switch\n");
121 sim_io_write_stderr(const char *buf,
124 switch (CURRENT_STDIO) {
128 for (i = 0; i < sizeof_buf; i++) {
129 fputc(buf[i], stderr);
135 return write(2, buf, sizeof_buf);
138 error("sim_io_write_stdout: invalid switch\n");
144 sim_io_read_stdin(char *buf,
147 switch (CURRENT_STDIO) {
149 if (sizeof_buf > 1) {
150 if (fgets(buf, sizeof_buf, stdin) != NULL)
153 else if (sizeof_buf == 1) {
155 if (fgets(b, sizeof(b), stdin) != NULL) {
156 memcpy(buf, b, strlen(b));
160 else if (sizeof_buf == 0)
165 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
167 /* check for input */
172 /* get the old status */
173 flags = fcntl(0, F_GETFL, 0);
175 perror("sim_io_read_stdin");
178 /* temp, disable blocking IO */
179 status = fcntl(0, F_SETFL, flags | O_NDELAY);
181 perror("sim_io_read_stdin");
185 nr_read = read(0, buf, sizeof_buf);
187 || (nr_read == 0 && sizeof_buf == 0))
189 else if (nr_read == 0)
191 else { /* nr_read < 0 */
193 result = sim_io_not_ready;
197 /* return to regular vewing */
198 status = fcntl(0, F_SETFL, flags);
200 perror("sim_io_read_stdin");
208 error("sim_io_read_stdin: invalid switch\n");
215 sim_io_flush_stdoutput(void)
217 switch (CURRENT_STDIO) {
224 error("sim_io_flush_stdoutput: invalid switch\n");
230 sim_io_error (SIM_DESC sd, const char *msg, ...)
238 /* any final clean up */
239 if (ppc_trace[trace_print_info] && simulation != NULL)
240 psim_print_info (simulation, ppc_trace[trace_print_info]);
249 void *memory = malloc(size);
251 error("zalloc failed\n");
252 memset(memory, 0, size);
262 /* When a CNTRL-C occures, queue an event to shut down the simulation */
267 psim_stop (simulation);
272 main(int argc, char **argv)
274 const char *name_of_file;
277 device *root = psim_tree();
279 /* parse the arguments */
280 argv = psim_options(root, argv + 1);
281 if (argv[0] == NULL) {
282 if (ppc_trace[trace_opts]) {
289 name_of_file = argv[0];
291 if (ppc_trace[trace_opts])
294 /* create the simulator */
295 simulation = psim_create(name_of_file, root);
297 /* fudge the environment so that _=prog-name */
298 arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
300 strcat(arg_, argv[0]);
304 psim_init(simulation);
305 psim_stack(simulation, argv, environ);
308 RETSIGTYPE (*prev) ();
309 prev = signal(SIGINT, cntrl_c);
310 psim_run(simulation);
311 signal(SIGINT, prev);
314 /* any final clean up */
315 if (ppc_trace[trace_print_info])
316 psim_print_info (simulation, ppc_trace[trace_print_info]);
318 /* why did we stop */
319 status = psim_get_status(simulation);
320 switch (status.reason) {
322 error("psim: continuing while stoped!\n");
325 error("psim: no trap insn\n");
328 return status.signal;
330 printf ("%s: Caught signal %d at address 0x%lx\n",
331 name_of_file, (int)status.signal,
332 (long)status.program_counter);
333 return status.signal;
335 error("unknown halt condition\n");