5 #include "mn10300_sim.h"
15 host_callback *mn10300_callback;
18 static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int));
19 static long hash PARAMS ((long));
20 static void init_system PARAMS ((void));
25 struct hash_entry *next;
31 struct hash_entry hash_table[MAX_HASH+1];
34 /* This probably doesn't do a very good job at bucket filling, but
40 /* These are one byte insns. */
41 if ((insn & 0xffffff00) == 0)
43 if ((insn & 0xf0) == 0x00
44 || (insn & 0xf0) == 0x40)
45 return (insn & 0xf3) & 0x3f;
47 if ((insn & 0xf0) == 0x10
48 || (insn & 0xf0) == 0x30
49 || (insn & 0xf0) == 0x50)
50 return (insn & 0xfc) & 0x3f;
52 if ((insn & 0xf0) == 0x60
53 || (insn & 0xf0) == 0x70
54 || (insn & 0xf0) == 0x80
55 || (insn & 0xf0) == 0x90
56 || (insn & 0xf0) == 0xa0
57 || (insn & 0xf0) == 0xb0
58 || (insn & 0xf0) == 0xe0)
59 return (insn & 0xf0) & 0x3f;
61 return (insn & 0xff) & 0x3f;
64 /* These are two byte insns */
65 if ((insn & 0xffff0000) == 0)
67 if ((insn & 0xf000) == 0x2000
68 || (insn & 0xf000) == 0x5000)
69 return ((insn & 0xfc00) >> 8) & 0x3f;
71 if ((insn & 0xf000) == 0x4000)
72 return ((insn & 0xf300) >> 8) & 0x3f;
74 if ((insn & 0xf000) == 0x8000
75 || (insn & 0xf000) == 0x9000
76 || (insn & 0xf000) == 0xa000
77 || (insn & 0xf000) == 0xb000)
78 return ((insn & 0xf000) >> 8) & 0x3f;
80 return ((insn & 0xff00) >> 8) & 0x3f;
83 /* These are three byte insns. */
84 if ((insn & 0xff000000) == 0)
86 if ((insn & 0xf00000) == 0x000000)
87 return ((insn & 0xf30000) >> 16) & 0x3f;
89 if ((insn & 0xf00000) == 0x200000
90 || (insn & 0xf00000) == 0x300000)
91 return ((insn & 0xfc0000) >> 16) & 0x3f;
93 return ((insn & 0xff0000) >> 16) & 0x3f;
96 /* These are four byte or larger insns. */
97 return ((insn & 0xff000000) >> 24) & 0x3f;
100 static struct hash_entry *
101 lookup_hash (ins, length)
105 struct hash_entry *h;
107 h = &hash_table[hash(ins)];
109 while ((ins & h->mask) != h->opcode
110 || (length != h->ops->length))
114 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR looking up hash for 0x%x, PC=0x%x\n", ins, PC);
122 /* FIXME These would more efficient to use than load_mem/store_mem,
123 but need to be changed to use the memory map. */
137 return (a[1] << 8) + (a[0]);
145 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
149 put_byte (addr, data)
158 put_half (addr, data)
164 a[1] = (data >> 8) & 0xff;
168 put_word (addr, data)
174 a[1] = (data >> 8) & 0xff;
175 a[2] = (data >> 16) & 0xff;
176 a[3] = (data >> 24) & 0xff;
181 load_mem_big (addr, len)
185 uint8 *p = addr + State.mem;
192 return p[0] << 8 | p[1];
194 return p[0] << 16 | p[1] << 8 | p[2];
196 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
207 uint8 *p = addr + State.mem;
214 return p[1] << 8 | p[0];
216 return p[2] << 16 | p[1] << 8 | p[0];
218 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
225 store_mem (addr, len, data)
230 uint8 *p = addr + State.mem;
260 State.mem = (uint8 *) calloc (1, 1 << power);
263 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
276 sim_write (addr, buffer, size)
278 unsigned char *buffer;
285 for (i = 0; i < size; i++)
286 store_mem (addr + i, 1, buffer[i]);
296 struct hash_entry *h;
300 if (strcmp (args, "-t") == 0)
301 mn10300_debug = DEBUG;
304 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",args);
307 /* put all the opcodes in the hash table */
308 for (s = Simops; s->func; s++)
310 h = &hash_table[hash(s->opcode)];
312 /* go to the last entry in the chain */
318 h->next = calloc(1,sizeof(struct hash_entry));
323 h->opcode = s->opcode;
339 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
343 sim_set_profile_size (n)
346 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
350 sim_resume (step, siggnal)
355 struct hash_entry *h;
358 State.exception = SIGTRAP;
364 unsigned long insn, extension;
366 /* Fetch the current instruction. */
367 inst = load_mem_big (PC, 1);
370 /* These are one byte insns. */
371 if ((inst & 0xf3) == 0x00
372 || (inst & 0xf0) == 0x10
373 || (inst & 0xfc) == 0x3c
374 || (inst & 0xf3) == 0x41
375 || (inst & 0xf3) == 0x40
376 || (inst & 0xfc) == 0x50
377 || (inst & 0xfc) == 0x54
378 || (inst & 0xf0) == 0x60
379 || (inst & 0xf0) == 0x70
380 || ((inst & 0xf0) == 0x80
381 && (inst & 0x0c) >> 2 != (inst & 0x03))
382 || ((inst & 0xf0) == 0x90
383 && (inst & 0x0c) >> 2 != (inst & 0x03))
384 || ((inst & 0xf0) == 0xa0
385 && (inst & 0x0c) >> 2 != (inst & 0x03))
386 || ((inst & 0xf0) == 0xb0
387 && (inst & 0x0c) >> 2 != (inst & 0x03))
388 || (inst & 0xff) == 0xcb
389 || (inst & 0xfc) == 0xd0
390 || (inst & 0xfc) == 0xd4
391 || (inst & 0xfc) == 0xd8
392 || (inst & 0xf0) == 0xe0)
395 h = lookup_hash (insn, 1);
397 (h->ops->func)(insn, extension);
401 /* These are two byte insns. */
402 else if ((inst & 0xf0) == 0x80
403 || (inst & 0xf0) == 0x90
404 || (inst & 0xf0) == 0xa0
405 || (inst & 0xf0) == 0xb0
406 || (inst & 0xfc) == 0x20
407 || (inst & 0xfc) == 0x28
408 || (inst & 0xf3) == 0x43
409 || (inst & 0xf3) == 0x42
410 || (inst & 0xfc) == 0x58
411 || (inst & 0xfc) == 0x5c
412 || ((inst & 0xf0) == 0xc0
413 && (inst & 0xff) != 0xcb
414 && (inst & 0xff) != 0xcc
415 && (inst & 0xff) != 0xcd)
416 || (inst & 0xff) == 0xf0
417 || (inst & 0xff) == 0xf1
418 || (inst & 0xff) == 0xf2
419 || (inst & 0xff) == 0xf3
420 || (inst & 0xff) == 0xf4
421 || (inst & 0xff) == 0xf5
422 || (inst & 0xff) == 0xf6)
424 insn = load_mem_big (PC, 2);
425 h = lookup_hash (insn, 2);
427 (h->ops->func)(insn, extension);
431 /* These are three byte insns. */
432 else if ((inst & 0xff) == 0xf8
433 || (inst & 0xff) == 0xcc
434 || (inst & 0xff) == 0xf9
435 || (inst & 0xf3) == 0x01
436 || (inst & 0xf3) == 0x02
437 || (inst & 0xf3) == 0x03
438 || (inst & 0xfc) == 0x24
439 || (inst & 0xfc) == 0x2c
440 || (inst & 0xfc) == 0x30
441 || (inst & 0xfc) == 0x34
442 || (inst & 0xfc) == 0x38
443 || (inst & 0xff) == 0xde
444 || (inst & 0xff) == 0xdf
445 || (inst & 0xff) == 0xcc)
447 insn = load_mem_big (PC, 3);
448 h = lookup_hash (insn, 3);
450 (h->ops->func)(insn, extension);
454 /* These are four byte insns. */
455 else if ((inst & 0xff) == 0xfa
456 || (inst & 0xff) == 0xfb)
458 insn = load_mem_big (PC, 4);
459 h = lookup_hash (insn, 4);
465 /* These are five byte insns. */
466 else if ((inst & 0xff) == 0xcd
467 || (inst & 0xff) == 0xdc)
469 insn = load_mem_big (PC, 4);
470 h = lookup_hash (insn, 5);
471 extension = load_mem_big (PC + 4, 1);
472 (h->ops->func)(insn, extension);
476 /* These are six byte insns. */
477 else if ((inst & 0xff) == 0xfd
478 || (inst & 0xff) == 0xfc)
480 insn = load_mem_big (PC, 4);
481 h = lookup_hash (insn, 6);
482 extension = load_mem_big (PC + 4, 2);
483 (h->ops->func)(insn, extension);
487 /* Else its a seven byte insns (in theory). */
490 insn = load_mem_big (PC, 4);
491 h = lookup_hash (insn, 7);
492 extension = load_mem_big (PC + 4, 3);
493 (h->ops->func)(insn, extension);
497 while (!State.exception);
504 mn10300_debug = DEBUG;
514 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
518 sim_create_inferior (start_address, argv, env)
519 SIM_ADDR start_address;
533 sim_set_callbacks (p)
536 mn10300_callback = p;
539 /* All the code for exiting, signals, etc needs to be revamped.
541 This is enough to get c-torture limping though. */
544 sim_stop_reason (reason, sigrc)
545 enum sim_stop *reason;
548 *reason = sim_stopped;
549 if (State.exception == SIGQUIT)
552 *sigrc = State.exception;
556 sim_fetch_register (rn, memory)
558 unsigned char *memory;
560 put_word (memory, State.regs[rn]);
564 sim_store_register (rn, memory)
566 unsigned char *memory;
568 State.regs[rn] = get_word (memory);
572 sim_read (addr, buffer, size)
574 unsigned char *buffer;
578 for (i = 0; i < size; i++)
579 buffer[i] = load_mem (addr + i, 1);
588 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
592 sim_load (prog, from_tty)
596 /* Return nonzero so GDB will handle it. */