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/>.
24 #ifndef CONFIG_USER_ONLY
25 #include "exec/softmmu_exec.h"
27 #define MMUSUFFIX _mmu
30 #include "exec/softmmu_template.h"
33 #include "exec/softmmu_template.h"
36 #include "exec/softmmu_template.h"
39 #include "exec/softmmu_template.h"
41 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
46 ret = superh_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
48 /* now we have a real cpu fault */
50 cpu_restore_state(cs, retaddr);
58 void helper_ldtlb(CPUSH4State *env)
60 #ifdef CONFIG_USER_ONLY
61 SuperHCPU *cpu = sh_env_get_cpu(env);
64 cpu_abort(CPU(cpu), "Unhandled ldtlb");
70 static inline void QEMU_NORETURN raise_exception(CPUSH4State *env, int index,
73 CPUState *cs = CPU(sh_env_get_cpu(env));
75 cs->exception_index = index;
77 cpu_restore_state(cs, retaddr);
82 void helper_raise_illegal_instruction(CPUSH4State *env)
84 raise_exception(env, 0x180, 0);
87 void helper_raise_slot_illegal_instruction(CPUSH4State *env)
89 raise_exception(env, 0x1a0, 0);
92 void helper_raise_fpu_disable(CPUSH4State *env)
94 raise_exception(env, 0x800, 0);
97 void helper_raise_slot_fpu_disable(CPUSH4State *env)
99 raise_exception(env, 0x820, 0);
102 void helper_debug(CPUSH4State *env)
104 raise_exception(env, EXCP_DEBUG, 0);
107 void helper_sleep(CPUSH4State *env)
109 CPUState *cs = CPU(sh_env_get_cpu(env));
113 raise_exception(env, EXCP_HLT, 0);
116 void helper_trapa(CPUSH4State *env, uint32_t tra)
119 raise_exception(env, 0x160, 0);
122 void helper_movcal(CPUSH4State *env, uint32_t address, uint32_t value)
124 if (cpu_sh4_is_cached (env, address))
126 memory_content *r = malloc (sizeof(memory_content));
127 r->address = address;
131 *(env->movcal_backup_tail) = r;
132 env->movcal_backup_tail = &(r->next);
136 void helper_discard_movcal_backup(CPUSH4State *env)
138 memory_content *current = env->movcal_backup;
142 memory_content *next = current->next;
144 env->movcal_backup = current = next;
146 env->movcal_backup_tail = &(env->movcal_backup);
150 void helper_ocbi(CPUSH4State *env, uint32_t address)
152 memory_content **current = &(env->movcal_backup);
155 uint32_t a = (*current)->address;
156 if ((a & ~0x1F) == (address & ~0x1F))
158 memory_content *next = (*current)->next;
159 cpu_stl_data(env, a, (*current)->value);
163 env->movcal_backup_tail = current;
173 #define T (env->sr & SR_T)
174 #define Q (env->sr & SR_Q ? 1 : 0)
175 #define M (env->sr & SR_M ? 1 : 0)
176 #define SETT env->sr |= SR_T
177 #define CLRT env->sr &= ~SR_T
178 #define SETQ env->sr |= SR_Q
179 #define CLRQ env->sr &= ~SR_Q
180 #define SETM env->sr |= SR_M
181 #define CLRM env->sr &= ~SR_M
183 uint32_t helper_div1(CPUSH4State *env, uint32_t arg0, uint32_t arg1)
186 uint8_t old_q, tmp1 = 0xff;
188 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
190 if ((0x80000000 & arg1) != 0)
287 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
291 void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1)
295 res = ((uint64_t) env->mach << 32) | env->macl;
296 res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1;
297 env->mach = (res >> 32) & 0xffffffff;
298 env->macl = res & 0xffffffff;
299 if (env->sr & SR_S) {
301 env->mach |= 0xffff0000;
303 env->mach &= 0x00007fff;
307 void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1)
311 res = ((uint64_t) env->mach << 32) | env->macl;
312 res += (int64_t) (int16_t) arg0 *(int64_t) (int16_t) arg1;
313 env->mach = (res >> 32) & 0xffffffff;
314 env->macl = res & 0xffffffff;
315 if (env->sr & SR_S) {
316 if (res < -0x80000000) {
318 env->macl = 0x80000000;
319 } else if (res > 0x000000007fffffff) {
321 env->macl = 0x7fffffff;
326 static inline void set_t(CPUSH4State *env)
331 static inline void clr_t(CPUSH4State *env)
336 void helper_ld_fpscr(CPUSH4State *env, uint32_t val)
338 env->fpscr = val & FPSCR_MASK;
339 if ((val & FPSCR_RM_MASK) == FPSCR_RM_ZERO) {
340 set_float_rounding_mode(float_round_to_zero, &env->fp_status);
342 set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
344 set_flush_to_zero((val & FPSCR_DN) != 0, &env->fp_status);
347 static void update_fpscr(CPUSH4State *env, uintptr_t retaddr)
349 int xcpt, cause, enable;
351 xcpt = get_float_exception_flags(&env->fp_status);
353 /* Clear the flag entries */
354 env->fpscr &= ~FPSCR_FLAG_MASK;
356 if (unlikely(xcpt)) {
357 if (xcpt & float_flag_invalid) {
358 env->fpscr |= FPSCR_FLAG_V;
360 if (xcpt & float_flag_divbyzero) {
361 env->fpscr |= FPSCR_FLAG_Z;
363 if (xcpt & float_flag_overflow) {
364 env->fpscr |= FPSCR_FLAG_O;
366 if (xcpt & float_flag_underflow) {
367 env->fpscr |= FPSCR_FLAG_U;
369 if (xcpt & float_flag_inexact) {
370 env->fpscr |= FPSCR_FLAG_I;
373 /* Accumulate in cause entries */
374 env->fpscr |= (env->fpscr & FPSCR_FLAG_MASK)
375 << (FPSCR_CAUSE_SHIFT - FPSCR_FLAG_SHIFT);
377 /* Generate an exception if enabled */
378 cause = (env->fpscr & FPSCR_CAUSE_MASK) >> FPSCR_CAUSE_SHIFT;
379 enable = (env->fpscr & FPSCR_ENABLE_MASK) >> FPSCR_ENABLE_SHIFT;
380 if (cause & enable) {
381 raise_exception(env, 0x120, retaddr);
386 float32 helper_fabs_FT(float32 t0)
388 return float32_abs(t0);
391 float64 helper_fabs_DT(float64 t0)
393 return float64_abs(t0);
396 float32 helper_fadd_FT(CPUSH4State *env, float32 t0, float32 t1)
398 set_float_exception_flags(0, &env->fp_status);
399 t0 = float32_add(t0, t1, &env->fp_status);
400 update_fpscr(env, GETPC());
404 float64 helper_fadd_DT(CPUSH4State *env, float64 t0, float64 t1)
406 set_float_exception_flags(0, &env->fp_status);
407 t0 = float64_add(t0, t1, &env->fp_status);
408 update_fpscr(env, GETPC());
412 void helper_fcmp_eq_FT(CPUSH4State *env, float32 t0, float32 t1)
416 set_float_exception_flags(0, &env->fp_status);
417 relation = float32_compare(t0, t1, &env->fp_status);
418 if (unlikely(relation == float_relation_unordered)) {
419 update_fpscr(env, GETPC());
420 } else if (relation == float_relation_equal) {
427 void helper_fcmp_eq_DT(CPUSH4State *env, float64 t0, float64 t1)
431 set_float_exception_flags(0, &env->fp_status);
432 relation = float64_compare(t0, t1, &env->fp_status);
433 if (unlikely(relation == float_relation_unordered)) {
434 update_fpscr(env, GETPC());
435 } else if (relation == float_relation_equal) {
442 void helper_fcmp_gt_FT(CPUSH4State *env, float32 t0, float32 t1)
446 set_float_exception_flags(0, &env->fp_status);
447 relation = float32_compare(t0, t1, &env->fp_status);
448 if (unlikely(relation == float_relation_unordered)) {
449 update_fpscr(env, GETPC());
450 } else if (relation == float_relation_greater) {
457 void helper_fcmp_gt_DT(CPUSH4State *env, float64 t0, float64 t1)
461 set_float_exception_flags(0, &env->fp_status);
462 relation = float64_compare(t0, t1, &env->fp_status);
463 if (unlikely(relation == float_relation_unordered)) {
464 update_fpscr(env, GETPC());
465 } else if (relation == float_relation_greater) {
472 float64 helper_fcnvsd_FT_DT(CPUSH4State *env, float32 t0)
475 set_float_exception_flags(0, &env->fp_status);
476 ret = float32_to_float64(t0, &env->fp_status);
477 update_fpscr(env, GETPC());
481 float32 helper_fcnvds_DT_FT(CPUSH4State *env, float64 t0)
484 set_float_exception_flags(0, &env->fp_status);
485 ret = float64_to_float32(t0, &env->fp_status);
486 update_fpscr(env, GETPC());
490 float32 helper_fdiv_FT(CPUSH4State *env, float32 t0, float32 t1)
492 set_float_exception_flags(0, &env->fp_status);
493 t0 = float32_div(t0, t1, &env->fp_status);
494 update_fpscr(env, GETPC());
498 float64 helper_fdiv_DT(CPUSH4State *env, float64 t0, float64 t1)
500 set_float_exception_flags(0, &env->fp_status);
501 t0 = float64_div(t0, t1, &env->fp_status);
502 update_fpscr(env, GETPC());
506 float32 helper_float_FT(CPUSH4State *env, uint32_t t0)
509 set_float_exception_flags(0, &env->fp_status);
510 ret = int32_to_float32(t0, &env->fp_status);
511 update_fpscr(env, GETPC());
515 float64 helper_float_DT(CPUSH4State *env, uint32_t t0)
518 set_float_exception_flags(0, &env->fp_status);
519 ret = int32_to_float64(t0, &env->fp_status);
520 update_fpscr(env, GETPC());
524 float32 helper_fmac_FT(CPUSH4State *env, float32 t0, float32 t1, float32 t2)
526 set_float_exception_flags(0, &env->fp_status);
527 t0 = float32_muladd(t0, t1, t2, 0, &env->fp_status);
528 update_fpscr(env, GETPC());
532 float32 helper_fmul_FT(CPUSH4State *env, float32 t0, float32 t1)
534 set_float_exception_flags(0, &env->fp_status);
535 t0 = float32_mul(t0, t1, &env->fp_status);
536 update_fpscr(env, GETPC());
540 float64 helper_fmul_DT(CPUSH4State *env, float64 t0, float64 t1)
542 set_float_exception_flags(0, &env->fp_status);
543 t0 = float64_mul(t0, t1, &env->fp_status);
544 update_fpscr(env, GETPC());
548 float32 helper_fneg_T(float32 t0)
550 return float32_chs(t0);
553 float32 helper_fsqrt_FT(CPUSH4State *env, float32 t0)
555 set_float_exception_flags(0, &env->fp_status);
556 t0 = float32_sqrt(t0, &env->fp_status);
557 update_fpscr(env, GETPC());
561 float64 helper_fsqrt_DT(CPUSH4State *env, float64 t0)
563 set_float_exception_flags(0, &env->fp_status);
564 t0 = float64_sqrt(t0, &env->fp_status);
565 update_fpscr(env, GETPC());
569 float32 helper_fsub_FT(CPUSH4State *env, float32 t0, float32 t1)
571 set_float_exception_flags(0, &env->fp_status);
572 t0 = float32_sub(t0, t1, &env->fp_status);
573 update_fpscr(env, GETPC());
577 float64 helper_fsub_DT(CPUSH4State *env, float64 t0, float64 t1)
579 set_float_exception_flags(0, &env->fp_status);
580 t0 = float64_sub(t0, t1, &env->fp_status);
581 update_fpscr(env, GETPC());
585 uint32_t helper_ftrc_FT(CPUSH4State *env, float32 t0)
588 set_float_exception_flags(0, &env->fp_status);
589 ret = float32_to_int32_round_to_zero(t0, &env->fp_status);
590 update_fpscr(env, GETPC());
594 uint32_t helper_ftrc_DT(CPUSH4State *env, float64 t0)
597 set_float_exception_flags(0, &env->fp_status);
598 ret = float64_to_int32_round_to_zero(t0, &env->fp_status);
599 update_fpscr(env, GETPC());
603 void helper_fipr(CPUSH4State *env, uint32_t m, uint32_t n)
608 bank = (env->sr & FPSCR_FR) ? 16 : 0;
610 set_float_exception_flags(0, &env->fp_status);
612 for (i = 0 ; i < 4 ; i++) {
613 p = float32_mul(env->fregs[bank + m + i],
614 env->fregs[bank + n + i],
616 r = float32_add(r, p, &env->fp_status);
618 update_fpscr(env, GETPC());
620 env->fregs[bank + n + 3] = r;
623 void helper_ftrv(CPUSH4State *env, uint32_t n)
625 int bank_matrix, bank_vector;
630 bank_matrix = (env->sr & FPSCR_FR) ? 0 : 16;
631 bank_vector = (env->sr & FPSCR_FR) ? 16 : 0;
632 set_float_exception_flags(0, &env->fp_status);
633 for (i = 0 ; i < 4 ; i++) {
635 for (j = 0 ; j < 4 ; j++) {
636 p = float32_mul(env->fregs[bank_matrix + 4 * j + i],
637 env->fregs[bank_vector + j],
639 r[i] = float32_add(r[i], p, &env->fp_status);
642 update_fpscr(env, GETPC());
644 for (i = 0 ; i < 4 ; i++) {
645 env->fregs[bank_vector + i] = r[i];