4 * Copyright (c) 2005-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
35 //#define DEBUG_DISPATCH 1
37 #define DEFO32(name, offset) static TCGv QREG_##name;
38 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
43 static TCGv_i32 cpu_halted;
44 static TCGv_i32 cpu_exception_index;
46 static TCGv_env cpu_env;
48 static char cpu_reg_names[2 * 8 * 3 + 5 * 4];
49 static TCGv cpu_dregs[8];
50 static TCGv cpu_aregs[8];
51 static TCGv_i64 cpu_macc[4];
53 #define REG(insn, pos) (((insn) >> (pos)) & 7)
54 #define DREG(insn, pos) cpu_dregs[REG(insn, pos)]
55 #define AREG(insn, pos) get_areg(s, REG(insn, pos))
56 #define MACREG(acc) cpu_macc[acc]
57 #define QREG_SP get_areg(s, 7)
59 static TCGv NULL_QREG;
60 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
61 /* Used to distinguish stores from bad addressing modes. */
62 static TCGv store_dummy;
64 #include "exec/gen-icount.h"
66 void m68k_tcg_init(void)
71 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
72 tcg_ctx.tcg_env = cpu_env;
74 #define DEFO32(name, offset) \
75 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
76 offsetof(CPUM68KState, offset), #name);
77 #define DEFO64(name, offset) \
78 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
79 offsetof(CPUM68KState, offset), #name);
84 cpu_halted = tcg_global_mem_new_i32(cpu_env,
85 -offsetof(M68kCPU, env) +
86 offsetof(CPUState, halted), "HALTED");
87 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
88 -offsetof(M68kCPU, env) +
89 offsetof(CPUState, exception_index),
93 for (i = 0; i < 8; i++) {
95 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
96 offsetof(CPUM68KState, dregs[i]), p);
99 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
100 offsetof(CPUM68KState, aregs[i]), p);
103 for (i = 0; i < 4; i++) {
104 sprintf(p, "ACC%d", i);
105 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
106 offsetof(CPUM68KState, macc[i]), p);
110 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
111 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
114 /* internal defines */
115 typedef struct DisasContext {
117 target_ulong insn_pc; /* Start of the current instruction. */
120 CCOp cc_op; /* Current CC operation */
123 struct TranslationBlock *tb;
124 int singlestep_enabled;
131 static TCGv get_areg(DisasContext *s, unsigned regno)
133 if (s->writeback_mask & (1 << regno)) {
134 return s->writeback[regno];
136 return cpu_aregs[regno];
140 static void delay_set_areg(DisasContext *s, unsigned regno,
141 TCGv val, bool give_temp)
143 if (s->writeback_mask & (1 << regno)) {
145 tcg_temp_free(s->writeback[regno]);
146 s->writeback[regno] = val;
148 tcg_gen_mov_i32(s->writeback[regno], val);
151 s->writeback_mask |= 1 << regno;
153 s->writeback[regno] = val;
155 TCGv tmp = tcg_temp_new();
156 s->writeback[regno] = tmp;
157 tcg_gen_mov_i32(tmp, val);
162 static void do_writebacks(DisasContext *s)
164 unsigned mask = s->writeback_mask;
166 s->writeback_mask = 0;
168 unsigned regno = ctz32(mask);
169 tcg_gen_mov_i32(cpu_aregs[regno], s->writeback[regno]);
170 tcg_temp_free(s->writeback[regno]);
176 #define DISAS_JUMP_NEXT 4
178 #if defined(CONFIG_USER_ONLY)
181 #define IS_USER(s) s->user
184 /* XXX: move that elsewhere */
185 /* ??? Fix exceptions. */
186 static void *gen_throws_exception;
187 #define gen_last_qop NULL
189 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
191 #ifdef DEBUG_DISPATCH
192 #define DISAS_INSN(name) \
193 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
195 static void disas_##name(CPUM68KState *env, DisasContext *s, \
198 qemu_log("Dispatch " #name "\n"); \
199 real_disas_##name(env, s, insn); \
201 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
204 #define DISAS_INSN(name) \
205 static void disas_##name(CPUM68KState *env, DisasContext *s, \
209 static const uint8_t cc_op_live[CC_OP_NB] = {
210 [CC_OP_FLAGS] = CCF_C | CCF_V | CCF_Z | CCF_N | CCF_X,
211 [CC_OP_ADDB ... CC_OP_ADDL] = CCF_X | CCF_N | CCF_V,
212 [CC_OP_SUBB ... CC_OP_SUBL] = CCF_X | CCF_N | CCF_V,
213 [CC_OP_CMPB ... CC_OP_CMPL] = CCF_X | CCF_N | CCF_V,
214 [CC_OP_LOGIC] = CCF_X | CCF_N
217 static void set_cc_op(DisasContext *s, CCOp op)
219 CCOp old_op = s->cc_op;
228 /* Discard CC computation that will no longer be used.
229 Note that X and N are never dead. */
230 dead = cc_op_live[old_op] & ~cc_op_live[op];
232 tcg_gen_discard_i32(QREG_CC_C);
235 tcg_gen_discard_i32(QREG_CC_Z);
238 tcg_gen_discard_i32(QREG_CC_V);
242 /* Update the CPU env CC_OP state. */
243 static void update_cc_op(DisasContext *s)
245 if (!s->cc_op_synced) {
247 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
251 /* Generate a jump to an immediate address. */
252 static void gen_jmp_im(DisasContext *s, uint32_t dest)
255 tcg_gen_movi_i32(QREG_PC, dest);
256 s->is_jmp = DISAS_JUMP;
259 /* Generate a jump to the address in qreg DEST. */
260 static void gen_jmp(DisasContext *s, TCGv dest)
263 tcg_gen_mov_i32(QREG_PC, dest);
264 s->is_jmp = DISAS_JUMP;
267 static void gen_raise_exception(int nr)
269 TCGv_i32 tmp = tcg_const_i32(nr);
271 gen_helper_raise_exception(cpu_env, tmp);
272 tcg_temp_free_i32(tmp);
275 static void gen_exception(DisasContext *s, uint32_t where, int nr)
278 gen_jmp_im(s, where);
279 gen_raise_exception(nr);
282 static inline void gen_addr_fault(DisasContext *s)
284 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
287 /* Generate a load from the specified address. Narrow values are
288 sign extended to full register width. */
289 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
292 int index = IS_USER(s);
293 tmp = tcg_temp_new_i32();
297 tcg_gen_qemu_ld8s(tmp, addr, index);
299 tcg_gen_qemu_ld8u(tmp, addr, index);
303 tcg_gen_qemu_ld16s(tmp, addr, index);
305 tcg_gen_qemu_ld16u(tmp, addr, index);
308 tcg_gen_qemu_ld32u(tmp, addr, index);
311 g_assert_not_reached();
313 gen_throws_exception = gen_last_qop;
317 /* Generate a store. */
318 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
320 int index = IS_USER(s);
323 tcg_gen_qemu_st8(val, addr, index);
326 tcg_gen_qemu_st16(val, addr, index);
329 tcg_gen_qemu_st32(val, addr, index);
332 g_assert_not_reached();
334 gen_throws_exception = gen_last_qop;
343 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
344 otherwise generate a store. */
345 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
348 if (what == EA_STORE) {
349 gen_store(s, opsize, addr, val);
352 return gen_load(s, opsize, addr, what == EA_LOADS);
356 /* Read a 16-bit immediate constant */
357 static inline uint16_t read_im16(CPUM68KState *env, DisasContext *s)
360 im = cpu_lduw_code(env, s->pc);
365 /* Read an 8-bit immediate constant */
366 static inline uint8_t read_im8(CPUM68KState *env, DisasContext *s)
368 return read_im16(env, s);
371 /* Read a 32-bit immediate constant. */
372 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
375 im = read_im16(env, s) << 16;
376 im |= 0xffff & read_im16(env, s);
380 /* Read a 64-bit immediate constant. */
381 static inline uint64_t read_im64(CPUM68KState *env, DisasContext *s)
384 im = (uint64_t)read_im32(env, s) << 32;
385 im |= (uint64_t)read_im32(env, s);
389 /* Calculate and address index. */
390 static TCGv gen_addr_index(DisasContext *s, uint16_t ext, TCGv tmp)
395 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
396 if ((ext & 0x800) == 0) {
397 tcg_gen_ext16s_i32(tmp, add);
400 scale = (ext >> 9) & 3;
402 tcg_gen_shli_i32(tmp, add, scale);
408 /* Handle a base + index + displacement effective addresss.
409 A NULL_QREG base means pc-relative. */
410 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
419 ext = read_im16(env, s);
421 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
424 if (m68k_feature(s->env, M68K_FEATURE_M68000) &&
425 !m68k_feature(s->env, M68K_FEATURE_SCALED_INDEX)) {
430 /* full extension word format */
431 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
434 if ((ext & 0x30) > 0x10) {
435 /* base displacement */
436 if ((ext & 0x30) == 0x20) {
437 bd = (int16_t)read_im16(env, s);
439 bd = read_im32(env, s);
444 tmp = tcg_temp_new();
445 if ((ext & 0x44) == 0) {
447 add = gen_addr_index(s, ext, tmp);
451 if ((ext & 0x80) == 0) {
452 /* base not suppressed */
453 if (IS_NULL_QREG(base)) {
454 base = tcg_const_i32(offset + bd);
457 if (!IS_NULL_QREG(add)) {
458 tcg_gen_add_i32(tmp, add, base);
464 if (!IS_NULL_QREG(add)) {
466 tcg_gen_addi_i32(tmp, add, bd);
470 add = tcg_const_i32(bd);
472 if ((ext & 3) != 0) {
473 /* memory indirect */
474 base = gen_load(s, OS_LONG, add, 0);
475 if ((ext & 0x44) == 4) {
476 add = gen_addr_index(s, ext, tmp);
477 tcg_gen_add_i32(tmp, add, base);
483 /* outer displacement */
484 if ((ext & 3) == 2) {
485 od = (int16_t)read_im16(env, s);
487 od = read_im32(env, s);
493 tcg_gen_addi_i32(tmp, add, od);
498 /* brief extension word format */
499 tmp = tcg_temp_new();
500 add = gen_addr_index(s, ext, tmp);
501 if (!IS_NULL_QREG(base)) {
502 tcg_gen_add_i32(tmp, add, base);
504 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
506 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
513 /* Sign or zero extend a value. */
515 static inline void gen_ext(TCGv res, TCGv val, int opsize, int sign)
520 tcg_gen_ext8s_i32(res, val);
522 tcg_gen_ext8u_i32(res, val);
527 tcg_gen_ext16s_i32(res, val);
529 tcg_gen_ext16u_i32(res, val);
533 tcg_gen_mov_i32(res, val);
536 g_assert_not_reached();
540 /* Evaluate all the CC flags. */
542 static void gen_flush_flags(DisasContext *s)
553 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
554 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
555 /* Compute signed overflow for addition. */
558 tcg_gen_sub_i32(t0, QREG_CC_N, QREG_CC_V);
559 gen_ext(t0, t0, s->cc_op - CC_OP_ADDB, 1);
560 tcg_gen_xor_i32(t1, QREG_CC_N, QREG_CC_V);
561 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
563 tcg_gen_andc_i32(QREG_CC_V, t1, QREG_CC_V);
570 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
571 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
572 /* Compute signed overflow for subtraction. */
575 tcg_gen_add_i32(t0, QREG_CC_N, QREG_CC_V);
576 gen_ext(t0, t0, s->cc_op - CC_OP_SUBB, 1);
577 tcg_gen_xor_i32(t1, QREG_CC_N, t0);
578 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, t0);
580 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t1);
587 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_C, QREG_CC_N, QREG_CC_V);
588 tcg_gen_sub_i32(QREG_CC_Z, QREG_CC_N, QREG_CC_V);
589 gen_ext(QREG_CC_Z, QREG_CC_Z, s->cc_op - CC_OP_CMPB, 1);
590 /* Compute signed overflow for subtraction. */
592 tcg_gen_xor_i32(t0, QREG_CC_Z, QREG_CC_N);
593 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_V, QREG_CC_N);
594 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, t0);
596 tcg_gen_mov_i32(QREG_CC_N, QREG_CC_Z);
600 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
601 tcg_gen_movi_i32(QREG_CC_C, 0);
602 tcg_gen_movi_i32(QREG_CC_V, 0);
606 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
611 t0 = tcg_const_i32(s->cc_op);
612 gen_helper_flush_flags(cpu_env, t0);
618 /* Note that flush_flags also assigned to env->cc_op. */
619 s->cc_op = CC_OP_FLAGS;
622 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
626 if (opsize == OS_LONG) {
629 tmp = tcg_temp_new();
630 gen_ext(tmp, val, opsize, sign);
636 static void gen_logic_cc(DisasContext *s, TCGv val, int opsize)
638 gen_ext(QREG_CC_N, val, opsize, 1);
639 set_cc_op(s, CC_OP_LOGIC);
642 static void gen_update_cc_cmp(DisasContext *s, TCGv dest, TCGv src, int opsize)
644 tcg_gen_mov_i32(QREG_CC_N, dest);
645 tcg_gen_mov_i32(QREG_CC_V, src);
646 set_cc_op(s, CC_OP_CMPB + opsize);
649 static void gen_update_cc_add(TCGv dest, TCGv src, int opsize)
651 gen_ext(QREG_CC_N, dest, opsize, 1);
652 tcg_gen_mov_i32(QREG_CC_V, src);
655 static inline int opsize_bytes(int opsize)
658 case OS_BYTE: return 1;
659 case OS_WORD: return 2;
660 case OS_LONG: return 4;
661 case OS_SINGLE: return 4;
662 case OS_DOUBLE: return 8;
663 case OS_EXTENDED: return 12;
664 case OS_PACKED: return 12;
666 g_assert_not_reached();
670 static inline int insn_opsize(int insn)
672 switch ((insn >> 6) & 3) {
673 case 0: return OS_BYTE;
674 case 1: return OS_WORD;
675 case 2: return OS_LONG;
677 g_assert_not_reached();
681 static inline int ext_opsize(int ext, int pos)
683 switch ((ext >> pos) & 7) {
684 case 0: return OS_LONG;
685 case 1: return OS_SINGLE;
686 case 2: return OS_EXTENDED;
687 case 3: return OS_PACKED;
688 case 4: return OS_WORD;
689 case 5: return OS_DOUBLE;
690 case 6: return OS_BYTE;
692 g_assert_not_reached();
696 /* Assign value to a register. If the width is less than the register width
697 only the low part of the register is set. */
698 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
703 tcg_gen_andi_i32(reg, reg, 0xffffff00);
704 tmp = tcg_temp_new();
705 tcg_gen_ext8u_i32(tmp, val);
706 tcg_gen_or_i32(reg, reg, tmp);
710 tcg_gen_andi_i32(reg, reg, 0xffff0000);
711 tmp = tcg_temp_new();
712 tcg_gen_ext16u_i32(tmp, val);
713 tcg_gen_or_i32(reg, reg, tmp);
718 tcg_gen_mov_i32(reg, val);
721 g_assert_not_reached();
725 /* Generate code for an "effective address". Does not adjust the base
726 register for autoincrement addressing modes. */
727 static TCGv gen_lea_mode(CPUM68KState *env, DisasContext *s,
728 int mode, int reg0, int opsize)
736 case 0: /* Data register direct. */
737 case 1: /* Address register direct. */
739 case 3: /* Indirect postincrement. */
740 if (opsize == OS_UNSIZED) {
744 case 2: /* Indirect register */
745 return get_areg(s, reg0);
746 case 4: /* Indirect predecrememnt. */
747 if (opsize == OS_UNSIZED) {
750 reg = get_areg(s, reg0);
751 tmp = tcg_temp_new();
752 if (reg0 == 7 && opsize == OS_BYTE &&
753 m68k_feature(s->env, M68K_FEATURE_M68000)) {
754 tcg_gen_subi_i32(tmp, reg, 2);
756 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
759 case 5: /* Indirect displacement. */
760 reg = get_areg(s, reg0);
761 tmp = tcg_temp_new();
762 ext = read_im16(env, s);
763 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
765 case 6: /* Indirect index + displacement. */
766 reg = get_areg(s, reg0);
767 return gen_lea_indexed(env, s, reg);
770 case 0: /* Absolute short. */
771 offset = (int16_t)read_im16(env, s);
772 return tcg_const_i32(offset);
773 case 1: /* Absolute long. */
774 offset = read_im32(env, s);
775 return tcg_const_i32(offset);
776 case 2: /* pc displacement */
778 offset += (int16_t)read_im16(env, s);
779 return tcg_const_i32(offset);
780 case 3: /* pc index+displacement. */
781 return gen_lea_indexed(env, s, NULL_QREG);
782 case 4: /* Immediate. */
787 /* Should never happen. */
791 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
794 int mode = extract32(insn, 3, 3);
795 int reg0 = REG(insn, 0);
796 return gen_lea_mode(env, s, mode, reg0, opsize);
799 /* Generate code to load/store a value from/into an EA. If WHAT > 0 this is
800 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
801 ADDRP is non-null for readwrite operands. */
802 static TCGv gen_ea_mode(CPUM68KState *env, DisasContext *s, int mode, int reg0,
803 int opsize, TCGv val, TCGv *addrp, ea_what what)
805 TCGv reg, tmp, result;
809 case 0: /* Data register direct. */
810 reg = cpu_dregs[reg0];
811 if (what == EA_STORE) {
812 gen_partset_reg(opsize, reg, val);
815 return gen_extend(reg, opsize, what == EA_LOADS);
817 case 1: /* Address register direct. */
818 reg = get_areg(s, reg0);
819 if (what == EA_STORE) {
820 tcg_gen_mov_i32(reg, val);
823 return gen_extend(reg, opsize, what == EA_LOADS);
825 case 2: /* Indirect register */
826 reg = get_areg(s, reg0);
827 return gen_ldst(s, opsize, reg, val, what);
828 case 3: /* Indirect postincrement. */
829 reg = get_areg(s, reg0);
830 result = gen_ldst(s, opsize, reg, val, what);
831 if (what == EA_STORE || !addrp) {
832 TCGv tmp = tcg_temp_new();
833 if (reg0 == 7 && opsize == OS_BYTE &&
834 m68k_feature(s->env, M68K_FEATURE_M68000)) {
835 tcg_gen_addi_i32(tmp, reg, 2);
837 tcg_gen_addi_i32(tmp, reg, opsize_bytes(opsize));
839 delay_set_areg(s, reg0, tmp, true);
842 case 4: /* Indirect predecrememnt. */
843 if (addrp && what == EA_STORE) {
846 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
847 if (IS_NULL_QREG(tmp)) {
854 result = gen_ldst(s, opsize, tmp, val, what);
855 if (what == EA_STORE || !addrp) {
856 delay_set_areg(s, reg0, tmp, false);
859 case 5: /* Indirect displacement. */
860 case 6: /* Indirect index + displacement. */
862 if (addrp && what == EA_STORE) {
865 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
866 if (IS_NULL_QREG(tmp)) {
873 return gen_ldst(s, opsize, tmp, val, what);
876 case 0: /* Absolute short. */
877 case 1: /* Absolute long. */
878 case 2: /* pc displacement */
879 case 3: /* pc index+displacement. */
881 case 4: /* Immediate. */
882 /* Sign extend values for consistency. */
885 if (what == EA_LOADS) {
886 offset = (int8_t)read_im8(env, s);
888 offset = read_im8(env, s);
892 if (what == EA_LOADS) {
893 offset = (int16_t)read_im16(env, s);
895 offset = read_im16(env, s);
899 offset = read_im32(env, s);
902 g_assert_not_reached();
904 return tcg_const_i32(offset);
909 /* Should never happen. */
913 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
914 int opsize, TCGv val, TCGv *addrp, ea_what what)
916 int mode = extract32(insn, 3, 3);
917 int reg0 = REG(insn, 0);
918 return gen_ea_mode(env, s, mode, reg0, opsize, val, addrp, what);
921 static TCGv_ptr gen_fp_ptr(int freg)
923 TCGv_ptr fp = tcg_temp_new_ptr();
924 tcg_gen_addi_ptr(fp, cpu_env, offsetof(CPUM68KState, fregs[freg]));
928 static TCGv_ptr gen_fp_result_ptr(void)
930 TCGv_ptr fp = tcg_temp_new_ptr();
931 tcg_gen_addi_ptr(fp, cpu_env, offsetof(CPUM68KState, fp_result));
935 static void gen_fp_move(TCGv_ptr dest, TCGv_ptr src)
940 t32 = tcg_temp_new();
941 tcg_gen_ld16u_i32(t32, src, offsetof(FPReg, l.upper));
942 tcg_gen_st16_i32(t32, dest, offsetof(FPReg, l.upper));
945 t64 = tcg_temp_new_i64();
946 tcg_gen_ld_i64(t64, src, offsetof(FPReg, l.lower));
947 tcg_gen_st_i64(t64, dest, offsetof(FPReg, l.lower));
948 tcg_temp_free_i64(t64);
951 static void gen_load_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp)
955 int index = IS_USER(s);
957 t64 = tcg_temp_new_i64();
958 tmp = tcg_temp_new();
961 tcg_gen_qemu_ld8s(tmp, addr, index);
962 gen_helper_exts32(cpu_env, fp, tmp);
965 tcg_gen_qemu_ld16s(tmp, addr, index);
966 gen_helper_exts32(cpu_env, fp, tmp);
969 tcg_gen_qemu_ld32u(tmp, addr, index);
970 gen_helper_exts32(cpu_env, fp, tmp);
973 tcg_gen_qemu_ld32u(tmp, addr, index);
974 gen_helper_extf32(cpu_env, fp, tmp);
977 tcg_gen_qemu_ld64(t64, addr, index);
978 gen_helper_extf64(cpu_env, fp, t64);
979 tcg_temp_free_i64(t64);
982 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
983 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
986 tcg_gen_qemu_ld32u(tmp, addr, index);
987 tcg_gen_shri_i32(tmp, tmp, 16);
988 tcg_gen_st16_i32(tmp, fp, offsetof(FPReg, l.upper));
989 tcg_gen_addi_i32(tmp, addr, 4);
990 tcg_gen_qemu_ld64(t64, tmp, index);
991 tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
994 /* unimplemented data type on 68040/ColdFire
995 * FIXME if needed for another FPU
997 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1000 g_assert_not_reached();
1003 tcg_temp_free_i64(t64);
1004 gen_throws_exception = gen_last_qop;
1007 static void gen_store_fp(DisasContext *s, int opsize, TCGv addr, TCGv_ptr fp)
1011 int index = IS_USER(s);
1013 t64 = tcg_temp_new_i64();
1014 tmp = tcg_temp_new();
1017 gen_helper_reds32(tmp, cpu_env, fp);
1018 tcg_gen_qemu_st8(tmp, addr, index);
1021 gen_helper_reds32(tmp, cpu_env, fp);
1022 tcg_gen_qemu_st16(tmp, addr, index);
1025 gen_helper_reds32(tmp, cpu_env, fp);
1026 tcg_gen_qemu_st32(tmp, addr, index);
1029 gen_helper_redf32(tmp, cpu_env, fp);
1030 tcg_gen_qemu_st32(tmp, addr, index);
1033 gen_helper_redf64(t64, cpu_env, fp);
1034 tcg_gen_qemu_st64(t64, addr, index);
1037 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
1038 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1041 tcg_gen_ld16u_i32(tmp, fp, offsetof(FPReg, l.upper));
1042 tcg_gen_shli_i32(tmp, tmp, 16);
1043 tcg_gen_qemu_st32(tmp, addr, index);
1044 tcg_gen_addi_i32(tmp, addr, 4);
1045 tcg_gen_ld_i64(t64, fp, offsetof(FPReg, l.lower));
1046 tcg_gen_qemu_st64(t64, tmp, index);
1049 /* unimplemented data type on 68040/ColdFire
1050 * FIXME if needed for another FPU
1052 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1055 g_assert_not_reached();
1058 tcg_temp_free_i64(t64);
1059 gen_throws_exception = gen_last_qop;
1062 static void gen_ldst_fp(DisasContext *s, int opsize, TCGv addr,
1063 TCGv_ptr fp, ea_what what)
1065 if (what == EA_STORE) {
1066 gen_store_fp(s, opsize, addr, fp);
1068 gen_load_fp(s, opsize, addr, fp);
1072 static int gen_ea_mode_fp(CPUM68KState *env, DisasContext *s, int mode,
1073 int reg0, int opsize, TCGv_ptr fp, ea_what what)
1075 TCGv reg, addr, tmp;
1079 case 0: /* Data register direct. */
1080 reg = cpu_dregs[reg0];
1081 if (what == EA_STORE) {
1086 gen_helper_reds32(reg, cpu_env, fp);
1089 gen_helper_redf32(reg, cpu_env, fp);
1092 g_assert_not_reached();
1095 tmp = tcg_temp_new();
1098 tcg_gen_ext8s_i32(tmp, reg);
1099 gen_helper_exts32(cpu_env, fp, tmp);
1102 tcg_gen_ext16s_i32(tmp, reg);
1103 gen_helper_exts32(cpu_env, fp, tmp);
1106 gen_helper_exts32(cpu_env, fp, reg);
1109 gen_helper_extf32(cpu_env, fp, reg);
1112 g_assert_not_reached();
1117 case 1: /* Address register direct. */
1119 case 2: /* Indirect register */
1120 addr = get_areg(s, reg0);
1121 gen_ldst_fp(s, opsize, addr, fp, what);
1123 case 3: /* Indirect postincrement. */
1124 addr = cpu_aregs[reg0];
1125 gen_ldst_fp(s, opsize, addr, fp, what);
1126 tcg_gen_addi_i32(addr, addr, opsize_bytes(opsize));
1128 case 4: /* Indirect predecrememnt. */
1129 addr = gen_lea_mode(env, s, mode, reg0, opsize);
1130 if (IS_NULL_QREG(addr)) {
1133 gen_ldst_fp(s, opsize, addr, fp, what);
1134 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
1136 case 5: /* Indirect displacement. */
1137 case 6: /* Indirect index + displacement. */
1139 addr = gen_lea_mode(env, s, mode, reg0, opsize);
1140 if (IS_NULL_QREG(addr)) {
1143 gen_ldst_fp(s, opsize, addr, fp, what);
1147 case 0: /* Absolute short. */
1148 case 1: /* Absolute long. */
1149 case 2: /* pc displacement */
1150 case 3: /* pc index+displacement. */
1152 case 4: /* Immediate. */
1153 if (what == EA_STORE) {
1158 tmp = tcg_const_i32((int8_t)read_im8(env, s));
1159 gen_helper_exts32(cpu_env, fp, tmp);
1163 tmp = tcg_const_i32((int16_t)read_im16(env, s));
1164 gen_helper_exts32(cpu_env, fp, tmp);
1168 tmp = tcg_const_i32(read_im32(env, s));
1169 gen_helper_exts32(cpu_env, fp, tmp);
1173 tmp = tcg_const_i32(read_im32(env, s));
1174 gen_helper_extf32(cpu_env, fp, tmp);
1178 t64 = tcg_const_i64(read_im64(env, s));
1179 gen_helper_extf64(cpu_env, fp, t64);
1180 tcg_temp_free_i64(t64);
1183 if (m68k_feature(s->env, M68K_FEATURE_CF_FPU)) {
1184 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1187 tmp = tcg_const_i32(read_im32(env, s) >> 16);
1188 tcg_gen_st16_i32(tmp, fp, offsetof(FPReg, l.upper));
1190 t64 = tcg_const_i64(read_im64(env, s));
1191 tcg_gen_st_i64(t64, fp, offsetof(FPReg, l.lower));
1192 tcg_temp_free_i64(t64);
1195 /* unimplemented data type on 68040/ColdFire
1196 * FIXME if needed for another FPU
1198 gen_exception(s, s->insn_pc, EXCP_FP_UNIMP);
1201 g_assert_not_reached();
1211 static int gen_ea_fp(CPUM68KState *env, DisasContext *s, uint16_t insn,
1212 int opsize, TCGv_ptr fp, ea_what what)
1214 int mode = extract32(insn, 3, 3);
1215 int reg0 = REG(insn, 0);
1216 return gen_ea_mode_fp(env, s, mode, reg0, opsize, fp, what);
1227 static void gen_cc_cond(DisasCompare *c, DisasContext *s, int cond)
1233 /* The CC_OP_CMP form can handle most normal comparisons directly. */
1234 if (op == CC_OP_CMPB || op == CC_OP_CMPW || op == CC_OP_CMPL) {
1241 tcond = TCG_COND_LEU;
1245 tcond = TCG_COND_LTU;
1249 tcond = TCG_COND_EQ;
1254 c->v2 = tcg_const_i32(0);
1255 c->v1 = tmp = tcg_temp_new();
1256 tcg_gen_sub_i32(tmp, QREG_CC_N, QREG_CC_V);
1257 gen_ext(tmp, tmp, op - CC_OP_CMPB, 1);
1261 tcond = TCG_COND_LT;
1265 tcond = TCG_COND_LE;
1272 c->v2 = tcg_const_i32(0);
1278 tcond = TCG_COND_NEVER;
1280 case 14: /* GT (!(Z || (N ^ V))) */
1281 case 15: /* LE (Z || (N ^ V)) */
1282 /* Logic operations clear V, which simplifies LE to (Z || N),
1283 and since Z and N are co-located, this becomes a normal
1285 if (op == CC_OP_LOGIC) {
1287 tcond = TCG_COND_LE;
1291 case 12: /* GE (!(N ^ V)) */
1292 case 13: /* LT (N ^ V) */
1293 /* Logic operations clear V, which simplifies this to N. */
1294 if (op != CC_OP_LOGIC) {
1298 case 10: /* PL (!N) */
1299 case 11: /* MI (N) */
1300 /* Several cases represent N normally. */
1301 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1302 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
1303 op == CC_OP_LOGIC) {
1305 tcond = TCG_COND_LT;
1309 case 6: /* NE (!Z) */
1310 case 7: /* EQ (Z) */
1311 /* Some cases fold Z into N. */
1312 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1313 op == CC_OP_SUBB || op == CC_OP_SUBW || op == CC_OP_SUBL ||
1314 op == CC_OP_LOGIC) {
1315 tcond = TCG_COND_EQ;
1320 case 4: /* CC (!C) */
1321 case 5: /* CS (C) */
1322 /* Some cases fold C into X. */
1323 if (op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL ||
1324 op == CC_OP_ADDB || op == CC_OP_ADDW || op == CC_OP_ADDL) {
1325 tcond = TCG_COND_NE;
1330 case 8: /* VC (!V) */
1331 case 9: /* VS (V) */
1332 /* Logic operations clear V and C. */
1333 if (op == CC_OP_LOGIC) {
1334 tcond = TCG_COND_NEVER;
1341 /* Otherwise, flush flag state to CC_OP_FLAGS. */
1348 /* Invalid, or handled above. */
1350 case 2: /* HI (!C && !Z) -> !(C || Z)*/
1351 case 3: /* LS (C || Z) */
1352 c->v1 = tmp = tcg_temp_new();
1354 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
1355 tcg_gen_or_i32(tmp, tmp, QREG_CC_C);
1356 tcond = TCG_COND_NE;
1358 case 4: /* CC (!C) */
1359 case 5: /* CS (C) */
1361 tcond = TCG_COND_NE;
1363 case 6: /* NE (!Z) */
1364 case 7: /* EQ (Z) */
1366 tcond = TCG_COND_EQ;
1368 case 8: /* VC (!V) */
1369 case 9: /* VS (V) */
1371 tcond = TCG_COND_LT;
1373 case 10: /* PL (!N) */
1374 case 11: /* MI (N) */
1376 tcond = TCG_COND_LT;
1378 case 12: /* GE (!(N ^ V)) */
1379 case 13: /* LT (N ^ V) */
1380 c->v1 = tmp = tcg_temp_new();
1382 tcg_gen_xor_i32(tmp, QREG_CC_N, QREG_CC_V);
1383 tcond = TCG_COND_LT;
1385 case 14: /* GT (!(Z || (N ^ V))) */
1386 case 15: /* LE (Z || (N ^ V)) */
1387 c->v1 = tmp = tcg_temp_new();
1389 tcg_gen_setcond_i32(TCG_COND_EQ, tmp, QREG_CC_Z, c->v2);
1390 tcg_gen_neg_i32(tmp, tmp);
1391 tmp2 = tcg_temp_new();
1392 tcg_gen_xor_i32(tmp2, QREG_CC_N, QREG_CC_V);
1393 tcg_gen_or_i32(tmp, tmp, tmp2);
1394 tcg_temp_free(tmp2);
1395 tcond = TCG_COND_LT;
1400 if ((cond & 1) == 0) {
1401 tcond = tcg_invert_cond(tcond);
1406 static void free_cond(DisasCompare *c)
1409 tcg_temp_free(c->v1);
1412 tcg_temp_free(c->v2);
1416 static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
1420 gen_cc_cond(&c, s, cond);
1422 tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
1426 /* Force a TB lookup after an instruction that changes the CPU state. */
1427 static void gen_lookup_tb(DisasContext *s)
1430 tcg_gen_movi_i32(QREG_PC, s->pc);
1431 s->is_jmp = DISAS_UPDATE;
1434 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
1435 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
1436 op_sign ? EA_LOADS : EA_LOADU); \
1437 if (IS_NULL_QREG(result)) { \
1438 gen_addr_fault(s); \
1443 #define DEST_EA(env, insn, opsize, val, addrp) do { \
1444 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
1445 if (IS_NULL_QREG(ea_result)) { \
1446 gen_addr_fault(s); \
1451 static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
1453 #ifndef CONFIG_USER_ONLY
1454 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
1455 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
1461 /* Generate a jump to an immediate address. */
1462 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
1464 if (unlikely(s->singlestep_enabled)) {
1465 gen_exception(s, dest, EXCP_DEBUG);
1466 } else if (use_goto_tb(s, dest)) {
1468 tcg_gen_movi_i32(QREG_PC, dest);
1469 tcg_gen_exit_tb((uintptr_t)s->tb + n);
1471 gen_jmp_im(s, dest);
1474 s->is_jmp = DISAS_TB_JUMP;
1483 cond = (insn >> 8) & 0xf;
1484 gen_cc_cond(&c, s, cond);
1486 tmp = tcg_temp_new();
1487 tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
1490 tcg_gen_neg_i32(tmp, tmp);
1491 DEST_EA(env, insn, OS_BYTE, tmp, NULL);
1503 reg = DREG(insn, 0);
1505 offset = (int16_t)read_im16(env, s);
1506 l1 = gen_new_label();
1507 gen_jmpcc(s, (insn >> 8) & 0xf, l1);
1509 tmp = tcg_temp_new();
1510 tcg_gen_ext16s_i32(tmp, reg);
1511 tcg_gen_addi_i32(tmp, tmp, -1);
1512 gen_partset_reg(OS_WORD, reg, tmp);
1513 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, -1, l1);
1514 gen_jmp_tb(s, 1, base + offset);
1516 gen_jmp_tb(s, 0, s->pc);
1519 DISAS_INSN(undef_mac)
1521 gen_exception(s, s->pc - 2, EXCP_LINEA);
1524 DISAS_INSN(undef_fpu)
1526 gen_exception(s, s->pc - 2, EXCP_LINEF);
1531 /* ??? This is both instructions that are as yet unimplemented
1532 for the 680x0 series, as well as those that are implemented
1533 but actually illegal for CPU32 or pre-68020. */
1534 qemu_log_mask(LOG_UNIMP, "Illegal instruction: %04x @ %08x",
1536 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
1546 sign = (insn & 0x100) != 0;
1547 reg = DREG(insn, 9);
1548 tmp = tcg_temp_new();
1550 tcg_gen_ext16s_i32(tmp, reg);
1552 tcg_gen_ext16u_i32(tmp, reg);
1553 SRC_EA(env, src, OS_WORD, sign, NULL);
1554 tcg_gen_mul_i32(tmp, tmp, src);
1555 tcg_gen_mov_i32(reg, tmp);
1556 gen_logic_cc(s, tmp, OS_LONG);
1566 /* divX.w <EA>,Dn 32/16 -> 16r:16q */
1568 sign = (insn & 0x100) != 0;
1570 /* dest.l / src.w */
1572 SRC_EA(env, src, OS_WORD, sign, NULL);
1573 destr = tcg_const_i32(REG(insn, 9));
1575 gen_helper_divsw(cpu_env, destr, src);
1577 gen_helper_divuw(cpu_env, destr, src);
1579 tcg_temp_free(destr);
1581 set_cc_op(s, CC_OP_FLAGS);
1590 ext = read_im16(env, s);
1592 sign = (ext & 0x0800) != 0;
1595 if (!m68k_feature(s->env, M68K_FEATURE_QUAD_MULDIV)) {
1596 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
1600 /* divX.l <EA>, Dr:Dq 64/32 -> 32r:32q */
1602 SRC_EA(env, den, OS_LONG, 0, NULL);
1603 num = tcg_const_i32(REG(ext, 12));
1604 reg = tcg_const_i32(REG(ext, 0));
1606 gen_helper_divsll(cpu_env, num, reg, den);
1608 gen_helper_divull(cpu_env, num, reg, den);
1612 set_cc_op(s, CC_OP_FLAGS);
1616 /* divX.l <EA>, Dq 32/32 -> 32q */
1617 /* divXl.l <EA>, Dr:Dq 32/32 -> 32r:32q */
1619 SRC_EA(env, den, OS_LONG, 0, NULL);
1620 num = tcg_const_i32(REG(ext, 12));
1621 reg = tcg_const_i32(REG(ext, 0));
1623 gen_helper_divsl(cpu_env, num, reg, den);
1625 gen_helper_divul(cpu_env, num, reg, den);
1630 set_cc_op(s, CC_OP_FLAGS);
1633 static void bcd_add(TCGv dest, TCGv src)
1637 /* dest10 = dest10 + src10 + X
1641 * t3 = t2 + dest + X
1645 * t7 = (t6 >> 2) | (t6 >> 3)
1649 /* t1 = (src + 0x066) + dest + X
1650 * = result with some possible exceding 0x6
1653 t0 = tcg_const_i32(0x066);
1654 tcg_gen_add_i32(t0, t0, src);
1656 t1 = tcg_temp_new();
1657 tcg_gen_add_i32(t1, t0, dest);
1658 tcg_gen_add_i32(t1, t1, QREG_CC_X);
1660 /* we will remove exceding 0x6 where there is no carry */
1662 /* t0 = (src + 0x0066) ^ dest
1663 * = t1 without carries
1666 tcg_gen_xor_i32(t0, t0, dest);
1668 /* extract the carries
1670 * = only the carries
1673 tcg_gen_xor_i32(t0, t0, t1);
1675 /* generate 0x1 where there is no carry
1676 * and for each 0x10, generate a 0x6
1679 tcg_gen_shri_i32(t0, t0, 3);
1680 tcg_gen_not_i32(t0, t0);
1681 tcg_gen_andi_i32(t0, t0, 0x22);
1682 tcg_gen_add_i32(dest, t0, t0);
1683 tcg_gen_add_i32(dest, dest, t0);
1686 /* remove the exceding 0x6
1687 * for digits that have not generated a carry
1690 tcg_gen_sub_i32(dest, t1, dest);
1694 static void bcd_sub(TCGv dest, TCGv src)
1698 /* dest10 = dest10 - src10 - X
1699 * = bcd_add(dest + 1 - X, 0x199 - src)
1702 /* t0 = 0x066 + (0x199 - src) */
1704 t0 = tcg_temp_new();
1705 tcg_gen_subfi_i32(t0, 0x1ff, src);
1707 /* t1 = t0 + dest + 1 - X*/
1709 t1 = tcg_temp_new();
1710 tcg_gen_add_i32(t1, t0, dest);
1711 tcg_gen_addi_i32(t1, t1, 1);
1712 tcg_gen_sub_i32(t1, t1, QREG_CC_X);
1714 /* t2 = t0 ^ dest */
1716 t2 = tcg_temp_new();
1717 tcg_gen_xor_i32(t2, t0, dest);
1721 tcg_gen_xor_i32(t0, t1, t2);
1724 * t0 = (t2 >> 2) | (t2 >> 3)
1726 * to fit on 8bit operands, changed in:
1728 * t2 = ~(t0 >> 3) & 0x22
1733 tcg_gen_shri_i32(t2, t0, 3);
1734 tcg_gen_not_i32(t2, t2);
1735 tcg_gen_andi_i32(t2, t2, 0x22);
1736 tcg_gen_add_i32(t0, t2, t2);
1737 tcg_gen_add_i32(t0, t0, t2);
1740 /* return t1 - t0 */
1742 tcg_gen_sub_i32(dest, t1, t0);
1747 static void bcd_flags(TCGv val)
1749 tcg_gen_andi_i32(QREG_CC_C, val, 0x0ff);
1750 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_C);
1752 tcg_gen_shri_i32(QREG_CC_C, val, 8);
1753 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
1755 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
1758 DISAS_INSN(abcd_reg)
1763 gen_flush_flags(s); /* !Z is sticky */
1765 src = gen_extend(DREG(insn, 0), OS_BYTE, 0);
1766 dest = gen_extend(DREG(insn, 9), OS_BYTE, 0);
1768 gen_partset_reg(OS_BYTE, DREG(insn, 9), dest);
1773 DISAS_INSN(abcd_mem)
1775 TCGv src, dest, addr;
1777 gen_flush_flags(s); /* !Z is sticky */
1779 /* Indirect pre-decrement load (mode 4) */
1781 src = gen_ea_mode(env, s, 4, REG(insn, 0), OS_BYTE,
1782 NULL_QREG, NULL, EA_LOADU);
1783 dest = gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE,
1784 NULL_QREG, &addr, EA_LOADU);
1788 gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE, dest, &addr, EA_STORE);
1793 DISAS_INSN(sbcd_reg)
1797 gen_flush_flags(s); /* !Z is sticky */
1799 src = gen_extend(DREG(insn, 0), OS_BYTE, 0);
1800 dest = gen_extend(DREG(insn, 9), OS_BYTE, 0);
1804 gen_partset_reg(OS_BYTE, DREG(insn, 9), dest);
1809 DISAS_INSN(sbcd_mem)
1811 TCGv src, dest, addr;
1813 gen_flush_flags(s); /* !Z is sticky */
1815 /* Indirect pre-decrement load (mode 4) */
1817 src = gen_ea_mode(env, s, 4, REG(insn, 0), OS_BYTE,
1818 NULL_QREG, NULL, EA_LOADU);
1819 dest = gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE,
1820 NULL_QREG, &addr, EA_LOADU);
1824 gen_ea_mode(env, s, 4, REG(insn, 9), OS_BYTE, dest, &addr, EA_STORE);
1834 gen_flush_flags(s); /* !Z is sticky */
1836 SRC_EA(env, src, OS_BYTE, 0, &addr);
1838 dest = tcg_const_i32(0);
1841 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1845 tcg_temp_free(dest);
1858 add = (insn & 0x4000) != 0;
1859 opsize = insn_opsize(insn);
1860 reg = gen_extend(DREG(insn, 9), opsize, 1);
1861 dest = tcg_temp_new();
1863 SRC_EA(env, tmp, opsize, 1, &addr);
1867 SRC_EA(env, src, opsize, 1, NULL);
1870 tcg_gen_add_i32(dest, tmp, src);
1871 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, src);
1872 set_cc_op(s, CC_OP_ADDB + opsize);
1874 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, tmp, src);
1875 tcg_gen_sub_i32(dest, tmp, src);
1876 set_cc_op(s, CC_OP_SUBB + opsize);
1878 gen_update_cc_add(dest, src, opsize);
1880 DEST_EA(env, insn, opsize, dest, &addr);
1882 gen_partset_reg(opsize, DREG(insn, 9), dest);
1884 tcg_temp_free(dest);
1887 /* Reverse the order of the bits in REG. */
1891 reg = DREG(insn, 0);
1892 gen_helper_bitrev(reg, reg);
1895 DISAS_INSN(bitop_reg)
1905 if ((insn & 0x38) != 0)
1909 op = (insn >> 6) & 3;
1910 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1913 src2 = tcg_temp_new();
1914 if (opsize == OS_BYTE)
1915 tcg_gen_andi_i32(src2, DREG(insn, 9), 7);
1917 tcg_gen_andi_i32(src2, DREG(insn, 9), 31);
1919 tmp = tcg_const_i32(1);
1920 tcg_gen_shl_i32(tmp, tmp, src2);
1921 tcg_temp_free(src2);
1923 tcg_gen_and_i32(QREG_CC_Z, src1, tmp);
1925 dest = tcg_temp_new();
1928 tcg_gen_xor_i32(dest, src1, tmp);
1931 tcg_gen_andc_i32(dest, src1, tmp);
1934 tcg_gen_or_i32(dest, src1, tmp);
1941 DEST_EA(env, insn, opsize, dest, &addr);
1943 tcg_temp_free(dest);
1949 reg = DREG(insn, 0);
1951 gen_helper_sats(reg, reg, QREG_CC_V);
1952 gen_logic_cc(s, reg, OS_LONG);
1955 static void gen_push(DisasContext *s, TCGv val)
1959 tmp = tcg_temp_new();
1960 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1961 gen_store(s, OS_LONG, tmp, val);
1962 tcg_gen_mov_i32(QREG_SP, tmp);
1966 static TCGv mreg(int reg)
1970 return cpu_dregs[reg];
1973 return cpu_aregs[reg & 7];
1978 TCGv addr, incr, tmp, r[16];
1979 int is_load = (insn & 0x0400) != 0;
1980 int opsize = (insn & 0x40) != 0 ? OS_LONG : OS_WORD;
1981 uint16_t mask = read_im16(env, s);
1982 int mode = extract32(insn, 3, 3);
1983 int reg0 = REG(insn, 0);
1986 tmp = cpu_aregs[reg0];
1989 case 0: /* data register direct */
1990 case 1: /* addr register direct */
1995 case 2: /* indirect */
1998 case 3: /* indirect post-increment */
2000 /* post-increment is not allowed */
2005 case 4: /* indirect pre-decrement */
2007 /* pre-decrement is not allowed */
2010 /* We want a bare copy of the address reg, without any pre-decrement
2011 adjustment, as gen_lea would provide. */
2015 tmp = gen_lea_mode(env, s, mode, reg0, opsize);
2016 if (IS_NULL_QREG(tmp)) {
2022 addr = tcg_temp_new();
2023 tcg_gen_mov_i32(addr, tmp);
2024 incr = tcg_const_i32(opsize_bytes(opsize));
2027 /* memory to register */
2028 for (i = 0; i < 16; i++) {
2029 if (mask & (1 << i)) {
2030 r[i] = gen_load(s, opsize, addr, 1);
2031 tcg_gen_add_i32(addr, addr, incr);
2034 for (i = 0; i < 16; i++) {
2035 if (mask & (1 << i)) {
2036 tcg_gen_mov_i32(mreg(i), r[i]);
2037 tcg_temp_free(r[i]);
2041 /* post-increment: movem (An)+,X */
2042 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
2045 /* register to memory */
2047 /* pre-decrement: movem X,-(An) */
2048 for (i = 15; i >= 0; i--) {
2049 if ((mask << i) & 0x8000) {
2050 tcg_gen_sub_i32(addr, addr, incr);
2051 if (reg0 + 8 == i &&
2052 m68k_feature(s->env, M68K_FEATURE_EXT_FULL)) {
2053 /* M68020+: if the addressing register is the
2054 * register moved to memory, the value written
2055 * is the initial value decremented by the size of
2056 * the operation, regardless of how many actual
2057 * stores have been performed until this point.
2058 * M68000/M68010: the value is the initial value.
2060 tmp = tcg_temp_new();
2061 tcg_gen_sub_i32(tmp, cpu_aregs[reg0], incr);
2062 gen_store(s, opsize, addr, tmp);
2065 gen_store(s, opsize, addr, mreg(i));
2069 tcg_gen_mov_i32(cpu_aregs[reg0], addr);
2071 for (i = 0; i < 16; i++) {
2072 if (mask & (1 << i)) {
2073 gen_store(s, opsize, addr, mreg(i));
2074 tcg_gen_add_i32(addr, addr, incr);
2080 tcg_temp_free(incr);
2081 tcg_temp_free(addr);
2084 DISAS_INSN(bitop_im)
2094 if ((insn & 0x38) != 0)
2098 op = (insn >> 6) & 3;
2100 bitnum = read_im16(env, s);
2101 if (m68k_feature(s->env, M68K_FEATURE_M68000)) {
2102 if (bitnum & 0xfe00) {
2103 disas_undef(env, s, insn);
2107 if (bitnum & 0xff00) {
2108 disas_undef(env, s, insn);
2113 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
2116 if (opsize == OS_BYTE)
2122 tcg_gen_andi_i32(QREG_CC_Z, src1, mask);
2125 tmp = tcg_temp_new();
2128 tcg_gen_xori_i32(tmp, src1, mask);
2131 tcg_gen_andi_i32(tmp, src1, ~mask);
2134 tcg_gen_ori_i32(tmp, src1, mask);
2139 DEST_EA(env, insn, opsize, tmp, &addr);
2144 DISAS_INSN(arith_im)
2153 op = (insn >> 9) & 7;
2154 opsize = insn_opsize(insn);
2157 im = tcg_const_i32((int8_t)read_im8(env, s));
2160 im = tcg_const_i32((int16_t)read_im16(env, s));
2163 im = tcg_const_i32(read_im32(env, s));
2168 SRC_EA(env, src1, opsize, 1, (op == 6) ? NULL : &addr);
2169 dest = tcg_temp_new();
2172 tcg_gen_or_i32(dest, src1, im);
2173 gen_logic_cc(s, dest, opsize);
2176 tcg_gen_and_i32(dest, src1, im);
2177 gen_logic_cc(s, dest, opsize);
2180 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, src1, im);
2181 tcg_gen_sub_i32(dest, src1, im);
2182 gen_update_cc_add(dest, im, opsize);
2183 set_cc_op(s, CC_OP_SUBB + opsize);
2186 tcg_gen_add_i32(dest, src1, im);
2187 gen_update_cc_add(dest, im, opsize);
2188 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, im);
2189 set_cc_op(s, CC_OP_ADDB + opsize);
2192 tcg_gen_xor_i32(dest, src1, im);
2193 gen_logic_cc(s, dest, opsize);
2196 gen_update_cc_cmp(s, src1, im, opsize);
2203 DEST_EA(env, insn, opsize, dest, &addr);
2205 tcg_temp_free(dest);
2217 switch ((insn >> 9) & 3) {
2231 g_assert_not_reached();
2234 ext = read_im16(env, s);
2236 /* cas Dc,Du,<EA> */
2238 addr = gen_lea(env, s, insn, opsize);
2239 if (IS_NULL_QREG(addr)) {
2244 cmp = gen_extend(DREG(ext, 0), opsize, 1);
2246 /* if <EA> == Dc then
2248 * Dc = <EA> (because <EA> == Dc)
2253 load = tcg_temp_new();
2254 tcg_gen_atomic_cmpxchg_i32(load, addr, cmp, DREG(ext, 6),
2256 /* update flags before setting cmp to load */
2257 gen_update_cc_cmp(s, load, cmp, opsize);
2258 gen_partset_reg(opsize, DREG(ext, 0), load);
2260 tcg_temp_free(load);
2262 switch (extract32(insn, 3, 3)) {
2263 case 3: /* Indirect postincrement. */
2264 tcg_gen_addi_i32(AREG(insn, 0), addr, opsize_bytes(opsize));
2266 case 4: /* Indirect predecrememnt. */
2267 tcg_gen_mov_i32(AREG(insn, 0), addr);
2274 uint16_t ext1, ext2;
2278 /* cas2 Dc1:Dc2,Du1:Du2,(Rn1):(Rn2) */
2280 ext1 = read_im16(env, s);
2282 if (ext1 & 0x8000) {
2283 /* Address Register */
2284 addr1 = AREG(ext1, 12);
2287 addr1 = DREG(ext1, 12);
2290 ext2 = read_im16(env, s);
2291 if (ext2 & 0x8000) {
2292 /* Address Register */
2293 addr2 = AREG(ext2, 12);
2296 addr2 = DREG(ext2, 12);
2299 /* if (R1) == Dc1 && (R2) == Dc2 then
2307 regs = tcg_const_i32(REG(ext2, 6) |
2308 (REG(ext1, 6) << 3) |
2309 (REG(ext2, 0) << 6) |
2310 (REG(ext1, 0) << 9));
2311 gen_helper_cas2w(cpu_env, regs, addr1, addr2);
2312 tcg_temp_free(regs);
2314 /* Note that cas2w also assigned to env->cc_op. */
2315 s->cc_op = CC_OP_CMPW;
2316 s->cc_op_synced = 1;
2321 uint16_t ext1, ext2;
2322 TCGv addr1, addr2, regs;
2324 /* cas2 Dc1:Dc2,Du1:Du2,(Rn1):(Rn2) */
2326 ext1 = read_im16(env, s);
2328 if (ext1 & 0x8000) {
2329 /* Address Register */
2330 addr1 = AREG(ext1, 12);
2333 addr1 = DREG(ext1, 12);
2336 ext2 = read_im16(env, s);
2337 if (ext2 & 0x8000) {
2338 /* Address Register */
2339 addr2 = AREG(ext2, 12);
2342 addr2 = DREG(ext2, 12);
2345 /* if (R1) == Dc1 && (R2) == Dc2 then
2353 regs = tcg_const_i32(REG(ext2, 6) |
2354 (REG(ext1, 6) << 3) |
2355 (REG(ext2, 0) << 6) |
2356 (REG(ext1, 0) << 9));
2357 gen_helper_cas2l(cpu_env, regs, addr1, addr2);
2358 tcg_temp_free(regs);
2360 /* Note that cas2l also assigned to env->cc_op. */
2361 s->cc_op = CC_OP_CMPL;
2362 s->cc_op_synced = 1;
2369 reg = DREG(insn, 0);
2370 tcg_gen_bswap32_i32(reg, reg);
2380 switch (insn >> 12) {
2381 case 1: /* move.b */
2384 case 2: /* move.l */
2387 case 3: /* move.w */
2393 SRC_EA(env, src, opsize, 1, NULL);
2394 op = (insn >> 6) & 7;
2397 /* The value will already have been sign extended. */
2398 dest = AREG(insn, 9);
2399 tcg_gen_mov_i32(dest, src);
2403 dest_ea = ((insn >> 9) & 7) | (op << 3);
2404 DEST_EA(env, dest_ea, opsize, src, NULL);
2405 /* This will be correct because loads sign extend. */
2406 gen_logic_cc(s, src, opsize);
2417 opsize = insn_opsize(insn);
2418 SRC_EA(env, src, opsize, 1, &addr);
2420 gen_flush_flags(s); /* compute old Z */
2422 /* Perform substract with borrow.
2423 * (X, N) = -(src + X);
2426 z = tcg_const_i32(0);
2427 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, z, QREG_CC_X, z);
2428 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, z, z, QREG_CC_N, QREG_CC_X);
2430 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
2432 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
2434 /* Compute signed-overflow for negation. The normal formula for
2435 * subtraction is (res ^ src) & (src ^ dest), but with dest==0
2436 * this simplies to res & src.
2439 tcg_gen_and_i32(QREG_CC_V, QREG_CC_N, src);
2441 /* Copy the rest of the results into place. */
2442 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
2443 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
2445 set_cc_op(s, CC_OP_FLAGS);
2447 /* result is in QREG_CC_N */
2449 DEST_EA(env, insn, opsize, QREG_CC_N, &addr);
2457 reg = AREG(insn, 9);
2458 tmp = gen_lea(env, s, insn, OS_LONG);
2459 if (IS_NULL_QREG(tmp)) {
2463 tcg_gen_mov_i32(reg, tmp);
2471 zero = tcg_const_i32(0);
2473 opsize = insn_opsize(insn);
2474 DEST_EA(env, insn, opsize, zero, NULL);
2475 gen_logic_cc(s, zero, opsize);
2476 tcg_temp_free(zero);
2479 static TCGv gen_get_ccr(DisasContext *s)
2485 dest = tcg_temp_new();
2486 gen_helper_get_ccr(dest, cpu_env);
2490 DISAS_INSN(move_from_ccr)
2494 ccr = gen_get_ccr(s);
2495 DEST_EA(env, insn, OS_WORD, ccr, NULL);
2505 opsize = insn_opsize(insn);
2506 SRC_EA(env, src1, opsize, 1, &addr);
2507 dest = tcg_temp_new();
2508 tcg_gen_neg_i32(dest, src1);
2509 set_cc_op(s, CC_OP_SUBB + opsize);
2510 gen_update_cc_add(dest, src1, opsize);
2511 tcg_gen_setcondi_i32(TCG_COND_NE, QREG_CC_X, dest, 0);
2512 DEST_EA(env, insn, opsize, dest, &addr);
2513 tcg_temp_free(dest);
2516 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
2519 tcg_gen_movi_i32(QREG_CC_C, val & CCF_C ? 1 : 0);
2520 tcg_gen_movi_i32(QREG_CC_V, val & CCF_V ? -1 : 0);
2521 tcg_gen_movi_i32(QREG_CC_Z, val & CCF_Z ? 0 : 1);
2522 tcg_gen_movi_i32(QREG_CC_N, val & CCF_N ? -1 : 0);
2523 tcg_gen_movi_i32(QREG_CC_X, val & CCF_X ? 1 : 0);
2525 gen_helper_set_sr(cpu_env, tcg_const_i32(val));
2527 set_cc_op(s, CC_OP_FLAGS);
2530 static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
2533 if ((insn & 0x38) == 0) {
2535 gen_helper_set_ccr(cpu_env, DREG(insn, 0));
2537 gen_helper_set_sr(cpu_env, DREG(insn, 0));
2539 set_cc_op(s, CC_OP_FLAGS);
2540 } else if ((insn & 0x3f) == 0x3c) {
2542 val = read_im16(env, s);
2543 gen_set_sr_im(s, val, ccr_only);
2545 disas_undef(env, s, insn);
2550 DISAS_INSN(move_to_ccr)
2552 gen_set_sr(env, s, insn, 1);
2562 opsize = insn_opsize(insn);
2563 SRC_EA(env, src1, opsize, 1, &addr);
2564 dest = tcg_temp_new();
2565 tcg_gen_not_i32(dest, src1);
2566 DEST_EA(env, insn, opsize, dest, &addr);
2567 gen_logic_cc(s, dest, opsize);
2576 src1 = tcg_temp_new();
2577 src2 = tcg_temp_new();
2578 reg = DREG(insn, 0);
2579 tcg_gen_shli_i32(src1, reg, 16);
2580 tcg_gen_shri_i32(src2, reg, 16);
2581 tcg_gen_or_i32(reg, src1, src2);
2582 tcg_temp_free(src2);
2583 tcg_temp_free(src1);
2584 gen_logic_cc(s, reg, OS_LONG);
2589 gen_exception(s, s->pc - 2, EXCP_DEBUG);
2596 tmp = gen_lea(env, s, insn, OS_LONG);
2597 if (IS_NULL_QREG(tmp)) {
2610 reg = DREG(insn, 0);
2611 op = (insn >> 6) & 7;
2612 tmp = tcg_temp_new();
2614 tcg_gen_ext16s_i32(tmp, reg);
2616 tcg_gen_ext8s_i32(tmp, reg);
2618 gen_partset_reg(OS_WORD, reg, tmp);
2620 tcg_gen_mov_i32(reg, tmp);
2621 gen_logic_cc(s, tmp, OS_LONG);
2630 opsize = insn_opsize(insn);
2631 SRC_EA(env, tmp, opsize, 1, NULL);
2632 gen_logic_cc(s, tmp, opsize);
2637 /* Implemented as a NOP. */
2642 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2645 /* ??? This should be atomic. */
2652 dest = tcg_temp_new();
2653 SRC_EA(env, src1, OS_BYTE, 1, &addr);
2654 gen_logic_cc(s, src1, OS_BYTE);
2655 tcg_gen_ori_i32(dest, src1, 0x80);
2656 DEST_EA(env, insn, OS_BYTE, dest, &addr);
2657 tcg_temp_free(dest);
2666 ext = read_im16(env, s);
2671 if (!m68k_feature(s->env, M68K_FEATURE_QUAD_MULDIV)) {
2672 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
2676 SRC_EA(env, src1, OS_LONG, 0, NULL);
2679 tcg_gen_muls2_i32(QREG_CC_Z, QREG_CC_N, src1, DREG(ext, 12));
2681 tcg_gen_mulu2_i32(QREG_CC_Z, QREG_CC_N, src1, DREG(ext, 12));
2683 /* if Dl == Dh, 68040 returns low word */
2684 tcg_gen_mov_i32(DREG(ext, 0), QREG_CC_N);
2685 tcg_gen_mov_i32(DREG(ext, 12), QREG_CC_Z);
2686 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N);
2688 tcg_gen_movi_i32(QREG_CC_V, 0);
2689 tcg_gen_movi_i32(QREG_CC_C, 0);
2691 set_cc_op(s, CC_OP_FLAGS);
2694 SRC_EA(env, src1, OS_LONG, 0, NULL);
2695 if (m68k_feature(s->env, M68K_FEATURE_M68000)) {
2696 tcg_gen_movi_i32(QREG_CC_C, 0);
2698 tcg_gen_muls2_i32(QREG_CC_N, QREG_CC_V, src1, DREG(ext, 12));
2699 /* QREG_CC_V is -(QREG_CC_V != (QREG_CC_N >> 31)) */
2700 tcg_gen_sari_i32(QREG_CC_Z, QREG_CC_N, 31);
2701 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, QREG_CC_Z);
2703 tcg_gen_mulu2_i32(QREG_CC_N, QREG_CC_V, src1, DREG(ext, 12));
2704 /* QREG_CC_V is -(QREG_CC_V != 0), use QREG_CC_C as 0 */
2705 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, QREG_CC_C);
2707 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
2708 tcg_gen_mov_i32(DREG(ext, 12), QREG_CC_N);
2710 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
2712 set_cc_op(s, CC_OP_FLAGS);
2714 /* The upper 32 bits of the product are discarded, so
2715 muls.l and mulu.l are functionally equivalent. */
2716 tcg_gen_mul_i32(DREG(ext, 12), src1, DREG(ext, 12));
2717 gen_logic_cc(s, DREG(ext, 12), OS_LONG);
2721 static void gen_link(DisasContext *s, uint16_t insn, int32_t offset)
2726 reg = AREG(insn, 0);
2727 tmp = tcg_temp_new();
2728 tcg_gen_subi_i32(tmp, QREG_SP, 4);
2729 gen_store(s, OS_LONG, tmp, reg);
2730 if ((insn & 7) != 7) {
2731 tcg_gen_mov_i32(reg, tmp);
2733 tcg_gen_addi_i32(QREG_SP, tmp, offset);
2741 offset = read_im16(env, s);
2742 gen_link(s, insn, offset);
2749 offset = read_im32(env, s);
2750 gen_link(s, insn, offset);
2759 src = tcg_temp_new();
2760 reg = AREG(insn, 0);
2761 tcg_gen_mov_i32(src, reg);
2762 tmp = gen_load(s, OS_LONG, src, 0);
2763 tcg_gen_mov_i32(reg, tmp);
2764 tcg_gen_addi_i32(QREG_SP, src, 4);
2775 int16_t offset = read_im16(env, s);
2777 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
2778 tcg_gen_addi_i32(QREG_SP, QREG_SP, offset + 4);
2786 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
2787 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
2795 /* Load the target address first to ensure correct exception
2797 tmp = gen_lea(env, s, insn, OS_LONG);
2798 if (IS_NULL_QREG(tmp)) {
2802 if ((insn & 0x40) == 0) {
2804 gen_push(s, tcg_const_i32(s->pc));
2818 if ((insn & 070) == 010) {
2819 /* Operation on address register is always long. */
2822 opsize = insn_opsize(insn);
2824 SRC_EA(env, src, opsize, 1, &addr);
2825 imm = (insn >> 9) & 7;
2829 val = tcg_const_i32(imm);
2830 dest = tcg_temp_new();
2831 tcg_gen_mov_i32(dest, src);
2832 if ((insn & 0x38) == 0x08) {
2833 /* Don't update condition codes if the destination is an
2834 address register. */
2835 if (insn & 0x0100) {
2836 tcg_gen_sub_i32(dest, dest, val);
2838 tcg_gen_add_i32(dest, dest, val);
2841 if (insn & 0x0100) {
2842 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
2843 tcg_gen_sub_i32(dest, dest, val);
2844 set_cc_op(s, CC_OP_SUBB + opsize);
2846 tcg_gen_add_i32(dest, dest, val);
2847 tcg_gen_setcond_i32(TCG_COND_LTU, QREG_CC_X, dest, val);
2848 set_cc_op(s, CC_OP_ADDB + opsize);
2850 gen_update_cc_add(dest, val, opsize);
2853 DEST_EA(env, insn, opsize, dest, &addr);
2854 tcg_temp_free(dest);
2860 case 2: /* One extension word. */
2863 case 3: /* Two extension words. */
2866 case 4: /* No extension words. */
2869 disas_undef(env, s, insn);
2881 op = (insn >> 8) & 0xf;
2882 offset = (int8_t)insn;
2884 offset = (int16_t)read_im16(env, s);
2885 } else if (offset == -1) {
2886 offset = read_im32(env, s);
2890 gen_push(s, tcg_const_i32(s->pc));
2894 l1 = gen_new_label();
2895 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
2896 gen_jmp_tb(s, 1, base + offset);
2898 gen_jmp_tb(s, 0, s->pc);
2900 /* Unconditional branch. */
2901 gen_jmp_tb(s, 0, base + offset);
2907 tcg_gen_movi_i32(DREG(insn, 9), (int8_t)insn);
2908 gen_logic_cc(s, DREG(insn, 9), OS_LONG);
2921 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
2922 reg = DREG(insn, 9);
2923 tcg_gen_mov_i32(reg, src);
2924 gen_logic_cc(s, src, opsize);
2935 opsize = insn_opsize(insn);
2936 reg = gen_extend(DREG(insn, 9), opsize, 0);
2937 dest = tcg_temp_new();
2939 SRC_EA(env, src, opsize, 0, &addr);
2940 tcg_gen_or_i32(dest, src, reg);
2941 DEST_EA(env, insn, opsize, dest, &addr);
2943 SRC_EA(env, src, opsize, 0, NULL);
2944 tcg_gen_or_i32(dest, src, reg);
2945 gen_partset_reg(opsize, DREG(insn, 9), dest);
2947 gen_logic_cc(s, dest, opsize);
2948 tcg_temp_free(dest);
2956 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
2957 reg = AREG(insn, 9);
2958 tcg_gen_sub_i32(reg, reg, src);
2961 static inline void gen_subx(DisasContext *s, TCGv src, TCGv dest, int opsize)
2965 gen_flush_flags(s); /* compute old Z */
2967 /* Perform substract with borrow.
2968 * (X, N) = dest - (src + X);
2971 tmp = tcg_const_i32(0);
2972 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, src, tmp, QREG_CC_X, tmp);
2973 tcg_gen_sub2_i32(QREG_CC_N, QREG_CC_X, dest, tmp, QREG_CC_N, QREG_CC_X);
2974 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
2975 tcg_gen_andi_i32(QREG_CC_X, QREG_CC_X, 1);
2977 /* Compute signed-overflow for substract. */
2979 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, dest);
2980 tcg_gen_xor_i32(tmp, dest, src);
2981 tcg_gen_and_i32(QREG_CC_V, QREG_CC_V, tmp);
2984 /* Copy the rest of the results into place. */
2985 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
2986 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
2988 set_cc_op(s, CC_OP_FLAGS);
2990 /* result is in QREG_CC_N */
2993 DISAS_INSN(subx_reg)
2999 opsize = insn_opsize(insn);
3001 src = gen_extend(DREG(insn, 0), opsize, 1);
3002 dest = gen_extend(DREG(insn, 9), opsize, 1);
3004 gen_subx(s, src, dest, opsize);
3006 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
3009 DISAS_INSN(subx_mem)
3017 opsize = insn_opsize(insn);
3019 addr_src = AREG(insn, 0);
3020 tcg_gen_subi_i32(addr_src, addr_src, opsize);
3021 src = gen_load(s, opsize, addr_src, 1);
3023 addr_dest = AREG(insn, 9);
3024 tcg_gen_subi_i32(addr_dest, addr_dest, opsize);
3025 dest = gen_load(s, opsize, addr_dest, 1);
3027 gen_subx(s, src, dest, opsize);
3029 gen_store(s, opsize, addr_dest, QREG_CC_N);
3037 val = (insn >> 9) & 7;
3040 src = tcg_const_i32(val);
3041 gen_logic_cc(s, src, OS_LONG);
3042 DEST_EA(env, insn, OS_LONG, src, NULL);
3052 opsize = insn_opsize(insn);
3053 SRC_EA(env, src, opsize, 1, NULL);
3054 reg = gen_extend(DREG(insn, 9), opsize, 1);
3055 gen_update_cc_cmp(s, reg, src, opsize);
3069 SRC_EA(env, src, opsize, 1, NULL);
3070 reg = AREG(insn, 9);
3071 gen_update_cc_cmp(s, reg, src, OS_LONG);
3076 int opsize = insn_opsize(insn);
3079 /* Post-increment load (mode 3) from Ay. */
3080 src = gen_ea_mode(env, s, 3, REG(insn, 0), opsize,
3081 NULL_QREG, NULL, EA_LOADS);
3082 /* Post-increment load (mode 3) from Ax. */
3083 dst = gen_ea_mode(env, s, 3, REG(insn, 9), opsize,
3084 NULL_QREG, NULL, EA_LOADS);
3086 gen_update_cc_cmp(s, dst, src, opsize);
3096 opsize = insn_opsize(insn);
3098 SRC_EA(env, src, opsize, 0, &addr);
3099 dest = tcg_temp_new();
3100 tcg_gen_xor_i32(dest, src, DREG(insn, 9));
3101 gen_logic_cc(s, dest, opsize);
3102 DEST_EA(env, insn, opsize, dest, &addr);
3103 tcg_temp_free(dest);
3106 static void do_exg(TCGv reg1, TCGv reg2)
3108 TCGv temp = tcg_temp_new();
3109 tcg_gen_mov_i32(temp, reg1);
3110 tcg_gen_mov_i32(reg1, reg2);
3111 tcg_gen_mov_i32(reg2, temp);
3112 tcg_temp_free(temp);
3117 /* exchange Dx and Dy */
3118 do_exg(DREG(insn, 9), DREG(insn, 0));
3123 /* exchange Ax and Ay */
3124 do_exg(AREG(insn, 9), AREG(insn, 0));
3129 /* exchange Dx and Ay */
3130 do_exg(DREG(insn, 9), AREG(insn, 0));
3141 dest = tcg_temp_new();
3143 opsize = insn_opsize(insn);
3144 reg = DREG(insn, 9);
3146 SRC_EA(env, src, opsize, 0, &addr);
3147 tcg_gen_and_i32(dest, src, reg);
3148 DEST_EA(env, insn, opsize, dest, &addr);
3150 SRC_EA(env, src, opsize, 0, NULL);
3151 tcg_gen_and_i32(dest, src, reg);
3152 gen_partset_reg(opsize, reg, dest);
3154 gen_logic_cc(s, dest, opsize);
3155 tcg_temp_free(dest);
3163 SRC_EA(env, src, (insn & 0x100) ? OS_LONG : OS_WORD, 1, NULL);
3164 reg = AREG(insn, 9);
3165 tcg_gen_add_i32(reg, reg, src);
3168 static inline void gen_addx(DisasContext *s, TCGv src, TCGv dest, int opsize)
3172 gen_flush_flags(s); /* compute old Z */
3174 /* Perform addition with carry.
3175 * (X, N) = src + dest + X;
3178 tmp = tcg_const_i32(0);
3179 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_X, tmp, dest, tmp);
3180 tcg_gen_add2_i32(QREG_CC_N, QREG_CC_X, QREG_CC_N, QREG_CC_X, src, tmp);
3181 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3183 /* Compute signed-overflow for addition. */
3185 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, src);
3186 tcg_gen_xor_i32(tmp, dest, src);
3187 tcg_gen_andc_i32(QREG_CC_V, QREG_CC_V, tmp);
3190 /* Copy the rest of the results into place. */
3191 tcg_gen_or_i32(QREG_CC_Z, QREG_CC_Z, QREG_CC_N); /* !Z is sticky */
3192 tcg_gen_mov_i32(QREG_CC_C, QREG_CC_X);
3194 set_cc_op(s, CC_OP_FLAGS);
3196 /* result is in QREG_CC_N */
3199 DISAS_INSN(addx_reg)
3205 opsize = insn_opsize(insn);
3207 dest = gen_extend(DREG(insn, 9), opsize, 1);
3208 src = gen_extend(DREG(insn, 0), opsize, 1);
3210 gen_addx(s, src, dest, opsize);
3212 gen_partset_reg(opsize, DREG(insn, 9), QREG_CC_N);
3215 DISAS_INSN(addx_mem)
3223 opsize = insn_opsize(insn);
3225 addr_src = AREG(insn, 0);
3226 tcg_gen_subi_i32(addr_src, addr_src, opsize_bytes(opsize));
3227 src = gen_load(s, opsize, addr_src, 1);
3229 addr_dest = AREG(insn, 9);
3230 tcg_gen_subi_i32(addr_dest, addr_dest, opsize_bytes(opsize));
3231 dest = gen_load(s, opsize, addr_dest, 1);
3233 gen_addx(s, src, dest, opsize);
3235 gen_store(s, opsize, addr_dest, QREG_CC_N);
3238 static inline void shift_im(DisasContext *s, uint16_t insn, int opsize)
3240 int count = (insn >> 9) & 7;
3241 int logical = insn & 8;
3242 int left = insn & 0x100;
3243 int bits = opsize_bytes(opsize) * 8;
3244 TCGv reg = gen_extend(DREG(insn, 0), opsize, !logical);
3250 tcg_gen_movi_i32(QREG_CC_V, 0);
3252 tcg_gen_shri_i32(QREG_CC_C, reg, bits - count);
3253 tcg_gen_shli_i32(QREG_CC_N, reg, count);
3255 /* Note that ColdFire always clears V (done above),
3256 while M68000 sets if the most significant bit is changed at
3257 any time during the shift operation */
3258 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3259 /* if shift count >= bits, V is (reg != 0) */
3260 if (count >= bits) {
3261 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, reg, QREG_CC_V);
3263 TCGv t0 = tcg_temp_new();
3264 tcg_gen_sari_i32(QREG_CC_V, reg, bits - 1);
3265 tcg_gen_sari_i32(t0, reg, bits - count - 1);
3266 tcg_gen_setcond_i32(TCG_COND_NE, QREG_CC_V, QREG_CC_V, t0);
3269 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
3272 tcg_gen_shri_i32(QREG_CC_C, reg, count - 1);
3274 tcg_gen_shri_i32(QREG_CC_N, reg, count);
3276 tcg_gen_sari_i32(QREG_CC_N, reg, count);
3280 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3281 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3282 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3283 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
3285 gen_partset_reg(opsize, DREG(insn, 0), QREG_CC_N);
3286 set_cc_op(s, CC_OP_FLAGS);
3289 static inline void shift_reg(DisasContext *s, uint16_t insn, int opsize)
3291 int logical = insn & 8;
3292 int left = insn & 0x100;
3293 int bits = opsize_bytes(opsize) * 8;
3294 TCGv reg = gen_extend(DREG(insn, 0), opsize, !logical);
3298 t64 = tcg_temp_new_i64();
3299 s64 = tcg_temp_new_i64();
3300 s32 = tcg_temp_new();
3302 /* Note that m68k truncates the shift count modulo 64, not 32.
3303 In addition, a 64-bit shift makes it easy to find "the last
3304 bit shifted out", for the carry flag. */
3305 tcg_gen_andi_i32(s32, DREG(insn, 9), 63);
3306 tcg_gen_extu_i32_i64(s64, s32);
3307 tcg_gen_extu_i32_i64(t64, reg);
3309 /* Optimistically set V=0. Also used as a zero source below. */
3310 tcg_gen_movi_i32(QREG_CC_V, 0);
3312 tcg_gen_shl_i64(t64, t64, s64);
3314 if (opsize == OS_LONG) {
3315 tcg_gen_extr_i64_i32(QREG_CC_N, QREG_CC_C, t64);
3316 /* Note that C=0 if shift count is 0, and we get that for free. */
3318 TCGv zero = tcg_const_i32(0);
3319 tcg_gen_extrl_i64_i32(QREG_CC_N, t64);
3320 tcg_gen_shri_i32(QREG_CC_C, QREG_CC_N, bits);
3321 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3322 s32, zero, zero, QREG_CC_C);
3323 tcg_temp_free(zero);
3325 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3327 /* X = C, but only if the shift count was non-zero. */
3328 tcg_gen_movcond_i32(TCG_COND_NE, QREG_CC_X, s32, QREG_CC_V,
3329 QREG_CC_C, QREG_CC_X);
3331 /* M68000 sets V if the most significant bit is changed at
3332 * any time during the shift operation. Do this via creating
3333 * an extension of the sign bit, comparing, and discarding
3334 * the bits below the sign bit. I.e.
3335 * int64_t s = (intN_t)reg;
3336 * int64_t t = (int64_t)(intN_t)reg << count;
3337 * V = ((s ^ t) & (-1 << (bits - 1))) != 0
3339 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3340 TCGv_i64 tt = tcg_const_i64(32);
3341 /* if shift is greater than 32, use 32 */
3342 tcg_gen_movcond_i64(TCG_COND_GT, s64, s64, tt, tt, s64);
3343 tcg_temp_free_i64(tt);
3344 /* Sign extend the input to 64 bits; re-do the shift. */
3345 tcg_gen_ext_i32_i64(t64, reg);
3346 tcg_gen_shl_i64(s64, t64, s64);
3347 /* Clear all bits that are unchanged. */
3348 tcg_gen_xor_i64(t64, t64, s64);
3349 /* Ignore the bits below the sign bit. */
3350 tcg_gen_andi_i64(t64, t64, -1ULL << (bits - 1));
3351 /* If any bits remain set, we have overflow. */
3352 tcg_gen_setcondi_i64(TCG_COND_NE, t64, t64, 0);
3353 tcg_gen_extrl_i64_i32(QREG_CC_V, t64);
3354 tcg_gen_neg_i32(QREG_CC_V, QREG_CC_V);
3357 tcg_gen_shli_i64(t64, t64, 32);
3359 tcg_gen_shr_i64(t64, t64, s64);
3361 tcg_gen_sar_i64(t64, t64, s64);
3363 tcg_gen_extr_i64_i32(QREG_CC_C, QREG_CC_N, t64);
3365 /* Note that C=0 if shift count is 0, and we get that for free. */
3366 tcg_gen_shri_i32(QREG_CC_C, QREG_CC_C, 31);
3368 /* X = C, but only if the shift count was non-zero. */
3369 tcg_gen_movcond_i32(TCG_COND_NE, QREG_CC_X, s32, QREG_CC_V,
3370 QREG_CC_C, QREG_CC_X);
3372 gen_ext(QREG_CC_N, QREG_CC_N, opsize, 1);
3373 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3376 tcg_temp_free_i64(s64);
3377 tcg_temp_free_i64(t64);
3379 /* Write back the result. */
3380 gen_partset_reg(opsize, DREG(insn, 0), QREG_CC_N);
3381 set_cc_op(s, CC_OP_FLAGS);
3384 DISAS_INSN(shift8_im)
3386 shift_im(s, insn, OS_BYTE);
3389 DISAS_INSN(shift16_im)
3391 shift_im(s, insn, OS_WORD);
3394 DISAS_INSN(shift_im)
3396 shift_im(s, insn, OS_LONG);
3399 DISAS_INSN(shift8_reg)
3401 shift_reg(s, insn, OS_BYTE);
3404 DISAS_INSN(shift16_reg)
3406 shift_reg(s, insn, OS_WORD);
3409 DISAS_INSN(shift_reg)
3411 shift_reg(s, insn, OS_LONG);
3414 DISAS_INSN(shift_mem)
3416 int logical = insn & 8;
3417 int left = insn & 0x100;
3421 SRC_EA(env, src, OS_WORD, !logical, &addr);
3422 tcg_gen_movi_i32(QREG_CC_V, 0);
3424 tcg_gen_shri_i32(QREG_CC_C, src, 15);
3425 tcg_gen_shli_i32(QREG_CC_N, src, 1);
3427 /* Note that ColdFire always clears V,
3428 while M68000 sets if the most significant bit is changed at
3429 any time during the shift operation */
3430 if (!logical && m68k_feature(s->env, M68K_FEATURE_M68000)) {
3431 src = gen_extend(src, OS_WORD, 1);
3432 tcg_gen_xor_i32(QREG_CC_V, QREG_CC_N, src);
3435 tcg_gen_mov_i32(QREG_CC_C, src);
3437 tcg_gen_shri_i32(QREG_CC_N, src, 1);
3439 tcg_gen_sari_i32(QREG_CC_N, src, 1);
3443 gen_ext(QREG_CC_N, QREG_CC_N, OS_WORD, 1);
3444 tcg_gen_andi_i32(QREG_CC_C, QREG_CC_C, 1);
3445 tcg_gen_mov_i32(QREG_CC_Z, QREG_CC_N);
3446 tcg_gen_mov_i32(QREG_CC_X, QREG_CC_C);
3448 DEST_EA(env, insn, OS_WORD, QREG_CC_N, &addr);
3449 set_cc_op(s, CC_OP_FLAGS);
3452 static void rotate(TCGv reg, TCGv shift, int left, int size)
3456 /* Replicate the 8-bit input so that a 32-bit rotate works. */
3457 tcg_gen_ext8u_i32(reg, reg);
3458 tcg_gen_muli_i32(reg, reg, 0x01010101);
3461 /* Replicate the 16-bit input so that a 32-bit rotate works. */
3462 tcg_gen_deposit_i32(reg, reg, reg, 16, 16);
3467 tcg_gen_rotl_i32(reg, reg, shift);
3469 tcg_gen_rotr_i32(reg, reg, shift);
3477 tcg_gen_ext8s_i32(reg, reg);
3480 tcg_gen_ext16s_i32(reg, reg);
3486 /* QREG_CC_X is not affected */
3488 tcg_gen_mov_i32(QREG_CC_N, reg);
3489 tcg_gen_mov_i32(QREG_CC_Z, reg);
3492 tcg_gen_andi_i32(QREG_CC_C, reg, 1);
3494 tcg_gen_shri_i32(QREG_CC_C, reg, 31);
3497 tcg_gen_movi_i32(QREG_CC_V, 0); /* always cleared */
3500 static void rotate_x_flags(TCGv reg, TCGv X, int size)
3504 tcg_gen_ext8s_i32(reg, reg);
3507 tcg_gen_ext16s_i32(reg, reg);
3512 tcg_gen_mov_i32(QREG_CC_N, reg);
3513 tcg_gen_mov_i32(QREG_CC_Z, reg);
3514 tcg_gen_mov_i32(QREG_CC_X, X);
3515 tcg_gen_mov_i32(QREG_CC_C, X);
3516 tcg_gen_movi_i32(QREG_CC_V, 0);
3519 /* Result of rotate_x() is valid if 0 <= shift <= size */
3520 static TCGv rotate_x(TCGv reg, TCGv shift, int left, int size)
3522 TCGv X, shl, shr, shx, sz, zero;
3524 sz = tcg_const_i32(size);
3526 shr = tcg_temp_new();
3527 shl = tcg_temp_new();
3528 shx = tcg_temp_new();
3530 tcg_gen_mov_i32(shl, shift); /* shl = shift */
3531 tcg_gen_movi_i32(shr, size + 1);
3532 tcg_gen_sub_i32(shr, shr, shift); /* shr = size + 1 - shift */
3533 tcg_gen_subi_i32(shx, shift, 1); /* shx = shift - 1 */
3534 /* shx = shx < 0 ? size : shx; */
3535 zero = tcg_const_i32(0);
3536 tcg_gen_movcond_i32(TCG_COND_LT, shx, shx, zero, sz, shx);
3537 tcg_temp_free(zero);
3539 tcg_gen_mov_i32(shr, shift); /* shr = shift */
3540 tcg_gen_movi_i32(shl, size + 1);
3541 tcg_gen_sub_i32(shl, shl, shift); /* shl = size + 1 - shift */
3542 tcg_gen_sub_i32(shx, sz, shift); /* shx = size - shift */
3545 /* reg = (reg << shl) | (reg >> shr) | (x << shx); */
3547 tcg_gen_shl_i32(shl, reg, shl);
3548 tcg_gen_shr_i32(shr, reg, shr);
3549 tcg_gen_or_i32(reg, shl, shr);
3552 tcg_gen_shl_i32(shx, QREG_CC_X, shx);
3553 tcg_gen_or_i32(reg, reg, shx);
3556 /* X = (reg >> size) & 1 */
3559 tcg_gen_shr_i32(X, reg, sz);
3560 tcg_gen_andi_i32(X, X, 1);
3566 /* Result of rotate32_x() is valid if 0 <= shift < 33 */
3567 static TCGv rotate32_x(TCGv reg, TCGv shift, int left)
3569 TCGv_i64 t0, shift64;
3570 TCGv X, lo, hi, zero;
3572 shift64 = tcg_temp_new_i64();
3573 tcg_gen_extu_i32_i64(shift64, shift);
3575 t0 = tcg_temp_new_i64();
3578 lo = tcg_temp_new();
3579 hi = tcg_temp_new();
3582 /* create [reg:X:..] */
3584 tcg_gen_shli_i32(lo, QREG_CC_X, 31);
3585 tcg_gen_concat_i32_i64(t0, lo, reg);
3589 tcg_gen_rotl_i64(t0, t0, shift64);
3590 tcg_temp_free_i64(shift64);
3592 /* result is [reg:..:reg:X] */
3594 tcg_gen_extr_i64_i32(lo, hi, t0);
3595 tcg_gen_andi_i32(X, lo, 1);
3597 tcg_gen_shri_i32(lo, lo, 1);
3599 /* create [..:X:reg] */
3601 tcg_gen_concat_i32_i64(t0, reg, QREG_CC_X);
3603 tcg_gen_rotr_i64(t0, t0, shift64);
3604 tcg_temp_free_i64(shift64);
3606 /* result is value: [X:reg:..:reg] */
3608 tcg_gen_extr_i64_i32(lo, hi, t0);
3612 tcg_gen_shri_i32(X, hi, 31);
3614 /* extract result */
3616 tcg_gen_shli_i32(hi, hi, 1);
3618 tcg_temp_free_i64(t0);
3619 tcg_gen_or_i32(lo, lo, hi);
3622 /* if shift == 0, register and X are not affected */
3624 zero = tcg_const_i32(0);
3625 tcg_gen_movcond_i32(TCG_COND_EQ, X, shift, zero, QREG_CC_X, X);
3626 tcg_gen_movcond_i32(TCG_COND_EQ, reg, shift, zero, reg, lo);
3627 tcg_temp_free(zero);
3633 DISAS_INSN(rotate_im)
3637 int left = (insn & 0x100);
3639 tmp = (insn >> 9) & 7;
3644 shift = tcg_const_i32(tmp);
3646 rotate(DREG(insn, 0), shift, left, 32);
3648 TCGv X = rotate32_x(DREG(insn, 0), shift, left);
3649 rotate_x_flags(DREG(insn, 0), X, 32);
3652 tcg_temp_free(shift);
3654 set_cc_op(s, CC_OP_FLAGS);
3657 DISAS_INSN(rotate8_im)
3659 int left = (insn & 0x100);
3664 reg = gen_extend(DREG(insn, 0), OS_BYTE, 0);
3666 tmp = (insn >> 9) & 7;
3671 shift = tcg_const_i32(tmp);
3673 rotate(reg, shift, left, 8);
3675 TCGv X = rotate_x(reg, shift, left, 8);
3676 rotate_x_flags(reg, X, 8);
3679 tcg_temp_free(shift);
3680 gen_partset_reg(OS_BYTE, DREG(insn, 0), reg);
3681 set_cc_op(s, CC_OP_FLAGS);
3684 DISAS_INSN(rotate16_im)
3686 int left = (insn & 0x100);
3691 reg = gen_extend(DREG(insn, 0), OS_WORD, 0);
3692 tmp = (insn >> 9) & 7;
3697 shift = tcg_const_i32(tmp);
3699 rotate(reg, shift, left, 16);
3701 TCGv X = rotate_x(reg, shift, left, 16);
3702 rotate_x_flags(reg, X, 16);
3705 tcg_temp_free(shift);
3706 gen_partset_reg(OS_WORD, DREG(insn, 0), reg);
3707 set_cc_op(s, CC_OP_FLAGS);
3710 DISAS_INSN(rotate_reg)
3715 int left = (insn & 0x100);
3717 reg = DREG(insn, 0);
3718 src = DREG(insn, 9);
3719 /* shift in [0..63] */
3720 t0 = tcg_temp_new();
3721 tcg_gen_andi_i32(t0, src, 63);
3722 t1 = tcg_temp_new_i32();
3724 tcg_gen_andi_i32(t1, src, 31);
3725 rotate(reg, t1, left, 32);
3726 /* if shift == 0, clear C */
3727 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3728 t0, QREG_CC_V /* 0 */,
3729 QREG_CC_V /* 0 */, QREG_CC_C);
3733 tcg_gen_movi_i32(t1, 33);
3734 tcg_gen_remu_i32(t1, t0, t1);
3735 X = rotate32_x(DREG(insn, 0), t1, left);
3736 rotate_x_flags(DREG(insn, 0), X, 32);
3741 set_cc_op(s, CC_OP_FLAGS);
3744 DISAS_INSN(rotate8_reg)
3749 int left = (insn & 0x100);
3751 reg = gen_extend(DREG(insn, 0), OS_BYTE, 0);
3752 src = DREG(insn, 9);
3753 /* shift in [0..63] */
3754 t0 = tcg_temp_new_i32();
3755 tcg_gen_andi_i32(t0, src, 63);
3756 t1 = tcg_temp_new_i32();
3758 tcg_gen_andi_i32(t1, src, 7);
3759 rotate(reg, t1, left, 8);
3760 /* if shift == 0, clear C */
3761 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3762 t0, QREG_CC_V /* 0 */,
3763 QREG_CC_V /* 0 */, QREG_CC_C);
3767 tcg_gen_movi_i32(t1, 9);
3768 tcg_gen_remu_i32(t1, t0, t1);
3769 X = rotate_x(reg, t1, left, 8);
3770 rotate_x_flags(reg, X, 8);
3775 gen_partset_reg(OS_BYTE, DREG(insn, 0), reg);
3776 set_cc_op(s, CC_OP_FLAGS);
3779 DISAS_INSN(rotate16_reg)
3784 int left = (insn & 0x100);
3786 reg = gen_extend(DREG(insn, 0), OS_WORD, 0);
3787 src = DREG(insn, 9);
3788 /* shift in [0..63] */
3789 t0 = tcg_temp_new_i32();
3790 tcg_gen_andi_i32(t0, src, 63);
3791 t1 = tcg_temp_new_i32();
3793 tcg_gen_andi_i32(t1, src, 15);
3794 rotate(reg, t1, left, 16);
3795 /* if shift == 0, clear C */
3796 tcg_gen_movcond_i32(TCG_COND_EQ, QREG_CC_C,
3797 t0, QREG_CC_V /* 0 */,
3798 QREG_CC_V /* 0 */, QREG_CC_C);
3802 tcg_gen_movi_i32(t1, 17);
3803 tcg_gen_remu_i32(t1, t0, t1);
3804 X = rotate_x(reg, t1, left, 16);
3805 rotate_x_flags(reg, X, 16);
3810 gen_partset_reg(OS_WORD, DREG(insn, 0), reg);
3811 set_cc_op(s, CC_OP_FLAGS);
3814 DISAS_INSN(rotate_mem)
3819 int left = (insn & 0x100);
3821 SRC_EA(env, src, OS_WORD, 0, &addr);
3823 shift = tcg_const_i32(1);
3824 if (insn & 0x0200) {
3825 rotate(src, shift, left, 16);
3827 TCGv X = rotate_x(src, shift, left, 16);
3828 rotate_x_flags(src, X, 16);
3831 tcg_temp_free(shift);
3832 DEST_EA(env, insn, OS_WORD, src, &addr);
3833 set_cc_op(s, CC_OP_FLAGS);
3836 DISAS_INSN(bfext_reg)
3838 int ext = read_im16(env, s);
3839 int is_sign = insn & 0x200;
3840 TCGv src = DREG(insn, 0);
3841 TCGv dst = DREG(ext, 12);
3842 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
3843 int ofs = extract32(ext, 6, 5); /* big bit-endian */
3844 int pos = 32 - ofs - len; /* little bit-endian */
3845 TCGv tmp = tcg_temp_new();
3848 /* In general, we're going to rotate the field so that it's at the
3849 top of the word and then right-shift by the compliment of the
3850 width to extend the field. */
3852 /* Variable width. */
3854 /* Variable offset. */
3855 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
3856 tcg_gen_rotl_i32(tmp, src, tmp);
3858 tcg_gen_rotli_i32(tmp, src, ofs);
3861 shift = tcg_temp_new();
3862 tcg_gen_neg_i32(shift, DREG(ext, 0));
3863 tcg_gen_andi_i32(shift, shift, 31);
3864 tcg_gen_sar_i32(QREG_CC_N, tmp, shift);
3866 tcg_gen_mov_i32(dst, QREG_CC_N);
3868 tcg_gen_shr_i32(dst, tmp, shift);
3870 tcg_temp_free(shift);
3872 /* Immediate width. */
3874 /* Variable offset */
3875 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
3876 tcg_gen_rotl_i32(tmp, src, tmp);
3880 /* Immediate offset. If the field doesn't wrap around the
3881 end of the word, rely on (s)extract completely. */
3883 tcg_gen_rotli_i32(tmp, src, ofs);
3889 tcg_gen_sextract_i32(QREG_CC_N, src, pos, len);
3891 tcg_gen_mov_i32(dst, QREG_CC_N);
3893 tcg_gen_extract_i32(dst, src, pos, len);
3898 set_cc_op(s, CC_OP_LOGIC);
3901 DISAS_INSN(bfext_mem)
3903 int ext = read_im16(env, s);
3904 int is_sign = insn & 0x200;
3905 TCGv dest = DREG(ext, 12);
3906 TCGv addr, len, ofs;
3908 addr = gen_lea(env, s, insn, OS_UNSIZED);
3909 if (IS_NULL_QREG(addr)) {
3917 len = tcg_const_i32(extract32(ext, 0, 5));
3922 ofs = tcg_const_i32(extract32(ext, 6, 5));
3926 gen_helper_bfexts_mem(dest, cpu_env, addr, ofs, len);
3927 tcg_gen_mov_i32(QREG_CC_N, dest);
3929 TCGv_i64 tmp = tcg_temp_new_i64();
3930 gen_helper_bfextu_mem(tmp, cpu_env, addr, ofs, len);
3931 tcg_gen_extr_i64_i32(dest, QREG_CC_N, tmp);
3932 tcg_temp_free_i64(tmp);
3934 set_cc_op(s, CC_OP_LOGIC);
3936 if (!(ext & 0x20)) {
3939 if (!(ext & 0x800)) {
3944 DISAS_INSN(bfop_reg)
3946 int ext = read_im16(env, s);
3947 TCGv src = DREG(insn, 0);
3948 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
3949 int ofs = extract32(ext, 6, 5); /* big bit-endian */
3950 TCGv mask, tofs, tlen;
3954 if ((insn & 0x0f00) == 0x0d00) { /* bfffo */
3955 tofs = tcg_temp_new();
3956 tlen = tcg_temp_new();
3959 if ((ext & 0x820) == 0) {
3960 /* Immediate width and offset. */
3961 uint32_t maski = 0x7fffffffu >> (len - 1);
3962 if (ofs + len <= 32) {
3963 tcg_gen_shli_i32(QREG_CC_N, src, ofs);
3965 tcg_gen_rotli_i32(QREG_CC_N, src, ofs);
3967 tcg_gen_andi_i32(QREG_CC_N, QREG_CC_N, ~maski);
3968 mask = tcg_const_i32(ror32(maski, ofs));
3969 if (!TCGV_IS_UNUSED(tofs)) {
3970 tcg_gen_movi_i32(tofs, ofs);
3971 tcg_gen_movi_i32(tlen, len);
3974 TCGv tmp = tcg_temp_new();
3976 /* Variable width */
3977 tcg_gen_subi_i32(tmp, DREG(ext, 0), 1);
3978 tcg_gen_andi_i32(tmp, tmp, 31);
3979 mask = tcg_const_i32(0x7fffffffu);
3980 tcg_gen_shr_i32(mask, mask, tmp);
3981 if (!TCGV_IS_UNUSED(tlen)) {
3982 tcg_gen_addi_i32(tlen, tmp, 1);
3985 /* Immediate width */
3986 mask = tcg_const_i32(0x7fffffffu >> (len - 1));
3987 if (!TCGV_IS_UNUSED(tlen)) {
3988 tcg_gen_movi_i32(tlen, len);
3992 /* Variable offset */
3993 tcg_gen_andi_i32(tmp, DREG(ext, 6), 31);
3994 tcg_gen_rotl_i32(QREG_CC_N, src, tmp);
3995 tcg_gen_andc_i32(QREG_CC_N, QREG_CC_N, mask);
3996 tcg_gen_rotr_i32(mask, mask, tmp);
3997 if (!TCGV_IS_UNUSED(tofs)) {
3998 tcg_gen_mov_i32(tofs, tmp);
4001 /* Immediate offset (and variable width) */
4002 tcg_gen_rotli_i32(QREG_CC_N, src, ofs);
4003 tcg_gen_andc_i32(QREG_CC_N, QREG_CC_N, mask);
4004 tcg_gen_rotri_i32(mask, mask, ofs);
4005 if (!TCGV_IS_UNUSED(tofs)) {
4006 tcg_gen_movi_i32(tofs, ofs);
4011 set_cc_op(s, CC_OP_LOGIC);
4013 switch (insn & 0x0f00) {
4014 case 0x0a00: /* bfchg */
4015 tcg_gen_eqv_i32(src, src, mask);
4017 case 0x0c00: /* bfclr */
4018 tcg_gen_and_i32(src, src, mask);
4020 case 0x0d00: /* bfffo */
4021 gen_helper_bfffo_reg(DREG(ext, 12), QREG_CC_N, tofs, tlen);
4022 tcg_temp_free(tlen);
4023 tcg_temp_free(tofs);
4025 case 0x0e00: /* bfset */
4026 tcg_gen_orc_i32(src, src, mask);
4028 case 0x0800: /* bftst */
4029 /* flags already set; no other work to do. */
4032 g_assert_not_reached();
4034 tcg_temp_free(mask);
4037 DISAS_INSN(bfop_mem)
4039 int ext = read_im16(env, s);
4040 TCGv addr, len, ofs;
4043 addr = gen_lea(env, s, insn, OS_UNSIZED);
4044 if (IS_NULL_QREG(addr)) {
4052 len = tcg_const_i32(extract32(ext, 0, 5));
4057 ofs = tcg_const_i32(extract32(ext, 6, 5));
4060 switch (insn & 0x0f00) {
4061 case 0x0a00: /* bfchg */
4062 gen_helper_bfchg_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4064 case 0x0c00: /* bfclr */
4065 gen_helper_bfclr_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4067 case 0x0d00: /* bfffo */
4068 t64 = tcg_temp_new_i64();
4069 gen_helper_bfffo_mem(t64, cpu_env, addr, ofs, len);
4070 tcg_gen_extr_i64_i32(DREG(ext, 12), QREG_CC_N, t64);
4071 tcg_temp_free_i64(t64);
4073 case 0x0e00: /* bfset */
4074 gen_helper_bfset_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4076 case 0x0800: /* bftst */
4077 gen_helper_bfexts_mem(QREG_CC_N, cpu_env, addr, ofs, len);
4080 g_assert_not_reached();
4082 set_cc_op(s, CC_OP_LOGIC);
4084 if (!(ext & 0x20)) {
4087 if (!(ext & 0x800)) {
4092 DISAS_INSN(bfins_reg)
4094 int ext = read_im16(env, s);
4095 TCGv dst = DREG(insn, 0);
4096 TCGv src = DREG(ext, 12);
4097 int len = ((extract32(ext, 0, 5) - 1) & 31) + 1;
4098 int ofs = extract32(ext, 6, 5); /* big bit-endian */
4099 int pos = 32 - ofs - len; /* little bit-endian */
4102 tmp = tcg_temp_new();
4105 /* Variable width */
4106 tcg_gen_neg_i32(tmp, DREG(ext, 0));
4107 tcg_gen_andi_i32(tmp, tmp, 31);
4108 tcg_gen_shl_i32(QREG_CC_N, src, tmp);
4110 /* Immediate width */
4111 tcg_gen_shli_i32(QREG_CC_N, src, 32 - len);
4113 set_cc_op(s, CC_OP_LOGIC);
4115 /* Immediate width and offset */
4116 if ((ext & 0x820) == 0) {
4117 /* Check for suitability for deposit. */
4119 tcg_gen_deposit_i32(dst, dst, src, pos, len);
4121 uint32_t maski = -2U << (len - 1);
4122 uint32_t roti = (ofs + len) & 31;
4123 tcg_gen_andi_i32(tmp, src, ~maski);
4124 tcg_gen_rotri_i32(tmp, tmp, roti);
4125 tcg_gen_andi_i32(dst, dst, ror32(maski, roti));
4126 tcg_gen_or_i32(dst, dst, tmp);
4129 TCGv mask = tcg_temp_new();
4130 TCGv rot = tcg_temp_new();
4133 /* Variable width */
4134 tcg_gen_subi_i32(rot, DREG(ext, 0), 1);
4135 tcg_gen_andi_i32(rot, rot, 31);
4136 tcg_gen_movi_i32(mask, -2);
4137 tcg_gen_shl_i32(mask, mask, rot);
4138 tcg_gen_mov_i32(rot, DREG(ext, 0));
4139 tcg_gen_andc_i32(tmp, src, mask);
4141 /* Immediate width (variable offset) */
4142 uint32_t maski = -2U << (len - 1);
4143 tcg_gen_andi_i32(tmp, src, ~maski);
4144 tcg_gen_movi_i32(mask, maski);
4145 tcg_gen_movi_i32(rot, len & 31);
4148 /* Variable offset */
4149 tcg_gen_add_i32(rot, rot, DREG(ext, 6));
4151 /* Immediate offset (variable width) */
4152 tcg_gen_addi_i32(rot, rot, ofs);
4154 tcg_gen_andi_i32(rot, rot, 31);
4155 tcg_gen_rotr_i32(mask, mask, rot);
4156 tcg_gen_rotr_i32(tmp, tmp, rot);
4157 tcg_gen_and_i32(dst, dst, mask);
4158 tcg_gen_or_i32(dst, dst, tmp);
4161 tcg_temp_free(mask);
4166 DISAS_INSN(bfins_mem)
4168 int ext = read_im16(env, s);
4169 TCGv src = DREG(ext, 12);
4170 TCGv addr, len, ofs;
4172 addr = gen_lea(env, s, insn, OS_UNSIZED);
4173 if (IS_NULL_QREG(addr)) {
4181 len = tcg_const_i32(extract32(ext, 0, 5));
4186 ofs = tcg_const_i32(extract32(ext, 6, 5));
4189 gen_helper_bfins_mem(QREG_CC_N, cpu_env, addr, src, ofs, len);
4190 set_cc_op(s, CC_OP_LOGIC);
4192 if (!(ext & 0x20)) {
4195 if (!(ext & 0x800)) {
4203 reg = DREG(insn, 0);
4204 gen_logic_cc(s, reg, OS_LONG);
4205 gen_helper_ff1(reg, reg);
4208 static TCGv gen_get_sr(DisasContext *s)
4213 ccr = gen_get_ccr(s);
4214 sr = tcg_temp_new();
4215 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
4216 tcg_gen_or_i32(sr, sr, ccr);
4226 ext = read_im16(env, s);
4227 if (ext != 0x46FC) {
4228 gen_exception(s, addr, EXCP_UNSUPPORTED);
4231 ext = read_im16(env, s);
4232 if (IS_USER(s) || (ext & SR_S) == 0) {
4233 gen_exception(s, addr, EXCP_PRIVILEGE);
4236 gen_push(s, gen_get_sr(s));
4237 gen_set_sr_im(s, ext, 0);
4240 DISAS_INSN(move_from_sr)
4244 if (IS_USER(s) && !m68k_feature(env, M68K_FEATURE_M68000)) {
4245 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4249 DEST_EA(env, insn, OS_WORD, sr, NULL);
4252 DISAS_INSN(move_to_sr)
4255 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4258 gen_set_sr(env, s, insn, 0);
4262 DISAS_INSN(move_from_usp)
4265 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4268 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
4269 offsetof(CPUM68KState, sp[M68K_USP]));
4272 DISAS_INSN(move_to_usp)
4275 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4278 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
4279 offsetof(CPUM68KState, sp[M68K_USP]));
4284 gen_exception(s, s->pc, EXCP_HALT_INSN);
4292 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4296 ext = read_im16(env, s);
4298 gen_set_sr_im(s, ext, 0);
4299 tcg_gen_movi_i32(cpu_halted, 1);
4300 gen_exception(s, s->pc, EXCP_HLT);
4306 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4309 gen_exception(s, s->pc - 2, EXCP_RTE);
4318 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4322 ext = read_im16(env, s);
4325 reg = AREG(ext, 12);
4327 reg = DREG(ext, 12);
4329 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
4336 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4339 /* ICache fetch. Implement as no-op. */
4345 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4348 /* Cache push/invalidate. Implement as no-op. */
4353 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4358 M68kCPU *cpu = m68k_env_get_cpu(env);
4361 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
4364 /* TODO: Implement wdebug. */
4365 cpu_abort(CPU(cpu), "WDEBUG not implemented");
4370 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
4373 static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
4377 tcg_gen_movi_i32(res, 0);
4380 tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpsr));
4383 tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpcr));
4388 static void gen_store_fcr(DisasContext *s, TCGv val, int reg)
4394 tcg_gen_st_i32(val, cpu_env, offsetof(CPUM68KState, fpsr));
4397 gen_helper_set_fpcr(cpu_env, val);
4402 static void gen_qemu_store_fcr(DisasContext *s, TCGv addr, int reg)
4404 int index = IS_USER(s);
4407 tmp = tcg_temp_new();
4408 gen_load_fcr(s, tmp, reg);
4409 tcg_gen_qemu_st32(tmp, addr, index);
4413 static void gen_qemu_load_fcr(DisasContext *s, TCGv addr, int reg)
4415 int index = IS_USER(s);
4418 tmp = tcg_temp_new();
4419 tcg_gen_qemu_ld32u(tmp, addr, index);
4420 gen_store_fcr(s, tmp, reg);
4425 static void gen_op_fmove_fcr(CPUM68KState *env, DisasContext *s,
4426 uint32_t insn, uint32_t ext)
4428 int mask = (ext >> 10) & 7;
4429 int is_write = (ext >> 13) & 1;
4430 int mode = extract32(insn, 3, 3);
4436 if (mask != M68K_FPIAR && mask != M68K_FPSR && mask != M68K_FPCR) {
4437 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4441 gen_load_fcr(s, DREG(insn, 0), mask);
4443 gen_store_fcr(s, DREG(insn, 0), mask);
4446 case 1: /* An, only with FPIAR */
4447 if (mask != M68K_FPIAR) {
4448 gen_exception(s, s->insn_pc, EXCP_ILLEGAL);
4452 gen_load_fcr(s, AREG(insn, 0), mask);
4454 gen_store_fcr(s, AREG(insn, 0), mask);
4461 tmp = gen_lea(env, s, insn, OS_LONG);
4462 if (IS_NULL_QREG(tmp)) {
4467 addr = tcg_temp_new();
4468 tcg_gen_mov_i32(addr, tmp);
4472 * 0b100 Floating-Point Control Register
4473 * 0b010 Floating-Point Status Register
4474 * 0b001 Floating-Point Instruction Address Register
4478 if (is_write && mode == 4) {
4479 for (i = 2; i >= 0; i--, mask >>= 1) {
4481 gen_qemu_store_fcr(s, addr, 1 << i);
4483 tcg_gen_subi_i32(addr, addr, opsize_bytes(OS_LONG));
4487 tcg_gen_mov_i32(AREG(insn, 0), addr);
4489 for (i = 0; i < 3; i++, mask >>= 1) {
4492 gen_qemu_store_fcr(s, addr, 1 << i);
4494 gen_qemu_load_fcr(s, addr, 1 << i);
4496 if (mask != 1 || mode == 3) {
4497 tcg_gen_addi_i32(addr, addr, opsize_bytes(OS_LONG));
4502 tcg_gen_mov_i32(AREG(insn, 0), addr);
4505 tcg_temp_free_i32(addr);
4508 static void gen_op_fmovem(CPUM68KState *env, DisasContext *s,
4509 uint32_t insn, uint32_t ext)
4513 int mode = (ext >> 11) & 0x3;
4514 int is_load = ((ext & 0x2000) == 0);
4516 if (m68k_feature(s->env, M68K_FEATURE_FPU)) {
4517 opsize = OS_EXTENDED;
4519 opsize = OS_DOUBLE; /* FIXME */
4522 addr = gen_lea(env, s, insn, opsize);
4523 if (IS_NULL_QREG(addr)) {
4528 tmp = tcg_temp_new();
4530 /* Dynamic register list */
4531 tcg_gen_ext8u_i32(tmp, DREG(ext, 4));
4533 /* Static register list */
4534 tcg_gen_movi_i32(tmp, ext & 0xff);
4537 if (!is_load && (mode & 2) == 0) {
4538 /* predecrement addressing mode
4539 * only available to store register to memory
4541 if (opsize == OS_EXTENDED) {
4542 gen_helper_fmovemx_st_predec(tmp, cpu_env, addr, tmp);
4544 gen_helper_fmovemd_st_predec(tmp, cpu_env, addr, tmp);
4547 /* postincrement addressing mode */
4548 if (opsize == OS_EXTENDED) {
4550 gen_helper_fmovemx_ld_postinc(tmp, cpu_env, addr, tmp);
4552 gen_helper_fmovemx_st_postinc(tmp, cpu_env, addr, tmp);
4556 gen_helper_fmovemd_ld_postinc(tmp, cpu_env, addr, tmp);
4558 gen_helper_fmovemd_st_postinc(tmp, cpu_env, addr, tmp);
4562 if ((insn & 070) == 030 || (insn & 070) == 040) {
4563 tcg_gen_mov_i32(AREG(insn, 0), tmp);
4568 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
4569 immediately before the next FP instruction is executed. */
4575 TCGv_ptr cpu_src, cpu_dest;
4577 ext = read_im16(env, s);
4578 opmode = ext & 0x7f;
4579 switch ((ext >> 13) & 7) {
4585 if (insn == 0xf200 && (ext & 0xfc00) == 0x5c00) {
4587 TCGv rom_offset = tcg_const_i32(opmode);
4588 cpu_dest = gen_fp_ptr(REG(ext, 7));
4589 gen_helper_fconst(cpu_env, cpu_dest, rom_offset);
4590 tcg_temp_free_ptr(cpu_dest);
4591 tcg_temp_free(rom_offset);
4595 case 3: /* fmove out */
4596 cpu_src = gen_fp_ptr(REG(ext, 7));
4597 opsize = ext_opsize(ext, 10);
4598 if (gen_ea_fp(env, s, insn, opsize, cpu_src, EA_STORE) == -1) {
4601 gen_helper_ftst(cpu_env, cpu_src);
4602 tcg_temp_free_ptr(cpu_src);
4604 case 4: /* fmove to control register. */
4605 case 5: /* fmove from control register. */
4606 gen_op_fmove_fcr(env, s, insn, ext);
4608 case 6: /* fmovem */
4610 if ((ext & 0x1000) == 0 && !m68k_feature(s->env, M68K_FEATURE_FPU)) {
4613 gen_op_fmovem(env, s, insn, ext);
4616 if (ext & (1 << 14)) {
4617 /* Source effective address. */
4618 opsize = ext_opsize(ext, 10);
4619 cpu_src = gen_fp_result_ptr();
4620 if (gen_ea_fp(env, s, insn, opsize, cpu_src, EA_LOADS) == -1) {
4625 /* Source register. */
4626 opsize = OS_EXTENDED;
4627 cpu_src = gen_fp_ptr(REG(ext, 10));
4629 cpu_dest = gen_fp_ptr(REG(ext, 7));
4632 gen_fp_move(cpu_dest, cpu_src);
4634 case 0x40: /* fsmove */
4635 gen_helper_fsround(cpu_env, cpu_dest, cpu_src);
4637 case 0x44: /* fdmove */
4638 gen_helper_fdround(cpu_env, cpu_dest, cpu_src);
4641 gen_helper_firound(cpu_env, cpu_dest, cpu_src);
4643 case 3: /* fintrz */
4644 gen_helper_fitrunc(cpu_env, cpu_dest, cpu_src);
4647 gen_helper_fsqrt(cpu_env, cpu_dest, cpu_src);
4649 case 0x41: /* fssqrt */
4650 gen_helper_fssqrt(cpu_env, cpu_dest, cpu_src);
4652 case 0x45: /* fdsqrt */
4653 gen_helper_fdsqrt(cpu_env, cpu_dest, cpu_src);
4655 case 0x18: /* fabs */
4656 gen_helper_fabs(cpu_env, cpu_dest, cpu_src);
4658 case 0x58: /* fsabs */
4659 gen_helper_fsabs(cpu_env, cpu_dest, cpu_src);
4661 case 0x5c: /* fdabs */
4662 gen_helper_fdabs(cpu_env, cpu_dest, cpu_src);
4664 case 0x1a: /* fneg */
4665 gen_helper_fneg(cpu_env, cpu_dest, cpu_src);
4667 case 0x5a: /* fsneg */
4668 gen_helper_fsneg(cpu_env, cpu_dest, cpu_src);
4670 case 0x5e: /* fdneg */
4671 gen_helper_fdneg(cpu_env, cpu_dest, cpu_src);
4673 case 0x20: /* fdiv */
4674 gen_helper_fdiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
4676 case 0x60: /* fsdiv */
4677 gen_helper_fsdiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
4679 case 0x64: /* fddiv */
4680 gen_helper_fddiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
4682 case 0x22: /* fadd */
4683 gen_helper_fadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
4685 case 0x62: /* fsadd */
4686 gen_helper_fsadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
4688 case 0x66: /* fdadd */
4689 gen_helper_fdadd(cpu_env, cpu_dest, cpu_src, cpu_dest);
4691 case 0x23: /* fmul */
4692 gen_helper_fmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
4694 case 0x63: /* fsmul */
4695 gen_helper_fsmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
4697 case 0x67: /* fdmul */
4698 gen_helper_fdmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
4700 case 0x24: /* fsgldiv */
4701 gen_helper_fsgldiv(cpu_env, cpu_dest, cpu_src, cpu_dest);
4703 case 0x27: /* fsglmul */
4704 gen_helper_fsglmul(cpu_env, cpu_dest, cpu_src, cpu_dest);
4706 case 0x28: /* fsub */
4707 gen_helper_fsub(cpu_env, cpu_dest, cpu_src, cpu_dest);
4709 case 0x68: /* fssub */
4710 gen_helper_fssub(cpu_env, cpu_dest, cpu_src, cpu_dest);
4712 case 0x6c: /* fdsub */
4713 gen_helper_fdsub(cpu_env, cpu_dest, cpu_src, cpu_dest);
4715 case 0x38: /* fcmp */
4716 gen_helper_fcmp(cpu_env, cpu_src, cpu_dest);
4718 case 0x3a: /* ftst */
4719 gen_helper_ftst(cpu_env, cpu_src);
4724 tcg_temp_free_ptr(cpu_src);
4725 gen_helper_ftst(cpu_env, cpu_dest);
4726 tcg_temp_free_ptr(cpu_dest);
4729 /* FIXME: Is this right for offset addressing modes? */
4731 disas_undef_fpu(env, s, insn);
4734 static void gen_fcc_cond(DisasCompare *c, DisasContext *s, int cond)
4739 c->v2 = tcg_const_i32(0);
4741 /* TODO: Raise BSUN exception. */
4742 fpsr = tcg_temp_new();
4743 gen_load_fcr(s, fpsr, M68K_FPSR);
4746 case 16: /* Signaling False */
4748 c->tcond = TCG_COND_NEVER;
4750 case 1: /* EQual Z */
4751 case 17: /* Signaling EQual Z */
4752 c->v1 = tcg_temp_new();
4754 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
4755 c->tcond = TCG_COND_NE;
4757 case 2: /* Ordered Greater Than !(A || Z || N) */
4758 case 18: /* Greater Than !(A || Z || N) */
4759 c->v1 = tcg_temp_new();
4761 tcg_gen_andi_i32(c->v1, fpsr,
4762 FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
4763 c->tcond = TCG_COND_EQ;
4765 case 3: /* Ordered Greater than or Equal Z || !(A || N) */
4766 case 19: /* Greater than or Equal Z || !(A || N) */
4767 c->v1 = tcg_temp_new();
4769 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
4770 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_A));
4771 tcg_gen_andi_i32(fpsr, fpsr, FPSR_CC_Z | FPSR_CC_N);
4772 tcg_gen_or_i32(c->v1, c->v1, fpsr);
4773 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
4774 c->tcond = TCG_COND_NE;
4776 case 4: /* Ordered Less Than !(!N || A || Z); */
4777 case 20: /* Less Than !(!N || A || Z); */
4778 c->v1 = tcg_temp_new();
4780 tcg_gen_xori_i32(c->v1, fpsr, FPSR_CC_N);
4781 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_N | FPSR_CC_A | FPSR_CC_Z);
4782 c->tcond = TCG_COND_EQ;
4784 case 5: /* Ordered Less than or Equal Z || (N && !A) */
4785 case 21: /* Less than or Equal Z || (N && !A) */
4786 c->v1 = tcg_temp_new();
4788 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
4789 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_A));
4790 tcg_gen_andc_i32(c->v1, fpsr, c->v1);
4791 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_Z | FPSR_CC_N);
4792 c->tcond = TCG_COND_NE;
4794 case 6: /* Ordered Greater or Less than !(A || Z) */
4795 case 22: /* Greater or Less than !(A || Z) */
4796 c->v1 = tcg_temp_new();
4798 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z);
4799 c->tcond = TCG_COND_EQ;
4801 case 7: /* Ordered !A */
4802 case 23: /* Greater, Less or Equal !A */
4803 c->v1 = tcg_temp_new();
4805 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
4806 c->tcond = TCG_COND_EQ;
4808 case 8: /* Unordered A */
4809 case 24: /* Not Greater, Less or Equal A */
4810 c->v1 = tcg_temp_new();
4812 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A);
4813 c->tcond = TCG_COND_NE;
4815 case 9: /* Unordered or Equal A || Z */
4816 case 25: /* Not Greater or Less then A || Z */
4817 c->v1 = tcg_temp_new();
4819 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z);
4820 c->tcond = TCG_COND_NE;
4822 case 10: /* Unordered or Greater Than A || !(N || Z)) */
4823 case 26: /* Not Less or Equal A || !(N || Z)) */
4824 c->v1 = tcg_temp_new();
4826 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
4827 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_Z));
4828 tcg_gen_andi_i32(fpsr, fpsr, FPSR_CC_A | FPSR_CC_N);
4829 tcg_gen_or_i32(c->v1, c->v1, fpsr);
4830 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
4831 c->tcond = TCG_COND_NE;
4833 case 11: /* Unordered or Greater or Equal A || Z || !N */
4834 case 27: /* Not Less Than A || Z || !N */
4835 c->v1 = tcg_temp_new();
4837 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
4838 tcg_gen_xori_i32(c->v1, c->v1, FPSR_CC_N);
4839 c->tcond = TCG_COND_NE;
4841 case 12: /* Unordered or Less Than A || (N && !Z) */
4842 case 28: /* Not Greater than or Equal A || (N && !Z) */
4843 c->v1 = tcg_temp_new();
4845 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
4846 tcg_gen_shli_i32(c->v1, c->v1, ctz32(FPSR_CC_N) - ctz32(FPSR_CC_Z));
4847 tcg_gen_andc_i32(c->v1, fpsr, c->v1);
4848 tcg_gen_andi_i32(c->v1, c->v1, FPSR_CC_A | FPSR_CC_N);
4849 c->tcond = TCG_COND_NE;
4851 case 13: /* Unordered or Less or Equal A || Z || N */
4852 case 29: /* Not Greater Than A || Z || N */
4853 c->v1 = tcg_temp_new();
4855 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_A | FPSR_CC_Z | FPSR_CC_N);
4856 c->tcond = TCG_COND_NE;
4858 case 14: /* Not Equal !Z */
4859 case 30: /* Signaling Not Equal !Z */
4860 c->v1 = tcg_temp_new();
4862 tcg_gen_andi_i32(c->v1, fpsr, FPSR_CC_Z);
4863 c->tcond = TCG_COND_EQ;
4866 case 31: /* Signaling True */
4868 c->tcond = TCG_COND_ALWAYS;
4871 tcg_temp_free(fpsr);
4874 static void gen_fjmpcc(DisasContext *s, int cond, TCGLabel *l1)
4878 gen_fcc_cond(&c, s, cond);
4879 tcg_gen_brcond_i32(c.tcond, c.v1, c.v2, l1);
4890 offset = (int16_t)read_im16(env, s);
4891 if (insn & (1 << 6)) {
4892 offset = (offset << 16) | read_im16(env, s);
4895 l1 = gen_new_label();
4897 gen_fjmpcc(s, insn & 0x3f, l1);
4898 gen_jmp_tb(s, 0, s->pc);
4900 gen_jmp_tb(s, 1, base + offset);
4910 ext = read_im16(env, s);
4912 gen_fcc_cond(&c, s, cond);
4914 tmp = tcg_temp_new();
4915 tcg_gen_setcond_i32(c.tcond, tmp, c.v1, c.v2);
4918 tcg_gen_neg_i32(tmp, tmp);
4919 DEST_EA(env, insn, OS_BYTE, tmp, NULL);
4923 DISAS_INSN(frestore)
4925 M68kCPU *cpu = m68k_env_get_cpu(env);
4927 /* TODO: Implement frestore. */
4928 cpu_abort(CPU(cpu), "FRESTORE not implemented");
4933 M68kCPU *cpu = m68k_env_get_cpu(env);
4935 /* TODO: Implement fsave. */
4936 cpu_abort(CPU(cpu), "FSAVE not implemented");
4939 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
4941 TCGv tmp = tcg_temp_new();
4942 if (s->env->macsr & MACSR_FI) {
4944 tcg_gen_andi_i32(tmp, val, 0xffff0000);
4946 tcg_gen_shli_i32(tmp, val, 16);
4947 } else if (s->env->macsr & MACSR_SU) {
4949 tcg_gen_sari_i32(tmp, val, 16);
4951 tcg_gen_ext16s_i32(tmp, val);
4954 tcg_gen_shri_i32(tmp, val, 16);
4956 tcg_gen_ext16u_i32(tmp, val);
4961 static void gen_mac_clear_flags(void)
4963 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
4964 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
4980 s->mactmp = tcg_temp_new_i64();
4984 ext = read_im16(env, s);
4986 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
4987 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
4988 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
4989 disas_undef(env, s, insn);
4993 /* MAC with load. */
4994 tmp = gen_lea(env, s, insn, OS_LONG);
4995 addr = tcg_temp_new();
4996 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
4997 /* Load the value now to ensure correct exception behavior.
4998 Perform writeback after reading the MAC inputs. */
4999 loadval = gen_load(s, OS_LONG, addr, 0);
5002 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
5003 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
5005 loadval = addr = NULL_QREG;
5006 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
5007 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5010 gen_mac_clear_flags();
5013 /* Disabled because conditional branches clobber temporary vars. */
5014 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
5015 /* Skip the multiply if we know we will ignore it. */
5016 l1 = gen_new_label();
5017 tmp = tcg_temp_new();
5018 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
5019 gen_op_jmp_nz32(tmp, l1);
5023 if ((ext & 0x0800) == 0) {
5025 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
5026 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
5028 if (s->env->macsr & MACSR_FI) {
5029 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
5031 if (s->env->macsr & MACSR_SU)
5032 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
5034 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
5035 switch ((ext >> 9) & 3) {
5037 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
5040 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
5046 /* Save the overflow flag from the multiply. */
5047 saved_flags = tcg_temp_new();
5048 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
5050 saved_flags = NULL_QREG;
5054 /* Disabled because conditional branches clobber temporary vars. */
5055 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
5056 /* Skip the accumulate if the value is already saturated. */
5057 l1 = gen_new_label();
5058 tmp = tcg_temp_new();
5059 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
5060 gen_op_jmp_nz32(tmp, l1);
5065 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
5067 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
5069 if (s->env->macsr & MACSR_FI)
5070 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
5071 else if (s->env->macsr & MACSR_SU)
5072 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
5074 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
5077 /* Disabled because conditional branches clobber temporary vars. */
5083 /* Dual accumulate variant. */
5084 acc = (ext >> 2) & 3;
5085 /* Restore the overflow flag from the multiplier. */
5086 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
5088 /* Disabled because conditional branches clobber temporary vars. */
5089 if ((s->env->macsr & MACSR_OMC) != 0) {
5090 /* Skip the accumulate if the value is already saturated. */
5091 l1 = gen_new_label();
5092 tmp = tcg_temp_new();
5093 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
5094 gen_op_jmp_nz32(tmp, l1);
5098 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
5100 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
5101 if (s->env->macsr & MACSR_FI)
5102 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
5103 else if (s->env->macsr & MACSR_SU)
5104 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
5106 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
5108 /* Disabled because conditional branches clobber temporary vars. */
5113 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
5117 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
5118 tcg_gen_mov_i32(rw, loadval);
5119 /* FIXME: Should address writeback happen with the masked or
5121 switch ((insn >> 3) & 7) {
5122 case 3: /* Post-increment. */
5123 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
5125 case 4: /* Pre-decrement. */
5126 tcg_gen_mov_i32(AREG(insn, 0), addr);
5131 DISAS_INSN(from_mac)
5137 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5138 accnum = (insn >> 9) & 3;
5139 acc = MACREG(accnum);
5140 if (s->env->macsr & MACSR_FI) {
5141 gen_helper_get_macf(rx, cpu_env, acc);
5142 } else if ((s->env->macsr & MACSR_OMC) == 0) {
5143 tcg_gen_extrl_i64_i32(rx, acc);
5144 } else if (s->env->macsr & MACSR_SU) {
5145 gen_helper_get_macs(rx, acc);
5147 gen_helper_get_macu(rx, acc);
5150 tcg_gen_movi_i64(acc, 0);
5151 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
5155 DISAS_INSN(move_mac)
5157 /* FIXME: This can be done without a helper. */
5161 dest = tcg_const_i32((insn >> 9) & 3);
5162 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
5163 gen_mac_clear_flags();
5164 gen_helper_mac_set_flags(cpu_env, dest);
5167 DISAS_INSN(from_macsr)
5171 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5172 tcg_gen_mov_i32(reg, QREG_MACSR);
5175 DISAS_INSN(from_mask)
5178 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5179 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
5182 DISAS_INSN(from_mext)
5186 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
5187 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
5188 if (s->env->macsr & MACSR_FI)
5189 gen_helper_get_mac_extf(reg, cpu_env, acc);
5191 gen_helper_get_mac_exti(reg, cpu_env, acc);
5194 DISAS_INSN(macsr_to_ccr)
5196 TCGv tmp = tcg_temp_new();
5197 tcg_gen_andi_i32(tmp, QREG_MACSR, 0xf);
5198 gen_helper_set_sr(cpu_env, tmp);
5200 set_cc_op(s, CC_OP_FLAGS);
5208 accnum = (insn >> 9) & 3;
5209 acc = MACREG(accnum);
5210 SRC_EA(env, val, OS_LONG, 0, NULL);
5211 if (s->env->macsr & MACSR_FI) {
5212 tcg_gen_ext_i32_i64(acc, val);
5213 tcg_gen_shli_i64(acc, acc, 8);
5214 } else if (s->env->macsr & MACSR_SU) {
5215 tcg_gen_ext_i32_i64(acc, val);
5217 tcg_gen_extu_i32_i64(acc, val);
5219 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
5220 gen_mac_clear_flags();
5221 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
5224 DISAS_INSN(to_macsr)
5227 SRC_EA(env, val, OS_LONG, 0, NULL);
5228 gen_helper_set_macsr(cpu_env, val);
5235 SRC_EA(env, val, OS_LONG, 0, NULL);
5236 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
5243 SRC_EA(env, val, OS_LONG, 0, NULL);
5244 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
5245 if (s->env->macsr & MACSR_FI)
5246 gen_helper_set_mac_extf(cpu_env, val, acc);
5247 else if (s->env->macsr & MACSR_SU)
5248 gen_helper_set_mac_exts(cpu_env, val, acc);
5250 gen_helper_set_mac_extu(cpu_env, val, acc);
5253 static disas_proc opcode_table[65536];
5256 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
5262 /* Sanity check. All set bits must be included in the mask. */
5263 if (opcode & ~mask) {
5265 "qemu internal error: bogus opcode definition %04x/%04x\n",
5269 /* This could probably be cleverer. For now just optimize the case where
5270 the top bits are known. */
5271 /* Find the first zero bit in the mask. */
5273 while ((i & mask) != 0)
5275 /* Iterate over all combinations of this and lower bits. */
5280 from = opcode & ~(i - 1);
5282 for (i = from; i < to; i++) {
5283 if ((i & mask) == opcode)
5284 opcode_table[i] = proc;
5288 /* Register m68k opcode handlers. Order is important.
5289 Later insn override earlier ones. */
5290 void register_m68k_insns (CPUM68KState *env)
5292 /* Build the opcode table only once to avoid
5293 multithreading issues. */
5294 if (opcode_table[0] != NULL) {
5298 /* use BASE() for instruction available
5299 * for CF_ISA_A and M68000.
5301 #define BASE(name, opcode, mask) \
5302 register_opcode(disas_##name, 0x##opcode, 0x##mask)
5303 #define INSN(name, opcode, mask, feature) do { \
5304 if (m68k_feature(env, M68K_FEATURE_##feature)) \
5305 BASE(name, opcode, mask); \
5307 BASE(undef, 0000, 0000);
5308 INSN(arith_im, 0080, fff8, CF_ISA_A);
5309 INSN(arith_im, 0000, ff00, M68000);
5310 INSN(undef, 00c0, ffc0, M68000);
5311 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
5312 BASE(bitop_reg, 0100, f1c0);
5313 BASE(bitop_reg, 0140, f1c0);
5314 BASE(bitop_reg, 0180, f1c0);
5315 BASE(bitop_reg, 01c0, f1c0);
5316 INSN(arith_im, 0280, fff8, CF_ISA_A);
5317 INSN(arith_im, 0200, ff00, M68000);
5318 INSN(undef, 02c0, ffc0, M68000);
5319 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
5320 INSN(arith_im, 0480, fff8, CF_ISA_A);
5321 INSN(arith_im, 0400, ff00, M68000);
5322 INSN(undef, 04c0, ffc0, M68000);
5323 INSN(arith_im, 0600, ff00, M68000);
5324 INSN(undef, 06c0, ffc0, M68000);
5325 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
5326 INSN(arith_im, 0680, fff8, CF_ISA_A);
5327 INSN(arith_im, 0c00, ff38, CF_ISA_A);
5328 INSN(arith_im, 0c00, ff00, M68000);
5329 BASE(bitop_im, 0800, ffc0);
5330 BASE(bitop_im, 0840, ffc0);
5331 BASE(bitop_im, 0880, ffc0);
5332 BASE(bitop_im, 08c0, ffc0);
5333 INSN(arith_im, 0a80, fff8, CF_ISA_A);
5334 INSN(arith_im, 0a00, ff00, M68000);
5335 INSN(cas, 0ac0, ffc0, CAS);
5336 INSN(cas, 0cc0, ffc0, CAS);
5337 INSN(cas, 0ec0, ffc0, CAS);
5338 INSN(cas2w, 0cfc, ffff, CAS);
5339 INSN(cas2l, 0efc, ffff, CAS);
5340 BASE(move, 1000, f000);
5341 BASE(move, 2000, f000);
5342 BASE(move, 3000, f000);
5343 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
5344 INSN(negx, 4080, fff8, CF_ISA_A);
5345 INSN(negx, 4000, ff00, M68000);
5346 INSN(undef, 40c0, ffc0, M68000);
5347 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
5348 INSN(move_from_sr, 40c0, ffc0, M68000);
5349 BASE(lea, 41c0, f1c0);
5350 BASE(clr, 4200, ff00);
5351 BASE(undef, 42c0, ffc0);
5352 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
5353 INSN(move_from_ccr, 42c0, ffc0, M68000);
5354 INSN(neg, 4480, fff8, CF_ISA_A);
5355 INSN(neg, 4400, ff00, M68000);
5356 INSN(undef, 44c0, ffc0, M68000);
5357 BASE(move_to_ccr, 44c0, ffc0);
5358 INSN(not, 4680, fff8, CF_ISA_A);
5359 INSN(not, 4600, ff00, M68000);
5360 INSN(undef, 46c0, ffc0, M68000);
5361 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
5362 INSN(nbcd, 4800, ffc0, M68000);
5363 INSN(linkl, 4808, fff8, M68000);
5364 BASE(pea, 4840, ffc0);
5365 BASE(swap, 4840, fff8);
5366 INSN(bkpt, 4848, fff8, BKPT);
5367 INSN(movem, 48d0, fbf8, CF_ISA_A);
5368 INSN(movem, 48e8, fbf8, CF_ISA_A);
5369 INSN(movem, 4880, fb80, M68000);
5370 BASE(ext, 4880, fff8);
5371 BASE(ext, 48c0, fff8);
5372 BASE(ext, 49c0, fff8);
5373 BASE(tst, 4a00, ff00);
5374 INSN(tas, 4ac0, ffc0, CF_ISA_B);
5375 INSN(tas, 4ac0, ffc0, M68000);
5376 INSN(halt, 4ac8, ffff, CF_ISA_A);
5377 INSN(pulse, 4acc, ffff, CF_ISA_A);
5378 BASE(illegal, 4afc, ffff);
5379 INSN(mull, 4c00, ffc0, CF_ISA_A);
5380 INSN(mull, 4c00, ffc0, LONG_MULDIV);
5381 INSN(divl, 4c40, ffc0, CF_ISA_A);
5382 INSN(divl, 4c40, ffc0, LONG_MULDIV);
5383 INSN(sats, 4c80, fff8, CF_ISA_B);
5384 BASE(trap, 4e40, fff0);
5385 BASE(link, 4e50, fff8);
5386 BASE(unlk, 4e58, fff8);
5387 INSN(move_to_usp, 4e60, fff8, USP);
5388 INSN(move_from_usp, 4e68, fff8, USP);
5389 BASE(nop, 4e71, ffff);
5390 BASE(stop, 4e72, ffff);
5391 BASE(rte, 4e73, ffff);
5392 INSN(rtd, 4e74, ffff, RTD);
5393 BASE(rts, 4e75, ffff);
5394 INSN(movec, 4e7b, ffff, CF_ISA_A);
5395 BASE(jump, 4e80, ffc0);
5396 BASE(jump, 4ec0, ffc0);
5397 INSN(addsubq, 5000, f080, M68000);
5398 BASE(addsubq, 5080, f0c0);
5399 INSN(scc, 50c0, f0f8, CF_ISA_A); /* Scc.B Dx */
5400 INSN(scc, 50c0, f0c0, M68000); /* Scc.B <EA> */
5401 INSN(dbcc, 50c8, f0f8, M68000);
5402 INSN(tpf, 51f8, fff8, CF_ISA_A);
5404 /* Branch instructions. */
5405 BASE(branch, 6000, f000);
5406 /* Disable long branch instructions, then add back the ones we want. */
5407 BASE(undef, 60ff, f0ff); /* All long branches. */
5408 INSN(branch, 60ff, f0ff, CF_ISA_B);
5409 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
5410 INSN(branch, 60ff, ffff, BRAL);
5411 INSN(branch, 60ff, f0ff, BCCL);
5413 BASE(moveq, 7000, f100);
5414 INSN(mvzs, 7100, f100, CF_ISA_B);
5415 BASE(or, 8000, f000);
5416 BASE(divw, 80c0, f0c0);
5417 INSN(sbcd_reg, 8100, f1f8, M68000);
5418 INSN(sbcd_mem, 8108, f1f8, M68000);
5419 BASE(addsub, 9000, f000);
5420 INSN(undef, 90c0, f0c0, CF_ISA_A);
5421 INSN(subx_reg, 9180, f1f8, CF_ISA_A);
5422 INSN(subx_reg, 9100, f138, M68000);
5423 INSN(subx_mem, 9108, f138, M68000);
5424 INSN(suba, 91c0, f1c0, CF_ISA_A);
5425 INSN(suba, 90c0, f0c0, M68000);
5427 BASE(undef_mac, a000, f000);
5428 INSN(mac, a000, f100, CF_EMAC);
5429 INSN(from_mac, a180, f9b0, CF_EMAC);
5430 INSN(move_mac, a110, f9fc, CF_EMAC);
5431 INSN(from_macsr,a980, f9f0, CF_EMAC);
5432 INSN(from_mask, ad80, fff0, CF_EMAC);
5433 INSN(from_mext, ab80, fbf0, CF_EMAC);
5434 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
5435 INSN(to_mac, a100, f9c0, CF_EMAC);
5436 INSN(to_macsr, a900, ffc0, CF_EMAC);
5437 INSN(to_mext, ab00, fbc0, CF_EMAC);
5438 INSN(to_mask, ad00, ffc0, CF_EMAC);
5440 INSN(mov3q, a140, f1c0, CF_ISA_B);
5441 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
5442 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
5443 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
5444 INSN(cmp, b080, f1c0, CF_ISA_A);
5445 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
5446 INSN(cmp, b000, f100, M68000);
5447 INSN(eor, b100, f100, M68000);
5448 INSN(cmpm, b108, f138, M68000);
5449 INSN(cmpa, b0c0, f0c0, M68000);
5450 INSN(eor, b180, f1c0, CF_ISA_A);
5451 BASE(and, c000, f000);
5452 INSN(exg_dd, c140, f1f8, M68000);
5453 INSN(exg_aa, c148, f1f8, M68000);
5454 INSN(exg_da, c188, f1f8, M68000);
5455 BASE(mulw, c0c0, f0c0);
5456 INSN(abcd_reg, c100, f1f8, M68000);
5457 INSN(abcd_mem, c108, f1f8, M68000);
5458 BASE(addsub, d000, f000);
5459 INSN(undef, d0c0, f0c0, CF_ISA_A);
5460 INSN(addx_reg, d180, f1f8, CF_ISA_A);
5461 INSN(addx_reg, d100, f138, M68000);
5462 INSN(addx_mem, d108, f138, M68000);
5463 INSN(adda, d1c0, f1c0, CF_ISA_A);
5464 INSN(adda, d0c0, f0c0, M68000);
5465 INSN(shift_im, e080, f0f0, CF_ISA_A);
5466 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
5467 INSN(shift8_im, e000, f0f0, M68000);
5468 INSN(shift16_im, e040, f0f0, M68000);
5469 INSN(shift_im, e080, f0f0, M68000);
5470 INSN(shift8_reg, e020, f0f0, M68000);
5471 INSN(shift16_reg, e060, f0f0, M68000);
5472 INSN(shift_reg, e0a0, f0f0, M68000);
5473 INSN(shift_mem, e0c0, fcc0, M68000);
5474 INSN(rotate_im, e090, f0f0, M68000);
5475 INSN(rotate8_im, e010, f0f0, M68000);
5476 INSN(rotate16_im, e050, f0f0, M68000);
5477 INSN(rotate_reg, e0b0, f0f0, M68000);
5478 INSN(rotate8_reg, e030, f0f0, M68000);
5479 INSN(rotate16_reg, e070, f0f0, M68000);
5480 INSN(rotate_mem, e4c0, fcc0, M68000);
5481 INSN(bfext_mem, e9c0, fdc0, BITFIELD); /* bfextu & bfexts */
5482 INSN(bfext_reg, e9c0, fdf8, BITFIELD);
5483 INSN(bfins_mem, efc0, ffc0, BITFIELD);
5484 INSN(bfins_reg, efc0, fff8, BITFIELD);
5485 INSN(bfop_mem, eac0, ffc0, BITFIELD); /* bfchg */
5486 INSN(bfop_reg, eac0, fff8, BITFIELD); /* bfchg */
5487 INSN(bfop_mem, ecc0, ffc0, BITFIELD); /* bfclr */
5488 INSN(bfop_reg, ecc0, fff8, BITFIELD); /* bfclr */
5489 INSN(bfop_mem, edc0, ffc0, BITFIELD); /* bfffo */
5490 INSN(bfop_reg, edc0, fff8, BITFIELD); /* bfffo */
5491 INSN(bfop_mem, eec0, ffc0, BITFIELD); /* bfset */
5492 INSN(bfop_reg, eec0, fff8, BITFIELD); /* bfset */
5493 INSN(bfop_mem, e8c0, ffc0, BITFIELD); /* bftst */
5494 INSN(bfop_reg, e8c0, fff8, BITFIELD); /* bftst */
5495 BASE(undef_fpu, f000, f000);
5496 INSN(fpu, f200, ffc0, CF_FPU);
5497 INSN(fbcc, f280, ffc0, CF_FPU);
5498 INSN(frestore, f340, ffc0, CF_FPU);
5499 INSN(fsave, f300, ffc0, CF_FPU);
5500 INSN(fpu, f200, ffc0, FPU);
5501 INSN(fscc, f240, ffc0, FPU);
5502 INSN(fbcc, f280, ff80, FPU);
5503 INSN(frestore, f340, ffc0, FPU);
5504 INSN(fsave, f300, ffc0, FPU);
5505 INSN(intouch, f340, ffc0, CF_ISA_A);
5506 INSN(cpushl, f428, ff38, CF_ISA_A);
5507 INSN(wddata, fb00, ff00, CF_ISA_A);
5508 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
5512 /* ??? Some of this implementation is not exception safe. We should always
5513 write back the result to memory before setting the condition codes. */
5514 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
5516 uint16_t insn = read_im16(env, s);
5517 opcode_table[insn](env, s, insn);
5521 /* generate intermediate code for basic block 'tb'. */
5522 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
5524 M68kCPU *cpu = m68k_env_get_cpu(env);
5525 CPUState *cs = CPU(cpu);
5526 DisasContext dc1, *dc = &dc1;
5527 target_ulong pc_start;
5532 /* generate intermediate code */
5538 dc->is_jmp = DISAS_NEXT;
5540 dc->cc_op = CC_OP_DYNAMIC;
5541 dc->cc_op_synced = 1;
5542 dc->singlestep_enabled = cs->singlestep_enabled;
5543 dc->user = (env->sr & SR_S) == 0;
5545 dc->writeback_mask = 0;
5547 max_insns = tb->cflags & CF_COUNT_MASK;
5548 if (max_insns == 0) {
5549 max_insns = CF_COUNT_MASK;
5551 if (max_insns > TCG_MAX_INSNS) {
5552 max_insns = TCG_MAX_INSNS;
5557 pc_offset = dc->pc - pc_start;
5558 gen_throws_exception = NULL;
5559 tcg_gen_insn_start(dc->pc, dc->cc_op);
5562 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
5563 gen_exception(dc, dc->pc, EXCP_DEBUG);
5564 dc->is_jmp = DISAS_JUMP;
5565 /* The address covered by the breakpoint must be included in
5566 [tb->pc, tb->pc + tb->size) in order to for it to be
5567 properly cleared -- thus we increment the PC here so that
5568 the logic setting tb->size below does the right thing. */
5573 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
5577 dc->insn_pc = dc->pc;
5578 disas_m68k_insn(env, dc);
5579 } while (!dc->is_jmp && !tcg_op_buf_full() &&
5580 !cs->singlestep_enabled &&
5582 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
5583 num_insns < max_insns);
5585 if (tb->cflags & CF_LAST_IO)
5587 if (unlikely(cs->singlestep_enabled)) {
5588 /* Make sure the pc is updated, and raise a debug exception. */
5591 tcg_gen_movi_i32(QREG_PC, dc->pc);
5593 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
5595 switch(dc->is_jmp) {
5598 gen_jmp_tb(dc, 0, dc->pc);
5604 /* indicate that the hash table must be used to find the next TB */
5608 /* nothing more to generate */
5612 gen_tb_end(tb, num_insns);
5615 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
5616 && qemu_log_in_addr_range(pc_start)) {
5618 qemu_log("----------------\n");
5619 qemu_log("IN: %s\n", lookup_symbol(pc_start));
5620 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
5625 tb->size = dc->pc - pc_start;
5626 tb->icount = num_insns;
5629 static double floatx80_to_double(CPUM68KState *env, uint16_t high, uint64_t low)
5631 floatx80 a = { .high = high, .low = low };
5637 u.f64 = floatx80_to_float64(a, &env->fp_status);
5641 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
5644 M68kCPU *cpu = M68K_CPU(cs);
5645 CPUM68KState *env = &cpu->env;
5648 for (i = 0; i < 8; i++) {
5649 cpu_fprintf(f, "D%d = %08x A%d = %08x "
5650 "F%d = %04x %016"PRIx64" (%12g)\n",
5651 i, env->dregs[i], i, env->aregs[i],
5652 i, env->fregs[i].l.upper, env->fregs[i].l.lower,
5653 floatx80_to_double(env, env->fregs[i].l.upper,
5654 env->fregs[i].l.lower));
5656 cpu_fprintf (f, "PC = %08x ", env->pc);
5657 sr = env->sr | cpu_m68k_get_ccr(env);
5658 cpu_fprintf(f, "SR = %04x %c%c%c%c%c ", sr, (sr & CCF_X) ? 'X' : '-',
5659 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
5660 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
5661 cpu_fprintf(f, "FPSR = %08x %c%c%c%c ", env->fpsr,
5662 (env->fpsr & FPSR_CC_A) ? 'A' : '-',
5663 (env->fpsr & FPSR_CC_I) ? 'I' : '-',
5664 (env->fpsr & FPSR_CC_Z) ? 'Z' : '-',
5665 (env->fpsr & FPSR_CC_N) ? 'N' : '-');
5666 cpu_fprintf(f, "\n "
5667 "FPCR = %04x ", env->fpcr);
5668 switch (env->fpcr & FPCR_PREC_MASK) {
5670 cpu_fprintf(f, "X ");
5673 cpu_fprintf(f, "S ");
5676 cpu_fprintf(f, "D ");
5679 switch (env->fpcr & FPCR_RND_MASK) {
5681 cpu_fprintf(f, "RN ");
5684 cpu_fprintf(f, "RZ ");
5687 cpu_fprintf(f, "RM ");
5690 cpu_fprintf(f, "RP ");
5695 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,
5698 int cc_op = data[1];
5700 if (cc_op != CC_OP_DYNAMIC) {