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.
28 #include "device.h" /* FIXME: psim should provide the interface */
48 #if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
50 #define WITH_STDIO DO_USE_STDIO
54 extern char **environ
;
56 static psim
*simulation
= NULL
;
60 sim_io_printf_filtered(const char *msg
, ...)
69 error (char *msg
, ...)
77 /* any final clean up */
78 if (ppc_trace
[trace_print_info
] && simulation
!= NULL
)
79 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
85 sim_io_write_stdout(const char *buf
,
88 switch (CURRENT_STDIO
) {
92 for (i
= 0; i
< sizeof_buf
; i
++) {
99 return write(1, buf
, sizeof_buf
);
102 error("sim_io_write_stdout: invalid switch\n");
108 sim_io_write_stderr(const char *buf
,
111 switch (CURRENT_STDIO
) {
115 for (i
= 0; i
< sizeof_buf
; i
++) {
116 fputc(buf
[i
], stderr
);
122 return write(2, buf
, sizeof_buf
);
125 error("sim_io_write_stdout: invalid switch\n");
131 sim_io_read_stdin(char *buf
,
134 switch (CURRENT_STDIO
) {
136 if (sizeof_buf
> 1) {
137 if (fgets(buf
, sizeof_buf
, stdin
) != NULL
)
140 else if (sizeof_buf
== 1) {
142 if (fgets(b
, sizeof(b
), stdin
) != NULL
) {
143 memcpy(buf
, b
, strlen(b
));
147 else if (sizeof_buf
== 0)
153 /* check for input */
158 /* get the old status */
159 flags
= fcntl(0, F_GETFL
, 0);
161 perror("sim_io_read_stdin");
164 /* temp, disable blocking IO */
165 status
= fcntl(0, F_SETFL
, flags
| O_NDELAY
);
167 perror("sim_io_read_stdin");
171 nr_read
= read(0, buf
, sizeof_buf
);
173 || (nr_read
== 0 && sizeof_buf
== 0))
175 else if (nr_read
== 0)
177 else { /* nr_read < 0 */
179 result
= sim_io_not_ready
;
183 /* return to regular vewing */
184 status
= fcntl(0, F_SETFL
, flags
);
186 perror("sim_io_read_stdin");
193 error("sim_io_read_stdin: invalid switch\n");
200 sim_io_flush_stdoutput(void)
202 switch (CURRENT_STDIO
) {
209 error("sim_io_flush_stdoutput: invalid switch\n");
218 void *memory
= malloc(size
);
220 error("zmalloc failed\n");
221 memset(memory
, 0, size
);
232 main(int argc
, char **argv
)
234 const char *name_of_file
;
237 device
*root
= psim_tree();
239 /* parse the arguments */
240 argv
= psim_options(root
, argv
+ 1);
241 if (argv
[0] == NULL
) {
242 if (ppc_trace
[trace_opts
]) {
249 name_of_file
= argv
[0];
251 if (ppc_trace
[trace_opts
])
254 /* create the simulator */
255 simulation
= psim_create(name_of_file
, root
);
257 /* fudge the environment so that _=prog-name */
258 arg_
= (char*)zalloc(strlen(argv
[0]) + strlen("_=") + 1);
260 strcat(arg_
, argv
[0]);
264 psim_init(simulation
);
265 psim_stack(simulation
, argv
, environ
);
267 psim_run(simulation
);
269 /* any final clean up */
270 if (ppc_trace
[trace_print_info
])
271 psim_print_info (simulation
, ppc_trace
[trace_print_info
]);
273 /* why did we stop */
274 status
= psim_get_status(simulation
);
275 switch (status
.reason
) {
277 error("psim: continuing while stoped!\n");
280 error("psim: no trap insn\n");
283 return status
.signal
;
285 printf ("%s: Caught signal %d at address 0x%lx\n",
286 name_of_file
, (int)status
.signal
,
287 (long)status
.program_counter
);
288 return status
.signal
;
290 error("unknown halt condition\n");
This page took 0.034243 seconds and 4 git commands to generate.