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