This commit was generated by cvs2svn to track changes on a CVS vendor
[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
118 sim_write (sd, addr, buffer, size)
119      SIM_DESC sd ATTRIBUTE_UNUSED;
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     {
128       ARMul_WriteByte (state, addr + i, buffer[i]);
129     }
130   return size;
131 }
132
133 int
134 sim_read (sd, addr, buffer, size)
135      SIM_DESC sd ATTRIBUTE_UNUSED;
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
149 int
150 sim_trace (sd)
151      SIM_DESC sd ATTRIBUTE_UNUSED;
152 {  
153   (*sim_callback->printf_filtered) (sim_callback,
154                                     "This simulator does not support tracing\n");
155   return 1;
156 }
157
158 int
159 sim_stop (sd)
160      SIM_DESC sd ATTRIBUTE_UNUSED;
161 {
162   state->Emulate = STOP;
163   stop_simulator = 1;
164   return 1;
165 }
166
167 void
168 sim_resume (sd, step, siggnal)
169      SIM_DESC sd ATTRIBUTE_UNUSED;
170      int step;
171      int siggnal ATTRIBUTE_UNUSED;
172 {
173   state->EndCondition = 0;
174   stop_simulator = 0;
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     {
184 #if 1                           /* JGS */
185       state->NextInstr = RESUME;        /* treat as PC change */
186 #endif
187       state->Reg[15] = ARMul_DoProg (state);
188     }
189
190   FLUSHPIPE;
191 }
192
193 SIM_RC
194 sim_create_inferior (sd, abfd, argv, env)
195      SIM_DESC sd ATTRIBUTE_UNUSED;
196      struct _bfd *abfd;
197      char **argv;
198      char **env;
199 {
200   int argvlen = 0;
201   int mach;
202   char **arg;
203
204   if (abfd != NULL)
205     ARMul_SetPC (state, bfd_get_start_address (abfd));
206   else
207     ARMul_SetPC (state, 0);     /* ??? */
208
209   mach = bfd_get_mach (abfd);
210
211   switch (mach) {
212   default:
213     (*sim_callback->printf_filtered) (sim_callback,
214                                       "Unknown machine type; please update sim_create_inferior.\n");
215     /* fall through */
216
217   case 0: /* arm */
218     /* We wouldn't set the machine type with earlier toolchains, so we
219        explicitly select a processor capable of supporting all ARM
220        32bit mode. */
221     /* fall through */
222
223   case 5: /* armv4 */
224   case 6: /* armv4t */
225   case 7: /* armv5 */
226   case 8: /* armv5t */
227     ARMul_SelectProcessor (state, STRONGARM);
228     break;
229
230   case 3: /* armv3 */
231   case 4: /* armv3m */
232     ARMul_SelectProcessor (state, ARM600);
233     break;
234     
235   case 1: /* armv2 */
236   case 2: /* armv2a */
237     ARMul_SelectProcessor (state, ARM2);
238     break;
239   }
240     
241   if (argv != NULL)
242     {
243       /*
244          ** Set up the command line (by laboriously stringing together the
245          ** environment carefully picked apart by our caller...)
246        */
247       /* Free any old stuff */
248       if (state->CommandLine != NULL)
249         {
250           free (state->CommandLine);
251           state->CommandLine = NULL;
252         }
253
254       /* See how much we need */
255       for (arg = argv; *arg != NULL; arg++)
256         argvlen += strlen (*arg) + 1;
257
258       /* allocate it... */
259       state->CommandLine = malloc (argvlen + 1);
260       if (state->CommandLine != NULL)
261         {
262           arg = argv;
263           state->CommandLine[0] = '\0';
264           for (arg = argv; *arg != NULL; arg++)
265             {
266               strcat (state->CommandLine, *arg);
267               strcat (state->CommandLine, " ");
268             }
269         }
270     }
271
272   if (env != NULL)
273     {
274       /* Now see if there's a MEMSIZE spec in the environment */
275       while (*env)
276         {
277           if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0)
278             {
279               char *end_of_num;
280
281               /* Set up memory limit */
282               state->MemSize =
283                 strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0);
284             }
285           env++;
286         }
287     }
288
289   return SIM_RC_OK;
290 }
291
292 void
293 sim_info (sd, verbose)
294      SIM_DESC sd ATTRIBUTE_UNUSED;
295      int verbose ATTRIBUTE_UNUSED;
296 {
297 }
298
299
300 static int
301 frommem (state, memory)
302      struct ARMul_State *state;
303      unsigned char *memory;
304 {
305   if (state->bigendSig == HIGH)
306     {
307       return (memory[0] << 24)
308         | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0);
309     }
310   else
311     {
312       return (memory[3] << 24)
313         | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0);
314     }
315 }
316
317
318 static void
319 tomem (state, memory, val)
320      struct ARMul_State *state;
321      unsigned char *memory;
322      int val;
323 {
324   if (state->bigendSig == HIGH)
325     {
326       memory[0] = val >> 24;
327       memory[1] = val >> 16;
328       memory[2] = val >> 8;
329       memory[3] = val >> 0;
330     }
331   else
332     {
333       memory[3] = val >> 24;
334       memory[2] = val >> 16;
335       memory[1] = val >> 8;
336       memory[0] = val >> 0;
337     }
338 }
339
340 int
341 sim_store_register (sd, rn, memory, length)
342      SIM_DESC sd ATTRIBUTE_UNUSED;
343      int rn;
344      unsigned char *memory;
345      int length ATTRIBUTE_UNUSED;
346 {
347   init ();
348   if (rn == 25)
349     {
350       state->Cpsr = frommem (state, memory);
351       ARMul_CPSRAltered (state);             
352     }
353   else
354     ARMul_SetReg (state, state->Mode, rn, frommem (state, memory));
355   return -1;
356 }
357
358 int
359 sim_fetch_register (sd, rn, memory, length)
360      SIM_DESC sd ATTRIBUTE_UNUSED;
361      int rn;
362      unsigned char *memory;
363      int length ATTRIBUTE_UNUSED;
364 {
365   ARMword regval;
366
367   init ();
368   if (rn < 16)
369     regval = ARMul_GetReg (state, state->Mode, rn);
370   else if (rn == 25)            /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */
371     regval = ARMul_GetCPSR (state);
372   else
373     regval = 0;                 /* FIXME: should report an error */
374   tomem (state, memory, regval);
375   return -1;
376 }
377
378 SIM_DESC
379 sim_open (kind, ptr, abfd, argv)
380      SIM_OPEN_KIND kind;
381      host_callback *ptr;
382      struct _bfd *abfd;
383      char **argv;
384 {
385   sim_kind = kind;
386   if (myname) free (myname);
387   myname = (char *) xstrdup (argv[0]);
388   sim_callback = ptr;
389
390   /* Decide upon the endian-ness of the processor.
391      If we can, get the information from the bfd itself.
392      Otherwise look to see if we have been given a command
393      line switch that tells us.  Otherwise default to little endian.  */
394   if (abfd != NULL)
395     big_endian = bfd_big_endian (abfd);
396   else if (argv[1] != NULL)
397     {
398       int i;
399
400       /* Scan for endian-ness switch.  */
401       for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++)
402         if (argv[i][0] == '-' && argv[i][1] == 'E')
403           {
404             char c;
405
406             if ((c = argv[i][2]) == 0)
407               {
408                 ++i;
409                 c = argv[i][0];
410               }
411
412             switch (c)
413               {
414               case 0:
415                 sim_callback->printf_filtered
416                   (sim_callback, "No argument to -E option provided\n");
417                 break;
418
419               case 'b':
420               case 'B':
421                 big_endian = 1;
422                 break;
423
424               case 'l':
425               case 'L':
426                 big_endian = 0;
427                 break;
428
429               default:
430                 sim_callback->printf_filtered
431                   (sim_callback, "Unrecognised argument to -E option\n");
432                 break;
433               }
434           }
435     }
436
437   return (SIM_DESC) 1;
438 }
439
440 void
441 sim_close (sd, quitting)
442      SIM_DESC sd ATTRIBUTE_UNUSED;
443      int quitting ATTRIBUTE_UNUSED;
444 {
445   if (myname) free (myname);
446   myname = NULL;
447 }
448
449 SIM_RC
450 sim_load (sd, prog, abfd, from_tty)
451      SIM_DESC sd;
452      char *prog;
453      bfd *abfd;
454      int from_tty ATTRIBUTE_UNUSED;
455 {
456   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
457   bfd *prog_bfd;
458
459   prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
460                             sim_kind == SIM_OPEN_DEBUG, 0, sim_write);
461   if (prog_bfd == NULL)
462     return SIM_RC_FAIL;
463   ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
464   if (abfd == NULL)
465     bfd_close (prog_bfd);
466   return SIM_RC_OK;
467 }
468
469 void
470 sim_stop_reason (sd, reason, sigrc)
471      SIM_DESC sd ATTRIBUTE_UNUSED;
472      enum sim_stop *reason;
473      int *sigrc;
474 {
475   if (stop_simulator)
476     {
477       *reason = sim_stopped;
478       *sigrc = SIGINT;
479     }
480   else if (state->EndCondition == 0)
481     {
482       *reason = sim_exited;
483       *sigrc = state->Reg[0] & 255;
484     }
485   else
486     {
487       *reason = sim_stopped;
488       if (state->EndCondition == RDIError_BreakpointReached)
489         *sigrc = SIGTRAP;
490       else
491         *sigrc = 0;
492     }
493 }
494
495 void
496 sim_do_command (sd, cmd)
497      SIM_DESC sd ATTRIBUTE_UNUSED;
498      char *cmd ATTRIBUTE_UNUSED;
499 {  
500   (*sim_callback->printf_filtered) (sim_callback,
501                                     "This simulator does not accept any commands.\n");
502 }
503
504
505 void
506 sim_set_callbacks (ptr)
507      host_callback *ptr;
508 {
509   sim_callback = ptr;
510 }