4 * Copyright (c) 2005-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
36 //#define DEBUG_DISPATCH 1
38 /* Fake floating point. */
39 #define tcg_gen_mov_f64 tcg_gen_mov_i64
40 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
41 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
43 #define DEFO32(name, offset) static TCGv QREG_##name;
44 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
45 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
51 static TCGv_i32 cpu_halted;
52 static TCGv_i32 cpu_exception_index;
54 static TCGv_env cpu_env;
56 static char cpu_reg_names[3*8*3 + 5*4];
57 static TCGv cpu_dregs[8];
58 static TCGv cpu_aregs[8];
59 static TCGv_i64 cpu_fregs[8];
60 static TCGv_i64 cpu_macc[4];
62 #define REG(insn, pos) (((insn) >> (pos)) & 7)
63 #define DREG(insn, pos) cpu_dregs[REG(insn, pos)]
64 #define AREG(insn, pos) cpu_aregs[REG(insn, pos)]
65 #define FREG(insn, pos) cpu_fregs[REG(insn, pos)]
66 #define MACREG(acc) cpu_macc[acc]
67 #define QREG_SP cpu_aregs[7]
69 static TCGv NULL_QREG;
70 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
71 /* Used to distinguish stores from bad addressing modes. */
72 static TCGv store_dummy;
74 #include "exec/gen-icount.h"
76 void m68k_tcg_init(void)
81 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
82 tcg_ctx.tcg_env = cpu_env;
84 #define DEFO32(name, offset) \
85 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
86 offsetof(CPUM68KState, offset), #name);
87 #define DEFO64(name, offset) \
88 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
89 offsetof(CPUM68KState, offset), #name);
90 #define DEFF64(name, offset) DEFO64(name, offset)
96 cpu_halted = tcg_global_mem_new_i32(cpu_env,
97 -offsetof(M68kCPU, env) +
98 offsetof(CPUState, halted), "HALTED");
99 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
100 -offsetof(M68kCPU, env) +
101 offsetof(CPUState, exception_index),
105 for (i = 0; i < 8; i++) {
106 sprintf(p, "D%d", i);
107 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
108 offsetof(CPUM68KState, dregs[i]), p);
110 sprintf(p, "A%d", i);
111 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
112 offsetof(CPUM68KState, aregs[i]), p);
114 sprintf(p, "F%d", i);
115 cpu_fregs[i] = tcg_global_mem_new_i64(cpu_env,
116 offsetof(CPUM68KState, fregs[i]), p);
119 for (i = 0; i < 4; i++) {
120 sprintf(p, "ACC%d", i);
121 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
122 offsetof(CPUM68KState, macc[i]), p);
126 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
127 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
130 /* internal defines */
131 typedef struct DisasContext {
133 target_ulong insn_pc; /* Start of the current instruction. */
136 CCOp cc_op; /* Current CC operation */
140 struct TranslationBlock *tb;
141 int singlestep_enabled;
146 #define DISAS_JUMP_NEXT 4
148 #if defined(CONFIG_USER_ONLY)
151 #define IS_USER(s) s->user
154 /* XXX: move that elsewhere */
155 /* ??? Fix exceptions. */
156 static void *gen_throws_exception;
157 #define gen_last_qop NULL
159 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
161 #ifdef DEBUG_DISPATCH
162 #define DISAS_INSN(name) \
163 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
165 static void disas_##name(CPUM68KState *env, DisasContext *s, \
168 qemu_log("Dispatch " #name "\n"); \
169 real_disas_##name(env, s, insn); \
171 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
174 #define DISAS_INSN(name) \
175 static void disas_##name(CPUM68KState *env, DisasContext *s, \
179 static const uint8_t cc_op_live[CC_OP_NB] = {
180 [CC_OP_FLAGS] = CCF_C | CCF_V | CCF_Z | CCF_N | CCF_X,
181 [CC_OP_ADDB ... CC_OP_ADDL] = CCF_X | CCF_N | CCF_V,
182 [CC_OP_SUBB ... CC_OP_SUBL] = CCF_X | CCF_N | CCF_V,
183 [CC_OP_CMPB ... CC_OP_CMPL] = CCF_X | CCF_N | CCF_V,
184 [CC_OP_LOGIC] = CCF_X | CCF_N
187 static void set_cc_op(DisasContext *s, CCOp op)
189 CCOp old_op = s->cc_op;
198 /* Discard CC computation that will no longer be used.
199 Note that X and N are never dead. */
200 dead = cc_op_live[old_op] & ~cc_op_live[op];
202 tcg_gen_discard_i32(QREG_CC_C);
205 tcg_gen_discard_i32(QREG_CC_Z);
208 tcg_gen_discard_i32(QREG_CC_V);
212 /* Update the CPU env CC_OP state. */
213 static void update_cc_op(DisasContext *s)
215 if (!s->cc_op_synced) {
217 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
221 /* Generate a load from the specified address. Narrow values are
222 sign extended to full register width. */
223 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
226 int index = IS_USER(s);
227 tmp = tcg_temp_new_i32();
231 tcg_gen_qemu_ld8s(tmp, addr, index);
233 tcg_gen_qemu_ld8u(tmp, addr, index);
237 tcg_gen_qemu_ld16s(tmp, addr, index);
239 tcg_gen_qemu_ld16u(tmp, addr, index);
243 tcg_gen_qemu_ld32u(tmp, addr, index);
246 g_assert_not_reached();
248 gen_throws_exception = gen_last_qop;
252 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
255 int index = IS_USER(s);
256 tmp = tcg_temp_new_i64();
257 tcg_gen_qemu_ldf64(tmp, addr, index);
258 gen_throws_exception = gen_last_qop;
262 /* Generate a store. */
263 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
265 int index = IS_USER(s);
268 tcg_gen_qemu_st8(val, addr, index);
271 tcg_gen_qemu_st16(val, addr, index);
275 tcg_gen_qemu_st32(val, addr, index);
278 g_assert_not_reached();
280 gen_throws_exception = gen_last_qop;
283 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
285 int index = IS_USER(s);
286 tcg_gen_qemu_stf64(val, addr, index);
287 gen_throws_exception = gen_last_qop;
296 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
297 otherwise generate a store. */
298 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
301 if (what == EA_STORE) {
302 gen_store(s, opsize, addr, val);
305 return gen_load(s, opsize, addr, what == EA_LOADS);
309 /* Read a 16-bit immediate constant */
310 static inline uint16_t read_im16(CPUM68KState *env, DisasContext *s)
313 im = cpu_lduw_code(env, s->pc);
318 /* Read an 8-bit immediate constant */
319 static inline uint8_t read_im8(CPUM68KState *env, DisasContext *s)
321 return read_im16(env, s);
324 /* Read a 32-bit immediate constant. */
325 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
328 im = read_im16(env, s) << 16;
329 im |= 0xffff & read_im16(env, s);
333 /* Calculate and address index. */
334 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
339 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
340 if ((ext & 0x800) == 0) {
341 tcg_gen_ext16s_i32(tmp, add);
344 scale = (ext >> 9) & 3;
346 tcg_gen_shli_i32(tmp, add, scale);
352 /* Handle a base + index + displacement effective addresss.
353 A NULL_QREG base means pc-relative. */
354 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
363 ext = read_im16(env, s);
365 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
368 if (m68k_feature(s->env, M68K_FEATURE_M68000) &&
369 !m68k_feature(s->env, M68K_FEATURE_SCALED_INDEX)) {
374 /* full extension word format */
375 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
378 if ((ext & 0x30) > 0x10) {
379 /* base displacement */
380 if ((ext & 0x30) == 0x20) {
381 bd = (int16_t)read_im16(env, s);
383 bd = read_im32(env, s);
388 tmp = tcg_temp_new();
389 if ((ext & 0x44) == 0) {
391 add = gen_addr_index(ext, tmp);
395 if ((ext & 0x80) == 0) {
396 /* base not suppressed */
397 if (IS_NULL_QREG(base)) {
398 base = tcg_const_i32(offset + bd);
401 if (!IS_NULL_QREG(add)) {
402 tcg_gen_add_i32(tmp, add, base);
408 if (!IS_NULL_QREG(add)) {
410 tcg_gen_addi_i32(tmp, add, bd);
414 add = tcg_const_i32(bd);
416 if ((ext & 3) != 0) {
417 /* memory indirect */
418 base = gen_load(s, OS_LONG, add, 0);
419 if ((ext & 0x44) == 4) {
420 add = gen_addr_index(ext, tmp);
421 tcg_gen_add_i32(tmp, add, base);
427 /* outer displacement */
428 if ((ext & 3) == 2) {
429 od = (int16_t)read_im16(env, s);
431 od = read_im32(env, s);
437 tcg_gen_addi_i32(tmp, add, od);
442 /* brief extension word format */
443 tmp = tcg_temp_new();
444 add = gen_addr_index(ext, tmp);
445 if (!IS_NULL_QREG(base)) {
446 tcg_gen_add_i32(tmp, add, base);
448 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
450 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
457 /* Sign or zero extend a value. */
459 static inline void gen_ext(TCGv res, TCGv val, int opsize, int sign)
464 tcg_gen_ext8s_i32(res, val);
466 tcg_gen_ext8u_i32(res, val);
471 tcg_gen_ext16s_i32(res, val);
473 tcg_gen_ext16u_i32(res, val);
477 tcg_gen_mov_i32(res, val);
480 g_assert_not_reached();
484 /* Evaluate all the CC flags. */
486 static void gen_flush_flags(DisasContext *s)
497 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
498 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
499 /* Compute signed overflow for addition. */
502 tcg_gen_sub_i32(t0, QREG_CC_N, QREG_CC_V);
503 gen_ext(t0, t0, s->cc_op - CC_OP_ADDB, 1);
504 tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
505 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
507 tcg_gen_andc_i32(QREG_CC_V, t1, QREG_CC_V);
514 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
515 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
516 /* Compute signed overflow for subtraction. */
519 tcg_gen_add_i32(t0, QREG_CC_N, QREG_CC_V);
520 gen_ext(t0, t0, s->cc_op - CC_OP_SUBB, 1);
521 tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
522 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
524 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t1);
531 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_C, QREG_CC_N, QREG_CC_V);
532 tcg_gen_sub_i32(QREG_CC_Z, QREG_CC_N, QREG_CC_V);
533 gen_ext(QREG_CC_Z, QREG_CC_Z, s->cc_op - CC_OP_CMPB, 1);
534 /* Compute signed overflow for subtraction. */
536 tcg_gen_xor_i32(t0, QREG_CC_Z, QREG_CC_N);
537 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, QREG_CC_N);
538 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t0);
540 tcg_gen_mov_i32(QREG_CC_N, QREG_CC_Z);
544 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
545 tcg_gen_movi_i32(QREG_CC_C, 0);
546 tcg_gen_movi_i32(QREG_CC_V, 0);
550 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
554 t0 = tcg_const_i32(s->cc_op);
555 gen_helper_flush_flags(cpu_env, t0);
560 /* Note that flush_flags also assigned to env->cc_op. */
561 s->cc_op = CC_OP_FLAGS;
565 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
569 if (opsize == OS_LONG) {
572 tmp = tcg_temp_new();
573 gen_ext(tmp, val, opsize, sign);
579 static void gen_logic_cc(DisasContext *s, TCGv val, int opsize)
581 gen_ext(QREG_CC_N, val, opsize, 1);
582 set_cc_op(s, CC_OP_LOGIC);
585 static void gen_update_cc_cmp(DisasContext *s, TCGv dest, TCGv src, int opsize)
587 tcg_gen_mov_i32(QREG_CC_N, dest);
588 tcg_gen_mov_i32(QREG_CC_V, src);
589 set_cc_op(s, CC_OP_CMPB + opsize);
592 static void gen_update_cc_add(TCGv dest, TCGv src, int opsize)
594 gen_ext(QREG_CC_N, dest, opsize, 1);
595 tcg_gen_mov_i32(QREG_CC_V, src);
598 static inline int opsize_bytes(int opsize)
601 case OS_BYTE: return 1;
602 case OS_WORD: return 2;
603 case OS_LONG: return 4;
604 case OS_SINGLE: return 4;
605 case OS_DOUBLE: return 8;
606 case OS_EXTENDED: return 12;
607 case OS_PACKED: return 12;
609 g_assert_not_reached();
613 static inline int insn_opsize(int insn)
615 switch ((insn >> 6) & 3) {
616 case 0: return OS_BYTE;
617 case 1: return OS_WORD;
618 case 2: return OS_LONG;
620 g_assert_not_reached();
624 /* Assign value to a register. If the width is less than the register width
625 only the low part of the register is set. */
626 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
631 tcg_gen_andi_i32(reg, reg, 0xffffff00);
632 tmp = tcg_temp_new();
633 tcg_gen_ext8u_i32(tmp, val);
634 tcg_gen_or_i32(reg, reg, tmp);
637 tcg_gen_andi_i32(reg, reg, 0xffff0000);
638 tmp = tcg_temp_new();
639 tcg_gen_ext16u_i32(tmp, val);
640 tcg_gen_or_i32(reg, reg, tmp);
644 tcg_gen_mov_i32(reg, val);
647 g_assert_not_reached();
651 /* Generate code for an "effective address". Does not adjust the base
652 register for autoincrement addressing modes. */
653 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
661 switch ((insn >> 3) & 7) {
662 case 0: /* Data register direct. */
663 case 1: /* Address register direct. */
665 case 2: /* Indirect register */
666 case 3: /* Indirect postincrement. */
667 return AREG(insn, 0);
668 case 4: /* Indirect predecrememnt. */
670 tmp = tcg_temp_new();
671 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
673 case 5: /* Indirect displacement. */
675 tmp = tcg_temp_new();
676 ext = read_im16(env, s);
677 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
679 case 6: /* Indirect index + displacement. */
681 return gen_lea_indexed(env, s, reg);
684 case 0: /* Absolute short. */
685 offset = (int16_t)read_im16(env, s);
686 return tcg_const_i32(offset);
687 case 1: /* Absolute long. */
688 offset = read_im32(env, s);
689 return tcg_const_i32(offset);
690 case 2: /* pc displacement */
692 offset += (int16_t)read_im16(env, s);
693 return tcg_const_i32(offset);
694 case 3: /* pc index+displacement. */
695 return gen_lea_indexed(env, s, NULL_QREG);
696 case 4: /* Immediate. */
701 /* Should never happen. */
705 /* Helper function for gen_ea. Reuse the computed address between the
706 for read/write operands. */
707 static inline TCGv gen_ea_once(CPUM68KState *env, DisasContext *s,
708 uint16_t insn, int opsize, TCGv val,
709 TCGv *addrp, ea_what what)
713 if (addrp && what == EA_STORE) {
716 tmp = gen_lea(env, s, insn, opsize);
717 if (IS_NULL_QREG(tmp))
722 return gen_ldst(s, opsize, tmp, val, what);
725 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
726 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
727 ADDRP is non-null for readwrite operands. */
728 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
729 int opsize, TCGv val, TCGv *addrp, ea_what what)
735 switch ((insn >> 3) & 7) {
736 case 0: /* Data register direct. */
738 if (what == EA_STORE) {
739 gen_partset_reg(opsize, reg, val);
742 return gen_extend(reg, opsize, what == EA_LOADS);
744 case 1: /* Address register direct. */
746 if (what == EA_STORE) {
747 tcg_gen_mov_i32(reg, val);
750 return gen_extend(reg, opsize, what == EA_LOADS);
752 case 2: /* Indirect register */
754 return gen_ldst(s, opsize, reg, val, what);
755 case 3: /* Indirect postincrement. */
757 result = gen_ldst(s, opsize, reg, val, what);
758 /* ??? This is not exception safe. The instruction may still
759 fault after this point. */
760 if (what == EA_STORE || !addrp)
761 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
763 case 4: /* Indirect predecrememnt. */
766 if (addrp && what == EA_STORE) {
769 tmp = gen_lea(env, s, insn, opsize);
770 if (IS_NULL_QREG(tmp))
775 result = gen_ldst(s, opsize, tmp, val, what);
776 /* ??? This is not exception safe. The instruction may still
777 fault after this point. */
778 if (what == EA_STORE || !addrp) {
780 tcg_gen_mov_i32(reg, tmp);
784 case 5: /* Indirect displacement. */
785 case 6: /* Indirect index + displacement. */
786 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
789 case 0: /* Absolute short. */
790 case 1: /* Absolute long. */
791 case 2: /* pc displacement */
792 case 3: /* pc index+displacement. */
793 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
794 case 4: /* Immediate. */
795 /* Sign extend values for consistency. */
798 if (what == EA_LOADS) {
799 offset = (int8_t)read_im8(env, s);
801 offset = read_im8(env, s);
805 if (what == EA_LOADS) {
806 offset = (int16_t)read_im16(env, s);
808 offset = read_im16(env, s);
812 offset = read_im32(env, s);
815 g_assert_not_reached();
817 return tcg_const_i32(offset);
822 /* Should never happen. */
834 static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
840 /* The CC_OP_CMP form can handle most normal comparisons directly. */
841 if (op == CC_OP_CMPB || op == CC_OP_CMPW || op == CC_OP_CMPL) {
848 tcond = TCG_COND_LEU;
852 tcond = TCG_COND_LTU;
861 c->v2 = tcg_const_i32(0);
862 c->v1 = tmp = tcg_temp_new();
863 tcg_gen_sub_i32(tmp, QREG_CC_N, QREG_CC_V);
864 gen_ext(tmp, tmp, op - CC_OP_CMPB, 1);
879 c->v2 = tcg_const_i32(0);
885 tcond = TCG_COND_NEVER;
887 case 14: /* GT (!(Z || (N ^ V))) */
888 case 15: /* LE (Z || (N ^ V)) */
889 /* Logic operations clear V, which simplifies LE to (Z || N),
890 and since Z and N are co-located, this becomes a normal
892 if (op == CC_OP_LOGIC) {
898 case 12: /* GE (!(N ^ V)) */
899 case 13: /* LT (N ^ V) */
900 /* Logic operations clear V, which simplifies this to N. */
901 if (op != CC_OP_LOGIC) {
905 case 10: /* PL (!N) */
906 case 11: /* MI (N) */
907 /* Several cases represent N normally. */
908 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
909 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
916 case 6: /* NE (!Z) */
918 /* Some cases fold Z into N. */
919 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
920 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
927 case 4: /* CC (!C) */
929 /* Some cases fold C into X. */
930 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
931 op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
937 case 8: /* VC (!V) */
939 /* Logic operations clear V and C. */
940 if (op == CC_OP_LOGIC) {
941 tcond = TCG_COND_NEVER;
948 /* Otherwise, flush flag state to CC_OP_FLAGS. */
955 /* Invalid, or handled above. */
957 case 2: /* HI (!C && !Z) -> !(C || Z)*/
958 case 3: /* LS (C || Z) */
959 c->v1 = tmp = tcg_temp_new();
961 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
962 tcg_gen_or_i32(tmp, tmp, QREG_CC_C);
965 case 4: /* CC (!C) */
970 case 6: /* NE (!Z) */
975 case 8: /* VC (!V) */
980 case 10: /* PL (!N) */
981 case 11: /* MI (N) */
985 case 12: /* GE (!(N ^ V)) */
986 case 13: /* LT (N ^ V) */
987 c->v1 = tmp = tcg_temp_new();
989 tcg_gen_xor_i32(tmp, QREG_CC_N, QREG_CC_V);
992 case 14: /* GT (!(Z || (N ^ V))) */
993 case 15: /* LE (Z || (N ^ V)) */
994 c->v1 = tmp = tcg_temp_new();
996 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
997 tcg_gen_neg_i32(tmp, tmp);
998 tmp2 = tcg_temp_new();
999 tcg_gen_xor_i32(tmp2, QREG_CC_N, QREG_CC_V);
1000 tcg_gen_or_i32(tmp, tmp, tmp2);
1001 tcg_temp_free(tmp2);
1002 tcond = TCG_COND_LT;
1007 if ((cond & 1) == 0) {
1008 tcond = tcg_invert_cond(tcond);
1013 static void free_cond(DisasCompare *c)
1016 tcg_temp_free(c->v1);
1019 tcg_temp_free(c->v2);
1023 static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
1027 gen_cc_cond(&c, s, cond);
1029 tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
1033 /* Force a TB lookup after an instruction that changes the CPU state. */
1034 static void gen_lookup_tb(DisasContext *s)
1037 tcg_gen_movi_i32(QREG_PC, s->pc);
1038 s->is_jmp = DISAS_UPDATE;
1041 /* Generate a jump to an immediate address. */
1042 static void gen_jmp_im(DisasContext *s, uint32_t dest)
1045 tcg_gen_movi_i32(QREG_PC, dest);
1046 s->is_jmp = DISAS_JUMP;
1049 /* Generate a jump to the address in qreg DEST. */
1050 static void gen_jmp(DisasContext *s, TCGv dest)
1053 tcg_gen_mov_i32(QREG_PC, dest);
1054 s->is_jmp = DISAS_JUMP;
1057 static void gen_exception(DisasContext *s, uint32_t where, int nr)
1060 gen_jmp_im(s, where);
1061 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
1064 static inline void gen_addr_fault(DisasContext *s)
1066 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
1069 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
1070 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
1071 op_sign ? EA_LOADS : EA_LOADU); \
1072 if (IS_NULL_QREG(result)) { \
1073 gen_addr_fault(s); \
1078 #define DEST_EA(env, insn, opsize, val, addrp) do { \
1079 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
1080 if (IS_NULL_QREG(ea_result)) { \
1081 gen_addr_fault(s); \
1086 static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
1088 #ifndef CONFIG_USER_ONLY
1089 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
1090 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
1096 /* Generate a jump to an immediate address. */
1097 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
1099 if (unlikely(s->singlestep_enabled)) {
1100 gen_exception(s, dest, EXCP_DEBUG);
1101 } else if (use_goto_tb(s, dest)) {
1103 tcg_gen_movi_i32(QREG_PC, dest);
1104 tcg_gen_exit_tb((uintptr_t)s->tb + n);
1106 gen_jmp_im(s, dest);
1109 s->is_jmp = DISAS_TB_JUMP;
1118 cond = (insn >> 8) & 0xf;
1119 gen_cc_cond(&c, s, cond);
1121 tmp = tcg_temp_new();
1122 tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
1125 tcg_gen_neg_i32(tmp, tmp);
1126 DEST_EA(env, insn, OS_BYTE, tmp, NULL);
1138 reg = DREG(insn, 0);
1140 offset = (int16_t)read_im16(env, s);
1141 l1 = gen_new_label();
1142 gen_jmpcc(s, (insn >> 8) & 0xf, l1);
1144 tmp = tcg_temp_new();
1145 tcg_gen_ext16s_i32(tmp, reg);
1146 tcg_gen_addi_i32(tmp, tmp, -1);
1147 gen_partset_reg(OS_WORD, reg, tmp);
1148 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, -1, l1);
1149 gen_jmp_tb(s, 1, base + offset);
1151 gen_jmp_tb(s, 0, s->pc);
1154 DISAS_INSN(undef_mac)
1156 gen_exception(s, s->pc - 2, EXCP_LINEA);
1159 DISAS_INSN(undef_fpu)
1161 gen_exception(s, s->pc - 2, EXCP_LINEF);
1166 M68kCPU *cpu = m68k_env_get_cpu(env);
1168 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
1169 cpu_abort(CPU(cpu), "Illegal instruction: %04x @ %08x", insn, s->pc - 2);
1179 sign = (insn & 0x100) != 0;
1180 reg = DREG(insn, 9);
1181 tmp = tcg_temp_new();
1183 tcg_gen_ext16s_i32(tmp, reg);
1185 tcg_gen_ext16u_i32(tmp, reg);
1186 SRC_EA(env, src, OS_WORD, sign, NULL);
1187 tcg_gen_mul_i32(tmp, tmp, src);
1188 tcg_gen_mov_i32(reg, tmp);
1189 gen_logic_cc(s, tmp, OS_LONG);
1199 sign = (insn & 0x100) != 0;
1200 reg = DREG(insn, 9);
1202 tcg_gen_ext16s_i32(QREG_DIV1, reg);
1204 tcg_gen_ext16u_i32(QREG_DIV1, reg);
1206 SRC_EA(env, src, OS_WORD, sign, NULL);
1207 tcg_gen_mov_i32(QREG_DIV2, src);
1209 gen_helper_divs(cpu_env, tcg_const_i32(1));
1211 gen_helper_divu(cpu_env, tcg_const_i32(1));
1214 tmp = tcg_temp_new();
1215 src = tcg_temp_new();
1216 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
1217 tcg_gen_shli_i32(src, QREG_DIV2, 16);
1218 tcg_gen_or_i32(reg, tmp, src);
1220 set_cc_op(s, CC_OP_FLAGS);
1230 ext = read_im16(env, s);
1232 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1235 num = DREG(ext, 12);
1237 tcg_gen_mov_i32(QREG_DIV1, num);
1238 SRC_EA(env, den, OS_LONG, 0, NULL);
1239 tcg_gen_mov_i32(QREG_DIV2, den);
1241 gen_helper_divs(cpu_env, tcg_const_i32(0));
1243 gen_helper_divu(cpu_env, tcg_const_i32(0));
1245 if ((ext & 7) == ((ext >> 12) & 7)) {
1247 tcg_gen_mov_i32 (reg, QREG_DIV1);
1250 tcg_gen_mov_i32 (reg, QREG_DIV2);
1252 set_cc_op(s, CC_OP_FLAGS);
1265 add = (insn & 0x4000) != 0;
1266 opsize = insn_opsize(insn);
1267 reg = gen_extend(DREG(insn, 9), opsize, 1);
1268 dest = tcg_temp_new();
1270 SRC_EA(env, tmp, opsize, 1, &addr);
1274 SRC_EA(env, src, opsize, 1, NULL);
1277 tcg_gen_add_i32(dest, tmp, src);
1278 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src);
1279 set_cc_op(s, CC_OP_ADDB + opsize);
1281 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, tmp, src);
1282 tcg_gen_sub_i32(dest, tmp, src);
1283 set_cc_op(s, CC_OP_SUBB + opsize);
1285 gen_update_cc_add(dest, src, opsize);
1287 DEST_EA(env, insn, opsize, dest, &addr);
1289 gen_partset_reg(opsize, DREG(insn, 9), dest);
1291 tcg_temp_free(dest);
1294 /* Reverse the order of the bits in REG. */
1298 reg = DREG(insn, 0);
1299 gen_helper_bitrev(reg, reg);
1302 DISAS_INSN(bitop_reg)
1312 if ((insn & 0x38) != 0)
1316 op = (insn >> 6) & 3;
1317 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1320 src2 = tcg_temp_new();
1321 if (opsize == OS_BYTE)
1322 tcg_gen_andi_i32(src2, DREG(insn, 9), 7);
1324 tcg_gen_andi_i32(src2, DREG(insn, 9), 31);
1326 tmp = tcg_const_i32(1);
1327 tcg_gen_shl_i32(tmp, tmp, src2);
1328 tcg_temp_free(src2);
1330 tcg_gen_and_i32(QREG_CC_Z, src1, tmp);
1332 dest = tcg_temp_new();
1335 tcg_gen_xor_i32(dest, src1, tmp);
1338 tcg_gen_andc_i32(dest, src1, tmp);
1341 tcg_gen_or_i32(dest, src1, tmp);
1348 DEST_EA(env, insn, opsize, dest, &addr);
1350 tcg_temp_free(dest);
1356 reg = DREG(insn, 0);
1358 gen_helper_sats(reg, reg, QREG_CC_V);
1359 gen_logic_cc(s, reg, OS_LONG);
1362 static void gen_push(DisasContext *s, TCGv val)
1366 tmp = tcg_temp_new();
1367 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1368 gen_store(s, OS_LONG, tmp, val);
1369 tcg_gen_mov_i32(QREG_SP, tmp);
1381 mask = read_im16(env, s);
1382 tmp = gen_lea(env, s, insn, OS_LONG);
1383 if (IS_NULL_QREG(tmp)) {
1387 addr = tcg_temp_new();
1388 tcg_gen_mov_i32(addr, tmp);
1389 is_load = ((insn & 0x0400) != 0);
1390 for (i = 0; i < 16; i++, mask >>= 1) {
1397 tmp = gen_load(s, OS_LONG, addr, 0);
1398 tcg_gen_mov_i32(reg, tmp);
1400 gen_store(s, OS_LONG, addr, reg);
1403 tcg_gen_addi_i32(addr, addr, 4);
1408 DISAS_INSN(bitop_im)
1418 if ((insn & 0x38) != 0)
1422 op = (insn >> 6) & 3;
1424 bitnum = read_im16(env, s);
1425 if (bitnum & 0xff00) {
1426 disas_undef(env, s, insn);
1430 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1433 if (opsize == OS_BYTE)
1439 tcg_gen_andi_i32(QREG_CC_Z, src1, mask);
1442 tmp = tcg_temp_new();
1445 tcg_gen_xori_i32(tmp, src1, mask);
1448 tcg_gen_andi_i32(tmp, src1, ~mask);
1451 tcg_gen_ori_i32(tmp, src1, mask);
1456 DEST_EA(env, insn, opsize, tmp, &addr);
1461 DISAS_INSN(arith_im)
1470 op = (insn >> 9) & 7;
1471 opsize = insn_opsize(insn);
1474 im = tcg_const_i32((int8_t)read_im8(env, s));
1477 im = tcg_const_i32((int16_t)read_im16(env, s));
1480 im = tcg_const_i32(read_im32(env, s));
1485 SRC_EA(env, src1, opsize, 1, (op == 6) ? NULL : &addr);
1486 dest = tcg_temp_new();
1489 tcg_gen_or_i32(dest, src1, im);
1490 gen_logic_cc(s, dest, opsize);
1493 tcg_gen_and_i32(dest, src1, im);
1494 gen_logic_cc(s, dest, opsize);
1497 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, src1, im);
1498 tcg_gen_sub_i32(dest, src1, im);
1499 gen_update_cc_add(dest, im, opsize);
1500 set_cc_op(s, CC_OP_SUBB + opsize);
1503 tcg_gen_add_i32(dest, src1, im);
1504 gen_update_cc_add(dest, im, opsize);
1505 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, im);
1506 set_cc_op(s, CC_OP_ADDB + opsize);
1509 tcg_gen_xor_i32(dest, src1, im);
1510 gen_logic_cc(s, dest, opsize);
1513 gen_update_cc_cmp(s, src1, im, opsize);
1520 DEST_EA(env, insn, opsize, dest, &addr);
1522 tcg_temp_free(dest);
1529 reg = DREG(insn, 0);
1530 tcg_gen_bswap32_i32(reg, reg);
1540 switch (insn >> 12) {
1541 case 1: /* move.b */
1544 case 2: /* move.l */
1547 case 3: /* move.w */
1553 SRC_EA(env, src, opsize, 1, NULL);
1554 op = (insn >> 6) & 7;
1557 /* The value will already have been sign extended. */
1558 dest = AREG(insn, 9);
1559 tcg_gen_mov_i32(dest, src);
1563 dest_ea = ((insn >> 9) & 7) | (op << 3);
1564 DEST_EA(env, dest_ea, opsize, src, NULL);
1565 /* This will be correct because loads sign extend. */
1566 gen_logic_cc(s, src, opsize);
1577 opsize = insn_opsize(insn);
1578 SRC_EA(env, src, opsize, 1, &addr);
1580 gen_flush_flags(s); /* compute old Z */
1582 /* Perform substract with borrow.
1583 * (X, N) = -(src + X);
1586 z = tcg_const_i32(0);
1587 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, z, QREG_CC_X, z);
1588 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, z, z, QREG_CC_N, QREG_CC_X);
1590 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
1592 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
1594 /* Compute signed-overflow for negation. The normal formula for
1595 * subtraction is (res ^ src) & (src ^ dest), but with dest==0
1596 * this simplies to res & src.
1599 tcg_gen_and_i32(QREG_CC_V, QREG_CC_N, src);
1601 /* Copy the rest of the results into place. */
1602 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
1603 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
1605 set_cc_op(s, CC_OP_FLAGS);
1607 /* result is in QREG_CC_N */
1609 DEST_EA(env, insn, opsize, QREG_CC_N, &addr);
1617 reg = AREG(insn, 9);
1618 tmp = gen_lea(env, s, insn, OS_LONG);
1619 if (IS_NULL_QREG(tmp)) {
1623 tcg_gen_mov_i32(reg, tmp);
1630 opsize = insn_opsize(insn);
1631 DEST_EA(env, insn, opsize, tcg_const_i32(0), NULL);
1632 gen_logic_cc(s, tcg_const_i32(0), opsize);
1635 static TCGv gen_get_ccr(DisasContext *s)
1641 dest = tcg_temp_new();
1642 gen_helper_get_ccr(dest, cpu_env);
1646 DISAS_INSN(move_from_ccr)
1650 ccr = gen_get_ccr(s);
1651 DEST_EA(env, insn, OS_WORD, ccr, NULL);
1661 opsize = insn_opsize(insn);
1662 SRC_EA(env, src1, opsize, 1, &addr);
1663 dest = tcg_temp_new();
1664 tcg_gen_neg_i32(dest, src1);
1665 set_cc_op(s, CC_OP_SUBB + opsize);
1666 gen_update_cc_add(dest, src1, opsize);
1667 tcg_gen_setcondi_i32(TCG_COND_NE, QREG_CC_X, dest, 0);
1668 DEST_EA(env, insn, opsize, dest, &addr);
1669 tcg_temp_free(dest);
1672 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1675 tcg_gen_movi_i32(QREG_CC_C, val & CCF_C ? 1 : 0);
1676 tcg_gen_movi_i32(QREG_CC_V, val & CCF_V ? -1 : 0);
1677 tcg_gen_movi_i32(QREG_CC_Z, val & CCF_Z ? 0 : 1);
1678 tcg_gen_movi_i32(QREG_CC_N, val & CCF_N ? -1 : 0);
1679 tcg_gen_movi_i32(QREG_CC_X, val & CCF_X ? 1 : 0);
1681 gen_helper_set_sr(cpu_env, tcg_const_i32(val));
1683 set_cc_op(s, CC_OP_FLAGS);
1686 static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
1689 if ((insn & 0x38) == 0) {
1691 gen_helper_set_ccr(cpu_env, DREG(insn, 0));
1693 gen_helper_set_sr(cpu_env, DREG(insn, 0));
1695 set_cc_op(s, CC_OP_FLAGS);
1696 } else if ((insn & 0x3f) == 0x3c) {
1698 val = read_im16(env, s);
1699 gen_set_sr_im(s, val, ccr_only);
1701 disas_undef(env, s, insn);
1706 DISAS_INSN(move_to_ccr)
1708 gen_set_sr(env, s, insn, 1);
1718 opsize = insn_opsize(insn);
1719 SRC_EA(env, src1, opsize, 1, &addr);
1720 dest = tcg_temp_new();
1721 tcg_gen_not_i32(dest, src1);
1722 DEST_EA(env, insn, opsize, dest, &addr);
1723 gen_logic_cc(s, dest, opsize);
1732 src1 = tcg_temp_new();
1733 src2 = tcg_temp_new();
1734 reg = DREG(insn, 0);
1735 tcg_gen_shli_i32(src1, reg, 16);
1736 tcg_gen_shri_i32(src2, reg, 16);
1737 tcg_gen_or_i32(reg, src1, src2);
1738 gen_logic_cc(s, reg, OS_LONG);
1743 gen_exception(s, s->pc - 2, EXCP_DEBUG);
1750 tmp = gen_lea(env, s, insn, OS_LONG);
1751 if (IS_NULL_QREG(tmp)) {
1764 reg = DREG(insn, 0);
1765 op = (insn >> 6) & 7;
1766 tmp = tcg_temp_new();
1768 tcg_gen_ext16s_i32(tmp, reg);
1770 tcg_gen_ext8s_i32(tmp, reg);
1772 gen_partset_reg(OS_WORD, reg, tmp);
1774 tcg_gen_mov_i32(reg, tmp);
1775 gen_logic_cc(s, tmp, OS_LONG);
1783 opsize = insn_opsize(insn);
1784 SRC_EA(env, tmp, opsize, 1, NULL);
1785 gen_logic_cc(s, tmp, opsize);
1790 /* Implemented as a NOP. */
1795 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1798 /* ??? This should be atomic. */
1805 dest = tcg_temp_new();
1806 SRC_EA(env, src1, OS_BYTE, 1, &addr);
1807 gen_logic_cc(s, src1, OS_BYTE);
1808 tcg_gen_ori_i32(dest, src1, 0x80);
1809 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1819 /* The upper 32 bits of the product are discarded, so
1820 muls.l and mulu.l are functionally equivalent. */
1821 ext = read_im16(env, s);
1823 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1826 reg = DREG(ext, 12);
1827 SRC_EA(env, src1, OS_LONG, 0, NULL);
1828 dest = tcg_temp_new();
1829 tcg_gen_mul_i32(dest, src1, reg);
1830 tcg_gen_mov_i32(reg, dest);
1831 /* Unlike m68k, coldfire always clears the overflow bit. */
1832 gen_logic_cc(s, dest, OS_LONG);
1835 static void gen_link(DisasContext *s, uint16_t insn, int32_t offset)
1840 reg = AREG(insn, 0);
1841 tmp = tcg_temp_new();
1842 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1843 gen_store(s, OS_LONG, tmp, reg);
1844 if ((insn & 7) != 7) {
1845 tcg_gen_mov_i32(reg, tmp);
1847 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1855 offset = read_im16(env, s);
1856 gen_link(s, insn, offset);
1863 offset = read_im32(env, s);
1864 gen_link(s, insn, offset);
1873 src = tcg_temp_new();
1874 reg = AREG(insn, 0);
1875 tcg_gen_mov_i32(src, reg);
1876 tmp = gen_load(s, OS_LONG, src, 0);
1877 tcg_gen_mov_i32(reg, tmp);
1878 tcg_gen_addi_i32(QREG_SP, src, 4);
1889 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1890 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1898 /* Load the target address first to ensure correct exception
1900 tmp = gen_lea(env, s, insn, OS_LONG);
1901 if (IS_NULL_QREG(tmp)) {
1905 if ((insn & 0x40) == 0) {
1907 gen_push(s, tcg_const_i32(s->pc));
1921 if ((insn & 070) == 010) {
1922 /* Operation on address register is always long. */
1925 opsize = insn_opsize(insn);
1927 SRC_EA(env, src, opsize, 1, &addr);
1928 imm = (insn >> 9) & 7;
1932 val = tcg_const_i32(imm);
1933 dest = tcg_temp_new();
1934 tcg_gen_mov_i32(dest, src);
1935 if ((insn & 0x38) == 0x08) {
1936 /* Don't update condition codes if the destination is an
1937 address register. */
1938 if (insn & 0x0100) {
1939 tcg_gen_sub_i32(dest, dest, val);
1941 tcg_gen_add_i32(dest, dest, val);
1944 if (insn & 0x0100) {
1945 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
1946 tcg_gen_sub_i32(dest, dest, val);
1947 set_cc_op(s, CC_OP_SUBB + opsize);
1949 tcg_gen_add_i32(dest, dest, val);
1950 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
1951 set_cc_op(s, CC_OP_ADDB + opsize);
1953 gen_update_cc_add(dest, val, opsize);
1955 DEST_EA(env, insn, opsize, dest, &addr);
1961 case 2: /* One extension word. */
1964 case 3: /* Two extension words. */
1967 case 4: /* No extension words. */
1970 disas_undef(env, s, insn);
1982 op = (insn >> 8) & 0xf;
1983 offset = (int8_t)insn;
1985 offset = (int16_t)read_im16(env, s);
1986 } else if (offset == -1) {
1987 offset = read_im32(env, s);
1991 gen_push(s, tcg_const_i32(s->pc));
1995 l1 = gen_new_label();
1996 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1997 gen_jmp_tb(s, 1, base + offset);
1999 gen_jmp_tb(s, 0, s->pc);
2001 /* Unconditional branch. */
2002 gen_jmp_tb(s, 0, base + offset);
2011 tcg_gen_movi_i32(DREG(insn, 9), val);
2012 gen_logic_cc(s, tcg_const_i32(val), OS_LONG);
2025 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
2026 reg = DREG(insn, 9);
2027 tcg_gen_mov_i32(reg, src);
2028 gen_logic_cc(s, src, opsize);
2039 opsize = insn_opsize(insn);
2040 reg = gen_extend(DREG(insn, 9), opsize, 0);
2041 dest = tcg_temp_new();
2043 SRC_EA(env, src, opsize, 0, &addr);
2044 tcg_gen_or_i32(dest, src, reg);
2045 DEST_EA(env, insn, opsize, dest, &addr);
2047 SRC_EA(env, src, opsize, 0, NULL);
2048 tcg_gen_or_i32(dest, src, reg);
2049 gen_partset_reg(opsize, DREG(insn, 9), dest);
2051 gen_logic_cc(s, dest, opsize);
2059 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
2060 reg = AREG(insn, 9);
2061 tcg_gen_sub_i32(reg, reg, src);
2064 static inline void gen_subx(DisasContext *s, TCGv src, TCGv dest, int opsize)
2068 gen_flush_flags(s); /* compute old Z */
2070 /* Perform substract with borrow.
2071 * (X, N) = dest - (src + X);
2074 tmp = tcg_const_i32(0);
2075 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, tmp, QREG_CC_X, tmp);
2076 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, dest, tmp, QREG_CC_N, QREG_CC_X);
2077 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
2078 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
2080 /* Compute signed-overflow for substract. */
2082 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, dest);
2083 tcg_gen_xor_i32(tmp, dest, src);
2084 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, tmp);
2087 /* Copy the rest of the results into place. */
2088 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
2089 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
2091 set_cc_op(s, CC_OP_FLAGS);
2093 /* result is in QREG_CC_N */
2096 DISAS_INSN(subx_reg)
2102 opsize = insn_opsize(insn);
2104 src = gen_extend(DREG(insn, 0), opsize, 1);
2105 dest = gen_extend(DREG(insn, 9), opsize, 1);
2107 gen_subx(s, src, dest, opsize);
2109 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
2112 DISAS_INSN(subx_mem)
2120 opsize = insn_opsize(insn);
2122 addr_src = AREG(insn, 0);
2123 tcg_gen_subi_i32(addr_src, addr_src, opsize);
2124 src = gen_load(s, opsize, addr_src, 1);
2126 addr_dest = AREG(insn, 9);
2127 tcg_gen_subi_i32(addr_dest, addr_dest, opsize);
2128 dest = gen_load(s, opsize, addr_dest, 1);
2130 gen_subx(s, src, dest, opsize);
2132 gen_store(s, opsize, addr_dest, QREG_CC_N);
2140 val = (insn >> 9) & 7;
2143 src = tcg_const_i32(val);
2144 gen_logic_cc(s, src, OS_LONG);
2145 DEST_EA(env, insn, OS_LONG, src, NULL);
2154 opsize = insn_opsize(insn);
2155 SRC_EA(env, src, opsize, 1, NULL);
2156 reg = gen_extend(DREG(insn, 9), opsize, 1);
2157 gen_update_cc_cmp(s, reg, src, opsize);
2171 SRC_EA(env, src, opsize, 1, NULL);
2172 reg = AREG(insn, 9);
2173 gen_update_cc_cmp(s, reg, src, OS_LONG);
2183 opsize = insn_opsize(insn);
2185 SRC_EA(env, src, opsize, 0, &addr);
2186 dest = tcg_temp_new();
2187 tcg_gen_xor_i32(dest, src, DREG(insn, 9));
2188 gen_logic_cc(s, dest, opsize);
2189 DEST_EA(env, insn, opsize, dest, &addr);
2192 static void do_exg(TCGv reg1, TCGv reg2)
2194 TCGv temp = tcg_temp_new();
2195 tcg_gen_mov_i32(temp, reg1);
2196 tcg_gen_mov_i32(reg1, reg2);
2197 tcg_gen_mov_i32(reg2, temp);
2198 tcg_temp_free(temp);
2203 /* exchange Dx and Dy */
2204 do_exg(DREG(insn, 9), DREG(insn, 0));
2209 /* exchange Ax and Ay */
2210 do_exg(AREG(insn, 9), AREG(insn, 0));
2215 /* exchange Dx and Ay */
2216 do_exg(DREG(insn, 9), AREG(insn, 0));
2227 dest = tcg_temp_new();
2229 opsize = insn_opsize(insn);
2230 reg = DREG(insn, 9);
2232 SRC_EA(env, src, opsize, 0, &addr);
2233 tcg_gen_and_i32(dest, src, reg);
2234 DEST_EA(env, insn, opsize, dest, &addr);
2236 SRC_EA(env, src, opsize, 0, NULL);
2237 tcg_gen_and_i32(dest, src, reg);
2238 gen_partset_reg(opsize, reg, dest);
2240 tcg_temp_free(dest);
2241 gen_logic_cc(s, dest, opsize);
2249 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
2250 reg = AREG(insn, 9);
2251 tcg_gen_add_i32(reg, reg, src);
2254 static inline void gen_addx(DisasContext *s, TCGv src, TCGv dest, int opsize)
2258 gen_flush_flags(s); /* compute old Z */
2260 /* Perform addition with carry.
2261 * (X, N) = src + dest + X;
2264 tmp = tcg_const_i32(0);
2265 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_X, tmp, dest, tmp);
2266 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_N, QREG_CC_X, src, tmp);
2267 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
2269 /* Compute signed-overflow for addition. */
2271 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, src);
2272 tcg_gen_xor_i32(tmp, dest, src);
2273 tcg_gen_andc_i32(QREG_CC_V, QREG_CC_V, tmp);
2276 /* Copy the rest of the results into place. */
2277 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
2278 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
2280 set_cc_op(s, CC_OP_FLAGS);
2282 /* result is in QREG_CC_N */
2285 DISAS_INSN(addx_reg)
2291 opsize = insn_opsize(insn);
2293 dest = gen_extend(DREG(insn, 9), opsize, 1);
2294 src = gen_extend(DREG(insn, 0), opsize, 1);
2296 gen_addx(s, src, dest, opsize);
2298 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
2301 DISAS_INSN(addx_mem)
2309 opsize = insn_opsize(insn);
2311 addr_src = AREG(insn, 0);
2312 tcg_gen_subi_i32(addr_src, addr_src, opsize_bytes(opsize));
2313 src = gen_load(s, opsize, addr_src, 1);
2315 addr_dest = AREG(insn, 9);
2316 tcg_gen_subi_i32(addr_dest, addr_dest, opsize_bytes(opsize));
2317 dest = gen_load(s, opsize, addr_dest, 1);
2319 gen_addx(s, src, dest, opsize);
2321 gen_store(s, opsize, addr_dest, QREG_CC_N);
2324 /* TODO: This could be implemented without helper functions. */
2325 DISAS_INSN(shift_im)
2331 set_cc_op(s, CC_OP_FLAGS);
2333 reg = DREG(insn, 0);
2334 tmp = (insn >> 9) & 7;
2337 shift = tcg_const_i32(tmp);
2338 /* No need to flush flags becuse we know we will set C flag. */
2340 gen_helper_shl_cc(reg, cpu_env, reg, shift);
2343 gen_helper_shr_cc(reg, cpu_env, reg, shift);
2345 gen_helper_sar_cc(reg, cpu_env, reg, shift);
2350 DISAS_INSN(shift_reg)
2355 reg = DREG(insn, 0);
2356 shift = DREG(insn, 9);
2358 gen_helper_shl_cc(reg, cpu_env, reg, shift);
2361 gen_helper_shr_cc(reg, cpu_env, reg, shift);
2363 gen_helper_sar_cc(reg, cpu_env, reg, shift);
2366 set_cc_op(s, CC_OP_FLAGS);
2372 reg = DREG(insn, 0);
2373 gen_logic_cc(s, reg, OS_LONG);
2374 gen_helper_ff1(reg, reg);
2377 static TCGv gen_get_sr(DisasContext *s)
2382 ccr = gen_get_ccr(s);
2383 sr = tcg_temp_new();
2384 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
2385 tcg_gen_or_i32(sr, sr, ccr);
2395 ext = read_im16(env, s);
2396 if (ext != 0x46FC) {
2397 gen_exception(s, addr, EXCP_UNSUPPORTED);
2400 ext = read_im16(env, s);
2401 if (IS_USER(s) || (ext & SR_S) == 0) {
2402 gen_exception(s, addr, EXCP_PRIVILEGE);
2405 gen_push(s, gen_get_sr(s));
2406 gen_set_sr_im(s, ext, 0);
2409 DISAS_INSN(move_from_sr)
2413 if (IS_USER(s) && !m68k_feature(env, M68K_FEATURE_M68000)) {
2414 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2418 DEST_EA(env, insn, OS_WORD, sr, NULL);
2421 DISAS_INSN(move_to_sr)
2424 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2427 gen_set_sr(env, s, insn, 0);
2431 DISAS_INSN(move_from_usp)
2434 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2437 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
2438 offsetof(CPUM68KState, sp[M68K_USP]));
2441 DISAS_INSN(move_to_usp)
2444 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2447 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
2448 offsetof(CPUM68KState, sp[M68K_USP]));
2453 gen_exception(s, s->pc, EXCP_HALT_INSN);
2461 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2465 ext = read_im16(env, s);
2467 gen_set_sr_im(s, ext, 0);
2468 tcg_gen_movi_i32(cpu_halted, 1);
2469 gen_exception(s, s->pc, EXCP_HLT);
2475 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2478 gen_exception(s, s->pc - 2, EXCP_RTE);
2487 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2491 ext = read_im16(env, s);
2494 reg = AREG(ext, 12);
2496 reg = DREG(ext, 12);
2498 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2505 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2508 /* ICache fetch. Implement as no-op. */
2514 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2517 /* Cache push/invalidate. Implement as no-op. */
2522 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2527 M68kCPU *cpu = m68k_env_get_cpu(env);
2530 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2533 /* TODO: Implement wdebug. */
2534 cpu_abort(CPU(cpu), "WDEBUG not implemented");
2539 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2542 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2543 immediately before the next FP instruction is executed. */
2557 ext = read_im16(env, s);
2558 opmode = ext & 0x7f;
2559 switch ((ext >> 13) & 7) {
2564 case 3: /* fmove out */
2566 tmp32 = tcg_temp_new_i32();
2568 /* ??? TODO: Proper behavior on overflow. */
2569 switch ((ext >> 10) & 7) {
2572 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2576 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2580 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2582 case 5: /* OS_DOUBLE */
2583 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2584 switch ((insn >> 3) & 7) {
2589 tcg_gen_addi_i32(tmp32, tmp32, -8);
2592 offset = cpu_ldsw_code(env, s->pc);
2594 tcg_gen_addi_i32(tmp32, tmp32, offset);
2599 gen_store64(s, tmp32, src);
2600 switch ((insn >> 3) & 7) {
2602 tcg_gen_addi_i32(tmp32, tmp32, 8);
2603 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2606 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2609 tcg_temp_free_i32(tmp32);
2613 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2618 DEST_EA(env, insn, opsize, tmp32, NULL);
2619 tcg_temp_free_i32(tmp32);
2621 case 4: /* fmove to control register. */
2622 switch ((ext >> 10) & 7) {
2624 /* Not implemented. Ignore writes. */
2629 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2633 case 5: /* fmove from control register. */
2634 switch ((ext >> 10) & 7) {
2636 /* Not implemented. Always return zero. */
2637 tmp32 = tcg_const_i32(0);
2642 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2646 DEST_EA(env, insn, OS_LONG, tmp32, NULL);
2648 case 6: /* fmovem */
2654 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2656 tmp32 = gen_lea(env, s, insn, OS_LONG);
2657 if (IS_NULL_QREG(tmp32)) {
2661 addr = tcg_temp_new_i32();
2662 tcg_gen_mov_i32(addr, tmp32);
2664 for (i = 0; i < 8; i++) {
2667 if (ext & (1 << 13)) {
2669 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2672 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2674 if (ext & (mask - 1))
2675 tcg_gen_addi_i32(addr, addr, 8);
2679 tcg_temp_free_i32(addr);
2683 if (ext & (1 << 14)) {
2684 /* Source effective address. */
2685 switch ((ext >> 10) & 7) {
2686 case 0: opsize = OS_LONG; break;
2687 case 1: opsize = OS_SINGLE; break;
2688 case 4: opsize = OS_WORD; break;
2689 case 5: opsize = OS_DOUBLE; break;
2690 case 6: opsize = OS_BYTE; break;
2694 if (opsize == OS_DOUBLE) {
2695 tmp32 = tcg_temp_new_i32();
2696 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2697 switch ((insn >> 3) & 7) {
2702 tcg_gen_addi_i32(tmp32, tmp32, -8);
2705 offset = cpu_ldsw_code(env, s->pc);
2707 tcg_gen_addi_i32(tmp32, tmp32, offset);
2710 offset = cpu_ldsw_code(env, s->pc);
2711 offset += s->pc - 2;
2713 tcg_gen_addi_i32(tmp32, tmp32, offset);
2718 src = gen_load64(s, tmp32);
2719 switch ((insn >> 3) & 7) {
2721 tcg_gen_addi_i32(tmp32, tmp32, 8);
2722 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2725 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2728 tcg_temp_free_i32(tmp32);
2730 SRC_EA(env, tmp32, opsize, 1, NULL);
2731 src = tcg_temp_new_i64();
2736 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2739 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2744 /* Source register. */
2745 src = FREG(ext, 10);
2747 dest = FREG(ext, 7);
2748 res = tcg_temp_new_i64();
2750 tcg_gen_mov_f64(res, dest);
2754 case 0: case 0x40: case 0x44: /* fmove */
2755 tcg_gen_mov_f64(res, src);
2758 gen_helper_iround_f64(res, cpu_env, src);
2761 case 3: /* fintrz */
2762 gen_helper_itrunc_f64(res, cpu_env, src);
2765 case 4: case 0x41: case 0x45: /* fsqrt */
2766 gen_helper_sqrt_f64(res, cpu_env, src);
2768 case 0x18: case 0x58: case 0x5c: /* fabs */
2769 gen_helper_abs_f64(res, src);
2771 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2772 gen_helper_chs_f64(res, src);
2774 case 0x20: case 0x60: case 0x64: /* fdiv */
2775 gen_helper_div_f64(res, cpu_env, res, src);
2777 case 0x22: case 0x62: case 0x66: /* fadd */
2778 gen_helper_add_f64(res, cpu_env, res, src);
2780 case 0x23: case 0x63: case 0x67: /* fmul */
2781 gen_helper_mul_f64(res, cpu_env, res, src);
2783 case 0x28: case 0x68: case 0x6c: /* fsub */
2784 gen_helper_sub_f64(res, cpu_env, res, src);
2786 case 0x38: /* fcmp */
2787 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2791 case 0x3a: /* ftst */
2792 tcg_gen_mov_f64(res, src);
2799 if (ext & (1 << 14)) {
2800 tcg_temp_free_i64(src);
2803 if (opmode & 0x40) {
2804 if ((opmode & 0x4) != 0)
2806 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2811 TCGv tmp = tcg_temp_new_i32();
2812 gen_helper_f64_to_f32(tmp, cpu_env, res);
2813 gen_helper_f32_to_f64(res, cpu_env, tmp);
2814 tcg_temp_free_i32(tmp);
2816 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2818 tcg_gen_mov_f64(dest, res);
2820 tcg_temp_free_i64(res);
2823 /* FIXME: Is this right for offset addressing modes? */
2825 disas_undef_fpu(env, s, insn);
2836 offset = cpu_ldsw_code(env, s->pc);
2838 if (insn & (1 << 6)) {
2839 offset = (offset << 16) | read_im16(env, s);
2842 l1 = gen_new_label();
2843 /* TODO: Raise BSUN exception. */
2844 flag = tcg_temp_new();
2845 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2846 /* Jump to l1 if condition is true. */
2847 switch (insn & 0xf) {
2850 case 1: /* eq (=0) */
2851 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2853 case 2: /* ogt (=1) */
2854 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2856 case 3: /* oge (=0 or =1) */
2857 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2859 case 4: /* olt (=-1) */
2860 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2862 case 5: /* ole (=-1 or =0) */
2863 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2865 case 6: /* ogl (=-1 or =1) */
2866 tcg_gen_andi_i32(flag, flag, 1);
2867 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2869 case 7: /* or (=2) */
2870 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2872 case 8: /* un (<2) */
2873 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2875 case 9: /* ueq (=0 or =2) */
2876 tcg_gen_andi_i32(flag, flag, 1);
2877 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2879 case 10: /* ugt (>0) */
2880 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2882 case 11: /* uge (>=0) */
2883 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2885 case 12: /* ult (=-1 or =2) */
2886 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2888 case 13: /* ule (!=1) */
2889 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2891 case 14: /* ne (!=0) */
2892 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2898 gen_jmp_tb(s, 0, s->pc);
2900 gen_jmp_tb(s, 1, addr + offset);
2903 DISAS_INSN(frestore)
2905 M68kCPU *cpu = m68k_env_get_cpu(env);
2907 /* TODO: Implement frestore. */
2908 cpu_abort(CPU(cpu), "FRESTORE not implemented");
2913 M68kCPU *cpu = m68k_env_get_cpu(env);
2915 /* TODO: Implement fsave. */
2916 cpu_abort(CPU(cpu), "FSAVE not implemented");
2919 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2921 TCGv tmp = tcg_temp_new();
2922 if (s->env->macsr & MACSR_FI) {
2924 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2926 tcg_gen_shli_i32(tmp, val, 16);
2927 } else if (s->env->macsr & MACSR_SU) {
2929 tcg_gen_sari_i32(tmp, val, 16);
2931 tcg_gen_ext16s_i32(tmp, val);
2934 tcg_gen_shri_i32(tmp, val, 16);
2936 tcg_gen_ext16u_i32(tmp, val);
2941 static void gen_mac_clear_flags(void)
2943 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2944 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2960 s->mactmp = tcg_temp_new_i64();
2964 ext = read_im16(env, s);
2966 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2967 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2968 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2969 disas_undef(env, s, insn);
2973 /* MAC with load. */
2974 tmp = gen_lea(env, s, insn, OS_LONG);
2975 addr = tcg_temp_new();
2976 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2977 /* Load the value now to ensure correct exception behavior.
2978 Perform writeback after reading the MAC inputs. */
2979 loadval = gen_load(s, OS_LONG, addr, 0);
2982 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2983 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2985 loadval = addr = NULL_QREG;
2986 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2987 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2990 gen_mac_clear_flags();
2993 /* Disabled because conditional branches clobber temporary vars. */
2994 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2995 /* Skip the multiply if we know we will ignore it. */
2996 l1 = gen_new_label();
2997 tmp = tcg_temp_new();
2998 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2999 gen_op_jmp_nz32(tmp, l1);
3003 if ((ext & 0x0800) == 0) {
3005 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
3006 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
3008 if (s->env->macsr & MACSR_FI) {
3009 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
3011 if (s->env->macsr & MACSR_SU)
3012 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
3014 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
3015 switch ((ext >> 9) & 3) {
3017 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
3020 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
3026 /* Save the overflow flag from the multiply. */
3027 saved_flags = tcg_temp_new();
3028 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
3030 saved_flags = NULL_QREG;
3034 /* Disabled because conditional branches clobber temporary vars. */
3035 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
3036 /* Skip the accumulate if the value is already saturated. */
3037 l1 = gen_new_label();
3038 tmp = tcg_temp_new();
3039 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
3040 gen_op_jmp_nz32(tmp, l1);
3045 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
3047 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
3049 if (s->env->macsr & MACSR_FI)
3050 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
3051 else if (s->env->macsr & MACSR_SU)
3052 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
3054 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
3057 /* Disabled because conditional branches clobber temporary vars. */
3063 /* Dual accumulate variant. */
3064 acc = (ext >> 2) & 3;
3065 /* Restore the overflow flag from the multiplier. */
3066 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
3068 /* Disabled because conditional branches clobber temporary vars. */
3069 if ((s->env->macsr & MACSR_OMC) != 0) {
3070 /* Skip the accumulate if the value is already saturated. */
3071 l1 = gen_new_label();
3072 tmp = tcg_temp_new();
3073 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
3074 gen_op_jmp_nz32(tmp, l1);
3078 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
3080 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
3081 if (s->env->macsr & MACSR_FI)
3082 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
3083 else if (s->env->macsr & MACSR_SU)
3084 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
3086 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
3088 /* Disabled because conditional branches clobber temporary vars. */
3093 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
3097 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
3098 tcg_gen_mov_i32(rw, loadval);
3099 /* FIXME: Should address writeback happen with the masked or
3101 switch ((insn >> 3) & 7) {
3102 case 3: /* Post-increment. */
3103 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
3105 case 4: /* Pre-decrement. */
3106 tcg_gen_mov_i32(AREG(insn, 0), addr);
3111 DISAS_INSN(from_mac)
3117 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
3118 accnum = (insn >> 9) & 3;
3119 acc = MACREG(accnum);
3120 if (s->env->macsr & MACSR_FI) {
3121 gen_helper_get_macf(rx, cpu_env, acc);
3122 } else if ((s->env->macsr & MACSR_OMC) == 0) {
3123 tcg_gen_extrl_i64_i32(rx, acc);
3124 } else if (s->env->macsr & MACSR_SU) {
3125 gen_helper_get_macs(rx, acc);
3127 gen_helper_get_macu(rx, acc);
3130 tcg_gen_movi_i64(acc, 0);
3131 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
3135 DISAS_INSN(move_mac)
3137 /* FIXME: This can be done without a helper. */
3141 dest = tcg_const_i32((insn >> 9) & 3);
3142 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
3143 gen_mac_clear_flags();
3144 gen_helper_mac_set_flags(cpu_env, dest);
3147 DISAS_INSN(from_macsr)
3151 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
3152 tcg_gen_mov_i32(reg, QREG_MACSR);
3155 DISAS_INSN(from_mask)
3158 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
3159 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
3162 DISAS_INSN(from_mext)
3166 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
3167 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
3168 if (s->env->macsr & MACSR_FI)
3169 gen_helper_get_mac_extf(reg, cpu_env, acc);
3171 gen_helper_get_mac_exti(reg, cpu_env, acc);
3174 DISAS_INSN(macsr_to_ccr)
3176 TCGv tmp = tcg_temp_new();
3177 tcg_gen_andi_i32(tmp, QREG_MACSR, 0xf);
3178 gen_helper_set_sr(cpu_env, tmp);
3180 set_cc_op(s, CC_OP_FLAGS);
3188 accnum = (insn >> 9) & 3;
3189 acc = MACREG(accnum);
3190 SRC_EA(env, val, OS_LONG, 0, NULL);
3191 if (s->env->macsr & MACSR_FI) {
3192 tcg_gen_ext_i32_i64(acc, val);
3193 tcg_gen_shli_i64(acc, acc, 8);
3194 } else if (s->env->macsr & MACSR_SU) {
3195 tcg_gen_ext_i32_i64(acc, val);
3197 tcg_gen_extu_i32_i64(acc, val);
3199 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
3200 gen_mac_clear_flags();
3201 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
3204 DISAS_INSN(to_macsr)
3207 SRC_EA(env, val, OS_LONG, 0, NULL);
3208 gen_helper_set_macsr(cpu_env, val);
3215 SRC_EA(env, val, OS_LONG, 0, NULL);
3216 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
3223 SRC_EA(env, val, OS_LONG, 0, NULL);
3224 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
3225 if (s->env->macsr & MACSR_FI)
3226 gen_helper_set_mac_extf(cpu_env, val, acc);
3227 else if (s->env->macsr & MACSR_SU)
3228 gen_helper_set_mac_exts(cpu_env, val, acc);
3230 gen_helper_set_mac_extu(cpu_env, val, acc);
3233 static disas_proc opcode_table[65536];
3236 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
3242 /* Sanity check. All set bits must be included in the mask. */
3243 if (opcode & ~mask) {
3245 "qemu internal error: bogus opcode definition %04x/%04x\n",
3249 /* This could probably be cleverer. For now just optimize the case where
3250 the top bits are known. */
3251 /* Find the first zero bit in the mask. */
3253 while ((i & mask) != 0)
3255 /* Iterate over all combinations of this and lower bits. */
3260 from = opcode & ~(i - 1);
3262 for (i = from; i < to; i++) {
3263 if ((i & mask) == opcode)
3264 opcode_table[i] = proc;
3268 /* Register m68k opcode handlers. Order is important.
3269 Later insn override earlier ones. */
3270 void register_m68k_insns (CPUM68KState *env)
3272 /* Build the opcode table only once to avoid
3273 multithreading issues. */
3274 if (opcode_table[0] != NULL) {
3278 /* use BASE() for instruction available
3279 * for CF_ISA_A and M68000.
3281 #define BASE(name, opcode, mask) \
3282 register_opcode(disas_##name, 0x##opcode, 0x##mask)
3283 #define INSN(name, opcode, mask, feature) do { \
3284 if (m68k_feature(env, M68K_FEATURE_##feature)) \
3285 BASE(name, opcode, mask); \
3287 BASE(undef, 0000, 0000);
3288 INSN(arith_im, 0080, fff8, CF_ISA_A);
3289 INSN(arith_im, 0000, ff00, M68000);
3290 INSN(undef, 00c0, ffc0, M68000);
3291 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
3292 BASE(bitop_reg, 0100, f1c0);
3293 BASE(bitop_reg, 0140, f1c0);
3294 BASE(bitop_reg, 0180, f1c0);
3295 BASE(bitop_reg, 01c0, f1c0);
3296 INSN(arith_im, 0280, fff8, CF_ISA_A);
3297 INSN(arith_im, 0200, ff00, M68000);
3298 INSN(undef, 02c0, ffc0, M68000);
3299 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
3300 INSN(arith_im, 0480, fff8, CF_ISA_A);
3301 INSN(arith_im, 0400, ff00, M68000);
3302 INSN(undef, 04c0, ffc0, M68000);
3303 INSN(arith_im, 0600, ff00, M68000);
3304 INSN(undef, 06c0, ffc0, M68000);
3305 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
3306 INSN(arith_im, 0680, fff8, CF_ISA_A);
3307 INSN(arith_im, 0c00, ff38, CF_ISA_A);
3308 INSN(arith_im, 0c00, ff00, M68000);
3309 BASE(bitop_im, 0800, ffc0);
3310 BASE(bitop_im, 0840, ffc0);
3311 BASE(bitop_im, 0880, ffc0);
3312 BASE(bitop_im, 08c0, ffc0);
3313 INSN(arith_im, 0a80, fff8, CF_ISA_A);
3314 INSN(arith_im, 0a00, ff00, M68000);
3315 BASE(move, 1000, f000);
3316 BASE(move, 2000, f000);
3317 BASE(move, 3000, f000);
3318 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
3319 INSN(negx, 4080, fff8, CF_ISA_A);
3320 INSN(negx, 4000, ff00, M68000);
3321 INSN(undef, 40c0, ffc0, M68000);
3322 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
3323 INSN(move_from_sr, 40c0, ffc0, M68000);
3324 BASE(lea, 41c0, f1c0);
3325 BASE(clr, 4200, ff00);
3326 BASE(undef, 42c0, ffc0);
3327 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
3328 INSN(move_from_ccr, 42c0, ffc0, M68000);
3329 INSN(neg, 4480, fff8, CF_ISA_A);
3330 INSN(neg, 4400, ff00, M68000);
3331 INSN(undef, 44c0, ffc0, M68000);
3332 BASE(move_to_ccr, 44c0, ffc0);
3333 INSN(not, 4680, fff8, CF_ISA_A);
3334 INSN(not, 4600, ff00, M68000);
3335 INSN(undef, 46c0, ffc0, M68000);
3336 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
3337 INSN(linkl, 4808, fff8, M68000);
3338 BASE(pea, 4840, ffc0);
3339 BASE(swap, 4840, fff8);
3340 INSN(bkpt, 4848, fff8, BKPT);
3341 BASE(movem, 48c0, fbc0);
3342 BASE(ext, 4880, fff8);
3343 BASE(ext, 48c0, fff8);
3344 BASE(ext, 49c0, fff8);
3345 BASE(tst, 4a00, ff00);
3346 INSN(tas, 4ac0, ffc0, CF_ISA_B);
3347 INSN(tas, 4ac0, ffc0, M68000);
3348 INSN(halt, 4ac8, ffff, CF_ISA_A);
3349 INSN(pulse, 4acc, ffff, CF_ISA_A);
3350 BASE(illegal, 4afc, ffff);
3351 INSN(mull, 4c00, ffc0, CF_ISA_A);
3352 INSN(mull, 4c00, ffc0, LONG_MULDIV);
3353 INSN(divl, 4c40, ffc0, CF_ISA_A);
3354 INSN(divl, 4c40, ffc0, LONG_MULDIV);
3355 INSN(sats, 4c80, fff8, CF_ISA_B);
3356 BASE(trap, 4e40, fff0);
3357 BASE(link, 4e50, fff8);
3358 BASE(unlk, 4e58, fff8);
3359 INSN(move_to_usp, 4e60, fff8, USP);
3360 INSN(move_from_usp, 4e68, fff8, USP);
3361 BASE(nop, 4e71, ffff);
3362 BASE(stop, 4e72, ffff);
3363 BASE(rte, 4e73, ffff);
3364 BASE(rts, 4e75, ffff);
3365 INSN(movec, 4e7b, ffff, CF_ISA_A);
3366 BASE(jump, 4e80, ffc0);
3367 BASE(jump, 4ec0, ffc0);
3368 INSN(addsubq, 5000, f080, M68000);
3369 BASE(addsubq, 5080, f0c0);
3370 INSN(scc, 50c0, f0f8, CF_ISA_A); /* Scc.B Dx */
3371 INSN(scc, 50c0, f0c0, M68000); /* Scc.B <EA> */
3372 INSN(dbcc, 50c8, f0f8, M68000);
3373 INSN(tpf, 51f8, fff8, CF_ISA_A);
3375 /* Branch instructions. */
3376 BASE(branch, 6000, f000);
3377 /* Disable long branch instructions, then add back the ones we want. */
3378 BASE(undef, 60ff, f0ff); /* All long branches. */
3379 INSN(branch, 60ff, f0ff, CF_ISA_B);
3380 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
3381 INSN(branch, 60ff, ffff, BRAL);
3382 INSN(branch, 60ff, f0ff, BCCL);
3384 BASE(moveq, 7000, f100);
3385 INSN(mvzs, 7100, f100, CF_ISA_B);
3386 BASE(or, 8000, f000);
3387 BASE(divw, 80c0, f0c0);
3388 BASE(addsub, 9000, f000);
3389 INSN(undef, 90c0, f0c0, CF_ISA_A);
3390 INSN(subx_reg, 9180, f1f8, CF_ISA_A);
3391 INSN(subx_reg, 9100, f138, M68000);
3392 INSN(subx_mem, 9108, f138, M68000);
3393 INSN(suba, 91c0, f1c0, CF_ISA_A);
3394 INSN(suba, 90c0, f0c0, M68000);
3396 BASE(undef_mac, a000, f000);
3397 INSN(mac, a000, f100, CF_EMAC);
3398 INSN(from_mac, a180, f9b0, CF_EMAC);
3399 INSN(move_mac, a110, f9fc, CF_EMAC);
3400 INSN(from_macsr,a980, f9f0, CF_EMAC);
3401 INSN(from_mask, ad80, fff0, CF_EMAC);
3402 INSN(from_mext, ab80, fbf0, CF_EMAC);
3403 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
3404 INSN(to_mac, a100, f9c0, CF_EMAC);
3405 INSN(to_macsr, a900, ffc0, CF_EMAC);
3406 INSN(to_mext, ab00, fbc0, CF_EMAC);
3407 INSN(to_mask, ad00, ffc0, CF_EMAC);
3409 INSN(mov3q, a140, f1c0, CF_ISA_B);
3410 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
3411 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
3412 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
3413 INSN(cmp, b080, f1c0, CF_ISA_A);
3414 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
3415 INSN(cmp, b000, f100, M68000);
3416 INSN(eor, b100, f100, M68000);
3417 INSN(cmpa, b0c0, f0c0, M68000);
3418 INSN(eor, b180, f1c0, CF_ISA_A);
3419 BASE(and, c000, f000);
3420 INSN(exg_dd, c140, f1f8, M68000);
3421 INSN(exg_aa, c148, f1f8, M68000);
3422 INSN(exg_da, c188, f1f8, M68000);
3423 BASE(mulw, c0c0, f0c0);
3424 BASE(addsub, d000, f000);
3425 INSN(undef, d0c0, f0c0, CF_ISA_A);
3426 INSN(addx_reg, d180, f1f8, CF_ISA_A);
3427 INSN(addx_reg, d100, f138, M68000);
3428 INSN(addx_mem, d108, f138, M68000);
3429 INSN(adda, d1c0, f1c0, CF_ISA_A);
3430 INSN(adda, d0c0, f0c0, M68000);
3431 INSN(shift_im, e080, f0f0, CF_ISA_A);
3432 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
3433 INSN(undef_fpu, f000, f000, CF_ISA_A);
3434 INSN(fpu, f200, ffc0, CF_FPU);
3435 INSN(fbcc, f280, ffc0, CF_FPU);
3436 INSN(frestore, f340, ffc0, CF_FPU);
3437 INSN(fsave, f340, ffc0, CF_FPU);
3438 INSN(intouch, f340, ffc0, CF_ISA_A);
3439 INSN(cpushl, f428, ff38, CF_ISA_A);
3440 INSN(wddata, fb00, ff00, CF_ISA_A);
3441 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
3445 /* ??? Some of this implementation is not exception safe. We should always
3446 write back the result to memory before setting the condition codes. */
3447 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
3451 insn = read_im16(env, s);
3453 opcode_table[insn](env, s, insn);
3456 /* generate intermediate code for basic block 'tb'. */
3457 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
3459 M68kCPU *cpu = m68k_env_get_cpu(env);
3460 CPUState *cs = CPU(cpu);
3461 DisasContext dc1, *dc = &dc1;
3462 target_ulong pc_start;
3467 /* generate intermediate code */
3473 dc->is_jmp = DISAS_NEXT;
3475 dc->cc_op = CC_OP_DYNAMIC;
3476 dc->cc_op_synced = 1;
3477 dc->singlestep_enabled = cs->singlestep_enabled;
3478 dc->fpcr = env->fpcr;
3479 dc->user = (env->sr & SR_S) == 0;
3482 max_insns = tb->cflags & CF_COUNT_MASK;
3483 if (max_insns == 0) {
3484 max_insns = CF_COUNT_MASK;
3486 if (max_insns > TCG_MAX_INSNS) {
3487 max_insns = TCG_MAX_INSNS;
3492 pc_offset = dc->pc - pc_start;
3493 gen_throws_exception = NULL;
3494 tcg_gen_insn_start(dc->pc, dc->cc_op);
3497 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
3498 gen_exception(dc, dc->pc, EXCP_DEBUG);
3499 dc->is_jmp = DISAS_JUMP;
3500 /* The address covered by the breakpoint must be included in
3501 [tb->pc, tb->pc + tb->size) in order to for it to be
3502 properly cleared -- thus we increment the PC here so that
3503 the logic setting tb->size below does the right thing. */
3508 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
3512 dc->insn_pc = dc->pc;
3513 disas_m68k_insn(env, dc);
3514 } while (!dc->is_jmp && !tcg_op_buf_full() &&
3515 !cs->singlestep_enabled &&
3517 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3518 num_insns < max_insns);
3520 if (tb->cflags & CF_LAST_IO)
3522 if (unlikely(cs->singlestep_enabled)) {
3523 /* Make sure the pc is updated, and raise a debug exception. */
3526 tcg_gen_movi_i32(QREG_PC, dc->pc);
3528 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
3530 switch(dc->is_jmp) {
3533 gen_jmp_tb(dc, 0, dc->pc);
3539 /* indicate that the hash table must be used to find the next TB */
3543 /* nothing more to generate */
3547 gen_tb_end(tb, num_insns);
3550 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
3551 && qemu_log_in_addr_range(pc_start)) {
3553 qemu_log("----------------\n");
3554 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3555 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
3560 tb->size = dc->pc - pc_start;
3561 tb->icount = num_insns;
3564 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
3567 M68kCPU *cpu = M68K_CPU(cs);
3568 CPUM68KState *env = &cpu->env;
3572 for (i = 0; i < 8; i++)
3574 u.d = env->fregs[i];
3575 cpu_fprintf(f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3576 i, env->dregs[i], i, env->aregs[i],
3577 i, u.l.upper, u.l.lower, *(double *)&u.d);
3579 cpu_fprintf (f, "PC = %08x ", env->pc);
3580 sr = env->sr | cpu_m68k_get_ccr(env);
3581 cpu_fprintf(f, "SR = %04x %c%c%c%c%c ", sr, (sr & CCF_X) ? 'X' : '-',
3582 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3583 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3584 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3587 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,
3590 int cc_op = data[1];
3592 if (cc_op != CC_OP_DYNAMIC) {