* gencode.c (write_header): Add "insn" and "extension" arguments
[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 #ifndef INLINE
8 #ifdef __GNUC__
9 #define INLINE inline
10 #else
11 #define INLINE
12 #endif
13 #endif
14
15 host_callback *mn10300_callback;
16 int mn10300_debug;
17
18 uint32 OP[4];
19
20 static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int));
21 static long hash PARAMS ((long));
22 static void init_system PARAMS ((void));
23 #define MAX_HASH  63
24
25 struct hash_entry
26 {
27   struct hash_entry *next;
28   long opcode;
29   long mask;
30   struct simops *ops;
31 };
32
33 struct hash_entry hash_table[MAX_HASH+1];
34
35
36 /* This probably doesn't do a very good job at bucket filling, but
37    it's simple... */
38 static INLINE long 
39 hash(insn)
40      long insn;
41 {
42   /* These are one byte insns.  */
43   if ((insn & 0xffffff00) == 0)
44     {
45       if ((insn & 0xf0) == 0x00
46           || (insn & 0xf0) == 0x40)
47         return (insn & 0xf3) & 0x3f;
48
49       if ((insn & 0xf0) == 0x10
50           || (insn & 0xf0) == 0x30
51           || (insn & 0xf0) == 0x50)
52         return (insn & 0xfc) & 0x3f;
53
54       if ((insn & 0xf0) == 0x60
55           || (insn & 0xf0) == 0x70
56           || (insn & 0xf0) == 0x80
57           || (insn & 0xf0) == 0x90
58           || (insn & 0xf0) == 0xa0
59           || (insn & 0xf0) == 0xb0
60           || (insn & 0xf0) == 0xe0)
61         return (insn & 0xf0) & 0x3f;
62
63       return (insn & 0xff) & 0x3f;
64     }
65
66   /* These are two byte insns */
67   if ((insn & 0xffff0000) == 0)
68     {
69       if ((insn & 0xf000) == 0x2000
70           || (insn & 0xf000) == 0x5000)
71         return ((insn & 0xfc00) >> 8) & 0x3f;
72
73       if ((insn & 0xf000) == 0x4000)
74         return ((insn & 0xf300) >> 8) & 0x3f;
75
76       if ((insn & 0xf000) == 0x8000
77           || (insn & 0xf000) == 0x9000
78           || (insn & 0xf000) == 0xa000
79           || (insn & 0xf000) == 0xb000)
80         return ((insn & 0xf000) >> 8) & 0x3f;
81
82       return ((insn & 0xff00) >> 8) & 0x3f;
83     }
84
85   /* These are three byte insns.  */
86   if ((insn & 0xff000000) == 0)
87     {
88       if ((insn & 0xf00000) == 0x000000)
89         return ((insn & 0xf30000) >> 16) & 0x3f;
90
91       if ((insn & 0xf00000) == 0x200000
92           || (insn & 0xf00000) == 0x300000)
93         return ((insn & 0xfc0000) >> 16) & 0x3f;
94
95         return ((insn & 0xff0000) >> 16) & 0x3f;
96     }
97
98   /* These are four byte or larger insns.  */
99   return ((insn & 0xff000000) >> 24) & 0x3f;
100 }
101
102 static struct hash_entry *
103 lookup_hash (ins, length)
104      uint32 ins;
105      int length;
106 {
107   struct hash_entry *h;
108
109   h = &hash_table[hash(ins)];
110
111   while ((ins & h->mask) != h->opcode
112          || (length != h->ops->length))
113     {
114       if (h->next == NULL)
115         {
116           (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR looking up hash for 0x%x, PC=0x%x\n", ins, PC);
117           exit(1);
118         }
119       h = h->next;
120     }
121   return (h);
122 }
123
124 /* FIXME These would more efficient to use than load_mem/store_mem,
125    but need to be changed to use the memory map.  */
126
127 uint8
128 get_byte (x)
129      uint8 *x;
130 {
131   return *x;
132 }
133
134 uint16
135 get_half (x)
136      uint8 *x;
137 {
138   uint8 *a = x;
139   return (a[1] << 8) + (a[0]);
140 }
141
142 uint32
143 get_word (x)
144       uint8 *x;
145 {
146   uint8 *a = x;
147   return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
148 }
149
150 void
151 put_byte (addr, data)
152      uint8 *addr;
153      uint8 data;
154 {
155   uint8 *a = addr;
156   a[0] = data;
157 }
158
159 void
160 put_half (addr, data)
161      uint8 *addr;
162      uint16 data;
163 {
164   uint8 *a = addr;
165   a[0] = data & 0xff;
166   a[1] = (data >> 8) & 0xff;
167 }
168
169 void
170 put_word (addr, data)
171      uint8 *addr;
172      uint32 data;
173 {
174   uint8 *a = addr;
175   a[0] = data & 0xff;
176   a[1] = (data >> 8) & 0xff;
177   a[2] = (data >> 16) & 0xff;
178   a[3] = (data >> 24) & 0xff;
179 }
180
181
182 uint32
183 load_mem_big (addr, len)
184      SIM_ADDR addr;
185      int len;
186 {
187   uint8 *p = addr + State.mem;
188
189   switch (len)
190     {
191     case 1:
192       return p[0];
193     case 2:
194       return p[0] << 8 | p[1];
195     case 3:
196       return p[0] << 16 | p[1] << 8 | p[2];
197     case 4:
198       return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
199     default:
200       abort ();
201     }
202 }
203
204 uint32
205 load_mem (addr, len)
206      SIM_ADDR addr;
207      int len;
208 {
209   uint8 *p = addr + State.mem;
210
211   switch (len)
212     {
213     case 1:
214       return p[0];
215     case 2:
216       return p[1] << 8 | p[0];
217     case 3:
218       return p[2] << 16 | p[1] << 8 | p[0];
219     case 4:
220       return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
221     default:
222       abort ();
223     }
224 }
225
226 void
227 store_mem (addr, len, data)
228      SIM_ADDR addr;
229      int len;
230      uint32 data;
231 {
232   uint8 *p = addr + State.mem;
233
234   switch (len)
235     {
236     case 1:
237       p[0] = data;
238       return;
239     case 2:
240       p[0] = data;
241       p[1] = data >> 8;
242       return;
243     case 4:
244       p[0] = data;
245       p[1] = data >> 8;
246       p[2] = data >> 16;
247       p[3] = data >> 24;
248       return;
249     default:
250       abort ();
251     }
252 }
253
254 void
255 sim_size (power)
256      int power;
257
258 {
259   if (State.mem)
260     free (State.mem);
261
262   State.mem = (uint8 *) calloc (1,  1 << power);
263   if (!State.mem)
264     {
265       (*mn10300_callback->printf_filtered) (mn10300_callback, "Allocation of main memory failed.\n");
266       exit (1);
267     }
268 }
269
270 static void
271 init_system ()
272 {
273   if (!State.mem)
274     sim_size(18);
275 }
276
277 int
278 sim_write (addr, buffer, size)
279      SIM_ADDR addr;
280      unsigned char *buffer;
281      int size;
282 {
283   int i;
284
285   init_system ();
286
287   for (i = 0; i < size; i++)
288     store_mem (addr + i, 1, buffer[i]);
289
290   return size;
291 }
292
293 void
294 sim_open (args)
295      char *args;
296 {
297   struct simops *s;
298   struct hash_entry *h;
299   if (args != NULL)
300     {
301 #ifdef DEBUG
302       if (strcmp (args, "-t") == 0)
303         mn10300_debug = DEBUG;
304       else
305 #endif
306         (*mn10300_callback->printf_filtered) (mn10300_callback, "ERROR: unsupported option(s): %s\n",args);
307     }
308
309   /* put all the opcodes in the hash table */
310   for (s = Simops; s->func; s++)
311     {
312       h = &hash_table[hash(s->opcode)];
313       
314       /* go to the last entry in the chain */
315       while (h->next)
316           h = h->next;
317
318       if (h->ops)
319         {
320           h->next = calloc(1,sizeof(struct hash_entry));
321           h = h->next;
322         }
323       h->ops = s;
324       h->mask = s->mask;
325       h->opcode = s->opcode;
326     }
327 }
328
329
330 void
331 sim_close (quitting)
332      int quitting;
333 {
334   /* nothing to do */
335 }
336
337 void
338 sim_set_profile (n)
339      int n;
340 {
341   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile %d\n", n);
342 }
343
344 void
345 sim_set_profile_size (n)
346      int n;
347 {
348   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_set_profile_size %d\n", n);
349 }
350
351 void
352 sim_resume (step, siggnal)
353      int step, siggnal;
354 {
355   uint32 inst, opcode;
356   reg_t oldpc;
357   struct hash_entry *h;
358
359   if (step)
360     State.exception = SIGTRAP;
361   else
362     State.exception = 0;
363
364   do
365     {
366       unsigned long insn, extension;
367
368       /* Fetch the current instruction.  */
369       inst = load_mem_big (PC, 1);
370       oldpc = PC;
371
372       /* These are one byte insns.  */
373       if ((inst & 0xf3) == 0x00
374           || (inst & 0xf0) == 0x10
375           || (inst & 0xfc) == 0x3c
376           || (inst & 0xf3) == 0x41
377           || (inst & 0xf3) == 0x40
378           || (inst & 0xfc) == 0x50
379           || (inst & 0xfc) == 0x54
380           || (inst & 0xf0) == 0x60
381           || (inst & 0xf0) == 0x70
382           || ((inst & 0xf0) == 0x80
383               && (inst & 0x0c) >> 2 != (inst & 0x03))
384           || ((inst & 0xf0) == 0x90
385               && (inst & 0x0c) >> 2 != (inst & 0x03))
386           || ((inst & 0xf0) == 0xa0
387               && (inst & 0x0c) >> 2 != (inst & 0x03))
388           || ((inst & 0xf0) == 0xb0
389               && (inst & 0x0c) >> 2 != (inst & 0x03))
390           || (inst & 0xff) == 0xcb
391           || (inst & 0xfc) == 0xd0
392           || (inst & 0xfc) == 0xd4
393           || (inst & 0xfc) == 0xd8
394           || (inst & 0xf0) == 0xe0)
395         {
396           insn = inst;
397           h = lookup_hash (insn, 1);
398           extension = 0;
399           (h->ops->func)(insn, extension);
400           PC += 1;
401         }
402
403       /* These are two byte insns.  */
404       else if ((inst & 0xf0) == 0x80
405                || (inst & 0xf0) == 0x90
406                || (inst & 0xf0) == 0xa0
407                || (inst & 0xf0) == 0xb0
408                || (inst & 0xfc) == 0x20
409                || (inst & 0xfc) == 0x28
410                || (inst & 0xf3) == 0x43
411                || (inst & 0xf3) == 0x42
412                || (inst & 0xfc) == 0x58
413                || (inst & 0xfc) == 0x5c
414                || ((inst & 0xf0) == 0xc0
415                    && (inst & 0xff) != 0xcb
416                    && (inst & 0xff) != 0xcc
417                    && (inst & 0xff) != 0xcd)
418                || (inst & 0xff) == 0xf0
419                || (inst & 0xff) == 0xf1
420                || (inst & 0xff) == 0xf2
421                || (inst & 0xff) == 0xf3
422                || (inst & 0xff) == 0xf4
423                || (inst & 0xff) == 0xf5
424                || (inst & 0xff) == 0xf6)
425         {
426           insn = load_mem_big (PC, 2);
427           h = lookup_hash (insn, 2);
428           extension = 0;
429           (h->ops->func)(insn, extension);
430           PC += 2;
431         }
432
433       /* These are three byte insns.  */
434       else if ((inst & 0xff) == 0xf8
435                || (inst & 0xff) == 0xcc 
436                || (inst & 0xff) == 0xf9
437                || (inst & 0xf3) == 0x01
438                || (inst & 0xf3) == 0x02
439                || (inst & 0xf3) == 0x03
440                || (inst & 0xfc) == 0x24
441                || (inst & 0xfc) == 0x2c
442                || (inst & 0xfc) == 0x30
443                || (inst & 0xfc) == 0x34
444                || (inst & 0xfc) == 0x38
445                || (inst & 0xff) == 0xde
446                || (inst & 0xff) == 0xdf
447                || (inst & 0xff) == 0xcc)
448         {
449           insn = load_mem_big (PC, 3);
450           h = lookup_hash (insn, 3);
451           extension = 0;
452           (h->ops->func)(insn, extension);
453           PC += 3;
454         }
455
456       /* These are four byte insns.  */
457       else if ((inst & 0xff) == 0xfa
458                || (inst & 0xff) == 0xfb)
459         {
460           insn = load_mem_big (PC, 4);
461           h = lookup_hash (insn, 4);
462           extension = 0;
463           (h->ops->func)();
464           PC += 4;
465         }
466
467       /* These are five byte insns.  */
468       else if ((inst & 0xff) == 0xcd
469                || (inst & 0xff) == 0xdc)
470         {
471           insn = load_mem_big (PC, 4);
472           h = lookup_hash (insn, 5);
473           extension = load_mem_big (PC + 4, 1);
474           (h->ops->func)(insn, extension);
475           PC += 5;
476         }
477
478       /* These are six byte insns.  */
479       else if ((inst & 0xff) == 0xfd
480                || (inst & 0xff) == 0xfc)
481         {
482           insn = load_mem_big (PC, 4);
483           h = lookup_hash (insn, 6);
484           extension = load_mem_big (PC + 4, 2);
485           (h->ops->func)(insn, extension);
486           PC += 6;
487         }
488
489       /* Else its a seven byte insns (in theory).  */
490       else
491         {
492           insn = load_mem_big (PC, 4);
493           h = lookup_hash (insn, 7);
494           extension = load_mem_big (PC + 4, 3);
495           (h->ops->func)(insn, extension);
496           PC += 7;
497         }
498     }
499   while (!State.exception);
500 }
501
502 int
503 sim_trace ()
504 {
505 #ifdef DEBUG
506   mn10300_debug = DEBUG;
507 #endif
508   sim_resume (0, 0);
509   return 1;
510 }
511
512 void
513 sim_info (verbose)
514      int verbose;
515 {
516   (*mn10300_callback->printf_filtered) (mn10300_callback, "sim_info\n");
517 }
518
519 void
520 sim_create_inferior (start_address, argv, env)
521      SIM_ADDR start_address;
522      char **argv;
523      char **env;
524 {
525   PC = start_address;
526 }
527
528 void
529 sim_kill ()
530 {
531   /* nothing to do */
532 }
533
534 void
535 sim_set_callbacks (p)
536      host_callback *p;
537 {
538   mn10300_callback = p;
539 }
540
541 /* All the code for exiting, signals, etc needs to be revamped.
542
543    This is enough to get c-torture limping though.  */
544
545 void
546 sim_stop_reason (reason, sigrc)
547      enum sim_stop *reason;
548      int *sigrc;
549 {
550   *reason = sim_stopped;
551   if (State.exception == SIGQUIT)
552     *sigrc = 0;
553   else
554     *sigrc = State.exception;
555 }
556
557 void
558 sim_fetch_register (rn, memory)
559      int rn;
560      unsigned char *memory;
561 {
562   put_word (memory, State.regs[rn]);
563 }
564  
565 void
566 sim_store_register (rn, memory)
567      int rn;
568      unsigned char *memory;
569 {
570   State.regs[rn] = get_word (memory);
571 }
572
573 int
574 sim_read (addr, buffer, size)
575      SIM_ADDR addr;
576      unsigned char *buffer;
577      int size;
578 {
579   int i;
580   for (i = 0; i < size; i++)
581     buffer[i] = load_mem (addr + i, 1);
582
583   return size;
584
585
586 void
587 sim_do_command (cmd)
588      char *cmd;
589 {
590   (*mn10300_callback->printf_filtered) (mn10300_callback, "\"%s\" is not a valid mn10300 simulator command.\n", cmd);
591 }
592
593 int
594 sim_load (prog, from_tty)
595      char *prog;
596      int from_tty;
597 {
598   /* Return nonzero so GDB will handle it.  */
599   return 1;
600