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