2003-06-18 Michael Snyder <msnyder@redhat.com>
[external/binutils.git] / sim / h8300 / compile.c
1 /*
2  * Simulator for the Renesas (formerly 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 <signal.h>
21 #ifdef HAVE_TIME_H
22 #include <time.h>
23 #endif
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
29 #endif
30
31 #include "bfd.h"
32 #include "sim-main.h"
33 #include "gdb/sim-h8300.h"
34 #include "sys/stat.h"
35 #include "sys/types.h"
36
37 #ifndef SIGTRAP
38 # define SIGTRAP 5
39 #endif
40
41 int debug;
42
43 host_callback *sim_callback;
44
45 static SIM_OPEN_KIND sim_kind;
46 static char *myname;
47
48 /* FIXME: Needs to live in header file.
49    This header should also include the things in remote-sim.h.
50    One could move this to remote-sim.h but this function isn't needed
51    by gdb.  */
52 static void set_simcache_size (SIM_DESC, int);
53
54 #define X(op, size)  (op * 4 + size)
55
56 #define SP (h8300hmode ? SL : SW)
57
58 #define h8_opcodes ops
59 #define DEFINE_TABLE
60 #include "opcode/h8300.h"
61
62 /* CPU data object: */
63
64 static int
65 sim_state_initialize (SIM_DESC sd, sim_cpu *cpu)
66 {
67   /* FIXME: not really necessary, since sim_cpu_alloc calls zalloc.  */
68
69   memset (&cpu->regs, 0, sizeof(cpu->regs));
70   cpu->regs[SBR_REGNUM] = 0xFFFFFF00;
71   cpu->pc = 0;
72   cpu->delayed_branch = 0;
73   cpu->memory = NULL;
74   cpu->eightbit = NULL;
75   cpu->mask = 0;
76
77   /* Initialize local simulator state.  */
78   sd->sim_cache = NULL;
79   sd->sim_cache_size = 0;
80   sd->cache_idx = NULL;
81   sd->cache_top = 0;
82   sd->memory_size = 0;
83   sd->compiles = 0;
84 #ifdef ADEBUG
85   memset (&cpu->stats, 0, sizeof (cpu->stats));
86 #endif
87   return 0;
88 }
89
90 static unsigned int
91 h8_get_pc (SIM_DESC sd)
92 {
93   return (STATE_CPU (sd, 0)) -> pc;
94 }
95
96 static void
97 h8_set_pc (SIM_DESC sd, unsigned int val)
98 {
99   (STATE_CPU (sd, 0)) -> pc = val;
100 }
101
102 static unsigned int
103 h8_get_ccr (SIM_DESC sd)
104 {
105   return (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM];
106 }
107
108 static void
109 h8_set_ccr (SIM_DESC sd, unsigned int val)
110 {
111   (STATE_CPU (sd, 0)) -> regs[CCR_REGNUM] = val;
112 }
113
114 static unsigned int
115 h8_get_exr (SIM_DESC sd)
116 {
117   return (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM];
118 }
119
120 static void
121 h8_set_exr (SIM_DESC sd, unsigned int val)
122 {
123   (STATE_CPU (sd, 0)) -> regs[EXR_REGNUM] = val;
124 }
125
126 static int
127 h8_get_sbr (SIM_DESC sd)
128 {
129   return (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM];
130 }
131
132 static void
133 h8_set_sbr (SIM_DESC sd, int val)
134 {
135   (STATE_CPU (sd, 0)) -> regs[SBR_REGNUM] = val;
136 }
137
138 static int
139 h8_get_vbr (SIM_DESC sd)
140 {
141   return (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM];
142 }
143
144 static void
145 h8_set_vbr (SIM_DESC sd, int val)
146 {
147   (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM] = val;
148 }
149
150 static int
151 h8_get_cache_top (SIM_DESC sd)
152 {
153   return sd -> cache_top;
154 }
155
156 static void
157 h8_set_cache_top (SIM_DESC sd, int val)
158 {
159   sd -> cache_top = val;
160 }
161
162 static int
163 h8_get_mask (SIM_DESC sd)
164 {
165   return (STATE_CPU (sd, 0)) -> mask;
166 }
167
168 static void
169 h8_set_mask (SIM_DESC sd, int val)
170 {
171   (STATE_CPU (sd, 0)) -> mask = val;
172 }
173 #if 0
174 static int
175 h8_get_exception (SIM_DESC sd)
176 {
177   return (STATE_CPU (sd, 0)) -> exception;
178 }
179
180 static void
181 h8_set_exception (SIM_DESC sd, int val)
182 {
183   (STATE_CPU (sd, 0)) -> exception = val;
184 }
185
186 static enum h8300_sim_state
187 h8_get_state (SIM_DESC sd)
188 {
189   return sd -> state;
190 }
191
192 static void
193 h8_set_state (SIM_DESC sd, enum h8300_sim_state val)
194 {
195   sd -> state = val;
196 }
197 #endif
198 static unsigned int
199 h8_get_cycles (SIM_DESC sd)
200 {
201   return (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM];
202 }
203
204 static void
205 h8_set_cycles (SIM_DESC sd, unsigned int val)
206 {
207   (STATE_CPU (sd, 0)) -> regs[CYCLE_REGNUM] = val;
208 }
209
210 static unsigned int
211 h8_get_insts (SIM_DESC sd)
212 {
213   return (STATE_CPU (sd, 0)) -> regs[INST_REGNUM];
214 }
215
216 static void
217 h8_set_insts (SIM_DESC sd, unsigned int val)
218 {
219   (STATE_CPU (sd, 0)) -> regs[INST_REGNUM] = val;
220 }
221
222 static unsigned int
223 h8_get_ticks (SIM_DESC sd)
224 {
225   return (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM];
226 }
227
228 static void
229 h8_set_ticks (SIM_DESC sd, unsigned int val)
230 {
231   (STATE_CPU (sd, 0)) -> regs[TICK_REGNUM] = val;
232 }
233
234 static unsigned int
235 h8_get_mach (SIM_DESC sd)
236 {
237   return (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM];
238 }
239
240 static void
241 h8_set_mach (SIM_DESC sd, unsigned int val)
242 {
243   (STATE_CPU (sd, 0)) -> regs[MACH_REGNUM] = val;
244 }
245
246 static unsigned int
247 h8_get_macl (SIM_DESC sd)
248 {
249   return (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM];
250 }
251
252 static void
253 h8_set_macl (SIM_DESC sd, unsigned int val)
254 {
255   (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM] = val;
256 }
257
258 static int
259 h8_get_compiles (SIM_DESC sd)
260 {
261   return sd -> compiles;
262 }
263
264 static void
265 h8_increment_compiles (SIM_DESC sd)
266 {
267   sd -> compiles ++;
268 }
269
270 static unsigned int *
271 h8_get_reg_buf (SIM_DESC sd)
272 {
273   return &(((STATE_CPU (sd, 0)) -> regs)[0]);
274 }
275
276 static unsigned int
277 h8_get_reg (SIM_DESC sd, int regnum)
278 {
279   return (STATE_CPU (sd, 0)) -> regs[regnum];
280 }
281
282 static void
283 h8_set_reg (SIM_DESC sd, int regnum, int val)
284 {
285   (STATE_CPU (sd, 0)) -> regs[regnum] = val;
286 }
287
288 #ifdef ADEBUG
289 static int
290 h8_get_stats (SIM_DESC sd, int idx)
291 {
292   return sd -> stats[idx];
293 }
294
295 static void
296 h8_increment_stats (SIM_DESC sd, int idx)
297 {
298   sd -> stats[idx] ++;
299 }
300 #endif /* ADEBUG */
301
302 static unsigned short *
303 h8_get_cache_idx_buf (SIM_DESC sd)
304 {
305   return sd -> cache_idx;
306 }
307
308 static void
309 h8_set_cache_idx_buf (SIM_DESC sd, unsigned short *ptr)
310 {
311   sd -> cache_idx = ptr;
312 }
313
314 static unsigned short
315 h8_get_cache_idx (SIM_DESC sd, unsigned int idx)
316 {
317   if (idx > sd->memory_size)
318     return (unsigned short) -1;
319   return sd -> cache_idx[idx];
320 }
321
322 static void
323 h8_set_cache_idx (SIM_DESC sd, int idx, unsigned int val)
324 {
325   sd -> cache_idx[idx] = (unsigned short) val;
326 }
327
328 static unsigned char *
329 h8_get_memory_buf (SIM_DESC sd)
330 {
331   return (STATE_CPU (sd, 0)) -> memory;
332 }
333
334 static void
335 h8_set_memory_buf (SIM_DESC sd, unsigned char *ptr)
336 {
337   (STATE_CPU (sd, 0)) -> memory = ptr;
338 }
339
340 static unsigned char
341 h8_get_memory (SIM_DESC sd, int idx)
342 {
343   return (STATE_CPU (sd, 0)) -> memory[idx];
344 }
345
346 static void
347 h8_set_memory (SIM_DESC sd, int idx, unsigned int val)
348 {
349   (STATE_CPU (sd, 0)) -> memory[idx] = (unsigned char) val;
350 }
351
352 static unsigned char *
353 h8_get_eightbit_buf (SIM_DESC sd)
354 {
355   return (STATE_CPU (sd, 0)) -> eightbit;
356 }
357
358 static void
359 h8_set_eightbit_buf (SIM_DESC sd, unsigned char *ptr)
360 {
361   (STATE_CPU (sd, 0)) -> eightbit = ptr;
362 }
363
364 static unsigned char
365 h8_get_eightbit (SIM_DESC sd, int idx)
366 {
367   return (STATE_CPU (sd, 0)) -> eightbit[idx];
368 }
369
370 static void
371 h8_set_eightbit (SIM_DESC sd, int idx, unsigned int val)
372 {
373   (STATE_CPU (sd, 0)) -> eightbit[idx] = (unsigned char) val;
374 }
375
376 static unsigned int
377 h8_get_delayed_branch (SIM_DESC sd)
378 {
379   return (STATE_CPU (sd, 0)) -> delayed_branch;
380 }
381
382 static void
383 h8_set_delayed_branch (SIM_DESC sd, unsigned int dest)
384 {
385   (STATE_CPU (sd, 0)) -> delayed_branch = dest;
386 }
387
388 static char **
389 h8_get_command_line (SIM_DESC sd)
390 {
391   return (STATE_CPU (sd, 0)) -> command_line;
392 }
393
394 static void
395 h8_set_command_line (SIM_DESC sd, char ** val)
396 {
397   (STATE_CPU (sd, 0)) -> command_line = val;
398 }
399
400 static char *
401 h8_get_cmdline_arg (SIM_DESC sd, int index)
402 {
403   return (STATE_CPU (sd, 0)) -> command_line[index];
404 }
405
406 static void
407 h8_set_cmdline_arg (SIM_DESC sd, int index, char * val)
408 {
409   (STATE_CPU (sd, 0)) -> command_line[index] = val;
410 }
411
412 /* MAC Saturation Mode */
413 static int
414 h8_get_macS (SIM_DESC sd)
415 {
416   return (STATE_CPU (sd, 0)) -> macS;
417 }
418
419 static void
420 h8_set_macS (SIM_DESC sd, int val)
421 {
422   (STATE_CPU (sd, 0)) -> macS = (val != 0);
423 }
424
425 /* MAC Zero Flag */
426 static int
427 h8_get_macZ (SIM_DESC sd)
428 {
429   return (STATE_CPU (sd, 0)) -> macZ;
430 }
431
432 static void
433 h8_set_macZ (SIM_DESC sd, int val)
434 {
435   (STATE_CPU (sd, 0)) -> macZ = (val != 0);
436 }
437
438 /* MAC Negative Flag */
439 static int
440 h8_get_macN (SIM_DESC sd)
441 {
442   return (STATE_CPU (sd, 0)) -> macN;
443 }
444
445 static void
446 h8_set_macN (SIM_DESC sd, int val)
447 {
448   (STATE_CPU (sd, 0)) -> macN = (val != 0);
449 }
450
451 /* MAC Overflow Flag */
452 static int
453 h8_get_macV (SIM_DESC sd)
454 {
455   return (STATE_CPU (sd, 0)) -> macV;
456 }
457
458 static void
459 h8_set_macV (SIM_DESC sd, int val)
460 {
461   (STATE_CPU (sd, 0)) -> macV = (val != 0);
462 }
463
464 /* End CPU data object.  */
465
466 /* The rate at which to call the host's poll_quit callback.  */
467
468 enum { POLL_QUIT_INTERVAL = 0x80000 };
469
470 #define LOW_BYTE(x) ((x) & 0xff)
471 #define HIGH_BYTE(x) (((x) >> 8) & 0xff)
472 #define P(X, Y) ((X << 8) | Y)
473
474 #define C (c != 0)
475 #define Z (nz == 0)
476 #define V (v != 0)
477 #define N (n != 0)
478 #define U (u != 0)
479 #define H (h != 0)
480 #define UI (ui != 0)
481 #define I (intMaskBit != 0)
482
483 #define BUILDSR(SD)                                             \
484   h8_set_ccr (SD, (I << 7) | (UI << 6) | (H << 5) | (U << 4)    \
485              | (N << 3) | (Z << 2) | (V << 1) | C)
486
487 #define GETSR(SD) \
488   /* Get Status Register (flags).  */           \
489   c = (h8_get_ccr (sd) >> 0) & 1;               \
490   v = (h8_get_ccr (sd) >> 1) & 1;               \
491   nz = !((h8_get_ccr (sd) >> 2) & 1);           \
492   n = (h8_get_ccr (sd) >> 3) & 1;               \
493   u = (h8_get_ccr (sd) >> 4) & 1;               \
494   h = (h8_get_ccr (sd) >> 5) & 1;               \
495   ui = ((h8_get_ccr (sd) >> 6) & 1);            \
496   intMaskBit = (h8_get_ccr (sd) >> 7) & 1
497
498
499 #ifdef __CHAR_IS_SIGNED__
500 #define SEXTCHAR(x) ((char) (x))
501 #endif
502
503 #ifndef SEXTCHAR
504 #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff) : x & 0xff)
505 #endif
506
507 #define UEXTCHAR(x) ((x) & 0xff)
508 #define UEXTSHORT(x) ((x) & 0xffff)
509 #define SEXTSHORT(x) ((short) (x))
510
511 int h8300hmode  = 0;
512 int h8300smode  = 0;
513 int h8300sxmode = 0;
514
515 static int memory_size;
516
517 static int
518 get_now (void)
519 {
520   return time (0);      /* WinXX HAS UNIX like 'time', so why not use it? */
521 }
522
523 static int
524 now_persec (void)
525 {
526   return 1;
527 }
528
529 static int
530 bitfrom (int x)
531 {
532   switch (x & SIZE)
533     {
534     case L_8:
535       return SB;
536     case L_16:
537     case L_16U:
538       return SW;
539     case L_32:
540       return SL;
541     case L_P:
542       return h8300hmode ? SL : SW;
543     }
544   return 0;
545 }
546
547 /* Simulate an indirection / dereference.  
548    return 0 for success, -1 for failure.
549 */
550
551 static unsigned int
552 lvalue (SIM_DESC sd, int x, int rn, unsigned int *val)
553 {
554   if (val == NULL)      /* Paranoia.  */
555     return -1;
556
557   switch (x / 4)
558     {
559     case OP_DISP:
560       if (rn == ZERO_REGNUM)
561         *val = X (OP_IMM, SP);
562       else
563         *val = X (OP_REG, SP);
564       break;
565     case OP_MEM:
566       *val = X (OP_MEM, SP);
567       break;
568     default:
569       sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
570       return -1;
571     }
572   return 0;
573 }
574
575 static int
576 cmdline_location()
577 {
578   if (h8300smode)
579     return 0xffff00L;
580   else if (h8300hmode)
581     return 0x2ff00L;
582   else
583     return 0xff00L;
584 }
585
586 static void
587 decode (SIM_DESC sd, int addr, unsigned char *data, decoded_inst *dst)
588 {
589   int cst[3]   = {0, 0, 0};
590   int reg[3]   = {0, 0, 0};
591   int rdisp[3] = {0, 0, 0};
592   int opnum;
593   const struct h8_opcode *q;
594
595   dst->dst.type = -1;
596   dst->src.type = -1;
597
598   /* Find the exact opcode/arg combo.  */
599   for (q = h8_opcodes; q->name; q++)
600     {
601       op_type *nib = q->data.nib;
602       unsigned int len = 0;
603
604       if ((q->available == AV_H8SX && !h8300sxmode) ||
605           (q->available == AV_H8S  && !h8300smode)  ||
606           (q->available == AV_H8H  && !h8300hmode))
607         continue;
608
609       while (1)
610         {
611           op_type looking_for = *nib;
612           int thisnib = data[len / 2];
613
614           thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
615           opnum = ((looking_for & OP3) ? 2 :
616                    (looking_for & DST) ? 1 : 0);
617
618           if (looking_for < 16 && looking_for >= 0)
619             {
620               if (looking_for != thisnib)
621                 goto fail;
622             }
623           else
624             {
625               if (looking_for & B31)
626                 {
627                   if (!((thisnib & 0x8) != 0))
628                     goto fail;
629
630                   looking_for = (op_type) (looking_for & ~B31);
631                   thisnib &= 0x7;
632                 }
633               else if (looking_for & B30)
634                 {
635                   if (!((thisnib & 0x8) == 0))
636                     goto fail;
637
638                   looking_for = (op_type) (looking_for & ~B30);
639                 }
640
641               if (looking_for & B21)
642                 {
643                   if (!((thisnib & 0x4) != 0))
644                     goto fail;
645
646                   looking_for = (op_type) (looking_for & ~B21);
647                   thisnib &= 0xb;
648                 }
649               else if (looking_for & B20)
650                 {
651                   if (!((thisnib & 0x4) == 0))
652                     goto fail;
653
654                   looking_for = (op_type) (looking_for & ~B20);
655                 }
656
657               if (looking_for & B11)
658                 {
659                   if (!((thisnib & 0x2) != 0))
660                     goto fail;
661
662                   looking_for = (op_type) (looking_for & ~B11);
663                   thisnib &= 0xd;
664                 }
665               else if (looking_for & B10)
666                 {
667                   if (!((thisnib & 0x2) == 0))
668                     goto fail;
669
670                   looking_for = (op_type) (looking_for & ~B10);
671                 }
672
673               if (looking_for & B01)
674                 {
675                   if (!((thisnib & 0x1) != 0))
676                     goto fail;
677
678                   looking_for = (op_type) (looking_for & ~B01);
679                   thisnib &= 0xe;
680                 }
681               else if (looking_for & B00)
682                 {
683                   if (!((thisnib & 0x1) == 0))
684                     goto fail;
685
686                   looking_for = (op_type) (looking_for & ~B00);
687                 }
688
689               if (looking_for & IGNORE)
690                 {
691                   /* Hitachi has declared that IGNORE must be zero.  */
692                   if (thisnib != 0)
693                     goto fail;
694                 }
695               else if ((looking_for & MODE) == DATA)
696                 {
697                   ;                     /* Skip embedded data.  */
698                 }
699               else if ((looking_for & MODE) == DBIT)
700                 {
701                   /* Exclude adds/subs by looking at bit 0 and 2, and
702                      make sure the operand size, either w or l,
703                      matches by looking at bit 1.  */
704                   if ((looking_for & 7) != (thisnib & 7))
705                     goto fail;
706
707                   cst[opnum] = (thisnib & 0x8) ? 2 : 1;
708                 }
709               else if ((looking_for & MODE) == REG     ||
710                        (looking_for & MODE) == LOWREG  ||
711                        (looking_for & MODE) == IND     ||
712                        (looking_for & MODE) == PREINC  ||
713                        (looking_for & MODE) == POSTINC ||
714                        (looking_for & MODE) == PREDEC  ||
715                        (looking_for & MODE) == POSTDEC)
716                 {
717                   reg[opnum] = thisnib;
718                 }
719               else if (looking_for & CTRL)
720                 {
721                   thisnib &= 7;
722                   if (((looking_for & MODE) == CCR  && (thisnib != C_CCR))  ||
723                       ((looking_for & MODE) == EXR  && (thisnib != C_EXR))  ||
724                       ((looking_for & MODE) == MACH && (thisnib != C_MACH)) ||
725                       ((looking_for & MODE) == MACL && (thisnib != C_MACL)) ||
726                       ((looking_for & MODE) == VBR  && (thisnib != C_VBR))  ||
727                       ((looking_for & MODE) == SBR  && (thisnib != C_SBR)))
728                     goto fail;
729                   if (((looking_for & MODE) == CCR_EXR && 
730                        (thisnib != C_CCR && thisnib != C_EXR)) ||
731                       ((looking_for & MODE) == VBR_SBR && 
732                        (thisnib != C_VBR && thisnib != C_SBR)) ||
733                       ((looking_for & MODE) == MACREG && 
734                        (thisnib != C_MACH && thisnib != C_MACL)))
735                     goto fail;
736                   if (((looking_for & MODE) == CC_EX_VB_SB && 
737                        (thisnib != C_CCR && thisnib != C_EXR &&
738                         thisnib != C_VBR && thisnib != C_SBR)))
739                     goto fail;
740
741                   reg[opnum] = thisnib;
742                 }
743               else if ((looking_for & MODE) == ABS)
744                 {
745                   /* Absolute addresses are unsigned.  */
746                   switch (looking_for & SIZE)
747                     {
748                     case L_8:
749                       cst[opnum] = UEXTCHAR (data[len / 2]);
750                       break;
751                     case L_16:
752                     case L_16U:
753                       cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
754                       break;
755                     case L_32:
756                       cst[opnum] = 
757                         (data[len / 2 + 0] << 24) + 
758                         (data[len / 2 + 1] << 16) +
759                         (data[len / 2 + 2] <<  8) +  
760                         (data[len / 2 + 3]);
761                       break;
762                     default:
763                       printf ("decode: bad size ABS: %d\n", 
764                               (looking_for & SIZE));
765                       goto end;
766                     }
767                 }
768               else if ((looking_for & MODE) == DISP   ||
769                        (looking_for & MODE) == PCREL  ||
770                        (looking_for & MODE) == INDEXB ||
771                        (looking_for & MODE) == INDEXW ||
772                        (looking_for & MODE) == INDEXL)
773
774                 {
775                   switch (looking_for & SIZE)
776                     {
777                     case L_2:
778                       cst[opnum] = thisnib & 3;
779
780                       /* DISP2 special treatment.  */
781                       if ((looking_for & MODE) == DISP)
782                         {
783                           switch (OP_SIZE (q->how)) {
784                           default: break;
785                           case SW:
786                             cst[opnum] *= 2;
787                             break;
788                           case SL:
789                             cst[opnum] *= 4;
790                             break;
791                           }
792                         }
793                       break;
794                     case L_8:
795                       cst[opnum] = SEXTCHAR (data[len / 2]);
796                       break;
797                     case L_16:
798                       cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
799                       cst[opnum] = (short) cst[opnum];  /* Sign extend.  */
800                       break;
801                     case L_16U:
802                       cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
803                       break;
804                     case L_32:
805                       cst[opnum] = 
806                         (data[len / 2 + 0] << 24) + 
807                         (data[len / 2 + 1] << 16) +
808                         (data[len / 2 + 2] <<  8) +  
809                         (data[len / 2 + 3]);
810                       break;
811                     default:
812                       printf ("decode: bad size DISP/PCREL/INDEX: %d\n", 
813                               (looking_for & SIZE));
814                       goto end;
815                     }
816                 }
817               else if ((looking_for & SIZE) == L_16 ||
818                        (looking_for & SIZE) == L_16U)
819                 {
820                   cst[opnum] = (data[len / 2] << 8) + data[len / 2 + 1];
821                   if ((looking_for & SIZE) != L_16U)
822                     cst[opnum] = (short) cst[opnum];    /* Sign extend.  */
823                 }
824               else if (looking_for & ABSJMP)
825                 {
826                   switch (looking_for & SIZE) {
827                   case L_24:
828                     cst[opnum] = (data[1] << 16) | (data[2] << 8) | (data[3]);
829                     break;
830                   case L_32:
831                     cst[opnum] = 
832                       (data[len / 2 + 0] << 24) + 
833                       (data[len / 2 + 1] << 16) +
834                       (data[len / 2 + 2] <<  8) +  
835                       (data[len / 2 + 3]);
836                     break;
837                   default:
838                     printf ("decode: bad size ABSJMP: %d\n", 
839                             (looking_for & SIZE));
840                       goto end;
841                   }
842                 }
843               else if ((looking_for & MODE) == MEMIND)
844                 {
845                   cst[opnum] = data[1];
846                 }
847               else if ((looking_for & MODE) == VECIND)
848                 {
849                   /* FIXME: Multiplier should be 2 for "normal" mode.  */
850                   cst[opnum] = ((data[1] & 0x7f) + 0x80) * 4;
851                   cst[opnum] += h8_get_vbr (sd); /* Add vector base reg.  */
852                 }
853               else if ((looking_for & SIZE) == L_32)
854                 {
855                   int i = len / 2;
856
857                   cst[opnum] = 
858                     (data[i + 0] << 24) |
859                     (data[i + 1] << 16) |
860                     (data[i + 2] <<  8) |
861                     (data[i + 3]);
862                 }
863               else if ((looking_for & SIZE) == L_24)
864                 {
865                   int i = len / 2;
866
867                   cst[opnum] = 
868                     (data[i + 0] << 16) | 
869                     (data[i + 1] << 8) | 
870                     (data[i + 2]);
871                 }
872               else if (looking_for & DISPREG)
873                 {
874                   rdisp[opnum] = thisnib & 0x7;
875                 }
876               else if ((looking_for & MODE) == KBIT)
877                 {
878                   switch (thisnib)
879                     {
880                     case 9:
881                       cst[opnum] = 4;
882                       break;
883                     case 8:
884                       cst[opnum] = 2;
885                       break;
886                     case 0:
887                       cst[opnum] = 1;
888                       break;
889                     default:
890                       goto fail;
891                     }
892                 }
893               else if ((looking_for & SIZE) == L_8)
894                 {
895                   if ((looking_for & MODE) == ABS)
896                     {
897                       /* Will be combined with contents of SBR_REGNUM
898                          by fetch ().  For all modes except h8sx, this
899                          will always contain the value 0xFFFFFF00.  */
900                       cst[opnum] = data[len / 2] & 0xff;
901                     }
902                   else
903                     {
904                       cst[opnum] = data[len / 2] & 0xff;
905                     }
906                 }
907               else if ((looking_for & SIZE) == L_2)
908                 {
909                   cst[opnum] = thisnib & 3;
910                 }
911               else if ((looking_for & SIZE) == L_3 ||
912                        (looking_for & SIZE) == L_3NZ)
913                 {
914                   cst[opnum] = thisnib & 7;
915                   if (cst[opnum] == 0 && (looking_for & SIZE) == L_3NZ)
916                     goto fail;
917                 }
918               else if ((looking_for & SIZE) == L_4)
919                 {
920                   cst[opnum] = thisnib & 15;
921                 }
922               else if ((looking_for & SIZE) == L_5)
923                 {
924                   cst[opnum] = data[len / 2] & 0x1f;
925                 }
926               else if (looking_for == E)
927                 {
928 #ifdef ADEBUG
929                   dst->op = q;
930 #endif
931                   /* Fill in the args.  */
932                   {
933                     op_type *args = q->args.nib;
934                     int hadone = 0;
935                     int nargs;
936
937                     for (nargs = 0; 
938                          nargs < 3 && *args != E; 
939                          nargs++)
940                       {
941                         int x = *args;
942                         ea_type *p;
943
944                         opnum = ((x & OP3) ? 2 :
945                                  (x & DST) ? 1 : 0);
946                         if (x & DST)
947                           p = &dst->dst;
948                         else if (x & OP3)
949                           p = &dst->op3;
950                         else
951                           p = &dst->src;
952
953                         if ((x & MODE) == IMM  ||
954                             (x & MODE) == KBIT ||
955                             (x & MODE) == DBIT)
956                           {
957                             /* Use the instruction to determine 
958                                the operand size.  */
959                             p->type = X (OP_IMM, OP_SIZE (q->how));
960                             p->literal = cst[opnum];
961                           }
962                         else if ((x & MODE) == CONST_2 ||
963                                  (x & MODE) == CONST_4 ||
964                                  (x & MODE) == CONST_8 ||
965                                  (x & MODE) == CONST_16)
966                           {
967                             /* Use the instruction to determine 
968                                the operand size.  */
969                             p->type = X (OP_IMM, OP_SIZE (q->how));
970                             switch (x & MODE) {
971                             case CONST_2:       p->literal =  2; break;
972                             case CONST_4:       p->literal =  4; break;
973                             case CONST_8:       p->literal =  8; break;
974                             case CONST_16:      p->literal = 16; break;
975                             }
976                           }
977                         else if ((x & MODE) == REG)
978                           {
979                             p->type = X (OP_REG, bitfrom (x));
980                             p->reg = reg[opnum];
981                           }
982                         else if ((x & MODE) == LOWREG)
983                           {
984                             p->type = X (OP_LOWREG, bitfrom (x));
985                             p->reg = reg[opnum];
986                           }
987                         else if ((x & MODE) == PREINC)
988                           {
989                             /* Use the instruction to determine 
990                                the operand size.  */
991                             p->type = X (OP_PREINC, OP_SIZE (q->how));
992                             p->reg = reg[opnum] & 0x7;
993                           }
994                         else if ((x & MODE) == POSTINC)
995                           {
996                             /* Use the instruction to determine 
997                                the operand size.  */
998                             p->type = X (OP_POSTINC, OP_SIZE (q->how));
999                             p->reg = reg[opnum] & 0x7;
1000                           }
1001                         else if ((x & MODE) == PREDEC)
1002                           {
1003                             /* Use the instruction to determine 
1004                                the operand size.  */
1005                             p->type = X (OP_PREDEC, OP_SIZE (q->how));
1006                             p->reg = reg[opnum] & 0x7;
1007                           }
1008                         else if ((x & MODE) == POSTDEC)
1009                           {
1010                             /* Use the instruction to determine 
1011                                the operand size.  */
1012                             p->type = X (OP_POSTDEC, OP_SIZE (q->how));
1013                             p->reg = reg[opnum] & 0x7;
1014                           }
1015                         else if ((x & MODE) == IND)
1016                           {
1017                             /* Note: an indirect is transformed into
1018                                a displacement of zero.  
1019                             */
1020                             /* Use the instruction to determine 
1021                                the operand size.  */
1022                             p->type = X (OP_DISP, OP_SIZE (q->how));
1023                             p->reg = reg[opnum] & 0x7;
1024                             p->literal = 0;
1025                             if (OP_KIND (q->how) == O_JSR ||
1026                                 OP_KIND (q->how) == O_JMP)
1027                               if (lvalue (sd, p->type, p->reg, &p->type))
1028                                 goto end;
1029                           }
1030                         else if ((x & MODE) == ABS)
1031                           {
1032                             /* Note: a 16 or 32 bit ABS is transformed into a 
1033                                displacement from pseudo-register ZERO_REGNUM,
1034                                which is always zero.  An 8 bit ABS becomes
1035                                a displacement from SBR_REGNUM.
1036                             */
1037                             /* Use the instruction to determine 
1038                                the operand size.  */
1039                             p->type = X (OP_DISP, OP_SIZE (q->how));
1040                             p->literal = cst[opnum];
1041
1042                             /* 8-bit ABS is displacement from SBR.
1043                                16 and 32-bit ABS are displacement from ZERO.
1044                                (SBR will always be zero except for h8/sx)
1045                             */
1046                             if ((x & SIZE) == L_8)
1047                               p->reg = SBR_REGNUM;
1048                             else
1049                               p->reg = ZERO_REGNUM;;
1050                           }
1051                         else if ((x & MODE) == MEMIND ||
1052                                  (x & MODE) == VECIND)
1053                           {
1054                             /* Size doesn't matter.  */
1055                             p->type = X (OP_MEM, SB);
1056                             p->literal = cst[opnum];
1057                             if (OP_KIND (q->how) == O_JSR ||
1058                                 OP_KIND (q->how) == O_JMP)
1059                               if (lvalue (sd, p->type, p->reg, &p->type))
1060                                 goto end;
1061                           }
1062                         else if ((x & MODE) == PCREL)
1063                           {
1064                             /* Size doesn't matter.  */
1065                             p->type = X (OP_PCREL, SB);
1066                             p->literal = cst[opnum];
1067                           }
1068                         else if (x & ABSJMP)
1069                           {
1070                             p->type = X (OP_IMM, SP);
1071                             p->literal = cst[opnum];
1072                           }
1073                         else if ((x & MODE) == INDEXB ||
1074                                  (x & MODE) == INDEXW ||
1075                                  (x & MODE) == INDEXL ||
1076                                  (x & MODE) == DISP)
1077                           {
1078                             /* Use the instruction to determine 
1079                                the operand size.  */
1080                             switch (x & MODE) {
1081                             case INDEXB:
1082                               p->type = X (OP_INDEXB, OP_SIZE (q->how));
1083                               break;
1084                             case INDEXW:
1085                               p->type = X (OP_INDEXW, OP_SIZE (q->how));
1086                               break;
1087                             case INDEXL:
1088                               p->type = X (OP_INDEXL, OP_SIZE (q->how));
1089                               break;
1090                             case DISP:
1091                               p->type = X (OP_DISP,   OP_SIZE (q->how));
1092                               break;
1093                             }
1094
1095                             p->literal = cst[opnum];
1096                             p->reg     = rdisp[opnum];
1097                           }
1098                         else if (x & CTRL)
1099                           {
1100                             switch (reg[opnum])
1101                               {
1102                               case C_CCR:
1103                                 p->type = X (OP_CCR, SB);
1104                                 break;
1105                               case C_EXR:
1106                                 p->type = X (OP_EXR, SB);
1107                                 break;
1108                               case C_MACH:
1109                                 p->type = X (OP_MACH, SL);
1110                                 break;
1111                               case C_MACL:
1112                                 p->type = X (OP_MACL, SL);
1113                                 break;
1114                               case C_VBR:
1115                                 p->type = X (OP_VBR, SL);
1116                                 break;
1117                               case C_SBR:
1118                                 p->type = X (OP_SBR, SL);
1119                                 break;
1120                               }
1121                           }
1122                         else if ((x & MODE) == CCR)
1123                           {
1124                             p->type = OP_CCR;
1125                           }
1126                         else if ((x & MODE) == EXR)
1127                           {
1128                             p->type = OP_EXR;
1129                           }
1130                         else
1131                           printf ("Hmmmm 0x%x...\n", x);
1132
1133                         args++;
1134                       }
1135                   }
1136
1137                   /* Unary operators: treat src and dst as equivalent.  */
1138                   if (dst->dst.type == -1)
1139                     dst->dst = dst->src;
1140                   if (dst->src.type == -1)
1141                     dst->src = dst->dst;
1142
1143                   dst->opcode = q->how;
1144                   dst->cycles = q->time;
1145
1146                   /* And jsr's to these locations are turned into 
1147                      magic traps.  */
1148
1149                   if (OP_KIND (dst->opcode) == O_JSR)
1150                     {
1151                       switch (dst->src.literal)
1152                         {
1153                         case 0xc5:
1154                           dst->opcode = O (O_SYS_OPEN, SB);
1155                           break;
1156                         case 0xc6:
1157                           dst->opcode = O (O_SYS_READ, SB);
1158                           break;
1159                         case 0xc7:
1160                           dst->opcode = O (O_SYS_WRITE, SB);
1161                           break;
1162                         case 0xc8:
1163                           dst->opcode = O (O_SYS_LSEEK, SB);
1164                           break;
1165                         case 0xc9:
1166                           dst->opcode = O (O_SYS_CLOSE, SB);
1167                           break;
1168                         case 0xca:
1169                           dst->opcode = O (O_SYS_STAT, SB);
1170                           break;
1171                         case 0xcb:
1172                           dst->opcode = O (O_SYS_FSTAT, SB);
1173                           break;
1174                         case 0xcc:
1175                           dst->opcode = O (O_SYS_CMDLINE, SB);
1176                           break;
1177                         }
1178                       /* End of Processing for system calls.  */
1179                     }
1180
1181                   dst->next_pc = addr + len / 2;
1182                   return;
1183                 }
1184               else
1185                 printf ("Don't understand 0x%x \n", looking_for);
1186             }
1187
1188           len++;
1189           nib++;
1190         }
1191
1192     fail:
1193       ;
1194     }
1195  end:
1196   /* Fell off the end.  */
1197   dst->opcode = O (O_ILL, SB);
1198 }
1199
1200 static void
1201 compile (SIM_DESC sd, int pc)
1202 {
1203   int idx;
1204
1205   /* Find the next cache entry to use.  */
1206   idx = h8_get_cache_top (sd) + 1;
1207   h8_increment_compiles (sd);
1208   if (idx >= sd->sim_cache_size)
1209     {
1210       idx = 1;
1211     }
1212   h8_set_cache_top (sd, idx);
1213
1214   /* Throw away its old meaning.  */
1215   h8_set_cache_idx (sd, sd->sim_cache[idx].oldpc, 0);
1216
1217   /* Set to new address.  */
1218   sd->sim_cache[idx].oldpc = pc;
1219
1220   /* Fill in instruction info.  */
1221   decode (sd, pc, h8_get_memory_buf (sd) + pc, sd->sim_cache + idx);
1222
1223   /* Point to new cache entry.  */
1224   h8_set_cache_idx (sd, pc, idx);
1225 }
1226
1227
1228 static unsigned char  *breg[32];
1229 static unsigned short *wreg[16];
1230 static unsigned int   *lreg[18];
1231
1232 #define GET_B_REG(X)     *(breg[X])
1233 #define SET_B_REG(X, Y) (*(breg[X])) = (Y)
1234 #define GET_W_REG(X)     *(wreg[X])
1235 #define SET_W_REG(X, Y) (*(wreg[X])) = (Y)
1236 #define GET_L_REG(X)     h8_get_reg (sd, X)
1237 #define SET_L_REG(X, Y)  h8_set_reg (sd, X, Y)
1238
1239 #define GET_MEMORY_L(X) \
1240   ((X) < memory_size \
1241    ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16)  \
1242     | (h8_get_memory (sd, (X)+2) <<  8) | (h8_get_memory (sd, (X)+3) <<  0)) \
1243    : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
1244     | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
1245     | (h8_get_eightbit (sd, ((X)+2) & 0xff) <<  8) \
1246     | (h8_get_eightbit (sd, ((X)+3) & 0xff) <<  0)))
1247
1248 #define GET_MEMORY_W(X) \
1249   ((X) < memory_size \
1250    ? ((h8_get_memory   (sd, (X)+0) << 8) \
1251     | (h8_get_memory   (sd, (X)+1) << 0)) \
1252    : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
1253     | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
1254
1255
1256 #define GET_MEMORY_B(X) \
1257   ((X) < memory_size ? (h8_get_memory   (sd, (X))) \
1258                      : (h8_get_eightbit (sd, (X) & 0xff)))
1259
1260 #define SET_MEMORY_L(X, Y)  \
1261 {  register unsigned char *_p; register int __y = (Y); \
1262    _p = ((X) < memory_size ? h8_get_memory_buf   (sd) +  (X) : \
1263                              h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1264    _p[0] = __y >> 24; _p[1] = __y >> 16; \
1265    _p[2] = __y >>  8; _p[3] = __y >>  0; \
1266 }
1267
1268 #define SET_MEMORY_W(X, Y) \
1269 {  register unsigned char *_p; register int __y = (Y); \
1270    _p = ((X) < memory_size ? h8_get_memory_buf   (sd) +  (X) : \
1271                              h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
1272    _p[0] = __y >> 8; _p[1] = __y; \
1273 }
1274
1275 #define SET_MEMORY_B(X, Y) \
1276   ((X) < memory_size ? (h8_set_memory   (sd, (X), (Y))) \
1277                      : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
1278
1279 /* Simulate a memory fetch.
1280    Return 0 for success, -1 for failure.
1281 */
1282
1283 static int
1284 fetch_1 (SIM_DESC sd, ea_type *arg, int *val, int twice)
1285 {
1286   int rn = arg->reg;
1287   int abs = arg->literal;
1288   int r;
1289   int t;
1290
1291   if (val == NULL)
1292     return -1;          /* Paranoia.  */
1293
1294   switch (arg->type)
1295     {
1296       /* Indexed register plus displacement mode:
1297
1298          This new family of addressing modes are similar to OP_DISP
1299          (register plus displacement), with two differences:
1300            1) INDEXB uses only the least significant byte of the register,
1301               INDEXW uses only the least significant word, and
1302               INDEXL uses the entire register (just like OP_DISP).
1303          and
1304            2) The displacement value in abs is multiplied by two
1305               for SW-sized operations, and by four for SL-size.
1306
1307         This gives nine possible variations.
1308       */
1309
1310     case X (OP_INDEXB, SB):
1311     case X (OP_INDEXB, SW):
1312     case X (OP_INDEXB, SL):
1313     case X (OP_INDEXW, SB):
1314     case X (OP_INDEXW, SW):
1315     case X (OP_INDEXW, SL):
1316     case X (OP_INDEXL, SB):
1317     case X (OP_INDEXL, SW):
1318     case X (OP_INDEXL, SL):
1319       t = GET_L_REG (rn);
1320       switch (OP_KIND (arg->type)) {
1321       case OP_INDEXB:   t &= 0xff;      break;
1322       case OP_INDEXW:   t &= 0xffff;    break;
1323       case OP_INDEXL:
1324       default:          break;
1325       }
1326       switch (OP_SIZE (arg->type)) {
1327       case SB:
1328         *val = GET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd));
1329         break;
1330       case SW:
1331         *val = GET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd));
1332         break;
1333       case SL:
1334         *val = GET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd));
1335         break;
1336       }
1337       break;
1338
1339     case X (OP_LOWREG, SB):
1340       *val = GET_L_REG (rn) & 0xff;
1341       break;
1342     case X (OP_LOWREG, SW):
1343       *val = GET_L_REG (rn) & 0xffff; 
1344       break;
1345
1346     case X (OP_REG, SB):        /* Register direct, byte.  */
1347       *val = GET_B_REG (rn);
1348       break;
1349     case X (OP_REG, SW):        /* Register direct, word.  */
1350       *val = GET_W_REG (rn);
1351       break;
1352     case X (OP_REG, SL):        /* Register direct, long.  */
1353       *val = GET_L_REG (rn);
1354       break;
1355     case X (OP_IMM, SB):        /* Immediate, byte.  */
1356     case X (OP_IMM, SW):        /* Immediate, word.  */
1357     case X (OP_IMM, SL):        /* Immediate, long.  */
1358       *val = abs;
1359       break;
1360     case X (OP_POSTINC, SB):    /* Register indirect w/post-incr: byte.  */
1361       t = GET_L_REG (rn);
1362       t &= h8_get_mask (sd);
1363       r = GET_MEMORY_B (t);
1364       if (!twice)
1365         t += 1;
1366       t = t & h8_get_mask (sd);
1367       SET_L_REG (rn, t);
1368       *val = r;
1369       break;
1370     case X (OP_POSTINC, SW):    /* Register indirect w/post-incr: word.  */
1371       t = GET_L_REG (rn);
1372       t &= h8_get_mask (sd);
1373       r = GET_MEMORY_W (t);
1374       if (!twice)
1375         t += 2;
1376       t = t & h8_get_mask (sd);
1377       SET_L_REG (rn, t);
1378       *val = r;
1379       break;
1380     case X (OP_POSTINC, SL):    /* Register indirect w/post-incr: long.  */
1381       t = GET_L_REG (rn);
1382       t &= h8_get_mask (sd);
1383       r = GET_MEMORY_L (t);
1384       if (!twice)
1385         t += 4;
1386       t = t & h8_get_mask (sd);
1387       SET_L_REG (rn, t);
1388       *val = r;
1389       break;
1390
1391     case X (OP_POSTDEC, SB):    /* Register indirect w/post-decr: byte.  */
1392       t = GET_L_REG (rn);
1393       t &= h8_get_mask (sd);
1394       r = GET_MEMORY_B (t);
1395       if (!twice)
1396         t -= 1;
1397       t = t & h8_get_mask (sd);
1398       SET_L_REG (rn, t);
1399       *val = r;
1400       break;
1401     case X (OP_POSTDEC, SW):    /* Register indirect w/post-decr: word.  */
1402       t = GET_L_REG (rn);
1403       t &= h8_get_mask (sd);
1404       r = GET_MEMORY_W (t);
1405       if (!twice)
1406         t -= 2;
1407       t = t & h8_get_mask (sd);
1408       SET_L_REG (rn, t);
1409       *val = r;
1410       break;
1411     case X (OP_POSTDEC, SL):    /* Register indirect w/post-decr: long.  */
1412       t = GET_L_REG (rn);
1413       t &= h8_get_mask (sd);
1414       r = GET_MEMORY_L (t);
1415       if (!twice)
1416         t -= 4;
1417       t = t & h8_get_mask (sd);
1418       SET_L_REG (rn, t);
1419       *val = r;
1420       break;
1421
1422     case X (OP_PREDEC, SB):     /* Register indirect w/pre-decr: byte.  */
1423       t = GET_L_REG (rn) - 1;
1424       t &= h8_get_mask (sd);
1425       SET_L_REG (rn, t);
1426       *val = GET_MEMORY_B (t);
1427       break;
1428       
1429     case X (OP_PREDEC, SW):     /* Register indirect w/pre-decr: word.  */
1430       t = GET_L_REG (rn) - 2;
1431       t &= h8_get_mask (sd);
1432       SET_L_REG (rn, t);
1433       *val = GET_MEMORY_W (t);
1434       break;
1435       
1436     case X (OP_PREDEC, SL):     /* Register indirect w/pre-decr: long.  */
1437       t = GET_L_REG (rn) - 4;
1438       t &= h8_get_mask (sd);
1439       SET_L_REG (rn, t);
1440       *val = GET_MEMORY_L (t);
1441       break;
1442       
1443     case X (OP_PREINC, SB):     /* Register indirect w/pre-incr: byte.  */
1444       t = GET_L_REG (rn) + 1;
1445       t &= h8_get_mask (sd);
1446       SET_L_REG (rn, t);
1447       *val = GET_MEMORY_B (t);
1448       break;
1449
1450     case X (OP_PREINC, SW):     /* Register indirect w/pre-incr: long.  */
1451       t = GET_L_REG (rn) + 2;
1452       t &= h8_get_mask (sd);
1453       SET_L_REG (rn, t);
1454       *val = GET_MEMORY_W (t);
1455       break;
1456
1457     case X (OP_PREINC, SL):     /* Register indirect w/pre-incr: long.  */
1458       t = GET_L_REG (rn) + 4;
1459       t &= h8_get_mask (sd);
1460       SET_L_REG (rn, t);
1461       *val = GET_MEMORY_L (t);
1462       break;
1463
1464     case X (OP_DISP, SB):       /* Register indirect w/displacement: byte.  */
1465       t = GET_L_REG (rn) + abs;
1466       t &= h8_get_mask (sd);
1467       *val = GET_MEMORY_B (t);
1468       break;
1469
1470     case X (OP_DISP, SW):       /* Register indirect w/displacement: word.  */
1471       t = GET_L_REG (rn) + abs;
1472       t &= h8_get_mask (sd);
1473       *val = GET_MEMORY_W (t);
1474       break;
1475
1476     case X (OP_DISP, SL):       /* Register indirect w/displacement: long.  */
1477       t = GET_L_REG (rn) + abs;
1478       t &= h8_get_mask (sd);
1479       *val =GET_MEMORY_L (t);
1480       break;
1481
1482     case X (OP_MEM, SL):        /* Absolute memory address, long.  */
1483       t = GET_MEMORY_L (abs);
1484       t &= h8_get_mask (sd);
1485       *val = t;
1486       break;
1487
1488     case X (OP_MEM, SW):        /* Absolute memory address, word.  */
1489       t = GET_MEMORY_W (abs);
1490       t &= h8_get_mask (sd);
1491       *val = t;
1492       break;
1493
1494     case X (OP_PCREL, SB):      /* PC relative (for jump, branch etc).  */
1495     case X (OP_PCREL, SW):
1496     case X (OP_PCREL, SL):
1497     case X (OP_PCREL, SN):
1498       *val = abs;
1499       break;
1500
1501     case X (OP_MEM, SB):        /* Why isn't this implemented?  */
1502     default:
1503       sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1504       return -1;
1505     }
1506   return 0;     /* Success.  */
1507 }
1508
1509 /* Normal fetch.  */
1510
1511 static int
1512 fetch (SIM_DESC sd, ea_type *arg, int *val)
1513 {
1514   return fetch_1 (sd, arg, val, 0);
1515 }
1516
1517 /* Fetch which will be followed by a store to the same location.
1518    The difference being that we don't want to do a post-increment
1519    or post-decrement at this time: we'll do it when we store.  */
1520
1521 static int
1522 fetch2 (SIM_DESC sd, ea_type *arg, int *val)
1523 {
1524   return fetch_1 (sd, arg, val, 1);
1525 }
1526
1527 /* Simulate a memory store.
1528    Return 0 for success, -1 for failure.
1529 */
1530
1531 static int
1532 store_1 (SIM_DESC sd, ea_type *arg, int n, int twice)
1533 {
1534   int rn = arg->reg;
1535   int abs = arg->literal;
1536   int t;
1537
1538   switch (arg->type)
1539     {
1540       /* Indexed register plus displacement mode:
1541
1542          This new family of addressing modes are similar to OP_DISP
1543          (register plus displacement), with two differences:
1544            1) INDEXB uses only the least significant byte of the register,
1545               INDEXW uses only the least significant word, and
1546               INDEXL uses the entire register (just like OP_DISP).
1547          and
1548            2) The displacement value in abs is multiplied by two
1549               for SW-sized operations, and by four for SL-size.
1550
1551         This gives nine possible variations.
1552       */
1553
1554     case X (OP_INDEXB, SB):
1555     case X (OP_INDEXB, SW):
1556     case X (OP_INDEXB, SL):
1557     case X (OP_INDEXW, SB):
1558     case X (OP_INDEXW, SW):
1559     case X (OP_INDEXW, SL):
1560     case X (OP_INDEXL, SB):
1561     case X (OP_INDEXL, SW):
1562     case X (OP_INDEXL, SL):
1563       t = GET_L_REG (rn);
1564       switch (OP_KIND (arg->type)) {
1565       case OP_INDEXB:   t &= 0xff;      break;
1566       case OP_INDEXW:   t &= 0xffff;    break;
1567       case OP_INDEXL:
1568       default:          break;
1569       }
1570       switch (OP_SIZE (arg->type)) {
1571       case SB:
1572         SET_MEMORY_B ((t * 1 + abs) & h8_get_mask (sd), n);
1573         break;
1574       case SW:
1575         SET_MEMORY_W ((t * 2 + abs) & h8_get_mask (sd), n);
1576         break;
1577       case SL:
1578         SET_MEMORY_L ((t * 4 + abs) & h8_get_mask (sd), n);
1579         break;
1580       }
1581       break;
1582
1583     case X (OP_REG, SB):        /* Register direct, byte.  */
1584       SET_B_REG (rn, n);
1585       break;
1586     case X (OP_REG, SW):        /* Register direct, word.  */
1587       SET_W_REG (rn, n);
1588       break;
1589     case X (OP_REG, SL):        /* Register direct, long.  */
1590       SET_L_REG (rn, n);
1591       break;
1592
1593     case X (OP_PREDEC, SB):     /* Register indirect w/pre-decr, byte.  */
1594       t = GET_L_REG (rn);
1595       if (!twice)
1596         t -= 1;
1597       t &= h8_get_mask (sd);
1598       SET_L_REG (rn, t);
1599       SET_MEMORY_B (t, n);
1600
1601       break;
1602     case X (OP_PREDEC, SW):     /* Register indirect w/pre-decr, word.  */
1603       t = GET_L_REG (rn);
1604       if (!twice)
1605         t -= 2;
1606       t &= h8_get_mask (sd);
1607       SET_L_REG (rn, t);
1608       SET_MEMORY_W (t, n);
1609       break;
1610
1611     case X (OP_PREDEC, SL):     /* Register indirect w/pre-decr, long.  */
1612       t = GET_L_REG (rn);
1613       if (!twice)
1614         t -= 4;
1615       t &= h8_get_mask (sd);
1616       SET_L_REG (rn, t);
1617       SET_MEMORY_L (t, n);
1618       break;
1619
1620     case X (OP_PREINC, SB):     /* Register indirect w/pre-incr, byte.  */
1621       t = GET_L_REG (rn);
1622       if (!twice)
1623         t += 1;
1624       t &= h8_get_mask (sd);
1625       SET_L_REG (rn, t);
1626       SET_MEMORY_B (t, n);
1627
1628       break;
1629     case X (OP_PREINC, SW):     /* Register indirect w/pre-incr, word.  */
1630       t = GET_L_REG (rn);
1631       if (!twice)
1632         t += 2;
1633       t &= h8_get_mask (sd);
1634       SET_L_REG (rn, t);
1635       SET_MEMORY_W (t, n);
1636       break;
1637
1638     case X (OP_PREINC, SL):     /* Register indirect w/pre-incr, long.  */
1639       t = GET_L_REG (rn);
1640       if (!twice)
1641         t += 4;
1642       t &= h8_get_mask (sd);
1643       SET_L_REG (rn, t);
1644       SET_MEMORY_L (t, n);
1645       break;
1646
1647     case X (OP_POSTDEC, SB):    /* Register indirect w/post-decr, byte.  */
1648       t = GET_L_REG (rn) & h8_get_mask (sd);
1649       SET_MEMORY_B (t, n);
1650       SET_L_REG (rn, t - 1);
1651       break;
1652
1653     case X (OP_POSTDEC, SW):    /* Register indirect w/post-decr, word.  */
1654       t = GET_L_REG (rn) & h8_get_mask (sd);
1655       SET_MEMORY_W (t, n);
1656       SET_L_REG (rn, t - 2);
1657       break;
1658
1659     case X (OP_POSTDEC, SL):    /* Register indirect w/post-decr, long.  */
1660       t = GET_L_REG (rn) & h8_get_mask (sd);
1661       SET_MEMORY_L (t, n);
1662       SET_L_REG (rn, t - 4);
1663       break;
1664
1665     case X (OP_POSTINC, SB):    /* Register indirect w/post-incr, byte.  */
1666       t = GET_L_REG (rn) & h8_get_mask (sd);
1667       SET_MEMORY_B (t, n);
1668       SET_L_REG (rn, t + 1);
1669       break;
1670
1671     case X (OP_POSTINC, SW):    /* Register indirect w/post-incr, word.  */
1672       t = GET_L_REG (rn) & h8_get_mask (sd);
1673       SET_MEMORY_W (t, n);
1674       SET_L_REG (rn, t + 2);
1675       break;
1676
1677     case X (OP_POSTINC, SL):    /* Register indirect w/post-incr, long.  */
1678       t = GET_L_REG (rn) & h8_get_mask (sd);
1679       SET_MEMORY_L (t, n);
1680       SET_L_REG (rn, t + 4);
1681       break;
1682
1683     case X (OP_DISP, SB):       /* Register indirect w/displacement, byte.  */
1684       t = GET_L_REG (rn) + abs;
1685       t &= h8_get_mask (sd);
1686       SET_MEMORY_B (t, n);
1687       break;
1688
1689     case X (OP_DISP, SW):       /* Register indirect w/displacement, word.  */
1690       t = GET_L_REG (rn) + abs;
1691       t &= h8_get_mask (sd);
1692       SET_MEMORY_W (t, n);
1693       break;
1694
1695     case X (OP_DISP, SL):       /* Register indirect w/displacement, long.  */
1696       t = GET_L_REG (rn) + abs;
1697       t &= h8_get_mask (sd);
1698       SET_MEMORY_L (t, n);
1699       break;
1700
1701
1702     case X (OP_MEM, SB):        /* Why isn't this implemented?  */
1703     case X (OP_MEM, SW):        /* Why isn't this implemented?  */
1704     case X (OP_MEM, SL):        /* Why isn't this implemented?  */
1705     default:
1706       sim_engine_set_run_state (sd, sim_stopped, SIGSEGV);
1707       return -1;
1708     }
1709   return 0;
1710 }
1711
1712 /* Normal store.  */
1713
1714 static int
1715 store (SIM_DESC sd, ea_type *arg, int n)
1716 {
1717   return store_1 (sd, arg, n, 0);
1718 }
1719
1720 /* Store which follows a fetch from the same location.
1721    The difference being that we don't want to do a pre-increment
1722    or pre-decrement at this time: it was already done when we fetched.  */
1723
1724 static int
1725 store2 (SIM_DESC sd, ea_type *arg, int n)
1726 {
1727   return store_1 (sd, arg, n, 1);
1728 }
1729
1730 static union
1731 {
1732   short int i;
1733   struct
1734     {
1735       char low;
1736       char high;
1737     }
1738   u;
1739 } littleendian;
1740
1741 /* Flag to be set whenever a new SIM_DESC object is created.  */
1742 static int init_pointers_needed = 1;
1743
1744 static void
1745 init_pointers (SIM_DESC sd)
1746 {
1747   if (init_pointers_needed)
1748     {
1749       int i;
1750
1751       littleendian.i = 1;
1752
1753       if (h8300smode)
1754         memory_size = H8300S_MSIZE;
1755       else if (h8300hmode)
1756         memory_size = H8300H_MSIZE;
1757       else
1758         memory_size = H8300_MSIZE;
1759       /* `msize' must be a power of two.  */
1760       if ((memory_size & (memory_size - 1)) != 0)
1761         {
1762           (*sim_callback->printf_filtered) 
1763             (sim_callback,
1764              "init_pointers: bad memory size %d, defaulting to %d.\n", 
1765              memory_size, memory_size = H8300S_MSIZE);
1766         }
1767
1768       if (h8_get_memory_buf (sd))
1769         free (h8_get_memory_buf (sd));
1770       if (h8_get_cache_idx_buf (sd))
1771         free (h8_get_cache_idx_buf (sd));
1772       if (h8_get_eightbit_buf (sd))
1773         free (h8_get_eightbit_buf (sd));
1774
1775       h8_set_memory_buf (sd, (unsigned char *) 
1776                          calloc (sizeof (char), memory_size));
1777       h8_set_cache_idx_buf (sd, (unsigned short *) 
1778                             calloc (sizeof (short), memory_size));
1779       sd->memory_size = memory_size;
1780       h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
1781
1782       h8_set_mask (sd, memory_size - 1);
1783
1784       memset (h8_get_reg_buf (sd), 0, sizeof (((STATE_CPU (sd, 0))->regs)));
1785
1786       for (i = 0; i < 8; i++)
1787         {
1788           /* FIXME: rewrite using local buffer.  */
1789           unsigned char *p = (unsigned char *) (h8_get_reg_buf (sd) + i);
1790           unsigned char *e = (unsigned char *) (h8_get_reg_buf (sd) + i + 1);
1791           unsigned short *q = (unsigned short *) (h8_get_reg_buf (sd) + i);
1792           unsigned short *u = (unsigned short *) (h8_get_reg_buf (sd) + i + 1);
1793           h8_set_reg (sd, i, 0x00112233);
1794
1795           while (p < e)
1796             {
1797               if (*p == 0x22)
1798                   breg[i] = p;
1799               if (*p == 0x33)
1800                   breg[i + 8] = p;
1801               if (*p == 0x11)
1802                 breg[i + 16] = p;
1803               if (*p == 0x00)
1804                 breg[i + 24] = p;
1805               p++;
1806             }
1807
1808           wreg[i] = wreg[i + 8] = 0;
1809           while (q < u)
1810             {
1811               if (*q == 0x2233)
1812                 {
1813                   wreg[i] = q;
1814                 }
1815               if (*q == 0x0011)
1816                 {
1817                   wreg[i + 8] = q;
1818                 }
1819               q++;
1820             }
1821
1822           if (wreg[i] == 0 || wreg[i + 8] == 0)
1823             (*sim_callback->printf_filtered) (sim_callback, 
1824                                               "init_pointers: internal error.\n");
1825
1826           h8_set_reg (sd, i, 0);
1827           lreg[i] = h8_get_reg_buf (sd) + i;
1828         }
1829
1830       /* Note: sim uses pseudo-register ZERO as a zero register.  */
1831       lreg[ZERO_REGNUM] = h8_get_reg_buf (sd) + ZERO_REGNUM;
1832       init_pointers_needed = 0;
1833
1834       /* Initialize the seg registers.  */
1835       if (!sd->sim_cache)
1836         set_simcache_size (sd, CSIZE);
1837     }
1838 }
1839
1840 /* Grotty global variable for use by control_c signal handler.  */
1841 static SIM_DESC control_c_sim_desc;
1842
1843 static void
1844 control_c (int sig)
1845 {
1846   sim_engine_set_run_state (control_c_sim_desc, sim_stopped, SIGINT);
1847 }
1848
1849 int
1850 sim_stop (SIM_DESC sd)
1851 {
1852   /* FIXME: use a real signal value.  */
1853   sim_engine_set_run_state (sd, sim_stopped, SIGINT);
1854   return 1;
1855 }
1856
1857 #define OBITOP(name, f, s, op)                  \
1858 case O (name, SB):                              \
1859 {                                               \
1860   int m, tmp;                                   \
1861                                                 \
1862   if (f)                                        \
1863     if (fetch (sd, &code->dst, &ea))            \
1864       goto end;                                 \
1865   if (fetch (sd, &code->src, &tmp))             \
1866     goto end;                                   \
1867   m = 1 << tmp;                                 \
1868   op;                                           \
1869   if (s)                                        \
1870     if (store (sd, &code->dst,ea))              \
1871       goto end;                                 \
1872   goto next;                                    \
1873 }
1874
1875 void
1876 sim_resume (SIM_DESC sd, int step, int siggnal)
1877 {
1878   static int init1;
1879   int cycles = 0;
1880   int insts = 0;
1881   int tick_start = get_now ();
1882   void (*prev) ();
1883   int poll_count = 0;
1884   int res;
1885   int tmp;
1886   int rd;
1887   int ea;
1888   int bit;
1889   int pc;
1890   int c, nz, v, n, u, h, ui, intMaskBit;
1891   int trace, intMask;
1892   int oldmask;
1893   enum sim_stop reason;
1894   int sigrc;
1895
1896   init_pointers (sd);
1897
1898   control_c_sim_desc = sd;
1899   prev = signal (SIGINT, control_c);
1900
1901   if (step)
1902     {
1903       sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
1904     }
1905   else
1906     {
1907       sim_engine_set_run_state (sd, sim_running, 0);
1908     }
1909
1910   pc = h8_get_pc (sd);
1911
1912   /* The PC should never be odd.  */
1913   if (pc & 0x1)
1914     {
1915       sim_engine_set_run_state (sd, sim_stopped, SIGBUS);
1916       return;
1917     }
1918
1919   /* Get Status Register (flags).  */
1920   GETSR (sd);
1921
1922   if (h8300smode)       /* Get exr.  */
1923     {
1924       trace = (h8_get_exr (sd) >> 7) & 1;
1925       intMask = h8_get_exr (sd) & 7;
1926     }
1927
1928   oldmask = h8_get_mask (sd);
1929   if (!h8300hmode)
1930     h8_set_mask (sd, 0xffff);
1931   do
1932     {
1933       unsigned short cidx;
1934       decoded_inst *code;
1935
1936     top:
1937       cidx = h8_get_cache_idx (sd, pc);
1938       if (cidx == (unsigned short) -1 ||
1939           cidx >= sd->sim_cache_size)
1940         goto illegal;
1941           
1942       code = sd->sim_cache + cidx;
1943
1944 #if ADEBUG
1945       if (debug)
1946         {
1947           printf ("%x %d %s\n", pc, code->opcode,
1948                   code->op ? code->op->name : "**");
1949         }
1950       h8_increment_stats (sd, code->opcode);
1951 #endif
1952
1953       if (code->opcode)
1954         {
1955           cycles += code->cycles;
1956           insts++;
1957         }
1958
1959       switch (code->opcode)
1960         {
1961         case 0:
1962           /*
1963            * This opcode is a fake for when we get to an
1964            * instruction which hasnt been compiled
1965            */
1966           compile (sd, pc);
1967           goto top;
1968           break;
1969
1970         case O (O_MOVAB, SL):
1971         case O (O_MOVAW, SL):
1972         case O (O_MOVAL, SL):
1973           /* 1) Evaluate 2nd argument (dst).
1974              2) Mask / zero extend according to whether 1st argument (src)
1975                 is INDEXB, INDEXW, or INDEXL.
1976              3) Left-shift the result by 0, 1 or 2, according to size of mova
1977                 (mova/b, mova/w, mova/l).
1978              4) Add literal value of 1st argument (src).
1979              5) Store result in 3rd argument (op3).
1980
1981           */
1982           if (fetch (sd, &code->dst, &ea))
1983             goto end;
1984
1985           switch (OP_KIND (code->src.type)) {
1986           case OP_INDEXB:    ea = ea & 0xff;            break;
1987           case OP_INDEXW:    ea = ea & 0xffff;          break;
1988           case OP_INDEXL:                               break;
1989           default:           goto illegal;
1990           }
1991
1992           switch (code->opcode) {
1993           case O (O_MOVAB, SL):                         break;
1994           case O (O_MOVAW, SL):     ea = ea << 1;       break;
1995           case O (O_MOVAL, SL):     ea = ea << 2;       break;
1996           default:                  goto illegal;
1997           }
1998           
1999           ea = ea + code->src.literal;
2000
2001           if (store (sd, &code->op3, ea))
2002             goto end;
2003
2004           goto next;      
2005
2006         case O (O_SUBX, SB):    /* subx, extended sub */
2007           if (fetch2 (sd, &code->dst, &rd))
2008             goto end;
2009           if (fetch (sd, &code->src, &ea))
2010             goto end;
2011           ea = -(ea + C);
2012           res = rd + ea;
2013           goto alu8;
2014
2015         case O (O_SUBX, SW):    /* subx, extended sub */
2016           if (fetch2 (sd, &code->dst, &rd))
2017             goto end;
2018           if (fetch (sd, &code->src, &ea))
2019             goto end;
2020           ea = -(ea + C);
2021           res = rd + ea;
2022           goto alu16;
2023
2024         case O (O_SUBX, SL):    /* subx, extended sub */
2025           if (fetch2 (sd, &code->dst, &rd))
2026             goto end;
2027           if (fetch (sd, &code->src, &ea))
2028             goto end;
2029           ea = -(ea + C);
2030           res = rd + ea;
2031           goto alu32;
2032
2033         case O (O_ADDX, SB):    /* addx, extended add */
2034           if (fetch2 (sd, &code->dst, &rd))
2035             goto end;
2036           if (fetch (sd, &code->src, &ea))
2037             goto end;
2038           ea = ea + C;
2039           res = rd + ea;
2040           goto alu8;
2041
2042         case O (O_ADDX, SW):    /* addx, extended add */
2043           if (fetch2 (sd, &code->dst, &rd))
2044             goto end;
2045           if (fetch (sd, &code->src, &ea))
2046             goto end;
2047           ea = ea + C;
2048           res = rd + ea;
2049           goto alu16;
2050
2051         case O (O_ADDX, SL):    /* addx, extended add */
2052           if (fetch2 (sd, &code->dst, &rd))
2053             goto end;
2054           if (fetch (sd, &code->src, &ea))
2055             goto end;
2056           ea = ea + C;
2057           res = rd + ea;
2058           goto alu32;
2059
2060         case O (O_SUB, SB):             /* sub.b */
2061           /* Fetch rd and ea.  */
2062           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2063             goto end;
2064           ea = -ea;
2065           res = rd + ea;
2066           goto alu8;
2067
2068         case O (O_SUB, SW):             /* sub.w */
2069           /* Fetch rd and ea.  */
2070           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2071             goto end;
2072           ea = -ea;
2073           res = rd + ea;
2074           goto alu16;
2075
2076         case O (O_SUB, SL):             /* sub.l */
2077           /* Fetch rd and ea.  */
2078           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2079             goto end;
2080           ea = -ea;
2081           res = rd + ea;
2082           goto alu32;
2083
2084         case O (O_NEG, SB):             /* neg.b */
2085           /* Fetch ea.  */
2086           if (fetch2 (sd, &code->src, &ea)) 
2087             goto end;
2088           ea = -ea;
2089           rd = 0;
2090           res = rd + ea;
2091           goto alu8;
2092
2093         case O (O_NEG, SW):             /* neg.w */
2094           /* Fetch ea.  */
2095           if (fetch2 (sd, &code->src, &ea)) 
2096             goto end;
2097           ea = -ea;
2098           rd = 0;
2099           res = rd + ea;
2100           goto alu16;
2101
2102         case O (O_NEG, SL):             /* neg.l */
2103           /* Fetch ea.  */
2104           if (fetch2 (sd, &code->src, &ea)) 
2105             goto end;
2106           ea = -ea;
2107           rd = 0;
2108           res = rd + ea;
2109           goto alu32;
2110
2111         case O (O_ADD, SB):             /* add.b */
2112           if (fetch2 (sd, &code->dst, &rd))
2113             goto end;
2114           if (fetch (sd, &code->src, &ea))
2115             goto end;
2116           res = rd + ea;
2117           goto alu8;
2118
2119         case O (O_ADD, SW):             /* add.w */
2120           if (fetch2 (sd, &code->dst, &rd))
2121             goto end;
2122           if (fetch (sd, &code->src, &ea))
2123             goto end;
2124           res = rd + ea;
2125           goto alu16;
2126
2127         case O (O_ADD, SL):             /* add.l */
2128           if (fetch2 (sd, &code->dst, &rd))
2129             goto end;
2130           if (fetch (sd, &code->src, &ea))
2131             goto end;
2132           res = rd + ea;
2133           goto alu32;
2134
2135         case O (O_AND, SB):             /* and.b */
2136           /* Fetch rd and ea.  */
2137           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2138             goto end;
2139           res = rd & ea;
2140           goto log8;
2141
2142         case O (O_AND, SW):             /* and.w */
2143           /* Fetch rd and ea.  */
2144           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2145             goto end;
2146           res = rd & ea;
2147           goto log16;
2148
2149         case O (O_AND, SL):             /* and.l */
2150           /* Fetch rd and ea.  */
2151           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2152             goto end;
2153           res = rd & ea;
2154           goto log32;
2155
2156         case O (O_OR, SB):              /* or.b */
2157           /* Fetch rd and ea.  */
2158           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2159             goto end;
2160           res = rd | ea;
2161           goto log8;
2162
2163         case O (O_OR, SW):              /* or.w */
2164           /* Fetch rd and ea.  */
2165           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2166             goto end;
2167           res = rd | ea;
2168           goto log16;
2169
2170         case O (O_OR, SL):              /* or.l */
2171           /* Fetch rd and ea.  */
2172           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2173             goto end;
2174           res = rd | ea;
2175           goto log32;
2176
2177         case O (O_XOR, SB):             /* xor.b */
2178           /* Fetch rd and ea.  */
2179           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2180             goto end;
2181           res = rd ^ ea;
2182           goto log8;
2183
2184         case O (O_XOR, SW):             /* xor.w */
2185           /* Fetch rd and ea.  */
2186           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2187             goto end;
2188           res = rd ^ ea;
2189           goto log16;
2190
2191         case O (O_XOR, SL):             /* xor.l */
2192           /* Fetch rd and ea.  */
2193           if (fetch (sd, &code->src, &ea) || fetch2 (sd, &code->dst, &rd)) 
2194             goto end;
2195           res = rd ^ ea;
2196           goto log32;
2197
2198         case O (O_MOV, SB):
2199           if (fetch (sd, &code->src, &res))
2200             goto end;
2201           if (store (sd, &code->dst, res))
2202             goto end;
2203           goto just_flags_log8;
2204         case O (O_MOV, SW):
2205           if (fetch (sd, &code->src, &res))
2206             goto end;
2207           if (store (sd, &code->dst, res))
2208             goto end;
2209           goto just_flags_log16;
2210         case O (O_MOV, SL):
2211           if (fetch (sd, &code->src, &res))
2212             goto end;
2213           if (store (sd, &code->dst, res))
2214             goto end;
2215           goto just_flags_log32;
2216
2217         case O (O_MOVMD, SB):           /* movmd.b */
2218           ea = GET_W_REG (4);
2219           if (ea == 0)
2220             ea = 0x10000;
2221
2222           while (ea--)
2223             {
2224               rd = GET_MEMORY_B (GET_L_REG (5));
2225               SET_MEMORY_B (GET_L_REG (6), rd);
2226               SET_L_REG (5, GET_L_REG (5) + 1);
2227               SET_L_REG (6, GET_L_REG (6) + 1);
2228               SET_W_REG (4, ea);
2229             }
2230           goto next;
2231
2232         case O (O_MOVMD, SW):           /* movmd.w */
2233           ea = GET_W_REG (4);
2234           if (ea == 0)
2235             ea = 0x10000;
2236
2237           while (ea--)
2238             {
2239               rd = GET_MEMORY_W (GET_L_REG (5));
2240               SET_MEMORY_W (GET_L_REG (6), rd);
2241               SET_L_REG (5, GET_L_REG (5) + 2);
2242               SET_L_REG (6, GET_L_REG (6) + 2);
2243               SET_W_REG (4, ea);
2244             }
2245           goto next;
2246
2247         case O (O_MOVMD, SL):           /* movmd.l */
2248           ea = GET_W_REG (4);
2249           if (ea == 0)
2250             ea = 0x10000;
2251
2252           while (ea--)
2253             {
2254               rd = GET_MEMORY_L (GET_L_REG (5));
2255               SET_MEMORY_L (GET_L_REG (6), rd);
2256               SET_L_REG (5, GET_L_REG (5) + 4);
2257               SET_L_REG (6, GET_L_REG (6) + 4);
2258               SET_W_REG (4, ea);
2259             }
2260           goto next;
2261
2262         case O (O_MOVSD, SB):           /* movsd.b */
2263           /* This instruction implements strncpy, with a conditional branch.
2264              r4 contains n, r5 contains src, and r6 contains dst.
2265              The 16-bit displacement operand is added to the pc
2266              if and only if the end of string is reached before
2267              n bytes are transferred.  */
2268
2269           ea = GET_L_REG (4) & 0xffff;
2270           if (ea == 0)
2271             ea = 0x10000;
2272
2273           while (ea--)
2274             {
2275               rd = GET_MEMORY_B (GET_L_REG (5));
2276               SET_MEMORY_B (GET_L_REG (6), rd);
2277               SET_L_REG (5, GET_L_REG (5) + 1);
2278               SET_L_REG (6, GET_L_REG (6) + 1);
2279               SET_W_REG (4, ea); 
2280               if (rd == 0)
2281                 goto condtrue;
2282             }
2283           goto next;
2284
2285         case O (O_EEPMOV, SB):          /* eepmov.b */
2286         case O (O_EEPMOV, SW):          /* eepmov.w */
2287           if (h8300hmode || h8300smode)
2288             {
2289               register unsigned char *_src, *_dst;
2290               unsigned int count = ((code->opcode == O (O_EEPMOV, SW))
2291                                     ? h8_get_reg (sd, R4_REGNUM) & 0xffff
2292                                     : h8_get_reg (sd, R4_REGNUM) & 0xff);
2293
2294               _src = (h8_get_reg (sd, R5_REGNUM) < memory_size
2295                       ? h8_get_memory_buf   (sd) + h8_get_reg (sd, R5_REGNUM)
2296                       : h8_get_eightbit_buf (sd) + 
2297                        (h8_get_reg (sd, R5_REGNUM) & 0xff));
2298               if ((_src + count) >= (h8_get_memory_buf (sd) + memory_size))
2299                 {
2300                   if ((_src + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2301                     goto illegal;
2302                 }
2303               _dst = (h8_get_reg (sd, R6_REGNUM) < memory_size
2304                       ? h8_get_memory_buf   (sd) + h8_get_reg (sd, R6_REGNUM)
2305                       : h8_get_eightbit_buf (sd) + 
2306                        (h8_get_reg (sd, R6_REGNUM) & 0xff));
2307
2308               if ((_dst + count) >= (h8_get_memory_buf (sd) + memory_size))
2309                 {
2310                   if ((_dst + count) >= (h8_get_eightbit_buf (sd) + 0x100))
2311                     goto illegal;
2312                 }
2313               memcpy (_dst, _src, count);
2314
2315               h8_set_reg (sd, R5_REGNUM, h8_get_reg (sd, R5_REGNUM) + count);
2316               h8_set_reg (sd, R6_REGNUM, h8_get_reg (sd, R6_REGNUM) + count);
2317               h8_set_reg (sd, R4_REGNUM, h8_get_reg (sd, R4_REGNUM) &
2318                           ((code->opcode == O (O_EEPMOV, SW))
2319                           ? (~0xffff) : (~0xff)));
2320               cycles += 2 * count;
2321               goto next;
2322             }
2323           goto illegal;
2324
2325         case O (O_ADDS, SL):            /* adds (.l) */
2326           /* FIXME fetch.
2327            * This insn only uses register operands, but still
2328            * it would be cleaner to use fetch and store...  */    
2329           SET_L_REG (code->dst.reg,
2330                      GET_L_REG (code->dst.reg)
2331                      + code->src.literal);
2332
2333           goto next;
2334
2335         case O (O_SUBS, SL):            /* subs (.l) */
2336           /* FIXME fetch.
2337            * This insn only uses register operands, but still
2338            * it would be cleaner to use fetch and store...  */    
2339           SET_L_REG (code->dst.reg,
2340                      GET_L_REG (code->dst.reg)
2341                      - code->src.literal);
2342           goto next;
2343
2344         case O (O_CMP, SB):             /* cmp.b */
2345           if (fetch (sd, &code->dst, &rd))
2346             goto end;
2347           if (fetch (sd, &code->src, &ea))
2348             goto end;
2349           ea = -ea;
2350           res = rd + ea;
2351           goto just_flags_alu8;
2352
2353         case O (O_CMP, SW):             /* cmp.w */
2354           if (fetch (sd, &code->dst, &rd))
2355             goto end;
2356           if (fetch (sd, &code->src, &ea))
2357             goto end;
2358           ea = -ea;
2359           res = rd + ea;
2360           goto just_flags_alu16;
2361
2362         case O (O_CMP, SL):             /* cmp.l */
2363           if (fetch (sd, &code->dst, &rd))
2364             goto end;
2365           if (fetch (sd, &code->src, &ea))
2366             goto end;
2367           ea = -ea;
2368           res = rd + ea;
2369           goto just_flags_alu32;
2370
2371         case O (O_DEC, SB):             /* dec.b */
2372           /* FIXME fetch.
2373            * This insn only uses register operands, but still
2374            * it would be cleaner to use fetch and store...  */    
2375           rd = GET_B_REG (code->src.reg);
2376           ea = -1;
2377           res = rd + ea;
2378           SET_B_REG (code->src.reg, res);
2379           goto just_flags_inc8;
2380
2381         case O (O_DEC, SW):             /* dec.w */
2382           /* FIXME fetch.
2383            * This insn only uses register operands, but still
2384            * it would be cleaner to use fetch and store...  */    
2385           rd = GET_W_REG (code->dst.reg);
2386           ea = -code->src.literal;
2387           res = rd + ea;
2388           SET_W_REG (code->dst.reg, res);
2389           goto just_flags_inc16;
2390
2391         case O (O_DEC, SL):             /* dec.l */
2392           /* FIXME fetch.
2393            * This insn only uses register operands, but still
2394            * it would be cleaner to use fetch and store...  */    
2395           rd = GET_L_REG (code->dst.reg);
2396           ea = -code->src.literal;
2397           res = rd + ea;
2398           SET_L_REG (code->dst.reg, res);
2399           goto just_flags_inc32;
2400
2401         case O (O_INC, SB):             /* inc.b */
2402           /* FIXME fetch.
2403            * This insn only uses register operands, but still
2404            * it would be cleaner to use fetch and store...  */    
2405           rd = GET_B_REG (code->src.reg);
2406           ea = 1;
2407           res = rd + ea;
2408           SET_B_REG (code->src.reg, res);
2409           goto just_flags_inc8;
2410
2411         case O (O_INC, SW):             /* inc.w */
2412           /* FIXME fetch.
2413            * This insn only uses register operands, but still
2414            * it would be cleaner to use fetch and store...  */    
2415           rd = GET_W_REG (code->dst.reg);
2416           ea = code->src.literal;
2417           res = rd + ea;
2418           SET_W_REG (code->dst.reg, res);
2419           goto just_flags_inc16;
2420
2421         case O (O_INC, SL):             /* inc.l */
2422           /* FIXME fetch.
2423            * This insn only uses register operands, but still
2424            * it would be cleaner to use fetch and store...  */    
2425           rd = GET_L_REG (code->dst.reg);
2426           ea = code->src.literal;
2427           res = rd + ea;
2428           SET_L_REG (code->dst.reg, res);
2429           goto just_flags_inc32;
2430
2431         case O (O_LDC, SB):             /* ldc.b */
2432           if (fetch (sd, &code->src, &res))
2433             goto end;
2434           goto setc;
2435
2436         case O (O_LDC, SW):             /* ldc.w */
2437           if (fetch (sd, &code->src, &res))
2438             goto end;
2439
2440           /* Word operand, value from MSB, must be shifted.  */
2441           res >>= 8;
2442           goto setc;
2443
2444         case O (O_LDC, SL):             /* ldc.l */
2445           if (fetch (sd, &code->src, &res))
2446             goto end;
2447           switch (code->dst.type) {
2448           case X (OP_SBR, SL):
2449             h8_set_sbr (sd, res);
2450             break;
2451           case X (OP_VBR, SL):
2452             h8_set_vbr (sd, res);
2453             break;
2454           default:
2455             goto illegal;
2456           }
2457           goto next;
2458
2459         case O (O_STC, SW):             /* stc.w */
2460         case O (O_STC, SB):             /* stc.b */
2461           if (code->src.type == X (OP_CCR, SB))
2462             {
2463               BUILDSR (sd);
2464               res = h8_get_ccr (sd);
2465             }
2466           else if (code->src.type == X (OP_EXR, SB) && h8300smode)
2467             {
2468               if (h8300smode)
2469                 h8_set_exr (sd, (trace << 7) | intMask);
2470               res = h8_get_exr (sd);
2471             }
2472           else
2473             goto illegal;
2474
2475           /* Word operand, value to MSB, must be shifted.  */
2476           if (code->opcode == X (O_STC, SW))
2477             res <<= 8;
2478           if (store (sd, &code->dst, res))
2479             goto end;
2480           goto next;
2481         case O (O_STC, SL):             /* stc.l */
2482           switch (code->src.type) {
2483           case X (OP_SBR, SL):
2484             res = h8_get_sbr (sd);
2485             break;
2486           case X (OP_VBR, SL):
2487             res = h8_get_vbr (sd);
2488             break;
2489           default:
2490             goto illegal;
2491           }
2492           if (store (sd, &code->dst, res))
2493             goto end;
2494           goto next;
2495
2496         case O (O_ANDC, SB):            /* andc.b */
2497           if (code->dst.type == X (OP_CCR, SB))
2498             {
2499               BUILDSR (sd);
2500               rd = h8_get_ccr (sd);
2501             }
2502           else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2503             {
2504               if (h8300smode)
2505                 h8_set_exr (sd, (trace << 7) | intMask);
2506               res = h8_get_exr (sd);
2507             }
2508           else
2509             goto illegal;
2510           ea = code->src.literal;
2511           res = rd & ea;
2512           goto setc;
2513
2514         case O (O_ORC, SB):             /* orc.b */
2515           if (code->dst.type == X (OP_CCR, SB))
2516             {
2517               BUILDSR (sd);
2518               rd = h8_get_ccr (sd);
2519             }
2520           else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2521             {
2522               if (h8300smode)
2523                 h8_set_exr (sd, (trace << 7) | intMask);
2524               rd = h8_get_exr (sd);
2525             }
2526           else
2527             goto illegal;
2528           ea = code->src.literal;
2529           res = rd | ea;
2530           goto setc;
2531
2532         case O (O_XORC, SB):            /* xorc.b */
2533           if (code->dst.type == X (OP_CCR, SB))
2534             {
2535               BUILDSR (sd);
2536               rd = h8_get_ccr (sd);
2537             }
2538           else if (code->dst.type == X (OP_EXR, SB) && h8300smode)
2539             {
2540               if (h8300smode)
2541                 h8_set_exr (sd, (trace << 7) | intMask);
2542               rd = h8_get_exr (sd);
2543             }
2544           else
2545             goto illegal;
2546           ea = code->src.literal;
2547           res = rd ^ ea;
2548           goto setc;
2549
2550         case O (O_BRAS, SB):            /* bra/s  */
2551           /* This is basically an ordinary branch, with a delay slot.  */
2552           if (fetch (sd, &code->src, &res))
2553             goto end;
2554
2555           if ((res & 1) == 0)
2556             goto illegal;
2557
2558           res -= 1;
2559
2560           /* Execution continues at next instruction, but
2561              delayed_branch is set up for next cycle.  */
2562           h8_set_delayed_branch (sd, code->next_pc + res);
2563           pc = code->next_pc;
2564           goto end;
2565
2566         case O (O_BRAB, SB):            /* bra rd.b */
2567         case O (O_BRAW, SW):            /* bra rd.w */
2568         case O (O_BRAL, SL):            /* bra erd.l */
2569           if (fetch (sd, &code->src, &rd))
2570             goto end;
2571           switch (OP_SIZE (code->opcode)) {
2572           case SB:      rd &= 0xff;             break;
2573           case SW:      rd &= 0xffff;           break;
2574           case SL:      rd &= 0xffffffff;       break;
2575           }
2576           pc = code->next_pc + rd;
2577           goto end;
2578
2579         case O (O_BRABC, SB):           /* bra/bc, branch if bit clear */
2580         case O (O_BRABS, SB):           /* bra/bs, branch if bit set   */
2581         case O (O_BSRBC, SB):           /* bsr/bc, call   if bit clear */
2582         case O (O_BSRBS, SB):           /* bsr/bs, call   if bit set   */
2583           if (fetch (sd, &code->dst, &rd) ||
2584               fetch (sd, &code->src, &bit))
2585             goto end;
2586
2587           if (code->opcode == O (O_BRABC, SB) || /* branch if clear */
2588               code->opcode == O (O_BSRBC, SB))   /* call   if clear */
2589             {
2590               if ((rd & (1 << bit)))            /* no branch */
2591                 goto next;
2592             }
2593           else                                  /* branch/call if set */
2594             {
2595               if (!(rd & (1 << bit)))           /* no branch */
2596                 goto next;
2597             }
2598
2599           if (fetch (sd, &code->op3, &res))     /* branch */
2600             goto end;
2601           pc = code->next_pc + res;
2602
2603           if (code->opcode == O (O_BRABC, SB) ||
2604               code->opcode == O (O_BRABS, SB))  /* branch */
2605             goto end;
2606           else                                  /* call   */
2607             goto call;
2608
2609         case O (O_BRA, SN):
2610         case O (O_BRA, SL):
2611         case O (O_BRA, SW):
2612         case O (O_BRA, SB):             /* bra, branch always */
2613           if (1)
2614             goto condtrue;
2615           goto next;
2616
2617         case O (O_BRN, SB):             /* brn, ;-/  branch never? */
2618           if (0)
2619             goto condtrue;
2620           goto next;
2621
2622         case O (O_BHI, SB):             /* bhi */
2623           if ((C || Z) == 0)
2624             goto condtrue;
2625           goto next;
2626
2627
2628         case O (O_BLS, SB):             /* bls */
2629           if ((C || Z))
2630             goto condtrue;
2631           goto next;
2632
2633         case O (O_BCS, SB):             /* bcs, branch if carry set */
2634           if ((C == 1))
2635             goto condtrue;
2636           goto next;
2637
2638         case O (O_BCC, SB):             /* bcc, branch if carry clear */
2639           if ((C == 0))
2640             goto condtrue;
2641           goto next;
2642
2643         case O (O_BEQ, SB):             /* beq, branch if zero set */
2644           if (Z)
2645             goto condtrue;
2646           goto next;
2647         case O (O_BGT, SB):             /* bgt */
2648           if (((Z || (N ^ V)) == 0))
2649             goto condtrue;
2650           goto next;
2651
2652         case O (O_BLE, SB):             /* ble */
2653           if (((Z || (N ^ V)) == 1))
2654             goto condtrue;
2655           goto next;
2656
2657         case O (O_BGE, SB):             /* bge */
2658           if ((N ^ V) == 0)
2659             goto condtrue;
2660           goto next;
2661         case O (O_BLT, SB):             /* blt */
2662           if ((N ^ V))
2663             goto condtrue;
2664           goto next;
2665         case O (O_BMI, SB):             /* bmi */
2666           if ((N))
2667             goto condtrue;
2668           goto next;
2669         case O (O_BNE, SB):             /* bne, branch if zero clear */
2670           if ((Z == 0))
2671             goto condtrue;
2672           goto next;
2673
2674         case O (O_BPL, SB):             /* bpl */
2675           if (N == 0)
2676             goto condtrue;
2677           goto next;
2678         case O (O_BVC, SB):             /* bvc */
2679           if ((V == 0))
2680             goto condtrue;
2681           goto next;
2682         case O (O_BVS, SB):             /* bvs */
2683           if ((V == 1))
2684             goto condtrue;
2685           goto next;
2686
2687         /* Trap for Command Line setup.  */
2688         case O (O_SYS_CMDLINE, SB):
2689           {
2690             int i = 0;          /* Loop counter.  */
2691             int j = 0;          /* Loop counter.  */
2692             int ind_arg_len = 0;        /* Length of each argument.  */
2693             int no_of_args = 0; /* The no. or cmdline args.  */
2694             int current_location = 0;   /* Location of string.  */
2695             int old_sp = 0;     /* The Initial Stack Pointer.  */
2696             int no_of_slots = 0;        /* No. of slots required on the stack
2697                                            for storing cmdline args.  */
2698             int sp_move = 0;    /* No. of locations by which the stack needs
2699                                    to grow.  */
2700             int new_sp = 0;     /* The final stack pointer location passed
2701                                    back.  */
2702             int *argv_ptrs;     /* Pointers of argv strings to be stored.  */
2703             int argv_ptrs_location = 0; /* Location of pointers to cmdline
2704                                            args on the stack.  */
2705             int char_ptr_size = 0;      /* Size of a character pointer on
2706                                            target machine.  */
2707             int addr_cmdline = 0;       /* Memory location where cmdline has
2708                                            to be stored.  */
2709             int size_cmdline = 0;       /* Size of cmdline.  */
2710
2711             /* Set the address of 256 free locations where command line is
2712                stored.  */
2713             addr_cmdline = cmdline_location();
2714             h8_set_reg (sd, 0, addr_cmdline);
2715
2716             /* Counting the no. of commandline arguments.  */
2717             for (i = 0; h8_get_cmdline_arg (sd, i) != NULL; i++)
2718               continue;
2719
2720             /* No. of arguments in the command line.  */
2721             no_of_args = i;
2722
2723             /* Current location is just a temporary variable,which we are
2724                setting to the point to the start of our commandline string.  */
2725             current_location = addr_cmdline;
2726
2727             /* Allocating space for storing pointers of the command line
2728                arguments.  */
2729             argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
2730
2731             /* Setting char_ptr_size to the sizeof (char *) on the different
2732                architectures.  */
2733             if (h8300hmode || h8300smode)
2734               {
2735                 char_ptr_size = 4;
2736               }
2737             else
2738               {
2739                 char_ptr_size = 2;
2740               }
2741
2742             for (i = 0; i < no_of_args; i++)
2743               {
2744                 ind_arg_len = 0;
2745
2746                 /* The size of the commandline argument.  */
2747                 ind_arg_len = strlen (h8_get_cmdline_arg (sd, i) + 1);
2748
2749                 /* The total size of the command line string.  */
2750                 size_cmdline += ind_arg_len;
2751
2752                 /* As we have only 256 bytes, we need to provide a graceful
2753                    exit. Anyways, a program using command line arguments 
2754                    where we cannot store all the command line arguments
2755                    given may behave unpredictably.  */
2756                 if (size_cmdline >= 256)
2757                   {
2758                     h8_set_reg (sd, 0, 0);
2759                     goto next;
2760                   }
2761                 else
2762                   {
2763                     /* current_location points to the memory where the next
2764                        commandline argument is stored.  */
2765                     argv_ptrs[i] = current_location;
2766                     for (j = 0; j < ind_arg_len; j++)
2767                       {
2768                         SET_MEMORY_B ((current_location +
2769                                        (sizeof (char) * j)),
2770                                       *(h8_get_cmdline_arg (sd, i) + 
2771                                        sizeof (char) * j));
2772                       }
2773
2774                     /* Setting current_location to the starting of next
2775                        argument.  */
2776                     current_location += ind_arg_len;
2777                   }
2778               }
2779
2780             /* This is the original position of the stack pointer.  */
2781             old_sp = h8_get_reg (sd, SP_REGNUM);
2782
2783             /* We need space from the stack to store the pointers to argvs.  */
2784             /* As we will infringe on the stack, we need to shift the stack
2785                pointer so that the data is not overwritten. We calculate how
2786                much space is required.  */
2787             sp_move = (no_of_args) * (char_ptr_size);
2788
2789             /* The final position of stack pointer, we have thus taken some
2790                space from the stack.  */
2791             new_sp = old_sp - sp_move;
2792
2793             /* Temporary variable holding value where the argv pointers need
2794                to be stored.  */
2795             argv_ptrs_location = new_sp;
2796
2797             /* The argv pointers are stored at sequential locations. As per
2798                the H8300 ABI.  */
2799             for (i = 0; i < no_of_args; i++)
2800               {
2801                 /* Saving the argv pointer.  */
2802                 if (h8300hmode || h8300smode)
2803                   {
2804                     SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
2805                   }
2806                 else
2807                   {
2808                     SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
2809                   }
2810         
2811                 /* The next location where the pointer to the next argv
2812                    string has to be stored.  */    
2813                 argv_ptrs_location += char_ptr_size;
2814               }
2815
2816             /* Required by POSIX, Setting 0x0 at the end of the list of argv
2817                pointers.  */
2818             if (h8300hmode || h8300smode)
2819               {
2820                 SET_MEMORY_L (old_sp, 0x0);
2821               }
2822             else
2823               {
2824                 SET_MEMORY_W (old_sp, 0x0);
2825               }
2826
2827             /* Freeing allocated memory.  */
2828             free (argv_ptrs);
2829             for (i = 0; i <= no_of_args; i++)
2830               {
2831                 free (h8_get_cmdline_arg (sd, i));
2832               }
2833             free (h8_get_command_line (sd));
2834
2835             /* The no. of argv arguments are returned in Reg 0.  */
2836             h8_set_reg (sd, 0, no_of_args);
2837             /* The Pointer to argv in Register 1.  */
2838             h8_set_reg (sd, 1, new_sp);
2839             /* Setting the stack pointer to the new value.  */
2840             h8_set_reg (sd, SP_REGNUM, new_sp);
2841           }
2842           goto next;
2843
2844           /* System call processing starts.  */
2845         case O (O_SYS_OPEN, SB):
2846           {
2847             int len = 0;        /* Length of filename.  */
2848             char *filename;     /* Filename would go here.  */
2849             char temp_char;     /* Temporary character */
2850             int mode = 0;       /* Mode bits for the file.  */
2851             int open_return;    /* Return value of open, file descriptor.  */
2852             int i;              /* Loop counter */
2853             int filename_ptr;   /* Pointer to filename in cpu memory.  */
2854
2855             /* Setting filename_ptr to first argument of open,  */
2856             /* and trying to get mode.  */
2857             if (h8300sxmode || h8300hmode || h8300smode)
2858               {
2859                 filename_ptr = GET_L_REG (0);
2860                 mode = GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM) + 4);
2861               }
2862             else
2863               {
2864                 filename_ptr = GET_W_REG (0);
2865                 mode = GET_MEMORY_W (h8_get_reg (sd, SP_REGNUM) + 2);
2866               }
2867
2868             /* Trying to find the length of the filename.  */
2869             temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
2870
2871             len = 1;
2872             while (temp_char != '\0')
2873               {
2874                 temp_char = GET_MEMORY_B (filename_ptr + len);
2875                 len++;
2876               }
2877
2878             /* Allocating space for the filename.  */
2879             filename = (char *) malloc (sizeof (char) * len);
2880
2881             /* String copying the filename from memory.  */
2882             for (i = 0; i < len; i++)
2883               {
2884                 temp_char = GET_MEMORY_B (filename_ptr + i);
2885                 filename[i] = temp_char;
2886               }
2887
2888             /* Callback to open and return the file descriptor.  */
2889             open_return = sim_callback->open (sim_callback, filename, mode);
2890
2891             /* Return value in register 0.  */
2892             h8_set_reg (sd, 0, open_return);
2893
2894             /* Freeing memory used for filename. */
2895             free (filename);
2896           }
2897           goto next;
2898
2899         case O (O_SYS_READ, SB):
2900           {
2901             char *char_ptr;     /* Where characters read would be stored.  */
2902             int fd;             /* File descriptor */
2903             int buf_size;       /* BUF_SIZE parameter in read.  */
2904             int i = 0;          /* Temporary Loop counter */
2905             int read_return = 0;        /* Return value from callback to
2906                                            read.  */
2907
2908             fd = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
2909             buf_size = h8300hmode ? GET_L_REG (2) : GET_W_REG (2);
2910
2911             char_ptr = (char *) malloc (sizeof (char) * buf_size);
2912
2913             /* Callback to read and return the no. of characters read.  */
2914             read_return =
2915               sim_callback->read (sim_callback, fd, char_ptr, buf_size);
2916
2917             /* The characters read are stored in cpu memory.  */
2918             for (i = 0; i < buf_size; i++)
2919               {
2920                 SET_MEMORY_B ((h8_get_reg (sd, 1) + (sizeof (char) * i)),
2921                               *(char_ptr + (sizeof (char) * i)));
2922               }
2923
2924             /* Return value in Register 0.  */
2925             h8_set_reg (sd, 0, read_return);
2926
2927             /* Freeing memory used as buffer.  */
2928             free (char_ptr);
2929           }
2930           goto next;
2931
2932         case O (O_SYS_WRITE, SB):
2933           {
2934             int fd;             /* File descriptor */
2935             char temp_char;     /* Temporary character */
2936             int len;            /* Length of write, Parameter II to write.  */
2937             int char_ptr;       /* Character Pointer, Parameter I of write.  */
2938             char *ptr;          /* Where characters to be written are stored. 
2939                                  */
2940             int write_return;   /* Return value from callback to write.  */
2941             int i = 0;          /* Loop counter */
2942
2943             fd = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
2944             char_ptr = h8300hmode ? GET_L_REG (1) : GET_W_REG (1);
2945             len = h8300hmode ? GET_L_REG (2) : GET_W_REG (2);
2946
2947             /* Allocating space for the characters to be written.  */
2948             ptr = (char *) malloc (sizeof (char) * len);
2949
2950             /* Fetching the characters from cpu memory.  */
2951             for (i = 0; i < len; i++)
2952               {
2953                 temp_char = GET_MEMORY_B (char_ptr + i);
2954                 ptr[i] = temp_char;
2955               }
2956
2957             /* Callback write and return the no. of characters written.  */
2958             write_return = sim_callback->write (sim_callback, fd, ptr, len);
2959
2960             /* Return value in Register 0.  */
2961             h8_set_reg (sd, 0, write_return);
2962
2963             /* Freeing memory used as buffer.  */
2964             free (ptr);
2965           }
2966           goto next;
2967
2968         case O (O_SYS_LSEEK, SB):
2969           {
2970             int fd;             /* File descriptor */
2971             int offset;         /* Offset */
2972             int origin;         /* Origin */
2973             int lseek_return;   /* Return value from callback to lseek.  */
2974
2975             fd = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
2976             offset = h8300hmode ? GET_L_REG (1) : GET_W_REG (1);
2977             origin = h8300hmode ? GET_L_REG (2) : GET_W_REG (2);
2978
2979             /* Callback lseek and return offset.  */
2980             lseek_return =
2981               sim_callback->lseek (sim_callback, fd, offset, origin);
2982
2983             /* Return value in register 0.  */
2984             h8_set_reg (sd, 0, lseek_return);
2985           }
2986           goto next;
2987
2988         case O (O_SYS_CLOSE, SB):
2989           {
2990             int fd;             /* File descriptor */
2991             int close_return;   /* Return value from callback to close.  */
2992
2993             fd = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
2994
2995             /* Callback close and return.  */
2996             close_return = sim_callback->close (sim_callback, fd);
2997
2998             /* Return value in register 0.  */
2999             h8_set_reg (sd, 0, close_return);
3000           }
3001           goto next;
3002
3003         case O (O_SYS_FSTAT, SB):
3004           {
3005             int fd;             /* File descriptor */
3006             struct stat stat_rec;       /* Stat record */
3007             int fstat_return;   /* Return value from callback to stat.  */
3008             int stat_ptr;       /* Pointer to stat record.  */
3009             char *temp_stat_ptr;        /* Temporary stat_rec pointer.  */
3010
3011             fd = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
3012
3013             /* Setting stat_ptr to second argument of stat.  */
3014             stat_ptr = h8300hmode ? GET_L_REG (1) : GET_W_REG (1);
3015
3016             /* Callback stat and return.  */
3017             fstat_return = sim_callback->fstat (sim_callback, fd, &stat_rec);
3018
3019             /* Have stat_ptr point to starting of stat_rec.  */
3020             temp_stat_ptr = (char *) (&stat_rec);
3021
3022             /* Setting up the stat structure returned.  */
3023             SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3024             stat_ptr += 2;
3025             SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3026             stat_ptr += 2;
3027             SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3028             stat_ptr += 4;
3029             SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3030             stat_ptr += 2;
3031             SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3032             stat_ptr += 2;
3033             SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3034             stat_ptr += 2;
3035             SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3036             stat_ptr += 2;
3037             SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3038             stat_ptr += 4;
3039             SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3040             stat_ptr += 8;
3041             SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3042             stat_ptr += 8;
3043             SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3044
3045             /* Return value in register 0.  */
3046             h8_set_reg (sd, 0, fstat_return);
3047           }
3048           goto next;
3049
3050         case O (O_SYS_STAT, SB):
3051           {
3052             int len = 0;        /* Length of filename.  */
3053             char *filename;     /* Filename would go here.  */
3054             char temp_char;     /* Temporary character */
3055             int filename_ptr;   /* Pointer to filename in cpu memory.  */
3056             struct stat stat_rec;       /* Stat record */
3057             int stat_return;    /* Return value from callback to stat */
3058             int stat_ptr;       /* Pointer to stat record.  */
3059             char *temp_stat_ptr;        /* Temporary stat_rec pointer.  */
3060             int i = 0;          /* Loop Counter */
3061
3062             /* Setting filename_ptr to first argument of open.  */
3063             filename_ptr = h8300hmode ? GET_L_REG (0) : GET_W_REG (0);
3064
3065             /* Trying to find the length of the filename.  */
3066             temp_char = GET_MEMORY_B (h8_get_reg (sd, 0));
3067
3068             len = 1;
3069             while (temp_char != '\0')
3070               {
3071                 temp_char = GET_MEMORY_B (filename_ptr + len);
3072                 len++;
3073               }
3074
3075             /* Allocating space for the filename.  */
3076             filename = (char *) malloc (sizeof (char) * len);
3077
3078             /* String copying the filename from memory.  */
3079             for (i = 0; i < len; i++)
3080               {
3081                 temp_char = GET_MEMORY_B (filename_ptr + i);
3082                 filename[i] = temp_char;
3083               }
3084
3085             /* Setting stat_ptr to second argument of stat.  */
3086             /* stat_ptr = h8_get_reg (sd, 1); */
3087             stat_ptr = h8300hmode ? GET_L_REG (1) : GET_W_REG (1);
3088
3089             /* Callback stat and return.  */
3090             stat_return =
3091               sim_callback->stat (sim_callback, filename, &stat_rec);
3092
3093             /* Have stat_ptr point to starting of stat_rec.  */
3094             temp_stat_ptr = (char *) (&stat_rec);
3095  
3096             /* Freeing memory used for filename.  */
3097             free (filename);
3098  
3099             /* Setting up the stat structure returned.  */
3100             SET_MEMORY_W (stat_ptr, stat_rec.st_dev);
3101             stat_ptr += 2;
3102             SET_MEMORY_W (stat_ptr, stat_rec.st_ino);
3103             stat_ptr += 2;
3104             SET_MEMORY_L (stat_ptr, stat_rec.st_mode);
3105             stat_ptr += 4;
3106             SET_MEMORY_W (stat_ptr, stat_rec.st_nlink);
3107             stat_ptr += 2;
3108             SET_MEMORY_W (stat_ptr, stat_rec.st_uid);
3109             stat_ptr += 2;
3110             SET_MEMORY_W (stat_ptr, stat_rec.st_gid);
3111             stat_ptr += 2;
3112             SET_MEMORY_W (stat_ptr, stat_rec.st_rdev);
3113             stat_ptr += 2;
3114             SET_MEMORY_L (stat_ptr, stat_rec.st_size);
3115             stat_ptr += 4;
3116             SET_MEMORY_L (stat_ptr, stat_rec.st_atime);
3117             stat_ptr += 8;
3118             SET_MEMORY_L (stat_ptr, stat_rec.st_mtime);
3119             stat_ptr += 8;
3120             SET_MEMORY_L (stat_ptr, stat_rec.st_ctime);
3121  
3122             /* Return value in register 0.  */
3123             h8_set_reg (sd, 0, stat_return);
3124           }
3125           goto next;
3126           /* End of system call processing.  */
3127
3128         case O (O_NOT, SB):             /* not.b */
3129           if (fetch2 (sd, &code->src, &rd))
3130             goto end;
3131           rd = ~rd; 
3132           v = 0;
3133           goto shift8;
3134
3135         case O (O_NOT, SW):             /* not.w */
3136           if (fetch2 (sd, &code->src, &rd))
3137             goto end;
3138           rd = ~rd; 
3139           v = 0;
3140           goto shift16;
3141
3142         case O (O_NOT, SL):             /* not.l */
3143           if (fetch2 (sd, &code->src, &rd))
3144             goto end;
3145           rd = ~rd; 
3146           v = 0;
3147           goto shift32;
3148
3149         case O (O_SHLL, SB):    /* shll.b */
3150         case O (O_SHLR, SB):    /* shlr.b */
3151           if (fetch2 (sd, &code->dst, &rd))
3152             goto end;
3153
3154           if (code->src.type == X (OP_IMM, SB))
3155             fetch (sd, &code->src, &ea);
3156           else
3157             ea = 1;
3158
3159           if (code->opcode == O (O_SHLL, SB))
3160             {
3161               v = (ea > 8);
3162               c = rd & (0x80 >> (ea - 1));
3163               rd <<= ea;
3164             }
3165           else
3166             {
3167               v = 0;
3168               c = rd & (1 << (ea - 1));
3169               rd = (unsigned char) rd >> ea;
3170             }
3171           goto shift8;
3172
3173         case O (O_SHLL, SW):    /* shll.w */
3174         case O (O_SHLR, SW):    /* shlr.w */
3175           if (fetch2 (sd, &code->dst, &rd))
3176             goto end;
3177
3178           if (code->src.type == X (OP_IMM, SW))
3179             fetch (sd, &code->src, &ea);
3180           else
3181             ea = 1;
3182
3183           if (code->opcode == O (O_SHLL, SW))
3184             {
3185               v = (ea > 16);
3186               c = rd & (0x8000 >> (ea - 1));
3187               rd <<= ea;
3188             }
3189           else
3190             {
3191               v = 0;
3192               c = rd & (1 << (ea - 1));
3193               rd = (unsigned short) rd >> ea;
3194             }
3195           goto shift16;
3196
3197         case O (O_SHLL, SL):    /* shll.l */
3198         case O (O_SHLR, SL):    /* shlr.l */
3199           if (fetch2 (sd, &code->dst, &rd))
3200             goto end;
3201
3202           if (code->src.type == X (OP_IMM, SL))
3203             fetch (sd, &code->src, &ea);
3204           else
3205             ea = 1;
3206
3207           if (code->opcode == O (O_SHLL, SL))
3208             {
3209               v = (ea > 32);
3210               c = rd & (0x80000000 >> (ea - 1));
3211               rd <<= ea;
3212             }
3213           else
3214             {
3215               v = 0;
3216               c = rd & (1 << (ea - 1));
3217               rd = (unsigned int) rd >> ea;
3218             }
3219           goto shift32;
3220
3221         case O (O_SHAL, SB):
3222         case O (O_SHAR, SB):
3223           if (fetch2 (sd, &code->dst, &rd))
3224             goto end;
3225
3226           if (code->src.type == X (OP_IMM, SB))
3227             fetch (sd, &code->src, &ea);
3228           else
3229             ea = 1;
3230
3231           if (code->opcode == O (O_SHAL, SB))
3232             {
3233               c = rd & (0x80 >> (ea - 1));
3234               res = rd >> (7 - ea);
3235               v = ((res & 1) && !(res & 2)) 
3236                 || (!(res & 1) && (res & 2));
3237               rd <<= ea;
3238             }
3239           else
3240             {
3241               c = rd & (1 << (ea - 1));
3242               v = 0;
3243               rd = ((signed char) rd) >> ea;
3244             }
3245           goto shift8;
3246
3247         case O (O_SHAL, SW):
3248         case O (O_SHAR, SW):
3249           if (fetch2 (sd, &code->dst, &rd))
3250             goto end;
3251
3252           if (code->src.type == X (OP_IMM, SW))
3253             fetch (sd, &code->src, &ea);
3254           else
3255             ea = 1;
3256
3257           if (code->opcode == O (O_SHAL, SW))
3258             {
3259               c = rd & (0x8000 >> (ea - 1));
3260               res = rd >> (15 - ea);
3261               v = ((res & 1) && !(res & 2)) 
3262                 || (!(res & 1) && (res & 2));
3263               rd <<= ea;
3264             }
3265           else
3266             {
3267               c = rd & (1 << (ea - 1));
3268               v = 0;
3269               rd = ((signed short) rd) >> ea;
3270             }
3271           goto shift16;
3272
3273         case O (O_SHAL, SL):
3274         case O (O_SHAR, SL):
3275           if (fetch2 (sd, &code->dst, &rd))
3276             goto end;
3277
3278           if (code->src.type == X (OP_IMM, SL))
3279             fetch (sd, &code->src, &ea);
3280           else
3281             ea = 1;
3282
3283           if (code->opcode == O (O_SHAL, SL))
3284             {
3285               c = rd & (0x80000000 >> (ea - 1));
3286               res = rd >> (31 - ea);
3287               v = ((res & 1) && !(res & 2)) 
3288                 || (!(res & 1) && (res & 2));
3289               rd <<= ea;
3290             }
3291           else
3292             {
3293               c = rd & (1 << (ea - 1));
3294               v = 0;
3295               rd = ((signed int) rd) >> ea;
3296             }
3297           goto shift32;
3298
3299         case O (O_ROTL, SB):
3300         case O (O_ROTR, SB):
3301           if (fetch2 (sd, &code->dst, &rd))
3302             goto end;
3303
3304           if (code->src.type == X (OP_IMM, SB))
3305             fetch (sd, &code->src, &ea);
3306           else
3307             ea = 1;
3308
3309           while (ea--)
3310             if (code->opcode == O (O_ROTL, SB))
3311               {
3312                 c = rd & 0x80;
3313                 rd <<= 1;
3314                 if (c)
3315                   rd |= 1;
3316               }
3317             else
3318               {
3319                 c = rd & 1;
3320                 rd = ((unsigned char) rd) >> 1;
3321                 if (c)
3322                   rd |= 0x80;
3323               }
3324
3325           v = 0;
3326           goto shift8;
3327
3328         case O (O_ROTL, SW):
3329         case O (O_ROTR, SW):
3330           if (fetch2 (sd, &code->dst, &rd))
3331             goto end;
3332
3333           if (code->src.type == X (OP_IMM, SW))
3334             fetch (sd, &code->src, &ea);
3335           else
3336             ea = 1;
3337
3338           while (ea--)
3339             if (code->opcode == O (O_ROTL, SW))
3340               {
3341                 c = rd & 0x8000;
3342                 rd <<= 1;
3343                 if (c)
3344                   rd |= 1;
3345               }
3346             else
3347               {
3348                 c = rd & 1;
3349                 rd = ((unsigned short) rd) >> 1;
3350                 if (c)
3351                   rd |= 0x8000;
3352               }
3353
3354           v = 0;
3355           goto shift16;
3356
3357         case O (O_ROTL, SL):
3358         case O (O_ROTR, SL):
3359           if (fetch2 (sd, &code->dst, &rd))
3360             goto end;
3361
3362           if (code->src.type == X (OP_IMM, SL))
3363             fetch (sd, &code->src, &ea);
3364           else
3365             ea = 1;
3366
3367           while (ea--)
3368             if (code->opcode == O (O_ROTL, SL))
3369               {
3370                 c = rd & 0x80000000;
3371                 rd <<= 1;
3372                 if (c)
3373                   rd |= 1;
3374               }
3375             else
3376               {
3377                 c = rd & 1;
3378                 rd = ((unsigned int) rd) >> 1;
3379                 if (c)
3380                   rd |= 0x80000000;
3381               }
3382
3383           v = 0;
3384           goto shift32;
3385
3386         case O (O_ROTXL, SB):
3387         case O (O_ROTXR, SB):
3388           if (fetch2 (sd, &code->dst, &rd))
3389             goto end;
3390
3391           if (code->src.type == X (OP_IMM, SB))
3392             fetch (sd, &code->src, &ea);
3393           else
3394             ea = 1;
3395
3396           while (ea--)
3397             if (code->opcode == O (O_ROTXL, SB))
3398               {
3399                 res = rd & 0x80;
3400                 rd <<= 1;
3401                 if (C)
3402                   rd |= 1;
3403                 c = res;
3404               }
3405             else
3406               {
3407                 res = rd & 1;
3408                 rd = ((unsigned char) rd) >> 1;
3409                 if (C)
3410                   rd |= 0x80;
3411                 c = res;
3412               }
3413
3414           v = 0;
3415           goto shift8;
3416
3417         case O (O_ROTXL, SW):
3418         case O (O_ROTXR, SW):
3419           if (fetch2 (sd, &code->dst, &rd))
3420             goto end;
3421
3422           if (code->src.type == X (OP_IMM, SW))
3423             fetch (sd, &code->src, &ea);
3424           else
3425             ea = 1;
3426
3427           while (ea--)
3428             if (code->opcode == O (O_ROTXL, SW))
3429               {
3430                 res = rd & 0x8000;
3431                 rd <<= 1;
3432                 if (C)
3433                   rd |= 1;
3434                 c = res;
3435               }
3436             else
3437               {
3438                 res = rd & 1;
3439                 rd = ((unsigned short) rd) >> 1;
3440                 if (C)
3441                   rd |= 0x8000;
3442                 c = res;
3443               }
3444
3445           v = 0;
3446           goto shift16;
3447
3448         case O (O_ROTXL, SL):
3449         case O (O_ROTXR, SL):
3450           if (fetch2 (sd, &code->dst, &rd))
3451             goto end;
3452
3453           if (code->src.type == X (OP_IMM, SL))
3454             fetch (sd, &code->src, &ea);
3455           else
3456             ea = 1;
3457
3458           while (ea--)
3459             if (code->opcode == O (O_ROTXL, SL))
3460               {
3461                 res = rd & 0x80000000;
3462                 rd <<= 1;
3463                 if (C)
3464                   rd |= 1;
3465                 c = res;
3466               }
3467             else
3468               {
3469                 res = rd & 1;
3470                 rd = ((unsigned int) rd) >> 1;
3471                 if (C)
3472                   rd |= 0x80000000;
3473                 c = res;
3474               }
3475
3476           v = 0;
3477           goto shift32;
3478
3479         case O (O_JMP, SN):
3480         case O (O_JMP, SL):
3481         case O (O_JMP, SB):             /* jmp */
3482         case O (O_JMP, SW):
3483           fetch (sd, &code->src, &pc);
3484           goto end;
3485
3486         case O (O_JSR, SN):
3487         case O (O_JSR, SL):
3488         case O (O_JSR, SB):             /* jsr, jump to subroutine */
3489         case O (O_JSR, SW):
3490           if (fetch (sd, &code->src, &pc))
3491             goto end;
3492         call:
3493           tmp = h8_get_reg (sd, SP_REGNUM);
3494
3495           if (h8300hmode)
3496             {
3497               tmp -= 4;
3498               SET_MEMORY_L (tmp, code->next_pc);
3499             }
3500           else
3501             {
3502               tmp -= 2;
3503               SET_MEMORY_W (tmp, code->next_pc);
3504             }
3505           h8_set_reg (sd, SP_REGNUM, tmp);
3506
3507           goto end;
3508
3509         case O (O_BSR, SW):
3510         case O (O_BSR, SL):
3511         case O (O_BSR, SB):             /* bsr, branch to subroutine */
3512           if (fetch (sd, &code->src, &res))
3513             goto end;
3514           pc = code->next_pc + res;
3515           goto call;
3516
3517         case O (O_RTE, SN):             /* rte, return from exception */
3518         rte:
3519           /* Pops exr and ccr before pc -- otherwise identical to rts.  */
3520           tmp = h8_get_reg (sd, SP_REGNUM);
3521
3522           if (h8300smode)                       /* pop exr */
3523             {
3524               h8_set_exr (sd, GET_MEMORY_L (tmp));
3525               tmp += 4;
3526             }
3527           if (h8300hmode)
3528             {
3529               h8_set_ccr (sd, GET_MEMORY_L (tmp));
3530               tmp += 4;
3531               pc = GET_MEMORY_L (tmp);
3532               tmp += 4;
3533             }
3534           else
3535             {
3536               h8_set_ccr (sd, GET_MEMORY_W (tmp));
3537               tmp += 2;
3538               pc = GET_MEMORY_W (tmp);
3539               tmp += 2;
3540             }
3541
3542           GETSR (sd);
3543           h8_set_reg (sd, SP_REGNUM, tmp);
3544           goto end;
3545
3546         case O (O_RTS, SN):             /* rts, return from subroutine */
3547         rts:
3548           tmp = h8_get_reg (sd, SP_REGNUM);
3549
3550           if (h8300hmode)
3551             {
3552               pc = GET_MEMORY_L (tmp);
3553               tmp += 4;
3554             }
3555           else
3556             {
3557               pc = GET_MEMORY_W (tmp);
3558               tmp += 2;
3559             }
3560
3561           h8_set_reg (sd, SP_REGNUM, tmp);
3562           goto end;
3563
3564         case O (O_ILL, SB):             /* illegal */
3565           sim_engine_set_run_state (sd, sim_stopped, SIGILL);
3566           goto end;
3567
3568         case O (O_SLEEP, SN):           /* sleep */
3569           /* Check for magic numbers in r1 and r2.  */
3570           if ((h8_get_reg (sd, R1_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC1 &&
3571               (h8_get_reg (sd, R2_REGNUM) & 0xffff) == LIBC_EXIT_MAGIC2 &&
3572               SIM_WIFEXITED (h8_get_reg (sd, 0)))
3573             {
3574               /* This trap comes from _exit, not from gdb.  */
3575               sim_engine_set_run_state (sd, sim_exited, 
3576                                         SIM_WEXITSTATUS (h8_get_reg (sd, 0)));
3577             }
3578           else
3579             {
3580               /* Treat it as a sigtrap.  */
3581               sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3582             }
3583           goto end;
3584
3585         case O (O_TRAPA, SB):           /* trapa */
3586           if (fetch (sd, &code->src, &res))
3587             goto end;                   /* res is vector number.  */
3588
3589           tmp = h8_get_reg (sd, SP_REGNUM);
3590           tmp -= 4;
3591           SET_MEMORY_L (tmp, code->next_pc);
3592           tmp -= 4; 
3593           SET_MEMORY_L (tmp, h8_get_ccr (sd));
3594           intMaskBit = 1;
3595           BUILDSR (sd);
3596
3597           if (h8300smode)
3598             {
3599               tmp -= 4;
3600               SET_MEMORY_L (tmp, h8_get_exr (sd));
3601             }
3602
3603           h8_set_reg (sd, SP_REGNUM, tmp);
3604
3605           /* FIXME: "normal" mode should use 2-byte ptrs.  */
3606           pc = GET_MEMORY_L (0x20 + res * 4);
3607           goto end;
3608
3609         case O (O_BPT, SN):
3610           sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
3611           goto end;
3612
3613         case O (O_BSETEQ, SB):
3614           if (Z)
3615             goto bset;
3616           goto next;
3617
3618         case O (O_BSETNE, SB):
3619           if (!Z)
3620             goto bset;
3621           goto next;
3622
3623         case O (O_BCLREQ, SB):
3624           if (Z)
3625             goto bclr;
3626           goto next;
3627
3628         case O (O_BCLRNE, SB):
3629           if (!Z)
3630             goto bclr;
3631           goto next;
3632
3633           OBITOP (O_BNOT, 1, 1, ea ^= m);               /* bnot */
3634           OBITOP (O_BTST, 1, 0, nz = ea & m);           /* btst */
3635         bset:
3636           OBITOP (O_BSET, 1, 1, ea |= m);               /* bset */
3637         bclr:
3638           OBITOP (O_BCLR, 1, 1, ea &= ~m);              /* bclr */
3639           OBITOP (O_BLD, 1, 0, c = ea & m);             /* bld  */
3640           OBITOP (O_BILD, 1, 0, c = !(ea & m));         /* bild */
3641           OBITOP (O_BST, 1, 1, ea &= ~m;
3642                   if (C) ea |= m);                      /* bst  */
3643           OBITOP (O_BIST, 1, 1, ea &= ~m;
3644                   if (!C) ea |= m);                     /* bist */
3645           OBITOP (O_BSTZ, 1, 1, ea &= ~m;
3646                   if (Z) ea |= m);                      /* bstz */
3647           OBITOP (O_BISTZ, 1, 1, ea &= ~m;
3648                   if (!Z) ea |= m);                     /* bistz */
3649           OBITOP (O_BAND, 1, 0, c = (ea & m) && C);     /* band */
3650           OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);   /* biand */
3651           OBITOP (O_BOR, 1, 0, c = (ea & m) || C);      /* bor  */
3652           OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);    /* bior */
3653           OBITOP (O_BXOR, 1, 0, c = ((ea & m) != 0)!= C);       /* bxor */
3654           OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);   /* bixor */
3655
3656         case O (O_BFLD, SB):                            /* bfld */
3657           /* bitfield load */
3658           ea = 0;
3659           if (fetch (sd, &code->src, &bit))
3660             goto end;
3661
3662           if (bit != 0)
3663             {
3664               if (fetch (sd, &code->dst, &ea))
3665                 goto end;
3666
3667               ea &= bit;
3668               while (!(bit & 1))
3669                 {
3670                   ea  >>= 1;
3671                   bit >>= 1;
3672                 }
3673             }
3674           if (store (sd, &code->op3, ea))
3675             goto end;
3676
3677           goto next;
3678
3679         case O(O_BFST, SB):                     /* bfst */
3680           /* bitfield store */
3681           /* NOTE: the imm8 value is in dst, and the ea value
3682              (which is actually the destination) is in op3.
3683              It has to be that way, to avoid breaking the assembler.  */
3684
3685           if (fetch (sd, &code->dst, &bit))     /* imm8 */
3686             goto end;
3687           if (bit == 0)                         /* noop -- nothing to do.  */
3688             goto next;
3689
3690           if (fetch (sd, &code->src, &rd))      /* reg8 src */
3691             goto end;
3692
3693           if (fetch2 (sd, &code->op3, &ea))     /* ea dst */
3694             goto end;
3695
3696           /* Left-shift the register data into position.  */
3697           for (tmp = bit; !(tmp & 1); tmp >>= 1)
3698             rd <<= 1;
3699
3700           /* Combine it with the neighboring bits.  */
3701           ea = (ea & ~bit) | (rd & bit);
3702
3703           /* Put it back.  */
3704           if (store2 (sd, &code->op3, ea))
3705             goto end;
3706           goto next;
3707
3708         case O (O_CLRMAC, SN):          /* clrmac */
3709           h8_set_mach (sd, 0);
3710           h8_set_macl (sd, 0);
3711           h8_set_macZ (sd, 1);
3712           h8_set_macV (sd, 0);
3713           h8_set_macN (sd, 0);
3714           goto next;
3715
3716         case O (O_STMAC, SL):           /* stmac, 260 */
3717           switch (code->src.type) {
3718           case X (OP_MACH, SL): 
3719             res = h8_get_mach (sd);
3720             if (res & 0x200)            /* sign extend */
3721               res |= 0xfffffc00;
3722             break;
3723           case X (OP_MACL, SL): 
3724             res = h8_get_macl (sd);
3725             break;
3726           default:      goto illegal;
3727           }
3728           nz = !h8_get_macZ (sd);
3729           n = h8_get_macN (sd);
3730           v = h8_get_macV (sd);
3731
3732           if (store (sd, &code->dst, res))
3733             goto end;
3734
3735           goto next;
3736
3737         case O (O_LDMAC, SL):           /* ldmac, 179 */
3738           if (fetch (sd, &code->src, &rd))
3739             goto end;
3740
3741           switch (code->dst.type) {
3742           case X (OP_MACH, SL): 
3743             rd &= 0x3ff;                /* Truncate to 10 bits */
3744             h8_set_mach (sd, rd);
3745             break;
3746           case X (OP_MACL, SL): 
3747             h8_set_macl (sd, rd);
3748             break;
3749           default:      goto illegal;
3750           }
3751           h8_set_macV (sd, 0);
3752           goto next;
3753
3754         case O (O_MAC, SW):
3755           if (fetch (sd, &code->src, &rd) ||
3756               fetch (sd, &code->dst, &res))
3757             goto end;
3758
3759           /* Ye gods, this is non-portable!
3760              However, the existing mul/div code is similar.  */
3761           res = SEXTSHORT (res) * SEXTSHORT (rd);
3762
3763           if (h8_get_macS (sd))         /* Saturating mode */
3764             {
3765               long long mac = h8_get_macl (sd);
3766
3767               if (mac & 0x80000000)             /* sign extend */
3768                 mac |= 0xffffffff00000000LL;
3769
3770               mac += res;
3771               if (mac > 0x7fffffff || mac < 0xffffffff80000000LL)
3772                 h8_set_macV (sd, 1);
3773               h8_set_macZ (sd, (mac == 0));
3774               h8_set_macN (sd, (mac  < 0));
3775               h8_set_macl (sd, (int) mac);
3776             }
3777           else                          /* "Less Saturating" mode */
3778             {
3779               long long mac = h8_get_mach (sd);
3780               mac <<= 32;
3781               mac += h8_get_macl (sd);
3782
3783               if (mac & 0x20000000000LL)        /* sign extend */
3784                 mac |= 0xfffffc0000000000LL;
3785
3786               mac += res;
3787               if (mac > 0x1ffffffffffLL || 
3788                   mac < (long long) 0xfffffe0000000000LL)
3789                 h8_set_macV (sd, 1);
3790               h8_set_macZ (sd, (mac == 0));
3791               h8_set_macN (sd, (mac  < 0));
3792               h8_set_macl (sd, (int) mac);
3793               mac >>= 32;
3794               h8_set_mach (sd, (int) (mac & 0x3ff));
3795             }
3796           goto next;
3797
3798         case O (O_MULS, SW):            /* muls.w */
3799           if (fetch (sd, &code->src, &ea) ||
3800               fetch (sd, &code->dst, &rd))
3801             goto end;
3802
3803           /* FIXME: is this the right place to be doing sign extend?  */
3804           if (OP_KIND (code->src.type) == OP_IMM &&
3805               (ea & 8) != 0)
3806             ea |= 0xfff0;
3807           else
3808             ea = SEXTSHORT (ea);
3809
3810           res = SEXTSHORT (ea * SEXTSHORT (rd));
3811
3812           n  = res & 0x8000;
3813           nz = res & 0xffff;
3814           if (store (sd, &code->dst, res))
3815             goto end;
3816
3817           goto next;
3818
3819         case O (O_MULS, SL):            /* muls.l */
3820           if (fetch (sd, &code->src, &ea) ||
3821               fetch (sd, &code->dst, &rd))
3822             goto end;
3823
3824           /* FIXME: is this the right place to be doing sign extend?  */
3825           if (OP_KIND (code->src.type) == OP_IMM &&
3826               (ea & 8) != 0)
3827             ea |= 0xfffffff0;
3828
3829           res = ea * rd;
3830
3831           n  = res & 0x80000000;
3832           nz = res & 0xffffffff;
3833           if (store (sd, &code->dst, res))
3834             goto end;
3835           goto next;
3836
3837         case O (O_MULSU, SL):           /* muls/u.l */
3838           if (fetch (sd, &code->src, &ea) ||
3839               fetch (sd, &code->dst, &rd))
3840             goto end;
3841
3842           /* FIXME: is this the right place to be doing sign extend?  */
3843           if (OP_KIND (code->src.type) == OP_IMM &&
3844               (ea & 8) != 0)
3845             ea |= 0xfffffff0;
3846
3847           /* Compute upper 32 bits of the 64-bit result.  */
3848           res = (((long long) ea) * ((long long) rd)) >> 32;
3849
3850           n  = res & 0x80000000;
3851           nz = res & 0xffffffff;
3852           if (store (sd, &code->dst, res))
3853             goto end;
3854           goto next;
3855
3856         case O (O_MULU, SW):            /* mulu.w */
3857           if (fetch (sd, &code->src, &ea) ||
3858               fetch (sd, &code->dst, &rd))
3859             goto end;
3860
3861           res = UEXTSHORT ((UEXTSHORT (ea) * UEXTSHORT (rd)));
3862
3863           /* Don't set Z or N.  */
3864           if (store (sd, &code->dst, res))
3865             goto end;
3866
3867           goto next;
3868
3869         case O (O_MULU, SL):            /* mulu.l */
3870           if (fetch (sd, &code->src, &ea) ||
3871               fetch (sd, &code->dst, &rd))
3872             goto end;
3873
3874           res = ea * rd;
3875
3876           /* Don't set Z or N.  */
3877           if (store (sd, &code->dst, res))
3878             goto end;
3879
3880           goto next;
3881
3882         case O (O_MULUU, SL):           /* mulu/u.l */
3883           if (fetch (sd, &code->src, &ea) ||
3884               fetch (sd, &code->dst, &rd))
3885             goto end;
3886
3887           /* Compute upper 32 bits of the 64-bit result.  */
3888           res = (((unsigned long long) (unsigned) ea) *
3889                  ((unsigned long long) (unsigned) rd)) >> 32;
3890
3891           /* Don't set Z or N.  */
3892           if (store (sd, &code->dst, res))
3893             goto end;
3894
3895           goto next;
3896
3897         case O (O_MULXS, SB):           /* mulxs.b */
3898           if (fetch (sd, &code->src, &ea) ||
3899               fetch (sd, &code->dst, &rd))
3900             goto end;
3901
3902           /* FIXME: is this the right place to be doing sign extend?  */
3903           if (OP_KIND (code->src.type) == OP_IMM &&
3904               (ea & 8) != 0)
3905             ea |= 0xfffffff0;
3906           else
3907             ea = SEXTCHAR (ea);
3908
3909           res = ea * SEXTCHAR (rd);
3910
3911           n  = res & 0x8000;
3912           nz = res & 0xffff;
3913           if (store (sd, &code->dst, res))
3914             goto end;
3915
3916           goto next;
3917
3918         case O (O_MULXS, SW):           /* mulxs.w */
3919           if (fetch (sd, &code->src, &ea) ||
3920               fetch (sd, &code->dst, &rd))
3921             goto end;
3922
3923           /* FIXME: is this the right place to be doing sign extend?  */
3924           if (OP_KIND (code->src.type) == OP_IMM &&
3925               (ea & 8) != 0)
3926             ea |= 0xfff0;
3927           else
3928             ea = SEXTSHORT (ea);
3929
3930           res = ea * SEXTSHORT (rd & 0xffff);
3931
3932           n  = res & 0x80000000;
3933           nz = res & 0xffffffff;
3934           if (store (sd, &code->dst, res))
3935             goto end;
3936
3937           goto next;
3938
3939         case O (O_MULXU, SB):           /* mulxu.b */
3940           if (fetch (sd, &code->src, &ea) ||
3941               fetch (sd, &code->dst, &rd))
3942             goto end;
3943
3944           res = UEXTCHAR (ea) * UEXTCHAR (rd);
3945
3946           if (store (sd, &code->dst, res))
3947             goto end;
3948
3949           goto next;
3950
3951         case O (O_MULXU, SW):           /* mulxu.w */
3952           if (fetch (sd, &code->src, &ea) ||
3953               fetch (sd, &code->dst, &rd))
3954             goto end;
3955
3956           res = UEXTSHORT (ea) * UEXTSHORT (rd);
3957
3958           if (store (sd, &code->dst, res))
3959             goto end;
3960
3961           goto next;
3962
3963         case O (O_TAS, SB):             /* tas (test and set) */
3964           if (!h8300sxmode)             /* h8sx can use any register. */
3965             switch (code->src.reg)
3966               {
3967               case R0_REGNUM:
3968               case R1_REGNUM:
3969               case R4_REGNUM:
3970               case R5_REGNUM:
3971                 break;
3972               default:
3973                 goto illegal;
3974               }
3975
3976           if (fetch (sd, &code->src, &res))
3977             goto end;
3978           if (store (sd, &code->src, res | 0x80))
3979             goto end;
3980
3981           goto just_flags_log8;
3982
3983         case O (O_DIVU, SW):                    /* divu.w */
3984           if (fetch (sd, &code->src, &ea) ||
3985               fetch (sd, &code->dst, &rd))
3986             goto end;
3987
3988           n  = ea & 0x8000;
3989           nz = ea & 0xffff;
3990           if (ea)
3991             res = (unsigned) (UEXTSHORT (rd) / UEXTSHORT (ea));
3992           else
3993             res = 0;
3994
3995           if (store (sd, &code->dst, res))
3996             goto end;
3997           goto next;
3998
3999         case O (O_DIVU, SL):                    /* divu.l */
4000           if (fetch (sd, &code->src, &ea) ||
4001               fetch (sd, &code->dst, &rd))
4002             goto end;
4003
4004           n  = ea & 0x80000000;
4005           nz = ea & 0xffffffff;
4006           if (ea)
4007             res = (unsigned) rd / ea;
4008           else
4009             res = 0;
4010
4011           if (store (sd, &code->dst, res))
4012             goto end;
4013           goto next;
4014
4015         case O (O_DIVS, SW):                    /* divs.w */
4016           if (fetch (sd, &code->src, &ea) ||
4017               fetch (sd, &code->dst, &rd))
4018             goto end;
4019
4020           /* FIXME: is this the right place to be doing sign extend?  */
4021           if (OP_KIND (code->src.type) == OP_IMM &&
4022               (ea & 8) != 0)
4023             ea |= 0xfffffff0;
4024
4025           if (ea)
4026             {
4027               res = SEXTSHORT (rd) / SEXTSHORT (ea);
4028               nz  = 1;
4029             }
4030           else
4031             {
4032               res = 0;
4033               nz  = 0;
4034             }
4035
4036           n = res & 0x8000;
4037           if (store (sd, &code->dst, res))
4038             goto end;
4039           goto next;
4040
4041         case O (O_DIVS, SL):                    /* divs.l */
4042           if (fetch (sd, &code->src, &ea) ||
4043               fetch (sd, &code->dst, &rd))
4044             goto end;
4045
4046           /* FIXME: is this the right place to be doing sign extend?  */
4047           if (OP_KIND (code->src.type) == OP_IMM &&
4048               (ea & 8) != 0)
4049             ea |= 0xfffffff0;
4050
4051           if (ea)
4052             {
4053               res = rd / ea;
4054               nz  = 1;
4055             }
4056           else
4057             {
4058               res = 0;
4059               nz  = 0;
4060             }
4061
4062           n = res & 0x80000000;
4063           if (store (sd, &code->dst, res))
4064             goto end;
4065           goto next;
4066
4067         case O (O_DIVXU, SB):                   /* divxu.b */
4068           if (fetch (sd, &code->src, &ea) ||
4069               fetch (sd, &code->dst, &rd))
4070             goto end;
4071
4072           rd = UEXTSHORT (rd);
4073           ea = UEXTCHAR (ea);
4074
4075           n  = ea & 0x80;
4076           nz = ea & 0xff;
4077           if (ea)
4078             {
4079               tmp = (unsigned) rd % ea;
4080               res = (unsigned) rd / ea;
4081             }
4082           else
4083             {
4084               tmp = 0;
4085               res = 0;
4086             }
4087
4088           if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4089             goto end;
4090           goto next;
4091
4092         case O (O_DIVXU, SW):                   /* divxu.w */
4093           if (fetch (sd, &code->src, &ea) ||
4094               fetch (sd, &code->dst, &rd))
4095             goto end;
4096
4097           ea = UEXTSHORT (ea);
4098
4099           n  = ea & 0x8000;
4100           nz = ea & 0xffff;
4101           if (ea)
4102             {
4103               tmp = (unsigned) rd % ea;
4104               res = (unsigned) rd / ea;
4105             }
4106           else
4107             {
4108               tmp = 0;
4109               res = 0;
4110             }
4111
4112           if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4113             goto end;
4114           goto next;
4115
4116         case O (O_DIVXS, SB):                   /* divxs.b */
4117           if (fetch (sd, &code->src, &ea) ||
4118               fetch (sd, &code->dst, &rd))
4119             goto end;
4120
4121           rd = SEXTSHORT (rd);
4122
4123           /* FIXME: is this the right place to be doing sign extend?  */
4124           if (OP_KIND (code->src.type) == OP_IMM &&
4125               (ea & 8) != 0)
4126             ea |= 0xfffffff0;
4127           else
4128             ea = SEXTCHAR (ea);
4129
4130           if (ea)
4131             {
4132               tmp = (int) rd % (int) ea;
4133               res = (int) rd / (int) ea;
4134               nz  = 1;
4135             }
4136           else
4137             {
4138               tmp = 0;
4139               res = 0;
4140               nz  = 0;
4141             }
4142
4143           n = res & 0x8000;
4144           if (store (sd, &code->dst, (res & 0xff) | (tmp << 8)))
4145             goto end;
4146           goto next;
4147
4148         case O (O_DIVXS, SW):                   /* divxs.w */
4149           if (fetch (sd, &code->src, &ea) ||
4150               fetch (sd, &code->dst, &rd))
4151             goto end;
4152
4153           /* FIXME: is this the right place to be doing sign extend?  */
4154           if (OP_KIND (code->src.type) == OP_IMM &&
4155               (ea & 8) != 0)
4156             ea |= 0xfffffff0;
4157           else
4158             ea = SEXTSHORT (ea);
4159
4160           if (ea)
4161             {
4162               tmp = (int) rd % (int) ea;
4163               res = (int) rd / (int) ea;
4164               nz  = 1;
4165             }
4166           else
4167             {
4168               tmp = 0;
4169               res = 0;
4170               nz  = 0;
4171             }
4172
4173           n = res & 0x80000000;
4174           if (store (sd, &code->dst, (res & 0xffff) | (tmp << 16)))
4175             goto end;
4176           goto next;
4177
4178         case O (O_EXTS, SW):                    /* exts.w, signed extend */
4179           if (fetch2 (sd, &code->dst, &rd))
4180             goto end;
4181           ea = rd & 0x80 ? -256 : 0;
4182           res = (rd & 0xff) + ea;
4183           goto log16;
4184
4185         case O (O_EXTS, SL):                    /* exts.l, signed extend */
4186           if (fetch2 (sd, &code->dst, &rd))
4187             goto end;
4188           if (code->src.type == X (OP_IMM, SL))
4189             {
4190               if (fetch (sd, &code->src, &ea))
4191                 goto end;
4192
4193               if (ea == 2)                      /* exts.l #2, nn */
4194                 {
4195                   /* Sign-extend from 8-bit to 32-bit.  */
4196                   ea = rd & 0x80 ? -256 : 0;
4197                   res = (rd & 0xff) + ea;
4198                   goto log32;
4199                 }
4200             }
4201           /* Sign-extend from 16-bit to 32-bit.  */
4202           ea = rd & 0x8000 ? -65536 : 0;
4203           res = (rd & 0xffff) + ea;
4204           goto log32;
4205
4206         case O (O_EXTU, SW):                    /* extu.w, unsigned extend */
4207           if (fetch2 (sd, &code->dst, &rd))
4208             goto end;
4209           ea = 0;
4210           res = (rd & 0xff) + ea;
4211           goto log16;
4212
4213         case O (O_EXTU, SL):                    /* extu.l, unsigned extend */
4214           if (fetch2 (sd, &code->dst, &rd))
4215             goto end;
4216           if (code->src.type == X (OP_IMM, SL))
4217             {
4218               if (fetch (sd, &code->src, &ea))
4219                 goto end;
4220
4221               if (ea == 2)                      /* extu.l #2, nn */
4222                 {
4223                   /* Zero-extend from 8-bit to 32-bit.  */
4224                   ea = 0;
4225                   res = (rd & 0xff) + ea;
4226                   goto log32;
4227                 }
4228             }
4229           /* Zero-extend from 16-bit to 32-bit.  */
4230           ea = 0;
4231           res = (rd & 0xffff) + ea;
4232           goto log32;
4233
4234         case O (O_NOP, SN):                     /* nop */
4235           goto next;
4236
4237         case O (O_STM, SL):                     /* stm, store to memory */
4238           {
4239             int nregs, firstreg, i;
4240
4241             nregs = GET_MEMORY_B (pc + 1);
4242             nregs >>= 4;
4243             nregs &= 0xf;
4244             firstreg = code->src.reg;
4245             firstreg &= 0xf;
4246             for (i = firstreg; i <= firstreg + nregs; i++)
4247               {
4248                 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) - 4);
4249                 SET_MEMORY_L (h8_get_reg (sd, SP_REGNUM), h8_get_reg (sd, i));
4250               }
4251           }
4252           goto next;
4253
4254         case O (O_LDM, SL):                     /* ldm,  load from memory */
4255         case O (O_RTEL, SN):                    /* rte/l, ldm plus rte */
4256         case O (O_RTSL, SN):                    /* rts/l, ldm plus rts */
4257           {
4258             int nregs, firstreg, i;
4259
4260             nregs = ((GET_MEMORY_B (pc + 1) >> 4) & 0xf);
4261             firstreg = code->dst.reg & 0xf;
4262             for (i = firstreg; i >= firstreg - nregs; i--)
4263               {
4264                 h8_set_reg (sd, i, GET_MEMORY_L (h8_get_reg (sd, SP_REGNUM)));
4265                 h8_set_reg (sd, SP_REGNUM, h8_get_reg (sd, SP_REGNUM) + 4);
4266               }
4267           }
4268           switch (code->opcode) {
4269           case O (O_RTEL, SN):
4270             goto rte;
4271           case O (O_RTSL, SN):
4272             goto rts;
4273           case O (O_LDM, SL):
4274             goto next;
4275           default:
4276             goto illegal;
4277           }
4278
4279         case O (O_DAA, SB):
4280           /* Decimal Adjust Addition.  This is for BCD arithmetic.  */
4281           res = GET_B_REG (code->src.reg);      /* FIXME fetch? */
4282           if (!c && (0 <= (res >>  4) && (res >>  4) <= 9) && 
4283               !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4284             res = res;          /* Value added == 0.  */
4285           else if (!c && (0  <= (res >>  4) && (res >>  4) <=  8) && 
4286                    !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4287             res = res + 0x6;            /* Value added == 6.  */
4288           else if (!c && (0 <= (res >>  4) && (res >>  4) <= 9) && 
4289                     h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4290             res = res + 0x6;            /* Value added == 6.  */
4291           else if (!c && (10 <= (res >>  4) && (res >>  4) <= 15) && 
4292                    !h && (0  <= (res & 0xf) && (res & 0xf) <=  9))
4293             res = res + 0x60;           /* Value added == 60.  */
4294           else if (!c && (9  <= (res >>  4) && (res >>  4) <= 15) && 
4295                    !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4296             res = res + 0x66;           /* Value added == 66.  */
4297           else if (!c && (10 <= (res >>  4) && (res >>  4) <= 15) && 
4298                     h && (0  <= (res & 0xf) && (res & 0xf) <=  3))
4299             res = res + 0x66;           /* Value added == 66.  */
4300           else if ( c && (1 <= (res >>  4) && (res >>  4) <= 2) && 
4301                    !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4302             res = res + 0x60;           /* Value added == 60.  */
4303           else if ( c && (1  <= (res >>  4) && (res >>  4) <=  2) && 
4304                    !h && (10 <= (res & 0xf) && (res & 0xf) <= 15))
4305             res = res + 0x66;           /* Value added == 66.  */
4306           else if (c && (1 <= (res >>  4) && (res >>  4) <= 3) && 
4307                    h && (0 <= (res & 0xf) && (res & 0xf) <= 3))
4308             res = res + 0x66;           /* Value added == 66.  */
4309
4310           goto alu8;
4311
4312         case O (O_DAS, SB):
4313           /* Decimal Adjust Subtraction.  This is for BCD arithmetic.  */
4314           res = GET_B_REG (code->src.reg); /* FIXME fetch, fetch2... */
4315           if (!c && (0 <= (res >>  4) && (res >>  4) <= 9) && 
4316               !h && (0 <= (res & 0xf) && (res & 0xf) <= 9))
4317             res = res;          /* Value added == 0.  */
4318           else if (!c && (0 <= (res >>  4) && (res >>  4) <=  8) && 
4319                     h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4320             res = res + 0xfa;           /* Value added == 0xfa.  */
4321           else if ( c && (7 <= (res >>  4) && (res >>  4) <= 15) && 
4322                    !h && (0 <= (res & 0xf) && (res & 0xf) <=  9))
4323             res = res + 0xa0;           /* Value added == 0xa0.  */
4324           else if (c && (6 <= (res >>  4) && (res >>  4) <= 15) && 
4325                    h && (6 <= (res & 0xf) && (res & 0xf) <= 15))
4326             res = res + 0x9a;           /* Value added == 0x9a.  */
4327
4328           goto alu8;
4329
4330         default:
4331         illegal:
4332           sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4333           goto end;
4334
4335         }
4336
4337       (*sim_callback->printf_filtered) (sim_callback,
4338                                         "sim_resume: internal error.\n");
4339       sim_engine_set_run_state (sd, sim_stopped, SIGILL);
4340       goto end;
4341
4342     setc:
4343       if (code->dst.type == X (OP_CCR, SB) ||
4344           code->dst.type == X (OP_CCR, SW))
4345         {
4346           h8_set_ccr (sd, res);
4347           GETSR (sd);
4348         }
4349       else if (h8300smode &&
4350                (code->dst.type == X (OP_EXR, SB) ||
4351                 code->dst.type == X (OP_EXR, SW)))
4352         {
4353           h8_set_exr (sd, res);
4354           if (h8300smode)       /* Get exr.  */
4355             {
4356               trace = (h8_get_exr (sd) >> 7) & 1;
4357               intMask = h8_get_exr (sd) & 7;
4358             }
4359         }
4360       else
4361         goto illegal;
4362
4363       goto next;
4364
4365     condtrue:
4366       /* When a branch works */
4367       if (fetch (sd, &code->src, &res))
4368         goto end;
4369       if (res & 1)              /* bad address */
4370         goto illegal;
4371       pc = code->next_pc + res;
4372       goto end;
4373
4374       /* Set the cond codes from res */
4375     bitop:
4376
4377       /* Set the flags after an 8 bit inc/dec operation */
4378     just_flags_inc8:
4379       n = res & 0x80;
4380       nz = res & 0xff;
4381       v = (rd & 0x7f) == 0x7f;
4382       goto next;
4383
4384       /* Set the flags after an 16 bit inc/dec operation */
4385     just_flags_inc16:
4386       n = res & 0x8000;
4387       nz = res & 0xffff;
4388       v = (rd & 0x7fff) == 0x7fff;
4389       goto next;
4390
4391       /* Set the flags after an 32 bit inc/dec operation */
4392     just_flags_inc32:
4393       n = res & 0x80000000;
4394       nz = res & 0xffffffff;
4395       v = (rd & 0x7fffffff) == 0x7fffffff;
4396       goto next;
4397
4398     shift8:
4399       /* Set flags after an 8 bit shift op, carry,overflow set in insn */
4400       n = (rd & 0x80);
4401       nz = rd & 0xff;
4402       if (store2 (sd, &code->dst, rd))
4403         goto end;
4404       goto next;
4405
4406     shift16:
4407       /* Set flags after an 16 bit shift op, carry,overflow set in insn */
4408       n = (rd & 0x8000);
4409       nz = rd & 0xffff;
4410       if (store2 (sd, &code->dst, rd))
4411         goto end;
4412       goto next;
4413
4414     shift32:
4415       /* Set flags after an 32 bit shift op, carry,overflow set in insn */
4416       n = (rd & 0x80000000);
4417       nz = rd & 0xffffffff;
4418       if (store2 (sd, &code->dst, rd))
4419         goto end;
4420       goto next;
4421
4422     log32:
4423       if (store2 (sd, &code->dst, res))
4424         goto end;
4425
4426     just_flags_log32:
4427       /* flags after a 32bit logical operation */
4428       n = res & 0x80000000;
4429       nz = res & 0xffffffff;
4430       v = 0;
4431       goto next;
4432
4433     log16:
4434       if (store2 (sd, &code->dst, res))
4435         goto end;
4436
4437     just_flags_log16:
4438       /* flags after a 16bit logical operation */
4439       n = res & 0x8000;
4440       nz = res & 0xffff;
4441       v = 0;
4442       goto next;
4443
4444     log8:
4445       if (store2 (sd, &code->dst, res))
4446         goto end;
4447
4448     just_flags_log8:
4449       n = res & 0x80;
4450       nz = res & 0xff;
4451       v = 0;
4452       goto next;
4453
4454     alu8:
4455       if (store2 (sd, &code->dst, res))
4456         goto end;
4457
4458     just_flags_alu8:
4459       n = res & 0x80;
4460       nz = res & 0xff;
4461       c = (res & 0x100);
4462       switch (code->opcode / 4)
4463         {
4464         case O_ADD:
4465         case O_ADDX:
4466           v = ((rd & 0x80) == (ea & 0x80)
4467                && (rd & 0x80) != (res & 0x80));
4468           break;
4469         case O_SUB:
4470         case O_SUBX:
4471         case O_CMP:
4472           v = ((rd & 0x80) != (-ea & 0x80)
4473                && (rd & 0x80) != (res & 0x80));
4474           break;
4475         case O_NEG:
4476           v = (rd == 0x80);
4477           break;
4478         case O_DAA:
4479         case O_DAS:
4480           break;        /* No effect on v flag.  */
4481         }
4482       goto next;
4483
4484     alu16:
4485       if (store2 (sd, &code->dst, res))
4486         goto end;
4487
4488     just_flags_alu16:
4489       n = res & 0x8000;
4490       nz = res & 0xffff;
4491       c = (res & 0x10000);
4492       switch (code->opcode / 4)
4493         {
4494         case O_ADD:
4495         case O_ADDX:
4496           v = ((rd & 0x8000) == (ea & 0x8000)
4497                && (rd & 0x8000) != (res & 0x8000));
4498           break;
4499         case O_SUB:
4500         case O_SUBX:
4501         case O_CMP:
4502           v = ((rd & 0x8000) != (-ea & 0x8000)
4503                && (rd & 0x8000) != (res & 0x8000));
4504           break;
4505         case O_NEG:
4506           v = (rd == 0x8000);
4507           break;
4508         }
4509       goto next;
4510
4511     alu32:
4512       if (store2 (sd, &code->dst, res))
4513         goto end;
4514
4515     just_flags_alu32:
4516       n = res & 0x80000000;
4517       nz = res & 0xffffffff;
4518       switch (code->opcode / 4)
4519         {
4520         case O_ADD:
4521         case O_ADDX:
4522           v = ((rd & 0x80000000) == (ea & 0x80000000)
4523                && (rd & 0x80000000) != (res & 0x80000000));
4524           c = ((unsigned) res < (unsigned) rd) || 
4525             ((unsigned) res < (unsigned) ea);
4526           break;
4527         case O_SUB:
4528         case O_SUBX:
4529         case O_CMP:
4530           v = ((rd & 0x80000000) != (-ea & 0x80000000)
4531                && (rd & 0x80000000) != (res & 0x80000000));
4532           c = (unsigned) rd < (unsigned) -ea;
4533           break;
4534         case O_NEG:
4535           v = (rd == 0x80000000);
4536           c = res != 0;
4537           break;
4538         }
4539       goto next;
4540
4541     next:
4542       if ((res = h8_get_delayed_branch (sd)) != 0)
4543         {
4544           pc = res;
4545           h8_set_delayed_branch (sd, 0);
4546         }
4547       else
4548         pc = code->next_pc;
4549
4550     end:
4551       
4552       if (--poll_count < 0)
4553         {
4554           poll_count = POLL_QUIT_INTERVAL;
4555           if ((*sim_callback->poll_quit) != NULL
4556               && (*sim_callback->poll_quit) (sim_callback))
4557             sim_engine_set_run_state (sd, sim_stopped, SIGINT);
4558         }
4559       sim_engine_get_run_state (sd, &reason, &sigrc);
4560     } while (reason == sim_running);
4561
4562   h8_set_ticks (sd, h8_get_ticks (sd) + get_now () - tick_start);
4563   h8_set_cycles (sd, h8_get_cycles (sd) + cycles);
4564   h8_set_insts (sd, h8_get_insts (sd) + insts);
4565   h8_set_pc (sd, pc);
4566   BUILDSR (sd);
4567
4568   if (h8300smode)
4569     h8_set_exr (sd, (trace<<7) | intMask);
4570
4571   h8_set_mask (sd, oldmask);
4572   signal (SIGINT, prev);
4573 }
4574
4575 int
4576 sim_trace (SIM_DESC sd)
4577 {
4578   /* FIXME: Unfinished.  */
4579   (*sim_callback->printf_filtered) (sim_callback,
4580                                     "sim_trace: trace not supported.\n");
4581   return 1;     /* Done.  */
4582 }
4583
4584 int
4585 sim_write (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
4586 {
4587   int i;
4588
4589   init_pointers (sd);
4590   if (addr < 0)
4591     return 0;
4592   for (i = 0; i < size; i++)
4593     {
4594       if (addr < memory_size)
4595         {
4596           h8_set_memory    (sd, addr + i, buffer[i]);
4597           h8_set_cache_idx (sd, addr + i,  0);
4598         }
4599       else
4600         {
4601           h8_set_eightbit (sd, (addr + i) & 0xff, buffer[i]);
4602         }
4603     }
4604   return size;
4605 }
4606
4607 int
4608 sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
4609 {
4610   init_pointers (sd);
4611   if (addr < 0)
4612     return 0;
4613   if (addr < memory_size)
4614     memcpy (buffer, h8_get_memory_buf (sd) + addr, size);
4615   else
4616     memcpy (buffer, h8_get_eightbit_buf (sd) + (addr & 0xff), size);
4617   return size;
4618 }
4619
4620
4621 int
4622 sim_store_register (SIM_DESC sd, int rn, unsigned char *value, int length)
4623 {
4624   int longval;
4625   int shortval;
4626   int intval;
4627   longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
4628   shortval = (value[0] << 8) | (value[1]);
4629   intval = h8300hmode ? longval : shortval;
4630
4631   init_pointers (sd);
4632   switch (rn)
4633     {
4634     case PC_REGNUM:
4635       h8_set_pc (sd, intval);
4636       break;
4637     default:
4638       (*sim_callback->printf_filtered) (sim_callback, 
4639                                         "sim_store_register: bad regnum %d.\n",
4640                                         rn);
4641     case R0_REGNUM:
4642     case R1_REGNUM:
4643     case R2_REGNUM:
4644     case R3_REGNUM:
4645     case R4_REGNUM:
4646     case R5_REGNUM:
4647     case R6_REGNUM:
4648     case R7_REGNUM:
4649       h8_set_reg (sd, rn, intval);
4650       break;
4651     case CCR_REGNUM:
4652       h8_set_ccr (sd, intval);
4653       break;
4654     case EXR_REGNUM:
4655       h8_set_exr (sd, intval);
4656       break;
4657     case SBR_REGNUM:
4658       h8_set_sbr (sd, intval);
4659       break;
4660     case VBR_REGNUM:
4661       h8_set_vbr (sd, intval);
4662       break;
4663     case MACH_REGNUM:
4664       h8_set_mach (sd, intval);
4665       break;
4666     case MACL_REGNUM:
4667       h8_set_macl (sd, intval);
4668       break;
4669     case CYCLE_REGNUM:
4670       h8_set_cycles (sd, longval);
4671       break;
4672
4673     case INST_REGNUM:
4674       h8_set_insts (sd, longval);
4675       break;
4676
4677     case TICK_REGNUM:
4678       h8_set_ticks (sd, longval);
4679       break;
4680     }
4681   return -1;
4682 }
4683
4684 int
4685 sim_fetch_register (SIM_DESC sd, int rn, unsigned char *buf, int length)
4686 {
4687   int v;
4688   int longreg = 0;
4689
4690   init_pointers (sd);
4691
4692   if (!h8300smode && rn >= EXR_REGNUM)
4693     rn++;
4694   switch (rn)
4695     {
4696     default:
4697       (*sim_callback->printf_filtered) (sim_callback, 
4698                                         "sim_fetch_register: bad regnum %d.\n",
4699                                         rn);
4700       v = 0;
4701       break;
4702     case CCR_REGNUM:
4703       v = h8_get_ccr (sd);
4704       break;
4705     case EXR_REGNUM:
4706       v = h8_get_exr (sd);
4707       break;
4708     case PC_REGNUM:
4709       v = h8_get_pc (sd);
4710       break;
4711     case SBR_REGNUM:
4712       v = h8_get_sbr (sd);
4713       break;
4714     case VBR_REGNUM:
4715       v = h8_get_vbr (sd);
4716       break;
4717     case MACH_REGNUM:
4718       v = h8_get_mach (sd);
4719       break;
4720     case MACL_REGNUM:
4721       v = h8_get_macl (sd);
4722       break;
4723     case R0_REGNUM:
4724     case R1_REGNUM:
4725     case R2_REGNUM:
4726     case R3_REGNUM:
4727     case R4_REGNUM:
4728     case R5_REGNUM:
4729     case R6_REGNUM:
4730     case R7_REGNUM:
4731       v = h8_get_reg (sd, rn);
4732       break;
4733     case CYCLE_REGNUM:
4734       v = h8_get_cycles (sd);
4735       longreg = 1;
4736       break;
4737     case TICK_REGNUM:
4738       v = h8_get_ticks (sd);
4739       longreg = 1;
4740       break;
4741     case INST_REGNUM:
4742       v = h8_get_insts (sd);
4743       longreg = 1;
4744       break;
4745     }
4746   if (h8300hmode || longreg)
4747     {
4748       buf[0] = v >> 24;
4749       buf[1] = v >> 16;
4750       buf[2] = v >> 8;
4751       buf[3] = v >> 0;
4752     }
4753   else
4754     {
4755       buf[0] = v >> 8;
4756       buf[1] = v;
4757     }
4758   return -1;
4759 }
4760
4761 void
4762 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
4763 {
4764   sim_engine_get_run_state (sd, reason, sigrc);
4765 }
4766
4767 /* FIXME: Rename to sim_set_mem_size.  */
4768
4769 void
4770 sim_size (int n)
4771 {
4772   /* Memory size is fixed.  */
4773 }
4774
4775 static void
4776 set_simcache_size (SIM_DESC sd, int n)
4777 {
4778   if (sd->sim_cache)
4779     free (sd->sim_cache);
4780   if (n < 2)
4781     n = 2;
4782   sd->sim_cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
4783   memset (sd->sim_cache, 0, sizeof (decoded_inst) * n);
4784   sd->sim_cache_size = n;
4785 }
4786
4787
4788 void
4789 sim_info (SIM_DESC sd, int verbose)
4790 {
4791   double timetaken = (double) h8_get_ticks (sd) / (double) now_persec ();
4792   double virttime = h8_get_cycles (sd) / 10.0e6;
4793
4794   (*sim_callback->printf_filtered) (sim_callback,
4795                                     "\n\n#instructions executed  %10d\n",
4796                                     h8_get_insts (sd));
4797   (*sim_callback->printf_filtered) (sim_callback,
4798                                     "#cycles (v approximate) %10d\n",
4799                                     h8_get_cycles (sd));
4800   (*sim_callback->printf_filtered) (sim_callback,
4801                                     "#real time taken        %10.4f\n",
4802                                     timetaken);
4803   (*sim_callback->printf_filtered) (sim_callback,
4804                                     "#virtual time taken     %10.4f\n",
4805                                     virttime);
4806   if (timetaken != 0.0)
4807     (*sim_callback->printf_filtered) (sim_callback,
4808                                       "#simulation ratio       %10.4f\n",
4809                                       virttime / timetaken);
4810   (*sim_callback->printf_filtered) (sim_callback,
4811                                     "#compiles               %10d\n",
4812                                     h8_get_compiles (sd));
4813   (*sim_callback->printf_filtered) (sim_callback,
4814                                     "#cache size             %10d\n",
4815                                     sd->sim_cache_size);
4816
4817 #ifdef ADEBUG
4818   /* This to be conditional on `what' (aka `verbose'),
4819      however it was never passed as non-zero.  */
4820   if (1)
4821     {
4822       int i;
4823       for (i = 0; i < O_LAST; i++)
4824         {
4825           if (h8_get_stats (sd, i))
4826             (*sim_callback->printf_filtered) (sim_callback, "%d: %d\n", 
4827                                               i, h8_get_stats (sd, i));
4828         }
4829     }
4830 #endif
4831 }
4832
4833 /* Indicate whether the cpu is an H8/300 or H8/300H.
4834    FLAG is non-zero for the H8/300H.  */
4835
4836 void
4837 set_h8300h (unsigned long machine)
4838 {
4839   /* FIXME: Much of the code in sim_load can be moved to sim_open.
4840      This function being replaced by a sim_open:ARGV configuration
4841      option.  */
4842
4843   if (machine == bfd_mach_h8300sx || machine == bfd_mach_h8300sxn)
4844     h8300sxmode = 1;
4845
4846   if (machine == bfd_mach_h8300s || machine == bfd_mach_h8300sn || h8300sxmode)
4847     h8300smode = 1;
4848
4849   if (machine == bfd_mach_h8300h || machine == bfd_mach_h8300hn || h8300smode)
4850     h8300hmode = 1;
4851 }
4852
4853 /* Cover function of sim_state_free to free the cpu buffers as well.  */
4854
4855 static void
4856 free_state (SIM_DESC sd)
4857 {
4858   if (STATE_MODULES (sd) != NULL)
4859     sim_module_uninstall (sd);
4860
4861   /* Fixme: free buffers in _sim_cpu.  */
4862   sim_state_free (sd);
4863 }
4864
4865 SIM_DESC
4866 sim_open (SIM_OPEN_KIND kind, 
4867           struct host_callback_struct *callback, 
4868           struct bfd *abfd, 
4869           char **argv)
4870 {
4871   SIM_DESC sd;
4872   sim_cpu *cpu;
4873
4874   sd = sim_state_alloc (kind, callback);
4875   sd->cpu = sim_cpu_alloc (sd, 0);
4876   cpu = STATE_CPU (sd, 0);
4877   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
4878   sim_state_initialize (sd, cpu);
4879   /* sim_cpu object is new, so some initialization is needed.  */
4880   init_pointers_needed = 1;
4881
4882   /* For compatibility (FIXME: is this right?).  */
4883   current_alignment = NONSTRICT_ALIGNMENT;
4884   current_target_byte_order = BIG_ENDIAN;
4885
4886   if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
4887     {
4888       free_state (sd);
4889       return 0;
4890     }
4891
4892     /* getopt will print the error message so we just have to exit if
4893        this fails.  FIXME: Hmmm...  in the case of gdb we need getopt
4894        to call print_filtered.  */
4895   if (sim_parse_args (sd, argv) != SIM_RC_OK)
4896     {
4897       /* Uninstall the modules to avoid memory leaks,
4898          file descriptor leaks, etc.  */
4899       free_state (sd);
4900       return 0;
4901     }
4902
4903   /* Check for/establish the a reference program image.  */
4904   if (sim_analyze_program (sd,
4905                            (STATE_PROG_ARGV (sd) != NULL
4906                             ? *STATE_PROG_ARGV (sd)
4907                             : NULL), abfd) != SIM_RC_OK)
4908     {
4909       free_state (sd);
4910       return 0;
4911     }
4912
4913   /* Establish any remaining configuration options.  */
4914   if (sim_config (sd) != SIM_RC_OK)
4915     {
4916       free_state (sd);
4917       return 0;
4918     }
4919
4920   if (sim_post_argv_init (sd) != SIM_RC_OK)
4921     {
4922       /* Uninstall the modules to avoid memory leaks,
4923          file descriptor leaks, etc.  */
4924       free_state (sd);
4925       return 0;
4926     }
4927
4928   /*  sim_hw_configure (sd); */
4929
4930   /* FIXME: Much of the code in sim_load can be moved here.  */
4931
4932   sim_kind = kind;
4933   myname = argv[0];
4934   sim_callback = callback;
4935   return sd;
4936 }
4937
4938 void
4939 sim_close (SIM_DESC sd, int quitting)
4940 {
4941   /* Nothing to do.  */
4942 }
4943
4944 /* Called by gdb to load a program into memory.  */
4945
4946 SIM_RC
4947 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
4948 {
4949   bfd *prog_bfd;
4950
4951   /* FIXME: The code below that sets a specific variant of the H8/300
4952      being simulated should be moved to sim_open().  */
4953
4954   /* See if the file is for the H8/300 or H8/300H.  */
4955   /* ??? This may not be the most efficient way.  The z8k simulator
4956      does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO).  */
4957   if (abfd != NULL)
4958     prog_bfd = abfd;
4959   else
4960     prog_bfd = bfd_openr (prog, "coff-h8300");
4961   if (prog_bfd != NULL)
4962     {
4963       /* Set the cpu type.  We ignore failure from bfd_check_format
4964          and bfd_openr as sim_load_file checks too.  */
4965       if (bfd_check_format (prog_bfd, bfd_object))
4966         {
4967           set_h8300h (bfd_get_mach (prog_bfd));
4968         }
4969     }
4970
4971   /* If we're using gdb attached to the simulator, then we have to
4972      reallocate memory for the simulator.
4973
4974      When gdb first starts, it calls fetch_registers (among other
4975      functions), which in turn calls init_pointers, which allocates
4976      simulator memory.
4977
4978      The problem is when we do that, we don't know whether we're
4979      debugging an H8/300 or H8/300H program.
4980
4981      This is the first point at which we can make that determination,
4982      so we just reallocate memory now; this will also allow us to handle
4983      switching between H8/300 and H8/300H programs without exiting
4984      gdb.  */
4985
4986   if (h8300smode)
4987     memory_size = H8300S_MSIZE;
4988   else if (h8300hmode)
4989     memory_size = H8300H_MSIZE;
4990   else
4991     memory_size = H8300_MSIZE;
4992
4993   if (h8_get_memory_buf (sd))
4994     free (h8_get_memory_buf (sd));
4995   if (h8_get_cache_idx_buf (sd))
4996     free (h8_get_cache_idx_buf (sd));
4997   if (h8_get_eightbit_buf (sd))
4998     free (h8_get_eightbit_buf (sd));
4999
5000   h8_set_memory_buf (sd, (unsigned char *) 
5001                      calloc (sizeof (char), memory_size));
5002   h8_set_cache_idx_buf (sd, (unsigned short *) 
5003                         calloc (sizeof (short), memory_size));
5004   h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
5005
5006   /* `msize' must be a power of two.  */
5007   if ((memory_size & (memory_size - 1)) != 0)
5008     {
5009       (*sim_callback->printf_filtered) (sim_callback, 
5010                                         "sim_load: bad memory size.\n");
5011       return SIM_RC_FAIL;
5012     }
5013   h8_set_mask (sd, memory_size - 1);
5014
5015   if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
5016                      sim_kind == SIM_OPEN_DEBUG,
5017                      0, sim_write)
5018       == NULL)
5019     {
5020       /* Close the bfd if we opened it.  */
5021       if (abfd == NULL && prog_bfd != NULL)
5022         bfd_close (prog_bfd);
5023       return SIM_RC_FAIL;
5024     }
5025
5026   /* Close the bfd if we opened it.  */
5027   if (abfd == NULL && prog_bfd != NULL)
5028     bfd_close (prog_bfd);
5029   return SIM_RC_OK;
5030 }
5031
5032 SIM_RC
5033 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
5034 {
5035   int i = 0;
5036   int len_arg = 0;
5037   int no_of_args = 0;
5038
5039   if (abfd != NULL)
5040     h8_set_pc (sd, bfd_get_start_address (abfd));
5041   else
5042     h8_set_pc (sd, 0);
5043
5044   /* Command Line support.  */
5045   if (argv != NULL)
5046     {
5047       /* Counting the no. of commandline arguments.  */
5048       for (no_of_args = 0; argv[no_of_args] != NULL; no_of_args++)
5049         continue;
5050
5051       /* Allocating memory for the argv pointers.  */
5052       h8_set_command_line (sd, (char **) malloc ((sizeof (char *))
5053                                                  * (no_of_args + 1)));
5054
5055       for (i = 0; i < no_of_args; i++)
5056         {
5057           /* Copying the argument string.  */
5058           h8_set_cmdline_arg (sd, i, (char *) strdup (argv[i]));
5059         }
5060       h8_set_cmdline_arg (sd, i, NULL);
5061     }
5062   
5063   return SIM_RC_OK;
5064 }
5065
5066 void
5067 sim_do_command (SIM_DESC sd, char *cmd)
5068 {
5069   (*sim_callback->printf_filtered) (sim_callback,
5070                                     "This simulator does not accept any commands.\n");
5071 }
5072
5073 void
5074 sim_set_callbacks (struct host_callback_struct *ptr)
5075 {
5076   sim_callback = ptr;
5077 }