1 /* Simulator hardware option handling.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Cygnus Support and Andrew Cagney.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "sim-assert.h"
23 #include "sim-options.h"
28 #include "hw-device.h"
43 #include <sys/errno.h>
50 /* if called from a processor */
57 sim_hw_parse (struct sim_state *sd,
63 hw_tree_vparse (STATE_HW (sd)->tree, fmt, ap);
68 struct sim_state *file;
69 void (*print) (struct sim_state *, const char *, va_list ap);
73 do_print (void *file, const char *fmt, ...)
75 struct printer *p = file;
78 p->print (p->file, fmt, ap);
83 sim_hw_print (struct sim_state *sd,
84 void (*print) (struct sim_state *, const char *, va_list ap))
89 hw_tree_print (STATE_HW (sd)->tree, do_print, &p);
95 /* command line options. */
98 OPTION_HW_INFO = OPTION_START,
104 static DECLARE_OPTION_HANDLER (hw_option_handler);
106 static const OPTION hw_options[] =
108 { {"hw-info", no_argument, NULL, OPTION_HW_INFO },
109 '\0', NULL, "List configurable hw regions",
111 { {"info-hw", no_argument, NULL, OPTION_HW_INFO },
115 { {"hw-trace", optional_argument, NULL, OPTION_HW_TRACE },
116 '\0', "on|off", "Trace all hardware devices",
118 { {"trace-hw", optional_argument, NULL, OPTION_HW_TRACE },
122 { {"hw-device", required_argument, NULL, OPTION_HW_DEVICE },
123 '\0', "DEVICE", "Add the specified device",
126 { {"hw-file", required_argument, NULL, OPTION_HW_FILE },
127 '\0', "FILE", "Add the devices listed in the file",
130 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
135 /* Copied from ../ppc/psim.c:psim_merge_device_file() */
138 merge_device_file (struct sim_state *sd,
139 const char *file_name)
142 struct hw *current = STATE_HW (sd)->tree;
144 char device_path[1000];
146 /* try opening the file */
147 description = fopen (file_name, "r");
148 if (description == NULL)
155 while (fgets (device_path, sizeof(device_path), description))
158 /* check that a complete line was read */
159 if (strchr (device_path, '\n') == NULL)
161 fclose (description);
162 sim_io_eprintf (sd, "%s:%d: line to long", file_name, line_nr);
165 *strchr (device_path, '\n') = '\0';
167 /* skip comments ("#" or ";") and blank lines lines */
168 for (device = device_path;
169 *device != '\0' && isspace (*device);
173 || device[0] == '\0')
175 /* merge any appended lines */
176 while (device_path[strlen (device_path) - 1] == '\\')
178 int curlen = strlen (device_path) - 1;
179 /* zap the `\' at the end of the line */
180 device_path[curlen] = '\0';
181 /* append the next line */
182 if (!fgets (device_path + curlen,
183 sizeof (device_path) - curlen,
186 fclose (description);
187 sim_io_eprintf (sd, "%s:%d: unexpected eof", file_name, line_nr);
190 if (strchr(device_path, '\n') == NULL)
193 sim_io_eprintf (sd, "%s:%d: line to long", file_name, line_nr);
196 *strchr(device_path, '\n') = '\0';
199 /* parse this line */
200 current = hw_tree_parse (current, "%s", device);
202 fclose (description);
208 hw_option_handler (struct sim_state *sd, sim_cpu *cpu, int opt,
209 char *arg, int is_command)
216 /* delay info until after the tree is finished */
217 STATE_HW (sd)->info_p = 1;
222 case OPTION_HW_TRACE:
226 STATE_HW (sd)->trace_p = 1;
228 else if (strcmp (arg, "yes") == 0
229 || strcmp (arg, "on") == 0)
231 STATE_HW (sd)->trace_p = 1;
233 else if (strcmp (arg, "no") == 0
234 || strcmp (arg, "off") == 0)
236 STATE_HW (sd)->trace_p = 0;
240 sim_io_eprintf (sd, "Option --hw-trace ignored\n");
241 /* set tracing on all devices */
244 /* FIXME: Not very nice - see also hw-base.c */
245 if (STATE_HW (sd)->trace_p)
246 hw_tree_parse (STATE_HW (sd)->tree, "/global-trace? true");
251 case OPTION_HW_DEVICE:
253 hw_tree_parse (STATE_HW (sd)->tree, arg);
259 return merge_device_file (sd, arg);
263 sim_io_eprintf (sd, "Unknown hw option %d\n", opt);
272 /* "hw" module install handler.
274 This is called via sim_module_install to install the "hw" subsystem
275 into the simulator. */
277 static MODULE_INIT_FN sim_hw_init;
278 static MODULE_UNINSTALL_FN sim_hw_uninstall;
281 sim_hw_install (struct sim_state *sd)
283 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
284 sim_add_option_table (sd, NULL, hw_options);
285 sim_module_add_uninstall_fn (sd, sim_hw_uninstall);
286 sim_module_add_init_fn (sd, sim_hw_init);
287 STATE_HW (sd) = ZALLOC (struct sim_hw);
288 STATE_HW (sd)->tree = hw_tree_create (sd, "core");
294 sim_hw_init (struct sim_state *sd)
296 /* FIXME: anything needed? */
297 hw_tree_finish (STATE_HW (sd)->tree);
298 if (STATE_HW (sd)->info_p)
299 sim_hw_print (sd, sim_io_vprintf);
303 /* Uninstall the "hw" subsystem from the simulator. */
306 sim_hw_uninstall (struct sim_state *sd)
308 /* hw_tree_delete (STATE_HW (sd)->tree); */
309 zfree (STATE_HW (sd));
310 STATE_HW (sd) = NULL;
315 /* Data transfers to/from the hardware device tree. There are several
319 /* CPU: The simulation is running and the current CPU/CIA
320 initiates a data transfer. */
323 sim_cpu_hw_io_read_buffer (sim_cpu *cpu,
331 SIM_DESC sd = CPU_STATE (cpu);
332 STATE_HW (sd)->cpu = cpu;
333 STATE_HW (sd)->cia = cia;
334 if (hw_io_read_buffer (hw, dest, space, addr, nr_bytes) != nr_bytes)
335 sim_engine_abort (sd, cpu, cia, "broken CPU read");
339 sim_cpu_hw_io_write_buffer (sim_cpu *cpu,
347 SIM_DESC sd = CPU_STATE (cpu);
348 STATE_HW (sd)->cpu = cpu;
349 STATE_HW (sd)->cia = cia;
350 if (hw_io_write_buffer (hw, source, space, addr, nr_bytes) != nr_bytes)
351 sim_engine_abort (sd, cpu, cia, "broken CPU write");
357 /* SYSTEM: A data transfer is being initiated by the system. */
360 sim_hw_io_read_buffer (struct sim_state *sd,
367 STATE_HW (sd)->cpu = NULL;
368 return hw_io_read_buffer (hw, dest, space, addr, nr_bytes);
372 sim_hw_io_write_buffer (struct sim_state *sd,
379 STATE_HW (sd)->cpu = NULL;
380 return hw_io_write_buffer (hw, source, space, addr, nr_bytes);
385 /* Abort the simulation specifying HW as the reason */
388 hw_vabort (struct hw *me,
394 /* find an identity */
395 if (me != NULL && hw_path (me) != NULL && hw_path (me) [0] != '\0')
397 else if (me != NULL && hw_name (me) != NULL && hw_name (me)[0] != '\0')
399 else if (me != NULL && hw_family (me) != NULL && hw_family (me)[0] != '\0')
400 name = hw_family (me);
403 /* construct an updated format string */
404 msg = alloca (strlen (name) + strlen (": ") + strlen (fmt) + 1);
408 /* report the problem */
409 sim_engine_vabort (hw_system (me),
410 STATE_HW (hw_system (me))->cpu,
411 STATE_HW (hw_system (me))->cia,
416 hw_abort (struct hw *me,
421 /* report the problem */
423 hw_vabort (me, fmt, ap);
428 sim_hw_abort (struct sim_state *sd,
436 sim_engine_vabort (sd, NULL, NULL_CIA, fmt, ap);
438 hw_vabort (me, fmt, ap);
443 /* MISC routines to tie HW into the rest of the system */
446 hw_halt (struct hw *me,
450 struct sim_state *sd = hw_system (me);
451 struct sim_hw *sim = STATE_HW (sd);
452 sim_engine_halt (sd, sim->cpu, NULL, sim->cia, reason, status);
456 hw_system_cpu (struct hw *me)
458 return STATE_HW (hw_system (me))->cpu;
462 hw_trace (struct hw *me,
466 if (hw_trace_p (me)) /* to be sure, to be sure */
470 sim_io_eprintf (hw_system (me), "%s: ", hw_path (me));
471 sim_io_evprintf (hw_system (me), fmt, ap);
472 sim_io_eprintf (hw_system (me), "\n");
478 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin() */
481 do_hw_poll_read (struct hw *me,
482 do_hw_poll_read_method *read,
487 int status = read (hw_system (me), sim_io_fd, buf, sizeof_buf);
490 else if (status == 0 && sizeof_buf == 0)
492 else if (status == 0)
494 else /* status < 0 */
497 if (STATE_CALLBACK (hw_system (me))->last_errno == EAGAIN)
498 return HW_IO_NOT_READY;
508 /* The event queue abstraction (for devices) */
514 hw_event_handler *handler;
518 /* Pass the H/W event onto the real handler */
521 bounce_hw_event (SIM_DESC sd,
524 hw_event event = * (hw_event*) data;
526 /* if we are delivering an event, we don't have a CPU. */
527 STATE_HW (sd)->cpu = NULL;
528 event.handler (event.me, event.data);
532 /* Map onto the event functions */
535 hw_event_queue_schedule (struct hw *me,
537 hw_event_handler *handler,
540 hw_event *event = ZALLOC (hw_event);
542 event->handler = handler;
544 event->real = sim_events_schedule (hw_system (me),
552 hw_event_queue_deschedule (struct hw *me,
553 hw_event *event_to_remove)
555 sim_events_deschedule (hw_system (me),
556 event_to_remove->real);
557 zfree (event_to_remove);
561 hw_event_queue_time (struct hw *me)
563 return sim_events_time (hw_system (me));