Cleanups to compile under FreeBSD
[external/binutils.git] / sim / arm / wrapper.c
1 /* run front end support for arm
2    Copyright (C) 1995, 1996 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 /* Memory size in bytes.  */
39 static int mem_size = (1 << 21);
40
41 /* Non-zero to display start up banner, and maybe other things.  */
42 static int verbosity;
43
44 static void 
45 init ()
46 {
47   static int done;
48
49   if (!done)
50     {
51       ARMul_EmulateInit();
52       state = ARMul_NewState ();
53       ARMul_MemoryInit(state, mem_size);
54       ARMul_OSInit(state);
55       ARMul_CoProInit(state); 
56       state->verbose = verbosity;
57       done = 1;
58     }
59 }
60
61 /* Set verbosity level of simulator.
62    This is not intended to produce detailed tracing or debugging information.
63    Just summaries.  */
64 /* FIXME: common/run.c doesn't do this yet.  */
65
66 void
67 sim_set_verbose (v)
68      int v;
69 {
70   verbosity = v;
71 }
72
73 /* Set the memory size to SIZE bytes.
74    Must be called before initializing simulator.  */   
75 /* FIXME: Rename to sim_set_mem_size.  */
76
77 void 
78 sim_size (size)
79      int size;
80 {
81   mem_size = size;
82 }
83
84 void 
85 ARMul_ConsolePrint (ARMul_State * state, const char *format,...)
86 {
87   va_list ap;
88
89   if (state->verbose)
90     {
91       va_start (ap, format);
92       vprintf (format, ap);
93       va_end (ap);
94     }
95 }
96
97 ARMword 
98 ARMul_Debug (ARMul_State * state, ARMword pc, ARMword instr)
99 {
100
101 }
102
103 int
104 sim_write (sd, addr, buffer, size)
105      SIM_DESC sd;
106      SIM_ADDR addr;
107      unsigned char *buffer;
108      int size;
109 {
110   int i;
111   init ();
112   for (i = 0; i < size; i++)
113     {
114       ARMul_WriteByte (state, addr+i, buffer[i]);
115     }
116   return size;
117 }
118
119 int
120 sim_read (sd, addr, buffer, size)
121      SIM_DESC sd;
122      SIM_ADDR addr;
123      unsigned char *buffer;
124      int size;
125 {
126   int i;
127   init ();
128   for (i = 0; i < size; i++)
129     {
130       buffer[i] = ARMul_ReadByte (state, addr + i);
131     }
132   return size;
133 }
134
135 int
136 sim_trace (sd)
137      SIM_DESC sd;
138 {
139   (*sim_callback->printf_filtered) (sim_callback, "This simulator does not support tracing\n");
140   return 1;
141 }
142
143 void
144 sim_resume (sd, step, siggnal)
145      SIM_DESC sd;
146      int step, siggnal;
147 {
148   state->EndCondition = 0;
149
150   if (step)
151     {
152       state->Reg[15] = ARMul_DoInstr (state);
153       if (state->EndCondition == 0)
154         state->EndCondition = RDIError_BreakpointReached;
155     }
156   else
157     {
158       state->Reg[15] = ARMul_DoProg (state);
159     }
160
161   FLUSHPIPE;
162 }
163
164 void
165 sim_create_inferior (sd, start_address, argv, env)
166      SIM_DESC sd;
167      SIM_ADDR start_address;
168      char **argv;
169      char **env;
170 {
171   ARMul_SetPC(state, start_address);
172 }
173
174 void
175 sim_info (sd, verbose)
176      SIM_DESC sd;
177      int verbose;
178 {
179 }
180
181
182 static int 
183 frommem (state, memory)
184      struct ARMul_State *state;
185      unsigned char *memory;
186 {
187   if (state->bigendSig == HIGH)
188     {
189       return (memory[0] << 24)
190         | (memory[1] << 16)
191         | (memory[2] << 8)
192         | (memory[3] << 0);
193     }
194   else
195     {
196       return (memory[3] << 24)
197         | (memory[2] << 16)
198         | (memory[1] << 8)
199         | (memory[0] << 0);
200     }
201 }
202
203
204 static void
205 tomem (state, memory,  val)
206      struct ARMul_State *state;
207      unsigned char *memory;
208      int val;
209 {
210   if (state->bigendSig == HIGH)
211     {
212       memory[0] = val >> 24;
213       memory[1] = val >> 16;
214       memory[2] = val >> 8;
215       memory[3] = val >> 0;
216     }
217   else
218     {
219       memory[3] = val >> 24;
220       memory[2] = val >> 16;
221       memory[1] = val >> 8;
222       memory[0] = val >> 0;
223     }
224 }
225
226 void
227 sim_store_register (sd, rn, memory)
228      SIM_DESC sd;
229      int rn;
230      unsigned char *memory;
231 {
232   init ();
233   ARMul_SetReg(state, state->Mode, rn, frommem (state, memory));
234 }
235
236 void
237 sim_fetch_register (sd, rn, memory)
238      SIM_DESC sd;
239      int rn;
240      unsigned char *memory;
241 {
242   init ();
243   tomem (state, memory, ARMul_GetReg(state, state->Mode, rn));
244 }
245
246
247
248
249 SIM_DESC
250 sim_open (kind, argv)
251      SIM_OPEN_KIND kind;
252      char **argv;
253 {
254   /*  (*sim_callback->error) (sim_callback, "testing 1 2 3\n");*/
255   /* nothing to do, fudge our descriptor */
256   return (SIM_DESC) 1;
257 }
258
259 void
260 sim_close (sd, quitting)
261      SIM_DESC sd;
262      int quitting;
263 {
264   /* nothing to do */
265 }
266
267 int
268 sim_load (sd, prog, from_tty)
269      SIM_DESC sd;
270      char *prog;
271      int from_tty;
272 {
273   /* Return nonzero so GDB will handle it.  */
274   return 1;
275 }
276
277 void
278 sim_stop_reason (sd, reason, sigrc)
279      SIM_DESC sd;
280      enum sim_stop *reason;
281      int *sigrc;
282 {
283   if (state->EndCondition == 0)
284     {
285       *reason = sim_exited;
286       *sigrc = state->Reg[0] & 255;
287     }
288   else
289     {
290       *reason = sim_stopped;
291       if (state->EndCondition == RDIError_BreakpointReached)
292         *sigrc = SIGTRAP;
293       else
294         *sigrc = 0;
295     }
296 }
297
298 void
299 sim_kill (sd)
300      SIM_DESC sd;
301 {
302   /* nothing to do */
303 }
304
305 void
306 sim_do_command (sd, cmd)
307      SIM_DESC sd;
308      char *cmd;
309 {
310   (*sim_callback->printf_filtered) (sim_callback, "This simulator does not accept any commands.\n");
311 }
312
313
314 void
315 sim_set_callbacks (sd, ptr)
316      SIM_DESC sd;
317      host_callback *ptr;
318 {
319   sim_callback = ptr;
320 }