5 #include "mn10300_sim.h"
15 host_callback *mn10300_callback;
17 static SIM_OPEN_KIND sim_kind;
20 static void dispatch PARAMS ((uint32, uint32, int));
21 static long hash PARAMS ((long));
22 static void init_system PARAMS ((void));
27 struct hash_entry *next;
36 static int max_mem = 0;
37 struct hash_entry hash_table[MAX_HASH+1];
40 /* This probably doesn't do a very good job at bucket filling, but
46 /* These are one byte insns, we special case these since, in theory,
47 they should be the most heavily used. */
48 if ((insn & 0xffffff00) == 0)
93 /* These are two byte insns */
94 if ((insn & 0xffff0000) == 0)
96 if ((insn & 0xf000) == 0x2000
97 || (insn & 0xf000) == 0x5000)
98 return ((insn & 0xfc00) >> 8) & 0x7f;
100 if ((insn & 0xf000) == 0x4000)
101 return ((insn & 0xf300) >> 8) & 0x7f;
103 if ((insn & 0xf000) == 0x8000
104 || (insn & 0xf000) == 0x9000
105 || (insn & 0xf000) == 0xa000
106 || (insn & 0xf000) == 0xb000)
107 return ((insn & 0xf000) >> 8) & 0x7f;
109 if ((insn & 0xff00) == 0xf000
110 || (insn & 0xff00) == 0xf100
111 || (insn & 0xff00) == 0xf200
112 || (insn & 0xff00) == 0xf500
113 || (insn & 0xff00) == 0xf600)
114 return ((insn & 0xfff0) >> 4) & 0x7f;
116 if ((insn & 0xf000) == 0xc000)
117 return ((insn & 0xff00) >> 8) & 0x7f;
119 return ((insn & 0xffc0) >> 6) & 0x7f;
122 /* These are three byte insns. */
123 if ((insn & 0xff000000) == 0)
125 if ((insn & 0xf00000) == 0x000000)
126 return ((insn & 0xf30000) >> 16) & 0x7f;
128 if ((insn & 0xf00000) == 0x200000
129 || (insn & 0xf00000) == 0x300000)
130 return ((insn & 0xfc0000) >> 16) & 0x7f;
132 if ((insn & 0xff0000) == 0xf80000)
133 return ((insn & 0xfff000) >> 12) & 0x7f;
135 if ((insn & 0xff0000) == 0xf90000)
136 return ((insn & 0xfffc00) >> 10) & 0x7f;
138 return ((insn & 0xff0000) >> 16) & 0x7f;
141 /* These are four byte or larger insns. */
142 if ((insn & 0xf0000000) == 0xf0000000)
143 return ((insn & 0xfff00000) >> 20) & 0x7f;
145 return ((insn & 0xff000000) >> 24) & 0x7f;
149 dispatch (insn, extension, length)
154 struct hash_entry *h;
156 h = &hash_table[hash(insn)];
158 while ((insn & h->mask) != h->opcode
159 || (length != h->ops->length))
163 (*mn10300_callback->printf_filtered) (mn10300_callback,
164 "ERROR looking up hash for 0x%x, PC=0x%x\n", insn, PC);
175 /* Now call the right function. */
176 (h->ops->func)(insn, extension);
180 /* FIXME These would more efficient to use than load_mem/store_mem,
181 but need to be changed to use the memory map. */
195 return (a[1] << 8) + (a[0]);
203 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
207 put_byte (addr, data)
216 put_half (addr, data)
222 a[1] = (data >> 8) & 0xff;
226 put_word (addr, data)
232 a[1] = (data >> 8) & 0xff;
233 a[2] = (data >> 16) & 0xff;
234 a[3] = (data >> 24) & 0xff;
242 uint8 *p = addr + State.mem;
252 return p[1] << 8 | p[0];
254 return p[2] << 16 | p[1] << 8 | p[0];
256 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
263 store_mem (addr, len, data)
268 uint8 *p = addr + State.mem;
301 max_mem = 1 << power;
302 State.mem = (uint8 *) calloc (1, 1 << power);
305 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
318 sim_write (sd, addr, buffer, size)
321 unsigned char *buffer;
328 for (i = 0; i < size; i++)
329 store_mem (addr + i, 1, buffer[i]);
334 /* Compare two opcode table entries for qsort. */
336 compare_simops (arg1, arg2)
340 unsigned long code1 = ((struct simops *)arg1)->opcode;
341 unsigned long code2 = ((struct simops *)arg2)->opcode;
356 struct hash_entry *h;
360 /* Sort the opcode array from smallest opcode to largest.
361 This will generally improve simulator performance as the smaller
362 opcodes are generally preferred to the larger opcodes. */
363 for (i = 0, s = Simops; s->func; s++, i++)
365 qsort (Simops, i, sizeof (Simops[0]), compare_simops);
370 for (p = argv + 1; *p; ++p)
372 if (strcmp (*p, "-E") == 0)
373 ++p; /* ignore endian spec */
376 if (strcmp (*p, "-t") == 0)
377 mn10300_debug = DEBUG;
380 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
383 /* put all the opcodes in the hash table */
384 for (s = Simops; s->func; s++)
386 h = &hash_table[hash(s->opcode)];
388 /* go to the last entry in the chain */
391 /* Don't insert the same opcode more than once. */
392 if (h->opcode == s->opcode
393 && h->mask == s->mask
400 /* Don't insert the same opcode more than once. */
401 if (h->opcode == s->opcode
402 && h->mask == s->mask
408 h->next = calloc(1,sizeof(struct hash_entry));
413 h->opcode = s->opcode;
420 /* fudge our descriptor for now */
426 sim_close (sd, quitting)
437 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
441 sim_set_profile_size (n)
444 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
455 sim_resume (sd, step, siggnal)
461 struct hash_entry *h;
464 State.exception = SIGTRAP;
470 unsigned long insn, extension;
472 /* Fetch the current instruction. */
473 inst = load_mem_big (PC, 2);
476 /* Using a giant case statement may seem like a waste because of the
477 code/rodata size the table itself will consume. However, using
478 a giant case statement speeds up the simulator by 10-15% by avoiding
479 cascading if/else statements or cascading case statements. */
481 switch ((inst >> 8) & 0xff)
483 /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
484 which must be handled specially. */
586 insn = (inst >> 8) & 0xff;
588 dispatch (insn, extension, 1);
591 /* Special cases where dm == dn is used to encode a different
611 dispatch (insn, extension, 2);
662 insn = (inst >> 8) & 0xff;
664 dispatch (insn, extension, 1);
667 /* The two byte instructions. */
714 dispatch (insn, extension, 2);
717 /* The three byte insns with a 16bit operand in little endian
752 insn = load_mem (PC, 1);
754 insn |= load_mem (PC + 1, 2);
756 dispatch (insn, extension, 3);
759 /* The three byte insns without 16bit operand. */
764 insn = load_mem_big (PC, 3);
766 dispatch (insn, extension, 3);
769 /* Four byte insns. */
772 if ((inst & 0xfffc) == 0xfaf0
773 || (inst & 0xfffc) == 0xfaf4
774 || (inst & 0xfffc) == 0xfaf8)
775 insn = load_mem_big (PC, 4);
780 insn |= load_mem (PC + 2, 2);
783 dispatch (insn, extension, 4);
786 /* Five byte insns. */
788 insn = load_mem (PC, 1);
790 insn |= (load_mem (PC + 1, 2) << 8);
791 insn |= load_mem (PC + 3, 1);
792 extension = load_mem (PC + 4, 1);
793 dispatch (insn, extension, 5);
797 insn = load_mem (PC, 1);
799 extension = load_mem (PC + 1, 4);
800 insn |= (extension & 0xffffff00) >> 8;
802 dispatch (insn, extension, 5);
805 /* Six byte insns. */
809 extension = load_mem (PC + 2, 4);
810 insn |= ((extension & 0xffff0000) >> 16);
812 dispatch (insn, extension, 6);
816 insn = load_mem (PC, 1) << 24;
817 extension = load_mem (PC + 1, 4);
818 insn |= ((extension >> 8) & 0xffffff);
819 extension = (extension & 0xff) << 16;
820 extension |= load_mem (PC + 5, 1) << 8;
821 extension |= load_mem (PC + 6, 1);
822 dispatch (insn, extension, 7);
827 extension = load_mem (PC + 2, 4);
828 insn |= ((extension >> 16) & 0xffff);
830 extension &= 0xffff00;
831 extension |= load_mem (PC + 6, 1);
832 dispatch (insn, extension, 7);
839 while (!State.exception);
844 for (i = 0; i < MAX_HASH; i++)
846 struct hash_entry *h;
849 printf("hash 0x%x:\n", i);
853 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
870 mn10300_debug = DEBUG;
872 sim_resume (sd, 0, 0);
877 sim_info (sd, verbose)
881 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
885 sim_create_inferior (sd, argv, env)
901 sim_set_callbacks (sd, p)
905 mn10300_callback = p;
908 /* All the code for exiting, signals, etc needs to be revamped.
910 This is enough to get c-torture limping though. */
913 sim_stop_reason (sd, reason, sigrc)
915 enum sim_stop *reason;
918 *reason = sim_stopped;
919 if (State.exception == SIGQUIT)
922 *sigrc = State.exception;
926 sim_fetch_register (sd, rn, memory)
929 unsigned char *memory;
931 put_word (memory, State.regs[rn]);
935 sim_store_register (sd, rn, memory)
938 unsigned char *memory;
940 State.regs[rn] = get_word (memory);
944 sim_read (sd, addr, buffer, size)
947 unsigned char *buffer;
951 for (i = 0; i < size; i++)
952 buffer[i] = load_mem (addr + i, 1);
958 sim_do_command (sd, cmd)
962 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
966 sim_load (sd, prog, abfd, from_tty)
972 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
975 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
976 sim_kind == SIM_OPEN_DEBUG);
977 if (prog_bfd == NULL)
979 PC = bfd_get_start_address (prog_bfd);
981 bfd_close (prog_bfd);