]> git.proxmox.com Git - mirror_qemu.git/blame - target-m68k/translate.c
target-m68k: fix DEBUG_DISPATCH
[mirror_qemu.git] / target-m68k / translate.c
CommitLineData
e6e5906b
PB
1/*
2 * m68k translation
5fafdf24 3 *
0633879f 4 * Copyright (c) 2005-2007 CodeSourcery
e6e5906b
PB
5 * Written by Paul Brook
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
8167ee88 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
e6e5906b 19 */
e6e5906b 20
d8416665 21#include "qemu/osdep.h"
e6e5906b 22#include "cpu.h"
76cad711 23#include "disas/disas.h"
63c91552 24#include "exec/exec-all.h"
57fec1fe 25#include "tcg-op.h"
1de7afc9 26#include "qemu/log.h"
f08b6170 27#include "exec/cpu_ldst.h"
e1f3808e 28
2ef6175a
RH
29#include "exec/helper-proto.h"
30#include "exec/helper-gen.h"
e6e5906b 31
a7e30d84 32#include "trace-tcg.h"
508127e2 33#include "exec/log.h"
a7e30d84
LV
34
35
0633879f
PB
36//#define DEBUG_DISPATCH 1
37
815a6742 38/* Fake floating point. */
815a6742 39#define tcg_gen_mov_f64 tcg_gen_mov_i64
815a6742 40#define tcg_gen_qemu_ldf64 tcg_gen_qemu_ld64
815a6742 41#define tcg_gen_qemu_stf64 tcg_gen_qemu_st64
815a6742 42
e1f3808e 43#define DEFO32(name, offset) static TCGv QREG_##name;
a7812ae4
PB
44#define DEFO64(name, offset) static TCGv_i64 QREG_##name;
45#define DEFF64(name, offset) static TCGv_i64 QREG_##name;
e1f3808e
PB
46#include "qregs.def"
47#undef DEFO32
48#undef DEFO64
49#undef DEFF64
50
259186a7 51static TCGv_i32 cpu_halted;
27103424 52static TCGv_i32 cpu_exception_index;
259186a7 53
1bcea73e 54static TCGv_env cpu_env;
e1f3808e
PB
55
56static char cpu_reg_names[3*8*3 + 5*4];
57static TCGv cpu_dregs[8];
58static TCGv cpu_aregs[8];
a7812ae4
PB
59static TCGv_i64 cpu_fregs[8];
60static TCGv_i64 cpu_macc[4];
e1f3808e
PB
61
62#define DREG(insn, pos) cpu_dregs[((insn) >> (pos)) & 7]
63#define AREG(insn, pos) cpu_aregs[((insn) >> (pos)) & 7]
64#define FREG(insn, pos) cpu_fregs[((insn) >> (pos)) & 7]
65#define MACREG(acc) cpu_macc[acc]
66#define QREG_SP cpu_aregs[7]
67
68static TCGv NULL_QREG;
a7812ae4 69#define IS_NULL_QREG(t) (TCGV_EQUAL(t, NULL_QREG))
e1f3808e
PB
70/* Used to distinguish stores from bad addressing modes. */
71static TCGv store_dummy;
72
022c62cb 73#include "exec/gen-icount.h"
2e70f6ef 74
e1f3808e
PB
75void m68k_tcg_init(void)
76{
77 char *p;
78 int i;
79
e1ccc054 80 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
7c255043 81 tcg_ctx.tcg_env = cpu_env;
e1ccc054
RH
82
83#define DEFO32(name, offset) \
84 QREG_##name = tcg_global_mem_new_i32(cpu_env, \
85 offsetof(CPUM68KState, offset), #name);
86#define DEFO64(name, offset) \
87 QREG_##name = tcg_global_mem_new_i64(cpu_env, \
88 offsetof(CPUM68KState, offset), #name);
89#define DEFF64(name, offset) DEFO64(name, offset)
e1f3808e
PB
90#include "qregs.def"
91#undef DEFO32
92#undef DEFO64
93#undef DEFF64
94
e1ccc054 95 cpu_halted = tcg_global_mem_new_i32(cpu_env,
259186a7
AF
96 -offsetof(M68kCPU, env) +
97 offsetof(CPUState, halted), "HALTED");
e1ccc054 98 cpu_exception_index = tcg_global_mem_new_i32(cpu_env,
27103424
AF
99 -offsetof(M68kCPU, env) +
100 offsetof(CPUState, exception_index),
101 "EXCEPTION");
259186a7 102
e1f3808e
PB
103 p = cpu_reg_names;
104 for (i = 0; i < 8; i++) {
105 sprintf(p, "D%d", i);
e1ccc054 106 cpu_dregs[i] = tcg_global_mem_new(cpu_env,
e1f3808e
PB
107 offsetof(CPUM68KState, dregs[i]), p);
108 p += 3;
109 sprintf(p, "A%d", i);
e1ccc054 110 cpu_aregs[i] = tcg_global_mem_new(cpu_env,
e1f3808e
PB
111 offsetof(CPUM68KState, aregs[i]), p);
112 p += 3;
113 sprintf(p, "F%d", i);
e1ccc054 114 cpu_fregs[i] = tcg_global_mem_new_i64(cpu_env,
e1f3808e
PB
115 offsetof(CPUM68KState, fregs[i]), p);
116 p += 3;
117 }
118 for (i = 0; i < 4; i++) {
119 sprintf(p, "ACC%d", i);
e1ccc054 120 cpu_macc[i] = tcg_global_mem_new_i64(cpu_env,
e1f3808e
PB
121 offsetof(CPUM68KState, macc[i]), p);
122 p += 5;
123 }
124
e1ccc054
RH
125 NULL_QREG = tcg_global_mem_new(cpu_env, -4, "NULL");
126 store_dummy = tcg_global_mem_new(cpu_env, -8, "NULL");
e1f3808e
PB
127}
128
e6e5906b
PB
129/* internal defines */
130typedef struct DisasContext {
e6dbd3b3 131 CPUM68KState *env;
510ff0b7 132 target_ulong insn_pc; /* Start of the current instruction. */
e6e5906b
PB
133 target_ulong pc;
134 int is_jmp;
135 int cc_op;
0633879f 136 int user;
e6e5906b
PB
137 uint32_t fpcr;
138 struct TranslationBlock *tb;
139 int singlestep_enabled;
a7812ae4
PB
140 TCGv_i64 mactmp;
141 int done_mac;
e6e5906b
PB
142} DisasContext;
143
144#define DISAS_JUMP_NEXT 4
145
0633879f
PB
146#if defined(CONFIG_USER_ONLY)
147#define IS_USER(s) 1
148#else
149#define IS_USER(s) s->user
150#endif
151
e6e5906b
PB
152/* XXX: move that elsewhere */
153/* ??? Fix exceptions. */
154static void *gen_throws_exception;
155#define gen_last_qop NULL
156
e6e5906b
PB
157#define OS_BYTE 0
158#define OS_WORD 1
159#define OS_LONG 2
160#define OS_SINGLE 4
161#define OS_DOUBLE 5
162
d4d79bb1 163typedef void (*disas_proc)(CPUM68KState *env, DisasContext *s, uint16_t insn);
e6e5906b 164
0633879f 165#ifdef DEBUG_DISPATCH
d4d79bb1
BS
166#define DISAS_INSN(name) \
167 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
168 uint16_t insn); \
169 static void disas_##name(CPUM68KState *env, DisasContext *s, \
170 uint16_t insn) \
171 { \
172 qemu_log("Dispatch " #name "\n"); \
a1ff1930 173 real_disas_##name(env, s, insn); \
d4d79bb1
BS
174 } \
175 static void real_disas_##name(CPUM68KState *env, DisasContext *s, \
176 uint16_t insn)
0633879f 177#else
d4d79bb1
BS
178#define DISAS_INSN(name) \
179 static void disas_##name(CPUM68KState *env, DisasContext *s, \
180 uint16_t insn)
0633879f 181#endif
e6e5906b
PB
182
183/* Generate a load from the specified address. Narrow values are
184 sign extended to full register width. */
e1f3808e 185static inline TCGv gen_load(DisasContext * s, int opsize, TCGv addr, int sign)
e6e5906b 186{
e1f3808e
PB
187 TCGv tmp;
188 int index = IS_USER(s);
a7812ae4 189 tmp = tcg_temp_new_i32();
e6e5906b
PB
190 switch(opsize) {
191 case OS_BYTE:
e6e5906b 192 if (sign)
e1f3808e 193 tcg_gen_qemu_ld8s(tmp, addr, index);
e6e5906b 194 else
e1f3808e 195 tcg_gen_qemu_ld8u(tmp, addr, index);
e6e5906b
PB
196 break;
197 case OS_WORD:
e6e5906b 198 if (sign)
e1f3808e 199 tcg_gen_qemu_ld16s(tmp, addr, index);
e6e5906b 200 else
e1f3808e 201 tcg_gen_qemu_ld16u(tmp, addr, index);
e6e5906b
PB
202 break;
203 case OS_LONG:
e6e5906b 204 case OS_SINGLE:
a7812ae4 205 tcg_gen_qemu_ld32u(tmp, addr, index);
e6e5906b
PB
206 break;
207 default:
7372c2b9 208 g_assert_not_reached();
e6e5906b
PB
209 }
210 gen_throws_exception = gen_last_qop;
211 return tmp;
212}
213
a7812ae4
PB
214static inline TCGv_i64 gen_load64(DisasContext * s, TCGv addr)
215{
216 TCGv_i64 tmp;
217 int index = IS_USER(s);
a7812ae4
PB
218 tmp = tcg_temp_new_i64();
219 tcg_gen_qemu_ldf64(tmp, addr, index);
220 gen_throws_exception = gen_last_qop;
221 return tmp;
222}
223
e6e5906b 224/* Generate a store. */
e1f3808e 225static inline void gen_store(DisasContext *s, int opsize, TCGv addr, TCGv val)
e6e5906b 226{
e1f3808e 227 int index = IS_USER(s);
e6e5906b
PB
228 switch(opsize) {
229 case OS_BYTE:
e1f3808e 230 tcg_gen_qemu_st8(val, addr, index);
e6e5906b
PB
231 break;
232 case OS_WORD:
e1f3808e 233 tcg_gen_qemu_st16(val, addr, index);
e6e5906b
PB
234 break;
235 case OS_LONG:
e6e5906b 236 case OS_SINGLE:
a7812ae4 237 tcg_gen_qemu_st32(val, addr, index);
e6e5906b
PB
238 break;
239 default:
7372c2b9 240 g_assert_not_reached();
e6e5906b
PB
241 }
242 gen_throws_exception = gen_last_qop;
243}
244
a7812ae4
PB
245static inline void gen_store64(DisasContext *s, TCGv addr, TCGv_i64 val)
246{
247 int index = IS_USER(s);
a7812ae4
PB
248 tcg_gen_qemu_stf64(val, addr, index);
249 gen_throws_exception = gen_last_qop;
250}
251
e1f3808e
PB
252typedef enum {
253 EA_STORE,
254 EA_LOADU,
255 EA_LOADS
256} ea_what;
257
e6e5906b
PB
258/* Generate an unsigned load if VAL is 0 a signed load if val is -1,
259 otherwise generate a store. */
e1f3808e
PB
260static TCGv gen_ldst(DisasContext *s, int opsize, TCGv addr, TCGv val,
261 ea_what what)
e6e5906b 262{
e1f3808e 263 if (what == EA_STORE) {
0633879f 264 gen_store(s, opsize, addr, val);
e1f3808e 265 return store_dummy;
e6e5906b 266 } else {
e1f3808e 267 return gen_load(s, opsize, addr, what == EA_LOADS);
e6e5906b
PB
268 }
269}
270
e6dbd3b3 271/* Read a 32-bit immediate constant. */
d4d79bb1 272static inline uint32_t read_im32(CPUM68KState *env, DisasContext *s)
e6dbd3b3
PB
273{
274 uint32_t im;
d4d79bb1 275 im = ((uint32_t)cpu_lduw_code(env, s->pc)) << 16;
e6dbd3b3 276 s->pc += 2;
d4d79bb1 277 im |= cpu_lduw_code(env, s->pc);
e6dbd3b3
PB
278 s->pc += 2;
279 return im;
280}
281
282/* Calculate and address index. */
e1f3808e 283static TCGv gen_addr_index(uint16_t ext, TCGv tmp)
e6dbd3b3 284{
e1f3808e 285 TCGv add;
e6dbd3b3
PB
286 int scale;
287
288 add = (ext & 0x8000) ? AREG(ext, 12) : DREG(ext, 12);
289 if ((ext & 0x800) == 0) {
e1f3808e 290 tcg_gen_ext16s_i32(tmp, add);
e6dbd3b3
PB
291 add = tmp;
292 }
293 scale = (ext >> 9) & 3;
294 if (scale != 0) {
e1f3808e 295 tcg_gen_shli_i32(tmp, add, scale);
e6dbd3b3
PB
296 add = tmp;
297 }
298 return add;
299}
300
e1f3808e
PB
301/* Handle a base + index + displacement effective addresss.
302 A NULL_QREG base means pc-relative. */
a4356126 303static TCGv gen_lea_indexed(CPUM68KState *env, DisasContext *s, TCGv base)
e6e5906b 304{
e6e5906b
PB
305 uint32_t offset;
306 uint16_t ext;
e1f3808e
PB
307 TCGv add;
308 TCGv tmp;
e6dbd3b3 309 uint32_t bd, od;
e6e5906b
PB
310
311 offset = s->pc;
d4d79bb1 312 ext = cpu_lduw_code(env, s->pc);
e6e5906b 313 s->pc += 2;
e6dbd3b3
PB
314
315 if ((ext & 0x800) == 0 && !m68k_feature(s->env, M68K_FEATURE_WORD_INDEX))
e1f3808e 316 return NULL_QREG;
e6dbd3b3
PB
317
318 if (ext & 0x100) {
319 /* full extension word format */
320 if (!m68k_feature(s->env, M68K_FEATURE_EXT_FULL))
e1f3808e 321 return NULL_QREG;
e6dbd3b3
PB
322
323 if ((ext & 0x30) > 0x10) {
324 /* base displacement */
325 if ((ext & 0x30) == 0x20) {
d4d79bb1 326 bd = (int16_t)cpu_lduw_code(env, s->pc);
e6dbd3b3
PB
327 s->pc += 2;
328 } else {
d4d79bb1 329 bd = read_im32(env, s);
e6dbd3b3
PB
330 }
331 } else {
332 bd = 0;
333 }
a7812ae4 334 tmp = tcg_temp_new();
e6dbd3b3
PB
335 if ((ext & 0x44) == 0) {
336 /* pre-index */
337 add = gen_addr_index(ext, tmp);
338 } else {
e1f3808e 339 add = NULL_QREG;
e6dbd3b3
PB
340 }
341 if ((ext & 0x80) == 0) {
342 /* base not suppressed */
e1f3808e 343 if (IS_NULL_QREG(base)) {
351326a6 344 base = tcg_const_i32(offset + bd);
e6dbd3b3
PB
345 bd = 0;
346 }
e1f3808e
PB
347 if (!IS_NULL_QREG(add)) {
348 tcg_gen_add_i32(tmp, add, base);
e6dbd3b3
PB
349 add = tmp;
350 } else {
351 add = base;
352 }
353 }
e1f3808e 354 if (!IS_NULL_QREG(add)) {
e6dbd3b3 355 if (bd != 0) {
e1f3808e 356 tcg_gen_addi_i32(tmp, add, bd);
e6dbd3b3
PB
357 add = tmp;
358 }
359 } else {
351326a6 360 add = tcg_const_i32(bd);
e6dbd3b3
PB
361 }
362 if ((ext & 3) != 0) {
363 /* memory indirect */
364 base = gen_load(s, OS_LONG, add, 0);
365 if ((ext & 0x44) == 4) {
366 add = gen_addr_index(ext, tmp);
e1f3808e 367 tcg_gen_add_i32(tmp, add, base);
e6dbd3b3
PB
368 add = tmp;
369 } else {
370 add = base;
371 }
372 if ((ext & 3) > 1) {
373 /* outer displacement */
374 if ((ext & 3) == 2) {
d4d79bb1 375 od = (int16_t)cpu_lduw_code(env, s->pc);
e6dbd3b3
PB
376 s->pc += 2;
377 } else {
d4d79bb1 378 od = read_im32(env, s);
e6dbd3b3
PB
379 }
380 } else {
381 od = 0;
382 }
383 if (od != 0) {
e1f3808e 384 tcg_gen_addi_i32(tmp, add, od);
e6dbd3b3
PB
385 add = tmp;
386 }
387 }
e6e5906b 388 } else {
e6dbd3b3 389 /* brief extension word format */
a7812ae4 390 tmp = tcg_temp_new();
e6dbd3b3 391 add = gen_addr_index(ext, tmp);
e1f3808e
PB
392 if (!IS_NULL_QREG(base)) {
393 tcg_gen_add_i32(tmp, add, base);
e6dbd3b3 394 if ((int8_t)ext)
e1f3808e 395 tcg_gen_addi_i32(tmp, tmp, (int8_t)ext);
e6dbd3b3 396 } else {
e1f3808e 397 tcg_gen_addi_i32(tmp, add, offset + (int8_t)ext);
e6dbd3b3
PB
398 }
399 add = tmp;
e6e5906b 400 }
e6dbd3b3 401 return add;
e6e5906b
PB
402}
403
e6e5906b
PB
404/* Update the CPU env CC_OP state. */
405static inline void gen_flush_cc_op(DisasContext *s)
406{
407 if (s->cc_op != CC_OP_DYNAMIC)
e1f3808e 408 tcg_gen_movi_i32(QREG_CC_OP, s->cc_op);
e6e5906b
PB
409}
410
411/* Evaluate all the CC flags. */
412static inline void gen_flush_flags(DisasContext *s)
413{
414 if (s->cc_op == CC_OP_FLAGS)
415 return;
0cf5c677 416 gen_flush_cc_op(s);
e1f3808e 417 gen_helper_flush_flags(cpu_env, QREG_CC_OP);
e6e5906b
PB
418 s->cc_op = CC_OP_FLAGS;
419}
420
e1f3808e
PB
421static void gen_logic_cc(DisasContext *s, TCGv val)
422{
423 tcg_gen_mov_i32(QREG_CC_DEST, val);
424 s->cc_op = CC_OP_LOGIC;
425}
426
427static void gen_update_cc_add(TCGv dest, TCGv src)
428{
429 tcg_gen_mov_i32(QREG_CC_DEST, dest);
430 tcg_gen_mov_i32(QREG_CC_SRC, src);
431}
432
e6e5906b
PB
433static inline int opsize_bytes(int opsize)
434{
435 switch (opsize) {
436 case OS_BYTE: return 1;
437 case OS_WORD: return 2;
438 case OS_LONG: return 4;
439 case OS_SINGLE: return 4;
440 case OS_DOUBLE: return 8;
441 default:
7372c2b9 442 g_assert_not_reached();
e6e5906b
PB
443 }
444}
445
446/* Assign value to a register. If the width is less than the register width
447 only the low part of the register is set. */
e1f3808e 448static void gen_partset_reg(int opsize, TCGv reg, TCGv val)
e6e5906b 449{
e1f3808e 450 TCGv tmp;
e6e5906b
PB
451 switch (opsize) {
452 case OS_BYTE:
e1f3808e 453 tcg_gen_andi_i32(reg, reg, 0xffffff00);
a7812ae4 454 tmp = tcg_temp_new();
e1f3808e
PB
455 tcg_gen_ext8u_i32(tmp, val);
456 tcg_gen_or_i32(reg, reg, tmp);
e6e5906b
PB
457 break;
458 case OS_WORD:
e1f3808e 459 tcg_gen_andi_i32(reg, reg, 0xffff0000);
a7812ae4 460 tmp = tcg_temp_new();
e1f3808e
PB
461 tcg_gen_ext16u_i32(tmp, val);
462 tcg_gen_or_i32(reg, reg, tmp);
e6e5906b
PB
463 break;
464 case OS_LONG:
e6e5906b 465 case OS_SINGLE:
a7812ae4 466 tcg_gen_mov_i32(reg, val);
e6e5906b
PB
467 break;
468 default:
7372c2b9 469 g_assert_not_reached();
e6e5906b
PB
470 }
471}
472
473/* Sign or zero extend a value. */
e1f3808e 474static inline TCGv gen_extend(TCGv val, int opsize, int sign)
e6e5906b 475{
e1f3808e 476 TCGv tmp;
e6e5906b
PB
477
478 switch (opsize) {
479 case OS_BYTE:
a7812ae4 480 tmp = tcg_temp_new();
e6e5906b 481 if (sign)
e1f3808e 482 tcg_gen_ext8s_i32(tmp, val);
e6e5906b 483 else
e1f3808e 484 tcg_gen_ext8u_i32(tmp, val);
e6e5906b
PB
485 break;
486 case OS_WORD:
a7812ae4 487 tmp = tcg_temp_new();
e6e5906b 488 if (sign)
e1f3808e 489 tcg_gen_ext16s_i32(tmp, val);
e6e5906b 490 else
e1f3808e 491 tcg_gen_ext16u_i32(tmp, val);
e6e5906b
PB
492 break;
493 case OS_LONG:
e6e5906b 494 case OS_SINGLE:
a7812ae4 495 tmp = val;
e6e5906b
PB
496 break;
497 default:
7372c2b9 498 g_assert_not_reached();
e6e5906b
PB
499 }
500 return tmp;
501}
502
503/* Generate code for an "effective address". Does not adjust the base
1addc7c5 504 register for autoincrement addressing modes. */
d4d79bb1
BS
505static TCGv gen_lea(CPUM68KState *env, DisasContext *s, uint16_t insn,
506 int opsize)
e6e5906b 507{
e1f3808e
PB
508 TCGv reg;
509 TCGv tmp;
e6e5906b
PB
510 uint16_t ext;
511 uint32_t offset;
512
e6e5906b
PB
513 switch ((insn >> 3) & 7) {
514 case 0: /* Data register direct. */
515 case 1: /* Address register direct. */
e1f3808e 516 return NULL_QREG;
e6e5906b
PB
517 case 2: /* Indirect register */
518 case 3: /* Indirect postincrement. */
e1f3808e 519 return AREG(insn, 0);
e6e5906b 520 case 4: /* Indirect predecrememnt. */
e1f3808e 521 reg = AREG(insn, 0);
a7812ae4 522 tmp = tcg_temp_new();
e1f3808e 523 tcg_gen_subi_i32(tmp, reg, opsize_bytes(opsize));
e6e5906b
PB
524 return tmp;
525 case 5: /* Indirect displacement. */
e1f3808e 526 reg = AREG(insn, 0);
a7812ae4 527 tmp = tcg_temp_new();
d4d79bb1 528 ext = cpu_lduw_code(env, s->pc);
e6e5906b 529 s->pc += 2;
e1f3808e 530 tcg_gen_addi_i32(tmp, reg, (int16_t)ext);
e6e5906b
PB
531 return tmp;
532 case 6: /* Indirect index + displacement. */
e1f3808e 533 reg = AREG(insn, 0);
a4356126 534 return gen_lea_indexed(env, s, reg);
e6e5906b 535 case 7: /* Other */
e1f3808e 536 switch (insn & 7) {
e6e5906b 537 case 0: /* Absolute short. */
d4d79bb1 538 offset = cpu_ldsw_code(env, s->pc);
e6e5906b 539 s->pc += 2;
351326a6 540 return tcg_const_i32(offset);
e6e5906b 541 case 1: /* Absolute long. */
d4d79bb1 542 offset = read_im32(env, s);
351326a6 543 return tcg_const_i32(offset);
e6e5906b 544 case 2: /* pc displacement */
e6e5906b 545 offset = s->pc;
d4d79bb1 546 offset += cpu_ldsw_code(env, s->pc);
e6e5906b 547 s->pc += 2;
351326a6 548 return tcg_const_i32(offset);
e6e5906b 549 case 3: /* pc index+displacement. */
a4356126 550 return gen_lea_indexed(env, s, NULL_QREG);
e6e5906b
PB
551 case 4: /* Immediate. */
552 default:
e1f3808e 553 return NULL_QREG;
e6e5906b
PB
554 }
555 }
556 /* Should never happen. */
e1f3808e 557 return NULL_QREG;
e6e5906b
PB
558}
559
560/* Helper function for gen_ea. Reuse the computed address between the
561 for read/write operands. */
d4d79bb1
BS
562static inline TCGv gen_ea_once(CPUM68KState *env, DisasContext *s,
563 uint16_t insn, int opsize, TCGv val,
564 TCGv *addrp, ea_what what)
e6e5906b 565{
e1f3808e 566 TCGv tmp;
e6e5906b 567
e1f3808e 568 if (addrp && what == EA_STORE) {
e6e5906b
PB
569 tmp = *addrp;
570 } else {
d4d79bb1 571 tmp = gen_lea(env, s, insn, opsize);
e1f3808e
PB
572 if (IS_NULL_QREG(tmp))
573 return tmp;
e6e5906b
PB
574 if (addrp)
575 *addrp = tmp;
576 }
e1f3808e 577 return gen_ldst(s, opsize, tmp, val, what);
e6e5906b
PB
578}
579
f38f7a84 580/* Generate code to load/store a value from/into an EA. If VAL > 0 this is
e6e5906b
PB
581 a write otherwise it is a read (0 == sign extend, -1 == zero extend).
582 ADDRP is non-null for readwrite operands. */
d4d79bb1
BS
583static TCGv gen_ea(CPUM68KState *env, DisasContext *s, uint16_t insn,
584 int opsize, TCGv val, TCGv *addrp, ea_what what)
e6e5906b 585{
e1f3808e
PB
586 TCGv reg;
587 TCGv result;
e6e5906b
PB
588 uint32_t offset;
589
e6e5906b
PB
590 switch ((insn >> 3) & 7) {
591 case 0: /* Data register direct. */
e1f3808e
PB
592 reg = DREG(insn, 0);
593 if (what == EA_STORE) {
e6e5906b 594 gen_partset_reg(opsize, reg, val);
e1f3808e 595 return store_dummy;
e6e5906b 596 } else {
e1f3808e 597 return gen_extend(reg, opsize, what == EA_LOADS);
e6e5906b
PB
598 }
599 case 1: /* Address register direct. */
e1f3808e
PB
600 reg = AREG(insn, 0);
601 if (what == EA_STORE) {
602 tcg_gen_mov_i32(reg, val);
603 return store_dummy;
e6e5906b 604 } else {
e1f3808e 605 return gen_extend(reg, opsize, what == EA_LOADS);
e6e5906b
PB
606 }
607 case 2: /* Indirect register */
e1f3808e
PB
608 reg = AREG(insn, 0);
609 return gen_ldst(s, opsize, reg, val, what);
e6e5906b 610 case 3: /* Indirect postincrement. */
e1f3808e
PB
611 reg = AREG(insn, 0);
612 result = gen_ldst(s, opsize, reg, val, what);
e6e5906b
PB
613 /* ??? This is not exception safe. The instruction may still
614 fault after this point. */
e1f3808e
PB
615 if (what == EA_STORE || !addrp)
616 tcg_gen_addi_i32(reg, reg, opsize_bytes(opsize));
e6e5906b
PB
617 return result;
618 case 4: /* Indirect predecrememnt. */
619 {
e1f3808e
PB
620 TCGv tmp;
621 if (addrp && what == EA_STORE) {
e6e5906b
PB
622 tmp = *addrp;
623 } else {
d4d79bb1 624 tmp = gen_lea(env, s, insn, opsize);
e1f3808e
PB
625 if (IS_NULL_QREG(tmp))
626 return tmp;
e6e5906b
PB
627 if (addrp)
628 *addrp = tmp;
629 }
e1f3808e 630 result = gen_ldst(s, opsize, tmp, val, what);
e6e5906b
PB
631 /* ??? This is not exception safe. The instruction may still
632 fault after this point. */
e1f3808e
PB
633 if (what == EA_STORE || !addrp) {
634 reg = AREG(insn, 0);
635 tcg_gen_mov_i32(reg, tmp);
e6e5906b
PB
636 }
637 }
638 return result;
639 case 5: /* Indirect displacement. */
640 case 6: /* Indirect index + displacement. */
d4d79bb1 641 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
e6e5906b 642 case 7: /* Other */
e1f3808e 643 switch (insn & 7) {
e6e5906b
PB
644 case 0: /* Absolute short. */
645 case 1: /* Absolute long. */
646 case 2: /* pc displacement */
647 case 3: /* pc index+displacement. */
d4d79bb1 648 return gen_ea_once(env, s, insn, opsize, val, addrp, what);
e6e5906b
PB
649 case 4: /* Immediate. */
650 /* Sign extend values for consistency. */
651 switch (opsize) {
652 case OS_BYTE:
31871141 653 if (what == EA_LOADS) {
d4d79bb1 654 offset = cpu_ldsb_code(env, s->pc + 1);
31871141 655 } else {
d4d79bb1 656 offset = cpu_ldub_code(env, s->pc + 1);
31871141 657 }
e6e5906b
PB
658 s->pc += 2;
659 break;
660 case OS_WORD:
31871141 661 if (what == EA_LOADS) {
d4d79bb1 662 offset = cpu_ldsw_code(env, s->pc);
31871141 663 } else {
d4d79bb1 664 offset = cpu_lduw_code(env, s->pc);
31871141 665 }
e6e5906b
PB
666 s->pc += 2;
667 break;
668 case OS_LONG:
d4d79bb1 669 offset = read_im32(env, s);
e6e5906b
PB
670 break;
671 default:
7372c2b9 672 g_assert_not_reached();
e6e5906b 673 }
e1f3808e 674 return tcg_const_i32(offset);
e6e5906b 675 default:
e1f3808e 676 return NULL_QREG;
e6e5906b
PB
677 }
678 }
679 /* Should never happen. */
e1f3808e 680 return NULL_QREG;
e6e5906b
PB
681}
682
e1f3808e 683/* This generates a conditional branch, clobbering all temporaries. */
42a268c2 684static void gen_jmpcc(DisasContext *s, int cond, TCGLabel *l1)
e6e5906b 685{
e1f3808e 686 TCGv tmp;
e6e5906b 687
e1f3808e
PB
688 /* TODO: Optimize compare/branch pairs rather than always flushing
689 flag state to CC_OP_FLAGS. */
e6e5906b
PB
690 gen_flush_flags(s);
691 switch (cond) {
692 case 0: /* T */
e1f3808e 693 tcg_gen_br(l1);
e6e5906b
PB
694 break;
695 case 1: /* F */
696 break;
697 case 2: /* HI (!C && !Z) */
a7812ae4 698 tmp = tcg_temp_new();
e1f3808e
PB
699 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
700 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
701 break;
702 case 3: /* LS (C || Z) */
a7812ae4 703 tmp = tcg_temp_new();
e1f3808e
PB
704 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C | CCF_Z);
705 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
706 break;
707 case 4: /* CC (!C) */
a7812ae4 708 tmp = tcg_temp_new();
e1f3808e
PB
709 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
710 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
711 break;
712 case 5: /* CS (C) */
a7812ae4 713 tmp = tcg_temp_new();
e1f3808e
PB
714 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_C);
715 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
716 break;
717 case 6: /* NE (!Z) */
a7812ae4 718 tmp = tcg_temp_new();
e1f3808e
PB
719 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
720 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
721 break;
722 case 7: /* EQ (Z) */
a7812ae4 723 tmp = tcg_temp_new();
e1f3808e
PB
724 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_Z);
725 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
726 break;
727 case 8: /* VC (!V) */
a7812ae4 728 tmp = tcg_temp_new();
e1f3808e
PB
729 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
730 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
731 break;
732 case 9: /* VS (V) */
a7812ae4 733 tmp = tcg_temp_new();
e1f3808e
PB
734 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_V);
735 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
736 break;
737 case 10: /* PL (!N) */
a7812ae4 738 tmp = tcg_temp_new();
e1f3808e
PB
739 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
740 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
741 break;
742 case 11: /* MI (N) */
a7812ae4 743 tmp = tcg_temp_new();
e1f3808e
PB
744 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
745 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
746 break;
747 case 12: /* GE (!(N ^ V)) */
a7812ae4 748 tmp = tcg_temp_new();
e1f3808e
PB
749 assert(CCF_V == (CCF_N >> 2));
750 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
751 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
752 tcg_gen_andi_i32(tmp, tmp, CCF_V);
753 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
754 break;
755 case 13: /* LT (N ^ V) */
a7812ae4 756 tmp = tcg_temp_new();
e1f3808e
PB
757 assert(CCF_V == (CCF_N >> 2));
758 tcg_gen_shri_i32(tmp, QREG_CC_DEST, 2);
759 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
760 tcg_gen_andi_i32(tmp, tmp, CCF_V);
761 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
762 break;
763 case 14: /* GT (!(Z || (N ^ V))) */
a7812ae4 764 tmp = tcg_temp_new();
e1f3808e
PB
765 assert(CCF_V == (CCF_N >> 2));
766 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
767 tcg_gen_shri_i32(tmp, tmp, 2);
768 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
769 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
770 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, l1);
e6e5906b
PB
771 break;
772 case 15: /* LE (Z || (N ^ V)) */
a7812ae4 773 tmp = tcg_temp_new();
e1f3808e
PB
774 assert(CCF_V == (CCF_N >> 2));
775 tcg_gen_andi_i32(tmp, QREG_CC_DEST, CCF_N);
776 tcg_gen_shri_i32(tmp, tmp, 2);
777 tcg_gen_xor_i32(tmp, tmp, QREG_CC_DEST);
778 tcg_gen_andi_i32(tmp, tmp, CCF_V | CCF_Z);
779 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, l1);
e6e5906b
PB
780 break;
781 default:
782 /* Should ever happen. */
783 abort();
784 }
785}
786
787DISAS_INSN(scc)
788{
42a268c2 789 TCGLabel *l1;
e6e5906b 790 int cond;
e1f3808e 791 TCGv reg;
e6e5906b
PB
792
793 l1 = gen_new_label();
794 cond = (insn >> 8) & 0xf;
795 reg = DREG(insn, 0);
e1f3808e
PB
796 tcg_gen_andi_i32(reg, reg, 0xffffff00);
797 /* This is safe because we modify the reg directly, with no other values
798 live. */
e6e5906b 799 gen_jmpcc(s, cond ^ 1, l1);
e1f3808e 800 tcg_gen_ori_i32(reg, reg, 0xff);
e6e5906b
PB
801 gen_set_label(l1);
802}
803
0633879f
PB
804/* Force a TB lookup after an instruction that changes the CPU state. */
805static void gen_lookup_tb(DisasContext *s)
806{
807 gen_flush_cc_op(s);
e1f3808e 808 tcg_gen_movi_i32(QREG_PC, s->pc);
0633879f
PB
809 s->is_jmp = DISAS_UPDATE;
810}
811
e1f3808e
PB
812/* Generate a jump to an immediate address. */
813static void gen_jmp_im(DisasContext *s, uint32_t dest)
814{
815 gen_flush_cc_op(s);
816 tcg_gen_movi_i32(QREG_PC, dest);
817 s->is_jmp = DISAS_JUMP;
818}
819
820/* Generate a jump to the address in qreg DEST. */
821static void gen_jmp(DisasContext *s, TCGv dest)
e6e5906b
PB
822{
823 gen_flush_cc_op(s);
e1f3808e 824 tcg_gen_mov_i32(QREG_PC, dest);
e6e5906b
PB
825 s->is_jmp = DISAS_JUMP;
826}
827
828static void gen_exception(DisasContext *s, uint32_t where, int nr)
829{
830 gen_flush_cc_op(s);
e1f3808e 831 gen_jmp_im(s, where);
31871141 832 gen_helper_raise_exception(cpu_env, tcg_const_i32(nr));
e6e5906b
PB
833}
834
510ff0b7
PB
835static inline void gen_addr_fault(DisasContext *s)
836{
837 gen_exception(s, s->insn_pc, EXCP_ADDRESS);
838}
839
d4d79bb1
BS
840#define SRC_EA(env, result, opsize, op_sign, addrp) do { \
841 result = gen_ea(env, s, insn, opsize, NULL_QREG, addrp, \
842 op_sign ? EA_LOADS : EA_LOADU); \
843 if (IS_NULL_QREG(result)) { \
844 gen_addr_fault(s); \
845 return; \
846 } \
510ff0b7
PB
847 } while (0)
848
d4d79bb1
BS
849#define DEST_EA(env, insn, opsize, val, addrp) do { \
850 TCGv ea_result = gen_ea(env, s, insn, opsize, val, addrp, EA_STORE); \
851 if (IS_NULL_QREG(ea_result)) { \
852 gen_addr_fault(s); \
853 return; \
854 } \
510ff0b7
PB
855 } while (0)
856
90aa39a1
SF
857static inline bool use_goto_tb(DisasContext *s, uint32_t dest)
858{
859#ifndef CONFIG_USER_ONLY
860 return (s->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
861 (s->insn_pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
862#else
863 return true;
864#endif
865}
866
e6e5906b
PB
867/* Generate a jump to an immediate address. */
868static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
869{
551bd27f 870 if (unlikely(s->singlestep_enabled)) {
e6e5906b 871 gen_exception(s, dest, EXCP_DEBUG);
90aa39a1 872 } else if (use_goto_tb(s, dest)) {
57fec1fe 873 tcg_gen_goto_tb(n);
e1f3808e 874 tcg_gen_movi_i32(QREG_PC, dest);
90aa39a1 875 tcg_gen_exit_tb((uintptr_t)s->tb + n);
e6e5906b 876 } else {
e1f3808e 877 gen_jmp_im(s, dest);
57fec1fe 878 tcg_gen_exit_tb(0);
e6e5906b
PB
879 }
880 s->is_jmp = DISAS_TB_JUMP;
881}
882
883DISAS_INSN(undef_mac)
884{
885 gen_exception(s, s->pc - 2, EXCP_LINEA);
886}
887
888DISAS_INSN(undef_fpu)
889{
890 gen_exception(s, s->pc - 2, EXCP_LINEF);
891}
892
893DISAS_INSN(undef)
894{
a47dddd7
AF
895 M68kCPU *cpu = m68k_env_get_cpu(env);
896
e6e5906b 897 gen_exception(s, s->pc - 2, EXCP_UNSUPPORTED);
a47dddd7 898 cpu_abort(CPU(cpu), "Illegal instruction: %04x @ %08x", insn, s->pc - 2);
e6e5906b
PB
899}
900
901DISAS_INSN(mulw)
902{
e1f3808e
PB
903 TCGv reg;
904 TCGv tmp;
905 TCGv src;
e6e5906b
PB
906 int sign;
907
908 sign = (insn & 0x100) != 0;
909 reg = DREG(insn, 9);
a7812ae4 910 tmp = tcg_temp_new();
e6e5906b 911 if (sign)
e1f3808e 912 tcg_gen_ext16s_i32(tmp, reg);
e6e5906b 913 else
e1f3808e 914 tcg_gen_ext16u_i32(tmp, reg);
d4d79bb1 915 SRC_EA(env, src, OS_WORD, sign, NULL);
e1f3808e
PB
916 tcg_gen_mul_i32(tmp, tmp, src);
917 tcg_gen_mov_i32(reg, tmp);
e6e5906b
PB
918 /* Unlike m68k, coldfire always clears the overflow bit. */
919 gen_logic_cc(s, tmp);
920}
921
922DISAS_INSN(divw)
923{
e1f3808e
PB
924 TCGv reg;
925 TCGv tmp;
926 TCGv src;
e6e5906b
PB
927 int sign;
928
929 sign = (insn & 0x100) != 0;
930 reg = DREG(insn, 9);
931 if (sign) {
e1f3808e 932 tcg_gen_ext16s_i32(QREG_DIV1, reg);
e6e5906b 933 } else {
e1f3808e 934 tcg_gen_ext16u_i32(QREG_DIV1, reg);
e6e5906b 935 }
d4d79bb1 936 SRC_EA(env, src, OS_WORD, sign, NULL);
e1f3808e 937 tcg_gen_mov_i32(QREG_DIV2, src);
e6e5906b 938 if (sign) {
e1f3808e 939 gen_helper_divs(cpu_env, tcg_const_i32(1));
e6e5906b 940 } else {
e1f3808e 941 gen_helper_divu(cpu_env, tcg_const_i32(1));
e6e5906b
PB
942 }
943
a7812ae4
PB
944 tmp = tcg_temp_new();
945 src = tcg_temp_new();
e1f3808e
PB
946 tcg_gen_ext16u_i32(tmp, QREG_DIV1);
947 tcg_gen_shli_i32(src, QREG_DIV2, 16);
948 tcg_gen_or_i32(reg, tmp, src);
e6e5906b
PB
949 s->cc_op = CC_OP_FLAGS;
950}
951
952DISAS_INSN(divl)
953{
e1f3808e
PB
954 TCGv num;
955 TCGv den;
956 TCGv reg;
e6e5906b
PB
957 uint16_t ext;
958
d4d79bb1 959 ext = cpu_lduw_code(env, s->pc);
e6e5906b
PB
960 s->pc += 2;
961 if (ext & 0x87f8) {
962 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
963 return;
964 }
965 num = DREG(ext, 12);
966 reg = DREG(ext, 0);
e1f3808e 967 tcg_gen_mov_i32(QREG_DIV1, num);
d4d79bb1 968 SRC_EA(env, den, OS_LONG, 0, NULL);
e1f3808e 969 tcg_gen_mov_i32(QREG_DIV2, den);
e6e5906b 970 if (ext & 0x0800) {
e1f3808e 971 gen_helper_divs(cpu_env, tcg_const_i32(0));
e6e5906b 972 } else {
e1f3808e 973 gen_helper_divu(cpu_env, tcg_const_i32(0));
e6e5906b 974 }
e1f3808e 975 if ((ext & 7) == ((ext >> 12) & 7)) {
e6e5906b 976 /* div */
e1f3808e 977 tcg_gen_mov_i32 (reg, QREG_DIV1);
e6e5906b
PB
978 } else {
979 /* rem */
e1f3808e 980 tcg_gen_mov_i32 (reg, QREG_DIV2);
e6e5906b 981 }
e6e5906b
PB
982 s->cc_op = CC_OP_FLAGS;
983}
984
985DISAS_INSN(addsub)
986{
e1f3808e
PB
987 TCGv reg;
988 TCGv dest;
989 TCGv src;
990 TCGv tmp;
991 TCGv addr;
e6e5906b
PB
992 int add;
993
994 add = (insn & 0x4000) != 0;
995 reg = DREG(insn, 9);
a7812ae4 996 dest = tcg_temp_new();
e6e5906b 997 if (insn & 0x100) {
d4d79bb1 998 SRC_EA(env, tmp, OS_LONG, 0, &addr);
e6e5906b
PB
999 src = reg;
1000 } else {
1001 tmp = reg;
d4d79bb1 1002 SRC_EA(env, src, OS_LONG, 0, NULL);
e6e5906b
PB
1003 }
1004 if (add) {
e1f3808e
PB
1005 tcg_gen_add_i32(dest, tmp, src);
1006 gen_helper_xflag_lt(QREG_CC_X, dest, src);
e6e5906b
PB
1007 s->cc_op = CC_OP_ADD;
1008 } else {
e1f3808e
PB
1009 gen_helper_xflag_lt(QREG_CC_X, tmp, src);
1010 tcg_gen_sub_i32(dest, tmp, src);
e6e5906b
PB
1011 s->cc_op = CC_OP_SUB;
1012 }
e1f3808e 1013 gen_update_cc_add(dest, src);
e6e5906b 1014 if (insn & 0x100) {
d4d79bb1 1015 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b 1016 } else {
e1f3808e 1017 tcg_gen_mov_i32(reg, dest);
e6e5906b
PB
1018 }
1019}
1020
1021
1022/* Reverse the order of the bits in REG. */
1023DISAS_INSN(bitrev)
1024{
e1f3808e 1025 TCGv reg;
e6e5906b 1026 reg = DREG(insn, 0);
e1f3808e 1027 gen_helper_bitrev(reg, reg);
e6e5906b
PB
1028}
1029
1030DISAS_INSN(bitop_reg)
1031{
1032 int opsize;
1033 int op;
e1f3808e
PB
1034 TCGv src1;
1035 TCGv src2;
1036 TCGv tmp;
1037 TCGv addr;
1038 TCGv dest;
e6e5906b
PB
1039
1040 if ((insn & 0x38) != 0)
1041 opsize = OS_BYTE;
1042 else
1043 opsize = OS_LONG;
1044 op = (insn >> 6) & 3;
d4d79bb1 1045 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
e6e5906b 1046 src2 = DREG(insn, 9);
a7812ae4 1047 dest = tcg_temp_new();
e6e5906b
PB
1048
1049 gen_flush_flags(s);
a7812ae4 1050 tmp = tcg_temp_new();
e6e5906b 1051 if (opsize == OS_BYTE)
e1f3808e 1052 tcg_gen_andi_i32(tmp, src2, 7);
e6e5906b 1053 else
e1f3808e 1054 tcg_gen_andi_i32(tmp, src2, 31);
e6e5906b 1055 src2 = tmp;
a7812ae4 1056 tmp = tcg_temp_new();
e1f3808e
PB
1057 tcg_gen_shr_i32(tmp, src1, src2);
1058 tcg_gen_andi_i32(tmp, tmp, 1);
1059 tcg_gen_shli_i32(tmp, tmp, 2);
1060 /* Clear CCF_Z if bit set. */
1061 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1062 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1063
1064 tcg_gen_shl_i32(tmp, tcg_const_i32(1), src2);
e6e5906b
PB
1065 switch (op) {
1066 case 1: /* bchg */
e1f3808e 1067 tcg_gen_xor_i32(dest, src1, tmp);
e6e5906b
PB
1068 break;
1069 case 2: /* bclr */
e1f3808e
PB
1070 tcg_gen_not_i32(tmp, tmp);
1071 tcg_gen_and_i32(dest, src1, tmp);
e6e5906b
PB
1072 break;
1073 case 3: /* bset */
e1f3808e 1074 tcg_gen_or_i32(dest, src1, tmp);
e6e5906b
PB
1075 break;
1076 default: /* btst */
1077 break;
1078 }
1079 if (op)
d4d79bb1 1080 DEST_EA(env, insn, opsize, dest, &addr);
e6e5906b
PB
1081}
1082
1083DISAS_INSN(sats)
1084{
e1f3808e 1085 TCGv reg;
e6e5906b 1086 reg = DREG(insn, 0);
e6e5906b 1087 gen_flush_flags(s);
e1f3808e
PB
1088 gen_helper_sats(reg, reg, QREG_CC_DEST);
1089 gen_logic_cc(s, reg);
e6e5906b
PB
1090}
1091
e1f3808e 1092static void gen_push(DisasContext *s, TCGv val)
e6e5906b 1093{
e1f3808e 1094 TCGv tmp;
e6e5906b 1095
a7812ae4 1096 tmp = tcg_temp_new();
e1f3808e 1097 tcg_gen_subi_i32(tmp, QREG_SP, 4);
0633879f 1098 gen_store(s, OS_LONG, tmp, val);
e1f3808e 1099 tcg_gen_mov_i32(QREG_SP, tmp);
e6e5906b
PB
1100}
1101
1102DISAS_INSN(movem)
1103{
e1f3808e 1104 TCGv addr;
e6e5906b
PB
1105 int i;
1106 uint16_t mask;
e1f3808e
PB
1107 TCGv reg;
1108 TCGv tmp;
e6e5906b
PB
1109 int is_load;
1110
d4d79bb1 1111 mask = cpu_lduw_code(env, s->pc);
e6e5906b 1112 s->pc += 2;
d4d79bb1 1113 tmp = gen_lea(env, s, insn, OS_LONG);
e1f3808e 1114 if (IS_NULL_QREG(tmp)) {
510ff0b7
PB
1115 gen_addr_fault(s);
1116 return;
1117 }
a7812ae4 1118 addr = tcg_temp_new();
e1f3808e 1119 tcg_gen_mov_i32(addr, tmp);
e6e5906b
PB
1120 is_load = ((insn & 0x0400) != 0);
1121 for (i = 0; i < 16; i++, mask >>= 1) {
1122 if (mask & 1) {
1123 if (i < 8)
1124 reg = DREG(i, 0);
1125 else
1126 reg = AREG(i, 0);
1127 if (is_load) {
0633879f 1128 tmp = gen_load(s, OS_LONG, addr, 0);
e1f3808e 1129 tcg_gen_mov_i32(reg, tmp);
e6e5906b 1130 } else {
0633879f 1131 gen_store(s, OS_LONG, addr, reg);
e6e5906b
PB
1132 }
1133 if (mask != 1)
e1f3808e 1134 tcg_gen_addi_i32(addr, addr, 4);
e6e5906b
PB
1135 }
1136 }
1137}
1138
1139DISAS_INSN(bitop_im)
1140{
1141 int opsize;
1142 int op;
e1f3808e 1143 TCGv src1;
e6e5906b
PB
1144 uint32_t mask;
1145 int bitnum;
e1f3808e
PB
1146 TCGv tmp;
1147 TCGv addr;
e6e5906b
PB
1148
1149 if ((insn & 0x38) != 0)
1150 opsize = OS_BYTE;
1151 else
1152 opsize = OS_LONG;
1153 op = (insn >> 6) & 3;
1154
d4d79bb1 1155 bitnum = cpu_lduw_code(env, s->pc);
e6e5906b
PB
1156 s->pc += 2;
1157 if (bitnum & 0xff00) {
d4d79bb1 1158 disas_undef(env, s, insn);
e6e5906b
PB
1159 return;
1160 }
1161
d4d79bb1 1162 SRC_EA(env, src1, opsize, 0, op ? &addr: NULL);
e6e5906b
PB
1163
1164 gen_flush_flags(s);
e6e5906b
PB
1165 if (opsize == OS_BYTE)
1166 bitnum &= 7;
1167 else
1168 bitnum &= 31;
1169 mask = 1 << bitnum;
1170
a7812ae4 1171 tmp = tcg_temp_new();
e1f3808e
PB
1172 assert (CCF_Z == (1 << 2));
1173 if (bitnum > 2)
1174 tcg_gen_shri_i32(tmp, src1, bitnum - 2);
1175 else if (bitnum < 2)
1176 tcg_gen_shli_i32(tmp, src1, 2 - bitnum);
e6e5906b 1177 else
e1f3808e
PB
1178 tcg_gen_mov_i32(tmp, src1);
1179 tcg_gen_andi_i32(tmp, tmp, CCF_Z);
1180 /* Clear CCF_Z if bit set. */
1181 tcg_gen_ori_i32(QREG_CC_DEST, QREG_CC_DEST, CCF_Z);
1182 tcg_gen_xor_i32(QREG_CC_DEST, QREG_CC_DEST, tmp);
1183 if (op) {
1184 switch (op) {
1185 case 1: /* bchg */
1186 tcg_gen_xori_i32(tmp, src1, mask);
1187 break;
1188 case 2: /* bclr */
1189 tcg_gen_andi_i32(tmp, src1, ~mask);
1190 break;
1191 case 3: /* bset */
1192 tcg_gen_ori_i32(tmp, src1, mask);
1193 break;
1194 default: /* btst */
1195 break;
1196 }
d4d79bb1 1197 DEST_EA(env, insn, opsize, tmp, &addr);
e6e5906b 1198 }
e6e5906b
PB
1199}
1200
1201DISAS_INSN(arith_im)
1202{
1203 int op;
e1f3808e
PB
1204 uint32_t im;
1205 TCGv src1;
1206 TCGv dest;
1207 TCGv addr;
e6e5906b
PB
1208
1209 op = (insn >> 9) & 7;
d4d79bb1
BS
1210 SRC_EA(env, src1, OS_LONG, 0, (op == 6) ? NULL : &addr);
1211 im = read_im32(env, s);
a7812ae4 1212 dest = tcg_temp_new();
e6e5906b
PB
1213 switch (op) {
1214 case 0: /* ori */
e1f3808e 1215 tcg_gen_ori_i32(dest, src1, im);
e6e5906b
PB
1216 gen_logic_cc(s, dest);
1217 break;
1218 case 1: /* andi */
e1f3808e 1219 tcg_gen_andi_i32(dest, src1, im);
e6e5906b
PB
1220 gen_logic_cc(s, dest);
1221 break;
1222 case 2: /* subi */
e1f3808e 1223 tcg_gen_mov_i32(dest, src1);
351326a6 1224 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
e1f3808e 1225 tcg_gen_subi_i32(dest, dest, im);
351326a6 1226 gen_update_cc_add(dest, tcg_const_i32(im));
e6e5906b
PB
1227 s->cc_op = CC_OP_SUB;
1228 break;
1229 case 3: /* addi */
e1f3808e
PB
1230 tcg_gen_mov_i32(dest, src1);
1231 tcg_gen_addi_i32(dest, dest, im);
351326a6
LV
1232 gen_update_cc_add(dest, tcg_const_i32(im));
1233 gen_helper_xflag_lt(QREG_CC_X, dest, tcg_const_i32(im));
e6e5906b
PB
1234 s->cc_op = CC_OP_ADD;
1235 break;
1236 case 5: /* eori */
e1f3808e 1237 tcg_gen_xori_i32(dest, src1, im);
e6e5906b
PB
1238 gen_logic_cc(s, dest);
1239 break;
1240 case 6: /* cmpi */
e1f3808e
PB
1241 tcg_gen_mov_i32(dest, src1);
1242 tcg_gen_subi_i32(dest, dest, im);
351326a6 1243 gen_update_cc_add(dest, tcg_const_i32(im));
e6e5906b
PB
1244 s->cc_op = CC_OP_SUB;
1245 break;
1246 default:
1247 abort();
1248 }
1249 if (op != 6) {
d4d79bb1 1250 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b
PB
1251 }
1252}
1253
1254DISAS_INSN(byterev)
1255{
e1f3808e 1256 TCGv reg;
e6e5906b
PB
1257
1258 reg = DREG(insn, 0);
66896cb8 1259 tcg_gen_bswap32_i32(reg, reg);
e6e5906b
PB
1260}
1261
1262DISAS_INSN(move)
1263{
e1f3808e
PB
1264 TCGv src;
1265 TCGv dest;
e6e5906b
PB
1266 int op;
1267 int opsize;
1268
1269 switch (insn >> 12) {
1270 case 1: /* move.b */
1271 opsize = OS_BYTE;
1272 break;
1273 case 2: /* move.l */
1274 opsize = OS_LONG;
1275 break;
1276 case 3: /* move.w */
1277 opsize = OS_WORD;
1278 break;
1279 default:
1280 abort();
1281 }
d4d79bb1 1282 SRC_EA(env, src, opsize, 1, NULL);
e6e5906b
PB
1283 op = (insn >> 6) & 7;
1284 if (op == 1) {
1285 /* movea */
1286 /* The value will already have been sign extended. */
1287 dest = AREG(insn, 9);
e1f3808e 1288 tcg_gen_mov_i32(dest, src);
e6e5906b
PB
1289 } else {
1290 /* normal move */
1291 uint16_t dest_ea;
1292 dest_ea = ((insn >> 9) & 7) | (op << 3);
d4d79bb1 1293 DEST_EA(env, dest_ea, opsize, src, NULL);
e6e5906b
PB
1294 /* This will be correct because loads sign extend. */
1295 gen_logic_cc(s, src);
1296 }
1297}
1298
1299DISAS_INSN(negx)
1300{
e1f3808e 1301 TCGv reg;
e6e5906b
PB
1302
1303 gen_flush_flags(s);
1304 reg = DREG(insn, 0);
e1f3808e 1305 gen_helper_subx_cc(reg, cpu_env, tcg_const_i32(0), reg);
e6e5906b
PB
1306}
1307
1308DISAS_INSN(lea)
1309{
e1f3808e
PB
1310 TCGv reg;
1311 TCGv tmp;
e6e5906b
PB
1312
1313 reg = AREG(insn, 9);
d4d79bb1 1314 tmp = gen_lea(env, s, insn, OS_LONG);
e1f3808e 1315 if (IS_NULL_QREG(tmp)) {
510ff0b7
PB
1316 gen_addr_fault(s);
1317 return;
1318 }
e1f3808e 1319 tcg_gen_mov_i32(reg, tmp);
e6e5906b
PB
1320}
1321
1322DISAS_INSN(clr)
1323{
1324 int opsize;
1325
1326 switch ((insn >> 6) & 3) {
1327 case 0: /* clr.b */
1328 opsize = OS_BYTE;
1329 break;
1330 case 1: /* clr.w */
1331 opsize = OS_WORD;
1332 break;
1333 case 2: /* clr.l */
1334 opsize = OS_LONG;
1335 break;
1336 default:
1337 abort();
1338 }
d4d79bb1 1339 DEST_EA(env, insn, opsize, tcg_const_i32(0), NULL);
351326a6 1340 gen_logic_cc(s, tcg_const_i32(0));
e6e5906b
PB
1341}
1342
e1f3808e 1343static TCGv gen_get_ccr(DisasContext *s)
e6e5906b 1344{
e1f3808e 1345 TCGv dest;
e6e5906b
PB
1346
1347 gen_flush_flags(s);
a7812ae4 1348 dest = tcg_temp_new();
e1f3808e
PB
1349 tcg_gen_shli_i32(dest, QREG_CC_X, 4);
1350 tcg_gen_or_i32(dest, dest, QREG_CC_DEST);
0633879f
PB
1351 return dest;
1352}
1353
1354DISAS_INSN(move_from_ccr)
1355{
e1f3808e
PB
1356 TCGv reg;
1357 TCGv ccr;
0633879f
PB
1358
1359 ccr = gen_get_ccr(s);
e6e5906b 1360 reg = DREG(insn, 0);
0633879f 1361 gen_partset_reg(OS_WORD, reg, ccr);
e6e5906b
PB
1362}
1363
1364DISAS_INSN(neg)
1365{
e1f3808e
PB
1366 TCGv reg;
1367 TCGv src1;
e6e5906b
PB
1368
1369 reg = DREG(insn, 0);
a7812ae4 1370 src1 = tcg_temp_new();
e1f3808e
PB
1371 tcg_gen_mov_i32(src1, reg);
1372 tcg_gen_neg_i32(reg, src1);
e6e5906b 1373 s->cc_op = CC_OP_SUB;
e1f3808e
PB
1374 gen_update_cc_add(reg, src1);
1375 gen_helper_xflag_lt(QREG_CC_X, tcg_const_i32(0), src1);
e6e5906b
PB
1376 s->cc_op = CC_OP_SUB;
1377}
1378
0633879f
PB
1379static void gen_set_sr_im(DisasContext *s, uint16_t val, int ccr_only)
1380{
e1f3808e
PB
1381 tcg_gen_movi_i32(QREG_CC_DEST, val & 0xf);
1382 tcg_gen_movi_i32(QREG_CC_X, (val & 0x10) >> 4);
0633879f 1383 if (!ccr_only) {
e1f3808e 1384 gen_helper_set_sr(cpu_env, tcg_const_i32(val & 0xff00));
0633879f
PB
1385 }
1386}
1387
d4d79bb1
BS
1388static void gen_set_sr(CPUM68KState *env, DisasContext *s, uint16_t insn,
1389 int ccr_only)
e6e5906b 1390{
e1f3808e
PB
1391 TCGv tmp;
1392 TCGv reg;
e6e5906b
PB
1393
1394 s->cc_op = CC_OP_FLAGS;
1395 if ((insn & 0x38) == 0)
1396 {
a7812ae4 1397 tmp = tcg_temp_new();
e6e5906b 1398 reg = DREG(insn, 0);
e1f3808e
PB
1399 tcg_gen_andi_i32(QREG_CC_DEST, reg, 0xf);
1400 tcg_gen_shri_i32(tmp, reg, 4);
1401 tcg_gen_andi_i32(QREG_CC_X, tmp, 1);
0633879f 1402 if (!ccr_only) {
e1f3808e 1403 gen_helper_set_sr(cpu_env, reg);
0633879f 1404 }
e6e5906b 1405 }
0633879f 1406 else if ((insn & 0x3f) == 0x3c)
e6e5906b 1407 {
0633879f 1408 uint16_t val;
d4d79bb1 1409 val = cpu_lduw_code(env, s->pc);
e6e5906b 1410 s->pc += 2;
0633879f 1411 gen_set_sr_im(s, val, ccr_only);
e6e5906b
PB
1412 }
1413 else
d4d79bb1 1414 disas_undef(env, s, insn);
e6e5906b
PB
1415}
1416
0633879f
PB
1417DISAS_INSN(move_to_ccr)
1418{
d4d79bb1 1419 gen_set_sr(env, s, insn, 1);
0633879f
PB
1420}
1421
e6e5906b
PB
1422DISAS_INSN(not)
1423{
e1f3808e 1424 TCGv reg;
e6e5906b
PB
1425
1426 reg = DREG(insn, 0);
e1f3808e 1427 tcg_gen_not_i32(reg, reg);
e6e5906b
PB
1428 gen_logic_cc(s, reg);
1429}
1430
1431DISAS_INSN(swap)
1432{
e1f3808e
PB
1433 TCGv src1;
1434 TCGv src2;
1435 TCGv reg;
e6e5906b 1436
a7812ae4
PB
1437 src1 = tcg_temp_new();
1438 src2 = tcg_temp_new();
e6e5906b 1439 reg = DREG(insn, 0);
e1f3808e
PB
1440 tcg_gen_shli_i32(src1, reg, 16);
1441 tcg_gen_shri_i32(src2, reg, 16);
1442 tcg_gen_or_i32(reg, src1, src2);
1443 gen_logic_cc(s, reg);
e6e5906b
PB
1444}
1445
1446DISAS_INSN(pea)
1447{
e1f3808e 1448 TCGv tmp;
e6e5906b 1449
d4d79bb1 1450 tmp = gen_lea(env, s, insn, OS_LONG);
e1f3808e 1451 if (IS_NULL_QREG(tmp)) {
510ff0b7
PB
1452 gen_addr_fault(s);
1453 return;
1454 }
0633879f 1455 gen_push(s, tmp);
e6e5906b
PB
1456}
1457
1458DISAS_INSN(ext)
1459{
e6e5906b 1460 int op;
e1f3808e
PB
1461 TCGv reg;
1462 TCGv tmp;
e6e5906b
PB
1463
1464 reg = DREG(insn, 0);
1465 op = (insn >> 6) & 7;
a7812ae4 1466 tmp = tcg_temp_new();
e6e5906b 1467 if (op == 3)
e1f3808e 1468 tcg_gen_ext16s_i32(tmp, reg);
e6e5906b 1469 else
e1f3808e 1470 tcg_gen_ext8s_i32(tmp, reg);
e6e5906b
PB
1471 if (op == 2)
1472 gen_partset_reg(OS_WORD, reg, tmp);
1473 else
e1f3808e 1474 tcg_gen_mov_i32(reg, tmp);
e6e5906b
PB
1475 gen_logic_cc(s, tmp);
1476}
1477
1478DISAS_INSN(tst)
1479{
1480 int opsize;
e1f3808e 1481 TCGv tmp;
e6e5906b
PB
1482
1483 switch ((insn >> 6) & 3) {
1484 case 0: /* tst.b */
1485 opsize = OS_BYTE;
1486 break;
1487 case 1: /* tst.w */
1488 opsize = OS_WORD;
1489 break;
1490 case 2: /* tst.l */
1491 opsize = OS_LONG;
1492 break;
1493 default:
1494 abort();
1495 }
d4d79bb1 1496 SRC_EA(env, tmp, opsize, 1, NULL);
e6e5906b
PB
1497 gen_logic_cc(s, tmp);
1498}
1499
1500DISAS_INSN(pulse)
1501{
1502 /* Implemented as a NOP. */
1503}
1504
1505DISAS_INSN(illegal)
1506{
1507 gen_exception(s, s->pc - 2, EXCP_ILLEGAL);
1508}
1509
1510/* ??? This should be atomic. */
1511DISAS_INSN(tas)
1512{
e1f3808e
PB
1513 TCGv dest;
1514 TCGv src1;
1515 TCGv addr;
e6e5906b 1516
a7812ae4 1517 dest = tcg_temp_new();
d4d79bb1 1518 SRC_EA(env, src1, OS_BYTE, 1, &addr);
e6e5906b 1519 gen_logic_cc(s, src1);
e1f3808e 1520 tcg_gen_ori_i32(dest, src1, 0x80);
d4d79bb1 1521 DEST_EA(env, insn, OS_BYTE, dest, &addr);
e6e5906b
PB
1522}
1523
1524DISAS_INSN(mull)
1525{
1526 uint16_t ext;
e1f3808e
PB
1527 TCGv reg;
1528 TCGv src1;
1529 TCGv dest;
e6e5906b
PB
1530
1531 /* The upper 32 bits of the product are discarded, so
1532 muls.l and mulu.l are functionally equivalent. */
d4d79bb1 1533 ext = cpu_lduw_code(env, s->pc);
e6e5906b
PB
1534 s->pc += 2;
1535 if (ext & 0x87ff) {
1536 gen_exception(s, s->pc - 4, EXCP_UNSUPPORTED);
1537 return;
1538 }
1539 reg = DREG(ext, 12);
d4d79bb1 1540 SRC_EA(env, src1, OS_LONG, 0, NULL);
a7812ae4 1541 dest = tcg_temp_new();
e1f3808e
PB
1542 tcg_gen_mul_i32(dest, src1, reg);
1543 tcg_gen_mov_i32(reg, dest);
e6e5906b
PB
1544 /* Unlike m68k, coldfire always clears the overflow bit. */
1545 gen_logic_cc(s, dest);
1546}
1547
1548DISAS_INSN(link)
1549{
1550 int16_t offset;
e1f3808e
PB
1551 TCGv reg;
1552 TCGv tmp;
e6e5906b 1553
d4d79bb1 1554 offset = cpu_ldsw_code(env, s->pc);
e6e5906b
PB
1555 s->pc += 2;
1556 reg = AREG(insn, 0);
a7812ae4 1557 tmp = tcg_temp_new();
e1f3808e 1558 tcg_gen_subi_i32(tmp, QREG_SP, 4);
0633879f 1559 gen_store(s, OS_LONG, tmp, reg);
e1f3808e
PB
1560 if ((insn & 7) != 7)
1561 tcg_gen_mov_i32(reg, tmp);
1562 tcg_gen_addi_i32(QREG_SP, tmp, offset);
e6e5906b
PB
1563}
1564
1565DISAS_INSN(unlk)
1566{
e1f3808e
PB
1567 TCGv src;
1568 TCGv reg;
1569 TCGv tmp;
e6e5906b 1570
a7812ae4 1571 src = tcg_temp_new();
e6e5906b 1572 reg = AREG(insn, 0);
e1f3808e 1573 tcg_gen_mov_i32(src, reg);
0633879f 1574 tmp = gen_load(s, OS_LONG, src, 0);
e1f3808e
PB
1575 tcg_gen_mov_i32(reg, tmp);
1576 tcg_gen_addi_i32(QREG_SP, src, 4);
e6e5906b
PB
1577}
1578
1579DISAS_INSN(nop)
1580{
1581}
1582
1583DISAS_INSN(rts)
1584{
e1f3808e 1585 TCGv tmp;
e6e5906b 1586
0633879f 1587 tmp = gen_load(s, OS_LONG, QREG_SP, 0);
e1f3808e 1588 tcg_gen_addi_i32(QREG_SP, QREG_SP, 4);
e6e5906b
PB
1589 gen_jmp(s, tmp);
1590}
1591
1592DISAS_INSN(jump)
1593{
e1f3808e 1594 TCGv tmp;
e6e5906b
PB
1595
1596 /* Load the target address first to ensure correct exception
1597 behavior. */
d4d79bb1 1598 tmp = gen_lea(env, s, insn, OS_LONG);
e1f3808e 1599 if (IS_NULL_QREG(tmp)) {
510ff0b7
PB
1600 gen_addr_fault(s);
1601 return;
1602 }
e6e5906b
PB
1603 if ((insn & 0x40) == 0) {
1604 /* jsr */
351326a6 1605 gen_push(s, tcg_const_i32(s->pc));
e6e5906b
PB
1606 }
1607 gen_jmp(s, tmp);
1608}
1609
1610DISAS_INSN(addsubq)
1611{
e1f3808e
PB
1612 TCGv src1;
1613 TCGv src2;
1614 TCGv dest;
e6e5906b 1615 int val;
e1f3808e 1616 TCGv addr;
e6e5906b 1617
d4d79bb1 1618 SRC_EA(env, src1, OS_LONG, 0, &addr);
e6e5906b
PB
1619 val = (insn >> 9) & 7;
1620 if (val == 0)
1621 val = 8;
a7812ae4 1622 dest = tcg_temp_new();
e1f3808e 1623 tcg_gen_mov_i32(dest, src1);
e6e5906b
PB
1624 if ((insn & 0x38) == 0x08) {
1625 /* Don't update condition codes if the destination is an
1626 address register. */
1627 if (insn & 0x0100) {
e1f3808e 1628 tcg_gen_subi_i32(dest, dest, val);
e6e5906b 1629 } else {
e1f3808e 1630 tcg_gen_addi_i32(dest, dest, val);
e6e5906b
PB
1631 }
1632 } else {
351326a6 1633 src2 = tcg_const_i32(val);
e6e5906b 1634 if (insn & 0x0100) {
e1f3808e
PB
1635 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
1636 tcg_gen_subi_i32(dest, dest, val);
e6e5906b
PB
1637 s->cc_op = CC_OP_SUB;
1638 } else {
e1f3808e
PB
1639 tcg_gen_addi_i32(dest, dest, val);
1640 gen_helper_xflag_lt(QREG_CC_X, dest, src2);
e6e5906b
PB
1641 s->cc_op = CC_OP_ADD;
1642 }
e1f3808e 1643 gen_update_cc_add(dest, src2);
e6e5906b 1644 }
d4d79bb1 1645 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b
PB
1646}
1647
1648DISAS_INSN(tpf)
1649{
1650 switch (insn & 7) {
1651 case 2: /* One extension word. */
1652 s->pc += 2;
1653 break;
1654 case 3: /* Two extension words. */
1655 s->pc += 4;
1656 break;
1657 case 4: /* No extension words. */
1658 break;
1659 default:
d4d79bb1 1660 disas_undef(env, s, insn);
e6e5906b
PB
1661 }
1662}
1663
1664DISAS_INSN(branch)
1665{
1666 int32_t offset;
1667 uint32_t base;
1668 int op;
42a268c2 1669 TCGLabel *l1;
3b46e624 1670
e6e5906b
PB
1671 base = s->pc;
1672 op = (insn >> 8) & 0xf;
1673 offset = (int8_t)insn;
1674 if (offset == 0) {
d4d79bb1 1675 offset = cpu_ldsw_code(env, s->pc);
e6e5906b
PB
1676 s->pc += 2;
1677 } else if (offset == -1) {
d4d79bb1 1678 offset = read_im32(env, s);
e6e5906b
PB
1679 }
1680 if (op == 1) {
1681 /* bsr */
351326a6 1682 gen_push(s, tcg_const_i32(s->pc));
e6e5906b
PB
1683 }
1684 gen_flush_cc_op(s);
1685 if (op > 1) {
1686 /* Bcc */
1687 l1 = gen_new_label();
1688 gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
1689 gen_jmp_tb(s, 1, base + offset);
1690 gen_set_label(l1);
1691 gen_jmp_tb(s, 0, s->pc);
1692 } else {
1693 /* Unconditional branch. */
1694 gen_jmp_tb(s, 0, base + offset);
1695 }
1696}
1697
1698DISAS_INSN(moveq)
1699{
e1f3808e 1700 uint32_t val;
e6e5906b 1701
e1f3808e
PB
1702 val = (int8_t)insn;
1703 tcg_gen_movi_i32(DREG(insn, 9), val);
1704 gen_logic_cc(s, tcg_const_i32(val));
e6e5906b
PB
1705}
1706
1707DISAS_INSN(mvzs)
1708{
1709 int opsize;
e1f3808e
PB
1710 TCGv src;
1711 TCGv reg;
e6e5906b
PB
1712
1713 if (insn & 0x40)
1714 opsize = OS_WORD;
1715 else
1716 opsize = OS_BYTE;
d4d79bb1 1717 SRC_EA(env, src, opsize, (insn & 0x80) == 0, NULL);
e6e5906b 1718 reg = DREG(insn, 9);
e1f3808e 1719 tcg_gen_mov_i32(reg, src);
e6e5906b
PB
1720 gen_logic_cc(s, src);
1721}
1722
1723DISAS_INSN(or)
1724{
e1f3808e
PB
1725 TCGv reg;
1726 TCGv dest;
1727 TCGv src;
1728 TCGv addr;
e6e5906b
PB
1729
1730 reg = DREG(insn, 9);
a7812ae4 1731 dest = tcg_temp_new();
e6e5906b 1732 if (insn & 0x100) {
d4d79bb1 1733 SRC_EA(env, src, OS_LONG, 0, &addr);
e1f3808e 1734 tcg_gen_or_i32(dest, src, reg);
d4d79bb1 1735 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b 1736 } else {
d4d79bb1 1737 SRC_EA(env, src, OS_LONG, 0, NULL);
e1f3808e
PB
1738 tcg_gen_or_i32(dest, src, reg);
1739 tcg_gen_mov_i32(reg, dest);
e6e5906b
PB
1740 }
1741 gen_logic_cc(s, dest);
1742}
1743
1744DISAS_INSN(suba)
1745{
e1f3808e
PB
1746 TCGv src;
1747 TCGv reg;
e6e5906b 1748
d4d79bb1 1749 SRC_EA(env, src, OS_LONG, 0, NULL);
e6e5906b 1750 reg = AREG(insn, 9);
e1f3808e 1751 tcg_gen_sub_i32(reg, reg, src);
e6e5906b
PB
1752}
1753
1754DISAS_INSN(subx)
1755{
e1f3808e
PB
1756 TCGv reg;
1757 TCGv src;
e6e5906b
PB
1758
1759 gen_flush_flags(s);
1760 reg = DREG(insn, 9);
1761 src = DREG(insn, 0);
e1f3808e 1762 gen_helper_subx_cc(reg, cpu_env, reg, src);
e6e5906b
PB
1763}
1764
1765DISAS_INSN(mov3q)
1766{
e1f3808e 1767 TCGv src;
e6e5906b
PB
1768 int val;
1769
1770 val = (insn >> 9) & 7;
1771 if (val == 0)
1772 val = -1;
351326a6 1773 src = tcg_const_i32(val);
e6e5906b 1774 gen_logic_cc(s, src);
d4d79bb1 1775 DEST_EA(env, insn, OS_LONG, src, NULL);
e6e5906b
PB
1776}
1777
1778DISAS_INSN(cmp)
1779{
1780 int op;
e1f3808e
PB
1781 TCGv src;
1782 TCGv reg;
1783 TCGv dest;
e6e5906b
PB
1784 int opsize;
1785
1786 op = (insn >> 6) & 3;
1787 switch (op) {
1788 case 0: /* cmp.b */
1789 opsize = OS_BYTE;
1790 s->cc_op = CC_OP_CMPB;
1791 break;
1792 case 1: /* cmp.w */
1793 opsize = OS_WORD;
1794 s->cc_op = CC_OP_CMPW;
1795 break;
1796 case 2: /* cmp.l */
1797 opsize = OS_LONG;
1798 s->cc_op = CC_OP_SUB;
1799 break;
1800 default:
1801 abort();
1802 }
d4d79bb1 1803 SRC_EA(env, src, opsize, 1, NULL);
e6e5906b 1804 reg = DREG(insn, 9);
a7812ae4 1805 dest = tcg_temp_new();
e1f3808e
PB
1806 tcg_gen_sub_i32(dest, reg, src);
1807 gen_update_cc_add(dest, src);
e6e5906b
PB
1808}
1809
1810DISAS_INSN(cmpa)
1811{
1812 int opsize;
e1f3808e
PB
1813 TCGv src;
1814 TCGv reg;
1815 TCGv dest;
e6e5906b
PB
1816
1817 if (insn & 0x100) {
1818 opsize = OS_LONG;
1819 } else {
1820 opsize = OS_WORD;
1821 }
d4d79bb1 1822 SRC_EA(env, src, opsize, 1, NULL);
e6e5906b 1823 reg = AREG(insn, 9);
a7812ae4 1824 dest = tcg_temp_new();
e1f3808e
PB
1825 tcg_gen_sub_i32(dest, reg, src);
1826 gen_update_cc_add(dest, src);
e6e5906b
PB
1827 s->cc_op = CC_OP_SUB;
1828}
1829
1830DISAS_INSN(eor)
1831{
e1f3808e
PB
1832 TCGv src;
1833 TCGv reg;
1834 TCGv dest;
1835 TCGv addr;
e6e5906b 1836
d4d79bb1 1837 SRC_EA(env, src, OS_LONG, 0, &addr);
e6e5906b 1838 reg = DREG(insn, 9);
a7812ae4 1839 dest = tcg_temp_new();
e1f3808e 1840 tcg_gen_xor_i32(dest, src, reg);
e6e5906b 1841 gen_logic_cc(s, dest);
d4d79bb1 1842 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b
PB
1843}
1844
1845DISAS_INSN(and)
1846{
e1f3808e
PB
1847 TCGv src;
1848 TCGv reg;
1849 TCGv dest;
1850 TCGv addr;
e6e5906b
PB
1851
1852 reg = DREG(insn, 9);
a7812ae4 1853 dest = tcg_temp_new();
e6e5906b 1854 if (insn & 0x100) {
d4d79bb1 1855 SRC_EA(env, src, OS_LONG, 0, &addr);
e1f3808e 1856 tcg_gen_and_i32(dest, src, reg);
d4d79bb1 1857 DEST_EA(env, insn, OS_LONG, dest, &addr);
e6e5906b 1858 } else {
d4d79bb1 1859 SRC_EA(env, src, OS_LONG, 0, NULL);
e1f3808e
PB
1860 tcg_gen_and_i32(dest, src, reg);
1861 tcg_gen_mov_i32(reg, dest);
e6e5906b
PB
1862 }
1863 gen_logic_cc(s, dest);
1864}
1865
1866DISAS_INSN(adda)
1867{
e1f3808e
PB
1868 TCGv src;
1869 TCGv reg;
e6e5906b 1870
d4d79bb1 1871 SRC_EA(env, src, OS_LONG, 0, NULL);
e6e5906b 1872 reg = AREG(insn, 9);
e1f3808e 1873 tcg_gen_add_i32(reg, reg, src);
e6e5906b
PB
1874}
1875
1876DISAS_INSN(addx)
1877{
e1f3808e
PB
1878 TCGv reg;
1879 TCGv src;
e6e5906b
PB
1880
1881 gen_flush_flags(s);
1882 reg = DREG(insn, 9);
1883 src = DREG(insn, 0);
e1f3808e 1884 gen_helper_addx_cc(reg, cpu_env, reg, src);
e6e5906b
PB
1885 s->cc_op = CC_OP_FLAGS;
1886}
1887
e1f3808e 1888/* TODO: This could be implemented without helper functions. */
e6e5906b
PB
1889DISAS_INSN(shift_im)
1890{
e1f3808e 1891 TCGv reg;
e6e5906b 1892 int tmp;
e1f3808e 1893 TCGv shift;
e6e5906b
PB
1894
1895 reg = DREG(insn, 0);
1896 tmp = (insn >> 9) & 7;
1897 if (tmp == 0)
e1f3808e 1898 tmp = 8;
351326a6 1899 shift = tcg_const_i32(tmp);
e1f3808e 1900 /* No need to flush flags becuse we know we will set C flag. */
e6e5906b 1901 if (insn & 0x100) {
e1f3808e 1902 gen_helper_shl_cc(reg, cpu_env, reg, shift);
e6e5906b
PB
1903 } else {
1904 if (insn & 8) {
e1f3808e 1905 gen_helper_shr_cc(reg, cpu_env, reg, shift);
e6e5906b 1906 } else {
e1f3808e 1907 gen_helper_sar_cc(reg, cpu_env, reg, shift);
e6e5906b
PB
1908 }
1909 }
e1f3808e 1910 s->cc_op = CC_OP_SHIFT;
e6e5906b
PB
1911}
1912
1913DISAS_INSN(shift_reg)
1914{
e1f3808e
PB
1915 TCGv reg;
1916 TCGv shift;
e6e5906b
PB
1917
1918 reg = DREG(insn, 0);
e1f3808e
PB
1919 shift = DREG(insn, 9);
1920 /* Shift by zero leaves C flag unmodified. */
1921 gen_flush_flags(s);
e6e5906b 1922 if (insn & 0x100) {
e1f3808e 1923 gen_helper_shl_cc(reg, cpu_env, reg, shift);
e6e5906b
PB
1924 } else {
1925 if (insn & 8) {
e1f3808e 1926 gen_helper_shr_cc(reg, cpu_env, reg, shift);
e6e5906b 1927 } else {
e1f3808e 1928 gen_helper_sar_cc(reg, cpu_env, reg, shift);
e6e5906b
PB
1929 }
1930 }
e1f3808e 1931 s->cc_op = CC_OP_SHIFT;
e6e5906b
PB
1932}
1933
1934DISAS_INSN(ff1)
1935{
e1f3808e 1936 TCGv reg;
821f7e76
PB
1937 reg = DREG(insn, 0);
1938 gen_logic_cc(s, reg);
e1f3808e 1939 gen_helper_ff1(reg, reg);
e6e5906b
PB
1940}
1941
e1f3808e 1942static TCGv gen_get_sr(DisasContext *s)
0633879f 1943{
e1f3808e
PB
1944 TCGv ccr;
1945 TCGv sr;
0633879f
PB
1946
1947 ccr = gen_get_ccr(s);
a7812ae4 1948 sr = tcg_temp_new();
e1f3808e
PB
1949 tcg_gen_andi_i32(sr, QREG_SR, 0xffe0);
1950 tcg_gen_or_i32(sr, sr, ccr);
0633879f
PB
1951 return sr;
1952}
1953
e6e5906b
PB
1954DISAS_INSN(strldsr)
1955{
1956 uint16_t ext;
1957 uint32_t addr;
1958
1959 addr = s->pc - 2;
d4d79bb1 1960 ext = cpu_lduw_code(env, s->pc);
e6e5906b 1961 s->pc += 2;
0633879f 1962 if (ext != 0x46FC) {
e6e5906b 1963 gen_exception(s, addr, EXCP_UNSUPPORTED);
0633879f
PB
1964 return;
1965 }
d4d79bb1 1966 ext = cpu_lduw_code(env, s->pc);
0633879f
PB
1967 s->pc += 2;
1968 if (IS_USER(s) || (ext & SR_S) == 0) {
e6e5906b 1969 gen_exception(s, addr, EXCP_PRIVILEGE);
0633879f
PB
1970 return;
1971 }
1972 gen_push(s, gen_get_sr(s));
1973 gen_set_sr_im(s, ext, 0);
e6e5906b
PB
1974}
1975
1976DISAS_INSN(move_from_sr)
1977{
e1f3808e
PB
1978 TCGv reg;
1979 TCGv sr;
0633879f
PB
1980
1981 if (IS_USER(s)) {
1982 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1983 return;
1984 }
1985 sr = gen_get_sr(s);
1986 reg = DREG(insn, 0);
1987 gen_partset_reg(OS_WORD, reg, sr);
e6e5906b
PB
1988}
1989
1990DISAS_INSN(move_to_sr)
1991{
0633879f
PB
1992 if (IS_USER(s)) {
1993 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
1994 return;
1995 }
d4d79bb1 1996 gen_set_sr(env, s, insn, 0);
0633879f 1997 gen_lookup_tb(s);
e6e5906b
PB
1998}
1999
2000DISAS_INSN(move_from_usp)
2001{
0633879f
PB
2002 if (IS_USER(s)) {
2003 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2004 return;
2005 }
2a8327e8
GU
2006 tcg_gen_ld_i32(AREG(insn, 0), cpu_env,
2007 offsetof(CPUM68KState, sp[M68K_USP]));
e6e5906b
PB
2008}
2009
2010DISAS_INSN(move_to_usp)
2011{
0633879f
PB
2012 if (IS_USER(s)) {
2013 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2014 return;
2015 }
2a8327e8
GU
2016 tcg_gen_st_i32(AREG(insn, 0), cpu_env,
2017 offsetof(CPUM68KState, sp[M68K_USP]));
e6e5906b
PB
2018}
2019
2020DISAS_INSN(halt)
2021{
e1f3808e 2022 gen_exception(s, s->pc, EXCP_HALT_INSN);
e6e5906b
PB
2023}
2024
2025DISAS_INSN(stop)
2026{
0633879f
PB
2027 uint16_t ext;
2028
2029 if (IS_USER(s)) {
2030 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2031 return;
2032 }
2033
d4d79bb1 2034 ext = cpu_lduw_code(env, s->pc);
0633879f
PB
2035 s->pc += 2;
2036
2037 gen_set_sr_im(s, ext, 0);
259186a7 2038 tcg_gen_movi_i32(cpu_halted, 1);
e1f3808e 2039 gen_exception(s, s->pc, EXCP_HLT);
e6e5906b
PB
2040}
2041
2042DISAS_INSN(rte)
2043{
0633879f
PB
2044 if (IS_USER(s)) {
2045 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2046 return;
2047 }
2048 gen_exception(s, s->pc - 2, EXCP_RTE);
e6e5906b
PB
2049}
2050
2051DISAS_INSN(movec)
2052{
0633879f 2053 uint16_t ext;
e1f3808e 2054 TCGv reg;
0633879f
PB
2055
2056 if (IS_USER(s)) {
2057 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2058 return;
2059 }
2060
d4d79bb1 2061 ext = cpu_lduw_code(env, s->pc);
0633879f
PB
2062 s->pc += 2;
2063
2064 if (ext & 0x8000) {
2065 reg = AREG(ext, 12);
2066 } else {
2067 reg = DREG(ext, 12);
2068 }
e1f3808e 2069 gen_helper_movec(cpu_env, tcg_const_i32(ext & 0xfff), reg);
0633879f 2070 gen_lookup_tb(s);
e6e5906b
PB
2071}
2072
2073DISAS_INSN(intouch)
2074{
0633879f
PB
2075 if (IS_USER(s)) {
2076 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2077 return;
2078 }
2079 /* ICache fetch. Implement as no-op. */
e6e5906b
PB
2080}
2081
2082DISAS_INSN(cpushl)
2083{
0633879f
PB
2084 if (IS_USER(s)) {
2085 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2086 return;
2087 }
2088 /* Cache push/invalidate. Implement as no-op. */
e6e5906b
PB
2089}
2090
2091DISAS_INSN(wddata)
2092{
2093 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2094}
2095
2096DISAS_INSN(wdebug)
2097{
a47dddd7
AF
2098 M68kCPU *cpu = m68k_env_get_cpu(env);
2099
0633879f
PB
2100 if (IS_USER(s)) {
2101 gen_exception(s, s->pc - 2, EXCP_PRIVILEGE);
2102 return;
2103 }
2104 /* TODO: Implement wdebug. */
a47dddd7 2105 cpu_abort(CPU(cpu), "WDEBUG not implemented");
e6e5906b
PB
2106}
2107
2108DISAS_INSN(trap)
2109{
2110 gen_exception(s, s->pc - 2, EXCP_TRAP0 + (insn & 0xf));
2111}
2112
2113/* ??? FP exceptions are not implemented. Most exceptions are deferred until
2114 immediately before the next FP instruction is executed. */
2115DISAS_INSN(fpu)
2116{
2117 uint16_t ext;
a7812ae4 2118 int32_t offset;
e6e5906b 2119 int opmode;
a7812ae4
PB
2120 TCGv_i64 src;
2121 TCGv_i64 dest;
2122 TCGv_i64 res;
2123 TCGv tmp32;
e6e5906b 2124 int round;
a7812ae4 2125 int set_dest;
e6e5906b
PB
2126 int opsize;
2127
d4d79bb1 2128 ext = cpu_lduw_code(env, s->pc);
e6e5906b
PB
2129 s->pc += 2;
2130 opmode = ext & 0x7f;
2131 switch ((ext >> 13) & 7) {
2132 case 0: case 2:
2133 break;
2134 case 1:
2135 goto undef;
2136 case 3: /* fmove out */
2137 src = FREG(ext, 7);
a7812ae4 2138 tmp32 = tcg_temp_new_i32();
e6e5906b
PB
2139 /* fmove */
2140 /* ??? TODO: Proper behavior on overflow. */
2141 switch ((ext >> 10) & 7) {
2142 case 0:
2143 opsize = OS_LONG;
a7812ae4 2144 gen_helper_f64_to_i32(tmp32, cpu_env, src);
e6e5906b
PB
2145 break;
2146 case 1:
2147 opsize = OS_SINGLE;
a7812ae4 2148 gen_helper_f64_to_f32(tmp32, cpu_env, src);
e6e5906b
PB
2149 break;
2150 case 4:
2151 opsize = OS_WORD;
a7812ae4 2152 gen_helper_f64_to_i32(tmp32, cpu_env, src);
e6e5906b 2153 break;
a7812ae4
PB
2154 case 5: /* OS_DOUBLE */
2155 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
c59b97aa 2156 switch ((insn >> 3) & 7) {
a7812ae4
PB
2157 case 2:
2158 case 3:
243ee8f7 2159 break;
a7812ae4
PB
2160 case 4:
2161 tcg_gen_addi_i32(tmp32, tmp32, -8);
2162 break;
2163 case 5:
d4d79bb1 2164 offset = cpu_ldsw_code(env, s->pc);
a7812ae4
PB
2165 s->pc += 2;
2166 tcg_gen_addi_i32(tmp32, tmp32, offset);
2167 break;
2168 default:
2169 goto undef;
2170 }
2171 gen_store64(s, tmp32, src);
c59b97aa 2172 switch ((insn >> 3) & 7) {
a7812ae4
PB
2173 case 3:
2174 tcg_gen_addi_i32(tmp32, tmp32, 8);
2175 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2176 break;
2177 case 4:
2178 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2179 break;
2180 }
2181 tcg_temp_free_i32(tmp32);
2182 return;
e6e5906b
PB
2183 case 6:
2184 opsize = OS_BYTE;
a7812ae4 2185 gen_helper_f64_to_i32(tmp32, cpu_env, src);
e6e5906b
PB
2186 break;
2187 default:
2188 goto undef;
2189 }
d4d79bb1 2190 DEST_EA(env, insn, opsize, tmp32, NULL);
a7812ae4 2191 tcg_temp_free_i32(tmp32);
e6e5906b
PB
2192 return;
2193 case 4: /* fmove to control register. */
2194 switch ((ext >> 10) & 7) {
2195 case 4: /* FPCR */
2196 /* Not implemented. Ignore writes. */
2197 break;
2198 case 1: /* FPIAR */
2199 case 2: /* FPSR */
2200 default:
2201 cpu_abort(NULL, "Unimplemented: fmove to control %d",
2202 (ext >> 10) & 7);
2203 }
2204 break;
2205 case 5: /* fmove from control register. */
2206 switch ((ext >> 10) & 7) {
2207 case 4: /* FPCR */
2208 /* Not implemented. Always return zero. */
351326a6 2209 tmp32 = tcg_const_i32(0);
e6e5906b
PB
2210 break;
2211 case 1: /* FPIAR */
2212 case 2: /* FPSR */
2213 default:
2214 cpu_abort(NULL, "Unimplemented: fmove from control %d",
2215 (ext >> 10) & 7);
2216 goto undef;
2217 }
d4d79bb1 2218 DEST_EA(env, insn, OS_LONG, tmp32, NULL);
e6e5906b 2219 break;
5fafdf24 2220 case 6: /* fmovem */
e6e5906b
PB
2221 case 7:
2222 {
e1f3808e
PB
2223 TCGv addr;
2224 uint16_t mask;
2225 int i;
2226 if ((ext & 0x1f00) != 0x1000 || (ext & 0xff) == 0)
2227 goto undef;
d4d79bb1 2228 tmp32 = gen_lea(env, s, insn, OS_LONG);
a7812ae4 2229 if (IS_NULL_QREG(tmp32)) {
e1f3808e
PB
2230 gen_addr_fault(s);
2231 return;
2232 }
a7812ae4
PB
2233 addr = tcg_temp_new_i32();
2234 tcg_gen_mov_i32(addr, tmp32);
e1f3808e
PB
2235 mask = 0x80;
2236 for (i = 0; i < 8; i++) {
2237 if (ext & mask) {
e1f3808e
PB
2238 dest = FREG(i, 0);
2239 if (ext & (1 << 13)) {
2240 /* store */
2241 tcg_gen_qemu_stf64(dest, addr, IS_USER(s));
2242 } else {
2243 /* load */
2244 tcg_gen_qemu_ldf64(dest, addr, IS_USER(s));
2245 }
2246 if (ext & (mask - 1))
2247 tcg_gen_addi_i32(addr, addr, 8);
e6e5906b 2248 }
e1f3808e 2249 mask >>= 1;
e6e5906b 2250 }
18307f26 2251 tcg_temp_free_i32(addr);
e6e5906b
PB
2252 }
2253 return;
2254 }
2255 if (ext & (1 << 14)) {
e6e5906b
PB
2256 /* Source effective address. */
2257 switch ((ext >> 10) & 7) {
2258 case 0: opsize = OS_LONG; break;
2259 case 1: opsize = OS_SINGLE; break;
2260 case 4: opsize = OS_WORD; break;
2261 case 5: opsize = OS_DOUBLE; break;
2262 case 6: opsize = OS_BYTE; break;
2263 default:
2264 goto undef;
2265 }
e6e5906b 2266 if (opsize == OS_DOUBLE) {
a7812ae4
PB
2267 tmp32 = tcg_temp_new_i32();
2268 tcg_gen_mov_i32(tmp32, AREG(insn, 0));
c59b97aa 2269 switch ((insn >> 3) & 7) {
a7812ae4
PB
2270 case 2:
2271 case 3:
243ee8f7 2272 break;
a7812ae4
PB
2273 case 4:
2274 tcg_gen_addi_i32(tmp32, tmp32, -8);
2275 break;
2276 case 5:
d4d79bb1 2277 offset = cpu_ldsw_code(env, s->pc);
a7812ae4
PB
2278 s->pc += 2;
2279 tcg_gen_addi_i32(tmp32, tmp32, offset);
2280 break;
2281 case 7:
d4d79bb1 2282 offset = cpu_ldsw_code(env, s->pc);
a7812ae4
PB
2283 offset += s->pc - 2;
2284 s->pc += 2;
2285 tcg_gen_addi_i32(tmp32, tmp32, offset);
2286 break;
2287 default:
2288 goto undef;
2289 }
2290 src = gen_load64(s, tmp32);
c59b97aa 2291 switch ((insn >> 3) & 7) {
a7812ae4
PB
2292 case 3:
2293 tcg_gen_addi_i32(tmp32, tmp32, 8);
2294 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2295 break;
2296 case 4:
2297 tcg_gen_mov_i32(AREG(insn, 0), tmp32);
2298 break;
2299 }
2300 tcg_temp_free_i32(tmp32);
e6e5906b 2301 } else {
d4d79bb1 2302 SRC_EA(env, tmp32, opsize, 1, NULL);
a7812ae4 2303 src = tcg_temp_new_i64();
e6e5906b
PB
2304 switch (opsize) {
2305 case OS_LONG:
2306 case OS_WORD:
2307 case OS_BYTE:
a7812ae4 2308 gen_helper_i32_to_f64(src, cpu_env, tmp32);
e6e5906b
PB
2309 break;
2310 case OS_SINGLE:
a7812ae4 2311 gen_helper_f32_to_f64(src, cpu_env, tmp32);
e6e5906b
PB
2312 break;
2313 }
2314 }
2315 } else {
2316 /* Source register. */
2317 src = FREG(ext, 10);
2318 }
2319 dest = FREG(ext, 7);
a7812ae4 2320 res = tcg_temp_new_i64();
e6e5906b 2321 if (opmode != 0x3a)
e1f3808e 2322 tcg_gen_mov_f64(res, dest);
e6e5906b 2323 round = 1;
a7812ae4 2324 set_dest = 1;
e6e5906b
PB
2325 switch (opmode) {
2326 case 0: case 0x40: case 0x44: /* fmove */
e1f3808e 2327 tcg_gen_mov_f64(res, src);
e6e5906b
PB
2328 break;
2329 case 1: /* fint */
e1f3808e 2330 gen_helper_iround_f64(res, cpu_env, src);
e6e5906b
PB
2331 round = 0;
2332 break;
2333 case 3: /* fintrz */
e1f3808e 2334 gen_helper_itrunc_f64(res, cpu_env, src);
e6e5906b
PB
2335 round = 0;
2336 break;
2337 case 4: case 0x41: case 0x45: /* fsqrt */
e1f3808e 2338 gen_helper_sqrt_f64(res, cpu_env, src);
e6e5906b
PB
2339 break;
2340 case 0x18: case 0x58: case 0x5c: /* fabs */
e1f3808e 2341 gen_helper_abs_f64(res, src);
e6e5906b
PB
2342 break;
2343 case 0x1a: case 0x5a: case 0x5e: /* fneg */
e1f3808e 2344 gen_helper_chs_f64(res, src);
e6e5906b
PB
2345 break;
2346 case 0x20: case 0x60: case 0x64: /* fdiv */
e1f3808e 2347 gen_helper_div_f64(res, cpu_env, res, src);
e6e5906b
PB
2348 break;
2349 case 0x22: case 0x62: case 0x66: /* fadd */
e1f3808e 2350 gen_helper_add_f64(res, cpu_env, res, src);
e6e5906b
PB
2351 break;
2352 case 0x23: case 0x63: case 0x67: /* fmul */
e1f3808e 2353 gen_helper_mul_f64(res, cpu_env, res, src);
e6e5906b
PB
2354 break;
2355 case 0x28: case 0x68: case 0x6c: /* fsub */
e1f3808e 2356 gen_helper_sub_f64(res, cpu_env, res, src);
e6e5906b
PB
2357 break;
2358 case 0x38: /* fcmp */
e1f3808e 2359 gen_helper_sub_cmp_f64(res, cpu_env, res, src);
a7812ae4 2360 set_dest = 0;
e6e5906b
PB
2361 round = 0;
2362 break;
2363 case 0x3a: /* ftst */
e1f3808e 2364 tcg_gen_mov_f64(res, src);
a7812ae4 2365 set_dest = 0;
e6e5906b
PB
2366 round = 0;
2367 break;
2368 default:
2369 goto undef;
2370 }
a7812ae4
PB
2371 if (ext & (1 << 14)) {
2372 tcg_temp_free_i64(src);
2373 }
e6e5906b
PB
2374 if (round) {
2375 if (opmode & 0x40) {
2376 if ((opmode & 0x4) != 0)
2377 round = 0;
2378 } else if ((s->fpcr & M68K_FPCR_PREC) == 0) {
2379 round = 0;
2380 }
2381 }
2382 if (round) {
a7812ae4 2383 TCGv tmp = tcg_temp_new_i32();
e1f3808e
PB
2384 gen_helper_f64_to_f32(tmp, cpu_env, res);
2385 gen_helper_f32_to_f64(res, cpu_env, tmp);
a7812ae4 2386 tcg_temp_free_i32(tmp);
5fafdf24 2387 }
e1f3808e 2388 tcg_gen_mov_f64(QREG_FP_RESULT, res);
a7812ae4 2389 if (set_dest) {
e1f3808e 2390 tcg_gen_mov_f64(dest, res);
e6e5906b 2391 }
a7812ae4 2392 tcg_temp_free_i64(res);
e6e5906b
PB
2393 return;
2394undef:
a7812ae4 2395 /* FIXME: Is this right for offset addressing modes? */
e6e5906b 2396 s->pc -= 2;
d4d79bb1 2397 disas_undef_fpu(env, s, insn);
e6e5906b
PB
2398}
2399
2400DISAS_INSN(fbcc)
2401{
2402 uint32_t offset;
2403 uint32_t addr;
e1f3808e 2404 TCGv flag;
42a268c2 2405 TCGLabel *l1;
e6e5906b
PB
2406
2407 addr = s->pc;
d4d79bb1 2408 offset = cpu_ldsw_code(env, s->pc);
e6e5906b
PB
2409 s->pc += 2;
2410 if (insn & (1 << 6)) {
d4d79bb1 2411 offset = (offset << 16) | cpu_lduw_code(env, s->pc);
e6e5906b
PB
2412 s->pc += 2;
2413 }
2414
2415 l1 = gen_new_label();
2416 /* TODO: Raise BSUN exception. */
a7812ae4 2417 flag = tcg_temp_new();
e1f3808e 2418 gen_helper_compare_f64(flag, cpu_env, QREG_FP_RESULT);
e6e5906b
PB
2419 /* Jump to l1 if condition is true. */
2420 switch (insn & 0xf) {
2421 case 0: /* f */
2422 break;
2423 case 1: /* eq (=0) */
e1f3808e 2424 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2425 break;
2426 case 2: /* ogt (=1) */
e1f3808e 2427 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(1), l1);
e6e5906b
PB
2428 break;
2429 case 3: /* oge (=0 or =1) */
e1f3808e 2430 tcg_gen_brcond_i32(TCG_COND_LEU, flag, tcg_const_i32(1), l1);
e6e5906b
PB
2431 break;
2432 case 4: /* olt (=-1) */
e1f3808e 2433 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2434 break;
2435 case 5: /* ole (=-1 or =0) */
e1f3808e 2436 tcg_gen_brcond_i32(TCG_COND_LE, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2437 break;
2438 case 6: /* ogl (=-1 or =1) */
e1f3808e
PB
2439 tcg_gen_andi_i32(flag, flag, 1);
2440 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2441 break;
2442 case 7: /* or (=2) */
e1f3808e 2443 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(2), l1);
e6e5906b
PB
2444 break;
2445 case 8: /* un (<2) */
e1f3808e 2446 tcg_gen_brcond_i32(TCG_COND_LT, flag, tcg_const_i32(2), l1);
e6e5906b
PB
2447 break;
2448 case 9: /* ueq (=0 or =2) */
e1f3808e
PB
2449 tcg_gen_andi_i32(flag, flag, 1);
2450 tcg_gen_brcond_i32(TCG_COND_EQ, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2451 break;
2452 case 10: /* ugt (>0) */
e1f3808e 2453 tcg_gen_brcond_i32(TCG_COND_GT, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2454 break;
2455 case 11: /* uge (>=0) */
e1f3808e 2456 tcg_gen_brcond_i32(TCG_COND_GE, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2457 break;
2458 case 12: /* ult (=-1 or =2) */
e1f3808e 2459 tcg_gen_brcond_i32(TCG_COND_GEU, flag, tcg_const_i32(2), l1);
e6e5906b
PB
2460 break;
2461 case 13: /* ule (!=1) */
e1f3808e 2462 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(1), l1);
e6e5906b
PB
2463 break;
2464 case 14: /* ne (!=0) */
e1f3808e 2465 tcg_gen_brcond_i32(TCG_COND_NE, flag, tcg_const_i32(0), l1);
e6e5906b
PB
2466 break;
2467 case 15: /* t */
e1f3808e 2468 tcg_gen_br(l1);
e6e5906b
PB
2469 break;
2470 }
2471 gen_jmp_tb(s, 0, s->pc);
2472 gen_set_label(l1);
2473 gen_jmp_tb(s, 1, addr + offset);
2474}
2475
0633879f
PB
2476DISAS_INSN(frestore)
2477{
a47dddd7
AF
2478 M68kCPU *cpu = m68k_env_get_cpu(env);
2479
0633879f 2480 /* TODO: Implement frestore. */
a47dddd7 2481 cpu_abort(CPU(cpu), "FRESTORE not implemented");
0633879f
PB
2482}
2483
2484DISAS_INSN(fsave)
2485{
a47dddd7
AF
2486 M68kCPU *cpu = m68k_env_get_cpu(env);
2487
0633879f 2488 /* TODO: Implement fsave. */
a47dddd7 2489 cpu_abort(CPU(cpu), "FSAVE not implemented");
0633879f
PB
2490}
2491
e1f3808e 2492static inline TCGv gen_mac_extract_word(DisasContext *s, TCGv val, int upper)
acf930aa 2493{
a7812ae4 2494 TCGv tmp = tcg_temp_new();
acf930aa
PB
2495 if (s->env->macsr & MACSR_FI) {
2496 if (upper)
e1f3808e 2497 tcg_gen_andi_i32(tmp, val, 0xffff0000);
acf930aa 2498 else
e1f3808e 2499 tcg_gen_shli_i32(tmp, val, 16);
acf930aa
PB
2500 } else if (s->env->macsr & MACSR_SU) {
2501 if (upper)
e1f3808e 2502 tcg_gen_sari_i32(tmp, val, 16);
acf930aa 2503 else
e1f3808e 2504 tcg_gen_ext16s_i32(tmp, val);
acf930aa
PB
2505 } else {
2506 if (upper)
e1f3808e 2507 tcg_gen_shri_i32(tmp, val, 16);
acf930aa 2508 else
e1f3808e 2509 tcg_gen_ext16u_i32(tmp, val);
acf930aa
PB
2510 }
2511 return tmp;
2512}
2513
e1f3808e
PB
2514static void gen_mac_clear_flags(void)
2515{
2516 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR,
2517 ~(MACSR_V | MACSR_Z | MACSR_N | MACSR_EV));
2518}
2519
acf930aa
PB
2520DISAS_INSN(mac)
2521{
e1f3808e
PB
2522 TCGv rx;
2523 TCGv ry;
acf930aa
PB
2524 uint16_t ext;
2525 int acc;
e1f3808e
PB
2526 TCGv tmp;
2527 TCGv addr;
2528 TCGv loadval;
acf930aa 2529 int dual;
e1f3808e
PB
2530 TCGv saved_flags;
2531
a7812ae4
PB
2532 if (!s->done_mac) {
2533 s->mactmp = tcg_temp_new_i64();
2534 s->done_mac = 1;
2535 }
acf930aa 2536
d4d79bb1 2537 ext = cpu_lduw_code(env, s->pc);
acf930aa
PB
2538 s->pc += 2;
2539
2540 acc = ((insn >> 7) & 1) | ((ext >> 3) & 2);
2541 dual = ((insn & 0x30) != 0 && (ext & 3) != 0);
d315c888 2542 if (dual && !m68k_feature(s->env, M68K_FEATURE_CF_EMAC_B)) {
d4d79bb1 2543 disas_undef(env, s, insn);
d315c888
PB
2544 return;
2545 }
acf930aa
PB
2546 if (insn & 0x30) {
2547 /* MAC with load. */
d4d79bb1 2548 tmp = gen_lea(env, s, insn, OS_LONG);
a7812ae4 2549 addr = tcg_temp_new();
e1f3808e 2550 tcg_gen_and_i32(addr, tmp, QREG_MAC_MASK);
acf930aa
PB
2551 /* Load the value now to ensure correct exception behavior.
2552 Perform writeback after reading the MAC inputs. */
2553 loadval = gen_load(s, OS_LONG, addr, 0);
2554
2555 acc ^= 1;
2556 rx = (ext & 0x8000) ? AREG(ext, 12) : DREG(insn, 12);
2557 ry = (ext & 8) ? AREG(ext, 0) : DREG(ext, 0);
2558 } else {
e1f3808e 2559 loadval = addr = NULL_QREG;
acf930aa
PB
2560 rx = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
2561 ry = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
2562 }
2563
e1f3808e
PB
2564 gen_mac_clear_flags();
2565#if 0
acf930aa 2566 l1 = -1;
e1f3808e 2567 /* Disabled because conditional branches clobber temporary vars. */
acf930aa
PB
2568 if ((s->env->macsr & MACSR_OMC) != 0 && !dual) {
2569 /* Skip the multiply if we know we will ignore it. */
2570 l1 = gen_new_label();
a7812ae4 2571 tmp = tcg_temp_new();
e1f3808e 2572 tcg_gen_andi_i32(tmp, QREG_MACSR, 1 << (acc + 8));
acf930aa
PB
2573 gen_op_jmp_nz32(tmp, l1);
2574 }
e1f3808e 2575#endif
acf930aa
PB
2576
2577 if ((ext & 0x0800) == 0) {
2578 /* Word. */
2579 rx = gen_mac_extract_word(s, rx, (ext & 0x80) != 0);
2580 ry = gen_mac_extract_word(s, ry, (ext & 0x40) != 0);
2581 }
2582 if (s->env->macsr & MACSR_FI) {
e1f3808e 2583 gen_helper_macmulf(s->mactmp, cpu_env, rx, ry);
acf930aa
PB
2584 } else {
2585 if (s->env->macsr & MACSR_SU)
e1f3808e 2586 gen_helper_macmuls(s->mactmp, cpu_env, rx, ry);
acf930aa 2587 else
e1f3808e 2588 gen_helper_macmulu(s->mactmp, cpu_env, rx, ry);
acf930aa
PB
2589 switch ((ext >> 9) & 3) {
2590 case 1:
e1f3808e 2591 tcg_gen_shli_i64(s->mactmp, s->mactmp, 1);
acf930aa
PB
2592 break;
2593 case 3:
e1f3808e 2594 tcg_gen_shri_i64(s->mactmp, s->mactmp, 1);
acf930aa
PB
2595 break;
2596 }
2597 }
2598
2599 if (dual) {
2600 /* Save the overflow flag from the multiply. */
a7812ae4 2601 saved_flags = tcg_temp_new();
e1f3808e
PB
2602 tcg_gen_mov_i32(saved_flags, QREG_MACSR);
2603 } else {
2604 saved_flags = NULL_QREG;
acf930aa
PB
2605 }
2606
e1f3808e
PB
2607#if 0
2608 /* Disabled because conditional branches clobber temporary vars. */
acf930aa
PB
2609 if ((s->env->macsr & MACSR_OMC) != 0 && dual) {
2610 /* Skip the accumulate if the value is already saturated. */
2611 l1 = gen_new_label();
a7812ae4 2612 tmp = tcg_temp_new();
351326a6 2613 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
acf930aa
PB
2614 gen_op_jmp_nz32(tmp, l1);
2615 }
e1f3808e 2616#endif
acf930aa
PB
2617
2618 if (insn & 0x100)
e1f3808e 2619 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
acf930aa 2620 else
e1f3808e 2621 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
acf930aa
PB
2622
2623 if (s->env->macsr & MACSR_FI)
e1f3808e 2624 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
acf930aa 2625 else if (s->env->macsr & MACSR_SU)
e1f3808e 2626 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
acf930aa 2627 else
e1f3808e 2628 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
acf930aa 2629
e1f3808e
PB
2630#if 0
2631 /* Disabled because conditional branches clobber temporary vars. */
acf930aa
PB
2632 if (l1 != -1)
2633 gen_set_label(l1);
e1f3808e 2634#endif
acf930aa
PB
2635
2636 if (dual) {
2637 /* Dual accumulate variant. */
2638 acc = (ext >> 2) & 3;
2639 /* Restore the overflow flag from the multiplier. */
e1f3808e
PB
2640 tcg_gen_mov_i32(QREG_MACSR, saved_flags);
2641#if 0
2642 /* Disabled because conditional branches clobber temporary vars. */
acf930aa
PB
2643 if ((s->env->macsr & MACSR_OMC) != 0) {
2644 /* Skip the accumulate if the value is already saturated. */
2645 l1 = gen_new_label();
a7812ae4 2646 tmp = tcg_temp_new();
351326a6 2647 gen_op_and32(tmp, QREG_MACSR, tcg_const_i32(MACSR_PAV0 << acc));
acf930aa
PB
2648 gen_op_jmp_nz32(tmp, l1);
2649 }
e1f3808e 2650#endif
acf930aa 2651 if (ext & 2)
e1f3808e 2652 tcg_gen_sub_i64(MACREG(acc), MACREG(acc), s->mactmp);
acf930aa 2653 else
e1f3808e 2654 tcg_gen_add_i64(MACREG(acc), MACREG(acc), s->mactmp);
acf930aa 2655 if (s->env->macsr & MACSR_FI)
e1f3808e 2656 gen_helper_macsatf(cpu_env, tcg_const_i32(acc));
acf930aa 2657 else if (s->env->macsr & MACSR_SU)
e1f3808e 2658 gen_helper_macsats(cpu_env, tcg_const_i32(acc));
acf930aa 2659 else
e1f3808e
PB
2660 gen_helper_macsatu(cpu_env, tcg_const_i32(acc));
2661#if 0
2662 /* Disabled because conditional branches clobber temporary vars. */
acf930aa
PB
2663 if (l1 != -1)
2664 gen_set_label(l1);
e1f3808e 2665#endif
acf930aa 2666 }
e1f3808e 2667 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(acc));
acf930aa
PB
2668
2669 if (insn & 0x30) {
e1f3808e 2670 TCGv rw;
acf930aa 2671 rw = (insn & 0x40) ? AREG(insn, 9) : DREG(insn, 9);
e1f3808e 2672 tcg_gen_mov_i32(rw, loadval);
acf930aa
PB
2673 /* FIXME: Should address writeback happen with the masked or
2674 unmasked value? */
2675 switch ((insn >> 3) & 7) {
2676 case 3: /* Post-increment. */
e1f3808e 2677 tcg_gen_addi_i32(AREG(insn, 0), addr, 4);
acf930aa
PB
2678 break;
2679 case 4: /* Pre-decrement. */
e1f3808e 2680 tcg_gen_mov_i32(AREG(insn, 0), addr);
acf930aa
PB
2681 }
2682 }
2683}
2684
2685DISAS_INSN(from_mac)
2686{
e1f3808e 2687 TCGv rx;
a7812ae4 2688 TCGv_i64 acc;
e1f3808e 2689 int accnum;
acf930aa
PB
2690
2691 rx = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
e1f3808e
PB
2692 accnum = (insn >> 9) & 3;
2693 acc = MACREG(accnum);
acf930aa 2694 if (s->env->macsr & MACSR_FI) {
a7812ae4 2695 gen_helper_get_macf(rx, cpu_env, acc);
acf930aa 2696 } else if ((s->env->macsr & MACSR_OMC) == 0) {
ecc7b3aa 2697 tcg_gen_extrl_i64_i32(rx, acc);
acf930aa 2698 } else if (s->env->macsr & MACSR_SU) {
e1f3808e 2699 gen_helper_get_macs(rx, acc);
acf930aa 2700 } else {
e1f3808e
PB
2701 gen_helper_get_macu(rx, acc);
2702 }
2703 if (insn & 0x40) {
2704 tcg_gen_movi_i64(acc, 0);
2705 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
acf930aa 2706 }
acf930aa
PB
2707}
2708
2709DISAS_INSN(move_mac)
2710{
e1f3808e 2711 /* FIXME: This can be done without a helper. */
acf930aa 2712 int src;
e1f3808e 2713 TCGv dest;
acf930aa 2714 src = insn & 3;
e1f3808e
PB
2715 dest = tcg_const_i32((insn >> 9) & 3);
2716 gen_helper_mac_move(cpu_env, dest, tcg_const_i32(src));
2717 gen_mac_clear_flags();
2718 gen_helper_mac_set_flags(cpu_env, dest);
acf930aa
PB
2719}
2720
2721DISAS_INSN(from_macsr)
2722{
e1f3808e 2723 TCGv reg;
acf930aa
PB
2724
2725 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
e1f3808e 2726 tcg_gen_mov_i32(reg, QREG_MACSR);
acf930aa
PB
2727}
2728
2729DISAS_INSN(from_mask)
2730{
e1f3808e 2731 TCGv reg;
acf930aa 2732 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
e1f3808e 2733 tcg_gen_mov_i32(reg, QREG_MAC_MASK);
acf930aa
PB
2734}
2735
2736DISAS_INSN(from_mext)
2737{
e1f3808e
PB
2738 TCGv reg;
2739 TCGv acc;
acf930aa 2740 reg = (insn & 8) ? AREG(insn, 0) : DREG(insn, 0);
e1f3808e 2741 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
acf930aa 2742 if (s->env->macsr & MACSR_FI)
e1f3808e 2743 gen_helper_get_mac_extf(reg, cpu_env, acc);
acf930aa 2744 else
e1f3808e 2745 gen_helper_get_mac_exti(reg, cpu_env, acc);
acf930aa
PB
2746}
2747
2748DISAS_INSN(macsr_to_ccr)
2749{
e1f3808e
PB
2750 tcg_gen_movi_i32(QREG_CC_X, 0);
2751 tcg_gen_andi_i32(QREG_CC_DEST, QREG_MACSR, 0xf);
acf930aa
PB
2752 s->cc_op = CC_OP_FLAGS;
2753}
2754
2755DISAS_INSN(to_mac)
2756{
a7812ae4 2757 TCGv_i64 acc;
e1f3808e
PB
2758 TCGv val;
2759 int accnum;
2760 accnum = (insn >> 9) & 3;
2761 acc = MACREG(accnum);
d4d79bb1 2762 SRC_EA(env, val, OS_LONG, 0, NULL);
acf930aa 2763 if (s->env->macsr & MACSR_FI) {
e1f3808e
PB
2764 tcg_gen_ext_i32_i64(acc, val);
2765 tcg_gen_shli_i64(acc, acc, 8);
acf930aa 2766 } else if (s->env->macsr & MACSR_SU) {
e1f3808e 2767 tcg_gen_ext_i32_i64(acc, val);
acf930aa 2768 } else {
e1f3808e 2769 tcg_gen_extu_i32_i64(acc, val);
acf930aa 2770 }
e1f3808e
PB
2771 tcg_gen_andi_i32(QREG_MACSR, QREG_MACSR, ~(MACSR_PAV0 << accnum));
2772 gen_mac_clear_flags();
2773 gen_helper_mac_set_flags(cpu_env, tcg_const_i32(accnum));
acf930aa
PB
2774}
2775
2776DISAS_INSN(to_macsr)
2777{
e1f3808e 2778 TCGv val;
d4d79bb1 2779 SRC_EA(env, val, OS_LONG, 0, NULL);
e1f3808e 2780 gen_helper_set_macsr(cpu_env, val);
acf930aa
PB
2781 gen_lookup_tb(s);
2782}
2783
2784DISAS_INSN(to_mask)
2785{
e1f3808e 2786 TCGv val;
d4d79bb1 2787 SRC_EA(env, val, OS_LONG, 0, NULL);
e1f3808e 2788 tcg_gen_ori_i32(QREG_MAC_MASK, val, 0xffff0000);
acf930aa
PB
2789}
2790
2791DISAS_INSN(to_mext)
2792{
e1f3808e
PB
2793 TCGv val;
2794 TCGv acc;
d4d79bb1 2795 SRC_EA(env, val, OS_LONG, 0, NULL);
e1f3808e 2796 acc = tcg_const_i32((insn & 0x400) ? 2 : 0);
acf930aa 2797 if (s->env->macsr & MACSR_FI)
e1f3808e 2798 gen_helper_set_mac_extf(cpu_env, val, acc);
acf930aa 2799 else if (s->env->macsr & MACSR_SU)
e1f3808e 2800 gen_helper_set_mac_exts(cpu_env, val, acc);
acf930aa 2801 else
e1f3808e 2802 gen_helper_set_mac_extu(cpu_env, val, acc);
acf930aa
PB
2803}
2804
e6e5906b
PB
2805static disas_proc opcode_table[65536];
2806
2807static void
2808register_opcode (disas_proc proc, uint16_t opcode, uint16_t mask)
2809{
2810 int i;
2811 int from;
2812 int to;
2813
2814 /* Sanity check. All set bits must be included in the mask. */
5fc4adf6
PB
2815 if (opcode & ~mask) {
2816 fprintf(stderr,
2817 "qemu internal error: bogus opcode definition %04x/%04x\n",
2818 opcode, mask);
e6e5906b 2819 abort();
5fc4adf6 2820 }
e6e5906b
PB
2821 /* This could probably be cleverer. For now just optimize the case where
2822 the top bits are known. */
2823 /* Find the first zero bit in the mask. */
2824 i = 0x8000;
2825 while ((i & mask) != 0)
2826 i >>= 1;
2827 /* Iterate over all combinations of this and lower bits. */
2828 if (i == 0)
2829 i = 1;
2830 else
2831 i <<= 1;
2832 from = opcode & ~(i - 1);
2833 to = from + i;
0633879f 2834 for (i = from; i < to; i++) {
e6e5906b
PB
2835 if ((i & mask) == opcode)
2836 opcode_table[i] = proc;
0633879f 2837 }
e6e5906b
PB
2838}
2839
2840/* Register m68k opcode handlers. Order is important.
2841 Later insn override earlier ones. */
0402f767 2842void register_m68k_insns (CPUM68KState *env)
e6e5906b 2843{
d315c888 2844#define INSN(name, opcode, mask, feature) do { \
0402f767 2845 if (m68k_feature(env, M68K_FEATURE_##feature)) \
d315c888
PB
2846 register_opcode(disas_##name, 0x##opcode, 0x##mask); \
2847 } while(0)
0402f767
PB
2848 INSN(undef, 0000, 0000, CF_ISA_A);
2849 INSN(arith_im, 0080, fff8, CF_ISA_A);
d315c888 2850 INSN(bitrev, 00c0, fff8, CF_ISA_APLUSC);
0402f767
PB
2851 INSN(bitop_reg, 0100, f1c0, CF_ISA_A);
2852 INSN(bitop_reg, 0140, f1c0, CF_ISA_A);
2853 INSN(bitop_reg, 0180, f1c0, CF_ISA_A);
2854 INSN(bitop_reg, 01c0, f1c0, CF_ISA_A);
2855 INSN(arith_im, 0280, fff8, CF_ISA_A);
d315c888 2856 INSN(byterev, 02c0, fff8, CF_ISA_APLUSC);
0402f767 2857 INSN(arith_im, 0480, fff8, CF_ISA_A);
d315c888 2858 INSN(ff1, 04c0, fff8, CF_ISA_APLUSC);
0402f767
PB
2859 INSN(arith_im, 0680, fff8, CF_ISA_A);
2860 INSN(bitop_im, 0800, ffc0, CF_ISA_A);
2861 INSN(bitop_im, 0840, ffc0, CF_ISA_A);
2862 INSN(bitop_im, 0880, ffc0, CF_ISA_A);
2863 INSN(bitop_im, 08c0, ffc0, CF_ISA_A);
2864 INSN(arith_im, 0a80, fff8, CF_ISA_A);
2865 INSN(arith_im, 0c00, ff38, CF_ISA_A);
2866 INSN(move, 1000, f000, CF_ISA_A);
2867 INSN(move, 2000, f000, CF_ISA_A);
2868 INSN(move, 3000, f000, CF_ISA_A);
d315c888 2869 INSN(strldsr, 40e7, ffff, CF_ISA_APLUSC);
0402f767
PB
2870 INSN(negx, 4080, fff8, CF_ISA_A);
2871 INSN(move_from_sr, 40c0, fff8, CF_ISA_A);
2872 INSN(lea, 41c0, f1c0, CF_ISA_A);
2873 INSN(clr, 4200, ff00, CF_ISA_A);
2874 INSN(undef, 42c0, ffc0, CF_ISA_A);
2875 INSN(move_from_ccr, 42c0, fff8, CF_ISA_A);
2876 INSN(neg, 4480, fff8, CF_ISA_A);
2877 INSN(move_to_ccr, 44c0, ffc0, CF_ISA_A);
2878 INSN(not, 4680, fff8, CF_ISA_A);
2879 INSN(move_to_sr, 46c0, ffc0, CF_ISA_A);
2880 INSN(pea, 4840, ffc0, CF_ISA_A);
2881 INSN(swap, 4840, fff8, CF_ISA_A);
2882 INSN(movem, 48c0, fbc0, CF_ISA_A);
2883 INSN(ext, 4880, fff8, CF_ISA_A);
2884 INSN(ext, 48c0, fff8, CF_ISA_A);
2885 INSN(ext, 49c0, fff8, CF_ISA_A);
2886 INSN(tst, 4a00, ff00, CF_ISA_A);
2887 INSN(tas, 4ac0, ffc0, CF_ISA_B);
2888 INSN(halt, 4ac8, ffff, CF_ISA_A);
2889 INSN(pulse, 4acc, ffff, CF_ISA_A);
2890 INSN(illegal, 4afc, ffff, CF_ISA_A);
2891 INSN(mull, 4c00, ffc0, CF_ISA_A);
2892 INSN(divl, 4c40, ffc0, CF_ISA_A);
2893 INSN(sats, 4c80, fff8, CF_ISA_B);
2894 INSN(trap, 4e40, fff0, CF_ISA_A);
2895 INSN(link, 4e50, fff8, CF_ISA_A);
2896 INSN(unlk, 4e58, fff8, CF_ISA_A);
20dcee94
PB
2897 INSN(move_to_usp, 4e60, fff8, USP);
2898 INSN(move_from_usp, 4e68, fff8, USP);
0402f767
PB
2899 INSN(nop, 4e71, ffff, CF_ISA_A);
2900 INSN(stop, 4e72, ffff, CF_ISA_A);
2901 INSN(rte, 4e73, ffff, CF_ISA_A);
2902 INSN(rts, 4e75, ffff, CF_ISA_A);
2903 INSN(movec, 4e7b, ffff, CF_ISA_A);
2904 INSN(jump, 4e80, ffc0, CF_ISA_A);
2905 INSN(jump, 4ec0, ffc0, CF_ISA_A);
2906 INSN(addsubq, 5180, f1c0, CF_ISA_A);
2907 INSN(scc, 50c0, f0f8, CF_ISA_A);
2908 INSN(addsubq, 5080, f1c0, CF_ISA_A);
2909 INSN(tpf, 51f8, fff8, CF_ISA_A);
d315c888
PB
2910
2911 /* Branch instructions. */
0402f767 2912 INSN(branch, 6000, f000, CF_ISA_A);
d315c888
PB
2913 /* Disable long branch instructions, then add back the ones we want. */
2914 INSN(undef, 60ff, f0ff, CF_ISA_A); /* All long branches. */
2915 INSN(branch, 60ff, f0ff, CF_ISA_B);
2916 INSN(undef, 60ff, ffff, CF_ISA_B); /* bra.l */
2917 INSN(branch, 60ff, ffff, BRAL);
2918
0402f767
PB
2919 INSN(moveq, 7000, f100, CF_ISA_A);
2920 INSN(mvzs, 7100, f100, CF_ISA_B);
2921 INSN(or, 8000, f000, CF_ISA_A);
2922 INSN(divw, 80c0, f0c0, CF_ISA_A);
2923 INSN(addsub, 9000, f000, CF_ISA_A);
2924 INSN(subx, 9180, f1f8, CF_ISA_A);
2925 INSN(suba, 91c0, f1c0, CF_ISA_A);
acf930aa 2926
0402f767 2927 INSN(undef_mac, a000, f000, CF_ISA_A);
acf930aa
PB
2928 INSN(mac, a000, f100, CF_EMAC);
2929 INSN(from_mac, a180, f9b0, CF_EMAC);
2930 INSN(move_mac, a110, f9fc, CF_EMAC);
2931 INSN(from_macsr,a980, f9f0, CF_EMAC);
2932 INSN(from_mask, ad80, fff0, CF_EMAC);
2933 INSN(from_mext, ab80, fbf0, CF_EMAC);
2934 INSN(macsr_to_ccr, a9c0, ffff, CF_EMAC);
2935 INSN(to_mac, a100, f9c0, CF_EMAC);
2936 INSN(to_macsr, a900, ffc0, CF_EMAC);
2937 INSN(to_mext, ab00, fbc0, CF_EMAC);
2938 INSN(to_mask, ad00, ffc0, CF_EMAC);
2939
0402f767
PB
2940 INSN(mov3q, a140, f1c0, CF_ISA_B);
2941 INSN(cmp, b000, f1c0, CF_ISA_B); /* cmp.b */
2942 INSN(cmp, b040, f1c0, CF_ISA_B); /* cmp.w */
2943 INSN(cmpa, b0c0, f1c0, CF_ISA_B); /* cmpa.w */
2944 INSN(cmp, b080, f1c0, CF_ISA_A);
2945 INSN(cmpa, b1c0, f1c0, CF_ISA_A);
2946 INSN(eor, b180, f1c0, CF_ISA_A);
2947 INSN(and, c000, f000, CF_ISA_A);
2948 INSN(mulw, c0c0, f0c0, CF_ISA_A);
2949 INSN(addsub, d000, f000, CF_ISA_A);
2950 INSN(addx, d180, f1f8, CF_ISA_A);
2951 INSN(adda, d1c0, f1c0, CF_ISA_A);
2952 INSN(shift_im, e080, f0f0, CF_ISA_A);
2953 INSN(shift_reg, e0a0, f0f0, CF_ISA_A);
2954 INSN(undef_fpu, f000, f000, CF_ISA_A);
e6e5906b
PB
2955 INSN(fpu, f200, ffc0, CF_FPU);
2956 INSN(fbcc, f280, ffc0, CF_FPU);
0633879f
PB
2957 INSN(frestore, f340, ffc0, CF_FPU);
2958 INSN(fsave, f340, ffc0, CF_FPU);
0402f767
PB
2959 INSN(intouch, f340, ffc0, CF_ISA_A);
2960 INSN(cpushl, f428, ff38, CF_ISA_A);
2961 INSN(wddata, fb00, ff00, CF_ISA_A);
2962 INSN(wdebug, fbc0, ffc0, CF_ISA_A);
e6e5906b
PB
2963#undef INSN
2964}
2965
2966/* ??? Some of this implementation is not exception safe. We should always
2967 write back the result to memory before setting the condition codes. */
2b3e3cfe 2968static void disas_m68k_insn(CPUM68KState * env, DisasContext *s)
e6e5906b
PB
2969{
2970 uint16_t insn;
2971
d4d79bb1 2972 insn = cpu_lduw_code(env, s->pc);
e6e5906b
PB
2973 s->pc += 2;
2974
d4d79bb1 2975 opcode_table[insn](env, s, insn);
e6e5906b
PB
2976}
2977
e6e5906b 2978/* generate intermediate code for basic block 'tb'. */
4e5e1215 2979void gen_intermediate_code(CPUM68KState *env, TranslationBlock *tb)
e6e5906b 2980{
4e5e1215 2981 M68kCPU *cpu = m68k_env_get_cpu(env);
ed2803da 2982 CPUState *cs = CPU(cpu);
e6e5906b 2983 DisasContext dc1, *dc = &dc1;
e6e5906b
PB
2984 target_ulong pc_start;
2985 int pc_offset;
2e70f6ef
PB
2986 int num_insns;
2987 int max_insns;
e6e5906b
PB
2988
2989 /* generate intermediate code */
2990 pc_start = tb->pc;
3b46e624 2991
e6e5906b
PB
2992 dc->tb = tb;
2993
e6dbd3b3 2994 dc->env = env;
e6e5906b
PB
2995 dc->is_jmp = DISAS_NEXT;
2996 dc->pc = pc_start;
2997 dc->cc_op = CC_OP_DYNAMIC;
ed2803da 2998 dc->singlestep_enabled = cs->singlestep_enabled;
e6e5906b 2999 dc->fpcr = env->fpcr;
0633879f 3000 dc->user = (env->sr & SR_S) == 0;
a7812ae4 3001 dc->done_mac = 0;
2e70f6ef
PB
3002 num_insns = 0;
3003 max_insns = tb->cflags & CF_COUNT_MASK;
190ce7fb 3004 if (max_insns == 0) {
2e70f6ef 3005 max_insns = CF_COUNT_MASK;
190ce7fb
RH
3006 }
3007 if (max_insns > TCG_MAX_INSNS) {
3008 max_insns = TCG_MAX_INSNS;
3009 }
2e70f6ef 3010
cd42d5b2 3011 gen_tb_start(tb);
e6e5906b 3012 do {
e6e5906b
PB
3013 pc_offset = dc->pc - pc_start;
3014 gen_throws_exception = NULL;
667b8e29 3015 tcg_gen_insn_start(dc->pc);
959082fc 3016 num_insns++;
667b8e29 3017
b933066a
RH
3018 if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
3019 gen_exception(dc, dc->pc, EXCP_DEBUG);
3020 dc->is_jmp = DISAS_JUMP;
522a0d4e
RH
3021 /* The address covered by the breakpoint must be included in
3022 [tb->pc, tb->pc + tb->size) in order to for it to be
3023 properly cleared -- thus we increment the PC here so that
3024 the logic setting tb->size below does the right thing. */
3025 dc->pc += 2;
b933066a
RH
3026 break;
3027 }
3028
959082fc 3029 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
2e70f6ef 3030 gen_io_start();
667b8e29
RH
3031 }
3032
510ff0b7 3033 dc->insn_pc = dc->pc;
e6e5906b 3034 disas_m68k_insn(env, dc);
fe700adb 3035 } while (!dc->is_jmp && !tcg_op_buf_full() &&
ed2803da 3036 !cs->singlestep_enabled &&
1b530a6d 3037 !singlestep &&
2e70f6ef
PB
3038 (pc_offset) < (TARGET_PAGE_SIZE - 32) &&
3039 num_insns < max_insns);
e6e5906b 3040
2e70f6ef
PB
3041 if (tb->cflags & CF_LAST_IO)
3042 gen_io_end();
ed2803da 3043 if (unlikely(cs->singlestep_enabled)) {
e6e5906b
PB
3044 /* Make sure the pc is updated, and raise a debug exception. */
3045 if (!dc->is_jmp) {
3046 gen_flush_cc_op(dc);
e1f3808e 3047 tcg_gen_movi_i32(QREG_PC, dc->pc);
e6e5906b 3048 }
31871141 3049 gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));
e6e5906b
PB
3050 } else {
3051 switch(dc->is_jmp) {
3052 case DISAS_NEXT:
3053 gen_flush_cc_op(dc);
3054 gen_jmp_tb(dc, 0, dc->pc);
3055 break;
3056 default:
3057 case DISAS_JUMP:
3058 case DISAS_UPDATE:
3059 gen_flush_cc_op(dc);
3060 /* indicate that the hash table must be used to find the next TB */
57fec1fe 3061 tcg_gen_exit_tb(0);
e6e5906b
PB
3062 break;
3063 case DISAS_TB_JUMP:
3064 /* nothing more to generate */
3065 break;
3066 }
3067 }
806f352d 3068 gen_tb_end(tb, num_insns);
e6e5906b
PB
3069
3070#ifdef DEBUG_DISAS
4910e6e4
RH
3071 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
3072 && qemu_log_in_addr_range(pc_start)) {
93fcfe39
AL
3073 qemu_log("----------------\n");
3074 qemu_log("IN: %s\n", lookup_symbol(pc_start));
d49190c4 3075 log_target_disas(cs, pc_start, dc->pc - pc_start, 0);
93fcfe39 3076 qemu_log("\n");
e6e5906b
PB
3077 }
3078#endif
4e5e1215
RH
3079 tb->size = dc->pc - pc_start;
3080 tb->icount = num_insns;
e6e5906b
PB
3081}
3082
878096ee
AF
3083void m68k_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
3084 int flags)
e6e5906b 3085{
878096ee
AF
3086 M68kCPU *cpu = M68K_CPU(cs);
3087 CPUM68KState *env = &cpu->env;
e6e5906b
PB
3088 int i;
3089 uint16_t sr;
3090 CPU_DoubleU u;
3091 for (i = 0; i < 8; i++)
3092 {
3093 u.d = env->fregs[i];
3094 cpu_fprintf (f, "D%d = %08x A%d = %08x F%d = %08x%08x (%12g)\n",
3095 i, env->dregs[i], i, env->aregs[i],
8fc7cc58 3096 i, u.l.upper, u.l.lower, *(double *)&u.d);
e6e5906b
PB
3097 }
3098 cpu_fprintf (f, "PC = %08x ", env->pc);
3099 sr = env->sr;
3100 cpu_fprintf (f, "SR = %04x %c%c%c%c%c ", sr, (sr & 0x10) ? 'X' : '-',
3101 (sr & CCF_N) ? 'N' : '-', (sr & CCF_Z) ? 'Z' : '-',
3102 (sr & CCF_V) ? 'V' : '-', (sr & CCF_C) ? 'C' : '-');
8fc7cc58 3103 cpu_fprintf (f, "FPRESULT = %12g\n", *(double *)&env->fp_result);
e6e5906b
PB
3104}
3105
bad729e2
RH
3106void restore_state_to_opc(CPUM68KState *env, TranslationBlock *tb,
3107 target_ulong *data)
d2856f1a 3108{
bad729e2 3109 env->pc = data[0];
d2856f1a 3110}