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/>.
29 #include "device.h" /* FIXME: psim should provide the interface */
30 #include "events.h" /* FIXME: psim should provide the interface */
33 #include "gdb/callback.h"
34 #include "gdb/remote-sim.h"
54 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
56 #define WITH_STDIO DO_USE_STDIO
60 extern char **environ
;
62 static psim
*simulation
= NULL
;
66 sim_io_poll_quit (void)
72 sim_io_printf_filtered(const char *msg
, ...)
81 error (const char *msg
, ...)
89 /* any final clean up */
90 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
91 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
97 sim_io_write_stdout(const char *buf
,
100 switch (CURRENT_STDIO
) {
104 for (i
= 0; i
< sizeof_buf
; i
++) {
111 return write(1, buf
, sizeof_buf
);
114 error("sim_io_write_stdout: invalid switch\n");
120 sim_io_write_stderr(const char *buf
,
123 switch (CURRENT_STDIO
) {
127 for (i
= 0; i
< sizeof_buf
; i
++) {
128 fputc(buf
[i
], stderr
);
134 return write(2, buf
, sizeof_buf
);
137 error("sim_io_write_stdout: invalid switch\n");
143 sim_io_read_stdin(char *buf
,
146 switch (CURRENT_STDIO
) {
148 if (sizeof_buf
> 1) {
149 if (fgets(buf
, sizeof_buf
, stdin
) != NULL
)
152 else if (sizeof_buf
== 1) {
154 if (fgets(b
, sizeof(b
), stdin
) != NULL
) {
155 memcpy(buf
, b
, strlen(b
));
159 else if (sizeof_buf
== 0)
164 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
166 /* check for input */
171 /* get the old status */
172 flags
= fcntl(0, F_GETFL
, 0);
174 perror("sim_io_read_stdin");
177 /* temp, disable blocking IO */
178 status
= fcntl(0, F_SETFL
, flags
| O_NDELAY
);
180 perror("sim_io_read_stdin");
184 nr_read
= read(0, buf
, sizeof_buf
);
186 || (nr_read
== 0 && sizeof_buf
== 0))
188 else if (nr_read
== 0)
190 else { /* nr_read < 0 */
192 result
= sim_io_not_ready
;
196 /* return to regular vewing */
197 status
= fcntl(0, F_SETFL
, flags
);
199 perror("sim_io_read_stdin");
207 error("sim_io_read_stdin: invalid switch\n");
214 sim_io_flush_stdoutput(void)
216 switch (CURRENT_STDIO
) {
223 error("sim_io_flush_stdoutput: invalid switch\n");
229 sim_io_error (SIM_DESC sd
, const char *msg
, ...)
237 /* any final clean up */
238 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
239 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
248 void *memory
= malloc(size
);
250 error("zalloc failed\n");
251 memset(memory
, 0, size
);
255 /* When a CNTRL-C occures, queue an event to shut down the simulation */
260 psim_stop (simulation
);
265 main(int argc
, char **argv
)
267 const char *name_of_file
;
270 device
*root
= psim_tree();
272 /* parse the arguments */
273 argv
= psim_options (root
, argv
+ 1, SIM_OPEN_STANDALONE
);
274 if (argv
[0] == NULL
) {
275 if (ppc_trace
[trace_opts
]) {
279 psim_usage (0, 0, SIM_OPEN_STANDALONE
);
282 name_of_file
= argv
[0];
284 if (ppc_trace
[trace_opts
])
287 /* create the simulator */
288 simulation
= psim_create(name_of_file
, root
);
290 /* fudge the environment so that _=prog-name */
291 arg_
= (char*)zalloc(strlen(argv
[0]) + strlen("_=") + 1);
293 strcat(arg_
, argv
[0]);
297 psim_init(simulation
);
298 psim_stack(simulation
, argv
, environ
);
301 RETSIGTYPE (*prev
) ();
302 prev
= signal(SIGINT
, cntrl_c
);
303 psim_run(simulation
);
304 signal(SIGINT
, prev
);
307 /* any final clean up */
308 if (ppc_trace
[trace_print_info
])
309 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
311 /* why did we stop */
312 status
= psim_get_status(simulation
);
313 switch (status
.reason
) {
315 error("psim: continuing while stopped!\n");
318 error("psim: no trap insn\n");
321 return status
.signal
;
323 printf ("%s: Caught signal %d at address 0x%lx\n",
324 name_of_file
, (int)status
.signal
,
325 (long)status
.program_counter
);
326 return status
.signal
;
328 error("unknown halt condition\n");