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/>.
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_ptr cpu_env;
47 static char cpu_reg_names[3*8*3 + 5*4];
48 static TCGv cpu_dregs[8];
49 static TCGv cpu_aregs[8];
50 static TCGv_i64 cpu_fregs[8];
51 static TCGv_i64 cpu_macc[4];
53 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
54 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
55 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
56 #define MACREG(acc) cpu_macc[acc]
57 #define QREG_SP cpu_aregs[7]
59 static TCGv NULL_QREG;
60 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
61 /* Used to distinguish stores from bad addressing modes. */
62 static TCGv store_dummy;
64 #include "gen-icount.h"
66 void m68k_tcg_init(void)
71 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
72 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
73 #define DEFF64(name, offset) DEFO64(name, offset)
79 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
82 for (i = 0; i < 8; i++) {
84 cpu_dregs[i] = tcg_global_mem_new(TCG_AREG0,
85 offsetof(CPUM68KState, dregs[i]), p);
88 cpu_aregs[i] = tcg_global_mem_new(TCG_AREG0,
89 offsetof(CPUM68KState, aregs[i]), p);
92 cpu_fregs[i] = tcg_global_mem_new_i64(TCG_AREG0,
93 offsetof(CPUM68KState, fregs[i]), p);
96 for (i = 0; i < 4; i++) {
97 sprintf(p, "ACC%d", i);
98 cpu_macc[i] = tcg_global_mem_new_i64(TCG_AREG0,
99 offsetof(CPUM68KState, macc[i]), p);
103 NULL_QREG = tcg_global_mem_new(TCG_AREG0, -4, "NULL");
104 store_dummy = tcg_global_mem_new(TCG_AREG0, -8, "NULL");
110 static inline void qemu_assert(int cond, const char *msg)
113 fprintf (stderr, "badness: %s\n", msg);
118 /* internal defines */
119 typedef struct DisasContext {
121 target_ulong insn_pc; /* Start of the current instruction. */
127 struct TranslationBlock *tb;
128 int singlestep_enabled;
134 #define DISAS_JUMP_NEXT 4
136 #if defined(CONFIG_USER_ONLY)
139 #define IS_USER(s) s->user
142 /* XXX: move that elsewhere */
143 /* ??? Fix exceptions. */
144 static void *gen_throws_exception;
145 #define gen_last_qop NULL
153 typedef void (*disas_proc)(DisasContext *, uint16_t);
155 #ifdef DEBUG_DISPATCH
156 #define DISAS_INSN(name) \
157 static void real_disas_##name (DisasContext *s, uint16_t insn); \
158 static void disas_##name (DisasContext *s, uint16_t insn) { \
159 qemu_log("Dispatch " #name "\n"); \
160 real_disas_##name(s, insn); } \
161 static void real_disas_##name (DisasContext *s, uint16_t insn)
163 #define DISAS_INSN(name) \
164 static void disas_##name (DisasContext *s, uint16_t insn)
167 /* Generate a load from the specified address. Narrow values are
168 sign extended to full register width. */
169 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
172 int index = IS_USER(s);
174 tmp = tcg_temp_new_i32();
178 tcg_gen_qemu_ld8s(tmp, addr, index);
180 tcg_gen_qemu_ld8u(tmp, addr, index);
184 tcg_gen_qemu_ld16s(tmp, addr, index);
186 tcg_gen_qemu_ld16u(tmp, addr, index);
190 tcg_gen_qemu_ld32u(tmp, addr, index);
193 qemu_assert(0, "bad load size");
195 gen_throws_exception = gen_last_qop;
199 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
202 int index = IS_USER(s);
204 tmp = tcg_temp_new_i64();
205 tcg_gen_qemu_ldf64(tmp, addr, index);
206 gen_throws_exception = gen_last_qop;
210 /* Generate a store. */
211 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
213 int index = IS_USER(s);
217 tcg_gen_qemu_st8(val, addr, index);
220 tcg_gen_qemu_st16(val, addr, index);
224 tcg_gen_qemu_st32(val, addr, index);
227 qemu_assert(0, "bad store size");
229 gen_throws_exception = gen_last_qop;
232 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
234 int index = IS_USER(s);
236 tcg_gen_qemu_stf64(val, addr, index);
237 gen_throws_exception = gen_last_qop;
246 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
247 otherwise generate a store. */
248 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
251 if (what == EA_STORE) {
252 gen_store(s, opsize, addr, val);
255 return gen_load(s, opsize, addr, what == EA_LOADS);
259 /* Read a 32-bit immediate constant. */
260 static inline uint32_t read_im32(DisasContext *s)
263 im = ((uint32_t)cpu_lduw_code(cpu_single_env, s->pc)) << 16;
265 im |= cpu_lduw_code(cpu_single_env, s->pc);
270 /* Calculate and address index. */
271 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
276 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
277 if ((ext & 0x800) == 0) {
278 tcg_gen_ext16s_i32(tmp, add);
281 scale = (ext >> 9) & 3;
283 tcg_gen_shli_i32(tmp, add, scale);
289 /* Handle a base + index + displacement effective addresss.
290 A NULL_QREG base means pc-relative. */
291 static TCGv gen_lea_indexed(DisasContext *s, int opsize, TCGv base)
300 ext = cpu_lduw_code(cpu_single_env, s->pc);
303 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
307 /* full extension word format */
308 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
311 if ((ext & 0x30) > 0x10) {
312 /* base displacement */
313 if ((ext & 0x30) == 0x20) {
314 bd = (int16_t)cpu_lduw_code(cpu_single_env, s->pc);
322 tmp = tcg_temp_new();
323 if ((ext & 0x44) == 0) {
325 add = gen_addr_index(ext, tmp);
329 if ((ext & 0x80) == 0) {
330 /* base not suppressed */
331 if (IS_NULL_QREG(base)) {
332 base = tcg_const_i32(offset + bd);
335 if (!IS_NULL_QREG(add)) {
336 tcg_gen_add_i32(tmp, add, base);
342 if (!IS_NULL_QREG(add)) {
344 tcg_gen_addi_i32(tmp, add, bd);
348 add = tcg_const_i32(bd);
350 if ((ext & 3) != 0) {
351 /* memory indirect */
352 base = gen_load(s, OS_LONG, add, 0);
353 if ((ext & 0x44) == 4) {
354 add = gen_addr_index(ext, tmp);
355 tcg_gen_add_i32(tmp, add, base);
361 /* outer displacement */
362 if ((ext & 3) == 2) {
363 od = (int16_t)cpu_lduw_code(cpu_single_env, s->pc);
372 tcg_gen_addi_i32(tmp, add, od);
377 /* brief extension word format */
378 tmp = tcg_temp_new();
379 add = gen_addr_index(ext, tmp);
380 if (!IS_NULL_QREG(base)) {
381 tcg_gen_add_i32(tmp, add, base);
383 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
385 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
392 /* Update the CPU env CC_OP state. */
393 static inline void gen_flush_cc_op(DisasContext *s)
395 if (s->cc_op != CC_OP_DYNAMIC)
396 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
399 /* Evaluate all the CC flags. */
400 static inline void gen_flush_flags(DisasContext *s)
402 if (s->cc_op == CC_OP_FLAGS)
405 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
406 s->cc_op = CC_OP_FLAGS;
409 static void gen_logic_cc(DisasContext *s, TCGv val)
411 tcg_gen_mov_i32(QREG_CC_DEST, val);
412 s->cc_op = CC_OP_LOGIC;
415 static void gen_update_cc_add(TCGv dest, TCGv src)
417 tcg_gen_mov_i32(QREG_CC_DEST, dest);
418 tcg_gen_mov_i32(QREG_CC_SRC, src);
421 static inline int opsize_bytes(int opsize)
424 case OS_BYTE: return 1;
425 case OS_WORD: return 2;
426 case OS_LONG: return 4;
427 case OS_SINGLE: return 4;
428 case OS_DOUBLE: return 8;
430 qemu_assert(0, "bad operand size");
435 /* Assign value to a register. If the width is less than the register width
436 only the low part of the register is set. */
437 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
442 tcg_gen_andi_i32(reg, reg, 0xffffff00);
443 tmp = tcg_temp_new();
444 tcg_gen_ext8u_i32(tmp, val);
445 tcg_gen_or_i32(reg, reg, tmp);
448 tcg_gen_andi_i32(reg, reg, 0xffff0000);
449 tmp = tcg_temp_new();
450 tcg_gen_ext16u_i32(tmp, val);
451 tcg_gen_or_i32(reg, reg, tmp);
455 tcg_gen_mov_i32(reg, val);
458 qemu_assert(0, "Bad operand size");
463 /* Sign or zero extend a value. */
464 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
470 tmp = tcg_temp_new();
472 tcg_gen_ext8s_i32(tmp, val);
474 tcg_gen_ext8u_i32(tmp, val);
477 tmp = tcg_temp_new();
479 tcg_gen_ext16s_i32(tmp, val);
481 tcg_gen_ext16u_i32(tmp, val);
488 qemu_assert(0, "Bad operand size");
493 /* Generate code for an "effective address". Does not adjust the base
494 register for autoincrement addressing modes. */
495 static TCGv gen_lea(DisasContext *s, uint16_t insn, int opsize)
502 switch ((insn >> 3) & 7) {
503 case 0: /* Data register direct. */
504 case 1: /* Address register direct. */
506 case 2: /* Indirect register */
507 case 3: /* Indirect postincrement. */
508 return AREG(insn, 0);
509 case 4: /* Indirect predecrememnt. */
511 tmp = tcg_temp_new();
512 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
514 case 5: /* Indirect displacement. */
516 tmp = tcg_temp_new();
517 ext = cpu_lduw_code(cpu_single_env, s->pc);
519 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
521 case 6: /* Indirect index + displacement. */
523 return gen_lea_indexed(s, opsize, reg);
526 case 0: /* Absolute short. */
527 offset = cpu_ldsw_code(cpu_single_env, s->pc);
529 return tcg_const_i32(offset);
530 case 1: /* Absolute long. */
531 offset = read_im32(s);
532 return tcg_const_i32(offset);
533 case 2: /* pc displacement */
535 offset += cpu_ldsw_code(cpu_single_env, s->pc);
537 return tcg_const_i32(offset);
538 case 3: /* pc index+displacement. */
539 return gen_lea_indexed(s, opsize, NULL_QREG);
540 case 4: /* Immediate. */
545 /* Should never happen. */
549 /* Helper function for gen_ea. Reuse the computed address between the
550 for read/write operands. */
551 static inline TCGv gen_ea_once(DisasContext *s, uint16_t insn, int opsize,
552 TCGv val, TCGv *addrp, ea_what what)
556 if (addrp && what == EA_STORE) {
559 tmp = gen_lea(s, insn, opsize);
560 if (IS_NULL_QREG(tmp))
565 return gen_ldst(s, opsize, tmp, val, what);
568 /* Generate code to load/store a value ito/from an EA. If VAL > 0 this is
569 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
570 ADDRP is non-null for readwrite operands. */
571 static TCGv gen_ea(DisasContext *s, uint16_t insn, int opsize, TCGv val,
572 TCGv *addrp, ea_what what)
578 switch ((insn >> 3) & 7) {
579 case 0: /* Data register direct. */
581 if (what == EA_STORE) {
582 gen_partset_reg(opsize, reg, val);
585 return gen_extend(reg, opsize, what == EA_LOADS);
587 case 1: /* Address register direct. */
589 if (what == EA_STORE) {
590 tcg_gen_mov_i32(reg, val);
593 return gen_extend(reg, opsize, what == EA_LOADS);
595 case 2: /* Indirect register */
597 return gen_ldst(s, opsize, reg, val, what);
598 case 3: /* Indirect postincrement. */
600 result = gen_ldst(s, opsize, reg, val, what);
601 /* ??? This is not exception safe. The instruction may still
602 fault after this point. */
603 if (what == EA_STORE || !addrp)
604 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
606 case 4: /* Indirect predecrememnt. */
609 if (addrp && what == EA_STORE) {
612 tmp = gen_lea(s, insn, opsize);
613 if (IS_NULL_QREG(tmp))
618 result = gen_ldst(s, opsize, tmp, val, what);
619 /* ??? This is not exception safe. The instruction may still
620 fault after this point. */
621 if (what == EA_STORE || !addrp) {
623 tcg_gen_mov_i32(reg, tmp);
627 case 5: /* Indirect displacement. */
628 case 6: /* Indirect index + displacement. */
629 return gen_ea_once(s, insn, opsize, val, addrp, what);
632 case 0: /* Absolute short. */
633 case 1: /* Absolute long. */
634 case 2: /* pc displacement */
635 case 3: /* pc index+displacement. */
636 return gen_ea_once(s, insn, opsize, val, addrp, what);
637 case 4: /* Immediate. */
638 /* Sign extend values for consistency. */
641 if (what == EA_LOADS) {
642 offset = cpu_ldsb_code(cpu_single_env, s->pc + 1);
644 offset = cpu_ldub_code(cpu_single_env, s->pc + 1);
649 if (what == EA_LOADS) {
650 offset = cpu_ldsw_code(cpu_single_env, s->pc);
652 offset = cpu_lduw_code(cpu_single_env, s->pc);
657 offset = read_im32(s);
660 qemu_assert(0, "Bad immediate operand");
662 return tcg_const_i32(offset);
667 /* Should never happen. */
671 /* This generates a conditional branch, clobbering all temporaries. */
672 static void gen_jmpcc(DisasContext *s, int cond, int l1)
676 /* TODO: Optimize compare/branch pairs rather than always flushing
677 flag state to CC_OP_FLAGS. */
685 case 2: /* HI (!C && !Z) */
686 tmp = tcg_temp_new();
687 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
688 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
690 case 3: /* LS (C || Z) */
691 tmp = tcg_temp_new();
692 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
693 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
695 case 4: /* CC (!C) */
696 tmp = tcg_temp_new();
697 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
698 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
701 tmp = tcg_temp_new();
702 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
703 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
705 case 6: /* NE (!Z) */
706 tmp = tcg_temp_new();
707 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
708 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
711 tmp = tcg_temp_new();
712 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
713 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
715 case 8: /* VC (!V) */
716 tmp = tcg_temp_new();
717 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
718 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
721 tmp = tcg_temp_new();
722 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
723 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
725 case 10: /* PL (!N) */
726 tmp = tcg_temp_new();
727 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
728 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
730 case 11: /* MI (N) */
731 tmp = tcg_temp_new();
732 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
733 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
735 case 12: /* GE (!(N ^ V)) */
736 tmp = tcg_temp_new();
737 assert(CCF_V == (CCF_N >> 2));
738 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
739 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
740 tcg_gen_andi_i32(tmp, tmp, CCF_V);
741 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
743 case 13: /* LT (N ^ V) */
744 tmp = tcg_temp_new();
745 assert(CCF_V == (CCF_N >> 2));
746 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
747 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
748 tcg_gen_andi_i32(tmp, tmp, CCF_V);
749 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
751 case 14: /* GT (!(Z || (N ^ V))) */
752 tmp = tcg_temp_new();
753 assert(CCF_V == (CCF_N >> 2));
754 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
755 tcg_gen_shri_i32(tmp, tmp, 2);
756 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
757 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
758 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
760 case 15: /* LE (Z || (N ^ V)) */
761 tmp = tcg_temp_new();
762 assert(CCF_V == (CCF_N >> 2));
763 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
764 tcg_gen_shri_i32(tmp, tmp, 2);
765 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
766 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
767 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
770 /* Should ever happen. */
781 l1 = gen_new_label();
782 cond = (insn >> 8) & 0xf;
784 tcg_gen_andi_i32(reg, reg, 0xffffff00);
785 /* This is safe because we modify the reg directly, with no other values
787 gen_jmpcc(s, cond ^ 1, l1);
788 tcg_gen_ori_i32(reg, reg, 0xff);
792 /* Force a TB lookup after an instruction that changes the CPU state. */
793 static void gen_lookup_tb(DisasContext *s)
796 tcg_gen_movi_i32(QREG_PC, s->pc);
797 s->is_jmp = DISAS_UPDATE;
800 /* Generate a jump to an immediate address. */
801 static void gen_jmp_im(DisasContext *s, uint32_t dest)
804 tcg_gen_movi_i32(QREG_PC, dest);
805 s->is_jmp = DISAS_JUMP;
808 /* Generate a jump to the address in qreg DEST. */
809 static void gen_jmp(DisasContext *s, TCGv dest)
812 tcg_gen_mov_i32(QREG_PC, dest);
813 s->is_jmp = DISAS_JUMP;
816 static void gen_exception(DisasContext *s, uint32_t where, int nr)
819 gen_jmp_im(s, where);
820 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
823 static inline void gen_addr_fault(DisasContext *s)
825 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
828 #define SRC_EA(result, opsize, op_sign, addrp) do { \
829 result = gen_ea(s, insn, opsize, NULL_QREG, addrp, op_sign ? EA_LOADS : EA_LOADU); \
830 if (IS_NULL_QREG(result)) { \
836 #define DEST_EA(insn, opsize, val, addrp) do { \
837 TCGv ea_result = gen_ea(s, insn, opsize, val, addrp, EA_STORE); \
838 if (IS_NULL_QREG(ea_result)) { \
844 /* Generate a jump to an immediate address. */
845 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
847 TranslationBlock *tb;
850 if (unlikely(s->singlestep_enabled)) {
851 gen_exception(s, dest, EXCP_DEBUG);
852 } else if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
853 (s->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
855 tcg_gen_movi_i32(QREG_PC, dest);
856 tcg_gen_exit_tb((tcg_target_long)tb + n);
861 s->is_jmp = DISAS_TB_JUMP;
864 DISAS_INSN(undef_mac)
866 gen_exception(s, s->pc - 2, EXCP_LINEA);
869 DISAS_INSN(undef_fpu)
871 gen_exception(s, s->pc - 2, EXCP_LINEF);
876 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
877 cpu_abort(cpu_single_env, "Illegal instruction: %04x @ %08x",
888 sign = (insn & 0x100) != 0;
890 tmp = tcg_temp_new();
892 tcg_gen_ext16s_i32(tmp, reg);
894 tcg_gen_ext16u_i32(tmp, reg);
895 SRC_EA(src, OS_WORD, sign, NULL);
896 tcg_gen_mul_i32(tmp, tmp, src);
897 tcg_gen_mov_i32(reg, tmp);
898 /* Unlike m68k, coldfire always clears the overflow bit. */
899 gen_logic_cc(s, tmp);
909 sign = (insn & 0x100) != 0;
912 tcg_gen_ext16s_i32(QREG_DIV1, reg);
914 tcg_gen_ext16u_i32(QREG_DIV1, reg);
916 SRC_EA(src, OS_WORD, sign, NULL);
917 tcg_gen_mov_i32(QREG_DIV2, src);
919 gen_helper_divs(cpu_env, tcg_const_i32(1));
921 gen_helper_divu(cpu_env, tcg_const_i32(1));
924 tmp = tcg_temp_new();
925 src = tcg_temp_new();
926 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
927 tcg_gen_shli_i32(src, QREG_DIV2, 16);
928 tcg_gen_or_i32(reg, tmp, src);
929 s->cc_op = CC_OP_FLAGS;
939 ext = cpu_lduw_code(cpu_single_env, s->pc);
942 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
947 tcg_gen_mov_i32(QREG_DIV1, num);
948 SRC_EA(den, OS_LONG, 0, NULL);
949 tcg_gen_mov_i32(QREG_DIV2, den);
951 gen_helper_divs(cpu_env, tcg_const_i32(0));
953 gen_helper_divu(cpu_env, tcg_const_i32(0));
955 if ((ext & 7) == ((ext >> 12) & 7)) {
957 tcg_gen_mov_i32 (reg, QREG_DIV1);
960 tcg_gen_mov_i32 (reg, QREG_DIV2);
962 s->cc_op = CC_OP_FLAGS;
974 add = (insn & 0x4000) != 0;
976 dest = tcg_temp_new();
978 SRC_EA(tmp, OS_LONG, 0, &addr);
982 SRC_EA(src, OS_LONG, 0, NULL);
985 tcg_gen_add_i32(dest, tmp, src);
986 gen_helper_xflag_lt(QREG_CC_X, dest, src);
987 s->cc_op = CC_OP_ADD;
989 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
990 tcg_gen_sub_i32(dest, tmp, src);
991 s->cc_op = CC_OP_SUB;
993 gen_update_cc_add(dest, src);
995 DEST_EA(insn, OS_LONG, dest, &addr);
997 tcg_gen_mov_i32(reg, dest);
1002 /* Reverse the order of the bits in REG. */
1006 reg = DREG(insn, 0);
1007 gen_helper_bitrev(reg, reg);
1010 DISAS_INSN(bitop_reg)
1020 if ((insn & 0x38) != 0)
1024 op = (insn >> 6) & 3;
1025 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1026 src2 = DREG(insn, 9);
1027 dest = tcg_temp_new();
1030 tmp = tcg_temp_new();
1031 if (opsize == OS_BYTE)
1032 tcg_gen_andi_i32(tmp, src2, 7);
1034 tcg_gen_andi_i32(tmp, src2, 31);
1036 tmp = tcg_temp_new();
1037 tcg_gen_shr_i32(tmp, src1, src2);
1038 tcg_gen_andi_i32(tmp, tmp, 1);
1039 tcg_gen_shli_i32(tmp, tmp, 2);
1040 /* Clear CCF_Z if bit set. */
1041 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1042 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1044 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
1047 tcg_gen_xor_i32(dest, src1, tmp);
1050 tcg_gen_not_i32(tmp, tmp);
1051 tcg_gen_and_i32(dest, src1, tmp);
1054 tcg_gen_or_i32(dest, src1, tmp);
1060 DEST_EA(insn, opsize, dest, &addr);
1066 reg = DREG(insn, 0);
1068 gen_helper_sats(reg, reg, QREG_CC_DEST);
1069 gen_logic_cc(s, reg);
1072 static void gen_push(DisasContext *s, TCGv val)
1076 tmp = tcg_temp_new();
1077 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1078 gen_store(s, OS_LONG, tmp, val);
1079 tcg_gen_mov_i32(QREG_SP, tmp);
1091 mask = cpu_lduw_code(cpu_single_env, s->pc);
1093 tmp = gen_lea(s, insn, OS_LONG);
1094 if (IS_NULL_QREG(tmp)) {
1098 addr = tcg_temp_new();
1099 tcg_gen_mov_i32(addr, tmp);
1100 is_load = ((insn & 0x0400) != 0);
1101 for (i = 0; i < 16; i++, mask >>= 1) {
1108 tmp = gen_load(s, OS_LONG, addr, 0);
1109 tcg_gen_mov_i32(reg, tmp);
1111 gen_store(s, OS_LONG, addr, reg);
1114 tcg_gen_addi_i32(addr, addr, 4);
1119 DISAS_INSN(bitop_im)
1129 if ((insn & 0x38) != 0)
1133 op = (insn >> 6) & 3;
1135 bitnum = cpu_lduw_code(cpu_single_env, s->pc);
1137 if (bitnum & 0xff00) {
1138 disas_undef(s, insn);
1142 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1145 if (opsize == OS_BYTE)
1151 tmp = tcg_temp_new();
1152 assert (CCF_Z == (1 << 2));
1154 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1155 else if (bitnum < 2)
1156 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1158 tcg_gen_mov_i32(tmp, src1);
1159 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1160 /* Clear CCF_Z if bit set. */
1161 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1162 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1166 tcg_gen_xori_i32(tmp, src1, mask);
1169 tcg_gen_andi_i32(tmp, src1, ~mask);
1172 tcg_gen_ori_i32(tmp, src1, mask);
1177 DEST_EA(insn, opsize, tmp, &addr);
1181 DISAS_INSN(arith_im)
1189 op = (insn >> 9) & 7;
1190 SRC_EA(src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1192 dest = tcg_temp_new();
1195 tcg_gen_ori_i32(dest, src1, im);
1196 gen_logic_cc(s, dest);
1199 tcg_gen_andi_i32(dest, src1, im);
1200 gen_logic_cc(s, dest);
1203 tcg_gen_mov_i32(dest, src1);
1204 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1205 tcg_gen_subi_i32(dest, dest, im);
1206 gen_update_cc_add(dest, tcg_const_i32(im));
1207 s->cc_op = CC_OP_SUB;
1210 tcg_gen_mov_i32(dest, src1);
1211 tcg_gen_addi_i32(dest, dest, im);
1212 gen_update_cc_add(dest, tcg_const_i32(im));
1213 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1214 s->cc_op = CC_OP_ADD;
1217 tcg_gen_xori_i32(dest, src1, im);
1218 gen_logic_cc(s, dest);
1221 tcg_gen_mov_i32(dest, src1);
1222 tcg_gen_subi_i32(dest, dest, im);
1223 gen_update_cc_add(dest, tcg_const_i32(im));
1224 s->cc_op = CC_OP_SUB;
1230 DEST_EA(insn, OS_LONG, dest, &addr);
1238 reg = DREG(insn, 0);
1239 tcg_gen_bswap32_i32(reg, reg);
1249 switch (insn >> 12) {
1250 case 1: /* move.b */
1253 case 2: /* move.l */
1256 case 3: /* move.w */
1262 SRC_EA(src, opsize, 1, NULL);
1263 op = (insn >> 6) & 7;
1266 /* The value will already have been sign extended. */
1267 dest = AREG(insn, 9);
1268 tcg_gen_mov_i32(dest, src);
1272 dest_ea = ((insn >> 9) & 7) | (op << 3);
1273 DEST_EA(dest_ea, opsize, src, NULL);
1274 /* This will be correct because loads sign extend. */
1275 gen_logic_cc(s, src);
1284 reg = DREG(insn, 0);
1285 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1293 reg = AREG(insn, 9);
1294 tmp = gen_lea(s, insn, OS_LONG);
1295 if (IS_NULL_QREG(tmp)) {
1299 tcg_gen_mov_i32(reg, tmp);
1306 switch ((insn >> 6) & 3) {
1319 DEST_EA(insn, opsize, tcg_const_i32(0), NULL);
1320 gen_logic_cc(s, tcg_const_i32(0));
1323 static TCGv gen_get_ccr(DisasContext *s)
1328 dest = tcg_temp_new();
1329 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1330 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
1334 DISAS_INSN(move_from_ccr)
1339 ccr = gen_get_ccr(s);
1340 reg = DREG(insn, 0);
1341 gen_partset_reg(OS_WORD, reg, ccr);
1349 reg = DREG(insn, 0);
1350 src1 = tcg_temp_new();
1351 tcg_gen_mov_i32(src1, reg);
1352 tcg_gen_neg_i32(reg, src1);
1353 s->cc_op = CC_OP_SUB;
1354 gen_update_cc_add(reg, src1);
1355 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
1356 s->cc_op = CC_OP_SUB;
1359 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1361 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1362 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
1364 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1368 static void gen_set_sr(DisasContext *s, uint16_t insn, int ccr_only)
1373 s->cc_op = CC_OP_FLAGS;
1374 if ((insn & 0x38) == 0)
1376 tmp = tcg_temp_new();
1377 reg = DREG(insn, 0);
1378 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1379 tcg_gen_shri_i32(tmp, reg, 4);
1380 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1382 gen_helper_set_sr(cpu_env, reg);
1385 else if ((insn & 0x3f) == 0x3c)
1388 val = cpu_lduw_code(cpu_single_env, s->pc);
1390 gen_set_sr_im(s, val, ccr_only);
1393 disas_undef(s, insn);
1396 DISAS_INSN(move_to_ccr)
1398 gen_set_sr(s, insn, 1);
1405 reg = DREG(insn, 0);
1406 tcg_gen_not_i32(reg, reg);
1407 gen_logic_cc(s, reg);
1416 src1 = tcg_temp_new();
1417 src2 = tcg_temp_new();
1418 reg = DREG(insn, 0);
1419 tcg_gen_shli_i32(src1, reg, 16);
1420 tcg_gen_shri_i32(src2, reg, 16);
1421 tcg_gen_or_i32(reg, src1, src2);
1422 gen_logic_cc(s, reg);
1429 tmp = gen_lea(s, insn, OS_LONG);
1430 if (IS_NULL_QREG(tmp)) {
1443 reg = DREG(insn, 0);
1444 op = (insn >> 6) & 7;
1445 tmp = tcg_temp_new();
1447 tcg_gen_ext16s_i32(tmp, reg);
1449 tcg_gen_ext8s_i32(tmp, reg);
1451 gen_partset_reg(OS_WORD, reg, tmp);
1453 tcg_gen_mov_i32(reg, tmp);
1454 gen_logic_cc(s, tmp);
1462 switch ((insn >> 6) & 3) {
1475 SRC_EA(tmp, opsize, 1, NULL);
1476 gen_logic_cc(s, tmp);
1481 /* Implemented as a NOP. */
1486 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1489 /* ??? This should be atomic. */
1496 dest = tcg_temp_new();
1497 SRC_EA(src1, OS_BYTE, 1, &addr);
1498 gen_logic_cc(s, src1);
1499 tcg_gen_ori_i32(dest, src1, 0x80);
1500 DEST_EA(insn, OS_BYTE, dest, &addr);
1510 /* The upper 32 bits of the product are discarded, so
1511 muls.l and mulu.l are functionally equivalent. */
1512 ext = cpu_lduw_code(cpu_single_env, s->pc);
1515 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1518 reg = DREG(ext, 12);
1519 SRC_EA(src1, OS_LONG, 0, NULL);
1520 dest = tcg_temp_new();
1521 tcg_gen_mul_i32(dest, src1, reg);
1522 tcg_gen_mov_i32(reg, dest);
1523 /* Unlike m68k, coldfire always clears the overflow bit. */
1524 gen_logic_cc(s, dest);
1533 offset = cpu_ldsw_code(cpu_single_env, s->pc);
1535 reg = AREG(insn, 0);
1536 tmp = tcg_temp_new();
1537 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1538 gen_store(s, OS_LONG, tmp, reg);
1539 if ((insn & 7) != 7)
1540 tcg_gen_mov_i32(reg, tmp);
1541 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1550 src = tcg_temp_new();
1551 reg = AREG(insn, 0);
1552 tcg_gen_mov_i32(src, reg);
1553 tmp = gen_load(s, OS_LONG, src, 0);
1554 tcg_gen_mov_i32(reg, tmp);
1555 tcg_gen_addi_i32(QREG_SP, src, 4);
1566 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1567 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1575 /* Load the target address first to ensure correct exception
1577 tmp = gen_lea(s, insn, OS_LONG);
1578 if (IS_NULL_QREG(tmp)) {
1582 if ((insn & 0x40) == 0) {
1584 gen_push(s, tcg_const_i32(s->pc));
1597 SRC_EA(src1, OS_LONG, 0, &addr);
1598 val = (insn >> 9) & 7;
1601 dest = tcg_temp_new();
1602 tcg_gen_mov_i32(dest, src1);
1603 if ((insn & 0x38) == 0x08) {
1604 /* Don't update condition codes if the destination is an
1605 address register. */
1606 if (insn & 0x0100) {
1607 tcg_gen_subi_i32(dest, dest, val);
1609 tcg_gen_addi_i32(dest, dest, val);
1612 src2 = tcg_const_i32(val);
1613 if (insn & 0x0100) {
1614 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1615 tcg_gen_subi_i32(dest, dest, val);
1616 s->cc_op = CC_OP_SUB;
1618 tcg_gen_addi_i32(dest, dest, val);
1619 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1620 s->cc_op = CC_OP_ADD;
1622 gen_update_cc_add(dest, src2);
1624 DEST_EA(insn, OS_LONG, dest, &addr);
1630 case 2: /* One extension word. */
1633 case 3: /* Two extension words. */
1636 case 4: /* No extension words. */
1639 disas_undef(s, insn);
1651 op = (insn >> 8) & 0xf;
1652 offset = (int8_t)insn;
1654 offset = cpu_ldsw_code(cpu_single_env, s->pc);
1656 } else if (offset == -1) {
1657 offset = read_im32(s);
1661 gen_push(s, tcg_const_i32(s->pc));
1666 l1 = gen_new_label();
1667 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1668 gen_jmp_tb(s, 1, base + offset);
1670 gen_jmp_tb(s, 0, s->pc);
1672 /* Unconditional branch. */
1673 gen_jmp_tb(s, 0, base + offset);
1682 tcg_gen_movi_i32(DREG(insn, 9), val);
1683 gen_logic_cc(s, tcg_const_i32(val));
1696 SRC_EA(src, opsize, (insn & 0x80) == 0, NULL);
1697 reg = DREG(insn, 9);
1698 tcg_gen_mov_i32(reg, src);
1699 gen_logic_cc(s, src);
1709 reg = DREG(insn, 9);
1710 dest = tcg_temp_new();
1712 SRC_EA(src, OS_LONG, 0, &addr);
1713 tcg_gen_or_i32(dest, src, reg);
1714 DEST_EA(insn, OS_LONG, dest, &addr);
1716 SRC_EA(src, OS_LONG, 0, NULL);
1717 tcg_gen_or_i32(dest, src, reg);
1718 tcg_gen_mov_i32(reg, dest);
1720 gen_logic_cc(s, dest);
1728 SRC_EA(src, OS_LONG, 0, NULL);
1729 reg = AREG(insn, 9);
1730 tcg_gen_sub_i32(reg, reg, src);
1739 reg = DREG(insn, 9);
1740 src = DREG(insn, 0);
1741 gen_helper_subx_cc(reg, cpu_env, reg, src);
1749 val = (insn >> 9) & 7;
1752 src = tcg_const_i32(val);
1753 gen_logic_cc(s, src);
1754 DEST_EA(insn, OS_LONG, src, NULL);
1765 op = (insn >> 6) & 3;
1769 s->cc_op = CC_OP_CMPB;
1773 s->cc_op = CC_OP_CMPW;
1777 s->cc_op = CC_OP_SUB;
1782 SRC_EA(src, opsize, 1, NULL);
1783 reg = DREG(insn, 9);
1784 dest = tcg_temp_new();
1785 tcg_gen_sub_i32(dest, reg, src);
1786 gen_update_cc_add(dest, src);
1801 SRC_EA(src, opsize, 1, NULL);
1802 reg = AREG(insn, 9);
1803 dest = tcg_temp_new();
1804 tcg_gen_sub_i32(dest, reg, src);
1805 gen_update_cc_add(dest, src);
1806 s->cc_op = CC_OP_SUB;
1816 SRC_EA(src, OS_LONG, 0, &addr);
1817 reg = DREG(insn, 9);
1818 dest = tcg_temp_new();
1819 tcg_gen_xor_i32(dest, src, reg);
1820 gen_logic_cc(s, dest);
1821 DEST_EA(insn, OS_LONG, dest, &addr);
1831 reg = DREG(insn, 9);
1832 dest = tcg_temp_new();
1834 SRC_EA(src, OS_LONG, 0, &addr);
1835 tcg_gen_and_i32(dest, src, reg);
1836 DEST_EA(insn, OS_LONG, dest, &addr);
1838 SRC_EA(src, OS_LONG, 0, NULL);
1839 tcg_gen_and_i32(dest, src, reg);
1840 tcg_gen_mov_i32(reg, dest);
1842 gen_logic_cc(s, dest);
1850 SRC_EA(src, OS_LONG, 0, NULL);
1851 reg = AREG(insn, 9);
1852 tcg_gen_add_i32(reg, reg, src);
1861 reg = DREG(insn, 9);
1862 src = DREG(insn, 0);
1863 gen_helper_addx_cc(reg, cpu_env, reg, src);
1864 s->cc_op = CC_OP_FLAGS;
1867 /* TODO: This could be implemented without helper functions. */
1868 DISAS_INSN(shift_im)
1874 reg = DREG(insn, 0);
1875 tmp = (insn >> 9) & 7;
1878 shift = tcg_const_i32(tmp);
1879 /* No need to flush flags becuse we know we will set C flag. */
1881 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1884 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1886 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1889 s->cc_op = CC_OP_SHIFT;
1892 DISAS_INSN(shift_reg)
1897 reg = DREG(insn, 0);
1898 shift = DREG(insn, 9);
1899 /* Shift by zero leaves C flag unmodified. */
1902 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1905 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1907 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1910 s->cc_op = CC_OP_SHIFT;
1916 reg = DREG(insn, 0);
1917 gen_logic_cc(s, reg);
1918 gen_helper_ff1(reg, reg);
1921 static TCGv gen_get_sr(DisasContext *s)
1926 ccr = gen_get_ccr(s);
1927 sr = tcg_temp_new();
1928 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1929 tcg_gen_or_i32(sr, sr, ccr);
1939 ext = cpu_lduw_code(cpu_single_env, s->pc);
1941 if (ext != 0x46FC) {
1942 gen_exception(s, addr, EXCP_UNSUPPORTED);
1945 ext = cpu_lduw_code(cpu_single_env, s->pc);
1947 if (IS_USER(s) || (ext & SR_S) == 0) {
1948 gen_exception(s, addr, EXCP_PRIVILEGE);
1951 gen_push(s, gen_get_sr(s));
1952 gen_set_sr_im(s, ext, 0);
1955 DISAS_INSN(move_from_sr)
1961 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1965 reg = DREG(insn, 0);
1966 gen_partset_reg(OS_WORD, reg, sr);
1969 DISAS_INSN(move_to_sr)
1972 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1975 gen_set_sr(s, insn, 0);
1979 DISAS_INSN(move_from_usp)
1982 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1985 /* TODO: Implement USP. */
1986 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1989 DISAS_INSN(move_to_usp)
1992 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1995 /* TODO: Implement USP. */
1996 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2001 gen_exception(s, s->pc, EXCP_HALT_INSN);
2009 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2013 ext = cpu_lduw_code(cpu_single_env, s->pc);
2016 gen_set_sr_im(s, ext, 0);
2017 tcg_gen_movi_i32(QREG_HALTED, 1);
2018 gen_exception(s, s->pc, EXCP_HLT);
2024 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2027 gen_exception(s, s->pc - 2, EXCP_RTE);
2036 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2040 ext = cpu_lduw_code(cpu_single_env, s->pc);
2044 reg = AREG(ext, 12);
2046 reg = DREG(ext, 12);
2048 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2055 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2058 /* ICache fetch. Implement as no-op. */
2064 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2067 /* Cache push/invalidate. Implement as no-op. */
2072 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2078 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2081 /* TODO: Implement wdebug. */
2082 qemu_assert(0, "WDEBUG not implemented");
2087 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2090 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2091 immediately before the next FP instruction is executed. */
2105 ext = cpu_lduw_code(cpu_single_env, s->pc);
2107 opmode = ext & 0x7f;
2108 switch ((ext >> 13) & 7) {
2113 case 3: /* fmove out */
2115 tmp32 = tcg_temp_new_i32();
2117 /* ??? TODO: Proper behavior on overflow. */
2118 switch ((ext >> 10) & 7) {
2121 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2125 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2129 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2131 case 5: /* OS_DOUBLE */
2132 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2133 switch ((insn >> 3) & 7) {
2138 tcg_gen_addi_i32(tmp32, tmp32, -8);
2141 offset = cpu_ldsw_code(cpu_single_env, s->pc);
2143 tcg_gen_addi_i32(tmp32, tmp32, offset);
2148 gen_store64(s, tmp32, src);
2149 switch ((insn >> 3) & 7) {
2151 tcg_gen_addi_i32(tmp32, tmp32, 8);
2152 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2155 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2158 tcg_temp_free_i32(tmp32);
2162 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2167 DEST_EA(insn, opsize, tmp32, NULL);
2168 tcg_temp_free_i32(tmp32);
2170 case 4: /* fmove to control register. */
2171 switch ((ext >> 10) & 7) {
2173 /* Not implemented. Ignore writes. */
2178 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2182 case 5: /* fmove from control register. */
2183 switch ((ext >> 10) & 7) {
2185 /* Not implemented. Always return zero. */
2186 tmp32 = tcg_const_i32(0);
2191 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2195 DEST_EA(insn, OS_LONG, tmp32, NULL);
2197 case 6: /* fmovem */
2203 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2205 tmp32 = gen_lea(s, insn, OS_LONG);
2206 if (IS_NULL_QREG(tmp32)) {
2210 addr = tcg_temp_new_i32();
2211 tcg_gen_mov_i32(addr, tmp32);
2213 for (i = 0; i < 8; i++) {
2217 if (ext & (1 << 13)) {
2219 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2222 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2224 if (ext & (mask - 1))
2225 tcg_gen_addi_i32(addr, addr, 8);
2229 tcg_temp_free_i32(addr);
2233 if (ext & (1 << 14)) {
2234 /* Source effective address. */
2235 switch ((ext >> 10) & 7) {
2236 case 0: opsize = OS_LONG; break;
2237 case 1: opsize = OS_SINGLE; break;
2238 case 4: opsize = OS_WORD; break;
2239 case 5: opsize = OS_DOUBLE; break;
2240 case 6: opsize = OS_BYTE; break;
2244 if (opsize == OS_DOUBLE) {
2245 tmp32 = tcg_temp_new_i32();
2246 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2247 switch ((insn >> 3) & 7) {
2252 tcg_gen_addi_i32(tmp32, tmp32, -8);
2255 offset = cpu_ldsw_code(cpu_single_env, s->pc);
2257 tcg_gen_addi_i32(tmp32, tmp32, offset);
2260 offset = cpu_ldsw_code(cpu_single_env, s->pc);
2261 offset += s->pc - 2;
2263 tcg_gen_addi_i32(tmp32, tmp32, offset);
2268 src = gen_load64(s, tmp32);
2269 switch ((insn >> 3) & 7) {
2271 tcg_gen_addi_i32(tmp32, tmp32, 8);
2272 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2275 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2278 tcg_temp_free_i32(tmp32);
2280 SRC_EA(tmp32, opsize, 1, NULL);
2281 src = tcg_temp_new_i64();
2286 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2289 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2294 /* Source register. */
2295 src = FREG(ext, 10);
2297 dest = FREG(ext, 7);
2298 res = tcg_temp_new_i64();
2300 tcg_gen_mov_f64(res, dest);
2304 case 0: case 0x40: case 0x44: /* fmove */
2305 tcg_gen_mov_f64(res, src);
2308 gen_helper_iround_f64(res, cpu_env, src);
2311 case 3: /* fintrz */
2312 gen_helper_itrunc_f64(res, cpu_env, src);
2315 case 4: case 0x41: case 0x45: /* fsqrt */
2316 gen_helper_sqrt_f64(res, cpu_env, src);
2318 case 0x18: case 0x58: case 0x5c: /* fabs */
2319 gen_helper_abs_f64(res, src);
2321 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2322 gen_helper_chs_f64(res, src);
2324 case 0x20: case 0x60: case 0x64: /* fdiv */
2325 gen_helper_div_f64(res, cpu_env, res, src);
2327 case 0x22: case 0x62: case 0x66: /* fadd */
2328 gen_helper_add_f64(res, cpu_env, res, src);
2330 case 0x23: case 0x63: case 0x67: /* fmul */
2331 gen_helper_mul_f64(res, cpu_env, res, src);
2333 case 0x28: case 0x68: case 0x6c: /* fsub */
2334 gen_helper_sub_f64(res, cpu_env, res, src);
2336 case 0x38: /* fcmp */
2337 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2341 case 0x3a: /* ftst */
2342 tcg_gen_mov_f64(res, src);
2349 if (ext & (1 << 14)) {
2350 tcg_temp_free_i64(src);
2353 if (opmode & 0x40) {
2354 if ((opmode & 0x4) != 0)
2356 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2361 TCGv tmp = tcg_temp_new_i32();
2362 gen_helper_f64_to_f32(tmp, cpu_env, res);
2363 gen_helper_f32_to_f64(res, cpu_env, tmp);
2364 tcg_temp_free_i32(tmp);
2366 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2368 tcg_gen_mov_f64(dest, res);
2370 tcg_temp_free_i64(res);
2373 /* FIXME: Is this right for offset addressing modes? */
2375 disas_undef_fpu(s, insn);
2386 offset = cpu_ldsw_code(cpu_single_env, s->pc);
2388 if (insn & (1 << 6)) {
2389 offset = (offset << 16) | cpu_lduw_code(cpu_single_env, s->pc);
2393 l1 = gen_new_label();
2394 /* TODO: Raise BSUN exception. */
2395 flag = tcg_temp_new();
2396 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2397 /* Jump to l1 if condition is true. */
2398 switch (insn & 0xf) {
2401 case 1: /* eq (=0) */
2402 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2404 case 2: /* ogt (=1) */
2405 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2407 case 3: /* oge (=0 or =1) */
2408 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2410 case 4: /* olt (=-1) */
2411 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2413 case 5: /* ole (=-1 or =0) */
2414 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2416 case 6: /* ogl (=-1 or =1) */
2417 tcg_gen_andi_i32(flag, flag, 1);
2418 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2420 case 7: /* or (=2) */
2421 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2423 case 8: /* un (<2) */
2424 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2426 case 9: /* ueq (=0 or =2) */
2427 tcg_gen_andi_i32(flag, flag, 1);
2428 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2430 case 10: /* ugt (>0) */
2431 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2433 case 11: /* uge (>=0) */
2434 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2436 case 12: /* ult (=-1 or =2) */
2437 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2439 case 13: /* ule (!=1) */
2440 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2442 case 14: /* ne (!=0) */
2443 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2449 gen_jmp_tb(s, 0, s->pc);
2451 gen_jmp_tb(s, 1, addr + offset);
2454 DISAS_INSN(frestore)
2456 /* TODO: Implement frestore. */
2457 qemu_assert(0, "FRESTORE not implemented");
2462 /* TODO: Implement fsave. */
2463 qemu_assert(0, "FSAVE not implemented");
2466 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2468 TCGv tmp = tcg_temp_new();
2469 if (s->env->macsr & MACSR_FI) {
2471 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2473 tcg_gen_shli_i32(tmp, val, 16);
2474 } else if (s->env->macsr & MACSR_SU) {
2476 tcg_gen_sari_i32(tmp, val, 16);
2478 tcg_gen_ext16s_i32(tmp, val);
2481 tcg_gen_shri_i32(tmp, val, 16);
2483 tcg_gen_ext16u_i32(tmp, val);
2488 static void gen_mac_clear_flags(void)
2490 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2491 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2507 s->mactmp = tcg_temp_new_i64();
2511 ext = cpu_lduw_code(cpu_single_env, s->pc);
2514 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2515 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2516 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2517 disas_undef(s, insn);
2521 /* MAC with load. */
2522 tmp = gen_lea(s, insn, OS_LONG);
2523 addr = tcg_temp_new();
2524 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2525 /* Load the value now to ensure correct exception behavior.
2526 Perform writeback after reading the MAC inputs. */
2527 loadval = gen_load(s, OS_LONG, addr, 0);
2530 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2531 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2533 loadval = addr = NULL_QREG;
2534 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2535 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2538 gen_mac_clear_flags();
2541 /* Disabled because conditional branches clobber temporary vars. */
2542 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2543 /* Skip the multiply if we know we will ignore it. */
2544 l1 = gen_new_label();
2545 tmp = tcg_temp_new();
2546 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2547 gen_op_jmp_nz32(tmp, l1);
2551 if ((ext & 0x0800) == 0) {
2553 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2554 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2556 if (s->env->macsr & MACSR_FI) {
2557 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2559 if (s->env->macsr & MACSR_SU)
2560 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2562 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2563 switch ((ext >> 9) & 3) {
2565 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2568 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2574 /* Save the overflow flag from the multiply. */
2575 saved_flags = tcg_temp_new();
2576 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2578 saved_flags = NULL_QREG;
2582 /* Disabled because conditional branches clobber temporary vars. */
2583 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2584 /* Skip the accumulate if the value is already saturated. */
2585 l1 = gen_new_label();
2586 tmp = tcg_temp_new();
2587 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2588 gen_op_jmp_nz32(tmp, l1);
2593 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2595 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2597 if (s->env->macsr & MACSR_FI)
2598 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2599 else if (s->env->macsr & MACSR_SU)
2600 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2602 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2605 /* Disabled because conditional branches clobber temporary vars. */
2611 /* Dual accumulate variant. */
2612 acc = (ext >> 2) & 3;
2613 /* Restore the overflow flag from the multiplier. */
2614 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2616 /* Disabled because conditional branches clobber temporary vars. */
2617 if ((s->env->macsr & MACSR_OMC) != 0) {
2618 /* Skip the accumulate if the value is already saturated. */
2619 l1 = gen_new_label();
2620 tmp = tcg_temp_new();
2621 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2622 gen_op_jmp_nz32(tmp, l1);
2626 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2628 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2629 if (s->env->macsr & MACSR_FI)
2630 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2631 else if (s->env->macsr & MACSR_SU)
2632 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2634 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2636 /* Disabled because conditional branches clobber temporary vars. */
2641 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2645 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2646 tcg_gen_mov_i32(rw, loadval);
2647 /* FIXME: Should address writeback happen with the masked or
2649 switch ((insn >> 3) & 7) {
2650 case 3: /* Post-increment. */
2651 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2653 case 4: /* Pre-decrement. */
2654 tcg_gen_mov_i32(AREG(insn, 0), addr);
2659 DISAS_INSN(from_mac)
2665 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2666 accnum = (insn >> 9) & 3;
2667 acc = MACREG(accnum);
2668 if (s->env->macsr & MACSR_FI) {
2669 gen_helper_get_macf(rx, cpu_env, acc);
2670 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2671 tcg_gen_trunc_i64_i32(rx, acc);
2672 } else if (s->env->macsr & MACSR_SU) {
2673 gen_helper_get_macs(rx, acc);
2675 gen_helper_get_macu(rx, acc);
2678 tcg_gen_movi_i64(acc, 0);
2679 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2683 DISAS_INSN(move_mac)
2685 /* FIXME: This can be done without a helper. */
2689 dest = tcg_const_i32((insn >> 9) & 3);
2690 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2691 gen_mac_clear_flags();
2692 gen_helper_mac_set_flags(cpu_env, dest);
2695 DISAS_INSN(from_macsr)
2699 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2700 tcg_gen_mov_i32(reg, QREG_MACSR);
2703 DISAS_INSN(from_mask)
2706 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2707 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2710 DISAS_INSN(from_mext)
2714 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2715 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2716 if (s->env->macsr & MACSR_FI)
2717 gen_helper_get_mac_extf(reg, cpu_env, acc);
2719 gen_helper_get_mac_exti(reg, cpu_env, acc);
2722 DISAS_INSN(macsr_to_ccr)
2724 tcg_gen_movi_i32(QREG_CC_X, 0);
2725 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2726 s->cc_op = CC_OP_FLAGS;
2734 accnum = (insn >> 9) & 3;
2735 acc = MACREG(accnum);
2736 SRC_EA(val, OS_LONG, 0, NULL);
2737 if (s->env->macsr & MACSR_FI) {
2738 tcg_gen_ext_i32_i64(acc, val);
2739 tcg_gen_shli_i64(acc, acc, 8);
2740 } else if (s->env->macsr & MACSR_SU) {
2741 tcg_gen_ext_i32_i64(acc, val);
2743 tcg_gen_extu_i32_i64(acc, val);
2745 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2746 gen_mac_clear_flags();
2747 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2750 DISAS_INSN(to_macsr)
2753 SRC_EA(val, OS_LONG, 0, NULL);
2754 gen_helper_set_macsr(cpu_env, val);
2761 SRC_EA(val, OS_LONG, 0, NULL);
2762 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2769 SRC_EA(val, OS_LONG, 0, NULL);
2770 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2771 if (s->env->macsr & MACSR_FI)
2772 gen_helper_set_mac_extf(cpu_env, val, acc);
2773 else if (s->env->macsr & MACSR_SU)
2774 gen_helper_set_mac_exts(cpu_env, val, acc);
2776 gen_helper_set_mac_extu(cpu_env, val, acc);
2779 static disas_proc opcode_table[65536];
2782 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2788 /* Sanity check. All set bits must be included in the mask. */
2789 if (opcode & ~mask) {
2791 "qemu internal error: bogus opcode definition %04x/%04x\n",
2795 /* This could probably be cleverer. For now just optimize the case where
2796 the top bits are known. */
2797 /* Find the first zero bit in the mask. */
2799 while ((i & mask) != 0)
2801 /* Iterate over all combinations of this and lower bits. */
2806 from = opcode & ~(i - 1);
2808 for (i = from; i < to; i++) {
2809 if ((i & mask) == opcode)
2810 opcode_table[i] = proc;
2814 /* Register m68k opcode handlers. Order is important.
2815 Later insn override earlier ones. */
2816 void register_m68k_insns (CPUM68KState *env)
2818 #define INSN(name, opcode, mask, feature) do { \
2819 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2820 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2822 INSN(undef, 0000, 0000, CF_ISA_A);
2823 INSN(arith_im, 0080, fff8, CF_ISA_A);
2824 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2825 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2826 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2827 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2828 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2829 INSN(arith_im, 0280, fff8, CF_ISA_A);
2830 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2831 INSN(arith_im, 0480, fff8, CF_ISA_A);
2832 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2833 INSN(arith_im, 0680, fff8, CF_ISA_A);
2834 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2835 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2836 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2837 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2838 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2839 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2840 INSN(move, 1000, f000, CF_ISA_A);
2841 INSN(move, 2000, f000, CF_ISA_A);
2842 INSN(move, 3000, f000, CF_ISA_A);
2843 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2844 INSN(negx, 4080, fff8, CF_ISA_A);
2845 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2846 INSN(lea, 41c0, f1c0, CF_ISA_A);
2847 INSN(clr, 4200, ff00, CF_ISA_A);
2848 INSN(undef, 42c0, ffc0, CF_ISA_A);
2849 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2850 INSN(neg, 4480, fff8, CF_ISA_A);
2851 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2852 INSN(not, 4680, fff8, CF_ISA_A);
2853 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2854 INSN(pea, 4840, ffc0, CF_ISA_A);
2855 INSN(swap, 4840, fff8, CF_ISA_A);
2856 INSN(movem, 48c0, fbc0, CF_ISA_A);
2857 INSN(ext, 4880, fff8, CF_ISA_A);
2858 INSN(ext, 48c0, fff8, CF_ISA_A);
2859 INSN(ext, 49c0, fff8, CF_ISA_A);
2860 INSN(tst, 4a00, ff00, CF_ISA_A);
2861 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2862 INSN(halt, 4ac8, ffff, CF_ISA_A);
2863 INSN(pulse, 4acc, ffff, CF_ISA_A);
2864 INSN(illegal, 4afc, ffff, CF_ISA_A);
2865 INSN(mull, 4c00, ffc0, CF_ISA_A);
2866 INSN(divl, 4c40, ffc0, CF_ISA_A);
2867 INSN(sats, 4c80, fff8, CF_ISA_B);
2868 INSN(trap, 4e40, fff0, CF_ISA_A);
2869 INSN(link, 4e50, fff8, CF_ISA_A);
2870 INSN(unlk, 4e58, fff8, CF_ISA_A);
2871 INSN(move_to_usp, 4e60, fff8, USP);
2872 INSN(move_from_usp, 4e68, fff8, USP);
2873 INSN(nop, 4e71, ffff, CF_ISA_A);
2874 INSN(stop, 4e72, ffff, CF_ISA_A);
2875 INSN(rte, 4e73, ffff, CF_ISA_A);
2876 INSN(rts, 4e75, ffff, CF_ISA_A);
2877 INSN(movec, 4e7b, ffff, CF_ISA_A);
2878 INSN(jump, 4e80, ffc0, CF_ISA_A);
2879 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2880 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2881 INSN(scc, 50c0, f0f8, CF_ISA_A);
2882 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2883 INSN(tpf, 51f8, fff8, CF_ISA_A);
2885 /* Branch instructions. */
2886 INSN(branch, 6000, f000, CF_ISA_A);
2887 /* Disable long branch instructions, then add back the ones we want. */
2888 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2889 INSN(branch, 60ff, f0ff, CF_ISA_B);
2890 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2891 INSN(branch, 60ff, ffff, BRAL);
2893 INSN(moveq, 7000, f100, CF_ISA_A);
2894 INSN(mvzs, 7100, f100, CF_ISA_B);
2895 INSN(or, 8000, f000, CF_ISA_A);
2896 INSN(divw, 80c0, f0c0, CF_ISA_A);
2897 INSN(addsub, 9000, f000, CF_ISA_A);
2898 INSN(subx, 9180, f1f8, CF_ISA_A);
2899 INSN(suba, 91c0, f1c0, CF_ISA_A);
2901 INSN(undef_mac, a000, f000, CF_ISA_A);
2902 INSN(mac, a000, f100, CF_EMAC);
2903 INSN(from_mac, a180, f9b0, CF_EMAC);
2904 INSN(move_mac, a110, f9fc, CF_EMAC);
2905 INSN(from_macsr,a980, f9f0, CF_EMAC);
2906 INSN(from_mask, ad80, fff0, CF_EMAC);
2907 INSN(from_mext, ab80, fbf0, CF_EMAC);
2908 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2909 INSN(to_mac, a100, f9c0, CF_EMAC);
2910 INSN(to_macsr, a900, ffc0, CF_EMAC);
2911 INSN(to_mext, ab00, fbc0, CF_EMAC);
2912 INSN(to_mask, ad00, ffc0, CF_EMAC);
2914 INSN(mov3q, a140, f1c0, CF_ISA_B);
2915 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2916 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2917 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2918 INSN(cmp, b080, f1c0, CF_ISA_A);
2919 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2920 INSN(eor, b180, f1c0, CF_ISA_A);
2921 INSN(and, c000, f000, CF_ISA_A);
2922 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2923 INSN(addsub, d000, f000, CF_ISA_A);
2924 INSN(addx, d180, f1f8, CF_ISA_A);
2925 INSN(adda, d1c0, f1c0, CF_ISA_A);
2926 INSN(shift_im, e080, f0f0, CF_ISA_A);
2927 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2928 INSN(undef_fpu, f000, f000, CF_ISA_A);
2929 INSN(fpu, f200, ffc0, CF_FPU);
2930 INSN(fbcc, f280, ffc0, CF_FPU);
2931 INSN(frestore, f340, ffc0, CF_FPU);
2932 INSN(fsave, f340, ffc0, CF_FPU);
2933 INSN(intouch, f340, ffc0, CF_ISA_A);
2934 INSN(cpushl, f428, ff38, CF_ISA_A);
2935 INSN(wddata, fb00, ff00, CF_ISA_A);
2936 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2940 /* ??? Some of this implementation is not exception safe. We should always
2941 write back the result to memory before setting the condition codes. */
2942 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
2946 insn = cpu_lduw_code(cpu_single_env, s->pc);
2949 opcode_table[insn](s, insn);
2952 /* generate intermediate code for basic block 'tb'. */
2954 gen_intermediate_code_internal(CPUM68KState *env, TranslationBlock *tb,
2957 DisasContext dc1, *dc = &dc1;
2958 uint16_t *gen_opc_end;
2961 target_ulong pc_start;
2966 /* generate intermediate code */
2971 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
2974 dc->is_jmp = DISAS_NEXT;
2976 dc->cc_op = CC_OP_DYNAMIC;
2977 dc->singlestep_enabled = env->singlestep_enabled;
2978 dc->fpcr = env->fpcr;
2979 dc->user = (env->sr & SR_S) == 0;
2984 max_insns = tb->cflags & CF_COUNT_MASK;
2986 max_insns = CF_COUNT_MASK;
2990 pc_offset = dc->pc - pc_start;
2991 gen_throws_exception = NULL;
2992 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
2993 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
2994 if (bp->pc == dc->pc) {
2995 gen_exception(dc, dc->pc, EXCP_DEBUG);
2996 dc->is_jmp = DISAS_JUMP;
3004 j = gen_opc_ptr - gen_opc_buf;
3008 gen_opc_instr_start[lj++] = 0;
3010 gen_opc_pc[lj] = dc->pc;
3011 gen_opc_instr_start[lj] = 1;
3012 gen_opc_icount[lj] = num_insns;
3014 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
3016 dc->insn_pc = dc->pc;
3017 disas_m68k_insn(env, dc);
3019 } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
3020 !env->singlestep_enabled &&
3022 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3023 num_insns < max_insns);
3025 if (tb->cflags & CF_LAST_IO)
3027 if (unlikely(env->singlestep_enabled)) {
3028 /* Make sure the pc is updated, and raise a debug exception. */
3030 gen_flush_cc_op(dc);
3031 tcg_gen_movi_i32(QREG_PC, dc->pc);
3033 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
3035 switch(dc->is_jmp) {
3037 gen_flush_cc_op(dc);
3038 gen_jmp_tb(dc, 0, dc->pc);
3043 gen_flush_cc_op(dc);
3044 /* indicate that the hash table must be used to find the next TB */
3048 /* nothing more to generate */
3052 gen_icount_end(tb, num_insns);
3053 *gen_opc_ptr = INDEX_op_end;
3056 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
3057 qemu_log("----------------\n");
3058 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3059 log_target_disas(pc_start, dc->pc - pc_start, 0);
3064 j = gen_opc_ptr - gen_opc_buf;
3067 gen_opc_instr_start[lj++] = 0;
3069 tb->size = dc->pc - pc_start;
3070 tb->icount = num_insns;
3074 //expand_target_qops();
3077 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
3079 gen_intermediate_code_internal(env, tb, 0);
3082 void gen_intermediate_code_pc(CPUM68KState *env, TranslationBlock *tb)
3084 gen_intermediate_code_internal(env, tb, 1);
3087 void cpu_dump_state(CPUM68KState *env, FILE *f, fprintf_function cpu_fprintf,
3093 for (i = 0; i < 8; i++)
3095 u.d = env->fregs[i];
3096 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3097 i, env->dregs[i], i, env->aregs[i],
3098 i, u.l.upper, u.l.lower, *(double *)&u.d);
3100 cpu_fprintf (f, "PC = %08x ", env->pc);
3102 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3103 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3104 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3105 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3108 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb, int pc_pos)
3110 env->pc = gen_opc_pc[pc_pos];