4 * Copyright (c) 2005 Samuel Tardieu
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "exec/helper-proto.h"
22 #include "exec/exec-all.h"
23 #include "exec/cpu_ldst.h"
25 #ifndef CONFIG_USER_ONLY
27 void superh_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
28 MMUAccessType access_type,
29 int mmu_idx, uintptr_t retaddr)
31 switch (access_type) {
34 cs->exception_index = 0x0e0;
37 cs->exception_index = 0x100;
40 cpu_loop_exit_restore(cs, retaddr);
43 void tlb_fill(CPUState *cs, target_ulong addr, MMUAccessType access_type,
44 int mmu_idx, uintptr_t retaddr)
48 ret = superh_cpu_handle_mmu_fault(cs, addr, access_type, mmu_idx);
50 /* now we have a real cpu fault */
51 cpu_loop_exit_restore(cs, retaddr);
57 void helper_ldtlb(CPUSH4State *env)
59 #ifdef CONFIG_USER_ONLY
60 SuperHCPU *cpu = sh_env_get_cpu(env);
63 cpu_abort(CPU(cpu), "Unhandled ldtlb");
69 static inline void QEMU_NORETURN raise_exception(CPUSH4State *env, int index,
72 CPUState *cs = CPU(sh_env_get_cpu(env));
74 cs->exception_index = index;
75 cpu_loop_exit_restore(cs, retaddr);
78 void helper_raise_illegal_instruction(CPUSH4State *env)
80 raise_exception(env, 0x180, 0);
83 void helper_raise_slot_illegal_instruction(CPUSH4State *env)
85 raise_exception(env, 0x1a0, 0);
88 void helper_raise_fpu_disable(CPUSH4State *env)
90 raise_exception(env, 0x800, 0);
93 void helper_raise_slot_fpu_disable(CPUSH4State *env)
95 raise_exception(env, 0x820, 0);
98 void helper_debug(CPUSH4State *env)
100 raise_exception(env, EXCP_DEBUG, 0);
103 void helper_sleep(CPUSH4State *env)
105 CPUState *cs = CPU(sh_env_get_cpu(env));
109 raise_exception(env, EXCP_HLT, 0);
112 void helper_trapa(CPUSH4State *env, uint32_t tra)
115 raise_exception(env, 0x160, 0);
118 void helper_movcal(CPUSH4State *env, uint32_t address, uint32_t value)
120 if (cpu_sh4_is_cached (env, address))
122 memory_content *r = g_new(memory_content, 1);
124 r->address = address;
128 *(env->movcal_backup_tail) = r;
129 env->movcal_backup_tail = &(r->next);
133 void helper_discard_movcal_backup(CPUSH4State *env)
135 memory_content *current = env->movcal_backup;
139 memory_content *next = current->next;
141 env->movcal_backup = current = next;
143 env->movcal_backup_tail = &(env->movcal_backup);
147 void helper_ocbi(CPUSH4State *env, uint32_t address)
149 memory_content **current = &(env->movcal_backup);
152 uint32_t a = (*current)->address;
153 if ((a & ~0x1F) == (address & ~0x1F))
155 memory_content *next = (*current)->next;
156 cpu_stl_data(env, a, (*current)->value);
160 env->movcal_backup_tail = current;
170 void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1)
174 res = ((uint64_t) env->mach << 32) | env->macl;
175 res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1;
176 env->mach = (res >> 32) & 0xffffffff;
177 env->macl = res & 0xffffffff;
178 if (env->sr & (1u << SR_S)) {
180 env->mach |= 0xffff0000;
182 env->mach &= 0x00007fff;
186 void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1)
190 res = ((uint64_t) env->mach << 32) | env->macl;
191 res += (int64_t) (int16_t) arg0 *(int64_t) (int16_t) arg1;
192 env->mach = (res >> 32) & 0xffffffff;
193 env->macl = res & 0xffffffff;
194 if (env->sr & (1u << SR_S)) {
195 if (res < -0x80000000) {
197 env->macl = 0x80000000;
198 } else if (res > 0x000000007fffffff) {
200 env->macl = 0x7fffffff;
205 void helper_ld_fpscr(CPUSH4State *env, uint32_t val)
207 env->fpscr = val & FPSCR_MASK;
208 if ((val & FPSCR_RM_MASK) == FPSCR_RM_ZERO) {
209 set_float_rounding_mode(float_round_to_zero, &env->fp_status);
211 set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
213 set_flush_to_zero((val & FPSCR_DN) != 0, &env->fp_status);
216 static void update_fpscr(CPUSH4State *env, uintptr_t retaddr)
218 int xcpt, cause, enable;
220 xcpt = get_float_exception_flags(&env->fp_status);
222 /* Clear the cause entries */
223 env->fpscr &= ~FPSCR_CAUSE_MASK;
225 if (unlikely(xcpt)) {
226 if (xcpt & float_flag_invalid) {
227 env->fpscr |= FPSCR_CAUSE_V;
229 if (xcpt & float_flag_divbyzero) {
230 env->fpscr |= FPSCR_CAUSE_Z;
232 if (xcpt & float_flag_overflow) {
233 env->fpscr |= FPSCR_CAUSE_O;
235 if (xcpt & float_flag_underflow) {
236 env->fpscr |= FPSCR_CAUSE_U;
238 if (xcpt & float_flag_inexact) {
239 env->fpscr |= FPSCR_CAUSE_I;
242 /* Accumulate in flag entries */
243 env->fpscr |= (env->fpscr & FPSCR_CAUSE_MASK)
244 >> (FPSCR_CAUSE_SHIFT - FPSCR_FLAG_SHIFT);
246 /* Generate an exception if enabled */
247 cause = (env->fpscr & FPSCR_CAUSE_MASK) >> FPSCR_CAUSE_SHIFT;
248 enable = (env->fpscr & FPSCR_ENABLE_MASK) >> FPSCR_ENABLE_SHIFT;
249 if (cause & enable) {
250 raise_exception(env, 0x120, retaddr);
255 float32 helper_fadd_FT(CPUSH4State *env, float32 t0, float32 t1)
257 set_float_exception_flags(0, &env->fp_status);
258 t0 = float32_add(t0, t1, &env->fp_status);
259 update_fpscr(env, GETPC());
263 float64 helper_fadd_DT(CPUSH4State *env, float64 t0, float64 t1)
265 set_float_exception_flags(0, &env->fp_status);
266 t0 = float64_add(t0, t1, &env->fp_status);
267 update_fpscr(env, GETPC());
271 uint32_t helper_fcmp_eq_FT(CPUSH4State *env, float32 t0, float32 t1)
275 set_float_exception_flags(0, &env->fp_status);
276 relation = float32_compare(t0, t1, &env->fp_status);
277 update_fpscr(env, GETPC());
278 return relation == float_relation_equal;
281 uint32_t helper_fcmp_eq_DT(CPUSH4State *env, float64 t0, float64 t1)
285 set_float_exception_flags(0, &env->fp_status);
286 relation = float64_compare(t0, t1, &env->fp_status);
287 update_fpscr(env, GETPC());
288 return relation == float_relation_equal;
291 uint32_t helper_fcmp_gt_FT(CPUSH4State *env, float32 t0, float32 t1)
295 set_float_exception_flags(0, &env->fp_status);
296 relation = float32_compare(t0, t1, &env->fp_status);
297 update_fpscr(env, GETPC());
298 return relation == float_relation_greater;
301 uint32_t helper_fcmp_gt_DT(CPUSH4State *env, float64 t0, float64 t1)
305 set_float_exception_flags(0, &env->fp_status);
306 relation = float64_compare(t0, t1, &env->fp_status);
307 update_fpscr(env, GETPC());
308 return relation == float_relation_greater;
311 float64 helper_fcnvsd_FT_DT(CPUSH4State *env, float32 t0)
314 set_float_exception_flags(0, &env->fp_status);
315 ret = float32_to_float64(t0, &env->fp_status);
316 update_fpscr(env, GETPC());
320 float32 helper_fcnvds_DT_FT(CPUSH4State *env, float64 t0)
323 set_float_exception_flags(0, &env->fp_status);
324 ret = float64_to_float32(t0, &env->fp_status);
325 update_fpscr(env, GETPC());
329 float32 helper_fdiv_FT(CPUSH4State *env, float32 t0, float32 t1)
331 set_float_exception_flags(0, &env->fp_status);
332 t0 = float32_div(t0, t1, &env->fp_status);
333 update_fpscr(env, GETPC());
337 float64 helper_fdiv_DT(CPUSH4State *env, float64 t0, float64 t1)
339 set_float_exception_flags(0, &env->fp_status);
340 t0 = float64_div(t0, t1, &env->fp_status);
341 update_fpscr(env, GETPC());
345 float32 helper_float_FT(CPUSH4State *env, uint32_t t0)
348 set_float_exception_flags(0, &env->fp_status);
349 ret = int32_to_float32(t0, &env->fp_status);
350 update_fpscr(env, GETPC());
354 float64 helper_float_DT(CPUSH4State *env, uint32_t t0)
357 set_float_exception_flags(0, &env->fp_status);
358 ret = int32_to_float64(t0, &env->fp_status);
359 update_fpscr(env, GETPC());
363 float32 helper_fmac_FT(CPUSH4State *env, float32 t0, float32 t1, float32 t2)
365 set_float_exception_flags(0, &env->fp_status);
366 t0 = float32_muladd(t0, t1, t2, 0, &env->fp_status);
367 update_fpscr(env, GETPC());
371 float32 helper_fmul_FT(CPUSH4State *env, float32 t0, float32 t1)
373 set_float_exception_flags(0, &env->fp_status);
374 t0 = float32_mul(t0, t1, &env->fp_status);
375 update_fpscr(env, GETPC());
379 float64 helper_fmul_DT(CPUSH4State *env, float64 t0, float64 t1)
381 set_float_exception_flags(0, &env->fp_status);
382 t0 = float64_mul(t0, t1, &env->fp_status);
383 update_fpscr(env, GETPC());
387 float32 helper_fsqrt_FT(CPUSH4State *env, float32 t0)
389 set_float_exception_flags(0, &env->fp_status);
390 t0 = float32_sqrt(t0, &env->fp_status);
391 update_fpscr(env, GETPC());
395 float64 helper_fsqrt_DT(CPUSH4State *env, float64 t0)
397 set_float_exception_flags(0, &env->fp_status);
398 t0 = float64_sqrt(t0, &env->fp_status);
399 update_fpscr(env, GETPC());
403 float32 helper_fsub_FT(CPUSH4State *env, float32 t0, float32 t1)
405 set_float_exception_flags(0, &env->fp_status);
406 t0 = float32_sub(t0, t1, &env->fp_status);
407 update_fpscr(env, GETPC());
411 float64 helper_fsub_DT(CPUSH4State *env, float64 t0, float64 t1)
413 set_float_exception_flags(0, &env->fp_status);
414 t0 = float64_sub(t0, t1, &env->fp_status);
415 update_fpscr(env, GETPC());
419 uint32_t helper_ftrc_FT(CPUSH4State *env, float32 t0)
422 set_float_exception_flags(0, &env->fp_status);
423 ret = float32_to_int32_round_to_zero(t0, &env->fp_status);
424 update_fpscr(env, GETPC());
428 uint32_t helper_ftrc_DT(CPUSH4State *env, float64 t0)
431 set_float_exception_flags(0, &env->fp_status);
432 ret = float64_to_int32_round_to_zero(t0, &env->fp_status);
433 update_fpscr(env, GETPC());
437 void helper_fipr(CPUSH4State *env, uint32_t m, uint32_t n)
442 bank = (env->sr & FPSCR_FR) ? 16 : 0;
444 set_float_exception_flags(0, &env->fp_status);
446 for (i = 0 ; i < 4 ; i++) {
447 p = float32_mul(env->fregs[bank + m + i],
448 env->fregs[bank + n + i],
450 r = float32_add(r, p, &env->fp_status);
452 update_fpscr(env, GETPC());
454 env->fregs[bank + n + 3] = r;
457 void helper_ftrv(CPUSH4State *env, uint32_t n)
459 int bank_matrix, bank_vector;
464 bank_matrix = (env->sr & FPSCR_FR) ? 0 : 16;
465 bank_vector = (env->sr & FPSCR_FR) ? 16 : 0;
466 set_float_exception_flags(0, &env->fp_status);
467 for (i = 0 ; i < 4 ; i++) {
469 for (j = 0 ; j < 4 ; j++) {
470 p = float32_mul(env->fregs[bank_matrix + 4 * j + i],
471 env->fregs[bank_vector + j],
473 r[i] = float32_add(r[i], p, &env->fp_status);
476 update_fpscr(env, GETPC());
478 for (i = 0 ; i < 4 ; i++) {
479 env->fregs[bank_vector + i] = r[i];