]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/arm/wrapper.c
import gdb-19990422 snapshot
[thirdparty/binutils-gdb.git] / sim / arm / wrapper.c
1 /* run front end support for arm
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of ARM SIM.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* This file provides the interface between the simulator and run.c and gdb
21 (when the simulator is linked with gdb).
22 All simulator interaction should go through this file. */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <bfd.h>
27 #include <signal.h>
28 #include "callback.h"
29 #include "remote-sim.h"
30 #include "armdefs.h"
31 #include "armemu.h"
32 #include "dbg_rdi.h"
33
34 host_callback *sim_callback;
35
36 static struct ARMul_State *state;
37
38 /* Who is using the simulator. */
39 static SIM_OPEN_KIND sim_kind;
40
41 /* argv[0] */
42 static char *myname;
43
44 /* Memory size in bytes. */
45 static int mem_size = (1 << 21);
46
47 /* Non-zero to display start up banner, and maybe other things. */
48 static int verbosity;
49
50 /* Non-zero to set big endian mode. */
51 static int big_endian;
52
53 int stop_simulator;
54
55 static void
56 init ()
57 {
58 static int done;
59
60 if (!done)
61 {
62 ARMul_EmulateInit();
63 state = ARMul_NewState ();
64 state->bigendSig = (big_endian ? HIGH : LOW);
65 ARMul_MemoryInit(state, mem_size);
66 ARMul_OSInit(state);
67 ARMul_CoProInit(state);
68 state->verbose = verbosity;
69 done = 1;
70 }
71 }
72
73 /* Set verbosity level of simulator.
74 This is not intended to produce detailed tracing or debugging information.
75 Just summaries. */
76 /* FIXME: common/run.c doesn't do this yet. */
77
78 void
79 sim_set_verbose (v)
80 int v;
81 {
82 verbosity = v;
83 }
84
85 /* Set the memory size to SIZE bytes.
86 Must be called before initializing simulator. */
87 /* FIXME: Rename to sim_set_mem_size. */
88
89 void
90 sim_size (size)
91 int size;
92 {
93 mem_size = size;
94 }
95
96 void
97 ARMul_ConsolePrint (ARMul_State * state, const char *format,...)
98 {
99 va_list ap;
100
101 if (state->verbose)
102 {
103 va_start (ap, format);
104 vprintf (format, ap);
105 va_end (ap);
106 }
107 }
108
109 ARMword
110 ARMul_Debug (ARMul_State * state, ARMword pc, ARMword instr)
111 {
112
113 }
114
115 int
116 sim_write (sd, addr, buffer, size)
117 SIM_DESC sd;
118 SIM_ADDR addr;
119 unsigned char *buffer;
120 int size;
121 {
122 int i;
123 init ();
124 for (i = 0; i < size; i++)
125 {
126 ARMul_WriteByte (state, addr+i, buffer[i]);
127 }
128 return size;
129 }
130
131 int
132 sim_read (sd, addr, buffer, size)
133 SIM_DESC sd;
134 SIM_ADDR addr;
135 unsigned char *buffer;
136 int size;
137 {
138 int i;
139 init ();
140 for (i = 0; i < size; i++)
141 {
142 buffer[i] = ARMul_ReadByte (state, addr + i);
143 }
144 return size;
145 }
146
147 int
148 sim_trace (sd)
149 SIM_DESC sd;
150 {
151 (*sim_callback->printf_filtered) (sim_callback, "This simulator does not support tracing\n");
152 return 1;
153 }
154
155 int
156 sim_stop (sd)
157 SIM_DESC sd;
158 {
159 state->Emulate = STOP;
160 stop_simulator = 1;
161 return 1;
162 }
163
164 void
165 sim_resume (sd, step, siggnal)
166 SIM_DESC sd;
167 int step, siggnal;
168 {
169 state->EndCondition = 0;
170 stop_simulator = 0;
171
172 if (step)
173 {
174 state->Reg[15] = ARMul_DoInstr (state);
175 if (state->EndCondition == 0)
176 state->EndCondition = RDIError_BreakpointReached;
177 }
178 else
179 {
180 #if 1 /* JGS */
181 state->NextInstr = RESUME; /* treat as PC change */
182 #endif
183 state->Reg[15] = ARMul_DoProg (state);
184 }
185
186 FLUSHPIPE;
187 }
188
189 SIM_RC
190 sim_create_inferior (sd, abfd, argv, env)
191 SIM_DESC sd;
192 struct _bfd *abfd;
193 char **argv;
194 char **env;
195 {
196 int argvlen=0;
197 char **arg;
198
199 if (abfd != NULL)
200 ARMul_SetPC (state, bfd_get_start_address (abfd));
201 else
202 ARMul_SetPC (state, 0); /* ??? */
203
204 #if 1 /* JGS */
205 /* We explicitly select a processor capable of supporting the ARM
206 32bit mode, and then we force the simulated CPU into the 32bit
207 User mode: */
208 ARMul_SelectProcessor(state, ARM600);
209 ARMul_SetCPSR(state, USER32MODE);
210 #endif
211
212 if (argv != NULL)
213 {
214 /*
215 ** Set up the command line (by laboriously stringing together the
216 ** environment carefully picked apart by our caller...)
217 */
218 /* Free any old stuff */
219 if (state->CommandLine != NULL)
220 {
221 free(state->CommandLine);
222 state->CommandLine = NULL;
223 }
224
225 /* See how much we need */
226 for (arg = argv; *arg != NULL; arg++)
227 argvlen += strlen(*arg)+1;
228
229 /* allocate it... */
230 state->CommandLine = malloc(argvlen+1);
231 if (state->CommandLine != NULL)
232 {
233 arg = argv;
234 state->CommandLine[0]='\0';
235 for (arg = argv; *arg != NULL; arg++)
236 {
237 strcat(state->CommandLine, *arg);
238 strcat(state->CommandLine, " ");
239 }
240 }
241 }
242
243 if (env != NULL)
244 {
245 /* Now see if there's a MEMSIZE spec in the environment */
246 while (*env)
247 {
248 if (strncmp(*env, "MEMSIZE=", sizeof("MEMSIZE=")-1)==0)
249 {
250 unsigned long top_of_memory;
251 char *end_of_num;
252
253 /* Set up memory limit */
254 state->MemSize = strtoul(*env + sizeof("MEMSIZE=")-1, &end_of_num, 0);
255 }
256 env++;
257 }
258 }
259
260 return SIM_RC_OK;
261 }
262
263 void
264 sim_info (sd, verbose)
265 SIM_DESC sd;
266 int verbose;
267 {
268 }
269
270
271 static int
272 frommem (state, memory)
273 struct ARMul_State *state;
274 unsigned char *memory;
275 {
276 if (state->bigendSig == HIGH)
277 {
278 return (memory[0] << 24)
279 | (memory[1] << 16)
280 | (memory[2] << 8)
281 | (memory[3] << 0);
282 }
283 else
284 {
285 return (memory[3] << 24)
286 | (memory[2] << 16)
287 | (memory[1] << 8)
288 | (memory[0] << 0);
289 }
290 }
291
292
293 static void
294 tomem (state, memory, val)
295 struct ARMul_State *state;
296 unsigned char *memory;
297 int val;
298 {
299 if (state->bigendSig == HIGH)
300 {
301 memory[0] = val >> 24;
302 memory[1] = val >> 16;
303 memory[2] = val >> 8;
304 memory[3] = val >> 0;
305 }
306 else
307 {
308 memory[3] = val >> 24;
309 memory[2] = val >> 16;
310 memory[1] = val >> 8;
311 memory[0] = val >> 0;
312 }
313 }
314
315 int
316 sim_store_register (sd, rn, memory, length)
317 SIM_DESC sd;
318 int rn;
319 unsigned char *memory;
320 int length;
321 {
322 init ();
323 ARMul_SetReg(state, state->Mode, rn, frommem (state, memory));
324 return -1;
325 }
326
327 int
328 sim_fetch_register (sd, rn, memory, length)
329 SIM_DESC sd;
330 int rn;
331 unsigned char *memory;
332 int length;
333 {
334 ARMword regval;
335
336 init ();
337 if (rn < 16)
338 regval = ARMul_GetReg(state, state->Mode, rn);
339 else if (rn == 25) /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */
340 regval = ARMul_GetCPSR(state);
341 else
342 regval = 0; /* FIXME: should report an error */
343 tomem (state, memory, regval);
344 return -1;
345 }
346
347 SIM_DESC
348 sim_open (kind, ptr, abfd, argv)
349 SIM_OPEN_KIND kind;
350 host_callback *ptr;
351 struct _bfd *abfd;
352 char **argv;
353 {
354 sim_kind = kind;
355 myname = argv[0];
356 sim_callback = ptr;
357
358 /* Decide upon the endian-ness of the processor.
359 If we can, get the information from the bfd itself.
360 Otherwise look to see if we have been given a command
361 line switch that tells us. Otherwise default to little endian. */
362 if (abfd != NULL)
363 big_endian = bfd_big_endian (abfd);
364 else if (argv[1] != NULL)
365 {
366 int i;
367
368 /* Scan for endian-ness switch. */
369 for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++)
370 if (argv[i][0] == '-' && argv[i][1] == 'E')
371 {
372 char c;
373
374 if ((c = argv[i][2]) == 0)
375 {
376 ++i;
377 c = argv[i][0];
378 }
379
380 switch (c)
381 {
382 case 0:
383 sim_callback->printf_filtered
384 (sim_callback, "No argument to -E option provided\n");
385 break;
386
387 case 'b':
388 case 'B':
389 big_endian = 1;
390 break;
391
392 case 'l':
393 case 'L':
394 big_endian = 0;
395 break;
396
397 default:
398 sim_callback->printf_filtered
399 (sim_callback, "Unrecognised argument to -E option\n");
400 break;
401 }
402 }
403 }
404
405 return (SIM_DESC) 1;
406 }
407
408 void
409 sim_close (sd, quitting)
410 SIM_DESC sd;
411 int quitting;
412 {
413 /* nothing to do */
414 }
415
416 SIM_RC
417 sim_load (sd, prog, abfd, from_tty)
418 SIM_DESC sd;
419 char *prog;
420 bfd *abfd;
421 int from_tty;
422 {
423 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
424 bfd *prog_bfd;
425
426 prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
427 sim_kind == SIM_OPEN_DEBUG,
428 0, sim_write);
429 if (prog_bfd == NULL)
430 return SIM_RC_FAIL;
431 ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
432 if (abfd == NULL)
433 bfd_close (prog_bfd);
434 return SIM_RC_OK;
435 }
436
437 void
438 sim_stop_reason (sd, reason, sigrc)
439 SIM_DESC sd;
440 enum sim_stop *reason;
441 int *sigrc;
442 {
443 if (stop_simulator)
444 {
445 *reason = sim_stopped;
446 *sigrc = SIGINT;
447 }
448 else if (state->EndCondition == 0)
449 {
450 *reason = sim_exited;
451 *sigrc = state->Reg[0] & 255;
452 }
453 else
454 {
455 *reason = sim_stopped;
456 if (state->EndCondition == RDIError_BreakpointReached)
457 *sigrc = SIGTRAP;
458 else
459 *sigrc = 0;
460 }
461 }
462
463 void
464 sim_do_command (sd, cmd)
465 SIM_DESC sd;
466 char *cmd;
467 {
468 (*sim_callback->printf_filtered) (sim_callback, "This simulator does not accept any commands.\n");
469 }
470
471
472 void
473 sim_set_callbacks (ptr)
474 host_callback *ptr;
475 {
476 sim_callback = ptr;
477 }