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 struct hash_entry hash_table[MAX_HASH+1];
39 /* This probably doesn't do a very good job at bucket filling, but
45 /* These are one byte insns, we special case these since, in theory,
46 they should be the most heavily used. */
47 if ((insn & 0xffffff00) == 0)
92 /* These are two byte insns */
93 if ((insn & 0xffff0000) == 0)
95 if ((insn & 0xf000) == 0x2000
96 || (insn & 0xf000) == 0x5000)
97 return ((insn & 0xfc00) >> 8) & 0x7f;
99 if ((insn & 0xf000) == 0x4000)
100 return ((insn & 0xf300) >> 8) & 0x7f;
102 if ((insn & 0xf000) == 0x8000
103 || (insn & 0xf000) == 0x9000
104 || (insn & 0xf000) == 0xa000
105 || (insn & 0xf000) == 0xb000)
106 return ((insn & 0xf000) >> 8) & 0x7f;
108 if ((insn & 0xff00) == 0xf000
109 || (insn & 0xff00) == 0xf100
110 || (insn & 0xff00) == 0xf200
111 || (insn & 0xff00) == 0xf500
112 || (insn & 0xff00) == 0xf600)
113 return ((insn & 0xfff0) >> 4) & 0x7f;
115 if ((insn & 0xf000) == 0xc000)
116 return ((insn & 0xff00) >> 8) & 0x7f;
118 return ((insn & 0xffc0) >> 6) & 0x7f;
121 /* These are three byte insns. */
122 if ((insn & 0xff000000) == 0)
124 if ((insn & 0xf00000) == 0x000000)
125 return ((insn & 0xf30000) >> 16) & 0x7f;
127 if ((insn & 0xf00000) == 0x200000
128 || (insn & 0xf00000) == 0x300000)
129 return ((insn & 0xfc0000) >> 16) & 0x7f;
131 if ((insn & 0xff0000) == 0xf80000)
132 return ((insn & 0xfff000) >> 12) & 0x7f;
134 if ((insn & 0xff0000) == 0xf90000)
135 return ((insn & 0xfffc00) >> 10) & 0x7f;
137 return ((insn & 0xff0000) >> 16) & 0x7f;
140 /* These are four byte or larger insns. */
141 if ((insn & 0xf0000000) == 0xf0000000)
142 return ((insn & 0xfff00000) >> 20) & 0x7f;
144 return ((insn & 0xff000000) >> 24) & 0x7f;
148 dispatch (insn, extension, length)
153 struct hash_entry *h;
155 h = &hash_table[hash(insn)];
157 while ((insn & h->mask) != h->opcode
158 || (length != h->ops->length))
162 (*mn10300_callback->printf_filtered) (mn10300_callback,
163 "ERROR looking up hash for 0x%x, PC=0x%x\n", insn, PC);
174 /* Now call the right function. */
175 (h->ops->func)(insn, extension);
179 /* FIXME These would more efficient to use than load_mem/store_mem,
180 but need to be changed to use the memory map. */
194 return (a[1] << 8) + (a[0]);
202 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
206 put_byte (addr, data)
215 put_half (addr, data)
221 a[1] = (data >> 8) & 0xff;
225 put_word (addr, data)
231 a[1] = (data >> 8) & 0xff;
232 a[2] = (data >> 16) & 0xff;
233 a[3] = (data >> 24) & 0xff;
238 load_mem_big (addr, len)
242 uint8 *p = addr + State.mem;
249 return p[0] << 8 | p[1];
251 return p[0] << 16 | p[1] << 8 | p[2];
253 return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
264 uint8 *p = addr + State.mem;
271 return p[1] << 8 | p[0];
273 return p[2] << 16 | p[1] << 8 | p[0];
275 return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
282 store_mem (addr, len, data)
287 uint8 *p = addr + State.mem;
317 State.mem = (uint8 *) calloc (1, 1 << power);
320 (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
333 sim_write (sd, addr, buffer, size)
336 unsigned char *buffer;
343 for (i = 0; i < size; i++)
344 store_mem (addr + i, 1, buffer[i]);
355 struct hash_entry *h;
361 for (p = argv + 1; *p; ++p)
363 if (strcmp (*p, "-E") == 0)
364 ++p; /* ignore endian spec */
367 if (strcmp (*p, "-t") == 0)
368 mn10300_debug = DEBUG;
371 (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
374 /* put all the opcodes in the hash table */
375 for (s = Simops; s->func; s++)
377 h = &hash_table[hash(s->opcode)];
379 /* go to the last entry in the chain */
382 /* Don't insert the same opcode more than once. */
383 if (h->opcode == s->opcode
384 && h->mask == s->mask
391 /* Don't insert the same opcode more than once. */
392 if (h->opcode == s->opcode
393 && h->mask == s->mask
399 h->next = calloc(1,sizeof(struct hash_entry));
404 h->opcode = s->opcode;
411 /* fudge our descriptor for now */
417 sim_close (sd, quitting)
428 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
432 sim_set_profile_size (n)
435 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
446 sim_resume (sd, step, siggnal)
452 struct hash_entry *h;
455 State.exception = SIGTRAP;
461 unsigned long insn, extension;
463 /* Fetch the current instruction. */
464 inst = load_mem_big (PC, 2);
467 /* Using a giant case statement may seem like a waste because of the
468 code/rodata size the table itself will consume. However, using
469 a giant case statement speeds up the simulator by 10-15% by avoiding
470 cascading if/else statements or cascading case statements. */
472 switch ((inst >> 8) & 0xff)
474 /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
475 which must be handled specially. */
577 insn = (inst >> 8) & 0xff;
579 dispatch (insn, extension, 1);
582 /* Special cases where dm == dn is used to encode a different
602 dispatch (insn, extension, 2);
653 insn = (inst >> 8) & 0xff;
655 dispatch (insn, extension, 1);
658 /* The two byte instructions. */
705 dispatch (insn, extension, 2);
708 /* The three byte insns with a 16bit operand in little endian
743 insn = load_mem (PC, 1);
745 insn |= load_mem (PC + 1, 2);
747 dispatch (insn, extension, 3);
750 /* The three byte insns without 16bit operand. */
755 insn = load_mem_big (PC, 3);
757 dispatch (insn, extension, 3);
760 /* Four byte insns. */
763 if ((inst & 0xfffc) == 0xfaf0
764 || (inst & 0xfffc) == 0xfaf4
765 || (inst & 0xfffc) == 0xfaf8)
766 insn = load_mem_big (PC, 4);
771 insn |= load_mem (PC + 2, 2);
774 dispatch (insn, extension, 4);
777 /* Five byte insns. */
779 insn = load_mem (PC, 1);
781 insn |= (load_mem (PC + 1, 2) << 8);
782 insn |= load_mem (PC + 3, 1);
783 extension = load_mem (PC + 4, 1);
784 dispatch (insn, extension, 5);
788 insn = load_mem (PC, 1);
790 extension = load_mem (PC + 1, 4);
791 insn |= (extension & 0xffffff00) >> 8;
793 dispatch (insn, extension, 5);
796 /* Six byte insns. */
800 extension = load_mem (PC + 2, 4);
801 insn |= ((extension & 0xffff0000) >> 16);
803 dispatch (insn, extension, 6);
807 insn = load_mem (PC, 1) << 24;
808 extension = load_mem (PC + 1, 4);
809 insn |= ((extension >> 8) & 0xffffff);
810 extension = (extension & 0xff) << 16;
811 extension |= load_mem (PC + 5, 1) << 8;
812 extension |= load_mem (PC + 6, 1);
813 dispatch (insn, extension, 7);
818 extension = load_mem (PC + 2, 4);
819 insn |= ((extension >> 16) & 0xffff);
821 extension &= 0xffff00;
822 extension |= load_mem (PC + 6, 1);
823 dispatch (insn, extension, 7);
830 while (!State.exception);
835 for (i = 0; i < MAX_HASH; i++)
837 struct hash_entry *h;
840 printf("hash 0x%x:\n", i);
844 printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
861 mn10300_debug = DEBUG;
863 sim_resume (sd, 0, 0);
868 sim_info (sd, verbose)
872 (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
876 sim_create_inferior (sd, argv, env)
892 sim_set_callbacks (sd, p)
896 mn10300_callback = p;
899 /* All the code for exiting, signals, etc needs to be revamped.
901 This is enough to get c-torture limping though. */
904 sim_stop_reason (sd, reason, sigrc)
906 enum sim_stop *reason;
909 *reason = sim_stopped;
910 if (State.exception == SIGQUIT)
913 *sigrc = State.exception;
917 sim_fetch_register (sd, rn, memory)
920 unsigned char *memory;
922 put_word (memory, State.regs[rn]);
926 sim_store_register (sd, rn, memory)
929 unsigned char *memory;
931 State.regs[rn] = get_word (memory);
935 sim_read (sd, addr, buffer, size)
938 unsigned char *buffer;
942 for (i = 0; i < size; i++)
943 buffer[i] = load_mem (addr + i, 1);
949 sim_do_command (sd, cmd)
953 (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
957 sim_load (sd, prog, abfd, from_tty)
963 extern bfd *sim_load_file (); /* ??? Don't know where this should live. */
966 prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
967 sim_kind == SIM_OPEN_DEBUG);
968 if (prog_bfd == NULL)
970 PC = bfd_get_start_address (prog_bfd);
972 bfd_close (prog_bfd);