* compile.c (sim_resume): Correctly handle divu.
[external/binutils.git] / sim / h8300 / compile.c
1 /*
2  * Simulator for the Hitachi H8/300 architecture.
3  *
4  * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
5  *
6  * This file is part of H8/300 sim
7  *
8  *
9  * THIS SOFTWARE IS NOT COPYRIGHTED
10  *
11  * Cygnus offers the following for use in the public domain.  Cygnus makes no
12  * warranty with regard to the software or its performance and the user
13  * accepts the software "AS IS" with all faults.
14  *
15  * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
16  * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #include "config.h"
21
22 #include <signal.h>
23 #ifdef HAVE_TIME_H
24 #include <time.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #include <sys/param.h>
30 #include "wait.h"
31 #include "ansidecl.h"
32 #include "callback.h"
33 #include "remote-sim.h"
34 #include "bfd.h"
35
36 int debug;
37
38
39 #define X(op, size)  op*4+size
40
41 #define SP (h8300hmode ? SL:SW)
42 #define SB 0
43 #define SW 1
44 #define SL 2
45 #define OP_REG 1
46 #define OP_DEC 2
47 #define OP_DISP 3
48 #define OP_INC 4
49 #define OP_PCREL 5
50 #define OP_MEM 6
51 #define OP_CCR 7
52 #define OP_IMM 8
53 #define OP_ABS 10
54 #define h8_opcodes ops
55 #define DEFINE_TABLE
56 #include "opcode/h8300.h"
57
58 #include "inst.h"
59
60 #define LOW_BYTE(x) ((x) & 0xff)
61 #define HIGH_BYTE(x) (((x)>>8) & 0xff)
62 #define P(X,Y) ((X<<8) | Y)
63
64 #define BUILDSR()   cpu.ccr = (N << 3) | (Z << 2) | (V<<1) | C;
65
66 #define GETSR()             \
67   c = (cpu.ccr >> 0) & 1;\
68   v = (cpu.ccr >> 1) & 1;\
69   nz = !((cpu.ccr >> 2) & 1);\
70   n = (cpu.ccr >> 3) & 1;
71
72 #ifdef __CHAR_IS_SIGNED__
73 #define SEXTCHAR(x) ((char)(x))
74 #endif
75
76 #ifndef SEXTCHAR
77 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff):x)
78 #endif
79
80 #define UEXTCHAR(x) ((x) & 0xff)
81 #define UEXTSHORT(x) ((x) & 0xffff)
82 #define SEXTSHORT(x) ((short)(x))
83
84 static cpu_state_type cpu;
85
86 int h8300hmode = 0;
87
88 static int memory_size;
89
90
91 static int
92 get_now ()
93 {
94 #ifndef WIN32
95   return time (0);
96 #endif
97   return 0;
98 }
99
100 static int
101 now_persec ()
102 {
103   return 1;
104 }
105
106
107 static int
108 bitfrom (x)
109 {
110   switch (x & SIZE)
111     {
112     case L_8:
113       return SB;
114     case L_16:
115       return SW;
116     case L_32:
117       return SL;
118     case L_P:
119       return h8300hmode ? SL : SW;
120     }
121 }
122
123 static
124 unsigned int
125 lvalue (x, rn)
126 {
127   switch (x / 4)
128     {
129     case OP_DISP:
130       if (rn == 8)
131         {
132           return X (OP_IMM, SP);
133         }
134       return X (OP_REG, SP);
135
136     case OP_MEM:
137
138       return X (OP_MEM, SP);
139     default:
140       abort ();
141     }
142 }
143
144 static unsigned int
145 decode (addr, data, dst)
146      int addr;
147      unsigned char *data;
148      decoded_inst *dst;
149
150 {
151   int rs = 0;
152   int rd = 0;
153   int rdisp = 0;
154   int abs = 0;
155   int plen = 0;
156   int bit = 0;
157
158   struct h8_opcode *q = h8_opcodes;
159   int size = 0;
160   dst->dst.type = -1;
161   dst->src.type = -1;
162   /* Find the exact opcode/arg combo */
163   while (q->name)
164     {
165       op_type *nib;
166       unsigned int len = 0;
167
168       nib = q->data.nib;
169
170       while (1)
171         {
172           op_type looking_for = *nib;
173           int thisnib = data[len >> 1];
174
175           thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
176
177           if (looking_for < 16 && looking_for >= 0)
178             {
179               if (looking_for != thisnib)
180                 goto fail;
181             }
182           else
183             {
184               if ((int) looking_for & (int) B31)
185                 {
186                   if (!(((int) thisnib & 0x8) != 0))
187                     goto fail;
188                   looking_for = (op_type) ((int) looking_for & ~(int)
189                                            B31);
190                   thisnib &= 0x7;
191                 }
192               if ((int) looking_for & (int) B30)
193                 {
194                   if (!(((int) thisnib & 0x8) == 0))
195                     goto fail;
196                   looking_for = (op_type) ((int) looking_for & ~(int) B30);
197                 }
198               if (looking_for & DBIT)
199                 {
200                   if ((looking_for & 5) != (thisnib & 5))
201                     goto fail;
202                   abs = (thisnib & 0x8) ? 2 : 1;
203                 }
204               else if (looking_for & (REG | IND | INC | DEC))
205                 {
206                   if (looking_for & REG)
207                     {
208                       /*
209                        * Can work out size from the
210                        * register
211                        */
212                       size = bitfrom (looking_for);
213                     }
214                   if (looking_for & SRC)
215                     {
216                       rs = thisnib;
217                     }
218                   else
219                     {
220                       rd = thisnib;
221                     }
222                 }
223               else if (looking_for & L_16)
224                 {
225                   abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
226                   plen = 16;
227                   if (looking_for & (PCREL | DISP))
228                     {
229                       abs = (short) (abs);
230                     }
231                 }
232               else if (looking_for & ABSJMP)
233                 {
234                   abs =
235                     (data[1] << 16)
236                     | (data[2] << 8)
237                     | (data[3]);
238                 }
239               else if (looking_for & MEMIND)
240                 {
241                   abs = data[1];
242                 }
243               else if (looking_for & L_32)
244                 {
245                   int i = len >> 1;
246                   abs = (data[i] << 24)
247                     | (data[i + 1] << 16)
248                     | (data[i + 2] << 8)
249                     | (data[i + 3]);
250
251                   plen = 32;
252                 }
253               else if (looking_for & L_24)
254                 {
255                   int i = len >> 1;
256                   abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
257                   plen = 24;
258                 }
259               else if (looking_for & IGNORE)
260                 {
261                   /* nothing to do */
262                 }
263               else if (looking_for & DISPREG)
264                 {
265                   rdisp = thisnib & 0x7;
266                 }
267               else if (looking_for & KBIT)
268                 {
269                   switch (thisnib)
270                     {
271                     case 9:
272                       abs = 4;
273                       break;
274                     case 8:
275                       abs = 2;
276                       break;
277                     case 0:
278                       abs = 1;
279                       break;
280                     }
281                 }
282               else if (looking_for & L_8)
283                 {
284                   plen = 8;
285
286                   if (looking_for & PCREL)
287                     {
288                       abs = SEXTCHAR (data[len >> 1]);
289                     }
290                   else if (looking_for & ABS8MEM)
291                     {
292                       plen = 8;
293                       abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff;
294                       abs |= data[len >> 1] & 0xff ;
295                     }
296                    else
297                     {
298                       abs = data[len >> 1] & 0xff;
299                     }
300                 }
301               else if (looking_for & L_3)
302                 {
303                   plen = 3;
304
305                   bit = thisnib;
306                 }
307               else if (looking_for == E)
308                 {
309                   dst->op = q;
310
311                   /* Fill in the args */
312                   {
313                     op_type *args = q->args.nib;
314                     int hadone = 0;
315
316                     while (*args != E)
317                       {
318                         int x = *args;
319                         int rn = (x & DST) ? rd : rs;
320                         ea_type *p;
321
322                         if (x & DST)
323                           {
324                             p = &(dst->dst);
325                           }
326                         else
327                           {
328                             p = &(dst->src);
329                           }
330
331                         if (x & (L_3))
332                           {
333                             p->type = X (OP_IMM, size);
334                             p->literal = bit;
335                           }
336                         else if (x & (IMM | KBIT | DBIT))
337                           {
338                             p->type = X (OP_IMM, size);
339                             p->literal = abs;
340                           }
341                         else if (x & REG)
342                           {
343                             /* Reset the size, some
344                                ops (like mul) have two sizes */
345
346                             size = bitfrom (x);
347                             p->type = X (OP_REG, size);
348                             p->reg = rn;
349                           }
350                         else if (x & INC)
351                           {
352                             p->type = X (OP_INC, size);
353                             p->reg = rn & 0x7;
354                           }
355                         else if (x & DEC)
356                           {
357                             p->type = X (OP_DEC, size);
358                             p->reg = rn & 0x7;
359                           }
360                         else if (x & IND)
361                           {
362                             p->type = X (OP_DISP, size);
363                             p->reg = rn & 0x7;
364                             p->literal = 0;
365                           }
366                         else if (x & (ABS | ABSJMP | ABS8MEM))
367                           {
368                             p->type = X (OP_DISP, size);
369                             p->literal = abs;
370                             p->reg = 8;
371                           }
372                         else if (x & MEMIND)
373                           {
374                             p->type = X (OP_MEM, size);
375                             p->literal = abs;
376                           }
377                         else if (x & PCREL)
378                           {
379                             p->type = X (OP_PCREL, size);
380                             p->literal = abs + addr + 2;
381                             if (x & L_16)
382                               p->literal += 2;
383                           }
384                         else if (x & ABSJMP)
385                           {
386                             p->type = X (OP_IMM, SP);
387                             p->literal = abs;
388                           }
389                         else if (x & DISP)
390                           {
391                             p->type = X (OP_DISP, size);
392                             p->literal = abs;
393                             p->reg = rdisp & 0x7;
394                           }
395                         else if (x & CCR)
396                           {
397                             p->type = OP_CCR;
398                           }
399                         else
400                           printf ("Hmmmm %x", x);
401
402                         args++;
403                       }
404                   }
405
406                   /*
407                      * But a jmp or a jsr gets
408                      * automagically lvalued, since we
409                      * branch to their address not their
410                      * contents
411                    */
412                   if (q->how == O (O_JSR, SB)
413                       || q->how == O (O_JMP, SB))
414                     {
415                       dst->src.type = lvalue (dst->src.type, dst->src.reg);
416                     }
417
418                   if (dst->dst.type == -1)
419                     dst->dst = dst->src;
420
421                   dst->opcode = q->how;
422                   dst->cycles = q->time;
423
424                   /* And a jsr to 0xc4 is turned into a magic trap */
425
426                   if (dst->opcode == O (O_JSR, SB))
427                     {
428                       if (dst->src.literal == 0xc4)
429                         {
430                           dst->opcode = O (O_SYSCALL, SB);
431                         }
432                     }
433
434                   dst->next_pc = addr + len / 2;
435                   return;
436                 }
437               else
438                 {
439                   printf ("Dont understand %x \n", looking_for);
440                 }
441             }
442
443           len++;
444           nib++;
445         }
446
447     fail:
448       q++;
449     }
450
451   dst->opcode = O (O_ILL, SB);
452 }
453
454
455 static void
456 compile (pc)
457 {
458   int idx;
459
460   /* find the next cache entry to use */
461
462   idx = cpu.cache_top + 1;
463   cpu.compiles++;
464   if (idx >= cpu.csize)
465     {
466       idx = 1;
467     }
468   cpu.cache_top = idx;
469
470   /* Throw away its old meaning */
471   cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
472
473   /* set to new address */
474   cpu.cache[idx].oldpc = pc;
475
476   /* fill in instruction info */
477   decode (pc, cpu.memory + pc, cpu.cache + idx);
478
479   /* point to new cache entry */
480   cpu.cache_idx[pc] = idx;
481 }
482
483
484 static unsigned char *breg[18];
485 static unsigned short *wreg[18];
486 static unsigned int *lreg[18];
487
488 #define GET_B_REG(x) *(breg[x])
489 #define SET_B_REG(x,y) (*(breg[x])) = (y)
490 #define GET_W_REG(x) *(wreg[x])
491 #define SET_W_REG(x,y) (*(wreg[x])) = (y)
492
493 #define GET_L_REG(x) *(lreg[x])
494 #define SET_L_REG(x,y) (*(lreg[x])) = (y)
495
496 #define GET_MEMORY_L(x) \
497   (x < memory_size \
498    ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
499       | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
500    : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
501       | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
502
503 #define GET_MEMORY_W(x) \
504   (x < memory_size \
505    ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
506    : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
507
508
509 #define GET_MEMORY_B(x) \
510   (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
511
512 #define SET_MEMORY_L(x,y)  \
513 {  register unsigned char *_p; register int __y = y; \
514    _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
515    _p[0] = (__y)>>24; _p[1] = (__y)>>16; \
516    _p[2] = (__y)>>8; _p[3] = (__y)>>0;}
517
518 #define SET_MEMORY_W(x,y) \
519 {  register unsigned char *_p; register int __y = y; \
520    _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
521    _p[0] = (__y)>>8; _p[1] =(__y);}
522
523 #define SET_MEMORY_B(x,y) \
524   (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
525
526 int
527 fetch (arg, n)
528      ea_type *arg;
529 {
530   int rn = arg->reg;
531   int abs = arg->literal;
532   int r;
533   int t;
534
535   switch (arg->type)
536     {
537     case X (OP_REG, SB):
538       return GET_B_REG (rn);
539     case X (OP_REG, SW):
540       return GET_W_REG (rn);
541     case X (OP_REG, SL):
542       return GET_L_REG (rn);
543     case X (OP_IMM, SB):
544     case X (OP_IMM, SW):
545     case X (OP_IMM, SL):
546       return abs;
547     case X (OP_DEC, SB):
548       abort ();
549
550     case X (OP_INC, SB):
551       t = GET_L_REG (rn);
552       t &= cpu.mask;
553       r = GET_MEMORY_B (t);
554       t++;
555       t = t & cpu.mask;
556       SET_L_REG (rn, t);
557       return r;
558       break;
559     case X (OP_INC, SW):
560       t = GET_L_REG (rn);
561       t &= cpu.mask;
562       r = GET_MEMORY_W (t);
563       t += 2;
564       t = t & cpu.mask;
565       SET_L_REG (rn, t);
566       return r;
567     case X (OP_INC, SL):
568       t = GET_L_REG (rn);
569       t &= cpu.mask;
570       r = GET_MEMORY_L (t);
571
572       t += 4;
573       t = t & cpu.mask;
574       SET_L_REG (rn, t);
575       return r;
576
577     case X (OP_DISP, SB):
578       t = GET_L_REG (rn) + abs;
579       t &= cpu.mask;
580       return GET_MEMORY_B (t);
581
582     case X (OP_DISP, SW):
583       t = GET_L_REG (rn) + abs;
584       t &= cpu.mask;
585       return GET_MEMORY_W (t);
586
587     case X (OP_DISP, SL):
588       t = GET_L_REG (rn) + abs;
589       t &= cpu.mask;
590       return GET_MEMORY_L (t);
591
592     case X (OP_MEM, SL):
593       t = GET_MEMORY_L (abs);
594       t &= cpu.mask;
595       return t;
596
597     case X (OP_MEM, SW):
598       t = GET_MEMORY_W (abs);
599       t &= cpu.mask;
600       return t;
601
602     default:
603       abort ();
604
605     }
606 }
607
608
609 static
610 void
611 store (arg, n)
612      ea_type *arg;
613      int n;
614 {
615   int rn = arg->reg;
616   int abs = arg->literal;
617   int t;
618
619   switch (arg->type)
620     {
621     case X (OP_REG, SB):
622       SET_B_REG (rn, n);
623       break;
624     case X (OP_REG, SW):
625       SET_W_REG (rn, n);
626       break;
627     case X (OP_REG, SL):
628       SET_L_REG (rn, n);
629       break;
630
631     case X (OP_DEC, SB):
632       t = GET_L_REG (rn) - 1;
633       t &= cpu.mask;
634       SET_L_REG (rn, t);
635       SET_MEMORY_B (t, n);
636
637       break;
638     case X (OP_DEC, SW):
639       t = (GET_L_REG (rn) - 2) & cpu.mask;
640       SET_L_REG (rn, t);
641       SET_MEMORY_W (t, n);
642       break;
643
644     case X (OP_DEC, SL):
645       t = (GET_L_REG (rn) - 4) & cpu.mask;
646       SET_L_REG (rn, t);
647       SET_MEMORY_L (t, n);
648       break;
649
650     case X (OP_DISP, SB):
651       t = GET_L_REG (rn) + abs;
652       t &= cpu.mask;
653       SET_MEMORY_B (t, n);
654       break;
655
656     case X (OP_DISP, SW):
657       t = GET_L_REG (rn) + abs;
658       t &= cpu.mask;
659       SET_MEMORY_W (t, n);
660       break;
661
662     case X (OP_DISP, SL):
663       t = GET_L_REG (rn) + abs;
664       t &= cpu.mask;
665       SET_MEMORY_L (t, n);
666       break;
667     default:
668       abort ();
669     }
670 }
671
672
673 static union
674 {
675   short int i;
676   struct
677     {
678       char low;
679       char high;
680     }
681   u;
682 }
683
684 littleendian;
685
686 static
687 void
688 init_pointers ()
689 {
690   static int init;
691
692   if (!init)
693     {
694       int i;
695
696       init = 1;
697       littleendian.i = 1;
698
699       if (h8300hmode)
700         memory_size = H8300H_MSIZE;
701       else
702         memory_size = H8300_MSIZE;
703       cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
704       cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
705       cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
706
707       /* `msize' must be a power of two */
708       if ((memory_size & (memory_size - 1)) != 0)
709         abort ();
710       cpu.mask = memory_size - 1;
711
712       for (i = 0; i < 9; i++)
713         {
714           cpu.regs[i] = 0;
715         }
716
717       for (i = 0; i < 8; i++)
718         {
719           unsigned char *p = (unsigned char *) (cpu.regs + i);
720           unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
721           unsigned short *q = (unsigned short *) (cpu.regs + i);
722           unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
723           cpu.regs[i] = 0x00112233;
724           while (p < e)
725             {
726               if (*p == 0x22)
727                 {
728                   breg[i] = p;
729                 }
730               if (*p == 0x33)
731                 {
732                   breg[i + 8] = p;
733                 }
734               p++;
735             }
736           while (q < u)
737             {
738               if (*q == 0x2233)
739                 {
740                   wreg[i] = q;
741                 }
742               if (*q == 0x0011)
743                 {
744                   wreg[i + 8] = q;
745                 }
746               q++;
747             }
748           cpu.regs[i] = 0;
749           lreg[i] = &cpu.regs[i];
750         }
751
752       lreg[8] = &cpu.regs[8];
753
754       /* initialize the seg registers */
755       if (!cpu.cache)
756         sim_csize (CSIZE);
757     }
758 }
759
760 static void
761 control_c (sig, code, scp, addr)
762      int sig;
763      int code;
764      char *scp;
765      char *addr;
766 {
767   cpu.exception = SIGINT;
768 }
769
770 #define C (c != 0)
771 #define Z (nz == 0)
772 #define V (v != 0)
773 #define N (n != 0)
774
775 static int
776 mop (code, bsize, sign)
777      decoded_inst *code;
778      int bsize;
779      int sign;
780 {
781   int multiplier;
782   int multiplicand;
783   int result;
784   int n, nz;
785
786   if (sign)
787     {
788       multiplicand =
789         bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
790         SEXTSHORT (GET_W_REG (code->dst.reg));
791       multiplier =
792         bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
793         SEXTSHORT (GET_W_REG (code->src.reg));
794     }
795   else
796     {
797       multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
798         UEXTSHORT (GET_W_REG (code->dst.reg));
799       multiplier =
800         bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
801         UEXTSHORT (GET_W_REG (code->src.reg));
802
803     }
804   result = multiplier * multiplicand;
805
806   if (sign)
807     {
808       n = result & (bsize ? 0x8000 : 0x80000000);
809       nz = result & (bsize ? 0xffff : 0xffffffff);
810     }
811   if (bsize)
812     {
813       SET_W_REG (code->dst.reg, result);
814     }
815   else
816     {
817       SET_L_REG (code->dst.reg, result);
818     }
819 /*  return ((n==1) << 1) | (nz==1); */
820
821 }
822
823 #define OSHIFTS(name, how) \
824 case O(name, SB):                               \
825 {                                               \
826   int t;                                        \
827   int hm = 0x80;                                \
828   rd = GET_B_REG (code->src.reg);               \
829   how;                                          \
830   goto shift8;                                  \
831 }                                               \
832 case O(name, SW):                               \
833 {                                               \
834   int t;                                        \
835   int hm = 0x8000;                              \
836   rd = GET_W_REG (code->src.reg);               \
837   how;                                          \
838   goto shift16;                                 \
839 }                                               \
840 case O(name, SL):                               \
841 {                                               \
842   int t;                                        \
843   int hm = 0x80000000;                          \
844   rd = GET_L_REG (code->src.reg);               \
845   how;                                          \
846   goto shift32;                                 \
847 }
848
849 #define OBITOP(name,f, s, op)                   \
850 case  O(name, SB):                              \
851 {                                               \
852   int m;                                        \
853   int b;                                        \
854   if (f) ea = fetch (&code->dst);               \
855   m=1<< fetch(&code->src);                      \
856   op;                                           \
857   if(s) store (&code->dst,ea); goto next;       \
858 }
859
860 void
861 sim_resume (step, siggnal)
862 {
863   static int init1;
864   int cycles = 0;
865   int insts = 0;
866   int tick_start = get_now ();
867   void (*prev) ();
868   int poll_count = 0;
869   int res;
870   int tmp;
871   int rd;
872   int ea;
873   int bit;
874   int pc;
875   int c, nz, v, n;
876   int oldmask;
877   init_pointers ();
878
879   prev = signal (SIGINT, control_c);
880
881   if (step)
882     {
883       cpu.exception = SIGTRAP;
884     }
885   else
886     {
887       cpu.exception = 0;
888     }
889
890   pc = cpu.pc;
891
892   /* The PC should never be odd.  */
893   if (pc & 0x1)
894     abort ();
895
896   GETSR ();
897   oldmask = cpu.mask;
898   if (!h8300hmode)
899     cpu.mask = 0xffff;
900   do
901     {
902       int cidx;
903       decoded_inst *code;
904
905     top:
906       cidx = cpu.cache_idx[pc];
907       code = cpu.cache + cidx;
908
909
910 #define ALUOP(STORE, NAME, HOW) \
911     case O(NAME,SB):  HOW; if(STORE)goto alu8;else goto just_flags_alu8;  \
912     case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
913     case O(NAME,SL):  HOW; if(STORE)goto alu32;else goto just_flags_alu32;
914
915
916 #define LOGOP(NAME, HOW) \
917     case O(NAME,SB): HOW; goto log8;\
918     case O(NAME, SW): HOW; goto log16;\
919     case O(NAME,SL): HOW; goto log32;
920
921
922
923 #if ADEBUG
924       if (debug)
925         {
926           printf ("%x %d %s\n", pc, code->opcode,
927                   code->op ? code->op->name : "**");
928         }
929       cpu.stats[code->opcode]++;
930
931 #endif
932
933       cycles += code->cycles;
934       insts++;
935       switch (code->opcode)
936         {
937         case 0:
938           /*
939            * This opcode is a fake for when we get to an
940            * instruction which hasnt been compiled
941            */
942           compile (pc);
943           goto top;
944           break;
945
946
947         case O (O_SUBX, SB):
948           rd = fetch (&code->dst);
949           ea = fetch (&code->src);
950           ea = -(ea + C);
951           res = rd + ea;
952           goto alu8;
953
954         case O (O_ADDX, SB):
955           rd = fetch (&code->dst);
956           ea = fetch (&code->src);
957           ea = C + ea;
958           res = rd + ea;
959           goto alu8;
960
961 #define EA    ea = fetch(&code->src);
962 #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
963
964           ALUOP (1, O_SUB, RD_EA;
965                  ea = -ea;
966                  res = rd + ea);
967           ALUOP (1, O_NEG, EA;
968                  ea = -ea;
969                  rd = 0;
970                  res = rd + ea);
971
972         case O (O_ADD, SB):
973           rd = GET_B_REG (code->dst.reg);
974           ea = fetch (&code->src);
975           res = rd + ea;
976           goto alu8;
977         case O (O_ADD, SW):
978           rd = GET_W_REG (code->dst.reg);
979           ea = fetch (&code->src);
980           res = rd + ea;
981           goto alu16;
982         case O (O_ADD, SL):
983           rd = GET_L_REG (code->dst.reg);
984           ea = fetch (&code->src);
985           res = rd + ea;
986           goto alu32;
987
988
989           LOGOP (O_AND, RD_EA;
990                  res = rd & ea);
991
992           LOGOP (O_OR, RD_EA;
993                  res = rd | ea);
994
995           LOGOP (O_XOR, RD_EA;
996                  res = rd ^ ea);
997
998
999         case O (O_MOV_TO_MEM, SB):
1000           res = GET_B_REG (code->src.reg);
1001           goto log8;
1002         case O (O_MOV_TO_MEM, SW):
1003           res = GET_W_REG (code->src.reg);
1004           goto log16;
1005         case O (O_MOV_TO_MEM, SL):
1006           res = GET_L_REG (code->src.reg);
1007           goto log32;
1008
1009
1010         case O (O_MOV_TO_REG, SB):
1011           res = fetch (&code->src);
1012           SET_B_REG (code->dst.reg, res);
1013           goto just_flags_log8;
1014         case O (O_MOV_TO_REG, SW):
1015           res = fetch (&code->src);
1016           SET_W_REG (code->dst.reg, res);
1017           goto just_flags_log16;
1018         case O (O_MOV_TO_REG, SL):
1019           res = fetch (&code->src);
1020           SET_L_REG (code->dst.reg, res);
1021           goto just_flags_log32;
1022
1023
1024         case O (O_ADDS, SL):
1025           SET_L_REG (code->dst.reg,
1026                      GET_L_REG (code->dst.reg)
1027                      + code->src.literal);
1028
1029           goto next;
1030
1031         case O (O_SUBS, SL):
1032           SET_L_REG (code->dst.reg,
1033                      GET_L_REG (code->dst.reg)
1034                      - code->src.literal);
1035           goto next;
1036
1037         case O (O_CMP, SB):
1038           rd = fetch (&code->dst);
1039           ea = fetch (&code->src);
1040           ea = -ea;
1041           res = rd + ea;
1042           goto just_flags_alu8;
1043
1044         case O (O_CMP, SW):
1045           rd = fetch (&code->dst);
1046           ea = fetch (&code->src);
1047           ea = -ea;
1048           res = rd + ea;
1049           goto just_flags_alu16;
1050
1051         case O (O_CMP, SL):
1052           rd = fetch (&code->dst);
1053           ea = fetch (&code->src);
1054           ea = -ea;
1055           res = rd + ea;
1056           goto just_flags_alu32;
1057
1058
1059         case O (O_DEC, SB):
1060           rd = GET_B_REG (code->src.reg);
1061           ea = -1;
1062           res = rd + ea;
1063           SET_B_REG (code->src.reg, res);
1064           goto just_flags_inc8;
1065
1066         case O (O_DEC, SW):
1067           rd = GET_W_REG (code->dst.reg);
1068           ea = -code->src.literal;
1069           res = rd + ea;
1070           SET_W_REG (code->dst.reg, res);
1071           goto just_flags_inc16;
1072
1073         case O (O_DEC, SL):
1074           rd = GET_L_REG (code->dst.reg);
1075           ea = -code->src.literal;
1076           res = rd + ea;
1077           SET_L_REG (code->dst.reg, res);
1078           goto just_flags_inc32;
1079
1080
1081         case O (O_INC, SB):
1082           rd = GET_B_REG (code->src.reg);
1083           ea = 1;
1084           res = rd + ea;
1085           SET_B_REG (code->src.reg, res);
1086           goto just_flags_inc8;
1087
1088         case O (O_INC, SW):
1089           rd = GET_W_REG (code->dst.reg);
1090           ea = code->src.literal;
1091           res = rd + ea;
1092           SET_W_REG (code->dst.reg, res);
1093           goto just_flags_inc16;
1094
1095         case O (O_INC, SL):
1096           rd = GET_L_REG (code->dst.reg);
1097           ea = code->src.literal;
1098           res = rd + ea;
1099           SET_L_REG (code->dst.reg, res);
1100           goto just_flags_inc32;
1101
1102
1103 #define GET_CCR(x) BUILDSR();x = cpu.ccr
1104
1105         case O (O_ANDC, SB):
1106           GET_CCR (rd);
1107           ea = code->src.literal;
1108           res = rd & ea;
1109           goto setc;
1110
1111         case O (O_ORC, SB):
1112           GET_CCR (rd);
1113           ea = code->src.literal;
1114           res = rd | ea;
1115           goto setc;
1116
1117         case O (O_XORC, SB):
1118           GET_CCR (rd);
1119           ea = code->src.literal;
1120           res = rd ^ ea;
1121           goto setc;
1122
1123
1124         case O (O_BRA, SB):
1125           if (1)
1126             goto condtrue;
1127           goto next;
1128
1129         case O (O_BRN, SB):
1130           if (0)
1131             goto condtrue;
1132           goto next;
1133
1134         case O (O_BHI, SB):
1135           if ((C || Z) == 0)
1136             goto condtrue;
1137           goto next;
1138
1139
1140         case O (O_BLS, SB):
1141           if ((C || Z))
1142             goto condtrue;
1143           goto next;
1144
1145         case O (O_BCS, SB):
1146           if ((C == 1))
1147             goto condtrue;
1148           goto next;
1149
1150         case O (O_BCC, SB):
1151           if ((C == 0))
1152             goto condtrue;
1153           goto next;
1154
1155         case O (O_BEQ, SB):
1156           if (Z)
1157             goto condtrue;
1158           goto next;
1159         case O (O_BGT, SB):
1160           if (((Z || (N ^ V)) == 0))
1161             goto condtrue;
1162           goto next;
1163
1164
1165         case O (O_BLE, SB):
1166           if (((Z || (N ^ V)) == 1))
1167             goto condtrue;
1168           goto next;
1169
1170         case O (O_BGE, SB):
1171           if ((N ^ V) == 0)
1172             goto condtrue;
1173           goto next;
1174         case O (O_BLT, SB):
1175           if ((N ^ V))
1176             goto condtrue;
1177           goto next;
1178         case O (O_BMI, SB):
1179           if ((N))
1180             goto condtrue;
1181           goto next;
1182         case O (O_BNE, SB):
1183           if ((Z == 0))
1184             goto condtrue;
1185           goto next;
1186
1187         case O (O_BPL, SB):
1188           if (N == 0)
1189             goto condtrue;
1190           goto next;
1191         case O (O_BVC, SB):
1192           if ((V == 0))
1193             goto condtrue;
1194           goto next;
1195         case O (O_BVS, SB):
1196           if ((V == 1))
1197             goto condtrue;
1198           goto next;
1199
1200         case O (O_SYSCALL, SB):
1201           printf ("%c", cpu.regs[2]);
1202           goto next;
1203
1204           OSHIFTS (O_NOT, rd = ~rd; v = 0;);
1205           OSHIFTS (O_SHLL, c = rd & hm; v = 0;
1206                    rd <<= 1);
1207           OSHIFTS (O_SHLR, c = rd & 1; v = 0;
1208                    rd = (unsigned int) rd >> 1);
1209           OSHIFTS (O_SHAL, c = rd & hm;
1210                    v = (rd & hm) != ((rd & (hm >> 1)) << 1);
1211                    rd <<= 1);
1212           OSHIFTS (O_SHAR, t = rd & hm;
1213                    c = rd & 1;
1214                    v = 0;
1215                    rd >>= 1;
1216                    rd |= t;
1217                    );
1218           OSHIFTS (O_ROTL, c = rd & hm;
1219                    v = 0;
1220                    rd <<= 1;
1221                    rd |= C);
1222           OSHIFTS (O_ROTR, c = rd & 1;
1223                    v = 0;
1224                    rd = (unsigned int) rd >> 1;
1225                    if (c) rd |= hm;);
1226           OSHIFTS (O_ROTXL, t = rd & hm;
1227                    rd <<= 1;
1228                    rd |= C;
1229                    c = t;
1230                    v = 0;
1231                    );
1232           OSHIFTS (O_ROTXR, t = rd & 1;
1233                    rd = (unsigned int) rd >> 1;
1234                    if (C) rd |= hm; c = t;
1235                    v = 0;);
1236
1237         case O (O_JMP, SB):
1238           {
1239             pc = fetch (&code->src);
1240             goto end;
1241
1242           }
1243
1244         case O (O_JSR, SB):
1245           {
1246             int tmp;
1247             pc = fetch (&code->src);
1248           call:
1249             tmp = cpu.regs[7];
1250
1251             if (h8300hmode)
1252               {
1253                 tmp -= 4;
1254                 SET_MEMORY_L (tmp, code->next_pc);
1255               }
1256             else
1257               {
1258                 tmp -= 2;
1259                 SET_MEMORY_W (tmp, code->next_pc);
1260               }
1261             cpu.regs[7] = tmp;
1262
1263             goto end;
1264           }
1265         case O (O_BSR, SB):
1266           pc = code->src.literal;
1267           goto call;
1268
1269         case O (O_RTS, SB):
1270           {
1271             int tmp;
1272
1273             tmp = cpu.regs[7];
1274
1275             if (h8300hmode)
1276               {
1277                 pc = GET_MEMORY_L (tmp);
1278                 tmp += 4;
1279               }
1280             else
1281               {
1282                 pc = GET_MEMORY_W (tmp);
1283                 tmp += 2;
1284               }
1285
1286             cpu.regs[7] = tmp;
1287             goto end;
1288           }
1289
1290         case O (O_ILL, SB):
1291           cpu.exception = SIGILL;
1292           goto end;
1293         case O (O_SLEEP, SB):
1294           /* The format of r0 is defined by devo/include/wait.h.
1295              cpu.exception handling needs some cleanup: we need to make the
1296              the handling of normal exits vs signals, etc. more sensible.  */
1297           if (! WIFEXITED (cpu.regs[0]) && WIFSIGNALED (cpu.regs[0]))
1298             cpu.exception = SIGILL;
1299           else
1300             cpu.exception = SIGTRAP;
1301           goto end;
1302         case O (O_BPT, SB):
1303           cpu.exception = SIGTRAP;
1304           goto end;
1305
1306           OBITOP (O_BNOT, 1, 1, ea ^= m);
1307           OBITOP (O_BTST, 1, 0, nz = ea & m);
1308           OBITOP (O_BCLR, 1, 1, ea &= ~m);
1309           OBITOP (O_BSET, 1, 1, ea |= m);       
1310           OBITOP (O_BLD, 1, 0, c = ea & m);
1311           OBITOP (O_BILD, 1, 0, c = !(ea & m));
1312           OBITOP (O_BST, 1, 1, ea &= ~m;
1313                   if (C) ea |= m);
1314           OBITOP (O_BIST, 1, 1, ea &= ~m;
1315                   if (!C) ea |= m);
1316           OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
1317           OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
1318           OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
1319           OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
1320           OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
1321           OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
1322
1323
1324 #define MOP(bsize, signed) mop(code, bsize,signed); goto next;
1325
1326         case O (O_MULS, SB):
1327           MOP (1, 1);
1328           break;
1329         case O (O_MULS, SW):
1330           MOP (0, 1);
1331           break;
1332         case O (O_MULU, SB):
1333           MOP (1, 0);
1334           break;
1335         case O (O_MULU, SW):
1336           MOP (0, 0);
1337           break;
1338
1339
1340         case O (O_DIVU, SB):
1341           {
1342             rd = GET_W_REG (code->dst.reg);
1343             ea = GET_B_REG (code->src.reg);
1344             if (ea)
1345               {
1346                 tmp = (unsigned)rd % ea;
1347                 rd = (unsigned)rd / ea;
1348               }
1349             SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1350             n = ea & 0x80;
1351             nz = ea & 0xff;
1352
1353             goto next;
1354           }
1355         case O (O_DIVU, SW):
1356           {
1357             rd = GET_L_REG (code->dst.reg);
1358             ea = GET_W_REG (code->src.reg);
1359             n = ea & 0x8000;
1360             nz = ea & 0xffff;
1361             if (ea)
1362               {
1363                 tmp = (unsigned)rd % ea;
1364                 rd = (unsigned)rd / ea;
1365               }
1366             SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1367             goto next;
1368           }
1369
1370         case O (O_DIVS, SB):
1371           {
1372
1373             rd = SEXTSHORT (GET_W_REG (code->dst.reg));
1374             ea = SEXTCHAR (GET_B_REG (code->src.reg));
1375             if (ea)
1376               {
1377                 tmp = (int) rd % (int) ea;
1378                 rd = (int) rd / (int) ea;
1379                 n = rd & 0x8000;
1380                 nz = 1;
1381               }
1382             else
1383               nz = 0;
1384             SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1385             goto next;
1386           }
1387         case O (O_DIVS, SW):
1388           {
1389             rd = GET_L_REG (code->dst.reg);
1390             ea = SEXTSHORT (GET_W_REG (code->src.reg));
1391             if (ea)
1392               {
1393                 tmp = (int) rd % (int) ea;
1394                 rd = (int) rd / (int) ea;
1395                 n = rd & 0x80000000;
1396                 nz = 1;
1397               }
1398             else
1399               nz = 0;
1400             SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1401             goto next;
1402           }
1403         case O (O_EXTS, SW):
1404           rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst.  */
1405           ea = rd & 0x80 ? -256 : 0;
1406           res = rd + ea;
1407           goto log16;
1408         case O (O_EXTS, SL):
1409           rd = GET_W_REG (code->src.reg) & 0xffff;
1410           ea = rd & 0x8000 ? -65536 : 0;
1411           res = rd + ea;
1412           goto log32;
1413         case O (O_EXTU, SW):
1414           rd = GET_B_REG (code->src.reg + 8) & 0xff;
1415           ea = 0;
1416           res = rd + ea;
1417           goto log16;
1418         case O (O_EXTU, SL):
1419           rd = GET_W_REG (code->src.reg) & 0xffff;
1420           ea = 0;
1421           res = rd + ea;
1422           goto log32;
1423
1424         case O (O_NOP, SB):
1425           goto next;
1426
1427         default:
1428           cpu.exception = SIGILL;
1429           goto end;
1430
1431         }
1432       abort ();
1433
1434     setc:
1435       cpu.ccr = res;
1436       GETSR ();
1437       goto next;
1438
1439     condtrue:
1440       /* When a branch works */
1441       pc = code->src.literal;
1442       goto end;
1443
1444       /* Set the cond codes from res */
1445     bitop:
1446
1447       /* Set the flags after an 8 bit inc/dec operation */
1448     just_flags_inc8:
1449       n = res & 0x80;
1450       nz = res & 0xff;
1451       v = (rd & 0x7f) == 0x7f;
1452       goto next;
1453
1454
1455       /* Set the flags after an 16 bit inc/dec operation */
1456     just_flags_inc16:
1457       n = res & 0x8000;
1458       nz = res & 0xffff;
1459       v = (rd & 0x7fff) == 0x7fff;
1460       goto next;
1461
1462
1463       /* Set the flags after an 32 bit inc/dec operation */
1464     just_flags_inc32:
1465       n = res & 0x80000000;
1466       nz = res & 0xffffffff;
1467       v = (rd & 0x7fffffff) == 0x7fffffff;
1468       goto next;
1469
1470
1471     shift8:
1472       /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1473       n = (rd & 0x80);
1474       nz = rd & 0xff;
1475       SET_B_REG (code->src.reg, rd);
1476       goto next;
1477
1478     shift16:
1479       /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1480       n = (rd & 0x8000);
1481       nz = rd & 0xffff;
1482       SET_W_REG (code->src.reg, rd);
1483       goto next;
1484
1485     shift32:
1486       /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1487       n = (rd & 0x80000000);
1488       nz = rd & 0xffffffff;
1489       SET_L_REG (code->src.reg, rd);
1490       goto next;
1491
1492     log32:
1493       store (&code->dst, res);
1494     just_flags_log32:
1495       /* flags after a 32bit logical operation */
1496       n = res & 0x80000000;
1497       nz = res & 0xffffffff;
1498       v = 0;
1499       goto next;
1500
1501     log16:
1502       store (&code->dst, res);
1503     just_flags_log16:
1504       /* flags after a 16bit logical operation */
1505       n = res & 0x8000;
1506       nz = res & 0xffff;
1507       v = 0;
1508       goto next;
1509
1510
1511     log8:
1512       store (&code->dst, res);
1513     just_flags_log8:
1514       n = res & 0x80;
1515       nz = res & 0xff;
1516       v = 0;
1517       goto next;
1518
1519     alu8:
1520       SET_B_REG (code->dst.reg, res);
1521     just_flags_alu8:
1522       n = res & 0x80;
1523       nz = res & 0xff;
1524       c = (res & 0x100);
1525       switch (code->opcode / 4)
1526         {
1527         case O_ADD:
1528           v = ((rd & 0x80) == (ea & 0x80)
1529                && (rd & 0x80) != (res & 0x80));
1530           break;
1531         case O_SUB:
1532         case O_CMP:
1533           v = ((rd & 0x80) != (-ea & 0x80)
1534                && (rd & 0x80) != (res & 0x80));
1535           break;
1536         case O_NEG:
1537           v = (rd == 0x80);
1538           break;
1539         }
1540       goto next;
1541
1542     alu16:
1543       SET_W_REG (code->dst.reg, res);
1544     just_flags_alu16:
1545       n = res & 0x8000;
1546       nz = res & 0xffff;
1547       c = (res & 0x10000);
1548       switch (code->opcode / 4)
1549         {
1550         case O_ADD:
1551           v = ((rd & 0x8000) == (ea & 0x8000)
1552                && (rd & 0x8000) != (res & 0x8000));
1553           break;
1554         case O_SUB:
1555         case O_CMP:
1556           v = ((rd & 0x8000) != (-ea & 0x8000)
1557                && (rd & 0x8000) != (res & 0x8000));
1558           break;
1559         case O_NEG:
1560           v = (rd == 0x8000);
1561           break;
1562         }
1563       goto next;
1564
1565     alu32:
1566       SET_L_REG (code->dst.reg, res);
1567     just_flags_alu32:
1568       n = res & 0x80000000;
1569       nz = res & 0xffffffff;
1570       switch (code->opcode / 4)
1571         {
1572         case O_ADD:
1573           v = ((rd & 0x80000000) == (ea & 0x80000000)
1574                && (rd & 0x80000000) != (res & 0x80000000));
1575           c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
1576           break;
1577         case O_SUB:
1578         case O_CMP:
1579           v = ((rd & 0x80000000) != (-ea & 0x80000000)
1580                && (rd & 0x80000000) != (res & 0x80000000));
1581           c = (unsigned) rd < (unsigned) -ea;
1582           break;
1583         case O_NEG:
1584           v = (rd == 0x80000000);
1585           c = res != 0;
1586           break;
1587         }
1588       goto next;
1589
1590     next:;
1591       pc = code->next_pc;
1592
1593     end:
1594       ;
1595       /*      if (cpu.regs[8] ) abort(); */
1596
1597 #if defined (WIN32)
1598       /* Poll after every 100th insn, */
1599       if (poll_count++ > 100)
1600         {
1601           poll_count = 0;
1602           if (win32pollquit())
1603             {
1604               control_c();
1605             }
1606         }
1607 #endif
1608 #if defined(__GO32__)
1609       /* Poll after every 100th insn, */
1610       if (poll_count++ > 100)
1611         {
1612           poll_count = 0;
1613           if (kbhit ())
1614             {
1615               int c = getkey ();
1616               control_c ();
1617             }
1618         }
1619 #endif
1620
1621     }
1622   while (!cpu.exception);
1623   cpu.ticks += get_now () - tick_start;
1624   cpu.cycles += cycles;
1625   cpu.insts += insts;
1626   
1627   cpu.pc = pc;
1628   BUILDSR ();
1629   cpu.mask = oldmask;
1630   signal (SIGINT, prev);
1631 }
1632
1633
1634 int
1635 sim_write (addr, buffer, size)
1636      SIM_ADDR addr;
1637      unsigned char *buffer;
1638      int size;
1639 {
1640   int i;
1641
1642   init_pointers ();
1643   if (addr < 0)
1644     return 0;
1645   for (i = 0; i < size; i++)
1646     {
1647       if (addr < memory_size)
1648         {
1649           cpu.memory[addr + i] = buffer[i];
1650           cpu.cache_idx[addr + i] = 0;
1651         }
1652       else
1653         cpu.eightbit[(addr + i) & 0xff] = buffer[i];
1654     }
1655   return size;
1656 }
1657
1658 int
1659 sim_read (addr, buffer, size)
1660      SIM_ADDR addr;
1661      unsigned char *buffer;
1662      int size;
1663 {
1664   init_pointers ();
1665   if (addr < 0)
1666     return 0;
1667   if (addr < memory_size)
1668     memcpy (buffer, cpu.memory + addr, size);
1669   else
1670     memcpy (buffer, cpu.eightbit + (addr & 0xff), size);
1671   return size;
1672 }
1673
1674
1675 #define R0_REGNUM       0
1676 #define R1_REGNUM       1
1677 #define R2_REGNUM       2
1678 #define R3_REGNUM       3
1679 #define R4_REGNUM       4
1680 #define R5_REGNUM       5
1681 #define R6_REGNUM       6
1682 #define R7_REGNUM       7
1683
1684 #define SP_REGNUM       R7_REGNUM       /* Contains address of top of stack */
1685 #define FP_REGNUM       R6_REGNUM       /* Contains address of executing
1686                                            * stack frame */
1687
1688 #define CCR_REGNUM      8       /* Contains processor status */
1689 #define PC_REGNUM       9       /* Contains program counter */
1690
1691 #define CYCLE_REGNUM    10
1692 #define INST_REGNUM     11
1693 #define TICK_REGNUM     12
1694
1695
1696 void
1697 sim_store_register (rn, value)
1698      int rn;
1699      unsigned char *value;
1700 {
1701   int longval;
1702   int shortval;
1703   int intval;
1704   longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
1705   shortval = (value[0] << 8) | (value[1]);
1706   intval = h8300hmode ? longval : shortval;
1707
1708   init_pointers ();
1709   switch (rn)
1710     {
1711     case PC_REGNUM:
1712       cpu.pc = intval;
1713       break;
1714     default:
1715       abort ();
1716     case R0_REGNUM:
1717     case R1_REGNUM:
1718     case R2_REGNUM:
1719     case R3_REGNUM:
1720     case R4_REGNUM:
1721     case R5_REGNUM:
1722     case R6_REGNUM:
1723     case R7_REGNUM:
1724       cpu.regs[rn] = intval;
1725       break;
1726     case CCR_REGNUM:
1727       cpu.ccr = intval;
1728       break;
1729     case CYCLE_REGNUM:
1730       cpu.cycles = longval;
1731       break;
1732
1733     case INST_REGNUM:
1734       cpu.insts = longval;
1735       break;
1736
1737     case TICK_REGNUM:
1738       cpu.ticks = longval;
1739       break;
1740     }
1741 }
1742
1743 void
1744 sim_fetch_register (rn, buf)
1745      int rn;
1746      unsigned char *buf;
1747 {
1748   int v;
1749   int longreg = 0;
1750
1751   init_pointers ();
1752
1753   switch (rn)
1754     {
1755     default:
1756       abort ();
1757     case 8:
1758       v = cpu.ccr;
1759       break;
1760     case 9:
1761       v = cpu.pc;
1762       break;
1763     case R0_REGNUM:
1764     case R1_REGNUM:
1765     case R2_REGNUM:
1766     case R3_REGNUM:
1767     case R4_REGNUM:
1768     case R5_REGNUM:
1769     case R6_REGNUM:
1770     case R7_REGNUM:
1771       v = cpu.regs[rn];
1772       break;
1773     case 10:
1774       v = cpu.cycles;
1775       longreg = 1;
1776       break;
1777     case 11:
1778       v = cpu.ticks;
1779       longreg = 1;
1780       break;
1781     case 12:
1782       v = cpu.insts;
1783       longreg = 1;
1784       break;
1785     }
1786   if (h8300hmode || longreg)
1787     {
1788       buf[0] = v >> 24;
1789       buf[1] = v >> 16;
1790       buf[2] = v >> 8;
1791       buf[3] = v >> 0;
1792     }
1793   else
1794     {
1795       buf[0] = v >> 8;
1796       buf[1] = v;
1797     }
1798 }
1799
1800 void
1801 sim_stop_reason (reason, sigrc)
1802      enum sim_stop *reason;
1803      int *sigrc;
1804 {
1805   *reason = sim_stopped;
1806   *sigrc = cpu.exception;
1807 }
1808
1809 sim_csize (n)
1810 {
1811   if (cpu.cache)
1812     free (cpu.cache);
1813   if (n < 2)
1814     n = 2;
1815   cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
1816   memset (cpu.cache, 0, sizeof (decoded_inst) * n);
1817   cpu.csize = n;
1818 }
1819
1820
1821 void
1822 sim_info (verbose)
1823      int verbose;
1824 {
1825   double timetaken = (double) cpu.ticks / (double) now_persec ();
1826   double virttime = cpu.cycles / 10.0e6;
1827
1828   printf_filtered ("\n\n#instructions executed  %10d\n", cpu.insts);
1829   printf_filtered ("#cycles (v approximate) %10d\n", cpu.cycles);
1830   printf_filtered ("#real time taken        %10.4f\n", timetaken);
1831   printf_filtered ("#virtual time taked     %10.4f\n", virttime);
1832   if (timetaken != 0.0)
1833     printf_filtered ("#simulation ratio       %10.4f\n", virttime / timetaken);
1834   printf_filtered ("#compiles               %10d\n", cpu.compiles);
1835   printf_filtered ("#cache size             %10d\n", cpu.csize);
1836
1837 #ifdef ADEBUG
1838   if (verbose)
1839     {
1840       int i;
1841       for (i = 0; i < O_LAST; i++)
1842         {
1843           if (cpu.stats[i])
1844             printf_filtered ("%d: %d\n", i, cpu.stats[i]);
1845         }
1846     }
1847 #endif
1848 }
1849
1850 /* Indicate whether the cpu is an h8/300 or h8/300h.
1851    FLAG is non-zero for the h8/300h.  */
1852
1853 void
1854 set_h8300h (flag)
1855      int flag;
1856 {
1857   h8300hmode = flag;
1858 }
1859
1860 void
1861 sim_kill ()
1862 {
1863   /* nothing to do */
1864 }
1865
1866 void
1867 sim_open (args)
1868      char *args;
1869 {
1870   /* nothing to do */
1871 }
1872
1873 void
1874 sim_close (quitting)
1875      int quitting;
1876 {
1877   /* nothing to do */
1878 }
1879
1880 /* Called by gdb to load a program into memory.  */
1881
1882 int
1883 sim_load (prog, from_tty)
1884      char *prog;
1885      int from_tty;
1886 {
1887   bfd *abfd;
1888
1889   /* See if the file is for the h8/300 or h8/300h.  */
1890   /* ??? This may not be the most efficient way.  The z8k simulator
1891      does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO).  */
1892   if ((abfd = bfd_openr (prog, "coff-h8300")) != 0)
1893     {
1894       if (bfd_check_format (abfd, bfd_object)) 
1895         set_h8300h (abfd->arch_info->mach == bfd_mach_h8300h);
1896       bfd_close (abfd);
1897     }
1898
1899   /* If we're using gdb attached to the simulator, then we have to
1900      reallocate memory for the simulator.
1901
1902      When gdb first starts, it calls fetch_registers (among other
1903      functions), which in turn calls init_pointers, which allocates
1904      simulator memory.
1905
1906      The problem is when we do that, we don't know whether we're
1907      debugging an h8/300 or h8/300h program.
1908
1909      This is the first point at which we can make that determination,
1910      so we just reallocate memory now; this will also allow us to handle
1911      switching between h8/300 and h8/300h programs without exiting
1912      gdb.  */
1913   if (h8300hmode)
1914     memory_size = H8300H_MSIZE;
1915   else
1916     memory_size = H8300_MSIZE;
1917
1918   if (cpu.memory)
1919     free (cpu.memory);
1920   if (cpu.cache_idx)
1921     free (cpu.cache_idx);
1922   if (cpu.eightbit)
1923     free (cpu.eightbit);
1924
1925   cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
1926   cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
1927   cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
1928
1929   /* `msize' must be a power of two */
1930   if ((memory_size & (memory_size - 1)) != 0)
1931     abort ();
1932   cpu.mask = memory_size - 1;
1933
1934   /* Return non-zero so gdb will handle it.  */
1935   return 1;
1936 }
1937
1938 void
1939 sim_create_inferior (start_address, argv, env)
1940      SIM_ADDR start_address;
1941      char **argv;
1942      char **env;
1943 {
1944   cpu.pc = start_address;
1945 }
1946
1947 void
1948 sim_do_command (cmd)
1949      char *cmd;
1950 {
1951   printf_filtered ("This simulator does not accept any commands.\n");
1952 }
1953
1954
1955
1956 void
1957 sim_set_callbacks (ptr)
1958 struct host_callback_struct *ptr;
1959 {
1960
1961 }
1962