]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/main.c
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 "sim/callback.h"
43 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
45 #define WITH_STDIO DO_USE_STDIO
49 extern char **environ
;
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_NDELAY) && 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_NDELAY
);
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");
218 sim_io_error (SIM_DESC sd
, const char *msg
, ...)
226 /* any final clean up */
227 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
228 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
237 void *memory
= malloc(size
);
239 error("zalloc failed\n");
240 memset(memory
, 0, size
);
244 /* When a CNTRL-C occures, queue an event to shut down the simulation */
249 psim_stop (simulation
);
254 main(int argc
, char **argv
)
256 const char *name_of_file
;
259 device
*root
= psim_tree();
261 /* parse the arguments */
262 argv
= psim_options (root
, argv
+ 1, SIM_OPEN_STANDALONE
);
263 if (argv
[0] == NULL
) {
264 if (ppc_trace
[trace_opts
]) {
268 psim_usage (0, 0, SIM_OPEN_STANDALONE
);
271 name_of_file
= argv
[0];
273 if (ppc_trace
[trace_opts
])
276 /* create the simulator */
277 simulation
= psim_create(name_of_file
, root
);
279 /* fudge the environment so that _=prog-name */
280 arg_
= (char*)zalloc(strlen(argv
[0]) + strlen("_=") + 1);
282 strcat(arg_
, argv
[0]);
286 psim_init(simulation
);
287 psim_stack(simulation
, argv
, environ
);
290 RETSIGTYPE (*prev
) ();
291 prev
= signal(SIGINT
, cntrl_c
);
292 psim_run(simulation
);
293 signal(SIGINT
, prev
);
296 /* any final clean up */
297 if (ppc_trace
[trace_print_info
])
298 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
300 /* why did we stop */
301 status
= psim_get_status(simulation
);
302 switch (status
.reason
) {
304 error("psim: continuing while stopped!\n");
307 error("psim: no trap insn\n");
310 return status
.signal
;
312 printf ("%s: Caught signal %d at address 0x%lx\n",
313 name_of_file
, (int)status
.signal
,
314 (long)status
.program_counter
);
315 return status
.signal
;
317 error("unknown halt condition\n");