Fix whitespace snafu in tc-riscv.c
[binutils-gdb.git] / sim / ppc / sim_calls.c
blob13e39a6e07b53f30fcad747705f569f39793edc1
1 /* This file is part of the program psim.
3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
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/>.
21 #include <signal.h> /* FIXME - should be machine dependant version */
22 #include <stdarg.h>
23 #include <ctype.h>
25 #include "psim.h"
26 #include "options.h"
28 #undef printf_filtered /* blow away the mapping */
30 #include <stdlib.h>
31 #include <string.h>
33 #include "ansidecl.h"
34 #include "libiberty.h"
35 #include "bfd.h"
36 #include "sim/callback.h"
37 #include "sim/sim.h"
38 #include "gdb/signals.h"
40 /* Define the rate at which the simulator should poll the host
41 for a quit. */
42 #ifndef POLL_QUIT_INTERVAL
43 #define POLL_QUIT_INTERVAL 0x20
44 #endif
46 static int poll_quit_count = POLL_QUIT_INTERVAL;
48 /* Structures used by the simulator, for gdb just have static structures */
50 psim *simulator;
51 static device *root_device;
52 static host_callback *callbacks;
54 SIM_DESC
55 sim_open (SIM_OPEN_KIND kind,
56 host_callback *callback,
57 struct bfd *abfd,
58 char * const *argv)
60 callbacks = callback;
62 /* Note: The simulation is not created by sim_open() because
63 complete information is not yet available */
64 /* trace the call */
65 TRACE(trace_gdb, ("sim_open called\n"));
67 if (root_device != NULL)
68 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
69 root_device = psim_tree();
70 simulator = NULL;
72 if (psim_options (root_device, argv + 1, kind) == NULL)
73 return NULL;
75 if (ppc_trace[trace_opts])
76 print_options ();
78 /* fudge our descriptor for now */
79 return (SIM_DESC) 1;
83 void
84 sim_close (SIM_DESC sd, int quitting)
86 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
87 if (ppc_trace[trace_print_info] && simulator != NULL)
88 psim_print_info (simulator, ppc_trace[trace_print_info]);
92 SIM_RC
93 sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
95 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
96 prog, from_tty));
97 ASSERT(prog != NULL);
99 /* create the simulator */
100 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
101 simulator = psim_create(prog, root_device);
103 /* bring in all the data section */
104 psim_init(simulator);
106 /* get the start address */
107 if (abfd == NULL)
109 abfd = bfd_openr (prog, 0);
110 if (abfd == NULL)
111 error ("psim: can't open \"%s\": %s\n",
112 prog, bfd_errmsg (bfd_get_error ()));
113 if (!bfd_check_format (abfd, bfd_object))
115 const char *errmsg = bfd_errmsg (bfd_get_error ());
116 bfd_close (abfd);
117 error ("psim: \"%s\" is not an object file: %s\n",
118 prog, errmsg);
120 bfd_close (abfd);
123 return SIM_RC_OK;
127 uint64_t
128 sim_read (SIM_DESC sd, uint64_t mem, void *buf, uint64_t length)
130 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
131 buf, mem, length);
132 TRACE(trace_gdb,
133 ("sim_read(mem=0x%" PRIx64 ", buf=%p, length=%" PRIx64 ") = %d\n",
134 mem, buf, length, result));
135 return result;
139 uint64_t
140 sim_write (SIM_DESC sd, uint64_t mem, const void *buf, uint64_t length)
142 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
143 buf, mem, length,
144 1/*violate_ro*/);
145 TRACE(trace_gdb,
146 ("sim_write(mem=0x%" PRIx64 ", buf=%p, length=%" PRIx64 ") = %d\n",
147 mem, buf, length, result));
148 return result;
151 void
152 sim_info (SIM_DESC sd, bool verbose)
154 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
155 psim_print_info (simulator, verbose);
159 SIM_RC
160 sim_create_inferior (SIM_DESC sd,
161 struct bfd *abfd,
162 char * const *argv,
163 char * const *envp)
165 unsigned_word entry_point;
167 if (simulator == NULL)
168 error ("No program loaded");
170 if (abfd != NULL)
171 entry_point = bfd_get_start_address (abfd);
172 else
173 entry_point = 0xfff00000; /* ??? */
175 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
176 entry_point));
178 psim_init(simulator);
179 psim_stack(simulator, argv, envp);
181 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
182 &entry_point, "pc", cooked_transfer) > 0);
183 return SIM_RC_OK;
187 void
188 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
190 psim_status status = psim_get_status(simulator);
192 switch (status.reason) {
193 case was_continuing:
194 *reason = sim_stopped;
195 if (status.signal == 0)
196 *sigrc = GDB_SIGNAL_TRAP;
197 else
198 *sigrc = status.signal;
199 break;
200 case was_trap:
201 *reason = sim_stopped;
202 *sigrc = GDB_SIGNAL_TRAP;
203 break;
204 case was_exited:
205 *reason = sim_exited;
206 *sigrc = status.signal;
207 break;
208 case was_signalled:
209 *reason = sim_signalled;
210 *sigrc = status.signal;
211 break;
214 TRACE(trace_gdb, ("sim_stop_reason(reason=%p(%ld), sigrc=%p(%ld))\n",
215 reason, (long)*reason, sigrc, (long)*sigrc));
220 /* Run (or resume) the program. */
223 sim_stop (SIM_DESC sd)
225 psim_stop (simulator);
226 return 1;
229 void
230 sim_resume (SIM_DESC sd, int step, int siggnal)
232 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
233 step, siggnal));
235 if (step)
237 psim_step (simulator);
239 else
241 psim_run (simulator);
245 void
246 sim_do_command (SIM_DESC sd, const char *cmd)
248 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
249 cmd ? cmd : "(null)"));
250 if (cmd != NULL) {
251 char **argv = buildargv(cmd);
252 psim_command(root_device, argv);
253 freeargv(argv);
257 char **
258 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
260 return NULL;
263 char *
264 sim_memory_map (SIM_DESC sd)
266 return NULL;
269 /* Polling, if required */
271 void
272 sim_io_poll_quit (void)
274 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
276 poll_quit_count = POLL_QUIT_INTERVAL;
277 if (callbacks->poll_quit (callbacks))
278 psim_stop (simulator);
284 /* Map simulator IO operations onto the corresponding GDB I/O
285 functions.
287 NB: Only a limited subset of operations are mapped across. More
288 advanced operations (such as dup or write) must either be mapped to
289 one of the below calls or handled internally */
292 sim_io_read_stdin(char *buf,
293 int sizeof_buf)
295 switch (CURRENT_STDIO) {
296 case DO_USE_STDIO:
297 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
298 break;
299 case DONT_USE_STDIO:
300 return callbacks->read(callbacks, 0, buf, sizeof_buf);
301 break;
302 default:
303 error("sim_io_read_stdin: unaccounted switch\n");
304 break;
306 return 0;
310 sim_io_write_stdout(const char *buf,
311 int sizeof_buf)
313 switch (CURRENT_STDIO) {
314 case DO_USE_STDIO:
315 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
316 break;
317 case DONT_USE_STDIO:
318 return callbacks->write(callbacks, 1, buf, sizeof_buf);
319 break;
320 default:
321 error("sim_io_write_stdout: unaccounted switch\n");
322 break;
324 return 0;
328 sim_io_write_stderr(const char *buf,
329 int sizeof_buf)
331 switch (CURRENT_STDIO) {
332 case DO_USE_STDIO:
333 /* NB: I think there should be an explicit write_stderr callback */
334 return callbacks->write(callbacks, 3, buf, sizeof_buf);
335 break;
336 case DONT_USE_STDIO:
337 return callbacks->write(callbacks, 3, buf, sizeof_buf);
338 break;
339 default:
340 error("sim_io_write_stderr: unaccounted switch\n");
341 break;
343 return 0;
347 void
348 sim_io_printf_filtered(const char *fmt,
349 ...)
351 char message[1024];
352 va_list ap;
353 /* format the message */
354 va_start(ap, fmt);
355 vsprintf(message, fmt, ap);
356 va_end(ap);
357 /* sanity check */
358 if (strlen(message) >= sizeof(message))
359 error("sim_io_printf_filtered: buffer overflow\n");
360 callbacks->printf_filtered(callbacks, "%s", message);
363 void
364 sim_io_flush_stdoutput(void)
366 switch (CURRENT_STDIO) {
367 case DO_USE_STDIO:
368 callbacks->flush_stdout (callbacks);
369 break;
370 case DONT_USE_STDIO:
371 break;
372 default:
373 error("sim_io_read_stdin: unaccounted switch\n");
374 break;
378 /* Glue to use sim-fpu module. */
380 void
381 sim_io_error (SIM_DESC sd, const char *fmt, ...)
383 va_list ap;
384 va_start(ap, fmt);
385 callbacks->evprintf_filtered (callbacks, fmt, ap);
386 va_end(ap);
387 /* Printing a space here avoids empty printf compiler warnings. Not ideal,
388 but we want error's side-effect where it halts processing. */
389 callbacks->error (callbacks, " ");
392 /****/
394 void
395 error (const char *msg, ...)
397 va_list ap;
398 va_start(ap, msg);
399 callbacks->evprintf_filtered (callbacks, msg, ap);
400 va_end(ap);
401 /* Printing a space here avoids empty printf compiler warnings. Not ideal,
402 but we want error's side-effect where it halts processing. */
403 callbacks->error (callbacks, " ");
406 void *
407 zalloc(long size)
409 void *memory = (void*)xmalloc(size);
410 if (memory == NULL)
411 error("xmalloc failed\n");
412 memset(memory, 0, size);
413 return memory;