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"
28 #include "exec/translator.h"
30 #include "exec/helper-proto.h"
31 #include "exec/helper-gen.h"
33 #include "trace-tcg.h"
35 #include "fpu/softfloat.h"
38 //#define DEBUG_DISPATCH 1
40 #define DEFO32(name, offset) static TCGv QREG_##name;
41 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
46 static TCGv_i32 cpu_halted;
47 static TCGv_i32 cpu_exception_index;
49 static char cpu_reg_names[2 * 8 * 3 + 5 * 4];
50 static TCGv cpu_dregs[8];
51 static TCGv cpu_aregs[8];
52 static TCGv_i64 cpu_macc[4];
54 #define REG(insn, pos) (((insn) >> (pos)) & 7)
55 #define DREG(insn, pos) cpu_dregs[REG(insn, pos)]
56 #define AREG(insn, pos) get_areg(s, REG(insn, pos))
57 #define MACREG(acc) cpu_macc[acc]
58 #define QREG_SP get_areg(s, 7)
60 static TCGv NULL_QREG;
61 #define IS_NULL_QREG(t) (t == NULL_QREG)
62 /* Used to distinguish stores from bad addressing modes. */
63 static TCGv store_dummy;
65 #include "exec/gen-icount.h"
67 void m68k_tcg_init(void)
72 #define DEFO32(name, offset) \
73 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
74 offsetof(CPUM68KState, offset), #name);
75 #define DEFO64(name, offset) \
76 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
77 offsetof(CPUM68KState, offset), #name);
82 cpu_halted = tcg_global_mem_new_i32(cpu_env,
83 -offsetof(M68kCPU, env) +
84 offsetof(CPUState, halted), "HALTED");
85 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
86 -offsetof(M68kCPU, env) +
87 offsetof(CPUState, exception_index),
91 for (i = 0; i < 8; i++) {
93 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
94 offsetof(CPUM68KState, dregs[i]), p);
97 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
98 offsetof(CPUM68KState, aregs[i]), p);
101 for (i = 0; i < 4; i++) {
102 sprintf(p, "ACC%d", i);
103 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
104 offsetof(CPUM68KState, macc[i]), p);
108 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
109 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
112 /* internal defines */
113 typedef struct DisasContext {
115 target_ulong insn_pc; /* Start of the current instruction. */
118 CCOp cc_op; /* Current CC operation */
120 struct TranslationBlock *tb;
121 int singlestep_enabled;
126 #define MAX_TO_RELEASE 8
128 TCGv release[MAX_TO_RELEASE];
131 static void init_release_array(DisasContext *s)
133 #ifdef CONFIG_DEBUG_TCG
134 memset(s->release, 0, sizeof(s->release));
136 s->release_count = 0;
139 static void do_release(DisasContext *s)
142 for (i = 0; i < s->release_count; i++) {
143 tcg_temp_free(s->release[i]);
145 init_release_array(s);
148 static TCGv mark_to_release(DisasContext *s, TCGv tmp)
150 g_assert(s->release_count < MAX_TO_RELEASE);
151 return s->release[s->release_count++] = tmp;
154 static TCGv get_areg(DisasContext *s, unsigned regno)
156 if (s->writeback_mask & (1 << regno)) {
157 return s->writeback[regno];
159 return cpu_aregs[regno];
163 static void delay_set_areg(DisasContext *s, unsigned regno,
164 TCGv val, bool give_temp)
166 if (s->writeback_mask & (1 << regno)) {
168 tcg_temp_free(s->writeback[regno]);
169 s->writeback[regno] = val;
171 tcg_gen_mov_i32(s->writeback[regno], val);
174 s->writeback_mask |= 1 << regno;
176 s->writeback[regno] = val;
178 TCGv tmp = tcg_temp_new();
179 s->writeback[regno] = tmp;
180 tcg_gen_mov_i32(tmp, val);
185 static void do_writebacks(DisasContext *s)
187 unsigned mask = s->writeback_mask;
189 s->writeback_mask = 0;
191 unsigned regno = ctz32(mask);
192 tcg_gen_mov_i32(cpu_aregs[regno], s->writeback[regno]);
193 tcg_temp_free(s->writeback[regno]);
199 /* is_jmp field values */
200 #define DISAS_JUMP DISAS_TARGET_0 /* only pc was modified dynamically */
201 #define DISAS_UPDATE DISAS_TARGET_1 /* cpu state was modified dynamically */
202 #define DISAS_TB_JUMP DISAS_TARGET_2 /* only pc was modified statically */
203 #define DISAS_JUMP_NEXT DISAS_TARGET_3
205 #if defined(CONFIG_USER_ONLY)
208 #define IS_USER(s) (!(s->tb->flags & TB_FLAGS_MSR_S))
209 #define SFC_INDEX(s) ((s->tb->flags & TB_FLAGS_SFC_S) ? \
210 MMU_KERNEL_IDX : MMU_USER_IDX)
211 #define DFC_INDEX(s) ((s->tb->flags & TB_FLAGS_DFC_S) ? \
212 MMU_KERNEL_IDX : MMU_USER_IDX)
215 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
217 #ifdef DEBUG_DISPATCH
218 #define DISAS_INSN(name) \
219 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
221 static void disas_##name(CPUM68KState *env, DisasContext *s, \
224 qemu_log("Dispatch " #name "\n"); \
225 real_disas_##name(env, s, insn); \
227 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
230 #define DISAS_INSN(name) \
231 static void disas_##name(CPUM68KState *env, DisasContext *s, \
235 static const uint8_t cc_op_live[CC_OP_NB] = {
236 [CC_OP_DYNAMIC] = CCF_C | CCF_V | CCF_Z | CCF_N | CCF_X,
237 [CC_OP_FLAGS] = CCF_C | CCF_V | CCF_Z | CCF_N | CCF_X,
238 [CC_OP_ADDB ... CC_OP_ADDL] = CCF_X | CCF_N | CCF_V,
239 [CC_OP_SUBB ... CC_OP_SUBL] = CCF_X | CCF_N | CCF_V,
240 [CC_OP_CMPB ... CC_OP_CMPL] = CCF_X | CCF_N | CCF_V,
241 [CC_OP_LOGIC] = CCF_X | CCF_N
244 static void set_cc_op(DisasContext *s, CCOp op)
246 CCOp old_op = s->cc_op;
255 /* Discard CC computation that will no longer be used.
256 Note that X and N are never dead. */
257 dead = cc_op_live[old_op] & ~cc_op_live[op];
259 tcg_gen_discard_i32(QREG_CC_C);
262 tcg_gen_discard_i32(QREG_CC_Z);
265 tcg_gen_discard_i32(QREG_CC_V);
269 /* Update the CPU env CC_OP state. */
270 static void update_cc_op(DisasContext *s)
272 if (!s->cc_op_synced) {
274 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
278 /* Generate a jump to an immediate address. */
279 static void gen_jmp_im(DisasContext *s, uint32_t dest)
282 tcg_gen_movi_i32(QREG_PC, dest);
283 s->is_jmp = DISAS_JUMP;
286 /* Generate a jump to the address in qreg DEST. */
287 static void gen_jmp(DisasContext *s, TCGv dest)
290 tcg_gen_mov_i32(QREG_PC, dest);
291 s->is_jmp = DISAS_JUMP;
294 static void gen_raise_exception(int nr)
296 TCGv_i32 tmp = tcg_const_i32(nr);
298 gen_helper_raise_exception(cpu_env, tmp);
299 tcg_temp_free_i32(tmp);
302 static void gen_exception(DisasContext *s, uint32_t where, int nr)
304 gen_jmp_im(s, where);
305 gen_raise_exception(nr);
308 static inline void gen_addr_fault(DisasContext *s)
310 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
313 /* Generate a load from the specified address. Narrow values are
314 sign extended to full register width. */
315 static inline TCGv gen_load(DisasContext *s, int opsize, TCGv addr,
319 tmp = tcg_temp_new_i32();
323 tcg_gen_qemu_ld8s(tmp, addr, index);
325 tcg_gen_qemu_ld8u(tmp, addr, index);
329 tcg_gen_qemu_ld16s(tmp, addr, index);
331 tcg_gen_qemu_ld16u(tmp, addr, index);
334 tcg_gen_qemu_ld32u(tmp, addr, index);
337 g_assert_not_reached();
342 /* Generate a store. */
343 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val,
348 tcg_gen_qemu_st8(val, addr, index);
351 tcg_gen_qemu_st16(val, addr, index);
354 tcg_gen_qemu_st32(val, addr, index);
357 g_assert_not_reached();
367 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
368 otherwise generate a store. */
369 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
370 ea_what what, int index)
372 if (what == EA_STORE) {
373 gen_store(s, opsize, addr, val, index);
376 return mark_to_release(s, gen_load(s, opsize, addr,
377 what == EA_LOADS, index));
381 /* Read a 16-bit immediate constant */
382 static inline uint16_t read_im16(CPUM68KState *env, DisasContext *s)
385 im = cpu_lduw_code(env, s->pc);
390 /* Read an 8-bit immediate constant */
391 static inline uint8_t read_im8(CPUM68KState *env, DisasContext *s)
393 return read_im16(env, s);
396 /* Read a 32-bit immediate constant. */
397 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
400 im = read_im16(env, s) << 16;
401 im |= 0xffff & read_im16(env, s);
405 /* Read a 64-bit immediate constant. */
406 static inline uint64_t read_im64(CPUM68KState *env, DisasContext *s)
409 im = (uint64_t)read_im32(env, s) << 32;
410 im |= (uint64_t)read_im32(env, s);
414 /* Calculate and address index. */
415 static TCGv gen_addr_index(DisasContext *s, uint16_t ext, TCGv tmp)
420 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
421 if ((ext & 0x800) == 0) {
422 tcg_gen_ext16s_i32(tmp, add);
425 scale = (ext >> 9) & 3;
427 tcg_gen_shli_i32(tmp, add, scale);
433 /* Handle a base + index + displacement effective addresss.
434 A NULL_QREG base means pc-relative. */
435 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
444 ext = read_im16(env, s);
446 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
449 if (m68k_feature(s->env, M68K_FEATURE_M68000) &&
450 !m68k_feature(s->env, M68K_FEATURE_SCALED_INDEX)) {
455 /* full extension word format */
456 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
459 if ((ext & 0x30) > 0x10) {
460 /* base displacement */
461 if ((ext & 0x30) == 0x20) {
462 bd = (int16_t)read_im16(env, s);
464 bd = read_im32(env, s);
469 tmp = mark_to_release(s, tcg_temp_new());
470 if ((ext & 0x44) == 0) {
472 add = gen_addr_index(s, ext, tmp);
476 if ((ext & 0x80) == 0) {
477 /* base not suppressed */
478 if (IS_NULL_QREG(base)) {
479 base = mark_to_release(s, tcg_const_i32(offset + bd));
482 if (!IS_NULL_QREG(add)) {
483 tcg_gen_add_i32(tmp, add, base);
489 if (!IS_NULL_QREG(add)) {
491 tcg_gen_addi_i32(tmp, add, bd);
495 add = mark_to_release(s, tcg_const_i32(bd));
497 if ((ext & 3) != 0) {
498 /* memory indirect */
499 base = mark_to_release(s, gen_load(s, OS_LONG, add, 0, IS_USER(s)));
500 if ((ext & 0x44) == 4) {
501 add = gen_addr_index(s, ext, tmp);
502 tcg_gen_add_i32(tmp, add, base);
508 /* outer displacement */
509 if ((ext & 3) == 2) {
510 od = (int16_t)read_im16(env, s);
512 od = read_im32(env, s);
518 tcg_gen_addi_i32(tmp, add, od);
523 /* brief extension word format */
524 tmp = mark_to_release(s, tcg_temp_new());
525 add = gen_addr_index(s, ext, tmp);
526 if (!IS_NULL_QREG(base)) {
527 tcg_gen_add_i32(tmp, add, base);
529 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
531 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
538 /* Sign or zero extend a value. */
540 static inline void gen_ext(TCGv res, TCGv val, int opsize, int sign)
545 tcg_gen_ext8s_i32(res, val);
547 tcg_gen_ext8u_i32(res, val);
552 tcg_gen_ext16s_i32(res, val);
554 tcg_gen_ext16u_i32(res, val);
558 tcg_gen_mov_i32(res, val);
561 g_assert_not_reached();
565 /* Evaluate all the CC flags. */
567 static void gen_flush_flags(DisasContext *s)
578 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
579 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
580 /* Compute signed overflow for addition. */
583 tcg_gen_sub_i32(t0, QREG_CC_N, QREG_CC_V);
584 gen_ext(t0, t0, s->cc_op - CC_OP_ADDB, 1);
585 tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
586 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
588 tcg_gen_andc_i32(QREG_CC_V, t1, QREG_CC_V);
595 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
596 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
597 /* Compute signed overflow for subtraction. */
600 tcg_gen_add_i32(t0, QREG_CC_N, QREG_CC_V);
601 gen_ext(t0, t0, s->cc_op - CC_OP_SUBB, 1);
602 tcg_gen_xor_i32(t1, QREG_CC_N, t0);
603 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
605 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t1);
612 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_C, QREG_CC_N, QREG_CC_V);
613 tcg_gen_sub_i32(QREG_CC_Z, QREG_CC_N, QREG_CC_V);
614 gen_ext(QREG_CC_Z, QREG_CC_Z, s->cc_op - CC_OP_CMPB, 1);
615 /* Compute signed overflow for subtraction. */
617 tcg_gen_xor_i32(t0, QREG_CC_Z, QREG_CC_N);
618 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, QREG_CC_N);
619 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t0);
621 tcg_gen_mov_i32(QREG_CC_N, QREG_CC_Z);
625 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
626 tcg_gen_movi_i32(QREG_CC_C, 0);
627 tcg_gen_movi_i32(QREG_CC_V, 0);
631 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
636 t0 = tcg_const_i32(s->cc_op);
637 gen_helper_flush_flags(cpu_env, t0);
643 /* Note that flush_flags also assigned to env->cc_op. */
644 s->cc_op = CC_OP_FLAGS;
647 static inline TCGv gen_extend(DisasContext *s, TCGv val, int opsize, int sign)
651 if (opsize == OS_LONG) {
654 tmp = mark_to_release(s, tcg_temp_new());
655 gen_ext(tmp, val, opsize, sign);
661 static void gen_logic_cc(DisasContext *s, TCGv val, int opsize)
663 gen_ext(QREG_CC_N, val, opsize, 1);
664 set_cc_op(s, CC_OP_LOGIC);
667 static void gen_update_cc_cmp(DisasContext *s, TCGv dest, TCGv src, int opsize)
669 tcg_gen_mov_i32(QREG_CC_N, dest);
670 tcg_gen_mov_i32(QREG_CC_V, src);
671 set_cc_op(s, CC_OP_CMPB + opsize);
674 static void gen_update_cc_add(TCGv dest, TCGv src, int opsize)
676 gen_ext(QREG_CC_N, dest, opsize, 1);
677 tcg_gen_mov_i32(QREG_CC_V, src);
680 static inline int opsize_bytes(int opsize)
683 case OS_BYTE: return 1;
684 case OS_WORD: return 2;
685 case OS_LONG: return 4;
686 case OS_SINGLE: return 4;
687 case OS_DOUBLE: return 8;
688 case OS_EXTENDED: return 12;
689 case OS_PACKED: return 12;
691 g_assert_not_reached();
695 static inline int insn_opsize(int insn)
697 switch ((insn >> 6) & 3) {
698 case 0: return OS_BYTE;
699 case 1: return OS_WORD;
700 case 2: return OS_LONG;
702 g_assert_not_reached();
706 static inline int ext_opsize(int ext, int pos)
708 switch ((ext >> pos) & 7) {
709 case 0: return OS_LONG;
710 case 1: return OS_SINGLE;
711 case 2: return OS_EXTENDED;
712 case 3: return OS_PACKED;
713 case 4: return OS_WORD;
714 case 5: return OS_DOUBLE;
715 case 6: return OS_BYTE;
717 g_assert_not_reached();
721 /* Assign value to a register. If the width is less than the register width
722 only the low part of the register is set. */
723 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
728 tcg_gen_andi_i32(reg, reg, 0xffffff00);
729 tmp = tcg_temp_new();
730 tcg_gen_ext8u_i32(tmp, val);
731 tcg_gen_or_i32(reg, reg, tmp);
735 tcg_gen_andi_i32(reg, reg, 0xffff0000);
736 tmp = tcg_temp_new();
737 tcg_gen_ext16u_i32(tmp, val);
738 tcg_gen_or_i32(reg, reg, tmp);
743 tcg_gen_mov_i32(reg, val);
746 g_assert_not_reached();
750 /* Generate code for an "effective address". Does not adjust the base
751 register for autoincrement addressing modes. */
752 static TCGv gen_lea_mode(CPUM68KState *env, DisasContext *s,
753 int mode, int reg0, int opsize)
761 case 0: /* Data register direct. */
762 case 1: /* Address register direct. */
764 case 3: /* Indirect postincrement. */
765 if (opsize == OS_UNSIZED) {
769 case 2: /* Indirect register */
770 return get_areg(s, reg0);
771 case 4: /* Indirect predecrememnt. */
772 if (opsize == OS_UNSIZED) {
775 reg = get_areg(s, reg0);
776 tmp = mark_to_release(s, tcg_temp_new());
777 if (reg0 == 7 && opsize == OS_BYTE &&
778 m68k_feature(s->env, M68K_FEATURE_M68000)) {
779 tcg_gen_subi_i32(tmp, reg, 2);
781 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
784 case 5: /* Indirect displacement. */
785 reg = get_areg(s, reg0);
786 tmp = mark_to_release(s, tcg_temp_new());
787 ext = read_im16(env, s);
788 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
790 case 6: /* Indirect index + displacement. */
791 reg = get_areg(s, reg0);
792 return gen_lea_indexed(env, s, reg);
795 case 0: /* Absolute short. */
796 offset = (int16_t)read_im16(env, s);
797 return mark_to_release(s, tcg_const_i32(offset));
798 case 1: /* Absolute long. */
799 offset = read_im32(env, s);
800 return mark_to_release(s, tcg_const_i32(offset));
801 case 2: /* pc displacement */
803 offset += (int16_t)read_im16(env, s);
804 return mark_to_release(s, tcg_const_i32(offset));
805 case 3: /* pc index+displacement. */
806 return gen_lea_indexed(env, s, NULL_QREG);
807 case 4: /* Immediate. */
812 /* Should never happen. */
816 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
819 int mode = extract32(insn, 3, 3);
820 int reg0 = REG(insn, 0);
821 return gen_lea_mode(env, s, mode, reg0, opsize);
824 /* Generate code to load/store a value from/into an EA. If WHAT > 0 this is
825 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
826 ADDRP is non-null for readwrite operands. */
827 static TCGv gen_ea_mode(CPUM68KState *env, DisasContext *s, int mode, int reg0,
828 int opsize, TCGv val, TCGv *addrp, ea_what what,
831 TCGv reg, tmp, result;
835 case 0: /* Data register direct. */
836 reg = cpu_dregs[reg0];
837 if (what == EA_STORE) {
838 gen_partset_reg(opsize, reg, val);
841 return gen_extend(s, reg, opsize, what == EA_LOADS);
843 case 1: /* Address register direct. */
844 reg = get_areg(s, reg0);
845 if (what == EA_STORE) {
846 tcg_gen_mov_i32(reg, val);
849 return gen_extend(s, reg, opsize, what == EA_LOADS);
851 case 2: /* Indirect register */
852 reg = get_areg(s, reg0);
853 return gen_ldst(s, opsize, reg, val, what, index);
854 case 3: /* Indirect postincrement. */
855 reg = get_areg(s, reg0);
856 result = gen_ldst(s, opsize, reg, val, what, index);
857 if (what == EA_STORE || !addrp) {
858 TCGv tmp = tcg_temp_new();
859 if (reg0 == 7 && opsize == OS_BYTE &&
860 m68k_feature(s->env, M68K_FEATURE_M68000)) {
861 tcg_gen_addi_i32(tmp, reg, 2);
863 tcg_gen_addi_i32(tmp, reg, opsize_bytes(opsize));
865 delay_set_areg(s, reg0, tmp, true);
868 case 4: /* Indirect predecrememnt. */
869 if (addrp && what == EA_STORE) {
872 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
873 if (IS_NULL_QREG(tmp)) {
880 result = gen_ldst(s, opsize, tmp, val, what, index);
881 if (what == EA_STORE || !addrp) {
882 delay_set_areg(s, reg0, tmp, false);
885 case 5: /* Indirect displacement. */
886 case 6: /* Indirect index + displacement. */
888 if (addrp && what == EA_STORE) {
891 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
892 if (IS_NULL_QREG(tmp)) {
899 return gen_ldst(s, opsize, tmp, val, what, index);
902 case 0: /* Absolute short. */
903 case 1: /* Absolute long. */
904 case 2: /* pc displacement */
905 case 3: /* pc index+displacement. */
907 case 4: /* Immediate. */
908 /* Sign extend values for consistency. */
911 if (what == EA_LOADS) {
912 offset = (int8_t)read_im8(env, s);
914 offset = read_im8(env, s);
918 if (what == EA_LOADS) {
919 offset = (int16_t)read_im16(env, s);
921 offset = read_im16(env, s);
925 offset = read_im32(env, s);
928 g_assert_not_reached();
930 return mark_to_release(s, tcg_const_i32(offset));
935 /* Should never happen. */
939 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
940 int opsize, TCGv val, TCGv *addrp, ea_what what, int index)
942 int mode = extract32(insn, 3, 3);
943 int reg0 = REG(insn, 0);
944 return gen_ea_mode(env, s, mode, reg0, opsize, val, addrp, what, index);
947 static TCGv_ptr gen_fp_ptr(int freg)
949 TCGv_ptr fp = tcg_temp_new_ptr();
950 tcg_gen_addi_ptr(fp, cpu_env, offsetof(CPUM68KState, fregs[freg]));
954 static TCGv_ptr gen_fp_result_ptr(void)
956 TCGv_ptr fp = tcg_temp_new_ptr();
957 tcg_gen_addi_ptr(fp, cpu_env, offsetof(CPUM68KState, fp_result));
961 static void gen_fp_move(TCGv_ptr dest, TCGv_ptr src)
966 t32 = tcg_temp_new();
967 tcg_gen_ld16u_i32(t32, src, offsetof(FPReg, l.upper));
968 tcg_gen_st16_i32(t32, dest, offsetof(FPReg, l.upper));
971 t64 = tcg_temp_new_i64();
972 tcg_gen_ld_i64(t64, src, offsetof(FPReg, l.lower));
973 tcg_gen_st_i64(t64, dest, offsetof(FPReg, l.lower));
974 tcg_temp_free_i64(t64);
977 static void gen_load_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp,
983 t64 = tcg_temp_new_i64();
984 tmp = tcg_temp_new();
987 tcg_gen_qemu_ld8s(tmp, addr, index);
988 gen_helper_exts32(cpu_env, fp, tmp);
991 tcg_gen_qemu_ld16s(tmp, addr, index);
992 gen_helper_exts32(cpu_env, fp, tmp);
995 tcg_gen_qemu_ld32u(tmp, addr, index);
996 gen_helper_exts32(cpu_env, fp, tmp);
999 tcg_gen_qemu_ld32u(tmp, addr, index);
1000 gen_helper_extf32(cpu_env, fp, tmp);
1003 tcg_gen_qemu_ld64(t64, addr, index);
1004 gen_helper_extf64(cpu_env, fp, t64);
1007 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
1008 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1011 tcg_gen_qemu_ld32u(tmp, addr, index);
1012 tcg_gen_shri_i32(tmp, tmp, 16);
1013 tcg_gen_st16_i32(tmp, fp, offsetof(FPReg, l.upper));
1014 tcg_gen_addi_i32(tmp, addr, 4);
1015 tcg_gen_qemu_ld64(t64, tmp, index);
1016 tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
1019 /* unimplemented data type on 68040/ColdFire
1020 * FIXME if needed for another FPU
1022 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1025 g_assert_not_reached();
1028 tcg_temp_free_i64(t64);
1031 static void gen_store_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp,
1037 t64 = tcg_temp_new_i64();
1038 tmp = tcg_temp_new();
1041 gen_helper_reds32(tmp, cpu_env, fp);
1042 tcg_gen_qemu_st8(tmp, addr, index);
1045 gen_helper_reds32(tmp, cpu_env, fp);
1046 tcg_gen_qemu_st16(tmp, addr, index);
1049 gen_helper_reds32(tmp, cpu_env, fp);
1050 tcg_gen_qemu_st32(tmp, addr, index);
1053 gen_helper_redf32(tmp, cpu_env, fp);
1054 tcg_gen_qemu_st32(tmp, addr, index);
1057 gen_helper_redf64(t64, cpu_env, fp);
1058 tcg_gen_qemu_st64(t64, addr, index);
1061 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
1062 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1065 tcg_gen_ld16u_i32(tmp, fp, offsetof(FPReg, l.upper));
1066 tcg_gen_shli_i32(tmp, tmp, 16);
1067 tcg_gen_qemu_st32(tmp, addr, index);
1068 tcg_gen_addi_i32(tmp, addr, 4);
1069 tcg_gen_ld_i64(t64, fp, offsetof(FPReg, l.lower));
1070 tcg_gen_qemu_st64(t64, tmp, index);
1073 /* unimplemented data type on 68040/ColdFire
1074 * FIXME if needed for another FPU
1076 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1079 g_assert_not_reached();
1082 tcg_temp_free_i64(t64);
1085 static void gen_ldst_fp(DisasContext *s, int opsize, TCGv addr,
1086 TCGv_ptr fp, ea_what what, int index)
1088 if (what == EA_STORE) {
1089 gen_store_fp(s, opsize, addr, fp, index);
1091 gen_load_fp(s, opsize, addr, fp, index);
1095 static int gen_ea_mode_fp(CPUM68KState *env, DisasContext *s, int mode,
1096 int reg0, int opsize, TCGv_ptr fp, ea_what what,
1099 TCGv reg, addr, tmp;
1103 case 0: /* Data register direct. */
1104 reg = cpu_dregs[reg0];
1105 if (what == EA_STORE) {
1110 gen_helper_reds32(reg, cpu_env, fp);
1113 gen_helper_redf32(reg, cpu_env, fp);
1116 g_assert_not_reached();
1119 tmp = tcg_temp_new();
1122 tcg_gen_ext8s_i32(tmp, reg);
1123 gen_helper_exts32(cpu_env, fp, tmp);
1126 tcg_gen_ext16s_i32(tmp, reg);
1127 gen_helper_exts32(cpu_env, fp, tmp);
1130 gen_helper_exts32(cpu_env, fp, reg);
1133 gen_helper_extf32(cpu_env, fp, reg);
1136 g_assert_not_reached();
1141 case 1: /* Address register direct. */
1143 case 2: /* Indirect register */
1144 addr = get_areg(s, reg0);
1145 gen_ldst_fp(s, opsize, addr, fp, what, index);
1147 case 3: /* Indirect postincrement. */
1148 addr = cpu_aregs[reg0];
1149 gen_ldst_fp(s, opsize, addr, fp, what, index);
1150 tcg_gen_addi_i32(addr, addr, opsize_bytes(opsize));
1152 case 4: /* Indirect predecrememnt. */
1153 addr = gen_lea_mode(env, s, mode, reg0, opsize);
1154 if (IS_NULL_QREG(addr)) {
1157 gen_ldst_fp(s, opsize, addr, fp, what, index);
1158 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
1160 case 5: /* Indirect displacement. */
1161 case 6: /* Indirect index + displacement. */
1163 addr = gen_lea_mode(env, s, mode, reg0, opsize);
1164 if (IS_NULL_QREG(addr)) {
1167 gen_ldst_fp(s, opsize, addr, fp, what, index);
1171 case 0: /* Absolute short. */
1172 case 1: /* Absolute long. */
1173 case 2: /* pc displacement */
1174 case 3: /* pc index+displacement. */
1176 case 4: /* Immediate. */
1177 if (what == EA_STORE) {
1182 tmp = tcg_const_i32((int8_t)read_im8(env, s));
1183 gen_helper_exts32(cpu_env, fp, tmp);
1187 tmp = tcg_const_i32((int16_t)read_im16(env, s));
1188 gen_helper_exts32(cpu_env, fp, tmp);
1192 tmp = tcg_const_i32(read_im32(env, s));
1193 gen_helper_exts32(cpu_env, fp, tmp);
1197 tmp = tcg_const_i32(read_im32(env, s));
1198 gen_helper_extf32(cpu_env, fp, tmp);
1202 t64 = tcg_const_i64(read_im64(env, s));
1203 gen_helper_extf64(cpu_env, fp, t64);
1204 tcg_temp_free_i64(t64);
1207 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
1208 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1211 tmp = tcg_const_i32(read_im32(env, s) >> 16);
1212 tcg_gen_st16_i32(tmp, fp, offsetof(FPReg, l.upper));
1214 t64 = tcg_const_i64(read_im64(env, s));
1215 tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
1216 tcg_temp_free_i64(t64);
1219 /* unimplemented data type on 68040/ColdFire
1220 * FIXME if needed for another FPU
1222 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1225 g_assert_not_reached();
1235 static int gen_ea_fp(CPUM68KState *env, DisasContext *s, uint16_t insn,
1236 int opsize, TCGv_ptr fp, ea_what what, int index)
1238 int mode = extract32(insn, 3, 3);
1239 int reg0 = REG(insn, 0);
1240 return gen_ea_mode_fp(env, s, mode, reg0, opsize, fp, what, index);
1251 static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
1257 /* The CC_OP_CMP form can handle most normal comparisons directly. */
1258 if (op == CC_OP_CMPB || op == CC_OP_CMPW || op == CC_OP_CMPL) {
1265 tcond = TCG_COND_LEU;
1269 tcond = TCG_COND_LTU;
1273 tcond = TCG_COND_EQ;
1278 c->v2 = tcg_const_i32(0);
1279 c->v1 = tmp = tcg_temp_new();
1280 tcg_gen_sub_i32(tmp, QREG_CC_N, QREG_CC_V);
1281 gen_ext(tmp, tmp, op - CC_OP_CMPB, 1);
1285 tcond = TCG_COND_LT;
1289 tcond = TCG_COND_LE;
1296 c->v2 = tcg_const_i32(0);
1302 tcond = TCG_COND_NEVER;
1304 case 14: /* GT (!(Z || (N ^ V))) */
1305 case 15: /* LE (Z || (N ^ V)) */
1306 /* Logic operations clear V, which simplifies LE to (Z || N),
1307 and since Z and N are co-located, this becomes a normal
1309 if (op == CC_OP_LOGIC) {
1311 tcond = TCG_COND_LE;
1315 case 12: /* GE (!(N ^ V)) */
1316 case 13: /* LT (N ^ V) */
1317 /* Logic operations clear V, which simplifies this to N. */
1318 if (op != CC_OP_LOGIC) {
1322 case 10: /* PL (!N) */
1323 case 11: /* MI (N) */
1324 /* Several cases represent N normally. */
1325 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1326 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
1327 op == CC_OP_LOGIC) {
1329 tcond = TCG_COND_LT;
1333 case 6: /* NE (!Z) */
1334 case 7: /* EQ (Z) */
1335 /* Some cases fold Z into N. */
1336 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1337 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
1338 op == CC_OP_LOGIC) {
1339 tcond = TCG_COND_EQ;
1344 case 4: /* CC (!C) */
1345 case 5: /* CS (C) */
1346 /* Some cases fold C into X. */
1347 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1348 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL) {
1349 tcond = TCG_COND_NE;
1354 case 8: /* VC (!V) */
1355 case 9: /* VS (V) */
1356 /* Logic operations clear V and C. */
1357 if (op == CC_OP_LOGIC) {
1358 tcond = TCG_COND_NEVER;
1365 /* Otherwise, flush flag state to CC_OP_FLAGS. */
1372 /* Invalid, or handled above. */
1374 case 2: /* HI (!C && !Z) -> !(C || Z)*/
1375 case 3: /* LS (C || Z) */
1376 c->v1 = tmp = tcg_temp_new();
1378 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
1379 tcg_gen_or_i32(tmp, tmp, QREG_CC_C);
1380 tcond = TCG_COND_NE;
1382 case 4: /* CC (!C) */
1383 case 5: /* CS (C) */
1385 tcond = TCG_COND_NE;
1387 case 6: /* NE (!Z) */
1388 case 7: /* EQ (Z) */
1390 tcond = TCG_COND_EQ;
1392 case 8: /* VC (!V) */
1393 case 9: /* VS (V) */
1395 tcond = TCG_COND_LT;
1397 case 10: /* PL (!N) */
1398 case 11: /* MI (N) */
1400 tcond = TCG_COND_LT;
1402 case 12: /* GE (!(N ^ V)) */
1403 case 13: /* LT (N ^ V) */
1404 c->v1 = tmp = tcg_temp_new();
1406 tcg_gen_xor_i32(tmp, QREG_CC_N, QREG_CC_V);
1407 tcond = TCG_COND_LT;
1409 case 14: /* GT (!(Z || (N ^ V))) */
1410 case 15: /* LE (Z || (N ^ V)) */
1411 c->v1 = tmp = tcg_temp_new();
1413 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
1414 tcg_gen_neg_i32(tmp, tmp);
1415 tmp2 = tcg_temp_new();
1416 tcg_gen_xor_i32(tmp2, QREG_CC_N, QREG_CC_V);
1417 tcg_gen_or_i32(tmp, tmp, tmp2);
1418 tcg_temp_free(tmp2);
1419 tcond = TCG_COND_LT;
1424 if ((cond & 1) == 0) {
1425 tcond = tcg_invert_cond(tcond);
1430 static void free_cond(DisasCompare *c)
1433 tcg_temp_free(c->v1);
1436 tcg_temp_free(c->v2);
1440 static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
1444 gen_cc_cond(&c, s, cond);
1446 tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
1450 /* Force a TB lookup after an instruction that changes the CPU state. */
1451 static void gen_lookup_tb(DisasContext *s)
1454 tcg_gen_movi_i32(QREG_PC, s->pc);
1455 s->is_jmp = DISAS_UPDATE;
1458 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
1459 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
1460 op_sign ? EA_LOADS : EA_LOADU, IS_USER(s)); \
1461 if (IS_NULL_QREG(result)) { \
1462 gen_addr_fault(s); \
1467 #define DEST_EA(env, insn, opsize, val, addrp) do { \
1468 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, \
1469 EA_STORE, IS_USER(s)); \
1470 if (IS_NULL_QREG(ea_result)) { \
1471 gen_addr_fault(s); \
1476 static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
1478 #ifndef CONFIG_USER_ONLY
1479 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
1480 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
1486 /* Generate a jump to an immediate address. */
1487 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
1489 if (unlikely(s->singlestep_enabled)) {
1490 gen_exception(s, dest, EXCP_DEBUG);
1491 } else if (use_goto_tb(s, dest)) {
1493 tcg_gen_movi_i32(QREG_PC, dest);
1494 tcg_gen_exit_tb(s->tb, n);
1496 gen_jmp_im(s, dest);
1497 tcg_gen_exit_tb(NULL, 0);
1499 s->is_jmp = DISAS_TB_JUMP;
1508 cond = (insn >> 8) & 0xf;
1509 gen_cc_cond(&c, s, cond);
1511 tmp = tcg_temp_new();
1512 tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
1515 tcg_gen_neg_i32(tmp, tmp);
1516 DEST_EA(env, insn, OS_BYTE, tmp, NULL);
1528 reg = DREG(insn, 0);
1530 offset = (int16_t)read_im16(env, s);
1531 l1 = gen_new_label();
1532 gen_jmpcc(s, (insn >> 8) & 0xf, l1);
1534 tmp = tcg_temp_new();
1535 tcg_gen_ext16s_i32(tmp, reg);
1536 tcg_gen_addi_i32(tmp, tmp, -1);
1537 gen_partset_reg(OS_WORD, reg, tmp);
1538 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, -1, l1);
1539 gen_jmp_tb(s, 1, base + offset);
1541 gen_jmp_tb(s, 0, s->pc);
1544 DISAS_INSN(undef_mac)
1546 gen_exception(s, s->insn_pc, EXCP_LINEA);
1549 DISAS_INSN(undef_fpu)
1551 gen_exception(s, s->insn_pc, EXCP_LINEF);
1556 /* ??? This is both instructions that are as yet unimplemented
1557 for the 680x0 series, as well as those that are implemented
1558 but actually illegal for CPU32 or pre-68020. */
1559 qemu_log_mask(LOG_UNIMP, "Illegal instruction: %04x @ %08x",
1561 gen_exception(s, s->insn_pc, EXCP_UNSUPPORTED);
1571 sign = (insn & 0x100) != 0;
1572 reg = DREG(insn, 9);
1573 tmp = tcg_temp_new();
1575 tcg_gen_ext16s_i32(tmp, reg);
1577 tcg_gen_ext16u_i32(tmp, reg);
1578 SRC_EA(env, src, OS_WORD, sign, NULL);
1579 tcg_gen_mul_i32(tmp, tmp, src);
1580 tcg_gen_mov_i32(reg, tmp);
1581 gen_logic_cc(s, tmp, OS_LONG);
1591 /* divX.w <EA>,Dn 32/16 -> 16r:16q */
1593 sign = (insn & 0x100) != 0;
1595 /* dest.l / src.w */
1597 SRC_EA(env, src, OS_WORD, sign, NULL);
1598 destr = tcg_const_i32(REG(insn, 9));
1600 gen_helper_divsw(cpu_env, destr, src);
1602 gen_helper_divuw(cpu_env, destr, src);
1604 tcg_temp_free(destr);
1606 set_cc_op(s, CC_OP_FLAGS);
1615 ext = read_im16(env, s);
1617 sign = (ext & 0x0800) != 0;
1620 if (!m68k_feature(s->env, M68K_FEATURE_QUAD_MULDIV)) {
1621 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
1625 /* divX.l <EA>, Dr:Dq 64/32 -> 32r:32q */
1627 SRC_EA(env, den, OS_LONG, 0, NULL);
1628 num = tcg_const_i32(REG(ext, 12));
1629 reg = tcg_const_i32(REG(ext, 0));
1631 gen_helper_divsll(cpu_env, num, reg, den);
1633 gen_helper_divull(cpu_env, num, reg, den);
1637 set_cc_op(s, CC_OP_FLAGS);
1641 /* divX.l <EA>, Dq 32/32 -> 32q */
1642 /* divXl.l <EA>, Dr:Dq 32/32 -> 32r:32q */
1644 SRC_EA(env, den, OS_LONG, 0, NULL);
1645 num = tcg_const_i32(REG(ext, 12));
1646 reg = tcg_const_i32(REG(ext, 0));
1648 gen_helper_divsl(cpu_env, num, reg, den);
1650 gen_helper_divul(cpu_env, num, reg, den);
1655 set_cc_op(s, CC_OP_FLAGS);
1658 static void bcd_add(TCGv dest, TCGv src)
1662 /* dest10 = dest10 + src10 + X
1666 * t3 = t2 + dest + X
1670 * t7 = (t6 >> 2) | (t6 >> 3)
1674 /* t1 = (src + 0x066) + dest + X
1675 * = result with some possible exceding 0x6
1678 t0 = tcg_const_i32(0x066);
1679 tcg_gen_add_i32(t0, t0, src);
1681 t1 = tcg_temp_new();
1682 tcg_gen_add_i32(t1, t0, dest);
1683 tcg_gen_add_i32(t1, t1, QREG_CC_X);
1685 /* we will remove exceding 0x6 where there is no carry */
1687 /* t0 = (src + 0x0066) ^ dest
1688 * = t1 without carries
1691 tcg_gen_xor_i32(t0, t0, dest);
1693 /* extract the carries
1695 * = only the carries
1698 tcg_gen_xor_i32(t0, t0, t1);
1700 /* generate 0x1 where there is no carry
1701 * and for each 0x10, generate a 0x6
1704 tcg_gen_shri_i32(t0, t0, 3);
1705 tcg_gen_not_i32(t0, t0);
1706 tcg_gen_andi_i32(t0, t0, 0x22);
1707 tcg_gen_add_i32(dest, t0, t0);
1708 tcg_gen_add_i32(dest, dest, t0);
1711 /* remove the exceding 0x6
1712 * for digits that have not generated a carry
1715 tcg_gen_sub_i32(dest, t1, dest);
1719 static void bcd_sub(TCGv dest, TCGv src)
1723 /* dest10 = dest10 - src10 - X
1724 * = bcd_add(dest + 1 - X, 0x199 - src)
1727 /* t0 = 0x066 + (0x199 - src) */
1729 t0 = tcg_temp_new();
1730 tcg_gen_subfi_i32(t0, 0x1ff, src);
1732 /* t1 = t0 + dest + 1 - X*/
1734 t1 = tcg_temp_new();
1735 tcg_gen_add_i32(t1, t0, dest);
1736 tcg_gen_addi_i32(t1, t1, 1);
1737 tcg_gen_sub_i32(t1, t1, QREG_CC_X);
1739 /* t2 = t0 ^ dest */
1741 t2 = tcg_temp_new();
1742 tcg_gen_xor_i32(t2, t0, dest);
1746 tcg_gen_xor_i32(t0, t1, t2);
1749 * t0 = (t2 >> 2) | (t2 >> 3)
1751 * to fit on 8bit operands, changed in:
1753 * t2 = ~(t0 >> 3) & 0x22
1758 tcg_gen_shri_i32(t2, t0, 3);
1759 tcg_gen_not_i32(t2, t2);
1760 tcg_gen_andi_i32(t2, t2, 0x22);
1761 tcg_gen_add_i32(t0, t2, t2);
1762 tcg_gen_add_i32(t0, t0, t2);
1765 /* return t1 - t0 */
1767 tcg_gen_sub_i32(dest, t1, t0);
1772 static void bcd_flags(TCGv val)
1774 tcg_gen_andi_i32(QREG_CC_C, val, 0x0ff);
1775 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_C);
1777 tcg_gen_extract_i32(QREG_CC_C, val, 8, 1);
1779 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
1782 DISAS_INSN(abcd_reg)
1787 gen_flush_flags(s); /* !Z is sticky */
1789 src = gen_extend(s, DREG(insn, 0), OS_BYTE, 0);
1790 dest = gen_extend(s, DREG(insn, 9), OS_BYTE, 0);
1792 gen_partset_reg(OS_BYTE, DREG(insn, 9), dest);
1797 DISAS_INSN(abcd_mem)
1799 TCGv src, dest, addr;
1801 gen_flush_flags(s); /* !Z is sticky */
1803 /* Indirect pre-decrement load (mode 4) */
1805 src = gen_ea_mode(env, s, 4, REG(insn, 0), OS_BYTE,
1806 NULL_QREG, NULL, EA_LOADU, IS_USER(s));
1807 dest = gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE,
1808 NULL_QREG, &addr, EA_LOADU, IS_USER(s));
1812 gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE, dest, &addr,
1813 EA_STORE, IS_USER(s));
1818 DISAS_INSN(sbcd_reg)
1822 gen_flush_flags(s); /* !Z is sticky */
1824 src = gen_extend(s, DREG(insn, 0), OS_BYTE, 0);
1825 dest = gen_extend(s, DREG(insn, 9), OS_BYTE, 0);
1829 gen_partset_reg(OS_BYTE, DREG(insn, 9), dest);
1834 DISAS_INSN(sbcd_mem)
1836 TCGv src, dest, addr;
1838 gen_flush_flags(s); /* !Z is sticky */
1840 /* Indirect pre-decrement load (mode 4) */
1842 src = gen_ea_mode(env, s, 4, REG(insn, 0), OS_BYTE,
1843 NULL_QREG, NULL, EA_LOADU, IS_USER(s));
1844 dest = gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE,
1845 NULL_QREG, &addr, EA_LOADU, IS_USER(s));
1849 gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE, dest, &addr,
1850 EA_STORE, IS_USER(s));
1860 gen_flush_flags(s); /* !Z is sticky */
1862 SRC_EA(env, src, OS_BYTE, 0, &addr);
1864 dest = tcg_const_i32(0);
1867 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1871 tcg_temp_free(dest);
1884 add = (insn & 0x4000) != 0;
1885 opsize = insn_opsize(insn);
1886 reg = gen_extend(s, DREG(insn, 9), opsize, 1);
1887 dest = tcg_temp_new();
1889 SRC_EA(env, tmp, opsize, 1, &addr);
1893 SRC_EA(env, src, opsize, 1, NULL);
1896 tcg_gen_add_i32(dest, tmp, src);
1897 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src);
1898 set_cc_op(s, CC_OP_ADDB + opsize);
1900 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, tmp, src);
1901 tcg_gen_sub_i32(dest, tmp, src);
1902 set_cc_op(s, CC_OP_SUBB + opsize);
1904 gen_update_cc_add(dest, src, opsize);
1906 DEST_EA(env, insn, opsize, dest, &addr);
1908 gen_partset_reg(opsize, DREG(insn, 9), dest);
1910 tcg_temp_free(dest);
1913 /* Reverse the order of the bits in REG. */
1917 reg = DREG(insn, 0);
1918 gen_helper_bitrev(reg, reg);
1921 DISAS_INSN(bitop_reg)
1931 if ((insn & 0x38) != 0)
1935 op = (insn >> 6) & 3;
1936 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1939 src2 = tcg_temp_new();
1940 if (opsize == OS_BYTE)
1941 tcg_gen_andi_i32(src2, DREG(insn, 9), 7);
1943 tcg_gen_andi_i32(src2, DREG(insn, 9), 31);
1945 tmp = tcg_const_i32(1);
1946 tcg_gen_shl_i32(tmp, tmp, src2);
1947 tcg_temp_free(src2);
1949 tcg_gen_and_i32(QREG_CC_Z, src1, tmp);
1951 dest = tcg_temp_new();
1954 tcg_gen_xor_i32(dest, src1, tmp);
1957 tcg_gen_andc_i32(dest, src1, tmp);
1960 tcg_gen_or_i32(dest, src1, tmp);
1967 DEST_EA(env, insn, opsize, dest, &addr);
1969 tcg_temp_free(dest);
1975 reg = DREG(insn, 0);
1977 gen_helper_sats(reg, reg, QREG_CC_V);
1978 gen_logic_cc(s, reg, OS_LONG);
1981 static void gen_push(DisasContext *s, TCGv val)
1985 tmp = tcg_temp_new();
1986 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1987 gen_store(s, OS_LONG, tmp, val, IS_USER(s));
1988 tcg_gen_mov_i32(QREG_SP, tmp);
1992 static TCGv mreg(int reg)
1996 return cpu_dregs[reg];
1999 return cpu_aregs[reg & 7];
2004 TCGv addr, incr, tmp, r[16];
2005 int is_load = (insn & 0x0400) != 0;
2006 int opsize = (insn & 0x40) != 0 ? OS_LONG : OS_WORD;
2007 uint16_t mask = read_im16(env, s);
2008 int mode = extract32(insn, 3, 3);
2009 int reg0 = REG(insn, 0);
2012 tmp = cpu_aregs[reg0];
2015 case 0: /* data register direct */
2016 case 1: /* addr register direct */
2021 case 2: /* indirect */
2024 case 3: /* indirect post-increment */
2026 /* post-increment is not allowed */
2031 case 4: /* indirect pre-decrement */
2033 /* pre-decrement is not allowed */
2036 /* We want a bare copy of the address reg, without any pre-decrement
2037 adjustment, as gen_lea would provide. */
2041 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
2042 if (IS_NULL_QREG(tmp)) {
2048 addr = tcg_temp_new();
2049 tcg_gen_mov_i32(addr, tmp);
2050 incr = tcg_const_i32(opsize_bytes(opsize));
2053 /* memory to register */
2054 for (i = 0; i < 16; i++) {
2055 if (mask & (1 << i)) {
2056 r[i] = gen_load(s, opsize, addr, 1, IS_USER(s));
2057 tcg_gen_add_i32(addr, addr, incr);
2060 for (i = 0; i < 16; i++) {
2061 if (mask & (1 << i)) {
2062 tcg_gen_mov_i32(mreg(i), r[i]);
2063 tcg_temp_free(r[i]);
2067 /* post-increment: movem (An)+,X */
2068 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
2071 /* register to memory */
2073 /* pre-decrement: movem X,-(An) */
2074 for (i = 15; i >= 0; i--) {
2075 if ((mask << i) & 0x8000) {
2076 tcg_gen_sub_i32(addr, addr, incr);
2077 if (reg0 + 8 == i &&
2078 m68k_feature(s->env, M68K_FEATURE_EXT_FULL)) {
2079 /* M68020+: if the addressing register is the
2080 * register moved to memory, the value written
2081 * is the initial value decremented by the size of
2082 * the operation, regardless of how many actual
2083 * stores have been performed until this point.
2084 * M68000/M68010: the value is the initial value.
2086 tmp = tcg_temp_new();
2087 tcg_gen_sub_i32(tmp, cpu_aregs[reg0], incr);
2088 gen_store(s, opsize, addr, tmp, IS_USER(s));
2091 gen_store(s, opsize, addr, mreg(i), IS_USER(s));
2095 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
2097 for (i = 0; i < 16; i++) {
2098 if (mask & (1 << i)) {
2099 gen_store(s, opsize, addr, mreg(i), IS_USER(s));
2100 tcg_gen_add_i32(addr, addr, incr);
2106 tcg_temp_free(incr);
2107 tcg_temp_free(addr);
2119 displ = read_im16(env, s);
2121 addr = AREG(insn, 0);
2122 reg = DREG(insn, 9);
2124 abuf = tcg_temp_new();
2125 tcg_gen_addi_i32(abuf, addr, displ);
2126 dbuf = tcg_temp_new();
2135 for ( ; i > 0 ; i--) {
2136 tcg_gen_shri_i32(dbuf, reg, (i - 1) * 8);
2137 tcg_gen_qemu_st8(dbuf, abuf, IS_USER(s));
2139 tcg_gen_addi_i32(abuf, abuf, 2);
2143 for ( ; i > 0 ; i--) {
2144 tcg_gen_qemu_ld8u(dbuf, abuf, IS_USER(s));
2145 tcg_gen_deposit_i32(reg, reg, dbuf, (i - 1) * 8, 8);
2147 tcg_gen_addi_i32(abuf, abuf, 2);
2151 tcg_temp_free(abuf);
2152 tcg_temp_free(dbuf);
2155 DISAS_INSN(bitop_im)
2165 if ((insn & 0x38) != 0)
2169 op = (insn >> 6) & 3;
2171 bitnum = read_im16(env, s);
2172 if (m68k_feature(s->env, M68K_FEATURE_M68000)) {
2173 if (bitnum & 0xfe00) {
2174 disas_undef(env, s, insn);
2178 if (bitnum & 0xff00) {
2179 disas_undef(env, s, insn);
2184 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
2187 if (opsize == OS_BYTE)
2193 tcg_gen_andi_i32(QREG_CC_Z, src1, mask);
2196 tmp = tcg_temp_new();
2199 tcg_gen_xori_i32(tmp, src1, mask);
2202 tcg_gen_andi_i32(tmp, src1, ~mask);
2205 tcg_gen_ori_i32(tmp, src1, mask);
2210 DEST_EA(env, insn, opsize, tmp, &addr);
2215 static TCGv gen_get_ccr(DisasContext *s)
2220 dest = tcg_temp_new();
2221 gen_helper_get_ccr(dest, cpu_env);
2225 static TCGv gen_get_sr(DisasContext *s)
2230 ccr = gen_get_ccr(s);
2231 sr = tcg_temp_new();
2232 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
2233 tcg_gen_or_i32(sr, sr, ccr);
2237 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
2240 tcg_gen_movi_i32(QREG_CC_C, val & CCF_C ? 1 : 0);
2241 tcg_gen_movi_i32(QREG_CC_V, val & CCF_V ? -1 : 0);
2242 tcg_gen_movi_i32(QREG_CC_Z, val & CCF_Z ? 0 : 1);
2243 tcg_gen_movi_i32(QREG_CC_N, val & CCF_N ? -1 : 0);
2244 tcg_gen_movi_i32(QREG_CC_X, val & CCF_X ? 1 : 0);
2246 TCGv sr = tcg_const_i32(val);
2247 gen_helper_set_sr(cpu_env, sr);
2250 set_cc_op(s, CC_OP_FLAGS);
2253 static void gen_set_sr(DisasContext *s, TCGv val, int ccr_only)
2256 gen_helper_set_ccr(cpu_env, val);
2258 gen_helper_set_sr(cpu_env, val);
2260 set_cc_op(s, CC_OP_FLAGS);
2263 static void gen_move_to_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
2266 if ((insn & 0x3f) == 0x3c) {
2268 val = read_im16(env, s);
2269 gen_set_sr_im(s, val, ccr_only);
2272 SRC_EA(env, src, OS_WORD, 0, NULL);
2273 gen_set_sr(s, src, ccr_only);
2277 DISAS_INSN(arith_im)
2285 bool with_SR = ((insn & 0x3f) == 0x3c);
2287 op = (insn >> 9) & 7;
2288 opsize = insn_opsize(insn);
2291 im = tcg_const_i32((int8_t)read_im8(env, s));
2294 im = tcg_const_i32((int16_t)read_im16(env, s));
2297 im = tcg_const_i32(read_im32(env, s));
2300 g_assert_not_reached();
2304 /* SR/CCR can only be used with andi/eori/ori */
2305 if (op == 2 || op == 3 || op == 6) {
2306 disas_undef(env, s, insn);
2311 src1 = gen_get_ccr(s);
2315 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
2318 src1 = gen_get_sr(s);
2321 /* OS_LONG; others already g_assert_not_reached. */
2322 disas_undef(env, s, insn);
2326 SRC_EA(env, src1, opsize, 1, (op == 6) ? NULL : &addr);
2328 dest = tcg_temp_new();
2331 tcg_gen_or_i32(dest, src1, im);
2333 gen_set_sr(s, dest, opsize == OS_BYTE);
2335 DEST_EA(env, insn, opsize, dest, &addr);
2336 gen_logic_cc(s, dest, opsize);
2340 tcg_gen_and_i32(dest, src1, im);
2342 gen_set_sr(s, dest, opsize == OS_BYTE);
2344 DEST_EA(env, insn, opsize, dest, &addr);
2345 gen_logic_cc(s, dest, opsize);
2349 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, src1, im);
2350 tcg_gen_sub_i32(dest, src1, im);
2351 gen_update_cc_add(dest, im, opsize);
2352 set_cc_op(s, CC_OP_SUBB + opsize);
2353 DEST_EA(env, insn, opsize, dest, &addr);
2356 tcg_gen_add_i32(dest, src1, im);
2357 gen_update_cc_add(dest, im, opsize);
2358 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, im);
2359 set_cc_op(s, CC_OP_ADDB + opsize);
2360 DEST_EA(env, insn, opsize, dest, &addr);
2363 tcg_gen_xor_i32(dest, src1, im);
2365 gen_set_sr(s, dest, opsize == OS_BYTE);
2367 DEST_EA(env, insn, opsize, dest, &addr);
2368 gen_logic_cc(s, dest, opsize);
2372 gen_update_cc_cmp(s, src1, im, opsize);
2378 tcg_temp_free(dest);
2390 switch ((insn >> 9) & 3) {
2404 g_assert_not_reached();
2407 ext = read_im16(env, s);
2409 /* cas Dc,Du,<EA> */
2411 addr = gen_lea(env, s, insn, opsize);
2412 if (IS_NULL_QREG(addr)) {
2417 cmp = gen_extend(s, DREG(ext, 0), opsize, 1);
2419 /* if <EA> == Dc then
2421 * Dc = <EA> (because <EA> == Dc)
2426 load = tcg_temp_new();
2427 tcg_gen_atomic_cmpxchg_i32(load, addr, cmp, DREG(ext, 6),
2429 /* update flags before setting cmp to load */
2430 gen_update_cc_cmp(s, load, cmp, opsize);
2431 gen_partset_reg(opsize, DREG(ext, 0), load);
2433 tcg_temp_free(load);
2435 switch (extract32(insn, 3, 3)) {
2436 case 3: /* Indirect postincrement. */
2437 tcg_gen_addi_i32(AREG(insn, 0), addr, opsize_bytes(opsize));
2439 case 4: /* Indirect predecrememnt. */
2440 tcg_gen_mov_i32(AREG(insn, 0), addr);
2447 uint16_t ext1, ext2;
2451 /* cas2 Dc1:Dc2,Du1:Du2,(Rn1):(Rn2) */
2453 ext1 = read_im16(env, s);
2455 if (ext1 & 0x8000) {
2456 /* Address Register */
2457 addr1 = AREG(ext1, 12);
2460 addr1 = DREG(ext1, 12);
2463 ext2 = read_im16(env, s);
2464 if (ext2 & 0x8000) {
2465 /* Address Register */
2466 addr2 = AREG(ext2, 12);
2469 addr2 = DREG(ext2, 12);
2472 /* if (R1) == Dc1 && (R2) == Dc2 then
2480 regs = tcg_const_i32(REG(ext2, 6) |
2481 (REG(ext1, 6) << 3) |
2482 (REG(ext2, 0) << 6) |
2483 (REG(ext1, 0) << 9));
2484 if (tb_cflags(s->tb) & CF_PARALLEL) {
2485 gen_helper_exit_atomic(cpu_env);
2487 gen_helper_cas2w(cpu_env, regs, addr1, addr2);
2489 tcg_temp_free(regs);
2491 /* Note that cas2w also assigned to env->cc_op. */
2492 s->cc_op = CC_OP_CMPW;
2493 s->cc_op_synced = 1;
2498 uint16_t ext1, ext2;
2499 TCGv addr1, addr2, regs;
2501 /* cas2 Dc1:Dc2,Du1:Du2,(Rn1):(Rn2) */
2503 ext1 = read_im16(env, s);
2505 if (ext1 & 0x8000) {
2506 /* Address Register */
2507 addr1 = AREG(ext1, 12);
2510 addr1 = DREG(ext1, 12);
2513 ext2 = read_im16(env, s);
2514 if (ext2 & 0x8000) {
2515 /* Address Register */
2516 addr2 = AREG(ext2, 12);
2519 addr2 = DREG(ext2, 12);
2522 /* if (R1) == Dc1 && (R2) == Dc2 then
2530 regs = tcg_const_i32(REG(ext2, 6) |
2531 (REG(ext1, 6) << 3) |
2532 (REG(ext2, 0) << 6) |
2533 (REG(ext1, 0) << 9));
2534 if (tb_cflags(s->tb) & CF_PARALLEL) {
2535 gen_helper_cas2l_parallel(cpu_env, regs, addr1, addr2);
2537 gen_helper_cas2l(cpu_env, regs, addr1, addr2);
2539 tcg_temp_free(regs);
2541 /* Note that cas2l also assigned to env->cc_op. */
2542 s->cc_op = CC_OP_CMPL;
2543 s->cc_op_synced = 1;
2550 reg = DREG(insn, 0);
2551 tcg_gen_bswap32_i32(reg, reg);
2561 switch (insn >> 12) {
2562 case 1: /* move.b */
2565 case 2: /* move.l */
2568 case 3: /* move.w */
2574 SRC_EA(env, src, opsize, 1, NULL);
2575 op = (insn >> 6) & 7;
2578 /* The value will already have been sign extended. */
2579 dest = AREG(insn, 9);
2580 tcg_gen_mov_i32(dest, src);
2584 dest_ea = ((insn >> 9) & 7) | (op << 3);
2585 DEST_EA(env, dest_ea, opsize, src, NULL);
2586 /* This will be correct because loads sign extend. */
2587 gen_logic_cc(s, src, opsize);
2598 opsize = insn_opsize(insn);
2599 SRC_EA(env, src, opsize, 1, &addr);
2601 gen_flush_flags(s); /* compute old Z */
2603 /* Perform substract with borrow.
2604 * (X, N) = -(src + X);
2607 z = tcg_const_i32(0);
2608 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, z, QREG_CC_X, z);
2609 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, z, z, QREG_CC_N, QREG_CC_X);
2611 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
2613 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
2615 /* Compute signed-overflow for negation. The normal formula for
2616 * subtraction is (res ^ src) & (src ^ dest), but with dest==0
2617 * this simplies to res & src.
2620 tcg_gen_and_i32(QREG_CC_V, QREG_CC_N, src);
2622 /* Copy the rest of the results into place. */
2623 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
2624 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
2626 set_cc_op(s, CC_OP_FLAGS);
2628 /* result is in QREG_CC_N */
2630 DEST_EA(env, insn, opsize, QREG_CC_N, &addr);
2638 reg = AREG(insn, 9);
2639 tmp = gen_lea(env, s, insn, OS_LONG);
2640 if (IS_NULL_QREG(tmp)) {
2644 tcg_gen_mov_i32(reg, tmp);
2652 zero = tcg_const_i32(0);
2654 opsize = insn_opsize(insn);
2655 DEST_EA(env, insn, opsize, zero, NULL);
2656 gen_logic_cc(s, zero, opsize);
2657 tcg_temp_free(zero);
2660 DISAS_INSN(move_from_ccr)
2664 ccr = gen_get_ccr(s);
2665 DEST_EA(env, insn, OS_WORD, ccr, NULL);
2675 opsize = insn_opsize(insn);
2676 SRC_EA(env, src1, opsize, 1, &addr);
2677 dest = tcg_temp_new();
2678 tcg_gen_neg_i32(dest, src1);
2679 set_cc_op(s, CC_OP_SUBB + opsize);
2680 gen_update_cc_add(dest, src1, opsize);
2681 tcg_gen_setcondi_i32(TCG_COND_NE, QREG_CC_X, dest, 0);
2682 DEST_EA(env, insn, opsize, dest, &addr);
2683 tcg_temp_free(dest);
2686 DISAS_INSN(move_to_ccr)
2688 gen_move_to_sr(env, s, insn, true);
2698 opsize = insn_opsize(insn);
2699 SRC_EA(env, src1, opsize, 1, &addr);
2700 dest = tcg_temp_new();
2701 tcg_gen_not_i32(dest, src1);
2702 DEST_EA(env, insn, opsize, dest, &addr);
2703 gen_logic_cc(s, dest, opsize);
2712 src1 = tcg_temp_new();
2713 src2 = tcg_temp_new();
2714 reg = DREG(insn, 0);
2715 tcg_gen_shli_i32(src1, reg, 16);
2716 tcg_gen_shri_i32(src2, reg, 16);
2717 tcg_gen_or_i32(reg, src1, src2);
2718 tcg_temp_free(src2);
2719 tcg_temp_free(src1);
2720 gen_logic_cc(s, reg, OS_LONG);
2725 gen_exception(s, s->insn_pc, EXCP_DEBUG);
2732 tmp = gen_lea(env, s, insn, OS_LONG);
2733 if (IS_NULL_QREG(tmp)) {
2746 reg = DREG(insn, 0);
2747 op = (insn >> 6) & 7;
2748 tmp = tcg_temp_new();
2750 tcg_gen_ext16s_i32(tmp, reg);
2752 tcg_gen_ext8s_i32(tmp, reg);
2754 gen_partset_reg(OS_WORD, reg, tmp);
2756 tcg_gen_mov_i32(reg, tmp);
2757 gen_logic_cc(s, tmp, OS_LONG);
2766 opsize = insn_opsize(insn);
2767 SRC_EA(env, tmp, opsize, 1, NULL);
2768 gen_logic_cc(s, tmp, opsize);
2773 /* Implemented as a NOP. */
2778 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
2781 /* ??? This should be atomic. */
2788 dest = tcg_temp_new();
2789 SRC_EA(env, src1, OS_BYTE, 1, &addr);
2790 gen_logic_cc(s, src1, OS_BYTE);
2791 tcg_gen_ori_i32(dest, src1, 0x80);
2792 DEST_EA(env, insn, OS_BYTE, dest, &addr);
2793 tcg_temp_free(dest);
2802 ext = read_im16(env, s);
2807 if (!m68k_feature(s->env, M68K_FEATURE_QUAD_MULDIV)) {
2808 gen_exception(s, s->insn_pc, EXCP_UNSUPPORTED);
2812 SRC_EA(env, src1, OS_LONG, 0, NULL);
2815 tcg_gen_muls2_i32(QREG_CC_Z, QREG_CC_N, src1, DREG(ext, 12));
2817 tcg_gen_mulu2_i32(QREG_CC_Z, QREG_CC_N, src1, DREG(ext, 12));
2819 /* if Dl == Dh, 68040 returns low word */
2820 tcg_gen_mov_i32(DREG(ext, 0), QREG_CC_N);
2821 tcg_gen_mov_i32(DREG(ext, 12), QREG_CC_Z);
2822 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N);
2824 tcg_gen_movi_i32(QREG_CC_V, 0);
2825 tcg_gen_movi_i32(QREG_CC_C, 0);
2827 set_cc_op(s, CC_OP_FLAGS);
2830 SRC_EA(env, src1, OS_LONG, 0, NULL);
2831 if (m68k_feature(s->env, M68K_FEATURE_M68000)) {
2832 tcg_gen_movi_i32(QREG_CC_C, 0);
2834 tcg_gen_muls2_i32(QREG_CC_N, QREG_CC_V, src1, DREG(ext, 12));
2835 /* QREG_CC_V is -(QREG_CC_V != (QREG_CC_N >> 31)) */
2836 tcg_gen_sari_i32(QREG_CC_Z, QREG_CC_N, 31);
2837 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, QREG_CC_Z);
2839 tcg_gen_mulu2_i32(QREG_CC_N, QREG_CC_V, src1, DREG(ext, 12));
2840 /* QREG_CC_V is -(QREG_CC_V != 0), use QREG_CC_C as 0 */
2841 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, QREG_CC_C);
2843 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
2844 tcg_gen_mov_i32(DREG(ext, 12), QREG_CC_N);
2846 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
2848 set_cc_op(s, CC_OP_FLAGS);
2850 /* The upper 32 bits of the product are discarded, so
2851 muls.l and mulu.l are functionally equivalent. */
2852 tcg_gen_mul_i32(DREG(ext, 12), src1, DREG(ext, 12));
2853 gen_logic_cc(s, DREG(ext, 12), OS_LONG);
2857 static void gen_link(DisasContext *s, uint16_t insn, int32_t offset)
2862 reg = AREG(insn, 0);
2863 tmp = tcg_temp_new();
2864 tcg_gen_subi_i32(tmp, QREG_SP, 4);
2865 gen_store(s, OS_LONG, tmp, reg, IS_USER(s));
2866 if ((insn & 7) != 7) {
2867 tcg_gen_mov_i32(reg, tmp);
2869 tcg_gen_addi_i32(QREG_SP, tmp, offset);
2877 offset = read_im16(env, s);
2878 gen_link(s, insn, offset);
2885 offset = read_im32(env, s);
2886 gen_link(s, insn, offset);
2895 src = tcg_temp_new();
2896 reg = AREG(insn, 0);
2897 tcg_gen_mov_i32(src, reg);
2898 tmp = gen_load(s, OS_LONG, src, 0, IS_USER(s));
2899 tcg_gen_mov_i32(reg, tmp);
2900 tcg_gen_addi_i32(QREG_SP, src, 4);
2905 #if defined(CONFIG_SOFTMMU)
2909 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
2913 gen_helper_reset(cpu_env);
2924 int16_t offset = read_im16(env, s);
2926 tmp = gen_load(s, OS_LONG, QREG_SP, 0, IS_USER(s));
2927 tcg_gen_addi_i32(QREG_SP, QREG_SP, offset + 4);
2935 tmp = gen_load(s, OS_LONG, QREG_SP, 0, IS_USER(s));
2936 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
2944 /* Load the target address first to ensure correct exception
2946 tmp = gen_lea(env, s, insn, OS_LONG);
2947 if (IS_NULL_QREG(tmp)) {
2951 if ((insn & 0x40) == 0) {
2953 gen_push(s, tcg_const_i32(s->pc));
2967 if ((insn & 070) == 010) {
2968 /* Operation on address register is always long. */
2971 opsize = insn_opsize(insn);
2973 SRC_EA(env, src, opsize, 1, &addr);
2974 imm = (insn >> 9) & 7;
2978 val = tcg_const_i32(imm);
2979 dest = tcg_temp_new();
2980 tcg_gen_mov_i32(dest, src);
2981 if ((insn & 0x38) == 0x08) {
2982 /* Don't update condition codes if the destination is an
2983 address register. */
2984 if (insn & 0x0100) {
2985 tcg_gen_sub_i32(dest, dest, val);
2987 tcg_gen_add_i32(dest, dest, val);
2990 if (insn & 0x0100) {
2991 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
2992 tcg_gen_sub_i32(dest, dest, val);
2993 set_cc_op(s, CC_OP_SUBB + opsize);
2995 tcg_gen_add_i32(dest, dest, val);
2996 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
2997 set_cc_op(s, CC_OP_ADDB + opsize);
2999 gen_update_cc_add(dest, val, opsize);
3002 DEST_EA(env, insn, opsize, dest, &addr);
3003 tcg_temp_free(dest);
3009 case 2: /* One extension word. */
3012 case 3: /* Two extension words. */
3015 case 4: /* No extension words. */
3018 disas_undef(env, s, insn);
3030 op = (insn >> 8) & 0xf;
3031 offset = (int8_t)insn;
3033 offset = (int16_t)read_im16(env, s);
3034 } else if (offset == -1) {
3035 offset = read_im32(env, s);
3039 gen_push(s, tcg_const_i32(s->pc));
3043 l1 = gen_new_label();
3044 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
3045 gen_jmp_tb(s, 1, base + offset);
3047 gen_jmp_tb(s, 0, s->pc);
3049 /* Unconditional branch. */
3051 gen_jmp_tb(s, 0, base + offset);
3057 tcg_gen_movi_i32(DREG(insn, 9), (int8_t)insn);
3058 gen_logic_cc(s, DREG(insn, 9), OS_LONG);
3071 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
3072 reg = DREG(insn, 9);
3073 tcg_gen_mov_i32(reg, src);
3074 gen_logic_cc(s, src, opsize);
3085 opsize = insn_opsize(insn);
3086 reg = gen_extend(s, DREG(insn, 9), opsize, 0);
3087 dest = tcg_temp_new();
3089 SRC_EA(env, src, opsize, 0, &addr);
3090 tcg_gen_or_i32(dest, src, reg);
3091 DEST_EA(env, insn, opsize, dest, &addr);
3093 SRC_EA(env, src, opsize, 0, NULL);
3094 tcg_gen_or_i32(dest, src, reg);
3095 gen_partset_reg(opsize, DREG(insn, 9), dest);
3097 gen_logic_cc(s, dest, opsize);
3098 tcg_temp_free(dest);
3106 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
3107 reg = AREG(insn, 9);
3108 tcg_gen_sub_i32(reg, reg, src);
3111 static inline void gen_subx(DisasContext *s, TCGv src, TCGv dest, int opsize)
3115 gen_flush_flags(s); /* compute old Z */
3117 /* Perform substract with borrow.
3118 * (X, N) = dest - (src + X);
3121 tmp = tcg_const_i32(0);
3122 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, tmp, QREG_CC_X, tmp);
3123 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, dest, tmp, QREG_CC_N, QREG_CC_X);
3124 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3125 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
3127 /* Compute signed-overflow for substract. */
3129 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, dest);
3130 tcg_gen_xor_i32(tmp, dest, src);
3131 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, tmp);
3134 /* Copy the rest of the results into place. */
3135 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
3136 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
3138 set_cc_op(s, CC_OP_FLAGS);
3140 /* result is in QREG_CC_N */
3143 DISAS_INSN(subx_reg)
3149 opsize = insn_opsize(insn);
3151 src = gen_extend(s, DREG(insn, 0), opsize, 1);
3152 dest = gen_extend(s, DREG(insn, 9), opsize, 1);
3154 gen_subx(s, src, dest, opsize);
3156 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
3159 DISAS_INSN(subx_mem)
3167 opsize = insn_opsize(insn);
3169 addr_src = AREG(insn, 0);
3170 tcg_gen_subi_i32(addr_src, addr_src, opsize_bytes(opsize));
3171 src = gen_load(s, opsize, addr_src, 1, IS_USER(s));
3173 addr_dest = AREG(insn, 9);
3174 tcg_gen_subi_i32(addr_dest, addr_dest, opsize_bytes(opsize));
3175 dest = gen_load(s, opsize, addr_dest, 1, IS_USER(s));
3177 gen_subx(s, src, dest, opsize);
3179 gen_store(s, opsize, addr_dest, QREG_CC_N, IS_USER(s));
3181 tcg_temp_free(dest);
3190 val = (insn >> 9) & 7;
3193 src = tcg_const_i32(val);
3194 gen_logic_cc(s, src, OS_LONG);
3195 DEST_EA(env, insn, OS_LONG, src, NULL);
3205 opsize = insn_opsize(insn);
3206 SRC_EA(env, src, opsize, 1, NULL);
3207 reg = gen_extend(s, DREG(insn, 9), opsize, 1);
3208 gen_update_cc_cmp(s, reg, src, opsize);
3222 SRC_EA(env, src, opsize, 1, NULL);
3223 reg = AREG(insn, 9);
3224 gen_update_cc_cmp(s, reg, src, OS_LONG);
3229 int opsize = insn_opsize(insn);
3232 /* Post-increment load (mode 3) from Ay. */
3233 src = gen_ea_mode(env, s, 3, REG(insn, 0), opsize,
3234 NULL_QREG, NULL, EA_LOADS, IS_USER(s));
3235 /* Post-increment load (mode 3) from Ax. */
3236 dst = gen_ea_mode(env, s, 3, REG(insn, 9), opsize,
3237 NULL_QREG, NULL, EA_LOADS, IS_USER(s));
3239 gen_update_cc_cmp(s, dst, src, opsize);
3249 opsize = insn_opsize(insn);
3251 SRC_EA(env, src, opsize, 0, &addr);
3252 dest = tcg_temp_new();
3253 tcg_gen_xor_i32(dest, src, DREG(insn, 9));
3254 gen_logic_cc(s, dest, opsize);
3255 DEST_EA(env, insn, opsize, dest, &addr);
3256 tcg_temp_free(dest);
3259 static void do_exg(TCGv reg1, TCGv reg2)
3261 TCGv temp = tcg_temp_new();
3262 tcg_gen_mov_i32(temp, reg1);
3263 tcg_gen_mov_i32(reg1, reg2);
3264 tcg_gen_mov_i32(reg2, temp);
3265 tcg_temp_free(temp);
3270 /* exchange Dx and Dy */
3271 do_exg(DREG(insn, 9), DREG(insn, 0));
3276 /* exchange Ax and Ay */
3277 do_exg(AREG(insn, 9), AREG(insn, 0));
3282 /* exchange Dx and Ay */
3283 do_exg(DREG(insn, 9), AREG(insn, 0));
3294 dest = tcg_temp_new();
3296 opsize = insn_opsize(insn);
3297 reg = DREG(insn, 9);
3299 SRC_EA(env, src, opsize, 0, &addr);
3300 tcg_gen_and_i32(dest, src, reg);
3301 DEST_EA(env, insn, opsize, dest, &addr);
3303 SRC_EA(env, src, opsize, 0, NULL);
3304 tcg_gen_and_i32(dest, src, reg);
3305 gen_partset_reg(opsize, reg, dest);
3307 gen_logic_cc(s, dest, opsize);
3308 tcg_temp_free(dest);
3316 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
3317 reg = AREG(insn, 9);
3318 tcg_gen_add_i32(reg, reg, src);
3321 static inline void gen_addx(DisasContext *s, TCGv src, TCGv dest, int opsize)
3325 gen_flush_flags(s); /* compute old Z */
3327 /* Perform addition with carry.
3328 * (X, N) = src + dest + X;
3331 tmp = tcg_const_i32(0);
3332 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_X, tmp, dest, tmp);
3333 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_N, QREG_CC_X, src, tmp);
3334 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3336 /* Compute signed-overflow for addition. */
3338 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, src);
3339 tcg_gen_xor_i32(tmp, dest, src);
3340 tcg_gen_andc_i32(QREG_CC_V, QREG_CC_V, tmp);
3343 /* Copy the rest of the results into place. */
3344 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
3345 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
3347 set_cc_op(s, CC_OP_FLAGS);
3349 /* result is in QREG_CC_N */
3352 DISAS_INSN(addx_reg)
3358 opsize = insn_opsize(insn);
3360 dest = gen_extend(s, DREG(insn, 9), opsize, 1);
3361 src = gen_extend(s, DREG(insn, 0), opsize, 1);
3363 gen_addx(s, src, dest, opsize);
3365 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
3368 DISAS_INSN(addx_mem)
3376 opsize = insn_opsize(insn);
3378 addr_src = AREG(insn, 0);
3379 tcg_gen_subi_i32(addr_src, addr_src, opsize_bytes(opsize));
3380 src = gen_load(s, opsize, addr_src, 1, IS_USER(s));
3382 addr_dest = AREG(insn, 9);
3383 tcg_gen_subi_i32(addr_dest, addr_dest, opsize_bytes(opsize));
3384 dest = gen_load(s, opsize, addr_dest, 1, IS_USER(s));
3386 gen_addx(s, src, dest, opsize);
3388 gen_store(s, opsize, addr_dest, QREG_CC_N, IS_USER(s));
3390 tcg_temp_free(dest);
3394 static inline void shift_im(DisasContext *s, uint16_t insn, int opsize)
3396 int count = (insn >> 9) & 7;
3397 int logical = insn & 8;
3398 int left = insn & 0x100;
3399 int bits = opsize_bytes(opsize) * 8;
3400 TCGv reg = gen_extend(s, DREG(insn, 0), opsize, !logical);
3406 tcg_gen_movi_i32(QREG_CC_V, 0);
3408 tcg_gen_shri_i32(QREG_CC_C, reg, bits - count);
3409 tcg_gen_shli_i32(QREG_CC_N, reg, count);
3411 /* Note that ColdFire always clears V (done above),
3412 while M68000 sets if the most significant bit is changed at
3413 any time during the shift operation */
3414 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3415 /* if shift count >= bits, V is (reg != 0) */
3416 if (count >= bits) {
3417 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, reg, QREG_CC_V);
3419 TCGv t0 = tcg_temp_new();
3420 tcg_gen_sari_i32(QREG_CC_V, reg, bits - 1);
3421 tcg_gen_sari_i32(t0, reg, bits - count - 1);
3422 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, t0);
3425 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
3428 tcg_gen_shri_i32(QREG_CC_C, reg, count - 1);
3430 tcg_gen_shri_i32(QREG_CC_N, reg, count);
3432 tcg_gen_sari_i32(QREG_CC_N, reg, count);
3436 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3437 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3438 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3439 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
3441 gen_partset_reg(opsize, DREG(insn, 0), QREG_CC_N);
3442 set_cc_op(s, CC_OP_FLAGS);
3445 static inline void shift_reg(DisasContext *s, uint16_t insn, int opsize)
3447 int logical = insn & 8;
3448 int left = insn & 0x100;
3449 int bits = opsize_bytes(opsize) * 8;
3450 TCGv reg = gen_extend(s, DREG(insn, 0), opsize, !logical);
3454 t64 = tcg_temp_new_i64();
3455 s64 = tcg_temp_new_i64();
3456 s32 = tcg_temp_new();
3458 /* Note that m68k truncates the shift count modulo 64, not 32.
3459 In addition, a 64-bit shift makes it easy to find "the last
3460 bit shifted out", for the carry flag. */
3461 tcg_gen_andi_i32(s32, DREG(insn, 9), 63);
3462 tcg_gen_extu_i32_i64(s64, s32);
3463 tcg_gen_extu_i32_i64(t64, reg);
3465 /* Optimistically set V=0. Also used as a zero source below. */
3466 tcg_gen_movi_i32(QREG_CC_V, 0);
3468 tcg_gen_shl_i64(t64, t64, s64);
3470 if (opsize == OS_LONG) {
3471 tcg_gen_extr_i64_i32(QREG_CC_N, QREG_CC_C, t64);
3472 /* Note that C=0 if shift count is 0, and we get that for free. */
3474 TCGv zero = tcg_const_i32(0);
3475 tcg_gen_extrl_i64_i32(QREG_CC_N, t64);
3476 tcg_gen_shri_i32(QREG_CC_C, QREG_CC_N, bits);
3477 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3478 s32, zero, zero, QREG_CC_C);
3479 tcg_temp_free(zero);
3481 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3483 /* X = C, but only if the shift count was non-zero. */
3484 tcg_gen_movcond_i32(TCG_COND_NE, QREG_CC_X, s32, QREG_CC_V,
3485 QREG_CC_C, QREG_CC_X);
3487 /* M68000 sets V if the most significant bit is changed at
3488 * any time during the shift operation. Do this via creating
3489 * an extension of the sign bit, comparing, and discarding
3490 * the bits below the sign bit. I.e.
3491 * int64_t s = (intN_t)reg;
3492 * int64_t t = (int64_t)(intN_t)reg << count;
3493 * V = ((s ^ t) & (-1 << (bits - 1))) != 0
3495 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3496 TCGv_i64 tt = tcg_const_i64(32);
3497 /* if shift is greater than 32, use 32 */
3498 tcg_gen_movcond_i64(TCG_COND_GT, s64, s64, tt, tt, s64);
3499 tcg_temp_free_i64(tt);
3500 /* Sign extend the input to 64 bits; re-do the shift. */
3501 tcg_gen_ext_i32_i64(t64, reg);
3502 tcg_gen_shl_i64(s64, t64, s64);
3503 /* Clear all bits that are unchanged. */
3504 tcg_gen_xor_i64(t64, t64, s64);
3505 /* Ignore the bits below the sign bit. */
3506 tcg_gen_andi_i64(t64, t64, -1ULL << (bits - 1));
3507 /* If any bits remain set, we have overflow. */
3508 tcg_gen_setcondi_i64(TCG_COND_NE, t64, t64, 0);
3509 tcg_gen_extrl_i64_i32(QREG_CC_V, t64);
3510 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
3513 tcg_gen_shli_i64(t64, t64, 32);
3515 tcg_gen_shr_i64(t64, t64, s64);
3517 tcg_gen_sar_i64(t64, t64, s64);
3519 tcg_gen_extr_i64_i32(QREG_CC_C, QREG_CC_N, t64);
3521 /* Note that C=0 if shift count is 0, and we get that for free. */
3522 tcg_gen_shri_i32(QREG_CC_C, QREG_CC_C, 31);
3524 /* X = C, but only if the shift count was non-zero. */
3525 tcg_gen_movcond_i32(TCG_COND_NE, QREG_CC_X, s32, QREG_CC_V,
3526 QREG_CC_C, QREG_CC_X);
3528 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3529 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3532 tcg_temp_free_i64(s64);
3533 tcg_temp_free_i64(t64);
3535 /* Write back the result. */
3536 gen_partset_reg(opsize, DREG(insn, 0), QREG_CC_N);
3537 set_cc_op(s, CC_OP_FLAGS);
3540 DISAS_INSN(shift8_im)
3542 shift_im(s, insn, OS_BYTE);
3545 DISAS_INSN(shift16_im)
3547 shift_im(s, insn, OS_WORD);
3550 DISAS_INSN(shift_im)
3552 shift_im(s, insn, OS_LONG);
3555 DISAS_INSN(shift8_reg)
3557 shift_reg(s, insn, OS_BYTE);
3560 DISAS_INSN(shift16_reg)
3562 shift_reg(s, insn, OS_WORD);
3565 DISAS_INSN(shift_reg)
3567 shift_reg(s, insn, OS_LONG);
3570 DISAS_INSN(shift_mem)
3572 int logical = insn & 8;
3573 int left = insn & 0x100;
3577 SRC_EA(env, src, OS_WORD, !logical, &addr);
3578 tcg_gen_movi_i32(QREG_CC_V, 0);
3580 tcg_gen_shri_i32(QREG_CC_C, src, 15);
3581 tcg_gen_shli_i32(QREG_CC_N, src, 1);
3583 /* Note that ColdFire always clears V,
3584 while M68000 sets if the most significant bit is changed at
3585 any time during the shift operation */
3586 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3587 src = gen_extend(s, src, OS_WORD, 1);
3588 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, src);
3591 tcg_gen_mov_i32(QREG_CC_C, src);
3593 tcg_gen_shri_i32(QREG_CC_N, src, 1);
3595 tcg_gen_sari_i32(QREG_CC_N, src, 1);
3599 gen_ext(QREG_CC_N, QREG_CC_N, OS_WORD, 1);
3600 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3601 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3602 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
3604 DEST_EA(env, insn, OS_WORD, QREG_CC_N, &addr);
3605 set_cc_op(s, CC_OP_FLAGS);
3608 static void rotate(TCGv reg, TCGv shift, int left, int size)
3612 /* Replicate the 8-bit input so that a 32-bit rotate works. */
3613 tcg_gen_ext8u_i32(reg, reg);
3614 tcg_gen_muli_i32(reg, reg, 0x01010101);
3617 /* Replicate the 16-bit input so that a 32-bit rotate works. */
3618 tcg_gen_deposit_i32(reg, reg, reg, 16, 16);
3623 tcg_gen_rotl_i32(reg, reg, shift);
3625 tcg_gen_rotr_i32(reg, reg, shift);
3633 tcg_gen_ext8s_i32(reg, reg);
3636 tcg_gen_ext16s_i32(reg, reg);
3642 /* QREG_CC_X is not affected */
3644 tcg_gen_mov_i32(QREG_CC_N, reg);
3645 tcg_gen_mov_i32(QREG_CC_Z, reg);
3648 tcg_gen_andi_i32(QREG_CC_C, reg, 1);
3650 tcg_gen_shri_i32(QREG_CC_C, reg, 31);
3653 tcg_gen_movi_i32(QREG_CC_V, 0); /* always cleared */
3656 static void rotate_x_flags(TCGv reg, TCGv X, int size)
3660 tcg_gen_ext8s_i32(reg, reg);
3663 tcg_gen_ext16s_i32(reg, reg);
3668 tcg_gen_mov_i32(QREG_CC_N, reg);
3669 tcg_gen_mov_i32(QREG_CC_Z, reg);
3670 tcg_gen_mov_i32(QREG_CC_X, X);
3671 tcg_gen_mov_i32(QREG_CC_C, X);
3672 tcg_gen_movi_i32(QREG_CC_V, 0);
3675 /* Result of rotate_x() is valid if 0 <= shift <= size */
3676 static TCGv rotate_x(TCGv reg, TCGv shift, int left, int size)
3678 TCGv X, shl, shr, shx, sz, zero;
3680 sz = tcg_const_i32(size);
3682 shr = tcg_temp_new();
3683 shl = tcg_temp_new();
3684 shx = tcg_temp_new();
3686 tcg_gen_mov_i32(shl, shift); /* shl = shift */
3687 tcg_gen_movi_i32(shr, size + 1);
3688 tcg_gen_sub_i32(shr, shr, shift); /* shr = size + 1 - shift */
3689 tcg_gen_subi_i32(shx, shift, 1); /* shx = shift - 1 */
3690 /* shx = shx < 0 ? size : shx; */
3691 zero = tcg_const_i32(0);
3692 tcg_gen_movcond_i32(TCG_COND_LT, shx, shx, zero, sz, shx);
3693 tcg_temp_free(zero);
3695 tcg_gen_mov_i32(shr, shift); /* shr = shift */
3696 tcg_gen_movi_i32(shl, size + 1);
3697 tcg_gen_sub_i32(shl, shl, shift); /* shl = size + 1 - shift */
3698 tcg_gen_sub_i32(shx, sz, shift); /* shx = size - shift */
3701 /* reg = (reg << shl) | (reg >> shr) | (x << shx); */
3703 tcg_gen_shl_i32(shl, reg, shl);
3704 tcg_gen_shr_i32(shr, reg, shr);
3705 tcg_gen_or_i32(reg, shl, shr);
3708 tcg_gen_shl_i32(shx, QREG_CC_X, shx);
3709 tcg_gen_or_i32(reg, reg, shx);
3712 /* X = (reg >> size) & 1 */
3715 tcg_gen_shr_i32(X, reg, sz);
3716 tcg_gen_andi_i32(X, X, 1);
3722 /* Result of rotate32_x() is valid if 0 <= shift < 33 */
3723 static TCGv rotate32_x(TCGv reg, TCGv shift, int left)
3725 TCGv_i64 t0, shift64;
3726 TCGv X, lo, hi, zero;
3728 shift64 = tcg_temp_new_i64();
3729 tcg_gen_extu_i32_i64(shift64, shift);
3731 t0 = tcg_temp_new_i64();
3734 lo = tcg_temp_new();
3735 hi = tcg_temp_new();
3738 /* create [reg:X:..] */
3740 tcg_gen_shli_i32(lo, QREG_CC_X, 31);
3741 tcg_gen_concat_i32_i64(t0, lo, reg);
3745 tcg_gen_rotl_i64(t0, t0, shift64);
3746 tcg_temp_free_i64(shift64);
3748 /* result is [reg:..:reg:X] */
3750 tcg_gen_extr_i64_i32(lo, hi, t0);
3751 tcg_gen_andi_i32(X, lo, 1);
3753 tcg_gen_shri_i32(lo, lo, 1);
3755 /* create [..:X:reg] */
3757 tcg_gen_concat_i32_i64(t0, reg, QREG_CC_X);
3759 tcg_gen_rotr_i64(t0, t0, shift64);
3760 tcg_temp_free_i64(shift64);
3762 /* result is value: [X:reg:..:reg] */
3764 tcg_gen_extr_i64_i32(lo, hi, t0);
3768 tcg_gen_shri_i32(X, hi, 31);
3770 /* extract result */
3772 tcg_gen_shli_i32(hi, hi, 1);
3774 tcg_temp_free_i64(t0);
3775 tcg_gen_or_i32(lo, lo, hi);
3778 /* if shift == 0, register and X are not affected */
3780 zero = tcg_const_i32(0);
3781 tcg_gen_movcond_i32(TCG_COND_EQ, X, shift, zero, QREG_CC_X, X);
3782 tcg_gen_movcond_i32(TCG_COND_EQ, reg, shift, zero, reg, lo);
3783 tcg_temp_free(zero);
3789 DISAS_INSN(rotate_im)
3793 int left = (insn & 0x100);
3795 tmp = (insn >> 9) & 7;
3800 shift = tcg_const_i32(tmp);
3802 rotate(DREG(insn, 0), shift, left, 32);
3804 TCGv X = rotate32_x(DREG(insn, 0), shift, left);
3805 rotate_x_flags(DREG(insn, 0), X, 32);
3808 tcg_temp_free(shift);
3810 set_cc_op(s, CC_OP_FLAGS);
3813 DISAS_INSN(rotate8_im)
3815 int left = (insn & 0x100);
3820 reg = gen_extend(s, DREG(insn, 0), OS_BYTE, 0);
3822 tmp = (insn >> 9) & 7;
3827 shift = tcg_const_i32(tmp);
3829 rotate(reg, shift, left, 8);
3831 TCGv X = rotate_x(reg, shift, left, 8);
3832 rotate_x_flags(reg, X, 8);
3835 tcg_temp_free(shift);
3836 gen_partset_reg(OS_BYTE, DREG(insn, 0), reg);
3837 set_cc_op(s, CC_OP_FLAGS);
3840 DISAS_INSN(rotate16_im)
3842 int left = (insn & 0x100);
3847 reg = gen_extend(s, DREG(insn, 0), OS_WORD, 0);
3848 tmp = (insn >> 9) & 7;
3853 shift = tcg_const_i32(tmp);
3855 rotate(reg, shift, left, 16);
3857 TCGv X = rotate_x(reg, shift, left, 16);
3858 rotate_x_flags(reg, X, 16);
3861 tcg_temp_free(shift);
3862 gen_partset_reg(OS_WORD, DREG(insn, 0), reg);
3863 set_cc_op(s, CC_OP_FLAGS);
3866 DISAS_INSN(rotate_reg)
3871 int left = (insn & 0x100);
3873 reg = DREG(insn, 0);
3874 src = DREG(insn, 9);
3875 /* shift in [0..63] */
3876 t0 = tcg_temp_new();
3877 tcg_gen_andi_i32(t0, src, 63);
3878 t1 = tcg_temp_new_i32();
3880 tcg_gen_andi_i32(t1, src, 31);
3881 rotate(reg, t1, left, 32);
3882 /* if shift == 0, clear C */
3883 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3884 t0, QREG_CC_V /* 0 */,
3885 QREG_CC_V /* 0 */, QREG_CC_C);
3889 tcg_gen_movi_i32(t1, 33);
3890 tcg_gen_remu_i32(t1, t0, t1);
3891 X = rotate32_x(DREG(insn, 0), t1, left);
3892 rotate_x_flags(DREG(insn, 0), X, 32);
3897 set_cc_op(s, CC_OP_FLAGS);
3900 DISAS_INSN(rotate8_reg)
3905 int left = (insn & 0x100);
3907 reg = gen_extend(s, DREG(insn, 0), OS_BYTE, 0);
3908 src = DREG(insn, 9);
3909 /* shift in [0..63] */
3910 t0 = tcg_temp_new_i32();
3911 tcg_gen_andi_i32(t0, src, 63);
3912 t1 = tcg_temp_new_i32();
3914 tcg_gen_andi_i32(t1, src, 7);
3915 rotate(reg, t1, left, 8);
3916 /* if shift == 0, clear C */
3917 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3918 t0, QREG_CC_V /* 0 */,
3919 QREG_CC_V /* 0 */, QREG_CC_C);
3923 tcg_gen_movi_i32(t1, 9);
3924 tcg_gen_remu_i32(t1, t0, t1);
3925 X = rotate_x(reg, t1, left, 8);
3926 rotate_x_flags(reg, X, 8);
3931 gen_partset_reg(OS_BYTE, DREG(insn, 0), reg);
3932 set_cc_op(s, CC_OP_FLAGS);
3935 DISAS_INSN(rotate16_reg)
3940 int left = (insn & 0x100);
3942 reg = gen_extend(s, DREG(insn, 0), OS_WORD, 0);
3943 src = DREG(insn, 9);
3944 /* shift in [0..63] */
3945 t0 = tcg_temp_new_i32();
3946 tcg_gen_andi_i32(t0, src, 63);
3947 t1 = tcg_temp_new_i32();
3949 tcg_gen_andi_i32(t1, src, 15);
3950 rotate(reg, t1, left, 16);
3951 /* if shift == 0, clear C */
3952 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3953 t0, QREG_CC_V /* 0 */,
3954 QREG_CC_V /* 0 */, QREG_CC_C);
3958 tcg_gen_movi_i32(t1, 17);
3959 tcg_gen_remu_i32(t1, t0, t1);
3960 X = rotate_x(reg, t1, left, 16);
3961 rotate_x_flags(reg, X, 16);
3966 gen_partset_reg(OS_WORD, DREG(insn, 0), reg);
3967 set_cc_op(s, CC_OP_FLAGS);
3970 DISAS_INSN(rotate_mem)
3975 int left = (insn & 0x100);
3977 SRC_EA(env, src, OS_WORD, 0, &addr);
3979 shift = tcg_const_i32(1);
3980 if (insn & 0x0200) {
3981 rotate(src, shift, left, 16);
3983 TCGv X = rotate_x(src, shift, left, 16);
3984 rotate_x_flags(src, X, 16);
3987 tcg_temp_free(shift);
3988 DEST_EA(env, insn, OS_WORD, src, &addr);
3989 set_cc_op(s, CC_OP_FLAGS);
3992 DISAS_INSN(bfext_reg)
3994 int ext = read_im16(env, s);
3995 int is_sign = insn & 0x200;
3996 TCGv src = DREG(insn, 0);
3997 TCGv dst = DREG(ext, 12);
3998 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
3999 int ofs = extract32(ext, 6, 5); /* big bit-endian */
4000 int pos = 32 - ofs - len; /* little bit-endian */
4001 TCGv tmp = tcg_temp_new();
4004 /* In general, we're going to rotate the field so that it's at the
4005 top of the word and then right-shift by the complement of the
4006 width to extend the field. */
4008 /* Variable width. */
4010 /* Variable offset. */
4011 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
4012 tcg_gen_rotl_i32(tmp, src, tmp);
4014 tcg_gen_rotli_i32(tmp, src, ofs);
4017 shift = tcg_temp_new();
4018 tcg_gen_neg_i32(shift, DREG(ext, 0));
4019 tcg_gen_andi_i32(shift, shift, 31);
4020 tcg_gen_sar_i32(QREG_CC_N, tmp, shift);
4022 tcg_gen_mov_i32(dst, QREG_CC_N);
4024 tcg_gen_shr_i32(dst, tmp, shift);
4026 tcg_temp_free(shift);
4028 /* Immediate width. */
4030 /* Variable offset */
4031 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
4032 tcg_gen_rotl_i32(tmp, src, tmp);
4036 /* Immediate offset. If the field doesn't wrap around the
4037 end of the word, rely on (s)extract completely. */
4039 tcg_gen_rotli_i32(tmp, src, ofs);
4045 tcg_gen_sextract_i32(QREG_CC_N, src, pos, len);
4047 tcg_gen_mov_i32(dst, QREG_CC_N);
4049 tcg_gen_extract_i32(dst, src, pos, len);
4054 set_cc_op(s, CC_OP_LOGIC);
4057 DISAS_INSN(bfext_mem)
4059 int ext = read_im16(env, s);
4060 int is_sign = insn & 0x200;
4061 TCGv dest = DREG(ext, 12);
4062 TCGv addr, len, ofs;
4064 addr = gen_lea(env, s, insn, OS_UNSIZED);
4065 if (IS_NULL_QREG(addr)) {
4073 len = tcg_const_i32(extract32(ext, 0, 5));
4078 ofs = tcg_const_i32(extract32(ext, 6, 5));
4082 gen_helper_bfexts_mem(dest, cpu_env, addr, ofs, len);
4083 tcg_gen_mov_i32(QREG_CC_N, dest);
4085 TCGv_i64 tmp = tcg_temp_new_i64();
4086 gen_helper_bfextu_mem(tmp, cpu_env, addr, ofs, len);
4087 tcg_gen_extr_i64_i32(dest, QREG_CC_N, tmp);
4088 tcg_temp_free_i64(tmp);
4090 set_cc_op(s, CC_OP_LOGIC);
4092 if (!(ext & 0x20)) {
4095 if (!(ext & 0x800)) {
4100 DISAS_INSN(bfop_reg)
4102 int ext = read_im16(env, s);
4103 TCGv src = DREG(insn, 0);
4104 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
4105 int ofs = extract32(ext, 6, 5); /* big bit-endian */
4106 TCGv mask, tofs, tlen;
4110 if ((insn & 0x0f00) == 0x0d00) { /* bfffo */
4111 tofs = tcg_temp_new();
4112 tlen = tcg_temp_new();
4115 if ((ext & 0x820) == 0) {
4116 /* Immediate width and offset. */
4117 uint32_t maski = 0x7fffffffu >> (len - 1);
4118 if (ofs + len <= 32) {
4119 tcg_gen_shli_i32(QREG_CC_N, src, ofs);
4121 tcg_gen_rotli_i32(QREG_CC_N, src, ofs);
4123 tcg_gen_andi_i32(QREG_CC_N, QREG_CC_N, ~maski);
4124 mask = tcg_const_i32(ror32(maski, ofs));
4126 tcg_gen_movi_i32(tofs, ofs);
4127 tcg_gen_movi_i32(tlen, len);
4130 TCGv tmp = tcg_temp_new();
4132 /* Variable width */
4133 tcg_gen_subi_i32(tmp, DREG(ext, 0), 1);
4134 tcg_gen_andi_i32(tmp, tmp, 31);
4135 mask = tcg_const_i32(0x7fffffffu);
4136 tcg_gen_shr_i32(mask, mask, tmp);
4138 tcg_gen_addi_i32(tlen, tmp, 1);
4141 /* Immediate width */
4142 mask = tcg_const_i32(0x7fffffffu >> (len - 1));
4144 tcg_gen_movi_i32(tlen, len);
4148 /* Variable offset */
4149 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
4150 tcg_gen_rotl_i32(QREG_CC_N, src, tmp);
4151 tcg_gen_andc_i32(QREG_CC_N, QREG_CC_N, mask);
4152 tcg_gen_rotr_i32(mask, mask, tmp);
4154 tcg_gen_mov_i32(tofs, tmp);
4157 /* Immediate offset (and variable width) */
4158 tcg_gen_rotli_i32(QREG_CC_N, src, ofs);
4159 tcg_gen_andc_i32(QREG_CC_N, QREG_CC_N, mask);
4160 tcg_gen_rotri_i32(mask, mask, ofs);
4162 tcg_gen_movi_i32(tofs, ofs);
4167 set_cc_op(s, CC_OP_LOGIC);
4169 switch (insn & 0x0f00) {
4170 case 0x0a00: /* bfchg */
4171 tcg_gen_eqv_i32(src, src, mask);
4173 case 0x0c00: /* bfclr */
4174 tcg_gen_and_i32(src, src, mask);
4176 case 0x0d00: /* bfffo */
4177 gen_helper_bfffo_reg(DREG(ext, 12), QREG_CC_N, tofs, tlen);
4178 tcg_temp_free(tlen);
4179 tcg_temp_free(tofs);
4181 case 0x0e00: /* bfset */
4182 tcg_gen_orc_i32(src, src, mask);
4184 case 0x0800: /* bftst */
4185 /* flags already set; no other work to do. */
4188 g_assert_not_reached();
4190 tcg_temp_free(mask);
4193 DISAS_INSN(bfop_mem)
4195 int ext = read_im16(env, s);
4196 TCGv addr, len, ofs;
4199 addr = gen_lea(env, s, insn, OS_UNSIZED);
4200 if (IS_NULL_QREG(addr)) {
4208 len = tcg_const_i32(extract32(ext, 0, 5));
4213 ofs = tcg_const_i32(extract32(ext, 6, 5));
4216 switch (insn & 0x0f00) {
4217 case 0x0a00: /* bfchg */
4218 gen_helper_bfchg_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4220 case 0x0c00: /* bfclr */
4221 gen_helper_bfclr_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4223 case 0x0d00: /* bfffo */
4224 t64 = tcg_temp_new_i64();
4225 gen_helper_bfffo_mem(t64, cpu_env, addr, ofs, len);
4226 tcg_gen_extr_i64_i32(DREG(ext, 12), QREG_CC_N, t64);
4227 tcg_temp_free_i64(t64);
4229 case 0x0e00: /* bfset */
4230 gen_helper_bfset_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4232 case 0x0800: /* bftst */
4233 gen_helper_bfexts_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4236 g_assert_not_reached();
4238 set_cc_op(s, CC_OP_LOGIC);
4240 if (!(ext & 0x20)) {
4243 if (!(ext & 0x800)) {
4248 DISAS_INSN(bfins_reg)
4250 int ext = read_im16(env, s);
4251 TCGv dst = DREG(insn, 0);
4252 TCGv src = DREG(ext, 12);
4253 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
4254 int ofs = extract32(ext, 6, 5); /* big bit-endian */
4255 int pos = 32 - ofs - len; /* little bit-endian */
4258 tmp = tcg_temp_new();
4261 /* Variable width */
4262 tcg_gen_neg_i32(tmp, DREG(ext, 0));
4263 tcg_gen_andi_i32(tmp, tmp, 31);
4264 tcg_gen_shl_i32(QREG_CC_N, src, tmp);
4266 /* Immediate width */
4267 tcg_gen_shli_i32(QREG_CC_N, src, 32 - len);
4269 set_cc_op(s, CC_OP_LOGIC);
4271 /* Immediate width and offset */
4272 if ((ext & 0x820) == 0) {
4273 /* Check for suitability for deposit. */
4275 tcg_gen_deposit_i32(dst, dst, src, pos, len);
4277 uint32_t maski = -2U << (len - 1);
4278 uint32_t roti = (ofs + len) & 31;
4279 tcg_gen_andi_i32(tmp, src, ~maski);
4280 tcg_gen_rotri_i32(tmp, tmp, roti);
4281 tcg_gen_andi_i32(dst, dst, ror32(maski, roti));
4282 tcg_gen_or_i32(dst, dst, tmp);
4285 TCGv mask = tcg_temp_new();
4286 TCGv rot = tcg_temp_new();
4289 /* Variable width */
4290 tcg_gen_subi_i32(rot, DREG(ext, 0), 1);
4291 tcg_gen_andi_i32(rot, rot, 31);
4292 tcg_gen_movi_i32(mask, -2);
4293 tcg_gen_shl_i32(mask, mask, rot);
4294 tcg_gen_mov_i32(rot, DREG(ext, 0));
4295 tcg_gen_andc_i32(tmp, src, mask);
4297 /* Immediate width (variable offset) */
4298 uint32_t maski = -2U << (len - 1);
4299 tcg_gen_andi_i32(tmp, src, ~maski);
4300 tcg_gen_movi_i32(mask, maski);
4301 tcg_gen_movi_i32(rot, len & 31);
4304 /* Variable offset */
4305 tcg_gen_add_i32(rot, rot, DREG(ext, 6));
4307 /* Immediate offset (variable width) */
4308 tcg_gen_addi_i32(rot, rot, ofs);
4310 tcg_gen_andi_i32(rot, rot, 31);
4311 tcg_gen_rotr_i32(mask, mask, rot);
4312 tcg_gen_rotr_i32(tmp, tmp, rot);
4313 tcg_gen_and_i32(dst, dst, mask);
4314 tcg_gen_or_i32(dst, dst, tmp);
4317 tcg_temp_free(mask);
4322 DISAS_INSN(bfins_mem)
4324 int ext = read_im16(env, s);
4325 TCGv src = DREG(ext, 12);
4326 TCGv addr, len, ofs;
4328 addr = gen_lea(env, s, insn, OS_UNSIZED);
4329 if (IS_NULL_QREG(addr)) {
4337 len = tcg_const_i32(extract32(ext, 0, 5));
4342 ofs = tcg_const_i32(extract32(ext, 6, 5));
4345 gen_helper_bfins_mem(QREG_CC_N, cpu_env, addr, src, ofs, len);
4346 set_cc_op(s, CC_OP_LOGIC);
4348 if (!(ext & 0x20)) {
4351 if (!(ext & 0x800)) {
4359 reg = DREG(insn, 0);
4360 gen_logic_cc(s, reg, OS_LONG);
4361 gen_helper_ff1(reg, reg);
4369 switch ((insn >> 7) & 3) {
4374 if (m68k_feature(env, M68K_FEATURE_CHK2)) {
4380 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4383 SRC_EA(env, src, opsize, 1, NULL);
4384 reg = gen_extend(s, DREG(insn, 9), opsize, 1);
4387 gen_helper_chk(cpu_env, reg, src);
4393 TCGv addr1, addr2, bound1, bound2, reg;
4396 switch ((insn >> 9) & 3) {
4407 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4411 ext = read_im16(env, s);
4412 if ((ext & 0x0800) == 0) {
4413 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4417 addr1 = gen_lea(env, s, insn, OS_UNSIZED);
4418 addr2 = tcg_temp_new();
4419 tcg_gen_addi_i32(addr2, addr1, opsize_bytes(opsize));
4421 bound1 = gen_load(s, opsize, addr1, 1, IS_USER(s));
4422 tcg_temp_free(addr1);
4423 bound2 = gen_load(s, opsize, addr2, 1, IS_USER(s));
4424 tcg_temp_free(addr2);
4426 reg = tcg_temp_new();
4428 tcg_gen_mov_i32(reg, AREG(ext, 12));
4430 gen_ext(reg, DREG(ext, 12), opsize, 1);
4434 gen_helper_chk2(cpu_env, reg, bound1, bound2);
4436 tcg_temp_free(bound1);
4437 tcg_temp_free(bound2);
4440 static void m68k_copy_line(TCGv dst, TCGv src, int index)
4445 addr = tcg_temp_new();
4447 t0 = tcg_temp_new_i64();
4448 t1 = tcg_temp_new_i64();
4450 tcg_gen_andi_i32(addr, src, ~15);
4451 tcg_gen_qemu_ld64(t0, addr, index);
4452 tcg_gen_addi_i32(addr, addr, 8);
4453 tcg_gen_qemu_ld64(t1, addr, index);
4455 tcg_gen_andi_i32(addr, dst, ~15);
4456 tcg_gen_qemu_st64(t0, addr, index);
4457 tcg_gen_addi_i32(addr, addr, 8);
4458 tcg_gen_qemu_st64(t1, addr, index);
4460 tcg_temp_free_i64(t0);
4461 tcg_temp_free_i64(t1);
4462 tcg_temp_free(addr);
4465 DISAS_INSN(move16_reg)
4467 int index = IS_USER(s);
4471 ext = read_im16(env, s);
4472 if ((ext & (1 << 15)) == 0) {
4473 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4476 m68k_copy_line(AREG(ext, 12), AREG(insn, 0), index);
4478 /* Ax can be Ay, so save Ay before incrementing Ax */
4479 tmp = tcg_temp_new();
4480 tcg_gen_mov_i32(tmp, AREG(ext, 12));
4481 tcg_gen_addi_i32(AREG(insn, 0), AREG(insn, 0), 16);
4482 tcg_gen_addi_i32(AREG(ext, 12), tmp, 16);
4486 DISAS_INSN(move16_mem)
4488 int index = IS_USER(s);
4491 reg = AREG(insn, 0);
4492 addr = tcg_const_i32(read_im32(env, s));
4494 if ((insn >> 3) & 1) {
4495 /* MOVE16 (xxx).L, (Ay) */
4496 m68k_copy_line(reg, addr, index);
4498 /* MOVE16 (Ay), (xxx).L */
4499 m68k_copy_line(addr, reg, index);
4502 tcg_temp_free(addr);
4504 if (((insn >> 3) & 2) == 0) {
4506 tcg_gen_addi_i32(reg, reg, 16);
4516 ext = read_im16(env, s);
4517 if (ext != 0x46FC) {
4518 gen_exception(s, addr, EXCP_UNSUPPORTED);
4521 ext = read_im16(env, s);
4522 if (IS_USER(s) || (ext & SR_S) == 0) {
4523 gen_exception(s, addr, EXCP_PRIVILEGE);
4526 gen_push(s, gen_get_sr(s));
4527 gen_set_sr_im(s, ext, 0);
4530 DISAS_INSN(move_from_sr)
4534 if (IS_USER(s) && !m68k_feature(env, M68K_FEATURE_M68000)) {
4535 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4539 DEST_EA(env, insn, OS_WORD, sr, NULL);
4542 #if defined(CONFIG_SOFTMMU)
4552 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4556 ext = read_im16(env, s);
4558 opsize = insn_opsize(insn);
4561 /* address register */
4562 reg = AREG(ext, 12);
4566 reg = DREG(ext, 12);
4570 addr = gen_lea(env, s, insn, opsize);
4571 if (IS_NULL_QREG(addr)) {
4577 /* from reg to ea */
4578 gen_store(s, opsize, addr, reg, DFC_INDEX(s));
4580 /* from ea to reg */
4581 TCGv tmp = gen_load(s, opsize, addr, 0, SFC_INDEX(s));
4583 gen_ext(reg, tmp, opsize, 1);
4585 gen_partset_reg(opsize, reg, tmp);
4589 switch (extract32(insn, 3, 3)) {
4590 case 3: /* Indirect postincrement. */
4591 tcg_gen_addi_i32(AREG(insn, 0), addr,
4592 REG(insn, 0) == 7 && opsize == OS_BYTE
4594 : opsize_bytes(opsize));
4596 case 4: /* Indirect predecrememnt. */
4597 tcg_gen_mov_i32(AREG(insn, 0), addr);
4602 DISAS_INSN(move_to_sr)
4605 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4608 gen_move_to_sr(env, s, insn, false);
4612 DISAS_INSN(move_from_usp)
4615 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4618 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
4619 offsetof(CPUM68KState, sp[M68K_USP]));
4622 DISAS_INSN(move_to_usp)
4625 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4628 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
4629 offsetof(CPUM68KState, sp[M68K_USP]));
4635 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4639 gen_exception(s, s->pc, EXCP_HALT_INSN);
4647 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4651 ext = read_im16(env, s);
4653 gen_set_sr_im(s, ext, 0);
4654 tcg_gen_movi_i32(cpu_halted, 1);
4655 gen_exception(s, s->pc, EXCP_HLT);
4661 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4664 gen_exception(s, s->insn_pc, EXCP_RTE);
4667 DISAS_INSN(cf_movec)
4673 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4677 ext = read_im16(env, s);
4680 reg = AREG(ext, 12);
4682 reg = DREG(ext, 12);
4684 gen_helper_cf_movec_to(cpu_env, tcg_const_i32(ext & 0xfff), reg);
4688 DISAS_INSN(m68k_movec)
4694 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4698 ext = read_im16(env, s);
4701 reg = AREG(ext, 12);
4703 reg = DREG(ext, 12);
4706 gen_helper_m68k_movec_to(cpu_env, tcg_const_i32(ext & 0xfff), reg);
4708 gen_helper_m68k_movec_from(reg, cpu_env, tcg_const_i32(ext & 0xfff));
4716 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4719 /* ICache fetch. Implement as no-op. */
4725 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4728 /* Cache push/invalidate. Implement as no-op. */
4734 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4737 /* Cache push/invalidate. Implement as no-op. */
4743 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4746 /* Invalidate cache line. Implement as no-op. */
4749 #if defined(CONFIG_SOFTMMU)
4755 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4759 opmode = tcg_const_i32((insn >> 3) & 3);
4760 gen_helper_pflush(cpu_env, AREG(insn, 0), opmode);
4761 tcg_temp_free(opmode);
4769 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4772 is_read = tcg_const_i32((insn >> 5) & 1);
4773 gen_helper_ptest(cpu_env, AREG(insn, 0), is_read);
4774 tcg_temp_free(is_read);
4780 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4785 M68kCPU *cpu = m68k_env_get_cpu(env);
4788 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
4791 /* TODO: Implement wdebug. */
4792 cpu_abort(CPU(cpu), "WDEBUG not implemented");
4798 gen_exception(s, s->insn_pc, EXCP_TRAP0 + (insn & 0xf));
4801 static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
4805 tcg_gen_movi_i32(res, 0);
4808 tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpsr));
4811 tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpcr));
4816 static void gen_store_fcr(DisasContext *s, TCGv val, int reg)
4822 tcg_gen_st_i32(val, cpu_env, offsetof(CPUM68KState, fpsr));
4825 gen_helper_set_fpcr(cpu_env, val);
4830 static void gen_qemu_store_fcr(DisasContext *s, TCGv addr, int reg)
4832 int index = IS_USER(s);
4835 tmp = tcg_temp_new();
4836 gen_load_fcr(s, tmp, reg);
4837 tcg_gen_qemu_st32(tmp, addr, index);
4841 static void gen_qemu_load_fcr(DisasContext *s, TCGv addr, int reg)
4843 int index = IS_USER(s);
4846 tmp = tcg_temp_new();
4847 tcg_gen_qemu_ld32u(tmp, addr, index);
4848 gen_store_fcr(s, tmp, reg);
4853 static void gen_op_fmove_fcr(CPUM68KState *env, DisasContext *s,
4854 uint32_t insn, uint32_t ext)
4856 int mask = (ext >> 10) & 7;
4857 int is_write = (ext >> 13) & 1;
4858 int mode = extract32(insn, 3, 3);
4864 if (mask != M68K_FPIAR && mask != M68K_FPSR && mask != M68K_FPCR) {
4865 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4869 gen_load_fcr(s, DREG(insn, 0), mask);
4871 gen_store_fcr(s, DREG(insn, 0), mask);
4874 case 1: /* An, only with FPIAR */
4875 if (mask != M68K_FPIAR) {
4876 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4880 gen_load_fcr(s, AREG(insn, 0), mask);
4882 gen_store_fcr(s, AREG(insn, 0), mask);
4889 tmp = gen_lea(env, s, insn, OS_LONG);
4890 if (IS_NULL_QREG(tmp)) {
4895 addr = tcg_temp_new();
4896 tcg_gen_mov_i32(addr, tmp);
4900 * 0b100 Floating-Point Control Register
4901 * 0b010 Floating-Point Status Register
4902 * 0b001 Floating-Point Instruction Address Register
4906 if (is_write && mode == 4) {
4907 for (i = 2; i >= 0; i--, mask >>= 1) {
4909 gen_qemu_store_fcr(s, addr, 1 << i);
4911 tcg_gen_subi_i32(addr, addr, opsize_bytes(OS_LONG));
4915 tcg_gen_mov_i32(AREG(insn, 0), addr);
4917 for (i = 0; i < 3; i++, mask >>= 1) {
4920 gen_qemu_store_fcr(s, addr, 1 << i);
4922 gen_qemu_load_fcr(s, addr, 1 << i);
4924 if (mask != 1 || mode == 3) {
4925 tcg_gen_addi_i32(addr, addr, opsize_bytes(OS_LONG));
4930 tcg_gen_mov_i32(AREG(insn, 0), addr);
4933 tcg_temp_free_i32(addr);
4936 static void gen_op_fmovem(CPUM68KState *env, DisasContext *s,
4937 uint32_t insn, uint32_t ext)
4941 int mode = (ext >> 11) & 0x3;
4942 int is_load = ((ext & 0x2000) == 0);
4944 if (m68k_feature(s->env, M68K_FEATURE_FPU)) {
4945 opsize = OS_EXTENDED;
4947 opsize = OS_DOUBLE; /* FIXME */
4950 addr = gen_lea(env, s, insn, opsize);
4951 if (IS_NULL_QREG(addr)) {
4956 tmp = tcg_temp_new();
4958 /* Dynamic register list */
4959 tcg_gen_ext8u_i32(tmp, DREG(ext, 4));
4961 /* Static register list */
4962 tcg_gen_movi_i32(tmp, ext & 0xff);
4965 if (!is_load && (mode & 2) == 0) {
4966 /* predecrement addressing mode
4967 * only available to store register to memory
4969 if (opsize == OS_EXTENDED) {
4970 gen_helper_fmovemx_st_predec(tmp, cpu_env, addr, tmp);
4972 gen_helper_fmovemd_st_predec(tmp, cpu_env, addr, tmp);
4975 /* postincrement addressing mode */
4976 if (opsize == OS_EXTENDED) {
4978 gen_helper_fmovemx_ld_postinc(tmp, cpu_env, addr, tmp);
4980 gen_helper_fmovemx_st_postinc(tmp, cpu_env, addr, tmp);
4984 gen_helper_fmovemd_ld_postinc(tmp, cpu_env, addr, tmp);
4986 gen_helper_fmovemd_st_postinc(tmp, cpu_env, addr, tmp);
4990 if ((insn & 070) == 030 || (insn & 070) == 040) {
4991 tcg_gen_mov_i32(AREG(insn, 0), tmp);
4996 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
4997 immediately before the next FP instruction is executed. */
5003 TCGv_ptr cpu_src, cpu_dest;
5005 ext = read_im16(env, s);
5006 opmode = ext & 0x7f;
5007 switch ((ext >> 13) & 7) {
5013 if (insn == 0xf200 && (ext & 0xfc00) == 0x5c00) {
5015 TCGv rom_offset = tcg_const_i32(opmode);
5016 cpu_dest = gen_fp_ptr(REG(ext, 7));
5017 gen_helper_fconst(cpu_env, cpu_dest, rom_offset);
5018 tcg_temp_free_ptr(cpu_dest);
5019 tcg_temp_free(rom_offset);
5023 case 3: /* fmove out */
5024 cpu_src = gen_fp_ptr(REG(ext, 7));
5025 opsize = ext_opsize(ext, 10);
5026 if (gen_ea_fp(env, s, insn, opsize, cpu_src,
5027 EA_STORE, IS_USER(s)) == -1) {
5030 gen_helper_ftst(cpu_env, cpu_src);
5031 tcg_temp_free_ptr(cpu_src);
5033 case 4: /* fmove to control register. */
5034 case 5: /* fmove from control register. */
5035 gen_op_fmove_fcr(env, s, insn, ext);
5037 case 6: /* fmovem */
5039 if ((ext & 0x1000) == 0 && !m68k_feature(s->env, M68K_FEATURE_FPU)) {
5042 gen_op_fmovem(env, s, insn, ext);
5045 if (ext & (1 << 14)) {
5046 /* Source effective address. */
5047 opsize = ext_opsize(ext, 10);
5048 cpu_src = gen_fp_result_ptr();
5049 if (gen_ea_fp(env, s, insn, opsize, cpu_src,
5050 EA_LOADS, IS_USER(s)) == -1) {
5055 /* Source register. */
5056 opsize = OS_EXTENDED;
5057 cpu_src = gen_fp_ptr(REG(ext, 10));
5059 cpu_dest = gen_fp_ptr(REG(ext, 7));
5062 gen_fp_move(cpu_dest, cpu_src);
5064 case 0x40: /* fsmove */
5065 gen_helper_fsround(cpu_env, cpu_dest, cpu_src);
5067 case 0x44: /* fdmove */
5068 gen_helper_fdround(cpu_env, cpu_dest, cpu_src);
5071 gen_helper_firound(cpu_env, cpu_dest, cpu_src);
5074 gen_helper_fsinh(cpu_env, cpu_dest, cpu_src);
5076 case 3: /* fintrz */
5077 gen_helper_fitrunc(cpu_env, cpu_dest, cpu_src);
5080 gen_helper_fsqrt(cpu_env, cpu_dest, cpu_src);
5082 case 0x41: /* fssqrt */
5083 gen_helper_fssqrt(cpu_env, cpu_dest, cpu_src);
5085 case 0x45: /* fdsqrt */
5086 gen_helper_fdsqrt(cpu_env, cpu_dest, cpu_src);
5088 case 0x06: /* flognp1 */
5089 gen_helper_flognp1(cpu_env, cpu_dest, cpu_src);
5091 case 0x09: /* ftanh */
5092 gen_helper_ftanh(cpu_env, cpu_dest, cpu_src);
5094 case 0x0a: /* fatan */
5095 gen_helper_fatan(cpu_env, cpu_dest, cpu_src);
5097 case 0x0c: /* fasin */
5098 gen_helper_fasin(cpu_env, cpu_dest, cpu_src);
5100 case 0x0d: /* fatanh */
5101 gen_helper_fatanh(cpu_env, cpu_dest, cpu_src);
5103 case 0x0e: /* fsin */
5104 gen_helper_fsin(cpu_env, cpu_dest, cpu_src);
5106 case 0x0f: /* ftan */
5107 gen_helper_ftan(cpu_env, cpu_dest, cpu_src);
5109 case 0x10: /* fetox */
5110 gen_helper_fetox(cpu_env, cpu_dest, cpu_src);
5112 case 0x11: /* ftwotox */
5113 gen_helper_ftwotox(cpu_env, cpu_dest, cpu_src);
5115 case 0x12: /* ftentox */
5116 gen_helper_ftentox(cpu_env, cpu_dest, cpu_src);
5118 case 0x14: /* flogn */
5119 gen_helper_flogn(cpu_env, cpu_dest, cpu_src);
5121 case 0x15: /* flog10 */
5122 gen_helper_flog10(cpu_env, cpu_dest, cpu_src);
5124 case 0x16: /* flog2 */
5125 gen_helper_flog2(cpu_env, cpu_dest, cpu_src);
5127 case 0x18: /* fabs */
5128 gen_helper_fabs(cpu_env, cpu_dest, cpu_src);
5130 case 0x58: /* fsabs */
5131 gen_helper_fsabs(cpu_env, cpu_dest, cpu_src);
5133 case 0x5c: /* fdabs */
5134 gen_helper_fdabs(cpu_env, cpu_dest, cpu_src);
5136 case 0x19: /* fcosh */
5137 gen_helper_fcosh(cpu_env, cpu_dest, cpu_src);
5139 case 0x1a: /* fneg */
5140 gen_helper_fneg(cpu_env, cpu_dest, cpu_src);
5142 case 0x5a: /* fsneg */
5143 gen_helper_fsneg(cpu_env, cpu_dest, cpu_src);
5145 case 0x5e: /* fdneg */
5146 gen_helper_fdneg(cpu_env, cpu_dest, cpu_src);
5148 case 0x1c: /* facos */
5149 gen_helper_facos(cpu_env, cpu_dest, cpu_src);
5151 case 0x1d: /* fcos */
5152 gen_helper_fcos(cpu_env, cpu_dest, cpu_src);
5154 case 0x1e: /* fgetexp */
5155 gen_helper_fgetexp(cpu_env, cpu_dest, cpu_src);
5157 case 0x1f: /* fgetman */
5158 gen_helper_fgetman(cpu_env, cpu_dest, cpu_src);
5160 case 0x20: /* fdiv */
5161 gen_helper_fdiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
5163 case 0x60: /* fsdiv */
5164 gen_helper_fsdiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
5166 case 0x64: /* fddiv */
5167 gen_helper_fddiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
5169 case 0x21: /* fmod */
5170 gen_helper_fmod(cpu_env, cpu_dest, cpu_src, cpu_dest);
5172 case 0x22: /* fadd */
5173 gen_helper_fadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
5175 case 0x62: /* fsadd */
5176 gen_helper_fsadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
5178 case 0x66: /* fdadd */
5179 gen_helper_fdadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
5181 case 0x23: /* fmul */
5182 gen_helper_fmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
5184 case 0x63: /* fsmul */
5185 gen_helper_fsmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
5187 case 0x67: /* fdmul */
5188 gen_helper_fdmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
5190 case 0x24: /* fsgldiv */
5191 gen_helper_fsgldiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
5193 case 0x25: /* frem */
5194 gen_helper_frem(cpu_env, cpu_dest, cpu_src, cpu_dest);
5196 case 0x26: /* fscale */
5197 gen_helper_fscale(cpu_env, cpu_dest, cpu_src, cpu_dest);
5199 case 0x27: /* fsglmul */
5200 gen_helper_fsglmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
5202 case 0x28: /* fsub */
5203 gen_helper_fsub(cpu_env, cpu_dest, cpu_src, cpu_dest);
5205 case 0x68: /* fssub */
5206 gen_helper_fssub(cpu_env, cpu_dest, cpu_src, cpu_dest);
5208 case 0x6c: /* fdsub */
5209 gen_helper_fdsub(cpu_env, cpu_dest, cpu_src, cpu_dest);
5211 case 0x30: case 0x31: case 0x32:
5212 case 0x33: case 0x34: case 0x35:
5213 case 0x36: case 0x37: {
5214 TCGv_ptr cpu_dest2 = gen_fp_ptr(REG(ext, 0));
5215 gen_helper_fsincos(cpu_env, cpu_dest, cpu_dest2, cpu_src);
5216 tcg_temp_free_ptr(cpu_dest2);
5219 case 0x38: /* fcmp */
5220 gen_helper_fcmp(cpu_env, cpu_src, cpu_dest);
5222 case 0x3a: /* ftst */
5223 gen_helper_ftst(cpu_env, cpu_src);
5228 tcg_temp_free_ptr(cpu_src);
5229 gen_helper_ftst(cpu_env, cpu_dest);
5230 tcg_temp_free_ptr(cpu_dest);
5233 /* FIXME: Is this right for offset addressing modes? */
5235 disas_undef_fpu(env, s, insn);
5238 static void gen_fcc_cond(DisasCompare *c, DisasContext *s, int cond)
5243 c->v2 = tcg_const_i32(0);
5245 /* TODO: Raise BSUN exception. */
5246 fpsr = tcg_temp_new();
5247 gen_load_fcr(s, fpsr, M68K_FPSR);
5250 case 16: /* Signaling False */
5252 c->tcond = TCG_COND_NEVER;
5254 case 1: /* EQual Z */
5255 case 17: /* Signaling EQual Z */
5256 c->v1 = tcg_temp_new();
5258 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
5259 c->tcond = TCG_COND_NE;
5261 case 2: /* Ordered Greater Than !(A || Z || N) */
5262 case 18: /* Greater Than !(A || Z || N) */
5263 c->v1 = tcg_temp_new();
5265 tcg_gen_andi_i32(c->v1, fpsr,
5266 FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
5267 c->tcond = TCG_COND_EQ;
5269 case 3: /* Ordered Greater than or Equal Z || !(A || N) */
5270 case 19: /* Greater than or Equal Z || !(A || N) */
5271 c->v1 = tcg_temp_new();
5273 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
5274 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_A));
5275 tcg_gen_andi_i32(fpsr, fpsr, FPSR_CC_Z | FPSR_CC_N);
5276 tcg_gen_or_i32(c->v1, c->v1, fpsr);
5277 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
5278 c->tcond = TCG_COND_NE;
5280 case 4: /* Ordered Less Than !(!N || A || Z); */
5281 case 20: /* Less Than !(!N || A || Z); */
5282 c->v1 = tcg_temp_new();
5284 tcg_gen_xori_i32(c->v1, fpsr, FPSR_CC_N);
5285 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_N | FPSR_CC_A | FPSR_CC_Z);
5286 c->tcond = TCG_COND_EQ;
5288 case 5: /* Ordered Less than or Equal Z || (N && !A) */
5289 case 21: /* Less than or Equal Z || (N && !A) */
5290 c->v1 = tcg_temp_new();
5292 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
5293 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_A));
5294 tcg_gen_andc_i32(c->v1, fpsr, c->v1);
5295 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_Z | FPSR_CC_N);
5296 c->tcond = TCG_COND_NE;
5298 case 6: /* Ordered Greater or Less than !(A || Z) */
5299 case 22: /* Greater or Less than !(A || Z) */
5300 c->v1 = tcg_temp_new();
5302 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z);
5303 c->tcond = TCG_COND_EQ;
5305 case 7: /* Ordered !A */
5306 case 23: /* Greater, Less or Equal !A */
5307 c->v1 = tcg_temp_new();
5309 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
5310 c->tcond = TCG_COND_EQ;
5312 case 8: /* Unordered A */
5313 case 24: /* Not Greater, Less or Equal A */
5314 c->v1 = tcg_temp_new();
5316 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
5317 c->tcond = TCG_COND_NE;
5319 case 9: /* Unordered or Equal A || Z */
5320 case 25: /* Not Greater or Less then A || Z */
5321 c->v1 = tcg_temp_new();
5323 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z);
5324 c->tcond = TCG_COND_NE;
5326 case 10: /* Unordered or Greater Than A || !(N || Z)) */
5327 case 26: /* Not Less or Equal A || !(N || Z)) */
5328 c->v1 = tcg_temp_new();
5330 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
5331 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_Z));
5332 tcg_gen_andi_i32(fpsr, fpsr, FPSR_CC_A | FPSR_CC_N);
5333 tcg_gen_or_i32(c->v1, c->v1, fpsr);
5334 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
5335 c->tcond = TCG_COND_NE;
5337 case 11: /* Unordered or Greater or Equal A || Z || !N */
5338 case 27: /* Not Less Than A || Z || !N */
5339 c->v1 = tcg_temp_new();
5341 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
5342 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
5343 c->tcond = TCG_COND_NE;
5345 case 12: /* Unordered or Less Than A || (N && !Z) */
5346 case 28: /* Not Greater than or Equal A || (N && !Z) */
5347 c->v1 = tcg_temp_new();
5349 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
5350 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_Z));
5351 tcg_gen_andc_i32(c->v1, fpsr, c->v1);
5352 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_A | FPSR_CC_N);
5353 c->tcond = TCG_COND_NE;
5355 case 13: /* Unordered or Less or Equal A || Z || N */
5356 case 29: /* Not Greater Than A || Z || N */
5357 c->v1 = tcg_temp_new();
5359 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
5360 c->tcond = TCG_COND_NE;
5362 case 14: /* Not Equal !Z */
5363 case 30: /* Signaling Not Equal !Z */
5364 c->v1 = tcg_temp_new();
5366 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
5367 c->tcond = TCG_COND_EQ;
5370 case 31: /* Signaling True */
5372 c->tcond = TCG_COND_ALWAYS;
5375 tcg_temp_free(fpsr);
5378 static void gen_fjmpcc(DisasContext *s, int cond, TCGLabel *l1)
5382 gen_fcc_cond(&c, s, cond);
5384 tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
5395 offset = (int16_t)read_im16(env, s);
5396 if (insn & (1 << 6)) {
5397 offset = (offset << 16) | read_im16(env, s);
5400 l1 = gen_new_label();
5402 gen_fjmpcc(s, insn & 0x3f, l1);
5403 gen_jmp_tb(s, 0, s->pc);
5405 gen_jmp_tb(s, 1, base + offset);
5415 ext = read_im16(env, s);
5417 gen_fcc_cond(&c, s, cond);
5419 tmp = tcg_temp_new();
5420 tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
5423 tcg_gen_neg_i32(tmp, tmp);
5424 DEST_EA(env, insn, OS_BYTE, tmp, NULL);
5428 #if defined(CONFIG_SOFTMMU)
5429 DISAS_INSN(frestore)
5434 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
5437 if (m68k_feature(s->env, M68K_FEATURE_M68040)) {
5438 SRC_EA(env, addr, OS_LONG, 0, NULL);
5439 /* FIXME: check the state frame */
5441 disas_undef(env, s, insn);
5448 gen_exception(s, s->insn_pc, EXCP_PRIVILEGE);
5452 if (m68k_feature(s->env, M68K_FEATURE_M68040)) {
5453 /* always write IDLE */
5454 TCGv idle = tcg_const_i32(0x41000000);
5455 DEST_EA(env, insn, OS_LONG, idle, NULL);
5456 tcg_temp_free(idle);
5458 disas_undef(env, s, insn);
5463 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
5465 TCGv tmp = tcg_temp_new();
5466 if (s->env->macsr & MACSR_FI) {
5468 tcg_gen_andi_i32(tmp, val, 0xffff0000);
5470 tcg_gen_shli_i32(tmp, val, 16);
5471 } else if (s->env->macsr & MACSR_SU) {
5473 tcg_gen_sari_i32(tmp, val, 16);
5475 tcg_gen_ext16s_i32(tmp, val);
5478 tcg_gen_shri_i32(tmp, val, 16);
5480 tcg_gen_ext16u_i32(tmp, val);
5485 static void gen_mac_clear_flags(void)
5487 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
5488 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
5504 s->mactmp = tcg_temp_new_i64();
5508 ext = read_im16(env, s);
5510 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
5511 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
5512 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
5513 disas_undef(env, s, insn);
5517 /* MAC with load. */
5518 tmp = gen_lea(env, s, insn, OS_LONG);
5519 addr = tcg_temp_new();
5520 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
5521 /* Load the value now to ensure correct exception behavior.
5522 Perform writeback after reading the MAC inputs. */
5523 loadval = gen_load(s, OS_LONG, addr, 0, IS_USER(s));
5526 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
5527 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
5529 loadval = addr = NULL_QREG;
5530 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
5531 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5534 gen_mac_clear_flags();
5537 /* Disabled because conditional branches clobber temporary vars. */
5538 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
5539 /* Skip the multiply if we know we will ignore it. */
5540 l1 = gen_new_label();
5541 tmp = tcg_temp_new();
5542 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
5543 gen_op_jmp_nz32(tmp, l1);
5547 if ((ext & 0x0800) == 0) {
5549 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
5550 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
5552 if (s->env->macsr & MACSR_FI) {
5553 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
5555 if (s->env->macsr & MACSR_SU)
5556 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
5558 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
5559 switch ((ext >> 9) & 3) {
5561 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
5564 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
5570 /* Save the overflow flag from the multiply. */
5571 saved_flags = tcg_temp_new();
5572 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
5574 saved_flags = NULL_QREG;
5578 /* Disabled because conditional branches clobber temporary vars. */
5579 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
5580 /* Skip the accumulate if the value is already saturated. */
5581 l1 = gen_new_label();
5582 tmp = tcg_temp_new();
5583 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
5584 gen_op_jmp_nz32(tmp, l1);
5589 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
5591 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
5593 if (s->env->macsr & MACSR_FI)
5594 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
5595 else if (s->env->macsr & MACSR_SU)
5596 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
5598 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
5601 /* Disabled because conditional branches clobber temporary vars. */
5607 /* Dual accumulate variant. */
5608 acc = (ext >> 2) & 3;
5609 /* Restore the overflow flag from the multiplier. */
5610 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
5612 /* Disabled because conditional branches clobber temporary vars. */
5613 if ((s->env->macsr & MACSR_OMC) != 0) {
5614 /* Skip the accumulate if the value is already saturated. */
5615 l1 = gen_new_label();
5616 tmp = tcg_temp_new();
5617 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
5618 gen_op_jmp_nz32(tmp, l1);
5622 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
5624 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
5625 if (s->env->macsr & MACSR_FI)
5626 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
5627 else if (s->env->macsr & MACSR_SU)
5628 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
5630 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
5632 /* Disabled because conditional branches clobber temporary vars. */
5637 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
5641 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
5642 tcg_gen_mov_i32(rw, loadval);
5643 /* FIXME: Should address writeback happen with the masked or
5645 switch ((insn >> 3) & 7) {
5646 case 3: /* Post-increment. */
5647 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
5649 case 4: /* Pre-decrement. */
5650 tcg_gen_mov_i32(AREG(insn, 0), addr);
5652 tcg_temp_free(loadval);
5656 DISAS_INSN(from_mac)
5662 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5663 accnum = (insn >> 9) & 3;
5664 acc = MACREG(accnum);
5665 if (s->env->macsr & MACSR_FI) {
5666 gen_helper_get_macf(rx, cpu_env, acc);
5667 } else if ((s->env->macsr & MACSR_OMC) == 0) {
5668 tcg_gen_extrl_i64_i32(rx, acc);
5669 } else if (s->env->macsr & MACSR_SU) {
5670 gen_helper_get_macs(rx, acc);
5672 gen_helper_get_macu(rx, acc);
5675 tcg_gen_movi_i64(acc, 0);
5676 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
5680 DISAS_INSN(move_mac)
5682 /* FIXME: This can be done without a helper. */
5686 dest = tcg_const_i32((insn >> 9) & 3);
5687 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
5688 gen_mac_clear_flags();
5689 gen_helper_mac_set_flags(cpu_env, dest);
5692 DISAS_INSN(from_macsr)
5696 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5697 tcg_gen_mov_i32(reg, QREG_MACSR);
5700 DISAS_INSN(from_mask)
5703 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5704 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
5707 DISAS_INSN(from_mext)
5711 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5712 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
5713 if (s->env->macsr & MACSR_FI)
5714 gen_helper_get_mac_extf(reg, cpu_env, acc);
5716 gen_helper_get_mac_exti(reg, cpu_env, acc);
5719 DISAS_INSN(macsr_to_ccr)
5721 TCGv tmp = tcg_temp_new();
5722 tcg_gen_andi_i32(tmp, QREG_MACSR, 0xf);
5723 gen_helper_set_sr(cpu_env, tmp);
5725 set_cc_op(s, CC_OP_FLAGS);
5733 accnum = (insn >> 9) & 3;
5734 acc = MACREG(accnum);
5735 SRC_EA(env, val, OS_LONG, 0, NULL);
5736 if (s->env->macsr & MACSR_FI) {
5737 tcg_gen_ext_i32_i64(acc, val);
5738 tcg_gen_shli_i64(acc, acc, 8);
5739 } else if (s->env->macsr & MACSR_SU) {
5740 tcg_gen_ext_i32_i64(acc, val);
5742 tcg_gen_extu_i32_i64(acc, val);
5744 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
5745 gen_mac_clear_flags();
5746 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
5749 DISAS_INSN(to_macsr)
5752 SRC_EA(env, val, OS_LONG, 0, NULL);
5753 gen_helper_set_macsr(cpu_env, val);
5760 SRC_EA(env, val, OS_LONG, 0, NULL);
5761 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
5768 SRC_EA(env, val, OS_LONG, 0, NULL);
5769 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
5770 if (s->env->macsr & MACSR_FI)
5771 gen_helper_set_mac_extf(cpu_env, val, acc);
5772 else if (s->env->macsr & MACSR_SU)
5773 gen_helper_set_mac_exts(cpu_env, val, acc);
5775 gen_helper_set_mac_extu(cpu_env, val, acc);
5778 static disas_proc opcode_table[65536];
5781 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
5787 /* Sanity check. All set bits must be included in the mask. */
5788 if (opcode & ~mask) {
5790 "qemu internal error: bogus opcode definition %04x/%04x\n",
5794 /* This could probably be cleverer. For now just optimize the case where
5795 the top bits are known. */
5796 /* Find the first zero bit in the mask. */
5798 while ((i & mask) != 0)
5800 /* Iterate over all combinations of this and lower bits. */
5805 from = opcode & ~(i - 1);
5807 for (i = from; i < to; i++) {
5808 if ((i & mask) == opcode)
5809 opcode_table[i] = proc;
5813 /* Register m68k opcode handlers. Order is important.
5814 Later insn override earlier ones. */
5815 void register_m68k_insns (CPUM68KState *env)
5817 /* Build the opcode table only once to avoid
5818 multithreading issues. */
5819 if (opcode_table[0] != NULL) {
5823 /* use BASE() for instruction available
5824 * for CF_ISA_A and M68000.
5826 #define BASE(name, opcode, mask) \
5827 register_opcode(disas_##name, 0x##opcode, 0x##mask)
5828 #define INSN(name, opcode, mask, feature) do { \
5829 if (m68k_feature(env, M68K_FEATURE_##feature)) \
5830 BASE(name, opcode, mask); \
5832 BASE(undef, 0000, 0000);
5833 INSN(arith_im, 0080, fff8, CF_ISA_A);
5834 INSN(arith_im, 0000, ff00, M68000);
5835 INSN(chk2, 00c0, f9c0, CHK2);
5836 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
5837 BASE(bitop_reg, 0100, f1c0);
5838 BASE(bitop_reg, 0140, f1c0);
5839 BASE(bitop_reg, 0180, f1c0);
5840 BASE(bitop_reg, 01c0, f1c0);
5841 INSN(movep, 0108, f138, MOVEP);
5842 INSN(arith_im, 0280, fff8, CF_ISA_A);
5843 INSN(arith_im, 0200, ff00, M68000);
5844 INSN(undef, 02c0, ffc0, M68000);
5845 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
5846 INSN(arith_im, 0480, fff8, CF_ISA_A);
5847 INSN(arith_im, 0400, ff00, M68000);
5848 INSN(undef, 04c0, ffc0, M68000);
5849 INSN(arith_im, 0600, ff00, M68000);
5850 INSN(undef, 06c0, ffc0, M68000);
5851 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
5852 INSN(arith_im, 0680, fff8, CF_ISA_A);
5853 INSN(arith_im, 0c00, ff38, CF_ISA_A);
5854 INSN(arith_im, 0c00, ff00, M68000);
5855 BASE(bitop_im, 0800, ffc0);
5856 BASE(bitop_im, 0840, ffc0);
5857 BASE(bitop_im, 0880, ffc0);
5858 BASE(bitop_im, 08c0, ffc0);
5859 INSN(arith_im, 0a80, fff8, CF_ISA_A);
5860 INSN(arith_im, 0a00, ff00, M68000);
5861 #if defined(CONFIG_SOFTMMU)
5862 INSN(moves, 0e00, ff00, M68000);
5864 INSN(cas, 0ac0, ffc0, CAS);
5865 INSN(cas, 0cc0, ffc0, CAS);
5866 INSN(cas, 0ec0, ffc0, CAS);
5867 INSN(cas2w, 0cfc, ffff, CAS);
5868 INSN(cas2l, 0efc, ffff, CAS);
5869 BASE(move, 1000, f000);
5870 BASE(move, 2000, f000);
5871 BASE(move, 3000, f000);
5872 INSN(chk, 4000, f040, M68000);
5873 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
5874 INSN(negx, 4080, fff8, CF_ISA_A);
5875 INSN(negx, 4000, ff00, M68000);
5876 INSN(undef, 40c0, ffc0, M68000);
5877 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
5878 INSN(move_from_sr, 40c0, ffc0, M68000);
5879 BASE(lea, 41c0, f1c0);
5880 BASE(clr, 4200, ff00);
5881 BASE(undef, 42c0, ffc0);
5882 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
5883 INSN(move_from_ccr, 42c0, ffc0, M68000);
5884 INSN(neg, 4480, fff8, CF_ISA_A);
5885 INSN(neg, 4400, ff00, M68000);
5886 INSN(undef, 44c0, ffc0, M68000);
5887 BASE(move_to_ccr, 44c0, ffc0);
5888 INSN(not, 4680, fff8, CF_ISA_A);
5889 INSN(not, 4600, ff00, M68000);
5890 #if defined(CONFIG_SOFTMMU)
5891 BASE(move_to_sr, 46c0, ffc0);
5893 INSN(nbcd, 4800, ffc0, M68000);
5894 INSN(linkl, 4808, fff8, M68000);
5895 BASE(pea, 4840, ffc0);
5896 BASE(swap, 4840, fff8);
5897 INSN(bkpt, 4848, fff8, BKPT);
5898 INSN(movem, 48d0, fbf8, CF_ISA_A);
5899 INSN(movem, 48e8, fbf8, CF_ISA_A);
5900 INSN(movem, 4880, fb80, M68000);
5901 BASE(ext, 4880, fff8);
5902 BASE(ext, 48c0, fff8);
5903 BASE(ext, 49c0, fff8);
5904 BASE(tst, 4a00, ff00);
5905 INSN(tas, 4ac0, ffc0, CF_ISA_B);
5906 INSN(tas, 4ac0, ffc0, M68000);
5907 #if defined(CONFIG_SOFTMMU)
5908 INSN(halt, 4ac8, ffff, CF_ISA_A);
5910 INSN(pulse, 4acc, ffff, CF_ISA_A);
5911 BASE(illegal, 4afc, ffff);
5912 INSN(mull, 4c00, ffc0, CF_ISA_A);
5913 INSN(mull, 4c00, ffc0, LONG_MULDIV);
5914 INSN(divl, 4c40, ffc0, CF_ISA_A);
5915 INSN(divl, 4c40, ffc0, LONG_MULDIV);
5916 INSN(sats, 4c80, fff8, CF_ISA_B);
5917 BASE(trap, 4e40, fff0);
5918 BASE(link, 4e50, fff8);
5919 BASE(unlk, 4e58, fff8);
5920 #if defined(CONFIG_SOFTMMU)
5921 INSN(move_to_usp, 4e60, fff8, USP);
5922 INSN(move_from_usp, 4e68, fff8, USP);
5923 INSN(reset, 4e70, ffff, M68000);
5924 BASE(stop, 4e72, ffff);
5925 BASE(rte, 4e73, ffff);
5926 INSN(cf_movec, 4e7b, ffff, CF_ISA_A);
5927 INSN(m68k_movec, 4e7a, fffe, M68000);
5929 BASE(nop, 4e71, ffff);
5930 INSN(rtd, 4e74, ffff, RTD);
5931 BASE(rts, 4e75, ffff);
5932 BASE(jump, 4e80, ffc0);
5933 BASE(jump, 4ec0, ffc0);
5934 INSN(addsubq, 5000, f080, M68000);
5935 BASE(addsubq, 5080, f0c0);
5936 INSN(scc, 50c0, f0f8, CF_ISA_A); /* Scc.B Dx */
5937 INSN(scc, 50c0, f0c0, M68000); /* Scc.B <EA> */
5938 INSN(dbcc, 50c8, f0f8, M68000);
5939 INSN(tpf, 51f8, fff8, CF_ISA_A);
5941 /* Branch instructions. */
5942 BASE(branch, 6000, f000);
5943 /* Disable long branch instructions, then add back the ones we want. */
5944 BASE(undef, 60ff, f0ff); /* All long branches. */
5945 INSN(branch, 60ff, f0ff, CF_ISA_B);
5946 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
5947 INSN(branch, 60ff, ffff, BRAL);
5948 INSN(branch, 60ff, f0ff, BCCL);
5950 BASE(moveq, 7000, f100);
5951 INSN(mvzs, 7100, f100, CF_ISA_B);
5952 BASE(or, 8000, f000);
5953 BASE(divw, 80c0, f0c0);
5954 INSN(sbcd_reg, 8100, f1f8, M68000);
5955 INSN(sbcd_mem, 8108, f1f8, M68000);
5956 BASE(addsub, 9000, f000);
5957 INSN(undef, 90c0, f0c0, CF_ISA_A);
5958 INSN(subx_reg, 9180, f1f8, CF_ISA_A);
5959 INSN(subx_reg, 9100, f138, M68000);
5960 INSN(subx_mem, 9108, f138, M68000);
5961 INSN(suba, 91c0, f1c0, CF_ISA_A);
5962 INSN(suba, 90c0, f0c0, M68000);
5964 BASE(undef_mac, a000, f000);
5965 INSN(mac, a000, f100, CF_EMAC);
5966 INSN(from_mac, a180, f9b0, CF_EMAC);
5967 INSN(move_mac, a110, f9fc, CF_EMAC);
5968 INSN(from_macsr,a980, f9f0, CF_EMAC);
5969 INSN(from_mask, ad80, fff0, CF_EMAC);
5970 INSN(from_mext, ab80, fbf0, CF_EMAC);
5971 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
5972 INSN(to_mac, a100, f9c0, CF_EMAC);
5973 INSN(to_macsr, a900, ffc0, CF_EMAC);
5974 INSN(to_mext, ab00, fbc0, CF_EMAC);
5975 INSN(to_mask, ad00, ffc0, CF_EMAC);
5977 INSN(mov3q, a140, f1c0, CF_ISA_B);
5978 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
5979 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
5980 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
5981 INSN(cmp, b080, f1c0, CF_ISA_A);
5982 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
5983 INSN(cmp, b000, f100, M68000);
5984 INSN(eor, b100, f100, M68000);
5985 INSN(cmpm, b108, f138, M68000);
5986 INSN(cmpa, b0c0, f0c0, M68000);
5987 INSN(eor, b180, f1c0, CF_ISA_A);
5988 BASE(and, c000, f000);
5989 INSN(exg_dd, c140, f1f8, M68000);
5990 INSN(exg_aa, c148, f1f8, M68000);
5991 INSN(exg_da, c188, f1f8, M68000);
5992 BASE(mulw, c0c0, f0c0);
5993 INSN(abcd_reg, c100, f1f8, M68000);
5994 INSN(abcd_mem, c108, f1f8, M68000);
5995 BASE(addsub, d000, f000);
5996 INSN(undef, d0c0, f0c0, CF_ISA_A);
5997 INSN(addx_reg, d180, f1f8, CF_ISA_A);
5998 INSN(addx_reg, d100, f138, M68000);
5999 INSN(addx_mem, d108, f138, M68000);
6000 INSN(adda, d1c0, f1c0, CF_ISA_A);
6001 INSN(adda, d0c0, f0c0, M68000);
6002 INSN(shift_im, e080, f0f0, CF_ISA_A);
6003 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
6004 INSN(shift8_im, e000, f0f0, M68000);
6005 INSN(shift16_im, e040, f0f0, M68000);
6006 INSN(shift_im, e080, f0f0, M68000);
6007 INSN(shift8_reg, e020, f0f0, M68000);
6008 INSN(shift16_reg, e060, f0f0, M68000);
6009 INSN(shift_reg, e0a0, f0f0, M68000);
6010 INSN(shift_mem, e0c0, fcc0, M68000);
6011 INSN(rotate_im, e090, f0f0, M68000);
6012 INSN(rotate8_im, e010, f0f0, M68000);
6013 INSN(rotate16_im, e050, f0f0, M68000);
6014 INSN(rotate_reg, e0b0, f0f0, M68000);
6015 INSN(rotate8_reg, e030, f0f0, M68000);
6016 INSN(rotate16_reg, e070, f0f0, M68000);
6017 INSN(rotate_mem, e4c0, fcc0, M68000);
6018 INSN(bfext_mem, e9c0, fdc0, BITFIELD); /* bfextu & bfexts */
6019 INSN(bfext_reg, e9c0, fdf8, BITFIELD);
6020 INSN(bfins_mem, efc0, ffc0, BITFIELD);
6021 INSN(bfins_reg, efc0, fff8, BITFIELD);
6022 INSN(bfop_mem, eac0, ffc0, BITFIELD); /* bfchg */
6023 INSN(bfop_reg, eac0, fff8, BITFIELD); /* bfchg */
6024 INSN(bfop_mem, ecc0, ffc0, BITFIELD); /* bfclr */
6025 INSN(bfop_reg, ecc0, fff8, BITFIELD); /* bfclr */
6026 INSN(bfop_mem, edc0, ffc0, BITFIELD); /* bfffo */
6027 INSN(bfop_reg, edc0, fff8, BITFIELD); /* bfffo */
6028 INSN(bfop_mem, eec0, ffc0, BITFIELD); /* bfset */
6029 INSN(bfop_reg, eec0, fff8, BITFIELD); /* bfset */
6030 INSN(bfop_mem, e8c0, ffc0, BITFIELD); /* bftst */
6031 INSN(bfop_reg, e8c0, fff8, BITFIELD); /* bftst */
6032 BASE(undef_fpu, f000, f000);
6033 INSN(fpu, f200, ffc0, CF_FPU);
6034 INSN(fbcc, f280, ffc0, CF_FPU);
6035 INSN(fpu, f200, ffc0, FPU);
6036 INSN(fscc, f240, ffc0, FPU);
6037 INSN(fbcc, f280, ff80, FPU);
6038 #if defined(CONFIG_SOFTMMU)
6039 INSN(frestore, f340, ffc0, CF_FPU);
6040 INSN(fsave, f300, ffc0, CF_FPU);
6041 INSN(frestore, f340, ffc0, FPU);
6042 INSN(fsave, f300, ffc0, FPU);
6043 INSN(intouch, f340, ffc0, CF_ISA_A);
6044 INSN(cpushl, f428, ff38, CF_ISA_A);
6045 INSN(cpush, f420, ff20, M68040);
6046 INSN(cinv, f400, ff20, M68040);
6047 INSN(pflush, f500, ffe0, M68040);
6048 INSN(ptest, f548, ffd8, M68040);
6049 INSN(wddata, fb00, ff00, CF_ISA_A);
6050 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
6052 INSN(move16_mem, f600, ffe0, M68040);
6053 INSN(move16_reg, f620, fff8, M68040);
6057 /* ??? Some of this implementation is not exception safe. We should always
6058 write back the result to memory before setting the condition codes. */
6059 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
6061 uint16_t insn = read_im16(env, s);
6062 opcode_table[insn](env, s, insn);
6067 /* generate intermediate code for basic block 'tb'. */
6068 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb)
6070 CPUM68KState *env = cs->env_ptr;
6071 DisasContext dc1, *dc = &dc1;
6072 target_ulong pc_start;
6077 /* generate intermediate code */
6083 dc->is_jmp = DISAS_NEXT;
6085 dc->cc_op = CC_OP_DYNAMIC;
6086 dc->cc_op_synced = 1;
6087 dc->singlestep_enabled = cs->singlestep_enabled;
6089 dc->writeback_mask = 0;
6091 max_insns = tb_cflags(tb) & CF_COUNT_MASK;
6092 if (max_insns == 0) {
6093 max_insns = CF_COUNT_MASK;
6095 if (max_insns > TCG_MAX_INSNS) {
6096 max_insns = TCG_MAX_INSNS;
6099 init_release_array(dc);
6103 pc_offset = dc->pc - pc_start;
6104 tcg_gen_insn_start(dc->pc, dc->cc_op);
6107 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
6108 gen_exception(dc, dc->pc, EXCP_DEBUG);
6109 dc->is_jmp = DISAS_JUMP;
6110 /* The address covered by the breakpoint must be included in
6111 [tb->pc, tb->pc + tb->size) in order to for it to be
6112 properly cleared -- thus we increment the PC here so that
6113 the logic setting tb->size below does the right thing. */
6118 if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
6122 dc->insn_pc = dc->pc;
6123 disas_m68k_insn(env, dc);
6124 } while (!dc->is_jmp && !tcg_op_buf_full() &&
6125 !cs->singlestep_enabled &&
6127 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
6128 num_insns < max_insns);
6130 if (tb_cflags(tb) & CF_LAST_IO)
6132 if (unlikely(cs->singlestep_enabled)) {
6133 /* Make sure the pc is updated, and raise a debug exception. */
6136 tcg_gen_movi_i32(QREG_PC, dc->pc);
6138 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
6140 switch(dc->is_jmp) {
6143 gen_jmp_tb(dc, 0, dc->pc);
6149 /* indicate that the hash table must be used to find the next TB */
6150 tcg_gen_exit_tb(NULL, 0);
6153 /* nothing more to generate */
6157 gen_tb_end(tb, num_insns);
6160 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
6161 && qemu_log_in_addr_range(pc_start)) {
6163 qemu_log("----------------\n");
6164 qemu_log("IN: %s\n", lookup_symbol(pc_start));
6165 log_target_disas(cs, pc_start, dc->pc - pc_start);
6170 tb->size = dc->pc - pc_start;
6171 tb->icount = num_insns;
6174 static double floatx80_to_double(CPUM68KState *env, uint16_t high, uint64_t low)
6176 floatx80 a = { .high = high, .low = low };
6182 u.f64 = floatx80_to_float64(a, &env->fp_status);
6186 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
6189 M68kCPU *cpu = M68K_CPU(cs);
6190 CPUM68KState *env = &cpu->env;
6193 for (i = 0; i < 8; i++) {
6194 cpu_fprintf(f, "D%d = %08x A%d = %08x "
6195 "F%d = %04x %016"PRIx64" (%12g)\n",
6196 i, env->dregs[i], i, env->aregs[i],
6197 i, env->fregs[i].l.upper, env->fregs[i].l.lower,
6198 floatx80_to_double(env, env->fregs[i].l.upper,
6199 env->fregs[i].l.lower));
6201 cpu_fprintf (f, "PC = %08x ", env->pc);
6202 sr = env->sr | cpu_m68k_get_ccr(env);
6203 cpu_fprintf(f, "SR = %04x T:%x I:%x %c%c %c%c%c%c%c\n",
6204 sr, (sr & SR_T) >> SR_T_SHIFT, (sr & SR_I) >> SR_I_SHIFT,
6205 (sr & SR_S) ? 'S' : 'U', (sr & SR_M) ? '%' : 'I',
6206 (sr & CCF_X) ? 'X' : '-', (sr & CCF_N) ? 'N' : '-',
6207 (sr & CCF_Z) ? 'Z' : '-', (sr & CCF_V) ? 'V' : '-',
6208 (sr & CCF_C) ? 'C' : '-');
6209 cpu_fprintf(f, "FPSR = %08x %c%c%c%c ", env->fpsr,
6210 (env->fpsr & FPSR_CC_A) ? 'A' : '-',
6211 (env->fpsr & FPSR_CC_I) ? 'I' : '-',
6212 (env->fpsr & FPSR_CC_Z) ? 'Z' : '-',
6213 (env->fpsr & FPSR_CC_N) ? 'N' : '-');
6214 cpu_fprintf(f, "\n "
6215 "FPCR = %04x ", env->fpcr);
6216 switch (env->fpcr & FPCR_PREC_MASK) {
6218 cpu_fprintf(f, "X ");
6221 cpu_fprintf(f, "S ");
6224 cpu_fprintf(f, "D ");
6227 switch (env->fpcr & FPCR_RND_MASK) {
6229 cpu_fprintf(f, "RN ");
6232 cpu_fprintf(f, "RZ ");
6235 cpu_fprintf(f, "RM ");
6238 cpu_fprintf(f, "RP ");
6241 cpu_fprintf(f, "\n");
6242 #ifdef CONFIG_SOFTMMU
6243 cpu_fprintf(f, "%sA7(MSP) = %08x %sA7(USP) = %08x %sA7(ISP) = %08x\n",
6244 env->current_sp == M68K_SSP ? "->" : " ", env->sp[M68K_SSP],
6245 env->current_sp == M68K_USP ? "->" : " ", env->sp[M68K_USP],
6246 env->current_sp == M68K_ISP ? "->" : " ", env->sp[M68K_ISP]);
6247 cpu_fprintf(f, "VBR = 0x%08x\n", env->vbr);
6248 cpu_fprintf(f, "SFC = %x DFC %x\n", env->sfc, env->dfc);
6249 cpu_fprintf(f, "SSW %08x TCR %08x URP %08x SRP %08x\n",
6250 env->mmu.ssw, env->mmu.tcr, env->mmu.urp, env->mmu.srp);
6251 cpu_fprintf(f, "DTTR0/1: %08x/%08x ITTR0/1: %08x/%08x\n",
6252 env->mmu.ttr[M68K_DTTR0], env->mmu.ttr[M68K_DTTR1],
6253 env->mmu.ttr[M68K_ITTR0], env->mmu.ttr[M68K_ITTR1]);
6254 cpu_fprintf(f, "MMUSR %08x, fault at %08x\n",
6255 env->mmu.mmusr, env->mmu.ar);
6259 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,
6262 int cc_op = data[1];
6264 if (cc_op != CC_OP_DYNAMIC) {