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;
239 load_mem_big (addr, len)
243 uint8 *p = addr + State.mem;
253 return p[0] << 8 | p[1];
255 return p[0] << 16 | p[1] << 8 | p[2];
257 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
268 uint8 *p = addr + State.mem;
278 return p[1] << 8 | p[0];
280 return p[2] << 16 | p[1] << 8 | p[0];
282 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
289 store_mem (addr, len, data)
294 uint8 *p = addr + State.mem;
327 max_mem = 1 << power;
328 State.mem = (uint8 *) calloc (1, 1 << power);
331 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
344 sim_write (sd, addr, buffer, size)
347 unsigned char *buffer;
354 for (i = 0; i < size; i++)
355 store_mem (addr + i, 1, buffer[i]);
366 struct hash_entry *h;
372 for (p = argv + 1; *p; ++p)
374 if (strcmp (*p, "-E") == 0)
375 ++p; /* ignore endian spec */
378 if (strcmp (*p, "-t") == 0)
379 mn10300_debug = DEBUG;
382 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
385 /* put all the opcodes in the hash table */
386 for (s = Simops; s->func; s++)
388 h = &hash_table[hash(s->opcode)];
390 /* go to the last entry in the chain */
393 /* Don't insert the same opcode more than once. */
394 if (h->opcode == s->opcode
395 && h->mask == s->mask
402 /* Don't insert the same opcode more than once. */
403 if (h->opcode == s->opcode
404 && h->mask == s->mask
410 h->next = calloc(1,sizeof(struct hash_entry));
415 h->opcode = s->opcode;
422 /* fudge our descriptor for now */
428 sim_close (sd, quitting)
439 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
443 sim_set_profile_size (n)
446 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
457 sim_resume (sd, step, siggnal)
463 struct hash_entry *h;
466 State.exception = SIGTRAP;
472 unsigned long insn, extension;
474 /* Fetch the current instruction. */
475 inst = load_mem_big (PC, 2);
478 /* Using a giant case statement may seem like a waste because of the
479 code/rodata size the table itself will consume. However, using
480 a giant case statement speeds up the simulator by 10-15% by avoiding
481 cascading if/else statements or cascading case statements. */
483 switch ((inst >> 8) & 0xff)
485 /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
486 which must be handled specially. */
588 insn = (inst >> 8) & 0xff;
590 dispatch (insn, extension, 1);
593 /* Special cases where dm == dn is used to encode a different
613 dispatch (insn, extension, 2);
664 insn = (inst >> 8) & 0xff;
666 dispatch (insn, extension, 1);
669 /* The two byte instructions. */
716 dispatch (insn, extension, 2);
719 /* The three byte insns with a 16bit operand in little endian
754 insn = load_mem (PC, 1);
756 insn |= load_mem (PC + 1, 2);
758 dispatch (insn, extension, 3);
761 /* The three byte insns without 16bit operand. */
766 insn = load_mem_big (PC, 3);
768 dispatch (insn, extension, 3);
771 /* Four byte insns. */
774 if ((inst & 0xfffc) == 0xfaf0
775 || (inst & 0xfffc) == 0xfaf4
776 || (inst & 0xfffc) == 0xfaf8)
777 insn = load_mem_big (PC, 4);
782 insn |= load_mem (PC + 2, 2);
785 dispatch (insn, extension, 4);
788 /* Five byte insns. */
790 insn = load_mem (PC, 1);
792 insn |= (load_mem (PC + 1, 2) << 8);
793 insn |= load_mem (PC + 3, 1);
794 extension = load_mem (PC + 4, 1);
795 dispatch (insn, extension, 5);
799 insn = load_mem (PC, 1);
801 extension = load_mem (PC + 1, 4);
802 insn |= (extension & 0xffffff00) >> 8;
804 dispatch (insn, extension, 5);
807 /* Six byte insns. */
811 extension = load_mem (PC + 2, 4);
812 insn |= ((extension & 0xffff0000) >> 16);
814 dispatch (insn, extension, 6);
818 insn = load_mem (PC, 1) << 24;
819 extension = load_mem (PC + 1, 4);
820 insn |= ((extension >> 8) & 0xffffff);
821 extension = (extension & 0xff) << 16;
822 extension |= load_mem (PC + 5, 1) << 8;
823 extension |= load_mem (PC + 6, 1);
824 dispatch (insn, extension, 7);
829 extension = load_mem (PC + 2, 4);
830 insn |= ((extension >> 16) & 0xffff);
832 extension &= 0xffff00;
833 extension |= load_mem (PC + 6, 1);
834 dispatch (insn, extension, 7);
841 while (!State.exception);
846 for (i = 0; i < MAX_HASH; i++)
848 struct hash_entry *h;
851 printf("hash 0x%x:\n", i);
855 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
872 mn10300_debug = DEBUG;
874 sim_resume (sd, 0, 0);
879 sim_info (sd, verbose)
883 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
887 sim_create_inferior (sd, argv, env)
903 sim_set_callbacks (sd, p)
907 mn10300_callback = p;
910 /* All the code for exiting, signals, etc needs to be revamped.
912 This is enough to get c-torture limping though. */
915 sim_stop_reason (sd, reason, sigrc)
917 enum sim_stop *reason;
920 *reason = sim_stopped;
921 if (State.exception == SIGQUIT)
924 *sigrc = State.exception;
928 sim_fetch_register (sd, rn, memory)
931 unsigned char *memory;
933 put_word (memory, State.regs[rn]);
937 sim_store_register (sd, rn, memory)
940 unsigned char *memory;
942 State.regs[rn] = get_word (memory);
946 sim_read (sd, addr, buffer, size)
949 unsigned char *buffer;
953 for (i = 0; i < size; i++)
954 buffer[i] = load_mem (addr + i, 1);
960 sim_do_command (sd, cmd)
964 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
968 sim_load (sd, prog, abfd, from_tty)
974 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
977 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
978 sim_kind == SIM_OPEN_DEBUG);
979 if (prog_bfd == NULL)
981 PC = bfd_get_start_address (prog_bfd);
983 bfd_close (prog_bfd);