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/>.
37 //#define DEBUG_DISPATCH 1
39 /* Fake floating point. */
40 #define tcg_gen_mov_f64 tcg_gen_mov_i64
41 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
42 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
44 #define DEFO32(name, offset) static TCGv QREG_##name;
45 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
46 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
52 static TCGv_ptr cpu_env;
54 static char cpu_reg_names[3*8*3 + 5*4];
55 static TCGv cpu_dregs[8];
56 static TCGv cpu_aregs[8];
57 static TCGv_i64 cpu_fregs[8];
58 static TCGv_i64 cpu_macc[4];
60 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
61 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
62 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
63 #define MACREG(acc) cpu_macc[acc]
64 #define QREG_SP cpu_aregs[7]
66 static TCGv NULL_QREG;
67 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
68 /* Used to distinguish stores from bad addressing modes. */
69 static TCGv store_dummy;
71 #include "gen-icount.h"
73 void m68k_tcg_init(void)
78 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUState, offset), #name);
79 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, offset), #name);
80 #define DEFF64(name, offset) DEFO64(name, offset)
86 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
89 for (i = 0; i < 8; i++) {
91 cpu_dregs[i] = tcg_global_mem_new(TCG_AREG0,
92 offsetof(CPUM68KState, dregs[i]), p);
95 cpu_aregs[i] = tcg_global_mem_new(TCG_AREG0,
96 offsetof(CPUM68KState, aregs[i]), p);
99 cpu_fregs[i] = tcg_global_mem_new_i64(TCG_AREG0,
100 offsetof(CPUM68KState, fregs[i]), p);
103 for (i = 0; i < 4; i++) {
104 sprintf(p, "ACC%d", i);
105 cpu_macc[i] = tcg_global_mem_new_i64(TCG_AREG0,
106 offsetof(CPUM68KState, macc[i]), p);
110 NULL_QREG = tcg_global_mem_new(TCG_AREG0, -4, "NULL");
111 store_dummy = tcg_global_mem_new(TCG_AREG0, -8, "NULL");
117 static inline void qemu_assert(int cond, const char *msg)
120 fprintf (stderr, "badness: %s\n", msg);
125 /* internal defines */
126 typedef struct DisasContext {
128 target_ulong insn_pc; /* Start of the current instruction. */
134 struct TranslationBlock *tb;
135 int singlestep_enabled;
141 #define DISAS_JUMP_NEXT 4
143 #if defined(CONFIG_USER_ONLY)
146 #define IS_USER(s) s->user
149 /* XXX: move that elsewhere */
150 /* ??? Fix exceptions. */
151 static void *gen_throws_exception;
152 #define gen_last_qop NULL
160 typedef void (*disas_proc)(DisasContext *, uint16_t);
162 #ifdef DEBUG_DISPATCH
163 #define DISAS_INSN(name) \
164 static void real_disas_##name (DisasContext *s, uint16_t insn); \
165 static void disas_##name (DisasContext *s, uint16_t insn) { \
166 qemu_log("Dispatch " #name "\n"); \
167 real_disas_##name(s, insn); } \
168 static void real_disas_##name (DisasContext *s, uint16_t insn)
170 #define DISAS_INSN(name) \
171 static void disas_##name (DisasContext *s, uint16_t insn)
174 /* FIXME: Remove this. */
175 #define gen_im32(val) tcg_const_i32(val)
177 /* Generate a load from the specified address. Narrow values are
178 sign extended to full register width. */
179 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
182 int index = IS_USER(s);
184 tmp = tcg_temp_new_i32();
188 tcg_gen_qemu_ld8s(tmp, addr, index);
190 tcg_gen_qemu_ld8u(tmp, addr, index);
194 tcg_gen_qemu_ld16s(tmp, addr, index);
196 tcg_gen_qemu_ld16u(tmp, addr, index);
200 tcg_gen_qemu_ld32u(tmp, addr, index);
203 qemu_assert(0, "bad load size");
205 gen_throws_exception = gen_last_qop;
209 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
212 int index = IS_USER(s);
214 tmp = tcg_temp_new_i64();
215 tcg_gen_qemu_ldf64(tmp, addr, index);
216 gen_throws_exception = gen_last_qop;
220 /* Generate a store. */
221 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
223 int index = IS_USER(s);
227 tcg_gen_qemu_st8(val, addr, index);
230 tcg_gen_qemu_st16(val, addr, index);
234 tcg_gen_qemu_st32(val, addr, index);
237 qemu_assert(0, "bad store size");
239 gen_throws_exception = gen_last_qop;
242 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
244 int index = IS_USER(s);
246 tcg_gen_qemu_stf64(val, addr, index);
247 gen_throws_exception = gen_last_qop;
256 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
257 otherwise generate a store. */
258 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
261 if (what == EA_STORE) {
262 gen_store(s, opsize, addr, val);
265 return gen_load(s, opsize, addr, what == EA_LOADS);
269 /* Read a 32-bit immediate constant. */
270 static inline uint32_t read_im32(DisasContext *s)
273 im = ((uint32_t)lduw_code(s->pc)) << 16;
275 im |= lduw_code(s->pc);
280 /* Calculate and address index. */
281 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
286 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
287 if ((ext & 0x800) == 0) {
288 tcg_gen_ext16s_i32(tmp, add);
291 scale = (ext >> 9) & 3;
293 tcg_gen_shli_i32(tmp, add, scale);
299 /* Handle a base + index + displacement effective addresss.
300 A NULL_QREG base means pc-relative. */
301 static TCGv gen_lea_indexed(DisasContext *s, int opsize, TCGv base)
310 ext = lduw_code(s->pc);
313 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
317 /* full extension word format */
318 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
321 if ((ext & 0x30) > 0x10) {
322 /* base displacement */
323 if ((ext & 0x30) == 0x20) {
324 bd = (int16_t)lduw_code(s->pc);
332 tmp = tcg_temp_new();
333 if ((ext & 0x44) == 0) {
335 add = gen_addr_index(ext, tmp);
339 if ((ext & 0x80) == 0) {
340 /* base not suppressed */
341 if (IS_NULL_QREG(base)) {
342 base = gen_im32(offset + bd);
345 if (!IS_NULL_QREG(add)) {
346 tcg_gen_add_i32(tmp, add, base);
352 if (!IS_NULL_QREG(add)) {
354 tcg_gen_addi_i32(tmp, add, bd);
360 if ((ext & 3) != 0) {
361 /* memory indirect */
362 base = gen_load(s, OS_LONG, add, 0);
363 if ((ext & 0x44) == 4) {
364 add = gen_addr_index(ext, tmp);
365 tcg_gen_add_i32(tmp, add, base);
371 /* outer displacement */
372 if ((ext & 3) == 2) {
373 od = (int16_t)lduw_code(s->pc);
382 tcg_gen_addi_i32(tmp, add, od);
387 /* brief extension word format */
388 tmp = tcg_temp_new();
389 add = gen_addr_index(ext, tmp);
390 if (!IS_NULL_QREG(base)) {
391 tcg_gen_add_i32(tmp, add, base);
393 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
395 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
402 /* Update the CPU env CC_OP state. */
403 static inline void gen_flush_cc_op(DisasContext *s)
405 if (s->cc_op != CC_OP_DYNAMIC)
406 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
409 /* Evaluate all the CC flags. */
410 static inline void gen_flush_flags(DisasContext *s)
412 if (s->cc_op == CC_OP_FLAGS)
415 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
416 s->cc_op = CC_OP_FLAGS;
419 static void gen_logic_cc(DisasContext *s, TCGv val)
421 tcg_gen_mov_i32(QREG_CC_DEST, val);
422 s->cc_op = CC_OP_LOGIC;
425 static void gen_update_cc_add(TCGv dest, TCGv src)
427 tcg_gen_mov_i32(QREG_CC_DEST, dest);
428 tcg_gen_mov_i32(QREG_CC_SRC, src);
431 static inline int opsize_bytes(int opsize)
434 case OS_BYTE: return 1;
435 case OS_WORD: return 2;
436 case OS_LONG: return 4;
437 case OS_SINGLE: return 4;
438 case OS_DOUBLE: return 8;
440 qemu_assert(0, "bad operand size");
445 /* Assign value to a register. If the width is less than the register width
446 only the low part of the register is set. */
447 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
452 tcg_gen_andi_i32(reg, reg, 0xffffff00);
453 tmp = tcg_temp_new();
454 tcg_gen_ext8u_i32(tmp, val);
455 tcg_gen_or_i32(reg, reg, tmp);
458 tcg_gen_andi_i32(reg, reg, 0xffff0000);
459 tmp = tcg_temp_new();
460 tcg_gen_ext16u_i32(tmp, val);
461 tcg_gen_or_i32(reg, reg, tmp);
465 tcg_gen_mov_i32(reg, val);
468 qemu_assert(0, "Bad operand size");
473 /* Sign or zero extend a value. */
474 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
480 tmp = tcg_temp_new();
482 tcg_gen_ext8s_i32(tmp, val);
484 tcg_gen_ext8u_i32(tmp, val);
487 tmp = tcg_temp_new();
489 tcg_gen_ext16s_i32(tmp, val);
491 tcg_gen_ext16u_i32(tmp, val);
498 qemu_assert(0, "Bad operand size");
503 /* Generate code for an "effective address". Does not adjust the base
504 register for autoincrement addressing modes. */
505 static TCGv gen_lea(DisasContext *s, uint16_t insn, int opsize)
512 switch ((insn >> 3) & 7) {
513 case 0: /* Data register direct. */
514 case 1: /* Address register direct. */
516 case 2: /* Indirect register */
517 case 3: /* Indirect postincrement. */
518 return AREG(insn, 0);
519 case 4: /* Indirect predecrememnt. */
521 tmp = tcg_temp_new();
522 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
524 case 5: /* Indirect displacement. */
526 tmp = tcg_temp_new();
527 ext = lduw_code(s->pc);
529 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
531 case 6: /* Indirect index + displacement. */
533 return gen_lea_indexed(s, opsize, reg);
536 case 0: /* Absolute short. */
537 offset = ldsw_code(s->pc);
539 return gen_im32(offset);
540 case 1: /* Absolute long. */
541 offset = read_im32(s);
542 return gen_im32(offset);
543 case 2: /* pc displacement */
545 offset += ldsw_code(s->pc);
547 return gen_im32(offset);
548 case 3: /* pc index+displacement. */
549 return gen_lea_indexed(s, opsize, NULL_QREG);
550 case 4: /* Immediate. */
555 /* Should never happen. */
559 /* Helper function for gen_ea. Reuse the computed address between the
560 for read/write operands. */
561 static inline TCGv gen_ea_once(DisasContext *s, uint16_t insn, int opsize,
562 TCGv val, TCGv *addrp, ea_what what)
566 if (addrp && what == EA_STORE) {
569 tmp = gen_lea(s, insn, opsize);
570 if (IS_NULL_QREG(tmp))
575 return gen_ldst(s, opsize, tmp, val, what);
578 /* Generate code to load/store a value ito/from an EA. If VAL > 0 this is
579 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
580 ADDRP is non-null for readwrite operands. */
581 static TCGv gen_ea(DisasContext *s, uint16_t insn, int opsize, TCGv val,
582 TCGv *addrp, ea_what what)
588 switch ((insn >> 3) & 7) {
589 case 0: /* Data register direct. */
591 if (what == EA_STORE) {
592 gen_partset_reg(opsize, reg, val);
595 return gen_extend(reg, opsize, what == EA_LOADS);
597 case 1: /* Address register direct. */
599 if (what == EA_STORE) {
600 tcg_gen_mov_i32(reg, val);
603 return gen_extend(reg, opsize, what == EA_LOADS);
605 case 2: /* Indirect register */
607 return gen_ldst(s, opsize, reg, val, what);
608 case 3: /* Indirect postincrement. */
610 result = gen_ldst(s, opsize, reg, val, what);
611 /* ??? This is not exception safe. The instruction may still
612 fault after this point. */
613 if (what == EA_STORE || !addrp)
614 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
616 case 4: /* Indirect predecrememnt. */
619 if (addrp && what == EA_STORE) {
622 tmp = gen_lea(s, insn, opsize);
623 if (IS_NULL_QREG(tmp))
628 result = gen_ldst(s, opsize, tmp, val, what);
629 /* ??? This is not exception safe. The instruction may still
630 fault after this point. */
631 if (what == EA_STORE || !addrp) {
633 tcg_gen_mov_i32(reg, tmp);
637 case 5: /* Indirect displacement. */
638 case 6: /* Indirect index + displacement. */
639 return gen_ea_once(s, insn, opsize, val, addrp, what);
642 case 0: /* Absolute short. */
643 case 1: /* Absolute long. */
644 case 2: /* pc displacement */
645 case 3: /* pc index+displacement. */
646 return gen_ea_once(s, insn, opsize, val, addrp, what);
647 case 4: /* Immediate. */
648 /* Sign extend values for consistency. */
651 if (what == EA_LOADS)
652 offset = ldsb_code(s->pc + 1);
654 offset = ldub_code(s->pc + 1);
658 if (what == EA_LOADS)
659 offset = ldsw_code(s->pc);
661 offset = lduw_code(s->pc);
665 offset = read_im32(s);
668 qemu_assert(0, "Bad immediate operand");
670 return tcg_const_i32(offset);
675 /* Should never happen. */
679 /* This generates a conditional branch, clobbering all temporaries. */
680 static void gen_jmpcc(DisasContext *s, int cond, int l1)
684 /* TODO: Optimize compare/branch pairs rather than always flushing
685 flag state to CC_OP_FLAGS. */
693 case 2: /* HI (!C && !Z) */
694 tmp = tcg_temp_new();
695 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
696 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
698 case 3: /* LS (C || Z) */
699 tmp = tcg_temp_new();
700 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
701 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
703 case 4: /* CC (!C) */
704 tmp = tcg_temp_new();
705 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
706 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
709 tmp = tcg_temp_new();
710 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
711 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
713 case 6: /* NE (!Z) */
714 tmp = tcg_temp_new();
715 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
716 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
719 tmp = tcg_temp_new();
720 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
721 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
723 case 8: /* VC (!V) */
724 tmp = tcg_temp_new();
725 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
726 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
729 tmp = tcg_temp_new();
730 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
731 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
733 case 10: /* PL (!N) */
734 tmp = tcg_temp_new();
735 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
736 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
738 case 11: /* MI (N) */
739 tmp = tcg_temp_new();
740 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
741 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
743 case 12: /* GE (!(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_EQ, tmp, 0, l1);
751 case 13: /* LT (N ^ V) */
752 tmp = tcg_temp_new();
753 assert(CCF_V == (CCF_N >> 2));
754 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
755 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
756 tcg_gen_andi_i32(tmp, tmp, CCF_V);
757 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
759 case 14: /* GT (!(Z || (N ^ V))) */
760 tmp = tcg_temp_new();
761 assert(CCF_V == (CCF_N >> 2));
762 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
763 tcg_gen_shri_i32(tmp, tmp, 2);
764 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
765 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
766 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
768 case 15: /* LE (Z || (N ^ V)) */
769 tmp = tcg_temp_new();
770 assert(CCF_V == (CCF_N >> 2));
771 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
772 tcg_gen_shri_i32(tmp, tmp, 2);
773 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
774 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
775 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
778 /* Should ever happen. */
789 l1 = gen_new_label();
790 cond = (insn >> 8) & 0xf;
792 tcg_gen_andi_i32(reg, reg, 0xffffff00);
793 /* This is safe because we modify the reg directly, with no other values
795 gen_jmpcc(s, cond ^ 1, l1);
796 tcg_gen_ori_i32(reg, reg, 0xff);
800 /* Force a TB lookup after an instruction that changes the CPU state. */
801 static void gen_lookup_tb(DisasContext *s)
804 tcg_gen_movi_i32(QREG_PC, s->pc);
805 s->is_jmp = DISAS_UPDATE;
808 /* Generate a jump to an immediate address. */
809 static void gen_jmp_im(DisasContext *s, uint32_t dest)
812 tcg_gen_movi_i32(QREG_PC, dest);
813 s->is_jmp = DISAS_JUMP;
816 /* Generate a jump to the address in qreg DEST. */
817 static void gen_jmp(DisasContext *s, TCGv dest)
820 tcg_gen_mov_i32(QREG_PC, dest);
821 s->is_jmp = DISAS_JUMP;
824 static void gen_exception(DisasContext *s, uint32_t where, int nr)
827 gen_jmp_im(s, where);
828 gen_helper_raise_exception(tcg_const_i32(nr));
831 static inline void gen_addr_fault(DisasContext *s)
833 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
836 #define SRC_EA(result, opsize, op_sign, addrp) do { \
837 result = gen_ea(s, insn, opsize, NULL_QREG, addrp, op_sign ? EA_LOADS : EA_LOADU); \
838 if (IS_NULL_QREG(result)) { \
844 #define DEST_EA(insn, opsize, val, addrp) do { \
845 TCGv ea_result = gen_ea(s, insn, opsize, val, addrp, EA_STORE); \
846 if (IS_NULL_QREG(ea_result)) { \
852 /* Generate a jump to an immediate address. */
853 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
855 TranslationBlock *tb;
858 if (unlikely(s->singlestep_enabled)) {
859 gen_exception(s, dest, EXCP_DEBUG);
860 } else if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
861 (s->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
863 tcg_gen_movi_i32(QREG_PC, dest);
864 tcg_gen_exit_tb((long)tb + n);
869 s->is_jmp = DISAS_TB_JUMP;
872 DISAS_INSN(undef_mac)
874 gen_exception(s, s->pc - 2, EXCP_LINEA);
877 DISAS_INSN(undef_fpu)
879 gen_exception(s, s->pc - 2, EXCP_LINEF);
884 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
885 cpu_abort(cpu_single_env, "Illegal instruction: %04x @ %08x",
896 sign = (insn & 0x100) != 0;
898 tmp = tcg_temp_new();
900 tcg_gen_ext16s_i32(tmp, reg);
902 tcg_gen_ext16u_i32(tmp, reg);
903 SRC_EA(src, OS_WORD, sign, NULL);
904 tcg_gen_mul_i32(tmp, tmp, src);
905 tcg_gen_mov_i32(reg, tmp);
906 /* Unlike m68k, coldfire always clears the overflow bit. */
907 gen_logic_cc(s, tmp);
917 sign = (insn & 0x100) != 0;
920 tcg_gen_ext16s_i32(QREG_DIV1, reg);
922 tcg_gen_ext16u_i32(QREG_DIV1, reg);
924 SRC_EA(src, OS_WORD, sign, NULL);
925 tcg_gen_mov_i32(QREG_DIV2, src);
927 gen_helper_divs(cpu_env, tcg_const_i32(1));
929 gen_helper_divu(cpu_env, tcg_const_i32(1));
932 tmp = tcg_temp_new();
933 src = tcg_temp_new();
934 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
935 tcg_gen_shli_i32(src, QREG_DIV2, 16);
936 tcg_gen_or_i32(reg, tmp, src);
937 s->cc_op = CC_OP_FLAGS;
947 ext = lduw_code(s->pc);
950 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
955 tcg_gen_mov_i32(QREG_DIV1, num);
956 SRC_EA(den, OS_LONG, 0, NULL);
957 tcg_gen_mov_i32(QREG_DIV2, den);
959 gen_helper_divs(cpu_env, tcg_const_i32(0));
961 gen_helper_divu(cpu_env, tcg_const_i32(0));
963 if ((ext & 7) == ((ext >> 12) & 7)) {
965 tcg_gen_mov_i32 (reg, QREG_DIV1);
968 tcg_gen_mov_i32 (reg, QREG_DIV2);
970 s->cc_op = CC_OP_FLAGS;
982 add = (insn & 0x4000) != 0;
984 dest = tcg_temp_new();
986 SRC_EA(tmp, OS_LONG, 0, &addr);
990 SRC_EA(src, OS_LONG, 0, NULL);
993 tcg_gen_add_i32(dest, tmp, src);
994 gen_helper_xflag_lt(QREG_CC_X, dest, src);
995 s->cc_op = CC_OP_ADD;
997 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
998 tcg_gen_sub_i32(dest, tmp, src);
999 s->cc_op = CC_OP_SUB;
1001 gen_update_cc_add(dest, src);
1003 DEST_EA(insn, OS_LONG, dest, &addr);
1005 tcg_gen_mov_i32(reg, dest);
1010 /* Reverse the order of the bits in REG. */
1014 reg = DREG(insn, 0);
1015 gen_helper_bitrev(reg, reg);
1018 DISAS_INSN(bitop_reg)
1028 if ((insn & 0x38) != 0)
1032 op = (insn >> 6) & 3;
1033 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1034 src2 = DREG(insn, 9);
1035 dest = tcg_temp_new();
1038 tmp = tcg_temp_new();
1039 if (opsize == OS_BYTE)
1040 tcg_gen_andi_i32(tmp, src2, 7);
1042 tcg_gen_andi_i32(tmp, src2, 31);
1044 tmp = tcg_temp_new();
1045 tcg_gen_shr_i32(tmp, src1, src2);
1046 tcg_gen_andi_i32(tmp, tmp, 1);
1047 tcg_gen_shli_i32(tmp, tmp, 2);
1048 /* Clear CCF_Z if bit set. */
1049 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1050 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1052 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
1055 tcg_gen_xor_i32(dest, src1, tmp);
1058 tcg_gen_not_i32(tmp, tmp);
1059 tcg_gen_and_i32(dest, src1, tmp);
1062 tcg_gen_or_i32(dest, src1, tmp);
1068 DEST_EA(insn, opsize, dest, &addr);
1074 reg = DREG(insn, 0);
1076 gen_helper_sats(reg, reg, QREG_CC_DEST);
1077 gen_logic_cc(s, reg);
1080 static void gen_push(DisasContext *s, TCGv val)
1084 tmp = tcg_temp_new();
1085 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1086 gen_store(s, OS_LONG, tmp, val);
1087 tcg_gen_mov_i32(QREG_SP, tmp);
1099 mask = lduw_code(s->pc);
1101 tmp = gen_lea(s, insn, OS_LONG);
1102 if (IS_NULL_QREG(tmp)) {
1106 addr = tcg_temp_new();
1107 tcg_gen_mov_i32(addr, tmp);
1108 is_load = ((insn & 0x0400) != 0);
1109 for (i = 0; i < 16; i++, mask >>= 1) {
1116 tmp = gen_load(s, OS_LONG, addr, 0);
1117 tcg_gen_mov_i32(reg, tmp);
1119 gen_store(s, OS_LONG, addr, reg);
1122 tcg_gen_addi_i32(addr, addr, 4);
1127 DISAS_INSN(bitop_im)
1137 if ((insn & 0x38) != 0)
1141 op = (insn >> 6) & 3;
1143 bitnum = lduw_code(s->pc);
1145 if (bitnum & 0xff00) {
1146 disas_undef(s, insn);
1150 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1153 if (opsize == OS_BYTE)
1159 tmp = tcg_temp_new();
1160 assert (CCF_Z == (1 << 2));
1162 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1163 else if (bitnum < 2)
1164 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1166 tcg_gen_mov_i32(tmp, src1);
1167 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1168 /* Clear CCF_Z if bit set. */
1169 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1170 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1174 tcg_gen_xori_i32(tmp, src1, mask);
1177 tcg_gen_andi_i32(tmp, src1, ~mask);
1180 tcg_gen_ori_i32(tmp, src1, mask);
1185 DEST_EA(insn, opsize, tmp, &addr);
1189 DISAS_INSN(arith_im)
1197 op = (insn >> 9) & 7;
1198 SRC_EA(src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1200 dest = tcg_temp_new();
1203 tcg_gen_ori_i32(dest, src1, im);
1204 gen_logic_cc(s, dest);
1207 tcg_gen_andi_i32(dest, src1, im);
1208 gen_logic_cc(s, dest);
1211 tcg_gen_mov_i32(dest, src1);
1212 gen_helper_xflag_lt(QREG_CC_X, dest, gen_im32(im));
1213 tcg_gen_subi_i32(dest, dest, im);
1214 gen_update_cc_add(dest, gen_im32(im));
1215 s->cc_op = CC_OP_SUB;
1218 tcg_gen_mov_i32(dest, src1);
1219 tcg_gen_addi_i32(dest, dest, im);
1220 gen_update_cc_add(dest, gen_im32(im));
1221 gen_helper_xflag_lt(QREG_CC_X, dest, gen_im32(im));
1222 s->cc_op = CC_OP_ADD;
1225 tcg_gen_xori_i32(dest, src1, im);
1226 gen_logic_cc(s, dest);
1229 tcg_gen_mov_i32(dest, src1);
1230 tcg_gen_subi_i32(dest, dest, im);
1231 gen_update_cc_add(dest, gen_im32(im));
1232 s->cc_op = CC_OP_SUB;
1238 DEST_EA(insn, OS_LONG, dest, &addr);
1246 reg = DREG(insn, 0);
1247 tcg_gen_bswap32_i32(reg, reg);
1257 switch (insn >> 12) {
1258 case 1: /* move.b */
1261 case 2: /* move.l */
1264 case 3: /* move.w */
1270 SRC_EA(src, opsize, 1, NULL);
1271 op = (insn >> 6) & 7;
1274 /* The value will already have been sign extended. */
1275 dest = AREG(insn, 9);
1276 tcg_gen_mov_i32(dest, src);
1280 dest_ea = ((insn >> 9) & 7) | (op << 3);
1281 DEST_EA(dest_ea, opsize, src, NULL);
1282 /* This will be correct because loads sign extend. */
1283 gen_logic_cc(s, src);
1292 reg = DREG(insn, 0);
1293 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1301 reg = AREG(insn, 9);
1302 tmp = gen_lea(s, insn, OS_LONG);
1303 if (IS_NULL_QREG(tmp)) {
1307 tcg_gen_mov_i32(reg, tmp);
1314 switch ((insn >> 6) & 3) {
1327 DEST_EA(insn, opsize, gen_im32(0), NULL);
1328 gen_logic_cc(s, gen_im32(0));
1331 static TCGv gen_get_ccr(DisasContext *s)
1336 dest = tcg_temp_new();
1337 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1338 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
1342 DISAS_INSN(move_from_ccr)
1347 ccr = gen_get_ccr(s);
1348 reg = DREG(insn, 0);
1349 gen_partset_reg(OS_WORD, reg, ccr);
1357 reg = DREG(insn, 0);
1358 src1 = tcg_temp_new();
1359 tcg_gen_mov_i32(src1, reg);
1360 tcg_gen_neg_i32(reg, src1);
1361 s->cc_op = CC_OP_SUB;
1362 gen_update_cc_add(reg, src1);
1363 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
1364 s->cc_op = CC_OP_SUB;
1367 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1369 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1370 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
1372 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1376 static void gen_set_sr(DisasContext *s, uint16_t insn, int ccr_only)
1381 s->cc_op = CC_OP_FLAGS;
1382 if ((insn & 0x38) == 0)
1384 tmp = tcg_temp_new();
1385 reg = DREG(insn, 0);
1386 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1387 tcg_gen_shri_i32(tmp, reg, 4);
1388 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1390 gen_helper_set_sr(cpu_env, reg);
1393 else if ((insn & 0x3f) == 0x3c)
1396 val = lduw_code(s->pc);
1398 gen_set_sr_im(s, val, ccr_only);
1401 disas_undef(s, insn);
1404 DISAS_INSN(move_to_ccr)
1406 gen_set_sr(s, insn, 1);
1413 reg = DREG(insn, 0);
1414 tcg_gen_not_i32(reg, reg);
1415 gen_logic_cc(s, reg);
1424 src1 = tcg_temp_new();
1425 src2 = tcg_temp_new();
1426 reg = DREG(insn, 0);
1427 tcg_gen_shli_i32(src1, reg, 16);
1428 tcg_gen_shri_i32(src2, reg, 16);
1429 tcg_gen_or_i32(reg, src1, src2);
1430 gen_logic_cc(s, reg);
1437 tmp = gen_lea(s, insn, OS_LONG);
1438 if (IS_NULL_QREG(tmp)) {
1451 reg = DREG(insn, 0);
1452 op = (insn >> 6) & 7;
1453 tmp = tcg_temp_new();
1455 tcg_gen_ext16s_i32(tmp, reg);
1457 tcg_gen_ext8s_i32(tmp, reg);
1459 gen_partset_reg(OS_WORD, reg, tmp);
1461 tcg_gen_mov_i32(reg, tmp);
1462 gen_logic_cc(s, tmp);
1470 switch ((insn >> 6) & 3) {
1483 SRC_EA(tmp, opsize, 1, NULL);
1484 gen_logic_cc(s, tmp);
1489 /* Implemented as a NOP. */
1494 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1497 /* ??? This should be atomic. */
1504 dest = tcg_temp_new();
1505 SRC_EA(src1, OS_BYTE, 1, &addr);
1506 gen_logic_cc(s, src1);
1507 tcg_gen_ori_i32(dest, src1, 0x80);
1508 DEST_EA(insn, OS_BYTE, dest, &addr);
1518 /* The upper 32 bits of the product are discarded, so
1519 muls.l and mulu.l are functionally equivalent. */
1520 ext = lduw_code(s->pc);
1523 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1526 reg = DREG(ext, 12);
1527 SRC_EA(src1, OS_LONG, 0, NULL);
1528 dest = tcg_temp_new();
1529 tcg_gen_mul_i32(dest, src1, reg);
1530 tcg_gen_mov_i32(reg, dest);
1531 /* Unlike m68k, coldfire always clears the overflow bit. */
1532 gen_logic_cc(s, dest);
1541 offset = ldsw_code(s->pc);
1543 reg = AREG(insn, 0);
1544 tmp = tcg_temp_new();
1545 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1546 gen_store(s, OS_LONG, tmp, reg);
1547 if ((insn & 7) != 7)
1548 tcg_gen_mov_i32(reg, tmp);
1549 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1558 src = tcg_temp_new();
1559 reg = AREG(insn, 0);
1560 tcg_gen_mov_i32(src, reg);
1561 tmp = gen_load(s, OS_LONG, src, 0);
1562 tcg_gen_mov_i32(reg, tmp);
1563 tcg_gen_addi_i32(QREG_SP, src, 4);
1574 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1575 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1583 /* Load the target address first to ensure correct exception
1585 tmp = gen_lea(s, insn, OS_LONG);
1586 if (IS_NULL_QREG(tmp)) {
1590 if ((insn & 0x40) == 0) {
1592 gen_push(s, gen_im32(s->pc));
1605 SRC_EA(src1, OS_LONG, 0, &addr);
1606 val = (insn >> 9) & 7;
1609 dest = tcg_temp_new();
1610 tcg_gen_mov_i32(dest, src1);
1611 if ((insn & 0x38) == 0x08) {
1612 /* Don't update condition codes if the destination is an
1613 address register. */
1614 if (insn & 0x0100) {
1615 tcg_gen_subi_i32(dest, dest, val);
1617 tcg_gen_addi_i32(dest, dest, val);
1620 src2 = gen_im32(val);
1621 if (insn & 0x0100) {
1622 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1623 tcg_gen_subi_i32(dest, dest, val);
1624 s->cc_op = CC_OP_SUB;
1626 tcg_gen_addi_i32(dest, dest, val);
1627 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1628 s->cc_op = CC_OP_ADD;
1630 gen_update_cc_add(dest, src2);
1632 DEST_EA(insn, OS_LONG, dest, &addr);
1638 case 2: /* One extension word. */
1641 case 3: /* Two extension words. */
1644 case 4: /* No extension words. */
1647 disas_undef(s, insn);
1659 op = (insn >> 8) & 0xf;
1660 offset = (int8_t)insn;
1662 offset = ldsw_code(s->pc);
1664 } else if (offset == -1) {
1665 offset = read_im32(s);
1669 gen_push(s, gen_im32(s->pc));
1674 l1 = gen_new_label();
1675 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1676 gen_jmp_tb(s, 1, base + offset);
1678 gen_jmp_tb(s, 0, s->pc);
1680 /* Unconditional branch. */
1681 gen_jmp_tb(s, 0, base + offset);
1690 tcg_gen_movi_i32(DREG(insn, 9), val);
1691 gen_logic_cc(s, tcg_const_i32(val));
1704 SRC_EA(src, opsize, (insn & 0x80) == 0, NULL);
1705 reg = DREG(insn, 9);
1706 tcg_gen_mov_i32(reg, src);
1707 gen_logic_cc(s, src);
1717 reg = DREG(insn, 9);
1718 dest = tcg_temp_new();
1720 SRC_EA(src, OS_LONG, 0, &addr);
1721 tcg_gen_or_i32(dest, src, reg);
1722 DEST_EA(insn, OS_LONG, dest, &addr);
1724 SRC_EA(src, OS_LONG, 0, NULL);
1725 tcg_gen_or_i32(dest, src, reg);
1726 tcg_gen_mov_i32(reg, dest);
1728 gen_logic_cc(s, dest);
1736 SRC_EA(src, OS_LONG, 0, NULL);
1737 reg = AREG(insn, 9);
1738 tcg_gen_sub_i32(reg, reg, src);
1747 reg = DREG(insn, 9);
1748 src = DREG(insn, 0);
1749 gen_helper_subx_cc(reg, cpu_env, reg, src);
1757 val = (insn >> 9) & 7;
1760 src = gen_im32(val);
1761 gen_logic_cc(s, src);
1762 DEST_EA(insn, OS_LONG, src, NULL);
1773 op = (insn >> 6) & 3;
1777 s->cc_op = CC_OP_CMPB;
1781 s->cc_op = CC_OP_CMPW;
1785 s->cc_op = CC_OP_SUB;
1790 SRC_EA(src, opsize, 1, NULL);
1791 reg = DREG(insn, 9);
1792 dest = tcg_temp_new();
1793 tcg_gen_sub_i32(dest, reg, src);
1794 gen_update_cc_add(dest, src);
1809 SRC_EA(src, opsize, 1, NULL);
1810 reg = AREG(insn, 9);
1811 dest = tcg_temp_new();
1812 tcg_gen_sub_i32(dest, reg, src);
1813 gen_update_cc_add(dest, src);
1814 s->cc_op = CC_OP_SUB;
1824 SRC_EA(src, OS_LONG, 0, &addr);
1825 reg = DREG(insn, 9);
1826 dest = tcg_temp_new();
1827 tcg_gen_xor_i32(dest, src, reg);
1828 gen_logic_cc(s, dest);
1829 DEST_EA(insn, OS_LONG, dest, &addr);
1839 reg = DREG(insn, 9);
1840 dest = tcg_temp_new();
1842 SRC_EA(src, OS_LONG, 0, &addr);
1843 tcg_gen_and_i32(dest, src, reg);
1844 DEST_EA(insn, OS_LONG, dest, &addr);
1846 SRC_EA(src, OS_LONG, 0, NULL);
1847 tcg_gen_and_i32(dest, src, reg);
1848 tcg_gen_mov_i32(reg, dest);
1850 gen_logic_cc(s, dest);
1858 SRC_EA(src, OS_LONG, 0, NULL);
1859 reg = AREG(insn, 9);
1860 tcg_gen_add_i32(reg, reg, src);
1869 reg = DREG(insn, 9);
1870 src = DREG(insn, 0);
1871 gen_helper_addx_cc(reg, cpu_env, reg, src);
1872 s->cc_op = CC_OP_FLAGS;
1875 /* TODO: This could be implemented without helper functions. */
1876 DISAS_INSN(shift_im)
1882 reg = DREG(insn, 0);
1883 tmp = (insn >> 9) & 7;
1886 shift = gen_im32(tmp);
1887 /* No need to flush flags becuse we know we will set C flag. */
1889 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1892 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1894 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1897 s->cc_op = CC_OP_SHIFT;
1900 DISAS_INSN(shift_reg)
1905 reg = DREG(insn, 0);
1906 shift = DREG(insn, 9);
1907 /* Shift by zero leaves C flag unmodified. */
1910 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1913 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1915 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1918 s->cc_op = CC_OP_SHIFT;
1924 reg = DREG(insn, 0);
1925 gen_logic_cc(s, reg);
1926 gen_helper_ff1(reg, reg);
1929 static TCGv gen_get_sr(DisasContext *s)
1934 ccr = gen_get_ccr(s);
1935 sr = tcg_temp_new();
1936 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1937 tcg_gen_or_i32(sr, sr, ccr);
1947 ext = lduw_code(s->pc);
1949 if (ext != 0x46FC) {
1950 gen_exception(s, addr, EXCP_UNSUPPORTED);
1953 ext = lduw_code(s->pc);
1955 if (IS_USER(s) || (ext & SR_S) == 0) {
1956 gen_exception(s, addr, EXCP_PRIVILEGE);
1959 gen_push(s, gen_get_sr(s));
1960 gen_set_sr_im(s, ext, 0);
1963 DISAS_INSN(move_from_sr)
1969 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1973 reg = DREG(insn, 0);
1974 gen_partset_reg(OS_WORD, reg, sr);
1977 DISAS_INSN(move_to_sr)
1980 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1983 gen_set_sr(s, insn, 0);
1987 DISAS_INSN(move_from_usp)
1990 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1993 /* TODO: Implement USP. */
1994 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1997 DISAS_INSN(move_to_usp)
2000 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2003 /* TODO: Implement USP. */
2004 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2009 gen_exception(s, s->pc, EXCP_HALT_INSN);
2017 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2021 ext = lduw_code(s->pc);
2024 gen_set_sr_im(s, ext, 0);
2025 tcg_gen_movi_i32(QREG_HALTED, 1);
2026 gen_exception(s, s->pc, EXCP_HLT);
2032 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2035 gen_exception(s, s->pc - 2, EXCP_RTE);
2044 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2048 ext = lduw_code(s->pc);
2052 reg = AREG(ext, 12);
2054 reg = DREG(ext, 12);
2056 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2063 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2066 /* ICache fetch. Implement as no-op. */
2072 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2075 /* Cache push/invalidate. Implement as no-op. */
2080 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2086 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2089 /* TODO: Implement wdebug. */
2090 qemu_assert(0, "WDEBUG not implemented");
2095 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2098 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2099 immediately before the next FP instruction is executed. */
2113 ext = lduw_code(s->pc);
2115 opmode = ext & 0x7f;
2116 switch ((ext >> 13) & 7) {
2121 case 3: /* fmove out */
2123 tmp32 = tcg_temp_new_i32();
2125 /* ??? TODO: Proper behavior on overflow. */
2126 switch ((ext >> 10) & 7) {
2129 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2133 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2137 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2139 case 5: /* OS_DOUBLE */
2140 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2141 switch ((insn >> 3) & 7) {
2146 tcg_gen_addi_i32(tmp32, tmp32, -8);
2149 offset = ldsw_code(s->pc);
2151 tcg_gen_addi_i32(tmp32, tmp32, offset);
2156 gen_store64(s, tmp32, src);
2157 switch ((insn >> 3) & 7) {
2159 tcg_gen_addi_i32(tmp32, tmp32, 8);
2160 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2163 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2166 tcg_temp_free_i32(tmp32);
2170 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2175 DEST_EA(insn, opsize, tmp32, NULL);
2176 tcg_temp_free_i32(tmp32);
2178 case 4: /* fmove to control register. */
2179 switch ((ext >> 10) & 7) {
2181 /* Not implemented. Ignore writes. */
2186 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2190 case 5: /* fmove from control register. */
2191 switch ((ext >> 10) & 7) {
2193 /* Not implemented. Always return zero. */
2194 tmp32 = gen_im32(0);
2199 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2203 DEST_EA(insn, OS_LONG, tmp32, NULL);
2205 case 6: /* fmovem */
2211 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2213 tmp32 = gen_lea(s, insn, OS_LONG);
2214 if (IS_NULL_QREG(tmp32)) {
2218 addr = tcg_temp_new_i32();
2219 tcg_gen_mov_i32(addr, tmp32);
2221 for (i = 0; i < 8; i++) {
2225 if (ext & (1 << 13)) {
2227 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2230 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2232 if (ext & (mask - 1))
2233 tcg_gen_addi_i32(addr, addr, 8);
2237 tcg_temp_free_i32(addr);
2241 if (ext & (1 << 14)) {
2242 /* Source effective address. */
2243 switch ((ext >> 10) & 7) {
2244 case 0: opsize = OS_LONG; break;
2245 case 1: opsize = OS_SINGLE; break;
2246 case 4: opsize = OS_WORD; break;
2247 case 5: opsize = OS_DOUBLE; break;
2248 case 6: opsize = OS_BYTE; break;
2252 if (opsize == OS_DOUBLE) {
2253 tmp32 = tcg_temp_new_i32();
2254 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2255 switch ((insn >> 3) & 7) {
2260 tcg_gen_addi_i32(tmp32, tmp32, -8);
2263 offset = ldsw_code(s->pc);
2265 tcg_gen_addi_i32(tmp32, tmp32, offset);
2268 offset = ldsw_code(s->pc);
2269 offset += s->pc - 2;
2271 tcg_gen_addi_i32(tmp32, tmp32, offset);
2276 src = gen_load64(s, tmp32);
2277 switch ((insn >> 3) & 7) {
2279 tcg_gen_addi_i32(tmp32, tmp32, 8);
2280 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2283 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2286 tcg_temp_free_i32(tmp32);
2288 SRC_EA(tmp32, opsize, 1, NULL);
2289 src = tcg_temp_new_i64();
2294 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2297 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2302 /* Source register. */
2303 src = FREG(ext, 10);
2305 dest = FREG(ext, 7);
2306 res = tcg_temp_new_i64();
2308 tcg_gen_mov_f64(res, dest);
2312 case 0: case 0x40: case 0x44: /* fmove */
2313 tcg_gen_mov_f64(res, src);
2316 gen_helper_iround_f64(res, cpu_env, src);
2319 case 3: /* fintrz */
2320 gen_helper_itrunc_f64(res, cpu_env, src);
2323 case 4: case 0x41: case 0x45: /* fsqrt */
2324 gen_helper_sqrt_f64(res, cpu_env, src);
2326 case 0x18: case 0x58: case 0x5c: /* fabs */
2327 gen_helper_abs_f64(res, src);
2329 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2330 gen_helper_chs_f64(res, src);
2332 case 0x20: case 0x60: case 0x64: /* fdiv */
2333 gen_helper_div_f64(res, cpu_env, res, src);
2335 case 0x22: case 0x62: case 0x66: /* fadd */
2336 gen_helper_add_f64(res, cpu_env, res, src);
2338 case 0x23: case 0x63: case 0x67: /* fmul */
2339 gen_helper_mul_f64(res, cpu_env, res, src);
2341 case 0x28: case 0x68: case 0x6c: /* fsub */
2342 gen_helper_sub_f64(res, cpu_env, res, src);
2344 case 0x38: /* fcmp */
2345 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2349 case 0x3a: /* ftst */
2350 tcg_gen_mov_f64(res, src);
2357 if (ext & (1 << 14)) {
2358 tcg_temp_free_i64(src);
2361 if (opmode & 0x40) {
2362 if ((opmode & 0x4) != 0)
2364 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2369 TCGv tmp = tcg_temp_new_i32();
2370 gen_helper_f64_to_f32(tmp, cpu_env, res);
2371 gen_helper_f32_to_f64(res, cpu_env, tmp);
2372 tcg_temp_free_i32(tmp);
2374 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2376 tcg_gen_mov_f64(dest, res);
2378 tcg_temp_free_i64(res);
2381 /* FIXME: Is this right for offset addressing modes? */
2383 disas_undef_fpu(s, insn);
2394 offset = ldsw_code(s->pc);
2396 if (insn & (1 << 6)) {
2397 offset = (offset << 16) | lduw_code(s->pc);
2401 l1 = gen_new_label();
2402 /* TODO: Raise BSUN exception. */
2403 flag = tcg_temp_new();
2404 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2405 /* Jump to l1 if condition is true. */
2406 switch (insn & 0xf) {
2409 case 1: /* eq (=0) */
2410 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2412 case 2: /* ogt (=1) */
2413 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2415 case 3: /* oge (=0 or =1) */
2416 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2418 case 4: /* olt (=-1) */
2419 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2421 case 5: /* ole (=-1 or =0) */
2422 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2424 case 6: /* ogl (=-1 or =1) */
2425 tcg_gen_andi_i32(flag, flag, 1);
2426 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2428 case 7: /* or (=2) */
2429 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2431 case 8: /* un (<2) */
2432 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2434 case 9: /* ueq (=0 or =2) */
2435 tcg_gen_andi_i32(flag, flag, 1);
2436 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2438 case 10: /* ugt (>0) */
2439 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2441 case 11: /* uge (>=0) */
2442 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2444 case 12: /* ult (=-1 or =2) */
2445 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2447 case 13: /* ule (!=1) */
2448 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2450 case 14: /* ne (!=0) */
2451 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2457 gen_jmp_tb(s, 0, s->pc);
2459 gen_jmp_tb(s, 1, addr + offset);
2462 DISAS_INSN(frestore)
2464 /* TODO: Implement frestore. */
2465 qemu_assert(0, "FRESTORE not implemented");
2470 /* TODO: Implement fsave. */
2471 qemu_assert(0, "FSAVE not implemented");
2474 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2476 TCGv tmp = tcg_temp_new();
2477 if (s->env->macsr & MACSR_FI) {
2479 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2481 tcg_gen_shli_i32(tmp, val, 16);
2482 } else if (s->env->macsr & MACSR_SU) {
2484 tcg_gen_sari_i32(tmp, val, 16);
2486 tcg_gen_ext16s_i32(tmp, val);
2489 tcg_gen_shri_i32(tmp, val, 16);
2491 tcg_gen_ext16u_i32(tmp, val);
2496 static void gen_mac_clear_flags(void)
2498 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2499 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2515 s->mactmp = tcg_temp_new_i64();
2519 ext = lduw_code(s->pc);
2522 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2523 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2524 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2525 disas_undef(s, insn);
2529 /* MAC with load. */
2530 tmp = gen_lea(s, insn, OS_LONG);
2531 addr = tcg_temp_new();
2532 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2533 /* Load the value now to ensure correct exception behavior.
2534 Perform writeback after reading the MAC inputs. */
2535 loadval = gen_load(s, OS_LONG, addr, 0);
2538 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2539 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2541 loadval = addr = NULL_QREG;
2542 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2543 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2546 gen_mac_clear_flags();
2549 /* Disabled because conditional branches clobber temporary vars. */
2550 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2551 /* Skip the multiply if we know we will ignore it. */
2552 l1 = gen_new_label();
2553 tmp = tcg_temp_new();
2554 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2555 gen_op_jmp_nz32(tmp, l1);
2559 if ((ext & 0x0800) == 0) {
2561 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2562 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2564 if (s->env->macsr & MACSR_FI) {
2565 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2567 if (s->env->macsr & MACSR_SU)
2568 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2570 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2571 switch ((ext >> 9) & 3) {
2573 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2576 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2582 /* Save the overflow flag from the multiply. */
2583 saved_flags = tcg_temp_new();
2584 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2586 saved_flags = NULL_QREG;
2590 /* Disabled because conditional branches clobber temporary vars. */
2591 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2592 /* Skip the accumulate if the value is already saturated. */
2593 l1 = gen_new_label();
2594 tmp = tcg_temp_new();
2595 gen_op_and32(tmp, QREG_MACSR, gen_im32(MACSR_PAV0 << acc));
2596 gen_op_jmp_nz32(tmp, l1);
2601 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2603 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2605 if (s->env->macsr & MACSR_FI)
2606 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2607 else if (s->env->macsr & MACSR_SU)
2608 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2610 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2613 /* Disabled because conditional branches clobber temporary vars. */
2619 /* Dual accumulate variant. */
2620 acc = (ext >> 2) & 3;
2621 /* Restore the overflow flag from the multiplier. */
2622 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2624 /* Disabled because conditional branches clobber temporary vars. */
2625 if ((s->env->macsr & MACSR_OMC) != 0) {
2626 /* Skip the accumulate if the value is already saturated. */
2627 l1 = gen_new_label();
2628 tmp = tcg_temp_new();
2629 gen_op_and32(tmp, QREG_MACSR, gen_im32(MACSR_PAV0 << acc));
2630 gen_op_jmp_nz32(tmp, l1);
2634 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2636 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2637 if (s->env->macsr & MACSR_FI)
2638 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2639 else if (s->env->macsr & MACSR_SU)
2640 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2642 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2644 /* Disabled because conditional branches clobber temporary vars. */
2649 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2653 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2654 tcg_gen_mov_i32(rw, loadval);
2655 /* FIXME: Should address writeback happen with the masked or
2657 switch ((insn >> 3) & 7) {
2658 case 3: /* Post-increment. */
2659 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2661 case 4: /* Pre-decrement. */
2662 tcg_gen_mov_i32(AREG(insn, 0), addr);
2667 DISAS_INSN(from_mac)
2673 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2674 accnum = (insn >> 9) & 3;
2675 acc = MACREG(accnum);
2676 if (s->env->macsr & MACSR_FI) {
2677 gen_helper_get_macf(rx, cpu_env, acc);
2678 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2679 tcg_gen_trunc_i64_i32(rx, acc);
2680 } else if (s->env->macsr & MACSR_SU) {
2681 gen_helper_get_macs(rx, acc);
2683 gen_helper_get_macu(rx, acc);
2686 tcg_gen_movi_i64(acc, 0);
2687 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2691 DISAS_INSN(move_mac)
2693 /* FIXME: This can be done without a helper. */
2697 dest = tcg_const_i32((insn >> 9) & 3);
2698 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2699 gen_mac_clear_flags();
2700 gen_helper_mac_set_flags(cpu_env, dest);
2703 DISAS_INSN(from_macsr)
2707 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2708 tcg_gen_mov_i32(reg, QREG_MACSR);
2711 DISAS_INSN(from_mask)
2714 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2715 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2718 DISAS_INSN(from_mext)
2722 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2723 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2724 if (s->env->macsr & MACSR_FI)
2725 gen_helper_get_mac_extf(reg, cpu_env, acc);
2727 gen_helper_get_mac_exti(reg, cpu_env, acc);
2730 DISAS_INSN(macsr_to_ccr)
2732 tcg_gen_movi_i32(QREG_CC_X, 0);
2733 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2734 s->cc_op = CC_OP_FLAGS;
2742 accnum = (insn >> 9) & 3;
2743 acc = MACREG(accnum);
2744 SRC_EA(val, OS_LONG, 0, NULL);
2745 if (s->env->macsr & MACSR_FI) {
2746 tcg_gen_ext_i32_i64(acc, val);
2747 tcg_gen_shli_i64(acc, acc, 8);
2748 } else if (s->env->macsr & MACSR_SU) {
2749 tcg_gen_ext_i32_i64(acc, val);
2751 tcg_gen_extu_i32_i64(acc, val);
2753 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2754 gen_mac_clear_flags();
2755 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2758 DISAS_INSN(to_macsr)
2761 SRC_EA(val, OS_LONG, 0, NULL);
2762 gen_helper_set_macsr(cpu_env, val);
2769 SRC_EA(val, OS_LONG, 0, NULL);
2770 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2777 SRC_EA(val, OS_LONG, 0, NULL);
2778 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2779 if (s->env->macsr & MACSR_FI)
2780 gen_helper_set_mac_extf(cpu_env, val, acc);
2781 else if (s->env->macsr & MACSR_SU)
2782 gen_helper_set_mac_exts(cpu_env, val, acc);
2784 gen_helper_set_mac_extu(cpu_env, val, acc);
2787 static disas_proc opcode_table[65536];
2790 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2796 /* Sanity check. All set bits must be included in the mask. */
2797 if (opcode & ~mask) {
2799 "qemu internal error: bogus opcode definition %04x/%04x\n",
2803 /* This could probably be cleverer. For now just optimize the case where
2804 the top bits are known. */
2805 /* Find the first zero bit in the mask. */
2807 while ((i & mask) != 0)
2809 /* Iterate over all combinations of this and lower bits. */
2814 from = opcode & ~(i - 1);
2816 for (i = from; i < to; i++) {
2817 if ((i & mask) == opcode)
2818 opcode_table[i] = proc;
2822 /* Register m68k opcode handlers. Order is important.
2823 Later insn override earlier ones. */
2824 void register_m68k_insns (CPUM68KState *env)
2826 #define INSN(name, opcode, mask, feature) do { \
2827 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2828 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2830 INSN(undef, 0000, 0000, CF_ISA_A);
2831 INSN(arith_im, 0080, fff8, CF_ISA_A);
2832 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2833 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2834 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2835 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2836 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2837 INSN(arith_im, 0280, fff8, CF_ISA_A);
2838 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2839 INSN(arith_im, 0480, fff8, CF_ISA_A);
2840 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2841 INSN(arith_im, 0680, fff8, CF_ISA_A);
2842 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2843 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2844 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2845 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2846 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2847 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2848 INSN(move, 1000, f000, CF_ISA_A);
2849 INSN(move, 2000, f000, CF_ISA_A);
2850 INSN(move, 3000, f000, CF_ISA_A);
2851 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2852 INSN(negx, 4080, fff8, CF_ISA_A);
2853 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2854 INSN(lea, 41c0, f1c0, CF_ISA_A);
2855 INSN(clr, 4200, ff00, CF_ISA_A);
2856 INSN(undef, 42c0, ffc0, CF_ISA_A);
2857 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2858 INSN(neg, 4480, fff8, CF_ISA_A);
2859 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2860 INSN(not, 4680, fff8, CF_ISA_A);
2861 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2862 INSN(pea, 4840, ffc0, CF_ISA_A);
2863 INSN(swap, 4840, fff8, CF_ISA_A);
2864 INSN(movem, 48c0, fbc0, CF_ISA_A);
2865 INSN(ext, 4880, fff8, CF_ISA_A);
2866 INSN(ext, 48c0, fff8, CF_ISA_A);
2867 INSN(ext, 49c0, fff8, CF_ISA_A);
2868 INSN(tst, 4a00, ff00, CF_ISA_A);
2869 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2870 INSN(halt, 4ac8, ffff, CF_ISA_A);
2871 INSN(pulse, 4acc, ffff, CF_ISA_A);
2872 INSN(illegal, 4afc, ffff, CF_ISA_A);
2873 INSN(mull, 4c00, ffc0, CF_ISA_A);
2874 INSN(divl, 4c40, ffc0, CF_ISA_A);
2875 INSN(sats, 4c80, fff8, CF_ISA_B);
2876 INSN(trap, 4e40, fff0, CF_ISA_A);
2877 INSN(link, 4e50, fff8, CF_ISA_A);
2878 INSN(unlk, 4e58, fff8, CF_ISA_A);
2879 INSN(move_to_usp, 4e60, fff8, USP);
2880 INSN(move_from_usp, 4e68, fff8, USP);
2881 INSN(nop, 4e71, ffff, CF_ISA_A);
2882 INSN(stop, 4e72, ffff, CF_ISA_A);
2883 INSN(rte, 4e73, ffff, CF_ISA_A);
2884 INSN(rts, 4e75, ffff, CF_ISA_A);
2885 INSN(movec, 4e7b, ffff, CF_ISA_A);
2886 INSN(jump, 4e80, ffc0, CF_ISA_A);
2887 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2888 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2889 INSN(scc, 50c0, f0f8, CF_ISA_A);
2890 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2891 INSN(tpf, 51f8, fff8, CF_ISA_A);
2893 /* Branch instructions. */
2894 INSN(branch, 6000, f000, CF_ISA_A);
2895 /* Disable long branch instructions, then add back the ones we want. */
2896 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2897 INSN(branch, 60ff, f0ff, CF_ISA_B);
2898 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2899 INSN(branch, 60ff, ffff, BRAL);
2901 INSN(moveq, 7000, f100, CF_ISA_A);
2902 INSN(mvzs, 7100, f100, CF_ISA_B);
2903 INSN(or, 8000, f000, CF_ISA_A);
2904 INSN(divw, 80c0, f0c0, CF_ISA_A);
2905 INSN(addsub, 9000, f000, CF_ISA_A);
2906 INSN(subx, 9180, f1f8, CF_ISA_A);
2907 INSN(suba, 91c0, f1c0, CF_ISA_A);
2909 INSN(undef_mac, a000, f000, CF_ISA_A);
2910 INSN(mac, a000, f100, CF_EMAC);
2911 INSN(from_mac, a180, f9b0, CF_EMAC);
2912 INSN(move_mac, a110, f9fc, CF_EMAC);
2913 INSN(from_macsr,a980, f9f0, CF_EMAC);
2914 INSN(from_mask, ad80, fff0, CF_EMAC);
2915 INSN(from_mext, ab80, fbf0, CF_EMAC);
2916 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2917 INSN(to_mac, a100, f9c0, CF_EMAC);
2918 INSN(to_macsr, a900, ffc0, CF_EMAC);
2919 INSN(to_mext, ab00, fbc0, CF_EMAC);
2920 INSN(to_mask, ad00, ffc0, CF_EMAC);
2922 INSN(mov3q, a140, f1c0, CF_ISA_B);
2923 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2924 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2925 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2926 INSN(cmp, b080, f1c0, CF_ISA_A);
2927 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2928 INSN(eor, b180, f1c0, CF_ISA_A);
2929 INSN(and, c000, f000, CF_ISA_A);
2930 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2931 INSN(addsub, d000, f000, CF_ISA_A);
2932 INSN(addx, d180, f1f8, CF_ISA_A);
2933 INSN(adda, d1c0, f1c0, CF_ISA_A);
2934 INSN(shift_im, e080, f0f0, CF_ISA_A);
2935 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2936 INSN(undef_fpu, f000, f000, CF_ISA_A);
2937 INSN(fpu, f200, ffc0, CF_FPU);
2938 INSN(fbcc, f280, ffc0, CF_FPU);
2939 INSN(frestore, f340, ffc0, CF_FPU);
2940 INSN(fsave, f340, ffc0, CF_FPU);
2941 INSN(intouch, f340, ffc0, CF_ISA_A);
2942 INSN(cpushl, f428, ff38, CF_ISA_A);
2943 INSN(wddata, fb00, ff00, CF_ISA_A);
2944 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2948 /* ??? Some of this implementation is not exception safe. We should always
2949 write back the result to memory before setting the condition codes. */
2950 static void disas_m68k_insn(CPUState * env, DisasContext *s)
2954 insn = lduw_code(s->pc);
2957 opcode_table[insn](s, insn);
2960 /* generate intermediate code for basic block 'tb'. */
2962 gen_intermediate_code_internal(CPUState *env, TranslationBlock *tb,
2965 DisasContext dc1, *dc = &dc1;
2966 uint16_t *gen_opc_end;
2969 target_ulong pc_start;
2974 /* generate intermediate code */
2979 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
2982 dc->is_jmp = DISAS_NEXT;
2984 dc->cc_op = CC_OP_DYNAMIC;
2985 dc->singlestep_enabled = env->singlestep_enabled;
2986 dc->fpcr = env->fpcr;
2987 dc->user = (env->sr & SR_S) == 0;
2992 max_insns = tb->cflags & CF_COUNT_MASK;
2994 max_insns = CF_COUNT_MASK;
2998 pc_offset = dc->pc - pc_start;
2999 gen_throws_exception = NULL;
3000 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
3001 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
3002 if (bp->pc == dc->pc) {
3003 gen_exception(dc, dc->pc, EXCP_DEBUG);
3004 dc->is_jmp = DISAS_JUMP;
3012 j = gen_opc_ptr - gen_opc_buf;
3016 gen_opc_instr_start[lj++] = 0;
3018 gen_opc_pc[lj] = dc->pc;
3019 gen_opc_instr_start[lj] = 1;
3020 gen_opc_icount[lj] = num_insns;
3022 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
3024 dc->insn_pc = dc->pc;
3025 disas_m68k_insn(env, dc);
3027 } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
3028 !env->singlestep_enabled &&
3030 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3031 num_insns < max_insns);
3033 if (tb->cflags & CF_LAST_IO)
3035 if (unlikely(env->singlestep_enabled)) {
3036 /* Make sure the pc is updated, and raise a debug exception. */
3038 gen_flush_cc_op(dc);
3039 tcg_gen_movi_i32(QREG_PC, dc->pc);
3041 gen_helper_raise_exception(tcg_const_i32(EXCP_DEBUG));
3043 switch(dc->is_jmp) {
3045 gen_flush_cc_op(dc);
3046 gen_jmp_tb(dc, 0, dc->pc);
3051 gen_flush_cc_op(dc);
3052 /* indicate that the hash table must be used to find the next TB */
3056 /* nothing more to generate */
3060 gen_icount_end(tb, num_insns);
3061 *gen_opc_ptr = INDEX_op_end;
3064 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
3065 qemu_log("----------------\n");
3066 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3067 log_target_disas(pc_start, dc->pc - pc_start, 0);
3072 j = gen_opc_ptr - gen_opc_buf;
3075 gen_opc_instr_start[lj++] = 0;
3077 tb->size = dc->pc - pc_start;
3078 tb->icount = num_insns;
3082 //expand_target_qops();
3085 void gen_intermediate_code(CPUState *env, TranslationBlock *tb)
3087 gen_intermediate_code_internal(env, tb, 0);
3090 void gen_intermediate_code_pc(CPUState *env, TranslationBlock *tb)
3092 gen_intermediate_code_internal(env, tb, 1);
3095 void cpu_dump_state(CPUState *env, FILE *f,
3096 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
3102 for (i = 0; i < 8; i++)
3104 u.d = env->fregs[i];
3105 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3106 i, env->dregs[i], i, env->aregs[i],
3107 i, u.l.upper, u.l.lower, *(double *)&u.d);
3109 cpu_fprintf (f, "PC = %08x ", env->pc);
3111 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3112 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3113 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3114 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3117 void gen_pc_load(CPUState *env, TranslationBlock *tb,
3118 unsigned long searched_pc, int pc_pos, void *puc)
3120 env->pc = gen_opc_pc[pc_pos];