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/>.
23 #ifndef CONFIG_USER_ONLY
24 #include "exec/softmmu_exec.h"
26 #define MMUSUFFIX _mmu
29 #include "exec/softmmu_template.h"
32 #include "exec/softmmu_template.h"
35 #include "exec/softmmu_template.h"
38 #include "exec/softmmu_template.h"
40 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
45 ret
= superh_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
47 /* now we have a real cpu fault */
49 cpu_restore_state(cs
, retaddr
);
57 #ifdef CONFIG_USER_ONLY
58 void QEMU_NORETURN
helper_ldtlb(CPUSH4State
*env
)
60 void helper_ldtlb(CPUSH4State
*env
)
63 #ifdef CONFIG_USER_ONLY
64 SuperHCPU
*cpu
= sh_env_get_cpu(env
);
67 cpu_abort(CPU(cpu
), "Unhandled ldtlb");
73 static inline void QEMU_NORETURN
raise_exception(CPUSH4State
*env
, int index
,
76 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
78 cs
->exception_index
= index
;
80 cpu_restore_state(cs
, retaddr
);
85 void QEMU_NORETURN
helper_raise_illegal_instruction(CPUSH4State
*env
)
87 raise_exception(env
, 0x180, 0);
90 void QEMU_NORETURN
helper_raise_slot_illegal_instruction(CPUSH4State
*env
)
92 raise_exception(env
, 0x1a0, 0);
95 void QEMU_NORETURN
helper_raise_fpu_disable(CPUSH4State
*env
)
97 raise_exception(env
, 0x800, 0);
100 void QEMU_NORETURN
helper_raise_slot_fpu_disable(CPUSH4State
*env
)
102 raise_exception(env
, 0x820, 0);
105 void QEMU_NORETURN
helper_debug(CPUSH4State
*env
)
107 raise_exception(env
, EXCP_DEBUG
, 0);
110 void QEMU_NORETURN
helper_sleep(CPUSH4State
*env
)
112 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
116 raise_exception(env
, EXCP_HLT
, 0);
119 void QEMU_NORETURN
helper_trapa(CPUSH4State
*env
, uint32_t tra
)
122 raise_exception(env
, 0x160, 0);
125 void helper_movcal(CPUSH4State
*env
, uint32_t address
, uint32_t value
)
127 if (cpu_sh4_is_cached (env
, address
))
129 memory_content
*r
= malloc (sizeof(memory_content
));
130 r
->address
= address
;
134 *(env
->movcal_backup_tail
) = r
;
135 env
->movcal_backup_tail
= &(r
->next
);
139 void helper_discard_movcal_backup(CPUSH4State
*env
)
141 memory_content
*current
= env
->movcal_backup
;
145 memory_content
*next
= current
->next
;
147 env
->movcal_backup
= current
= next
;
149 env
->movcal_backup_tail
= &(env
->movcal_backup
);
153 void helper_ocbi(CPUSH4State
*env
, uint32_t address
)
155 memory_content
**current
= &(env
->movcal_backup
);
158 uint32_t a
= (*current
)->address
;
159 if ((a
& ~0x1F) == (address
& ~0x1F))
161 memory_content
*next
= (*current
)->next
;
162 cpu_stl_data(env
, a
, (*current
)->value
);
166 env
->movcal_backup_tail
= current
;
176 #define T (env->sr & SR_T)
177 #define Q (env->sr & SR_Q ? 1 : 0)
178 #define M (env->sr & SR_M ? 1 : 0)
179 #define SETT env->sr |= SR_T
180 #define CLRT env->sr &= ~SR_T
181 #define SETQ env->sr |= SR_Q
182 #define CLRQ env->sr &= ~SR_Q
183 #define SETM env->sr |= SR_M
184 #define CLRM env->sr &= ~SR_M
186 uint32_t helper_div1(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
189 uint8_t old_q
, tmp1
= 0xff;
191 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
193 if ((0x80000000 & arg1
) != 0)
290 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
294 void helper_macl(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
298 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
299 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
300 env
->mach
= (res
>> 32) & 0xffffffff;
301 env
->macl
= res
& 0xffffffff;
302 if (env
->sr
& SR_S
) {
304 env
->mach
|= 0xffff0000;
306 env
->mach
&= 0x00007fff;
310 void helper_macw(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
314 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
315 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
316 env
->mach
= (res
>> 32) & 0xffffffff;
317 env
->macl
= res
& 0xffffffff;
318 if (env
->sr
& SR_S
) {
319 if (res
< -0x80000000) {
321 env
->macl
= 0x80000000;
322 } else if (res
> 0x000000007fffffff) {
324 env
->macl
= 0x7fffffff;
329 static inline void set_t(CPUSH4State
*env
)
334 static inline void clr_t(CPUSH4State
*env
)
339 void helper_ld_fpscr(CPUSH4State
*env
, uint32_t val
)
341 env
->fpscr
= val
& FPSCR_MASK
;
342 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
343 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
345 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
347 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
350 static void update_fpscr(CPUSH4State
*env
, uintptr_t retaddr
)
352 int xcpt
, cause
, enable
;
354 xcpt
= get_float_exception_flags(&env
->fp_status
);
356 /* Clear the flag entries */
357 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
359 if (unlikely(xcpt
)) {
360 if (xcpt
& float_flag_invalid
) {
361 env
->fpscr
|= FPSCR_FLAG_V
;
363 if (xcpt
& float_flag_divbyzero
) {
364 env
->fpscr
|= FPSCR_FLAG_Z
;
366 if (xcpt
& float_flag_overflow
) {
367 env
->fpscr
|= FPSCR_FLAG_O
;
369 if (xcpt
& float_flag_underflow
) {
370 env
->fpscr
|= FPSCR_FLAG_U
;
372 if (xcpt
& float_flag_inexact
) {
373 env
->fpscr
|= FPSCR_FLAG_I
;
376 /* Accumulate in cause entries */
377 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
378 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
380 /* Generate an exception if enabled */
381 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
382 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
383 if (cause
& enable
) {
384 raise_exception(env
, 0x120, retaddr
);
389 float32
helper_fabs_FT(float32 t0
)
391 return float32_abs(t0
);
394 float64
helper_fabs_DT(float64 t0
)
396 return float64_abs(t0
);
399 float32
helper_fadd_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
401 set_float_exception_flags(0, &env
->fp_status
);
402 t0
= float32_add(t0
, t1
, &env
->fp_status
);
403 update_fpscr(env
, GETPC());
407 float64
helper_fadd_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
409 set_float_exception_flags(0, &env
->fp_status
);
410 t0
= float64_add(t0
, t1
, &env
->fp_status
);
411 update_fpscr(env
, GETPC());
415 void helper_fcmp_eq_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
419 set_float_exception_flags(0, &env
->fp_status
);
420 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
421 if (unlikely(relation
== float_relation_unordered
)) {
422 update_fpscr(env
, GETPC());
423 } else if (relation
== float_relation_equal
) {
430 void helper_fcmp_eq_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
434 set_float_exception_flags(0, &env
->fp_status
);
435 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
436 if (unlikely(relation
== float_relation_unordered
)) {
437 update_fpscr(env
, GETPC());
438 } else if (relation
== float_relation_equal
) {
445 void helper_fcmp_gt_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
449 set_float_exception_flags(0, &env
->fp_status
);
450 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
451 if (unlikely(relation
== float_relation_unordered
)) {
452 update_fpscr(env
, GETPC());
453 } else if (relation
== float_relation_greater
) {
460 void helper_fcmp_gt_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
464 set_float_exception_flags(0, &env
->fp_status
);
465 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
466 if (unlikely(relation
== float_relation_unordered
)) {
467 update_fpscr(env
, GETPC());
468 } else if (relation
== float_relation_greater
) {
475 float64
helper_fcnvsd_FT_DT(CPUSH4State
*env
, float32 t0
)
478 set_float_exception_flags(0, &env
->fp_status
);
479 ret
= float32_to_float64(t0
, &env
->fp_status
);
480 update_fpscr(env
, GETPC());
484 float32
helper_fcnvds_DT_FT(CPUSH4State
*env
, float64 t0
)
487 set_float_exception_flags(0, &env
->fp_status
);
488 ret
= float64_to_float32(t0
, &env
->fp_status
);
489 update_fpscr(env
, GETPC());
493 float32
helper_fdiv_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
495 set_float_exception_flags(0, &env
->fp_status
);
496 t0
= float32_div(t0
, t1
, &env
->fp_status
);
497 update_fpscr(env
, GETPC());
501 float64
helper_fdiv_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
503 set_float_exception_flags(0, &env
->fp_status
);
504 t0
= float64_div(t0
, t1
, &env
->fp_status
);
505 update_fpscr(env
, GETPC());
509 float32
helper_float_FT(CPUSH4State
*env
, uint32_t t0
)
512 set_float_exception_flags(0, &env
->fp_status
);
513 ret
= int32_to_float32(t0
, &env
->fp_status
);
514 update_fpscr(env
, GETPC());
518 float64
helper_float_DT(CPUSH4State
*env
, uint32_t t0
)
521 set_float_exception_flags(0, &env
->fp_status
);
522 ret
= int32_to_float64(t0
, &env
->fp_status
);
523 update_fpscr(env
, GETPC());
527 float32
helper_fmac_FT(CPUSH4State
*env
, float32 t0
, float32 t1
, float32 t2
)
529 set_float_exception_flags(0, &env
->fp_status
);
530 t0
= float32_muladd(t0
, t1
, t2
, 0, &env
->fp_status
);
531 update_fpscr(env
, GETPC());
535 float32
helper_fmul_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
537 set_float_exception_flags(0, &env
->fp_status
);
538 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
539 update_fpscr(env
, GETPC());
543 float64
helper_fmul_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
545 set_float_exception_flags(0, &env
->fp_status
);
546 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
547 update_fpscr(env
, GETPC());
551 float32
helper_fneg_T(float32 t0
)
553 return float32_chs(t0
);
556 float32
helper_fsqrt_FT(CPUSH4State
*env
, float32 t0
)
558 set_float_exception_flags(0, &env
->fp_status
);
559 t0
= float32_sqrt(t0
, &env
->fp_status
);
560 update_fpscr(env
, GETPC());
564 float64
helper_fsqrt_DT(CPUSH4State
*env
, float64 t0
)
566 set_float_exception_flags(0, &env
->fp_status
);
567 t0
= float64_sqrt(t0
, &env
->fp_status
);
568 update_fpscr(env
, GETPC());
572 float32
helper_fsub_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
574 set_float_exception_flags(0, &env
->fp_status
);
575 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
576 update_fpscr(env
, GETPC());
580 float64
helper_fsub_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
582 set_float_exception_flags(0, &env
->fp_status
);
583 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
584 update_fpscr(env
, GETPC());
588 uint32_t helper_ftrc_FT(CPUSH4State
*env
, float32 t0
)
591 set_float_exception_flags(0, &env
->fp_status
);
592 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
593 update_fpscr(env
, GETPC());
597 uint32_t helper_ftrc_DT(CPUSH4State
*env
, float64 t0
)
600 set_float_exception_flags(0, &env
->fp_status
);
601 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
602 update_fpscr(env
, GETPC());
606 void helper_fipr(CPUSH4State
*env
, uint32_t m
, uint32_t n
)
611 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
613 set_float_exception_flags(0, &env
->fp_status
);
615 for (i
= 0 ; i
< 4 ; i
++) {
616 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
617 env
->fregs
[bank
+ n
+ i
],
619 r
= float32_add(r
, p
, &env
->fp_status
);
621 update_fpscr(env
, GETPC());
623 env
->fregs
[bank
+ n
+ 3] = r
;
626 void helper_ftrv(CPUSH4State
*env
, uint32_t n
)
628 int bank_matrix
, bank_vector
;
633 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
634 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
635 set_float_exception_flags(0, &env
->fp_status
);
636 for (i
= 0 ; i
< 4 ; i
++) {
638 for (j
= 0 ; j
< 4 ; j
++) {
639 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
640 env
->fregs
[bank_vector
+ j
],
642 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
645 update_fpscr(env
, GETPC());
647 for (i
= 0 ; i
< 4 ; i
++) {
648 env
->fregs
[bank_vector
+ i
] = r
[i
];