* Makefile.in (SIM_OBJS): Add sim-load.o.
[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 void
150 sim_resume (sd, step, siggnal)
151      SIM_DESC sd;
152      int step, siggnal;
153 {
154   state->EndCondition = 0;
155
156   if (step)
157     {
158       state->Reg[15] = ARMul_DoInstr (state);
159       if (state->EndCondition == 0)
160         state->EndCondition = RDIError_BreakpointReached;
161     }
162   else
163     {
164       state->Reg[15] = ARMul_DoProg (state);
165     }
166
167   FLUSHPIPE;
168 }
169
170 SIM_RC
171 sim_create_inferior (sd, argv, env)
172      SIM_DESC sd;
173      char **argv;
174      char **env;
175 {
176   return SIM_RC_OK;
177 }
178
179 void
180 sim_info (sd, verbose)
181      SIM_DESC sd;
182      int verbose;
183 {
184 }
185
186
187 static int 
188 frommem (state, memory)
189      struct ARMul_State *state;
190      unsigned char *memory;
191 {
192   if (state->bigendSig == HIGH)
193     {
194       return (memory[0] << 24)
195         | (memory[1] << 16)
196         | (memory[2] << 8)
197         | (memory[3] << 0);
198     }
199   else
200     {
201       return (memory[3] << 24)
202         | (memory[2] << 16)
203         | (memory[1] << 8)
204         | (memory[0] << 0);
205     }
206 }
207
208
209 static void
210 tomem (state, memory,  val)
211      struct ARMul_State *state;
212      unsigned char *memory;
213      int val;
214 {
215   if (state->bigendSig == HIGH)
216     {
217       memory[0] = val >> 24;
218       memory[1] = val >> 16;
219       memory[2] = val >> 8;
220       memory[3] = val >> 0;
221     }
222   else
223     {
224       memory[3] = val >> 24;
225       memory[2] = val >> 16;
226       memory[1] = val >> 8;
227       memory[0] = val >> 0;
228     }
229 }
230
231 void
232 sim_store_register (sd, rn, memory)
233      SIM_DESC sd;
234      int rn;
235      unsigned char *memory;
236 {
237   init ();
238   ARMul_SetReg(state, state->Mode, rn, frommem (state, memory));
239 }
240
241 void
242 sim_fetch_register (sd, rn, memory)
243      SIM_DESC sd;
244      int rn;
245      unsigned char *memory;
246 {
247   init ();
248   tomem (state, memory, ARMul_GetReg(state, state->Mode, rn));
249 }
250
251
252
253
254 SIM_DESC
255 sim_open (kind, argv)
256      SIM_OPEN_KIND kind;
257      char **argv;
258 {
259   sim_kind = kind;
260   myname = argv[0];
261   return (SIM_DESC) 1;
262 }
263
264 void
265 sim_close (sd, quitting)
266      SIM_DESC sd;
267      int quitting;
268 {
269   /* nothing to do */
270 }
271
272 SIM_RC
273 sim_load (sd, prog, abfd, from_tty)
274      SIM_DESC sd;
275      char *prog;
276      bfd *abfd;
277      int from_tty;
278 {
279   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
280   bfd *prog_bfd;
281
282   prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
283                             sim_kind == SIM_OPEN_DEBUG);
284   if (prog_bfd == NULL)
285     return SIM_RC_FAIL;
286   ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
287   if (abfd == NULL)
288     bfd_close (prog_bfd);
289   return SIM_RC_OK;
290 }
291
292 void
293 sim_stop_reason (sd, reason, sigrc)
294      SIM_DESC sd;
295      enum sim_stop *reason;
296      int *sigrc;
297 {
298   if (state->EndCondition == 0)
299     {
300       *reason = sim_exited;
301       *sigrc = state->Reg[0] & 255;
302     }
303   else
304     {
305       *reason = sim_stopped;
306       if (state->EndCondition == RDIError_BreakpointReached)
307         *sigrc = SIGTRAP;
308       else
309         *sigrc = 0;
310     }
311 }
312
313 void
314 sim_kill (sd)
315      SIM_DESC sd;
316 {
317   /* nothing to do */
318 }
319
320 void
321 sim_do_command (sd, cmd)
322      SIM_DESC sd;
323      char *cmd;
324 {
325   (*sim_callback->printf_filtered) (sim_callback, "This simulator does not accept any commands.\n");
326 }
327
328
329 void
330 sim_set_callbacks (sd, ptr)
331      SIM_DESC sd;
332      host_callback *ptr;
333 {
334   sim_callback = ptr;
335 }