* interp.c: Replace all references to load_mem and store_mem
[external/binutils.git] / sim / mn10300 / interp.c
1 #include <signal.h>
2 #include "sysdep.h"
3 #include "bfd.h"
4
5 #include "mn10300_sim.h"
6
7 host_callback *mn10300_callback;
8 int mn10300_debug;
9 static SIM_OPEN_KIND sim_kind;
10 static char *myname;
11
12 static void dispatch PARAMS ((uint32, uint32, int));
13 static long hash PARAMS ((long));
14 static void init_system PARAMS ((void));
15 #define MAX_HASH  127
16
17 struct hash_entry
18 {
19   struct hash_entry *next;
20   long opcode;
21   long mask;
22   struct simops *ops;
23 #ifdef HASH_STAT
24   unsigned long count;
25 #endif
26 };
27
28 static int max_mem = 0;
29 struct hash_entry hash_table[MAX_HASH+1];
30
31
32 /* This probably doesn't do a very good job at bucket filling, but
33    it's simple... */
34 static INLINE long 
35 hash(insn)
36      long insn;
37 {
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)
41     {
42       switch (insn & 0xf0)
43         {
44           case 0x00:
45             return 0x70;
46
47           case 0x40:
48             return 0x71;
49
50           case 0x10:
51             return 0x72;
52
53           case 0x30:
54             return 0x73;
55
56           case 0x50:
57             return 0x74;
58
59           case 0x60:
60             return 0x75;
61
62           case 0x70:
63             return 0x76;
64
65           case 0x80:
66             return 0x77;
67
68           case 0x90:
69             return 0x78;
70
71           case 0xa0:
72             return 0x79;
73
74           case 0xb0:
75             return 0x7a;
76
77           case 0xe0:
78             return 0x7b;
79
80           default:
81             return 0x7c;
82         }
83     }
84
85   /* These are two byte insns */
86   if ((insn & 0xffff0000) == 0)
87     {
88       if ((insn & 0xf000) == 0x2000
89           || (insn & 0xf000) == 0x5000)
90         return ((insn & 0xfc00) >> 8) & 0x7f;
91
92       if ((insn & 0xf000) == 0x4000)
93         return ((insn & 0xf300) >> 8) & 0x7f;
94
95       if ((insn & 0xf000) == 0x8000
96           || (insn & 0xf000) == 0x9000
97           || (insn & 0xf000) == 0xa000
98           || (insn & 0xf000) == 0xb000)
99         return ((insn & 0xf000) >> 8) & 0x7f;
100
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;
107  
108       if ((insn & 0xf000) == 0xc000)
109         return ((insn & 0xff00) >> 8) & 0x7f;
110
111       return ((insn & 0xffc0) >> 6) & 0x7f;
112     }
113
114   /* These are three byte insns.  */
115   if ((insn & 0xff000000) == 0)
116     {
117       if ((insn & 0xf00000) == 0x000000)
118         return ((insn & 0xf30000) >> 16) & 0x7f;
119
120       if ((insn & 0xf00000) == 0x200000
121           || (insn & 0xf00000) == 0x300000)
122         return ((insn & 0xfc0000) >> 16) & 0x7f;
123
124       if ((insn & 0xff0000) == 0xf80000)
125         return ((insn & 0xfff000) >> 12) & 0x7f;
126
127       if ((insn & 0xff0000) == 0xf90000)
128         return ((insn & 0xfffc00) >> 10) & 0x7f;
129
130       return ((insn & 0xff0000) >> 16) & 0x7f;
131     }
132
133   /* These are four byte or larger insns.  */
134   if ((insn & 0xf0000000) == 0xf0000000)
135     return ((insn & 0xfff00000) >> 20) & 0x7f;
136
137   return ((insn & 0xff000000) >> 24) & 0x7f;
138 }
139
140 static INLINE void
141 dispatch (insn, extension, length)
142      uint32 insn;
143      uint32 extension;
144      int length;
145 {
146   struct hash_entry *h;
147
148   h = &hash_table[hash(insn)];
149
150   while ((insn & h->mask) != h->opcode
151           || (length != h->ops->length))
152     {
153       if (!h->next)
154         {
155           (*mn10300_callback->printf_filtered) (mn10300_callback,
156             "ERROR looking up hash for 0x%x, PC=0x%x\n", insn, PC);
157           exit(1);
158         }
159       h = h->next;
160     }
161
162
163 #ifdef HASH_STAT
164   h->count++;
165 #endif
166
167   /* Now call the right function.  */
168   (h->ops->func)(insn, extension);
169   PC += length;
170 }
171
172 /* FIXME These would more efficient to use than load_mem/store_mem,
173    but need to be changed to use the memory map.  */
174
175 uint8
176 get_byte (x)
177      uint8 *x;
178 {
179   return *x;
180 }
181
182 uint16
183 get_half (x)
184      uint8 *x;
185 {
186   uint8 *a = x;
187   return (a[1] << 8) + (a[0]);
188 }
189
190 uint32
191 get_word (x)
192       uint8 *x;
193 {
194   uint8 *a = x;
195   return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
196 }
197
198 void
199 put_byte (addr, data)
200      uint8 *addr;
201      uint8 data;
202 {
203   uint8 *a = addr;
204   a[0] = data;
205 }
206
207 void
208 put_half (addr, data)
209      uint8 *addr;
210      uint16 data;
211 {
212   uint8 *a = addr;
213   a[0] = data & 0xff;
214   a[1] = (data >> 8) & 0xff;
215 }
216
217 void
218 put_word (addr, data)
219      uint8 *addr;
220      uint32 data;
221 {
222   uint8 *a = addr;
223   a[0] = data & 0xff;
224   a[1] = (data >> 8) & 0xff;
225   a[2] = (data >> 16) & 0xff;
226   a[3] = (data >> 24) & 0xff;
227 }
228
229 void
230 sim_size (power)
231      int power;
232
233 {
234   if (State.mem)
235     free (State.mem);
236
237   max_mem = 1 << power;
238   State.mem = (uint8 *) calloc (1,  1 << power);
239   if (!State.mem)
240     {
241       (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
242       exit (1);
243     }
244 }
245
246 static void
247 init_system ()
248 {
249   if (!State.mem)
250     sim_size(19);
251 }
252
253 int
254 sim_write (sd, addr, buffer, size)
255      SIM_DESC sd;
256      SIM_ADDR addr;
257      unsigned char *buffer;
258      int size;
259 {
260   int i;
261
262   init_system ();
263
264   for (i = 0; i < size; i++)
265     store_byte (addr + i, buffer[i]);
266
267   return size;
268 }
269
270 /* Compare two opcode table entries for qsort.  */
271 static int
272 compare_simops (arg1, arg2)
273      const PTR arg1;
274      const PTR arg2;
275 {
276   unsigned long code1 = ((struct simops *)arg1)->opcode;
277   unsigned long code2 = ((struct simops *)arg2)->opcode;
278
279   if (code1 < code2)
280     return -1;
281   if (code2 < code1)
282     return 1;
283   return 0;
284 }
285
286 SIM_DESC
287 sim_open (kind,cb,argv)
288      SIM_OPEN_KIND kind;
289      host_callback *cb;
290      char **argv;
291 {
292   struct simops *s;
293   struct hash_entry *h;
294   char **p;
295   int i;
296
297   mn10300_callback = cb;
298
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++)
303     ;
304   qsort (Simops, i, sizeof (Simops[0]), compare_simops);
305
306   sim_kind = kind;
307   myname = argv[0];
308
309   for (p = argv + 1; *p; ++p)
310     {
311       if (strcmp (*p, "-E") == 0)
312         ++p; /* ignore endian spec */
313       else
314 #ifdef DEBUG
315       if (strcmp (*p, "-t") == 0)
316         mn10300_debug = DEBUG;
317       else
318 #endif
319         (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",*p);
320     }
321
322  /* put all the opcodes in the hash table */
323   for (s = Simops; s->func; s++)
324     {
325       h = &hash_table[hash(s->opcode)];
326
327       /* go to the last entry in the chain */
328       while (h->next)
329         {
330           /* Don't insert the same opcode more than once.  */
331           if (h->opcode == s->opcode
332               && h->mask == s->mask
333               && h->ops == s)
334             continue;
335           else
336             h = h->next;
337         }
338
339       /* Don't insert the same opcode more than once.  */
340       if (h->opcode == s->opcode
341           && h->mask == s->mask
342           && h->ops == s)
343         continue;
344
345       if (h->ops)
346         {
347           h->next = calloc(1,sizeof(struct hash_entry));
348           h = h->next;
349         }
350       h->ops = s;
351       h->mask = s->mask;
352       h->opcode = s->opcode;
353 #if HASH_STAT
354       h->count = 0;
355 #endif
356     }
357
358
359   /* fudge our descriptor for now */
360   return (SIM_DESC) 1;
361 }
362
363
364 void
365 sim_close (sd, quitting)
366      SIM_DESC sd;
367      int quitting;
368 {
369   /* nothing to do */
370 }
371
372 void
373 sim_set_profile (n)
374      int n;
375 {
376   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
377 }
378
379 void
380 sim_set_profile_size (n)
381      int n;
382 {
383   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
384 }
385
386 int
387 sim_stop (sd)
388      SIM_DESC sd;
389 {
390   return 0;
391 }
392
393 void
394 sim_resume (sd, step, siggnal)
395      SIM_DESC sd;
396      int step, siggnal;
397 {
398   uint32 inst;
399   reg_t oldpc;
400   struct hash_entry *h;
401
402   if (step)
403     State.exception = SIGTRAP;
404   else
405     State.exception = 0;
406
407   do
408     {
409       unsigned long insn, extension;
410
411       /* Fetch the current instruction.  */
412       inst = load_mem_big (PC, 2);
413       oldpc = PC;
414
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.  */
419
420       switch ((inst >> 8) & 0xff)
421         {
422           /* All the single byte insns except 0x80, 0x90, 0xa0, 0xb0
423              which must be handled specially.  */
424           case 0x00:
425           case 0x04:
426           case 0x08:
427           case 0x0c:
428           case 0x11:
429           case 0x12:
430           case 0x13:
431           case 0x14:
432           case 0x15:
433           case 0x16:
434           case 0x17:
435           case 0x18:
436           case 0x19:
437           case 0x1a:
438           case 0x1b:
439           case 0x1c:
440           case 0x1d:
441           case 0x1e:
442           case 0x1f:
443           case 0x3c:
444           case 0x3d:
445           case 0x3e:
446           case 0x3f:
447           case 0x40:
448           case 0x41:
449           case 0x44:
450           case 0x45:
451           case 0x48:
452           case 0x49:
453           case 0x4c:
454           case 0x4d:
455           case 0x50:
456           case 0x51:
457           case 0x52:
458           case 0x53:
459           case 0x54:
460           case 0x55:
461           case 0x56:
462           case 0x57:
463           case 0x60:
464           case 0x61:
465           case 0x62:
466           case 0x63:
467           case 0x64:
468           case 0x65:
469           case 0x66:
470           case 0x67:
471           case 0x68:
472           case 0x69:
473           case 0x6a:
474           case 0x6b:
475           case 0x6c:
476           case 0x6d:
477           case 0x6e:
478           case 0x6f:
479           case 0x70:
480           case 0x71:
481           case 0x72:
482           case 0x73:
483           case 0x74:
484           case 0x75:
485           case 0x76:
486           case 0x77:
487           case 0x78:
488           case 0x79:
489           case 0x7a:
490           case 0x7b:
491           case 0x7c:
492           case 0x7d:
493           case 0x7e:
494           case 0x7f:
495           case 0xcb:
496           case 0xd0:
497           case 0xd1:
498           case 0xd2:
499           case 0xd3:
500           case 0xd4:
501           case 0xd5:
502           case 0xd6:
503           case 0xd7:
504           case 0xd8:
505           case 0xd9:
506           case 0xda:
507           case 0xdb:
508           case 0xe0:
509           case 0xe1:
510           case 0xe2:
511           case 0xe3:
512           case 0xe4:
513           case 0xe5:
514           case 0xe6:
515           case 0xe7:
516           case 0xe8:
517           case 0xe9:
518           case 0xea:
519           case 0xeb:
520           case 0xec:
521           case 0xed:
522           case 0xee:
523           case 0xef:
524           case 0xff:
525             insn = (inst >> 8) & 0xff;
526             extension = 0;
527             dispatch (insn, extension, 1);
528             break;
529
530           /* Special cases where dm == dn is used to encode a different
531              instruction.  */
532           case 0x80:
533           case 0x85:
534           case 0x8a:
535           case 0x8f:
536           case 0x90:
537           case 0x95:
538           case 0x9a:
539           case 0x9f:
540           case 0xa0:
541           case 0xa5:
542           case 0xaa:
543           case 0xaf:
544           case 0xb0:
545           case 0xb5:
546           case 0xba:
547           case 0xbf:
548             insn = inst;
549             extension = 0;
550             dispatch (insn, extension, 2);
551             break;
552
553           case 0x81:
554           case 0x82:
555           case 0x83:
556           case 0x84:
557           case 0x86:
558           case 0x87:
559           case 0x88:
560           case 0x89:
561           case 0x8b:
562           case 0x8c:
563           case 0x8d:
564           case 0x8e:
565           case 0x91:
566           case 0x92:
567           case 0x93:
568           case 0x94:
569           case 0x96:
570           case 0x97:
571           case 0x98:
572           case 0x99:
573           case 0x9b:
574           case 0x9c:
575           case 0x9d:
576           case 0x9e:
577           case 0xa1:
578           case 0xa2:
579           case 0xa3:
580           case 0xa4:
581           case 0xa6:
582           case 0xa7:
583           case 0xa8:
584           case 0xa9:
585           case 0xab:
586           case 0xac:
587           case 0xad:
588           case 0xae:
589           case 0xb1:
590           case 0xb2:
591           case 0xb3:
592           case 0xb4:
593           case 0xb6:
594           case 0xb7:
595           case 0xb8:
596           case 0xb9:
597           case 0xbb:
598           case 0xbc:
599           case 0xbd:
600           case 0xbe:
601             insn = (inst >> 8) & 0xff;
602             extension = 0;
603           dispatch (insn, extension, 1);
604           break;
605
606           /* The two byte instructions.  */
607           case 0x20:
608           case 0x21:
609           case 0x22:
610           case 0x23:
611           case 0x28:
612           case 0x29:
613           case 0x2a:
614           case 0x2b:
615           case 0x42:
616           case 0x43:
617           case 0x46:
618           case 0x47:
619           case 0x4a:
620           case 0x4b:
621           case 0x4e:
622           case 0x4f:
623           case 0x58:
624           case 0x59:
625           case 0x5a:
626           case 0x5b:
627           case 0x5c:
628           case 0x5d:
629           case 0x5e:
630           case 0x5f:
631           case 0xc0:
632           case 0xc1:
633           case 0xc2:
634           case 0xc3:
635           case 0xc4:
636           case 0xc5:
637           case 0xc6:
638           case 0xc7:
639           case 0xc8:
640           case 0xc9:
641           case 0xca:
642           case 0xce:
643           case 0xcf:
644           case 0xf0:
645           case 0xf1:
646           case 0xf2:
647           case 0xf3:
648           case 0xf4:
649           case 0xf5:
650           case 0xf6:
651             insn = inst;
652             extension = 0;
653             dispatch (insn, extension, 2);
654             break;
655
656           /* The three byte insns with a 16bit operand in little endian
657              format.  */
658           case 0x01:
659           case 0x02:
660           case 0x03:
661           case 0x05:
662           case 0x06:
663           case 0x07:
664           case 0x09:
665           case 0x0a:
666           case 0x0b:
667           case 0x0d:
668           case 0x0e:
669           case 0x0f:
670           case 0x24:
671           case 0x25:
672           case 0x26:
673           case 0x27:
674           case 0x2c:
675           case 0x2d:
676           case 0x2e:
677           case 0x2f:
678           case 0x30:
679           case 0x31:
680           case 0x32:
681           case 0x33:
682           case 0x34:
683           case 0x35:
684           case 0x36:
685           case 0x37:
686           case 0x38:
687           case 0x39:
688           case 0x3a:
689           case 0x3b:
690           case 0xcc:
691             insn = load_byte (PC);
692             insn <<= 16;
693             insn |= load_half (PC + 1);
694             extension = 0;
695             dispatch (insn, extension, 3);
696             break;
697
698           /* The three byte insns without 16bit operand.  */
699           case 0xde:
700           case 0xdf:
701           case 0xf8:
702           case 0xf9:
703             insn = load_mem_big (PC, 3);
704             extension = 0;
705             dispatch (insn, extension, 3);
706             break;
707           
708           /* Four byte insns.  */
709           case 0xfa:
710           case 0xfb:
711             if ((inst & 0xfffc) == 0xfaf0
712                 || (inst & 0xfffc) == 0xfaf4
713                 || (inst & 0xfffc) == 0xfaf8)
714               insn = load_mem_big (PC, 4);
715             else
716               {
717                 insn = inst;
718                 insn <<= 16;
719                 insn |= load_half (PC + 2);
720                 extension = 0;
721               }
722             dispatch (insn, extension, 4);
723             break;
724
725           /* Five byte insns.  */
726           case 0xcd:
727             insn = load_byte (PC);
728             insn <<= 24;
729             insn |= (load_half (PC + 1) << 8);
730             insn |= load_byte (PC + 3);
731             extension = load_byte (PC + 4);
732             dispatch (insn, extension, 5);
733             break;
734
735           case 0xdc:
736             insn = load_byte (PC);
737             insn <<= 24;
738             extension = load_word (PC + 1);
739             insn |= (extension & 0xffffff00) >> 8;
740             extension &= 0xff;
741             dispatch (insn, extension, 5);
742             break;
743         
744           /* Six byte insns.  */
745           case 0xfc:
746           case 0xfd:
747             insn = (inst << 16);
748             extension = load_word (PC + 2);
749             insn |= ((extension & 0xffff0000) >> 16);
750             extension &= 0xffff;
751             dispatch (insn, extension, 6);
752             break;
753             
754           case 0xdd:
755             insn = load_byte (PC) << 24;
756             extension = load_word (PC + 1);
757             insn |= ((extension >> 8) & 0xffffff);
758             extension = (extension & 0xff) << 16;
759             extension |= load_byte (PC + 5) << 8;
760             extension |= load_byte (PC + 6);
761             dispatch (insn, extension, 7);
762             break;
763
764           case 0xfe:
765             insn = inst << 16;
766             extension = load_word (PC + 2);
767             insn |= ((extension >> 16) & 0xffff);
768             extension <<= 8;
769             extension &= 0xffff00;
770             extension |= load_byte (PC + 6);
771             dispatch (insn, extension, 7);
772             break;
773
774           default:
775             abort ();
776         }
777     }
778   while (!State.exception);
779
780 #ifdef HASH_STAT
781   {
782     int i;
783     for (i = 0; i < MAX_HASH; i++)
784       {
785          struct hash_entry *h;
786          h = &hash_table[i];
787
788          printf("hash 0x%x:\n", i);
789
790          while (h)
791            {
792              printf("h->opcode = 0x%x, count = 0x%x\n", h->opcode, h->count);
793              h = h->next;
794            }
795
796          printf("\n\n");
797       }
798     fflush (stdout);
799   }
800 #endif
801
802 }
803
804 int
805 sim_trace (sd)
806      SIM_DESC sd;
807 {
808 #ifdef DEBUG
809   mn10300_debug = DEBUG;
810 #endif
811   sim_resume (sd, 0, 0);
812   return 1;
813 }
814
815 void
816 sim_info (sd, verbose)
817      SIM_DESC sd;
818      int verbose;
819 {
820   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
821 }
822
823 SIM_RC
824 sim_create_inferior (sd, argv, env)
825      SIM_DESC sd;
826      char **argv;
827      char **env;
828 {
829   return SIM_RC_OK;
830 }
831
832 void
833 sim_kill (sd)
834      SIM_DESC sd;
835 {
836   /* nothing to do */
837 }
838
839 void
840 sim_set_callbacks (p)
841      host_callback *p;
842 {
843   mn10300_callback = p;
844 }
845
846 /* All the code for exiting, signals, etc needs to be revamped.
847
848    This is enough to get c-torture limping though.  */
849
850 void
851 sim_stop_reason (sd, reason, sigrc)
852      SIM_DESC sd;
853      enum sim_stop *reason;
854      int *sigrc;
855 {
856   *reason = sim_stopped;
857   if (State.exception == SIGQUIT)
858     *sigrc = 0;
859   else
860     *sigrc = State.exception;
861 }
862
863 void
864 sim_fetch_register (sd, rn, memory)
865      SIM_DESC sd;
866      int rn;
867      unsigned char *memory;
868 {
869   put_word (memory, State.regs[rn]);
870 }
871  
872 void
873 sim_store_register (sd, rn, memory)
874      SIM_DESC sd;
875      int rn;
876      unsigned char *memory;
877 {
878   State.regs[rn] = get_word (memory);
879 }
880
881 int
882 sim_read (sd, addr, buffer, size)
883      SIM_DESC sd;
884      SIM_ADDR addr;
885      unsigned char *buffer;
886      int size;
887 {
888   int i;
889   for (i = 0; i < size; i++)
890     buffer[i] = load_byte (addr + i);
891
892   return size;
893
894
895 void
896 sim_do_command (sd, cmd)
897      SIM_DESC sd;
898      char *cmd;
899 {
900   (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
901 }
902
903 SIM_RC
904 sim_load (sd, prog, abfd, from_tty)
905      SIM_DESC sd;
906      char *prog;
907      bfd *abfd;
908      int from_tty;
909 {
910   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
911   bfd *prog_bfd;
912
913   prog_bfd = sim_load_file (sd, myname, mn10300_callback, prog, abfd,
914                             sim_kind == SIM_OPEN_DEBUG);
915   if (prog_bfd == NULL)
916     return SIM_RC_FAIL;
917   PC = bfd_get_start_address (prog_bfd);
918   if (abfd == NULL)
919     bfd_close (prog_bfd);
920   return SIM_RC_OK;
921