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