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