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/>.
22 #include "dyngen-exec.h"
25 static void cpu_restore_state_from_retaddr(uintptr_t retaddr
)
30 tb
= tb_find_pc(retaddr
);
32 /* the PC is inside the translated code. It means that we have
33 a virtual CPU fault */
34 cpu_restore_state(tb
, env
, retaddr
);
39 #ifndef CONFIG_USER_ONLY
40 #include "softmmu_exec.h"
42 #define MMUSUFFIX _mmu
45 #include "softmmu_template.h"
48 #include "softmmu_template.h"
51 #include "softmmu_template.h"
54 #include "softmmu_template.h"
56 void tlb_fill(CPUSH4State
*env1
, target_ulong addr
, int is_write
, int mmu_idx
,
59 CPUSH4State
*saved_env
;
64 ret
= cpu_sh4_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
66 /* now we have a real cpu fault */
67 cpu_restore_state_from_retaddr(retaddr
);
75 void helper_ldtlb(void)
77 #ifdef CONFIG_USER_ONLY
79 cpu_abort(env
, "Unhandled ldtlb");
85 static inline void raise_exception(int index
, uintptr_t retaddr
)
87 env
->exception_index
= index
;
88 cpu_restore_state_from_retaddr(retaddr
);
92 void helper_raise_illegal_instruction(void)
94 raise_exception(0x180, GETPC());
97 void helper_raise_slot_illegal_instruction(void)
99 raise_exception(0x1a0, GETPC());
102 void helper_raise_fpu_disable(void)
104 raise_exception(0x800, GETPC());
107 void helper_raise_slot_fpu_disable(void)
109 raise_exception(0x820, GETPC());
112 void helper_debug(void)
114 env
->exception_index
= EXCP_DEBUG
;
118 void helper_sleep(uint32_t next_pc
)
122 env
->exception_index
= EXCP_HLT
;
127 void helper_trapa(uint32_t tra
)
130 raise_exception(0x160, GETPC());
133 void helper_movcal(uint32_t address
, uint32_t value
)
135 if (cpu_sh4_is_cached (env
, address
))
137 memory_content
*r
= malloc (sizeof(memory_content
));
138 r
->address
= address
;
142 *(env
->movcal_backup_tail
) = r
;
143 env
->movcal_backup_tail
= &(r
->next
);
147 void helper_discard_movcal_backup(void)
149 memory_content
*current
= env
->movcal_backup
;
153 memory_content
*next
= current
->next
;
155 env
->movcal_backup
= current
= next
;
157 env
->movcal_backup_tail
= &(env
->movcal_backup
);
161 void helper_ocbi(uint32_t address
)
163 memory_content
**current
= &(env
->movcal_backup
);
166 uint32_t a
= (*current
)->address
;
167 if ((a
& ~0x1F) == (address
& ~0x1F))
169 memory_content
*next
= (*current
)->next
;
170 stl(a
, (*current
)->value
);
174 env
->movcal_backup_tail
= current
;
184 uint32_t helper_addc(uint32_t arg0
, uint32_t arg1
)
190 arg1
= tmp1
+ (env
->sr
& 1);
200 uint32_t helper_addv(uint32_t arg0
, uint32_t arg1
)
202 uint32_t dest
, src
, ans
;
204 if ((int32_t) arg1
>= 0)
208 if ((int32_t) arg0
>= 0)
214 if ((int32_t) arg1
>= 0)
219 if (src
== 0 || src
== 2) {
229 #define T (env->sr & SR_T)
230 #define Q (env->sr & SR_Q ? 1 : 0)
231 #define M (env->sr & SR_M ? 1 : 0)
232 #define SETT env->sr |= SR_T
233 #define CLRT env->sr &= ~SR_T
234 #define SETQ env->sr |= SR_Q
235 #define CLRQ env->sr &= ~SR_Q
236 #define SETM env->sr |= SR_M
237 #define CLRM env->sr &= ~SR_M
239 uint32_t helper_div1(uint32_t arg0
, uint32_t arg1
)
242 uint8_t old_q
, tmp1
= 0xff;
244 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
246 if ((0x80000000 & arg1
) != 0)
343 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
347 void helper_macl(uint32_t arg0
, uint32_t arg1
)
351 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
352 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
353 env
->mach
= (res
>> 32) & 0xffffffff;
354 env
->macl
= res
& 0xffffffff;
355 if (env
->sr
& SR_S
) {
357 env
->mach
|= 0xffff0000;
359 env
->mach
&= 0x00007fff;
363 void helper_macw(uint32_t arg0
, uint32_t arg1
)
367 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
368 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
369 env
->mach
= (res
>> 32) & 0xffffffff;
370 env
->macl
= res
& 0xffffffff;
371 if (env
->sr
& SR_S
) {
372 if (res
< -0x80000000) {
374 env
->macl
= 0x80000000;
375 } else if (res
> 0x000000007fffffff) {
377 env
->macl
= 0x7fffffff;
382 uint32_t helper_subc(uint32_t arg0
, uint32_t arg1
)
388 arg1
= tmp1
- (env
->sr
& SR_T
);
398 uint32_t helper_subv(uint32_t arg0
, uint32_t arg1
)
400 int32_t dest
, src
, ans
;
402 if ((int32_t) arg1
>= 0)
406 if ((int32_t) arg0
>= 0)
412 if ((int32_t) arg1
>= 0)
427 static inline void set_t(void)
432 static inline void clr_t(void)
437 void helper_ld_fpscr(uint32_t val
)
439 env
->fpscr
= val
& FPSCR_MASK
;
440 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
441 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
443 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
445 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
448 static void update_fpscr(uintptr_t retaddr
)
450 int xcpt
, cause
, enable
;
452 xcpt
= get_float_exception_flags(&env
->fp_status
);
454 /* Clear the flag entries */
455 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
457 if (unlikely(xcpt
)) {
458 if (xcpt
& float_flag_invalid
) {
459 env
->fpscr
|= FPSCR_FLAG_V
;
461 if (xcpt
& float_flag_divbyzero
) {
462 env
->fpscr
|= FPSCR_FLAG_Z
;
464 if (xcpt
& float_flag_overflow
) {
465 env
->fpscr
|= FPSCR_FLAG_O
;
467 if (xcpt
& float_flag_underflow
) {
468 env
->fpscr
|= FPSCR_FLAG_U
;
470 if (xcpt
& float_flag_inexact
) {
471 env
->fpscr
|= FPSCR_FLAG_I
;
474 /* Accumulate in cause entries */
475 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
476 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
478 /* Generate an exception if enabled */
479 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
480 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
481 if (cause
& enable
) {
482 cpu_restore_state_from_retaddr(retaddr
);
483 env
->exception_index
= 0x120;
489 float32
helper_fabs_FT(float32 t0
)
491 return float32_abs(t0
);
494 float64
helper_fabs_DT(float64 t0
)
496 return float64_abs(t0
);
499 float32
helper_fadd_FT(float32 t0
, float32 t1
)
501 set_float_exception_flags(0, &env
->fp_status
);
502 t0
= float32_add(t0
, t1
, &env
->fp_status
);
503 update_fpscr(GETPC());
507 float64
helper_fadd_DT(float64 t0
, float64 t1
)
509 set_float_exception_flags(0, &env
->fp_status
);
510 t0
= float64_add(t0
, t1
, &env
->fp_status
);
511 update_fpscr(GETPC());
515 void helper_fcmp_eq_FT(float32 t0
, float32 t1
)
519 set_float_exception_flags(0, &env
->fp_status
);
520 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
521 if (unlikely(relation
== float_relation_unordered
)) {
522 update_fpscr(GETPC());
523 } else if (relation
== float_relation_equal
) {
530 void helper_fcmp_eq_DT(float64 t0
, float64 t1
)
534 set_float_exception_flags(0, &env
->fp_status
);
535 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
536 if (unlikely(relation
== float_relation_unordered
)) {
537 update_fpscr(GETPC());
538 } else if (relation
== float_relation_equal
) {
545 void helper_fcmp_gt_FT(float32 t0
, float32 t1
)
549 set_float_exception_flags(0, &env
->fp_status
);
550 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
551 if (unlikely(relation
== float_relation_unordered
)) {
552 update_fpscr(GETPC());
553 } else if (relation
== float_relation_greater
) {
560 void helper_fcmp_gt_DT(float64 t0
, float64 t1
)
564 set_float_exception_flags(0, &env
->fp_status
);
565 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
566 if (unlikely(relation
== float_relation_unordered
)) {
567 update_fpscr(GETPC());
568 } else if (relation
== float_relation_greater
) {
575 float64
helper_fcnvsd_FT_DT(float32 t0
)
578 set_float_exception_flags(0, &env
->fp_status
);
579 ret
= float32_to_float64(t0
, &env
->fp_status
);
580 update_fpscr(GETPC());
584 float32
helper_fcnvds_DT_FT(float64 t0
)
587 set_float_exception_flags(0, &env
->fp_status
);
588 ret
= float64_to_float32(t0
, &env
->fp_status
);
589 update_fpscr(GETPC());
593 float32
helper_fdiv_FT(float32 t0
, float32 t1
)
595 set_float_exception_flags(0, &env
->fp_status
);
596 t0
= float32_div(t0
, t1
, &env
->fp_status
);
597 update_fpscr(GETPC());
601 float64
helper_fdiv_DT(float64 t0
, float64 t1
)
603 set_float_exception_flags(0, &env
->fp_status
);
604 t0
= float64_div(t0
, t1
, &env
->fp_status
);
605 update_fpscr(GETPC());
609 float32
helper_float_FT(uint32_t t0
)
612 set_float_exception_flags(0, &env
->fp_status
);
613 ret
= int32_to_float32(t0
, &env
->fp_status
);
614 update_fpscr(GETPC());
618 float64
helper_float_DT(uint32_t t0
)
621 set_float_exception_flags(0, &env
->fp_status
);
622 ret
= int32_to_float64(t0
, &env
->fp_status
);
623 update_fpscr(GETPC());
627 float32
helper_fmac_FT(float32 t0
, float32 t1
, float32 t2
)
629 set_float_exception_flags(0, &env
->fp_status
);
630 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
631 t0
= float32_add(t0
, t2
, &env
->fp_status
);
632 update_fpscr(GETPC());
636 float32
helper_fmul_FT(float32 t0
, float32 t1
)
638 set_float_exception_flags(0, &env
->fp_status
);
639 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
640 update_fpscr(GETPC());
644 float64
helper_fmul_DT(float64 t0
, float64 t1
)
646 set_float_exception_flags(0, &env
->fp_status
);
647 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
648 update_fpscr(GETPC());
652 float32
helper_fneg_T(float32 t0
)
654 return float32_chs(t0
);
657 float32
helper_fsqrt_FT(float32 t0
)
659 set_float_exception_flags(0, &env
->fp_status
);
660 t0
= float32_sqrt(t0
, &env
->fp_status
);
661 update_fpscr(GETPC());
665 float64
helper_fsqrt_DT(float64 t0
)
667 set_float_exception_flags(0, &env
->fp_status
);
668 t0
= float64_sqrt(t0
, &env
->fp_status
);
669 update_fpscr(GETPC());
673 float32
helper_fsub_FT(float32 t0
, float32 t1
)
675 set_float_exception_flags(0, &env
->fp_status
);
676 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
677 update_fpscr(GETPC());
681 float64
helper_fsub_DT(float64 t0
, float64 t1
)
683 set_float_exception_flags(0, &env
->fp_status
);
684 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
685 update_fpscr(GETPC());
689 uint32_t helper_ftrc_FT(float32 t0
)
692 set_float_exception_flags(0, &env
->fp_status
);
693 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
694 update_fpscr(GETPC());
698 uint32_t helper_ftrc_DT(float64 t0
)
701 set_float_exception_flags(0, &env
->fp_status
);
702 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
703 update_fpscr(GETPC());
707 void helper_fipr(uint32_t m
, uint32_t n
)
712 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
714 set_float_exception_flags(0, &env
->fp_status
);
716 for (i
= 0 ; i
< 4 ; i
++) {
717 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
718 env
->fregs
[bank
+ n
+ i
],
720 r
= float32_add(r
, p
, &env
->fp_status
);
722 update_fpscr(GETPC());
724 env
->fregs
[bank
+ n
+ 3] = r
;
727 void helper_ftrv(uint32_t n
)
729 int bank_matrix
, bank_vector
;
734 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
735 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
736 set_float_exception_flags(0, &env
->fp_status
);
737 for (i
= 0 ; i
< 4 ; i
++) {
739 for (j
= 0 ; j
< 4 ; j
++) {
740 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
741 env
->fregs
[bank_vector
+ j
],
743 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
746 update_fpscr(GETPC());
748 for (i
= 0 ; i
< 4 ; i
++) {
749 env
->fregs
[bank_vector
+ i
] = r
[i
];