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