5 #include "mn10300_sim.h"
7 host_callback *mn10300_callback;
9 static SIM_OPEN_KIND sim_kind;
12 static void dispatch PARAMS ((uint32, uint32, int));
13 static long hash PARAMS ((long));
14 static void init_system PARAMS ((void));
19 struct hash_entry *next;
28 static int max_mem = 0;
29 struct hash_entry hash_table[MAX_HASH+1];
32 /* This probably doesn't do a very good job at bucket filling, but
38 /* These are one byte insns, we special case these since, in theory,
39 they should be the most heavily used. */
40 if ((insn & 0xffffff00) == 0)
85 /* These are two byte insns */
86 if ((insn & 0xffff0000) == 0)
88 if ((insn & 0xf000) == 0x2000
89 || (insn & 0xf000) == 0x5000)
90 return ((insn & 0xfc00) >> 8) & 0x7f;
92 if ((insn & 0xf000) == 0x4000)
93 return ((insn & 0xf300) >> 8) & 0x7f;
95 if ((insn & 0xf000) == 0x8000
96 || (insn & 0xf000) == 0x9000
97 || (insn & 0xf000) == 0xa000
98 || (insn & 0xf000) == 0xb000)
99 return ((insn & 0xf000) >> 8) & 0x7f;
101 if ((insn & 0xff00) == 0xf000
102 || (insn & 0xff00) == 0xf100
103 || (insn & 0xff00) == 0xf200
104 || (insn & 0xff00) == 0xf500
105 || (insn & 0xff00) == 0xf600)
106 return ((insn & 0xfff0) >> 4) & 0x7f;
108 if ((insn & 0xf000) == 0xc000)
109 return ((insn & 0xff00) >> 8) & 0x7f;
111 return ((insn & 0xffc0) >> 6) & 0x7f;
114 /* These are three byte insns. */
115 if ((insn & 0xff000000) == 0)
117 if ((insn & 0xf00000) == 0x000000)
118 return ((insn & 0xf30000) >> 16) & 0x7f;
120 if ((insn & 0xf00000) == 0x200000
121 || (insn & 0xf00000) == 0x300000)
122 return ((insn & 0xfc0000) >> 16) & 0x7f;
124 if ((insn & 0xff0000) == 0xf80000)
125 return ((insn & 0xfff000) >> 12) & 0x7f;
127 if ((insn & 0xff0000) == 0xf90000)
128 return ((insn & 0xfffc00) >> 10) & 0x7f;
130 return ((insn & 0xff0000) >> 16) & 0x7f;
133 /* These are four byte or larger insns. */
134 if ((insn & 0xf0000000) == 0xf0000000)
135 return ((insn & 0xfff00000) >> 20) & 0x7f;
137 return ((insn & 0xff000000) >> 24) & 0x7f;
141 dispatch (insn, extension, length)
146 struct hash_entry *h;
148 h = &hash_table[hash(insn)];
150 while ((insn & h->mask) != h->opcode
151 || (length != h->ops->length))
155 (*mn10300_callback->printf_filtered) (mn10300_callback,
156 "ERROR looking up hash for 0x%x, PC=0x%x\n", insn, PC);
167 /* Now call the right function. */
168 (h->ops->func)(insn, extension);
172 /* FIXME These would more efficient to use than load_mem/store_mem,
173 but need to be changed to use the memory map. */
187 return (a[1] << 8) + (a[0]);
195 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
199 put_byte (addr, data)
208 put_half (addr, data)
214 a[1] = (data >> 8) & 0xff;
218 put_word (addr, data)
224 a[1] = (data >> 8) & 0xff;
225 a[2] = (data >> 16) & 0xff;
226 a[3] = (data >> 24) & 0xff;
237 max_mem = 1 << power;
238 State.mem = (uint8 *) calloc (1, 1 << power);
241 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
254 sim_write (sd, addr, buffer, size)
257 unsigned char *buffer;
264 for (i = 0; i < size; i++)
265 store_byte (addr + i, buffer[i]);
270 /* Compare two opcode table entries for qsort. */
272 compare_simops (arg1, arg2)
276 unsigned long code1 = ((struct simops *)arg1)->opcode;
277 unsigned long code2 = ((struct simops *)arg2)->opcode;
287 sim_open (kind,cb,argv)
293 struct hash_entry *h;
297 mn10300_callback = cb;
299 /* Sort the opcode array from smallest opcode to largest.
300 This will generally improve simulator performance as the smaller
301 opcodes are generally preferred to the larger opcodes. */
302 for (i = 0, s = Simops; s->func; s++, i++)
304 qsort (Simops, i, sizeof (Simops[0]), compare_simops);
309 for (p = argv + 1; *p; ++p)
311 if (strcmp (*p, "-E") == 0)
312 ++p; /* ignore endian spec */
315 if (strcmp (*p, "-t") == 0)
316 mn10300_debug = DEBUG;
319 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
322 /* put all the opcodes in the hash table */
323 for (s = Simops; s->func; s++)
325 h = &hash_table[hash(s->opcode)];
327 /* go to the last entry in the chain */
330 /* Don't insert the same opcode more than once. */
331 if (h->opcode == s->opcode
332 && h->mask == s->mask
339 /* Don't insert the same opcode more than once. */
340 if (h->opcode == s->opcode
341 && h->mask == s->mask
347 h->next = calloc(1,sizeof(struct hash_entry));
352 h->opcode = s->opcode;
359 /* fudge our descriptor for now */
365 sim_close (sd, quitting)
376 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
380 sim_set_profile_size (n)
383 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
394 sim_resume (sd, step, siggnal)
400 struct hash_entry *h;
403 State.exception = SIGTRAP;
409 unsigned long insn, extension;
411 /* Fetch the current instruction. */
412 inst = load_mem_big (PC, 2);
415 /* Using a giant case statement may seem like a waste because of the
416 code/rodata size the table itself will consume. However, using
417 a giant case statement speeds up the simulator by 10-15% by avoiding
418 cascading if/else statements or cascading case statements. */
420 switch ((inst >> 8) & 0xff)
422 /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
423 which must be handled specially. */
525 insn = (inst >> 8) & 0xff;
527 dispatch (insn, extension, 1);
530 /* Special cases where dm == dn is used to encode a different
550 dispatch (insn, extension, 2);
601 insn = (inst >> 8) & 0xff;
603 dispatch (insn, extension, 1);
606 /* The two byte instructions. */
653 dispatch (insn, extension, 2);
656 /* The three byte insns with a 16bit operand in little endian
691 insn = load_byte (PC);
693 insn |= load_half (PC + 1);
695 dispatch (insn, extension, 3);
698 /* The three byte insns without 16bit operand. */
703 insn = load_mem_big (PC, 3);
705 dispatch (insn, extension, 3);
708 /* Four byte insns. */
711 if ((inst & 0xfffc) == 0xfaf0
712 || (inst & 0xfffc) == 0xfaf4
713 || (inst & 0xfffc) == 0xfaf8)
714 insn = load_mem_big (PC, 4);
719 insn |= load_half (PC + 2);
722 dispatch (insn, extension, 4);
725 /* Five byte insns. */
727 insn = load_byte (PC);
729 insn |= (load_half (PC + 1) << 8);
730 insn |= load_byte (PC + 3);
731 extension = load_byte (PC + 4);
732 dispatch (insn, extension, 5);
736 insn = load_byte (PC);
738 extension = load_word (PC + 1);
739 insn |= (extension & 0xffffff00) >> 8;
741 dispatch (insn, extension, 5);
744 /* Six byte insns. */
748 extension = load_word (PC + 2);
749 insn |= ((extension & 0xffff0000) >> 16);
751 dispatch (insn, extension, 6);
755 insn = load_byte (PC) << 24;
756 extension = load_word (PC + 1);
757 insn |= ((extension >> 8) & 0xffffff);
758 extension = (extension & 0xff) << 16;
759 extension |= load_byte (PC + 5) << 8;
760 extension |= load_byte (PC + 6);
761 dispatch (insn, extension, 7);
766 extension = load_word (PC + 2);
767 insn |= ((extension >> 16) & 0xffff);
769 extension &= 0xffff00;
770 extension |= load_byte (PC + 6);
771 dispatch (insn, extension, 7);
778 while (!State.exception);
783 for (i = 0; i < MAX_HASH; i++)
785 struct hash_entry *h;
788 printf("hash 0x%x:\n", i);
792 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
809 mn10300_debug = DEBUG;
811 sim_resume (sd, 0, 0);
816 sim_info (sd, verbose)
820 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
824 sim_create_inferior (sd, argv, env)
840 sim_set_callbacks (p)
843 mn10300_callback = p;
846 /* All the code for exiting, signals, etc needs to be revamped.
848 This is enough to get c-torture limping though. */
851 sim_stop_reason (sd, reason, sigrc)
853 enum sim_stop *reason;
856 *reason = sim_stopped;
857 if (State.exception == SIGQUIT)
860 *sigrc = State.exception;
864 sim_fetch_register (sd, rn, memory)
867 unsigned char *memory;
869 put_word (memory, State.regs[rn]);
873 sim_store_register (sd, rn, memory)
876 unsigned char *memory;
878 State.regs[rn] = get_word (memory);
882 sim_read (sd, addr, buffer, size)
885 unsigned char *buffer;
889 for (i = 0; i < size; i++)
890 buffer[i] = load_byte (addr + i);
896 sim_do_command (sd, cmd)
900 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
904 sim_load (sd, prog, abfd, from_tty)
910 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
913 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
914 sim_kind == SIM_OPEN_DEBUG);
915 if (prog_bfd == NULL)
917 PC = bfd_get_start_address (prog_bfd);
919 bfd_close (prog_bfd);