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/bitops.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "exec/cpu_ldst.h"
25 #include "fpu/softfloat.h"
27 static inline void QEMU_NORETURN raise_exception(CPURXState *env, int index,
30 static void _set_psw(CPURXState *env, uint32_t psw, uint32_t rte)
34 rx_cpu_unpack_psw(env, psw, rte);
35 if (prev_u != env->psw_u) {
38 env->isp = env->regs[0];
39 env->regs[0] = env->usp;
41 env->usp = env->regs[0];
42 env->regs[0] = env->isp;
47 void helper_set_psw(CPURXState *env, uint32_t psw)
49 _set_psw(env, psw, 0);
52 void helper_set_psw_rte(CPURXState *env, uint32_t psw)
54 _set_psw(env, psw, 1);
57 uint32_t helper_pack_psw(CPURXState *env)
59 return rx_cpu_pack_psw(env);
64 env->fpsw = FIELD_DP32(env->fpsw, FPSW, C ## b, 1); \
65 if (!FIELD_EX32(env->fpsw, FPSW, E ## b)) { \
66 env->fpsw = FIELD_DP32(env->fpsw, FPSW, F ## b, 1); \
71 static void update_fpsw(CPURXState *env, float32 ret, uintptr_t retaddr)
73 int xcpt, cause, enable;
75 env->psw_z = ret & ~(1 << 31); /* mask sign bit */
78 xcpt = get_float_exception_flags(&env->fp_status);
80 /* Clear the cause entries */
81 env->fpsw = FIELD_DP32(env->fpsw, FPSW, CAUSE, 0);
85 if (xcpt & float_flag_invalid) {
88 if (xcpt & float_flag_divbyzero) {
91 if (xcpt & float_flag_overflow) {
94 if (xcpt & float_flag_underflow) {
97 if (xcpt & float_flag_inexact) {
100 if ((xcpt & (float_flag_input_denormal
101 | float_flag_output_denormal))
102 && !FIELD_EX32(env->fpsw, FPSW, DN)) {
103 env->fpsw = FIELD_DP32(env->fpsw, FPSW, CE, 1);
106 /* update FPSW_FLAG_S */
107 if (FIELD_EX32(env->fpsw, FPSW, FLAGS) != 0) {
108 env->fpsw = FIELD_DP32(env->fpsw, FPSW, FS, 1);
111 /* Generate an exception if enabled */
112 cause = FIELD_EX32(env->fpsw, FPSW, CAUSE);
113 enable = FIELD_EX32(env->fpsw, FPSW, ENABLE);
114 enable |= 1 << 5; /* CE always enabled */
115 if (cause & enable) {
116 raise_exception(env, 21, retaddr);
121 void helper_set_fpsw(CPURXState *env, uint32_t val)
123 static const int roundmode[] = {
124 float_round_nearest_even,
129 uint32_t fpsw = env->fpsw;
133 FIELD_DP32(fpsw, FPSW, FS, FIELD_EX32(fpsw, FPSW, FLAGS) != 0);
135 set_float_rounding_mode(roundmode[FIELD_EX32(env->fpsw, FPSW, RM)],
139 #define FLOATOP(op, func) \
140 float32 helper_##op(CPURXState *env, float32 t0, float32 t1) \
143 ret = func(t0, t1, &env->fp_status); \
144 update_fpsw(env, *(uint32_t *)&ret, GETPC()); \
148 FLOATOP(fadd, float32_add)
149 FLOATOP(fsub, float32_sub)
150 FLOATOP(fmul, float32_mul)
151 FLOATOP(fdiv, float32_div)
153 void helper_fcmp(CPURXState *env, float32 t0, float32 t1)
156 st = float32_compare(t0, t1, &env->fp_status);
157 update_fpsw(env, 0, GETPC());
159 env->psw_s = env->psw_o = 0;
161 case float_relation_equal:
164 case float_relation_less:
167 case float_relation_unordered:
173 uint32_t helper_ftoi(CPURXState *env, float32 t0)
176 ret = float32_to_int32_round_to_zero(t0, &env->fp_status);
177 update_fpsw(env, ret, GETPC());
181 uint32_t helper_round(CPURXState *env, float32 t0)
184 ret = float32_to_int32(t0, &env->fp_status);
185 update_fpsw(env, ret, GETPC());
189 float32 helper_itof(CPURXState *env, uint32_t t0)
192 ret = int32_to_float32(t0, &env->fp_status);
193 update_fpsw(env, ret, GETPC());
197 /* string operations */
198 void helper_scmpu(CPURXState *env)
201 if (env->regs[3] == 0) {
204 while (env->regs[3] != 0) {
205 tmp0 = cpu_ldub_data_ra(env, env->regs[1]++, GETPC());
206 tmp1 = cpu_ldub_data_ra(env, env->regs[2]++, GETPC());
208 if (tmp0 != tmp1 || tmp0 == '\0') {
212 env->psw_z = tmp0 - tmp1;
213 env->psw_c = (tmp0 >= tmp1);
216 static uint32_t (* const cpu_ldufn[])(CPUArchState *env,
218 uintptr_t retaddr) = {
219 cpu_ldub_data_ra, cpu_lduw_data_ra, cpu_ldl_data_ra,
222 static uint32_t (* const cpu_ldfn[])(CPUArchState *env,
224 uintptr_t retaddr) = {
225 cpu_ldub_data_ra, cpu_lduw_data_ra, cpu_ldl_data_ra,
228 static void (* const cpu_stfn[])(CPUArchState *env,
231 uintptr_t retaddr) = {
232 cpu_stb_data_ra, cpu_stw_data_ra, cpu_stl_data_ra,
235 void helper_sstr(CPURXState *env, uint32_t sz)
237 tcg_debug_assert(sz < 3);
238 while (env->regs[3] != 0) {
239 cpu_stfn[sz](env, env->regs[1], env->regs[2], GETPC());
240 env->regs[1] += 1 << sz;
249 static void smov(uint32_t mode, CPURXState *env)
254 dir = (mode & OP_SMOVB) ? -1 : 1;
255 while (env->regs[3] != 0) {
256 tmp = cpu_ldub_data_ra(env, env->regs[2], GETPC());
257 cpu_stb_data_ra(env, env->regs[1], tmp, GETPC());
261 if ((mode & OP_SMOVU) && tmp == 0) {
267 void helper_smovu(CPURXState *env)
272 void helper_smovf(CPURXState *env)
277 void helper_smovb(CPURXState *env)
283 void helper_suntil(CPURXState *env, uint32_t sz)
286 tcg_debug_assert(sz < 3);
287 if (env->regs[3] == 0) {
290 while (env->regs[3] != 0) {
291 tmp = cpu_ldufn[sz](env, env->regs[1], GETPC());
292 env->regs[1] += 1 << sz;
294 if (tmp == env->regs[2]) {
298 env->psw_z = tmp - env->regs[2];
299 env->psw_c = (tmp <= env->regs[2]);
302 void helper_swhile(CPURXState *env, uint32_t sz)
305 tcg_debug_assert(sz < 3);
306 if (env->regs[3] == 0) {
309 while (env->regs[3] != 0) {
310 tmp = cpu_ldufn[sz](env, env->regs[1], GETPC());
311 env->regs[1] += 1 << sz;
313 if (tmp != env->regs[2]) {
317 env->psw_z = env->regs[3];
318 env->psw_c = (tmp <= env->regs[2]);
321 /* accumulator operations */
322 void helper_rmpa(CPURXState *env, uint32_t sz)
324 uint64_t result_l, prev;
328 if (env->regs[3] == 0) {
331 result_l = env->regs[5];
333 result_l |= env->regs[4];
334 result_h = env->regs[6];
337 while (env->regs[3] != 0) {
338 tmp0 = cpu_ldfn[sz](env, env->regs[1], GETPC());
339 tmp1 = cpu_ldfn[sz](env, env->regs[2], GETPC());
345 if (prev > result_l) {
349 if (prev < result_l) {
354 env->regs[1] += 1 << sz;
355 env->regs[2] += 1 << sz;
357 env->psw_s = result_h;
358 env->psw_o = (result_h != 0 && result_h != -1) << 31;
359 env->regs[6] = result_h;
360 env->regs[5] = result_l >> 32;
361 env->regs[4] = result_l & 0xffffffff;
364 void helper_racw(CPURXState *env, uint32_t imm)
369 acc += 0x0000000080000000LL;
370 if (acc > 0x00007fff00000000LL) {
371 acc = 0x00007fff00000000LL;
372 } else if (acc < -0x800000000000LL) {
373 acc = -0x800000000000LL;
375 acc &= 0xffffffff00000000LL;
380 void helper_satr(CPURXState *env)
382 if (env->psw_o >> 31) {
383 if ((int)env->psw_s < 0) {
384 env->regs[6] = 0x00000000;
385 env->regs[5] = 0x7fffffff;
386 env->regs[4] = 0xffffffff;
388 env->regs[6] = 0xffffffff;
389 env->regs[5] = 0x80000000;
390 env->regs[4] = 0x00000000;
396 uint32_t helper_div(CPURXState *env, uint32_t num, uint32_t den)
399 if (!((num == INT_MIN && den == -1) || den == 0)) {
400 ret = (int32_t)num / (int32_t)den;
408 uint32_t helper_divu(CPURXState *env, uint32_t num, uint32_t den)
421 static inline void QEMU_NORETURN raise_exception(CPURXState *env, int index,
424 CPUState *cs = env_cpu(env);
426 cs->exception_index = index;
427 cpu_loop_exit_restore(cs, retaddr);
430 void QEMU_NORETURN helper_raise_privilege_violation(CPURXState *env)
432 raise_exception(env, 20, GETPC());
435 void QEMU_NORETURN helper_raise_access_fault(CPURXState *env)
437 raise_exception(env, 21, GETPC());
440 void QEMU_NORETURN helper_raise_illegal_instruction(CPURXState *env)
442 raise_exception(env, 23, GETPC());
445 void QEMU_NORETURN helper_wait(CPURXState *env)
447 CPUState *cs = env_cpu(env);
451 raise_exception(env, EXCP_HLT, 0);
454 void QEMU_NORETURN helper_debug(CPURXState *env)
456 CPUState *cs = env_cpu(env);
458 cs->exception_index = EXCP_DEBUG;
462 void QEMU_NORETURN helper_rxint(CPURXState *env, uint32_t vec)
464 raise_exception(env, 0x100 + vec, 0);
467 void QEMU_NORETURN helper_rxbrk(CPURXState *env)
469 raise_exception(env, 0x100, 0);