5 #include "mn10300_sim.h"
15 host_callback
*mn10300_callback
;
17 static SIM_OPEN_KIND sim_kind
;
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 (sd
, addr
, buffer
, size
)
281 unsigned char *buffer
;
288 for (i
= 0; i
< size
; i
++)
289 store_mem (addr
+ i
, 1, buffer
[i
]);
300 struct hash_entry
*h
;
306 for (p
= argv
+ 1; *p
; ++p
)
308 if (strcmp (*p
, "-E") == 0)
309 ++p
; /* ignore endian spec */
312 if (strcmp (*p
, "-t") == 0)
313 mn10300_debug
= DEBUG
;
316 (*mn10300_callback
->printf_filtered
) (mn10300_callback
, "ERROR: unsupported option(s): %s\n",*p
);
319 /* put all the opcodes in the hash table */
320 for (s
= Simops
; s
->func
; s
++)
322 h
= &hash_table
[hash(s
->opcode
)];
324 /* go to the last entry in the chain */
330 h
->next
= calloc(1,sizeof(struct hash_entry
));
335 h
->opcode
= s
->opcode
;
338 /* fudge our descriptor for now */
344 sim_close (sd
, quitting
)
355 (*mn10300_callback
->printf_filtered
) (mn10300_callback
, "sim_set_profile %d\n", n
);
359 sim_set_profile_size (n
)
362 (*mn10300_callback
->printf_filtered
) (mn10300_callback
, "sim_set_profile_size %d\n", n
);
366 sim_resume (sd
, step
, siggnal
)
372 struct hash_entry
*h
;
375 State
.exception
= SIGTRAP
;
381 unsigned long insn
, extension
;
383 /* Fetch the current instruction. */
384 inst
= load_mem_big (PC
, 1);
387 /* These are one byte insns. */
388 if ((inst
& 0xf3) == 0x00
389 || (inst
& 0xf0) == 0x10
390 || (inst
& 0xfc) == 0x3c
391 || (inst
& 0xf3) == 0x41
392 || (inst
& 0xf3) == 0x40
393 || (inst
& 0xfc) == 0x50
394 || (inst
& 0xfc) == 0x54
395 || (inst
& 0xf0) == 0x60
396 || (inst
& 0xf0) == 0x70
397 || ((inst
& 0xf0) == 0x80
398 && (inst
& 0x0c) >> 2 != (inst
& 0x03))
399 || ((inst
& 0xf0) == 0x90
400 && (inst
& 0x0c) >> 2 != (inst
& 0x03))
401 || ((inst
& 0xf0) == 0xa0
402 && (inst
& 0x0c) >> 2 != (inst
& 0x03))
403 || ((inst
& 0xf0) == 0xb0
404 && (inst
& 0x0c) >> 2 != (inst
& 0x03))
405 || (inst
& 0xff) == 0xcb
406 || (inst
& 0xfc) == 0xd0
407 || (inst
& 0xfc) == 0xd4
408 || (inst
& 0xfc) == 0xd8
409 || (inst
& 0xf0) == 0xe0
410 || (inst
& 0xff) == 0xff)
413 h
= lookup_hash (insn
, 1);
415 (h
->ops
->func
)(insn
, extension
);
419 /* These are two byte insns. */
420 else if ((inst
& 0xf0) == 0x80
421 || (inst
& 0xf0) == 0x90
422 || (inst
& 0xf0) == 0xa0
423 || (inst
& 0xf0) == 0xb0
424 || (inst
& 0xfc) == 0x20
425 || (inst
& 0xfc) == 0x28
426 || (inst
& 0xf3) == 0x43
427 || (inst
& 0xf3) == 0x42
428 || (inst
& 0xfc) == 0x58
429 || (inst
& 0xfc) == 0x5c
430 || ((inst
& 0xf0) == 0xc0
431 && (inst
& 0xff) != 0xcb
432 && (inst
& 0xff) != 0xcc
433 && (inst
& 0xff) != 0xcd)
434 || (inst
& 0xff) == 0xf0
435 || (inst
& 0xff) == 0xf1
436 || (inst
& 0xff) == 0xf2
437 || (inst
& 0xff) == 0xf3
438 || (inst
& 0xff) == 0xf4
439 || (inst
& 0xff) == 0xf5
440 || (inst
& 0xff) == 0xf6)
442 insn
= load_mem_big (PC
, 2);
443 h
= lookup_hash (insn
, 2);
445 (h
->ops
->func
)(insn
, extension
);
449 /* These are three byte insns. */
450 else if ((inst
& 0xff) == 0xf8
451 || (inst
& 0xff) == 0xcc
452 || (inst
& 0xff) == 0xf9
453 || (inst
& 0xf3) == 0x01
454 || (inst
& 0xf3) == 0x02
455 || (inst
& 0xf3) == 0x03
456 || (inst
& 0xfc) == 0x24
457 || (inst
& 0xfc) == 0x2c
458 || (inst
& 0xfc) == 0x30
459 || (inst
& 0xfc) == 0x34
460 || (inst
& 0xfc) == 0x38
461 || (inst
& 0xff) == 0xde
462 || (inst
& 0xff) == 0xdf
463 || (inst
& 0xff) == 0xcc)
465 insn
= load_mem_big (PC
, 3);
466 h
= lookup_hash (insn
, 3);
468 /* If it's a format D1 insn, "ret", or "retf" insn, then
469 there's no need to worry about endianness. Others have
470 a 16bit immediate in little endian form that we need to
472 if (h
->ops
->format
== FMT_D1
473 || h
->opcode
== 0xdf0000
474 || h
->opcode
== 0xde0000)
475 (h
->ops
->func
)(insn
, extension
);
479 insn
|= load_mem (PC
+ 1, 2);
480 (h
->ops
->func
)(insn
, extension
);
485 /* These are four byte insns. */
486 else if ((inst
& 0xff) == 0xfa
487 || (inst
& 0xff) == 0xfb)
489 insn
= load_mem_big (PC
, 4);
490 h
= lookup_hash (insn
, 4);
492 /* This must be a format D2 insn; a small number of such insns
493 don't have any 16bit immediates (they instead have two 8 bit
495 if (h
->opcode
== 0xfaf80000
496 || h
->opcode
== 0xfaf00000
497 || h
->opcode
== 0xfaf40000)
498 (h
->ops
->func
)(insn
, extension
);
502 insn
|= load_mem (PC
+ 2, 2);
503 (h
->ops
->func
)(insn
, extension
);
508 /* These are five byte insns. */
509 else if ((inst
& 0xff) == 0xcd
510 || (inst
& 0xff) == 0xdc)
512 insn
= load_mem_big (PC
, 4);
513 h
= lookup_hash (insn
, 5);
515 /* This must be a format S4 insn. */
516 if (h
->opcode
== 0xdc000000)
518 /* A "jmp" instruction with a 32bit immediate stored
519 in little endian form. */
521 temp
= load_mem (PC
+ 1, 4);
523 insn
|= (temp
& 0xffffff00) >> 8;
524 extension
= temp
& 0xff;
528 /* A "call" instruction with a 16bit immediate in little
531 temp
= load_mem (PC
+ 1, 2);
534 extension
= load_mem (PC
+ 4, 1);
536 (h
->ops
->func
)(insn
, extension
);
540 /* These are six byte insns. */
541 else if ((inst
& 0xff) == 0xfd
542 || (inst
& 0xff) == 0xfc)
546 insn
= load_mem_big (PC
, 4);
547 h
= lookup_hash (insn
, 6);
549 temp
= load_mem (PC
+ 2, 4);
551 insn
|= (temp
>> 16) & 0xffff;
552 extension
= temp
& 0xffff;
553 (h
->ops
->func
)(insn
, extension
);
557 /* Else its a seven byte insns (in theory). */
560 insn
= load_mem_big (PC
, 4);
561 h
= lookup_hash (insn
, 7);
563 if (h
->ops
->format
== FMT_S6
)
567 temp
= load_mem (PC
+ 1, 4);
569 insn
|= (temp
>> 8) & 0xffffff;
571 extension
= (temp
& 0xff) << 16;
572 extension
|= load_mem (PC
+ 5, 1) << 8;
573 extension
|= load_mem (PC
+ 6, 1);
579 temp
= load_mem (PC
+ 2, 4);
581 insn
|= (temp
>> 16) & 0xffff;
582 extension
= (temp
& 0xffff) << 8;
583 extension
= load_mem (PC
+ 6, 1);
585 (h
->ops
->func
)(insn
, extension
);
589 while (!State
.exception
);
597 mn10300_debug
= DEBUG
;
599 sim_resume (sd
, 0, 0);
604 sim_info (sd
, verbose
)
608 (*mn10300_callback
->printf_filtered
) (mn10300_callback
, "sim_info\n");
612 sim_create_inferior (sd
, argv
, env
)
628 sim_set_callbacks (sd
, p
)
632 mn10300_callback
= p
;
635 /* All the code for exiting, signals, etc needs to be revamped.
637 This is enough to get c-torture limping though. */
640 sim_stop_reason (sd
, reason
, sigrc
)
642 enum sim_stop
*reason
;
645 *reason
= sim_stopped
;
646 if (State
.exception
== SIGQUIT
)
649 *sigrc
= State
.exception
;
653 sim_fetch_register (sd
, rn
, memory
)
656 unsigned char *memory
;
658 put_word (memory
, State
.regs
[rn
]);
662 sim_store_register (sd
, rn
, memory
)
665 unsigned char *memory
;
667 State
.regs
[rn
] = get_word (memory
);
671 sim_read (sd
, addr
, buffer
, size
)
674 unsigned char *buffer
;
678 for (i
= 0; i
< size
; i
++)
679 buffer
[i
] = load_mem (addr
+ i
, 1);
685 sim_do_command (sd
, cmd
)
689 (*mn10300_callback
->printf_filtered
) (mn10300_callback
, "\"%s\" is not a valid mn10300 simulator command.\n", cmd
);
693 sim_load (sd
, prog
, abfd
, from_tty
)
699 extern bfd
*sim_load_file (); /* ??? Don't know where this should live. */
702 prog_bfd
= sim_load_file (sd
, myname
, mn10300_callback
, prog
, abfd
,
703 sim_kind
== SIM_OPEN_DEBUG
);
704 if (prog_bfd
== NULL
)
706 PC
= bfd_get_start_address (prog_bfd
);
708 bfd_close (prog_bfd
);