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