4 * Copyright (c) 2005-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
36 //#define DEBUG_DISPATCH 1
38 /* Fake floating point. */
39 #define tcg_gen_mov_f64 tcg_gen_mov_i64
40 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
41 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
43 #define DEFO32(name, offset) static TCGv QREG_##name;
44 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
45 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
51 static TCGv_i32 cpu_halted
;
52 static TCGv_i32 cpu_exception_index
;
54 static TCGv_env cpu_env
;
56 static char cpu_reg_names
[3*8*3 + 5*4];
57 static TCGv cpu_dregs
[8];
58 static TCGv cpu_aregs
[8];
59 static TCGv_i64 cpu_fregs
[8];
60 static TCGv_i64 cpu_macc
[4];
62 #define REG(insn, pos) (((insn) >> (pos)) & 7)
63 #define DREG(insn, pos) cpu_dregs[REG(insn, pos)]
64 #define AREG(insn, pos) cpu_aregs[REG(insn, pos)]
65 #define FREG(insn, pos) cpu_fregs[REG(insn, pos)]
66 #define MACREG(acc) cpu_macc[acc]
67 #define QREG_SP cpu_aregs[7]
69 static TCGv NULL_QREG
;
70 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
71 /* Used to distinguish stores from bad addressing modes. */
72 static TCGv store_dummy
;
74 #include "exec/gen-icount.h"
76 void m68k_tcg_init(void)
81 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
82 tcg_ctx
.tcg_env
= cpu_env
;
84 #define DEFO32(name, offset) \
85 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
86 offsetof(CPUM68KState, offset), #name);
87 #define DEFO64(name, offset) \
88 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
89 offsetof(CPUM68KState, offset), #name);
90 #define DEFF64(name, offset) DEFO64(name, offset)
96 cpu_halted
= tcg_global_mem_new_i32(cpu_env
,
97 -offsetof(M68kCPU
, env
) +
98 offsetof(CPUState
, halted
), "HALTED");
99 cpu_exception_index
= tcg_global_mem_new_i32(cpu_env
,
100 -offsetof(M68kCPU
, env
) +
101 offsetof(CPUState
, exception_index
),
105 for (i
= 0; i
< 8; i
++) {
106 sprintf(p
, "D%d", i
);
107 cpu_dregs
[i
] = tcg_global_mem_new(cpu_env
,
108 offsetof(CPUM68KState
, dregs
[i
]), p
);
110 sprintf(p
, "A%d", i
);
111 cpu_aregs
[i
] = tcg_global_mem_new(cpu_env
,
112 offsetof(CPUM68KState
, aregs
[i
]), p
);
114 sprintf(p
, "F%d", i
);
115 cpu_fregs
[i
] = tcg_global_mem_new_i64(cpu_env
,
116 offsetof(CPUM68KState
, fregs
[i
]), p
);
119 for (i
= 0; i
< 4; i
++) {
120 sprintf(p
, "ACC%d", i
);
121 cpu_macc
[i
] = tcg_global_mem_new_i64(cpu_env
,
122 offsetof(CPUM68KState
, macc
[i
]), p
);
126 NULL_QREG
= tcg_global_mem_new(cpu_env
, -4, "NULL");
127 store_dummy
= tcg_global_mem_new(cpu_env
, -8, "NULL");
130 /* internal defines */
131 typedef struct DisasContext
{
133 target_ulong insn_pc
; /* Start of the current instruction. */
136 CCOp cc_op
; /* Current CC operation */
140 struct TranslationBlock
*tb
;
141 int singlestep_enabled
;
146 #define DISAS_JUMP_NEXT 4
148 #if defined(CONFIG_USER_ONLY)
151 #define IS_USER(s) s->user
154 /* XXX: move that elsewhere */
155 /* ??? Fix exceptions. */
156 static void *gen_throws_exception
;
157 #define gen_last_qop NULL
159 typedef void (*disas_proc
)(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
);
161 #ifdef DEBUG_DISPATCH
162 #define DISAS_INSN(name) \
163 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
165 static void disas_##name(CPUM68KState *env, DisasContext *s, \
168 qemu_log("Dispatch " #name "\n"); \
169 real_disas_##name(env, s, insn); \
171 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
174 #define DISAS_INSN(name) \
175 static void disas_##name(CPUM68KState *env, DisasContext *s, \
179 static const uint8_t cc_op_live
[CC_OP_NB
] = {
180 [CC_OP_FLAGS
] = CCF_C
| CCF_V
| CCF_Z
| CCF_N
| CCF_X
,
181 [CC_OP_ADD
] = CCF_X
| CCF_N
| CCF_V
,
182 [CC_OP_SUB
] = CCF_X
| CCF_N
| CCF_V
,
183 [CC_OP_CMP
] = CCF_X
| CCF_N
| CCF_V
,
184 [CC_OP_LOGIC
] = CCF_X
| CCF_N
187 static void set_cc_op(DisasContext
*s
, CCOp op
)
189 CCOp old_op
= s
->cc_op
;
198 /* Discard CC computation that will no longer be used.
199 Note that X and N are never dead. */
200 dead
= cc_op_live
[old_op
] & ~cc_op_live
[op
];
202 tcg_gen_discard_i32(QREG_CC_C
);
205 tcg_gen_discard_i32(QREG_CC_Z
);
208 tcg_gen_discard_i32(QREG_CC_V
);
212 /* Update the CPU env CC_OP state. */
213 static void update_cc_op(DisasContext
*s
)
215 if (!s
->cc_op_synced
) {
217 tcg_gen_movi_i32(QREG_CC_OP
, s
->cc_op
);
221 /* Generate a load from the specified address. Narrow values are
222 sign extended to full register width. */
223 static inline TCGv
gen_load(DisasContext
* s
, int opsize
, TCGv addr
, int sign
)
226 int index
= IS_USER(s
);
227 tmp
= tcg_temp_new_i32();
231 tcg_gen_qemu_ld8s(tmp
, addr
, index
);
233 tcg_gen_qemu_ld8u(tmp
, addr
, index
);
237 tcg_gen_qemu_ld16s(tmp
, addr
, index
);
239 tcg_gen_qemu_ld16u(tmp
, addr
, index
);
243 tcg_gen_qemu_ld32u(tmp
, addr
, index
);
246 g_assert_not_reached();
248 gen_throws_exception
= gen_last_qop
;
252 static inline TCGv_i64
gen_load64(DisasContext
* s
, TCGv addr
)
255 int index
= IS_USER(s
);
256 tmp
= tcg_temp_new_i64();
257 tcg_gen_qemu_ldf64(tmp
, addr
, index
);
258 gen_throws_exception
= gen_last_qop
;
262 /* Generate a store. */
263 static inline void gen_store(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
)
265 int index
= IS_USER(s
);
268 tcg_gen_qemu_st8(val
, addr
, index
);
271 tcg_gen_qemu_st16(val
, addr
, index
);
275 tcg_gen_qemu_st32(val
, addr
, index
);
278 g_assert_not_reached();
280 gen_throws_exception
= gen_last_qop
;
283 static inline void gen_store64(DisasContext
*s
, TCGv addr
, TCGv_i64 val
)
285 int index
= IS_USER(s
);
286 tcg_gen_qemu_stf64(val
, addr
, index
);
287 gen_throws_exception
= gen_last_qop
;
296 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
297 otherwise generate a store. */
298 static TCGv
gen_ldst(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
,
301 if (what
== EA_STORE
) {
302 gen_store(s
, opsize
, addr
, val
);
305 return gen_load(s
, opsize
, addr
, what
== EA_LOADS
);
309 /* Read a 16-bit immediate constant */
310 static inline uint16_t read_im16(CPUM68KState
*env
, DisasContext
*s
)
313 im
= cpu_lduw_code(env
, s
->pc
);
318 /* Read an 8-bit immediate constant */
319 static inline uint8_t read_im8(CPUM68KState
*env
, DisasContext
*s
)
321 return read_im16(env
, s
);
324 /* Read a 32-bit immediate constant. */
325 static inline uint32_t read_im32(CPUM68KState
*env
, DisasContext
*s
)
328 im
= read_im16(env
, s
) << 16;
329 im
|= 0xffff & read_im16(env
, s
);
333 /* Calculate and address index. */
334 static TCGv
gen_addr_index(uint16_t ext
, TCGv tmp
)
339 add
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(ext
, 12);
340 if ((ext
& 0x800) == 0) {
341 tcg_gen_ext16s_i32(tmp
, add
);
344 scale
= (ext
>> 9) & 3;
346 tcg_gen_shli_i32(tmp
, add
, scale
);
352 /* Handle a base + index + displacement effective addresss.
353 A NULL_QREG base means pc-relative. */
354 static TCGv
gen_lea_indexed(CPUM68KState
*env
, DisasContext
*s
, TCGv base
)
363 ext
= read_im16(env
, s
);
365 if ((ext
& 0x800) == 0 && !m68k_feature(s
->env
, M68K_FEATURE_WORD_INDEX
))
368 if (m68k_feature(s
->env
, M68K_FEATURE_M68000
) &&
369 !m68k_feature(s
->env
, M68K_FEATURE_SCALED_INDEX
)) {
374 /* full extension word format */
375 if (!m68k_feature(s
->env
, M68K_FEATURE_EXT_FULL
))
378 if ((ext
& 0x30) > 0x10) {
379 /* base displacement */
380 if ((ext
& 0x30) == 0x20) {
381 bd
= (int16_t)read_im16(env
, s
);
383 bd
= read_im32(env
, s
);
388 tmp
= tcg_temp_new();
389 if ((ext
& 0x44) == 0) {
391 add
= gen_addr_index(ext
, tmp
);
395 if ((ext
& 0x80) == 0) {
396 /* base not suppressed */
397 if (IS_NULL_QREG(base
)) {
398 base
= tcg_const_i32(offset
+ bd
);
401 if (!IS_NULL_QREG(add
)) {
402 tcg_gen_add_i32(tmp
, add
, base
);
408 if (!IS_NULL_QREG(add
)) {
410 tcg_gen_addi_i32(tmp
, add
, bd
);
414 add
= tcg_const_i32(bd
);
416 if ((ext
& 3) != 0) {
417 /* memory indirect */
418 base
= gen_load(s
, OS_LONG
, add
, 0);
419 if ((ext
& 0x44) == 4) {
420 add
= gen_addr_index(ext
, tmp
);
421 tcg_gen_add_i32(tmp
, add
, base
);
427 /* outer displacement */
428 if ((ext
& 3) == 2) {
429 od
= (int16_t)read_im16(env
, s
);
431 od
= read_im32(env
, s
);
437 tcg_gen_addi_i32(tmp
, add
, od
);
442 /* brief extension word format */
443 tmp
= tcg_temp_new();
444 add
= gen_addr_index(ext
, tmp
);
445 if (!IS_NULL_QREG(base
)) {
446 tcg_gen_add_i32(tmp
, add
, base
);
448 tcg_gen_addi_i32(tmp
, tmp
, (int8_t)ext
);
450 tcg_gen_addi_i32(tmp
, add
, offset
+ (int8_t)ext
);
457 /* Evaluate all the CC flags. */
459 static void gen_flush_flags(DisasContext
*s
)
467 gen_helper_flush_flags(cpu_env
, QREG_CC_OP
);
470 tmp
= tcg_const_i32(s
->cc_op
);
471 gen_helper_flush_flags(cpu_env
, tmp
);
476 /* Note that flush_flags also assigned to env->cc_op. */
477 s
->cc_op
= CC_OP_FLAGS
;
481 /* Sign or zero extend a value. */
483 static inline void gen_ext(TCGv res
, TCGv val
, int opsize
, int sign
)
488 tcg_gen_ext8s_i32(res
, val
);
490 tcg_gen_ext8u_i32(res
, val
);
495 tcg_gen_ext16s_i32(res
, val
);
497 tcg_gen_ext16u_i32(res
, val
);
501 tcg_gen_mov_i32(res
, val
);
504 g_assert_not_reached();
508 static TCGv
gen_extend(TCGv val
, int opsize
, int sign
)
512 if (opsize
== OS_LONG
) {
515 tmp
= tcg_temp_new();
516 gen_ext(tmp
, val
, opsize
, sign
);
522 static void gen_logic_cc(DisasContext
*s
, TCGv val
, int opsize
)
524 gen_ext(QREG_CC_N
, val
, opsize
, 1);
525 set_cc_op(s
, CC_OP_LOGIC
);
528 static void gen_update_cc_add(TCGv dest
, TCGv src
)
530 tcg_gen_mov_i32(QREG_CC_N
, dest
);
531 tcg_gen_mov_i32(QREG_CC_V
, src
);
534 static inline int opsize_bytes(int opsize
)
537 case OS_BYTE
: return 1;
538 case OS_WORD
: return 2;
539 case OS_LONG
: return 4;
540 case OS_SINGLE
: return 4;
541 case OS_DOUBLE
: return 8;
542 case OS_EXTENDED
: return 12;
543 case OS_PACKED
: return 12;
545 g_assert_not_reached();
549 static inline int insn_opsize(int insn
)
551 switch ((insn
>> 6) & 3) {
552 case 0: return OS_BYTE
;
553 case 1: return OS_WORD
;
554 case 2: return OS_LONG
;
556 g_assert_not_reached();
560 /* Assign value to a register. If the width is less than the register width
561 only the low part of the register is set. */
562 static void gen_partset_reg(int opsize
, TCGv reg
, TCGv val
)
567 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
568 tmp
= tcg_temp_new();
569 tcg_gen_ext8u_i32(tmp
, val
);
570 tcg_gen_or_i32(reg
, reg
, tmp
);
573 tcg_gen_andi_i32(reg
, reg
, 0xffff0000);
574 tmp
= tcg_temp_new();
575 tcg_gen_ext16u_i32(tmp
, val
);
576 tcg_gen_or_i32(reg
, reg
, tmp
);
580 tcg_gen_mov_i32(reg
, val
);
583 g_assert_not_reached();
587 /* Generate code for an "effective address". Does not adjust the base
588 register for autoincrement addressing modes. */
589 static TCGv
gen_lea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
597 switch ((insn
>> 3) & 7) {
598 case 0: /* Data register direct. */
599 case 1: /* Address register direct. */
601 case 2: /* Indirect register */
602 case 3: /* Indirect postincrement. */
603 return AREG(insn
, 0);
604 case 4: /* Indirect predecrememnt. */
606 tmp
= tcg_temp_new();
607 tcg_gen_subi_i32(tmp
, reg
, opsize_bytes(opsize
));
609 case 5: /* Indirect displacement. */
611 tmp
= tcg_temp_new();
612 ext
= read_im16(env
, s
);
613 tcg_gen_addi_i32(tmp
, reg
, (int16_t)ext
);
615 case 6: /* Indirect index + displacement. */
617 return gen_lea_indexed(env
, s
, reg
);
620 case 0: /* Absolute short. */
621 offset
= (int16_t)read_im16(env
, s
);
622 return tcg_const_i32(offset
);
623 case 1: /* Absolute long. */
624 offset
= read_im32(env
, s
);
625 return tcg_const_i32(offset
);
626 case 2: /* pc displacement */
628 offset
+= (int16_t)read_im16(env
, s
);
629 return tcg_const_i32(offset
);
630 case 3: /* pc index+displacement. */
631 return gen_lea_indexed(env
, s
, NULL_QREG
);
632 case 4: /* Immediate. */
637 /* Should never happen. */
641 /* Helper function for gen_ea. Reuse the computed address between the
642 for read/write operands. */
643 static inline TCGv
gen_ea_once(CPUM68KState
*env
, DisasContext
*s
,
644 uint16_t insn
, int opsize
, TCGv val
,
645 TCGv
*addrp
, ea_what what
)
649 if (addrp
&& what
== EA_STORE
) {
652 tmp
= gen_lea(env
, s
, insn
, opsize
);
653 if (IS_NULL_QREG(tmp
))
658 return gen_ldst(s
, opsize
, tmp
, val
, what
);
661 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
662 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
663 ADDRP is non-null for readwrite operands. */
664 static TCGv
gen_ea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
665 int opsize
, TCGv val
, TCGv
*addrp
, ea_what what
)
671 switch ((insn
>> 3) & 7) {
672 case 0: /* Data register direct. */
674 if (what
== EA_STORE
) {
675 gen_partset_reg(opsize
, reg
, val
);
678 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
680 case 1: /* Address register direct. */
682 if (what
== EA_STORE
) {
683 tcg_gen_mov_i32(reg
, val
);
686 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
688 case 2: /* Indirect register */
690 return gen_ldst(s
, opsize
, reg
, val
, what
);
691 case 3: /* Indirect postincrement. */
693 result
= gen_ldst(s
, opsize
, reg
, val
, what
);
694 /* ??? This is not exception safe. The instruction may still
695 fault after this point. */
696 if (what
== EA_STORE
|| !addrp
)
697 tcg_gen_addi_i32(reg
, reg
, opsize_bytes(opsize
));
699 case 4: /* Indirect predecrememnt. */
702 if (addrp
&& what
== EA_STORE
) {
705 tmp
= gen_lea(env
, s
, insn
, opsize
);
706 if (IS_NULL_QREG(tmp
))
711 result
= gen_ldst(s
, opsize
, tmp
, val
, what
);
712 /* ??? This is not exception safe. The instruction may still
713 fault after this point. */
714 if (what
== EA_STORE
|| !addrp
) {
716 tcg_gen_mov_i32(reg
, tmp
);
720 case 5: /* Indirect displacement. */
721 case 6: /* Indirect index + displacement. */
722 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
725 case 0: /* Absolute short. */
726 case 1: /* Absolute long. */
727 case 2: /* pc displacement */
728 case 3: /* pc index+displacement. */
729 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
730 case 4: /* Immediate. */
731 /* Sign extend values for consistency. */
734 if (what
== EA_LOADS
) {
735 offset
= (int8_t)read_im8(env
, s
);
737 offset
= read_im8(env
, s
);
741 if (what
== EA_LOADS
) {
742 offset
= (int16_t)read_im16(env
, s
);
744 offset
= read_im16(env
, s
);
748 offset
= read_im32(env
, s
);
751 g_assert_not_reached();
753 return tcg_const_i32(offset
);
758 /* Should never happen. */
762 /* This generates a conditional branch, clobbering all temporaries. */
763 static void gen_jmpcc(DisasContext
*s
, int cond
, TCGLabel
*l1
)
768 /* TODO: Optimize compare/branch pairs rather than always flushing
769 flag state to CC_OP_FLAGS. */
778 case 2: /* HI (!C && !Z) -> !(C || Z)*/
779 case 3: /* LS (C || Z) */
780 tmp
= tcg_temp_new();
781 tcg_gen_setcondi_i32(TCG_COND_EQ
, tmp
, QREG_CC_Z
, 0);
782 tcg_gen_or_i32(tmp
, tmp
, QREG_CC_C
);
783 tcond
= (cond
& 1 ? TCG_COND_NE
: TCG_COND_EQ
);
785 case 4: /* CC (!C) */
788 tcond
= (cond
& 1 ? TCG_COND_NE
: TCG_COND_EQ
);
790 case 6: /* NE (!Z) */
793 tcond
= (cond
& 1 ? TCG_COND_EQ
: TCG_COND_NE
);
795 case 8: /* VC (!V) */
798 tcond
= (cond
& 1 ? TCG_COND_LT
: TCG_COND_GE
);
800 case 10: /* PL (!N) */
801 case 11: /* MI (N) */
803 tcond
= (cond
& 1 ? TCG_COND_LT
: TCG_COND_GE
);
805 case 12: /* GE (!(N ^ V)) */
806 case 13: /* LT (N ^ V) */
807 tmp
= tcg_temp_new();
808 tcg_gen_xor_i32(tmp
, QREG_CC_N
, QREG_CC_V
);
809 tcond
= (cond
& 1 ? TCG_COND_LT
: TCG_COND_GE
);
811 case 14: /* GT (!(Z || (N ^ V))) */
812 case 15: /* LE (Z || (N ^ V)) */
813 tmp
= tcg_temp_new();
814 tcg_gen_setcondi_i32(TCG_COND_EQ
, tmp
, QREG_CC_Z
, 0);
815 tcg_gen_neg_i32(tmp
, tmp
);
816 tmp2
= tcg_temp_new();
817 tcg_gen_xor_i32(tmp2
, QREG_CC_N
, QREG_CC_V
);
818 tcg_gen_or_i32(tmp
, tmp
, tmp2
);
819 tcond
= (cond
& 1 ? TCG_COND_LT
: TCG_COND_GE
);
822 /* Should ever happen. */
825 tcg_gen_brcondi_i32(tcond
, tmp
, 0, l1
);
834 l1
= gen_new_label();
835 cond
= (insn
>> 8) & 0xf;
837 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
838 /* This is safe because we modify the reg directly, with no other values
840 gen_jmpcc(s
, cond
^ 1, l1
);
841 tcg_gen_ori_i32(reg
, reg
, 0xff);
845 /* Force a TB lookup after an instruction that changes the CPU state. */
846 static void gen_lookup_tb(DisasContext
*s
)
849 tcg_gen_movi_i32(QREG_PC
, s
->pc
);
850 s
->is_jmp
= DISAS_UPDATE
;
853 /* Generate a jump to an immediate address. */
854 static void gen_jmp_im(DisasContext
*s
, uint32_t dest
)
857 tcg_gen_movi_i32(QREG_PC
, dest
);
858 s
->is_jmp
= DISAS_JUMP
;
861 /* Generate a jump to the address in qreg DEST. */
862 static void gen_jmp(DisasContext
*s
, TCGv dest
)
865 tcg_gen_mov_i32(QREG_PC
, dest
);
866 s
->is_jmp
= DISAS_JUMP
;
869 static void gen_exception(DisasContext
*s
, uint32_t where
, int nr
)
872 gen_jmp_im(s
, where
);
873 gen_helper_raise_exception(cpu_env
, tcg_const_i32(nr
));
876 static inline void gen_addr_fault(DisasContext
*s
)
878 gen_exception(s
, s
->insn_pc
, EXCP_ADDRESS
);
881 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
882 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
883 op_sign ? EA_LOADS : EA_LOADU); \
884 if (IS_NULL_QREG(result)) { \
890 #define DEST_EA(env, insn, opsize, val, addrp) do { \
891 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
892 if (IS_NULL_QREG(ea_result)) { \
898 static inline bool use_goto_tb(DisasContext
*s
, uint32_t dest
)
900 #ifndef CONFIG_USER_ONLY
901 return (s
->tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
) ||
902 (s
->insn_pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
);
908 /* Generate a jump to an immediate address. */
909 static void gen_jmp_tb(DisasContext
*s
, int n
, uint32_t dest
)
911 if (unlikely(s
->singlestep_enabled
)) {
912 gen_exception(s
, dest
, EXCP_DEBUG
);
913 } else if (use_goto_tb(s
, dest
)) {
915 tcg_gen_movi_i32(QREG_PC
, dest
);
916 tcg_gen_exit_tb((uintptr_t)s
->tb
+ n
);
921 s
->is_jmp
= DISAS_TB_JUMP
;
924 DISAS_INSN(undef_mac
)
926 gen_exception(s
, s
->pc
- 2, EXCP_LINEA
);
929 DISAS_INSN(undef_fpu
)
931 gen_exception(s
, s
->pc
- 2, EXCP_LINEF
);
936 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
938 gen_exception(s
, s
->pc
- 2, EXCP_UNSUPPORTED
);
939 cpu_abort(CPU(cpu
), "Illegal instruction: %04x @ %08x", insn
, s
->pc
- 2);
949 sign
= (insn
& 0x100) != 0;
951 tmp
= tcg_temp_new();
953 tcg_gen_ext16s_i32(tmp
, reg
);
955 tcg_gen_ext16u_i32(tmp
, reg
);
956 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
957 tcg_gen_mul_i32(tmp
, tmp
, src
);
958 tcg_gen_mov_i32(reg
, tmp
);
959 gen_logic_cc(s
, tmp
, OS_WORD
);
969 sign
= (insn
& 0x100) != 0;
972 tcg_gen_ext16s_i32(QREG_DIV1
, reg
);
974 tcg_gen_ext16u_i32(QREG_DIV1
, reg
);
976 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
977 tcg_gen_mov_i32(QREG_DIV2
, src
);
979 gen_helper_divs(cpu_env
, tcg_const_i32(1));
981 gen_helper_divu(cpu_env
, tcg_const_i32(1));
984 tmp
= tcg_temp_new();
985 src
= tcg_temp_new();
986 tcg_gen_ext16u_i32(tmp
, QREG_DIV1
);
987 tcg_gen_shli_i32(src
, QREG_DIV2
, 16);
988 tcg_gen_or_i32(reg
, tmp
, src
);
990 set_cc_op(s
, CC_OP_FLAGS
);
1000 ext
= read_im16(env
, s
);
1002 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
1005 num
= DREG(ext
, 12);
1007 tcg_gen_mov_i32(QREG_DIV1
, num
);
1008 SRC_EA(env
, den
, OS_LONG
, 0, NULL
);
1009 tcg_gen_mov_i32(QREG_DIV2
, den
);
1011 gen_helper_divs(cpu_env
, tcg_const_i32(0));
1013 gen_helper_divu(cpu_env
, tcg_const_i32(0));
1015 if ((ext
& 7) == ((ext
>> 12) & 7)) {
1017 tcg_gen_mov_i32 (reg
, QREG_DIV1
);
1020 tcg_gen_mov_i32 (reg
, QREG_DIV2
);
1022 set_cc_op(s
, CC_OP_FLAGS
);
1034 add
= (insn
& 0x4000) != 0;
1035 reg
= DREG(insn
, 9);
1036 dest
= tcg_temp_new();
1038 SRC_EA(env
, tmp
, OS_LONG
, 0, &addr
);
1042 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1045 tcg_gen_add_i32(dest
, tmp
, src
);
1046 tcg_gen_setcond_i32(TCG_COND_LTU
, QREG_CC_X
, dest
, src
);
1047 set_cc_op(s
, CC_OP_ADD
);
1049 tcg_gen_setcond_i32(TCG_COND_LTU
, QREG_CC_X
, tmp
, src
);
1050 tcg_gen_sub_i32(dest
, tmp
, src
);
1051 set_cc_op(s
, CC_OP_SUB
);
1053 gen_update_cc_add(dest
, src
);
1055 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1057 tcg_gen_mov_i32(reg
, dest
);
1062 /* Reverse the order of the bits in REG. */
1066 reg
= DREG(insn
, 0);
1067 gen_helper_bitrev(reg
, reg
);
1070 DISAS_INSN(bitop_reg
)
1080 if ((insn
& 0x38) != 0)
1084 op
= (insn
>> 6) & 3;
1088 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1089 src2
= DREG(insn
, 9);
1090 dest
= tcg_temp_new();
1092 tmp
= tcg_temp_new();
1093 if (opsize
== OS_BYTE
)
1094 tcg_gen_andi_i32(tmp
, src2
, 7);
1096 tcg_gen_andi_i32(tmp
, src2
, 31);
1098 src2
= tcg_const_i32(1);
1099 tcg_gen_shl_i32(src2
, src2
, tmp
);
1102 tcg_gen_and_i32(QREG_CC_Z
, src1
, src2
);
1106 tcg_gen_xor_i32(dest
, src1
, src2
);
1109 tcg_gen_andc_i32(dest
, src1
, src2
);
1112 tcg_gen_or_i32(dest
, src1
, src2
);
1117 tcg_temp_free(src2
);
1119 DEST_EA(env
, insn
, opsize
, dest
, &addr
);
1121 tcg_temp_free(dest
);
1127 reg
= DREG(insn
, 0);
1129 gen_helper_sats(reg
, reg
, QREG_CC_V
);
1130 gen_logic_cc(s
, reg
, OS_LONG
);
1133 static void gen_push(DisasContext
*s
, TCGv val
)
1137 tmp
= tcg_temp_new();
1138 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1139 gen_store(s
, OS_LONG
, tmp
, val
);
1140 tcg_gen_mov_i32(QREG_SP
, tmp
);
1152 mask
= read_im16(env
, s
);
1153 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1154 if (IS_NULL_QREG(tmp
)) {
1158 addr
= tcg_temp_new();
1159 tcg_gen_mov_i32(addr
, tmp
);
1160 is_load
= ((insn
& 0x0400) != 0);
1161 for (i
= 0; i
< 16; i
++, mask
>>= 1) {
1168 tmp
= gen_load(s
, OS_LONG
, addr
, 0);
1169 tcg_gen_mov_i32(reg
, tmp
);
1171 gen_store(s
, OS_LONG
, addr
, reg
);
1174 tcg_gen_addi_i32(addr
, addr
, 4);
1179 DISAS_INSN(bitop_im
)
1189 if ((insn
& 0x38) != 0)
1193 op
= (insn
>> 6) & 3;
1195 bitnum
= read_im16(env
, s
);
1196 if (bitnum
& 0xff00) {
1197 disas_undef(env
, s
, insn
);
1203 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1205 if (opsize
== OS_BYTE
)
1211 tcg_gen_andi_i32(QREG_CC_Z
, src1
, mask
);
1214 tmp
= tcg_temp_new();
1217 tcg_gen_xori_i32(tmp
, src1
, mask
);
1220 tcg_gen_andi_i32(tmp
, src1
, ~mask
);
1223 tcg_gen_ori_i32(tmp
, src1
, mask
);
1228 DEST_EA(env
, insn
, opsize
, tmp
, &addr
);
1233 DISAS_INSN(arith_im
)
1241 op
= (insn
>> 9) & 7;
1242 SRC_EA(env
, src1
, OS_LONG
, 0, (op
== 6) ? NULL
: &addr
);
1243 im
= read_im32(env
, s
);
1244 dest
= tcg_temp_new();
1247 tcg_gen_ori_i32(dest
, src1
, im
);
1248 gen_logic_cc(s
, dest
, OS_LONG
);
1251 tcg_gen_andi_i32(dest
, src1
, im
);
1252 gen_logic_cc(s
, dest
, OS_LONG
);
1255 tcg_gen_mov_i32(dest
, src1
);
1256 tcg_gen_setcondi_i32(TCG_COND_LTU
, QREG_CC_X
, dest
, im
);
1257 tcg_gen_subi_i32(dest
, dest
, im
);
1258 gen_update_cc_add(dest
, tcg_const_i32(im
));
1259 set_cc_op(s
, CC_OP_SUB
);
1262 tcg_gen_mov_i32(dest
, src1
);
1263 tcg_gen_addi_i32(dest
, dest
, im
);
1264 gen_update_cc_add(dest
, tcg_const_i32(im
));
1265 tcg_gen_setcondi_i32(TCG_COND_LTU
, QREG_CC_X
, dest
, im
);
1266 set_cc_op(s
, CC_OP_ADD
);
1269 tcg_gen_xori_i32(dest
, src1
, im
);
1270 gen_logic_cc(s
, dest
, OS_LONG
);
1273 gen_update_cc_add(src1
, tcg_const_i32(im
));
1274 set_cc_op(s
, CC_OP_CMP
);
1280 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1288 reg
= DREG(insn
, 0);
1289 tcg_gen_bswap32_i32(reg
, reg
);
1299 switch (insn
>> 12) {
1300 case 1: /* move.b */
1303 case 2: /* move.l */
1306 case 3: /* move.w */
1312 SRC_EA(env
, src
, opsize
, 1, NULL
);
1313 op
= (insn
>> 6) & 7;
1316 /* The value will already have been sign extended. */
1317 dest
= AREG(insn
, 9);
1318 tcg_gen_mov_i32(dest
, src
);
1322 dest_ea
= ((insn
>> 9) & 7) | (op
<< 3);
1323 DEST_EA(env
, dest_ea
, opsize
, src
, NULL
);
1324 /* This will be correct because loads sign extend. */
1325 gen_logic_cc(s
, src
, opsize
);
1334 reg
= DREG(insn
, 0);
1335 gen_helper_subx_cc(reg
, cpu_env
, tcg_const_i32(0), reg
);
1343 reg
= AREG(insn
, 9);
1344 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1345 if (IS_NULL_QREG(tmp
)) {
1349 tcg_gen_mov_i32(reg
, tmp
);
1356 opsize
= insn_opsize(insn
);
1357 DEST_EA(env
, insn
, opsize
, tcg_const_i32(0), NULL
);
1358 gen_logic_cc(s
, tcg_const_i32(0), opsize
);
1361 static TCGv
gen_get_ccr(DisasContext
*s
)
1367 dest
= tcg_temp_new();
1368 gen_helper_get_ccr(dest
, cpu_env
);
1372 DISAS_INSN(move_from_ccr
)
1376 ccr
= gen_get_ccr(s
);
1377 DEST_EA(env
, insn
, OS_WORD
, ccr
, NULL
);
1385 reg
= DREG(insn
, 0);
1386 src1
= tcg_temp_new();
1387 tcg_gen_mov_i32(src1
, reg
);
1388 tcg_gen_neg_i32(reg
, src1
);
1389 gen_update_cc_add(reg
, src1
);
1390 tcg_gen_setcondi_i32(TCG_COND_NE
, QREG_CC_X
, src1
, 0);
1391 set_cc_op(s
, CC_OP_SUB
);
1394 static void gen_set_sr_im(DisasContext
*s
, uint16_t val
, int ccr_only
)
1397 tcg_gen_movi_i32(QREG_CC_C
, val
& CCF_C
? 1 : 0);
1398 tcg_gen_movi_i32(QREG_CC_V
, val
& CCF_V
? -1 : 0);
1399 tcg_gen_movi_i32(QREG_CC_Z
, val
& CCF_Z
? 0 : 1);
1400 tcg_gen_movi_i32(QREG_CC_N
, val
& CCF_N
? -1 : 0);
1401 tcg_gen_movi_i32(QREG_CC_X
, val
& CCF_X
? 1 : 0);
1403 gen_helper_set_sr(cpu_env
, tcg_const_i32(val
));
1405 set_cc_op(s
, CC_OP_FLAGS
);
1408 static void gen_set_sr(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
1411 if ((insn
& 0x38) == 0) {
1413 gen_helper_set_ccr(cpu_env
, DREG(insn
, 0));
1415 gen_helper_set_sr(cpu_env
, DREG(insn
, 0));
1417 set_cc_op(s
, CC_OP_FLAGS
);
1418 } else if ((insn
& 0x3f) == 0x3c) {
1420 val
= read_im16(env
, s
);
1421 gen_set_sr_im(s
, val
, ccr_only
);
1423 disas_undef(env
, s
, insn
);
1428 DISAS_INSN(move_to_ccr
)
1430 gen_set_sr(env
, s
, insn
, 1);
1437 reg
= DREG(insn
, 0);
1438 tcg_gen_not_i32(reg
, reg
);
1439 gen_logic_cc(s
, reg
, OS_LONG
);
1448 src1
= tcg_temp_new();
1449 src2
= tcg_temp_new();
1450 reg
= DREG(insn
, 0);
1451 tcg_gen_shli_i32(src1
, reg
, 16);
1452 tcg_gen_shri_i32(src2
, reg
, 16);
1453 tcg_gen_or_i32(reg
, src1
, src2
);
1454 gen_logic_cc(s
, reg
, OS_LONG
);
1461 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1462 if (IS_NULL_QREG(tmp
)) {
1475 reg
= DREG(insn
, 0);
1476 op
= (insn
>> 6) & 7;
1477 tmp
= tcg_temp_new();
1479 tcg_gen_ext16s_i32(tmp
, reg
);
1481 tcg_gen_ext8s_i32(tmp
, reg
);
1483 gen_partset_reg(OS_WORD
, reg
, tmp
);
1485 tcg_gen_mov_i32(reg
, tmp
);
1486 gen_logic_cc(s
, tmp
, OS_LONG
);
1494 opsize
= insn_opsize(insn
);
1495 SRC_EA(env
, tmp
, opsize
, 1, NULL
);
1496 gen_logic_cc(s
, tmp
, opsize
);
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
, OS_BYTE
);
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
= read_im16(env
, s
);
1534 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
1537 reg
= DREG(ext
, 12);
1538 SRC_EA(env
, src1
, OS_LONG
, 0, NULL
);
1539 dest
= tcg_temp_new();
1540 tcg_gen_mul_i32(dest
, src1
, reg
);
1541 tcg_gen_mov_i32(reg
, dest
);
1542 /* Unlike m68k, coldfire always clears the overflow bit. */
1543 gen_logic_cc(s
, dest
, OS_LONG
);
1552 offset
= cpu_ldsw_code(env
, s
->pc
);
1554 reg
= AREG(insn
, 0);
1555 tmp
= tcg_temp_new();
1556 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1557 gen_store(s
, OS_LONG
, tmp
, reg
);
1558 if ((insn
& 7) != 7)
1559 tcg_gen_mov_i32(reg
, tmp
);
1560 tcg_gen_addi_i32(QREG_SP
, tmp
, offset
);
1569 src
= tcg_temp_new();
1570 reg
= AREG(insn
, 0);
1571 tcg_gen_mov_i32(src
, reg
);
1572 tmp
= gen_load(s
, OS_LONG
, src
, 0);
1573 tcg_gen_mov_i32(reg
, tmp
);
1574 tcg_gen_addi_i32(QREG_SP
, src
, 4);
1585 tmp
= gen_load(s
, OS_LONG
, QREG_SP
, 0);
1586 tcg_gen_addi_i32(QREG_SP
, QREG_SP
, 4);
1594 /* Load the target address first to ensure correct exception
1596 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1597 if (IS_NULL_QREG(tmp
)) {
1601 if ((insn
& 0x40) == 0) {
1603 gen_push(s
, tcg_const_i32(s
->pc
));
1616 SRC_EA(env
, src1
, OS_LONG
, 0, &addr
);
1617 val
= (insn
>> 9) & 7;
1620 dest
= tcg_temp_new();
1621 tcg_gen_mov_i32(dest
, src1
);
1622 if ((insn
& 0x38) == 0x08) {
1623 /* Don't update condition codes if the destination is an
1624 address register. */
1625 if (insn
& 0x0100) {
1626 tcg_gen_subi_i32(dest
, dest
, val
);
1628 tcg_gen_addi_i32(dest
, dest
, val
);
1631 src2
= tcg_const_i32(val
);
1632 if (insn
& 0x0100) {
1633 tcg_gen_setcond_i32(TCG_COND_LTU
, QREG_CC_X
, dest
, src2
);
1634 tcg_gen_sub_i32(dest
, dest
, src2
);
1635 set_cc_op(s
, CC_OP_SUB
);
1637 tcg_gen_add_i32(dest
, dest
, src2
);
1638 tcg_gen_setcond_i32(TCG_COND_LTU
, QREG_CC_X
, dest
, src2
);
1639 set_cc_op(s
, CC_OP_ADD
);
1641 gen_update_cc_add(dest
, src2
);
1643 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1649 case 2: /* One extension word. */
1652 case 3: /* Two extension words. */
1655 case 4: /* No extension words. */
1658 disas_undef(env
, s
, insn
);
1670 op
= (insn
>> 8) & 0xf;
1671 offset
= (int8_t)insn
;
1673 offset
= (int16_t)read_im16(env
, s
);
1674 } else if (offset
== -1) {
1675 offset
= read_im32(env
, s
);
1679 gen_push(s
, tcg_const_i32(s
->pc
));
1684 l1
= gen_new_label();
1685 gen_jmpcc(s
, ((insn
>> 8) & 0xf) ^ 1, l1
);
1686 gen_jmp_tb(s
, 1, base
+ offset
);
1688 gen_jmp_tb(s
, 0, s
->pc
);
1690 /* Unconditional branch. */
1691 gen_jmp_tb(s
, 0, base
+ offset
);
1700 tcg_gen_movi_i32(DREG(insn
, 9), val
);
1701 gen_logic_cc(s
, tcg_const_i32(val
), OS_LONG
);
1714 SRC_EA(env
, src
, opsize
, (insn
& 0x80) == 0, NULL
);
1715 reg
= DREG(insn
, 9);
1716 tcg_gen_mov_i32(reg
, src
);
1717 gen_logic_cc(s
, src
, opsize
);
1727 reg
= DREG(insn
, 9);
1728 dest
= tcg_temp_new();
1730 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1731 tcg_gen_or_i32(dest
, src
, reg
);
1732 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1734 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1735 tcg_gen_or_i32(dest
, src
, reg
);
1736 tcg_gen_mov_i32(reg
, dest
);
1738 gen_logic_cc(s
, dest
, OS_LONG
);
1746 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1747 reg
= AREG(insn
, 9);
1748 tcg_gen_sub_i32(reg
, reg
, src
);
1757 reg
= DREG(insn
, 9);
1758 src
= DREG(insn
, 0);
1759 gen_helper_subx_cc(reg
, cpu_env
, reg
, src
);
1767 val
= (insn
>> 9) & 7;
1770 src
= tcg_const_i32(val
);
1771 gen_logic_cc(s
, src
, OS_LONG
);
1772 DEST_EA(env
, insn
, OS_LONG
, src
, NULL
);
1781 opsize
= insn_opsize(insn
);
1782 SRC_EA(env
, src
, opsize
, -1, NULL
);
1783 reg
= DREG(insn
, 9);
1784 gen_update_cc_add(reg
, src
);
1785 set_cc_op(s
, CC_OP_CMP
);
1799 SRC_EA(env
, src
, opsize
, 1, NULL
);
1800 reg
= AREG(insn
, 9);
1801 gen_update_cc_add(reg
, src
);
1802 set_cc_op(s
, CC_OP_CMP
);
1812 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1813 reg
= DREG(insn
, 9);
1814 dest
= tcg_temp_new();
1815 tcg_gen_xor_i32(dest
, src
, reg
);
1816 gen_logic_cc(s
, dest
, OS_LONG
);
1817 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1827 reg
= DREG(insn
, 9);
1828 dest
= tcg_temp_new();
1830 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1831 tcg_gen_and_i32(dest
, src
, reg
);
1832 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1834 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1835 tcg_gen_and_i32(dest
, src
, reg
);
1836 tcg_gen_mov_i32(reg
, dest
);
1838 gen_logic_cc(s
, dest
, OS_LONG
);
1846 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1847 reg
= AREG(insn
, 9);
1848 tcg_gen_add_i32(reg
, reg
, src
);
1857 reg
= DREG(insn
, 9);
1858 src
= DREG(insn
, 0);
1859 gen_helper_addx_cc(reg
, cpu_env
, reg
, src
);
1862 /* TODO: This could be implemented without helper functions. */
1863 DISAS_INSN(shift_im
)
1869 set_cc_op(s
, CC_OP_FLAGS
);
1871 reg
= DREG(insn
, 0);
1872 tmp
= (insn
>> 9) & 7;
1875 shift
= tcg_const_i32(tmp
);
1876 /* No need to flush flags becuse we know we will set C flag. */
1878 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1881 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1883 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1888 DISAS_INSN(shift_reg
)
1893 reg
= DREG(insn
, 0);
1894 shift
= DREG(insn
, 9);
1896 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1899 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1901 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1904 set_cc_op(s
, CC_OP_FLAGS
);
1910 reg
= DREG(insn
, 0);
1911 gen_logic_cc(s
, reg
, OS_LONG
);
1912 gen_helper_ff1(reg
, reg
);
1915 static TCGv
gen_get_sr(DisasContext
*s
)
1920 ccr
= gen_get_ccr(s
);
1921 sr
= tcg_temp_new();
1922 tcg_gen_andi_i32(sr
, QREG_SR
, 0xffe0);
1923 tcg_gen_or_i32(sr
, sr
, ccr
);
1933 ext
= read_im16(env
, s
);
1934 if (ext
!= 0x46FC) {
1935 gen_exception(s
, addr
, EXCP_UNSUPPORTED
);
1938 ext
= read_im16(env
, s
);
1939 if (IS_USER(s
) || (ext
& SR_S
) == 0) {
1940 gen_exception(s
, addr
, EXCP_PRIVILEGE
);
1943 gen_push(s
, gen_get_sr(s
));
1944 gen_set_sr_im(s
, ext
, 0);
1947 DISAS_INSN(move_from_sr
)
1951 if (IS_USER(s
) && !m68k_feature(env
, M68K_FEATURE_M68000
)) {
1952 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1956 DEST_EA(env
, insn
, OS_WORD
, sr
, NULL
);
1959 DISAS_INSN(move_to_sr
)
1962 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1965 gen_set_sr(env
, s
, insn
, 0);
1969 DISAS_INSN(move_from_usp
)
1972 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1975 tcg_gen_ld_i32(AREG(insn
, 0), cpu_env
,
1976 offsetof(CPUM68KState
, sp
[M68K_USP
]));
1979 DISAS_INSN(move_to_usp
)
1982 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1985 tcg_gen_st_i32(AREG(insn
, 0), cpu_env
,
1986 offsetof(CPUM68KState
, sp
[M68K_USP
]));
1991 gen_exception(s
, s
->pc
, EXCP_HALT_INSN
);
1999 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2003 ext
= read_im16(env
, s
);
2005 gen_set_sr_im(s
, ext
, 0);
2006 tcg_gen_movi_i32(cpu_halted
, 1);
2007 gen_exception(s
, s
->pc
, EXCP_HLT
);
2013 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2016 gen_exception(s
, s
->pc
- 2, EXCP_RTE
);
2025 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2029 ext
= read_im16(env
, s
);
2032 reg
= AREG(ext
, 12);
2034 reg
= DREG(ext
, 12);
2036 gen_helper_movec(cpu_env
, tcg_const_i32(ext
& 0xfff), reg
);
2043 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2046 /* ICache fetch. Implement as no-op. */
2052 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2055 /* Cache push/invalidate. Implement as no-op. */
2060 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2065 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2068 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2071 /* TODO: Implement wdebug. */
2072 cpu_abort(CPU(cpu
), "WDEBUG not implemented");
2077 gen_exception(s
, s
->pc
- 2, EXCP_TRAP0
+ (insn
& 0xf));
2080 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2081 immediately before the next FP instruction is executed. */
2095 ext
= read_im16(env
, s
);
2096 opmode
= ext
& 0x7f;
2097 switch ((ext
>> 13) & 7) {
2102 case 3: /* fmove out */
2104 tmp32
= tcg_temp_new_i32();
2106 /* ??? TODO: Proper behavior on overflow. */
2107 switch ((ext
>> 10) & 7) {
2110 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2114 gen_helper_f64_to_f32(tmp32
, cpu_env
, src
);
2118 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2120 case 5: /* OS_DOUBLE */
2121 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2122 switch ((insn
>> 3) & 7) {
2127 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2130 offset
= cpu_ldsw_code(env
, s
->pc
);
2132 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2137 gen_store64(s
, tmp32
, src
);
2138 switch ((insn
>> 3) & 7) {
2140 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2141 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2144 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2147 tcg_temp_free_i32(tmp32
);
2151 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2156 DEST_EA(env
, insn
, opsize
, tmp32
, NULL
);
2157 tcg_temp_free_i32(tmp32
);
2159 case 4: /* fmove to control register. */
2160 switch ((ext
>> 10) & 7) {
2162 /* Not implemented. Ignore writes. */
2167 cpu_abort(NULL
, "Unimplemented: fmove to control %d",
2171 case 5: /* fmove from control register. */
2172 switch ((ext
>> 10) & 7) {
2174 /* Not implemented. Always return zero. */
2175 tmp32
= tcg_const_i32(0);
2180 cpu_abort(NULL
, "Unimplemented: fmove from control %d",
2184 DEST_EA(env
, insn
, OS_LONG
, tmp32
, NULL
);
2186 case 6: /* fmovem */
2192 if ((ext
& 0x1f00) != 0x1000 || (ext
& 0xff) == 0)
2194 tmp32
= gen_lea(env
, s
, insn
, OS_LONG
);
2195 if (IS_NULL_QREG(tmp32
)) {
2199 addr
= tcg_temp_new_i32();
2200 tcg_gen_mov_i32(addr
, tmp32
);
2202 for (i
= 0; i
< 8; i
++) {
2205 if (ext
& (1 << 13)) {
2207 tcg_gen_qemu_stf64(dest
, addr
, IS_USER(s
));
2210 tcg_gen_qemu_ldf64(dest
, addr
, IS_USER(s
));
2212 if (ext
& (mask
- 1))
2213 tcg_gen_addi_i32(addr
, addr
, 8);
2217 tcg_temp_free_i32(addr
);
2221 if (ext
& (1 << 14)) {
2222 /* Source effective address. */
2223 switch ((ext
>> 10) & 7) {
2224 case 0: opsize
= OS_LONG
; break;
2225 case 1: opsize
= OS_SINGLE
; break;
2226 case 4: opsize
= OS_WORD
; break;
2227 case 5: opsize
= OS_DOUBLE
; break;
2228 case 6: opsize
= OS_BYTE
; break;
2232 if (opsize
== OS_DOUBLE
) {
2233 tmp32
= tcg_temp_new_i32();
2234 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2235 switch ((insn
>> 3) & 7) {
2240 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2243 offset
= cpu_ldsw_code(env
, s
->pc
);
2245 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2248 offset
= cpu_ldsw_code(env
, s
->pc
);
2249 offset
+= s
->pc
- 2;
2251 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2256 src
= gen_load64(s
, tmp32
);
2257 switch ((insn
>> 3) & 7) {
2259 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2260 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2263 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2266 tcg_temp_free_i32(tmp32
);
2268 SRC_EA(env
, tmp32
, opsize
, 1, NULL
);
2269 src
= tcg_temp_new_i64();
2274 gen_helper_i32_to_f64(src
, cpu_env
, tmp32
);
2277 gen_helper_f32_to_f64(src
, cpu_env
, tmp32
);
2282 /* Source register. */
2283 src
= FREG(ext
, 10);
2285 dest
= FREG(ext
, 7);
2286 res
= tcg_temp_new_i64();
2288 tcg_gen_mov_f64(res
, dest
);
2292 case 0: case 0x40: case 0x44: /* fmove */
2293 tcg_gen_mov_f64(res
, src
);
2296 gen_helper_iround_f64(res
, cpu_env
, src
);
2299 case 3: /* fintrz */
2300 gen_helper_itrunc_f64(res
, cpu_env
, src
);
2303 case 4: case 0x41: case 0x45: /* fsqrt */
2304 gen_helper_sqrt_f64(res
, cpu_env
, src
);
2306 case 0x18: case 0x58: case 0x5c: /* fabs */
2307 gen_helper_abs_f64(res
, src
);
2309 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2310 gen_helper_chs_f64(res
, src
);
2312 case 0x20: case 0x60: case 0x64: /* fdiv */
2313 gen_helper_div_f64(res
, cpu_env
, res
, src
);
2315 case 0x22: case 0x62: case 0x66: /* fadd */
2316 gen_helper_add_f64(res
, cpu_env
, res
, src
);
2318 case 0x23: case 0x63: case 0x67: /* fmul */
2319 gen_helper_mul_f64(res
, cpu_env
, res
, src
);
2321 case 0x28: case 0x68: case 0x6c: /* fsub */
2322 gen_helper_sub_f64(res
, cpu_env
, res
, src
);
2324 case 0x38: /* fcmp */
2325 gen_helper_sub_cmp_f64(res
, cpu_env
, res
, src
);
2329 case 0x3a: /* ftst */
2330 tcg_gen_mov_f64(res
, src
);
2337 if (ext
& (1 << 14)) {
2338 tcg_temp_free_i64(src
);
2341 if (opmode
& 0x40) {
2342 if ((opmode
& 0x4) != 0)
2344 } else if ((s
->fpcr
& M68K_FPCR_PREC
) == 0) {
2349 TCGv tmp
= tcg_temp_new_i32();
2350 gen_helper_f64_to_f32(tmp
, cpu_env
, res
);
2351 gen_helper_f32_to_f64(res
, cpu_env
, tmp
);
2352 tcg_temp_free_i32(tmp
);
2354 tcg_gen_mov_f64(QREG_FP_RESULT
, res
);
2356 tcg_gen_mov_f64(dest
, res
);
2358 tcg_temp_free_i64(res
);
2361 /* FIXME: Is this right for offset addressing modes? */
2363 disas_undef_fpu(env
, s
, insn
);
2374 offset
= cpu_ldsw_code(env
, s
->pc
);
2376 if (insn
& (1 << 6)) {
2377 offset
= (offset
<< 16) | read_im16(env
, s
);
2380 l1
= gen_new_label();
2381 /* TODO: Raise BSUN exception. */
2382 flag
= tcg_temp_new();
2383 gen_helper_compare_f64(flag
, cpu_env
, QREG_FP_RESULT
);
2384 /* Jump to l1 if condition is true. */
2385 switch (insn
& 0xf) {
2388 case 1: /* eq (=0) */
2389 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2391 case 2: /* ogt (=1) */
2392 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(1), l1
);
2394 case 3: /* oge (=0 or =1) */
2395 tcg_gen_brcond_i32(TCG_COND_LEU
, flag
, tcg_const_i32(1), l1
);
2397 case 4: /* olt (=-1) */
2398 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(0), l1
);
2400 case 5: /* ole (=-1 or =0) */
2401 tcg_gen_brcond_i32(TCG_COND_LE
, flag
, tcg_const_i32(0), l1
);
2403 case 6: /* ogl (=-1 or =1) */
2404 tcg_gen_andi_i32(flag
, flag
, 1);
2405 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2407 case 7: /* or (=2) */
2408 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(2), l1
);
2410 case 8: /* un (<2) */
2411 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(2), l1
);
2413 case 9: /* ueq (=0 or =2) */
2414 tcg_gen_andi_i32(flag
, flag
, 1);
2415 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2417 case 10: /* ugt (>0) */
2418 tcg_gen_brcond_i32(TCG_COND_GT
, flag
, tcg_const_i32(0), l1
);
2420 case 11: /* uge (>=0) */
2421 tcg_gen_brcond_i32(TCG_COND_GE
, flag
, tcg_const_i32(0), l1
);
2423 case 12: /* ult (=-1 or =2) */
2424 tcg_gen_brcond_i32(TCG_COND_GEU
, flag
, tcg_const_i32(2), l1
);
2426 case 13: /* ule (!=1) */
2427 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(1), l1
);
2429 case 14: /* ne (!=0) */
2430 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2436 gen_jmp_tb(s
, 0, s
->pc
);
2438 gen_jmp_tb(s
, 1, addr
+ offset
);
2441 DISAS_INSN(frestore
)
2443 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2445 /* TODO: Implement frestore. */
2446 cpu_abort(CPU(cpu
), "FRESTORE not implemented");
2451 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2453 /* TODO: Implement fsave. */
2454 cpu_abort(CPU(cpu
), "FSAVE not implemented");
2457 static inline TCGv
gen_mac_extract_word(DisasContext
*s
, TCGv val
, int upper
)
2459 TCGv tmp
= tcg_temp_new();
2460 if (s
->env
->macsr
& MACSR_FI
) {
2462 tcg_gen_andi_i32(tmp
, val
, 0xffff0000);
2464 tcg_gen_shli_i32(tmp
, val
, 16);
2465 } else if (s
->env
->macsr
& MACSR_SU
) {
2467 tcg_gen_sari_i32(tmp
, val
, 16);
2469 tcg_gen_ext16s_i32(tmp
, val
);
2472 tcg_gen_shri_i32(tmp
, val
, 16);
2474 tcg_gen_ext16u_i32(tmp
, val
);
2479 static void gen_mac_clear_flags(void)
2481 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
,
2482 ~(MACSR_V
| MACSR_Z
| MACSR_N
| MACSR_EV
));
2498 s
->mactmp
= tcg_temp_new_i64();
2502 ext
= read_im16(env
, s
);
2504 acc
= ((insn
>> 7) & 1) | ((ext
>> 3) & 2);
2505 dual
= ((insn
& 0x30) != 0 && (ext
& 3) != 0);
2506 if (dual
&& !m68k_feature(s
->env
, M68K_FEATURE_CF_EMAC_B
)) {
2507 disas_undef(env
, s
, insn
);
2511 /* MAC with load. */
2512 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
2513 addr
= tcg_temp_new();
2514 tcg_gen_and_i32(addr
, tmp
, QREG_MAC_MASK
);
2515 /* Load the value now to ensure correct exception behavior.
2516 Perform writeback after reading the MAC inputs. */
2517 loadval
= gen_load(s
, OS_LONG
, addr
, 0);
2520 rx
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(insn
, 12);
2521 ry
= (ext
& 8) ? AREG(ext
, 0) : DREG(ext
, 0);
2523 loadval
= addr
= NULL_QREG
;
2524 rx
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2525 ry
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2528 gen_mac_clear_flags();
2531 /* Disabled because conditional branches clobber temporary vars. */
2532 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && !dual
) {
2533 /* Skip the multiply if we know we will ignore it. */
2534 l1
= gen_new_label();
2535 tmp
= tcg_temp_new();
2536 tcg_gen_andi_i32(tmp
, QREG_MACSR
, 1 << (acc
+ 8));
2537 gen_op_jmp_nz32(tmp
, l1
);
2541 if ((ext
& 0x0800) == 0) {
2543 rx
= gen_mac_extract_word(s
, rx
, (ext
& 0x80) != 0);
2544 ry
= gen_mac_extract_word(s
, ry
, (ext
& 0x40) != 0);
2546 if (s
->env
->macsr
& MACSR_FI
) {
2547 gen_helper_macmulf(s
->mactmp
, cpu_env
, rx
, ry
);
2549 if (s
->env
->macsr
& MACSR_SU
)
2550 gen_helper_macmuls(s
->mactmp
, cpu_env
, rx
, ry
);
2552 gen_helper_macmulu(s
->mactmp
, cpu_env
, rx
, ry
);
2553 switch ((ext
>> 9) & 3) {
2555 tcg_gen_shli_i64(s
->mactmp
, s
->mactmp
, 1);
2558 tcg_gen_shri_i64(s
->mactmp
, s
->mactmp
, 1);
2564 /* Save the overflow flag from the multiply. */
2565 saved_flags
= tcg_temp_new();
2566 tcg_gen_mov_i32(saved_flags
, QREG_MACSR
);
2568 saved_flags
= NULL_QREG
;
2572 /* Disabled because conditional branches clobber temporary vars. */
2573 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && dual
) {
2574 /* Skip the accumulate if the value is already saturated. */
2575 l1
= gen_new_label();
2576 tmp
= tcg_temp_new();
2577 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2578 gen_op_jmp_nz32(tmp
, l1
);
2583 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2585 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2587 if (s
->env
->macsr
& MACSR_FI
)
2588 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2589 else if (s
->env
->macsr
& MACSR_SU
)
2590 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2592 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2595 /* Disabled because conditional branches clobber temporary vars. */
2601 /* Dual accumulate variant. */
2602 acc
= (ext
>> 2) & 3;
2603 /* Restore the overflow flag from the multiplier. */
2604 tcg_gen_mov_i32(QREG_MACSR
, saved_flags
);
2606 /* Disabled because conditional branches clobber temporary vars. */
2607 if ((s
->env
->macsr
& MACSR_OMC
) != 0) {
2608 /* Skip the accumulate if the value is already saturated. */
2609 l1
= gen_new_label();
2610 tmp
= tcg_temp_new();
2611 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2612 gen_op_jmp_nz32(tmp
, l1
);
2616 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2618 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2619 if (s
->env
->macsr
& MACSR_FI
)
2620 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2621 else if (s
->env
->macsr
& MACSR_SU
)
2622 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2624 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2626 /* Disabled because conditional branches clobber temporary vars. */
2631 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(acc
));
2635 rw
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2636 tcg_gen_mov_i32(rw
, loadval
);
2637 /* FIXME: Should address writeback happen with the masked or
2639 switch ((insn
>> 3) & 7) {
2640 case 3: /* Post-increment. */
2641 tcg_gen_addi_i32(AREG(insn
, 0), addr
, 4);
2643 case 4: /* Pre-decrement. */
2644 tcg_gen_mov_i32(AREG(insn
, 0), addr
);
2649 DISAS_INSN(from_mac
)
2655 rx
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2656 accnum
= (insn
>> 9) & 3;
2657 acc
= MACREG(accnum
);
2658 if (s
->env
->macsr
& MACSR_FI
) {
2659 gen_helper_get_macf(rx
, cpu_env
, acc
);
2660 } else if ((s
->env
->macsr
& MACSR_OMC
) == 0) {
2661 tcg_gen_extrl_i64_i32(rx
, acc
);
2662 } else if (s
->env
->macsr
& MACSR_SU
) {
2663 gen_helper_get_macs(rx
, acc
);
2665 gen_helper_get_macu(rx
, acc
);
2668 tcg_gen_movi_i64(acc
, 0);
2669 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2673 DISAS_INSN(move_mac
)
2675 /* FIXME: This can be done without a helper. */
2679 dest
= tcg_const_i32((insn
>> 9) & 3);
2680 gen_helper_mac_move(cpu_env
, dest
, tcg_const_i32(src
));
2681 gen_mac_clear_flags();
2682 gen_helper_mac_set_flags(cpu_env
, dest
);
2685 DISAS_INSN(from_macsr
)
2689 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2690 tcg_gen_mov_i32(reg
, QREG_MACSR
);
2693 DISAS_INSN(from_mask
)
2696 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2697 tcg_gen_mov_i32(reg
, QREG_MAC_MASK
);
2700 DISAS_INSN(from_mext
)
2704 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2705 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2706 if (s
->env
->macsr
& MACSR_FI
)
2707 gen_helper_get_mac_extf(reg
, cpu_env
, acc
);
2709 gen_helper_get_mac_exti(reg
, cpu_env
, acc
);
2712 DISAS_INSN(macsr_to_ccr
)
2714 TCGv tmp
= tcg_temp_new();
2715 tcg_gen_andi_i32(tmp
, QREG_MACSR
, 0xf);
2716 gen_helper_set_sr(cpu_env
, tmp
);
2718 set_cc_op(s
, CC_OP_FLAGS
);
2726 accnum
= (insn
>> 9) & 3;
2727 acc
= MACREG(accnum
);
2728 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2729 if (s
->env
->macsr
& MACSR_FI
) {
2730 tcg_gen_ext_i32_i64(acc
, val
);
2731 tcg_gen_shli_i64(acc
, acc
, 8);
2732 } else if (s
->env
->macsr
& MACSR_SU
) {
2733 tcg_gen_ext_i32_i64(acc
, val
);
2735 tcg_gen_extu_i32_i64(acc
, val
);
2737 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2738 gen_mac_clear_flags();
2739 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(accnum
));
2742 DISAS_INSN(to_macsr
)
2745 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2746 gen_helper_set_macsr(cpu_env
, val
);
2753 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2754 tcg_gen_ori_i32(QREG_MAC_MASK
, val
, 0xffff0000);
2761 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2762 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2763 if (s
->env
->macsr
& MACSR_FI
)
2764 gen_helper_set_mac_extf(cpu_env
, val
, acc
);
2765 else if (s
->env
->macsr
& MACSR_SU
)
2766 gen_helper_set_mac_exts(cpu_env
, val
, acc
);
2768 gen_helper_set_mac_extu(cpu_env
, val
, acc
);
2771 static disas_proc opcode_table
[65536];
2774 register_opcode (disas_proc proc
, uint16_t opcode
, uint16_t mask
)
2780 /* Sanity check. All set bits must be included in the mask. */
2781 if (opcode
& ~mask
) {
2783 "qemu internal error: bogus opcode definition %04x/%04x\n",
2787 /* This could probably be cleverer. For now just optimize the case where
2788 the top bits are known. */
2789 /* Find the first zero bit in the mask. */
2791 while ((i
& mask
) != 0)
2793 /* Iterate over all combinations of this and lower bits. */
2798 from
= opcode
& ~(i
- 1);
2800 for (i
= from
; i
< to
; i
++) {
2801 if ((i
& mask
) == opcode
)
2802 opcode_table
[i
] = proc
;
2806 /* Register m68k opcode handlers. Order is important.
2807 Later insn override earlier ones. */
2808 void register_m68k_insns (CPUM68KState
*env
)
2810 /* Build the opcode table only once to avoid
2811 multithreading issues. */
2812 if (opcode_table
[0] != NULL
) {
2816 /* use BASE() for instruction available
2817 * for CF_ISA_A and M68000.
2819 #define BASE(name, opcode, mask) \
2820 register_opcode(disas_##name, 0x##opcode, 0x##mask)
2821 #define INSN(name, opcode, mask, feature) do { \
2822 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2823 BASE(name, opcode, mask); \
2825 BASE(undef
, 0000, 0000);
2826 INSN(arith_im
, 0080, fff8
, CF_ISA_A
);
2827 INSN(arith_im
, 0000, ff00
, M68000
);
2828 INSN(undef
, 00c0
, ffc0
, M68000
);
2829 INSN(bitrev
, 00c0
, fff8
, CF_ISA_APLUSC
);
2830 BASE(bitop_reg
, 0100, f1c0
);
2831 BASE(bitop_reg
, 0140, f1c0
);
2832 BASE(bitop_reg
, 0180, f1c0
);
2833 BASE(bitop_reg
, 01c0
, f1c0
);
2834 INSN(arith_im
, 0280, fff8
, CF_ISA_A
);
2835 INSN(arith_im
, 0200, ff00
, M68000
);
2836 INSN(undef
, 02c0
, ffc0
, M68000
);
2837 INSN(byterev
, 02c0
, fff8
, CF_ISA_APLUSC
);
2838 INSN(arith_im
, 0480, fff8
, CF_ISA_A
);
2839 INSN(arith_im
, 0400, ff00
, M68000
);
2840 INSN(undef
, 04c0
, ffc0
, M68000
);
2841 INSN(arith_im
, 0600, ff00
, M68000
);
2842 INSN(undef
, 06c0
, ffc0
, M68000
);
2843 INSN(ff1
, 04c0
, fff8
, CF_ISA_APLUSC
);
2844 INSN(arith_im
, 0680, fff8
, CF_ISA_A
);
2845 INSN(arith_im
, 0c00
, ff38
, CF_ISA_A
);
2846 INSN(arith_im
, 0c00
, ff00
, M68000
);
2847 BASE(bitop_im
, 0800, ffc0
);
2848 BASE(bitop_im
, 0840, ffc0
);
2849 BASE(bitop_im
, 0880, ffc0
);
2850 BASE(bitop_im
, 08c0
, ffc0
);
2851 INSN(arith_im
, 0a80
, fff8
, CF_ISA_A
);
2852 INSN(arith_im
, 0a00
, ff00
, M68000
);
2853 BASE(move
, 1000, f000
);
2854 BASE(move
, 2000, f000
);
2855 BASE(move
, 3000, f000
);
2856 INSN(strldsr
, 40e7
, ffff
, CF_ISA_APLUSC
);
2857 INSN(negx
, 4080, fff8
, CF_ISA_A
);
2858 INSN(move_from_sr
, 40c0
, fff8
, CF_ISA_A
);
2859 INSN(move_from_sr
, 40c0
, ffc0
, M68000
);
2860 BASE(lea
, 41c0
, f1c0
);
2861 BASE(clr
, 4200, ff00
);
2862 BASE(undef
, 42c0
, ffc0
);
2863 INSN(move_from_ccr
, 42c0
, fff8
, CF_ISA_A
);
2864 INSN(move_from_ccr
, 42c0
, ffc0
, M68000
);
2865 INSN(neg
, 4480, fff8
, CF_ISA_A
);
2866 INSN(neg
, 4400, ff00
, M68000
);
2867 INSN(undef
, 44c0
, ffc0
, M68000
);
2868 BASE(move_to_ccr
, 44c0
, ffc0
);
2869 INSN(not, 4680, fff8
, CF_ISA_A
);
2870 INSN(not, 4600, ff00
, M68000
);
2871 INSN(undef
, 46c0
, ffc0
, M68000
);
2872 INSN(move_to_sr
, 46c0
, ffc0
, CF_ISA_A
);
2873 BASE(pea
, 4840, ffc0
);
2874 BASE(swap
, 4840, fff8
);
2875 BASE(movem
, 48c0
, fbc0
);
2876 BASE(ext
, 4880, fff8
);
2877 BASE(ext
, 48c0
, fff8
);
2878 BASE(ext
, 49c0
, fff8
);
2879 BASE(tst
, 4a00
, ff00
);
2880 INSN(tas
, 4ac0
, ffc0
, CF_ISA_B
);
2881 INSN(tas
, 4ac0
, ffc0
, M68000
);
2882 INSN(halt
, 4ac8
, ffff
, CF_ISA_A
);
2883 INSN(pulse
, 4acc
, ffff
, CF_ISA_A
);
2884 BASE(illegal
, 4afc
, ffff
);
2885 INSN(mull
, 4c00
, ffc0
, CF_ISA_A
);
2886 INSN(mull
, 4c00
, ffc0
, LONG_MULDIV
);
2887 INSN(divl
, 4c40
, ffc0
, CF_ISA_A
);
2888 INSN(divl
, 4c40
, ffc0
, LONG_MULDIV
);
2889 INSN(sats
, 4c80
, fff8
, CF_ISA_B
);
2890 BASE(trap
, 4e40
, fff0
);
2891 BASE(link
, 4e50
, fff8
);
2892 BASE(unlk
, 4e58
, fff8
);
2893 INSN(move_to_usp
, 4e60
, fff8
, USP
);
2894 INSN(move_from_usp
, 4e68
, fff8
, USP
);
2895 BASE(nop
, 4e71
, ffff
);
2896 BASE(stop
, 4e72
, ffff
);
2897 BASE(rte
, 4e73
, ffff
);
2898 BASE(rts
, 4e75
, ffff
);
2899 INSN(movec
, 4e7b
, ffff
, CF_ISA_A
);
2900 BASE(jump
, 4e80
, ffc0
);
2901 INSN(jump
, 4ec0
, ffc0
, CF_ISA_A
);
2902 INSN(addsubq
, 5180, f1c0
, CF_ISA_A
);
2903 INSN(jump
, 4ec0
, ffc0
, M68000
);
2904 INSN(addsubq
, 5000, f080
, M68000
);
2905 INSN(addsubq
, 5080, f0c0
, M68000
);
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 BASE(branch
, 6000, f000
);
2912 /* Disable long branch instructions, then add back the ones we want. */
2913 BASE(undef
, 60ff
, f0ff
); /* 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
);
2917 INSN(branch
, 60ff
, f0ff
, BCCL
);
2919 BASE(moveq
, 7000, f100
);
2920 INSN(mvzs
, 7100, f100
, CF_ISA_B
);
2921 BASE(or, 8000, f000
);
2922 BASE(divw
, 80c0
, f0c0
);
2923 BASE(addsub
, 9000, f000
);
2924 INSN(subx
, 9180, f1f8
, CF_ISA_A
);
2925 INSN(suba
, 91c0
, f1c0
, CF_ISA_A
);
2927 BASE(undef_mac
, a000
, f000
);
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(cmp
, b000
, f100
, M68000
);
2947 INSN(eor
, b100
, f100
, M68000
);
2948 INSN(cmpa
, b0c0
, f0c0
, M68000
);
2949 INSN(eor
, b180
, f1c0
, CF_ISA_A
);
2950 BASE(and, c000
, f000
);
2951 BASE(mulw
, c0c0
, f0c0
);
2952 BASE(addsub
, d000
, f000
);
2953 INSN(addx
, d180
, f1f8
, CF_ISA_A
);
2954 INSN(adda
, d1c0
, f1c0
, CF_ISA_A
);
2955 INSN(adda
, d0c0
, f0c0
, M68000
);
2956 INSN(shift_im
, e080
, f0f0
, CF_ISA_A
);
2957 INSN(shift_reg
, e0a0
, f0f0
, CF_ISA_A
);
2958 INSN(undef_fpu
, f000
, f000
, CF_ISA_A
);
2959 INSN(fpu
, f200
, ffc0
, CF_FPU
);
2960 INSN(fbcc
, f280
, ffc0
, CF_FPU
);
2961 INSN(frestore
, f340
, ffc0
, CF_FPU
);
2962 INSN(fsave
, f340
, ffc0
, CF_FPU
);
2963 INSN(intouch
, f340
, ffc0
, CF_ISA_A
);
2964 INSN(cpushl
, f428
, ff38
, CF_ISA_A
);
2965 INSN(wddata
, fb00
, ff00
, CF_ISA_A
);
2966 INSN(wdebug
, fbc0
, ffc0
, CF_ISA_A
);
2970 /* ??? Some of this implementation is not exception safe. We should always
2971 write back the result to memory before setting the condition codes. */
2972 static void disas_m68k_insn(CPUM68KState
* env
, DisasContext
*s
)
2976 insn
= read_im16(env
, s
);
2978 opcode_table
[insn
](env
, s
, insn
);
2981 /* generate intermediate code for basic block 'tb'. */
2982 void gen_intermediate_code(CPUM68KState
*env
, TranslationBlock
*tb
)
2984 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2985 CPUState
*cs
= CPU(cpu
);
2986 DisasContext dc1
, *dc
= &dc1
;
2987 target_ulong pc_start
;
2992 /* generate intermediate code */
2998 dc
->is_jmp
= DISAS_NEXT
;
3000 dc
->cc_op
= CC_OP_DYNAMIC
;
3001 dc
->cc_op_synced
= 1;
3002 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
3003 dc
->fpcr
= env
->fpcr
;
3004 dc
->user
= (env
->sr
& SR_S
) == 0;
3007 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
3008 if (max_insns
== 0) {
3009 max_insns
= CF_COUNT_MASK
;
3011 if (max_insns
> TCG_MAX_INSNS
) {
3012 max_insns
= TCG_MAX_INSNS
;
3017 pc_offset
= dc
->pc
- pc_start
;
3018 gen_throws_exception
= NULL
;
3019 tcg_gen_insn_start(dc
->pc
, dc
->cc_op
);
3022 if (unlikely(cpu_breakpoint_test(cs
, dc
->pc
, BP_ANY
))) {
3023 gen_exception(dc
, dc
->pc
, EXCP_DEBUG
);
3024 dc
->is_jmp
= DISAS_JUMP
;
3025 /* The address covered by the breakpoint must be included in
3026 [tb->pc, tb->pc + tb->size) in order to for it to be
3027 properly cleared -- thus we increment the PC here so that
3028 the logic setting tb->size below does the right thing. */
3033 if (num_insns
== max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
3037 dc
->insn_pc
= dc
->pc
;
3038 disas_m68k_insn(env
, dc
);
3039 } while (!dc
->is_jmp
&& !tcg_op_buf_full() &&
3040 !cs
->singlestep_enabled
&&
3042 (pc_offset
) < (TARGET_PAGE_SIZE
- 32) &&
3043 num_insns
< max_insns
);
3045 if (tb
->cflags
& CF_LAST_IO
)
3047 if (unlikely(cs
->singlestep_enabled
)) {
3048 /* Make sure the pc is updated, and raise a debug exception. */
3051 tcg_gen_movi_i32(QREG_PC
, dc
->pc
);
3053 gen_helper_raise_exception(cpu_env
, tcg_const_i32(EXCP_DEBUG
));
3055 switch(dc
->is_jmp
) {
3058 gen_jmp_tb(dc
, 0, dc
->pc
);
3064 /* indicate that the hash table must be used to find the next TB */
3068 /* nothing more to generate */
3072 gen_tb_end(tb
, num_insns
);
3075 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)
3076 && qemu_log_in_addr_range(pc_start
)) {
3077 qemu_log("----------------\n");
3078 qemu_log("IN: %s\n", lookup_symbol(pc_start
));
3079 log_target_disas(cs
, pc_start
, dc
->pc
- pc_start
, 0);
3083 tb
->size
= dc
->pc
- pc_start
;
3084 tb
->icount
= num_insns
;
3087 void m68k_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
3090 M68kCPU
*cpu
= M68K_CPU(cs
);
3091 CPUM68KState
*env
= &cpu
->env
;
3095 for (i
= 0; i
< 8; i
++)
3097 u
.d
= env
->fregs
[i
];
3098 cpu_fprintf(f
, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3099 i
, env
->dregs
[i
], i
, env
->aregs
[i
],
3100 i
, u
.l
.upper
, u
.l
.lower
, *(double *)&u
.d
);
3102 cpu_fprintf (f
, "PC = %08x ", env
->pc
);
3103 sr
= env
->sr
| cpu_m68k_get_ccr(env
);
3104 cpu_fprintf(f
, "SR = %04x %c%c%c%c%c ", sr
, (sr
& CCF_X
) ? 'X' : '-',
3105 (sr
& CCF_N
) ? 'N' : '-', (sr
& CCF_Z
) ? 'Z' : '-',
3106 (sr
& CCF_V
) ? 'V' : '-', (sr
& CCF_C
) ? 'C' : '-');
3107 cpu_fprintf (f
, "FPRESULT = %12g\n", *(double *)&env
->fp_result
);
3110 void restore_state_to_opc(CPUM68KState
*env
, TranslationBlock
*tb
,
3113 int cc_op
= data
[1];
3115 if (cc_op
!= CC_OP_DYNAMIC
) {