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/>.
22 #include "disas/disas.h"
25 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
28 #include "exec/helper-gen.h"
30 #include "trace-tcg.h"
33 //#define DEBUG_DISPATCH 1
35 /* Fake floating point. */
36 #define tcg_gen_mov_f64 tcg_gen_mov_i64
37 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
38 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
40 #define DEFO32(name, offset) static TCGv QREG_##name;
41 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
42 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
48 static TCGv_i32 cpu_halted
;
49 static TCGv_i32 cpu_exception_index
;
51 static TCGv_ptr cpu_env
;
53 static char cpu_reg_names
[3*8*3 + 5*4];
54 static TCGv cpu_dregs
[8];
55 static TCGv cpu_aregs
[8];
56 static TCGv_i64 cpu_fregs
[8];
57 static TCGv_i64 cpu_macc
[4];
59 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
60 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
61 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
62 #define MACREG(acc) cpu_macc[acc]
63 #define QREG_SP cpu_aregs[7]
65 static TCGv NULL_QREG
;
66 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
67 /* Used to distinguish stores from bad addressing modes. */
68 static TCGv store_dummy
;
70 #include "exec/gen-icount.h"
72 void m68k_tcg_init(void)
77 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
78 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
79 #define DEFF64(name, offset) DEFO64(name, offset)
85 cpu_halted
= tcg_global_mem_new_i32(TCG_AREG0
,
86 -offsetof(M68kCPU
, env
) +
87 offsetof(CPUState
, halted
), "HALTED");
88 cpu_exception_index
= tcg_global_mem_new_i32(TCG_AREG0
,
89 -offsetof(M68kCPU
, env
) +
90 offsetof(CPUState
, exception_index
),
93 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
96 for (i
= 0; i
< 8; i
++) {
98 cpu_dregs
[i
] = tcg_global_mem_new(TCG_AREG0
,
99 offsetof(CPUM68KState
, dregs
[i
]), p
);
101 sprintf(p
, "A%d", i
);
102 cpu_aregs
[i
] = tcg_global_mem_new(TCG_AREG0
,
103 offsetof(CPUM68KState
, aregs
[i
]), p
);
105 sprintf(p
, "F%d", i
);
106 cpu_fregs
[i
] = tcg_global_mem_new_i64(TCG_AREG0
,
107 offsetof(CPUM68KState
, fregs
[i
]), p
);
110 for (i
= 0; i
< 4; i
++) {
111 sprintf(p
, "ACC%d", i
);
112 cpu_macc
[i
] = tcg_global_mem_new_i64(TCG_AREG0
,
113 offsetof(CPUM68KState
, macc
[i
]), p
);
117 NULL_QREG
= tcg_global_mem_new(TCG_AREG0
, -4, "NULL");
118 store_dummy
= tcg_global_mem_new(TCG_AREG0
, -8, "NULL");
121 /* internal defines */
122 typedef struct DisasContext
{
124 target_ulong insn_pc
; /* Start of the current instruction. */
130 struct TranslationBlock
*tb
;
131 int singlestep_enabled
;
136 #define DISAS_JUMP_NEXT 4
138 #if defined(CONFIG_USER_ONLY)
141 #define IS_USER(s) s->user
144 /* XXX: move that elsewhere */
145 /* ??? Fix exceptions. */
146 static void *gen_throws_exception
;
147 #define gen_last_qop NULL
155 typedef void (*disas_proc
)(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
);
157 #ifdef DEBUG_DISPATCH
158 #define DISAS_INSN(name) \
159 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
161 static void disas_##name(CPUM68KState *env, DisasContext *s, \
164 qemu_log("Dispatch " #name "\n"); \
165 real_disas_##name(s, env, insn); \
167 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
170 #define DISAS_INSN(name) \
171 static void disas_##name(CPUM68KState *env, DisasContext *s, \
175 /* Generate a load from the specified address. Narrow values are
176 sign extended to full register width. */
177 static inline TCGv
gen_load(DisasContext
* s
, int opsize
, TCGv addr
, int sign
)
180 int index
= IS_USER(s
);
181 tmp
= tcg_temp_new_i32();
185 tcg_gen_qemu_ld8s(tmp
, addr
, index
);
187 tcg_gen_qemu_ld8u(tmp
, addr
, index
);
191 tcg_gen_qemu_ld16s(tmp
, addr
, index
);
193 tcg_gen_qemu_ld16u(tmp
, addr
, index
);
197 tcg_gen_qemu_ld32u(tmp
, addr
, index
);
200 g_assert_not_reached();
202 gen_throws_exception
= gen_last_qop
;
206 static inline TCGv_i64
gen_load64(DisasContext
* s
, TCGv addr
)
209 int index
= IS_USER(s
);
210 tmp
= tcg_temp_new_i64();
211 tcg_gen_qemu_ldf64(tmp
, addr
, index
);
212 gen_throws_exception
= gen_last_qop
;
216 /* Generate a store. */
217 static inline void gen_store(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
)
219 int index
= IS_USER(s
);
222 tcg_gen_qemu_st8(val
, addr
, index
);
225 tcg_gen_qemu_st16(val
, addr
, index
);
229 tcg_gen_qemu_st32(val
, addr
, index
);
232 g_assert_not_reached();
234 gen_throws_exception
= gen_last_qop
;
237 static inline void gen_store64(DisasContext
*s
, TCGv addr
, TCGv_i64 val
)
239 int index
= IS_USER(s
);
240 tcg_gen_qemu_stf64(val
, addr
, index
);
241 gen_throws_exception
= gen_last_qop
;
250 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
251 otherwise generate a store. */
252 static TCGv
gen_ldst(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
,
255 if (what
== EA_STORE
) {
256 gen_store(s
, opsize
, addr
, val
);
259 return gen_load(s
, opsize
, addr
, what
== EA_LOADS
);
263 /* Read a 32-bit immediate constant. */
264 static inline uint32_t read_im32(CPUM68KState
*env
, DisasContext
*s
)
267 im
= ((uint32_t)cpu_lduw_code(env
, s
->pc
)) << 16;
269 im
|= cpu_lduw_code(env
, s
->pc
);
274 /* Calculate and address index. */
275 static TCGv
gen_addr_index(uint16_t ext
, TCGv tmp
)
280 add
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(ext
, 12);
281 if ((ext
& 0x800) == 0) {
282 tcg_gen_ext16s_i32(tmp
, add
);
285 scale
= (ext
>> 9) & 3;
287 tcg_gen_shli_i32(tmp
, add
, scale
);
293 /* Handle a base + index + displacement effective addresss.
294 A NULL_QREG base means pc-relative. */
295 static TCGv
gen_lea_indexed(CPUM68KState
*env
, DisasContext
*s
, TCGv base
)
304 ext
= cpu_lduw_code(env
, s
->pc
);
307 if ((ext
& 0x800) == 0 && !m68k_feature(s
->env
, M68K_FEATURE_WORD_INDEX
))
311 /* full extension word format */
312 if (!m68k_feature(s
->env
, M68K_FEATURE_EXT_FULL
))
315 if ((ext
& 0x30) > 0x10) {
316 /* base displacement */
317 if ((ext
& 0x30) == 0x20) {
318 bd
= (int16_t)cpu_lduw_code(env
, s
->pc
);
321 bd
= read_im32(env
, s
);
326 tmp
= tcg_temp_new();
327 if ((ext
& 0x44) == 0) {
329 add
= gen_addr_index(ext
, tmp
);
333 if ((ext
& 0x80) == 0) {
334 /* base not suppressed */
335 if (IS_NULL_QREG(base
)) {
336 base
= tcg_const_i32(offset
+ bd
);
339 if (!IS_NULL_QREG(add
)) {
340 tcg_gen_add_i32(tmp
, add
, base
);
346 if (!IS_NULL_QREG(add
)) {
348 tcg_gen_addi_i32(tmp
, add
, bd
);
352 add
= tcg_const_i32(bd
);
354 if ((ext
& 3) != 0) {
355 /* memory indirect */
356 base
= gen_load(s
, OS_LONG
, add
, 0);
357 if ((ext
& 0x44) == 4) {
358 add
= gen_addr_index(ext
, tmp
);
359 tcg_gen_add_i32(tmp
, add
, base
);
365 /* outer displacement */
366 if ((ext
& 3) == 2) {
367 od
= (int16_t)cpu_lduw_code(env
, s
->pc
);
370 od
= read_im32(env
, s
);
376 tcg_gen_addi_i32(tmp
, add
, od
);
381 /* brief extension word format */
382 tmp
= tcg_temp_new();
383 add
= gen_addr_index(ext
, tmp
);
384 if (!IS_NULL_QREG(base
)) {
385 tcg_gen_add_i32(tmp
, add
, base
);
387 tcg_gen_addi_i32(tmp
, tmp
, (int8_t)ext
);
389 tcg_gen_addi_i32(tmp
, add
, offset
+ (int8_t)ext
);
396 /* Update the CPU env CC_OP state. */
397 static inline void gen_flush_cc_op(DisasContext
*s
)
399 if (s
->cc_op
!= CC_OP_DYNAMIC
)
400 tcg_gen_movi_i32(QREG_CC_OP
, s
->cc_op
);
403 /* Evaluate all the CC flags. */
404 static inline void gen_flush_flags(DisasContext
*s
)
406 if (s
->cc_op
== CC_OP_FLAGS
)
409 gen_helper_flush_flags(cpu_env
, QREG_CC_OP
);
410 s
->cc_op
= CC_OP_FLAGS
;
413 static void gen_logic_cc(DisasContext
*s
, TCGv val
)
415 tcg_gen_mov_i32(QREG_CC_DEST
, val
);
416 s
->cc_op
= CC_OP_LOGIC
;
419 static void gen_update_cc_add(TCGv dest
, TCGv src
)
421 tcg_gen_mov_i32(QREG_CC_DEST
, dest
);
422 tcg_gen_mov_i32(QREG_CC_SRC
, src
);
425 static inline int opsize_bytes(int opsize
)
428 case OS_BYTE
: return 1;
429 case OS_WORD
: return 2;
430 case OS_LONG
: return 4;
431 case OS_SINGLE
: return 4;
432 case OS_DOUBLE
: return 8;
434 g_assert_not_reached();
438 /* Assign value to a register. If the width is less than the register width
439 only the low part of the register is set. */
440 static void gen_partset_reg(int opsize
, TCGv reg
, TCGv val
)
445 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
446 tmp
= tcg_temp_new();
447 tcg_gen_ext8u_i32(tmp
, val
);
448 tcg_gen_or_i32(reg
, reg
, tmp
);
451 tcg_gen_andi_i32(reg
, reg
, 0xffff0000);
452 tmp
= tcg_temp_new();
453 tcg_gen_ext16u_i32(tmp
, val
);
454 tcg_gen_or_i32(reg
, reg
, tmp
);
458 tcg_gen_mov_i32(reg
, val
);
461 g_assert_not_reached();
465 /* Sign or zero extend a value. */
466 static inline TCGv
gen_extend(TCGv val
, int opsize
, int sign
)
472 tmp
= tcg_temp_new();
474 tcg_gen_ext8s_i32(tmp
, val
);
476 tcg_gen_ext8u_i32(tmp
, val
);
479 tmp
= tcg_temp_new();
481 tcg_gen_ext16s_i32(tmp
, val
);
483 tcg_gen_ext16u_i32(tmp
, val
);
490 g_assert_not_reached();
495 /* Generate code for an "effective address". Does not adjust the base
496 register for autoincrement addressing modes. */
497 static TCGv
gen_lea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
505 switch ((insn
>> 3) & 7) {
506 case 0: /* Data register direct. */
507 case 1: /* Address register direct. */
509 case 2: /* Indirect register */
510 case 3: /* Indirect postincrement. */
511 return AREG(insn
, 0);
512 case 4: /* Indirect predecrememnt. */
514 tmp
= tcg_temp_new();
515 tcg_gen_subi_i32(tmp
, reg
, opsize_bytes(opsize
));
517 case 5: /* Indirect displacement. */
519 tmp
= tcg_temp_new();
520 ext
= cpu_lduw_code(env
, s
->pc
);
522 tcg_gen_addi_i32(tmp
, reg
, (int16_t)ext
);
524 case 6: /* Indirect index + displacement. */
526 return gen_lea_indexed(env
, s
, reg
);
529 case 0: /* Absolute short. */
530 offset
= cpu_ldsw_code(env
, s
->pc
);
532 return tcg_const_i32(offset
);
533 case 1: /* Absolute long. */
534 offset
= read_im32(env
, s
);
535 return tcg_const_i32(offset
);
536 case 2: /* pc displacement */
538 offset
+= cpu_ldsw_code(env
, s
->pc
);
540 return tcg_const_i32(offset
);
541 case 3: /* pc index+displacement. */
542 return gen_lea_indexed(env
, s
, NULL_QREG
);
543 case 4: /* Immediate. */
548 /* Should never happen. */
552 /* Helper function for gen_ea. Reuse the computed address between the
553 for read/write operands. */
554 static inline TCGv
gen_ea_once(CPUM68KState
*env
, DisasContext
*s
,
555 uint16_t insn
, int opsize
, TCGv val
,
556 TCGv
*addrp
, ea_what what
)
560 if (addrp
&& what
== EA_STORE
) {
563 tmp
= gen_lea(env
, s
, insn
, opsize
);
564 if (IS_NULL_QREG(tmp
))
569 return gen_ldst(s
, opsize
, tmp
, val
, what
);
572 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
573 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
574 ADDRP is non-null for readwrite operands. */
575 static TCGv
gen_ea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
576 int opsize
, TCGv val
, TCGv
*addrp
, ea_what what
)
582 switch ((insn
>> 3) & 7) {
583 case 0: /* Data register direct. */
585 if (what
== EA_STORE
) {
586 gen_partset_reg(opsize
, reg
, val
);
589 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
591 case 1: /* Address register direct. */
593 if (what
== EA_STORE
) {
594 tcg_gen_mov_i32(reg
, val
);
597 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
599 case 2: /* Indirect register */
601 return gen_ldst(s
, opsize
, reg
, val
, what
);
602 case 3: /* Indirect postincrement. */
604 result
= gen_ldst(s
, opsize
, reg
, val
, what
);
605 /* ??? This is not exception safe. The instruction may still
606 fault after this point. */
607 if (what
== EA_STORE
|| !addrp
)
608 tcg_gen_addi_i32(reg
, reg
, opsize_bytes(opsize
));
610 case 4: /* Indirect predecrememnt. */
613 if (addrp
&& what
== EA_STORE
) {
616 tmp
= gen_lea(env
, s
, insn
, opsize
);
617 if (IS_NULL_QREG(tmp
))
622 result
= gen_ldst(s
, opsize
, tmp
, val
, what
);
623 /* ??? This is not exception safe. The instruction may still
624 fault after this point. */
625 if (what
== EA_STORE
|| !addrp
) {
627 tcg_gen_mov_i32(reg
, tmp
);
631 case 5: /* Indirect displacement. */
632 case 6: /* Indirect index + displacement. */
633 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
636 case 0: /* Absolute short. */
637 case 1: /* Absolute long. */
638 case 2: /* pc displacement */
639 case 3: /* pc index+displacement. */
640 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
641 case 4: /* Immediate. */
642 /* Sign extend values for consistency. */
645 if (what
== EA_LOADS
) {
646 offset
= cpu_ldsb_code(env
, s
->pc
+ 1);
648 offset
= cpu_ldub_code(env
, s
->pc
+ 1);
653 if (what
== EA_LOADS
) {
654 offset
= cpu_ldsw_code(env
, s
->pc
);
656 offset
= cpu_lduw_code(env
, s
->pc
);
661 offset
= read_im32(env
, s
);
664 g_assert_not_reached();
666 return tcg_const_i32(offset
);
671 /* Should never happen. */
675 /* This generates a conditional branch, clobbering all temporaries. */
676 static void gen_jmpcc(DisasContext
*s
, int cond
, TCGLabel
*l1
)
680 /* TODO: Optimize compare/branch pairs rather than always flushing
681 flag state to CC_OP_FLAGS. */
689 case 2: /* HI (!C && !Z) */
690 tmp
= tcg_temp_new();
691 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
| CCF_Z
);
692 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
694 case 3: /* LS (C || Z) */
695 tmp
= tcg_temp_new();
696 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
| CCF_Z
);
697 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
699 case 4: /* CC (!C) */
700 tmp
= tcg_temp_new();
701 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
);
702 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
705 tmp
= tcg_temp_new();
706 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
);
707 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
709 case 6: /* NE (!Z) */
710 tmp
= tcg_temp_new();
711 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_Z
);
712 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
715 tmp
= tcg_temp_new();
716 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_Z
);
717 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
719 case 8: /* VC (!V) */
720 tmp
= tcg_temp_new();
721 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_V
);
722 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
725 tmp
= tcg_temp_new();
726 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_V
);
727 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
729 case 10: /* PL (!N) */
730 tmp
= tcg_temp_new();
731 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
732 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
734 case 11: /* MI (N) */
735 tmp
= tcg_temp_new();
736 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
737 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
739 case 12: /* GE (!(N ^ V)) */
740 tmp
= tcg_temp_new();
741 assert(CCF_V
== (CCF_N
>> 2));
742 tcg_gen_shri_i32(tmp
, QREG_CC_DEST
, 2);
743 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
744 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
);
745 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
747 case 13: /* LT (N ^ V) */
748 tmp
= tcg_temp_new();
749 assert(CCF_V
== (CCF_N
>> 2));
750 tcg_gen_shri_i32(tmp
, QREG_CC_DEST
, 2);
751 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
752 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
);
753 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
755 case 14: /* GT (!(Z || (N ^ V))) */
756 tmp
= tcg_temp_new();
757 assert(CCF_V
== (CCF_N
>> 2));
758 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
759 tcg_gen_shri_i32(tmp
, tmp
, 2);
760 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
761 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
| CCF_Z
);
762 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
764 case 15: /* LE (Z || (N ^ V)) */
765 tmp
= tcg_temp_new();
766 assert(CCF_V
== (CCF_N
>> 2));
767 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
768 tcg_gen_shri_i32(tmp
, tmp
, 2);
769 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
770 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
| CCF_Z
);
771 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
774 /* Should ever happen. */
785 l1
= gen_new_label();
786 cond
= (insn
>> 8) & 0xf;
788 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
789 /* This is safe because we modify the reg directly, with no other values
791 gen_jmpcc(s
, cond
^ 1, l1
);
792 tcg_gen_ori_i32(reg
, reg
, 0xff);
796 /* Force a TB lookup after an instruction that changes the CPU state. */
797 static void gen_lookup_tb(DisasContext
*s
)
800 tcg_gen_movi_i32(QREG_PC
, s
->pc
);
801 s
->is_jmp
= DISAS_UPDATE
;
804 /* Generate a jump to an immediate address. */
805 static void gen_jmp_im(DisasContext
*s
, uint32_t dest
)
808 tcg_gen_movi_i32(QREG_PC
, dest
);
809 s
->is_jmp
= DISAS_JUMP
;
812 /* Generate a jump to the address in qreg DEST. */
813 static void gen_jmp(DisasContext
*s
, TCGv dest
)
816 tcg_gen_mov_i32(QREG_PC
, dest
);
817 s
->is_jmp
= DISAS_JUMP
;
820 static void gen_exception(DisasContext
*s
, uint32_t where
, int nr
)
823 gen_jmp_im(s
, where
);
824 gen_helper_raise_exception(cpu_env
, tcg_const_i32(nr
));
827 static inline void gen_addr_fault(DisasContext
*s
)
829 gen_exception(s
, s
->insn_pc
, EXCP_ADDRESS
);
832 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
833 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
834 op_sign ? EA_LOADS : EA_LOADU); \
835 if (IS_NULL_QREG(result)) { \
841 #define DEST_EA(env, insn, opsize, val, addrp) do { \
842 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
843 if (IS_NULL_QREG(ea_result)) { \
849 /* Generate a jump to an immediate address. */
850 static void gen_jmp_tb(DisasContext
*s
, int n
, uint32_t dest
)
852 TranslationBlock
*tb
;
855 if (unlikely(s
->singlestep_enabled
)) {
856 gen_exception(s
, dest
, EXCP_DEBUG
);
857 } else if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
) ||
858 (s
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
860 tcg_gen_movi_i32(QREG_PC
, dest
);
861 tcg_gen_exit_tb((uintptr_t)tb
+ n
);
866 s
->is_jmp
= DISAS_TB_JUMP
;
869 DISAS_INSN(undef_mac
)
871 gen_exception(s
, s
->pc
- 2, EXCP_LINEA
);
874 DISAS_INSN(undef_fpu
)
876 gen_exception(s
, s
->pc
- 2, EXCP_LINEF
);
881 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
883 gen_exception(s
, s
->pc
- 2, EXCP_UNSUPPORTED
);
884 cpu_abort(CPU(cpu
), "Illegal instruction: %04x @ %08x", insn
, s
->pc
- 2);
894 sign
= (insn
& 0x100) != 0;
896 tmp
= tcg_temp_new();
898 tcg_gen_ext16s_i32(tmp
, reg
);
900 tcg_gen_ext16u_i32(tmp
, reg
);
901 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
902 tcg_gen_mul_i32(tmp
, tmp
, src
);
903 tcg_gen_mov_i32(reg
, tmp
);
904 /* Unlike m68k, coldfire always clears the overflow bit. */
905 gen_logic_cc(s
, tmp
);
915 sign
= (insn
& 0x100) != 0;
918 tcg_gen_ext16s_i32(QREG_DIV1
, reg
);
920 tcg_gen_ext16u_i32(QREG_DIV1
, reg
);
922 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
923 tcg_gen_mov_i32(QREG_DIV2
, src
);
925 gen_helper_divs(cpu_env
, tcg_const_i32(1));
927 gen_helper_divu(cpu_env
, tcg_const_i32(1));
930 tmp
= tcg_temp_new();
931 src
= tcg_temp_new();
932 tcg_gen_ext16u_i32(tmp
, QREG_DIV1
);
933 tcg_gen_shli_i32(src
, QREG_DIV2
, 16);
934 tcg_gen_or_i32(reg
, tmp
, src
);
935 s
->cc_op
= CC_OP_FLAGS
;
945 ext
= cpu_lduw_code(env
, s
->pc
);
948 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
953 tcg_gen_mov_i32(QREG_DIV1
, num
);
954 SRC_EA(env
, den
, OS_LONG
, 0, NULL
);
955 tcg_gen_mov_i32(QREG_DIV2
, den
);
957 gen_helper_divs(cpu_env
, tcg_const_i32(0));
959 gen_helper_divu(cpu_env
, tcg_const_i32(0));
961 if ((ext
& 7) == ((ext
>> 12) & 7)) {
963 tcg_gen_mov_i32 (reg
, QREG_DIV1
);
966 tcg_gen_mov_i32 (reg
, QREG_DIV2
);
968 s
->cc_op
= CC_OP_FLAGS
;
980 add
= (insn
& 0x4000) != 0;
982 dest
= tcg_temp_new();
984 SRC_EA(env
, tmp
, OS_LONG
, 0, &addr
);
988 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
991 tcg_gen_add_i32(dest
, tmp
, src
);
992 gen_helper_xflag_lt(QREG_CC_X
, dest
, src
);
993 s
->cc_op
= CC_OP_ADD
;
995 gen_helper_xflag_lt(QREG_CC_X
, tmp
, src
);
996 tcg_gen_sub_i32(dest
, tmp
, src
);
997 s
->cc_op
= CC_OP_SUB
;
999 gen_update_cc_add(dest
, src
);
1001 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1003 tcg_gen_mov_i32(reg
, dest
);
1008 /* Reverse the order of the bits in REG. */
1012 reg
= DREG(insn
, 0);
1013 gen_helper_bitrev(reg
, reg
);
1016 DISAS_INSN(bitop_reg
)
1026 if ((insn
& 0x38) != 0)
1030 op
= (insn
>> 6) & 3;
1031 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1032 src2
= DREG(insn
, 9);
1033 dest
= tcg_temp_new();
1036 tmp
= tcg_temp_new();
1037 if (opsize
== OS_BYTE
)
1038 tcg_gen_andi_i32(tmp
, src2
, 7);
1040 tcg_gen_andi_i32(tmp
, src2
, 31);
1042 tmp
= tcg_temp_new();
1043 tcg_gen_shr_i32(tmp
, src1
, src2
);
1044 tcg_gen_andi_i32(tmp
, tmp
, 1);
1045 tcg_gen_shli_i32(tmp
, tmp
, 2);
1046 /* Clear CCF_Z if bit set. */
1047 tcg_gen_ori_i32(QREG_CC_DEST
, QREG_CC_DEST
, CCF_Z
);
1048 tcg_gen_xor_i32(QREG_CC_DEST
, QREG_CC_DEST
, tmp
);
1050 tcg_gen_shl_i32(tmp
, tcg_const_i32(1), src2
);
1053 tcg_gen_xor_i32(dest
, src1
, tmp
);
1056 tcg_gen_not_i32(tmp
, tmp
);
1057 tcg_gen_and_i32(dest
, src1
, tmp
);
1060 tcg_gen_or_i32(dest
, src1
, tmp
);
1066 DEST_EA(env
, insn
, opsize
, dest
, &addr
);
1072 reg
= DREG(insn
, 0);
1074 gen_helper_sats(reg
, reg
, QREG_CC_DEST
);
1075 gen_logic_cc(s
, reg
);
1078 static void gen_push(DisasContext
*s
, TCGv val
)
1082 tmp
= tcg_temp_new();
1083 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1084 gen_store(s
, OS_LONG
, tmp
, val
);
1085 tcg_gen_mov_i32(QREG_SP
, tmp
);
1097 mask
= cpu_lduw_code(env
, s
->pc
);
1099 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1100 if (IS_NULL_QREG(tmp
)) {
1104 addr
= tcg_temp_new();
1105 tcg_gen_mov_i32(addr
, tmp
);
1106 is_load
= ((insn
& 0x0400) != 0);
1107 for (i
= 0; i
< 16; i
++, mask
>>= 1) {
1114 tmp
= gen_load(s
, OS_LONG
, addr
, 0);
1115 tcg_gen_mov_i32(reg
, tmp
);
1117 gen_store(s
, OS_LONG
, addr
, reg
);
1120 tcg_gen_addi_i32(addr
, addr
, 4);
1125 DISAS_INSN(bitop_im
)
1135 if ((insn
& 0x38) != 0)
1139 op
= (insn
>> 6) & 3;
1141 bitnum
= cpu_lduw_code(env
, s
->pc
);
1143 if (bitnum
& 0xff00) {
1144 disas_undef(env
, s
, insn
);
1148 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1151 if (opsize
== OS_BYTE
)
1157 tmp
= tcg_temp_new();
1158 assert (CCF_Z
== (1 << 2));
1160 tcg_gen_shri_i32(tmp
, src1
, bitnum
- 2);
1161 else if (bitnum
< 2)
1162 tcg_gen_shli_i32(tmp
, src1
, 2 - bitnum
);
1164 tcg_gen_mov_i32(tmp
, src1
);
1165 tcg_gen_andi_i32(tmp
, tmp
, CCF_Z
);
1166 /* Clear CCF_Z if bit set. */
1167 tcg_gen_ori_i32(QREG_CC_DEST
, QREG_CC_DEST
, CCF_Z
);
1168 tcg_gen_xor_i32(QREG_CC_DEST
, QREG_CC_DEST
, tmp
);
1172 tcg_gen_xori_i32(tmp
, src1
, mask
);
1175 tcg_gen_andi_i32(tmp
, src1
, ~mask
);
1178 tcg_gen_ori_i32(tmp
, src1
, mask
);
1183 DEST_EA(env
, insn
, opsize
, tmp
, &addr
);
1187 DISAS_INSN(arith_im
)
1195 op
= (insn
>> 9) & 7;
1196 SRC_EA(env
, src1
, OS_LONG
, 0, (op
== 6) ? NULL
: &addr
);
1197 im
= read_im32(env
, s
);
1198 dest
= tcg_temp_new();
1201 tcg_gen_ori_i32(dest
, src1
, im
);
1202 gen_logic_cc(s
, dest
);
1205 tcg_gen_andi_i32(dest
, src1
, im
);
1206 gen_logic_cc(s
, dest
);
1209 tcg_gen_mov_i32(dest
, src1
);
1210 gen_helper_xflag_lt(QREG_CC_X
, dest
, tcg_const_i32(im
));
1211 tcg_gen_subi_i32(dest
, dest
, im
);
1212 gen_update_cc_add(dest
, tcg_const_i32(im
));
1213 s
->cc_op
= CC_OP_SUB
;
1216 tcg_gen_mov_i32(dest
, src1
);
1217 tcg_gen_addi_i32(dest
, dest
, im
);
1218 gen_update_cc_add(dest
, tcg_const_i32(im
));
1219 gen_helper_xflag_lt(QREG_CC_X
, dest
, tcg_const_i32(im
));
1220 s
->cc_op
= CC_OP_ADD
;
1223 tcg_gen_xori_i32(dest
, src1
, im
);
1224 gen_logic_cc(s
, dest
);
1227 tcg_gen_mov_i32(dest
, src1
);
1228 tcg_gen_subi_i32(dest
, dest
, im
);
1229 gen_update_cc_add(dest
, tcg_const_i32(im
));
1230 s
->cc_op
= CC_OP_SUB
;
1236 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1244 reg
= DREG(insn
, 0);
1245 tcg_gen_bswap32_i32(reg
, reg
);
1255 switch (insn
>> 12) {
1256 case 1: /* move.b */
1259 case 2: /* move.l */
1262 case 3: /* move.w */
1268 SRC_EA(env
, src
, opsize
, 1, NULL
);
1269 op
= (insn
>> 6) & 7;
1272 /* The value will already have been sign extended. */
1273 dest
= AREG(insn
, 9);
1274 tcg_gen_mov_i32(dest
, src
);
1278 dest_ea
= ((insn
>> 9) & 7) | (op
<< 3);
1279 DEST_EA(env
, dest_ea
, opsize
, src
, NULL
);
1280 /* This will be correct because loads sign extend. */
1281 gen_logic_cc(s
, src
);
1290 reg
= DREG(insn
, 0);
1291 gen_helper_subx_cc(reg
, cpu_env
, tcg_const_i32(0), reg
);
1299 reg
= AREG(insn
, 9);
1300 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1301 if (IS_NULL_QREG(tmp
)) {
1305 tcg_gen_mov_i32(reg
, tmp
);
1312 switch ((insn
>> 6) & 3) {
1325 DEST_EA(env
, insn
, opsize
, tcg_const_i32(0), NULL
);
1326 gen_logic_cc(s
, tcg_const_i32(0));
1329 static TCGv
gen_get_ccr(DisasContext
*s
)
1334 dest
= tcg_temp_new();
1335 tcg_gen_shli_i32(dest
, QREG_CC_X
, 4);
1336 tcg_gen_or_i32(dest
, dest
, QREG_CC_DEST
);
1340 DISAS_INSN(move_from_ccr
)
1345 ccr
= gen_get_ccr(s
);
1346 reg
= DREG(insn
, 0);
1347 gen_partset_reg(OS_WORD
, reg
, ccr
);
1355 reg
= DREG(insn
, 0);
1356 src1
= tcg_temp_new();
1357 tcg_gen_mov_i32(src1
, reg
);
1358 tcg_gen_neg_i32(reg
, src1
);
1359 s
->cc_op
= CC_OP_SUB
;
1360 gen_update_cc_add(reg
, src1
);
1361 gen_helper_xflag_lt(QREG_CC_X
, tcg_const_i32(0), src1
);
1362 s
->cc_op
= CC_OP_SUB
;
1365 static void gen_set_sr_im(DisasContext
*s
, uint16_t val
, int ccr_only
)
1367 tcg_gen_movi_i32(QREG_CC_DEST
, val
& 0xf);
1368 tcg_gen_movi_i32(QREG_CC_X
, (val
& 0x10) >> 4);
1370 gen_helper_set_sr(cpu_env
, tcg_const_i32(val
& 0xff00));
1374 static void gen_set_sr(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
1380 s
->cc_op
= CC_OP_FLAGS
;
1381 if ((insn
& 0x38) == 0)
1383 tmp
= tcg_temp_new();
1384 reg
= DREG(insn
, 0);
1385 tcg_gen_andi_i32(QREG_CC_DEST
, reg
, 0xf);
1386 tcg_gen_shri_i32(tmp
, reg
, 4);
1387 tcg_gen_andi_i32(QREG_CC_X
, tmp
, 1);
1389 gen_helper_set_sr(cpu_env
, reg
);
1392 else if ((insn
& 0x3f) == 0x3c)
1395 val
= cpu_lduw_code(env
, s
->pc
);
1397 gen_set_sr_im(s
, val
, ccr_only
);
1400 disas_undef(env
, s
, insn
);
1403 DISAS_INSN(move_to_ccr
)
1405 gen_set_sr(env
, s
, insn
, 1);
1412 reg
= DREG(insn
, 0);
1413 tcg_gen_not_i32(reg
, reg
);
1414 gen_logic_cc(s
, reg
);
1423 src1
= tcg_temp_new();
1424 src2
= tcg_temp_new();
1425 reg
= DREG(insn
, 0);
1426 tcg_gen_shli_i32(src1
, reg
, 16);
1427 tcg_gen_shri_i32(src2
, reg
, 16);
1428 tcg_gen_or_i32(reg
, src1
, src2
);
1429 gen_logic_cc(s
, reg
);
1436 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1437 if (IS_NULL_QREG(tmp
)) {
1450 reg
= DREG(insn
, 0);
1451 op
= (insn
>> 6) & 7;
1452 tmp
= tcg_temp_new();
1454 tcg_gen_ext16s_i32(tmp
, reg
);
1456 tcg_gen_ext8s_i32(tmp
, reg
);
1458 gen_partset_reg(OS_WORD
, reg
, tmp
);
1460 tcg_gen_mov_i32(reg
, tmp
);
1461 gen_logic_cc(s
, tmp
);
1469 switch ((insn
>> 6) & 3) {
1482 SRC_EA(env
, tmp
, opsize
, 1, NULL
);
1483 gen_logic_cc(s
, tmp
);
1488 /* Implemented as a NOP. */
1493 gen_exception(s
, s
->pc
- 2, EXCP_ILLEGAL
);
1496 /* ??? This should be atomic. */
1503 dest
= tcg_temp_new();
1504 SRC_EA(env
, src1
, OS_BYTE
, 1, &addr
);
1505 gen_logic_cc(s
, src1
);
1506 tcg_gen_ori_i32(dest
, src1
, 0x80);
1507 DEST_EA(env
, insn
, OS_BYTE
, dest
, &addr
);
1517 /* The upper 32 bits of the product are discarded, so
1518 muls.l and mulu.l are functionally equivalent. */
1519 ext
= cpu_lduw_code(env
, s
->pc
);
1522 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
1525 reg
= DREG(ext
, 12);
1526 SRC_EA(env
, src1
, OS_LONG
, 0, NULL
);
1527 dest
= tcg_temp_new();
1528 tcg_gen_mul_i32(dest
, src1
, reg
);
1529 tcg_gen_mov_i32(reg
, dest
);
1530 /* Unlike m68k, coldfire always clears the overflow bit. */
1531 gen_logic_cc(s
, dest
);
1540 offset
= cpu_ldsw_code(env
, s
->pc
);
1542 reg
= AREG(insn
, 0);
1543 tmp
= tcg_temp_new();
1544 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1545 gen_store(s
, OS_LONG
, tmp
, reg
);
1546 if ((insn
& 7) != 7)
1547 tcg_gen_mov_i32(reg
, tmp
);
1548 tcg_gen_addi_i32(QREG_SP
, tmp
, offset
);
1557 src
= tcg_temp_new();
1558 reg
= AREG(insn
, 0);
1559 tcg_gen_mov_i32(src
, reg
);
1560 tmp
= gen_load(s
, OS_LONG
, src
, 0);
1561 tcg_gen_mov_i32(reg
, tmp
);
1562 tcg_gen_addi_i32(QREG_SP
, src
, 4);
1573 tmp
= gen_load(s
, OS_LONG
, QREG_SP
, 0);
1574 tcg_gen_addi_i32(QREG_SP
, QREG_SP
, 4);
1582 /* Load the target address first to ensure correct exception
1584 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1585 if (IS_NULL_QREG(tmp
)) {
1589 if ((insn
& 0x40) == 0) {
1591 gen_push(s
, tcg_const_i32(s
->pc
));
1604 SRC_EA(env
, src1
, OS_LONG
, 0, &addr
);
1605 val
= (insn
>> 9) & 7;
1608 dest
= tcg_temp_new();
1609 tcg_gen_mov_i32(dest
, src1
);
1610 if ((insn
& 0x38) == 0x08) {
1611 /* Don't update condition codes if the destination is an
1612 address register. */
1613 if (insn
& 0x0100) {
1614 tcg_gen_subi_i32(dest
, dest
, val
);
1616 tcg_gen_addi_i32(dest
, dest
, val
);
1619 src2
= tcg_const_i32(val
);
1620 if (insn
& 0x0100) {
1621 gen_helper_xflag_lt(QREG_CC_X
, dest
, src2
);
1622 tcg_gen_subi_i32(dest
, dest
, val
);
1623 s
->cc_op
= CC_OP_SUB
;
1625 tcg_gen_addi_i32(dest
, dest
, val
);
1626 gen_helper_xflag_lt(QREG_CC_X
, dest
, src2
);
1627 s
->cc_op
= CC_OP_ADD
;
1629 gen_update_cc_add(dest
, src2
);
1631 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1637 case 2: /* One extension word. */
1640 case 3: /* Two extension words. */
1643 case 4: /* No extension words. */
1646 disas_undef(env
, s
, insn
);
1658 op
= (insn
>> 8) & 0xf;
1659 offset
= (int8_t)insn
;
1661 offset
= cpu_ldsw_code(env
, s
->pc
);
1663 } else if (offset
== -1) {
1664 offset
= read_im32(env
, s
);
1668 gen_push(s
, tcg_const_i32(s
->pc
));
1673 l1
= gen_new_label();
1674 gen_jmpcc(s
, ((insn
>> 8) & 0xf) ^ 1, l1
);
1675 gen_jmp_tb(s
, 1, base
+ offset
);
1677 gen_jmp_tb(s
, 0, s
->pc
);
1679 /* Unconditional branch. */
1680 gen_jmp_tb(s
, 0, base
+ offset
);
1689 tcg_gen_movi_i32(DREG(insn
, 9), val
);
1690 gen_logic_cc(s
, tcg_const_i32(val
));
1703 SRC_EA(env
, src
, opsize
, (insn
& 0x80) == 0, NULL
);
1704 reg
= DREG(insn
, 9);
1705 tcg_gen_mov_i32(reg
, src
);
1706 gen_logic_cc(s
, src
);
1716 reg
= DREG(insn
, 9);
1717 dest
= tcg_temp_new();
1719 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1720 tcg_gen_or_i32(dest
, src
, reg
);
1721 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1723 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1724 tcg_gen_or_i32(dest
, src
, reg
);
1725 tcg_gen_mov_i32(reg
, dest
);
1727 gen_logic_cc(s
, dest
);
1735 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1736 reg
= AREG(insn
, 9);
1737 tcg_gen_sub_i32(reg
, reg
, src
);
1746 reg
= DREG(insn
, 9);
1747 src
= DREG(insn
, 0);
1748 gen_helper_subx_cc(reg
, cpu_env
, reg
, src
);
1756 val
= (insn
>> 9) & 7;
1759 src
= tcg_const_i32(val
);
1760 gen_logic_cc(s
, src
);
1761 DEST_EA(env
, insn
, OS_LONG
, src
, NULL
);
1772 op
= (insn
>> 6) & 3;
1776 s
->cc_op
= CC_OP_CMPB
;
1780 s
->cc_op
= CC_OP_CMPW
;
1784 s
->cc_op
= CC_OP_SUB
;
1789 SRC_EA(env
, src
, opsize
, 1, NULL
);
1790 reg
= DREG(insn
, 9);
1791 dest
= tcg_temp_new();
1792 tcg_gen_sub_i32(dest
, reg
, src
);
1793 gen_update_cc_add(dest
, src
);
1808 SRC_EA(env
, src
, opsize
, 1, NULL
);
1809 reg
= AREG(insn
, 9);
1810 dest
= tcg_temp_new();
1811 tcg_gen_sub_i32(dest
, reg
, src
);
1812 gen_update_cc_add(dest
, src
);
1813 s
->cc_op
= CC_OP_SUB
;
1823 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1824 reg
= DREG(insn
, 9);
1825 dest
= tcg_temp_new();
1826 tcg_gen_xor_i32(dest
, src
, reg
);
1827 gen_logic_cc(s
, dest
);
1828 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1838 reg
= DREG(insn
, 9);
1839 dest
= tcg_temp_new();
1841 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1842 tcg_gen_and_i32(dest
, src
, reg
);
1843 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1845 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1846 tcg_gen_and_i32(dest
, src
, reg
);
1847 tcg_gen_mov_i32(reg
, dest
);
1849 gen_logic_cc(s
, dest
);
1857 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1858 reg
= AREG(insn
, 9);
1859 tcg_gen_add_i32(reg
, reg
, src
);
1868 reg
= DREG(insn
, 9);
1869 src
= DREG(insn
, 0);
1870 gen_helper_addx_cc(reg
, cpu_env
, reg
, src
);
1871 s
->cc_op
= CC_OP_FLAGS
;
1874 /* TODO: This could be implemented without helper functions. */
1875 DISAS_INSN(shift_im
)
1881 reg
= DREG(insn
, 0);
1882 tmp
= (insn
>> 9) & 7;
1885 shift
= tcg_const_i32(tmp
);
1886 /* No need to flush flags becuse we know we will set C flag. */
1888 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1891 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1893 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1896 s
->cc_op
= CC_OP_SHIFT
;
1899 DISAS_INSN(shift_reg
)
1904 reg
= DREG(insn
, 0);
1905 shift
= DREG(insn
, 9);
1906 /* Shift by zero leaves C flag unmodified. */
1909 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1912 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1914 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1917 s
->cc_op
= CC_OP_SHIFT
;
1923 reg
= DREG(insn
, 0);
1924 gen_logic_cc(s
, reg
);
1925 gen_helper_ff1(reg
, reg
);
1928 static TCGv
gen_get_sr(DisasContext
*s
)
1933 ccr
= gen_get_ccr(s
);
1934 sr
= tcg_temp_new();
1935 tcg_gen_andi_i32(sr
, QREG_SR
, 0xffe0);
1936 tcg_gen_or_i32(sr
, sr
, ccr
);
1946 ext
= cpu_lduw_code(env
, s
->pc
);
1948 if (ext
!= 0x46FC) {
1949 gen_exception(s
, addr
, EXCP_UNSUPPORTED
);
1952 ext
= cpu_lduw_code(env
, s
->pc
);
1954 if (IS_USER(s
) || (ext
& SR_S
) == 0) {
1955 gen_exception(s
, addr
, EXCP_PRIVILEGE
);
1958 gen_push(s
, gen_get_sr(s
));
1959 gen_set_sr_im(s
, ext
, 0);
1962 DISAS_INSN(move_from_sr
)
1968 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1972 reg
= DREG(insn
, 0);
1973 gen_partset_reg(OS_WORD
, reg
, sr
);
1976 DISAS_INSN(move_to_sr
)
1979 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1982 gen_set_sr(env
, s
, insn
, 0);
1986 DISAS_INSN(move_from_usp
)
1989 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1992 tcg_gen_ld_i32(AREG(insn
, 0), cpu_env
,
1993 offsetof(CPUM68KState
, sp
[M68K_USP
]));
1996 DISAS_INSN(move_to_usp
)
1999 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2002 tcg_gen_st_i32(AREG(insn
, 0), cpu_env
,
2003 offsetof(CPUM68KState
, sp
[M68K_USP
]));
2008 gen_exception(s
, s
->pc
, EXCP_HALT_INSN
);
2016 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2020 ext
= cpu_lduw_code(env
, s
->pc
);
2023 gen_set_sr_im(s
, ext
, 0);
2024 tcg_gen_movi_i32(cpu_halted
, 1);
2025 gen_exception(s
, s
->pc
, EXCP_HLT
);
2031 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2034 gen_exception(s
, s
->pc
- 2, EXCP_RTE
);
2043 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2047 ext
= cpu_lduw_code(env
, s
->pc
);
2051 reg
= AREG(ext
, 12);
2053 reg
= DREG(ext
, 12);
2055 gen_helper_movec(cpu_env
, tcg_const_i32(ext
& 0xfff), reg
);
2062 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2065 /* ICache fetch. Implement as no-op. */
2071 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2074 /* Cache push/invalidate. Implement as no-op. */
2079 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2084 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2087 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2090 /* TODO: Implement wdebug. */
2091 cpu_abort(CPU(cpu
), "WDEBUG not implemented");
2096 gen_exception(s
, s
->pc
- 2, EXCP_TRAP0
+ (insn
& 0xf));
2099 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2100 immediately before the next FP instruction is executed. */
2114 ext
= cpu_lduw_code(env
, s
->pc
);
2116 opmode
= ext
& 0x7f;
2117 switch ((ext
>> 13) & 7) {
2122 case 3: /* fmove out */
2124 tmp32
= tcg_temp_new_i32();
2126 /* ??? TODO: Proper behavior on overflow. */
2127 switch ((ext
>> 10) & 7) {
2130 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2134 gen_helper_f64_to_f32(tmp32
, cpu_env
, src
);
2138 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2140 case 5: /* OS_DOUBLE */
2141 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2142 switch ((insn
>> 3) & 7) {
2147 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2150 offset
= cpu_ldsw_code(env
, s
->pc
);
2152 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2157 gen_store64(s
, tmp32
, src
);
2158 switch ((insn
>> 3) & 7) {
2160 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2161 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2164 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2167 tcg_temp_free_i32(tmp32
);
2171 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2176 DEST_EA(env
, insn
, opsize
, tmp32
, NULL
);
2177 tcg_temp_free_i32(tmp32
);
2179 case 4: /* fmove to control register. */
2180 switch ((ext
>> 10) & 7) {
2182 /* Not implemented. Ignore writes. */
2187 cpu_abort(NULL
, "Unimplemented: fmove to control %d",
2191 case 5: /* fmove from control register. */
2192 switch ((ext
>> 10) & 7) {
2194 /* Not implemented. Always return zero. */
2195 tmp32
= tcg_const_i32(0);
2200 cpu_abort(NULL
, "Unimplemented: fmove from control %d",
2204 DEST_EA(env
, insn
, OS_LONG
, tmp32
, NULL
);
2206 case 6: /* fmovem */
2212 if ((ext
& 0x1f00) != 0x1000 || (ext
& 0xff) == 0)
2214 tmp32
= gen_lea(env
, s
, insn
, OS_LONG
);
2215 if (IS_NULL_QREG(tmp32
)) {
2219 addr
= tcg_temp_new_i32();
2220 tcg_gen_mov_i32(addr
, tmp32
);
2222 for (i
= 0; i
< 8; i
++) {
2225 if (ext
& (1 << 13)) {
2227 tcg_gen_qemu_stf64(dest
, addr
, IS_USER(s
));
2230 tcg_gen_qemu_ldf64(dest
, addr
, IS_USER(s
));
2232 if (ext
& (mask
- 1))
2233 tcg_gen_addi_i32(addr
, addr
, 8);
2237 tcg_temp_free_i32(addr
);
2241 if (ext
& (1 << 14)) {
2242 /* Source effective address. */
2243 switch ((ext
>> 10) & 7) {
2244 case 0: opsize
= OS_LONG
; break;
2245 case 1: opsize
= OS_SINGLE
; break;
2246 case 4: opsize
= OS_WORD
; break;
2247 case 5: opsize
= OS_DOUBLE
; break;
2248 case 6: opsize
= OS_BYTE
; break;
2252 if (opsize
== OS_DOUBLE
) {
2253 tmp32
= tcg_temp_new_i32();
2254 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2255 switch ((insn
>> 3) & 7) {
2260 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2263 offset
= cpu_ldsw_code(env
, s
->pc
);
2265 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2268 offset
= cpu_ldsw_code(env
, s
->pc
);
2269 offset
+= s
->pc
- 2;
2271 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2276 src
= gen_load64(s
, tmp32
);
2277 switch ((insn
>> 3) & 7) {
2279 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2280 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2283 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2286 tcg_temp_free_i32(tmp32
);
2288 SRC_EA(env
, tmp32
, opsize
, 1, NULL
);
2289 src
= tcg_temp_new_i64();
2294 gen_helper_i32_to_f64(src
, cpu_env
, tmp32
);
2297 gen_helper_f32_to_f64(src
, cpu_env
, tmp32
);
2302 /* Source register. */
2303 src
= FREG(ext
, 10);
2305 dest
= FREG(ext
, 7);
2306 res
= tcg_temp_new_i64();
2308 tcg_gen_mov_f64(res
, dest
);
2312 case 0: case 0x40: case 0x44: /* fmove */
2313 tcg_gen_mov_f64(res
, src
);
2316 gen_helper_iround_f64(res
, cpu_env
, src
);
2319 case 3: /* fintrz */
2320 gen_helper_itrunc_f64(res
, cpu_env
, src
);
2323 case 4: case 0x41: case 0x45: /* fsqrt */
2324 gen_helper_sqrt_f64(res
, cpu_env
, src
);
2326 case 0x18: case 0x58: case 0x5c: /* fabs */
2327 gen_helper_abs_f64(res
, src
);
2329 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2330 gen_helper_chs_f64(res
, src
);
2332 case 0x20: case 0x60: case 0x64: /* fdiv */
2333 gen_helper_div_f64(res
, cpu_env
, res
, src
);
2335 case 0x22: case 0x62: case 0x66: /* fadd */
2336 gen_helper_add_f64(res
, cpu_env
, res
, src
);
2338 case 0x23: case 0x63: case 0x67: /* fmul */
2339 gen_helper_mul_f64(res
, cpu_env
, res
, src
);
2341 case 0x28: case 0x68: case 0x6c: /* fsub */
2342 gen_helper_sub_f64(res
, cpu_env
, res
, src
);
2344 case 0x38: /* fcmp */
2345 gen_helper_sub_cmp_f64(res
, cpu_env
, res
, src
);
2349 case 0x3a: /* ftst */
2350 tcg_gen_mov_f64(res
, src
);
2357 if (ext
& (1 << 14)) {
2358 tcg_temp_free_i64(src
);
2361 if (opmode
& 0x40) {
2362 if ((opmode
& 0x4) != 0)
2364 } else if ((s
->fpcr
& M68K_FPCR_PREC
) == 0) {
2369 TCGv tmp
= tcg_temp_new_i32();
2370 gen_helper_f64_to_f32(tmp
, cpu_env
, res
);
2371 gen_helper_f32_to_f64(res
, cpu_env
, tmp
);
2372 tcg_temp_free_i32(tmp
);
2374 tcg_gen_mov_f64(QREG_FP_RESULT
, res
);
2376 tcg_gen_mov_f64(dest
, res
);
2378 tcg_temp_free_i64(res
);
2381 /* FIXME: Is this right for offset addressing modes? */
2383 disas_undef_fpu(env
, s
, insn
);
2394 offset
= cpu_ldsw_code(env
, s
->pc
);
2396 if (insn
& (1 << 6)) {
2397 offset
= (offset
<< 16) | cpu_lduw_code(env
, s
->pc
);
2401 l1
= gen_new_label();
2402 /* TODO: Raise BSUN exception. */
2403 flag
= tcg_temp_new();
2404 gen_helper_compare_f64(flag
, cpu_env
, QREG_FP_RESULT
);
2405 /* Jump to l1 if condition is true. */
2406 switch (insn
& 0xf) {
2409 case 1: /* eq (=0) */
2410 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2412 case 2: /* ogt (=1) */
2413 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(1), l1
);
2415 case 3: /* oge (=0 or =1) */
2416 tcg_gen_brcond_i32(TCG_COND_LEU
, flag
, tcg_const_i32(1), l1
);
2418 case 4: /* olt (=-1) */
2419 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(0), l1
);
2421 case 5: /* ole (=-1 or =0) */
2422 tcg_gen_brcond_i32(TCG_COND_LE
, flag
, tcg_const_i32(0), l1
);
2424 case 6: /* ogl (=-1 or =1) */
2425 tcg_gen_andi_i32(flag
, flag
, 1);
2426 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2428 case 7: /* or (=2) */
2429 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(2), l1
);
2431 case 8: /* un (<2) */
2432 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(2), l1
);
2434 case 9: /* ueq (=0 or =2) */
2435 tcg_gen_andi_i32(flag
, flag
, 1);
2436 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2438 case 10: /* ugt (>0) */
2439 tcg_gen_brcond_i32(TCG_COND_GT
, flag
, tcg_const_i32(0), l1
);
2441 case 11: /* uge (>=0) */
2442 tcg_gen_brcond_i32(TCG_COND_GE
, flag
, tcg_const_i32(0), l1
);
2444 case 12: /* ult (=-1 or =2) */
2445 tcg_gen_brcond_i32(TCG_COND_GEU
, flag
, tcg_const_i32(2), l1
);
2447 case 13: /* ule (!=1) */
2448 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(1), l1
);
2450 case 14: /* ne (!=0) */
2451 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2457 gen_jmp_tb(s
, 0, s
->pc
);
2459 gen_jmp_tb(s
, 1, addr
+ offset
);
2462 DISAS_INSN(frestore
)
2464 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2466 /* TODO: Implement frestore. */
2467 cpu_abort(CPU(cpu
), "FRESTORE not implemented");
2472 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2474 /* TODO: Implement fsave. */
2475 cpu_abort(CPU(cpu
), "FSAVE not implemented");
2478 static inline TCGv
gen_mac_extract_word(DisasContext
*s
, TCGv val
, int upper
)
2480 TCGv tmp
= tcg_temp_new();
2481 if (s
->env
->macsr
& MACSR_FI
) {
2483 tcg_gen_andi_i32(tmp
, val
, 0xffff0000);
2485 tcg_gen_shli_i32(tmp
, val
, 16);
2486 } else if (s
->env
->macsr
& MACSR_SU
) {
2488 tcg_gen_sari_i32(tmp
, val
, 16);
2490 tcg_gen_ext16s_i32(tmp
, val
);
2493 tcg_gen_shri_i32(tmp
, val
, 16);
2495 tcg_gen_ext16u_i32(tmp
, val
);
2500 static void gen_mac_clear_flags(void)
2502 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
,
2503 ~(MACSR_V
| MACSR_Z
| MACSR_N
| MACSR_EV
));
2519 s
->mactmp
= tcg_temp_new_i64();
2523 ext
= cpu_lduw_code(env
, s
->pc
);
2526 acc
= ((insn
>> 7) & 1) | ((ext
>> 3) & 2);
2527 dual
= ((insn
& 0x30) != 0 && (ext
& 3) != 0);
2528 if (dual
&& !m68k_feature(s
->env
, M68K_FEATURE_CF_EMAC_B
)) {
2529 disas_undef(env
, s
, insn
);
2533 /* MAC with load. */
2534 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
2535 addr
= tcg_temp_new();
2536 tcg_gen_and_i32(addr
, tmp
, QREG_MAC_MASK
);
2537 /* Load the value now to ensure correct exception behavior.
2538 Perform writeback after reading the MAC inputs. */
2539 loadval
= gen_load(s
, OS_LONG
, addr
, 0);
2542 rx
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(insn
, 12);
2543 ry
= (ext
& 8) ? AREG(ext
, 0) : DREG(ext
, 0);
2545 loadval
= addr
= NULL_QREG
;
2546 rx
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2547 ry
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2550 gen_mac_clear_flags();
2553 /* Disabled because conditional branches clobber temporary vars. */
2554 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && !dual
) {
2555 /* Skip the multiply if we know we will ignore it. */
2556 l1
= gen_new_label();
2557 tmp
= tcg_temp_new();
2558 tcg_gen_andi_i32(tmp
, QREG_MACSR
, 1 << (acc
+ 8));
2559 gen_op_jmp_nz32(tmp
, l1
);
2563 if ((ext
& 0x0800) == 0) {
2565 rx
= gen_mac_extract_word(s
, rx
, (ext
& 0x80) != 0);
2566 ry
= gen_mac_extract_word(s
, ry
, (ext
& 0x40) != 0);
2568 if (s
->env
->macsr
& MACSR_FI
) {
2569 gen_helper_macmulf(s
->mactmp
, cpu_env
, rx
, ry
);
2571 if (s
->env
->macsr
& MACSR_SU
)
2572 gen_helper_macmuls(s
->mactmp
, cpu_env
, rx
, ry
);
2574 gen_helper_macmulu(s
->mactmp
, cpu_env
, rx
, ry
);
2575 switch ((ext
>> 9) & 3) {
2577 tcg_gen_shli_i64(s
->mactmp
, s
->mactmp
, 1);
2580 tcg_gen_shri_i64(s
->mactmp
, s
->mactmp
, 1);
2586 /* Save the overflow flag from the multiply. */
2587 saved_flags
= tcg_temp_new();
2588 tcg_gen_mov_i32(saved_flags
, QREG_MACSR
);
2590 saved_flags
= NULL_QREG
;
2594 /* Disabled because conditional branches clobber temporary vars. */
2595 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && dual
) {
2596 /* Skip the accumulate if the value is already saturated. */
2597 l1
= gen_new_label();
2598 tmp
= tcg_temp_new();
2599 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2600 gen_op_jmp_nz32(tmp
, l1
);
2605 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2607 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2609 if (s
->env
->macsr
& MACSR_FI
)
2610 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2611 else if (s
->env
->macsr
& MACSR_SU
)
2612 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2614 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2617 /* Disabled because conditional branches clobber temporary vars. */
2623 /* Dual accumulate variant. */
2624 acc
= (ext
>> 2) & 3;
2625 /* Restore the overflow flag from the multiplier. */
2626 tcg_gen_mov_i32(QREG_MACSR
, saved_flags
);
2628 /* Disabled because conditional branches clobber temporary vars. */
2629 if ((s
->env
->macsr
& MACSR_OMC
) != 0) {
2630 /* Skip the accumulate if the value is already saturated. */
2631 l1
= gen_new_label();
2632 tmp
= tcg_temp_new();
2633 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2634 gen_op_jmp_nz32(tmp
, l1
);
2638 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2640 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2641 if (s
->env
->macsr
& MACSR_FI
)
2642 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2643 else if (s
->env
->macsr
& MACSR_SU
)
2644 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2646 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2648 /* Disabled because conditional branches clobber temporary vars. */
2653 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(acc
));
2657 rw
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2658 tcg_gen_mov_i32(rw
, loadval
);
2659 /* FIXME: Should address writeback happen with the masked or
2661 switch ((insn
>> 3) & 7) {
2662 case 3: /* Post-increment. */
2663 tcg_gen_addi_i32(AREG(insn
, 0), addr
, 4);
2665 case 4: /* Pre-decrement. */
2666 tcg_gen_mov_i32(AREG(insn
, 0), addr
);
2671 DISAS_INSN(from_mac
)
2677 rx
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2678 accnum
= (insn
>> 9) & 3;
2679 acc
= MACREG(accnum
);
2680 if (s
->env
->macsr
& MACSR_FI
) {
2681 gen_helper_get_macf(rx
, cpu_env
, acc
);
2682 } else if ((s
->env
->macsr
& MACSR_OMC
) == 0) {
2683 tcg_gen_extrl_i64_i32(rx
, acc
);
2684 } else if (s
->env
->macsr
& MACSR_SU
) {
2685 gen_helper_get_macs(rx
, acc
);
2687 gen_helper_get_macu(rx
, acc
);
2690 tcg_gen_movi_i64(acc
, 0);
2691 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2695 DISAS_INSN(move_mac
)
2697 /* FIXME: This can be done without a helper. */
2701 dest
= tcg_const_i32((insn
>> 9) & 3);
2702 gen_helper_mac_move(cpu_env
, dest
, tcg_const_i32(src
));
2703 gen_mac_clear_flags();
2704 gen_helper_mac_set_flags(cpu_env
, dest
);
2707 DISAS_INSN(from_macsr
)
2711 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2712 tcg_gen_mov_i32(reg
, QREG_MACSR
);
2715 DISAS_INSN(from_mask
)
2718 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2719 tcg_gen_mov_i32(reg
, QREG_MAC_MASK
);
2722 DISAS_INSN(from_mext
)
2726 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2727 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2728 if (s
->env
->macsr
& MACSR_FI
)
2729 gen_helper_get_mac_extf(reg
, cpu_env
, acc
);
2731 gen_helper_get_mac_exti(reg
, cpu_env
, acc
);
2734 DISAS_INSN(macsr_to_ccr
)
2736 tcg_gen_movi_i32(QREG_CC_X
, 0);
2737 tcg_gen_andi_i32(QREG_CC_DEST
, QREG_MACSR
, 0xf);
2738 s
->cc_op
= CC_OP_FLAGS
;
2746 accnum
= (insn
>> 9) & 3;
2747 acc
= MACREG(accnum
);
2748 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2749 if (s
->env
->macsr
& MACSR_FI
) {
2750 tcg_gen_ext_i32_i64(acc
, val
);
2751 tcg_gen_shli_i64(acc
, acc
, 8);
2752 } else if (s
->env
->macsr
& MACSR_SU
) {
2753 tcg_gen_ext_i32_i64(acc
, val
);
2755 tcg_gen_extu_i32_i64(acc
, val
);
2757 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2758 gen_mac_clear_flags();
2759 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(accnum
));
2762 DISAS_INSN(to_macsr
)
2765 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2766 gen_helper_set_macsr(cpu_env
, val
);
2773 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2774 tcg_gen_ori_i32(QREG_MAC_MASK
, val
, 0xffff0000);
2781 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2782 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2783 if (s
->env
->macsr
& MACSR_FI
)
2784 gen_helper_set_mac_extf(cpu_env
, val
, acc
);
2785 else if (s
->env
->macsr
& MACSR_SU
)
2786 gen_helper_set_mac_exts(cpu_env
, val
, acc
);
2788 gen_helper_set_mac_extu(cpu_env
, val
, acc
);
2791 static disas_proc opcode_table
[65536];
2794 register_opcode (disas_proc proc
, uint16_t opcode
, uint16_t mask
)
2800 /* Sanity check. All set bits must be included in the mask. */
2801 if (opcode
& ~mask
) {
2803 "qemu internal error: bogus opcode definition %04x/%04x\n",
2807 /* This could probably be cleverer. For now just optimize the case where
2808 the top bits are known. */
2809 /* Find the first zero bit in the mask. */
2811 while ((i
& mask
) != 0)
2813 /* Iterate over all combinations of this and lower bits. */
2818 from
= opcode
& ~(i
- 1);
2820 for (i
= from
; i
< to
; i
++) {
2821 if ((i
& mask
) == opcode
)
2822 opcode_table
[i
] = proc
;
2826 /* Register m68k opcode handlers. Order is important.
2827 Later insn override earlier ones. */
2828 void register_m68k_insns (CPUM68KState
*env
)
2830 #define INSN(name, opcode, mask, feature) do { \
2831 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2832 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2834 INSN(undef
, 0000, 0000, CF_ISA_A
);
2835 INSN(arith_im
, 0080, fff8
, CF_ISA_A
);
2836 INSN(bitrev
, 00c0
, fff8
, CF_ISA_APLUSC
);
2837 INSN(bitop_reg
, 0100, f1c0
, CF_ISA_A
);
2838 INSN(bitop_reg
, 0140, f1c0
, CF_ISA_A
);
2839 INSN(bitop_reg
, 0180, f1c0
, CF_ISA_A
);
2840 INSN(bitop_reg
, 01c0
, f1c0
, CF_ISA_A
);
2841 INSN(arith_im
, 0280, fff8
, CF_ISA_A
);
2842 INSN(byterev
, 02c0
, fff8
, CF_ISA_APLUSC
);
2843 INSN(arith_im
, 0480, fff8
, CF_ISA_A
);
2844 INSN(ff1
, 04c0
, fff8
, CF_ISA_APLUSC
);
2845 INSN(arith_im
, 0680, fff8
, CF_ISA_A
);
2846 INSN(bitop_im
, 0800, ffc0
, CF_ISA_A
);
2847 INSN(bitop_im
, 0840, ffc0
, CF_ISA_A
);
2848 INSN(bitop_im
, 0880, ffc0
, CF_ISA_A
);
2849 INSN(bitop_im
, 08c0
, ffc0
, CF_ISA_A
);
2850 INSN(arith_im
, 0a80
, fff8
, CF_ISA_A
);
2851 INSN(arith_im
, 0c00
, ff38
, CF_ISA_A
);
2852 INSN(move
, 1000, f000
, CF_ISA_A
);
2853 INSN(move
, 2000, f000
, CF_ISA_A
);
2854 INSN(move
, 3000, f000
, CF_ISA_A
);
2855 INSN(strldsr
, 40e7
, ffff
, CF_ISA_APLUSC
);
2856 INSN(negx
, 4080, fff8
, CF_ISA_A
);
2857 INSN(move_from_sr
, 40c0
, fff8
, CF_ISA_A
);
2858 INSN(lea
, 41c0
, f1c0
, CF_ISA_A
);
2859 INSN(clr
, 4200, ff00
, CF_ISA_A
);
2860 INSN(undef
, 42c0
, ffc0
, CF_ISA_A
);
2861 INSN(move_from_ccr
, 42c0
, fff8
, CF_ISA_A
);
2862 INSN(neg
, 4480, fff8
, CF_ISA_A
);
2863 INSN(move_to_ccr
, 44c0
, ffc0
, CF_ISA_A
);
2864 INSN(not, 4680, fff8
, CF_ISA_A
);
2865 INSN(move_to_sr
, 46c0
, ffc0
, CF_ISA_A
);
2866 INSN(pea
, 4840, ffc0
, CF_ISA_A
);
2867 INSN(swap
, 4840, fff8
, CF_ISA_A
);
2868 INSN(movem
, 48c0
, fbc0
, CF_ISA_A
);
2869 INSN(ext
, 4880, fff8
, CF_ISA_A
);
2870 INSN(ext
, 48c0
, fff8
, CF_ISA_A
);
2871 INSN(ext
, 49c0
, fff8
, CF_ISA_A
);
2872 INSN(tst
, 4a00
, ff00
, CF_ISA_A
);
2873 INSN(tas
, 4ac0
, ffc0
, CF_ISA_B
);
2874 INSN(halt
, 4ac8
, ffff
, CF_ISA_A
);
2875 INSN(pulse
, 4acc
, ffff
, CF_ISA_A
);
2876 INSN(illegal
, 4afc
, ffff
, CF_ISA_A
);
2877 INSN(mull
, 4c00
, ffc0
, CF_ISA_A
);
2878 INSN(divl
, 4c40
, ffc0
, CF_ISA_A
);
2879 INSN(sats
, 4c80
, fff8
, CF_ISA_B
);
2880 INSN(trap
, 4e40
, fff0
, CF_ISA_A
);
2881 INSN(link
, 4e50
, fff8
, CF_ISA_A
);
2882 INSN(unlk
, 4e58
, fff8
, CF_ISA_A
);
2883 INSN(move_to_usp
, 4e60
, fff8
, USP
);
2884 INSN(move_from_usp
, 4e68
, fff8
, USP
);
2885 INSN(nop
, 4e71
, ffff
, CF_ISA_A
);
2886 INSN(stop
, 4e72
, ffff
, CF_ISA_A
);
2887 INSN(rte
, 4e73
, ffff
, CF_ISA_A
);
2888 INSN(rts
, 4e75
, ffff
, CF_ISA_A
);
2889 INSN(movec
, 4e7b
, ffff
, CF_ISA_A
);
2890 INSN(jump
, 4e80
, ffc0
, CF_ISA_A
);
2891 INSN(jump
, 4ec0
, ffc0
, CF_ISA_A
);
2892 INSN(addsubq
, 5180, f1c0
, CF_ISA_A
);
2893 INSN(scc
, 50c0
, f0f8
, CF_ISA_A
);
2894 INSN(addsubq
, 5080, f1c0
, CF_ISA_A
);
2895 INSN(tpf
, 51f8
, fff8
, CF_ISA_A
);
2897 /* Branch instructions. */
2898 INSN(branch
, 6000, f000
, CF_ISA_A
);
2899 /* Disable long branch instructions, then add back the ones we want. */
2900 INSN(undef
, 60ff
, f0ff
, CF_ISA_A
); /* All long branches. */
2901 INSN(branch
, 60ff
, f0ff
, CF_ISA_B
);
2902 INSN(undef
, 60ff
, ffff
, CF_ISA_B
); /* bra.l */
2903 INSN(branch
, 60ff
, ffff
, BRAL
);
2905 INSN(moveq
, 7000, f100
, CF_ISA_A
);
2906 INSN(mvzs
, 7100, f100
, CF_ISA_B
);
2907 INSN(or, 8000, f000
, CF_ISA_A
);
2908 INSN(divw
, 80c0
, f0c0
, CF_ISA_A
);
2909 INSN(addsub
, 9000, f000
, CF_ISA_A
);
2910 INSN(subx
, 9180, f1f8
, CF_ISA_A
);
2911 INSN(suba
, 91c0
, f1c0
, CF_ISA_A
);
2913 INSN(undef_mac
, a000
, f000
, CF_ISA_A
);
2914 INSN(mac
, a000
, f100
, CF_EMAC
);
2915 INSN(from_mac
, a180
, f9b0
, CF_EMAC
);
2916 INSN(move_mac
, a110
, f9fc
, CF_EMAC
);
2917 INSN(from_macsr
,a980
, f9f0
, CF_EMAC
);
2918 INSN(from_mask
, ad80
, fff0
, CF_EMAC
);
2919 INSN(from_mext
, ab80
, fbf0
, CF_EMAC
);
2920 INSN(macsr_to_ccr
, a9c0
, ffff
, CF_EMAC
);
2921 INSN(to_mac
, a100
, f9c0
, CF_EMAC
);
2922 INSN(to_macsr
, a900
, ffc0
, CF_EMAC
);
2923 INSN(to_mext
, ab00
, fbc0
, CF_EMAC
);
2924 INSN(to_mask
, ad00
, ffc0
, CF_EMAC
);
2926 INSN(mov3q
, a140
, f1c0
, CF_ISA_B
);
2927 INSN(cmp
, b000
, f1c0
, CF_ISA_B
); /* cmp.b */
2928 INSN(cmp
, b040
, f1c0
, CF_ISA_B
); /* cmp.w */
2929 INSN(cmpa
, b0c0
, f1c0
, CF_ISA_B
); /* cmpa.w */
2930 INSN(cmp
, b080
, f1c0
, CF_ISA_A
);
2931 INSN(cmpa
, b1c0
, f1c0
, CF_ISA_A
);
2932 INSN(eor
, b180
, f1c0
, CF_ISA_A
);
2933 INSN(and, c000
, f000
, CF_ISA_A
);
2934 INSN(mulw
, c0c0
, f0c0
, CF_ISA_A
);
2935 INSN(addsub
, d000
, f000
, CF_ISA_A
);
2936 INSN(addx
, d180
, f1f8
, CF_ISA_A
);
2937 INSN(adda
, d1c0
, f1c0
, CF_ISA_A
);
2938 INSN(shift_im
, e080
, f0f0
, CF_ISA_A
);
2939 INSN(shift_reg
, e0a0
, f0f0
, CF_ISA_A
);
2940 INSN(undef_fpu
, f000
, f000
, CF_ISA_A
);
2941 INSN(fpu
, f200
, ffc0
, CF_FPU
);
2942 INSN(fbcc
, f280
, ffc0
, CF_FPU
);
2943 INSN(frestore
, f340
, ffc0
, CF_FPU
);
2944 INSN(fsave
, f340
, ffc0
, CF_FPU
);
2945 INSN(intouch
, f340
, ffc0
, CF_ISA_A
);
2946 INSN(cpushl
, f428
, ff38
, CF_ISA_A
);
2947 INSN(wddata
, fb00
, ff00
, CF_ISA_A
);
2948 INSN(wdebug
, fbc0
, ffc0
, CF_ISA_A
);
2952 /* ??? Some of this implementation is not exception safe. We should always
2953 write back the result to memory before setting the condition codes. */
2954 static void disas_m68k_insn(CPUM68KState
* env
, DisasContext
*s
)
2958 insn
= cpu_lduw_code(env
, s
->pc
);
2961 opcode_table
[insn
](env
, s
, insn
);
2964 /* generate intermediate code for basic block 'tb'. */
2965 void gen_intermediate_code(CPUM68KState
*env
, TranslationBlock
*tb
)
2967 M68kCPU
*cpu
= m68k_env_get_cpu(env
);
2968 CPUState
*cs
= CPU(cpu
);
2969 DisasContext dc1
, *dc
= &dc1
;
2970 target_ulong pc_start
;
2975 /* generate intermediate code */
2981 dc
->is_jmp
= DISAS_NEXT
;
2983 dc
->cc_op
= CC_OP_DYNAMIC
;
2984 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
2985 dc
->fpcr
= env
->fpcr
;
2986 dc
->user
= (env
->sr
& SR_S
) == 0;
2989 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
2990 if (max_insns
== 0) {
2991 max_insns
= CF_COUNT_MASK
;
2993 if (max_insns
> TCG_MAX_INSNS
) {
2994 max_insns
= TCG_MAX_INSNS
;
2999 pc_offset
= dc
->pc
- pc_start
;
3000 gen_throws_exception
= NULL
;
3001 tcg_gen_insn_start(dc
->pc
);
3004 if (unlikely(cpu_breakpoint_test(cs
, dc
->pc
, BP_ANY
))) {
3005 gen_exception(dc
, dc
->pc
, EXCP_DEBUG
);
3006 dc
->is_jmp
= DISAS_JUMP
;
3007 /* The address covered by the breakpoint must be included in
3008 [tb->pc, tb->pc + tb->size) in order to for it to be
3009 properly cleared -- thus we increment the PC here so that
3010 the logic setting tb->size below does the right thing. */
3015 if (num_insns
== max_insns
&& (tb
->cflags
& CF_LAST_IO
)) {
3019 dc
->insn_pc
= dc
->pc
;
3020 disas_m68k_insn(env
, dc
);
3021 } while (!dc
->is_jmp
&& !tcg_op_buf_full() &&
3022 !cs
->singlestep_enabled
&&
3024 (pc_offset
) < (TARGET_PAGE_SIZE
- 32) &&
3025 num_insns
< max_insns
);
3027 if (tb
->cflags
& CF_LAST_IO
)
3029 if (unlikely(cs
->singlestep_enabled
)) {
3030 /* Make sure the pc is updated, and raise a debug exception. */
3032 gen_flush_cc_op(dc
);
3033 tcg_gen_movi_i32(QREG_PC
, dc
->pc
);
3035 gen_helper_raise_exception(cpu_env
, tcg_const_i32(EXCP_DEBUG
));
3037 switch(dc
->is_jmp
) {
3039 gen_flush_cc_op(dc
);
3040 gen_jmp_tb(dc
, 0, dc
->pc
);
3045 gen_flush_cc_op(dc
);
3046 /* indicate that the hash table must be used to find the next TB */
3050 /* nothing more to generate */
3054 gen_tb_end(tb
, num_insns
);
3057 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
3058 qemu_log("----------------\n");
3059 qemu_log("IN: %s\n", lookup_symbol(pc_start
));
3060 log_target_disas(cs
, pc_start
, dc
->pc
- pc_start
, 0);
3064 tb
->size
= dc
->pc
- pc_start
;
3065 tb
->icount
= num_insns
;
3068 void m68k_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
3071 M68kCPU
*cpu
= M68K_CPU(cs
);
3072 CPUM68KState
*env
= &cpu
->env
;
3076 for (i
= 0; i
< 8; i
++)
3078 u
.d
= env
->fregs
[i
];
3079 cpu_fprintf (f
, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3080 i
, env
->dregs
[i
], i
, env
->aregs
[i
],
3081 i
, u
.l
.upper
, u
.l
.lower
, *(double *)&u
.d
);
3083 cpu_fprintf (f
, "PC = %08x ", env
->pc
);
3085 cpu_fprintf (f
, "SR = %04x %c%c%c%c%c ", sr
, (sr
& 0x10) ? 'X' : '-',
3086 (sr
& CCF_N
) ? 'N' : '-', (sr
& CCF_Z
) ? 'Z' : '-',
3087 (sr
& CCF_V
) ? 'V' : '-', (sr
& CCF_C
) ? 'C' : '-');
3088 cpu_fprintf (f
, "FPRESULT = %12g\n", *(double *)&env
->fp_result
);
3091 void restore_state_to_opc(CPUM68KState
*env
, TranslationBlock
*tb
,