1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, 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_printf_filtered(const char *msg
, ...)
72 error (char *msg
, ...)
80 /* any final clean up */
81 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
82 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
88 sim_io_write_stdout(const char *buf
,
91 switch (CURRENT_STDIO
) {
95 for (i
= 0; i
< sizeof_buf
; i
++) {
102 return write(1, buf
, sizeof_buf
);
105 error("sim_io_write_stdout: invalid switch\n");
111 sim_io_write_stderr(const char *buf
,
114 switch (CURRENT_STDIO
) {
118 for (i
= 0; i
< sizeof_buf
; i
++) {
119 fputc(buf
[i
], stderr
);
125 return write(2, buf
, sizeof_buf
);
128 error("sim_io_write_stdout: invalid switch\n");
134 sim_io_read_stdin(char *buf
,
137 switch (CURRENT_STDIO
) {
139 if (sizeof_buf
> 1) {
140 if (fgets(buf
, sizeof_buf
, stdin
) != NULL
)
143 else if (sizeof_buf
== 1) {
145 if (fgets(b
, sizeof(b
), stdin
) != NULL
) {
146 memcpy(buf
, b
, strlen(b
));
150 else if (sizeof_buf
== 0)
155 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
157 /* check for input */
162 /* get the old status */
163 flags
= fcntl(0, F_GETFL
, 0);
165 perror("sim_io_read_stdin");
168 /* temp, disable blocking IO */
169 status
= fcntl(0, F_SETFL
, flags
| O_NDELAY
);
171 perror("sim_io_read_stdin");
175 nr_read
= read(0, buf
, sizeof_buf
);
177 || (nr_read
== 0 && sizeof_buf
== 0))
179 else if (nr_read
== 0)
181 else { /* nr_read < 0 */
183 result
= sim_io_not_ready
;
187 /* return to regular vewing */
188 status
= fcntl(0, F_SETFL
, flags
);
190 perror("sim_io_read_stdin");
198 error("sim_io_read_stdin: invalid switch\n");
205 sim_io_flush_stdoutput(void)
207 switch (CURRENT_STDIO
) {
214 error("sim_io_flush_stdoutput: invalid switch\n");
223 void *memory
= malloc(size
);
225 error("zmalloc failed\n");
226 memset(memory
, 0, size
);
236 /* When a CNTRL-C occures, queue an event to shut down the simulation */
241 psim_stop (simulation
);
246 main(int argc
, char **argv
)
248 const char *name_of_file
;
251 device
*root
= psim_tree();
253 /* parse the arguments */
254 argv
= psim_options(root
, argv
+ 1);
255 if (argv
[0] == NULL
) {
256 if (ppc_trace
[trace_opts
]) {
263 name_of_file
= argv
[0];
265 if (ppc_trace
[trace_opts
])
268 /* create the simulator */
269 simulation
= psim_create(name_of_file
, root
);
271 /* fudge the environment so that _=prog-name */
272 arg_
= (char*)zalloc(strlen(argv
[0]) + strlen("_=") + 1);
274 strcat(arg_
, argv
[0]);
278 psim_init(simulation
);
279 psim_stack(simulation
, argv
, environ
);
282 RETSIGTYPE (*prev
) ();
283 prev
= signal(SIGINT
, cntrl_c
);
284 psim_run(simulation
);
285 signal(SIGINT
, prev
);
288 /* any final clean up */
289 if (ppc_trace
[trace_print_info
])
290 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
292 /* why did we stop */
293 status
= psim_get_status(simulation
);
294 switch (status
.reason
) {
296 error("psim: continuing while stoped!\n");
299 error("psim: no trap insn\n");
302 return status
.signal
;
304 printf ("%s: Caught signal %d at address 0x%lx\n",
305 name_of_file
, (int)status
.signal
,
306 (long)status
.program_counter
);
307 return status
.signal
;
309 error("unknown halt condition\n");