4 * Copyright (c) 2005-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
36 //#define DEBUG_DISPATCH 1
38 /* Fake floating point. */
39 #define tcg_gen_mov_f64 tcg_gen_mov_i64
40 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
41 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
43 #define DEFO32(name, offset) static TCGv QREG_##name;
44 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
45 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
51 static TCGv_i32 cpu_halted;
52 static TCGv_i32 cpu_exception_index;
54 static TCGv_env cpu_env;
56 static char cpu_reg_names[3*8*3 + 5*4];
57 static TCGv cpu_dregs[8];
58 static TCGv cpu_aregs[8];
59 static TCGv_i64 cpu_fregs[8];
60 static TCGv_i64 cpu_macc[4];
62 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
63 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
64 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
65 #define MACREG(acc) cpu_macc[acc]
66 #define QREG_SP cpu_aregs[7]
68 static TCGv NULL_QREG;
69 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
70 /* Used to distinguish stores from bad addressing modes. */
71 static TCGv store_dummy;
73 #include "exec/gen-icount.h"
75 void m68k_tcg_init(void)
80 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
81 tcg_ctx.tcg_env = cpu_env;
83 #define DEFO32(name, offset) \
84 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
85 offsetof(CPUM68KState, offset), #name);
86 #define DEFO64(name, offset) \
87 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
88 offsetof(CPUM68KState, offset), #name);
89 #define DEFF64(name, offset) DEFO64(name, offset)
95 cpu_halted = tcg_global_mem_new_i32(cpu_env,
96 -offsetof(M68kCPU, env) +
97 offsetof(CPUState, halted), "HALTED");
98 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
99 -offsetof(M68kCPU, env) +
100 offsetof(CPUState, exception_index),
104 for (i = 0; i < 8; i++) {
105 sprintf(p, "D%d", i);
106 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
107 offsetof(CPUM68KState, dregs[i]), p);
109 sprintf(p, "A%d", i);
110 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
111 offsetof(CPUM68KState, aregs[i]), p);
113 sprintf(p, "F%d", i);
114 cpu_fregs[i] = tcg_global_mem_new_i64(cpu_env,
115 offsetof(CPUM68KState, fregs[i]), p);
118 for (i = 0; i < 4; i++) {
119 sprintf(p, "ACC%d", i);
120 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
121 offsetof(CPUM68KState, macc[i]), p);
125 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
126 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
129 /* internal defines */
130 typedef struct DisasContext {
132 target_ulong insn_pc; /* Start of the current instruction. */
138 struct TranslationBlock *tb;
139 int singlestep_enabled;
144 #define DISAS_JUMP_NEXT 4
146 #if defined(CONFIG_USER_ONLY)
149 #define IS_USER(s) s->user
152 /* XXX: move that elsewhere */
153 /* ??? Fix exceptions. */
154 static void *gen_throws_exception;
155 #define gen_last_qop NULL
163 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
165 #ifdef DEBUG_DISPATCH
166 #define DISAS_INSN(name) \
167 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
169 static void disas_##name(CPUM68KState *env, DisasContext *s, \
172 qemu_log("Dispatch " #name "\n"); \
173 real_disas_##name(s, env, insn); \
175 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
178 #define DISAS_INSN(name) \
179 static void disas_##name(CPUM68KState *env, DisasContext *s, \
183 /* Generate a load from the specified address. Narrow values are
184 sign extended to full register width. */
185 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
188 int index = IS_USER(s);
189 tmp = tcg_temp_new_i32();
193 tcg_gen_qemu_ld8s(tmp, addr, index);
195 tcg_gen_qemu_ld8u(tmp, addr, index);
199 tcg_gen_qemu_ld16s(tmp, addr, index);
201 tcg_gen_qemu_ld16u(tmp, addr, index);
205 tcg_gen_qemu_ld32u(tmp, addr, index);
208 g_assert_not_reached();
210 gen_throws_exception = gen_last_qop;
214 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
217 int index = IS_USER(s);
218 tmp = tcg_temp_new_i64();
219 tcg_gen_qemu_ldf64(tmp, addr, index);
220 gen_throws_exception = gen_last_qop;
224 /* Generate a store. */
225 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
227 int index = IS_USER(s);
230 tcg_gen_qemu_st8(val, addr, index);
233 tcg_gen_qemu_st16(val, addr, index);
237 tcg_gen_qemu_st32(val, addr, index);
240 g_assert_not_reached();
242 gen_throws_exception = gen_last_qop;
245 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
247 int index = IS_USER(s);
248 tcg_gen_qemu_stf64(val, addr, index);
249 gen_throws_exception = gen_last_qop;
258 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
259 otherwise generate a store. */
260 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
263 if (what == EA_STORE) {
264 gen_store(s, opsize, addr, val);
267 return gen_load(s, opsize, addr, what == EA_LOADS);
271 /* Read a 32-bit immediate constant. */
272 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
275 im = ((uint32_t)cpu_lduw_code(env, s->pc)) << 16;
277 im |= cpu_lduw_code(env, s->pc);
282 /* Calculate and address index. */
283 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
288 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
289 if ((ext & 0x800) == 0) {
290 tcg_gen_ext16s_i32(tmp, add);
293 scale = (ext >> 9) & 3;
295 tcg_gen_shli_i32(tmp, add, scale);
301 /* Handle a base + index + displacement effective addresss.
302 A NULL_QREG base means pc-relative. */
303 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
312 ext = cpu_lduw_code(env, s->pc);
315 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
319 /* full extension word format */
320 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
323 if ((ext & 0x30) > 0x10) {
324 /* base displacement */
325 if ((ext & 0x30) == 0x20) {
326 bd = (int16_t)cpu_lduw_code(env, s->pc);
329 bd = read_im32(env, s);
334 tmp = tcg_temp_new();
335 if ((ext & 0x44) == 0) {
337 add = gen_addr_index(ext, tmp);
341 if ((ext & 0x80) == 0) {
342 /* base not suppressed */
343 if (IS_NULL_QREG(base)) {
344 base = tcg_const_i32(offset + bd);
347 if (!IS_NULL_QREG(add)) {
348 tcg_gen_add_i32(tmp, add, base);
354 if (!IS_NULL_QREG(add)) {
356 tcg_gen_addi_i32(tmp, add, bd);
360 add = tcg_const_i32(bd);
362 if ((ext & 3) != 0) {
363 /* memory indirect */
364 base = gen_load(s, OS_LONG, add, 0);
365 if ((ext & 0x44) == 4) {
366 add = gen_addr_index(ext, tmp);
367 tcg_gen_add_i32(tmp, add, base);
373 /* outer displacement */
374 if ((ext & 3) == 2) {
375 od = (int16_t)cpu_lduw_code(env, s->pc);
378 od = read_im32(env, s);
384 tcg_gen_addi_i32(tmp, add, od);
389 /* brief extension word format */
390 tmp = tcg_temp_new();
391 add = gen_addr_index(ext, tmp);
392 if (!IS_NULL_QREG(base)) {
393 tcg_gen_add_i32(tmp, add, base);
395 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
397 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
404 /* Update the CPU env CC_OP state. */
405 static inline void gen_flush_cc_op(DisasContext *s)
407 if (s->cc_op != CC_OP_DYNAMIC)
408 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
411 /* Evaluate all the CC flags. */
412 static inline void gen_flush_flags(DisasContext *s)
414 if (s->cc_op == CC_OP_FLAGS)
417 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
418 s->cc_op = CC_OP_FLAGS;
421 static void gen_logic_cc(DisasContext *s, TCGv val)
423 tcg_gen_mov_i32(QREG_CC_DEST, val);
424 s->cc_op = CC_OP_LOGIC;
427 static void gen_update_cc_add(TCGv dest, TCGv src)
429 tcg_gen_mov_i32(QREG_CC_DEST, dest);
430 tcg_gen_mov_i32(QREG_CC_SRC, src);
433 static inline int opsize_bytes(int opsize)
436 case OS_BYTE: return 1;
437 case OS_WORD: return 2;
438 case OS_LONG: return 4;
439 case OS_SINGLE: return 4;
440 case OS_DOUBLE: return 8;
442 g_assert_not_reached();
446 /* Assign value to a register. If the width is less than the register width
447 only the low part of the register is set. */
448 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
453 tcg_gen_andi_i32(reg, reg, 0xffffff00);
454 tmp = tcg_temp_new();
455 tcg_gen_ext8u_i32(tmp, val);
456 tcg_gen_or_i32(reg, reg, tmp);
459 tcg_gen_andi_i32(reg, reg, 0xffff0000);
460 tmp = tcg_temp_new();
461 tcg_gen_ext16u_i32(tmp, val);
462 tcg_gen_or_i32(reg, reg, tmp);
466 tcg_gen_mov_i32(reg, val);
469 g_assert_not_reached();
473 /* Sign or zero extend a value. */
474 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
480 tmp = tcg_temp_new();
482 tcg_gen_ext8s_i32(tmp, val);
484 tcg_gen_ext8u_i32(tmp, val);
487 tmp = tcg_temp_new();
489 tcg_gen_ext16s_i32(tmp, val);
491 tcg_gen_ext16u_i32(tmp, val);
498 g_assert_not_reached();
503 /* Generate code for an "effective address". Does not adjust the base
504 register for autoincrement addressing modes. */
505 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
513 switch ((insn >> 3) & 7) {
514 case 0: /* Data register direct. */
515 case 1: /* Address register direct. */
517 case 2: /* Indirect register */
518 case 3: /* Indirect postincrement. */
519 return AREG(insn, 0);
520 case 4: /* Indirect predecrememnt. */
522 tmp = tcg_temp_new();
523 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
525 case 5: /* Indirect displacement. */
527 tmp = tcg_temp_new();
528 ext = cpu_lduw_code(env, s->pc);
530 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
532 case 6: /* Indirect index + displacement. */
534 return gen_lea_indexed(env, s, reg);
537 case 0: /* Absolute short. */
538 offset = cpu_ldsw_code(env, s->pc);
540 return tcg_const_i32(offset);
541 case 1: /* Absolute long. */
542 offset = read_im32(env, s);
543 return tcg_const_i32(offset);
544 case 2: /* pc displacement */
546 offset += cpu_ldsw_code(env, s->pc);
548 return tcg_const_i32(offset);
549 case 3: /* pc index+displacement. */
550 return gen_lea_indexed(env, s, NULL_QREG);
551 case 4: /* Immediate. */
556 /* Should never happen. */
560 /* Helper function for gen_ea. Reuse the computed address between the
561 for read/write operands. */
562 static inline TCGv gen_ea_once(CPUM68KState *env, DisasContext *s,
563 uint16_t insn, int opsize, TCGv val,
564 TCGv *addrp, ea_what what)
568 if (addrp && what == EA_STORE) {
571 tmp = gen_lea(env, s, insn, opsize);
572 if (IS_NULL_QREG(tmp))
577 return gen_ldst(s, opsize, tmp, val, what);
580 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
581 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
582 ADDRP is non-null for readwrite operands. */
583 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
584 int opsize, TCGv val, TCGv *addrp, ea_what what)
590 switch ((insn >> 3) & 7) {
591 case 0: /* Data register direct. */
593 if (what == EA_STORE) {
594 gen_partset_reg(opsize, reg, val);
597 return gen_extend(reg, opsize, what == EA_LOADS);
599 case 1: /* Address register direct. */
601 if (what == EA_STORE) {
602 tcg_gen_mov_i32(reg, val);
605 return gen_extend(reg, opsize, what == EA_LOADS);
607 case 2: /* Indirect register */
609 return gen_ldst(s, opsize, reg, val, what);
610 case 3: /* Indirect postincrement. */
612 result = gen_ldst(s, opsize, reg, val, what);
613 /* ??? This is not exception safe. The instruction may still
614 fault after this point. */
615 if (what == EA_STORE || !addrp)
616 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
618 case 4: /* Indirect predecrememnt. */
621 if (addrp && what == EA_STORE) {
624 tmp = gen_lea(env, s, insn, opsize);
625 if (IS_NULL_QREG(tmp))
630 result = gen_ldst(s, opsize, tmp, val, what);
631 /* ??? This is not exception safe. The instruction may still
632 fault after this point. */
633 if (what == EA_STORE || !addrp) {
635 tcg_gen_mov_i32(reg, tmp);
639 case 5: /* Indirect displacement. */
640 case 6: /* Indirect index + displacement. */
641 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
644 case 0: /* Absolute short. */
645 case 1: /* Absolute long. */
646 case 2: /* pc displacement */
647 case 3: /* pc index+displacement. */
648 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
649 case 4: /* Immediate. */
650 /* Sign extend values for consistency. */
653 if (what == EA_LOADS) {
654 offset = cpu_ldsb_code(env, s->pc + 1);
656 offset = cpu_ldub_code(env, s->pc + 1);
661 if (what == EA_LOADS) {
662 offset = cpu_ldsw_code(env, s->pc);
664 offset = cpu_lduw_code(env, s->pc);
669 offset = read_im32(env, s);
672 g_assert_not_reached();
674 return tcg_const_i32(offset);
679 /* Should never happen. */
683 /* This generates a conditional branch, clobbering all temporaries. */
684 static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
688 /* TODO: Optimize compare/branch pairs rather than always flushing
689 flag state to CC_OP_FLAGS. */
697 case 2: /* HI (!C && !Z) */
698 tmp = tcg_temp_new();
699 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
700 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
702 case 3: /* LS (C || Z) */
703 tmp = tcg_temp_new();
704 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
705 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
707 case 4: /* CC (!C) */
708 tmp = tcg_temp_new();
709 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
710 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
713 tmp = tcg_temp_new();
714 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
715 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
717 case 6: /* NE (!Z) */
718 tmp = tcg_temp_new();
719 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
720 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
723 tmp = tcg_temp_new();
724 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
725 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
727 case 8: /* VC (!V) */
728 tmp = tcg_temp_new();
729 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
730 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
733 tmp = tcg_temp_new();
734 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
735 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
737 case 10: /* PL (!N) */
738 tmp = tcg_temp_new();
739 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
740 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
742 case 11: /* MI (N) */
743 tmp = tcg_temp_new();
744 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
745 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
747 case 12: /* GE (!(N ^ V)) */
748 tmp = tcg_temp_new();
749 assert(CCF_V == (CCF_N >> 2));
750 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
751 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
752 tcg_gen_andi_i32(tmp, tmp, CCF_V);
753 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
755 case 13: /* LT (N ^ V) */
756 tmp = tcg_temp_new();
757 assert(CCF_V == (CCF_N >> 2));
758 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
759 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
760 tcg_gen_andi_i32(tmp, tmp, CCF_V);
761 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
763 case 14: /* GT (!(Z || (N ^ V))) */
764 tmp = tcg_temp_new();
765 assert(CCF_V == (CCF_N >> 2));
766 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
767 tcg_gen_shri_i32(tmp, tmp, 2);
768 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
769 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
770 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
772 case 15: /* LE (Z || (N ^ V)) */
773 tmp = tcg_temp_new();
774 assert(CCF_V == (CCF_N >> 2));
775 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
776 tcg_gen_shri_i32(tmp, tmp, 2);
777 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
778 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
779 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
782 /* Should ever happen. */
793 l1 = gen_new_label();
794 cond = (insn >> 8) & 0xf;
796 tcg_gen_andi_i32(reg, reg, 0xffffff00);
797 /* This is safe because we modify the reg directly, with no other values
799 gen_jmpcc(s, cond ^ 1, l1);
800 tcg_gen_ori_i32(reg, reg, 0xff);
804 /* Force a TB lookup after an instruction that changes the CPU state. */
805 static void gen_lookup_tb(DisasContext *s)
808 tcg_gen_movi_i32(QREG_PC, s->pc);
809 s->is_jmp = DISAS_UPDATE;
812 /* Generate a jump to an immediate address. */
813 static void gen_jmp_im(DisasContext *s, uint32_t dest)
816 tcg_gen_movi_i32(QREG_PC, dest);
817 s->is_jmp = DISAS_JUMP;
820 /* Generate a jump to the address in qreg DEST. */
821 static void gen_jmp(DisasContext *s, TCGv dest)
824 tcg_gen_mov_i32(QREG_PC, dest);
825 s->is_jmp = DISAS_JUMP;
828 static void gen_exception(DisasContext *s, uint32_t where, int nr)
831 gen_jmp_im(s, where);
832 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
835 static inline void gen_addr_fault(DisasContext *s)
837 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
840 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
841 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
842 op_sign ? EA_LOADS : EA_LOADU); \
843 if (IS_NULL_QREG(result)) { \
849 #define DEST_EA(env, insn, opsize, val, addrp) do { \
850 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
851 if (IS_NULL_QREG(ea_result)) { \
857 static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
859 #ifndef CONFIG_USER_ONLY
860 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
861 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
867 /* Generate a jump to an immediate address. */
868 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
870 if (unlikely(s->singlestep_enabled)) {
871 gen_exception(s, dest, EXCP_DEBUG);
872 } else if (use_goto_tb(s, dest)) {
874 tcg_gen_movi_i32(QREG_PC, dest);
875 tcg_gen_exit_tb((uintptr_t)s->tb + n);
880 s->is_jmp = DISAS_TB_JUMP;
883 DISAS_INSN(undef_mac)
885 gen_exception(s, s->pc - 2, EXCP_LINEA);
888 DISAS_INSN(undef_fpu)
890 gen_exception(s, s->pc - 2, EXCP_LINEF);
895 M68kCPU *cpu = m68k_env_get_cpu(env);
897 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
898 cpu_abort(CPU(cpu), "Illegal instruction: %04x @ %08x", insn, s->pc - 2);
908 sign = (insn & 0x100) != 0;
910 tmp = tcg_temp_new();
912 tcg_gen_ext16s_i32(tmp, reg);
914 tcg_gen_ext16u_i32(tmp, reg);
915 SRC_EA(env, src, OS_WORD, sign, NULL);
916 tcg_gen_mul_i32(tmp, tmp, src);
917 tcg_gen_mov_i32(reg, tmp);
918 /* Unlike m68k, coldfire always clears the overflow bit. */
919 gen_logic_cc(s, tmp);
929 sign = (insn & 0x100) != 0;
932 tcg_gen_ext16s_i32(QREG_DIV1, reg);
934 tcg_gen_ext16u_i32(QREG_DIV1, reg);
936 SRC_EA(env, src, OS_WORD, sign, NULL);
937 tcg_gen_mov_i32(QREG_DIV2, src);
939 gen_helper_divs(cpu_env, tcg_const_i32(1));
941 gen_helper_divu(cpu_env, tcg_const_i32(1));
944 tmp = tcg_temp_new();
945 src = tcg_temp_new();
946 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
947 tcg_gen_shli_i32(src, QREG_DIV2, 16);
948 tcg_gen_or_i32(reg, tmp, src);
949 s->cc_op = CC_OP_FLAGS;
959 ext = cpu_lduw_code(env, s->pc);
962 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
967 tcg_gen_mov_i32(QREG_DIV1, num);
968 SRC_EA(env, den, OS_LONG, 0, NULL);
969 tcg_gen_mov_i32(QREG_DIV2, den);
971 gen_helper_divs(cpu_env, tcg_const_i32(0));
973 gen_helper_divu(cpu_env, tcg_const_i32(0));
975 if ((ext & 7) == ((ext >> 12) & 7)) {
977 tcg_gen_mov_i32 (reg, QREG_DIV1);
980 tcg_gen_mov_i32 (reg, QREG_DIV2);
982 s->cc_op = CC_OP_FLAGS;
994 add = (insn & 0x4000) != 0;
996 dest = tcg_temp_new();
998 SRC_EA(env, tmp, OS_LONG, 0, &addr);
1002 SRC_EA(env, src, OS_LONG, 0, NULL);
1005 tcg_gen_add_i32(dest, tmp, src);
1006 gen_helper_xflag_lt(QREG_CC_X, dest, src);
1007 s->cc_op = CC_OP_ADD;
1009 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
1010 tcg_gen_sub_i32(dest, tmp, src);
1011 s->cc_op = CC_OP_SUB;
1013 gen_update_cc_add(dest, src);
1015 DEST_EA(env, insn, OS_LONG, dest, &addr);
1017 tcg_gen_mov_i32(reg, dest);
1022 /* Reverse the order of the bits in REG. */
1026 reg = DREG(insn, 0);
1027 gen_helper_bitrev(reg, reg);
1030 DISAS_INSN(bitop_reg)
1040 if ((insn & 0x38) != 0)
1044 op = (insn >> 6) & 3;
1045 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1046 src2 = DREG(insn, 9);
1047 dest = tcg_temp_new();
1050 tmp = tcg_temp_new();
1051 if (opsize == OS_BYTE)
1052 tcg_gen_andi_i32(tmp, src2, 7);
1054 tcg_gen_andi_i32(tmp, src2, 31);
1056 tmp = tcg_temp_new();
1057 tcg_gen_shr_i32(tmp, src1, src2);
1058 tcg_gen_andi_i32(tmp, tmp, 1);
1059 tcg_gen_shli_i32(tmp, tmp, 2);
1060 /* Clear CCF_Z if bit set. */
1061 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1062 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1064 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
1067 tcg_gen_xor_i32(dest, src1, tmp);
1070 tcg_gen_not_i32(tmp, tmp);
1071 tcg_gen_and_i32(dest, src1, tmp);
1074 tcg_gen_or_i32(dest, src1, tmp);
1080 DEST_EA(env, insn, opsize, dest, &addr);
1086 reg = DREG(insn, 0);
1088 gen_helper_sats(reg, reg, QREG_CC_DEST);
1089 gen_logic_cc(s, reg);
1092 static void gen_push(DisasContext *s, TCGv val)
1096 tmp = tcg_temp_new();
1097 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1098 gen_store(s, OS_LONG, tmp, val);
1099 tcg_gen_mov_i32(QREG_SP, tmp);
1111 mask = cpu_lduw_code(env, s->pc);
1113 tmp = gen_lea(env, s, insn, OS_LONG);
1114 if (IS_NULL_QREG(tmp)) {
1118 addr = tcg_temp_new();
1119 tcg_gen_mov_i32(addr, tmp);
1120 is_load = ((insn & 0x0400) != 0);
1121 for (i = 0; i < 16; i++, mask >>= 1) {
1128 tmp = gen_load(s, OS_LONG, addr, 0);
1129 tcg_gen_mov_i32(reg, tmp);
1131 gen_store(s, OS_LONG, addr, reg);
1134 tcg_gen_addi_i32(addr, addr, 4);
1139 DISAS_INSN(bitop_im)
1149 if ((insn & 0x38) != 0)
1153 op = (insn >> 6) & 3;
1155 bitnum = cpu_lduw_code(env, s->pc);
1157 if (bitnum & 0xff00) {
1158 disas_undef(env, s, insn);
1162 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1165 if (opsize == OS_BYTE)
1171 tmp = tcg_temp_new();
1172 assert (CCF_Z == (1 << 2));
1174 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1175 else if (bitnum < 2)
1176 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1178 tcg_gen_mov_i32(tmp, src1);
1179 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1180 /* Clear CCF_Z if bit set. */
1181 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1182 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1186 tcg_gen_xori_i32(tmp, src1, mask);
1189 tcg_gen_andi_i32(tmp, src1, ~mask);
1192 tcg_gen_ori_i32(tmp, src1, mask);
1197 DEST_EA(env, insn, opsize, tmp, &addr);
1201 DISAS_INSN(arith_im)
1209 op = (insn >> 9) & 7;
1210 SRC_EA(env, src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1211 im = read_im32(env, s);
1212 dest = tcg_temp_new();
1215 tcg_gen_ori_i32(dest, src1, im);
1216 gen_logic_cc(s, dest);
1219 tcg_gen_andi_i32(dest, src1, im);
1220 gen_logic_cc(s, dest);
1223 tcg_gen_mov_i32(dest, src1);
1224 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1225 tcg_gen_subi_i32(dest, dest, im);
1226 gen_update_cc_add(dest, tcg_const_i32(im));
1227 s->cc_op = CC_OP_SUB;
1230 tcg_gen_mov_i32(dest, src1);
1231 tcg_gen_addi_i32(dest, dest, im);
1232 gen_update_cc_add(dest, tcg_const_i32(im));
1233 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1234 s->cc_op = CC_OP_ADD;
1237 tcg_gen_xori_i32(dest, src1, im);
1238 gen_logic_cc(s, dest);
1241 tcg_gen_mov_i32(dest, src1);
1242 tcg_gen_subi_i32(dest, dest, im);
1243 gen_update_cc_add(dest, tcg_const_i32(im));
1244 s->cc_op = CC_OP_SUB;
1250 DEST_EA(env, insn, OS_LONG, dest, &addr);
1258 reg = DREG(insn, 0);
1259 tcg_gen_bswap32_i32(reg, reg);
1269 switch (insn >> 12) {
1270 case 1: /* move.b */
1273 case 2: /* move.l */
1276 case 3: /* move.w */
1282 SRC_EA(env, src, opsize, 1, NULL);
1283 op = (insn >> 6) & 7;
1286 /* The value will already have been sign extended. */
1287 dest = AREG(insn, 9);
1288 tcg_gen_mov_i32(dest, src);
1292 dest_ea = ((insn >> 9) & 7) | (op << 3);
1293 DEST_EA(env, dest_ea, opsize, src, NULL);
1294 /* This will be correct because loads sign extend. */
1295 gen_logic_cc(s, src);
1304 reg = DREG(insn, 0);
1305 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1313 reg = AREG(insn, 9);
1314 tmp = gen_lea(env, s, insn, OS_LONG);
1315 if (IS_NULL_QREG(tmp)) {
1319 tcg_gen_mov_i32(reg, tmp);
1326 switch ((insn >> 6) & 3) {
1339 DEST_EA(env, insn, opsize, tcg_const_i32(0), NULL);
1340 gen_logic_cc(s, tcg_const_i32(0));
1343 static TCGv gen_get_ccr(DisasContext *s)
1348 dest = tcg_temp_new();
1349 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1350 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
1354 DISAS_INSN(move_from_ccr)
1359 ccr = gen_get_ccr(s);
1360 reg = DREG(insn, 0);
1361 gen_partset_reg(OS_WORD, reg, ccr);
1369 reg = DREG(insn, 0);
1370 src1 = tcg_temp_new();
1371 tcg_gen_mov_i32(src1, reg);
1372 tcg_gen_neg_i32(reg, src1);
1373 s->cc_op = CC_OP_SUB;
1374 gen_update_cc_add(reg, src1);
1375 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
1376 s->cc_op = CC_OP_SUB;
1379 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1381 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1382 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
1384 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1388 static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
1394 s->cc_op = CC_OP_FLAGS;
1395 if ((insn & 0x38) == 0)
1397 tmp = tcg_temp_new();
1398 reg = DREG(insn, 0);
1399 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1400 tcg_gen_shri_i32(tmp, reg, 4);
1401 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1403 gen_helper_set_sr(cpu_env, reg);
1406 else if ((insn & 0x3f) == 0x3c)
1409 val = cpu_lduw_code(env, s->pc);
1411 gen_set_sr_im(s, val, ccr_only);
1414 disas_undef(env, s, insn);
1417 DISAS_INSN(move_to_ccr)
1419 gen_set_sr(env, s, insn, 1);
1426 reg = DREG(insn, 0);
1427 tcg_gen_not_i32(reg, reg);
1428 gen_logic_cc(s, reg);
1437 src1 = tcg_temp_new();
1438 src2 = tcg_temp_new();
1439 reg = DREG(insn, 0);
1440 tcg_gen_shli_i32(src1, reg, 16);
1441 tcg_gen_shri_i32(src2, reg, 16);
1442 tcg_gen_or_i32(reg, src1, src2);
1443 gen_logic_cc(s, reg);
1450 tmp = gen_lea(env, s, insn, OS_LONG);
1451 if (IS_NULL_QREG(tmp)) {
1464 reg = DREG(insn, 0);
1465 op = (insn >> 6) & 7;
1466 tmp = tcg_temp_new();
1468 tcg_gen_ext16s_i32(tmp, reg);
1470 tcg_gen_ext8s_i32(tmp, reg);
1472 gen_partset_reg(OS_WORD, reg, tmp);
1474 tcg_gen_mov_i32(reg, tmp);
1475 gen_logic_cc(s, tmp);
1483 switch ((insn >> 6) & 3) {
1496 SRC_EA(env, tmp, opsize, 1, NULL);
1497 gen_logic_cc(s, tmp);
1502 /* Implemented as a NOP. */
1507 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1510 /* ??? This should be atomic. */
1517 dest = tcg_temp_new();
1518 SRC_EA(env, src1, OS_BYTE, 1, &addr);
1519 gen_logic_cc(s, src1);
1520 tcg_gen_ori_i32(dest, src1, 0x80);
1521 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1531 /* The upper 32 bits of the product are discarded, so
1532 muls.l and mulu.l are functionally equivalent. */
1533 ext = cpu_lduw_code(env, s->pc);
1536 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1539 reg = DREG(ext, 12);
1540 SRC_EA(env, src1, OS_LONG, 0, NULL);
1541 dest = tcg_temp_new();
1542 tcg_gen_mul_i32(dest, src1, reg);
1543 tcg_gen_mov_i32(reg, dest);
1544 /* Unlike m68k, coldfire always clears the overflow bit. */
1545 gen_logic_cc(s, dest);
1554 offset = cpu_ldsw_code(env, s->pc);
1556 reg = AREG(insn, 0);
1557 tmp = tcg_temp_new();
1558 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1559 gen_store(s, OS_LONG, tmp, reg);
1560 if ((insn & 7) != 7)
1561 tcg_gen_mov_i32(reg, tmp);
1562 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1571 src = tcg_temp_new();
1572 reg = AREG(insn, 0);
1573 tcg_gen_mov_i32(src, reg);
1574 tmp = gen_load(s, OS_LONG, src, 0);
1575 tcg_gen_mov_i32(reg, tmp);
1576 tcg_gen_addi_i32(QREG_SP, src, 4);
1587 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1588 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1596 /* Load the target address first to ensure correct exception
1598 tmp = gen_lea(env, s, insn, OS_LONG);
1599 if (IS_NULL_QREG(tmp)) {
1603 if ((insn & 0x40) == 0) {
1605 gen_push(s, tcg_const_i32(s->pc));
1618 SRC_EA(env, src1, OS_LONG, 0, &addr);
1619 val = (insn >> 9) & 7;
1622 dest = tcg_temp_new();
1623 tcg_gen_mov_i32(dest, src1);
1624 if ((insn & 0x38) == 0x08) {
1625 /* Don't update condition codes if the destination is an
1626 address register. */
1627 if (insn & 0x0100) {
1628 tcg_gen_subi_i32(dest, dest, val);
1630 tcg_gen_addi_i32(dest, dest, val);
1633 src2 = tcg_const_i32(val);
1634 if (insn & 0x0100) {
1635 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1636 tcg_gen_subi_i32(dest, dest, val);
1637 s->cc_op = CC_OP_SUB;
1639 tcg_gen_addi_i32(dest, dest, val);
1640 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1641 s->cc_op = CC_OP_ADD;
1643 gen_update_cc_add(dest, src2);
1645 DEST_EA(env, insn, OS_LONG, dest, &addr);
1651 case 2: /* One extension word. */
1654 case 3: /* Two extension words. */
1657 case 4: /* No extension words. */
1660 disas_undef(env, s, insn);
1672 op = (insn >> 8) & 0xf;
1673 offset = (int8_t)insn;
1675 offset = cpu_ldsw_code(env, s->pc);
1677 } else if (offset == -1) {
1678 offset = read_im32(env, s);
1682 gen_push(s, tcg_const_i32(s->pc));
1687 l1 = gen_new_label();
1688 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1689 gen_jmp_tb(s, 1, base + offset);
1691 gen_jmp_tb(s, 0, s->pc);
1693 /* Unconditional branch. */
1694 gen_jmp_tb(s, 0, base + offset);
1703 tcg_gen_movi_i32(DREG(insn, 9), val);
1704 gen_logic_cc(s, tcg_const_i32(val));
1717 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
1718 reg = DREG(insn, 9);
1719 tcg_gen_mov_i32(reg, src);
1720 gen_logic_cc(s, src);
1730 reg = DREG(insn, 9);
1731 dest = tcg_temp_new();
1733 SRC_EA(env, src, OS_LONG, 0, &addr);
1734 tcg_gen_or_i32(dest, src, reg);
1735 DEST_EA(env, insn, OS_LONG, dest, &addr);
1737 SRC_EA(env, src, OS_LONG, 0, NULL);
1738 tcg_gen_or_i32(dest, src, reg);
1739 tcg_gen_mov_i32(reg, dest);
1741 gen_logic_cc(s, dest);
1749 SRC_EA(env, src, OS_LONG, 0, NULL);
1750 reg = AREG(insn, 9);
1751 tcg_gen_sub_i32(reg, reg, src);
1760 reg = DREG(insn, 9);
1761 src = DREG(insn, 0);
1762 gen_helper_subx_cc(reg, cpu_env, reg, src);
1770 val = (insn >> 9) & 7;
1773 src = tcg_const_i32(val);
1774 gen_logic_cc(s, src);
1775 DEST_EA(env, insn, OS_LONG, src, NULL);
1786 op = (insn >> 6) & 3;
1790 s->cc_op = CC_OP_CMPB;
1794 s->cc_op = CC_OP_CMPW;
1798 s->cc_op = CC_OP_SUB;
1803 SRC_EA(env, src, opsize, 1, NULL);
1804 reg = DREG(insn, 9);
1805 dest = tcg_temp_new();
1806 tcg_gen_sub_i32(dest, reg, src);
1807 gen_update_cc_add(dest, src);
1822 SRC_EA(env, src, opsize, 1, NULL);
1823 reg = AREG(insn, 9);
1824 dest = tcg_temp_new();
1825 tcg_gen_sub_i32(dest, reg, src);
1826 gen_update_cc_add(dest, src);
1827 s->cc_op = CC_OP_SUB;
1837 SRC_EA(env, src, OS_LONG, 0, &addr);
1838 reg = DREG(insn, 9);
1839 dest = tcg_temp_new();
1840 tcg_gen_xor_i32(dest, src, reg);
1841 gen_logic_cc(s, dest);
1842 DEST_EA(env, insn, OS_LONG, dest, &addr);
1852 reg = DREG(insn, 9);
1853 dest = tcg_temp_new();
1855 SRC_EA(env, src, OS_LONG, 0, &addr);
1856 tcg_gen_and_i32(dest, src, reg);
1857 DEST_EA(env, insn, OS_LONG, dest, &addr);
1859 SRC_EA(env, src, OS_LONG, 0, NULL);
1860 tcg_gen_and_i32(dest, src, reg);
1861 tcg_gen_mov_i32(reg, dest);
1863 gen_logic_cc(s, dest);
1871 SRC_EA(env, src, OS_LONG, 0, NULL);
1872 reg = AREG(insn, 9);
1873 tcg_gen_add_i32(reg, reg, src);
1882 reg = DREG(insn, 9);
1883 src = DREG(insn, 0);
1884 gen_helper_addx_cc(reg, cpu_env, reg, src);
1885 s->cc_op = CC_OP_FLAGS;
1888 /* TODO: This could be implemented without helper functions. */
1889 DISAS_INSN(shift_im)
1895 reg = DREG(insn, 0);
1896 tmp = (insn >> 9) & 7;
1899 shift = tcg_const_i32(tmp);
1900 /* No need to flush flags becuse we know we will set C flag. */
1902 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1905 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1907 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1910 s->cc_op = CC_OP_SHIFT;
1913 DISAS_INSN(shift_reg)
1918 reg = DREG(insn, 0);
1919 shift = DREG(insn, 9);
1920 /* Shift by zero leaves C flag unmodified. */
1923 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1926 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1928 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1931 s->cc_op = CC_OP_SHIFT;
1937 reg = DREG(insn, 0);
1938 gen_logic_cc(s, reg);
1939 gen_helper_ff1(reg, reg);
1942 static TCGv gen_get_sr(DisasContext *s)
1947 ccr = gen_get_ccr(s);
1948 sr = tcg_temp_new();
1949 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1950 tcg_gen_or_i32(sr, sr, ccr);
1960 ext = cpu_lduw_code(env, s->pc);
1962 if (ext != 0x46FC) {
1963 gen_exception(s, addr, EXCP_UNSUPPORTED);
1966 ext = cpu_lduw_code(env, s->pc);
1968 if (IS_USER(s) || (ext & SR_S) == 0) {
1969 gen_exception(s, addr, EXCP_PRIVILEGE);
1972 gen_push(s, gen_get_sr(s));
1973 gen_set_sr_im(s, ext, 0);
1976 DISAS_INSN(move_from_sr)
1982 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1986 reg = DREG(insn, 0);
1987 gen_partset_reg(OS_WORD, reg, sr);
1990 DISAS_INSN(move_to_sr)
1993 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1996 gen_set_sr(env, s, insn, 0);
2000 DISAS_INSN(move_from_usp)
2003 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2006 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
2007 offsetof(CPUM68KState, sp[M68K_USP]));
2010 DISAS_INSN(move_to_usp)
2013 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2016 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
2017 offsetof(CPUM68KState, sp[M68K_USP]));
2022 gen_exception(s, s->pc, EXCP_HALT_INSN);
2030 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2034 ext = cpu_lduw_code(env, s->pc);
2037 gen_set_sr_im(s, ext, 0);
2038 tcg_gen_movi_i32(cpu_halted, 1);
2039 gen_exception(s, s->pc, EXCP_HLT);
2045 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2048 gen_exception(s, s->pc - 2, EXCP_RTE);
2057 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2061 ext = cpu_lduw_code(env, s->pc);
2065 reg = AREG(ext, 12);
2067 reg = DREG(ext, 12);
2069 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2076 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2079 /* ICache fetch. Implement as no-op. */
2085 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2088 /* Cache push/invalidate. Implement as no-op. */
2093 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2098 M68kCPU *cpu = m68k_env_get_cpu(env);
2101 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2104 /* TODO: Implement wdebug. */
2105 cpu_abort(CPU(cpu), "WDEBUG not implemented");
2110 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2113 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2114 immediately before the next FP instruction is executed. */
2128 ext = cpu_lduw_code(env, s->pc);
2130 opmode = ext & 0x7f;
2131 switch ((ext >> 13) & 7) {
2136 case 3: /* fmove out */
2138 tmp32 = tcg_temp_new_i32();
2140 /* ??? TODO: Proper behavior on overflow. */
2141 switch ((ext >> 10) & 7) {
2144 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2148 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2152 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2154 case 5: /* OS_DOUBLE */
2155 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2156 switch ((insn >> 3) & 7) {
2161 tcg_gen_addi_i32(tmp32, tmp32, -8);
2164 offset = cpu_ldsw_code(env, s->pc);
2166 tcg_gen_addi_i32(tmp32, tmp32, offset);
2171 gen_store64(s, tmp32, src);
2172 switch ((insn >> 3) & 7) {
2174 tcg_gen_addi_i32(tmp32, tmp32, 8);
2175 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2178 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2181 tcg_temp_free_i32(tmp32);
2185 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2190 DEST_EA(env, insn, opsize, tmp32, NULL);
2191 tcg_temp_free_i32(tmp32);
2193 case 4: /* fmove to control register. */
2194 switch ((ext >> 10) & 7) {
2196 /* Not implemented. Ignore writes. */
2201 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2205 case 5: /* fmove from control register. */
2206 switch ((ext >> 10) & 7) {
2208 /* Not implemented. Always return zero. */
2209 tmp32 = tcg_const_i32(0);
2214 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2218 DEST_EA(env, insn, OS_LONG, tmp32, NULL);
2220 case 6: /* fmovem */
2226 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2228 tmp32 = gen_lea(env, s, insn, OS_LONG);
2229 if (IS_NULL_QREG(tmp32)) {
2233 addr = tcg_temp_new_i32();
2234 tcg_gen_mov_i32(addr, tmp32);
2236 for (i = 0; i < 8; i++) {
2239 if (ext & (1 << 13)) {
2241 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2244 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2246 if (ext & (mask - 1))
2247 tcg_gen_addi_i32(addr, addr, 8);
2251 tcg_temp_free_i32(addr);
2255 if (ext & (1 << 14)) {
2256 /* Source effective address. */
2257 switch ((ext >> 10) & 7) {
2258 case 0: opsize = OS_LONG; break;
2259 case 1: opsize = OS_SINGLE; break;
2260 case 4: opsize = OS_WORD; break;
2261 case 5: opsize = OS_DOUBLE; break;
2262 case 6: opsize = OS_BYTE; break;
2266 if (opsize == OS_DOUBLE) {
2267 tmp32 = tcg_temp_new_i32();
2268 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2269 switch ((insn >> 3) & 7) {
2274 tcg_gen_addi_i32(tmp32, tmp32, -8);
2277 offset = cpu_ldsw_code(env, s->pc);
2279 tcg_gen_addi_i32(tmp32, tmp32, offset);
2282 offset = cpu_ldsw_code(env, s->pc);
2283 offset += s->pc - 2;
2285 tcg_gen_addi_i32(tmp32, tmp32, offset);
2290 src = gen_load64(s, tmp32);
2291 switch ((insn >> 3) & 7) {
2293 tcg_gen_addi_i32(tmp32, tmp32, 8);
2294 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2297 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2300 tcg_temp_free_i32(tmp32);
2302 SRC_EA(env, tmp32, opsize, 1, NULL);
2303 src = tcg_temp_new_i64();
2308 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2311 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2316 /* Source register. */
2317 src = FREG(ext, 10);
2319 dest = FREG(ext, 7);
2320 res = tcg_temp_new_i64();
2322 tcg_gen_mov_f64(res, dest);
2326 case 0: case 0x40: case 0x44: /* fmove */
2327 tcg_gen_mov_f64(res, src);
2330 gen_helper_iround_f64(res, cpu_env, src);
2333 case 3: /* fintrz */
2334 gen_helper_itrunc_f64(res, cpu_env, src);
2337 case 4: case 0x41: case 0x45: /* fsqrt */
2338 gen_helper_sqrt_f64(res, cpu_env, src);
2340 case 0x18: case 0x58: case 0x5c: /* fabs */
2341 gen_helper_abs_f64(res, src);
2343 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2344 gen_helper_chs_f64(res, src);
2346 case 0x20: case 0x60: case 0x64: /* fdiv */
2347 gen_helper_div_f64(res, cpu_env, res, src);
2349 case 0x22: case 0x62: case 0x66: /* fadd */
2350 gen_helper_add_f64(res, cpu_env, res, src);
2352 case 0x23: case 0x63: case 0x67: /* fmul */
2353 gen_helper_mul_f64(res, cpu_env, res, src);
2355 case 0x28: case 0x68: case 0x6c: /* fsub */
2356 gen_helper_sub_f64(res, cpu_env, res, src);
2358 case 0x38: /* fcmp */
2359 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2363 case 0x3a: /* ftst */
2364 tcg_gen_mov_f64(res, src);
2371 if (ext & (1 << 14)) {
2372 tcg_temp_free_i64(src);
2375 if (opmode & 0x40) {
2376 if ((opmode & 0x4) != 0)
2378 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2383 TCGv tmp = tcg_temp_new_i32();
2384 gen_helper_f64_to_f32(tmp, cpu_env, res);
2385 gen_helper_f32_to_f64(res, cpu_env, tmp);
2386 tcg_temp_free_i32(tmp);
2388 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2390 tcg_gen_mov_f64(dest, res);
2392 tcg_temp_free_i64(res);
2395 /* FIXME: Is this right for offset addressing modes? */
2397 disas_undef_fpu(env, s, insn);
2408 offset = cpu_ldsw_code(env, s->pc);
2410 if (insn & (1 << 6)) {
2411 offset = (offset << 16) | cpu_lduw_code(env, s->pc);
2415 l1 = gen_new_label();
2416 /* TODO: Raise BSUN exception. */
2417 flag = tcg_temp_new();
2418 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2419 /* Jump to l1 if condition is true. */
2420 switch (insn & 0xf) {
2423 case 1: /* eq (=0) */
2424 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2426 case 2: /* ogt (=1) */
2427 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2429 case 3: /* oge (=0 or =1) */
2430 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2432 case 4: /* olt (=-1) */
2433 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2435 case 5: /* ole (=-1 or =0) */
2436 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2438 case 6: /* ogl (=-1 or =1) */
2439 tcg_gen_andi_i32(flag, flag, 1);
2440 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2442 case 7: /* or (=2) */
2443 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2445 case 8: /* un (<2) */
2446 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2448 case 9: /* ueq (=0 or =2) */
2449 tcg_gen_andi_i32(flag, flag, 1);
2450 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2452 case 10: /* ugt (>0) */
2453 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2455 case 11: /* uge (>=0) */
2456 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2458 case 12: /* ult (=-1 or =2) */
2459 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2461 case 13: /* ule (!=1) */
2462 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2464 case 14: /* ne (!=0) */
2465 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2471 gen_jmp_tb(s, 0, s->pc);
2473 gen_jmp_tb(s, 1, addr + offset);
2476 DISAS_INSN(frestore)
2478 M68kCPU *cpu = m68k_env_get_cpu(env);
2480 /* TODO: Implement frestore. */
2481 cpu_abort(CPU(cpu), "FRESTORE not implemented");
2486 M68kCPU *cpu = m68k_env_get_cpu(env);
2488 /* TODO: Implement fsave. */
2489 cpu_abort(CPU(cpu), "FSAVE not implemented");
2492 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2494 TCGv tmp = tcg_temp_new();
2495 if (s->env->macsr & MACSR_FI) {
2497 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2499 tcg_gen_shli_i32(tmp, val, 16);
2500 } else if (s->env->macsr & MACSR_SU) {
2502 tcg_gen_sari_i32(tmp, val, 16);
2504 tcg_gen_ext16s_i32(tmp, val);
2507 tcg_gen_shri_i32(tmp, val, 16);
2509 tcg_gen_ext16u_i32(tmp, val);
2514 static void gen_mac_clear_flags(void)
2516 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2517 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2533 s->mactmp = tcg_temp_new_i64();
2537 ext = cpu_lduw_code(env, s->pc);
2540 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2541 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2542 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2543 disas_undef(env, s, insn);
2547 /* MAC with load. */
2548 tmp = gen_lea(env, s, insn, OS_LONG);
2549 addr = tcg_temp_new();
2550 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2551 /* Load the value now to ensure correct exception behavior.
2552 Perform writeback after reading the MAC inputs. */
2553 loadval = gen_load(s, OS_LONG, addr, 0);
2556 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2557 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2559 loadval = addr = NULL_QREG;
2560 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2561 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2564 gen_mac_clear_flags();
2567 /* Disabled because conditional branches clobber temporary vars. */
2568 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2569 /* Skip the multiply if we know we will ignore it. */
2570 l1 = gen_new_label();
2571 tmp = tcg_temp_new();
2572 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2573 gen_op_jmp_nz32(tmp, l1);
2577 if ((ext & 0x0800) == 0) {
2579 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2580 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2582 if (s->env->macsr & MACSR_FI) {
2583 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2585 if (s->env->macsr & MACSR_SU)
2586 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2588 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2589 switch ((ext >> 9) & 3) {
2591 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2594 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2600 /* Save the overflow flag from the multiply. */
2601 saved_flags = tcg_temp_new();
2602 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2604 saved_flags = NULL_QREG;
2608 /* Disabled because conditional branches clobber temporary vars. */
2609 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2610 /* Skip the accumulate if the value is already saturated. */
2611 l1 = gen_new_label();
2612 tmp = tcg_temp_new();
2613 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2614 gen_op_jmp_nz32(tmp, l1);
2619 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2621 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2623 if (s->env->macsr & MACSR_FI)
2624 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2625 else if (s->env->macsr & MACSR_SU)
2626 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2628 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2631 /* Disabled because conditional branches clobber temporary vars. */
2637 /* Dual accumulate variant. */
2638 acc = (ext >> 2) & 3;
2639 /* Restore the overflow flag from the multiplier. */
2640 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2642 /* Disabled because conditional branches clobber temporary vars. */
2643 if ((s->env->macsr & MACSR_OMC) != 0) {
2644 /* Skip the accumulate if the value is already saturated. */
2645 l1 = gen_new_label();
2646 tmp = tcg_temp_new();
2647 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2648 gen_op_jmp_nz32(tmp, l1);
2652 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2654 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2655 if (s->env->macsr & MACSR_FI)
2656 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2657 else if (s->env->macsr & MACSR_SU)
2658 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2660 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2662 /* Disabled because conditional branches clobber temporary vars. */
2667 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2671 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2672 tcg_gen_mov_i32(rw, loadval);
2673 /* FIXME: Should address writeback happen with the masked or
2675 switch ((insn >> 3) & 7) {
2676 case 3: /* Post-increment. */
2677 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2679 case 4: /* Pre-decrement. */
2680 tcg_gen_mov_i32(AREG(insn, 0), addr);
2685 DISAS_INSN(from_mac)
2691 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2692 accnum = (insn >> 9) & 3;
2693 acc = MACREG(accnum);
2694 if (s->env->macsr & MACSR_FI) {
2695 gen_helper_get_macf(rx, cpu_env, acc);
2696 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2697 tcg_gen_extrl_i64_i32(rx, acc);
2698 } else if (s->env->macsr & MACSR_SU) {
2699 gen_helper_get_macs(rx, acc);
2701 gen_helper_get_macu(rx, acc);
2704 tcg_gen_movi_i64(acc, 0);
2705 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2709 DISAS_INSN(move_mac)
2711 /* FIXME: This can be done without a helper. */
2715 dest = tcg_const_i32((insn >> 9) & 3);
2716 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2717 gen_mac_clear_flags();
2718 gen_helper_mac_set_flags(cpu_env, dest);
2721 DISAS_INSN(from_macsr)
2725 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2726 tcg_gen_mov_i32(reg, QREG_MACSR);
2729 DISAS_INSN(from_mask)
2732 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2733 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2736 DISAS_INSN(from_mext)
2740 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2741 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2742 if (s->env->macsr & MACSR_FI)
2743 gen_helper_get_mac_extf(reg, cpu_env, acc);
2745 gen_helper_get_mac_exti(reg, cpu_env, acc);
2748 DISAS_INSN(macsr_to_ccr)
2750 tcg_gen_movi_i32(QREG_CC_X, 0);
2751 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2752 s->cc_op = CC_OP_FLAGS;
2760 accnum = (insn >> 9) & 3;
2761 acc = MACREG(accnum);
2762 SRC_EA(env, val, OS_LONG, 0, NULL);
2763 if (s->env->macsr & MACSR_FI) {
2764 tcg_gen_ext_i32_i64(acc, val);
2765 tcg_gen_shli_i64(acc, acc, 8);
2766 } else if (s->env->macsr & MACSR_SU) {
2767 tcg_gen_ext_i32_i64(acc, val);
2769 tcg_gen_extu_i32_i64(acc, val);
2771 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2772 gen_mac_clear_flags();
2773 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2776 DISAS_INSN(to_macsr)
2779 SRC_EA(env, val, OS_LONG, 0, NULL);
2780 gen_helper_set_macsr(cpu_env, val);
2787 SRC_EA(env, val, OS_LONG, 0, NULL);
2788 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2795 SRC_EA(env, val, OS_LONG, 0, NULL);
2796 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2797 if (s->env->macsr & MACSR_FI)
2798 gen_helper_set_mac_extf(cpu_env, val, acc);
2799 else if (s->env->macsr & MACSR_SU)
2800 gen_helper_set_mac_exts(cpu_env, val, acc);
2802 gen_helper_set_mac_extu(cpu_env, val, acc);
2805 static disas_proc opcode_table[65536];
2808 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2814 /* Sanity check. All set bits must be included in the mask. */
2815 if (opcode & ~mask) {
2817 "qemu internal error: bogus opcode definition %04x/%04x\n",
2821 /* This could probably be cleverer. For now just optimize the case where
2822 the top bits are known. */
2823 /* Find the first zero bit in the mask. */
2825 while ((i & mask) != 0)
2827 /* Iterate over all combinations of this and lower bits. */
2832 from = opcode & ~(i - 1);
2834 for (i = from; i < to; i++) {
2835 if ((i & mask) == opcode)
2836 opcode_table[i] = proc;
2840 /* Register m68k opcode handlers. Order is important.
2841 Later insn override earlier ones. */
2842 void register_m68k_insns (CPUM68KState *env)
2844 #define INSN(name, opcode, mask, feature) do { \
2845 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2846 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2848 INSN(undef, 0000, 0000, CF_ISA_A);
2849 INSN(arith_im, 0080, fff8, CF_ISA_A);
2850 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2851 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2852 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2853 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2854 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2855 INSN(arith_im, 0280, fff8, CF_ISA_A);
2856 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2857 INSN(arith_im, 0480, fff8, CF_ISA_A);
2858 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2859 INSN(arith_im, 0680, fff8, CF_ISA_A);
2860 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2861 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2862 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2863 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2864 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2865 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2866 INSN(move, 1000, f000, CF_ISA_A);
2867 INSN(move, 2000, f000, CF_ISA_A);
2868 INSN(move, 3000, f000, CF_ISA_A);
2869 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2870 INSN(negx, 4080, fff8, CF_ISA_A);
2871 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2872 INSN(lea, 41c0, f1c0, CF_ISA_A);
2873 INSN(clr, 4200, ff00, CF_ISA_A);
2874 INSN(undef, 42c0, ffc0, CF_ISA_A);
2875 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2876 INSN(neg, 4480, fff8, CF_ISA_A);
2877 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2878 INSN(not, 4680, fff8, CF_ISA_A);
2879 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2880 INSN(pea, 4840, ffc0, CF_ISA_A);
2881 INSN(swap, 4840, fff8, CF_ISA_A);
2882 INSN(movem, 48c0, fbc0, CF_ISA_A);
2883 INSN(ext, 4880, fff8, CF_ISA_A);
2884 INSN(ext, 48c0, fff8, CF_ISA_A);
2885 INSN(ext, 49c0, fff8, CF_ISA_A);
2886 INSN(tst, 4a00, ff00, CF_ISA_A);
2887 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2888 INSN(halt, 4ac8, ffff, CF_ISA_A);
2889 INSN(pulse, 4acc, ffff, CF_ISA_A);
2890 INSN(illegal, 4afc, ffff, CF_ISA_A);
2891 INSN(mull, 4c00, ffc0, CF_ISA_A);
2892 INSN(divl, 4c40, ffc0, CF_ISA_A);
2893 INSN(sats, 4c80, fff8, CF_ISA_B);
2894 INSN(trap, 4e40, fff0, CF_ISA_A);
2895 INSN(link, 4e50, fff8, CF_ISA_A);
2896 INSN(unlk, 4e58, fff8, CF_ISA_A);
2897 INSN(move_to_usp, 4e60, fff8, USP);
2898 INSN(move_from_usp, 4e68, fff8, USP);
2899 INSN(nop, 4e71, ffff, CF_ISA_A);
2900 INSN(stop, 4e72, ffff, CF_ISA_A);
2901 INSN(rte, 4e73, ffff, CF_ISA_A);
2902 INSN(rts, 4e75, ffff, CF_ISA_A);
2903 INSN(movec, 4e7b, ffff, CF_ISA_A);
2904 INSN(jump, 4e80, ffc0, CF_ISA_A);
2905 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2906 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2907 INSN(scc, 50c0, f0f8, CF_ISA_A);
2908 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2909 INSN(tpf, 51f8, fff8, CF_ISA_A);
2911 /* Branch instructions. */
2912 INSN(branch, 6000, f000, CF_ISA_A);
2913 /* Disable long branch instructions, then add back the ones we want. */
2914 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2915 INSN(branch, 60ff, f0ff, CF_ISA_B);
2916 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2917 INSN(branch, 60ff, ffff, BRAL);
2919 INSN(moveq, 7000, f100, CF_ISA_A);
2920 INSN(mvzs, 7100, f100, CF_ISA_B);
2921 INSN(or, 8000, f000, CF_ISA_A);
2922 INSN(divw, 80c0, f0c0, CF_ISA_A);
2923 INSN(addsub, 9000, f000, CF_ISA_A);
2924 INSN(subx, 9180, f1f8, CF_ISA_A);
2925 INSN(suba, 91c0, f1c0, CF_ISA_A);
2927 INSN(undef_mac, a000, f000, CF_ISA_A);
2928 INSN(mac, a000, f100, CF_EMAC);
2929 INSN(from_mac, a180, f9b0, CF_EMAC);
2930 INSN(move_mac, a110, f9fc, CF_EMAC);
2931 INSN(from_macsr,a980, f9f0, CF_EMAC);
2932 INSN(from_mask, ad80, fff0, CF_EMAC);
2933 INSN(from_mext, ab80, fbf0, CF_EMAC);
2934 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2935 INSN(to_mac, a100, f9c0, CF_EMAC);
2936 INSN(to_macsr, a900, ffc0, CF_EMAC);
2937 INSN(to_mext, ab00, fbc0, CF_EMAC);
2938 INSN(to_mask, ad00, ffc0, CF_EMAC);
2940 INSN(mov3q, a140, f1c0, CF_ISA_B);
2941 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2942 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2943 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2944 INSN(cmp, b080, f1c0, CF_ISA_A);
2945 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2946 INSN(eor, b180, f1c0, CF_ISA_A);
2947 INSN(and, c000, f000, CF_ISA_A);
2948 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2949 INSN(addsub, d000, f000, CF_ISA_A);
2950 INSN(addx, d180, f1f8, CF_ISA_A);
2951 INSN(adda, d1c0, f1c0, CF_ISA_A);
2952 INSN(shift_im, e080, f0f0, CF_ISA_A);
2953 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2954 INSN(undef_fpu, f000, f000, CF_ISA_A);
2955 INSN(fpu, f200, ffc0, CF_FPU);
2956 INSN(fbcc, f280, ffc0, CF_FPU);
2957 INSN(frestore, f340, ffc0, CF_FPU);
2958 INSN(fsave, f340, ffc0, CF_FPU);
2959 INSN(intouch, f340, ffc0, CF_ISA_A);
2960 INSN(cpushl, f428, ff38, CF_ISA_A);
2961 INSN(wddata, fb00, ff00, CF_ISA_A);
2962 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2966 /* ??? Some of this implementation is not exception safe. We should always
2967 write back the result to memory before setting the condition codes. */
2968 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
2972 insn = cpu_lduw_code(env, s->pc);
2975 opcode_table[insn](env, s, insn);
2978 /* generate intermediate code for basic block 'tb'. */
2979 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
2981 M68kCPU *cpu = m68k_env_get_cpu(env);
2982 CPUState *cs = CPU(cpu);
2983 DisasContext dc1, *dc = &dc1;
2984 target_ulong pc_start;
2989 /* generate intermediate code */
2995 dc->is_jmp = DISAS_NEXT;
2997 dc->cc_op = CC_OP_DYNAMIC;
2998 dc->singlestep_enabled = cs->singlestep_enabled;
2999 dc->fpcr = env->fpcr;
3000 dc->user = (env->sr & SR_S) == 0;
3003 max_insns = tb->cflags & CF_COUNT_MASK;
3004 if (max_insns == 0) {
3005 max_insns = CF_COUNT_MASK;
3007 if (max_insns > TCG_MAX_INSNS) {
3008 max_insns = TCG_MAX_INSNS;
3013 pc_offset = dc->pc - pc_start;
3014 gen_throws_exception = NULL;
3015 tcg_gen_insn_start(dc->pc);
3018 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
3019 gen_exception(dc, dc->pc, EXCP_DEBUG);
3020 dc->is_jmp = DISAS_JUMP;
3021 /* The address covered by the breakpoint must be included in
3022 [tb->pc, tb->pc + tb->size) in order to for it to be
3023 properly cleared -- thus we increment the PC here so that
3024 the logic setting tb->size below does the right thing. */
3029 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
3033 dc->insn_pc = dc->pc;
3034 disas_m68k_insn(env, dc);
3035 } while (!dc->is_jmp && !tcg_op_buf_full() &&
3036 !cs->singlestep_enabled &&
3038 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3039 num_insns < max_insns);
3041 if (tb->cflags & CF_LAST_IO)
3043 if (unlikely(cs->singlestep_enabled)) {
3044 /* Make sure the pc is updated, and raise a debug exception. */
3046 gen_flush_cc_op(dc);
3047 tcg_gen_movi_i32(QREG_PC, dc->pc);
3049 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
3051 switch(dc->is_jmp) {
3053 gen_flush_cc_op(dc);
3054 gen_jmp_tb(dc, 0, dc->pc);
3059 gen_flush_cc_op(dc);
3060 /* indicate that the hash table must be used to find the next TB */
3064 /* nothing more to generate */
3068 gen_tb_end(tb, num_insns);
3071 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
3072 && qemu_log_in_addr_range(pc_start)) {
3073 qemu_log("----------------\n");
3074 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3075 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
3079 tb->size = dc->pc - pc_start;
3080 tb->icount = num_insns;
3083 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
3086 M68kCPU *cpu = M68K_CPU(cs);
3087 CPUM68KState *env = &cpu->env;
3091 for (i = 0; i < 8; i++)
3093 u.d = env->fregs[i];
3094 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3095 i, env->dregs[i], i, env->aregs[i],
3096 i, u.l.upper, u.l.lower, *(double *)&u.d);
3098 cpu_fprintf (f, "PC = %08x ", env->pc);
3100 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3101 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3102 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3103 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3106 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,