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, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 //#define DEBUG_DISPATCH 1
40 /* Fake floating point. */
41 #define TCG_TYPE_F32 TCG_TYPE_I32
42 #define TCG_TYPE_F64 TCG_TYPE_I64
43 #define tcg_gen_mov_f64 tcg_gen_mov_i64
44 #define tcg_gen_qemu_ldf32 tcg_gen_qemu_ld32u
45 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
46 #define tcg_gen_qemu_stf32 tcg_gen_qemu_st32
47 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
48 #define gen_helper_pack_32_f32 tcg_gen_mov_i32
49 #define gen_helper_pack_f32_32 tcg_gen_mov_i32
51 #define DEFO32(name, offset) static TCGv QREG_##name;
52 #define DEFO64(name, offset) static TCGv QREG_##name;
53 #define DEFF64(name, offset) static TCGv QREG_##name;
61 static char cpu_reg_names[3*8*3 + 5*4];
62 static TCGv cpu_dregs[8];
63 static TCGv cpu_aregs[8];
64 static TCGv cpu_fregs[8];
65 static TCGv cpu_macc[4];
67 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
68 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
69 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
70 #define MACREG(acc) cpu_macc[acc]
71 #define QREG_SP cpu_aregs[7]
73 static TCGv NULL_QREG;
74 #define IS_NULL_QREG(t) (GET_TCGV(t) == GET_TCGV(NULL_QREG))
75 /* Used to distinguish stores from bad addressing modes. */
76 static TCGv store_dummy;
78 #include "gen-icount.h"
80 void m68k_tcg_init(void)
85 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, offsetof(CPUState, offset), #name);
86 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0, offsetof(CPUState, offset), #name);
87 #define DEFF64(name, offset) DEFO64(name, offset)
93 cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
96 for (i = 0; i < 8; i++) {
98 cpu_dregs[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
99 offsetof(CPUM68KState, dregs[i]), p);
101 sprintf(p, "A%d", i);
102 cpu_aregs[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
103 offsetof(CPUM68KState, aregs[i]), p);
105 sprintf(p, "F%d", i);
106 cpu_fregs[i] = tcg_global_mem_new(TCG_TYPE_F64, TCG_AREG0,
107 offsetof(CPUM68KState, fregs[i]), p);
110 for (i = 0; i < 4; i++) {
111 sprintf(p, "ACC%d", i);
112 cpu_macc[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
113 offsetof(CPUM68KState, macc[i]), p);
117 NULL_QREG = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, -4, "NULL");
118 store_dummy = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0, -8, "NULL");
120 #define DEF_HELPER(name, ret, args) \
121 tcg_register_helper(HELPER(name), #name);
125 static inline void qemu_assert(int cond, const char *msg)
128 fprintf (stderr, "badness: %s\n", msg);
133 /* internal defines */
134 typedef struct DisasContext {
136 target_ulong insn_pc; /* Start of the current instruction. */
142 struct TranslationBlock *tb;
143 int singlestep_enabled;
148 #define DISAS_JUMP_NEXT 4
150 #if defined(CONFIG_USER_ONLY)
153 #define IS_USER(s) s->user
156 /* XXX: move that elsewhere */
157 /* ??? Fix exceptions. */
158 static void *gen_throws_exception;
159 #define gen_last_qop NULL
167 typedef void (*disas_proc)(DisasContext *, uint16_t);
169 #ifdef DEBUG_DISPATCH
170 #define DISAS_INSN(name) \
171 static void real_disas_##name (DisasContext *s, uint16_t insn); \
172 static void disas_##name (DisasContext *s, uint16_t insn) { \
173 if (logfile) fprintf(logfile, "Dispatch " #name "\n"); \
174 real_disas_##name(s, insn); } \
175 static void real_disas_##name (DisasContext *s, uint16_t insn)
177 #define DISAS_INSN(name) \
178 static void disas_##name (DisasContext *s, uint16_t insn)
181 /* FIXME: Remove this. */
182 #define gen_im32(val) tcg_const_i32(val)
184 #define QMODE_I32 TCG_TYPE_I32
185 #define QMODE_I64 TCG_TYPE_I64
186 #define QMODE_F32 TCG_TYPE_F32
187 #define QMODE_F64 TCG_TYPE_F64
188 static inline TCGv gen_new_qreg(int mode)
190 return tcg_temp_new(mode);
193 /* Generate a load from the specified address. Narrow values are
194 sign extended to full register width. */
195 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
198 int index = IS_USER(s);
202 tmp = gen_new_qreg(QMODE_I32);
204 tcg_gen_qemu_ld8s(tmp, addr, index);
206 tcg_gen_qemu_ld8u(tmp, addr, index);
209 tmp = gen_new_qreg(QMODE_I32);
211 tcg_gen_qemu_ld16s(tmp, addr, index);
213 tcg_gen_qemu_ld16u(tmp, addr, index);
216 tmp = gen_new_qreg(QMODE_I32);
217 tcg_gen_qemu_ld32u(tmp, addr, index);
220 tmp = gen_new_qreg(QMODE_F32);
221 tcg_gen_qemu_ldf32(tmp, addr, index);
224 tmp = gen_new_qreg(QMODE_F64);
225 tcg_gen_qemu_ldf64(tmp, addr, index);
228 qemu_assert(0, "bad load size");
230 gen_throws_exception = gen_last_qop;
234 /* Generate a store. */
235 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
237 int index = IS_USER(s);
241 tcg_gen_qemu_st8(val, addr, index);
244 tcg_gen_qemu_st16(val, addr, index);
247 tcg_gen_qemu_st32(val, addr, index);
250 tcg_gen_qemu_stf32(val, addr, index);
253 tcg_gen_qemu_stf64(val, addr, index);
256 qemu_assert(0, "bad store size");
258 gen_throws_exception = gen_last_qop;
267 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
268 otherwise generate a store. */
269 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
272 if (what == EA_STORE) {
273 gen_store(s, opsize, addr, val);
276 return gen_load(s, opsize, addr, what == EA_LOADS);
280 /* Read a 32-bit immediate constant. */
281 static inline uint32_t read_im32(DisasContext *s)
284 im = ((uint32_t)lduw_code(s->pc)) << 16;
286 im |= lduw_code(s->pc);
291 /* Calculate and address index. */
292 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
297 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
298 if ((ext & 0x800) == 0) {
299 tcg_gen_ext16s_i32(tmp, add);
302 scale = (ext >> 9) & 3;
304 tcg_gen_shli_i32(tmp, add, scale);
310 /* Handle a base + index + displacement effective addresss.
311 A NULL_QREG base means pc-relative. */
312 static TCGv gen_lea_indexed(DisasContext *s, int opsize, TCGv base)
321 ext = lduw_code(s->pc);
324 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
328 /* full extension word format */
329 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
332 if ((ext & 0x30) > 0x10) {
333 /* base displacement */
334 if ((ext & 0x30) == 0x20) {
335 bd = (int16_t)lduw_code(s->pc);
343 tmp = gen_new_qreg(QMODE_I32);
344 if ((ext & 0x44) == 0) {
346 add = gen_addr_index(ext, tmp);
350 if ((ext & 0x80) == 0) {
351 /* base not suppressed */
352 if (IS_NULL_QREG(base)) {
353 base = gen_im32(offset + bd);
356 if (!IS_NULL_QREG(add)) {
357 tcg_gen_add_i32(tmp, add, base);
363 if (!IS_NULL_QREG(add)) {
365 tcg_gen_addi_i32(tmp, add, bd);
371 if ((ext & 3) != 0) {
372 /* memory indirect */
373 base = gen_load(s, OS_LONG, add, 0);
374 if ((ext & 0x44) == 4) {
375 add = gen_addr_index(ext, tmp);
376 tcg_gen_add_i32(tmp, add, base);
382 /* outer displacement */
383 if ((ext & 3) == 2) {
384 od = (int16_t)lduw_code(s->pc);
393 tcg_gen_addi_i32(tmp, add, od);
398 /* brief extension word format */
399 tmp = gen_new_qreg(QMODE_I32);
400 add = gen_addr_index(ext, tmp);
401 if (!IS_NULL_QREG(base)) {
402 tcg_gen_add_i32(tmp, add, base);
404 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
406 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
413 /* Update the CPU env CC_OP state. */
414 static inline void gen_flush_cc_op(DisasContext *s)
416 if (s->cc_op != CC_OP_DYNAMIC)
417 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
420 /* Evaluate all the CC flags. */
421 static inline void gen_flush_flags(DisasContext *s)
423 if (s->cc_op == CC_OP_FLAGS)
426 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
427 s->cc_op = CC_OP_FLAGS;
430 static void gen_logic_cc(DisasContext *s, TCGv val)
432 tcg_gen_mov_i32(QREG_CC_DEST, val);
433 s->cc_op = CC_OP_LOGIC;
436 static void gen_update_cc_add(TCGv dest, TCGv src)
438 tcg_gen_mov_i32(QREG_CC_DEST, dest);
439 tcg_gen_mov_i32(QREG_CC_SRC, src);
442 static inline int opsize_bytes(int opsize)
445 case OS_BYTE: return 1;
446 case OS_WORD: return 2;
447 case OS_LONG: return 4;
448 case OS_SINGLE: return 4;
449 case OS_DOUBLE: return 8;
451 qemu_assert(0, "bad operand size");
455 /* Assign value to a register. If the width is less than the register width
456 only the low part of the register is set. */
457 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
462 tcg_gen_andi_i32(reg, reg, 0xffffff00);
463 tmp = gen_new_qreg(QMODE_I32);
464 tcg_gen_ext8u_i32(tmp, val);
465 tcg_gen_or_i32(reg, reg, tmp);
468 tcg_gen_andi_i32(reg, reg, 0xffff0000);
469 tmp = gen_new_qreg(QMODE_I32);
470 tcg_gen_ext16u_i32(tmp, val);
471 tcg_gen_or_i32(reg, reg, tmp);
474 tcg_gen_mov_i32(reg, val);
477 gen_helper_pack_32_f32(reg, val);
480 qemu_assert(0, "Bad operand size");
485 /* Sign or zero extend a value. */
486 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
492 tmp = gen_new_qreg(QMODE_I32);
494 tcg_gen_ext8s_i32(tmp, val);
496 tcg_gen_ext8u_i32(tmp, val);
499 tmp = gen_new_qreg(QMODE_I32);
501 tcg_gen_ext16s_i32(tmp, val);
503 tcg_gen_ext16u_i32(tmp, val);
509 tmp = gen_new_qreg(QMODE_F32);
510 gen_helper_pack_f32_32(tmp, val);
513 qemu_assert(0, "Bad operand size");
518 /* Generate code for an "effective address". Does not adjust the base
519 register for autoincrememnt addressing modes. */
520 static TCGv gen_lea(DisasContext *s, uint16_t insn, int opsize)
527 switch ((insn >> 3) & 7) {
528 case 0: /* Data register direct. */
529 case 1: /* Address register direct. */
531 case 2: /* Indirect register */
532 case 3: /* Indirect postincrement. */
533 return AREG(insn, 0);
534 case 4: /* Indirect predecrememnt. */
536 tmp = gen_new_qreg(QMODE_I32);
537 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
539 case 5: /* Indirect displacement. */
541 tmp = gen_new_qreg(QMODE_I32);
542 ext = lduw_code(s->pc);
544 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
546 case 6: /* Indirect index + displacement. */
548 return gen_lea_indexed(s, opsize, reg);
551 case 0: /* Absolute short. */
552 offset = ldsw_code(s->pc);
554 return gen_im32(offset);
555 case 1: /* Absolute long. */
556 offset = read_im32(s);
557 return gen_im32(offset);
558 case 2: /* pc displacement */
559 tmp = gen_new_qreg(QMODE_I32);
561 offset += ldsw_code(s->pc);
563 return gen_im32(offset);
564 case 3: /* pc index+displacement. */
565 return gen_lea_indexed(s, opsize, NULL_QREG);
566 case 4: /* Immediate. */
571 /* Should never happen. */
575 /* Helper function for gen_ea. Reuse the computed address between the
576 for read/write operands. */
577 static inline TCGv gen_ea_once(DisasContext *s, uint16_t insn, int opsize,
578 TCGv val, TCGv *addrp, ea_what what)
582 if (addrp && what == EA_STORE) {
585 tmp = gen_lea(s, insn, opsize);
586 if (IS_NULL_QREG(tmp))
591 return gen_ldst(s, opsize, tmp, val, what);
594 /* Generate code to load/store a value ito/from an EA. If VAL > 0 this is
595 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
596 ADDRP is non-null for readwrite operands. */
597 static TCGv gen_ea(DisasContext *s, uint16_t insn, int opsize, TCGv val,
598 TCGv *addrp, ea_what what)
604 switch ((insn >> 3) & 7) {
605 case 0: /* Data register direct. */
607 if (what == EA_STORE) {
608 gen_partset_reg(opsize, reg, val);
611 return gen_extend(reg, opsize, what == EA_LOADS);
613 case 1: /* Address register direct. */
615 if (what == EA_STORE) {
616 tcg_gen_mov_i32(reg, val);
619 return gen_extend(reg, opsize, what == EA_LOADS);
621 case 2: /* Indirect register */
623 return gen_ldst(s, opsize, reg, val, what);
624 case 3: /* Indirect postincrement. */
626 result = gen_ldst(s, opsize, reg, val, what);
627 /* ??? This is not exception safe. The instruction may still
628 fault after this point. */
629 if (what == EA_STORE || !addrp)
630 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
632 case 4: /* Indirect predecrememnt. */
635 if (addrp && what == EA_STORE) {
638 tmp = gen_lea(s, insn, opsize);
639 if (IS_NULL_QREG(tmp))
644 result = gen_ldst(s, opsize, tmp, val, what);
645 /* ??? This is not exception safe. The instruction may still
646 fault after this point. */
647 if (what == EA_STORE || !addrp) {
649 tcg_gen_mov_i32(reg, tmp);
653 case 5: /* Indirect displacement. */
654 case 6: /* Indirect index + displacement. */
655 return gen_ea_once(s, insn, opsize, val, addrp, what);
658 case 0: /* Absolute short. */
659 case 1: /* Absolute long. */
660 case 2: /* pc displacement */
661 case 3: /* pc index+displacement. */
662 return gen_ea_once(s, insn, opsize, val, addrp, what);
663 case 4: /* Immediate. */
664 /* Sign extend values for consistency. */
667 if (what == EA_LOADS)
668 offset = ldsb_code(s->pc + 1);
670 offset = ldub_code(s->pc + 1);
674 if (what == EA_LOADS)
675 offset = ldsw_code(s->pc);
677 offset = lduw_code(s->pc);
681 offset = read_im32(s);
684 qemu_assert(0, "Bad immediate operand");
686 return tcg_const_i32(offset);
691 /* Should never happen. */
695 /* This generates a conditional branch, clobbering all temporaries. */
696 static void gen_jmpcc(DisasContext *s, int cond, int l1)
700 /* TODO: Optimize compare/branch pairs rather than always flushing
701 flag state to CC_OP_FLAGS. */
709 case 2: /* HI (!C && !Z) */
710 tmp = gen_new_qreg(QMODE_I32);
711 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
712 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
714 case 3: /* LS (C || Z) */
715 tmp = gen_new_qreg(QMODE_I32);
716 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
717 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
719 case 4: /* CC (!C) */
720 tmp = gen_new_qreg(QMODE_I32);
721 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
722 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
725 tmp = gen_new_qreg(QMODE_I32);
726 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
727 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
729 case 6: /* NE (!Z) */
730 tmp = gen_new_qreg(QMODE_I32);
731 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
732 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
735 tmp = gen_new_qreg(QMODE_I32);
736 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
737 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
739 case 8: /* VC (!V) */
740 tmp = gen_new_qreg(QMODE_I32);
741 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
742 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
745 tmp = gen_new_qreg(QMODE_I32);
746 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
747 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
749 case 10: /* PL (!N) */
750 tmp = gen_new_qreg(QMODE_I32);
751 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
752 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
754 case 11: /* MI (N) */
755 tmp = gen_new_qreg(QMODE_I32);
756 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
757 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
759 case 12: /* GE (!(N ^ V)) */
760 tmp = gen_new_qreg(QMODE_I32);
761 assert(CCF_V == (CCF_N >> 2));
762 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
763 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
764 tcg_gen_andi_i32(tmp, tmp, CCF_V);
765 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
767 case 13: /* LT (N ^ V) */
768 tmp = gen_new_qreg(QMODE_I32);
769 assert(CCF_V == (CCF_N >> 2));
770 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
771 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
772 tcg_gen_andi_i32(tmp, tmp, CCF_V);
773 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
775 case 14: /* GT (!(Z || (N ^ V))) */
776 tmp = gen_new_qreg(QMODE_I32);
777 assert(CCF_V == (CCF_N >> 2));
778 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
779 tcg_gen_shri_i32(tmp, tmp, 2);
780 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
781 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
782 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
784 case 15: /* LE (Z || (N ^ V)) */
785 tmp = gen_new_qreg(QMODE_I32);
786 assert(CCF_V == (CCF_N >> 2));
787 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
788 tcg_gen_shri_i32(tmp, tmp, 2);
789 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
790 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
791 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
794 /* Should ever happen. */
805 l1 = gen_new_label();
806 cond = (insn >> 8) & 0xf;
808 tcg_gen_andi_i32(reg, reg, 0xffffff00);
809 /* This is safe because we modify the reg directly, with no other values
811 gen_jmpcc(s, cond ^ 1, l1);
812 tcg_gen_ori_i32(reg, reg, 0xff);
816 /* Force a TB lookup after an instruction that changes the CPU state. */
817 static void gen_lookup_tb(DisasContext *s)
820 tcg_gen_movi_i32(QREG_PC, s->pc);
821 s->is_jmp = DISAS_UPDATE;
824 /* Generate a jump to an immediate address. */
825 static void gen_jmp_im(DisasContext *s, uint32_t dest)
828 tcg_gen_movi_i32(QREG_PC, dest);
829 s->is_jmp = DISAS_JUMP;
832 /* Generate a jump to the address in qreg DEST. */
833 static void gen_jmp(DisasContext *s, TCGv dest)
836 tcg_gen_mov_i32(QREG_PC, dest);
837 s->is_jmp = DISAS_JUMP;
840 static void gen_exception(DisasContext *s, uint32_t where, int nr)
843 gen_jmp_im(s, where);
844 gen_helper_raise_exception(tcg_const_i32(nr));
847 static inline void gen_addr_fault(DisasContext *s)
849 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
852 #define SRC_EA(result, opsize, op_sign, addrp) do { \
853 result = gen_ea(s, insn, opsize, NULL_QREG, addrp, op_sign ? EA_LOADS : EA_LOADU); \
854 if (IS_NULL_QREG(result)) { \
860 #define DEST_EA(insn, opsize, val, addrp) do { \
861 TCGv ea_result = gen_ea(s, insn, opsize, val, addrp, EA_STORE); \
862 if (IS_NULL_QREG(ea_result)) { \
868 /* Generate a jump to an immediate address. */
869 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
871 TranslationBlock *tb;
874 if (unlikely(s->singlestep_enabled)) {
875 gen_exception(s, dest, EXCP_DEBUG);
876 } else if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
877 (s->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
879 tcg_gen_movi_i32(QREG_PC, dest);
880 tcg_gen_exit_tb((long)tb + n);
885 s->is_jmp = DISAS_TB_JUMP;
888 DISAS_INSN(undef_mac)
890 gen_exception(s, s->pc - 2, EXCP_LINEA);
893 DISAS_INSN(undef_fpu)
895 gen_exception(s, s->pc - 2, EXCP_LINEF);
900 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
901 cpu_abort(cpu_single_env, "Illegal instruction: %04x @ %08x",
912 sign = (insn & 0x100) != 0;
914 tmp = gen_new_qreg(QMODE_I32);
916 tcg_gen_ext16s_i32(tmp, reg);
918 tcg_gen_ext16u_i32(tmp, reg);
919 SRC_EA(src, OS_WORD, sign, NULL);
920 tcg_gen_mul_i32(tmp, tmp, src);
921 tcg_gen_mov_i32(reg, tmp);
922 /* Unlike m68k, coldfire always clears the overflow bit. */
923 gen_logic_cc(s, tmp);
933 sign = (insn & 0x100) != 0;
936 tcg_gen_ext16s_i32(QREG_DIV1, reg);
938 tcg_gen_ext16u_i32(QREG_DIV1, reg);
940 SRC_EA(src, OS_WORD, sign, NULL);
941 tcg_gen_mov_i32(QREG_DIV2, src);
943 gen_helper_divs(cpu_env, tcg_const_i32(1));
945 gen_helper_divu(cpu_env, tcg_const_i32(1));
948 tmp = gen_new_qreg(QMODE_I32);
949 src = gen_new_qreg(QMODE_I32);
950 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
951 tcg_gen_shli_i32(src, QREG_DIV2, 16);
952 tcg_gen_or_i32(reg, tmp, src);
953 s->cc_op = CC_OP_FLAGS;
963 ext = lduw_code(s->pc);
966 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
971 tcg_gen_mov_i32(QREG_DIV1, num);
972 SRC_EA(den, OS_LONG, 0, NULL);
973 tcg_gen_mov_i32(QREG_DIV2, den);
975 gen_helper_divs(cpu_env, tcg_const_i32(0));
977 gen_helper_divu(cpu_env, tcg_const_i32(0));
979 if ((ext & 7) == ((ext >> 12) & 7)) {
981 tcg_gen_mov_i32 (reg, QREG_DIV1);
984 tcg_gen_mov_i32 (reg, QREG_DIV2);
986 s->cc_op = CC_OP_FLAGS;
998 add = (insn & 0x4000) != 0;
1000 dest = gen_new_qreg(QMODE_I32);
1002 SRC_EA(tmp, OS_LONG, 0, &addr);
1006 SRC_EA(src, OS_LONG, 0, NULL);
1009 tcg_gen_add_i32(dest, tmp, src);
1010 gen_helper_xflag_lt(QREG_CC_X, dest, src);
1011 s->cc_op = CC_OP_ADD;
1013 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
1014 tcg_gen_sub_i32(dest, tmp, src);
1015 s->cc_op = CC_OP_SUB;
1017 gen_update_cc_add(dest, src);
1019 DEST_EA(insn, OS_LONG, dest, &addr);
1021 tcg_gen_mov_i32(reg, dest);
1026 /* Reverse the order of the bits in REG. */
1030 reg = DREG(insn, 0);
1031 gen_helper_bitrev(reg, reg);
1034 DISAS_INSN(bitop_reg)
1044 if ((insn & 0x38) != 0)
1048 op = (insn >> 6) & 3;
1049 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1050 src2 = DREG(insn, 9);
1051 dest = gen_new_qreg(QMODE_I32);
1054 tmp = gen_new_qreg(QMODE_I32);
1055 if (opsize == OS_BYTE)
1056 tcg_gen_andi_i32(tmp, src2, 7);
1058 tcg_gen_andi_i32(tmp, src2, 31);
1060 tmp = gen_new_qreg(QMODE_I32);
1061 tcg_gen_shr_i32(tmp, src1, src2);
1062 tcg_gen_andi_i32(tmp, tmp, 1);
1063 tcg_gen_shli_i32(tmp, tmp, 2);
1064 /* Clear CCF_Z if bit set. */
1065 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1066 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1068 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
1071 tcg_gen_xor_i32(dest, src1, tmp);
1074 tcg_gen_not_i32(tmp, tmp);
1075 tcg_gen_and_i32(dest, src1, tmp);
1078 tcg_gen_or_i32(dest, src1, tmp);
1084 DEST_EA(insn, opsize, dest, &addr);
1090 reg = DREG(insn, 0);
1092 gen_helper_sats(reg, reg, QREG_CC_DEST);
1093 gen_logic_cc(s, reg);
1096 static void gen_push(DisasContext *s, TCGv val)
1100 tmp = gen_new_qreg(QMODE_I32);
1101 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1102 gen_store(s, OS_LONG, tmp, val);
1103 tcg_gen_mov_i32(QREG_SP, tmp);
1115 mask = lduw_code(s->pc);
1117 tmp = gen_lea(s, insn, OS_LONG);
1118 if (IS_NULL_QREG(tmp)) {
1122 addr = gen_new_qreg(QMODE_I32);
1123 tcg_gen_mov_i32(addr, tmp);
1124 is_load = ((insn & 0x0400) != 0);
1125 for (i = 0; i < 16; i++, mask >>= 1) {
1132 tmp = gen_load(s, OS_LONG, addr, 0);
1133 tcg_gen_mov_i32(reg, tmp);
1135 gen_store(s, OS_LONG, addr, reg);
1138 tcg_gen_addi_i32(addr, addr, 4);
1143 DISAS_INSN(bitop_im)
1153 if ((insn & 0x38) != 0)
1157 op = (insn >> 6) & 3;
1159 bitnum = lduw_code(s->pc);
1161 if (bitnum & 0xff00) {
1162 disas_undef(s, insn);
1166 SRC_EA(src1, opsize, 0, op ? &addr: NULL);
1169 if (opsize == OS_BYTE)
1175 tmp = gen_new_qreg(QMODE_I32);
1176 assert (CCF_Z == (1 << 2));
1178 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1179 else if (bitnum < 2)
1180 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1182 tcg_gen_mov_i32(tmp, src1);
1183 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1184 /* Clear CCF_Z if bit set. */
1185 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1186 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1190 tcg_gen_xori_i32(tmp, src1, mask);
1193 tcg_gen_andi_i32(tmp, src1, ~mask);
1196 tcg_gen_ori_i32(tmp, src1, mask);
1201 DEST_EA(insn, opsize, tmp, &addr);
1205 DISAS_INSN(arith_im)
1213 op = (insn >> 9) & 7;
1214 SRC_EA(src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1216 dest = gen_new_qreg(QMODE_I32);
1219 tcg_gen_ori_i32(dest, src1, im);
1220 gen_logic_cc(s, dest);
1223 tcg_gen_andi_i32(dest, src1, im);
1224 gen_logic_cc(s, dest);
1227 tcg_gen_mov_i32(dest, src1);
1228 gen_helper_xflag_lt(QREG_CC_X, dest, gen_im32(im));
1229 tcg_gen_subi_i32(dest, dest, im);
1230 gen_update_cc_add(dest, gen_im32(im));
1231 s->cc_op = CC_OP_SUB;
1234 tcg_gen_mov_i32(dest, src1);
1235 tcg_gen_addi_i32(dest, dest, im);
1236 gen_update_cc_add(dest, gen_im32(im));
1237 gen_helper_xflag_lt(QREG_CC_X, dest, gen_im32(im));
1238 s->cc_op = CC_OP_ADD;
1241 tcg_gen_xori_i32(dest, src1, im);
1242 gen_logic_cc(s, dest);
1245 tcg_gen_mov_i32(dest, src1);
1246 tcg_gen_subi_i32(dest, dest, im);
1247 gen_update_cc_add(dest, gen_im32(im));
1248 s->cc_op = CC_OP_SUB;
1254 DEST_EA(insn, OS_LONG, dest, &addr);
1262 reg = DREG(insn, 0);
1263 tcg_gen_bswap_i32(reg, reg);
1273 switch (insn >> 12) {
1274 case 1: /* move.b */
1277 case 2: /* move.l */
1280 case 3: /* move.w */
1286 SRC_EA(src, opsize, 1, NULL);
1287 op = (insn >> 6) & 7;
1290 /* The value will already have been sign extended. */
1291 dest = AREG(insn, 9);
1292 tcg_gen_mov_i32(dest, src);
1296 dest_ea = ((insn >> 9) & 7) | (op << 3);
1297 DEST_EA(dest_ea, opsize, src, NULL);
1298 /* This will be correct because loads sign extend. */
1299 gen_logic_cc(s, src);
1308 reg = DREG(insn, 0);
1309 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1317 reg = AREG(insn, 9);
1318 tmp = gen_lea(s, insn, OS_LONG);
1319 if (IS_NULL_QREG(tmp)) {
1323 tcg_gen_mov_i32(reg, tmp);
1330 switch ((insn >> 6) & 3) {
1343 DEST_EA(insn, opsize, gen_im32(0), NULL);
1344 gen_logic_cc(s, gen_im32(0));
1347 static TCGv gen_get_ccr(DisasContext *s)
1352 dest = gen_new_qreg(QMODE_I32);
1353 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1354 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
1358 DISAS_INSN(move_from_ccr)
1363 ccr = gen_get_ccr(s);
1364 reg = DREG(insn, 0);
1365 gen_partset_reg(OS_WORD, reg, ccr);
1373 reg = DREG(insn, 0);
1374 src1 = gen_new_qreg(QMODE_I32);
1375 tcg_gen_mov_i32(src1, reg);
1376 tcg_gen_neg_i32(reg, src1);
1377 s->cc_op = CC_OP_SUB;
1378 gen_update_cc_add(reg, src1);
1379 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
1380 s->cc_op = CC_OP_SUB;
1383 static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1385 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1386 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
1388 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1392 static void gen_set_sr(DisasContext *s, uint16_t insn, int ccr_only)
1397 s->cc_op = CC_OP_FLAGS;
1398 if ((insn & 0x38) == 0)
1400 tmp = gen_new_qreg(QMODE_I32);
1401 reg = DREG(insn, 0);
1402 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1403 tcg_gen_shri_i32(tmp, reg, 4);
1404 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1406 gen_helper_set_sr(cpu_env, reg);
1409 else if ((insn & 0x3f) == 0x3c)
1412 val = lduw_code(s->pc);
1414 gen_set_sr_im(s, val, ccr_only);
1417 disas_undef(s, insn);
1420 DISAS_INSN(move_to_ccr)
1422 gen_set_sr(s, insn, 1);
1429 reg = DREG(insn, 0);
1430 tcg_gen_not_i32(reg, reg);
1431 gen_logic_cc(s, reg);
1440 src1 = gen_new_qreg(QMODE_I32);
1441 src2 = gen_new_qreg(QMODE_I32);
1442 reg = DREG(insn, 0);
1443 tcg_gen_shli_i32(src1, reg, 16);
1444 tcg_gen_shri_i32(src2, reg, 16);
1445 tcg_gen_or_i32(reg, src1, src2);
1446 gen_logic_cc(s, reg);
1453 tmp = gen_lea(s, insn, OS_LONG);
1454 if (IS_NULL_QREG(tmp)) {
1467 reg = DREG(insn, 0);
1468 op = (insn >> 6) & 7;
1469 tmp = gen_new_qreg(QMODE_I32);
1471 tcg_gen_ext16s_i32(tmp, reg);
1473 tcg_gen_ext8s_i32(tmp, reg);
1475 gen_partset_reg(OS_WORD, reg, tmp);
1477 tcg_gen_mov_i32(reg, tmp);
1478 gen_logic_cc(s, tmp);
1486 switch ((insn >> 6) & 3) {
1499 SRC_EA(tmp, opsize, 1, NULL);
1500 gen_logic_cc(s, tmp);
1505 /* Implemented as a NOP. */
1510 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1513 /* ??? This should be atomic. */
1520 dest = gen_new_qreg(QMODE_I32);
1521 SRC_EA(src1, OS_BYTE, 1, &addr);
1522 gen_logic_cc(s, src1);
1523 tcg_gen_ori_i32(dest, src1, 0x80);
1524 DEST_EA(insn, OS_BYTE, dest, &addr);
1534 /* The upper 32 bits of the product are discarded, so
1535 muls.l and mulu.l are functionally equivalent. */
1536 ext = lduw_code(s->pc);
1539 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1542 reg = DREG(ext, 12);
1543 SRC_EA(src1, OS_LONG, 0, NULL);
1544 dest = gen_new_qreg(QMODE_I32);
1545 tcg_gen_mul_i32(dest, src1, reg);
1546 tcg_gen_mov_i32(reg, dest);
1547 /* Unlike m68k, coldfire always clears the overflow bit. */
1548 gen_logic_cc(s, dest);
1557 offset = ldsw_code(s->pc);
1559 reg = AREG(insn, 0);
1560 tmp = gen_new_qreg(QMODE_I32);
1561 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1562 gen_store(s, OS_LONG, tmp, reg);
1563 if ((insn & 7) != 7)
1564 tcg_gen_mov_i32(reg, tmp);
1565 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1574 src = gen_new_qreg(QMODE_I32);
1575 reg = AREG(insn, 0);
1576 tcg_gen_mov_i32(src, reg);
1577 tmp = gen_load(s, OS_LONG, src, 0);
1578 tcg_gen_mov_i32(reg, tmp);
1579 tcg_gen_addi_i32(QREG_SP, src, 4);
1590 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1591 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1599 /* Load the target address first to ensure correct exception
1601 tmp = gen_lea(s, insn, OS_LONG);
1602 if (IS_NULL_QREG(tmp)) {
1606 if ((insn & 0x40) == 0) {
1608 gen_push(s, gen_im32(s->pc));
1621 SRC_EA(src1, OS_LONG, 0, &addr);
1622 val = (insn >> 9) & 7;
1625 dest = gen_new_qreg(QMODE_I32);
1626 tcg_gen_mov_i32(dest, src1);
1627 if ((insn & 0x38) == 0x08) {
1628 /* Don't update condition codes if the destination is an
1629 address register. */
1630 if (insn & 0x0100) {
1631 tcg_gen_subi_i32(dest, dest, val);
1633 tcg_gen_addi_i32(dest, dest, val);
1636 src2 = gen_im32(val);
1637 if (insn & 0x0100) {
1638 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1639 tcg_gen_subi_i32(dest, dest, val);
1640 s->cc_op = CC_OP_SUB;
1642 tcg_gen_addi_i32(dest, dest, val);
1643 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1644 s->cc_op = CC_OP_ADD;
1646 gen_update_cc_add(dest, src2);
1648 DEST_EA(insn, OS_LONG, dest, &addr);
1654 case 2: /* One extension word. */
1657 case 3: /* Two extension words. */
1660 case 4: /* No extension words. */
1663 disas_undef(s, insn);
1675 op = (insn >> 8) & 0xf;
1676 offset = (int8_t)insn;
1678 offset = ldsw_code(s->pc);
1680 } else if (offset == -1) {
1681 offset = read_im32(s);
1685 gen_push(s, gen_im32(s->pc));
1690 l1 = gen_new_label();
1691 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1692 gen_jmp_tb(s, 1, base + offset);
1694 gen_jmp_tb(s, 0, s->pc);
1696 /* Unconditional branch. */
1697 gen_jmp_tb(s, 0, base + offset);
1706 tcg_gen_movi_i32(DREG(insn, 9), val);
1707 gen_logic_cc(s, tcg_const_i32(val));
1720 SRC_EA(src, opsize, (insn & 0x80) == 0, NULL);
1721 reg = DREG(insn, 9);
1722 tcg_gen_mov_i32(reg, src);
1723 gen_logic_cc(s, src);
1733 reg = DREG(insn, 9);
1734 dest = gen_new_qreg(QMODE_I32);
1736 SRC_EA(src, OS_LONG, 0, &addr);
1737 tcg_gen_or_i32(dest, src, reg);
1738 DEST_EA(insn, OS_LONG, dest, &addr);
1740 SRC_EA(src, OS_LONG, 0, NULL);
1741 tcg_gen_or_i32(dest, src, reg);
1742 tcg_gen_mov_i32(reg, dest);
1744 gen_logic_cc(s, dest);
1752 SRC_EA(src, OS_LONG, 0, NULL);
1753 reg = AREG(insn, 9);
1754 tcg_gen_sub_i32(reg, reg, src);
1763 reg = DREG(insn, 9);
1764 src = DREG(insn, 0);
1765 gen_helper_subx_cc(reg, cpu_env, reg, src);
1773 val = (insn >> 9) & 7;
1776 src = gen_im32(val);
1777 gen_logic_cc(s, src);
1778 DEST_EA(insn, OS_LONG, src, NULL);
1789 op = (insn >> 6) & 3;
1793 s->cc_op = CC_OP_CMPB;
1797 s->cc_op = CC_OP_CMPW;
1801 s->cc_op = CC_OP_SUB;
1806 SRC_EA(src, opsize, 1, NULL);
1807 reg = DREG(insn, 9);
1808 dest = gen_new_qreg(QMODE_I32);
1809 tcg_gen_sub_i32(dest, reg, src);
1810 gen_update_cc_add(dest, src);
1825 SRC_EA(src, opsize, 1, NULL);
1826 reg = AREG(insn, 9);
1827 dest = gen_new_qreg(QMODE_I32);
1828 tcg_gen_sub_i32(dest, reg, src);
1829 gen_update_cc_add(dest, src);
1830 s->cc_op = CC_OP_SUB;
1840 SRC_EA(src, OS_LONG, 0, &addr);
1841 reg = DREG(insn, 9);
1842 dest = gen_new_qreg(QMODE_I32);
1843 tcg_gen_xor_i32(dest, src, reg);
1844 gen_logic_cc(s, dest);
1845 DEST_EA(insn, OS_LONG, dest, &addr);
1855 reg = DREG(insn, 9);
1856 dest = gen_new_qreg(QMODE_I32);
1858 SRC_EA(src, OS_LONG, 0, &addr);
1859 tcg_gen_and_i32(dest, src, reg);
1860 DEST_EA(insn, OS_LONG, dest, &addr);
1862 SRC_EA(src, OS_LONG, 0, NULL);
1863 tcg_gen_and_i32(dest, src, reg);
1864 tcg_gen_mov_i32(reg, dest);
1866 gen_logic_cc(s, dest);
1874 SRC_EA(src, OS_LONG, 0, NULL);
1875 reg = AREG(insn, 9);
1876 tcg_gen_add_i32(reg, reg, src);
1885 reg = DREG(insn, 9);
1886 src = DREG(insn, 0);
1887 gen_helper_addx_cc(reg, cpu_env, reg, src);
1888 s->cc_op = CC_OP_FLAGS;
1891 /* TODO: This could be implemented without helper functions. */
1892 DISAS_INSN(shift_im)
1898 reg = DREG(insn, 0);
1899 tmp = (insn >> 9) & 7;
1902 shift = gen_im32(tmp);
1903 /* No need to flush flags becuse we know we will set C flag. */
1905 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1908 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1910 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1913 s->cc_op = CC_OP_SHIFT;
1916 DISAS_INSN(shift_reg)
1921 reg = DREG(insn, 0);
1922 shift = DREG(insn, 9);
1923 /* Shift by zero leaves C flag unmodified. */
1926 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1929 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1931 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1934 s->cc_op = CC_OP_SHIFT;
1940 reg = DREG(insn, 0);
1941 gen_logic_cc(s, reg);
1942 gen_helper_ff1(reg, reg);
1945 static TCGv gen_get_sr(DisasContext *s)
1950 ccr = gen_get_ccr(s);
1951 sr = gen_new_qreg(QMODE_I32);
1952 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1953 tcg_gen_or_i32(sr, sr, ccr);
1963 ext = lduw_code(s->pc);
1965 if (ext != 0x46FC) {
1966 gen_exception(s, addr, EXCP_UNSUPPORTED);
1969 ext = lduw_code(s->pc);
1971 if (IS_USER(s) || (ext & SR_S) == 0) {
1972 gen_exception(s, addr, EXCP_PRIVILEGE);
1975 gen_push(s, gen_get_sr(s));
1976 gen_set_sr_im(s, ext, 0);
1979 DISAS_INSN(move_from_sr)
1985 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1989 reg = DREG(insn, 0);
1990 gen_partset_reg(OS_WORD, reg, sr);
1993 DISAS_INSN(move_to_sr)
1996 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1999 gen_set_sr(s, insn, 0);
2003 DISAS_INSN(move_from_usp)
2006 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2009 /* TODO: Implement USP. */
2010 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2013 DISAS_INSN(move_to_usp)
2016 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2019 /* TODO: Implement USP. */
2020 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2025 gen_exception(s, s->pc, EXCP_HALT_INSN);
2033 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2037 ext = lduw_code(s->pc);
2040 gen_set_sr_im(s, ext, 0);
2041 tcg_gen_movi_i32(QREG_HALTED, 1);
2042 gen_exception(s, s->pc, EXCP_HLT);
2048 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2051 gen_exception(s, s->pc - 2, EXCP_RTE);
2060 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2064 ext = lduw_code(s->pc);
2068 reg = AREG(ext, 12);
2070 reg = DREG(ext, 12);
2072 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2079 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2082 /* ICache fetch. Implement as no-op. */
2088 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2091 /* Cache push/invalidate. Implement as no-op. */
2096 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2102 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2105 /* TODO: Implement wdebug. */
2106 qemu_assert(0, "WDEBUG not implemented");
2111 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2114 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2115 immediately before the next FP instruction is executed. */
2126 ext = lduw_code(s->pc);
2128 opmode = ext & 0x7f;
2129 switch ((ext >> 13) & 7) {
2134 case 3: /* fmove out */
2137 /* ??? TODO: Proper behavior on overflow. */
2138 switch ((ext >> 10) & 7) {
2141 res = gen_new_qreg(QMODE_I32);
2142 gen_helper_f64_to_i32(res, cpu_env, src);
2146 res = gen_new_qreg(QMODE_F32);
2147 gen_helper_f64_to_f32(res, cpu_env, src);
2151 res = gen_new_qreg(QMODE_I32);
2152 gen_helper_f64_to_i32(res, cpu_env, src);
2160 res = gen_new_qreg(QMODE_I32);
2161 gen_helper_f64_to_i32(res, cpu_env, src);
2166 DEST_EA(insn, opsize, res, NULL);
2168 case 4: /* fmove to control register. */
2169 switch ((ext >> 10) & 7) {
2171 /* Not implemented. Ignore writes. */
2176 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2180 case 5: /* fmove from control register. */
2181 switch ((ext >> 10) & 7) {
2183 /* Not implemented. Always return zero. */
2189 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2193 DEST_EA(insn, OS_LONG, res, NULL);
2195 case 6: /* fmovem */
2201 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2203 src = gen_lea(s, insn, OS_LONG);
2204 if (IS_NULL_QREG(src)) {
2208 addr = gen_new_qreg(QMODE_I32);
2209 tcg_gen_mov_i32(addr, src);
2211 for (i = 0; i < 8; i++) {
2215 if (ext & (1 << 13)) {
2217 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2220 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2222 if (ext & (mask - 1))
2223 tcg_gen_addi_i32(addr, addr, 8);
2230 if (ext & (1 << 14)) {
2233 /* Source effective address. */
2234 switch ((ext >> 10) & 7) {
2235 case 0: opsize = OS_LONG; break;
2236 case 1: opsize = OS_SINGLE; break;
2237 case 4: opsize = OS_WORD; break;
2238 case 5: opsize = OS_DOUBLE; break;
2239 case 6: opsize = OS_BYTE; break;
2243 SRC_EA(tmp, opsize, 1, NULL);
2244 if (opsize == OS_DOUBLE) {
2247 src = gen_new_qreg(QMODE_F64);
2252 gen_helper_i32_to_f64(src, cpu_env, tmp);
2255 gen_helper_f32_to_f64(src, cpu_env, tmp);
2260 /* Source register. */
2261 src = FREG(ext, 10);
2263 dest = FREG(ext, 7);
2264 res = gen_new_qreg(QMODE_F64);
2266 tcg_gen_mov_f64(res, dest);
2269 case 0: case 0x40: case 0x44: /* fmove */
2270 tcg_gen_mov_f64(res, src);
2273 gen_helper_iround_f64(res, cpu_env, src);
2276 case 3: /* fintrz */
2277 gen_helper_itrunc_f64(res, cpu_env, src);
2280 case 4: case 0x41: case 0x45: /* fsqrt */
2281 gen_helper_sqrt_f64(res, cpu_env, src);
2283 case 0x18: case 0x58: case 0x5c: /* fabs */
2284 gen_helper_abs_f64(res, src);
2286 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2287 gen_helper_chs_f64(res, src);
2289 case 0x20: case 0x60: case 0x64: /* fdiv */
2290 gen_helper_div_f64(res, cpu_env, res, src);
2292 case 0x22: case 0x62: case 0x66: /* fadd */
2293 gen_helper_add_f64(res, cpu_env, res, src);
2295 case 0x23: case 0x63: case 0x67: /* fmul */
2296 gen_helper_mul_f64(res, cpu_env, res, src);
2298 case 0x28: case 0x68: case 0x6c: /* fsub */
2299 gen_helper_sub_f64(res, cpu_env, res, src);
2301 case 0x38: /* fcmp */
2302 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2306 case 0x3a: /* ftst */
2307 tcg_gen_mov_f64(res, src);
2315 if (opmode & 0x40) {
2316 if ((opmode & 0x4) != 0)
2318 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2325 tmp = gen_new_qreg(QMODE_F32);
2326 gen_helper_f64_to_f32(tmp, cpu_env, res);
2327 gen_helper_f32_to_f64(res, cpu_env, tmp);
2329 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2330 if (!IS_NULL_QREG(dest)) {
2331 tcg_gen_mov_f64(dest, res);
2336 disas_undef_fpu(s, insn);
2347 offset = ldsw_code(s->pc);
2349 if (insn & (1 << 6)) {
2350 offset = (offset << 16) | lduw_code(s->pc);
2354 l1 = gen_new_label();
2355 /* TODO: Raise BSUN exception. */
2356 flag = gen_new_qreg(QMODE_I32);
2357 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2358 /* Jump to l1 if condition is true. */
2359 switch (insn & 0xf) {
2362 case 1: /* eq (=0) */
2363 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2365 case 2: /* ogt (=1) */
2366 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2368 case 3: /* oge (=0 or =1) */
2369 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2371 case 4: /* olt (=-1) */
2372 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2374 case 5: /* ole (=-1 or =0) */
2375 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2377 case 6: /* ogl (=-1 or =1) */
2378 tcg_gen_andi_i32(flag, flag, 1);
2379 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2381 case 7: /* or (=2) */
2382 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2384 case 8: /* un (<2) */
2385 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2387 case 9: /* ueq (=0 or =2) */
2388 tcg_gen_andi_i32(flag, flag, 1);
2389 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2391 case 10: /* ugt (>0) */
2392 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2394 case 11: /* uge (>=0) */
2395 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2397 case 12: /* ult (=-1 or =2) */
2398 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2400 case 13: /* ule (!=1) */
2401 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2403 case 14: /* ne (!=0) */
2404 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2410 gen_jmp_tb(s, 0, s->pc);
2412 gen_jmp_tb(s, 1, addr + offset);
2415 DISAS_INSN(frestore)
2417 /* TODO: Implement frestore. */
2418 qemu_assert(0, "FRESTORE not implemented");
2423 /* TODO: Implement fsave. */
2424 qemu_assert(0, "FSAVE not implemented");
2427 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2429 TCGv tmp = gen_new_qreg(QMODE_I32);
2430 if (s->env->macsr & MACSR_FI) {
2432 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2434 tcg_gen_shli_i32(tmp, val, 16);
2435 } else if (s->env->macsr & MACSR_SU) {
2437 tcg_gen_sari_i32(tmp, val, 16);
2439 tcg_gen_ext16s_i32(tmp, val);
2442 tcg_gen_shri_i32(tmp, val, 16);
2444 tcg_gen_ext16u_i32(tmp, val);
2449 static void gen_mac_clear_flags(void)
2451 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2452 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2467 if (IS_NULL_QREG(s->mactmp))
2468 s->mactmp = tcg_temp_new(TCG_TYPE_I64);
2470 ext = lduw_code(s->pc);
2473 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2474 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2475 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2476 disas_undef(s, insn);
2480 /* MAC with load. */
2481 tmp = gen_lea(s, insn, OS_LONG);
2482 addr = gen_new_qreg(QMODE_I32);
2483 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2484 /* Load the value now to ensure correct exception behavior.
2485 Perform writeback after reading the MAC inputs. */
2486 loadval = gen_load(s, OS_LONG, addr, 0);
2489 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2490 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2492 loadval = addr = NULL_QREG;
2493 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2494 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2497 gen_mac_clear_flags();
2500 /* Disabled because conditional branches clobber temporary vars. */
2501 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2502 /* Skip the multiply if we know we will ignore it. */
2503 l1 = gen_new_label();
2504 tmp = gen_new_qreg(QMODE_I32);
2505 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2506 gen_op_jmp_nz32(tmp, l1);
2510 if ((ext & 0x0800) == 0) {
2512 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2513 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2515 if (s->env->macsr & MACSR_FI) {
2516 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2518 if (s->env->macsr & MACSR_SU)
2519 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2521 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2522 switch ((ext >> 9) & 3) {
2524 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2527 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2533 /* Save the overflow flag from the multiply. */
2534 saved_flags = gen_new_qreg(QMODE_I32);
2535 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2537 saved_flags = NULL_QREG;
2541 /* Disabled because conditional branches clobber temporary vars. */
2542 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2543 /* Skip the accumulate if the value is already saturated. */
2544 l1 = gen_new_label();
2545 tmp = gen_new_qreg(QMODE_I32);
2546 gen_op_and32(tmp, QREG_MACSR, gen_im32(MACSR_PAV0 << acc));
2547 gen_op_jmp_nz32(tmp, l1);
2552 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2554 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2556 if (s->env->macsr & MACSR_FI)
2557 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2558 else if (s->env->macsr & MACSR_SU)
2559 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2561 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2564 /* Disabled because conditional branches clobber temporary vars. */
2570 /* Dual accumulate variant. */
2571 acc = (ext >> 2) & 3;
2572 /* Restore the overflow flag from the multiplier. */
2573 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2575 /* Disabled because conditional branches clobber temporary vars. */
2576 if ((s->env->macsr & MACSR_OMC) != 0) {
2577 /* Skip the accumulate if the value is already saturated. */
2578 l1 = gen_new_label();
2579 tmp = gen_new_qreg(QMODE_I32);
2580 gen_op_and32(tmp, QREG_MACSR, gen_im32(MACSR_PAV0 << acc));
2581 gen_op_jmp_nz32(tmp, l1);
2585 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2587 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2588 if (s->env->macsr & MACSR_FI)
2589 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2590 else if (s->env->macsr & MACSR_SU)
2591 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2593 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2595 /* Disabled because conditional branches clobber temporary vars. */
2600 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2604 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2605 tcg_gen_mov_i32(rw, loadval);
2606 /* FIXME: Should address writeback happen with the masked or
2608 switch ((insn >> 3) & 7) {
2609 case 3: /* Post-increment. */
2610 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2612 case 4: /* Pre-decrement. */
2613 tcg_gen_mov_i32(AREG(insn, 0), addr);
2618 DISAS_INSN(from_mac)
2624 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2625 accnum = (insn >> 9) & 3;
2626 acc = MACREG(accnum);
2627 if (s->env->macsr & MACSR_FI) {
2628 gen_helper_get_macf(cpu_env, rx, acc);
2629 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2630 tcg_gen_trunc_i64_i32(rx, acc);
2631 } else if (s->env->macsr & MACSR_SU) {
2632 gen_helper_get_macs(rx, acc);
2634 gen_helper_get_macu(rx, acc);
2637 tcg_gen_movi_i64(acc, 0);
2638 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2642 DISAS_INSN(move_mac)
2644 /* FIXME: This can be done without a helper. */
2648 dest = tcg_const_i32((insn >> 9) & 3);
2649 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2650 gen_mac_clear_flags();
2651 gen_helper_mac_set_flags(cpu_env, dest);
2654 DISAS_INSN(from_macsr)
2658 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2659 tcg_gen_mov_i32(reg, QREG_MACSR);
2662 DISAS_INSN(from_mask)
2665 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2666 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2669 DISAS_INSN(from_mext)
2673 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2674 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2675 if (s->env->macsr & MACSR_FI)
2676 gen_helper_get_mac_extf(reg, cpu_env, acc);
2678 gen_helper_get_mac_exti(reg, cpu_env, acc);
2681 DISAS_INSN(macsr_to_ccr)
2683 tcg_gen_movi_i32(QREG_CC_X, 0);
2684 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2685 s->cc_op = CC_OP_FLAGS;
2693 accnum = (insn >> 9) & 3;
2694 acc = MACREG(accnum);
2695 SRC_EA(val, OS_LONG, 0, NULL);
2696 if (s->env->macsr & MACSR_FI) {
2697 tcg_gen_ext_i32_i64(acc, val);
2698 tcg_gen_shli_i64(acc, acc, 8);
2699 } else if (s->env->macsr & MACSR_SU) {
2700 tcg_gen_ext_i32_i64(acc, val);
2702 tcg_gen_extu_i32_i64(acc, val);
2704 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2705 gen_mac_clear_flags();
2706 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2709 DISAS_INSN(to_macsr)
2712 SRC_EA(val, OS_LONG, 0, NULL);
2713 gen_helper_set_macsr(cpu_env, val);
2720 SRC_EA(val, OS_LONG, 0, NULL);
2721 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2728 SRC_EA(val, OS_LONG, 0, NULL);
2729 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2730 if (s->env->macsr & MACSR_FI)
2731 gen_helper_set_mac_extf(cpu_env, val, acc);
2732 else if (s->env->macsr & MACSR_SU)
2733 gen_helper_set_mac_exts(cpu_env, val, acc);
2735 gen_helper_set_mac_extu(cpu_env, val, acc);
2738 static disas_proc opcode_table[65536];
2741 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2747 /* Sanity check. All set bits must be included in the mask. */
2748 if (opcode & ~mask) {
2750 "qemu internal error: bogus opcode definition %04x/%04x\n",
2754 /* This could probably be cleverer. For now just optimize the case where
2755 the top bits are known. */
2756 /* Find the first zero bit in the mask. */
2758 while ((i & mask) != 0)
2760 /* Iterate over all combinations of this and lower bits. */
2765 from = opcode & ~(i - 1);
2767 for (i = from; i < to; i++) {
2768 if ((i & mask) == opcode)
2769 opcode_table[i] = proc;
2773 /* Register m68k opcode handlers. Order is important.
2774 Later insn override earlier ones. */
2775 void register_m68k_insns (CPUM68KState *env)
2777 #define INSN(name, opcode, mask, feature) do { \
2778 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2779 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2781 INSN(undef, 0000, 0000, CF_ISA_A);
2782 INSN(arith_im, 0080, fff8, CF_ISA_A);
2783 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2784 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2785 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2786 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2787 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2788 INSN(arith_im, 0280, fff8, CF_ISA_A);
2789 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2790 INSN(arith_im, 0480, fff8, CF_ISA_A);
2791 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2792 INSN(arith_im, 0680, fff8, CF_ISA_A);
2793 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2794 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2795 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2796 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2797 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2798 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2799 INSN(move, 1000, f000, CF_ISA_A);
2800 INSN(move, 2000, f000, CF_ISA_A);
2801 INSN(move, 3000, f000, CF_ISA_A);
2802 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2803 INSN(negx, 4080, fff8, CF_ISA_A);
2804 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2805 INSN(lea, 41c0, f1c0, CF_ISA_A);
2806 INSN(clr, 4200, ff00, CF_ISA_A);
2807 INSN(undef, 42c0, ffc0, CF_ISA_A);
2808 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2809 INSN(neg, 4480, fff8, CF_ISA_A);
2810 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2811 INSN(not, 4680, fff8, CF_ISA_A);
2812 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2813 INSN(pea, 4840, ffc0, CF_ISA_A);
2814 INSN(swap, 4840, fff8, CF_ISA_A);
2815 INSN(movem, 48c0, fbc0, CF_ISA_A);
2816 INSN(ext, 4880, fff8, CF_ISA_A);
2817 INSN(ext, 48c0, fff8, CF_ISA_A);
2818 INSN(ext, 49c0, fff8, CF_ISA_A);
2819 INSN(tst, 4a00, ff00, CF_ISA_A);
2820 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2821 INSN(halt, 4ac8, ffff, CF_ISA_A);
2822 INSN(pulse, 4acc, ffff, CF_ISA_A);
2823 INSN(illegal, 4afc, ffff, CF_ISA_A);
2824 INSN(mull, 4c00, ffc0, CF_ISA_A);
2825 INSN(divl, 4c40, ffc0, CF_ISA_A);
2826 INSN(sats, 4c80, fff8, CF_ISA_B);
2827 INSN(trap, 4e40, fff0, CF_ISA_A);
2828 INSN(link, 4e50, fff8, CF_ISA_A);
2829 INSN(unlk, 4e58, fff8, CF_ISA_A);
2830 INSN(move_to_usp, 4e60, fff8, USP);
2831 INSN(move_from_usp, 4e68, fff8, USP);
2832 INSN(nop, 4e71, ffff, CF_ISA_A);
2833 INSN(stop, 4e72, ffff, CF_ISA_A);
2834 INSN(rte, 4e73, ffff, CF_ISA_A);
2835 INSN(rts, 4e75, ffff, CF_ISA_A);
2836 INSN(movec, 4e7b, ffff, CF_ISA_A);
2837 INSN(jump, 4e80, ffc0, CF_ISA_A);
2838 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2839 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2840 INSN(scc, 50c0, f0f8, CF_ISA_A);
2841 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2842 INSN(tpf, 51f8, fff8, CF_ISA_A);
2844 /* Branch instructions. */
2845 INSN(branch, 6000, f000, CF_ISA_A);
2846 /* Disable long branch instructions, then add back the ones we want. */
2847 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2848 INSN(branch, 60ff, f0ff, CF_ISA_B);
2849 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2850 INSN(branch, 60ff, ffff, BRAL);
2852 INSN(moveq, 7000, f100, CF_ISA_A);
2853 INSN(mvzs, 7100, f100, CF_ISA_B);
2854 INSN(or, 8000, f000, CF_ISA_A);
2855 INSN(divw, 80c0, f0c0, CF_ISA_A);
2856 INSN(addsub, 9000, f000, CF_ISA_A);
2857 INSN(subx, 9180, f1f8, CF_ISA_A);
2858 INSN(suba, 91c0, f1c0, CF_ISA_A);
2860 INSN(undef_mac, a000, f000, CF_ISA_A);
2861 INSN(mac, a000, f100, CF_EMAC);
2862 INSN(from_mac, a180, f9b0, CF_EMAC);
2863 INSN(move_mac, a110, f9fc, CF_EMAC);
2864 INSN(from_macsr,a980, f9f0, CF_EMAC);
2865 INSN(from_mask, ad80, fff0, CF_EMAC);
2866 INSN(from_mext, ab80, fbf0, CF_EMAC);
2867 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2868 INSN(to_mac, a100, f9c0, CF_EMAC);
2869 INSN(to_macsr, a900, ffc0, CF_EMAC);
2870 INSN(to_mext, ab00, fbc0, CF_EMAC);
2871 INSN(to_mask, ad00, ffc0, CF_EMAC);
2873 INSN(mov3q, a140, f1c0, CF_ISA_B);
2874 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2875 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2876 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2877 INSN(cmp, b080, f1c0, CF_ISA_A);
2878 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2879 INSN(eor, b180, f1c0, CF_ISA_A);
2880 INSN(and, c000, f000, CF_ISA_A);
2881 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2882 INSN(addsub, d000, f000, CF_ISA_A);
2883 INSN(addx, d180, f1f8, CF_ISA_A);
2884 INSN(adda, d1c0, f1c0, CF_ISA_A);
2885 INSN(shift_im, e080, f0f0, CF_ISA_A);
2886 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2887 INSN(undef_fpu, f000, f000, CF_ISA_A);
2888 INSN(fpu, f200, ffc0, CF_FPU);
2889 INSN(fbcc, f280, ffc0, CF_FPU);
2890 INSN(frestore, f340, ffc0, CF_FPU);
2891 INSN(fsave, f340, ffc0, CF_FPU);
2892 INSN(intouch, f340, ffc0, CF_ISA_A);
2893 INSN(cpushl, f428, ff38, CF_ISA_A);
2894 INSN(wddata, fb00, ff00, CF_ISA_A);
2895 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2899 /* ??? Some of this implementation is not exception safe. We should always
2900 write back the result to memory before setting the condition codes. */
2901 static void disas_m68k_insn(CPUState * env, DisasContext *s)
2905 insn = lduw_code(s->pc);
2908 opcode_table[insn](s, insn);
2911 /* generate intermediate code for basic block 'tb'. */
2913 gen_intermediate_code_internal(CPUState *env, TranslationBlock *tb,
2916 DisasContext dc1, *dc = &dc1;
2917 uint16_t *gen_opc_end;
2919 target_ulong pc_start;
2925 /* generate intermediate code */
2930 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
2933 dc->is_jmp = DISAS_NEXT;
2935 dc->cc_op = CC_OP_DYNAMIC;
2936 dc->singlestep_enabled = env->singlestep_enabled;
2937 dc->fpcr = env->fpcr;
2938 dc->user = (env->sr & SR_S) == 0;
2940 dc->mactmp = NULL_QREG;
2943 max_insns = tb->cflags & CF_COUNT_MASK;
2945 max_insns = CF_COUNT_MASK;
2949 pc_offset = dc->pc - pc_start;
2950 gen_throws_exception = NULL;
2951 if (env->nb_breakpoints > 0) {
2952 for(j = 0; j < env->nb_breakpoints; j++) {
2953 if (env->breakpoints[j] == dc->pc) {
2954 gen_exception(dc, dc->pc, EXCP_DEBUG);
2955 dc->is_jmp = DISAS_JUMP;
2963 j = gen_opc_ptr - gen_opc_buf;
2967 gen_opc_instr_start[lj++] = 0;
2969 gen_opc_pc[lj] = dc->pc;
2970 gen_opc_instr_start[lj] = 1;
2971 gen_opc_icount[lj] = num_insns;
2973 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
2975 last_cc_op = dc->cc_op;
2976 dc->insn_pc = dc->pc;
2977 disas_m68k_insn(env, dc);
2980 /* Terminate the TB on memory ops if watchpoints are present. */
2981 /* FIXME: This should be replaced by the deterministic execution
2982 * IRQ raising bits. */
2983 if (dc->is_mem && env->nb_watchpoints)
2985 } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
2986 !env->singlestep_enabled &&
2987 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
2988 num_insns < max_insns);
2990 if (tb->cflags & CF_LAST_IO)
2992 if (unlikely(env->singlestep_enabled)) {
2993 /* Make sure the pc is updated, and raise a debug exception. */
2995 gen_flush_cc_op(dc);
2996 tcg_gen_movi_i32(QREG_PC, dc->pc);
2998 gen_helper_raise_exception(tcg_const_i32(EXCP_DEBUG));
3000 switch(dc->is_jmp) {
3002 gen_flush_cc_op(dc);
3003 gen_jmp_tb(dc, 0, dc->pc);
3008 gen_flush_cc_op(dc);
3009 /* indicate that the hash table must be used to find the next TB */
3013 /* nothing more to generate */
3017 gen_icount_end(tb, num_insns);
3018 *gen_opc_ptr = INDEX_op_end;
3021 if (loglevel & CPU_LOG_TB_IN_ASM) {
3022 fprintf(logfile, "----------------\n");
3023 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
3024 target_disas(logfile, pc_start, dc->pc - pc_start, 0);
3025 fprintf(logfile, "\n");
3029 j = gen_opc_ptr - gen_opc_buf;
3032 gen_opc_instr_start[lj++] = 0;
3034 tb->size = dc->pc - pc_start;
3035 tb->icount = num_insns;
3039 //expand_target_qops();
3042 void gen_intermediate_code(CPUState *env, TranslationBlock *tb)
3044 gen_intermediate_code_internal(env, tb, 0);
3047 void gen_intermediate_code_pc(CPUState *env, TranslationBlock *tb)
3049 gen_intermediate_code_internal(env, tb, 1);
3052 void cpu_dump_state(CPUState *env, FILE *f,
3053 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
3059 for (i = 0; i < 8; i++)
3061 u.d = env->fregs[i];
3062 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3063 i, env->dregs[i], i, env->aregs[i],
3064 i, u.l.upper, u.l.lower, *(double *)&u.d);
3066 cpu_fprintf (f, "PC = %08x ", env->pc);
3068 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3069 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3070 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3071 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3074 void gen_pc_load(CPUState *env, TranslationBlock *tb,
3075 unsigned long searched_pc, int pc_pos, void *puc)
3077 env->pc = gen_opc_pc[pc_pos];