Commit | Line | Data |
---|---|---|
05ccbdfd JL |
1 | #include <signal.h> |
2 | #include "sysdep.h" | |
3 | #include "bfd.h" | |
4 | ||
5 | #include "mn10300_sim.h" | |
6 | ||
7 | #ifndef INLINE | |
8 | #ifdef __GNUC__ | |
9 | #define INLINE inline | |
10 | #else | |
11 | #define INLINE | |
12 | #endif | |
13 | #endif | |
14 | ||
15 | host_callback *mn10300_callback; | |
16 | int mn10300_debug; | |
17 | ||
18 | uint32 OP[4]; | |
19 | ||
20 | static struct hash_entry *lookup_hash PARAMS ((uint32 ins)); | |
21 | static long hash PARAMS ((long)); | |
22 | static void init_system PARAMS ((void)); | |
23 | ||
24 | #define MAX_HASH 63 | |
25 | ||
26 | struct hash_entry | |
27 | { | |
28 | struct hash_entry *next; | |
29 | long opcode; | |
30 | long mask; | |
31 | struct simops *ops; | |
32 | }; | |
33 | ||
34 | struct hash_entry hash_table[MAX_HASH+1]; | |
35 | ||
36 | ||
37 | static INLINE long | |
38 | hash(insn) | |
39 | long insn; | |
40 | { | |
41 | /* XXX */ | |
42 | } | |
43 | ||
44 | static struct hash_entry * | |
45 | lookup_hash (ins) | |
46 | uint32 ins; | |
47 | { | |
48 | struct hash_entry *h; | |
49 | ||
50 | h = &hash_table[hash(ins)]; | |
51 | ||
52 | while ((ins & h->mask) != h->opcode) | |
53 | { | |
54 | if (h->next == NULL) | |
55 | { | |
56 | (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR looking up hash for 0x%x, PC=0x%x\n", ins, PC); | |
57 | exit(1); | |
58 | } | |
59 | h = h->next; | |
60 | } | |
61 | return (h); | |
62 | } | |
63 | ||
64 | /* FIXME These would more efficient to use than load_mem/store_mem, | |
65 | but need to be changed to use the memory map. */ | |
66 | ||
67 | uint8 | |
68 | get_byte (x) | |
69 | uint8 *x; | |
70 | { | |
71 | return *x; | |
72 | } | |
73 | ||
74 | uint16 | |
75 | get_half (x) | |
76 | uint8 *x; | |
77 | { | |
78 | uint8 *a = x; | |
79 | return (a[1] << 8) + (a[0]); | |
80 | } | |
81 | ||
82 | uint32 | |
83 | get_word (x) | |
84 | uint8 *x; | |
85 | { | |
86 | uint8 *a = x; | |
87 | return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]); | |
88 | } | |
89 | ||
90 | void | |
91 | put_byte (addr, data) | |
92 | uint8 *addr; | |
93 | uint8 data; | |
94 | { | |
95 | uint8 *a = addr; | |
96 | a[0] = data; | |
97 | } | |
98 | ||
99 | void | |
100 | put_half (addr, data) | |
101 | uint8 *addr; | |
102 | uint16 data; | |
103 | { | |
104 | uint8 *a = addr; | |
105 | a[0] = data & 0xff; | |
106 | a[1] = (data >> 8) & 0xff; | |
107 | } | |
108 | ||
109 | void | |
110 | put_word (addr, data) | |
111 | uint8 *addr; | |
112 | uint32 data; | |
113 | { | |
114 | uint8 *a = addr; | |
115 | a[0] = data & 0xff; | |
116 | a[1] = (data >> 8) & 0xff; | |
117 | a[2] = (data >> 16) & 0xff; | |
118 | a[3] = (data >> 24) & 0xff; | |
119 | } | |
120 | ||
121 | ||
122 | uint32 | |
123 | load_mem (addr, len) | |
124 | SIM_ADDR addr; | |
125 | int len; | |
126 | { | |
127 | uint8 *p = addr + State.mem; | |
128 | ||
129 | switch (len) | |
130 | { | |
131 | case 1: | |
132 | return p[0]; | |
133 | case 2: | |
134 | return p[1] << 8 | p[0]; | |
135 | case 4: | |
136 | return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0]; | |
137 | default: | |
138 | abort (); | |
139 | } | |
140 | } | |
141 | ||
142 | void | |
143 | store_mem (addr, len, data) | |
144 | SIM_ADDR addr; | |
145 | int len; | |
146 | uint32 data; | |
147 | { | |
148 | uint8 *p = addr + State.mem; | |
149 | ||
150 | switch (len) | |
151 | { | |
152 | case 1: | |
153 | p[0] = data; | |
154 | return; | |
155 | case 2: | |
156 | p[0] = data; | |
157 | p[1] = data >> 8; | |
158 | return; | |
159 | case 4: | |
160 | p[0] = data; | |
161 | p[1] = data >> 8; | |
162 | p[2] = data >> 16; | |
163 | p[3] = data >> 24; | |
164 | return; | |
165 | default: | |
166 | abort (); | |
167 | } | |
168 | } | |
169 | ||
170 | void | |
171 | sim_size (power) | |
172 | int power; | |
173 | ||
174 | { | |
175 | if (State.mem) | |
176 | free (State.mem); | |
177 | ||
178 | State.mem = (uint8 *) calloc (1, 1 << power); | |
179 | if (!State.mem) | |
180 | { | |
181 | (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n"); | |
182 | exit (1); | |
183 | } | |
184 | } | |
185 | ||
186 | static void | |
187 | init_system () | |
188 | { | |
189 | if (!State.mem) | |
190 | sim_size(18); | |
191 | } | |
192 | ||
193 | int | |
194 | sim_write (addr, buffer, size) | |
195 | SIM_ADDR addr; | |
196 | unsigned char *buffer; | |
197 | int size; | |
198 | { | |
199 | int i; | |
200 | ||
201 | init_system (); | |
202 | ||
203 | for (i = 0; i < size; i++) | |
204 | store_mem (addr + i, 1, buffer[i]); | |
205 | ||
206 | return size; | |
207 | } | |
208 | ||
209 | void | |
210 | sim_open (args) | |
211 | char *args; | |
212 | { | |
213 | struct simops *s; | |
214 | struct hash_entry *h; | |
215 | if (args != NULL) | |
216 | { | |
217 | #ifdef DEBUG | |
218 | if (strcmp (args, "-t") == 0) | |
219 | mn10300_debug = DEBUG; | |
220 | else | |
221 | #endif | |
222 | (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",args); | |
223 | } | |
224 | ||
225 | /* put all the opcodes in the hash table */ | |
226 | for (s = Simops; s->func; s++) | |
227 | { | |
228 | h = &hash_table[hash(s->opcode)]; | |
229 | ||
230 | /* go to the last entry in the chain */ | |
231 | while (h->next) | |
232 | h = h->next; | |
233 | ||
234 | if (h->ops) | |
235 | { | |
236 | h->next = calloc(1,sizeof(struct hash_entry)); | |
237 | h = h->next; | |
238 | } | |
239 | h->ops = s; | |
240 | h->mask = s->mask; | |
241 | h->opcode = s->opcode; | |
242 | } | |
243 | } | |
244 | ||
245 | ||
246 | void | |
247 | sim_close (quitting) | |
248 | int quitting; | |
249 | { | |
250 | /* nothing to do */ | |
251 | } | |
252 | ||
253 | void | |
254 | sim_set_profile (n) | |
255 | int n; | |
256 | { | |
257 | (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n); | |
258 | } | |
259 | ||
260 | void | |
261 | sim_set_profile_size (n) | |
262 | int n; | |
263 | { | |
264 | (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n); | |
265 | } | |
266 | ||
267 | void | |
268 | sim_resume (step, siggnal) | |
269 | int step, siggnal; | |
270 | { | |
271 | uint32 inst, opcode; | |
272 | reg_t oldpc; | |
273 | struct interrupt_generator *intgen; | |
274 | ||
275 | if (step) | |
276 | State.exception = SIGTRAP; | |
277 | else | |
278 | State.exception = 0; | |
279 | ||
280 | do | |
281 | { | |
282 | /* Fetch the current instruction. */ | |
283 | inst = RLW (PC); | |
284 | oldpc = PC; | |
285 | opcode = (inst & 0x07e0) >> 5; | |
286 | ||
287 | /* Decode the opcode field. */ | |
288 | if ((opcode & 0x30) == 0 | |
289 | || (opcode & 0x38) == 0x10) | |
290 | { | |
291 | } | |
292 | } | |
293 | while (!State.exception); | |
294 | } | |
295 | ||
296 | int | |
297 | sim_trace () | |
298 | { | |
299 | #ifdef DEBUG | |
300 | mn10300_debug = DEBUG; | |
301 | #endif | |
302 | sim_resume (0, 0); | |
303 | return 1; | |
304 | } | |
305 | ||
306 | void | |
307 | sim_info (verbose) | |
308 | int verbose; | |
309 | { | |
310 | (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n"); | |
311 | } | |
312 | ||
313 | void | |
314 | sim_create_inferior (start_address, argv, env) | |
315 | SIM_ADDR start_address; | |
316 | char **argv; | |
317 | char **env; | |
318 | { | |
319 | PC = start_address; | |
320 | } | |
321 | ||
322 | void | |
323 | sim_kill () | |
324 | { | |
325 | /* nothing to do */ | |
326 | } | |
327 | ||
328 | void | |
329 | sim_set_callbacks (p) | |
330 | host_callback *p; | |
331 | { | |
332 | mn10300_callback = p; | |
333 | } | |
334 | ||
335 | /* All the code for exiting, signals, etc needs to be revamped. | |
336 | ||
337 | This is enough to get c-torture limping though. */ | |
338 | ||
339 | void | |
340 | sim_stop_reason (reason, sigrc) | |
341 | enum sim_stop *reason; | |
342 | int *sigrc; | |
343 | { | |
344 | *reason = sim_stopped; | |
345 | if (State.exception == SIGQUIT) | |
346 | *sigrc = 0; | |
347 | else | |
348 | *sigrc = State.exception; | |
349 | } | |
350 | ||
351 | void | |
352 | sim_fetch_register (rn, memory) | |
353 | int rn; | |
354 | unsigned char *memory; | |
355 | { | |
356 | put_word (memory, State.regs[rn]); | |
357 | } | |
358 | ||
359 | void | |
360 | sim_store_register (rn, memory) | |
361 | int rn; | |
362 | unsigned char *memory; | |
363 | { | |
364 | State.regs[rn] = get_word (memory); | |
365 | } | |
366 | ||
367 | int | |
368 | sim_read (addr, buffer, size) | |
369 | SIM_ADDR addr; | |
370 | unsigned char *buffer; | |
371 | int size; | |
372 | { | |
373 | int i; | |
374 | for (i = 0; i < size; i++) | |
375 | buffer[i] = load_mem (addr + i, 1); | |
376 | ||
377 | return size; | |
378 | } | |
379 | ||
380 | void | |
381 | sim_do_command (cmd) | |
382 | char *cmd; | |
383 | { | |
384 | (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd); | |
385 | } | |
386 | ||
387 | int | |
388 | sim_load (prog, from_tty) | |
389 | char *prog; | |
390 | int from_tty; | |
391 | { | |
392 | /* Return nonzero so GDB will handle it. */ | |
393 | return 1; | |
394 | } |