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");
82 #define DEFO32(name, offset) \
83 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
84 offsetof(CPUM68KState, offset), #name);
85 #define DEFO64(name, offset) \
86 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
87 offsetof(CPUM68KState, offset), #name);
88 #define DEFF64(name, offset) DEFO64(name, offset)
94 cpu_halted = tcg_global_mem_new_i32(cpu_env,
95 -offsetof(M68kCPU, env) +
96 offsetof(CPUState, halted), "HALTED");
97 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
98 -offsetof(M68kCPU, env) +
99 offsetof(CPUState, exception_index),
103 for (i = 0; i < 8; i++) {
104 sprintf(p, "D%d", i);
105 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
106 offsetof(CPUM68KState, dregs[i]), p);
108 sprintf(p, "A%d", i);
109 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
110 offsetof(CPUM68KState, aregs[i]), p);
112 sprintf(p, "F%d", i);
113 cpu_fregs[i] = tcg_global_mem_new_i64(cpu_env,
114 offsetof(CPUM68KState, fregs[i]), p);
117 for (i = 0; i < 4; i++) {
118 sprintf(p, "ACC%d", i);
119 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
120 offsetof(CPUM68KState, macc[i]), p);
124 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
125 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
128 /* internal defines */
129 typedef struct DisasContext {
131 target_ulong insn_pc; /* Start of the current instruction. */
137 struct TranslationBlock *tb;
138 int singlestep_enabled;
143 #define DISAS_JUMP_NEXT 4
145 #if defined(CONFIG_USER_ONLY)
148 #define IS_USER(s) s->user
151 /* XXX: move that elsewhere */
152 /* ??? Fix exceptions. */
153 static void *gen_throws_exception;
154 #define gen_last_qop NULL
162 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
164 #ifdef DEBUG_DISPATCH
165 #define DISAS_INSN(name) \
166 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
168 static void disas_##name(CPUM68KState *env, DisasContext *s, \
171 qemu_log("Dispatch " #name "\n"); \
172 real_disas_##name(s, env, insn); \
174 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
177 #define DISAS_INSN(name) \
178 static void disas_##name(CPUM68KState *env, DisasContext *s, \
182 /* Generate a load from the specified address. Narrow values are
183 sign extended to full register width. */
184 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
187 int index = IS_USER(s);
188 tmp = tcg_temp_new_i32();
192 tcg_gen_qemu_ld8s(tmp, addr, index);
194 tcg_gen_qemu_ld8u(tmp, addr, index);
198 tcg_gen_qemu_ld16s(tmp, addr, index);
200 tcg_gen_qemu_ld16u(tmp, addr, index);
204 tcg_gen_qemu_ld32u(tmp, addr, index);
207 g_assert_not_reached();
209 gen_throws_exception = gen_last_qop;
213 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
216 int index = IS_USER(s);
217 tmp = tcg_temp_new_i64();
218 tcg_gen_qemu_ldf64(tmp, addr, index);
219 gen_throws_exception = gen_last_qop;
223 /* Generate a store. */
224 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
226 int index = IS_USER(s);
229 tcg_gen_qemu_st8(val, addr, index);
232 tcg_gen_qemu_st16(val, addr, index);
236 tcg_gen_qemu_st32(val, addr, index);
239 g_assert_not_reached();
241 gen_throws_exception = gen_last_qop;
244 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
246 int index = IS_USER(s);
247 tcg_gen_qemu_stf64(val, addr, index);
248 gen_throws_exception = gen_last_qop;
257 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
258 otherwise generate a store. */
259 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
262 if (what == EA_STORE) {
263 gen_store(s, opsize, addr, val);
266 return gen_load(s, opsize, addr, what == EA_LOADS);
270 /* Read a 32-bit immediate constant. */
271 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
274 im = ((uint32_t)cpu_lduw_code(env, s->pc)) << 16;
276 im |= cpu_lduw_code(env, s->pc);
281 /* Calculate and address index. */
282 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
287 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
288 if ((ext & 0x800) == 0) {
289 tcg_gen_ext16s_i32(tmp, add);
292 scale = (ext >> 9) & 3;
294 tcg_gen_shli_i32(tmp, add, scale);
300 /* Handle a base + index + displacement effective addresss.
301 A NULL_QREG base means pc-relative. */
302 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
311 ext = cpu_lduw_code(env, s->pc);
314 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
318 /* full extension word format */
319 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
322 if ((ext & 0x30) > 0x10) {
323 /* base displacement */
324 if ((ext & 0x30) == 0x20) {
325 bd = (int16_t)cpu_lduw_code(env, s->pc);
328 bd = read_im32(env, s);
333 tmp = tcg_temp_new();
334 if ((ext & 0x44) == 0) {
336 add = gen_addr_index(ext, tmp);
340 if ((ext & 0x80) == 0) {
341 /* base not suppressed */
342 if (IS_NULL_QREG(base)) {
343 base = tcg_const_i32(offset + bd);
346 if (!IS_NULL_QREG(add)) {
347 tcg_gen_add_i32(tmp, add, base);
353 if (!IS_NULL_QREG(add)) {
355 tcg_gen_addi_i32(tmp, add, bd);
359 add = tcg_const_i32(bd);
361 if ((ext & 3) != 0) {
362 /* memory indirect */
363 base = gen_load(s, OS_LONG, add, 0);
364 if ((ext & 0x44) == 4) {
365 add = gen_addr_index(ext, tmp);
366 tcg_gen_add_i32(tmp, add, base);
372 /* outer displacement */
373 if ((ext & 3) == 2) {
374 od = (int16_t)cpu_lduw_code(env, s->pc);
377 od = read_im32(env, s);
383 tcg_gen_addi_i32(tmp, add, od);
388 /* brief extension word format */
389 tmp = tcg_temp_new();
390 add = gen_addr_index(ext, tmp);
391 if (!IS_NULL_QREG(base)) {
392 tcg_gen_add_i32(tmp, add, base);
394 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
396 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
403 /* Update the CPU env CC_OP state. */
404 static inline void gen_flush_cc_op(DisasContext *s)
406 if (s->cc_op != CC_OP_DYNAMIC)
407 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
410 /* Evaluate all the CC flags. */
411 static inline void gen_flush_flags(DisasContext *s)
413 if (s->cc_op == CC_OP_FLAGS)
416 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
417 s->cc_op = CC_OP_FLAGS;
420 static void gen_logic_cc(DisasContext *s, TCGv val)
422 tcg_gen_mov_i32(QREG_CC_DEST, val);
423 s->cc_op = CC_OP_LOGIC;
426 static void gen_update_cc_add(TCGv dest, TCGv src)
428 tcg_gen_mov_i32(QREG_CC_DEST, dest);
429 tcg_gen_mov_i32(QREG_CC_SRC, src);
432 static inline int opsize_bytes(int opsize)
435 case OS_BYTE: return 1;
436 case OS_WORD: return 2;
437 case OS_LONG: return 4;
438 case OS_SINGLE: return 4;
439 case OS_DOUBLE: return 8;
441 g_assert_not_reached();
445 /* Assign value to a register. If the width is less than the register width
446 only the low part of the register is set. */
447 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
452 tcg_gen_andi_i32(reg, reg, 0xffffff00);
453 tmp = tcg_temp_new();
454 tcg_gen_ext8u_i32(tmp, val);
455 tcg_gen_or_i32(reg, reg, tmp);
458 tcg_gen_andi_i32(reg, reg, 0xffff0000);
459 tmp = tcg_temp_new();
460 tcg_gen_ext16u_i32(tmp, val);
461 tcg_gen_or_i32(reg, reg, tmp);
465 tcg_gen_mov_i32(reg, val);
468 g_assert_not_reached();
472 /* Sign or zero extend a value. */
473 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
479 tmp = tcg_temp_new();
481 tcg_gen_ext8s_i32(tmp, val);
483 tcg_gen_ext8u_i32(tmp, val);
486 tmp = tcg_temp_new();
488 tcg_gen_ext16s_i32(tmp, val);
490 tcg_gen_ext16u_i32(tmp, val);
497 g_assert_not_reached();
502 /* Generate code for an "effective address". Does not adjust the base
503 register for autoincrement addressing modes. */
504 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
512 switch ((insn >> 3) & 7) {
513 case 0: /* Data register direct. */
514 case 1: /* Address register direct. */
516 case 2: /* Indirect register */
517 case 3: /* Indirect postincrement. */
518 return AREG(insn, 0);
519 case 4: /* Indirect predecrememnt. */
521 tmp = tcg_temp_new();
522 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
524 case 5: /* Indirect displacement. */
526 tmp = tcg_temp_new();
527 ext = cpu_lduw_code(env, s->pc);
529 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
531 case 6: /* Indirect index + displacement. */
533 return gen_lea_indexed(env, s, reg);
536 case 0: /* Absolute short. */
537 offset = cpu_ldsw_code(env, s->pc);
539 return tcg_const_i32(offset);
540 case 1: /* Absolute long. */
541 offset = read_im32(env, s);
542 return tcg_const_i32(offset);
543 case 2: /* pc displacement */
545 offset += cpu_ldsw_code(env, s->pc);
547 return tcg_const_i32(offset);
548 case 3: /* pc index+displacement. */
549 return gen_lea_indexed(env, s, NULL_QREG);
550 case 4: /* Immediate. */
555 /* Should never happen. */
559 /* Helper function for gen_ea. Reuse the computed address between the
560 for read/write operands. */
561 static inline TCGv gen_ea_once(CPUM68KState *env, DisasContext *s,
562 uint16_t insn, int opsize, TCGv val,
563 TCGv *addrp, ea_what what)
567 if (addrp && what == EA_STORE) {
570 tmp = gen_lea(env, s, insn, opsize);
571 if (IS_NULL_QREG(tmp))
576 return gen_ldst(s, opsize, tmp, val, what);
579 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
580 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
581 ADDRP is non-null for readwrite operands. */
582 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
583 int opsize, TCGv val, TCGv *addrp, ea_what what)
589 switch ((insn >> 3) & 7) {
590 case 0: /* Data register direct. */
592 if (what == EA_STORE) {
593 gen_partset_reg(opsize, reg, val);
596 return gen_extend(reg, opsize, what == EA_LOADS);
598 case 1: /* Address register direct. */
600 if (what == EA_STORE) {
601 tcg_gen_mov_i32(reg, val);
604 return gen_extend(reg, opsize, what == EA_LOADS);
606 case 2: /* Indirect register */
608 return gen_ldst(s, opsize, reg, val, what);
609 case 3: /* Indirect postincrement. */
611 result = gen_ldst(s, opsize, reg, val, what);
612 /* ??? This is not exception safe. The instruction may still
613 fault after this point. */
614 if (what == EA_STORE || !addrp)
615 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
617 case 4: /* Indirect predecrememnt. */
620 if (addrp && what == EA_STORE) {
623 tmp = gen_lea(env, s, insn, opsize);
624 if (IS_NULL_QREG(tmp))
629 result = gen_ldst(s, opsize, tmp, val, what);
630 /* ??? This is not exception safe. The instruction may still
631 fault after this point. */
632 if (what == EA_STORE || !addrp) {
634 tcg_gen_mov_i32(reg, tmp);
638 case 5: /* Indirect displacement. */
639 case 6: /* Indirect index + displacement. */
640 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
643 case 0: /* Absolute short. */
644 case 1: /* Absolute long. */
645 case 2: /* pc displacement */
646 case 3: /* pc index+displacement. */
647 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
648 case 4: /* Immediate. */
649 /* Sign extend values for consistency. */
652 if (what == EA_LOADS) {
653 offset = cpu_ldsb_code(env, s->pc + 1);
655 offset = cpu_ldub_code(env, s->pc + 1);
660 if (what == EA_LOADS) {
661 offset = cpu_ldsw_code(env, s->pc);
663 offset = cpu_lduw_code(env, s->pc);
668 offset = read_im32(env, s);
671 g_assert_not_reached();
673 return tcg_const_i32(offset);
678 /* Should never happen. */
682 /* This generates a conditional branch, clobbering all temporaries. */
683 static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
687 /* TODO: Optimize compare/branch pairs rather than always flushing
688 flag state to CC_OP_FLAGS. */
696 case 2: /* HI (!C && !Z) */
697 tmp = tcg_temp_new();
698 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
699 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
701 case 3: /* LS (C || Z) */
702 tmp = tcg_temp_new();
703 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
704 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
706 case 4: /* CC (!C) */
707 tmp = tcg_temp_new();
708 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
709 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
712 tmp = tcg_temp_new();
713 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
714 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
716 case 6: /* NE (!Z) */
717 tmp = tcg_temp_new();
718 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
719 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
722 tmp = tcg_temp_new();
723 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
724 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
726 case 8: /* VC (!V) */
727 tmp = tcg_temp_new();
728 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
729 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
732 tmp = tcg_temp_new();
733 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
734 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
736 case 10: /* PL (!N) */
737 tmp = tcg_temp_new();
738 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
739 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
741 case 11: /* MI (N) */
742 tmp = tcg_temp_new();
743 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
744 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
746 case 12: /* GE (!(N ^ V)) */
747 tmp = tcg_temp_new();
748 assert(CCF_V == (CCF_N >> 2));
749 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
750 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
751 tcg_gen_andi_i32(tmp, tmp, CCF_V);
752 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
754 case 13: /* LT (N ^ V) */
755 tmp = tcg_temp_new();
756 assert(CCF_V == (CCF_N >> 2));
757 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
758 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
759 tcg_gen_andi_i32(tmp, tmp, CCF_V);
760 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
762 case 14: /* GT (!(Z || (N ^ V))) */
763 tmp = tcg_temp_new();
764 assert(CCF_V == (CCF_N >> 2));
765 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
766 tcg_gen_shri_i32(tmp, tmp, 2);
767 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
768 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
769 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
771 case 15: /* LE (Z || (N ^ V)) */
772 tmp = tcg_temp_new();
773 assert(CCF_V == (CCF_N >> 2));
774 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
775 tcg_gen_shri_i32(tmp, tmp, 2);
776 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
777 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
778 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
781 /* Should ever happen. */
792 l1 = gen_new_label();
793 cond = (insn >> 8) & 0xf;
795 tcg_gen_andi_i32(reg, reg, 0xffffff00);
796 /* This is safe because we modify the reg directly, with no other values
798 gen_jmpcc(s, cond ^ 1, l1);
799 tcg_gen_ori_i32(reg, reg, 0xff);
803 /* Force a TB lookup after an instruction that changes the CPU state. */
804 static void gen_lookup_tb(DisasContext *s)
807 tcg_gen_movi_i32(QREG_PC, s->pc);
808 s->is_jmp = DISAS_UPDATE;
811 /* Generate a jump to an immediate address. */
812 static void gen_jmp_im(DisasContext *s, uint32_t dest)
815 tcg_gen_movi_i32(QREG_PC, dest);
816 s->is_jmp = DISAS_JUMP;
819 /* Generate a jump to the address in qreg DEST. */
820 static void gen_jmp(DisasContext *s, TCGv dest)
823 tcg_gen_mov_i32(QREG_PC, dest);
824 s->is_jmp = DISAS_JUMP;
827 static void gen_exception(DisasContext *s, uint32_t where, int nr)
830 gen_jmp_im(s, where);
831 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
834 static inline void gen_addr_fault(DisasContext *s)
836 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
839 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
840 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
841 op_sign ? EA_LOADS : EA_LOADU); \
842 if (IS_NULL_QREG(result)) { \
848 #define DEST_EA(env, insn, opsize, val, addrp) do { \
849 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
850 if (IS_NULL_QREG(ea_result)) { \
856 static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
858 #ifndef CONFIG_USER_ONLY
859 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
860 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
866 /* Generate a jump to an immediate address. */
867 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
869 if (unlikely(s->singlestep_enabled)) {
870 gen_exception(s, dest, EXCP_DEBUG);
871 } else if (use_goto_tb(s, dest)) {
873 tcg_gen_movi_i32(QREG_PC, dest);
874 tcg_gen_exit_tb((uintptr_t)s->tb + n);
879 s->is_jmp = DISAS_TB_JUMP;
882 DISAS_INSN(undef_mac)
884 gen_exception(s, s->pc - 2, EXCP_LINEA);
887 DISAS_INSN(undef_fpu)
889 gen_exception(s, s->pc - 2, EXCP_LINEF);
894 M68kCPU *cpu = m68k_env_get_cpu(env);
896 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
897 cpu_abort(CPU(cpu), "Illegal instruction: %04x @ %08x", insn, s->pc - 2);
907 sign = (insn & 0x100) != 0;
909 tmp = tcg_temp_new();
911 tcg_gen_ext16s_i32(tmp, reg);
913 tcg_gen_ext16u_i32(tmp, reg);
914 SRC_EA(env, src, OS_WORD, sign, NULL);
915 tcg_gen_mul_i32(tmp, tmp, src);
916 tcg_gen_mov_i32(reg, tmp);
917 /* Unlike m68k, coldfire always clears the overflow bit. */
918 gen_logic_cc(s, tmp);
928 sign = (insn & 0x100) != 0;
931 tcg_gen_ext16s_i32(QREG_DIV1, reg);
933 tcg_gen_ext16u_i32(QREG_DIV1, reg);
935 SRC_EA(env, src, OS_WORD, sign, NULL);
936 tcg_gen_mov_i32(QREG_DIV2, src);
938 gen_helper_divs(cpu_env, tcg_const_i32(1));
940 gen_helper_divu(cpu_env, tcg_const_i32(1));
943 tmp = tcg_temp_new();
944 src = tcg_temp_new();
945 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
946 tcg_gen_shli_i32(src, QREG_DIV2, 16);
947 tcg_gen_or_i32(reg, tmp, src);
948 s->cc_op = CC_OP_FLAGS;
958 ext = cpu_lduw_code(env, s->pc);
961 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
966 tcg_gen_mov_i32(QREG_DIV1, num);
967 SRC_EA(env, den, OS_LONG, 0, NULL);
968 tcg_gen_mov_i32(QREG_DIV2, den);
970 gen_helper_divs(cpu_env, tcg_const_i32(0));
972 gen_helper_divu(cpu_env, tcg_const_i32(0));
974 if ((ext & 7) == ((ext >> 12) & 7)) {
976 tcg_gen_mov_i32 (reg, QREG_DIV1);
979 tcg_gen_mov_i32 (reg, QREG_DIV2);
981 s->cc_op = CC_OP_FLAGS;
993 add = (insn & 0x4000) != 0;
995 dest = tcg_temp_new();
997 SRC_EA(env, tmp, OS_LONG, 0, &addr);
1001 SRC_EA(env, src, OS_LONG, 0, NULL);
1004 tcg_gen_add_i32(dest, tmp, src);
1005 gen_helper_xflag_lt(QREG_CC_X, dest, src);
1006 s->cc_op = CC_OP_ADD;
1008 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
1009 tcg_gen_sub_i32(dest, tmp, src);
1010 s->cc_op = CC_OP_SUB;
1012 gen_update_cc_add(dest, src);
1014 DEST_EA(env, insn, OS_LONG, dest, &addr);
1016 tcg_gen_mov_i32(reg, dest);
1021 /* Reverse the order of the bits in REG. */
1025 reg = DREG(insn, 0);
1026 gen_helper_bitrev(reg, reg);
1029 DISAS_INSN(bitop_reg)
1039 if ((insn & 0x38) != 0)
1043 op = (insn >> 6) & 3;
1044 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1045 src2 = DREG(insn, 9);
1046 dest = tcg_temp_new();
1049 tmp = tcg_temp_new();
1050 if (opsize == OS_BYTE)
1051 tcg_gen_andi_i32(tmp, src2, 7);
1053 tcg_gen_andi_i32(tmp, src2, 31);
1055 tmp = tcg_temp_new();
1056 tcg_gen_shr_i32(tmp, src1, src2);
1057 tcg_gen_andi_i32(tmp, tmp, 1);
1058 tcg_gen_shli_i32(tmp, tmp, 2);
1059 /* Clear CCF_Z if bit set. */
1060 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1061 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1063 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
1066 tcg_gen_xor_i32(dest, src1, tmp);
1069 tcg_gen_not_i32(tmp, tmp);
1070 tcg_gen_and_i32(dest, src1, tmp);
1073 tcg_gen_or_i32(dest, src1, tmp);
1079 DEST_EA(env, insn, opsize, dest, &addr);
1085 reg = DREG(insn, 0);
1087 gen_helper_sats(reg, reg, QREG_CC_DEST);
1088 gen_logic_cc(s, reg);
1091 static void gen_push(DisasContext *s, TCGv val)
1095 tmp = tcg_temp_new();
1096 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1097 gen_store(s, OS_LONG, tmp, val);
1098 tcg_gen_mov_i32(QREG_SP, tmp);
1110 mask = cpu_lduw_code(env, s->pc);
1112 tmp = gen_lea(env, s, insn, OS_LONG);
1113 if (IS_NULL_QREG(tmp)) {
1117 addr = tcg_temp_new();
1118 tcg_gen_mov_i32(addr, tmp);
1119 is_load = ((insn & 0x0400) != 0);
1120 for (i = 0; i < 16; i++, mask >>= 1) {
1127 tmp = gen_load(s, OS_LONG, addr, 0);
1128 tcg_gen_mov_i32(reg, tmp);
1130 gen_store(s, OS_LONG, addr, reg);
1133 tcg_gen_addi_i32(addr, addr, 4);
1138 DISAS_INSN(bitop_im)
1148 if ((insn & 0x38) != 0)
1152 op = (insn >> 6) & 3;
1154 bitnum = cpu_lduw_code(env, s->pc);
1156 if (bitnum & 0xff00) {
1157 disas_undef(env, s, insn);
1161 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1164 if (opsize == OS_BYTE)
1170 tmp = tcg_temp_new();
1171 assert (CCF_Z == (1 << 2));
1173 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1174 else if (bitnum < 2)
1175 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1177 tcg_gen_mov_i32(tmp, src1);
1178 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1179 /* Clear CCF_Z if bit set. */
1180 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1181 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1185 tcg_gen_xori_i32(tmp, src1, mask);
1188 tcg_gen_andi_i32(tmp, src1, ~mask);
1191 tcg_gen_ori_i32(tmp, src1, mask);
1196 DEST_EA(env, insn, opsize, tmp, &addr);
1200 DISAS_INSN(arith_im)
1208 op = (insn >> 9) & 7;
1209 SRC_EA(env, src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1210 im = read_im32(env, s);
1211 dest = tcg_temp_new();
1214 tcg_gen_ori_i32(dest, src1, im);
1215 gen_logic_cc(s, dest);
1218 tcg_gen_andi_i32(dest, src1, im);
1219 gen_logic_cc(s, dest);
1222 tcg_gen_mov_i32(dest, src1);
1223 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1224 tcg_gen_subi_i32(dest, dest, im);
1225 gen_update_cc_add(dest, tcg_const_i32(im));
1226 s->cc_op = CC_OP_SUB;
1229 tcg_gen_mov_i32(dest, src1);
1230 tcg_gen_addi_i32(dest, dest, im);
1231 gen_update_cc_add(dest, tcg_const_i32(im));
1232 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1233 s->cc_op = CC_OP_ADD;
1236 tcg_gen_xori_i32(dest, src1, im);
1237 gen_logic_cc(s, dest);
1240 tcg_gen_mov_i32(dest, src1);
1241 tcg_gen_subi_i32(dest, dest, im);
1242 gen_update_cc_add(dest, tcg_const_i32(im));
1243 s->cc_op = CC_OP_SUB;
1249 DEST_EA(env, insn, OS_LONG, dest, &addr);
1257 reg = DREG(insn, 0);
1258 tcg_gen_bswap32_i32(reg, reg);
1268 switch (insn >> 12) {
1269 case 1: /* move.b */
1272 case 2: /* move.l */
1275 case 3: /* move.w */
1281 SRC_EA(env, src, opsize, 1, NULL);
1282 op = (insn >> 6) & 7;
1285 /* The value will already have been sign extended. */
1286 dest = AREG(insn, 9);
1287 tcg_gen_mov_i32(dest, src);
1291 dest_ea = ((insn >> 9) & 7) | (op << 3);
1292 DEST_EA(env, dest_ea, opsize, src, NULL);
1293 /* This will be correct because loads sign extend. */
1294 gen_logic_cc(s, src);
1303 reg = DREG(insn, 0);
1304 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1312 reg = AREG(insn, 9);
1313 tmp = gen_lea(env, s, insn, OS_LONG);
1314 if (IS_NULL_QREG(tmp)) {
1318 tcg_gen_mov_i32(reg, tmp);
1325 switch ((insn >> 6) & 3) {
1338 DEST_EA(env, insn, opsize, tcg_const_i32(0), NULL);
1339 gen_logic_cc(s, tcg_const_i32(0));
1342 static TCGv gen_get_ccr(DisasContext *s)
1347 dest = tcg_temp_new();
1348 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1349 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
1353 DISAS_INSN(move_from_ccr)
1358 ccr = gen_get_ccr(s);
1359 reg = DREG(insn, 0);
1360 gen_partset_reg(OS_WORD, reg, ccr);
1368 reg = DREG(insn, 0);
1369 src1 = tcg_temp_new();
1370 tcg_gen_mov_i32(src1, reg);
1371 tcg_gen_neg_i32(reg, src1);
1372 s->cc_op = CC_OP_SUB;
1373 gen_update_cc_add(reg, src1);
1374 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
1375 s->cc_op = CC_OP_SUB;
1378 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1380 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1381 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
1383 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1387 static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
1393 s->cc_op = CC_OP_FLAGS;
1394 if ((insn & 0x38) == 0)
1396 tmp = tcg_temp_new();
1397 reg = DREG(insn, 0);
1398 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1399 tcg_gen_shri_i32(tmp, reg, 4);
1400 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1402 gen_helper_set_sr(cpu_env, reg);
1405 else if ((insn & 0x3f) == 0x3c)
1408 val = cpu_lduw_code(env, s->pc);
1410 gen_set_sr_im(s, val, ccr_only);
1413 disas_undef(env, s, insn);
1416 DISAS_INSN(move_to_ccr)
1418 gen_set_sr(env, s, insn, 1);
1425 reg = DREG(insn, 0);
1426 tcg_gen_not_i32(reg, reg);
1427 gen_logic_cc(s, reg);
1436 src1 = tcg_temp_new();
1437 src2 = tcg_temp_new();
1438 reg = DREG(insn, 0);
1439 tcg_gen_shli_i32(src1, reg, 16);
1440 tcg_gen_shri_i32(src2, reg, 16);
1441 tcg_gen_or_i32(reg, src1, src2);
1442 gen_logic_cc(s, reg);
1449 tmp = gen_lea(env, s, insn, OS_LONG);
1450 if (IS_NULL_QREG(tmp)) {
1463 reg = DREG(insn, 0);
1464 op = (insn >> 6) & 7;
1465 tmp = tcg_temp_new();
1467 tcg_gen_ext16s_i32(tmp, reg);
1469 tcg_gen_ext8s_i32(tmp, reg);
1471 gen_partset_reg(OS_WORD, reg, tmp);
1473 tcg_gen_mov_i32(reg, tmp);
1474 gen_logic_cc(s, tmp);
1482 switch ((insn >> 6) & 3) {
1495 SRC_EA(env, tmp, opsize, 1, NULL);
1496 gen_logic_cc(s, tmp);
1501 /* Implemented as a NOP. */
1506 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1509 /* ??? This should be atomic. */
1516 dest = tcg_temp_new();
1517 SRC_EA(env, src1, OS_BYTE, 1, &addr);
1518 gen_logic_cc(s, src1);
1519 tcg_gen_ori_i32(dest, src1, 0x80);
1520 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1530 /* The upper 32 bits of the product are discarded, so
1531 muls.l and mulu.l are functionally equivalent. */
1532 ext = cpu_lduw_code(env, s->pc);
1535 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1538 reg = DREG(ext, 12);
1539 SRC_EA(env, src1, OS_LONG, 0, NULL);
1540 dest = tcg_temp_new();
1541 tcg_gen_mul_i32(dest, src1, reg);
1542 tcg_gen_mov_i32(reg, dest);
1543 /* Unlike m68k, coldfire always clears the overflow bit. */
1544 gen_logic_cc(s, dest);
1553 offset = cpu_ldsw_code(env, s->pc);
1555 reg = AREG(insn, 0);
1556 tmp = tcg_temp_new();
1557 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1558 gen_store(s, OS_LONG, tmp, reg);
1559 if ((insn & 7) != 7)
1560 tcg_gen_mov_i32(reg, tmp);
1561 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1570 src = tcg_temp_new();
1571 reg = AREG(insn, 0);
1572 tcg_gen_mov_i32(src, reg);
1573 tmp = gen_load(s, OS_LONG, src, 0);
1574 tcg_gen_mov_i32(reg, tmp);
1575 tcg_gen_addi_i32(QREG_SP, src, 4);
1586 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1587 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1595 /* Load the target address first to ensure correct exception
1597 tmp = gen_lea(env, s, insn, OS_LONG);
1598 if (IS_NULL_QREG(tmp)) {
1602 if ((insn & 0x40) == 0) {
1604 gen_push(s, tcg_const_i32(s->pc));
1617 SRC_EA(env, src1, OS_LONG, 0, &addr);
1618 val = (insn >> 9) & 7;
1621 dest = tcg_temp_new();
1622 tcg_gen_mov_i32(dest, src1);
1623 if ((insn & 0x38) == 0x08) {
1624 /* Don't update condition codes if the destination is an
1625 address register. */
1626 if (insn & 0x0100) {
1627 tcg_gen_subi_i32(dest, dest, val);
1629 tcg_gen_addi_i32(dest, dest, val);
1632 src2 = tcg_const_i32(val);
1633 if (insn & 0x0100) {
1634 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1635 tcg_gen_subi_i32(dest, dest, val);
1636 s->cc_op = CC_OP_SUB;
1638 tcg_gen_addi_i32(dest, dest, val);
1639 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1640 s->cc_op = CC_OP_ADD;
1642 gen_update_cc_add(dest, src2);
1644 DEST_EA(env, insn, OS_LONG, dest, &addr);
1650 case 2: /* One extension word. */
1653 case 3: /* Two extension words. */
1656 case 4: /* No extension words. */
1659 disas_undef(env, s, insn);
1671 op = (insn >> 8) & 0xf;
1672 offset = (int8_t)insn;
1674 offset = cpu_ldsw_code(env, s->pc);
1676 } else if (offset == -1) {
1677 offset = read_im32(env, s);
1681 gen_push(s, tcg_const_i32(s->pc));
1686 l1 = gen_new_label();
1687 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1688 gen_jmp_tb(s, 1, base + offset);
1690 gen_jmp_tb(s, 0, s->pc);
1692 /* Unconditional branch. */
1693 gen_jmp_tb(s, 0, base + offset);
1702 tcg_gen_movi_i32(DREG(insn, 9), val);
1703 gen_logic_cc(s, tcg_const_i32(val));
1716 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
1717 reg = DREG(insn, 9);
1718 tcg_gen_mov_i32(reg, src);
1719 gen_logic_cc(s, src);
1729 reg = DREG(insn, 9);
1730 dest = tcg_temp_new();
1732 SRC_EA(env, src, OS_LONG, 0, &addr);
1733 tcg_gen_or_i32(dest, src, reg);
1734 DEST_EA(env, insn, OS_LONG, dest, &addr);
1736 SRC_EA(env, src, OS_LONG, 0, NULL);
1737 tcg_gen_or_i32(dest, src, reg);
1738 tcg_gen_mov_i32(reg, dest);
1740 gen_logic_cc(s, dest);
1748 SRC_EA(env, src, OS_LONG, 0, NULL);
1749 reg = AREG(insn, 9);
1750 tcg_gen_sub_i32(reg, reg, src);
1759 reg = DREG(insn, 9);
1760 src = DREG(insn, 0);
1761 gen_helper_subx_cc(reg, cpu_env, reg, src);
1769 val = (insn >> 9) & 7;
1772 src = tcg_const_i32(val);
1773 gen_logic_cc(s, src);
1774 DEST_EA(env, insn, OS_LONG, src, NULL);
1785 op = (insn >> 6) & 3;
1789 s->cc_op = CC_OP_CMPB;
1793 s->cc_op = CC_OP_CMPW;
1797 s->cc_op = CC_OP_SUB;
1802 SRC_EA(env, src, opsize, 1, NULL);
1803 reg = DREG(insn, 9);
1804 dest = tcg_temp_new();
1805 tcg_gen_sub_i32(dest, reg, src);
1806 gen_update_cc_add(dest, src);
1821 SRC_EA(env, src, opsize, 1, NULL);
1822 reg = AREG(insn, 9);
1823 dest = tcg_temp_new();
1824 tcg_gen_sub_i32(dest, reg, src);
1825 gen_update_cc_add(dest, src);
1826 s->cc_op = CC_OP_SUB;
1836 SRC_EA(env, src, OS_LONG, 0, &addr);
1837 reg = DREG(insn, 9);
1838 dest = tcg_temp_new();
1839 tcg_gen_xor_i32(dest, src, reg);
1840 gen_logic_cc(s, dest);
1841 DEST_EA(env, insn, OS_LONG, dest, &addr);
1851 reg = DREG(insn, 9);
1852 dest = tcg_temp_new();
1854 SRC_EA(env, src, OS_LONG, 0, &addr);
1855 tcg_gen_and_i32(dest, src, reg);
1856 DEST_EA(env, insn, OS_LONG, dest, &addr);
1858 SRC_EA(env, src, OS_LONG, 0, NULL);
1859 tcg_gen_and_i32(dest, src, reg);
1860 tcg_gen_mov_i32(reg, dest);
1862 gen_logic_cc(s, dest);
1870 SRC_EA(env, src, OS_LONG, 0, NULL);
1871 reg = AREG(insn, 9);
1872 tcg_gen_add_i32(reg, reg, src);
1881 reg = DREG(insn, 9);
1882 src = DREG(insn, 0);
1883 gen_helper_addx_cc(reg, cpu_env, reg, src);
1884 s->cc_op = CC_OP_FLAGS;
1887 /* TODO: This could be implemented without helper functions. */
1888 DISAS_INSN(shift_im)
1894 reg = DREG(insn, 0);
1895 tmp = (insn >> 9) & 7;
1898 shift = tcg_const_i32(tmp);
1899 /* No need to flush flags becuse we know we will set C flag. */
1901 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1904 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1906 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1909 s->cc_op = CC_OP_SHIFT;
1912 DISAS_INSN(shift_reg)
1917 reg = DREG(insn, 0);
1918 shift = DREG(insn, 9);
1919 /* Shift by zero leaves C flag unmodified. */
1922 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1925 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1927 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1930 s->cc_op = CC_OP_SHIFT;
1936 reg = DREG(insn, 0);
1937 gen_logic_cc(s, reg);
1938 gen_helper_ff1(reg, reg);
1941 static TCGv gen_get_sr(DisasContext *s)
1946 ccr = gen_get_ccr(s);
1947 sr = tcg_temp_new();
1948 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1949 tcg_gen_or_i32(sr, sr, ccr);
1959 ext = cpu_lduw_code(env, s->pc);
1961 if (ext != 0x46FC) {
1962 gen_exception(s, addr, EXCP_UNSUPPORTED);
1965 ext = cpu_lduw_code(env, s->pc);
1967 if (IS_USER(s) || (ext & SR_S) == 0) {
1968 gen_exception(s, addr, EXCP_PRIVILEGE);
1971 gen_push(s, gen_get_sr(s));
1972 gen_set_sr_im(s, ext, 0);
1975 DISAS_INSN(move_from_sr)
1981 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1985 reg = DREG(insn, 0);
1986 gen_partset_reg(OS_WORD, reg, sr);
1989 DISAS_INSN(move_to_sr)
1992 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1995 gen_set_sr(env, s, insn, 0);
1999 DISAS_INSN(move_from_usp)
2002 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2005 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
2006 offsetof(CPUM68KState, sp[M68K_USP]));
2009 DISAS_INSN(move_to_usp)
2012 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2015 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
2016 offsetof(CPUM68KState, sp[M68K_USP]));
2021 gen_exception(s, s->pc, EXCP_HALT_INSN);
2029 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2033 ext = cpu_lduw_code(env, s->pc);
2036 gen_set_sr_im(s, ext, 0);
2037 tcg_gen_movi_i32(cpu_halted, 1);
2038 gen_exception(s, s->pc, EXCP_HLT);
2044 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2047 gen_exception(s, s->pc - 2, EXCP_RTE);
2056 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2060 ext = cpu_lduw_code(env, s->pc);
2064 reg = AREG(ext, 12);
2066 reg = DREG(ext, 12);
2068 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2075 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2078 /* ICache fetch. Implement as no-op. */
2084 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2087 /* Cache push/invalidate. Implement as no-op. */
2092 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2097 M68kCPU *cpu = m68k_env_get_cpu(env);
2100 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2103 /* TODO: Implement wdebug. */
2104 cpu_abort(CPU(cpu), "WDEBUG not implemented");
2109 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2112 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2113 immediately before the next FP instruction is executed. */
2127 ext = cpu_lduw_code(env, s->pc);
2129 opmode = ext & 0x7f;
2130 switch ((ext >> 13) & 7) {
2135 case 3: /* fmove out */
2137 tmp32 = tcg_temp_new_i32();
2139 /* ??? TODO: Proper behavior on overflow. */
2140 switch ((ext >> 10) & 7) {
2143 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2147 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2151 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2153 case 5: /* OS_DOUBLE */
2154 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2155 switch ((insn >> 3) & 7) {
2160 tcg_gen_addi_i32(tmp32, tmp32, -8);
2163 offset = cpu_ldsw_code(env, s->pc);
2165 tcg_gen_addi_i32(tmp32, tmp32, offset);
2170 gen_store64(s, tmp32, src);
2171 switch ((insn >> 3) & 7) {
2173 tcg_gen_addi_i32(tmp32, tmp32, 8);
2174 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2177 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2180 tcg_temp_free_i32(tmp32);
2184 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2189 DEST_EA(env, insn, opsize, tmp32, NULL);
2190 tcg_temp_free_i32(tmp32);
2192 case 4: /* fmove to control register. */
2193 switch ((ext >> 10) & 7) {
2195 /* Not implemented. Ignore writes. */
2200 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2204 case 5: /* fmove from control register. */
2205 switch ((ext >> 10) & 7) {
2207 /* Not implemented. Always return zero. */
2208 tmp32 = tcg_const_i32(0);
2213 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2217 DEST_EA(env, insn, OS_LONG, tmp32, NULL);
2219 case 6: /* fmovem */
2225 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2227 tmp32 = gen_lea(env, s, insn, OS_LONG);
2228 if (IS_NULL_QREG(tmp32)) {
2232 addr = tcg_temp_new_i32();
2233 tcg_gen_mov_i32(addr, tmp32);
2235 for (i = 0; i < 8; i++) {
2238 if (ext & (1 << 13)) {
2240 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2243 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2245 if (ext & (mask - 1))
2246 tcg_gen_addi_i32(addr, addr, 8);
2250 tcg_temp_free_i32(addr);
2254 if (ext & (1 << 14)) {
2255 /* Source effective address. */
2256 switch ((ext >> 10) & 7) {
2257 case 0: opsize = OS_LONG; break;
2258 case 1: opsize = OS_SINGLE; break;
2259 case 4: opsize = OS_WORD; break;
2260 case 5: opsize = OS_DOUBLE; break;
2261 case 6: opsize = OS_BYTE; break;
2265 if (opsize == OS_DOUBLE) {
2266 tmp32 = tcg_temp_new_i32();
2267 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2268 switch ((insn >> 3) & 7) {
2273 tcg_gen_addi_i32(tmp32, tmp32, -8);
2276 offset = cpu_ldsw_code(env, s->pc);
2278 tcg_gen_addi_i32(tmp32, tmp32, offset);
2281 offset = cpu_ldsw_code(env, s->pc);
2282 offset += s->pc - 2;
2284 tcg_gen_addi_i32(tmp32, tmp32, offset);
2289 src = gen_load64(s, tmp32);
2290 switch ((insn >> 3) & 7) {
2292 tcg_gen_addi_i32(tmp32, tmp32, 8);
2293 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2296 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2299 tcg_temp_free_i32(tmp32);
2301 SRC_EA(env, tmp32, opsize, 1, NULL);
2302 src = tcg_temp_new_i64();
2307 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2310 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2315 /* Source register. */
2316 src = FREG(ext, 10);
2318 dest = FREG(ext, 7);
2319 res = tcg_temp_new_i64();
2321 tcg_gen_mov_f64(res, dest);
2325 case 0: case 0x40: case 0x44: /* fmove */
2326 tcg_gen_mov_f64(res, src);
2329 gen_helper_iround_f64(res, cpu_env, src);
2332 case 3: /* fintrz */
2333 gen_helper_itrunc_f64(res, cpu_env, src);
2336 case 4: case 0x41: case 0x45: /* fsqrt */
2337 gen_helper_sqrt_f64(res, cpu_env, src);
2339 case 0x18: case 0x58: case 0x5c: /* fabs */
2340 gen_helper_abs_f64(res, src);
2342 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2343 gen_helper_chs_f64(res, src);
2345 case 0x20: case 0x60: case 0x64: /* fdiv */
2346 gen_helper_div_f64(res, cpu_env, res, src);
2348 case 0x22: case 0x62: case 0x66: /* fadd */
2349 gen_helper_add_f64(res, cpu_env, res, src);
2351 case 0x23: case 0x63: case 0x67: /* fmul */
2352 gen_helper_mul_f64(res, cpu_env, res, src);
2354 case 0x28: case 0x68: case 0x6c: /* fsub */
2355 gen_helper_sub_f64(res, cpu_env, res, src);
2357 case 0x38: /* fcmp */
2358 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2362 case 0x3a: /* ftst */
2363 tcg_gen_mov_f64(res, src);
2370 if (ext & (1 << 14)) {
2371 tcg_temp_free_i64(src);
2374 if (opmode & 0x40) {
2375 if ((opmode & 0x4) != 0)
2377 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2382 TCGv tmp = tcg_temp_new_i32();
2383 gen_helper_f64_to_f32(tmp, cpu_env, res);
2384 gen_helper_f32_to_f64(res, cpu_env, tmp);
2385 tcg_temp_free_i32(tmp);
2387 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2389 tcg_gen_mov_f64(dest, res);
2391 tcg_temp_free_i64(res);
2394 /* FIXME: Is this right for offset addressing modes? */
2396 disas_undef_fpu(env, s, insn);
2407 offset = cpu_ldsw_code(env, s->pc);
2409 if (insn & (1 << 6)) {
2410 offset = (offset << 16) | cpu_lduw_code(env, s->pc);
2414 l1 = gen_new_label();
2415 /* TODO: Raise BSUN exception. */
2416 flag = tcg_temp_new();
2417 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2418 /* Jump to l1 if condition is true. */
2419 switch (insn & 0xf) {
2422 case 1: /* eq (=0) */
2423 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2425 case 2: /* ogt (=1) */
2426 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2428 case 3: /* oge (=0 or =1) */
2429 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2431 case 4: /* olt (=-1) */
2432 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2434 case 5: /* ole (=-1 or =0) */
2435 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2437 case 6: /* ogl (=-1 or =1) */
2438 tcg_gen_andi_i32(flag, flag, 1);
2439 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2441 case 7: /* or (=2) */
2442 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2444 case 8: /* un (<2) */
2445 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2447 case 9: /* ueq (=0 or =2) */
2448 tcg_gen_andi_i32(flag, flag, 1);
2449 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2451 case 10: /* ugt (>0) */
2452 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2454 case 11: /* uge (>=0) */
2455 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2457 case 12: /* ult (=-1 or =2) */
2458 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2460 case 13: /* ule (!=1) */
2461 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2463 case 14: /* ne (!=0) */
2464 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2470 gen_jmp_tb(s, 0, s->pc);
2472 gen_jmp_tb(s, 1, addr + offset);
2475 DISAS_INSN(frestore)
2477 M68kCPU *cpu = m68k_env_get_cpu(env);
2479 /* TODO: Implement frestore. */
2480 cpu_abort(CPU(cpu), "FRESTORE not implemented");
2485 M68kCPU *cpu = m68k_env_get_cpu(env);
2487 /* TODO: Implement fsave. */
2488 cpu_abort(CPU(cpu), "FSAVE not implemented");
2491 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2493 TCGv tmp = tcg_temp_new();
2494 if (s->env->macsr & MACSR_FI) {
2496 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2498 tcg_gen_shli_i32(tmp, val, 16);
2499 } else if (s->env->macsr & MACSR_SU) {
2501 tcg_gen_sari_i32(tmp, val, 16);
2503 tcg_gen_ext16s_i32(tmp, val);
2506 tcg_gen_shri_i32(tmp, val, 16);
2508 tcg_gen_ext16u_i32(tmp, val);
2513 static void gen_mac_clear_flags(void)
2515 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2516 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2532 s->mactmp = tcg_temp_new_i64();
2536 ext = cpu_lduw_code(env, s->pc);
2539 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2540 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2541 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2542 disas_undef(env, s, insn);
2546 /* MAC with load. */
2547 tmp = gen_lea(env, s, insn, OS_LONG);
2548 addr = tcg_temp_new();
2549 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2550 /* Load the value now to ensure correct exception behavior.
2551 Perform writeback after reading the MAC inputs. */
2552 loadval = gen_load(s, OS_LONG, addr, 0);
2555 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2556 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2558 loadval = addr = NULL_QREG;
2559 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2560 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2563 gen_mac_clear_flags();
2566 /* Disabled because conditional branches clobber temporary vars. */
2567 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2568 /* Skip the multiply if we know we will ignore it. */
2569 l1 = gen_new_label();
2570 tmp = tcg_temp_new();
2571 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2572 gen_op_jmp_nz32(tmp, l1);
2576 if ((ext & 0x0800) == 0) {
2578 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2579 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2581 if (s->env->macsr & MACSR_FI) {
2582 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2584 if (s->env->macsr & MACSR_SU)
2585 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2587 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2588 switch ((ext >> 9) & 3) {
2590 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2593 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2599 /* Save the overflow flag from the multiply. */
2600 saved_flags = tcg_temp_new();
2601 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2603 saved_flags = NULL_QREG;
2607 /* Disabled because conditional branches clobber temporary vars. */
2608 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2609 /* Skip the accumulate if the value is already saturated. */
2610 l1 = gen_new_label();
2611 tmp = tcg_temp_new();
2612 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2613 gen_op_jmp_nz32(tmp, l1);
2618 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2620 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2622 if (s->env->macsr & MACSR_FI)
2623 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2624 else if (s->env->macsr & MACSR_SU)
2625 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2627 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2630 /* Disabled because conditional branches clobber temporary vars. */
2636 /* Dual accumulate variant. */
2637 acc = (ext >> 2) & 3;
2638 /* Restore the overflow flag from the multiplier. */
2639 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2641 /* Disabled because conditional branches clobber temporary vars. */
2642 if ((s->env->macsr & MACSR_OMC) != 0) {
2643 /* Skip the accumulate if the value is already saturated. */
2644 l1 = gen_new_label();
2645 tmp = tcg_temp_new();
2646 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2647 gen_op_jmp_nz32(tmp, l1);
2651 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2653 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2654 if (s->env->macsr & MACSR_FI)
2655 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2656 else if (s->env->macsr & MACSR_SU)
2657 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2659 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2661 /* Disabled because conditional branches clobber temporary vars. */
2666 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2670 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2671 tcg_gen_mov_i32(rw, loadval);
2672 /* FIXME: Should address writeback happen with the masked or
2674 switch ((insn >> 3) & 7) {
2675 case 3: /* Post-increment. */
2676 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2678 case 4: /* Pre-decrement. */
2679 tcg_gen_mov_i32(AREG(insn, 0), addr);
2684 DISAS_INSN(from_mac)
2690 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2691 accnum = (insn >> 9) & 3;
2692 acc = MACREG(accnum);
2693 if (s->env->macsr & MACSR_FI) {
2694 gen_helper_get_macf(rx, cpu_env, acc);
2695 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2696 tcg_gen_extrl_i64_i32(rx, acc);
2697 } else if (s->env->macsr & MACSR_SU) {
2698 gen_helper_get_macs(rx, acc);
2700 gen_helper_get_macu(rx, acc);
2703 tcg_gen_movi_i64(acc, 0);
2704 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2708 DISAS_INSN(move_mac)
2710 /* FIXME: This can be done without a helper. */
2714 dest = tcg_const_i32((insn >> 9) & 3);
2715 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2716 gen_mac_clear_flags();
2717 gen_helper_mac_set_flags(cpu_env, dest);
2720 DISAS_INSN(from_macsr)
2724 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2725 tcg_gen_mov_i32(reg, QREG_MACSR);
2728 DISAS_INSN(from_mask)
2731 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2732 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2735 DISAS_INSN(from_mext)
2739 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2740 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2741 if (s->env->macsr & MACSR_FI)
2742 gen_helper_get_mac_extf(reg, cpu_env, acc);
2744 gen_helper_get_mac_exti(reg, cpu_env, acc);
2747 DISAS_INSN(macsr_to_ccr)
2749 tcg_gen_movi_i32(QREG_CC_X, 0);
2750 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2751 s->cc_op = CC_OP_FLAGS;
2759 accnum = (insn >> 9) & 3;
2760 acc = MACREG(accnum);
2761 SRC_EA(env, val, OS_LONG, 0, NULL);
2762 if (s->env->macsr & MACSR_FI) {
2763 tcg_gen_ext_i32_i64(acc, val);
2764 tcg_gen_shli_i64(acc, acc, 8);
2765 } else if (s->env->macsr & MACSR_SU) {
2766 tcg_gen_ext_i32_i64(acc, val);
2768 tcg_gen_extu_i32_i64(acc, val);
2770 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2771 gen_mac_clear_flags();
2772 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2775 DISAS_INSN(to_macsr)
2778 SRC_EA(env, val, OS_LONG, 0, NULL);
2779 gen_helper_set_macsr(cpu_env, val);
2786 SRC_EA(env, val, OS_LONG, 0, NULL);
2787 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2794 SRC_EA(env, val, OS_LONG, 0, NULL);
2795 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2796 if (s->env->macsr & MACSR_FI)
2797 gen_helper_set_mac_extf(cpu_env, val, acc);
2798 else if (s->env->macsr & MACSR_SU)
2799 gen_helper_set_mac_exts(cpu_env, val, acc);
2801 gen_helper_set_mac_extu(cpu_env, val, acc);
2804 static disas_proc opcode_table[65536];
2807 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2813 /* Sanity check. All set bits must be included in the mask. */
2814 if (opcode & ~mask) {
2816 "qemu internal error: bogus opcode definition %04x/%04x\n",
2820 /* This could probably be cleverer. For now just optimize the case where
2821 the top bits are known. */
2822 /* Find the first zero bit in the mask. */
2824 while ((i & mask) != 0)
2826 /* Iterate over all combinations of this and lower bits. */
2831 from = opcode & ~(i - 1);
2833 for (i = from; i < to; i++) {
2834 if ((i & mask) == opcode)
2835 opcode_table[i] = proc;
2839 /* Register m68k opcode handlers. Order is important.
2840 Later insn override earlier ones. */
2841 void register_m68k_insns (CPUM68KState *env)
2843 #define INSN(name, opcode, mask, feature) do { \
2844 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2845 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2847 INSN(undef, 0000, 0000, CF_ISA_A);
2848 INSN(arith_im, 0080, fff8, CF_ISA_A);
2849 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2850 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2851 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2852 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2853 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2854 INSN(arith_im, 0280, fff8, CF_ISA_A);
2855 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2856 INSN(arith_im, 0480, fff8, CF_ISA_A);
2857 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2858 INSN(arith_im, 0680, fff8, CF_ISA_A);
2859 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2860 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2861 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2862 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2863 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2864 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2865 INSN(move, 1000, f000, CF_ISA_A);
2866 INSN(move, 2000, f000, CF_ISA_A);
2867 INSN(move, 3000, f000, CF_ISA_A);
2868 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2869 INSN(negx, 4080, fff8, CF_ISA_A);
2870 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2871 INSN(lea, 41c0, f1c0, CF_ISA_A);
2872 INSN(clr, 4200, ff00, CF_ISA_A);
2873 INSN(undef, 42c0, ffc0, CF_ISA_A);
2874 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2875 INSN(neg, 4480, fff8, CF_ISA_A);
2876 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2877 INSN(not, 4680, fff8, CF_ISA_A);
2878 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2879 INSN(pea, 4840, ffc0, CF_ISA_A);
2880 INSN(swap, 4840, fff8, CF_ISA_A);
2881 INSN(movem, 48c0, fbc0, CF_ISA_A);
2882 INSN(ext, 4880, fff8, CF_ISA_A);
2883 INSN(ext, 48c0, fff8, CF_ISA_A);
2884 INSN(ext, 49c0, fff8, CF_ISA_A);
2885 INSN(tst, 4a00, ff00, CF_ISA_A);
2886 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2887 INSN(halt, 4ac8, ffff, CF_ISA_A);
2888 INSN(pulse, 4acc, ffff, CF_ISA_A);
2889 INSN(illegal, 4afc, ffff, CF_ISA_A);
2890 INSN(mull, 4c00, ffc0, CF_ISA_A);
2891 INSN(divl, 4c40, ffc0, CF_ISA_A);
2892 INSN(sats, 4c80, fff8, CF_ISA_B);
2893 INSN(trap, 4e40, fff0, CF_ISA_A);
2894 INSN(link, 4e50, fff8, CF_ISA_A);
2895 INSN(unlk, 4e58, fff8, CF_ISA_A);
2896 INSN(move_to_usp, 4e60, fff8, USP);
2897 INSN(move_from_usp, 4e68, fff8, USP);
2898 INSN(nop, 4e71, ffff, CF_ISA_A);
2899 INSN(stop, 4e72, ffff, CF_ISA_A);
2900 INSN(rte, 4e73, ffff, CF_ISA_A);
2901 INSN(rts, 4e75, ffff, CF_ISA_A);
2902 INSN(movec, 4e7b, ffff, CF_ISA_A);
2903 INSN(jump, 4e80, ffc0, CF_ISA_A);
2904 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2905 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2906 INSN(scc, 50c0, f0f8, CF_ISA_A);
2907 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2908 INSN(tpf, 51f8, fff8, CF_ISA_A);
2910 /* Branch instructions. */
2911 INSN(branch, 6000, f000, CF_ISA_A);
2912 /* Disable long branch instructions, then add back the ones we want. */
2913 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2914 INSN(branch, 60ff, f0ff, CF_ISA_B);
2915 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2916 INSN(branch, 60ff, ffff, BRAL);
2918 INSN(moveq, 7000, f100, CF_ISA_A);
2919 INSN(mvzs, 7100, f100, CF_ISA_B);
2920 INSN(or, 8000, f000, CF_ISA_A);
2921 INSN(divw, 80c0, f0c0, CF_ISA_A);
2922 INSN(addsub, 9000, f000, CF_ISA_A);
2923 INSN(subx, 9180, f1f8, CF_ISA_A);
2924 INSN(suba, 91c0, f1c0, CF_ISA_A);
2926 INSN(undef_mac, a000, f000, CF_ISA_A);
2927 INSN(mac, a000, f100, CF_EMAC);
2928 INSN(from_mac, a180, f9b0, CF_EMAC);
2929 INSN(move_mac, a110, f9fc, CF_EMAC);
2930 INSN(from_macsr,a980, f9f0, CF_EMAC);
2931 INSN(from_mask, ad80, fff0, CF_EMAC);
2932 INSN(from_mext, ab80, fbf0, CF_EMAC);
2933 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2934 INSN(to_mac, a100, f9c0, CF_EMAC);
2935 INSN(to_macsr, a900, ffc0, CF_EMAC);
2936 INSN(to_mext, ab00, fbc0, CF_EMAC);
2937 INSN(to_mask, ad00, ffc0, CF_EMAC);
2939 INSN(mov3q, a140, f1c0, CF_ISA_B);
2940 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2941 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2942 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2943 INSN(cmp, b080, f1c0, CF_ISA_A);
2944 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2945 INSN(eor, b180, f1c0, CF_ISA_A);
2946 INSN(and, c000, f000, CF_ISA_A);
2947 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2948 INSN(addsub, d000, f000, CF_ISA_A);
2949 INSN(addx, d180, f1f8, CF_ISA_A);
2950 INSN(adda, d1c0, f1c0, CF_ISA_A);
2951 INSN(shift_im, e080, f0f0, CF_ISA_A);
2952 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2953 INSN(undef_fpu, f000, f000, CF_ISA_A);
2954 INSN(fpu, f200, ffc0, CF_FPU);
2955 INSN(fbcc, f280, ffc0, CF_FPU);
2956 INSN(frestore, f340, ffc0, CF_FPU);
2957 INSN(fsave, f340, ffc0, CF_FPU);
2958 INSN(intouch, f340, ffc0, CF_ISA_A);
2959 INSN(cpushl, f428, ff38, CF_ISA_A);
2960 INSN(wddata, fb00, ff00, CF_ISA_A);
2961 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2965 /* ??? Some of this implementation is not exception safe. We should always
2966 write back the result to memory before setting the condition codes. */
2967 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
2971 insn = cpu_lduw_code(env, s->pc);
2974 opcode_table[insn](env, s, insn);
2977 /* generate intermediate code for basic block 'tb'. */
2978 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
2980 M68kCPU *cpu = m68k_env_get_cpu(env);
2981 CPUState *cs = CPU(cpu);
2982 DisasContext dc1, *dc = &dc1;
2983 target_ulong pc_start;
2988 /* generate intermediate code */
2994 dc->is_jmp = DISAS_NEXT;
2996 dc->cc_op = CC_OP_DYNAMIC;
2997 dc->singlestep_enabled = cs->singlestep_enabled;
2998 dc->fpcr = env->fpcr;
2999 dc->user = (env->sr & SR_S) == 0;
3002 max_insns = tb->cflags & CF_COUNT_MASK;
3003 if (max_insns == 0) {
3004 max_insns = CF_COUNT_MASK;
3006 if (max_insns > TCG_MAX_INSNS) {
3007 max_insns = TCG_MAX_INSNS;
3012 pc_offset = dc->pc - pc_start;
3013 gen_throws_exception = NULL;
3014 tcg_gen_insn_start(dc->pc);
3017 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
3018 gen_exception(dc, dc->pc, EXCP_DEBUG);
3019 dc->is_jmp = DISAS_JUMP;
3020 /* The address covered by the breakpoint must be included in
3021 [tb->pc, tb->pc + tb->size) in order to for it to be
3022 properly cleared -- thus we increment the PC here so that
3023 the logic setting tb->size below does the right thing. */
3028 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
3032 dc->insn_pc = dc->pc;
3033 disas_m68k_insn(env, dc);
3034 } while (!dc->is_jmp && !tcg_op_buf_full() &&
3035 !cs->singlestep_enabled &&
3037 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3038 num_insns < max_insns);
3040 if (tb->cflags & CF_LAST_IO)
3042 if (unlikely(cs->singlestep_enabled)) {
3043 /* Make sure the pc is updated, and raise a debug exception. */
3045 gen_flush_cc_op(dc);
3046 tcg_gen_movi_i32(QREG_PC, dc->pc);
3048 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
3050 switch(dc->is_jmp) {
3052 gen_flush_cc_op(dc);
3053 gen_jmp_tb(dc, 0, dc->pc);
3058 gen_flush_cc_op(dc);
3059 /* indicate that the hash table must be used to find the next TB */
3063 /* nothing more to generate */
3067 gen_tb_end(tb, num_insns);
3070 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
3071 && qemu_log_in_addr_range(pc_start)) {
3072 qemu_log("----------------\n");
3073 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3074 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
3078 tb->size = dc->pc - pc_start;
3079 tb->icount = num_insns;
3082 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
3085 M68kCPU *cpu = M68K_CPU(cs);
3086 CPUM68KState *env = &cpu->env;
3090 for (i = 0; i < 8; i++)
3092 u.d = env->fregs[i];
3093 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3094 i, env->dregs[i], i, env->aregs[i],
3095 i, u.l.upper, u.l.lower, *(double *)&u.d);
3097 cpu_fprintf (f, "PC = %08x ", env->pc);
3099 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3100 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3101 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3102 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3105 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,