console: add kbd_put_qcode_console
[qemu-kvm.git] / target-m68k / translate.c
blobfa248d96b53ffe3d9368726a2c143b3776a1b1b9
1 /*
2 * m68k translation
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/>.
21 #include "cpu.h"
22 #include "disas/disas.h"
23 #include "tcg-op.h"
24 #include "qemu/log.h"
26 #include "exec/helper-proto.h"
27 #include "exec/helper-gen.h"
29 //#define DEBUG_DISPATCH 1
31 /* Fake floating point. */
32 #define tcg_gen_mov_f64 tcg_gen_mov_i64
33 #define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
34 #define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
36 #define DEFO32(name, offset) static TCGv QREG_##name;
37 #define DEFO64(name, offset) static TCGv_i64 QREG_##name;
38 #define DEFF64(name, offset) static TCGv_i64 QREG_##name;
39 #include "qregs.def"
40 #undef DEFO32
41 #undef DEFO64
42 #undef DEFF64
44 static TCGv_i32 cpu_halted;
45 static TCGv_i32 cpu_exception_index;
47 static TCGv_ptr cpu_env;
49 static char cpu_reg_names[3*8*3 + 5*4];
50 static TCGv cpu_dregs[8];
51 static TCGv cpu_aregs[8];
52 static TCGv_i64 cpu_fregs[8];
53 static TCGv_i64 cpu_macc[4];
55 #define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
56 #define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
57 #define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
58 #define MACREG(acc) cpu_macc[acc]
59 #define QREG_SP cpu_aregs[7]
61 static TCGv NULL_QREG;
62 #define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
63 /* Used to distinguish stores from bad addressing modes. */
64 static TCGv store_dummy;
66 #include "exec/gen-icount.h"
68 void m68k_tcg_init(void)
70 char *p;
71 int i;
73 #define DEFO32(name, offset) QREG_##name = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
74 #define DEFO64(name, offset) QREG_##name = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUM68KState, offset), #name);
75 #define DEFF64(name, offset) DEFO64(name, offset)
76 #include "qregs.def"
77 #undef DEFO32
78 #undef DEFO64
79 #undef DEFF64
81 cpu_halted = tcg_global_mem_new_i32(TCG_AREG0,
82 -offsetof(M68kCPU, env) +
83 offsetof(CPUState, halted), "HALTED");
84 cpu_exception_index = tcg_global_mem_new_i32(TCG_AREG0,
85 -offsetof(M68kCPU, env) +
86 offsetof(CPUState, exception_index),
87 "EXCEPTION");
89 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
91 p = cpu_reg_names;
92 for (i = 0; i < 8; i++) {
93 sprintf(p, "D%d", i);
94 cpu_dregs[i] = tcg_global_mem_new(TCG_AREG0,
95 offsetof(CPUM68KState, dregs[i]), p);
96 p += 3;
97 sprintf(p, "A%d", i);
98 cpu_aregs[i] = tcg_global_mem_new(TCG_AREG0,
99 offsetof(CPUM68KState, aregs[i]), p);
100 p += 3;
101 sprintf(p, "F%d", i);
102 cpu_fregs[i] = tcg_global_mem_new_i64(TCG_AREG0,
103 offsetof(CPUM68KState, fregs[i]), p);
104 p += 3;
106 for (i = 0; i < 4; i++) {
107 sprintf(p, "ACC%d", i);
108 cpu_macc[i] = tcg_global_mem_new_i64(TCG_AREG0,
109 offsetof(CPUM68KState, macc[i]), p);
110 p += 5;
113 NULL_QREG = tcg_global_mem_new(TCG_AREG0, -4, "NULL");
114 store_dummy = tcg_global_mem_new(TCG_AREG0, -8, "NULL");
117 /* internal defines */
118 typedef struct DisasContext {
119 CPUM68KState *env;
120 target_ulong insn_pc; /* Start of the current instruction. */
121 target_ulong pc;
122 int is_jmp;
123 int cc_op;
124 int user;
125 uint32_t fpcr;
126 struct TranslationBlock *tb;
127 int singlestep_enabled;
128 int is_mem;
129 TCGv_i64 mactmp;
130 int done_mac;
131 } DisasContext;
133 #define DISAS_JUMP_NEXT 4
135 #if defined(CONFIG_USER_ONLY)
136 #define IS_USER(s) 1
137 #else
138 #define IS_USER(s) s->user
139 #endif
141 /* XXX: move that elsewhere */
142 /* ??? Fix exceptions. */
143 static void *gen_throws_exception;
144 #define gen_last_qop NULL
146 #define OS_BYTE 0
147 #define OS_WORD 1
148 #define OS_LONG 2
149 #define OS_SINGLE 4
150 #define OS_DOUBLE 5
152 typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
154 #ifdef DEBUG_DISPATCH
155 #define DISAS_INSN(name) \
156 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
157 uint16_t insn); \
158 static void disas_##name(CPUM68KState *env, DisasContext *s, \
159 uint16_t insn) \
161 qemu_log("Dispatch " #name "\n"); \
162 real_disas_##name(s, env, insn); \
164 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
165 uint16_t insn)
166 #else
167 #define DISAS_INSN(name) \
168 static void disas_##name(CPUM68KState *env, DisasContext *s, \
169 uint16_t insn)
170 #endif
172 /* Generate a load from the specified address. Narrow values are
173 sign extended to full register width. */
174 static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
176 TCGv tmp;
177 int index = IS_USER(s);
178 s->is_mem = 1;
179 tmp = tcg_temp_new_i32();
180 switch(opsize) {
181 case OS_BYTE:
182 if (sign)
183 tcg_gen_qemu_ld8s(tmp, addr, index);
184 else
185 tcg_gen_qemu_ld8u(tmp, addr, index);
186 break;
187 case OS_WORD:
188 if (sign)
189 tcg_gen_qemu_ld16s(tmp, addr, index);
190 else
191 tcg_gen_qemu_ld16u(tmp, addr, index);
192 break;
193 case OS_LONG:
194 case OS_SINGLE:
195 tcg_gen_qemu_ld32u(tmp, addr, index);
196 break;
197 default:
198 g_assert_not_reached();
200 gen_throws_exception = gen_last_qop;
201 return tmp;
204 static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
206 TCGv_i64 tmp;
207 int index = IS_USER(s);
208 s->is_mem = 1;
209 tmp = tcg_temp_new_i64();
210 tcg_gen_qemu_ldf64(tmp, addr, index);
211 gen_throws_exception = gen_last_qop;
212 return tmp;
215 /* Generate a store. */
216 static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
218 int index = IS_USER(s);
219 s->is_mem = 1;
220 switch(opsize) {
221 case OS_BYTE:
222 tcg_gen_qemu_st8(val, addr, index);
223 break;
224 case OS_WORD:
225 tcg_gen_qemu_st16(val, addr, index);
226 break;
227 case OS_LONG:
228 case OS_SINGLE:
229 tcg_gen_qemu_st32(val, addr, index);
230 break;
231 default:
232 g_assert_not_reached();
234 gen_throws_exception = gen_last_qop;
237 static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
239 int index = IS_USER(s);
240 s->is_mem = 1;
241 tcg_gen_qemu_stf64(val, addr, index);
242 gen_throws_exception = gen_last_qop;
245 typedef enum {
246 EA_STORE,
247 EA_LOADU,
248 EA_LOADS
249 } ea_what;
251 /* Generate an unsigned load if VAL is 0 a signed load if val is -1,
252 otherwise generate a store. */
253 static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
254 ea_what what)
256 if (what == EA_STORE) {
257 gen_store(s, opsize, addr, val);
258 return store_dummy;
259 } else {
260 return gen_load(s, opsize, addr, what == EA_LOADS);
264 /* Read a 32-bit immediate constant. */
265 static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
267 uint32_t im;
268 im = ((uint32_t)cpu_lduw_code(env, s->pc)) << 16;
269 s->pc += 2;
270 im |= cpu_lduw_code(env, s->pc);
271 s->pc += 2;
272 return im;
275 /* Calculate and address index. */
276 static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
278 TCGv add;
279 int scale;
281 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
282 if ((ext & 0x800) == 0) {
283 tcg_gen_ext16s_i32(tmp, add);
284 add = tmp;
286 scale = (ext >> 9) & 3;
287 if (scale != 0) {
288 tcg_gen_shli_i32(tmp, add, scale);
289 add = tmp;
291 return add;
294 /* Handle a base + index + displacement effective addresss.
295 A NULL_QREG base means pc-relative. */
296 static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, int opsize,
297 TCGv base)
299 uint32_t offset;
300 uint16_t ext;
301 TCGv add;
302 TCGv tmp;
303 uint32_t bd, od;
305 offset = s->pc;
306 ext = cpu_lduw_code(env, s->pc);
307 s->pc += 2;
309 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
310 return NULL_QREG;
312 if (ext & 0x100) {
313 /* full extension word format */
314 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
315 return NULL_QREG;
317 if ((ext & 0x30) > 0x10) {
318 /* base displacement */
319 if ((ext & 0x30) == 0x20) {
320 bd = (int16_t)cpu_lduw_code(env, s->pc);
321 s->pc += 2;
322 } else {
323 bd = read_im32(env, s);
325 } else {
326 bd = 0;
328 tmp = tcg_temp_new();
329 if ((ext & 0x44) == 0) {
330 /* pre-index */
331 add = gen_addr_index(ext, tmp);
332 } else {
333 add = NULL_QREG;
335 if ((ext & 0x80) == 0) {
336 /* base not suppressed */
337 if (IS_NULL_QREG(base)) {
338 base = tcg_const_i32(offset + bd);
339 bd = 0;
341 if (!IS_NULL_QREG(add)) {
342 tcg_gen_add_i32(tmp, add, base);
343 add = tmp;
344 } else {
345 add = base;
348 if (!IS_NULL_QREG(add)) {
349 if (bd != 0) {
350 tcg_gen_addi_i32(tmp, add, bd);
351 add = tmp;
353 } else {
354 add = tcg_const_i32(bd);
356 if ((ext & 3) != 0) {
357 /* memory indirect */
358 base = gen_load(s, OS_LONG, add, 0);
359 if ((ext & 0x44) == 4) {
360 add = gen_addr_index(ext, tmp);
361 tcg_gen_add_i32(tmp, add, base);
362 add = tmp;
363 } else {
364 add = base;
366 if ((ext & 3) > 1) {
367 /* outer displacement */
368 if ((ext & 3) == 2) {
369 od = (int16_t)cpu_lduw_code(env, s->pc);
370 s->pc += 2;
371 } else {
372 od = read_im32(env, s);
374 } else {
375 od = 0;
377 if (od != 0) {
378 tcg_gen_addi_i32(tmp, add, od);
379 add = tmp;
382 } else {
383 /* brief extension word format */
384 tmp = tcg_temp_new();
385 add = gen_addr_index(ext, tmp);
386 if (!IS_NULL_QREG(base)) {
387 tcg_gen_add_i32(tmp, add, base);
388 if ((int8_t)ext)
389 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
390 } else {
391 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
393 add = tmp;
395 return add;
398 /* Update the CPU env CC_OP state. */
399 static inline void gen_flush_cc_op(DisasContext *s)
401 if (s->cc_op != CC_OP_DYNAMIC)
402 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
405 /* Evaluate all the CC flags. */
406 static inline void gen_flush_flags(DisasContext *s)
408 if (s->cc_op == CC_OP_FLAGS)
409 return;
410 gen_flush_cc_op(s);
411 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
412 s->cc_op = CC_OP_FLAGS;
415 static void gen_logic_cc(DisasContext *s, TCGv val)
417 tcg_gen_mov_i32(QREG_CC_DEST, val);
418 s->cc_op = CC_OP_LOGIC;
421 static void gen_update_cc_add(TCGv dest, TCGv src)
423 tcg_gen_mov_i32(QREG_CC_DEST, dest);
424 tcg_gen_mov_i32(QREG_CC_SRC, src);
427 static inline int opsize_bytes(int opsize)
429 switch (opsize) {
430 case OS_BYTE: return 1;
431 case OS_WORD: return 2;
432 case OS_LONG: return 4;
433 case OS_SINGLE: return 4;
434 case OS_DOUBLE: return 8;
435 default:
436 g_assert_not_reached();
440 /* Assign value to a register. If the width is less than the register width
441 only the low part of the register is set. */
442 static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
444 TCGv tmp;
445 switch (opsize) {
446 case OS_BYTE:
447 tcg_gen_andi_i32(reg, reg, 0xffffff00);
448 tmp = tcg_temp_new();
449 tcg_gen_ext8u_i32(tmp, val);
450 tcg_gen_or_i32(reg, reg, tmp);
451 break;
452 case OS_WORD:
453 tcg_gen_andi_i32(reg, reg, 0xffff0000);
454 tmp = tcg_temp_new();
455 tcg_gen_ext16u_i32(tmp, val);
456 tcg_gen_or_i32(reg, reg, tmp);
457 break;
458 case OS_LONG:
459 case OS_SINGLE:
460 tcg_gen_mov_i32(reg, val);
461 break;
462 default:
463 g_assert_not_reached();
467 /* Sign or zero extend a value. */
468 static inline TCGv gen_extend(TCGv val, int opsize, int sign)
470 TCGv tmp;
472 switch (opsize) {
473 case OS_BYTE:
474 tmp = tcg_temp_new();
475 if (sign)
476 tcg_gen_ext8s_i32(tmp, val);
477 else
478 tcg_gen_ext8u_i32(tmp, val);
479 break;
480 case OS_WORD:
481 tmp = tcg_temp_new();
482 if (sign)
483 tcg_gen_ext16s_i32(tmp, val);
484 else
485 tcg_gen_ext16u_i32(tmp, val);
486 break;
487 case OS_LONG:
488 case OS_SINGLE:
489 tmp = val;
490 break;
491 default:
492 g_assert_not_reached();
494 return tmp;
497 /* Generate code for an "effective address". Does not adjust the base
498 register for autoincrement addressing modes. */
499 static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
500 int opsize)
502 TCGv reg;
503 TCGv tmp;
504 uint16_t ext;
505 uint32_t offset;
507 switch ((insn >> 3) & 7) {
508 case 0: /* Data register direct. */
509 case 1: /* Address register direct. */
510 return NULL_QREG;
511 case 2: /* Indirect register */
512 case 3: /* Indirect postincrement. */
513 return AREG(insn, 0);
514 case 4: /* Indirect predecrememnt. */
515 reg = AREG(insn, 0);
516 tmp = tcg_temp_new();
517 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
518 return tmp;
519 case 5: /* Indirect displacement. */
520 reg = AREG(insn, 0);
521 tmp = tcg_temp_new();
522 ext = cpu_lduw_code(env, s->pc);
523 s->pc += 2;
524 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
525 return tmp;
526 case 6: /* Indirect index + displacement. */
527 reg = AREG(insn, 0);
528 return gen_lea_indexed(env, s, opsize, reg);
529 case 7: /* Other */
530 switch (insn & 7) {
531 case 0: /* Absolute short. */
532 offset = cpu_ldsw_code(env, s->pc);
533 s->pc += 2;
534 return tcg_const_i32(offset);
535 case 1: /* Absolute long. */
536 offset = read_im32(env, s);
537 return tcg_const_i32(offset);
538 case 2: /* pc displacement */
539 offset = s->pc;
540 offset += cpu_ldsw_code(env, s->pc);
541 s->pc += 2;
542 return tcg_const_i32(offset);
543 case 3: /* pc index+displacement. */
544 return gen_lea_indexed(env, s, opsize, NULL_QREG);
545 case 4: /* Immediate. */
546 default:
547 return NULL_QREG;
550 /* Should never happen. */
551 return NULL_QREG;
554 /* Helper function for gen_ea. Reuse the computed address between the
555 for read/write operands. */
556 static inline TCGv gen_ea_once(CPUM68KState *env, DisasContext *s,
557 uint16_t insn, int opsize, TCGv val,
558 TCGv *addrp, ea_what what)
560 TCGv tmp;
562 if (addrp && what == EA_STORE) {
563 tmp = *addrp;
564 } else {
565 tmp = gen_lea(env, s, insn, opsize);
566 if (IS_NULL_QREG(tmp))
567 return tmp;
568 if (addrp)
569 *addrp = tmp;
571 return gen_ldst(s, opsize, tmp, val, what);
574 /* Generate code to load/store a value from/into an EA. If VAL > 0 this is
575 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
576 ADDRP is non-null for readwrite operands. */
577 static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
578 int opsize, TCGv val, TCGv *addrp, ea_what what)
580 TCGv reg;
581 TCGv result;
582 uint32_t offset;
584 switch ((insn >> 3) & 7) {
585 case 0: /* Data register direct. */
586 reg = DREG(insn, 0);
587 if (what == EA_STORE) {
588 gen_partset_reg(opsize, reg, val);
589 return store_dummy;
590 } else {
591 return gen_extend(reg, opsize, what == EA_LOADS);
593 case 1: /* Address register direct. */
594 reg = AREG(insn, 0);
595 if (what == EA_STORE) {
596 tcg_gen_mov_i32(reg, val);
597 return store_dummy;
598 } else {
599 return gen_extend(reg, opsize, what == EA_LOADS);
601 case 2: /* Indirect register */
602 reg = AREG(insn, 0);
603 return gen_ldst(s, opsize, reg, val, what);
604 case 3: /* Indirect postincrement. */
605 reg = AREG(insn, 0);
606 result = gen_ldst(s, opsize, reg, val, what);
607 /* ??? This is not exception safe. The instruction may still
608 fault after this point. */
609 if (what == EA_STORE || !addrp)
610 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
611 return result;
612 case 4: /* Indirect predecrememnt. */
614 TCGv tmp;
615 if (addrp && what == EA_STORE) {
616 tmp = *addrp;
617 } else {
618 tmp = gen_lea(env, s, insn, opsize);
619 if (IS_NULL_QREG(tmp))
620 return tmp;
621 if (addrp)
622 *addrp = tmp;
624 result = gen_ldst(s, opsize, tmp, val, what);
625 /* ??? This is not exception safe. The instruction may still
626 fault after this point. */
627 if (what == EA_STORE || !addrp) {
628 reg = AREG(insn, 0);
629 tcg_gen_mov_i32(reg, tmp);
632 return result;
633 case 5: /* Indirect displacement. */
634 case 6: /* Indirect index + displacement. */
635 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
636 case 7: /* Other */
637 switch (insn & 7) {
638 case 0: /* Absolute short. */
639 case 1: /* Absolute long. */
640 case 2: /* pc displacement */
641 case 3: /* pc index+displacement. */
642 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
643 case 4: /* Immediate. */
644 /* Sign extend values for consistency. */
645 switch (opsize) {
646 case OS_BYTE:
647 if (what == EA_LOADS) {
648 offset = cpu_ldsb_code(env, s->pc + 1);
649 } else {
650 offset = cpu_ldub_code(env, s->pc + 1);
652 s->pc += 2;
653 break;
654 case OS_WORD:
655 if (what == EA_LOADS) {
656 offset = cpu_ldsw_code(env, s->pc);
657 } else {
658 offset = cpu_lduw_code(env, s->pc);
660 s->pc += 2;
661 break;
662 case OS_LONG:
663 offset = read_im32(env, s);
664 break;
665 default:
666 g_assert_not_reached();
668 return tcg_const_i32(offset);
669 default:
670 return NULL_QREG;
673 /* Should never happen. */
674 return NULL_QREG;
677 /* This generates a conditional branch, clobbering all temporaries. */
678 static void gen_jmpcc(DisasContext *s, int cond, int l1)
680 TCGv tmp;
682 /* TODO: Optimize compare/branch pairs rather than always flushing
683 flag state to CC_OP_FLAGS. */
684 gen_flush_flags(s);
685 switch (cond) {
686 case 0: /* T */
687 tcg_gen_br(l1);
688 break;
689 case 1: /* F */
690 break;
691 case 2: /* HI (!C && !Z) */
692 tmp = tcg_temp_new();
693 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
694 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
695 break;
696 case 3: /* LS (C || Z) */
697 tmp = tcg_temp_new();
698 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
699 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
700 break;
701 case 4: /* CC (!C) */
702 tmp = tcg_temp_new();
703 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
704 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
705 break;
706 case 5: /* CS (C) */
707 tmp = tcg_temp_new();
708 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
709 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
710 break;
711 case 6: /* NE (!Z) */
712 tmp = tcg_temp_new();
713 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
714 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
715 break;
716 case 7: /* EQ (Z) */
717 tmp = tcg_temp_new();
718 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
719 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
720 break;
721 case 8: /* VC (!V) */
722 tmp = tcg_temp_new();
723 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
724 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
725 break;
726 case 9: /* VS (V) */
727 tmp = tcg_temp_new();
728 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
729 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
730 break;
731 case 10: /* PL (!N) */
732 tmp = tcg_temp_new();
733 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
734 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
735 break;
736 case 11: /* MI (N) */
737 tmp = tcg_temp_new();
738 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
739 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
740 break;
741 case 12: /* GE (!(N ^ V)) */
742 tmp = tcg_temp_new();
743 assert(CCF_V == (CCF_N >> 2));
744 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
745 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
746 tcg_gen_andi_i32(tmp, tmp, CCF_V);
747 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
748 break;
749 case 13: /* LT (N ^ V) */
750 tmp = tcg_temp_new();
751 assert(CCF_V == (CCF_N >> 2));
752 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
753 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
754 tcg_gen_andi_i32(tmp, tmp, CCF_V);
755 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
756 break;
757 case 14: /* GT (!(Z || (N ^ V))) */
758 tmp = tcg_temp_new();
759 assert(CCF_V == (CCF_N >> 2));
760 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
761 tcg_gen_shri_i32(tmp, tmp, 2);
762 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
763 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
764 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
765 break;
766 case 15: /* LE (Z || (N ^ V)) */
767 tmp = tcg_temp_new();
768 assert(CCF_V == (CCF_N >> 2));
769 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
770 tcg_gen_shri_i32(tmp, tmp, 2);
771 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
772 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
773 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
774 break;
775 default:
776 /* Should ever happen. */
777 abort();
781 DISAS_INSN(scc)
783 int l1;
784 int cond;
785 TCGv reg;
787 l1 = gen_new_label();
788 cond = (insn >> 8) & 0xf;
789 reg = DREG(insn, 0);
790 tcg_gen_andi_i32(reg, reg, 0xffffff00);
791 /* This is safe because we modify the reg directly, with no other values
792 live. */
793 gen_jmpcc(s, cond ^ 1, l1);
794 tcg_gen_ori_i32(reg, reg, 0xff);
795 gen_set_label(l1);
798 /* Force a TB lookup after an instruction that changes the CPU state. */
799 static void gen_lookup_tb(DisasContext *s)
801 gen_flush_cc_op(s);
802 tcg_gen_movi_i32(QREG_PC, s->pc);
803 s->is_jmp = DISAS_UPDATE;
806 /* Generate a jump to an immediate address. */
807 static void gen_jmp_im(DisasContext *s, uint32_t dest)
809 gen_flush_cc_op(s);
810 tcg_gen_movi_i32(QREG_PC, dest);
811 s->is_jmp = DISAS_JUMP;
814 /* Generate a jump to the address in qreg DEST. */
815 static void gen_jmp(DisasContext *s, TCGv dest)
817 gen_flush_cc_op(s);
818 tcg_gen_mov_i32(QREG_PC, dest);
819 s->is_jmp = DISAS_JUMP;
822 static void gen_exception(DisasContext *s, uint32_t where, int nr)
824 gen_flush_cc_op(s);
825 gen_jmp_im(s, where);
826 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
829 static inline void gen_addr_fault(DisasContext *s)
831 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
834 #define SRC_EA(env, result, opsize, op_sign, addrp) do { \
835 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
836 op_sign ? EA_LOADS : EA_LOADU); \
837 if (IS_NULL_QREG(result)) { \
838 gen_addr_fault(s); \
839 return; \
841 } while (0)
843 #define DEST_EA(env, insn, opsize, val, addrp) do { \
844 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
845 if (IS_NULL_QREG(ea_result)) { \
846 gen_addr_fault(s); \
847 return; \
849 } while (0)
851 /* Generate a jump to an immediate address. */
852 static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
854 TranslationBlock *tb;
856 tb = s->tb;
857 if (unlikely(s->singlestep_enabled)) {
858 gen_exception(s, dest, EXCP_DEBUG);
859 } else if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
860 (s->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
861 tcg_gen_goto_tb(n);
862 tcg_gen_movi_i32(QREG_PC, dest);
863 tcg_gen_exit_tb((uintptr_t)tb + n);
864 } else {
865 gen_jmp_im(s, dest);
866 tcg_gen_exit_tb(0);
868 s->is_jmp = DISAS_TB_JUMP;
871 DISAS_INSN(undef_mac)
873 gen_exception(s, s->pc - 2, EXCP_LINEA);
876 DISAS_INSN(undef_fpu)
878 gen_exception(s, s->pc - 2, EXCP_LINEF);
881 DISAS_INSN(undef)
883 M68kCPU *cpu = m68k_env_get_cpu(env);
885 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
886 cpu_abort(CPU(cpu), "Illegal instruction: %04x @ %08x", insn, s->pc - 2);
889 DISAS_INSN(mulw)
891 TCGv reg;
892 TCGv tmp;
893 TCGv src;
894 int sign;
896 sign = (insn & 0x100) != 0;
897 reg = DREG(insn, 9);
898 tmp = tcg_temp_new();
899 if (sign)
900 tcg_gen_ext16s_i32(tmp, reg);
901 else
902 tcg_gen_ext16u_i32(tmp, reg);
903 SRC_EA(env, 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);
910 DISAS_INSN(divw)
912 TCGv reg;
913 TCGv tmp;
914 TCGv src;
915 int sign;
917 sign = (insn & 0x100) != 0;
918 reg = DREG(insn, 9);
919 if (sign) {
920 tcg_gen_ext16s_i32(QREG_DIV1, reg);
921 } else {
922 tcg_gen_ext16u_i32(QREG_DIV1, reg);
924 SRC_EA(env, src, OS_WORD, sign, NULL);
925 tcg_gen_mov_i32(QREG_DIV2, src);
926 if (sign) {
927 gen_helper_divs(cpu_env, tcg_const_i32(1));
928 } else {
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;
940 DISAS_INSN(divl)
942 TCGv num;
943 TCGv den;
944 TCGv reg;
945 uint16_t ext;
947 ext = cpu_lduw_code(env, s->pc);
948 s->pc += 2;
949 if (ext & 0x87f8) {
950 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
951 return;
953 num = DREG(ext, 12);
954 reg = DREG(ext, 0);
955 tcg_gen_mov_i32(QREG_DIV1, num);
956 SRC_EA(env, den, OS_LONG, 0, NULL);
957 tcg_gen_mov_i32(QREG_DIV2, den);
958 if (ext & 0x0800) {
959 gen_helper_divs(cpu_env, tcg_const_i32(0));
960 } else {
961 gen_helper_divu(cpu_env, tcg_const_i32(0));
963 if ((ext & 7) == ((ext >> 12) & 7)) {
964 /* div */
965 tcg_gen_mov_i32 (reg, QREG_DIV1);
966 } else {
967 /* rem */
968 tcg_gen_mov_i32 (reg, QREG_DIV2);
970 s->cc_op = CC_OP_FLAGS;
973 DISAS_INSN(addsub)
975 TCGv reg;
976 TCGv dest;
977 TCGv src;
978 TCGv tmp;
979 TCGv addr;
980 int add;
982 add = (insn & 0x4000) != 0;
983 reg = DREG(insn, 9);
984 dest = tcg_temp_new();
985 if (insn & 0x100) {
986 SRC_EA(env, tmp, OS_LONG, 0, &addr);
987 src = reg;
988 } else {
989 tmp = reg;
990 SRC_EA(env, src, OS_LONG, 0, NULL);
992 if (add) {
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;
996 } else {
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);
1002 if (insn & 0x100) {
1003 DEST_EA(env, insn, OS_LONG, dest, &addr);
1004 } else {
1005 tcg_gen_mov_i32(reg, dest);
1010 /* Reverse the order of the bits in REG. */
1011 DISAS_INSN(bitrev)
1013 TCGv reg;
1014 reg = DREG(insn, 0);
1015 gen_helper_bitrev(reg, reg);
1018 DISAS_INSN(bitop_reg)
1020 int opsize;
1021 int op;
1022 TCGv src1;
1023 TCGv src2;
1024 TCGv tmp;
1025 TCGv addr;
1026 TCGv dest;
1028 if ((insn & 0x38) != 0)
1029 opsize = OS_BYTE;
1030 else
1031 opsize = OS_LONG;
1032 op = (insn >> 6) & 3;
1033 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1034 src2 = DREG(insn, 9);
1035 dest = tcg_temp_new();
1037 gen_flush_flags(s);
1038 tmp = tcg_temp_new();
1039 if (opsize == OS_BYTE)
1040 tcg_gen_andi_i32(tmp, src2, 7);
1041 else
1042 tcg_gen_andi_i32(tmp, src2, 31);
1043 src2 = tmp;
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);
1053 switch (op) {
1054 case 1: /* bchg */
1055 tcg_gen_xor_i32(dest, src1, tmp);
1056 break;
1057 case 2: /* bclr */
1058 tcg_gen_not_i32(tmp, tmp);
1059 tcg_gen_and_i32(dest, src1, tmp);
1060 break;
1061 case 3: /* bset */
1062 tcg_gen_or_i32(dest, src1, tmp);
1063 break;
1064 default: /* btst */
1065 break;
1067 if (op)
1068 DEST_EA(env, insn, opsize, dest, &addr);
1071 DISAS_INSN(sats)
1073 TCGv reg;
1074 reg = DREG(insn, 0);
1075 gen_flush_flags(s);
1076 gen_helper_sats(reg, reg, QREG_CC_DEST);
1077 gen_logic_cc(s, reg);
1080 static void gen_push(DisasContext *s, TCGv val)
1082 TCGv tmp;
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);
1090 DISAS_INSN(movem)
1092 TCGv addr;
1093 int i;
1094 uint16_t mask;
1095 TCGv reg;
1096 TCGv tmp;
1097 int is_load;
1099 mask = cpu_lduw_code(env, s->pc);
1100 s->pc += 2;
1101 tmp = gen_lea(env, s, insn, OS_LONG);
1102 if (IS_NULL_QREG(tmp)) {
1103 gen_addr_fault(s);
1104 return;
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) {
1110 if (mask & 1) {
1111 if (i < 8)
1112 reg = DREG(i, 0);
1113 else
1114 reg = AREG(i, 0);
1115 if (is_load) {
1116 tmp = gen_load(s, OS_LONG, addr, 0);
1117 tcg_gen_mov_i32(reg, tmp);
1118 } else {
1119 gen_store(s, OS_LONG, addr, reg);
1121 if (mask != 1)
1122 tcg_gen_addi_i32(addr, addr, 4);
1127 DISAS_INSN(bitop_im)
1129 int opsize;
1130 int op;
1131 TCGv src1;
1132 uint32_t mask;
1133 int bitnum;
1134 TCGv tmp;
1135 TCGv addr;
1137 if ((insn & 0x38) != 0)
1138 opsize = OS_BYTE;
1139 else
1140 opsize = OS_LONG;
1141 op = (insn >> 6) & 3;
1143 bitnum = cpu_lduw_code(env, s->pc);
1144 s->pc += 2;
1145 if (bitnum & 0xff00) {
1146 disas_undef(env, s, insn);
1147 return;
1150 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
1152 gen_flush_flags(s);
1153 if (opsize == OS_BYTE)
1154 bitnum &= 7;
1155 else
1156 bitnum &= 31;
1157 mask = 1 << bitnum;
1159 tmp = tcg_temp_new();
1160 assert (CCF_Z == (1 << 2));
1161 if (bitnum > 2)
1162 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1163 else if (bitnum < 2)
1164 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
1165 else
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);
1171 if (op) {
1172 switch (op) {
1173 case 1: /* bchg */
1174 tcg_gen_xori_i32(tmp, src1, mask);
1175 break;
1176 case 2: /* bclr */
1177 tcg_gen_andi_i32(tmp, src1, ~mask);
1178 break;
1179 case 3: /* bset */
1180 tcg_gen_ori_i32(tmp, src1, mask);
1181 break;
1182 default: /* btst */
1183 break;
1185 DEST_EA(env, insn, opsize, tmp, &addr);
1189 DISAS_INSN(arith_im)
1191 int op;
1192 uint32_t im;
1193 TCGv src1;
1194 TCGv dest;
1195 TCGv addr;
1197 op = (insn >> 9) & 7;
1198 SRC_EA(env, src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1199 im = read_im32(env, s);
1200 dest = tcg_temp_new();
1201 switch (op) {
1202 case 0: /* ori */
1203 tcg_gen_ori_i32(dest, src1, im);
1204 gen_logic_cc(s, dest);
1205 break;
1206 case 1: /* andi */
1207 tcg_gen_andi_i32(dest, src1, im);
1208 gen_logic_cc(s, dest);
1209 break;
1210 case 2: /* subi */
1211 tcg_gen_mov_i32(dest, src1);
1212 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1213 tcg_gen_subi_i32(dest, dest, im);
1214 gen_update_cc_add(dest, tcg_const_i32(im));
1215 s->cc_op = CC_OP_SUB;
1216 break;
1217 case 3: /* addi */
1218 tcg_gen_mov_i32(dest, src1);
1219 tcg_gen_addi_i32(dest, dest, im);
1220 gen_update_cc_add(dest, tcg_const_i32(im));
1221 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
1222 s->cc_op = CC_OP_ADD;
1223 break;
1224 case 5: /* eori */
1225 tcg_gen_xori_i32(dest, src1, im);
1226 gen_logic_cc(s, dest);
1227 break;
1228 case 6: /* cmpi */
1229 tcg_gen_mov_i32(dest, src1);
1230 tcg_gen_subi_i32(dest, dest, im);
1231 gen_update_cc_add(dest, tcg_const_i32(im));
1232 s->cc_op = CC_OP_SUB;
1233 break;
1234 default:
1235 abort();
1237 if (op != 6) {
1238 DEST_EA(env, insn, OS_LONG, dest, &addr);
1242 DISAS_INSN(byterev)
1244 TCGv reg;
1246 reg = DREG(insn, 0);
1247 tcg_gen_bswap32_i32(reg, reg);
1250 DISAS_INSN(move)
1252 TCGv src;
1253 TCGv dest;
1254 int op;
1255 int opsize;
1257 switch (insn >> 12) {
1258 case 1: /* move.b */
1259 opsize = OS_BYTE;
1260 break;
1261 case 2: /* move.l */
1262 opsize = OS_LONG;
1263 break;
1264 case 3: /* move.w */
1265 opsize = OS_WORD;
1266 break;
1267 default:
1268 abort();
1270 SRC_EA(env, src, opsize, 1, NULL);
1271 op = (insn >> 6) & 7;
1272 if (op == 1) {
1273 /* movea */
1274 /* The value will already have been sign extended. */
1275 dest = AREG(insn, 9);
1276 tcg_gen_mov_i32(dest, src);
1277 } else {
1278 /* normal move */
1279 uint16_t dest_ea;
1280 dest_ea = ((insn >> 9) & 7) | (op << 3);
1281 DEST_EA(env, dest_ea, opsize, src, NULL);
1282 /* This will be correct because loads sign extend. */
1283 gen_logic_cc(s, src);
1287 DISAS_INSN(negx)
1289 TCGv reg;
1291 gen_flush_flags(s);
1292 reg = DREG(insn, 0);
1293 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
1296 DISAS_INSN(lea)
1298 TCGv reg;
1299 TCGv tmp;
1301 reg = AREG(insn, 9);
1302 tmp = gen_lea(env, s, insn, OS_LONG);
1303 if (IS_NULL_QREG(tmp)) {
1304 gen_addr_fault(s);
1305 return;
1307 tcg_gen_mov_i32(reg, tmp);
1310 DISAS_INSN(clr)
1312 int opsize;
1314 switch ((insn >> 6) & 3) {
1315 case 0: /* clr.b */
1316 opsize = OS_BYTE;
1317 break;
1318 case 1: /* clr.w */
1319 opsize = OS_WORD;
1320 break;
1321 case 2: /* clr.l */
1322 opsize = OS_LONG;
1323 break;
1324 default:
1325 abort();
1327 DEST_EA(env, insn, opsize, tcg_const_i32(0), NULL);
1328 gen_logic_cc(s, tcg_const_i32(0));
1331 static TCGv gen_get_ccr(DisasContext *s)
1333 TCGv dest;
1335 gen_flush_flags(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);
1339 return dest;
1342 DISAS_INSN(move_from_ccr)
1344 TCGv reg;
1345 TCGv ccr;
1347 ccr = gen_get_ccr(s);
1348 reg = DREG(insn, 0);
1349 gen_partset_reg(OS_WORD, reg, ccr);
1352 DISAS_INSN(neg)
1354 TCGv reg;
1355 TCGv src1;
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);
1371 if (!ccr_only) {
1372 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
1376 static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
1377 int ccr_only)
1379 TCGv tmp;
1380 TCGv reg;
1382 s->cc_op = CC_OP_FLAGS;
1383 if ((insn & 0x38) == 0)
1385 tmp = tcg_temp_new();
1386 reg = DREG(insn, 0);
1387 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1388 tcg_gen_shri_i32(tmp, reg, 4);
1389 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
1390 if (!ccr_only) {
1391 gen_helper_set_sr(cpu_env, reg);
1394 else if ((insn & 0x3f) == 0x3c)
1396 uint16_t val;
1397 val = cpu_lduw_code(env, s->pc);
1398 s->pc += 2;
1399 gen_set_sr_im(s, val, ccr_only);
1401 else
1402 disas_undef(env, s, insn);
1405 DISAS_INSN(move_to_ccr)
1407 gen_set_sr(env, s, insn, 1);
1410 DISAS_INSN(not)
1412 TCGv reg;
1414 reg = DREG(insn, 0);
1415 tcg_gen_not_i32(reg, reg);
1416 gen_logic_cc(s, reg);
1419 DISAS_INSN(swap)
1421 TCGv src1;
1422 TCGv src2;
1423 TCGv reg;
1425 src1 = tcg_temp_new();
1426 src2 = tcg_temp_new();
1427 reg = DREG(insn, 0);
1428 tcg_gen_shli_i32(src1, reg, 16);
1429 tcg_gen_shri_i32(src2, reg, 16);
1430 tcg_gen_or_i32(reg, src1, src2);
1431 gen_logic_cc(s, reg);
1434 DISAS_INSN(pea)
1436 TCGv tmp;
1438 tmp = gen_lea(env, s, insn, OS_LONG);
1439 if (IS_NULL_QREG(tmp)) {
1440 gen_addr_fault(s);
1441 return;
1443 gen_push(s, tmp);
1446 DISAS_INSN(ext)
1448 int op;
1449 TCGv reg;
1450 TCGv tmp;
1452 reg = DREG(insn, 0);
1453 op = (insn >> 6) & 7;
1454 tmp = tcg_temp_new();
1455 if (op == 3)
1456 tcg_gen_ext16s_i32(tmp, reg);
1457 else
1458 tcg_gen_ext8s_i32(tmp, reg);
1459 if (op == 2)
1460 gen_partset_reg(OS_WORD, reg, tmp);
1461 else
1462 tcg_gen_mov_i32(reg, tmp);
1463 gen_logic_cc(s, tmp);
1466 DISAS_INSN(tst)
1468 int opsize;
1469 TCGv tmp;
1471 switch ((insn >> 6) & 3) {
1472 case 0: /* tst.b */
1473 opsize = OS_BYTE;
1474 break;
1475 case 1: /* tst.w */
1476 opsize = OS_WORD;
1477 break;
1478 case 2: /* tst.l */
1479 opsize = OS_LONG;
1480 break;
1481 default:
1482 abort();
1484 SRC_EA(env, tmp, opsize, 1, NULL);
1485 gen_logic_cc(s, tmp);
1488 DISAS_INSN(pulse)
1490 /* Implemented as a NOP. */
1493 DISAS_INSN(illegal)
1495 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1498 /* ??? This should be atomic. */
1499 DISAS_INSN(tas)
1501 TCGv dest;
1502 TCGv src1;
1503 TCGv addr;
1505 dest = tcg_temp_new();
1506 SRC_EA(env, src1, OS_BYTE, 1, &addr);
1507 gen_logic_cc(s, src1);
1508 tcg_gen_ori_i32(dest, src1, 0x80);
1509 DEST_EA(env, insn, OS_BYTE, dest, &addr);
1512 DISAS_INSN(mull)
1514 uint16_t ext;
1515 TCGv reg;
1516 TCGv src1;
1517 TCGv dest;
1519 /* The upper 32 bits of the product are discarded, so
1520 muls.l and mulu.l are functionally equivalent. */
1521 ext = cpu_lduw_code(env, s->pc);
1522 s->pc += 2;
1523 if (ext & 0x87ff) {
1524 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1525 return;
1527 reg = DREG(ext, 12);
1528 SRC_EA(env, src1, OS_LONG, 0, NULL);
1529 dest = tcg_temp_new();
1530 tcg_gen_mul_i32(dest, src1, reg);
1531 tcg_gen_mov_i32(reg, dest);
1532 /* Unlike m68k, coldfire always clears the overflow bit. */
1533 gen_logic_cc(s, dest);
1536 DISAS_INSN(link)
1538 int16_t offset;
1539 TCGv reg;
1540 TCGv tmp;
1542 offset = cpu_ldsw_code(env, s->pc);
1543 s->pc += 2;
1544 reg = AREG(insn, 0);
1545 tmp = tcg_temp_new();
1546 tcg_gen_subi_i32(tmp, QREG_SP, 4);
1547 gen_store(s, OS_LONG, tmp, reg);
1548 if ((insn & 7) != 7)
1549 tcg_gen_mov_i32(reg, tmp);
1550 tcg_gen_addi_i32(QREG_SP, tmp, offset);
1553 DISAS_INSN(unlk)
1555 TCGv src;
1556 TCGv reg;
1557 TCGv tmp;
1559 src = tcg_temp_new();
1560 reg = AREG(insn, 0);
1561 tcg_gen_mov_i32(src, reg);
1562 tmp = gen_load(s, OS_LONG, src, 0);
1563 tcg_gen_mov_i32(reg, tmp);
1564 tcg_gen_addi_i32(QREG_SP, src, 4);
1567 DISAS_INSN(nop)
1571 DISAS_INSN(rts)
1573 TCGv tmp;
1575 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
1576 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
1577 gen_jmp(s, tmp);
1580 DISAS_INSN(jump)
1582 TCGv tmp;
1584 /* Load the target address first to ensure correct exception
1585 behavior. */
1586 tmp = gen_lea(env, s, insn, OS_LONG);
1587 if (IS_NULL_QREG(tmp)) {
1588 gen_addr_fault(s);
1589 return;
1591 if ((insn & 0x40) == 0) {
1592 /* jsr */
1593 gen_push(s, tcg_const_i32(s->pc));
1595 gen_jmp(s, tmp);
1598 DISAS_INSN(addsubq)
1600 TCGv src1;
1601 TCGv src2;
1602 TCGv dest;
1603 int val;
1604 TCGv addr;
1606 SRC_EA(env, src1, OS_LONG, 0, &addr);
1607 val = (insn >> 9) & 7;
1608 if (val == 0)
1609 val = 8;
1610 dest = tcg_temp_new();
1611 tcg_gen_mov_i32(dest, src1);
1612 if ((insn & 0x38) == 0x08) {
1613 /* Don't update condition codes if the destination is an
1614 address register. */
1615 if (insn & 0x0100) {
1616 tcg_gen_subi_i32(dest, dest, val);
1617 } else {
1618 tcg_gen_addi_i32(dest, dest, val);
1620 } else {
1621 src2 = tcg_const_i32(val);
1622 if (insn & 0x0100) {
1623 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1624 tcg_gen_subi_i32(dest, dest, val);
1625 s->cc_op = CC_OP_SUB;
1626 } else {
1627 tcg_gen_addi_i32(dest, dest, val);
1628 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1629 s->cc_op = CC_OP_ADD;
1631 gen_update_cc_add(dest, src2);
1633 DEST_EA(env, insn, OS_LONG, dest, &addr);
1636 DISAS_INSN(tpf)
1638 switch (insn & 7) {
1639 case 2: /* One extension word. */
1640 s->pc += 2;
1641 break;
1642 case 3: /* Two extension words. */
1643 s->pc += 4;
1644 break;
1645 case 4: /* No extension words. */
1646 break;
1647 default:
1648 disas_undef(env, s, insn);
1652 DISAS_INSN(branch)
1654 int32_t offset;
1655 uint32_t base;
1656 int op;
1657 int l1;
1659 base = s->pc;
1660 op = (insn >> 8) & 0xf;
1661 offset = (int8_t)insn;
1662 if (offset == 0) {
1663 offset = cpu_ldsw_code(env, s->pc);
1664 s->pc += 2;
1665 } else if (offset == -1) {
1666 offset = read_im32(env, s);
1668 if (op == 1) {
1669 /* bsr */
1670 gen_push(s, tcg_const_i32(s->pc));
1672 gen_flush_cc_op(s);
1673 if (op > 1) {
1674 /* Bcc */
1675 l1 = gen_new_label();
1676 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1677 gen_jmp_tb(s, 1, base + offset);
1678 gen_set_label(l1);
1679 gen_jmp_tb(s, 0, s->pc);
1680 } else {
1681 /* Unconditional branch. */
1682 gen_jmp_tb(s, 0, base + offset);
1686 DISAS_INSN(moveq)
1688 uint32_t val;
1690 val = (int8_t)insn;
1691 tcg_gen_movi_i32(DREG(insn, 9), val);
1692 gen_logic_cc(s, tcg_const_i32(val));
1695 DISAS_INSN(mvzs)
1697 int opsize;
1698 TCGv src;
1699 TCGv reg;
1701 if (insn & 0x40)
1702 opsize = OS_WORD;
1703 else
1704 opsize = OS_BYTE;
1705 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
1706 reg = DREG(insn, 9);
1707 tcg_gen_mov_i32(reg, src);
1708 gen_logic_cc(s, src);
1711 DISAS_INSN(or)
1713 TCGv reg;
1714 TCGv dest;
1715 TCGv src;
1716 TCGv addr;
1718 reg = DREG(insn, 9);
1719 dest = tcg_temp_new();
1720 if (insn & 0x100) {
1721 SRC_EA(env, src, OS_LONG, 0, &addr);
1722 tcg_gen_or_i32(dest, src, reg);
1723 DEST_EA(env, insn, OS_LONG, dest, &addr);
1724 } else {
1725 SRC_EA(env, src, OS_LONG, 0, NULL);
1726 tcg_gen_or_i32(dest, src, reg);
1727 tcg_gen_mov_i32(reg, dest);
1729 gen_logic_cc(s, dest);
1732 DISAS_INSN(suba)
1734 TCGv src;
1735 TCGv reg;
1737 SRC_EA(env, src, OS_LONG, 0, NULL);
1738 reg = AREG(insn, 9);
1739 tcg_gen_sub_i32(reg, reg, src);
1742 DISAS_INSN(subx)
1744 TCGv reg;
1745 TCGv src;
1747 gen_flush_flags(s);
1748 reg = DREG(insn, 9);
1749 src = DREG(insn, 0);
1750 gen_helper_subx_cc(reg, cpu_env, reg, src);
1753 DISAS_INSN(mov3q)
1755 TCGv src;
1756 int val;
1758 val = (insn >> 9) & 7;
1759 if (val == 0)
1760 val = -1;
1761 src = tcg_const_i32(val);
1762 gen_logic_cc(s, src);
1763 DEST_EA(env, insn, OS_LONG, src, NULL);
1766 DISAS_INSN(cmp)
1768 int op;
1769 TCGv src;
1770 TCGv reg;
1771 TCGv dest;
1772 int opsize;
1774 op = (insn >> 6) & 3;
1775 switch (op) {
1776 case 0: /* cmp.b */
1777 opsize = OS_BYTE;
1778 s->cc_op = CC_OP_CMPB;
1779 break;
1780 case 1: /* cmp.w */
1781 opsize = OS_WORD;
1782 s->cc_op = CC_OP_CMPW;
1783 break;
1784 case 2: /* cmp.l */
1785 opsize = OS_LONG;
1786 s->cc_op = CC_OP_SUB;
1787 break;
1788 default:
1789 abort();
1791 SRC_EA(env, src, opsize, 1, NULL);
1792 reg = DREG(insn, 9);
1793 dest = tcg_temp_new();
1794 tcg_gen_sub_i32(dest, reg, src);
1795 gen_update_cc_add(dest, src);
1798 DISAS_INSN(cmpa)
1800 int opsize;
1801 TCGv src;
1802 TCGv reg;
1803 TCGv dest;
1805 if (insn & 0x100) {
1806 opsize = OS_LONG;
1807 } else {
1808 opsize = OS_WORD;
1810 SRC_EA(env, src, opsize, 1, NULL);
1811 reg = AREG(insn, 9);
1812 dest = tcg_temp_new();
1813 tcg_gen_sub_i32(dest, reg, src);
1814 gen_update_cc_add(dest, src);
1815 s->cc_op = CC_OP_SUB;
1818 DISAS_INSN(eor)
1820 TCGv src;
1821 TCGv reg;
1822 TCGv dest;
1823 TCGv addr;
1825 SRC_EA(env, src, OS_LONG, 0, &addr);
1826 reg = DREG(insn, 9);
1827 dest = tcg_temp_new();
1828 tcg_gen_xor_i32(dest, src, reg);
1829 gen_logic_cc(s, dest);
1830 DEST_EA(env, insn, OS_LONG, dest, &addr);
1833 DISAS_INSN(and)
1835 TCGv src;
1836 TCGv reg;
1837 TCGv dest;
1838 TCGv addr;
1840 reg = DREG(insn, 9);
1841 dest = tcg_temp_new();
1842 if (insn & 0x100) {
1843 SRC_EA(env, src, OS_LONG, 0, &addr);
1844 tcg_gen_and_i32(dest, src, reg);
1845 DEST_EA(env, insn, OS_LONG, dest, &addr);
1846 } else {
1847 SRC_EA(env, src, OS_LONG, 0, NULL);
1848 tcg_gen_and_i32(dest, src, reg);
1849 tcg_gen_mov_i32(reg, dest);
1851 gen_logic_cc(s, dest);
1854 DISAS_INSN(adda)
1856 TCGv src;
1857 TCGv reg;
1859 SRC_EA(env, src, OS_LONG, 0, NULL);
1860 reg = AREG(insn, 9);
1861 tcg_gen_add_i32(reg, reg, src);
1864 DISAS_INSN(addx)
1866 TCGv reg;
1867 TCGv src;
1869 gen_flush_flags(s);
1870 reg = DREG(insn, 9);
1871 src = DREG(insn, 0);
1872 gen_helper_addx_cc(reg, cpu_env, reg, src);
1873 s->cc_op = CC_OP_FLAGS;
1876 /* TODO: This could be implemented without helper functions. */
1877 DISAS_INSN(shift_im)
1879 TCGv reg;
1880 int tmp;
1881 TCGv shift;
1883 reg = DREG(insn, 0);
1884 tmp = (insn >> 9) & 7;
1885 if (tmp == 0)
1886 tmp = 8;
1887 shift = tcg_const_i32(tmp);
1888 /* No need to flush flags becuse we know we will set C flag. */
1889 if (insn & 0x100) {
1890 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1891 } else {
1892 if (insn & 8) {
1893 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1894 } else {
1895 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1898 s->cc_op = CC_OP_SHIFT;
1901 DISAS_INSN(shift_reg)
1903 TCGv reg;
1904 TCGv shift;
1906 reg = DREG(insn, 0);
1907 shift = DREG(insn, 9);
1908 /* Shift by zero leaves C flag unmodified. */
1909 gen_flush_flags(s);
1910 if (insn & 0x100) {
1911 gen_helper_shl_cc(reg, cpu_env, reg, shift);
1912 } else {
1913 if (insn & 8) {
1914 gen_helper_shr_cc(reg, cpu_env, reg, shift);
1915 } else {
1916 gen_helper_sar_cc(reg, cpu_env, reg, shift);
1919 s->cc_op = CC_OP_SHIFT;
1922 DISAS_INSN(ff1)
1924 TCGv reg;
1925 reg = DREG(insn, 0);
1926 gen_logic_cc(s, reg);
1927 gen_helper_ff1(reg, reg);
1930 static TCGv gen_get_sr(DisasContext *s)
1932 TCGv ccr;
1933 TCGv sr;
1935 ccr = gen_get_ccr(s);
1936 sr = tcg_temp_new();
1937 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1938 tcg_gen_or_i32(sr, sr, ccr);
1939 return sr;
1942 DISAS_INSN(strldsr)
1944 uint16_t ext;
1945 uint32_t addr;
1947 addr = s->pc - 2;
1948 ext = cpu_lduw_code(env, s->pc);
1949 s->pc += 2;
1950 if (ext != 0x46FC) {
1951 gen_exception(s, addr, EXCP_UNSUPPORTED);
1952 return;
1954 ext = cpu_lduw_code(env, s->pc);
1955 s->pc += 2;
1956 if (IS_USER(s) || (ext & SR_S) == 0) {
1957 gen_exception(s, addr, EXCP_PRIVILEGE);
1958 return;
1960 gen_push(s, gen_get_sr(s));
1961 gen_set_sr_im(s, ext, 0);
1964 DISAS_INSN(move_from_sr)
1966 TCGv reg;
1967 TCGv sr;
1969 if (IS_USER(s)) {
1970 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1971 return;
1973 sr = gen_get_sr(s);
1974 reg = DREG(insn, 0);
1975 gen_partset_reg(OS_WORD, reg, sr);
1978 DISAS_INSN(move_to_sr)
1980 if (IS_USER(s)) {
1981 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1982 return;
1984 gen_set_sr(env, s, insn, 0);
1985 gen_lookup_tb(s);
1988 DISAS_INSN(move_from_usp)
1990 if (IS_USER(s)) {
1991 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1992 return;
1994 /* TODO: Implement USP. */
1995 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1998 DISAS_INSN(move_to_usp)
2000 if (IS_USER(s)) {
2001 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2002 return;
2004 /* TODO: Implement USP. */
2005 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
2008 DISAS_INSN(halt)
2010 gen_exception(s, s->pc, EXCP_HALT_INSN);
2013 DISAS_INSN(stop)
2015 uint16_t ext;
2017 if (IS_USER(s)) {
2018 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2019 return;
2022 ext = cpu_lduw_code(env, s->pc);
2023 s->pc += 2;
2025 gen_set_sr_im(s, ext, 0);
2026 tcg_gen_movi_i32(cpu_halted, 1);
2027 gen_exception(s, s->pc, EXCP_HLT);
2030 DISAS_INSN(rte)
2032 if (IS_USER(s)) {
2033 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2034 return;
2036 gen_exception(s, s->pc - 2, EXCP_RTE);
2039 DISAS_INSN(movec)
2041 uint16_t ext;
2042 TCGv reg;
2044 if (IS_USER(s)) {
2045 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2046 return;
2049 ext = cpu_lduw_code(env, s->pc);
2050 s->pc += 2;
2052 if (ext & 0x8000) {
2053 reg = AREG(ext, 12);
2054 } else {
2055 reg = DREG(ext, 12);
2057 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
2058 gen_lookup_tb(s);
2061 DISAS_INSN(intouch)
2063 if (IS_USER(s)) {
2064 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2065 return;
2067 /* ICache fetch. Implement as no-op. */
2070 DISAS_INSN(cpushl)
2072 if (IS_USER(s)) {
2073 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2074 return;
2076 /* Cache push/invalidate. Implement as no-op. */
2079 DISAS_INSN(wddata)
2081 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2084 DISAS_INSN(wdebug)
2086 M68kCPU *cpu = m68k_env_get_cpu(env);
2088 if (IS_USER(s)) {
2089 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2090 return;
2092 /* TODO: Implement wdebug. */
2093 cpu_abort(CPU(cpu), "WDEBUG not implemented");
2096 DISAS_INSN(trap)
2098 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2101 /* ??? FP exceptions are not implemented. Most exceptions are deferred until
2102 immediately before the next FP instruction is executed. */
2103 DISAS_INSN(fpu)
2105 uint16_t ext;
2106 int32_t offset;
2107 int opmode;
2108 TCGv_i64 src;
2109 TCGv_i64 dest;
2110 TCGv_i64 res;
2111 TCGv tmp32;
2112 int round;
2113 int set_dest;
2114 int opsize;
2116 ext = cpu_lduw_code(env, s->pc);
2117 s->pc += 2;
2118 opmode = ext & 0x7f;
2119 switch ((ext >> 13) & 7) {
2120 case 0: case 2:
2121 break;
2122 case 1:
2123 goto undef;
2124 case 3: /* fmove out */
2125 src = FREG(ext, 7);
2126 tmp32 = tcg_temp_new_i32();
2127 /* fmove */
2128 /* ??? TODO: Proper behavior on overflow. */
2129 switch ((ext >> 10) & 7) {
2130 case 0:
2131 opsize = OS_LONG;
2132 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2133 break;
2134 case 1:
2135 opsize = OS_SINGLE;
2136 gen_helper_f64_to_f32(tmp32, cpu_env, src);
2137 break;
2138 case 4:
2139 opsize = OS_WORD;
2140 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2141 break;
2142 case 5: /* OS_DOUBLE */
2143 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2144 switch ((insn >> 3) & 7) {
2145 case 2:
2146 case 3:
2147 break;
2148 case 4:
2149 tcg_gen_addi_i32(tmp32, tmp32, -8);
2150 break;
2151 case 5:
2152 offset = cpu_ldsw_code(env, s->pc);
2153 s->pc += 2;
2154 tcg_gen_addi_i32(tmp32, tmp32, offset);
2155 break;
2156 default:
2157 goto undef;
2159 gen_store64(s, tmp32, src);
2160 switch ((insn >> 3) & 7) {
2161 case 3:
2162 tcg_gen_addi_i32(tmp32, tmp32, 8);
2163 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2164 break;
2165 case 4:
2166 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2167 break;
2169 tcg_temp_free_i32(tmp32);
2170 return;
2171 case 6:
2172 opsize = OS_BYTE;
2173 gen_helper_f64_to_i32(tmp32, cpu_env, src);
2174 break;
2175 default:
2176 goto undef;
2178 DEST_EA(env, insn, opsize, tmp32, NULL);
2179 tcg_temp_free_i32(tmp32);
2180 return;
2181 case 4: /* fmove to control register. */
2182 switch ((ext >> 10) & 7) {
2183 case 4: /* FPCR */
2184 /* Not implemented. Ignore writes. */
2185 break;
2186 case 1: /* FPIAR */
2187 case 2: /* FPSR */
2188 default:
2189 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2190 (ext >> 10) & 7);
2192 break;
2193 case 5: /* fmove from control register. */
2194 switch ((ext >> 10) & 7) {
2195 case 4: /* FPCR */
2196 /* Not implemented. Always return zero. */
2197 tmp32 = tcg_const_i32(0);
2198 break;
2199 case 1: /* FPIAR */
2200 case 2: /* FPSR */
2201 default:
2202 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2203 (ext >> 10) & 7);
2204 goto undef;
2206 DEST_EA(env, insn, OS_LONG, tmp32, NULL);
2207 break;
2208 case 6: /* fmovem */
2209 case 7:
2211 TCGv addr;
2212 uint16_t mask;
2213 int i;
2214 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2215 goto undef;
2216 tmp32 = gen_lea(env, s, insn, OS_LONG);
2217 if (IS_NULL_QREG(tmp32)) {
2218 gen_addr_fault(s);
2219 return;
2221 addr = tcg_temp_new_i32();
2222 tcg_gen_mov_i32(addr, tmp32);
2223 mask = 0x80;
2224 for (i = 0; i < 8; i++) {
2225 if (ext & mask) {
2226 s->is_mem = 1;
2227 dest = FREG(i, 0);
2228 if (ext & (1 << 13)) {
2229 /* store */
2230 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2231 } else {
2232 /* load */
2233 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2235 if (ext & (mask - 1))
2236 tcg_gen_addi_i32(addr, addr, 8);
2238 mask >>= 1;
2240 tcg_temp_free_i32(addr);
2242 return;
2244 if (ext & (1 << 14)) {
2245 /* Source effective address. */
2246 switch ((ext >> 10) & 7) {
2247 case 0: opsize = OS_LONG; break;
2248 case 1: opsize = OS_SINGLE; break;
2249 case 4: opsize = OS_WORD; break;
2250 case 5: opsize = OS_DOUBLE; break;
2251 case 6: opsize = OS_BYTE; break;
2252 default:
2253 goto undef;
2255 if (opsize == OS_DOUBLE) {
2256 tmp32 = tcg_temp_new_i32();
2257 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
2258 switch ((insn >> 3) & 7) {
2259 case 2:
2260 case 3:
2261 break;
2262 case 4:
2263 tcg_gen_addi_i32(tmp32, tmp32, -8);
2264 break;
2265 case 5:
2266 offset = cpu_ldsw_code(env, s->pc);
2267 s->pc += 2;
2268 tcg_gen_addi_i32(tmp32, tmp32, offset);
2269 break;
2270 case 7:
2271 offset = cpu_ldsw_code(env, s->pc);
2272 offset += s->pc - 2;
2273 s->pc += 2;
2274 tcg_gen_addi_i32(tmp32, tmp32, offset);
2275 break;
2276 default:
2277 goto undef;
2279 src = gen_load64(s, tmp32);
2280 switch ((insn >> 3) & 7) {
2281 case 3:
2282 tcg_gen_addi_i32(tmp32, tmp32, 8);
2283 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2284 break;
2285 case 4:
2286 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2287 break;
2289 tcg_temp_free_i32(tmp32);
2290 } else {
2291 SRC_EA(env, tmp32, opsize, 1, NULL);
2292 src = tcg_temp_new_i64();
2293 switch (opsize) {
2294 case OS_LONG:
2295 case OS_WORD:
2296 case OS_BYTE:
2297 gen_helper_i32_to_f64(src, cpu_env, tmp32);
2298 break;
2299 case OS_SINGLE:
2300 gen_helper_f32_to_f64(src, cpu_env, tmp32);
2301 break;
2304 } else {
2305 /* Source register. */
2306 src = FREG(ext, 10);
2308 dest = FREG(ext, 7);
2309 res = tcg_temp_new_i64();
2310 if (opmode != 0x3a)
2311 tcg_gen_mov_f64(res, dest);
2312 round = 1;
2313 set_dest = 1;
2314 switch (opmode) {
2315 case 0: case 0x40: case 0x44: /* fmove */
2316 tcg_gen_mov_f64(res, src);
2317 break;
2318 case 1: /* fint */
2319 gen_helper_iround_f64(res, cpu_env, src);
2320 round = 0;
2321 break;
2322 case 3: /* fintrz */
2323 gen_helper_itrunc_f64(res, cpu_env, src);
2324 round = 0;
2325 break;
2326 case 4: case 0x41: case 0x45: /* fsqrt */
2327 gen_helper_sqrt_f64(res, cpu_env, src);
2328 break;
2329 case 0x18: case 0x58: case 0x5c: /* fabs */
2330 gen_helper_abs_f64(res, src);
2331 break;
2332 case 0x1a: case 0x5a: case 0x5e: /* fneg */
2333 gen_helper_chs_f64(res, src);
2334 break;
2335 case 0x20: case 0x60: case 0x64: /* fdiv */
2336 gen_helper_div_f64(res, cpu_env, res, src);
2337 break;
2338 case 0x22: case 0x62: case 0x66: /* fadd */
2339 gen_helper_add_f64(res, cpu_env, res, src);
2340 break;
2341 case 0x23: case 0x63: case 0x67: /* fmul */
2342 gen_helper_mul_f64(res, cpu_env, res, src);
2343 break;
2344 case 0x28: case 0x68: case 0x6c: /* fsub */
2345 gen_helper_sub_f64(res, cpu_env, res, src);
2346 break;
2347 case 0x38: /* fcmp */
2348 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
2349 set_dest = 0;
2350 round = 0;
2351 break;
2352 case 0x3a: /* ftst */
2353 tcg_gen_mov_f64(res, src);
2354 set_dest = 0;
2355 round = 0;
2356 break;
2357 default:
2358 goto undef;
2360 if (ext & (1 << 14)) {
2361 tcg_temp_free_i64(src);
2363 if (round) {
2364 if (opmode & 0x40) {
2365 if ((opmode & 0x4) != 0)
2366 round = 0;
2367 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2368 round = 0;
2371 if (round) {
2372 TCGv tmp = tcg_temp_new_i32();
2373 gen_helper_f64_to_f32(tmp, cpu_env, res);
2374 gen_helper_f32_to_f64(res, cpu_env, tmp);
2375 tcg_temp_free_i32(tmp);
2377 tcg_gen_mov_f64(QREG_FP_RESULT, res);
2378 if (set_dest) {
2379 tcg_gen_mov_f64(dest, res);
2381 tcg_temp_free_i64(res);
2382 return;
2383 undef:
2384 /* FIXME: Is this right for offset addressing modes? */
2385 s->pc -= 2;
2386 disas_undef_fpu(env, s, insn);
2389 DISAS_INSN(fbcc)
2391 uint32_t offset;
2392 uint32_t addr;
2393 TCGv flag;
2394 int l1;
2396 addr = s->pc;
2397 offset = cpu_ldsw_code(env, s->pc);
2398 s->pc += 2;
2399 if (insn & (1 << 6)) {
2400 offset = (offset << 16) | cpu_lduw_code(env, s->pc);
2401 s->pc += 2;
2404 l1 = gen_new_label();
2405 /* TODO: Raise BSUN exception. */
2406 flag = tcg_temp_new();
2407 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
2408 /* Jump to l1 if condition is true. */
2409 switch (insn & 0xf) {
2410 case 0: /* f */
2411 break;
2412 case 1: /* eq (=0) */
2413 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2414 break;
2415 case 2: /* ogt (=1) */
2416 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
2417 break;
2418 case 3: /* oge (=0 or =1) */
2419 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
2420 break;
2421 case 4: /* olt (=-1) */
2422 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
2423 break;
2424 case 5: /* ole (=-1 or =0) */
2425 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
2426 break;
2427 case 6: /* ogl (=-1 or =1) */
2428 tcg_gen_andi_i32(flag, flag, 1);
2429 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2430 break;
2431 case 7: /* or (=2) */
2432 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
2433 break;
2434 case 8: /* un (<2) */
2435 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
2436 break;
2437 case 9: /* ueq (=0 or =2) */
2438 tcg_gen_andi_i32(flag, flag, 1);
2439 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
2440 break;
2441 case 10: /* ugt (>0) */
2442 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
2443 break;
2444 case 11: /* uge (>=0) */
2445 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
2446 break;
2447 case 12: /* ult (=-1 or =2) */
2448 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
2449 break;
2450 case 13: /* ule (!=1) */
2451 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
2452 break;
2453 case 14: /* ne (!=0) */
2454 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
2455 break;
2456 case 15: /* t */
2457 tcg_gen_br(l1);
2458 break;
2460 gen_jmp_tb(s, 0, s->pc);
2461 gen_set_label(l1);
2462 gen_jmp_tb(s, 1, addr + offset);
2465 DISAS_INSN(frestore)
2467 M68kCPU *cpu = m68k_env_get_cpu(env);
2469 /* TODO: Implement frestore. */
2470 cpu_abort(CPU(cpu), "FRESTORE not implemented");
2473 DISAS_INSN(fsave)
2475 M68kCPU *cpu = m68k_env_get_cpu(env);
2477 /* TODO: Implement fsave. */
2478 cpu_abort(CPU(cpu), "FSAVE not implemented");
2481 static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
2483 TCGv tmp = tcg_temp_new();
2484 if (s->env->macsr & MACSR_FI) {
2485 if (upper)
2486 tcg_gen_andi_i32(tmp, val, 0xffff0000);
2487 else
2488 tcg_gen_shli_i32(tmp, val, 16);
2489 } else if (s->env->macsr & MACSR_SU) {
2490 if (upper)
2491 tcg_gen_sari_i32(tmp, val, 16);
2492 else
2493 tcg_gen_ext16s_i32(tmp, val);
2494 } else {
2495 if (upper)
2496 tcg_gen_shri_i32(tmp, val, 16);
2497 else
2498 tcg_gen_ext16u_i32(tmp, val);
2500 return tmp;
2503 static void gen_mac_clear_flags(void)
2505 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2506 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2509 DISAS_INSN(mac)
2511 TCGv rx;
2512 TCGv ry;
2513 uint16_t ext;
2514 int acc;
2515 TCGv tmp;
2516 TCGv addr;
2517 TCGv loadval;
2518 int dual;
2519 TCGv saved_flags;
2521 if (!s->done_mac) {
2522 s->mactmp = tcg_temp_new_i64();
2523 s->done_mac = 1;
2526 ext = cpu_lduw_code(env, s->pc);
2527 s->pc += 2;
2529 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2530 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
2531 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
2532 disas_undef(env, s, insn);
2533 return;
2535 if (insn & 0x30) {
2536 /* MAC with load. */
2537 tmp = gen_lea(env, s, insn, OS_LONG);
2538 addr = tcg_temp_new();
2539 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
2540 /* Load the value now to ensure correct exception behavior.
2541 Perform writeback after reading the MAC inputs. */
2542 loadval = gen_load(s, OS_LONG, addr, 0);
2544 acc ^= 1;
2545 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2546 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2547 } else {
2548 loadval = addr = NULL_QREG;
2549 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2550 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2553 gen_mac_clear_flags();
2554 #if 0
2555 l1 = -1;
2556 /* Disabled because conditional branches clobber temporary vars. */
2557 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2558 /* Skip the multiply if we know we will ignore it. */
2559 l1 = gen_new_label();
2560 tmp = tcg_temp_new();
2561 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
2562 gen_op_jmp_nz32(tmp, l1);
2564 #endif
2566 if ((ext & 0x0800) == 0) {
2567 /* Word. */
2568 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2569 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2571 if (s->env->macsr & MACSR_FI) {
2572 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
2573 } else {
2574 if (s->env->macsr & MACSR_SU)
2575 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
2576 else
2577 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
2578 switch ((ext >> 9) & 3) {
2579 case 1:
2580 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
2581 break;
2582 case 3:
2583 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
2584 break;
2588 if (dual) {
2589 /* Save the overflow flag from the multiply. */
2590 saved_flags = tcg_temp_new();
2591 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2592 } else {
2593 saved_flags = NULL_QREG;
2596 #if 0
2597 /* Disabled because conditional branches clobber temporary vars. */
2598 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2599 /* Skip the accumulate if the value is already saturated. */
2600 l1 = gen_new_label();
2601 tmp = tcg_temp_new();
2602 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2603 gen_op_jmp_nz32(tmp, l1);
2605 #endif
2607 if (insn & 0x100)
2608 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2609 else
2610 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2612 if (s->env->macsr & MACSR_FI)
2613 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2614 else if (s->env->macsr & MACSR_SU)
2615 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2616 else
2617 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2619 #if 0
2620 /* Disabled because conditional branches clobber temporary vars. */
2621 if (l1 != -1)
2622 gen_set_label(l1);
2623 #endif
2625 if (dual) {
2626 /* Dual accumulate variant. */
2627 acc = (ext >> 2) & 3;
2628 /* Restore the overflow flag from the multiplier. */
2629 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2630 #if 0
2631 /* Disabled because conditional branches clobber temporary vars. */
2632 if ((s->env->macsr & MACSR_OMC) != 0) {
2633 /* Skip the accumulate if the value is already saturated. */
2634 l1 = gen_new_label();
2635 tmp = tcg_temp_new();
2636 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
2637 gen_op_jmp_nz32(tmp, l1);
2639 #endif
2640 if (ext & 2)
2641 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
2642 else
2643 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
2644 if (s->env->macsr & MACSR_FI)
2645 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
2646 else if (s->env->macsr & MACSR_SU)
2647 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
2648 else
2649 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2650 #if 0
2651 /* Disabled because conditional branches clobber temporary vars. */
2652 if (l1 != -1)
2653 gen_set_label(l1);
2654 #endif
2656 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
2658 if (insn & 0x30) {
2659 TCGv rw;
2660 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2661 tcg_gen_mov_i32(rw, loadval);
2662 /* FIXME: Should address writeback happen with the masked or
2663 unmasked value? */
2664 switch ((insn >> 3) & 7) {
2665 case 3: /* Post-increment. */
2666 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
2667 break;
2668 case 4: /* Pre-decrement. */
2669 tcg_gen_mov_i32(AREG(insn, 0), addr);
2674 DISAS_INSN(from_mac)
2676 TCGv rx;
2677 TCGv_i64 acc;
2678 int accnum;
2680 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2681 accnum = (insn >> 9) & 3;
2682 acc = MACREG(accnum);
2683 if (s->env->macsr & MACSR_FI) {
2684 gen_helper_get_macf(rx, cpu_env, acc);
2685 } else if ((s->env->macsr & MACSR_OMC) == 0) {
2686 tcg_gen_trunc_i64_i32(rx, acc);
2687 } else if (s->env->macsr & MACSR_SU) {
2688 gen_helper_get_macs(rx, acc);
2689 } else {
2690 gen_helper_get_macu(rx, acc);
2692 if (insn & 0x40) {
2693 tcg_gen_movi_i64(acc, 0);
2694 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2698 DISAS_INSN(move_mac)
2700 /* FIXME: This can be done without a helper. */
2701 int src;
2702 TCGv dest;
2703 src = insn & 3;
2704 dest = tcg_const_i32((insn >> 9) & 3);
2705 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2706 gen_mac_clear_flags();
2707 gen_helper_mac_set_flags(cpu_env, dest);
2710 DISAS_INSN(from_macsr)
2712 TCGv reg;
2714 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2715 tcg_gen_mov_i32(reg, QREG_MACSR);
2718 DISAS_INSN(from_mask)
2720 TCGv reg;
2721 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2722 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
2725 DISAS_INSN(from_mext)
2727 TCGv reg;
2728 TCGv acc;
2729 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2730 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2731 if (s->env->macsr & MACSR_FI)
2732 gen_helper_get_mac_extf(reg, cpu_env, acc);
2733 else
2734 gen_helper_get_mac_exti(reg, cpu_env, acc);
2737 DISAS_INSN(macsr_to_ccr)
2739 tcg_gen_movi_i32(QREG_CC_X, 0);
2740 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
2741 s->cc_op = CC_OP_FLAGS;
2744 DISAS_INSN(to_mac)
2746 TCGv_i64 acc;
2747 TCGv val;
2748 int accnum;
2749 accnum = (insn >> 9) & 3;
2750 acc = MACREG(accnum);
2751 SRC_EA(env, val, OS_LONG, 0, NULL);
2752 if (s->env->macsr & MACSR_FI) {
2753 tcg_gen_ext_i32_i64(acc, val);
2754 tcg_gen_shli_i64(acc, acc, 8);
2755 } else if (s->env->macsr & MACSR_SU) {
2756 tcg_gen_ext_i32_i64(acc, val);
2757 } else {
2758 tcg_gen_extu_i32_i64(acc, val);
2760 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2761 gen_mac_clear_flags();
2762 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
2765 DISAS_INSN(to_macsr)
2767 TCGv val;
2768 SRC_EA(env, val, OS_LONG, 0, NULL);
2769 gen_helper_set_macsr(cpu_env, val);
2770 gen_lookup_tb(s);
2773 DISAS_INSN(to_mask)
2775 TCGv val;
2776 SRC_EA(env, val, OS_LONG, 0, NULL);
2777 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
2780 DISAS_INSN(to_mext)
2782 TCGv val;
2783 TCGv acc;
2784 SRC_EA(env, val, OS_LONG, 0, NULL);
2785 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
2786 if (s->env->macsr & MACSR_FI)
2787 gen_helper_set_mac_extf(cpu_env, val, acc);
2788 else if (s->env->macsr & MACSR_SU)
2789 gen_helper_set_mac_exts(cpu_env, val, acc);
2790 else
2791 gen_helper_set_mac_extu(cpu_env, val, acc);
2794 static disas_proc opcode_table[65536];
2796 static void
2797 register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2799 int i;
2800 int from;
2801 int to;
2803 /* Sanity check. All set bits must be included in the mask. */
2804 if (opcode & ~mask) {
2805 fprintf(stderr,
2806 "qemu internal error: bogus opcode definition %04x/%04x\n",
2807 opcode, mask);
2808 abort();
2810 /* This could probably be cleverer. For now just optimize the case where
2811 the top bits are known. */
2812 /* Find the first zero bit in the mask. */
2813 i = 0x8000;
2814 while ((i & mask) != 0)
2815 i >>= 1;
2816 /* Iterate over all combinations of this and lower bits. */
2817 if (i == 0)
2818 i = 1;
2819 else
2820 i <<= 1;
2821 from = opcode & ~(i - 1);
2822 to = from + i;
2823 for (i = from; i < to; i++) {
2824 if ((i & mask) == opcode)
2825 opcode_table[i] = proc;
2829 /* Register m68k opcode handlers. Order is important.
2830 Later insn override earlier ones. */
2831 void register_m68k_insns (CPUM68KState *env)
2833 #define INSN(name, opcode, mask, feature) do { \
2834 if (m68k_feature(env, M68K_FEATURE_##feature)) \
2835 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2836 } while(0)
2837 INSN(undef, 0000, 0000, CF_ISA_A);
2838 INSN(arith_im, 0080, fff8, CF_ISA_A);
2839 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
2840 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2841 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2842 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2843 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2844 INSN(arith_im, 0280, fff8, CF_ISA_A);
2845 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
2846 INSN(arith_im, 0480, fff8, CF_ISA_A);
2847 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
2848 INSN(arith_im, 0680, fff8, CF_ISA_A);
2849 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2850 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2851 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2852 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2853 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2854 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2855 INSN(move, 1000, f000, CF_ISA_A);
2856 INSN(move, 2000, f000, CF_ISA_A);
2857 INSN(move, 3000, f000, CF_ISA_A);
2858 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
2859 INSN(negx, 4080, fff8, CF_ISA_A);
2860 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2861 INSN(lea, 41c0, f1c0, CF_ISA_A);
2862 INSN(clr, 4200, ff00, CF_ISA_A);
2863 INSN(undef, 42c0, ffc0, CF_ISA_A);
2864 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2865 INSN(neg, 4480, fff8, CF_ISA_A);
2866 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2867 INSN(not, 4680, fff8, CF_ISA_A);
2868 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2869 INSN(pea, 4840, ffc0, CF_ISA_A);
2870 INSN(swap, 4840, fff8, CF_ISA_A);
2871 INSN(movem, 48c0, fbc0, CF_ISA_A);
2872 INSN(ext, 4880, fff8, CF_ISA_A);
2873 INSN(ext, 48c0, fff8, CF_ISA_A);
2874 INSN(ext, 49c0, fff8, CF_ISA_A);
2875 INSN(tst, 4a00, ff00, CF_ISA_A);
2876 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2877 INSN(halt, 4ac8, ffff, CF_ISA_A);
2878 INSN(pulse, 4acc, ffff, CF_ISA_A);
2879 INSN(illegal, 4afc, ffff, CF_ISA_A);
2880 INSN(mull, 4c00, ffc0, CF_ISA_A);
2881 INSN(divl, 4c40, ffc0, CF_ISA_A);
2882 INSN(sats, 4c80, fff8, CF_ISA_B);
2883 INSN(trap, 4e40, fff0, CF_ISA_A);
2884 INSN(link, 4e50, fff8, CF_ISA_A);
2885 INSN(unlk, 4e58, fff8, CF_ISA_A);
2886 INSN(move_to_usp, 4e60, fff8, USP);
2887 INSN(move_from_usp, 4e68, fff8, USP);
2888 INSN(nop, 4e71, ffff, CF_ISA_A);
2889 INSN(stop, 4e72, ffff, CF_ISA_A);
2890 INSN(rte, 4e73, ffff, CF_ISA_A);
2891 INSN(rts, 4e75, ffff, CF_ISA_A);
2892 INSN(movec, 4e7b, ffff, CF_ISA_A);
2893 INSN(jump, 4e80, ffc0, CF_ISA_A);
2894 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2895 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2896 INSN(scc, 50c0, f0f8, CF_ISA_A);
2897 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2898 INSN(tpf, 51f8, fff8, CF_ISA_A);
2900 /* Branch instructions. */
2901 INSN(branch, 6000, f000, CF_ISA_A);
2902 /* Disable long branch instructions, then add back the ones we want. */
2903 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2904 INSN(branch, 60ff, f0ff, CF_ISA_B);
2905 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2906 INSN(branch, 60ff, ffff, BRAL);
2908 INSN(moveq, 7000, f100, CF_ISA_A);
2909 INSN(mvzs, 7100, f100, CF_ISA_B);
2910 INSN(or, 8000, f000, CF_ISA_A);
2911 INSN(divw, 80c0, f0c0, CF_ISA_A);
2912 INSN(addsub, 9000, f000, CF_ISA_A);
2913 INSN(subx, 9180, f1f8, CF_ISA_A);
2914 INSN(suba, 91c0, f1c0, CF_ISA_A);
2916 INSN(undef_mac, a000, f000, CF_ISA_A);
2917 INSN(mac, a000, f100, CF_EMAC);
2918 INSN(from_mac, a180, f9b0, CF_EMAC);
2919 INSN(move_mac, a110, f9fc, CF_EMAC);
2920 INSN(from_macsr,a980, f9f0, CF_EMAC);
2921 INSN(from_mask, ad80, fff0, CF_EMAC);
2922 INSN(from_mext, ab80, fbf0, CF_EMAC);
2923 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2924 INSN(to_mac, a100, f9c0, CF_EMAC);
2925 INSN(to_macsr, a900, ffc0, CF_EMAC);
2926 INSN(to_mext, ab00, fbc0, CF_EMAC);
2927 INSN(to_mask, ad00, ffc0, CF_EMAC);
2929 INSN(mov3q, a140, f1c0, CF_ISA_B);
2930 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2931 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2932 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2933 INSN(cmp, b080, f1c0, CF_ISA_A);
2934 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2935 INSN(eor, b180, f1c0, CF_ISA_A);
2936 INSN(and, c000, f000, CF_ISA_A);
2937 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2938 INSN(addsub, d000, f000, CF_ISA_A);
2939 INSN(addx, d180, f1f8, CF_ISA_A);
2940 INSN(adda, d1c0, f1c0, CF_ISA_A);
2941 INSN(shift_im, e080, f0f0, CF_ISA_A);
2942 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2943 INSN(undef_fpu, f000, f000, CF_ISA_A);
2944 INSN(fpu, f200, ffc0, CF_FPU);
2945 INSN(fbcc, f280, ffc0, CF_FPU);
2946 INSN(frestore, f340, ffc0, CF_FPU);
2947 INSN(fsave, f340, ffc0, CF_FPU);
2948 INSN(intouch, f340, ffc0, CF_ISA_A);
2949 INSN(cpushl, f428, ff38, CF_ISA_A);
2950 INSN(wddata, fb00, ff00, CF_ISA_A);
2951 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
2952 #undef INSN
2955 /* ??? Some of this implementation is not exception safe. We should always
2956 write back the result to memory before setting the condition codes. */
2957 static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
2959 uint16_t insn;
2961 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
2962 tcg_gen_debug_insn_start(s->pc);
2965 insn = cpu_lduw_code(env, s->pc);
2966 s->pc += 2;
2968 opcode_table[insn](env, s, insn);
2971 /* generate intermediate code for basic block 'tb'. */
2972 static inline void
2973 gen_intermediate_code_internal(M68kCPU *cpu, TranslationBlock *tb,
2974 bool search_pc)
2976 CPUState *cs = CPU(cpu);
2977 CPUM68KState *env = &cpu->env;
2978 DisasContext dc1, *dc = &dc1;
2979 uint16_t *gen_opc_end;
2980 CPUBreakpoint *bp;
2981 int j, lj;
2982 target_ulong pc_start;
2983 int pc_offset;
2984 int num_insns;
2985 int max_insns;
2987 /* generate intermediate code */
2988 pc_start = tb->pc;
2990 dc->tb = tb;
2992 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
2994 dc->env = env;
2995 dc->is_jmp = DISAS_NEXT;
2996 dc->pc = pc_start;
2997 dc->cc_op = CC_OP_DYNAMIC;
2998 dc->singlestep_enabled = cs->singlestep_enabled;
2999 dc->fpcr = env->fpcr;
3000 dc->user = (env->sr & SR_S) == 0;
3001 dc->is_mem = 0;
3002 dc->done_mac = 0;
3003 lj = -1;
3004 num_insns = 0;
3005 max_insns = tb->cflags & CF_COUNT_MASK;
3006 if (max_insns == 0)
3007 max_insns = CF_COUNT_MASK;
3009 gen_tb_start();
3010 do {
3011 pc_offset = dc->pc - pc_start;
3012 gen_throws_exception = NULL;
3013 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
3014 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
3015 if (bp->pc == dc->pc) {
3016 gen_exception(dc, dc->pc, EXCP_DEBUG);
3017 dc->is_jmp = DISAS_JUMP;
3018 break;
3021 if (dc->is_jmp)
3022 break;
3024 if (search_pc) {
3025 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
3026 if (lj < j) {
3027 lj++;
3028 while (lj < j)
3029 tcg_ctx.gen_opc_instr_start[lj++] = 0;
3031 tcg_ctx.gen_opc_pc[lj] = dc->pc;
3032 tcg_ctx.gen_opc_instr_start[lj] = 1;
3033 tcg_ctx.gen_opc_icount[lj] = num_insns;
3035 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
3036 gen_io_start();
3037 dc->insn_pc = dc->pc;
3038 disas_m68k_insn(env, dc);
3039 num_insns++;
3040 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
3041 !cs->singlestep_enabled &&
3042 !singlestep &&
3043 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3044 num_insns < max_insns);
3046 if (tb->cflags & CF_LAST_IO)
3047 gen_io_end();
3048 if (unlikely(cs->singlestep_enabled)) {
3049 /* Make sure the pc is updated, and raise a debug exception. */
3050 if (!dc->is_jmp) {
3051 gen_flush_cc_op(dc);
3052 tcg_gen_movi_i32(QREG_PC, dc->pc);
3054 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
3055 } else {
3056 switch(dc->is_jmp) {
3057 case DISAS_NEXT:
3058 gen_flush_cc_op(dc);
3059 gen_jmp_tb(dc, 0, dc->pc);
3060 break;
3061 default:
3062 case DISAS_JUMP:
3063 case DISAS_UPDATE:
3064 gen_flush_cc_op(dc);
3065 /* indicate that the hash table must be used to find the next TB */
3066 tcg_gen_exit_tb(0);
3067 break;
3068 case DISAS_TB_JUMP:
3069 /* nothing more to generate */
3070 break;
3073 gen_tb_end(tb, num_insns);
3074 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
3076 #ifdef DEBUG_DISAS
3077 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
3078 qemu_log("----------------\n");
3079 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3080 log_target_disas(env, pc_start, dc->pc - pc_start, 0);
3081 qemu_log("\n");
3083 #endif
3084 if (search_pc) {
3085 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
3086 lj++;
3087 while (lj <= j)
3088 tcg_ctx.gen_opc_instr_start[lj++] = 0;
3089 } else {
3090 tb->size = dc->pc - pc_start;
3091 tb->icount = num_insns;
3094 //optimize_flags();
3095 //expand_target_qops();
3098 void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
3100 gen_intermediate_code_internal(m68k_env_get_cpu(env), tb, false);
3103 void gen_intermediate_code_pc(CPUM68KState *env, TranslationBlock *tb)
3105 gen_intermediate_code_internal(m68k_env_get_cpu(env), tb, true);
3108 void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
3109 int flags)
3111 M68kCPU *cpu = M68K_CPU(cs);
3112 CPUM68KState *env = &cpu->env;
3113 int i;
3114 uint16_t sr;
3115 CPU_DoubleU u;
3116 for (i = 0; i < 8; i++)
3118 u.d = env->fregs[i];
3119 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3120 i, env->dregs[i], i, env->aregs[i],
3121 i, u.l.upper, u.l.lower, *(double *)&u.d);
3123 cpu_fprintf (f, "PC = %08x ", env->pc);
3124 sr = env->sr;
3125 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3126 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3127 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
3128 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
3131 void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb, int pc_pos)
3133 env->pc = tcg_ctx.gen_opc_pc[pc_pos];