4 * Copyright (c) 2019 Yoshinori Sato
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu/bswap.h"
21 #include "qemu/qemu-print.h"
23 #include "exec/exec-all.h"
24 #include "tcg/tcg-op.h"
25 #include "exec/cpu_ldst.h"
26 #include "exec/helper-proto.h"
27 #include "exec/helper-gen.h"
28 #include "exec/translator.h"
29 #include "trace-tcg.h"
32 typedef struct DisasContext
{
33 DisasContextBase base
;
38 typedef struct DisasCompare
{
44 const char *rx_crname(uint8_t cr
)
46 static const char *cr_names
[] = {
47 "psw", "pc", "usp", "fpsw", "", "", "", "",
48 "bpsw", "bpc", "isp", "fintv", "intb", "", "", ""
50 if (cr
>= ARRAY_SIZE(cr_names
)) {
56 /* Target-specific values for dc->base.is_jmp. */
57 #define DISAS_JUMP DISAS_TARGET_0
58 #define DISAS_UPDATE DISAS_TARGET_1
59 #define DISAS_EXIT DISAS_TARGET_2
61 /* global register indexes */
62 static TCGv cpu_regs
[16];
63 static TCGv cpu_psw_o
, cpu_psw_s
, cpu_psw_z
, cpu_psw_c
;
64 static TCGv cpu_psw_i
, cpu_psw_pm
, cpu_psw_u
, cpu_psw_ipl
;
65 static TCGv cpu_usp
, cpu_fpsw
, cpu_bpsw
, cpu_bpc
, cpu_isp
;
66 static TCGv cpu_fintv
, cpu_intb
, cpu_pc
;
67 static TCGv_i64 cpu_acc
;
69 #define cpu_sp cpu_regs[0]
71 #include "exec/gen-icount.h"
74 static uint32_t decode_load_bytes(DisasContext
*ctx
, uint32_t insn
,
78 uint8_t b
= cpu_ldub_code(ctx
->env
, ctx
->base
.pc_next
++);
79 insn
|= b
<< (32 - i
* 8);
84 static uint32_t li(DisasContext
*ctx
, int sz
)
87 CPURXState
*env
= ctx
->env
;
88 addr
= ctx
->base
.pc_next
;
90 tcg_debug_assert(sz
< 4);
93 ctx
->base
.pc_next
+= 1;
94 return cpu_ldsb_code(env
, addr
);
96 ctx
->base
.pc_next
+= 2;
97 return cpu_ldsw_code(env
, addr
);
99 ctx
->base
.pc_next
+= 3;
100 tmp
= cpu_ldsb_code(env
, addr
+ 2) << 16;
101 tmp
|= cpu_lduw_code(env
, addr
) & 0xffff;
104 ctx
->base
.pc_next
+= 4;
105 return cpu_ldl_code(env
, addr
);
110 static int bdsp_s(DisasContext
*ctx
, int d
)
126 /* Include the auto-generated decoder. */
127 #include "decode.inc.c"
129 void rx_cpu_dump_state(CPUState
*cs
, FILE *f
, int flags
)
131 RXCPU
*cpu
= RXCPU(cs
);
132 CPURXState
*env
= &cpu
->env
;
136 psw
= rx_cpu_pack_psw(env
);
137 qemu_fprintf(f
, "pc=0x%08x psw=0x%08x\n",
139 for (i
= 0; i
< 16; i
+= 4) {
140 qemu_fprintf(f
, "r%d=0x%08x r%d=0x%08x r%d=0x%08x r%d=0x%08x\n",
141 i
, env
->regs
[i
], i
+ 1, env
->regs
[i
+ 1],
142 i
+ 2, env
->regs
[i
+ 2], i
+ 3, env
->regs
[i
+ 3]);
146 static bool use_goto_tb(DisasContext
*dc
, target_ulong dest
)
148 if (unlikely(dc
->base
.singlestep_enabled
)) {
155 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
157 if (use_goto_tb(dc
, dest
)) {
159 tcg_gen_movi_i32(cpu_pc
, dest
);
160 tcg_gen_exit_tb(dc
->base
.tb
, n
);
162 tcg_gen_movi_i32(cpu_pc
, dest
);
163 if (dc
->base
.singlestep_enabled
) {
164 gen_helper_debug(cpu_env
);
166 tcg_gen_lookup_and_goto_ptr();
169 dc
->base
.is_jmp
= DISAS_NORETURN
;
172 /* generic load wrapper */
173 static inline void rx_gen_ld(unsigned int size
, TCGv reg
, TCGv mem
)
175 tcg_gen_qemu_ld_i32(reg
, mem
, 0, size
| MO_SIGN
| MO_TE
);
178 /* unsigned load wrapper */
179 static inline void rx_gen_ldu(unsigned int size
, TCGv reg
, TCGv mem
)
181 tcg_gen_qemu_ld_i32(reg
, mem
, 0, size
| MO_TE
);
184 /* generic store wrapper */
185 static inline void rx_gen_st(unsigned int size
, TCGv reg
, TCGv mem
)
187 tcg_gen_qemu_st_i32(reg
, mem
, 0, size
| MO_TE
);
191 static inline void rx_gen_regindex(DisasContext
*ctx
, TCGv mem
,
192 int size
, int ri
, int rb
)
194 tcg_gen_shli_i32(mem
, cpu_regs
[ri
], size
);
195 tcg_gen_add_i32(mem
, mem
, cpu_regs
[rb
]);
199 static inline TCGv
rx_index_addr(DisasContext
*ctx
, TCGv mem
,
200 int ld
, int size
, int reg
)
204 tcg_debug_assert(ld
< 3);
207 return cpu_regs
[reg
];
209 dsp
= cpu_ldub_code(ctx
->env
, ctx
->base
.pc_next
) << size
;
210 tcg_gen_addi_i32(mem
, cpu_regs
[reg
], dsp
);
211 ctx
->base
.pc_next
+= 1;
214 dsp
= cpu_lduw_code(ctx
->env
, ctx
->base
.pc_next
) << size
;
215 tcg_gen_addi_i32(mem
, cpu_regs
[reg
], dsp
);
216 ctx
->base
.pc_next
+= 2;
222 static inline MemOp
mi_to_mop(unsigned mi
)
224 static const MemOp mop
[5] = { MO_SB
, MO_SW
, MO_UL
, MO_UW
, MO_UB
};
225 tcg_debug_assert(mi
< 5);
229 /* load source operand */
230 static inline TCGv
rx_load_source(DisasContext
*ctx
, TCGv mem
,
231 int ld
, int mi
, int rs
)
237 addr
= rx_index_addr(ctx
, mem
, ld
, mop
& MO_SIZE
, rs
);
238 tcg_gen_qemu_ld_i32(mem
, addr
, 0, mop
| MO_TE
);
245 /* Processor mode check */
246 static int is_privileged(DisasContext
*ctx
, int is_exception
)
248 if (FIELD_EX32(ctx
->base
.tb
->flags
, PSW
, PM
)) {
250 gen_helper_raise_privilege_violation(cpu_env
);
258 /* generate QEMU condition */
259 static void psw_cond(DisasCompare
*dc
, uint32_t cond
)
261 tcg_debug_assert(cond
< 16);
264 dc
->cond
= TCG_COND_EQ
;
265 dc
->value
= cpu_psw_z
;
268 dc
->cond
= TCG_COND_NE
;
269 dc
->value
= cpu_psw_z
;
272 dc
->cond
= TCG_COND_NE
;
273 dc
->value
= cpu_psw_c
;
276 dc
->cond
= TCG_COND_EQ
;
277 dc
->value
= cpu_psw_c
;
279 case 4: /* gtu (C& ~Z) == 1 */
280 case 5: /* leu (C& ~Z) == 0 */
281 tcg_gen_setcondi_i32(TCG_COND_NE
, dc
->temp
, cpu_psw_z
, 0);
282 tcg_gen_and_i32(dc
->temp
, dc
->temp
, cpu_psw_c
);
283 dc
->cond
= (cond
== 4) ? TCG_COND_NE
: TCG_COND_EQ
;
284 dc
->value
= dc
->temp
;
286 case 6: /* pz (S == 0) */
287 dc
->cond
= TCG_COND_GE
;
288 dc
->value
= cpu_psw_s
;
290 case 7: /* n (S == 1) */
291 dc
->cond
= TCG_COND_LT
;
292 dc
->value
= cpu_psw_s
;
294 case 8: /* ge (S^O)==0 */
295 case 9: /* lt (S^O)==1 */
296 tcg_gen_xor_i32(dc
->temp
, cpu_psw_o
, cpu_psw_s
);
297 dc
->cond
= (cond
== 8) ? TCG_COND_GE
: TCG_COND_LT
;
298 dc
->value
= dc
->temp
;
300 case 10: /* gt ((S^O)|Z)==0 */
301 case 11: /* le ((S^O)|Z)==1 */
302 tcg_gen_xor_i32(dc
->temp
, cpu_psw_o
, cpu_psw_s
);
303 tcg_gen_sari_i32(dc
->temp
, dc
->temp
, 31);
304 tcg_gen_andc_i32(dc
->temp
, cpu_psw_z
, dc
->temp
);
305 dc
->cond
= (cond
== 10) ? TCG_COND_NE
: TCG_COND_EQ
;
306 dc
->value
= dc
->temp
;
309 dc
->cond
= TCG_COND_LT
;
310 dc
->value
= cpu_psw_o
;
313 dc
->cond
= TCG_COND_GE
;
314 dc
->value
= cpu_psw_o
;
316 case 14: /* always true */
317 dc
->cond
= TCG_COND_ALWAYS
;
318 dc
->value
= dc
->temp
;
320 case 15: /* always false */
321 dc
->cond
= TCG_COND_NEVER
;
322 dc
->value
= dc
->temp
;
327 static void move_from_cr(TCGv ret
, int cr
, uint32_t pc
)
329 TCGv z
= tcg_const_i32(0);
332 gen_helper_pack_psw(ret
, cpu_env
);
335 tcg_gen_movi_i32(ret
, pc
);
338 tcg_gen_movcond_i32(TCG_COND_NE
, ret
,
339 cpu_psw_u
, z
, cpu_sp
, cpu_usp
);
342 tcg_gen_mov_i32(ret
, cpu_fpsw
);
345 tcg_gen_mov_i32(ret
, cpu_bpsw
);
348 tcg_gen_mov_i32(ret
, cpu_bpc
);
351 tcg_gen_movcond_i32(TCG_COND_EQ
, ret
,
352 cpu_psw_u
, z
, cpu_sp
, cpu_isp
);
355 tcg_gen_mov_i32(ret
, cpu_fintv
);
358 tcg_gen_mov_i32(ret
, cpu_intb
);
361 qemu_log_mask(LOG_GUEST_ERROR
, "Unimplement control register %d", cr
);
362 /* Unimplement registers return 0 */
363 tcg_gen_movi_i32(ret
, 0);
369 static void move_to_cr(DisasContext
*ctx
, TCGv val
, int cr
)
372 if (cr
>= 8 && !is_privileged(ctx
, 0)) {
373 /* Some control registers can only be written in privileged mode. */
374 qemu_log_mask(LOG_GUEST_ERROR
,
375 "disallow control register write %s", rx_crname(cr
));
378 z
= tcg_const_i32(0);
381 gen_helper_set_psw(cpu_env
, val
);
383 /* case 1: to PC not supported */
385 tcg_gen_mov_i32(cpu_usp
, val
);
386 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_sp
,
387 cpu_psw_u
, z
, cpu_usp
, cpu_sp
);
390 gen_helper_set_fpsw(cpu_env
, val
);
393 tcg_gen_mov_i32(cpu_bpsw
, val
);
396 tcg_gen_mov_i32(cpu_bpc
, val
);
399 tcg_gen_mov_i32(cpu_isp
, val
);
400 /* if PSW.U is 0, copy isp to r0 */
401 tcg_gen_movcond_i32(TCG_COND_EQ
, cpu_sp
,
402 cpu_psw_u
, z
, cpu_isp
, cpu_sp
);
405 tcg_gen_mov_i32(cpu_fintv
, val
);
408 tcg_gen_mov_i32(cpu_intb
, val
);
411 qemu_log_mask(LOG_GUEST_ERROR
,
412 "Unimplement control register %d", cr
);
418 static void push(TCGv val
)
420 tcg_gen_subi_i32(cpu_sp
, cpu_sp
, 4);
421 rx_gen_st(MO_32
, val
, cpu_sp
);
424 static void pop(TCGv ret
)
426 rx_gen_ld(MO_32
, ret
, cpu_sp
);
427 tcg_gen_addi_i32(cpu_sp
, cpu_sp
, 4);
430 /* mov.<bwl> rs,dsp5[rd] */
431 static bool trans_MOV_rm(DisasContext
*ctx
, arg_MOV_rm
*a
)
434 mem
= tcg_temp_new();
435 tcg_gen_addi_i32(mem
, cpu_regs
[a
->rd
], a
->dsp
<< a
->sz
);
436 rx_gen_st(a
->sz
, cpu_regs
[a
->rs
], mem
);
441 /* mov.<bwl> dsp5[rs],rd */
442 static bool trans_MOV_mr(DisasContext
*ctx
, arg_MOV_mr
*a
)
445 mem
= tcg_temp_new();
446 tcg_gen_addi_i32(mem
, cpu_regs
[a
->rs
], a
->dsp
<< a
->sz
);
447 rx_gen_ld(a
->sz
, cpu_regs
[a
->rd
], mem
);
452 /* mov.l #uimm4,rd */
453 /* mov.l #uimm8,rd */
455 static bool trans_MOV_ir(DisasContext
*ctx
, arg_MOV_ir
*a
)
457 tcg_gen_movi_i32(cpu_regs
[a
->rd
], a
->imm
);
461 /* mov.<bwl> #uimm8,dsp[rd] */
462 /* mov.<bwl> #imm, dsp[rd] */
463 static bool trans_MOV_im(DisasContext
*ctx
, arg_MOV_im
*a
)
466 imm
= tcg_const_i32(a
->imm
);
467 mem
= tcg_temp_new();
468 tcg_gen_addi_i32(mem
, cpu_regs
[a
->rd
], a
->dsp
<< a
->sz
);
469 rx_gen_st(a
->sz
, imm
, mem
);
475 /* mov.<bwl> [ri,rb],rd */
476 static bool trans_MOV_ar(DisasContext
*ctx
, arg_MOV_ar
*a
)
479 mem
= tcg_temp_new();
480 rx_gen_regindex(ctx
, mem
, a
->sz
, a
->ri
, a
->rb
);
481 rx_gen_ld(a
->sz
, cpu_regs
[a
->rd
], mem
);
486 /* mov.<bwl> rd,[ri,rb] */
487 static bool trans_MOV_ra(DisasContext
*ctx
, arg_MOV_ra
*a
)
490 mem
= tcg_temp_new();
491 rx_gen_regindex(ctx
, mem
, a
->sz
, a
->ri
, a
->rb
);
492 rx_gen_st(a
->sz
, cpu_regs
[a
->rs
], mem
);
497 /* mov.<bwl> dsp[rs],dsp[rd] */
498 /* mov.<bwl> rs,dsp[rd] */
499 /* mov.<bwl> dsp[rs],rd */
500 /* mov.<bwl> rs,rd */
501 static bool trans_MOV_mm(DisasContext
*ctx
, arg_MOV_mm
*a
)
503 static void (* const mov
[])(TCGv ret
, TCGv arg
) = {
504 tcg_gen_ext8s_i32
, tcg_gen_ext16s_i32
, tcg_gen_mov_i32
,
507 if (a
->lds
== 3 && a
->ldd
== 3) {
508 /* mov.<bwl> rs,rd */
509 mov
[a
->sz
](cpu_regs
[a
->rd
], cpu_regs
[a
->rs
]);
513 mem
= tcg_temp_new();
515 /* mov.<bwl> rs,dsp[rd] */
516 addr
= rx_index_addr(ctx
, mem
, a
->ldd
, a
->sz
, a
->rs
);
517 rx_gen_st(a
->sz
, cpu_regs
[a
->rd
], addr
);
518 } else if (a
->ldd
== 3) {
519 /* mov.<bwl> dsp[rs],rd */
520 addr
= rx_index_addr(ctx
, mem
, a
->lds
, a
->sz
, a
->rs
);
521 rx_gen_ld(a
->sz
, cpu_regs
[a
->rd
], addr
);
523 /* mov.<bwl> dsp[rs],dsp[rd] */
524 tmp
= tcg_temp_new();
525 addr
= rx_index_addr(ctx
, mem
, a
->lds
, a
->sz
, a
->rs
);
526 rx_gen_ld(a
->sz
, tmp
, addr
);
527 addr
= rx_index_addr(ctx
, mem
, a
->ldd
, a
->sz
, a
->rd
);
528 rx_gen_st(a
->sz
, tmp
, addr
);
535 /* mov.<bwl> rs,[rd+] */
536 /* mov.<bwl> rs,[-rd] */
537 static bool trans_MOV_rp(DisasContext
*ctx
, arg_MOV_rp
*a
)
540 val
= tcg_temp_new();
541 tcg_gen_mov_i32(val
, cpu_regs
[a
->rs
]);
543 tcg_gen_subi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
545 rx_gen_st(a
->sz
, val
, cpu_regs
[a
->rd
]);
547 tcg_gen_addi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
553 /* mov.<bwl> [rd+],rs */
554 /* mov.<bwl> [-rd],rs */
555 static bool trans_MOV_pr(DisasContext
*ctx
, arg_MOV_pr
*a
)
558 val
= tcg_temp_new();
560 tcg_gen_subi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
562 rx_gen_ld(a
->sz
, val
, cpu_regs
[a
->rd
]);
564 tcg_gen_addi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
566 tcg_gen_mov_i32(cpu_regs
[a
->rs
], val
);
571 /* movu.<bw> dsp5[rs],rd */
572 /* movu.<bw> dsp[rs],rd */
573 static bool trans_MOVU_mr(DisasContext
*ctx
, arg_MOVU_mr
*a
)
576 mem
= tcg_temp_new();
577 tcg_gen_addi_i32(mem
, cpu_regs
[a
->rs
], a
->dsp
<< a
->sz
);
578 rx_gen_ldu(a
->sz
, cpu_regs
[a
->rd
], mem
);
583 /* movu.<bw> rs,rd */
584 static bool trans_MOVU_rr(DisasContext
*ctx
, arg_MOVU_rr
*a
)
586 static void (* const ext
[])(TCGv ret
, TCGv arg
) = {
587 tcg_gen_ext8u_i32
, tcg_gen_ext16u_i32
,
589 ext
[a
->sz
](cpu_regs
[a
->rd
], cpu_regs
[a
->rs
]);
593 /* movu.<bw> [ri,rb],rd */
594 static bool trans_MOVU_ar(DisasContext
*ctx
, arg_MOVU_ar
*a
)
597 mem
= tcg_temp_new();
598 rx_gen_regindex(ctx
, mem
, a
->sz
, a
->ri
, a
->rb
);
599 rx_gen_ldu(a
->sz
, cpu_regs
[a
->rd
], mem
);
604 /* movu.<bw> [rd+],rs */
605 /* mov.<bw> [-rd],rs */
606 static bool trans_MOVU_pr(DisasContext
*ctx
, arg_MOVU_pr
*a
)
609 val
= tcg_temp_new();
611 tcg_gen_subi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
613 rx_gen_ldu(a
->sz
, val
, cpu_regs
[a
->rd
]);
615 tcg_gen_addi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1 << a
->sz
);
617 tcg_gen_mov_i32(cpu_regs
[a
->rs
], val
);
624 static bool trans_POP(DisasContext
*ctx
, arg_POP
*a
)
626 /* mov.l [r0+], rd */
632 trans_MOV_pr(ctx
, &mov_a
);
637 static bool trans_POPC(DisasContext
*ctx
, arg_POPC
*a
)
640 val
= tcg_temp_new();
642 move_to_cr(ctx
, val
, a
->cr
);
643 if (a
->cr
== 0 && is_privileged(ctx
, 0)) {
644 /* PSW.I may be updated here. exit TB. */
645 ctx
->base
.is_jmp
= DISAS_UPDATE
;
652 static bool trans_POPM(DisasContext
*ctx
, arg_POPM
*a
)
655 if (a
->rd
== 0 || a
->rd
>= a
->rd2
) {
656 qemu_log_mask(LOG_GUEST_ERROR
,
657 "Invalid register ranges r%d-r%d", a
->rd
, a
->rd2
);
660 while (r
<= a
->rd2
&& r
< 16) {
668 static bool trans_PUSH_r(DisasContext
*ctx
, arg_PUSH_r
*a
)
671 val
= tcg_temp_new();
672 tcg_gen_mov_i32(val
, cpu_regs
[a
->rs
]);
673 tcg_gen_subi_i32(cpu_sp
, cpu_sp
, 4);
674 rx_gen_st(a
->sz
, val
, cpu_sp
);
679 /* push.<bwl> dsp[rs] */
680 static bool trans_PUSH_m(DisasContext
*ctx
, arg_PUSH_m
*a
)
683 mem
= tcg_temp_new();
684 val
= tcg_temp_new();
685 addr
= rx_index_addr(ctx
, mem
, a
->ld
, a
->sz
, a
->rs
);
686 rx_gen_ld(a
->sz
, val
, addr
);
687 tcg_gen_subi_i32(cpu_sp
, cpu_sp
, 4);
688 rx_gen_st(a
->sz
, val
, cpu_sp
);
695 static bool trans_PUSHC(DisasContext
*ctx
, arg_PUSHC
*a
)
698 val
= tcg_temp_new();
699 move_from_cr(val
, a
->cr
, ctx
->pc
);
706 static bool trans_PUSHM(DisasContext
*ctx
, arg_PUSHM
*a
)
710 if (a
->rs
== 0 || a
->rs
>= a
->rs2
) {
711 qemu_log_mask(LOG_GUEST_ERROR
,
712 "Invalid register ranges r%d-r%d", a
->rs
, a
->rs2
);
715 while (r
>= a
->rs
&& r
>= 0) {
722 static bool trans_XCHG_rr(DisasContext
*ctx
, arg_XCHG_rr
*a
)
725 tmp
= tcg_temp_new();
726 tcg_gen_mov_i32(tmp
, cpu_regs
[a
->rs
]);
727 tcg_gen_mov_i32(cpu_regs
[a
->rs
], cpu_regs
[a
->rd
]);
728 tcg_gen_mov_i32(cpu_regs
[a
->rd
], tmp
);
733 /* xchg dsp[rs].<mi>,rd */
734 static bool trans_XCHG_mr(DisasContext
*ctx
, arg_XCHG_mr
*a
)
737 mem
= tcg_temp_new();
739 case 0: /* dsp[rs].b */
740 case 1: /* dsp[rs].w */
741 case 2: /* dsp[rs].l */
742 addr
= rx_index_addr(ctx
, mem
, a
->ld
, a
->mi
, a
->rs
);
744 case 3: /* dsp[rs].uw */
745 case 4: /* dsp[rs].ub */
746 addr
= rx_index_addr(ctx
, mem
, a
->ld
, 4 - a
->mi
, a
->rs
);
749 g_assert_not_reached();
751 tcg_gen_atomic_xchg_i32(cpu_regs
[a
->rd
], addr
, cpu_regs
[a
->rd
],
752 0, mi_to_mop(a
->mi
));
757 static inline void stcond(TCGCond cond
, int rd
, int imm
)
761 z
= tcg_const_i32(0);
762 _imm
= tcg_const_i32(imm
);
763 tcg_gen_movcond_i32(cond
, cpu_regs
[rd
], cpu_psw_z
, z
,
770 static bool trans_STZ(DisasContext
*ctx
, arg_STZ
*a
)
772 stcond(TCG_COND_EQ
, a
->rd
, a
->imm
);
777 static bool trans_STNZ(DisasContext
*ctx
, arg_STNZ
*a
)
779 stcond(TCG_COND_NE
, a
->rd
, a
->imm
);
784 /* sccnd.<bwl> dsp:[rd] */
785 static bool trans_SCCnd(DisasContext
*ctx
, arg_SCCnd
*a
)
789 dc
.temp
= tcg_temp_new();
790 psw_cond(&dc
, a
->cd
);
792 val
= tcg_temp_new();
793 mem
= tcg_temp_new();
794 tcg_gen_setcondi_i32(dc
.cond
, val
, dc
.value
, 0);
795 addr
= rx_index_addr(ctx
, mem
, a
->sz
, a
->ld
, a
->rd
);
796 rx_gen_st(a
->sz
, val
, addr
);
800 tcg_gen_setcondi_i32(dc
.cond
, cpu_regs
[a
->rd
], dc
.value
, 0);
802 tcg_temp_free(dc
.temp
);
807 static bool trans_RTSD_i(DisasContext
*ctx
, arg_RTSD_i
*a
)
809 tcg_gen_addi_i32(cpu_sp
, cpu_sp
, a
->imm
<< 2);
811 ctx
->base
.is_jmp
= DISAS_JUMP
;
815 /* rtsd #imm, rd-rd2 */
816 static bool trans_RTSD_irr(DisasContext
*ctx
, arg_RTSD_irr
*a
)
821 if (a
->rd2
>= a
->rd
) {
822 adj
= a
->imm
- (a
->rd2
- a
->rd
+ 1);
824 adj
= a
->imm
- (15 - a
->rd
+ 1);
827 tcg_gen_addi_i32(cpu_sp
, cpu_sp
, adj
<< 2);
829 while (dst
<= a
->rd2
&& dst
< 16) {
830 pop(cpu_regs
[dst
++]);
833 ctx
->base
.is_jmp
= DISAS_JUMP
;
837 typedef void (*op2fn
)(TCGv ret
, TCGv arg1
);
838 typedef void (*op3fn
)(TCGv ret
, TCGv arg1
, TCGv arg2
);
840 static inline void rx_gen_op_rr(op2fn opr
, int dst
, int src
)
842 opr(cpu_regs
[dst
], cpu_regs
[src
]);
845 static inline void rx_gen_op_rrr(op3fn opr
, int dst
, int src
, int src2
)
847 opr(cpu_regs
[dst
], cpu_regs
[src
], cpu_regs
[src2
]);
850 static inline void rx_gen_op_irr(op3fn opr
, int dst
, int src
, uint32_t src2
)
852 TCGv imm
= tcg_const_i32(src2
);
853 opr(cpu_regs
[dst
], cpu_regs
[src
], imm
);
857 static inline void rx_gen_op_mr(op3fn opr
, DisasContext
*ctx
,
858 int dst
, int src
, int ld
, int mi
)
861 mem
= tcg_temp_new();
862 val
= rx_load_source(ctx
, mem
, ld
, mi
, src
);
863 opr(cpu_regs
[dst
], cpu_regs
[dst
], val
);
867 static void rx_and(TCGv ret
, TCGv arg1
, TCGv arg2
)
869 tcg_gen_and_i32(cpu_psw_s
, arg1
, arg2
);
870 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
871 tcg_gen_mov_i32(ret
, cpu_psw_s
);
874 /* and #uimm:4, rd */
876 static bool trans_AND_ir(DisasContext
*ctx
, arg_AND_ir
*a
)
878 rx_gen_op_irr(rx_and
, a
->rd
, a
->rd
, a
->imm
);
882 /* and dsp[rs], rd */
884 static bool trans_AND_mr(DisasContext
*ctx
, arg_AND_mr
*a
)
886 rx_gen_op_mr(rx_and
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
891 static bool trans_AND_rrr(DisasContext
*ctx
, arg_AND_rrr
*a
)
893 rx_gen_op_rrr(rx_and
, a
->rd
, a
->rs
, a
->rs2
);
897 static void rx_or(TCGv ret
, TCGv arg1
, TCGv arg2
)
899 tcg_gen_or_i32(cpu_psw_s
, arg1
, arg2
);
900 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
901 tcg_gen_mov_i32(ret
, cpu_psw_s
);
906 static bool trans_OR_ir(DisasContext
*ctx
, arg_OR_ir
*a
)
908 rx_gen_op_irr(rx_or
, a
->rd
, a
->rd
, a
->imm
);
914 static bool trans_OR_mr(DisasContext
*ctx
, arg_OR_mr
*a
)
916 rx_gen_op_mr(rx_or
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
921 static bool trans_OR_rrr(DisasContext
*ctx
, arg_OR_rrr
*a
)
923 rx_gen_op_rrr(rx_or
, a
->rd
, a
->rs
, a
->rs2
);
927 static void rx_xor(TCGv ret
, TCGv arg1
, TCGv arg2
)
929 tcg_gen_xor_i32(cpu_psw_s
, arg1
, arg2
);
930 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
931 tcg_gen_mov_i32(ret
, cpu_psw_s
);
935 static bool trans_XOR_ir(DisasContext
*ctx
, arg_XOR_ir
*a
)
937 rx_gen_op_irr(rx_xor
, a
->rd
, a
->rd
, a
->imm
);
941 /* xor dsp[rs], rd */
943 static bool trans_XOR_mr(DisasContext
*ctx
, arg_XOR_mr
*a
)
945 rx_gen_op_mr(rx_xor
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
949 static void rx_tst(TCGv ret
, TCGv arg1
, TCGv arg2
)
951 tcg_gen_and_i32(cpu_psw_s
, arg1
, arg2
);
952 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
956 static bool trans_TST_ir(DisasContext
*ctx
, arg_TST_ir
*a
)
958 rx_gen_op_irr(rx_tst
, a
->rd
, a
->rd
, a
->imm
);
962 /* tst dsp[rs], rd */
964 static bool trans_TST_mr(DisasContext
*ctx
, arg_TST_mr
*a
)
966 rx_gen_op_mr(rx_tst
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
970 static void rx_not(TCGv ret
, TCGv arg1
)
972 tcg_gen_not_i32(ret
, arg1
);
973 tcg_gen_mov_i32(cpu_psw_z
, ret
);
974 tcg_gen_mov_i32(cpu_psw_s
, ret
);
979 static bool trans_NOT_rr(DisasContext
*ctx
, arg_NOT_rr
*a
)
981 rx_gen_op_rr(rx_not
, a
->rd
, a
->rs
);
985 static void rx_neg(TCGv ret
, TCGv arg1
)
987 tcg_gen_setcondi_i32(TCG_COND_EQ
, cpu_psw_o
, arg1
, 0x80000000);
988 tcg_gen_neg_i32(ret
, arg1
);
989 tcg_gen_setcondi_i32(TCG_COND_EQ
, cpu_psw_c
, ret
, 0);
990 tcg_gen_mov_i32(cpu_psw_z
, ret
);
991 tcg_gen_mov_i32(cpu_psw_s
, ret
);
997 static bool trans_NEG_rr(DisasContext
*ctx
, arg_NEG_rr
*a
)
999 rx_gen_op_rr(rx_neg
, a
->rd
, a
->rs
);
1003 /* ret = arg1 + arg2 + psw_c */
1004 static void rx_adc(TCGv ret
, TCGv arg1
, TCGv arg2
)
1007 z
= tcg_const_i32(0);
1008 tcg_gen_add2_i32(cpu_psw_s
, cpu_psw_c
, arg1
, z
, cpu_psw_c
, z
);
1009 tcg_gen_add2_i32(cpu_psw_s
, cpu_psw_c
, cpu_psw_s
, cpu_psw_c
, arg2
, z
);
1010 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
1011 tcg_gen_xor_i32(cpu_psw_o
, cpu_psw_s
, arg1
);
1012 tcg_gen_xor_i32(z
, arg1
, arg2
);
1013 tcg_gen_andc_i32(cpu_psw_o
, cpu_psw_o
, z
);
1014 tcg_gen_mov_i32(ret
, cpu_psw_s
);
1019 static bool trans_ADC_ir(DisasContext
*ctx
, arg_ADC_ir
*a
)
1021 rx_gen_op_irr(rx_adc
, a
->rd
, a
->rd
, a
->imm
);
1026 static bool trans_ADC_rr(DisasContext
*ctx
, arg_ADC_rr
*a
)
1028 rx_gen_op_rrr(rx_adc
, a
->rd
, a
->rd
, a
->rs
);
1032 /* adc dsp[rs], rd */
1033 static bool trans_ADC_mr(DisasContext
*ctx
, arg_ADC_mr
*a
)
1039 rx_gen_op_mr(rx_adc
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1043 /* ret = arg1 + arg2 */
1044 static void rx_add(TCGv ret
, TCGv arg1
, TCGv arg2
)
1047 z
= tcg_const_i32(0);
1048 tcg_gen_add2_i32(cpu_psw_s
, cpu_psw_c
, arg1
, z
, arg2
, z
);
1049 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
1050 tcg_gen_xor_i32(cpu_psw_o
, cpu_psw_s
, arg1
);
1051 tcg_gen_xor_i32(z
, arg1
, arg2
);
1052 tcg_gen_andc_i32(cpu_psw_o
, cpu_psw_o
, z
);
1053 tcg_gen_mov_i32(ret
, cpu_psw_s
);
1057 /* add #uimm4, rd */
1058 /* add #imm, rs, rd */
1059 static bool trans_ADD_irr(DisasContext
*ctx
, arg_ADD_irr
*a
)
1061 rx_gen_op_irr(rx_add
, a
->rd
, a
->rs2
, a
->imm
);
1066 /* add dsp[rs], rd */
1067 static bool trans_ADD_mr(DisasContext
*ctx
, arg_ADD_mr
*a
)
1069 rx_gen_op_mr(rx_add
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1073 /* add rs, rs2, rd */
1074 static bool trans_ADD_rrr(DisasContext
*ctx
, arg_ADD_rrr
*a
)
1076 rx_gen_op_rrr(rx_add
, a
->rd
, a
->rs
, a
->rs2
);
1080 /* ret = arg1 - arg2 */
1081 static void rx_sub(TCGv ret
, TCGv arg1
, TCGv arg2
)
1084 tcg_gen_sub_i32(cpu_psw_s
, arg1
, arg2
);
1085 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_s
);
1086 tcg_gen_setcond_i32(TCG_COND_GEU
, cpu_psw_c
, arg1
, arg2
);
1087 tcg_gen_xor_i32(cpu_psw_o
, cpu_psw_s
, arg1
);
1088 temp
= tcg_temp_new_i32();
1089 tcg_gen_xor_i32(temp
, arg1
, arg2
);
1090 tcg_gen_and_i32(cpu_psw_o
, cpu_psw_o
, temp
);
1091 tcg_temp_free_i32(temp
);
1092 /* CMP not requred return */
1094 tcg_gen_mov_i32(ret
, cpu_psw_s
);
1097 static void rx_cmp(TCGv dummy
, TCGv arg1
, TCGv arg2
)
1099 rx_sub(NULL
, arg1
, arg2
);
1101 /* ret = arg1 - arg2 - !psw_c */
1102 /* -> ret = arg1 + ~arg2 + psw_c */
1103 static void rx_sbb(TCGv ret
, TCGv arg1
, TCGv arg2
)
1106 temp
= tcg_temp_new();
1107 tcg_gen_not_i32(temp
, arg2
);
1108 rx_adc(ret
, arg1
, temp
);
1109 tcg_temp_free(temp
);
1112 /* cmp #imm4, rs2 */
1113 /* cmp #imm8, rs2 */
1115 static bool trans_CMP_ir(DisasContext
*ctx
, arg_CMP_ir
*a
)
1117 rx_gen_op_irr(rx_cmp
, 0, a
->rs2
, a
->imm
);
1122 /* cmp dsp[rs], rs2 */
1123 static bool trans_CMP_mr(DisasContext
*ctx
, arg_CMP_mr
*a
)
1125 rx_gen_op_mr(rx_cmp
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1130 static bool trans_SUB_ir(DisasContext
*ctx
, arg_SUB_ir
*a
)
1132 rx_gen_op_irr(rx_sub
, a
->rd
, a
->rd
, a
->imm
);
1137 /* sub dsp[rs], rd */
1138 static bool trans_SUB_mr(DisasContext
*ctx
, arg_SUB_mr
*a
)
1140 rx_gen_op_mr(rx_sub
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1144 /* sub rs2, rs, rd */
1145 static bool trans_SUB_rrr(DisasContext
*ctx
, arg_SUB_rrr
*a
)
1147 rx_gen_op_rrr(rx_sub
, a
->rd
, a
->rs2
, a
->rs
);
1152 static bool trans_SBB_rr(DisasContext
*ctx
, arg_SBB_rr
*a
)
1154 rx_gen_op_rrr(rx_sbb
, a
->rd
, a
->rd
, a
->rs
);
1158 /* sbb dsp[rs], rd */
1159 static bool trans_SBB_mr(DisasContext
*ctx
, arg_SBB_mr
*a
)
1165 rx_gen_op_mr(rx_sbb
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1169 static void rx_abs(TCGv ret
, TCGv arg1
)
1173 neg
= tcg_temp_new();
1174 zero
= tcg_const_i32(0);
1175 tcg_gen_neg_i32(neg
, arg1
);
1176 tcg_gen_movcond_i32(TCG_COND_LT
, ret
, arg1
, zero
, neg
, arg1
);
1178 tcg_temp_free(zero
);
1183 static bool trans_ABS_rr(DisasContext
*ctx
, arg_ABS_rr
*a
)
1185 rx_gen_op_rr(rx_abs
, a
->rd
, a
->rs
);
1190 static bool trans_MAX_ir(DisasContext
*ctx
, arg_MAX_ir
*a
)
1192 rx_gen_op_irr(tcg_gen_smax_i32
, a
->rd
, a
->rd
, a
->imm
);
1197 /* max dsp[rs], rd */
1198 static bool trans_MAX_mr(DisasContext
*ctx
, arg_MAX_mr
*a
)
1200 rx_gen_op_mr(tcg_gen_smax_i32
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1205 static bool trans_MIN_ir(DisasContext
*ctx
, arg_MIN_ir
*a
)
1207 rx_gen_op_irr(tcg_gen_smin_i32
, a
->rd
, a
->rd
, a
->imm
);
1212 /* min dsp[rs], rd */
1213 static bool trans_MIN_mr(DisasContext
*ctx
, arg_MIN_mr
*a
)
1215 rx_gen_op_mr(tcg_gen_smin_i32
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1219 /* mul #uimm4, rd */
1221 static bool trans_MUL_ir(DisasContext
*ctx
, arg_MUL_ir
*a
)
1223 rx_gen_op_irr(tcg_gen_mul_i32
, a
->rd
, a
->rd
, a
->imm
);
1228 /* mul dsp[rs], rd */
1229 static bool trans_MUL_mr(DisasContext
*ctx
, arg_MUL_mr
*a
)
1231 rx_gen_op_mr(tcg_gen_mul_i32
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1235 /* mul rs, rs2, rd */
1236 static bool trans_MUL_rrr(DisasContext
*ctx
, arg_MUL_rrr
*a
)
1238 rx_gen_op_rrr(tcg_gen_mul_i32
, a
->rd
, a
->rs
, a
->rs2
);
1243 static bool trans_EMUL_ir(DisasContext
*ctx
, arg_EMUL_ir
*a
)
1245 TCGv imm
= tcg_const_i32(a
->imm
);
1247 qemu_log_mask(LOG_GUEST_ERROR
, "rd too large %d", a
->rd
);
1249 tcg_gen_muls2_i32(cpu_regs
[a
->rd
], cpu_regs
[(a
->rd
+ 1) & 15],
1250 cpu_regs
[a
->rd
], imm
);
1256 /* emul dsp[rs], rd */
1257 static bool trans_EMUL_mr(DisasContext
*ctx
, arg_EMUL_mr
*a
)
1261 qemu_log_mask(LOG_GUEST_ERROR
, "rd too large %d", a
->rd
);
1263 mem
= tcg_temp_new();
1264 val
= rx_load_source(ctx
, mem
, a
->ld
, a
->mi
, a
->rs
);
1265 tcg_gen_muls2_i32(cpu_regs
[a
->rd
], cpu_regs
[(a
->rd
+ 1) & 15],
1266 cpu_regs
[a
->rd
], val
);
1271 /* emulu #imm, rd */
1272 static bool trans_EMULU_ir(DisasContext
*ctx
, arg_EMULU_ir
*a
)
1274 TCGv imm
= tcg_const_i32(a
->imm
);
1276 qemu_log_mask(LOG_GUEST_ERROR
, "rd too large %d", a
->rd
);
1278 tcg_gen_mulu2_i32(cpu_regs
[a
->rd
], cpu_regs
[(a
->rd
+ 1) & 15],
1279 cpu_regs
[a
->rd
], imm
);
1285 /* emulu dsp[rs], rd */
1286 static bool trans_EMULU_mr(DisasContext
*ctx
, arg_EMULU_mr
*a
)
1290 qemu_log_mask(LOG_GUEST_ERROR
, "rd too large %d", a
->rd
);
1292 mem
= tcg_temp_new();
1293 val
= rx_load_source(ctx
, mem
, a
->ld
, a
->mi
, a
->rs
);
1294 tcg_gen_mulu2_i32(cpu_regs
[a
->rd
], cpu_regs
[(a
->rd
+ 1) & 15],
1295 cpu_regs
[a
->rd
], val
);
1300 static void rx_div(TCGv ret
, TCGv arg1
, TCGv arg2
)
1302 gen_helper_div(ret
, cpu_env
, arg1
, arg2
);
1305 static void rx_divu(TCGv ret
, TCGv arg1
, TCGv arg2
)
1307 gen_helper_divu(ret
, cpu_env
, arg1
, arg2
);
1311 static bool trans_DIV_ir(DisasContext
*ctx
, arg_DIV_ir
*a
)
1313 rx_gen_op_irr(rx_div
, a
->rd
, a
->rd
, a
->imm
);
1318 /* div dsp[rs], rd */
1319 static bool trans_DIV_mr(DisasContext
*ctx
, arg_DIV_mr
*a
)
1321 rx_gen_op_mr(rx_div
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1326 static bool trans_DIVU_ir(DisasContext
*ctx
, arg_DIVU_ir
*a
)
1328 rx_gen_op_irr(rx_divu
, a
->rd
, a
->rd
, a
->imm
);
1333 /* divu dsp[rs], rd */
1334 static bool trans_DIVU_mr(DisasContext
*ctx
, arg_DIVU_mr
*a
)
1336 rx_gen_op_mr(rx_divu
, ctx
, a
->rd
, a
->rs
, a
->ld
, a
->mi
);
1341 /* shll #imm:5, rd */
1342 /* shll #imm:5, rs2, rd */
1343 static bool trans_SHLL_irr(DisasContext
*ctx
, arg_SHLL_irr
*a
)
1346 tmp
= tcg_temp_new();
1348 tcg_gen_sari_i32(cpu_psw_c
, cpu_regs
[a
->rs2
], 32 - a
->imm
);
1349 tcg_gen_shli_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rs2
], a
->imm
);
1350 tcg_gen_setcondi_i32(TCG_COND_EQ
, cpu_psw_o
, cpu_psw_c
, 0);
1351 tcg_gen_setcondi_i32(TCG_COND_EQ
, tmp
, cpu_psw_c
, 0xffffffff);
1352 tcg_gen_or_i32(cpu_psw_o
, cpu_psw_o
, tmp
);
1353 tcg_gen_setcondi_i32(TCG_COND_NE
, cpu_psw_c
, cpu_psw_c
, 0);
1355 tcg_gen_mov_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rs2
]);
1356 tcg_gen_movi_i32(cpu_psw_c
, 0);
1357 tcg_gen_movi_i32(cpu_psw_o
, 0);
1359 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[a
->rd
]);
1360 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[a
->rd
]);
1365 static bool trans_SHLL_rr(DisasContext
*ctx
, arg_SHLL_rr
*a
)
1367 TCGLabel
*noshift
, *done
;
1370 noshift
= gen_new_label();
1371 done
= gen_new_label();
1372 /* if (cpu_regs[a->rs]) { */
1373 tcg_gen_brcondi_i32(TCG_COND_EQ
, cpu_regs
[a
->rs
], 0, noshift
);
1374 count
= tcg_const_i32(32);
1375 tmp
= tcg_temp_new();
1376 tcg_gen_andi_i32(tmp
, cpu_regs
[a
->rs
], 31);
1377 tcg_gen_sub_i32(count
, count
, tmp
);
1378 tcg_gen_sar_i32(cpu_psw_c
, cpu_regs
[a
->rd
], count
);
1379 tcg_gen_shl_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], tmp
);
1380 tcg_gen_setcondi_i32(TCG_COND_EQ
, cpu_psw_o
, cpu_psw_c
, 0);
1381 tcg_gen_setcondi_i32(TCG_COND_EQ
, tmp
, cpu_psw_c
, 0xffffffff);
1382 tcg_gen_or_i32(cpu_psw_o
, cpu_psw_o
, tmp
);
1383 tcg_gen_setcondi_i32(TCG_COND_NE
, cpu_psw_c
, cpu_psw_c
, 0);
1386 gen_set_label(noshift
);
1387 tcg_gen_movi_i32(cpu_psw_c
, 0);
1388 tcg_gen_movi_i32(cpu_psw_o
, 0);
1390 gen_set_label(done
);
1391 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[a
->rd
]);
1392 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[a
->rd
]);
1393 tcg_temp_free(count
);
1398 static inline void shiftr_imm(uint32_t rd
, uint32_t rs
, uint32_t imm
,
1401 static void (* const gen_sXri
[])(TCGv ret
, TCGv arg1
, int arg2
) = {
1402 tcg_gen_shri_i32
, tcg_gen_sari_i32
,
1404 tcg_debug_assert(alith
< 2);
1406 gen_sXri
[alith
](cpu_regs
[rd
], cpu_regs
[rs
], imm
- 1);
1407 tcg_gen_andi_i32(cpu_psw_c
, cpu_regs
[rd
], 0x00000001);
1408 gen_sXri
[alith
](cpu_regs
[rd
], cpu_regs
[rd
], 1);
1410 tcg_gen_mov_i32(cpu_regs
[rd
], cpu_regs
[rs
]);
1411 tcg_gen_movi_i32(cpu_psw_c
, 0);
1413 tcg_gen_movi_i32(cpu_psw_o
, 0);
1414 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[rd
]);
1415 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[rd
]);
1418 static inline void shiftr_reg(uint32_t rd
, uint32_t rs
, unsigned int alith
)
1420 TCGLabel
*noshift
, *done
;
1422 static void (* const gen_sXri
[])(TCGv ret
, TCGv arg1
, int arg2
) = {
1423 tcg_gen_shri_i32
, tcg_gen_sari_i32
,
1425 static void (* const gen_sXr
[])(TCGv ret
, TCGv arg1
, TCGv arg2
) = {
1426 tcg_gen_shr_i32
, tcg_gen_sar_i32
,
1428 tcg_debug_assert(alith
< 2);
1429 noshift
= gen_new_label();
1430 done
= gen_new_label();
1431 count
= tcg_temp_new();
1432 /* if (cpu_regs[rs]) { */
1433 tcg_gen_brcondi_i32(TCG_COND_EQ
, cpu_regs
[rs
], 0, noshift
);
1434 tcg_gen_andi_i32(count
, cpu_regs
[rs
], 31);
1435 tcg_gen_subi_i32(count
, count
, 1);
1436 gen_sXr
[alith
](cpu_regs
[rd
], cpu_regs
[rd
], count
);
1437 tcg_gen_andi_i32(cpu_psw_c
, cpu_regs
[rd
], 0x00000001);
1438 gen_sXri
[alith
](cpu_regs
[rd
], cpu_regs
[rd
], 1);
1441 gen_set_label(noshift
);
1442 tcg_gen_movi_i32(cpu_psw_c
, 0);
1444 gen_set_label(done
);
1445 tcg_gen_movi_i32(cpu_psw_o
, 0);
1446 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[rd
]);
1447 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[rd
]);
1448 tcg_temp_free(count
);
1451 /* shar #imm:5, rd */
1452 /* shar #imm:5, rs2, rd */
1453 static bool trans_SHAR_irr(DisasContext
*ctx
, arg_SHAR_irr
*a
)
1455 shiftr_imm(a
->rd
, a
->rs2
, a
->imm
, 1);
1460 static bool trans_SHAR_rr(DisasContext
*ctx
, arg_SHAR_rr
*a
)
1462 shiftr_reg(a
->rd
, a
->rs
, 1);
1466 /* shlr #imm:5, rd */
1467 /* shlr #imm:5, rs2, rd */
1468 static bool trans_SHLR_irr(DisasContext
*ctx
, arg_SHLR_irr
*a
)
1470 shiftr_imm(a
->rd
, a
->rs2
, a
->imm
, 0);
1475 static bool trans_SHLR_rr(DisasContext
*ctx
, arg_SHLR_rr
*a
)
1477 shiftr_reg(a
->rd
, a
->rs
, 0);
1482 static bool trans_ROLC(DisasContext
*ctx
, arg_ROLC
*a
)
1485 tmp
= tcg_temp_new();
1486 tcg_gen_shri_i32(tmp
, cpu_regs
[a
->rd
], 31);
1487 tcg_gen_shli_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1);
1488 tcg_gen_or_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], cpu_psw_c
);
1489 tcg_gen_mov_i32(cpu_psw_c
, tmp
);
1490 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[a
->rd
]);
1491 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[a
->rd
]);
1497 static bool trans_RORC(DisasContext
*ctx
, arg_RORC
*a
)
1500 tmp
= tcg_temp_new();
1501 tcg_gen_andi_i32(tmp
, cpu_regs
[a
->rd
], 0x00000001);
1502 tcg_gen_shri_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 1);
1503 tcg_gen_shli_i32(cpu_psw_c
, cpu_psw_c
, 31);
1504 tcg_gen_or_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], cpu_psw_c
);
1505 tcg_gen_mov_i32(cpu_psw_c
, tmp
);
1506 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[a
->rd
]);
1507 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[a
->rd
]);
1511 enum {ROTR
= 0, ROTL
= 1};
1512 enum {ROT_IMM
= 0, ROT_REG
= 1};
1513 static inline void rx_rot(int ir
, int dir
, int rd
, int src
)
1517 if (ir
== ROT_IMM
) {
1518 tcg_gen_rotli_i32(cpu_regs
[rd
], cpu_regs
[rd
], src
);
1520 tcg_gen_rotl_i32(cpu_regs
[rd
], cpu_regs
[rd
], cpu_regs
[src
]);
1522 tcg_gen_andi_i32(cpu_psw_c
, cpu_regs
[rd
], 0x00000001);
1525 if (ir
== ROT_IMM
) {
1526 tcg_gen_rotri_i32(cpu_regs
[rd
], cpu_regs
[rd
], src
);
1528 tcg_gen_rotr_i32(cpu_regs
[rd
], cpu_regs
[rd
], cpu_regs
[src
]);
1530 tcg_gen_shri_i32(cpu_psw_c
, cpu_regs
[rd
], 31);
1533 tcg_gen_mov_i32(cpu_psw_z
, cpu_regs
[rd
]);
1534 tcg_gen_mov_i32(cpu_psw_s
, cpu_regs
[rd
]);
1538 static bool trans_ROTL_ir(DisasContext
*ctx
, arg_ROTL_ir
*a
)
1540 rx_rot(ROT_IMM
, ROTL
, a
->rd
, a
->imm
);
1545 static bool trans_ROTL_rr(DisasContext
*ctx
, arg_ROTL_rr
*a
)
1547 rx_rot(ROT_REG
, ROTL
, a
->rd
, a
->rs
);
1552 static bool trans_ROTR_ir(DisasContext
*ctx
, arg_ROTR_ir
*a
)
1554 rx_rot(ROT_IMM
, ROTR
, a
->rd
, a
->imm
);
1559 static bool trans_ROTR_rr(DisasContext
*ctx
, arg_ROTR_rr
*a
)
1561 rx_rot(ROT_REG
, ROTR
, a
->rd
, a
->rs
);
1566 static bool trans_REVL(DisasContext
*ctx
, arg_REVL
*a
)
1568 tcg_gen_bswap32_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rs
]);
1573 static bool trans_REVW(DisasContext
*ctx
, arg_REVW
*a
)
1576 tmp
= tcg_temp_new();
1577 tcg_gen_andi_i32(tmp
, cpu_regs
[a
->rs
], 0x00ff00ff);
1578 tcg_gen_shli_i32(tmp
, tmp
, 8);
1579 tcg_gen_shri_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rs
], 8);
1580 tcg_gen_andi_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], 0x00ff00ff);
1581 tcg_gen_or_i32(cpu_regs
[a
->rd
], cpu_regs
[a
->rd
], tmp
);
1586 /* conditional branch helper */
1587 static void rx_bcnd_main(DisasContext
*ctx
, int cd
, int dst
)
1594 dc
.temp
= tcg_temp_new();
1596 t
= gen_new_label();
1597 done
= gen_new_label();
1598 tcg_gen_brcondi_i32(dc
.cond
, dc
.value
, 0, t
);
1599 gen_goto_tb(ctx
, 0, ctx
->base
.pc_next
);
1602 gen_goto_tb(ctx
, 1, ctx
->pc
+ dst
);
1603 gen_set_label(done
);
1604 tcg_temp_free(dc
.temp
);
1607 /* always true case */
1608 gen_goto_tb(ctx
, 0, ctx
->pc
+ dst
);
1611 /* always false case */
1617 /* beq dsp:3 / bne dsp:3 */
1618 /* beq dsp:8 / bne dsp:8 */
1619 /* bc dsp:8 / bnc dsp:8 */
1620 /* bgtu dsp:8 / bleu dsp:8 */
1621 /* bpz dsp:8 / bn dsp:8 */
1622 /* bge dsp:8 / blt dsp:8 */
1623 /* bgt dsp:8 / ble dsp:8 */
1624 /* bo dsp:8 / bno dsp:8 */
1625 /* beq dsp:16 / bne dsp:16 */
1626 static bool trans_BCnd(DisasContext
*ctx
, arg_BCnd
*a
)
1628 rx_bcnd_main(ctx
, a
->cd
, a
->dsp
);
1636 static bool trans_BRA(DisasContext
*ctx
, arg_BRA
*a
)
1638 rx_bcnd_main(ctx
, 14, a
->dsp
);
1643 static bool trans_BRA_l(DisasContext
*ctx
, arg_BRA_l
*a
)
1645 tcg_gen_addi_i32(cpu_pc
, cpu_regs
[a
->rd
], ctx
->pc
);
1646 ctx
->base
.is_jmp
= DISAS_JUMP
;
1650 static inline void rx_save_pc(DisasContext
*ctx
)
1652 TCGv pc
= tcg_const_i32(ctx
->base
.pc_next
);
1658 static bool trans_JMP(DisasContext
*ctx
, arg_JMP
*a
)
1660 tcg_gen_mov_i32(cpu_pc
, cpu_regs
[a
->rs
]);
1661 ctx
->base
.is_jmp
= DISAS_JUMP
;
1666 static bool trans_JSR(DisasContext
*ctx
, arg_JSR
*a
)
1669 tcg_gen_mov_i32(cpu_pc
, cpu_regs
[a
->rs
]);
1670 ctx
->base
.is_jmp
= DISAS_JUMP
;
1676 static bool trans_BSR(DisasContext
*ctx
, arg_BSR
*a
)
1679 rx_bcnd_main(ctx
, 14, a
->dsp
);
1684 static bool trans_BSR_l(DisasContext
*ctx
, arg_BSR_l
*a
)
1687 tcg_gen_addi_i32(cpu_pc
, cpu_regs
[a
->rd
], ctx
->pc
);
1688 ctx
->base
.is_jmp
= DISAS_JUMP
;
1693 static bool trans_RTS(DisasContext
*ctx
, arg_RTS
*a
)
1696 ctx
->base
.is_jmp
= DISAS_JUMP
;
1701 static bool trans_NOP(DisasContext
*ctx
, arg_NOP
*a
)
1707 static bool trans_SCMPU(DisasContext
*ctx
, arg_SCMPU
*a
)
1709 gen_helper_scmpu(cpu_env
);
1714 static bool trans_SMOVU(DisasContext
*ctx
, arg_SMOVU
*a
)
1716 gen_helper_smovu(cpu_env
);
1721 static bool trans_SMOVF(DisasContext
*ctx
, arg_SMOVF
*a
)
1723 gen_helper_smovf(cpu_env
);
1728 static bool trans_SMOVB(DisasContext
*ctx
, arg_SMOVB
*a
)
1730 gen_helper_smovb(cpu_env
);
1734 #define STRING(op) \
1736 TCGv size = tcg_const_i32(a->sz); \
1737 gen_helper_##op(cpu_env, size); \
1738 tcg_temp_free(size); \
1742 static bool trans_SUNTIL(DisasContext
*ctx
, arg_SUNTIL
*a
)
1749 static bool trans_SWHILE(DisasContext
*ctx
, arg_SWHILE
*a
)
1755 static bool trans_SSTR(DisasContext
*ctx
, arg_SSTR
*a
)
1762 static bool trans_RMPA(DisasContext
*ctx
, arg_RMPA
*a
)
1768 static void rx_mul64hi(TCGv_i64 ret
, int rs
, int rs2
)
1770 TCGv_i64 tmp0
, tmp1
;
1771 tmp0
= tcg_temp_new_i64();
1772 tmp1
= tcg_temp_new_i64();
1773 tcg_gen_ext_i32_i64(tmp0
, cpu_regs
[rs
]);
1774 tcg_gen_sari_i64(tmp0
, tmp0
, 16);
1775 tcg_gen_ext_i32_i64(tmp1
, cpu_regs
[rs2
]);
1776 tcg_gen_sari_i64(tmp1
, tmp1
, 16);
1777 tcg_gen_mul_i64(ret
, tmp0
, tmp1
);
1778 tcg_gen_shli_i64(ret
, ret
, 16);
1779 tcg_temp_free_i64(tmp0
);
1780 tcg_temp_free_i64(tmp1
);
1783 static void rx_mul64lo(TCGv_i64 ret
, int rs
, int rs2
)
1785 TCGv_i64 tmp0
, tmp1
;
1786 tmp0
= tcg_temp_new_i64();
1787 tmp1
= tcg_temp_new_i64();
1788 tcg_gen_ext_i32_i64(tmp0
, cpu_regs
[rs
]);
1789 tcg_gen_ext16s_i64(tmp0
, tmp0
);
1790 tcg_gen_ext_i32_i64(tmp1
, cpu_regs
[rs2
]);
1791 tcg_gen_ext16s_i64(tmp1
, tmp1
);
1792 tcg_gen_mul_i64(ret
, tmp0
, tmp1
);
1793 tcg_gen_shli_i64(ret
, ret
, 16);
1794 tcg_temp_free_i64(tmp0
);
1795 tcg_temp_free_i64(tmp1
);
1799 static bool trans_MULHI(DisasContext
*ctx
, arg_MULHI
*a
)
1801 rx_mul64hi(cpu_acc
, a
->rs
, a
->rs2
);
1806 static bool trans_MULLO(DisasContext
*ctx
, arg_MULLO
*a
)
1808 rx_mul64lo(cpu_acc
, a
->rs
, a
->rs2
);
1813 static bool trans_MACHI(DisasContext
*ctx
, arg_MACHI
*a
)
1816 tmp
= tcg_temp_new_i64();
1817 rx_mul64hi(tmp
, a
->rs
, a
->rs2
);
1818 tcg_gen_add_i64(cpu_acc
, cpu_acc
, tmp
);
1819 tcg_temp_free_i64(tmp
);
1824 static bool trans_MACLO(DisasContext
*ctx
, arg_MACLO
*a
)
1827 tmp
= tcg_temp_new_i64();
1828 rx_mul64lo(tmp
, a
->rs
, a
->rs2
);
1829 tcg_gen_add_i64(cpu_acc
, cpu_acc
, tmp
);
1830 tcg_temp_free_i64(tmp
);
1835 static bool trans_MVFACHI(DisasContext
*ctx
, arg_MVFACHI
*a
)
1837 tcg_gen_extrh_i64_i32(cpu_regs
[a
->rd
], cpu_acc
);
1842 static bool trans_MVFACMI(DisasContext
*ctx
, arg_MVFACMI
*a
)
1845 rd64
= tcg_temp_new_i64();
1846 tcg_gen_extract_i64(rd64
, cpu_acc
, 16, 32);
1847 tcg_gen_extrl_i64_i32(cpu_regs
[a
->rd
], rd64
);
1848 tcg_temp_free_i64(rd64
);
1853 static bool trans_MVTACHI(DisasContext
*ctx
, arg_MVTACHI
*a
)
1856 rs64
= tcg_temp_new_i64();
1857 tcg_gen_extu_i32_i64(rs64
, cpu_regs
[a
->rs
]);
1858 tcg_gen_deposit_i64(cpu_acc
, cpu_acc
, rs64
, 32, 32);
1859 tcg_temp_free_i64(rs64
);
1864 static bool trans_MVTACLO(DisasContext
*ctx
, arg_MVTACLO
*a
)
1867 rs64
= tcg_temp_new_i64();
1868 tcg_gen_extu_i32_i64(rs64
, cpu_regs
[a
->rs
]);
1869 tcg_gen_deposit_i64(cpu_acc
, cpu_acc
, rs64
, 0, 32);
1870 tcg_temp_free_i64(rs64
);
1875 static bool trans_RACW(DisasContext
*ctx
, arg_RACW
*a
)
1877 TCGv imm
= tcg_const_i32(a
->imm
+ 1);
1878 gen_helper_racw(cpu_env
, imm
);
1884 static bool trans_SAT(DisasContext
*ctx
, arg_SAT
*a
)
1887 tmp
= tcg_temp_new();
1888 z
= tcg_const_i32(0);
1889 /* S == 1 -> 0xffffffff / S == 0 -> 0x00000000 */
1890 tcg_gen_sari_i32(tmp
, cpu_psw_s
, 31);
1891 /* S == 1 -> 0x7fffffff / S == 0 -> 0x80000000 */
1892 tcg_gen_xori_i32(tmp
, tmp
, 0x80000000);
1893 tcg_gen_movcond_i32(TCG_COND_LT
, cpu_regs
[a
->rd
],
1894 cpu_psw_o
, z
, tmp
, cpu_regs
[a
->rd
]);
1901 static bool trans_SATR(DisasContext
*ctx
, arg_SATR
*a
)
1903 gen_helper_satr(cpu_env
);
1907 #define cat3(a, b, c) a##b##c
1908 #define FOP(name, op) \
1909 static bool cat3(trans_, name, _ir)(DisasContext *ctx, \
1910 cat3(arg_, name, _ir) * a) \
1912 TCGv imm = tcg_const_i32(li(ctx, 0)); \
1913 gen_helper_##op(cpu_regs[a->rd], cpu_env, \
1914 cpu_regs[a->rd], imm); \
1915 tcg_temp_free(imm); \
1918 static bool cat3(trans_, name, _mr)(DisasContext *ctx, \
1919 cat3(arg_, name, _mr) * a) \
1922 mem = tcg_temp_new(); \
1923 val = rx_load_source(ctx, mem, a->ld, MO_32, a->rs); \
1924 gen_helper_##op(cpu_regs[a->rd], cpu_env, \
1925 cpu_regs[a->rd], val); \
1926 tcg_temp_free(mem); \
1930 #define FCONVOP(name, op) \
1931 static bool trans_##name(DisasContext *ctx, arg_##name * a) \
1934 mem = tcg_temp_new(); \
1935 val = rx_load_source(ctx, mem, a->ld, MO_32, a->rs); \
1936 gen_helper_##op(cpu_regs[a->rd], cpu_env, val); \
1937 tcg_temp_free(mem); \
1947 static bool trans_FCMP_ir(DisasContext
*ctx
, arg_FCMP_ir
* a
)
1949 TCGv imm
= tcg_const_i32(li(ctx
, 0));
1950 gen_helper_fcmp(cpu_env
, cpu_regs
[a
->rd
], imm
);
1955 /* fcmp dsp[rs], rd */
1957 static bool trans_FCMP_mr(DisasContext
*ctx
, arg_FCMP_mr
*a
)
1960 mem
= tcg_temp_new();
1961 val
= rx_load_source(ctx
, mem
, a
->ld
, MO_32
, a
->rs
);
1962 gen_helper_fcmp(cpu_env
, cpu_regs
[a
->rd
], val
);
1968 FCONVOP(ROUND
, round
)
1971 /* itof dsp[rs], rd */
1972 static bool trans_ITOF(DisasContext
*ctx
, arg_ITOF
* a
)
1975 mem
= tcg_temp_new();
1976 val
= rx_load_source(ctx
, mem
, a
->ld
, a
->mi
, a
->rs
);
1977 gen_helper_itof(cpu_regs
[a
->rd
], cpu_env
, val
);
1982 static void rx_bsetm(TCGv mem
, TCGv mask
)
1985 val
= tcg_temp_new();
1986 rx_gen_ld(MO_8
, val
, mem
);
1987 tcg_gen_or_i32(val
, val
, mask
);
1988 rx_gen_st(MO_8
, val
, mem
);
1992 static void rx_bclrm(TCGv mem
, TCGv mask
)
1995 val
= tcg_temp_new();
1996 rx_gen_ld(MO_8
, val
, mem
);
1997 tcg_gen_andc_i32(val
, val
, mask
);
1998 rx_gen_st(MO_8
, val
, mem
);
2002 static void rx_btstm(TCGv mem
, TCGv mask
)
2005 val
= tcg_temp_new();
2006 rx_gen_ld(MO_8
, val
, mem
);
2007 tcg_gen_and_i32(val
, val
, mask
);
2008 tcg_gen_setcondi_i32(TCG_COND_NE
, cpu_psw_c
, val
, 0);
2009 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_c
);
2013 static void rx_bnotm(TCGv mem
, TCGv mask
)
2016 val
= tcg_temp_new();
2017 rx_gen_ld(MO_8
, val
, mem
);
2018 tcg_gen_xor_i32(val
, val
, mask
);
2019 rx_gen_st(MO_8
, val
, mem
);
2023 static void rx_bsetr(TCGv reg
, TCGv mask
)
2025 tcg_gen_or_i32(reg
, reg
, mask
);
2028 static void rx_bclrr(TCGv reg
, TCGv mask
)
2030 tcg_gen_andc_i32(reg
, reg
, mask
);
2033 static inline void rx_btstr(TCGv reg
, TCGv mask
)
2036 t0
= tcg_temp_new();
2037 tcg_gen_and_i32(t0
, reg
, mask
);
2038 tcg_gen_setcondi_i32(TCG_COND_NE
, cpu_psw_c
, t0
, 0);
2039 tcg_gen_mov_i32(cpu_psw_z
, cpu_psw_c
);
2043 static inline void rx_bnotr(TCGv reg
, TCGv mask
)
2045 tcg_gen_xor_i32(reg
, reg
, mask
);
2048 #define BITOP(name, op) \
2049 static bool cat3(trans_, name, _im)(DisasContext *ctx, \
2050 cat3(arg_, name, _im) * a) \
2052 TCGv mask, mem, addr; \
2053 mem = tcg_temp_new(); \
2054 mask = tcg_const_i32(1 << a->imm); \
2055 addr = rx_index_addr(ctx, mem, a->ld, MO_8, a->rs); \
2056 cat3(rx_, op, m)(addr, mask); \
2057 tcg_temp_free(mask); \
2058 tcg_temp_free(mem); \
2061 static bool cat3(trans_, name, _ir)(DisasContext *ctx, \
2062 cat3(arg_, name, _ir) * a) \
2065 mask = tcg_const_i32(1 << a->imm); \
2066 cat3(rx_, op, r)(cpu_regs[a->rd], mask); \
2067 tcg_temp_free(mask); \
2070 static bool cat3(trans_, name, _rr)(DisasContext *ctx, \
2071 cat3(arg_, name, _rr) * a) \
2074 mask = tcg_const_i32(1); \
2075 b = tcg_temp_new(); \
2076 tcg_gen_andi_i32(b, cpu_regs[a->rs], 31); \
2077 tcg_gen_shl_i32(mask, mask, b); \
2078 cat3(rx_, op, r)(cpu_regs[a->rd], mask); \
2079 tcg_temp_free(mask); \
2083 static bool cat3(trans_, name, _rm)(DisasContext *ctx, \
2084 cat3(arg_, name, _rm) * a) \
2086 TCGv mask, mem, addr, b; \
2087 mask = tcg_const_i32(1); \
2088 b = tcg_temp_new(); \
2089 tcg_gen_andi_i32(b, cpu_regs[a->rd], 7); \
2090 tcg_gen_shl_i32(mask, mask, b); \
2091 mem = tcg_temp_new(); \
2092 addr = rx_index_addr(ctx, mem, a->ld, MO_8, a->rs); \
2093 cat3(rx_, op, m)(addr, mask); \
2094 tcg_temp_free(mem); \
2095 tcg_temp_free(mask); \
2105 static inline void bmcnd_op(TCGv val
, TCGCond cond
, int pos
)
2109 dc
.temp
= tcg_temp_new();
2110 bit
= tcg_temp_new();
2111 psw_cond(&dc
, cond
);
2112 tcg_gen_andi_i32(val
, val
, ~(1 << pos
));
2113 tcg_gen_setcondi_i32(dc
.cond
, bit
, dc
.value
, 0);
2114 tcg_gen_deposit_i32(val
, val
, bit
, pos
, 1);
2116 tcg_temp_free(dc
.temp
);
2119 /* bmcnd #imm, dsp[rd] */
2120 static bool trans_BMCnd_im(DisasContext
*ctx
, arg_BMCnd_im
*a
)
2122 TCGv val
, mem
, addr
;
2123 val
= tcg_temp_new();
2124 mem
= tcg_temp_new();
2125 addr
= rx_index_addr(ctx
, mem
, a
->ld
, MO_8
, a
->rd
);
2126 rx_gen_ld(MO_8
, val
, addr
);
2127 bmcnd_op(val
, a
->cd
, a
->imm
);
2128 rx_gen_st(MO_8
, val
, addr
);
2134 /* bmcond #imm, rd */
2135 static bool trans_BMCnd_ir(DisasContext
*ctx
, arg_BMCnd_ir
*a
)
2137 bmcnd_op(cpu_regs
[a
->rd
], a
->cd
, a
->imm
);
2150 static inline void clrsetpsw(DisasContext
*ctx
, int cb
, int val
)
2155 tcg_gen_movi_i32(cpu_psw_c
, val
);
2158 tcg_gen_movi_i32(cpu_psw_z
, val
== 0);
2161 tcg_gen_movi_i32(cpu_psw_s
, val
? -1 : 0);
2164 tcg_gen_movi_i32(cpu_psw_o
, val
<< 31);
2167 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid distination %d", cb
);
2170 } else if (is_privileged(ctx
, 0)) {
2173 tcg_gen_movi_i32(cpu_psw_i
, val
);
2174 ctx
->base
.is_jmp
= DISAS_UPDATE
;
2177 tcg_gen_movi_i32(cpu_psw_u
, val
);
2180 qemu_log_mask(LOG_GUEST_ERROR
, "Invalid distination %d", cb
);
2187 static bool trans_CLRPSW(DisasContext
*ctx
, arg_CLRPSW
*a
)
2189 clrsetpsw(ctx
, a
->cb
, 0);
2194 static bool trans_SETPSW(DisasContext
*ctx
, arg_SETPSW
*a
)
2196 clrsetpsw(ctx
, a
->cb
, 1);
2201 static bool trans_MVTIPL(DisasContext
*ctx
, arg_MVTIPL
*a
)
2203 if (is_privileged(ctx
, 1)) {
2204 tcg_gen_movi_i32(cpu_psw_ipl
, a
->imm
);
2205 ctx
->base
.is_jmp
= DISAS_UPDATE
;
2211 static bool trans_MVTC_i(DisasContext
*ctx
, arg_MVTC_i
*a
)
2215 imm
= tcg_const_i32(a
->imm
);
2216 move_to_cr(ctx
, imm
, a
->cr
);
2217 if (a
->cr
== 0 && is_privileged(ctx
, 0)) {
2218 ctx
->base
.is_jmp
= DISAS_UPDATE
;
2225 static bool trans_MVTC_r(DisasContext
*ctx
, arg_MVTC_r
*a
)
2227 move_to_cr(ctx
, cpu_regs
[a
->rs
], a
->cr
);
2228 if (a
->cr
== 0 && is_privileged(ctx
, 0)) {
2229 ctx
->base
.is_jmp
= DISAS_UPDATE
;
2235 static bool trans_MVFC(DisasContext
*ctx
, arg_MVFC
*a
)
2237 move_from_cr(cpu_regs
[a
->rd
], a
->cr
, ctx
->pc
);
2242 static bool trans_RTFI(DisasContext
*ctx
, arg_RTFI
*a
)
2245 if (is_privileged(ctx
, 1)) {
2246 psw
= tcg_temp_new();
2247 tcg_gen_mov_i32(cpu_pc
, cpu_bpc
);
2248 tcg_gen_mov_i32(psw
, cpu_bpsw
);
2249 gen_helper_set_psw_rte(cpu_env
, psw
);
2250 ctx
->base
.is_jmp
= DISAS_EXIT
;
2257 static bool trans_RTE(DisasContext
*ctx
, arg_RTE
*a
)
2260 if (is_privileged(ctx
, 1)) {
2261 psw
= tcg_temp_new();
2264 gen_helper_set_psw_rte(cpu_env
, psw
);
2265 ctx
->base
.is_jmp
= DISAS_EXIT
;
2272 static bool trans_BRK(DisasContext
*ctx
, arg_BRK
*a
)
2274 tcg_gen_movi_i32(cpu_pc
, ctx
->base
.pc_next
);
2275 gen_helper_rxbrk(cpu_env
);
2276 ctx
->base
.is_jmp
= DISAS_NORETURN
;
2281 static bool trans_INT(DisasContext
*ctx
, arg_INT
*a
)
2285 tcg_debug_assert(a
->imm
< 0x100);
2286 vec
= tcg_const_i32(a
->imm
);
2287 tcg_gen_movi_i32(cpu_pc
, ctx
->base
.pc_next
);
2288 gen_helper_rxint(cpu_env
, vec
);
2290 ctx
->base
.is_jmp
= DISAS_NORETURN
;
2295 static bool trans_WAIT(DisasContext
*ctx
, arg_WAIT
*a
)
2297 if (is_privileged(ctx
, 1)) {
2298 tcg_gen_addi_i32(cpu_pc
, cpu_pc
, 2);
2299 gen_helper_wait(cpu_env
);
2304 static void rx_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cs
)
2306 CPURXState
*env
= cs
->env_ptr
;
2307 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
2311 static void rx_tr_tb_start(DisasContextBase
*dcbase
, CPUState
*cs
)
2315 static void rx_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cs
)
2317 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
2319 tcg_gen_insn_start(ctx
->base
.pc_next
);
2322 static bool rx_tr_breakpoint_check(DisasContextBase
*dcbase
, CPUState
*cs
,
2323 const CPUBreakpoint
*bp
)
2325 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
2327 /* We have hit a breakpoint - make sure PC is up-to-date */
2328 tcg_gen_movi_i32(cpu_pc
, ctx
->base
.pc_next
);
2329 gen_helper_debug(cpu_env
);
2330 ctx
->base
.is_jmp
= DISAS_NORETURN
;
2331 ctx
->base
.pc_next
+= 1;
2335 static void rx_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cs
)
2337 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
2340 ctx
->pc
= ctx
->base
.pc_next
;
2341 insn
= decode_load(ctx
);
2342 if (!decode(ctx
, insn
)) {
2343 gen_helper_raise_illegal_instruction(cpu_env
);
2347 static void rx_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cs
)
2349 DisasContext
*ctx
= container_of(dcbase
, DisasContext
, base
);
2351 switch (ctx
->base
.is_jmp
) {
2353 case DISAS_TOO_MANY
:
2354 gen_goto_tb(ctx
, 0, dcbase
->pc_next
);
2357 if (ctx
->base
.singlestep_enabled
) {
2358 gen_helper_debug(cpu_env
);
2360 tcg_gen_lookup_and_goto_ptr();
2364 tcg_gen_movi_i32(cpu_pc
, ctx
->base
.pc_next
);
2367 tcg_gen_exit_tb(NULL
, 0);
2369 case DISAS_NORETURN
:
2372 g_assert_not_reached();
2376 static void rx_tr_disas_log(const DisasContextBase
*dcbase
, CPUState
*cs
)
2378 qemu_log("IN:\n"); /* , lookup_symbol(dcbase->pc_first)); */
2379 log_target_disas(cs
, dcbase
->pc_first
, dcbase
->tb
->size
);
2382 static const TranslatorOps rx_tr_ops
= {
2383 .init_disas_context
= rx_tr_init_disas_context
,
2384 .tb_start
= rx_tr_tb_start
,
2385 .insn_start
= rx_tr_insn_start
,
2386 .breakpoint_check
= rx_tr_breakpoint_check
,
2387 .translate_insn
= rx_tr_translate_insn
,
2388 .tb_stop
= rx_tr_tb_stop
,
2389 .disas_log
= rx_tr_disas_log
,
2392 void gen_intermediate_code(CPUState
*cs
, TranslationBlock
*tb
, int max_insns
)
2396 translator_loop(&rx_tr_ops
, &dc
.base
, cs
, tb
, max_insns
);
2399 void restore_state_to_opc(CPURXState
*env
, TranslationBlock
*tb
,
2405 #define ALLOC_REGISTER(sym, name) \
2406 cpu_##sym = tcg_global_mem_new_i32(cpu_env, \
2407 offsetof(CPURXState, sym), name)
2409 void rx_translate_init(void)
2411 static const char * const regnames
[NUM_REGS
] = {
2412 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7",
2413 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
2417 for (i
= 0; i
< NUM_REGS
; i
++) {
2418 cpu_regs
[i
] = tcg_global_mem_new_i32(cpu_env
,
2419 offsetof(CPURXState
, regs
[i
]),
2422 ALLOC_REGISTER(pc
, "PC");
2423 ALLOC_REGISTER(psw_o
, "PSW(O)");
2424 ALLOC_REGISTER(psw_s
, "PSW(S)");
2425 ALLOC_REGISTER(psw_z
, "PSW(Z)");
2426 ALLOC_REGISTER(psw_c
, "PSW(C)");
2427 ALLOC_REGISTER(psw_u
, "PSW(U)");
2428 ALLOC_REGISTER(psw_i
, "PSW(I)");
2429 ALLOC_REGISTER(psw_pm
, "PSW(PM)");
2430 ALLOC_REGISTER(psw_ipl
, "PSW(IPL)");
2431 ALLOC_REGISTER(usp
, "USP");
2432 ALLOC_REGISTER(fpsw
, "FPSW");
2433 ALLOC_REGISTER(bpsw
, "BPSW");
2434 ALLOC_REGISTER(bpc
, "BPC");
2435 ALLOC_REGISTER(isp
, "ISP");
2436 ALLOC_REGISTER(fintv
, "FINTV");
2437 ALLOC_REGISTER(intb
, "INTB");
2438 cpu_acc
= tcg_global_mem_new_i64(cpu_env
,
2439 offsetof(CPURXState
, acc
), "ACC");