Commit | Line | Data |
---|---|---|
22c1c7dd JL |
1 | #include <signal.h> |
2 | #include "sysdep.h" | |
3 | #include "bfd.h" | |
4 | #include "remote-sim.h" | |
5 | #include "callback.h" | |
6 | ||
7 | #include "v850_sim.h" | |
8 | ||
9909e232 JL |
9 | #ifndef INLINE |
10 | #ifdef __GNUC__ | |
11 | #define INLINE inline | |
12 | #else | |
13 | #define INLINE | |
14 | #endif | |
15 | #endif | |
16 | ||
28647e4c | 17 | #define MEM_SIZE 18 /* V850 memory size is 18 bits XXX */ |
22c1c7dd | 18 | |
d81352b8 | 19 | host_callback *v850_callback; |
ead4a3f1 | 20 | int v850_debug; |
d81352b8 JL |
21 | |
22 | ||
23 | uint32 OP[4]; | |
22c1c7dd JL |
24 | |
25 | static struct hash_entry *lookup_hash PARAMS ((uint32 ins)); | |
9909e232 JL |
26 | static long hash PARAMS ((long)); |
27 | static void do_format_1_2 PARAMS ((uint32)); | |
28 | static void do_format_3 PARAMS ((uint32)); | |
29 | static void do_format_4 PARAMS ((uint32)); | |
30 | static void do_format_5 PARAMS ((uint32)); | |
31 | static void do_format_6 PARAMS ((uint32)); | |
32 | static void do_format_7 PARAMS ((uint32)); | |
33 | static void do_format_8 PARAMS ((uint32)); | |
34 | static void do_format_9_10 PARAMS ((uint32)); | |
35 | static void init_system PARAMS ((void)); | |
22c1c7dd JL |
36 | |
37 | #define MAX_HASH 63 | |
38 | struct hash_entry | |
39 | { | |
40 | struct hash_entry *next; | |
41 | long opcode; | |
42 | long mask; | |
43 | struct simops *ops; | |
44 | }; | |
45 | ||
46 | struct hash_entry hash_table[MAX_HASH+1]; | |
47 | ||
9909e232 JL |
48 | |
49 | static INLINE long | |
22c1c7dd JL |
50 | hash(insn) |
51 | long insn; | |
52 | { | |
83fc3bac | 53 | if ((insn & 0x0600) == 0 |
9909e232 JL |
54 | || (insn & 0x0700) == 0x0200 |
55 | || (insn & 0x0700) == 0x0600 | |
56 | || (insn & 0x0780) == 0x0700) | |
0ef0eba5 | 57 | return (insn & 0x07e0) >> 5; |
83fc3bac JL |
58 | if ((insn & 0x0700) == 0x0300 |
59 | || (insn & 0x0700) == 0x0400 | |
60 | || (insn & 0x0700) == 0x0500) | |
61 | return (insn & 0x0780) >> 7; | |
83fc3bac | 62 | if ((insn & 0x07c0) == 0x0780) |
0ef0eba5 | 63 | return (insn & 0x07c0) >> 6; |
83fc3bac | 64 | return (insn & 0x07e0) >> 5; |
22c1c7dd JL |
65 | } |
66 | ||
67 | static struct hash_entry * | |
68 | lookup_hash (ins) | |
69 | uint32 ins; | |
70 | { | |
71 | struct hash_entry *h; | |
72 | ||
0ef0eba5 | 73 | h = &hash_table[hash(ins)]; |
22c1c7dd | 74 | |
9909e232 | 75 | while ((ins & h->mask) != h->opcode) |
22c1c7dd JL |
76 | { |
77 | if (h->next == NULL) | |
78 | { | |
ead4a3f1 | 79 | (*v850_callback->printf_filtered) (v850_callback, "ERROR looking up hash for %x\n", ins); |
22c1c7dd JL |
80 | exit(1); |
81 | } | |
82 | h = h->next; | |
83 | } | |
84 | return (h); | |
85 | } | |
86 | ||
28647e4c JL |
87 | uint8 |
88 | get_byte (x) | |
89 | uint8 *x; | |
22c1c7dd | 90 | { |
28647e4c | 91 | return *x; |
0ef0eba5 JL |
92 | } |
93 | ||
28647e4c JL |
94 | uint16 |
95 | get_half (x) | |
96 | uint8 *x; | |
0ef0eba5 JL |
97 | { |
98 | uint8 *a = x; | |
28647e4c | 99 | return (a[1] << 8) + (a[0]); |
22c1c7dd JL |
100 | } |
101 | ||
28647e4c | 102 | uint32 |
0ef0eba5 JL |
103 | get_word (x) |
104 | uint8 *x; | |
22c1c7dd | 105 | { |
0ef0eba5 | 106 | uint8 *a = x; |
28647e4c | 107 | return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]); |
22c1c7dd JL |
108 | } |
109 | ||
110 | void | |
28647e4c | 111 | put_byte (addr, data) |
0ef0eba5 | 112 | uint8 *addr; |
28647e4c | 113 | uint8 data; |
22c1c7dd | 114 | { |
0ef0eba5 | 115 | uint8 *a = addr; |
28647e4c | 116 | a[0] = data; |
22c1c7dd JL |
117 | } |
118 | ||
0ef0eba5 | 119 | void |
28647e4c | 120 | put_half (addr, data) |
0ef0eba5 | 121 | uint8 *addr; |
28647e4c | 122 | uint16 data; |
0ef0eba5 | 123 | { |
28647e4c JL |
124 | uint8 *a = addr; |
125 | a[0] = data & 0xff; | |
126 | a[1] = (data >> 8) & 0xff; | |
0ef0eba5 JL |
127 | } |
128 | ||
129 | void | |
28647e4c | 130 | put_word (addr, data) |
0ef0eba5 | 131 | uint8 *addr; |
28647e4c | 132 | uint32 data; |
0ef0eba5 JL |
133 | { |
134 | uint8 *a = addr; | |
28647e4c JL |
135 | a[0] = data & 0xff; |
136 | a[1] = (data >> 8) & 0xff; | |
137 | a[2] = (data >> 16) & 0xff; | |
138 | a[3] = (data >> 24) & 0xff; | |
22c1c7dd JL |
139 | } |
140 | ||
141 | static void | |
0ef0eba5 | 142 | do_format_1_2 (insn) |
22c1c7dd JL |
143 | uint32 insn; |
144 | { | |
0ef0eba5 | 145 | struct hash_entry *h; |
0ef0eba5 JL |
146 | |
147 | h = lookup_hash (insn); | |
148 | OP[0] = insn & 0x1f; | |
149 | OP[1] = (insn >> 11) & 0x1f; | |
150 | (h->ops->func) (); | |
22c1c7dd JL |
151 | } |
152 | ||
153 | static void | |
154 | do_format_3 (insn) | |
155 | uint32 insn; | |
156 | { | |
2108e864 | 157 | struct hash_entry *h; |
2108e864 JL |
158 | |
159 | h = lookup_hash (insn); | |
160 | OP[0] = (((insn & 0x70) >> 4) | ((insn & 0xf800) >> 8)) << 1; | |
161 | (h->ops->func) (); | |
22c1c7dd JL |
162 | } |
163 | ||
164 | static void | |
165 | do_format_4 (insn) | |
166 | uint32 insn; | |
167 | { | |
3cb6bf78 | 168 | struct hash_entry *h; |
3cb6bf78 JL |
169 | |
170 | h = lookup_hash (insn); | |
171 | OP[0] = (insn >> 11) & 0x1f; | |
172 | OP[1] = (insn & 0x7f); | |
173 | (h->ops->func) (); | |
22c1c7dd JL |
174 | } |
175 | ||
176 | static void | |
177 | do_format_5 (insn) | |
178 | uint32 insn; | |
179 | { | |
e9b6cbac | 180 | struct hash_entry *h; |
e9b6cbac JL |
181 | |
182 | h = lookup_hash (insn); | |
d81352b8 | 183 | OP[0] = (((insn & 0x3f) << 15) | ((insn >> 17) & 0x7fff)) << 1; |
e9b6cbac JL |
184 | OP[1] = (insn >> 11) & 0x1f; |
185 | (h->ops->func) (); | |
22c1c7dd JL |
186 | } |
187 | ||
188 | static void | |
189 | do_format_6 (insn) | |
190 | uint32 insn; | |
191 | { | |
0ef0eba5 | 192 | struct hash_entry *h; |
0ef0eba5 JL |
193 | |
194 | h = lookup_hash (insn); | |
195 | OP[0] = (insn >> 16) & 0xffff; | |
196 | OP[1] = insn & 0x1f; | |
197 | OP[2] = (insn >> 11) & 0x1f; | |
198 | (h->ops->func) (); | |
22c1c7dd JL |
199 | } |
200 | ||
201 | static void | |
202 | do_format_7 (insn) | |
203 | uint32 insn; | |
204 | { | |
28647e4c | 205 | struct hash_entry *h; |
28647e4c JL |
206 | |
207 | h = lookup_hash (insn); | |
208 | OP[0] = insn & 0x1f; | |
209 | OP[1] = (insn >> 11) & 0x1f; | |
210 | OP[2] = (insn >> 16) & 0xffff; | |
211 | (h->ops->func) (); | |
22c1c7dd JL |
212 | } |
213 | ||
214 | static void | |
215 | do_format_8 (insn) | |
216 | uint32 insn; | |
217 | { | |
83fc3bac | 218 | struct hash_entry *h; |
83fc3bac JL |
219 | |
220 | h = lookup_hash (insn); | |
221 | OP[0] = insn & 0x1f; | |
222 | OP[1] = (insn >> 11) & 0x7; | |
223 | OP[2] = (insn >> 16) & 0xffff; | |
224 | (h->ops->func) (); | |
22c1c7dd JL |
225 | } |
226 | ||
227 | static void | |
9909e232 | 228 | do_format_9_10 (insn) |
22c1c7dd JL |
229 | uint32 insn; |
230 | { | |
0ef0eba5 | 231 | struct hash_entry *h; |
0ef0eba5 JL |
232 | |
233 | h = lookup_hash (insn); | |
234 | OP[0] = insn & 0x1f; | |
235 | OP[1] = (insn >> 11) & 0x1f; | |
236 | (h->ops->func) (); | |
22c1c7dd JL |
237 | } |
238 | ||
239 | void | |
240 | sim_size (power) | |
241 | int power; | |
242 | ||
243 | { | |
28647e4c | 244 | if (State.mem) |
22c1c7dd | 245 | { |
28647e4c | 246 | free (State.mem); |
22c1c7dd JL |
247 | } |
248 | ||
28647e4c JL |
249 | State.mem = (uint8 *)calloc(1,1<<MEM_SIZE); |
250 | if (!State.mem) | |
22c1c7dd | 251 | { |
ead4a3f1 | 252 | (*v850_callback->printf_filtered) (v850_callback, "Memory allocation failed.\n"); |
22c1c7dd JL |
253 | exit(1); |
254 | } | |
22c1c7dd JL |
255 | } |
256 | ||
257 | static void | |
258 | init_system () | |
259 | { | |
28647e4c | 260 | if (!State.mem) |
22c1c7dd JL |
261 | sim_size(1); |
262 | } | |
263 | ||
264 | int | |
265 | sim_write (addr, buffer, size) | |
266 | SIM_ADDR addr; | |
267 | unsigned char *buffer; | |
268 | int size; | |
269 | { | |
270 | int i; | |
271 | init_system (); | |
272 | ||
22c1c7dd JL |
273 | for (i = 0; i < size; i++) |
274 | { | |
28647e4c | 275 | State.mem[i+addr] = buffer[i]; |
22c1c7dd JL |
276 | } |
277 | return size; | |
278 | } | |
279 | ||
280 | void | |
281 | sim_open (args) | |
282 | char *args; | |
283 | { | |
284 | struct simops *s; | |
9909e232 | 285 | struct hash_entry *h; |
22c1c7dd | 286 | if (args != NULL) |
ead4a3f1 MM |
287 | { |
288 | #ifdef DEBUG | |
289 | if (strcmp (args, "-t") == 0) | |
88777ce2 | 290 | v850_debug = DEBUG; |
ead4a3f1 MM |
291 | else |
292 | #endif | |
88777ce2 | 293 | (*v850_callback->printf_filtered) (v850_callback, "ERROR: unsupported option(s): %s\n",args); |
ead4a3f1 | 294 | } |
22c1c7dd JL |
295 | |
296 | /* put all the opcodes in the hash table */ | |
297 | for (s = Simops; s->func; s++) | |
298 | { | |
299 | h = &hash_table[hash(s->opcode)]; | |
300 | ||
301 | /* go to the last entry in the chain */ | |
302 | while (h->next) | |
303 | h = h->next; | |
304 | ||
305 | if (h->ops) | |
306 | { | |
307 | h->next = calloc(1,sizeof(struct hash_entry)); | |
308 | h = h->next; | |
309 | } | |
310 | h->ops = s; | |
311 | h->mask = s->mask; | |
312 | h->opcode = s->opcode; | |
313 | } | |
314 | } | |
315 | ||
316 | ||
317 | void | |
318 | sim_close (quitting) | |
319 | int quitting; | |
320 | { | |
321 | /* nothing to do */ | |
322 | } | |
323 | ||
324 | void | |
325 | sim_set_profile (n) | |
326 | int n; | |
327 | { | |
ead4a3f1 | 328 | (*v850_callback->printf_filtered) (v850_callback, "sim_set_profile %d\n", n); |
22c1c7dd JL |
329 | } |
330 | ||
331 | void | |
332 | sim_set_profile_size (n) | |
333 | int n; | |
334 | { | |
ead4a3f1 | 335 | (*v850_callback->printf_filtered) (v850_callback, "sim_set_profile_size %d\n", n); |
22c1c7dd JL |
336 | } |
337 | ||
338 | void | |
339 | sim_resume (step, siggnal) | |
340 | int step, siggnal; | |
341 | { | |
342 | uint32 inst, opcode; | |
22c1c7dd JL |
343 | reg_t oldpc; |
344 | ||
88777ce2 SG |
345 | PC = State.sregs[0]; |
346 | ||
22c1c7dd JL |
347 | if (step) |
348 | State.exception = SIGTRAP; | |
349 | else | |
350 | State.exception = 0; | |
351 | ||
352 | do | |
353 | { | |
354 | inst = RLW (PC); | |
355 | oldpc = PC; | |
0ef0eba5 JL |
356 | opcode = (inst & 0x07e0) >> 5; |
357 | if ((opcode & 0x30) == 0 | |
358 | || (opcode & 0x38) == 0x10) | |
22c1c7dd | 359 | { |
0ef0eba5 | 360 | do_format_1_2 (inst & 0xffff); |
22c1c7dd JL |
361 | PC += 2; |
362 | } | |
363 | else if ((opcode & 0x3C) == 0x18 | |
364 | || (opcode & 0x3C) == 0x1C | |
365 | || (opcode & 0x3C) == 0x20 | |
366 | || (opcode & 0x3C) == 0x24 | |
367 | || (opcode & 0x3C) == 0x28) | |
368 | { | |
0ef0eba5 | 369 | do_format_4 (inst & 0xffff); |
22c1c7dd JL |
370 | PC += 2; |
371 | } | |
d81352b8 | 372 | else if ((opcode & 0x3C) == 0x2C) |
22c1c7dd | 373 | { |
0ef0eba5 JL |
374 | do_format_3 (inst & 0xffff); |
375 | /* No PC update, it's done in the instruction. */ | |
22c1c7dd JL |
376 | } |
377 | else if ((opcode & 0x38) == 0x30) | |
378 | { | |
379 | do_format_6 (inst); | |
380 | PC += 4; | |
381 | } | |
382 | else if ((opcode & 0x3C) == 0x38) | |
383 | { | |
384 | do_format_7 (inst); | |
385 | PC += 4; | |
386 | } | |
387 | else if ((opcode & 0x3E) == 0x3C) | |
388 | { | |
389 | do_format_5 (inst); | |
e9b6cbac | 390 | /* No PC update, it's done in the instruction. */ |
22c1c7dd | 391 | } |
0ef0eba5 | 392 | else if ((opcode & 0x3F) == 0x3E) |
22c1c7dd JL |
393 | { |
394 | do_format_8 (inst); | |
395 | PC += 4; | |
396 | } | |
397 | else | |
398 | { | |
9909e232 | 399 | do_format_9_10 (inst); |
22c1c7dd JL |
400 | PC += 4; |
401 | } | |
402 | } | |
403 | while (!State.exception); | |
88777ce2 SG |
404 | |
405 | State.sregs[0] = PC; | |
22c1c7dd JL |
406 | } |
407 | ||
408 | int | |
409 | sim_trace () | |
410 | { | |
ead4a3f1 MM |
411 | #ifdef DEBUG |
412 | v850_debug = DEBUG; | |
413 | #endif | |
414 | sim_resume (0, 0); | |
415 | return 1; | |
22c1c7dd JL |
416 | } |
417 | ||
418 | void | |
419 | sim_info (verbose) | |
420 | int verbose; | |
421 | { | |
ead4a3f1 | 422 | (*v850_callback->printf_filtered) (v850_callback, "sim_info\n"); |
22c1c7dd JL |
423 | } |
424 | ||
425 | void | |
426 | sim_create_inferior (start_address, argv, env) | |
427 | SIM_ADDR start_address; | |
428 | char **argv; | |
429 | char **env; | |
430 | { | |
22c1c7dd JL |
431 | PC = start_address; |
432 | } | |
433 | ||
434 | ||
435 | void | |
436 | sim_kill () | |
437 | { | |
438 | /* nothing to do */ | |
439 | } | |
440 | ||
441 | void | |
442 | sim_set_callbacks(p) | |
443 | host_callback *p; | |
444 | { | |
d81352b8 | 445 | v850_callback = p; |
22c1c7dd JL |
446 | } |
447 | ||
2b6b2c6d | 448 | /* All the code for exiting, signals, etc needs to be revamped. |
d81352b8 JL |
449 | |
450 | This is enough to get c-torture limping though. */ | |
22c1c7dd JL |
451 | void |
452 | sim_stop_reason (reason, sigrc) | |
453 | enum sim_stop *reason; | |
454 | int *sigrc; | |
455 | { | |
22c1c7dd | 456 | |
d81352b8 | 457 | *reason = sim_stopped; |
22c1c7dd | 458 | if (State.exception == SIGQUIT) |
d81352b8 | 459 | *sigrc = 0; |
22c1c7dd | 460 | else |
d81352b8 | 461 | *sigrc = State.exception; |
22c1c7dd JL |
462 | } |
463 | ||
464 | void | |
465 | sim_fetch_register (rn, memory) | |
466 | int rn; | |
467 | unsigned char *memory; | |
468 | { | |
88777ce2 | 469 | put_word (memory, State.regs[rn]); |
22c1c7dd JL |
470 | } |
471 | ||
472 | void | |
473 | sim_store_register (rn, memory) | |
474 | int rn; | |
475 | unsigned char *memory; | |
476 | { | |
88777ce2 | 477 | State.regs[rn] = get_word (memory); |
22c1c7dd JL |
478 | } |
479 | ||
9909e232 | 480 | int |
22c1c7dd JL |
481 | sim_read (addr, buffer, size) |
482 | SIM_ADDR addr; | |
483 | unsigned char *buffer; | |
484 | int size; | |
485 | { | |
486 | int i; | |
487 | for (i = 0; i < size; i++) | |
488 | { | |
28647e4c | 489 | buffer[i] = State.mem[addr + i]; |
22c1c7dd JL |
490 | } |
491 | return size; | |
492 | } | |
493 | ||
494 | void | |
495 | sim_do_command (cmd) | |
496 | char *cmd; | |
497 | { | |
ead4a3f1 | 498 | (*v850_callback->printf_filtered) (v850_callback, "sim_do_command: %s\n", cmd); |
22c1c7dd JL |
499 | } |
500 | ||
501 | int | |
502 | sim_load (prog, from_tty) | |
503 | char *prog; | |
504 | int from_tty; | |
505 | { | |
506 | /* Return nonzero so GDB will handle it. */ | |
507 | return 1; | |
508 | } |