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, abfd, argv)
294 struct hash_entry *h;
298 mn10300_callback = cb;
300 /* Sort the opcode array from smallest opcode to largest.
301 This will generally improve simulator performance as the smaller
302 opcodes are generally preferred to the larger opcodes. */
303 for (i = 0, s = Simops; s->func; s++, i++)
305 qsort (Simops, i, sizeof (Simops[0]), compare_simops);
310 for (p = argv + 1; *p; ++p)
312 if (strcmp (*p, "-E") == 0)
313 ++p; /* ignore endian spec */
316 if (strcmp (*p, "-t") == 0)
317 mn10300_debug = DEBUG;
320 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
323 /* put all the opcodes in the hash table */
324 for (s = Simops; s->func; s++)
326 h = &hash_table[hash(s->opcode)];
328 /* go to the last entry in the chain */
331 /* Don't insert the same opcode more than once. */
332 if (h->opcode == s->opcode
333 && h->mask == s->mask
340 /* Don't insert the same opcode more than once. */
341 if (h->opcode == s->opcode
342 && h->mask == s->mask
348 h->next = calloc(1,sizeof(struct hash_entry));
353 h->opcode = s->opcode;
360 /* fudge our descriptor for now */
366 sim_close (sd, quitting)
377 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
381 sim_set_profile_size (n)
384 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
395 sim_resume (sd, step, siggnal)
401 struct hash_entry *h;
404 State.exception = SIGTRAP;
412 unsigned long insn, extension;
414 /* Fetch the current instruction. */
415 inst = load_mem_big (PC, 2);
418 /* Using a giant case statement may seem like a waste because of the
419 code/rodata size the table itself will consume. However, using
420 a giant case statement speeds up the simulator by 10-15% by avoiding
421 cascading if/else statements or cascading case statements. */
423 switch ((inst >> 8) & 0xff)
425 /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
426 which must be handled specially. */
529 insn = (inst >> 8) & 0xff;
531 dispatch (insn, extension, 1);
534 /* Special cases where dm == dn is used to encode a different
554 dispatch (insn, extension, 2);
605 insn = (inst >> 8) & 0xff;
607 dispatch (insn, extension, 1);
610 /* The two byte instructions. */
657 dispatch (insn, extension, 2);
660 /* The three byte insns with a 16bit operand in little endian
695 insn = load_byte (PC);
697 insn |= load_half (PC + 1);
699 dispatch (insn, extension, 3);
702 /* The three byte insns without 16bit operand. */
707 insn = load_mem_big (PC, 3);
709 dispatch (insn, extension, 3);
712 /* Four byte insns. */
715 if ((inst & 0xfffc) == 0xfaf0
716 || (inst & 0xfffc) == 0xfaf4
717 || (inst & 0xfffc) == 0xfaf8)
718 insn = load_mem_big (PC, 4);
723 insn |= load_half (PC + 2);
726 dispatch (insn, extension, 4);
729 /* Five byte insns. */
731 insn = load_byte (PC);
733 insn |= (load_half (PC + 1) << 8);
734 insn |= load_byte (PC + 3);
735 extension = load_byte (PC + 4);
736 dispatch (insn, extension, 5);
740 insn = load_byte (PC);
742 extension = load_word (PC + 1);
743 insn |= (extension & 0xffffff00) >> 8;
745 dispatch (insn, extension, 5);
748 /* Six byte insns. */
752 extension = load_word (PC + 2);
753 insn |= ((extension & 0xffff0000) >> 16);
755 dispatch (insn, extension, 6);
759 insn = load_byte (PC) << 24;
760 extension = load_word (PC + 1);
761 insn |= ((extension >> 8) & 0xffffff);
762 extension = (extension & 0xff) << 16;
763 extension |= load_byte (PC + 5) << 8;
764 extension |= load_byte (PC + 6);
765 dispatch (insn, extension, 7);
770 extension = load_word (PC + 2);
771 insn |= ((extension >> 16) & 0xffff);
773 extension &= 0xffff00;
774 extension |= load_byte (PC + 6);
775 dispatch (insn, extension, 7);
782 while (!State.exception);
787 for (i = 0; i < MAX_HASH; i++)
789 struct hash_entry *h;
792 printf("hash 0x%x:\n", i);
796 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
813 mn10300_debug = DEBUG;
815 sim_resume (sd, 0, 0);
820 sim_info (sd, verbose)
824 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
828 sim_create_inferior (sd, argv, env)
844 sim_set_callbacks (p)
847 mn10300_callback = p;
850 /* All the code for exiting, signals, etc needs to be revamped.
852 This is enough to get c-torture limping though. */
855 sim_stop_reason (sd, reason, sigrc)
857 enum sim_stop *reason;
861 *reason = sim_exited;
863 *reason = sim_stopped;
864 if (State.exception == SIGQUIT)
867 *sigrc = State.exception;
871 sim_fetch_register (sd, rn, memory)
874 unsigned char *memory;
876 put_word (memory, State.regs[rn]);
880 sim_store_register (sd, rn, memory)
883 unsigned char *memory;
885 State.regs[rn] = get_word (memory);
889 sim_read (sd, addr, buffer, size)
892 unsigned char *buffer;
896 for (i = 0; i < size; i++)
897 buffer[i] = load_byte (addr + i);
903 sim_do_command (sd, cmd)
907 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
911 sim_load (sd, prog, abfd, from_tty)
917 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
920 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
921 sim_kind == SIM_OPEN_DEBUG);
922 if (prog_bfd == NULL)
924 PC = bfd_get_start_address (prog_bfd);
926 bfd_close (prog_bfd);