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.
24 #include "targ-vals.h"
26 /* See the file include/callbacks.h for a description */
30 sim_io_init(SIM_DESC sd)
32 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
37 sim_io_shutdown(SIM_DESC sd)
39 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
44 sim_io_unlink(SIM_DESC sd,
47 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
52 sim_io_time(SIM_DESC sd,
55 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd), t);
60 sim_io_system(SIM_DESC sd, const char *s)
62 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
67 sim_io_rename(SIM_DESC sd,
71 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
76 sim_io_write_stdout(SIM_DESC sd,
80 switch (CURRENT_STDIO) {
82 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
85 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
88 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
96 sim_io_flush_stdout(SIM_DESC sd)
98 switch (CURRENT_STDIO) {
100 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
105 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
112 sim_io_write_stderr(SIM_DESC sd,
116 switch (CURRENT_STDIO) {
118 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
121 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
124 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
132 sim_io_flush_stderr(SIM_DESC sd)
134 switch (CURRENT_STDIO) {
136 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
141 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
148 sim_io_write(SIM_DESC sd,
153 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
158 sim_io_read_stdin(SIM_DESC sd,
162 switch (CURRENT_STDIO) {
164 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
167 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
170 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
178 sim_io_read(SIM_DESC sd, int fd,
182 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
187 sim_io_open(SIM_DESC sd,
191 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
196 sim_io_lseek(SIM_DESC sd,
201 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
206 sim_io_isatty(SIM_DESC sd,
209 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
214 sim_io_get_errno(SIM_DESC sd)
216 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
221 sim_io_close(SIM_DESC sd,
224 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
229 sim_io_printf(SIM_DESC sd,
235 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
241 sim_io_vprintf(SIM_DESC sd,
245 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
250 sim_io_eprintf(SIM_DESC sd,
256 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
262 sim_io_evprintf(SIM_DESC sd,
266 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
271 sim_io_error(SIM_DESC sd,
275 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
278 vfprintf (stderr, fmt, ap);
280 fprintf (stderr, "\n");
286 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
288 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "");
294 sim_io_poll_quit(SIM_DESC sd)
296 if (STATE_CALLBACK (sd)->poll_quit != NULL)
297 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
303 sim_io_getstring(SIM_DESC sd,
306 address_word target_string)
311 while (sim_core_read_1 (cpu, cia, sim_core_read_map, target_string+len) != 0)
314 buf = NZALLOC (char, len+1);
316 sim_core_read_buffer (sd, cpu, sim_core_read_map, buf, target_string, len);
321 sim_io_syscalls(SIM_DESC sd,
328 address_word *syscall_return,
329 address_word *errno_return)
331 sim_cpu *cpu = STATE_CPU (sd, 0);
332 struct host_callback_struct *callback = STATE_CALLBACK (sd);
343 case 1: /* exit (status) */
344 sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, cia, sim_exited, parm1);
347 case 2: /* open (filename, flags, mode) */
348 buf = sim_io_getstring (sd, cpu, cia, parm1);
349 ret = *syscall_return = callback->open (callback, buf, parm2);
351 *errno_return = callback->get_errno (callback);
356 case 3: /* close (filedes) */
357 ret = *syscall_return = callback->close (callback, parm1);
359 *errno_return = callback->get_errno (callback);
362 case 4: /* read (filedes, buffer, len) */
365 buf = NZALLOC (char, len);
368 len2 = sim_io_read_stdin (sd, buf, len);
370 len2 = sim_io_read (sd, fd, buf, len);
374 len3 = sim_core_write_buffer (sd, cpu, sim_core_write_map, buf, parm2,
378 sim_engine_abort (sd, cpu, cia,
379 "Could only write back %d bytes for read system call, wanted to write %d\n",
382 *syscall_return = len2;
385 *errno_return = callback->get_errno (callback);
390 case 5: /* write (filedes, buffer, len) */
393 buf = NZALLOC (char, len);
395 len = sim_core_read_buffer (sd, cpu, sim_core_read_map, buf, parm2, len);
398 len2 = sim_io_write_stdout (sd, buf, len);
399 sim_io_flush_stdout (sd);
403 len2 = sim_io_write_stderr (sd, buf, len);
404 sim_io_flush_stderr (sd);
407 len2 = sim_io_write (sd, fd, buf, len);
410 *syscall_return = len2;
412 *errno_return = callback->get_errno (callback);
417 case 6: /* lseek (filedes, offset, whence) */
418 *errno_return = TARGET_ENOSYS;
421 case 7: /* unlink (filename) */
422 buf = sim_io_getstring (sd, cpu, cia, parm1);
423 ret = *syscall_return = callback->unlink (callback, buf);
425 *errno_return = callback->get_errno (callback);
430 case 8: /* getpid () */
431 case 9: /* kill (signal, pid) */
432 case 10: /* fstat (filedes, packet) */
433 case 11: /* reserved for sbrk */
434 case 12: /* argvlen () */
435 case 13: /* argv () */
436 case 14: /* chdir (dir) */
437 case 15: /* stat (filename, packet) */
438 case 16: /* chmod (filename, mode) */
439 case 17: /* utime (filename, packet) */
440 case 18: /* time (time_t *) */
441 *errno_return = TARGET_ENOSYS;
445 sim_engine_abort (sd, cpu, cia, "Unknown monitor call %d", syscall);
450 *syscall_return = -1;