5 #include "mn10300_sim.h"
15 host_callback *mn10300_callback;
20 static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int));
21 static long hash PARAMS ((long));
22 static void init_system PARAMS ((void));
27 struct hash_entry *next;
33 struct hash_entry hash_table[MAX_HASH+1];
36 /* This probably doesn't do a very good job at bucket filling, but
42 /* These are one byte insns. */
43 if ((insn & 0xffffff00) == 0)
45 if ((insn & 0xf0) == 0x00
46 || (insn & 0xf0) == 0x40)
47 return (insn & 0xf3) & 0x3f;
49 if ((insn & 0xf0) == 0x10
50 || (insn & 0xf0) == 0x30
51 || (insn & 0xf0) == 0x50)
52 return (insn & 0xfc) & 0x3f;
54 if ((insn & 0xf0) == 0x60
55 || (insn & 0xf0) == 0x70
56 || (insn & 0xf0) == 0x80
57 || (insn & 0xf0) == 0x90
58 || (insn & 0xf0) == 0xa0
59 || (insn & 0xf0) == 0xb0
60 || (insn & 0xf0) == 0xe0)
61 return (insn & 0xf0) & 0x3f;
63 return (insn & 0xff) & 0x3f;
66 /* These are two byte insns */
67 if ((insn & 0xffff0000) == 0)
69 if ((insn & 0xf000) == 0x2000
70 || (insn & 0xf000) == 0x5000)
71 return ((insn & 0xfc00) >> 8) & 0x3f;
73 if ((insn & 0xf000) == 0x4000)
74 return ((insn & 0xf300) >> 8) & 0x3f;
76 if ((insn & 0xf000) == 0x8000
77 || (insn & 0xf000) == 0x9000
78 || (insn & 0xf000) == 0xa000
79 || (insn & 0xf000) == 0xb000)
80 return ((insn & 0xf000) >> 8) & 0x3f;
82 return ((insn & 0xff00) >> 8) & 0x3f;
85 /* These are three byte insns. */
86 if ((insn & 0xff000000) == 0)
88 if ((insn & 0xf00000) == 0x000000)
89 return ((insn & 0xf30000) >> 16) & 0x3f;
91 if ((insn & 0xf00000) == 0x200000
92 || (insn & 0xf00000) == 0x300000)
93 return ((insn & 0xfc0000) >> 16) & 0x3f;
95 return ((insn & 0xff0000) >> 16) & 0x3f;
98 /* These are four byte or larger insns. */
99 return ((insn & 0xff000000) >> 24) & 0x3f;
102 static struct hash_entry *
103 lookup_hash (ins, length)
107 struct hash_entry *h;
109 h = &hash_table[hash(ins)];
111 while ((ins & h->mask) != h->opcode
112 || (length != h->ops->length))
116 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR looking up hash for 0x%x, PC=0x%x\n", ins, PC);
124 /* FIXME These would more efficient to use than load_mem/store_mem,
125 but need to be changed to use the memory map. */
139 return (a[1] << 8) + (a[0]);
147 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
151 put_byte (addr, data)
160 put_half (addr, data)
166 a[1] = (data >> 8) & 0xff;
170 put_word (addr, data)
176 a[1] = (data >> 8) & 0xff;
177 a[2] = (data >> 16) & 0xff;
178 a[3] = (data >> 24) & 0xff;
183 load_mem_big (addr, len)
187 uint8 *p = addr + State.mem;
194 return p[0] << 8 | p[1];
196 return p[0] << 16 | p[1] << 8 | p[2];
198 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
209 uint8 *p = addr + State.mem;
216 return p[1] << 8 | p[0];
218 return p[2] << 16 | p[1] << 8 | p[0];
220 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
227 store_mem (addr, len, data)
232 uint8 *p = addr + State.mem;
262 State.mem = (uint8 *) calloc (1, 1 << power);
265 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
278 sim_write (addr, buffer, size)
280 unsigned char *buffer;
287 for (i = 0; i < size; i++)
288 store_mem (addr + i, 1, buffer[i]);
298 struct hash_entry *h;
302 if (strcmp (args, "-t") == 0)
303 mn10300_debug = DEBUG;
306 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",args);
309 /* put all the opcodes in the hash table */
310 for (s = Simops; s->func; s++)
312 h = &hash_table[hash(s->opcode)];
314 /* go to the last entry in the chain */
320 h->next = calloc(1,sizeof(struct hash_entry));
325 h->opcode = s->opcode;
341 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
345 sim_set_profile_size (n)
348 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
352 sim_resume (step, siggnal)
357 struct hash_entry *h;
360 State.exception = SIGTRAP;
366 unsigned long insn, extension;
368 /* Fetch the current instruction. */
369 inst = load_mem_big (PC, 1);
372 /* These are one byte insns. */
373 if ((inst & 0xf3) == 0x00
374 || (inst & 0xf0) == 0x10
375 || (inst & 0xfc) == 0x3c
376 || (inst & 0xf3) == 0x41
377 || (inst & 0xf3) == 0x40
378 || (inst & 0xfc) == 0x50
379 || (inst & 0xfc) == 0x54
380 || (inst & 0xf0) == 0x60
381 || (inst & 0xf0) == 0x70
382 || ((inst & 0xf0) == 0x80
383 && (inst & 0x0c) >> 2 != (inst & 0x03))
384 || ((inst & 0xf0) == 0x90
385 && (inst & 0x0c) >> 2 != (inst & 0x03))
386 || ((inst & 0xf0) == 0xa0
387 && (inst & 0x0c) >> 2 != (inst & 0x03))
388 || ((inst & 0xf0) == 0xb0
389 && (inst & 0x0c) >> 2 != (inst & 0x03))
390 || (inst & 0xff) == 0xcb
391 || (inst & 0xfc) == 0xd0
392 || (inst & 0xfc) == 0xd4
393 || (inst & 0xfc) == 0xd8
394 || (inst & 0xf0) == 0xe0)
397 h = lookup_hash (insn, 1);
399 (h->ops->func)(insn, extension);
403 /* These are two byte insns. */
404 else if ((inst & 0xf0) == 0x80
405 || (inst & 0xf0) == 0x90
406 || (inst & 0xf0) == 0xa0
407 || (inst & 0xf0) == 0xb0
408 || (inst & 0xfc) == 0x20
409 || (inst & 0xfc) == 0x28
410 || (inst & 0xf3) == 0x43
411 || (inst & 0xf3) == 0x42
412 || (inst & 0xfc) == 0x58
413 || (inst & 0xfc) == 0x5c
414 || ((inst & 0xf0) == 0xc0
415 && (inst & 0xff) != 0xcb
416 && (inst & 0xff) != 0xcc
417 && (inst & 0xff) != 0xcd)
418 || (inst & 0xff) == 0xf0
419 || (inst & 0xff) == 0xf1
420 || (inst & 0xff) == 0xf2
421 || (inst & 0xff) == 0xf3
422 || (inst & 0xff) == 0xf4
423 || (inst & 0xff) == 0xf5
424 || (inst & 0xff) == 0xf6)
426 insn = load_mem_big (PC, 2);
427 h = lookup_hash (insn, 2);
429 (h->ops->func)(insn, extension);
433 /* These are three byte insns. */
434 else if ((inst & 0xff) == 0xf8
435 || (inst & 0xff) == 0xcc
436 || (inst & 0xff) == 0xf9
437 || (inst & 0xf3) == 0x01
438 || (inst & 0xf3) == 0x02
439 || (inst & 0xf3) == 0x03
440 || (inst & 0xfc) == 0x24
441 || (inst & 0xfc) == 0x2c
442 || (inst & 0xfc) == 0x30
443 || (inst & 0xfc) == 0x34
444 || (inst & 0xfc) == 0x38
445 || (inst & 0xff) == 0xde
446 || (inst & 0xff) == 0xdf
447 || (inst & 0xff) == 0xcc)
449 insn = load_mem_big (PC, 3);
450 h = lookup_hash (insn, 3);
452 (h->ops->func)(insn, extension);
456 /* These are four byte insns. */
457 else if ((inst & 0xff) == 0xfa
458 || (inst & 0xff) == 0xfb)
460 insn = load_mem_big (PC, 4);
461 h = lookup_hash (insn, 4);
467 /* These are five byte insns. */
468 else if ((inst & 0xff) == 0xcd
469 || (inst & 0xff) == 0xdc)
471 insn = load_mem_big (PC, 4);
472 h = lookup_hash (insn, 5);
473 extension = load_mem_big (PC + 4, 1);
474 (h->ops->func)(insn, extension);
478 /* These are six byte insns. */
479 else if ((inst & 0xff) == 0xfd
480 || (inst & 0xff) == 0xfc)
482 insn = load_mem_big (PC, 4);
483 h = lookup_hash (insn, 6);
484 extension = load_mem_big (PC + 4, 2);
485 (h->ops->func)(insn, extension);
489 /* Else its a seven byte insns (in theory). */
492 insn = load_mem_big (PC, 4);
493 h = lookup_hash (insn, 7);
494 extension = load_mem_big (PC + 4, 3);
495 (h->ops->func)(insn, extension);
499 while (!State.exception);
506 mn10300_debug = DEBUG;
516 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
520 sim_create_inferior (start_address, argv, env)
521 SIM_ADDR start_address;
535 sim_set_callbacks (p)
538 mn10300_callback = p;
541 /* All the code for exiting, signals, etc needs to be revamped.
543 This is enough to get c-torture limping though. */
546 sim_stop_reason (reason, sigrc)
547 enum sim_stop *reason;
550 *reason = sim_stopped;
551 if (State.exception == SIGQUIT)
554 *sigrc = State.exception;
558 sim_fetch_register (rn, memory)
560 unsigned char *memory;
562 put_word (memory, State.regs[rn]);
566 sim_store_register (rn, memory)
568 unsigned char *memory;
570 State.regs[rn] = get_word (memory);
574 sim_read (addr, buffer, size)
576 unsigned char *buffer;
580 for (i = 0; i < size; i++)
581 buffer[i] = load_mem (addr + i, 1);
590 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
594 sim_load (prog, from_tty)
598 /* Return nonzero so GDB will handle it. */