Add set_sim_callbacks new needed by gdb.
[binutils-gdb.git] / sim / ppc / sim_calls.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
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.
9
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.
14
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.
18
19 */
20
21
22 #include <signal.h> /* FIXME - should be machine dependant version */
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include "basics.h"
29 #include "psim.h"
30
31 #include "../../gdb/defs.h"
32
33 #include "devices.h"
34
35 #include "../../gdb/remote-sim.h"
36 #include "../../gdb/callback.h"
37
38
39 /* Structures used by the simulator, for gdb just have static structures */
40
41 static psim *simulator;
42 static int nr_cpus;
43 static char *register_names[] = REGISTER_NAMES;
44 static int print_info = 0;
45
46 void
47 sim_open (char *args)
48 {
49 int i;
50
51 /* trace the call */
52 TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
53
54 if (args) {
55 char *buf = (char *)alloca (strlen (args) + 1);
56 char *p;
57 strcpy (buf, args);
58
59 p = strtok (args, " \t");
60 while (p != (char *)0) {
61 if (*p != '-')
62 error ("Argument is not an option '%s'", p);
63
64 else {
65 /* check arguments -- note, main.c also contains argument processing
66 code for the standalone emulator. */
67 while (*++p != '\0') {
68 switch (*p) {
69 default:
70 error ("Usage: target sim [ -a -p -c -C -s -i -I -t ]\n");
71 break;
72 case 'a':
73 for (i = 0; i < nr_trace; i++)
74 trace[i] = 1;
75 break;
76 case 'p':
77 trace[trace_cpu] = trace[trace_semantics] = 1;
78 break;
79 case 'c':
80 trace[trace_core] = 1;
81 break;
82 case 'C':
83 trace[trace_console_device] = 1;
84 break;
85 case 's':
86 trace[trace_create_stack] = 1;
87 break;
88 case 'i':
89 trace[trace_icu_device] = 1;
90 break;
91 case 'I':
92 print_info = 1;
93 break;
94 case 't':
95 trace[trace_device_tree] = 1;
96 break;
97 }
98 }
99 }
100
101 p = strtok ((char *)0, " \t");
102 }
103 }
104
105 /* do something */
106 TRACE(trace_tbd, ("sim_open() - TBD - should parse the arguments\n"));
107 TRACE(trace_tbd, ("sim_open() - TBD - can not create simulator here as do not have description of it\n"));
108 }
109
110
111 void
112 sim_close (int quitting)
113 {
114 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
115 if (print_info)
116 psim_print_info (simulator, 1);
117
118 /* nothing to do */
119 }
120
121
122 int
123 sim_load (char *prog, int from_tty)
124 {
125 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
126 prog, from_tty));
127
128 /* sanity check */
129 if (prog == NULL) {
130 error ("sim_load() - TBD - read stan shebs e-mail about how to find the program name?\n");
131 return -1;
132 }
133 TRACE(trace_tbd, ("sim_load() - TBD - parse that prog stripping things like quotes\n"));
134
135 /* create the simulator */
136 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
137 nr_cpus = (WITH_SMP ? WITH_SMP : 1);
138 simulator = psim_create(prog, nr_cpus);
139
140 /* bring in all the data section */
141 psim_load(simulator);
142
143 return 0;
144 }
145
146
147 void
148 sim_kill (void)
149 {
150 TRACE(trace_gdb, ("sim_kill(void) called\n"));
151 /* do nothing, nothing to do */
152 }
153
154
155 int
156 sim_read (SIM_ADDR mem, unsigned char *buf, int length)
157 {
158 return psim_read_memory(simulator, nr_cpus, buf, mem, length,
159 raw_transfer);
160 }
161
162
163 int
164 sim_write (SIM_ADDR mem, unsigned char *buf, int length)
165 {
166 return psim_write_memory(simulator, nr_cpus, buf, mem, length,
167 raw_transfer, 1/*violate_ro*/);
168 }
169
170
171 void
172 sim_fetch_register (int regno, unsigned char *buf)
173 {
174 if (simulator == NULL) {
175 return;
176 }
177
178 psim_read_register(simulator, nr_cpus, buf, register_names[regno],
179 raw_transfer);
180 }
181
182
183 void
184 sim_store_register (int regno, unsigned char *buf)
185 {
186 if (simulator == NULL)
187 return;
188
189 psim_write_register(simulator, nr_cpus, buf, register_names[regno],
190 raw_transfer);
191 }
192
193
194 void
195 sim_info (int verbose)
196 {
197 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
198 psim_print_info (simulator, verbose);
199 }
200
201
202 void
203 sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
204 {
205 unsigned_word entry_point = start_address;
206
207 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
208 start_address));
209
210 psim_load(simulator);
211 psim_stack(simulator, argv, envp);
212
213 psim_write_register(simulator, -1 /* all start at same PC */,
214 &entry_point, "pc", cooked_transfer);
215 }
216
217
218 static volatile int sim_should_run;
219
220 void
221 sim_stop_reason (enum sim_stop *reason, int *sigrc)
222 {
223 psim_status status = psim_get_status(simulator);
224
225 switch (CURRENT_ENVIRONMENT) {
226
227 case VIRTUAL_ENVIRONMENT:
228 switch (status.reason) {
229 case was_continuing:
230 *reason = sim_stopped;
231 *sigrc = SIGTRAP;
232 if (sim_should_run) {
233 error("sim_stop_reason() unknown reason for halt\n");
234 }
235 break;
236 case was_trap:
237 *reason = sim_stopped;
238 *sigrc = SIGTRAP;
239 break;
240 case was_exited:
241 *reason = sim_exited;
242 *sigrc = 0;
243 break;
244 case was_signalled:
245 *reason = sim_signalled;
246 *sigrc = status.signal;
247 break;
248 }
249 break;
250
251 case OPERATING_ENVIRONMENT:
252 *reason = sim_stopped;
253 *sigrc = SIGTRAP;
254 break;
255
256 default:
257 error("sim_stop_reason() - unknown environment\n");
258
259 }
260 }
261
262
263
264 /* Run (or resume) the program. */
265 static void
266 sim_ctrl_c()
267 {
268 sim_should_run = 0;
269 }
270
271 void
272 sim_resume (int step, int siggnal)
273 {
274 void (*prev) ();
275 unsigned_word program_counter;
276
277 prev = signal(SIGINT, sim_ctrl_c);
278 sim_should_run = 1;
279
280 if (step)
281 psim_step(simulator);
282 else
283 psim_run_until_stop(simulator, &sim_should_run);
284
285 signal(SIGINT, prev);
286 }
287
288 void
289 sim_do_command(char *cmd)
290 {
291 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", cmd));
292 }
293
294 void
295 sim_set_callbacks (host_callback *callback)
296 {
297 TRACE(trace_gdb, ("sim_set_callbacks called\n"));
298 }
299
300 /****/
301
302 void *
303 zalloc(long size)
304 {
305 void *memory = (void*)xmalloc(size);
306 if (memory == NULL)
307 error("xmalloc failed\n");
308 bzero(memory, size);
309 return memory;
310 }
311
312 void zfree(void *data)
313 {
314 mfree(NULL, data);
315 }