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"
30 //#define DEBUG_DISPATCH 1
32 /* Fake floating point. */
33 #define tcg_gen_mov_f64 tcg_gen_mov_i64
34 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
35 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
37 #define DEFO32(name, offset) static TCGv QREG_##name;
38 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
39 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
45 static TCGv_i32 cpu_halted
;
47 static TCGv_ptr cpu_env
;
49 static char cpu_reg_names
[3*8*3 + 5*4];
50 static TCGv cpu_dregs
[8];
51 static TCGv cpu_aregs
[8];
52 static TCGv_i64 cpu_fregs
[8];
53 static TCGv_i64 cpu_macc
[4];
55 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
56 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
57 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
58 #define MACREG(acc) cpu_macc[acc]
59 #define QREG_SP cpu_aregs[7]
61 static TCGv NULL_QREG
;
62 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
63 /* Used to distinguish stores from bad addressing modes. */
64 static TCGv store_dummy
;
66 #include "exec/gen-icount.h"
68 void m68k_tcg_init(void)
73 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
74 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
75 #define DEFF64(name, offset) DEFO64(name, offset)
81 cpu_halted
= tcg_global_mem_new_i32(TCG_AREG0
,
82 -offsetof(M68kCPU
, env
) +
83 offsetof(CPUState
, halted
), "HALTED");
85 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
88 for (i
= 0; i
< 8; i
++) {
90 cpu_dregs
[i
] = tcg_global_mem_new(TCG_AREG0
,
91 offsetof(CPUM68KState
, dregs
[i
]), p
);
94 cpu_aregs
[i
] = tcg_global_mem_new(TCG_AREG0
,
95 offsetof(CPUM68KState
, aregs
[i
]), p
);
98 cpu_fregs
[i
] = tcg_global_mem_new_i64(TCG_AREG0
,
99 offsetof(CPUM68KState
, fregs
[i
]), p
);
102 for (i
= 0; i
< 4; i
++) {
103 sprintf(p
, "ACC%d", i
);
104 cpu_macc
[i
] = tcg_global_mem_new_i64(TCG_AREG0
,
105 offsetof(CPUM68KState
, macc
[i
]), p
);
109 NULL_QREG
= tcg_global_mem_new(TCG_AREG0
, -4, "NULL");
110 store_dummy
= tcg_global_mem_new(TCG_AREG0
, -8, "NULL");
116 static inline void qemu_assert(int cond
, const char *msg
)
119 fprintf (stderr
, "badness: %s\n", msg
);
124 /* internal defines */
125 typedef struct DisasContext
{
127 target_ulong insn_pc
; /* Start of the current instruction. */
133 struct TranslationBlock
*tb
;
134 int singlestep_enabled
;
140 #define DISAS_JUMP_NEXT 4
142 #if defined(CONFIG_USER_ONLY)
145 #define IS_USER(s) s->user
148 /* XXX: move that elsewhere */
149 /* ??? Fix exceptions. */
150 static void *gen_throws_exception
;
151 #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(s, env, 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 /* Generate a load from the specified address. Narrow values are
180 sign extended to full register width. */
181 static inline TCGv
gen_load(DisasContext
* s
, int opsize
, TCGv addr
, int sign
)
184 int index
= IS_USER(s
);
186 tmp
= tcg_temp_new_i32();
190 tcg_gen_qemu_ld8s(tmp
, addr
, index
);
192 tcg_gen_qemu_ld8u(tmp
, addr
, index
);
196 tcg_gen_qemu_ld16s(tmp
, addr
, index
);
198 tcg_gen_qemu_ld16u(tmp
, addr
, index
);
202 tcg_gen_qemu_ld32u(tmp
, addr
, index
);
205 qemu_assert(0, "bad load size");
207 gen_throws_exception
= gen_last_qop
;
211 static inline TCGv_i64
gen_load64(DisasContext
* s
, TCGv addr
)
214 int index
= IS_USER(s
);
216 tmp
= tcg_temp_new_i64();
217 tcg_gen_qemu_ldf64(tmp
, addr
, index
);
218 gen_throws_exception
= gen_last_qop
;
222 /* Generate a store. */
223 static inline void gen_store(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
)
225 int index
= IS_USER(s
);
229 tcg_gen_qemu_st8(val
, addr
, index
);
232 tcg_gen_qemu_st16(val
, addr
, index
);
236 tcg_gen_qemu_st32(val
, addr
, index
);
239 qemu_assert(0, "bad store size");
241 gen_throws_exception
= gen_last_qop
;
244 static inline void gen_store64(DisasContext
*s
, TCGv addr
, TCGv_i64 val
)
246 int index
= IS_USER(s
);
248 tcg_gen_qemu_stf64(val
, addr
, index
);
249 gen_throws_exception
= gen_last_qop
;
258 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
259 otherwise generate a store. */
260 static TCGv
gen_ldst(DisasContext
*s
, int opsize
, TCGv addr
, TCGv val
,
263 if (what
== EA_STORE
) {
264 gen_store(s
, opsize
, addr
, val
);
267 return gen_load(s
, opsize
, addr
, what
== EA_LOADS
);
271 /* Read a 32-bit immediate constant. */
272 static inline uint32_t read_im32(CPUM68KState
*env
, DisasContext
*s
)
275 im
= ((uint32_t)cpu_lduw_code(env
, s
->pc
)) << 16;
277 im
|= cpu_lduw_code(env
, s
->pc
);
282 /* Calculate and address index. */
283 static TCGv
gen_addr_index(uint16_t ext
, TCGv tmp
)
288 add
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(ext
, 12);
289 if ((ext
& 0x800) == 0) {
290 tcg_gen_ext16s_i32(tmp
, add
);
293 scale
= (ext
>> 9) & 3;
295 tcg_gen_shli_i32(tmp
, add
, scale
);
301 /* Handle a base + index + displacement effective addresss.
302 A NULL_QREG base means pc-relative. */
303 static TCGv
gen_lea_indexed(CPUM68KState
*env
, DisasContext
*s
, int opsize
,
313 ext
= cpu_lduw_code(env
, s
->pc
);
316 if ((ext
& 0x800) == 0 && !m68k_feature(s
->env
, M68K_FEATURE_WORD_INDEX
))
320 /* full extension word format */
321 if (!m68k_feature(s
->env
, M68K_FEATURE_EXT_FULL
))
324 if ((ext
& 0x30) > 0x10) {
325 /* base displacement */
326 if ((ext
& 0x30) == 0x20) {
327 bd
= (int16_t)cpu_lduw_code(env
, s
->pc
);
330 bd
= read_im32(env
, s
);
335 tmp
= tcg_temp_new();
336 if ((ext
& 0x44) == 0) {
338 add
= gen_addr_index(ext
, tmp
);
342 if ((ext
& 0x80) == 0) {
343 /* base not suppressed */
344 if (IS_NULL_QREG(base
)) {
345 base
= tcg_const_i32(offset
+ bd
);
348 if (!IS_NULL_QREG(add
)) {
349 tcg_gen_add_i32(tmp
, add
, base
);
355 if (!IS_NULL_QREG(add
)) {
357 tcg_gen_addi_i32(tmp
, add
, bd
);
361 add
= tcg_const_i32(bd
);
363 if ((ext
& 3) != 0) {
364 /* memory indirect */
365 base
= gen_load(s
, OS_LONG
, add
, 0);
366 if ((ext
& 0x44) == 4) {
367 add
= gen_addr_index(ext
, tmp
);
368 tcg_gen_add_i32(tmp
, add
, base
);
374 /* outer displacement */
375 if ((ext
& 3) == 2) {
376 od
= (int16_t)cpu_lduw_code(env
, s
->pc
);
379 od
= read_im32(env
, s
);
385 tcg_gen_addi_i32(tmp
, add
, od
);
390 /* brief extension word format */
391 tmp
= tcg_temp_new();
392 add
= gen_addr_index(ext
, tmp
);
393 if (!IS_NULL_QREG(base
)) {
394 tcg_gen_add_i32(tmp
, add
, base
);
396 tcg_gen_addi_i32(tmp
, tmp
, (int8_t)ext
);
398 tcg_gen_addi_i32(tmp
, add
, offset
+ (int8_t)ext
);
405 /* Update the CPU env CC_OP state. */
406 static inline void gen_flush_cc_op(DisasContext
*s
)
408 if (s
->cc_op
!= CC_OP_DYNAMIC
)
409 tcg_gen_movi_i32(QREG_CC_OP
, s
->cc_op
);
412 /* Evaluate all the CC flags. */
413 static inline void gen_flush_flags(DisasContext
*s
)
415 if (s
->cc_op
== CC_OP_FLAGS
)
418 gen_helper_flush_flags(cpu_env
, QREG_CC_OP
);
419 s
->cc_op
= CC_OP_FLAGS
;
422 static void gen_logic_cc(DisasContext
*s
, TCGv val
)
424 tcg_gen_mov_i32(QREG_CC_DEST
, val
);
425 s
->cc_op
= CC_OP_LOGIC
;
428 static void gen_update_cc_add(TCGv dest
, TCGv src
)
430 tcg_gen_mov_i32(QREG_CC_DEST
, dest
);
431 tcg_gen_mov_i32(QREG_CC_SRC
, src
);
434 static inline int opsize_bytes(int opsize
)
437 case OS_BYTE
: return 1;
438 case OS_WORD
: return 2;
439 case OS_LONG
: return 4;
440 case OS_SINGLE
: return 4;
441 case OS_DOUBLE
: return 8;
443 qemu_assert(0, "bad operand size");
448 /* Assign value to a register. If the width is less than the register width
449 only the low part of the register is set. */
450 static void gen_partset_reg(int opsize
, TCGv reg
, TCGv val
)
455 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
456 tmp
= tcg_temp_new();
457 tcg_gen_ext8u_i32(tmp
, val
);
458 tcg_gen_or_i32(reg
, reg
, tmp
);
461 tcg_gen_andi_i32(reg
, reg
, 0xffff0000);
462 tmp
= tcg_temp_new();
463 tcg_gen_ext16u_i32(tmp
, val
);
464 tcg_gen_or_i32(reg
, reg
, tmp
);
468 tcg_gen_mov_i32(reg
, val
);
471 qemu_assert(0, "Bad operand size");
476 /* Sign or zero extend a value. */
477 static inline TCGv
gen_extend(TCGv val
, int opsize
, int sign
)
483 tmp
= tcg_temp_new();
485 tcg_gen_ext8s_i32(tmp
, val
);
487 tcg_gen_ext8u_i32(tmp
, val
);
490 tmp
= tcg_temp_new();
492 tcg_gen_ext16s_i32(tmp
, val
);
494 tcg_gen_ext16u_i32(tmp
, val
);
501 qemu_assert(0, "Bad operand size");
506 /* Generate code for an "effective address". Does not adjust the base
507 register for autoincrement addressing modes. */
508 static TCGv
gen_lea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
516 switch ((insn
>> 3) & 7) {
517 case 0: /* Data register direct. */
518 case 1: /* Address register direct. */
520 case 2: /* Indirect register */
521 case 3: /* Indirect postincrement. */
522 return AREG(insn
, 0);
523 case 4: /* Indirect predecrememnt. */
525 tmp
= tcg_temp_new();
526 tcg_gen_subi_i32(tmp
, reg
, opsize_bytes(opsize
));
528 case 5: /* Indirect displacement. */
530 tmp
= tcg_temp_new();
531 ext
= cpu_lduw_code(env
, s
->pc
);
533 tcg_gen_addi_i32(tmp
, reg
, (int16_t)ext
);
535 case 6: /* Indirect index + displacement. */
537 return gen_lea_indexed(env
, s
, opsize
, reg
);
540 case 0: /* Absolute short. */
541 offset
= cpu_ldsw_code(env
, s
->pc
);
543 return tcg_const_i32(offset
);
544 case 1: /* Absolute long. */
545 offset
= read_im32(env
, s
);
546 return tcg_const_i32(offset
);
547 case 2: /* pc displacement */
549 offset
+= cpu_ldsw_code(env
, s
->pc
);
551 return tcg_const_i32(offset
);
552 case 3: /* pc index+displacement. */
553 return gen_lea_indexed(env
, s
, opsize
, NULL_QREG
);
554 case 4: /* Immediate. */
559 /* Should never happen. */
563 /* Helper function for gen_ea. Reuse the computed address between the
564 for read/write operands. */
565 static inline TCGv
gen_ea_once(CPUM68KState
*env
, DisasContext
*s
,
566 uint16_t insn
, int opsize
, TCGv val
,
567 TCGv
*addrp
, ea_what what
)
571 if (addrp
&& what
== EA_STORE
) {
574 tmp
= gen_lea(env
, s
, insn
, opsize
);
575 if (IS_NULL_QREG(tmp
))
580 return gen_ldst(s
, opsize
, tmp
, val
, what
);
583 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
584 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
585 ADDRP is non-null for readwrite operands. */
586 static TCGv
gen_ea(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
587 int opsize
, TCGv val
, TCGv
*addrp
, ea_what what
)
593 switch ((insn
>> 3) & 7) {
594 case 0: /* Data register direct. */
596 if (what
== EA_STORE
) {
597 gen_partset_reg(opsize
, reg
, val
);
600 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
602 case 1: /* Address register direct. */
604 if (what
== EA_STORE
) {
605 tcg_gen_mov_i32(reg
, val
);
608 return gen_extend(reg
, opsize
, what
== EA_LOADS
);
610 case 2: /* Indirect register */
612 return gen_ldst(s
, opsize
, reg
, val
, what
);
613 case 3: /* Indirect postincrement. */
615 result
= gen_ldst(s
, opsize
, reg
, val
, what
);
616 /* ??? This is not exception safe. The instruction may still
617 fault after this point. */
618 if (what
== EA_STORE
|| !addrp
)
619 tcg_gen_addi_i32(reg
, reg
, opsize_bytes(opsize
));
621 case 4: /* Indirect predecrememnt. */
624 if (addrp
&& what
== EA_STORE
) {
627 tmp
= gen_lea(env
, s
, insn
, opsize
);
628 if (IS_NULL_QREG(tmp
))
633 result
= gen_ldst(s
, opsize
, tmp
, val
, what
);
634 /* ??? This is not exception safe. The instruction may still
635 fault after this point. */
636 if (what
== EA_STORE
|| !addrp
) {
638 tcg_gen_mov_i32(reg
, tmp
);
642 case 5: /* Indirect displacement. */
643 case 6: /* Indirect index + displacement. */
644 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
647 case 0: /* Absolute short. */
648 case 1: /* Absolute long. */
649 case 2: /* pc displacement */
650 case 3: /* pc index+displacement. */
651 return gen_ea_once(env
, s
, insn
, opsize
, val
, addrp
, what
);
652 case 4: /* Immediate. */
653 /* Sign extend values for consistency. */
656 if (what
== EA_LOADS
) {
657 offset
= cpu_ldsb_code(env
, s
->pc
+ 1);
659 offset
= cpu_ldub_code(env
, s
->pc
+ 1);
664 if (what
== EA_LOADS
) {
665 offset
= cpu_ldsw_code(env
, s
->pc
);
667 offset
= cpu_lduw_code(env
, s
->pc
);
672 offset
= read_im32(env
, s
);
675 qemu_assert(0, "Bad immediate operand");
677 return tcg_const_i32(offset
);
682 /* Should never happen. */
686 /* This generates a conditional branch, clobbering all temporaries. */
687 static void gen_jmpcc(DisasContext
*s
, int cond
, int l1
)
691 /* TODO: Optimize compare/branch pairs rather than always flushing
692 flag state to CC_OP_FLAGS. */
700 case 2: /* HI (!C && !Z) */
701 tmp
= tcg_temp_new();
702 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
| CCF_Z
);
703 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
705 case 3: /* LS (C || Z) */
706 tmp
= tcg_temp_new();
707 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
| CCF_Z
);
708 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
710 case 4: /* CC (!C) */
711 tmp
= tcg_temp_new();
712 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
);
713 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
716 tmp
= tcg_temp_new();
717 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_C
);
718 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
720 case 6: /* NE (!Z) */
721 tmp
= tcg_temp_new();
722 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_Z
);
723 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
726 tmp
= tcg_temp_new();
727 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_Z
);
728 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
730 case 8: /* VC (!V) */
731 tmp
= tcg_temp_new();
732 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_V
);
733 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
736 tmp
= tcg_temp_new();
737 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_V
);
738 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
740 case 10: /* PL (!N) */
741 tmp
= tcg_temp_new();
742 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
743 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
745 case 11: /* MI (N) */
746 tmp
= tcg_temp_new();
747 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
748 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
750 case 12: /* GE (!(N ^ V)) */
751 tmp
= tcg_temp_new();
752 assert(CCF_V
== (CCF_N
>> 2));
753 tcg_gen_shri_i32(tmp
, QREG_CC_DEST
, 2);
754 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
755 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
);
756 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
758 case 13: /* LT (N ^ V) */
759 tmp
= tcg_temp_new();
760 assert(CCF_V
== (CCF_N
>> 2));
761 tcg_gen_shri_i32(tmp
, QREG_CC_DEST
, 2);
762 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
763 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
);
764 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
766 case 14: /* GT (!(Z || (N ^ V))) */
767 tmp
= tcg_temp_new();
768 assert(CCF_V
== (CCF_N
>> 2));
769 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
770 tcg_gen_shri_i32(tmp
, tmp
, 2);
771 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
772 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
| CCF_Z
);
773 tcg_gen_brcondi_i32(TCG_COND_EQ
, tmp
, 0, l1
);
775 case 15: /* LE (Z || (N ^ V)) */
776 tmp
= tcg_temp_new();
777 assert(CCF_V
== (CCF_N
>> 2));
778 tcg_gen_andi_i32(tmp
, QREG_CC_DEST
, CCF_N
);
779 tcg_gen_shri_i32(tmp
, tmp
, 2);
780 tcg_gen_xor_i32(tmp
, tmp
, QREG_CC_DEST
);
781 tcg_gen_andi_i32(tmp
, tmp
, CCF_V
| CCF_Z
);
782 tcg_gen_brcondi_i32(TCG_COND_NE
, tmp
, 0, l1
);
785 /* Should ever happen. */
796 l1
= gen_new_label();
797 cond
= (insn
>> 8) & 0xf;
799 tcg_gen_andi_i32(reg
, reg
, 0xffffff00);
800 /* This is safe because we modify the reg directly, with no other values
802 gen_jmpcc(s
, cond
^ 1, l1
);
803 tcg_gen_ori_i32(reg
, reg
, 0xff);
807 /* Force a TB lookup after an instruction that changes the CPU state. */
808 static void gen_lookup_tb(DisasContext
*s
)
811 tcg_gen_movi_i32(QREG_PC
, s
->pc
);
812 s
->is_jmp
= DISAS_UPDATE
;
815 /* Generate a jump to an immediate address. */
816 static void gen_jmp_im(DisasContext
*s
, uint32_t dest
)
819 tcg_gen_movi_i32(QREG_PC
, dest
);
820 s
->is_jmp
= DISAS_JUMP
;
823 /* Generate a jump to the address in qreg DEST. */
824 static void gen_jmp(DisasContext
*s
, TCGv dest
)
827 tcg_gen_mov_i32(QREG_PC
, dest
);
828 s
->is_jmp
= DISAS_JUMP
;
831 static void gen_exception(DisasContext
*s
, uint32_t where
, int nr
)
834 gen_jmp_im(s
, where
);
835 gen_helper_raise_exception(cpu_env
, tcg_const_i32(nr
));
838 static inline void gen_addr_fault(DisasContext
*s
)
840 gen_exception(s
, s
->insn_pc
, EXCP_ADDRESS
);
843 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
844 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
845 op_sign ? EA_LOADS : EA_LOADU); \
846 if (IS_NULL_QREG(result)) { \
852 #define DEST_EA(env, insn, opsize, val, addrp) do { \
853 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
854 if (IS_NULL_QREG(ea_result)) { \
860 /* Generate a jump to an immediate address. */
861 static void gen_jmp_tb(DisasContext
*s
, int n
, uint32_t dest
)
863 TranslationBlock
*tb
;
866 if (unlikely(s
->singlestep_enabled
)) {
867 gen_exception(s
, dest
, EXCP_DEBUG
);
868 } else if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
) ||
869 (s
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
871 tcg_gen_movi_i32(QREG_PC
, dest
);
872 tcg_gen_exit_tb((uintptr_t)tb
+ n
);
877 s
->is_jmp
= DISAS_TB_JUMP
;
880 DISAS_INSN(undef_mac
)
882 gen_exception(s
, s
->pc
- 2, EXCP_LINEA
);
885 DISAS_INSN(undef_fpu
)
887 gen_exception(s
, s
->pc
- 2, EXCP_LINEF
);
892 gen_exception(s
, s
->pc
- 2, EXCP_UNSUPPORTED
);
893 cpu_abort(env
, "Illegal instruction: %04x @ %08x", insn
, s
->pc
- 2);
903 sign
= (insn
& 0x100) != 0;
905 tmp
= tcg_temp_new();
907 tcg_gen_ext16s_i32(tmp
, reg
);
909 tcg_gen_ext16u_i32(tmp
, reg
);
910 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
911 tcg_gen_mul_i32(tmp
, tmp
, src
);
912 tcg_gen_mov_i32(reg
, tmp
);
913 /* Unlike m68k, coldfire always clears the overflow bit. */
914 gen_logic_cc(s
, tmp
);
924 sign
= (insn
& 0x100) != 0;
927 tcg_gen_ext16s_i32(QREG_DIV1
, reg
);
929 tcg_gen_ext16u_i32(QREG_DIV1
, reg
);
931 SRC_EA(env
, src
, OS_WORD
, sign
, NULL
);
932 tcg_gen_mov_i32(QREG_DIV2
, src
);
934 gen_helper_divs(cpu_env
, tcg_const_i32(1));
936 gen_helper_divu(cpu_env
, tcg_const_i32(1));
939 tmp
= tcg_temp_new();
940 src
= tcg_temp_new();
941 tcg_gen_ext16u_i32(tmp
, QREG_DIV1
);
942 tcg_gen_shli_i32(src
, QREG_DIV2
, 16);
943 tcg_gen_or_i32(reg
, tmp
, src
);
944 s
->cc_op
= CC_OP_FLAGS
;
954 ext
= cpu_lduw_code(env
, s
->pc
);
957 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
962 tcg_gen_mov_i32(QREG_DIV1
, num
);
963 SRC_EA(env
, den
, OS_LONG
, 0, NULL
);
964 tcg_gen_mov_i32(QREG_DIV2
, den
);
966 gen_helper_divs(cpu_env
, tcg_const_i32(0));
968 gen_helper_divu(cpu_env
, tcg_const_i32(0));
970 if ((ext
& 7) == ((ext
>> 12) & 7)) {
972 tcg_gen_mov_i32 (reg
, QREG_DIV1
);
975 tcg_gen_mov_i32 (reg
, QREG_DIV2
);
977 s
->cc_op
= CC_OP_FLAGS
;
989 add
= (insn
& 0x4000) != 0;
991 dest
= tcg_temp_new();
993 SRC_EA(env
, tmp
, OS_LONG
, 0, &addr
);
997 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1000 tcg_gen_add_i32(dest
, tmp
, src
);
1001 gen_helper_xflag_lt(QREG_CC_X
, dest
, src
);
1002 s
->cc_op
= CC_OP_ADD
;
1004 gen_helper_xflag_lt(QREG_CC_X
, tmp
, src
);
1005 tcg_gen_sub_i32(dest
, tmp
, src
);
1006 s
->cc_op
= CC_OP_SUB
;
1008 gen_update_cc_add(dest
, src
);
1010 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1012 tcg_gen_mov_i32(reg
, dest
);
1017 /* Reverse the order of the bits in REG. */
1021 reg
= DREG(insn
, 0);
1022 gen_helper_bitrev(reg
, reg
);
1025 DISAS_INSN(bitop_reg
)
1035 if ((insn
& 0x38) != 0)
1039 op
= (insn
>> 6) & 3;
1040 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1041 src2
= DREG(insn
, 9);
1042 dest
= tcg_temp_new();
1045 tmp
= tcg_temp_new();
1046 if (opsize
== OS_BYTE
)
1047 tcg_gen_andi_i32(tmp
, src2
, 7);
1049 tcg_gen_andi_i32(tmp
, src2
, 31);
1051 tmp
= tcg_temp_new();
1052 tcg_gen_shr_i32(tmp
, src1
, src2
);
1053 tcg_gen_andi_i32(tmp
, tmp
, 1);
1054 tcg_gen_shli_i32(tmp
, tmp
, 2);
1055 /* Clear CCF_Z if bit set. */
1056 tcg_gen_ori_i32(QREG_CC_DEST
, QREG_CC_DEST
, CCF_Z
);
1057 tcg_gen_xor_i32(QREG_CC_DEST
, QREG_CC_DEST
, tmp
);
1059 tcg_gen_shl_i32(tmp
, tcg_const_i32(1), src2
);
1062 tcg_gen_xor_i32(dest
, src1
, tmp
);
1065 tcg_gen_not_i32(tmp
, tmp
);
1066 tcg_gen_and_i32(dest
, src1
, tmp
);
1069 tcg_gen_or_i32(dest
, src1
, tmp
);
1075 DEST_EA(env
, insn
, opsize
, dest
, &addr
);
1081 reg
= DREG(insn
, 0);
1083 gen_helper_sats(reg
, reg
, QREG_CC_DEST
);
1084 gen_logic_cc(s
, reg
);
1087 static void gen_push(DisasContext
*s
, TCGv val
)
1091 tmp
= tcg_temp_new();
1092 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1093 gen_store(s
, OS_LONG
, tmp
, val
);
1094 tcg_gen_mov_i32(QREG_SP
, tmp
);
1106 mask
= cpu_lduw_code(env
, s
->pc
);
1108 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1109 if (IS_NULL_QREG(tmp
)) {
1113 addr
= tcg_temp_new();
1114 tcg_gen_mov_i32(addr
, tmp
);
1115 is_load
= ((insn
& 0x0400) != 0);
1116 for (i
= 0; i
< 16; i
++, mask
>>= 1) {
1123 tmp
= gen_load(s
, OS_LONG
, addr
, 0);
1124 tcg_gen_mov_i32(reg
, tmp
);
1126 gen_store(s
, OS_LONG
, addr
, reg
);
1129 tcg_gen_addi_i32(addr
, addr
, 4);
1134 DISAS_INSN(bitop_im
)
1144 if ((insn
& 0x38) != 0)
1148 op
= (insn
>> 6) & 3;
1150 bitnum
= cpu_lduw_code(env
, s
->pc
);
1152 if (bitnum
& 0xff00) {
1153 disas_undef(env
, s
, insn
);
1157 SRC_EA(env
, src1
, opsize
, 0, op
? &addr
: NULL
);
1160 if (opsize
== OS_BYTE
)
1166 tmp
= tcg_temp_new();
1167 assert (CCF_Z
== (1 << 2));
1169 tcg_gen_shri_i32(tmp
, src1
, bitnum
- 2);
1170 else if (bitnum
< 2)
1171 tcg_gen_shli_i32(tmp
, src1
, 2 - bitnum
);
1173 tcg_gen_mov_i32(tmp
, src1
);
1174 tcg_gen_andi_i32(tmp
, tmp
, CCF_Z
);
1175 /* Clear CCF_Z if bit set. */
1176 tcg_gen_ori_i32(QREG_CC_DEST
, QREG_CC_DEST
, CCF_Z
);
1177 tcg_gen_xor_i32(QREG_CC_DEST
, QREG_CC_DEST
, tmp
);
1181 tcg_gen_xori_i32(tmp
, src1
, mask
);
1184 tcg_gen_andi_i32(tmp
, src1
, ~mask
);
1187 tcg_gen_ori_i32(tmp
, src1
, mask
);
1192 DEST_EA(env
, insn
, opsize
, tmp
, &addr
);
1196 DISAS_INSN(arith_im
)
1204 op
= (insn
>> 9) & 7;
1205 SRC_EA(env
, src1
, OS_LONG
, 0, (op
== 6) ? NULL
: &addr
);
1206 im
= read_im32(env
, s
);
1207 dest
= tcg_temp_new();
1210 tcg_gen_ori_i32(dest
, src1
, im
);
1211 gen_logic_cc(s
, dest
);
1214 tcg_gen_andi_i32(dest
, src1
, im
);
1215 gen_logic_cc(s
, dest
);
1218 tcg_gen_mov_i32(dest
, src1
);
1219 gen_helper_xflag_lt(QREG_CC_X
, dest
, tcg_const_i32(im
));
1220 tcg_gen_subi_i32(dest
, dest
, im
);
1221 gen_update_cc_add(dest
, tcg_const_i32(im
));
1222 s
->cc_op
= CC_OP_SUB
;
1225 tcg_gen_mov_i32(dest
, src1
);
1226 tcg_gen_addi_i32(dest
, dest
, im
);
1227 gen_update_cc_add(dest
, tcg_const_i32(im
));
1228 gen_helper_xflag_lt(QREG_CC_X
, dest
, tcg_const_i32(im
));
1229 s
->cc_op
= CC_OP_ADD
;
1232 tcg_gen_xori_i32(dest
, src1
, im
);
1233 gen_logic_cc(s
, dest
);
1236 tcg_gen_mov_i32(dest
, src1
);
1237 tcg_gen_subi_i32(dest
, dest
, im
);
1238 gen_update_cc_add(dest
, tcg_const_i32(im
));
1239 s
->cc_op
= CC_OP_SUB
;
1245 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1253 reg
= DREG(insn
, 0);
1254 tcg_gen_bswap32_i32(reg
, reg
);
1264 switch (insn
>> 12) {
1265 case 1: /* move.b */
1268 case 2: /* move.l */
1271 case 3: /* move.w */
1277 SRC_EA(env
, src
, opsize
, 1, NULL
);
1278 op
= (insn
>> 6) & 7;
1281 /* The value will already have been sign extended. */
1282 dest
= AREG(insn
, 9);
1283 tcg_gen_mov_i32(dest
, src
);
1287 dest_ea
= ((insn
>> 9) & 7) | (op
<< 3);
1288 DEST_EA(env
, dest_ea
, opsize
, src
, NULL
);
1289 /* This will be correct because loads sign extend. */
1290 gen_logic_cc(s
, src
);
1299 reg
= DREG(insn
, 0);
1300 gen_helper_subx_cc(reg
, cpu_env
, tcg_const_i32(0), reg
);
1308 reg
= AREG(insn
, 9);
1309 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1310 if (IS_NULL_QREG(tmp
)) {
1314 tcg_gen_mov_i32(reg
, tmp
);
1321 switch ((insn
>> 6) & 3) {
1334 DEST_EA(env
, insn
, opsize
, tcg_const_i32(0), NULL
);
1335 gen_logic_cc(s
, tcg_const_i32(0));
1338 static TCGv
gen_get_ccr(DisasContext
*s
)
1343 dest
= tcg_temp_new();
1344 tcg_gen_shli_i32(dest
, QREG_CC_X
, 4);
1345 tcg_gen_or_i32(dest
, dest
, QREG_CC_DEST
);
1349 DISAS_INSN(move_from_ccr
)
1354 ccr
= gen_get_ccr(s
);
1355 reg
= DREG(insn
, 0);
1356 gen_partset_reg(OS_WORD
, reg
, ccr
);
1364 reg
= DREG(insn
, 0);
1365 src1
= tcg_temp_new();
1366 tcg_gen_mov_i32(src1
, reg
);
1367 tcg_gen_neg_i32(reg
, src1
);
1368 s
->cc_op
= CC_OP_SUB
;
1369 gen_update_cc_add(reg
, src1
);
1370 gen_helper_xflag_lt(QREG_CC_X
, tcg_const_i32(0), src1
);
1371 s
->cc_op
= CC_OP_SUB
;
1374 static void gen_set_sr_im(DisasContext
*s
, uint16_t val
, int ccr_only
)
1376 tcg_gen_movi_i32(QREG_CC_DEST
, val
& 0xf);
1377 tcg_gen_movi_i32(QREG_CC_X
, (val
& 0x10) >> 4);
1379 gen_helper_set_sr(cpu_env
, tcg_const_i32(val
& 0xff00));
1383 static void gen_set_sr(CPUM68KState
*env
, DisasContext
*s
, uint16_t insn
,
1389 s
->cc_op
= CC_OP_FLAGS
;
1390 if ((insn
& 0x38) == 0)
1392 tmp
= tcg_temp_new();
1393 reg
= DREG(insn
, 0);
1394 tcg_gen_andi_i32(QREG_CC_DEST
, reg
, 0xf);
1395 tcg_gen_shri_i32(tmp
, reg
, 4);
1396 tcg_gen_andi_i32(QREG_CC_X
, tmp
, 1);
1398 gen_helper_set_sr(cpu_env
, reg
);
1401 else if ((insn
& 0x3f) == 0x3c)
1404 val
= cpu_lduw_code(env
, s
->pc
);
1406 gen_set_sr_im(s
, val
, ccr_only
);
1409 disas_undef(env
, s
, insn
);
1412 DISAS_INSN(move_to_ccr
)
1414 gen_set_sr(env
, s
, insn
, 1);
1421 reg
= DREG(insn
, 0);
1422 tcg_gen_not_i32(reg
, reg
);
1423 gen_logic_cc(s
, reg
);
1432 src1
= tcg_temp_new();
1433 src2
= tcg_temp_new();
1434 reg
= DREG(insn
, 0);
1435 tcg_gen_shli_i32(src1
, reg
, 16);
1436 tcg_gen_shri_i32(src2
, reg
, 16);
1437 tcg_gen_or_i32(reg
, src1
, src2
);
1438 gen_logic_cc(s
, reg
);
1445 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1446 if (IS_NULL_QREG(tmp
)) {
1459 reg
= DREG(insn
, 0);
1460 op
= (insn
>> 6) & 7;
1461 tmp
= tcg_temp_new();
1463 tcg_gen_ext16s_i32(tmp
, reg
);
1465 tcg_gen_ext8s_i32(tmp
, reg
);
1467 gen_partset_reg(OS_WORD
, reg
, tmp
);
1469 tcg_gen_mov_i32(reg
, tmp
);
1470 gen_logic_cc(s
, tmp
);
1478 switch ((insn
>> 6) & 3) {
1491 SRC_EA(env
, tmp
, opsize
, 1, NULL
);
1492 gen_logic_cc(s
, tmp
);
1497 /* Implemented as a NOP. */
1502 gen_exception(s
, s
->pc
- 2, EXCP_ILLEGAL
);
1505 /* ??? This should be atomic. */
1512 dest
= tcg_temp_new();
1513 SRC_EA(env
, src1
, OS_BYTE
, 1, &addr
);
1514 gen_logic_cc(s
, src1
);
1515 tcg_gen_ori_i32(dest
, src1
, 0x80);
1516 DEST_EA(env
, insn
, OS_BYTE
, dest
, &addr
);
1526 /* The upper 32 bits of the product are discarded, so
1527 muls.l and mulu.l are functionally equivalent. */
1528 ext
= cpu_lduw_code(env
, s
->pc
);
1531 gen_exception(s
, s
->pc
- 4, EXCP_UNSUPPORTED
);
1534 reg
= DREG(ext
, 12);
1535 SRC_EA(env
, src1
, OS_LONG
, 0, NULL
);
1536 dest
= tcg_temp_new();
1537 tcg_gen_mul_i32(dest
, src1
, reg
);
1538 tcg_gen_mov_i32(reg
, dest
);
1539 /* Unlike m68k, coldfire always clears the overflow bit. */
1540 gen_logic_cc(s
, dest
);
1549 offset
= cpu_ldsw_code(env
, s
->pc
);
1551 reg
= AREG(insn
, 0);
1552 tmp
= tcg_temp_new();
1553 tcg_gen_subi_i32(tmp
, QREG_SP
, 4);
1554 gen_store(s
, OS_LONG
, tmp
, reg
);
1555 if ((insn
& 7) != 7)
1556 tcg_gen_mov_i32(reg
, tmp
);
1557 tcg_gen_addi_i32(QREG_SP
, tmp
, offset
);
1566 src
= tcg_temp_new();
1567 reg
= AREG(insn
, 0);
1568 tcg_gen_mov_i32(src
, reg
);
1569 tmp
= gen_load(s
, OS_LONG
, src
, 0);
1570 tcg_gen_mov_i32(reg
, tmp
);
1571 tcg_gen_addi_i32(QREG_SP
, src
, 4);
1582 tmp
= gen_load(s
, OS_LONG
, QREG_SP
, 0);
1583 tcg_gen_addi_i32(QREG_SP
, QREG_SP
, 4);
1591 /* Load the target address first to ensure correct exception
1593 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
1594 if (IS_NULL_QREG(tmp
)) {
1598 if ((insn
& 0x40) == 0) {
1600 gen_push(s
, tcg_const_i32(s
->pc
));
1613 SRC_EA(env
, src1
, OS_LONG
, 0, &addr
);
1614 val
= (insn
>> 9) & 7;
1617 dest
= tcg_temp_new();
1618 tcg_gen_mov_i32(dest
, src1
);
1619 if ((insn
& 0x38) == 0x08) {
1620 /* Don't update condition codes if the destination is an
1621 address register. */
1622 if (insn
& 0x0100) {
1623 tcg_gen_subi_i32(dest
, dest
, val
);
1625 tcg_gen_addi_i32(dest
, dest
, val
);
1628 src2
= tcg_const_i32(val
);
1629 if (insn
& 0x0100) {
1630 gen_helper_xflag_lt(QREG_CC_X
, dest
, src2
);
1631 tcg_gen_subi_i32(dest
, dest
, val
);
1632 s
->cc_op
= CC_OP_SUB
;
1634 tcg_gen_addi_i32(dest
, dest
, val
);
1635 gen_helper_xflag_lt(QREG_CC_X
, dest
, src2
);
1636 s
->cc_op
= CC_OP_ADD
;
1638 gen_update_cc_add(dest
, src2
);
1640 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1646 case 2: /* One extension word. */
1649 case 3: /* Two extension words. */
1652 case 4: /* No extension words. */
1655 disas_undef(env
, s
, insn
);
1667 op
= (insn
>> 8) & 0xf;
1668 offset
= (int8_t)insn
;
1670 offset
= cpu_ldsw_code(env
, s
->pc
);
1672 } else if (offset
== -1) {
1673 offset
= read_im32(env
, s
);
1677 gen_push(s
, tcg_const_i32(s
->pc
));
1682 l1
= gen_new_label();
1683 gen_jmpcc(s
, ((insn
>> 8) & 0xf) ^ 1, l1
);
1684 gen_jmp_tb(s
, 1, base
+ offset
);
1686 gen_jmp_tb(s
, 0, s
->pc
);
1688 /* Unconditional branch. */
1689 gen_jmp_tb(s
, 0, base
+ offset
);
1698 tcg_gen_movi_i32(DREG(insn
, 9), val
);
1699 gen_logic_cc(s
, tcg_const_i32(val
));
1712 SRC_EA(env
, src
, opsize
, (insn
& 0x80) == 0, NULL
);
1713 reg
= DREG(insn
, 9);
1714 tcg_gen_mov_i32(reg
, src
);
1715 gen_logic_cc(s
, src
);
1725 reg
= DREG(insn
, 9);
1726 dest
= tcg_temp_new();
1728 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1729 tcg_gen_or_i32(dest
, src
, reg
);
1730 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1732 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1733 tcg_gen_or_i32(dest
, src
, reg
);
1734 tcg_gen_mov_i32(reg
, dest
);
1736 gen_logic_cc(s
, dest
);
1744 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1745 reg
= AREG(insn
, 9);
1746 tcg_gen_sub_i32(reg
, reg
, src
);
1755 reg
= DREG(insn
, 9);
1756 src
= DREG(insn
, 0);
1757 gen_helper_subx_cc(reg
, cpu_env
, reg
, src
);
1765 val
= (insn
>> 9) & 7;
1768 src
= tcg_const_i32(val
);
1769 gen_logic_cc(s
, src
);
1770 DEST_EA(env
, insn
, OS_LONG
, src
, NULL
);
1781 op
= (insn
>> 6) & 3;
1785 s
->cc_op
= CC_OP_CMPB
;
1789 s
->cc_op
= CC_OP_CMPW
;
1793 s
->cc_op
= CC_OP_SUB
;
1798 SRC_EA(env
, src
, opsize
, 1, NULL
);
1799 reg
= DREG(insn
, 9);
1800 dest
= tcg_temp_new();
1801 tcg_gen_sub_i32(dest
, reg
, src
);
1802 gen_update_cc_add(dest
, src
);
1817 SRC_EA(env
, src
, opsize
, 1, NULL
);
1818 reg
= AREG(insn
, 9);
1819 dest
= tcg_temp_new();
1820 tcg_gen_sub_i32(dest
, reg
, src
);
1821 gen_update_cc_add(dest
, src
);
1822 s
->cc_op
= CC_OP_SUB
;
1832 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1833 reg
= DREG(insn
, 9);
1834 dest
= tcg_temp_new();
1835 tcg_gen_xor_i32(dest
, src
, reg
);
1836 gen_logic_cc(s
, dest
);
1837 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1847 reg
= DREG(insn
, 9);
1848 dest
= tcg_temp_new();
1850 SRC_EA(env
, src
, OS_LONG
, 0, &addr
);
1851 tcg_gen_and_i32(dest
, src
, reg
);
1852 DEST_EA(env
, insn
, OS_LONG
, dest
, &addr
);
1854 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1855 tcg_gen_and_i32(dest
, src
, reg
);
1856 tcg_gen_mov_i32(reg
, dest
);
1858 gen_logic_cc(s
, dest
);
1866 SRC_EA(env
, src
, OS_LONG
, 0, NULL
);
1867 reg
= AREG(insn
, 9);
1868 tcg_gen_add_i32(reg
, reg
, src
);
1877 reg
= DREG(insn
, 9);
1878 src
= DREG(insn
, 0);
1879 gen_helper_addx_cc(reg
, cpu_env
, reg
, src
);
1880 s
->cc_op
= CC_OP_FLAGS
;
1883 /* TODO: This could be implemented without helper functions. */
1884 DISAS_INSN(shift_im
)
1890 reg
= DREG(insn
, 0);
1891 tmp
= (insn
>> 9) & 7;
1894 shift
= tcg_const_i32(tmp
);
1895 /* No need to flush flags becuse we know we will set C flag. */
1897 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1900 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1902 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1905 s
->cc_op
= CC_OP_SHIFT
;
1908 DISAS_INSN(shift_reg
)
1913 reg
= DREG(insn
, 0);
1914 shift
= DREG(insn
, 9);
1915 /* Shift by zero leaves C flag unmodified. */
1918 gen_helper_shl_cc(reg
, cpu_env
, reg
, shift
);
1921 gen_helper_shr_cc(reg
, cpu_env
, reg
, shift
);
1923 gen_helper_sar_cc(reg
, cpu_env
, reg
, shift
);
1926 s
->cc_op
= CC_OP_SHIFT
;
1932 reg
= DREG(insn
, 0);
1933 gen_logic_cc(s
, reg
);
1934 gen_helper_ff1(reg
, reg
);
1937 static TCGv
gen_get_sr(DisasContext
*s
)
1942 ccr
= gen_get_ccr(s
);
1943 sr
= tcg_temp_new();
1944 tcg_gen_andi_i32(sr
, QREG_SR
, 0xffe0);
1945 tcg_gen_or_i32(sr
, sr
, ccr
);
1955 ext
= cpu_lduw_code(env
, s
->pc
);
1957 if (ext
!= 0x46FC) {
1958 gen_exception(s
, addr
, EXCP_UNSUPPORTED
);
1961 ext
= cpu_lduw_code(env
, s
->pc
);
1963 if (IS_USER(s
) || (ext
& SR_S
) == 0) {
1964 gen_exception(s
, addr
, EXCP_PRIVILEGE
);
1967 gen_push(s
, gen_get_sr(s
));
1968 gen_set_sr_im(s
, ext
, 0);
1971 DISAS_INSN(move_from_sr
)
1977 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1981 reg
= DREG(insn
, 0);
1982 gen_partset_reg(OS_WORD
, reg
, sr
);
1985 DISAS_INSN(move_to_sr
)
1988 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
1991 gen_set_sr(env
, s
, insn
, 0);
1995 DISAS_INSN(move_from_usp
)
1998 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2001 /* TODO: Implement USP. */
2002 gen_exception(s
, s
->pc
- 2, EXCP_ILLEGAL
);
2005 DISAS_INSN(move_to_usp
)
2008 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2011 /* TODO: Implement USP. */
2012 gen_exception(s
, s
->pc
- 2, EXCP_ILLEGAL
);
2017 gen_exception(s
, s
->pc
, EXCP_HALT_INSN
);
2025 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2029 ext
= cpu_lduw_code(env
, s
->pc
);
2032 gen_set_sr_im(s
, ext
, 0);
2033 tcg_gen_movi_i32(cpu_halted
, 1);
2034 gen_exception(s
, s
->pc
, EXCP_HLT
);
2040 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2043 gen_exception(s
, s
->pc
- 2, EXCP_RTE
);
2052 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2056 ext
= cpu_lduw_code(env
, s
->pc
);
2060 reg
= AREG(ext
, 12);
2062 reg
= DREG(ext
, 12);
2064 gen_helper_movec(cpu_env
, tcg_const_i32(ext
& 0xfff), reg
);
2071 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2074 /* ICache fetch. Implement as no-op. */
2080 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2083 /* Cache push/invalidate. Implement as no-op. */
2088 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2094 gen_exception(s
, s
->pc
- 2, EXCP_PRIVILEGE
);
2097 /* TODO: Implement wdebug. */
2098 qemu_assert(0, "WDEBUG not implemented");
2103 gen_exception(s
, s
->pc
- 2, EXCP_TRAP0
+ (insn
& 0xf));
2106 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2107 immediately before the next FP instruction is executed. */
2121 ext
= cpu_lduw_code(env
, s
->pc
);
2123 opmode
= ext
& 0x7f;
2124 switch ((ext
>> 13) & 7) {
2129 case 3: /* fmove out */
2131 tmp32
= tcg_temp_new_i32();
2133 /* ??? TODO: Proper behavior on overflow. */
2134 switch ((ext
>> 10) & 7) {
2137 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2141 gen_helper_f64_to_f32(tmp32
, cpu_env
, src
);
2145 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2147 case 5: /* OS_DOUBLE */
2148 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2149 switch ((insn
>> 3) & 7) {
2154 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2157 offset
= cpu_ldsw_code(env
, s
->pc
);
2159 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2164 gen_store64(s
, tmp32
, src
);
2165 switch ((insn
>> 3) & 7) {
2167 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2168 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2171 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2174 tcg_temp_free_i32(tmp32
);
2178 gen_helper_f64_to_i32(tmp32
, cpu_env
, src
);
2183 DEST_EA(env
, insn
, opsize
, tmp32
, NULL
);
2184 tcg_temp_free_i32(tmp32
);
2186 case 4: /* fmove to control register. */
2187 switch ((ext
>> 10) & 7) {
2189 /* Not implemented. Ignore writes. */
2194 cpu_abort(NULL
, "Unimplemented: fmove to control %d",
2198 case 5: /* fmove from control register. */
2199 switch ((ext
>> 10) & 7) {
2201 /* Not implemented. Always return zero. */
2202 tmp32
= tcg_const_i32(0);
2207 cpu_abort(NULL
, "Unimplemented: fmove from control %d",
2211 DEST_EA(env
, insn
, OS_LONG
, tmp32
, NULL
);
2213 case 6: /* fmovem */
2219 if ((ext
& 0x1f00) != 0x1000 || (ext
& 0xff) == 0)
2221 tmp32
= gen_lea(env
, s
, insn
, OS_LONG
);
2222 if (IS_NULL_QREG(tmp32
)) {
2226 addr
= tcg_temp_new_i32();
2227 tcg_gen_mov_i32(addr
, tmp32
);
2229 for (i
= 0; i
< 8; i
++) {
2233 if (ext
& (1 << 13)) {
2235 tcg_gen_qemu_stf64(dest
, addr
, IS_USER(s
));
2238 tcg_gen_qemu_ldf64(dest
, addr
, IS_USER(s
));
2240 if (ext
& (mask
- 1))
2241 tcg_gen_addi_i32(addr
, addr
, 8);
2245 tcg_temp_free_i32(addr
);
2249 if (ext
& (1 << 14)) {
2250 /* Source effective address. */
2251 switch ((ext
>> 10) & 7) {
2252 case 0: opsize
= OS_LONG
; break;
2253 case 1: opsize
= OS_SINGLE
; break;
2254 case 4: opsize
= OS_WORD
; break;
2255 case 5: opsize
= OS_DOUBLE
; break;
2256 case 6: opsize
= OS_BYTE
; break;
2260 if (opsize
== OS_DOUBLE
) {
2261 tmp32
= tcg_temp_new_i32();
2262 tcg_gen_mov_i32(tmp32
, AREG(insn
, 0));
2263 switch ((insn
>> 3) & 7) {
2268 tcg_gen_addi_i32(tmp32
, tmp32
, -8);
2271 offset
= cpu_ldsw_code(env
, s
->pc
);
2273 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2276 offset
= cpu_ldsw_code(env
, s
->pc
);
2277 offset
+= s
->pc
- 2;
2279 tcg_gen_addi_i32(tmp32
, tmp32
, offset
);
2284 src
= gen_load64(s
, tmp32
);
2285 switch ((insn
>> 3) & 7) {
2287 tcg_gen_addi_i32(tmp32
, tmp32
, 8);
2288 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2291 tcg_gen_mov_i32(AREG(insn
, 0), tmp32
);
2294 tcg_temp_free_i32(tmp32
);
2296 SRC_EA(env
, tmp32
, opsize
, 1, NULL
);
2297 src
= tcg_temp_new_i64();
2302 gen_helper_i32_to_f64(src
, cpu_env
, tmp32
);
2305 gen_helper_f32_to_f64(src
, cpu_env
, tmp32
);
2310 /* Source register. */
2311 src
= FREG(ext
, 10);
2313 dest
= FREG(ext
, 7);
2314 res
= tcg_temp_new_i64();
2316 tcg_gen_mov_f64(res
, dest
);
2320 case 0: case 0x40: case 0x44: /* fmove */
2321 tcg_gen_mov_f64(res
, src
);
2324 gen_helper_iround_f64(res
, cpu_env
, src
);
2327 case 3: /* fintrz */
2328 gen_helper_itrunc_f64(res
, cpu_env
, src
);
2331 case 4: case 0x41: case 0x45: /* fsqrt */
2332 gen_helper_sqrt_f64(res
, cpu_env
, src
);
2334 case 0x18: case 0x58: case 0x5c: /* fabs */
2335 gen_helper_abs_f64(res
, src
);
2337 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2338 gen_helper_chs_f64(res
, src
);
2340 case 0x20: case 0x60: case 0x64: /* fdiv */
2341 gen_helper_div_f64(res
, cpu_env
, res
, src
);
2343 case 0x22: case 0x62: case 0x66: /* fadd */
2344 gen_helper_add_f64(res
, cpu_env
, res
, src
);
2346 case 0x23: case 0x63: case 0x67: /* fmul */
2347 gen_helper_mul_f64(res
, cpu_env
, res
, src
);
2349 case 0x28: case 0x68: case 0x6c: /* fsub */
2350 gen_helper_sub_f64(res
, cpu_env
, res
, src
);
2352 case 0x38: /* fcmp */
2353 gen_helper_sub_cmp_f64(res
, cpu_env
, res
, src
);
2357 case 0x3a: /* ftst */
2358 tcg_gen_mov_f64(res
, src
);
2365 if (ext
& (1 << 14)) {
2366 tcg_temp_free_i64(src
);
2369 if (opmode
& 0x40) {
2370 if ((opmode
& 0x4) != 0)
2372 } else if ((s
->fpcr
& M68K_FPCR_PREC
) == 0) {
2377 TCGv tmp
= tcg_temp_new_i32();
2378 gen_helper_f64_to_f32(tmp
, cpu_env
, res
);
2379 gen_helper_f32_to_f64(res
, cpu_env
, tmp
);
2380 tcg_temp_free_i32(tmp
);
2382 tcg_gen_mov_f64(QREG_FP_RESULT
, res
);
2384 tcg_gen_mov_f64(dest
, res
);
2386 tcg_temp_free_i64(res
);
2389 /* FIXME: Is this right for offset addressing modes? */
2391 disas_undef_fpu(env
, s
, insn
);
2402 offset
= cpu_ldsw_code(env
, s
->pc
);
2404 if (insn
& (1 << 6)) {
2405 offset
= (offset
<< 16) | cpu_lduw_code(env
, s
->pc
);
2409 l1
= gen_new_label();
2410 /* TODO: Raise BSUN exception. */
2411 flag
= tcg_temp_new();
2412 gen_helper_compare_f64(flag
, cpu_env
, QREG_FP_RESULT
);
2413 /* Jump to l1 if condition is true. */
2414 switch (insn
& 0xf) {
2417 case 1: /* eq (=0) */
2418 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2420 case 2: /* ogt (=1) */
2421 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(1), l1
);
2423 case 3: /* oge (=0 or =1) */
2424 tcg_gen_brcond_i32(TCG_COND_LEU
, flag
, tcg_const_i32(1), l1
);
2426 case 4: /* olt (=-1) */
2427 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(0), l1
);
2429 case 5: /* ole (=-1 or =0) */
2430 tcg_gen_brcond_i32(TCG_COND_LE
, flag
, tcg_const_i32(0), l1
);
2432 case 6: /* ogl (=-1 or =1) */
2433 tcg_gen_andi_i32(flag
, flag
, 1);
2434 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2436 case 7: /* or (=2) */
2437 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(2), l1
);
2439 case 8: /* un (<2) */
2440 tcg_gen_brcond_i32(TCG_COND_LT
, flag
, tcg_const_i32(2), l1
);
2442 case 9: /* ueq (=0 or =2) */
2443 tcg_gen_andi_i32(flag
, flag
, 1);
2444 tcg_gen_brcond_i32(TCG_COND_EQ
, flag
, tcg_const_i32(0), l1
);
2446 case 10: /* ugt (>0) */
2447 tcg_gen_brcond_i32(TCG_COND_GT
, flag
, tcg_const_i32(0), l1
);
2449 case 11: /* uge (>=0) */
2450 tcg_gen_brcond_i32(TCG_COND_GE
, flag
, tcg_const_i32(0), l1
);
2452 case 12: /* ult (=-1 or =2) */
2453 tcg_gen_brcond_i32(TCG_COND_GEU
, flag
, tcg_const_i32(2), l1
);
2455 case 13: /* ule (!=1) */
2456 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(1), l1
);
2458 case 14: /* ne (!=0) */
2459 tcg_gen_brcond_i32(TCG_COND_NE
, flag
, tcg_const_i32(0), l1
);
2465 gen_jmp_tb(s
, 0, s
->pc
);
2467 gen_jmp_tb(s
, 1, addr
+ offset
);
2470 DISAS_INSN(frestore
)
2472 /* TODO: Implement frestore. */
2473 qemu_assert(0, "FRESTORE not implemented");
2478 /* TODO: Implement fsave. */
2479 qemu_assert(0, "FSAVE not implemented");
2482 static inline TCGv
gen_mac_extract_word(DisasContext
*s
, TCGv val
, int upper
)
2484 TCGv tmp
= tcg_temp_new();
2485 if (s
->env
->macsr
& MACSR_FI
) {
2487 tcg_gen_andi_i32(tmp
, val
, 0xffff0000);
2489 tcg_gen_shli_i32(tmp
, val
, 16);
2490 } else if (s
->env
->macsr
& MACSR_SU
) {
2492 tcg_gen_sari_i32(tmp
, val
, 16);
2494 tcg_gen_ext16s_i32(tmp
, val
);
2497 tcg_gen_shri_i32(tmp
, val
, 16);
2499 tcg_gen_ext16u_i32(tmp
, val
);
2504 static void gen_mac_clear_flags(void)
2506 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
,
2507 ~(MACSR_V
| MACSR_Z
| MACSR_N
| MACSR_EV
));
2523 s
->mactmp
= tcg_temp_new_i64();
2527 ext
= cpu_lduw_code(env
, s
->pc
);
2530 acc
= ((insn
>> 7) & 1) | ((ext
>> 3) & 2);
2531 dual
= ((insn
& 0x30) != 0 && (ext
& 3) != 0);
2532 if (dual
&& !m68k_feature(s
->env
, M68K_FEATURE_CF_EMAC_B
)) {
2533 disas_undef(env
, s
, insn
);
2537 /* MAC with load. */
2538 tmp
= gen_lea(env
, s
, insn
, OS_LONG
);
2539 addr
= tcg_temp_new();
2540 tcg_gen_and_i32(addr
, tmp
, QREG_MAC_MASK
);
2541 /* Load the value now to ensure correct exception behavior.
2542 Perform writeback after reading the MAC inputs. */
2543 loadval
= gen_load(s
, OS_LONG
, addr
, 0);
2546 rx
= (ext
& 0x8000) ? AREG(ext
, 12) : DREG(insn
, 12);
2547 ry
= (ext
& 8) ? AREG(ext
, 0) : DREG(ext
, 0);
2549 loadval
= addr
= NULL_QREG
;
2550 rx
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2551 ry
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2554 gen_mac_clear_flags();
2557 /* Disabled because conditional branches clobber temporary vars. */
2558 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && !dual
) {
2559 /* Skip the multiply if we know we will ignore it. */
2560 l1
= gen_new_label();
2561 tmp
= tcg_temp_new();
2562 tcg_gen_andi_i32(tmp
, QREG_MACSR
, 1 << (acc
+ 8));
2563 gen_op_jmp_nz32(tmp
, l1
);
2567 if ((ext
& 0x0800) == 0) {
2569 rx
= gen_mac_extract_word(s
, rx
, (ext
& 0x80) != 0);
2570 ry
= gen_mac_extract_word(s
, ry
, (ext
& 0x40) != 0);
2572 if (s
->env
->macsr
& MACSR_FI
) {
2573 gen_helper_macmulf(s
->mactmp
, cpu_env
, rx
, ry
);
2575 if (s
->env
->macsr
& MACSR_SU
)
2576 gen_helper_macmuls(s
->mactmp
, cpu_env
, rx
, ry
);
2578 gen_helper_macmulu(s
->mactmp
, cpu_env
, rx
, ry
);
2579 switch ((ext
>> 9) & 3) {
2581 tcg_gen_shli_i64(s
->mactmp
, s
->mactmp
, 1);
2584 tcg_gen_shri_i64(s
->mactmp
, s
->mactmp
, 1);
2590 /* Save the overflow flag from the multiply. */
2591 saved_flags
= tcg_temp_new();
2592 tcg_gen_mov_i32(saved_flags
, QREG_MACSR
);
2594 saved_flags
= NULL_QREG
;
2598 /* Disabled because conditional branches clobber temporary vars. */
2599 if ((s
->env
->macsr
& MACSR_OMC
) != 0 && dual
) {
2600 /* Skip the accumulate if the value is already saturated. */
2601 l1
= gen_new_label();
2602 tmp
= tcg_temp_new();
2603 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2604 gen_op_jmp_nz32(tmp
, l1
);
2609 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2611 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2613 if (s
->env
->macsr
& MACSR_FI
)
2614 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2615 else if (s
->env
->macsr
& MACSR_SU
)
2616 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2618 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2621 /* Disabled because conditional branches clobber temporary vars. */
2627 /* Dual accumulate variant. */
2628 acc
= (ext
>> 2) & 3;
2629 /* Restore the overflow flag from the multiplier. */
2630 tcg_gen_mov_i32(QREG_MACSR
, saved_flags
);
2632 /* Disabled because conditional branches clobber temporary vars. */
2633 if ((s
->env
->macsr
& MACSR_OMC
) != 0) {
2634 /* Skip the accumulate if the value is already saturated. */
2635 l1
= gen_new_label();
2636 tmp
= tcg_temp_new();
2637 gen_op_and32(tmp
, QREG_MACSR
, tcg_const_i32(MACSR_PAV0
<< acc
));
2638 gen_op_jmp_nz32(tmp
, l1
);
2642 tcg_gen_sub_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2644 tcg_gen_add_i64(MACREG(acc
), MACREG(acc
), s
->mactmp
);
2645 if (s
->env
->macsr
& MACSR_FI
)
2646 gen_helper_macsatf(cpu_env
, tcg_const_i32(acc
));
2647 else if (s
->env
->macsr
& MACSR_SU
)
2648 gen_helper_macsats(cpu_env
, tcg_const_i32(acc
));
2650 gen_helper_macsatu(cpu_env
, tcg_const_i32(acc
));
2652 /* Disabled because conditional branches clobber temporary vars. */
2657 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(acc
));
2661 rw
= (insn
& 0x40) ? AREG(insn
, 9) : DREG(insn
, 9);
2662 tcg_gen_mov_i32(rw
, loadval
);
2663 /* FIXME: Should address writeback happen with the masked or
2665 switch ((insn
>> 3) & 7) {
2666 case 3: /* Post-increment. */
2667 tcg_gen_addi_i32(AREG(insn
, 0), addr
, 4);
2669 case 4: /* Pre-decrement. */
2670 tcg_gen_mov_i32(AREG(insn
, 0), addr
);
2675 DISAS_INSN(from_mac
)
2681 rx
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2682 accnum
= (insn
>> 9) & 3;
2683 acc
= MACREG(accnum
);
2684 if (s
->env
->macsr
& MACSR_FI
) {
2685 gen_helper_get_macf(rx
, cpu_env
, acc
);
2686 } else if ((s
->env
->macsr
& MACSR_OMC
) == 0) {
2687 tcg_gen_trunc_i64_i32(rx
, acc
);
2688 } else if (s
->env
->macsr
& MACSR_SU
) {
2689 gen_helper_get_macs(rx
, acc
);
2691 gen_helper_get_macu(rx
, acc
);
2694 tcg_gen_movi_i64(acc
, 0);
2695 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2699 DISAS_INSN(move_mac
)
2701 /* FIXME: This can be done without a helper. */
2705 dest
= tcg_const_i32((insn
>> 9) & 3);
2706 gen_helper_mac_move(cpu_env
, dest
, tcg_const_i32(src
));
2707 gen_mac_clear_flags();
2708 gen_helper_mac_set_flags(cpu_env
, dest
);
2711 DISAS_INSN(from_macsr
)
2715 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2716 tcg_gen_mov_i32(reg
, QREG_MACSR
);
2719 DISAS_INSN(from_mask
)
2722 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2723 tcg_gen_mov_i32(reg
, QREG_MAC_MASK
);
2726 DISAS_INSN(from_mext
)
2730 reg
= (insn
& 8) ? AREG(insn
, 0) : DREG(insn
, 0);
2731 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2732 if (s
->env
->macsr
& MACSR_FI
)
2733 gen_helper_get_mac_extf(reg
, cpu_env
, acc
);
2735 gen_helper_get_mac_exti(reg
, cpu_env
, acc
);
2738 DISAS_INSN(macsr_to_ccr
)
2740 tcg_gen_movi_i32(QREG_CC_X
, 0);
2741 tcg_gen_andi_i32(QREG_CC_DEST
, QREG_MACSR
, 0xf);
2742 s
->cc_op
= CC_OP_FLAGS
;
2750 accnum
= (insn
>> 9) & 3;
2751 acc
= MACREG(accnum
);
2752 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2753 if (s
->env
->macsr
& MACSR_FI
) {
2754 tcg_gen_ext_i32_i64(acc
, val
);
2755 tcg_gen_shli_i64(acc
, acc
, 8);
2756 } else if (s
->env
->macsr
& MACSR_SU
) {
2757 tcg_gen_ext_i32_i64(acc
, val
);
2759 tcg_gen_extu_i32_i64(acc
, val
);
2761 tcg_gen_andi_i32(QREG_MACSR
, QREG_MACSR
, ~(MACSR_PAV0
<< accnum
));
2762 gen_mac_clear_flags();
2763 gen_helper_mac_set_flags(cpu_env
, tcg_const_i32(accnum
));
2766 DISAS_INSN(to_macsr
)
2769 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2770 gen_helper_set_macsr(cpu_env
, val
);
2777 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2778 tcg_gen_ori_i32(QREG_MAC_MASK
, val
, 0xffff0000);
2785 SRC_EA(env
, val
, OS_LONG
, 0, NULL
);
2786 acc
= tcg_const_i32((insn
& 0x400) ? 2 : 0);
2787 if (s
->env
->macsr
& MACSR_FI
)
2788 gen_helper_set_mac_extf(cpu_env
, val
, acc
);
2789 else if (s
->env
->macsr
& MACSR_SU
)
2790 gen_helper_set_mac_exts(cpu_env
, val
, acc
);
2792 gen_helper_set_mac_extu(cpu_env
, val
, acc
);
2795 static disas_proc opcode_table
[65536];
2798 register_opcode (disas_proc proc
, uint16_t opcode
, uint16_t mask
)
2804 /* Sanity check. All set bits must be included in the mask. */
2805 if (opcode
& ~mask
) {
2807 "qemu internal error: bogus opcode definition %04x/%04x\n",
2811 /* This could probably be cleverer. For now just optimize the case where
2812 the top bits are known. */
2813 /* Find the first zero bit in the mask. */
2815 while ((i
& mask
) != 0)
2817 /* Iterate over all combinations of this and lower bits. */
2822 from
= opcode
& ~(i
- 1);
2824 for (i
= from
; i
< to
; i
++) {
2825 if ((i
& mask
) == opcode
)
2826 opcode_table
[i
] = proc
;
2830 /* Register m68k opcode handlers. Order is important.
2831 Later insn override earlier ones. */
2832 void register_m68k_insns (CPUM68KState
*env
)
2834 #define INSN(name, opcode, mask, feature) do { \
2835 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2836 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2838 INSN(undef
, 0000, 0000, CF_ISA_A
);
2839 INSN(arith_im
, 0080, fff8
, CF_ISA_A
);
2840 INSN(bitrev
, 00c0
, fff8
, CF_ISA_APLUSC
);
2841 INSN(bitop_reg
, 0100, f1c0
, CF_ISA_A
);
2842 INSN(bitop_reg
, 0140, f1c0
, CF_ISA_A
);
2843 INSN(bitop_reg
, 0180, f1c0
, CF_ISA_A
);
2844 INSN(bitop_reg
, 01c0
, f1c0
, CF_ISA_A
);
2845 INSN(arith_im
, 0280, fff8
, CF_ISA_A
);
2846 INSN(byterev
, 02c0
, fff8
, CF_ISA_APLUSC
);
2847 INSN(arith_im
, 0480, fff8
, CF_ISA_A
);
2848 INSN(ff1
, 04c0
, fff8
, CF_ISA_APLUSC
);
2849 INSN(arith_im
, 0680, fff8
, CF_ISA_A
);
2850 INSN(bitop_im
, 0800, ffc0
, CF_ISA_A
);
2851 INSN(bitop_im
, 0840, ffc0
, CF_ISA_A
);
2852 INSN(bitop_im
, 0880, ffc0
, CF_ISA_A
);
2853 INSN(bitop_im
, 08c0
, ffc0
, CF_ISA_A
);
2854 INSN(arith_im
, 0a80
, fff8
, CF_ISA_A
);
2855 INSN(arith_im
, 0c00
, ff38
, CF_ISA_A
);
2856 INSN(move
, 1000, f000
, CF_ISA_A
);
2857 INSN(move
, 2000, f000
, CF_ISA_A
);
2858 INSN(move
, 3000, f000
, CF_ISA_A
);
2859 INSN(strldsr
, 40e7
, ffff
, CF_ISA_APLUSC
);
2860 INSN(negx
, 4080, fff8
, CF_ISA_A
);
2861 INSN(move_from_sr
, 40c0
, fff8
, CF_ISA_A
);
2862 INSN(lea
, 41c0
, f1c0
, CF_ISA_A
);
2863 INSN(clr
, 4200, ff00
, CF_ISA_A
);
2864 INSN(undef
, 42c0
, ffc0
, CF_ISA_A
);
2865 INSN(move_from_ccr
, 42c0
, fff8
, CF_ISA_A
);
2866 INSN(neg
, 4480, fff8
, CF_ISA_A
);
2867 INSN(move_to_ccr
, 44c0
, ffc0
, CF_ISA_A
);
2868 INSN(not, 4680, fff8
, CF_ISA_A
);
2869 INSN(move_to_sr
, 46c0
, ffc0
, CF_ISA_A
);
2870 INSN(pea
, 4840, ffc0
, CF_ISA_A
);
2871 INSN(swap
, 4840, fff8
, CF_ISA_A
);
2872 INSN(movem
, 48c0
, fbc0
, CF_ISA_A
);
2873 INSN(ext
, 4880, fff8
, CF_ISA_A
);
2874 INSN(ext
, 48c0
, fff8
, CF_ISA_A
);
2875 INSN(ext
, 49c0
, fff8
, CF_ISA_A
);
2876 INSN(tst
, 4a00
, ff00
, CF_ISA_A
);
2877 INSN(tas
, 4ac0
, ffc0
, CF_ISA_B
);
2878 INSN(halt
, 4ac8
, ffff
, CF_ISA_A
);
2879 INSN(pulse
, 4acc
, ffff
, CF_ISA_A
);
2880 INSN(illegal
, 4afc
, ffff
, CF_ISA_A
);
2881 INSN(mull
, 4c00
, ffc0
, CF_ISA_A
);
2882 INSN(divl
, 4c40
, ffc0
, CF_ISA_A
);
2883 INSN(sats
, 4c80
, fff8
, CF_ISA_B
);
2884 INSN(trap
, 4e40
, fff0
, CF_ISA_A
);
2885 INSN(link
, 4e50
, fff8
, CF_ISA_A
);
2886 INSN(unlk
, 4e58
, fff8
, CF_ISA_A
);
2887 INSN(move_to_usp
, 4e60
, fff8
, USP
);
2888 INSN(move_from_usp
, 4e68
, fff8
, USP
);
2889 INSN(nop
, 4e71
, ffff
, CF_ISA_A
);
2890 INSN(stop
, 4e72
, ffff
, CF_ISA_A
);
2891 INSN(rte
, 4e73
, ffff
, CF_ISA_A
);
2892 INSN(rts
, 4e75
, ffff
, CF_ISA_A
);
2893 INSN(movec
, 4e7b
, ffff
, CF_ISA_A
);
2894 INSN(jump
, 4e80
, ffc0
, CF_ISA_A
);
2895 INSN(jump
, 4ec0
, ffc0
, CF_ISA_A
);
2896 INSN(addsubq
, 5180, f1c0
, CF_ISA_A
);
2897 INSN(scc
, 50c0
, f0f8
, CF_ISA_A
);
2898 INSN(addsubq
, 5080, f1c0
, CF_ISA_A
);
2899 INSN(tpf
, 51f8
, fff8
, CF_ISA_A
);
2901 /* Branch instructions. */
2902 INSN(branch
, 6000, f000
, CF_ISA_A
);
2903 /* Disable long branch instructions, then add back the ones we want. */
2904 INSN(undef
, 60ff
, f0ff
, CF_ISA_A
); /* All long branches. */
2905 INSN(branch
, 60ff
, f0ff
, CF_ISA_B
);
2906 INSN(undef
, 60ff
, ffff
, CF_ISA_B
); /* bra.l */
2907 INSN(branch
, 60ff
, ffff
, BRAL
);
2909 INSN(moveq
, 7000, f100
, CF_ISA_A
);
2910 INSN(mvzs
, 7100, f100
, CF_ISA_B
);
2911 INSN(or, 8000, f000
, CF_ISA_A
);
2912 INSN(divw
, 80c0
, f0c0
, CF_ISA_A
);
2913 INSN(addsub
, 9000, f000
, CF_ISA_A
);
2914 INSN(subx
, 9180, f1f8
, CF_ISA_A
);
2915 INSN(suba
, 91c0
, f1c0
, CF_ISA_A
);
2917 INSN(undef_mac
, a000
, f000
, CF_ISA_A
);
2918 INSN(mac
, a000
, f100
, CF_EMAC
);
2919 INSN(from_mac
, a180
, f9b0
, CF_EMAC
);
2920 INSN(move_mac
, a110
, f9fc
, CF_EMAC
);
2921 INSN(from_macsr
,a980
, f9f0
, CF_EMAC
);
2922 INSN(from_mask
, ad80
, fff0
, CF_EMAC
);
2923 INSN(from_mext
, ab80
, fbf0
, CF_EMAC
);
2924 INSN(macsr_to_ccr
, a9c0
, ffff
, CF_EMAC
);
2925 INSN(to_mac
, a100
, f9c0
, CF_EMAC
);
2926 INSN(to_macsr
, a900
, ffc0
, CF_EMAC
);
2927 INSN(to_mext
, ab00
, fbc0
, CF_EMAC
);
2928 INSN(to_mask
, ad00
, ffc0
, CF_EMAC
);
2930 INSN(mov3q
, a140
, f1c0
, CF_ISA_B
);
2931 INSN(cmp
, b000
, f1c0
, CF_ISA_B
); /* cmp.b */
2932 INSN(cmp
, b040
, f1c0
, CF_ISA_B
); /* cmp.w */
2933 INSN(cmpa
, b0c0
, f1c0
, CF_ISA_B
); /* cmpa.w */
2934 INSN(cmp
, b080
, f1c0
, CF_ISA_A
);
2935 INSN(cmpa
, b1c0
, f1c0
, CF_ISA_A
);
2936 INSN(eor
, b180
, f1c0
, CF_ISA_A
);
2937 INSN(and, c000
, f000
, CF_ISA_A
);
2938 INSN(mulw
, c0c0
, f0c0
, CF_ISA_A
);
2939 INSN(addsub
, d000
, f000
, CF_ISA_A
);
2940 INSN(addx
, d180
, f1f8
, CF_ISA_A
);
2941 INSN(adda
, d1c0
, f1c0
, CF_ISA_A
);
2942 INSN(shift_im
, e080
, f0f0
, CF_ISA_A
);
2943 INSN(shift_reg
, e0a0
, f0f0
, CF_ISA_A
);
2944 INSN(undef_fpu
, f000
, f000
, CF_ISA_A
);
2945 INSN(fpu
, f200
, ffc0
, CF_FPU
);
2946 INSN(fbcc
, f280
, ffc0
, CF_FPU
);
2947 INSN(frestore
, f340
, ffc0
, CF_FPU
);
2948 INSN(fsave
, f340
, ffc0
, CF_FPU
);
2949 INSN(intouch
, f340
, ffc0
, CF_ISA_A
);
2950 INSN(cpushl
, f428
, ff38
, CF_ISA_A
);
2951 INSN(wddata
, fb00
, ff00
, CF_ISA_A
);
2952 INSN(wdebug
, fbc0
, ffc0
, CF_ISA_A
);
2956 /* ??? Some of this implementation is not exception safe. We should always
2957 write back the result to memory before setting the condition codes. */
2958 static void disas_m68k_insn(CPUM68KState
* env
, DisasContext
*s
)
2962 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
| CPU_LOG_TB_OP_OPT
))) {
2963 tcg_gen_debug_insn_start(s
->pc
);
2966 insn
= cpu_lduw_code(env
, s
->pc
);
2969 opcode_table
[insn
](env
, s
, insn
);
2972 /* generate intermediate code for basic block 'tb'. */
2974 gen_intermediate_code_internal(M68kCPU
*cpu
, TranslationBlock
*tb
,
2977 CPUState
*cs
= CPU(cpu
);
2978 CPUM68KState
*env
= &cpu
->env
;
2979 DisasContext dc1
, *dc
= &dc1
;
2980 uint16_t *gen_opc_end
;
2983 target_ulong pc_start
;
2988 /* generate intermediate code */
2993 gen_opc_end
= tcg_ctx
.gen_opc_buf
+ OPC_MAX_SIZE
;
2996 dc
->is_jmp
= DISAS_NEXT
;
2998 dc
->cc_op
= CC_OP_DYNAMIC
;
2999 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
3000 dc
->fpcr
= env
->fpcr
;
3001 dc
->user
= (env
->sr
& SR_S
) == 0;
3006 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
3008 max_insns
= CF_COUNT_MASK
;
3012 pc_offset
= dc
->pc
- pc_start
;
3013 gen_throws_exception
= NULL
;
3014 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
3015 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
3016 if (bp
->pc
== dc
->pc
) {
3017 gen_exception(dc
, dc
->pc
, EXCP_DEBUG
);
3018 dc
->is_jmp
= DISAS_JUMP
;
3026 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
3030 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
3032 tcg_ctx
.gen_opc_pc
[lj
] = dc
->pc
;
3033 tcg_ctx
.gen_opc_instr_start
[lj
] = 1;
3034 tcg_ctx
.gen_opc_icount
[lj
] = num_insns
;
3036 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
3038 dc
->insn_pc
= dc
->pc
;
3039 disas_m68k_insn(env
, dc
);
3041 } while (!dc
->is_jmp
&& tcg_ctx
.gen_opc_ptr
< gen_opc_end
&&
3042 !cs
->singlestep_enabled
&&
3044 (pc_offset
) < (TARGET_PAGE_SIZE
- 32) &&
3045 num_insns
< max_insns
);
3047 if (tb
->cflags
& CF_LAST_IO
)
3049 if (unlikely(cs
->singlestep_enabled
)) {
3050 /* Make sure the pc is updated, and raise a debug exception. */
3052 gen_flush_cc_op(dc
);
3053 tcg_gen_movi_i32(QREG_PC
, dc
->pc
);
3055 gen_helper_raise_exception(cpu_env
, tcg_const_i32(EXCP_DEBUG
));
3057 switch(dc
->is_jmp
) {
3059 gen_flush_cc_op(dc
);
3060 gen_jmp_tb(dc
, 0, dc
->pc
);
3065 gen_flush_cc_op(dc
);
3066 /* indicate that the hash table must be used to find the next TB */
3070 /* nothing more to generate */
3074 gen_tb_end(tb
, num_insns
);
3075 *tcg_ctx
.gen_opc_ptr
= INDEX_op_end
;
3078 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
3079 qemu_log("----------------\n");
3080 qemu_log("IN: %s\n", lookup_symbol(pc_start
));
3081 log_target_disas(env
, pc_start
, dc
->pc
- pc_start
, 0);
3086 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
3089 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
3091 tb
->size
= dc
->pc
- pc_start
;
3092 tb
->icount
= num_insns
;
3096 //expand_target_qops();
3099 void gen_intermediate_code(CPUM68KState
*env
, TranslationBlock
*tb
)
3101 gen_intermediate_code_internal(m68k_env_get_cpu(env
), tb
, false);
3104 void gen_intermediate_code_pc(CPUM68KState
*env
, TranslationBlock
*tb
)
3106 gen_intermediate_code_internal(m68k_env_get_cpu(env
), tb
, true);
3109 void m68k_cpu_dump_state(CPUState
*cs
, FILE *f
, fprintf_function cpu_fprintf
,
3112 M68kCPU
*cpu
= M68K_CPU(cs
);
3113 CPUM68KState
*env
= &cpu
->env
;
3117 for (i
= 0; i
< 8; i
++)
3119 u
.d
= env
->fregs
[i
];
3120 cpu_fprintf (f
, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3121 i
, env
->dregs
[i
], i
, env
->aregs
[i
],
3122 i
, u
.l
.upper
, u
.l
.lower
, *(double *)&u
.d
);
3124 cpu_fprintf (f
, "PC = %08x ", env
->pc
);
3126 cpu_fprintf (f
, "SR = %04x %c%c%c%c%c ", sr
, (sr
& 0x10) ? 'X' : '-',
3127 (sr
& CCF_N
) ? 'N' : '-', (sr
& CCF_Z
) ? 'Z' : '-',
3128 (sr
& CCF_V
) ? 'V' : '-', (sr
& CCF_C
) ? 'C' : '-');
3129 cpu_fprintf (f
, "FPRESULT = %12g\n", *(double *)&env
->fp_result
);
3132 void restore_state_to_opc(CPUM68KState
*env
, TranslationBlock
*tb
, int pc_pos
)
3134 env
->pc
= tcg_ctx
.gen_opc_pc
[pc_pos
];