Add support for ARM's v5TE architecture and Intel's XScale extenstions
[external/binutils.git] / sim / arm / wrapper.c
1 /* run front end support for arm
2    Copyright (C) 1995, 1996, 1997, 2000 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 <string.h>
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"
34 #include "ansidecl.h"
35
36 host_callback *sim_callback;
37
38 static struct ARMul_State *state;
39
40 /* Who is using the simulator.  */
41 static SIM_OPEN_KIND sim_kind;
42
43 /* argv[0] */
44 static char *myname;
45
46 /* Memory size in bytes.  */
47 static int mem_size = (1 << 21);
48
49 /* Non-zero to display start up banner, and maybe other things.  */
50 static int verbosity;
51
52 /* Non-zero to set big endian mode.  */
53 static int big_endian;
54
55 int stop_simulator;
56
57 static void
58 init ()
59 {
60   static int done;
61
62   if (!done)
63     {
64       ARMul_EmulateInit ();
65       state = ARMul_NewState ();
66       state->bigendSig = (big_endian ? HIGH : LOW);
67       ARMul_MemoryInit (state, mem_size);
68       ARMul_OSInit (state);
69       ARMul_CoProInit (state);
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
80 void
81 sim_set_verbose (v)
82      int v;
83 {
84   verbosity = v;
85 }
86
87 /* Set the memory size to SIZE bytes.
88    Must be called before initializing simulator.  */
89 /* FIXME: Rename to sim_set_mem_size.  */
90
91 void
92 sim_size (size)
93      int size;
94 {
95   mem_size = size;
96 }
97
98 void
99 ARMul_ConsolePrint (ARMul_State * state, const char *format, ...)
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
111 ARMword
112 ARMul_Debug (ARMul_State * state ATTRIBUTE_UNUSED, ARMword pc ATTRIBUTE_UNUSED, ARMword instr ATTRIBUTE_UNUSED)
113 {
114   return 0;
115 }
116
117 int SWI_vector_installed = FALSE;
118
119 int
120 sim_write (sd, addr, buffer, size)
121      SIM_DESC sd ATTRIBUTE_UNUSED;
122      SIM_ADDR addr;
123      unsigned char *buffer;
124      int size;
125 {
126   int i;
127
128   init ();
129
130   if ((addr <= 0x8) && ((addr + size) >= 0x8))
131     SWI_vector_installed = TRUE;
132
133   for (i = 0; i < size; i++)
134     ARMul_WriteByte (state, addr + i, buffer[i]);
135
136   return size;
137 }
138
139 int
140 sim_read (sd, addr, buffer, size)
141      SIM_DESC sd ATTRIBUTE_UNUSED;
142      SIM_ADDR addr;
143      unsigned char *buffer;
144      int size;
145 {
146   int i;
147   init ();
148   for (i = 0; i < size; i++)
149     {
150       buffer[i] = ARMul_ReadByte (state, addr + i);
151     }
152   return size;
153 }
154
155 int
156 sim_trace (sd)
157      SIM_DESC sd ATTRIBUTE_UNUSED;
158 {  
159   (*sim_callback->printf_filtered) (sim_callback,
160                                     "This simulator does not support tracing\n");
161   return 1;
162 }
163
164 int
165 sim_stop (sd)
166      SIM_DESC sd ATTRIBUTE_UNUSED;
167 {
168   state->Emulate = STOP;
169   stop_simulator = 1;
170   return 1;
171 }
172
173 void
174 sim_resume (sd, step, siggnal)
175      SIM_DESC sd ATTRIBUTE_UNUSED;
176      int step;
177      int siggnal ATTRIBUTE_UNUSED;
178 {
179   state->EndCondition = 0;
180   stop_simulator = 0;
181
182   if (step)
183     {
184       state->Reg[15] = ARMul_DoInstr (state);
185       if (state->EndCondition == 0)
186         state->EndCondition = RDIError_BreakpointReached;
187     }
188   else
189     {
190 #if 1                           /* JGS */
191       state->NextInstr = RESUME;        /* treat as PC change */
192 #endif
193       state->Reg[15] = ARMul_DoProg (state);
194     }
195
196   FLUSHPIPE;
197 }
198
199 SIM_RC
200 sim_create_inferior (sd, abfd, argv, env)
201      SIM_DESC sd ATTRIBUTE_UNUSED;
202      struct _bfd *abfd;
203      char **argv;
204      char **env;
205 {
206   int argvlen = 0;
207   int mach;
208   char **arg;
209
210   if (abfd != NULL)
211     ARMul_SetPC (state, bfd_get_start_address (abfd));
212   else
213     ARMul_SetPC (state, 0);     /* ??? */
214
215   mach = bfd_get_mach (abfd);
216
217   switch (mach)
218     {
219     default:
220       (*sim_callback->printf_filtered) (sim_callback,
221                                         "Unknown machine type; please update sim_create_inferior.\n");
222       /* fall through */
223
224     case 0:
225       /* We wouldn't set the machine type with earlier toolchains, so we
226          explicitly select a processor capable of supporting all ARMs in
227          32bit mode.  */
228     case bfd_mach_arm_5:
229     case bfd_mach_arm_5T:
230       ARMul_SelectProcessor (state, ARM_v5_Prop);
231       break;
232
233     case bfd_mach_arm_5TE:
234       ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop);
235       break;
236
237     case bfd_mach_arm_XScale:
238       ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_XScale_Prop);
239       break;
240
241     case bfd_mach_arm_4:
242     case bfd_mach_arm_4T:
243       ARMul_SelectProcessor (state, ARM_v4_Prop);
244       break;
245
246     case bfd_mach_arm_3:
247     case bfd_mach_arm_3M:
248       ARMul_SelectProcessor (state, ARM_Lock_Prop);
249       break;
250
251     case bfd_mach_arm_2:
252     case bfd_mach_arm_2a:
253       ARMul_SelectProcessor (state, ARM_Fix26_Prop);
254       break;
255     }
256
257   if (mach > 3)
258     {
259       /* Reset mode to ARM.  A gdb user may rerun a program that had entered
260          THUMB mode from the start and cause the ARM-mode startup code to be
261          executed in THUMB mode. */
262       ARMul_SetCPSR (state, USER32MODE);
263     }
264   
265   if (argv != NULL)
266     {
267       /*
268          ** Set up the command line (by laboriously stringing together the
269          ** environment carefully picked apart by our caller...)
270        */
271       /* Free any old stuff */
272       if (state->CommandLine != NULL)
273         {
274           free (state->CommandLine);
275           state->CommandLine = NULL;
276         }
277
278       /* See how much we need */
279       for (arg = argv; *arg != NULL; arg++)
280         argvlen += strlen (*arg) + 1;
281
282       /* allocate it... */
283       state->CommandLine = malloc (argvlen + 1);
284       if (state->CommandLine != NULL)
285         {
286           arg = argv;
287           state->CommandLine[0] = '\0';
288           for (arg = argv; *arg != NULL; arg++)
289             {
290               strcat (state->CommandLine, *arg);
291               strcat (state->CommandLine, " ");
292             }
293         }
294     }
295
296   if (env != NULL)
297     {
298       /* Now see if there's a MEMSIZE spec in the environment */
299       while (*env)
300         {
301           if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0)
302             {
303               char *end_of_num;
304
305               /* Set up memory limit */
306               state->MemSize =
307                 strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0);
308             }
309           env++;
310         }
311     }
312
313   return SIM_RC_OK;
314 }
315
316 void
317 sim_info (sd, verbose)
318      SIM_DESC sd ATTRIBUTE_UNUSED;
319      int verbose ATTRIBUTE_UNUSED;
320 {
321 }
322
323
324 static int
325 frommem (state, memory)
326      struct ARMul_State *state;
327      unsigned char *memory;
328 {
329   if (state->bigendSig == HIGH)
330     {
331       return (memory[0] << 24)
332         | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0);
333     }
334   else
335     {
336       return (memory[3] << 24)
337         | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0);
338     }
339 }
340
341
342 static void
343 tomem (state, memory, val)
344      struct ARMul_State *state;
345      unsigned char *memory;
346      int val;
347 {
348   if (state->bigendSig == HIGH)
349     {
350       memory[0] = val >> 24;
351       memory[1] = val >> 16;
352       memory[2] = val >> 8;
353       memory[3] = val >> 0;
354     }
355   else
356     {
357       memory[3] = val >> 24;
358       memory[2] = val >> 16;
359       memory[1] = val >> 8;
360       memory[0] = val >> 0;
361     }
362 }
363
364 int
365 sim_store_register (sd, rn, memory, length)
366      SIM_DESC sd ATTRIBUTE_UNUSED;
367      int rn;
368      unsigned char *memory;
369      int length ATTRIBUTE_UNUSED;
370 {
371   init ();
372
373   if (rn == 25)
374     {
375       state->Cpsr = frommem (state, memory);
376       ARMul_CPSRAltered (state);             
377     }
378   else
379     ARMul_SetReg (state, state->Mode, rn, frommem (state, memory));
380   return -1;
381 }
382
383 int
384 sim_fetch_register (sd, rn, memory, length)
385      SIM_DESC sd ATTRIBUTE_UNUSED;
386      int rn;
387      unsigned char *memory;
388      int length ATTRIBUTE_UNUSED;
389 {
390   ARMword regval;
391
392   init ();
393
394   if (rn < 16)
395     regval = ARMul_GetReg (state, state->Mode, rn);
396   else if (rn == 25)            /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */
397     regval = ARMul_GetCPSR (state);
398   else
399     regval = 0;                 /* FIXME: should report an error */
400   tomem (state, memory, regval);
401   return -1;
402 }
403
404 SIM_DESC
405 sim_open (kind, ptr, abfd, argv)
406      SIM_OPEN_KIND kind;
407      host_callback *ptr;
408      struct _bfd *abfd;
409      char **argv;
410 {
411   sim_kind = kind;
412   if (myname) free (myname);
413   myname = (char *) xstrdup (argv[0]);
414   sim_callback = ptr;
415
416   /* Decide upon the endian-ness of the processor.
417      If we can, get the information from the bfd itself.
418      Otherwise look to see if we have been given a command
419      line switch that tells us.  Otherwise default to little endian.  */
420   if (abfd != NULL)
421     big_endian = bfd_big_endian (abfd);
422   else if (argv[1] != NULL)
423     {
424       int i;
425
426       /* Scan for endian-ness switch.  */
427       for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++)
428         if (argv[i][0] == '-' && argv[i][1] == 'E')
429           {
430             char c;
431
432             if ((c = argv[i][2]) == 0)
433               {
434                 ++i;
435                 c = argv[i][0];
436               }
437
438             switch (c)
439               {
440               case 0:
441                 sim_callback->printf_filtered
442                   (sim_callback, "No argument to -E option provided\n");
443                 break;
444
445               case 'b':
446               case 'B':
447                 big_endian = 1;
448                 break;
449
450               case 'l':
451               case 'L':
452                 big_endian = 0;
453                 break;
454
455               default:
456                 sim_callback->printf_filtered
457                   (sim_callback, "Unrecognised argument to -E option\n");
458                 break;
459               }
460           }
461     }
462
463   return (SIM_DESC) 1;
464 }
465
466 void
467 sim_close (sd, quitting)
468      SIM_DESC sd ATTRIBUTE_UNUSED;
469      int quitting ATTRIBUTE_UNUSED;
470 {
471   if (myname) free (myname);
472   myname = NULL;
473 }
474
475 SIM_RC
476 sim_load (sd, prog, abfd, from_tty)
477      SIM_DESC sd;
478      char *prog;
479      bfd *abfd;
480      int from_tty ATTRIBUTE_UNUSED;
481 {
482   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
483   bfd *prog_bfd;
484
485   prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
486                             sim_kind == SIM_OPEN_DEBUG, 0, sim_write);
487   if (prog_bfd == NULL)
488     return SIM_RC_FAIL;
489   ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
490   if (abfd == NULL)
491     bfd_close (prog_bfd);
492   return SIM_RC_OK;
493 }
494
495 void
496 sim_stop_reason (sd, reason, sigrc)
497      SIM_DESC sd ATTRIBUTE_UNUSED;
498      enum sim_stop *reason;
499      int *sigrc;
500 {
501   if (stop_simulator)
502     {
503       *reason = sim_stopped;
504       *sigrc = SIGINT;
505     }
506   else if (state->EndCondition == 0)
507     {
508       *reason = sim_exited;
509       *sigrc = state->Reg[0] & 255;
510     }
511   else
512     {
513       *reason = sim_stopped;
514       if (state->EndCondition == RDIError_BreakpointReached)
515         *sigrc = SIGTRAP;
516       else
517         *sigrc = 0;
518     }
519 }
520
521 void
522 sim_do_command (sd, cmd)
523      SIM_DESC sd ATTRIBUTE_UNUSED;
524      char *cmd ATTRIBUTE_UNUSED;
525 {  
526   (*sim_callback->printf_filtered) (sim_callback,
527                                     "This simulator does not accept any commands.\n");
528 }
529
530
531 void
532 sim_set_callbacks (ptr)
533      host_callback *ptr;
534 {
535   sim_callback = ptr;
536 }