import gdb-2000-02-04 snapshot
[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 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 ARMul_Debug (ARMul_State * state, ARMword pc, ARMword instr)
110 {
111
112 }
113
114 int
115 sim_write (sd, addr, buffer, size)
116      SIM_DESC sd;
117      SIM_ADDR addr;
118      unsigned char *buffer;
119      int size;
120 {
121   int i;
122   init ();
123   for (i = 0; i < size; i++)
124     {
125       ARMul_WriteByte (state, addr + i, buffer[i]);
126     }
127   return size;
128 }
129
130 int
131 sim_read (sd, addr, buffer, size)
132      SIM_DESC sd;
133      SIM_ADDR addr;
134      unsigned char *buffer;
135      int size;
136 {
137   int i;
138   init ();
139   for (i = 0; i < size; i++)
140     {
141       buffer[i] = ARMul_ReadByte (state, addr + i);
142     }
143   return size;
144 }
145
146 int
147 sim_trace (sd)
148      SIM_DESC sd;
149 {  
150   (*sim_callback->printf_filtered) (sim_callback,
151                                     "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 =
255                 strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0);
256             }
257           env++;
258         }
259     }
260
261   return SIM_RC_OK;
262 }
263
264 void
265 sim_info (sd, verbose)
266      SIM_DESC sd;
267      int verbose;
268 {
269 }
270
271
272 static int
273 frommem (state, memory)
274      struct ARMul_State *state;
275      unsigned char *memory;
276 {
277   if (state->bigendSig == HIGH)
278     {
279       return (memory[0] << 24)
280         | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0);
281     }
282   else
283     {
284       return (memory[3] << 24)
285         | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0);
286     }
287 }
288
289
290 static void
291 tomem (state, memory, val)
292      struct ARMul_State *state;
293      unsigned char *memory;
294      int val;
295 {
296   if (state->bigendSig == HIGH)
297     {
298       memory[0] = val >> 24;
299       memory[1] = val >> 16;
300       memory[2] = val >> 8;
301       memory[3] = val >> 0;
302     }
303   else
304     {
305       memory[3] = val >> 24;
306       memory[2] = val >> 16;
307       memory[1] = val >> 8;
308       memory[0] = val >> 0;
309     }
310 }
311
312 int
313 sim_store_register (sd, rn, memory, length)
314      SIM_DESC sd;
315      int rn;
316      unsigned char *memory;
317      int length;
318 {
319   init ();
320   ARMul_SetReg (state, state->Mode, rn, frommem (state, memory));
321   return -1;
322 }
323
324 int
325 sim_fetch_register (sd, rn, memory, length)
326      SIM_DESC sd;
327      int rn;
328      unsigned char *memory;
329      int length;
330 {
331   ARMword regval;
332
333   init ();
334   if (rn < 16)
335     regval = ARMul_GetReg (state, state->Mode, rn);
336   else if (rn == 25)            /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */
337     regval = ARMul_GetCPSR (state);
338   else
339     regval = 0;                 /* FIXME: should report an error */
340   tomem (state, memory, regval);
341   return -1;
342 }
343
344 SIM_DESC
345 sim_open (kind, ptr, abfd, argv)
346      SIM_OPEN_KIND kind;
347      host_callback *ptr;
348      struct _bfd *abfd;
349      char **argv;
350 {
351   sim_kind = kind;
352   myname = argv[0];
353   sim_callback = ptr;
354
355   /* Decide upon the endian-ness of the processor.
356      If we can, get the information from the bfd itself.
357      Otherwise look to see if we have been given a command
358      line switch that tells us.  Otherwise default to little endian.  */
359   if (abfd != NULL)
360     big_endian = bfd_big_endian (abfd);
361   else if (argv[1] != NULL)
362     {
363       int i;
364
365       /* Scan for endian-ness switch.  */
366       for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++)
367         if (argv[i][0] == '-' && argv[i][1] == 'E')
368           {
369             char c;
370
371             if ((c = argv[i][2]) == 0)
372               {
373                 ++i;
374                 c = argv[i][0];
375               }
376
377             switch (c)
378               {
379               case 0:
380                 sim_callback->printf_filtered
381                   (sim_callback, "No argument to -E option provided\n");
382                 break;
383
384               case 'b':
385               case 'B':
386                 big_endian = 1;
387                 break;
388
389               case 'l':
390               case 'L':
391                 big_endian = 0;
392                 break;
393
394               default:
395                 sim_callback->printf_filtered
396                   (sim_callback, "Unrecognised argument to -E option\n");
397                 break;
398               }
399           }
400     }
401
402   return (SIM_DESC) 1;
403 }
404
405 void
406 sim_close (sd, quitting)
407      SIM_DESC sd;
408      int quitting;
409 {
410   /* nothing to do */
411 }
412
413 SIM_RC
414 sim_load (sd, prog, abfd, from_tty)
415      SIM_DESC sd;
416      char *prog;
417      bfd *abfd;
418      int from_tty;
419 {
420   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
421   bfd *prog_bfd;
422
423   prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
424                             sim_kind == SIM_OPEN_DEBUG, 0, sim_write);
425   if (prog_bfd == NULL)
426     return SIM_RC_FAIL;
427   ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
428   if (abfd == NULL)
429     bfd_close (prog_bfd);
430   return SIM_RC_OK;
431 }
432
433 void
434 sim_stop_reason (sd, reason, sigrc)
435      SIM_DESC sd;
436      enum sim_stop *reason;
437      int *sigrc;
438 {
439   if (stop_simulator)
440     {
441       *reason = sim_stopped;
442       *sigrc = SIGINT;
443     }
444   else if (state->EndCondition == 0)
445     {
446       *reason = sim_exited;
447       *sigrc = state->Reg[0] & 255;
448     }
449   else
450     {
451       *reason = sim_stopped;
452       if (state->EndCondition == RDIError_BreakpointReached)
453         *sigrc = SIGTRAP;
454       else
455         *sigrc = 0;
456     }
457 }
458
459 void
460 sim_do_command (sd, cmd)
461      SIM_DESC sd;
462      char *cmd;
463 {  
464   (*sim_callback->printf_filtered) (sim_callback,
465                                     "This simulator does not accept any commands.\n");
466 }
467
468
469 void
470 sim_set_callbacks (ptr)
471      host_callback *ptr;
472 {
473   sim_callback = ptr;
474 }