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. */
526 insn = (inst >> 8) & 0xff;
528 dispatch (insn, extension, 1);
531 /* Special cases where dm == dn is used to encode a different
551 dispatch (insn, extension, 2);
602 insn = (inst >> 8) & 0xff;
604 dispatch (insn, extension, 1);
607 /* The two byte instructions. */
654 dispatch (insn, extension, 2);
657 /* The three byte insns with a 16bit operand in little endian
692 insn = load_byte (PC);
694 insn |= load_half (PC + 1);
696 dispatch (insn, extension, 3);
699 /* The three byte insns without 16bit operand. */
704 insn = load_mem_big (PC, 3);
706 dispatch (insn, extension, 3);
709 /* Four byte insns. */
712 if ((inst & 0xfffc) == 0xfaf0
713 || (inst & 0xfffc) == 0xfaf4
714 || (inst & 0xfffc) == 0xfaf8)
715 insn = load_mem_big (PC, 4);
720 insn |= load_half (PC + 2);
723 dispatch (insn, extension, 4);
726 /* Five byte insns. */
728 insn = load_byte (PC);
730 insn |= (load_half (PC + 1) << 8);
731 insn |= load_byte (PC + 3);
732 extension = load_byte (PC + 4);
733 dispatch (insn, extension, 5);
737 insn = load_byte (PC);
739 extension = load_word (PC + 1);
740 insn |= (extension & 0xffffff00) >> 8;
742 dispatch (insn, extension, 5);
745 /* Six byte insns. */
749 extension = load_word (PC + 2);
750 insn |= ((extension & 0xffff0000) >> 16);
752 dispatch (insn, extension, 6);
756 insn = load_byte (PC) << 24;
757 extension = load_word (PC + 1);
758 insn |= ((extension >> 8) & 0xffffff);
759 extension = (extension & 0xff) << 16;
760 extension |= load_byte (PC + 5) << 8;
761 extension |= load_byte (PC + 6);
762 dispatch (insn, extension, 7);
767 extension = load_word (PC + 2);
768 insn |= ((extension >> 16) & 0xffff);
770 extension &= 0xffff00;
771 extension |= load_byte (PC + 6);
772 dispatch (insn, extension, 7);
779 while (!State.exception);
784 for (i = 0; i < MAX_HASH; i++)
786 struct hash_entry *h;
789 printf("hash 0x%x:\n", i);
793 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
810 mn10300_debug = DEBUG;
812 sim_resume (sd, 0, 0);
817 sim_info (sd, verbose)
821 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
825 sim_create_inferior (sd, argv, env)
841 sim_set_callbacks (p)
844 mn10300_callback = p;
847 /* All the code for exiting, signals, etc needs to be revamped.
849 This is enough to get c-torture limping though. */
852 sim_stop_reason (sd, reason, sigrc)
854 enum sim_stop *reason;
857 *reason = sim_stopped;
858 if (State.exception == SIGQUIT)
861 *sigrc = State.exception;
865 sim_fetch_register (sd, rn, memory)
868 unsigned char *memory;
870 put_word (memory, State.regs[rn]);
874 sim_store_register (sd, rn, memory)
877 unsigned char *memory;
879 State.regs[rn] = get_word (memory);
883 sim_read (sd, addr, buffer, size)
886 unsigned char *buffer;
890 for (i = 0; i < size; i++)
891 buffer[i] = load_byte (addr + i);
897 sim_do_command (sd, cmd)
901 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
905 sim_load (sd, prog, abfd, from_tty)
911 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
914 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
915 sim_kind == SIM_OPEN_DEBUG);
916 if (prog_bfd == NULL)
918 PC = bfd_get_start_address (prog_bfd);
920 bfd_close (prog_bfd);