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.
22 #include <signal.h> /* FIXME - should be machine dependant version */
29 #undef printf_filtered /* blow away the mapping */
46 #include "remote-sim.h"
49 /* Structures used by the simulator, for gdb just have static structures */
51 static psim
*simulator
;
52 static device
*root_device
;
53 static const char *register_names
[] = REGISTER_NAMES
;
54 static host_callback
*callbacks
;
57 sim_open (SIM_OPEN_KIND kind
,
58 host_callback
*callback
,
64 /* Note: The simulation is not created by sim_open() because
65 complete information is not yet available */
67 TRACE(trace_gdb
, ("sim_open called\n"));
69 if (root_device
!= NULL
)
70 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
71 root_device
= psim_tree();
74 psim_options(root_device
, argv
+ 1);
76 if (ppc_trace
[trace_opts
])
79 /* fudge our descriptor for now */
85 sim_close (SIM_DESC sd
, int quitting
)
87 TRACE(trace_gdb
, ("sim_close(quitting=%d) called\n", quitting
));
88 if (ppc_trace
[trace_print_info
] && simulator
!= NULL
)
89 psim_print_info (simulator
, ppc_trace
[trace_print_info
]);
94 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
97 TRACE(trace_gdb
, ("sim_load(prog=%s, from_tty=%d) called\n",
101 /* parse the arguments, assume that the file is argument 0 */
102 argv
= buildargv(prog
);
103 ASSERT(argv
!= NULL
&& argv
[0] != NULL
);
105 /* create the simulator */
106 TRACE(trace_gdb
, ("sim_load() - first time, create the simulator\n"));
107 simulator
= psim_create(argv
[0], root_device
);
109 /* bring in all the data section */
110 psim_init(simulator
);
112 /* get the start address */
115 abfd
= bfd_openr (argv
[0], 0);
117 error ("psim: can't open \"%s\": %s\n",
118 argv
[0], bfd_errmsg (bfd_get_error ()));
119 if (!bfd_check_format (abfd
, bfd_object
))
121 const char *errmsg
= bfd_errmsg (bfd_get_error ());
123 error ("psim: \"%s\" is not an object file: %s\n",
129 /* release the arguments */
137 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
139 int result
= psim_read_memory(simulator
, MAX_NR_PROCESSORS
,
141 TRACE(trace_gdb
, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
142 (long)mem
, (long)buf
, length
, result
));
148 sim_write (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
150 int result
= psim_write_memory(simulator
, MAX_NR_PROCESSORS
,
153 TRACE(trace_gdb
, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
154 (long)mem
, (long)buf
, length
, result
));
160 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
)
162 if (simulator
== NULL
) {
165 TRACE(trace_gdb
, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
166 regno
, register_names
[regno
], (long)buf
));
167 psim_read_register(simulator
, MAX_NR_PROCESSORS
,
168 buf
, register_names
[regno
],
174 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
)
176 if (simulator
== NULL
)
178 TRACE(trace_gdb
, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
179 regno
, register_names
[regno
], (long)buf
));
180 psim_write_register(simulator
, MAX_NR_PROCESSORS
,
181 buf
, register_names
[regno
],
187 sim_info (SIM_DESC sd
, int verbose
)
189 TRACE(trace_gdb
, ("sim_info(verbose=%d) called\n", verbose
));
190 psim_print_info (simulator
, verbose
);
195 sim_create_inferior (SIM_DESC sd
,
200 unsigned_word entry_point
;
201 TRACE(trace_gdb
, ("sim_create_inferior(start_address=0x%x, ...)\n",
204 if (simulator
== NULL
)
205 error ("No program loaded");
208 entry_point
= bfd_get_start_address (abfd
);
210 entry_point
= 0xfff00000; /* ??? */
212 psim_init(simulator
);
213 psim_stack(simulator
, argv
, envp
);
215 psim_write_register(simulator
, -1 /* all start at same PC */,
216 &entry_point
, "pc", cooked_transfer
);
222 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
224 psim_status status
= psim_get_status(simulator
);
226 switch (status
.reason
) {
228 *reason
= sim_stopped
;
229 if (status
.signal
== 0)
232 *sigrc
= status
.signal
;
235 *reason
= sim_stopped
;
239 *reason
= sim_exited
;
240 *sigrc
= status
.signal
;
243 *reason
= sim_signalled
;
244 *sigrc
= status
.signal
;
248 TRACE(trace_gdb
, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
249 (long)reason
, (long)*reason
, (long)sigrc
, (long)*sigrc
));
254 /* Run (or resume) the program. */
257 sim_stop (SIM_DESC sd
)
259 psim_stop (simulator
);
264 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
266 TRACE(trace_gdb
, ("sim_resume(step=%d, siggnal=%d)\n",
271 psim_step (simulator
);
275 psim_run (simulator
);
280 sim_do_command (SIM_DESC sd
, char *cmd
)
282 TRACE(trace_gdb
, ("sim_do_commands(cmd=%s) called\n",
283 cmd
? cmd
: "(null)"));
285 char **argv
= buildargv(cmd
);
286 psim_command(root_device
, argv
);
292 /* Map simulator IO operations onto the corresponding GDB I/O
295 NB: Only a limited subset of operations are mapped across. More
296 advanced operations (such as dup or write) must either be mapped to
297 one of the below calls or handled internally */
300 sim_io_read_stdin(char *buf
,
303 switch (CURRENT_STDIO
) {
305 return callbacks
->read_stdin(callbacks
, buf
, sizeof_buf
);
308 return callbacks
->read(callbacks
, 0, buf
, sizeof_buf
);
311 error("sim_io_read_stdin: unaccounted switch\n");
318 sim_io_write_stdout(const char *buf
,
321 switch (CURRENT_STDIO
) {
323 return callbacks
->write_stdout(callbacks
, buf
, sizeof_buf
);
326 return callbacks
->write(callbacks
, 1, buf
, sizeof_buf
);
329 error("sim_io_write_stdout: unaccounted switch\n");
336 sim_io_write_stderr(const char *buf
,
339 switch (CURRENT_STDIO
) {
341 /* NB: I think there should be an explicit write_stderr callback */
342 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
345 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
348 error("sim_io_write_stderr: unaccounted switch\n");
356 sim_io_printf_filtered(const char *fmt
,
361 /* format the message */
363 vsprintf(message
, fmt
, ap
);
366 if (strlen(message
) >= sizeof(message
))
367 error("sim_io_printf_filtered: buffer overflow\n");
368 callbacks
->printf_filtered(callbacks
, "%s", message
);
372 sim_io_flush_stdoutput(void)
374 switch (CURRENT_STDIO
) {
376 callbacks
->flush_stdout (callbacks
);
381 error("sim_io_read_stdin: unaccounted switch\n");
391 void *memory
= (void*)xmalloc(size
);
393 error("xmalloc failed\n");
394 memset(memory
, 0, size
);
398 void zfree(void *data
)