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