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 static void cpu_restore_state_from_retaddr(void *retaddr
)
30 pc
= (unsigned long) retaddr
;
33 /* the PC is inside the translated code. It means that we have
34 a virtual CPU fault */
35 cpu_restore_state(tb
, env
, pc
);
40 #ifndef CONFIG_USER_ONLY
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(target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
61 /* XXX: hack to restore env in all cases, even if not called from
65 ret
= cpu_sh4_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
67 /* now we have a real cpu fault */
68 cpu_restore_state_from_retaddr(retaddr
);
76 void helper_ldtlb(void)
78 #ifdef CONFIG_USER_ONLY
80 cpu_abort(env
, "Unhandled ldtlb");
86 static inline void raise_exception(int index
, void *retaddr
)
88 env
->exception_index
= index
;
89 cpu_restore_state_from_retaddr(retaddr
);
93 void helper_raise_illegal_instruction(void)
95 raise_exception(0x180, GETPC());
98 void helper_raise_slot_illegal_instruction(void)
100 raise_exception(0x1a0, GETPC());
103 void helper_raise_fpu_disable(void)
105 raise_exception(0x800, GETPC());
108 void helper_raise_slot_fpu_disable(void)
110 raise_exception(0x820, GETPC());
113 void helper_debug(void)
115 env
->exception_index
= EXCP_DEBUG
;
119 void helper_sleep(uint32_t next_pc
)
123 env
->exception_index
= EXCP_HLT
;
128 void helper_trapa(uint32_t tra
)
131 raise_exception(0x160, GETPC());
134 void helper_movcal(uint32_t address
, uint32_t value
)
136 if (cpu_sh4_is_cached (env
, address
))
138 memory_content
*r
= malloc (sizeof(memory_content
));
139 r
->address
= address
;
143 *(env
->movcal_backup_tail
) = r
;
144 env
->movcal_backup_tail
= &(r
->next
);
148 void helper_discard_movcal_backup(void)
150 memory_content
*current
= env
->movcal_backup
;
154 memory_content
*next
= current
->next
;
156 env
->movcal_backup
= current
= next
;
158 env
->movcal_backup_tail
= &(env
->movcal_backup
);
162 void helper_ocbi(uint32_t address
)
164 memory_content
**current
= &(env
->movcal_backup
);
167 uint32_t a
= (*current
)->address
;
168 if ((a
& ~0x1F) == (address
& ~0x1F))
170 memory_content
*next
= (*current
)->next
;
171 stl(a
, (*current
)->value
);
175 env
->movcal_backup_tail
= current
;
185 uint32_t helper_addc(uint32_t arg0
, uint32_t arg1
)
191 arg1
= tmp1
+ (env
->sr
& 1);
201 uint32_t helper_addv(uint32_t arg0
, uint32_t arg1
)
203 uint32_t dest
, src
, ans
;
205 if ((int32_t) arg1
>= 0)
209 if ((int32_t) arg0
>= 0)
215 if ((int32_t) arg1
>= 0)
220 if (src
== 0 || src
== 2) {
230 #define T (env->sr & SR_T)
231 #define Q (env->sr & SR_Q ? 1 : 0)
232 #define M (env->sr & SR_M ? 1 : 0)
233 #define SETT env->sr |= SR_T
234 #define CLRT env->sr &= ~SR_T
235 #define SETQ env->sr |= SR_Q
236 #define CLRQ env->sr &= ~SR_Q
237 #define SETM env->sr |= SR_M
238 #define CLRM env->sr &= ~SR_M
240 uint32_t helper_div1(uint32_t arg0
, uint32_t arg1
)
243 uint8_t old_q
, tmp1
= 0xff;
245 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
247 if ((0x80000000 & arg1
) != 0)
344 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
348 void helper_macl(uint32_t arg0
, uint32_t arg1
)
352 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
353 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
354 env
->mach
= (res
>> 32) & 0xffffffff;
355 env
->macl
= res
& 0xffffffff;
356 if (env
->sr
& SR_S
) {
358 env
->mach
|= 0xffff0000;
360 env
->mach
&= 0x00007fff;
364 void helper_macw(uint32_t arg0
, uint32_t arg1
)
368 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
369 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
370 env
->mach
= (res
>> 32) & 0xffffffff;
371 env
->macl
= res
& 0xffffffff;
372 if (env
->sr
& SR_S
) {
373 if (res
< -0x80000000) {
375 env
->macl
= 0x80000000;
376 } else if (res
> 0x000000007fffffff) {
378 env
->macl
= 0x7fffffff;
383 uint32_t helper_subc(uint32_t arg0
, uint32_t arg1
)
389 arg1
= tmp1
- (env
->sr
& SR_T
);
399 uint32_t helper_subv(uint32_t arg0
, uint32_t arg1
)
401 int32_t dest
, src
, ans
;
403 if ((int32_t) arg1
>= 0)
407 if ((int32_t) arg0
>= 0)
413 if ((int32_t) arg1
>= 0)
428 static inline void set_t(void)
433 static inline void clr_t(void)
438 void helper_ld_fpscr(uint32_t val
)
440 env
->fpscr
= val
& FPSCR_MASK
;
441 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
442 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
444 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
446 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
449 static void update_fpscr(void *retaddr
)
451 int xcpt
, cause
, enable
;
453 xcpt
= get_float_exception_flags(&env
->fp_status
);
455 /* Clear the flag entries */
456 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
458 if (unlikely(xcpt
)) {
459 if (xcpt
& float_flag_invalid
) {
460 env
->fpscr
|= FPSCR_FLAG_V
;
462 if (xcpt
& float_flag_divbyzero
) {
463 env
->fpscr
|= FPSCR_FLAG_Z
;
465 if (xcpt
& float_flag_overflow
) {
466 env
->fpscr
|= FPSCR_FLAG_O
;
468 if (xcpt
& float_flag_underflow
) {
469 env
->fpscr
|= FPSCR_FLAG_U
;
471 if (xcpt
& float_flag_inexact
) {
472 env
->fpscr
|= FPSCR_FLAG_I
;
475 /* Accumulate in cause entries */
476 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
477 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
479 /* Generate an exception if enabled */
480 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
481 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
482 if (cause
& enable
) {
483 cpu_restore_state_from_retaddr(retaddr
);
484 env
->exception_index
= 0x120;
490 float32
helper_fabs_FT(float32 t0
)
492 return float32_abs(t0
);
495 float64
helper_fabs_DT(float64 t0
)
497 return float64_abs(t0
);
500 float32
helper_fadd_FT(float32 t0
, float32 t1
)
502 set_float_exception_flags(0, &env
->fp_status
);
503 t0
= float32_add(t0
, t1
, &env
->fp_status
);
504 update_fpscr(GETPC());
508 float64
helper_fadd_DT(float64 t0
, float64 t1
)
510 set_float_exception_flags(0, &env
->fp_status
);
511 t0
= float64_add(t0
, t1
, &env
->fp_status
);
512 update_fpscr(GETPC());
516 void helper_fcmp_eq_FT(float32 t0
, float32 t1
)
520 set_float_exception_flags(0, &env
->fp_status
);
521 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
522 if (unlikely(relation
== float_relation_unordered
)) {
523 update_fpscr(GETPC());
524 } else if (relation
== float_relation_equal
) {
531 void helper_fcmp_eq_DT(float64 t0
, float64 t1
)
535 set_float_exception_flags(0, &env
->fp_status
);
536 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
537 if (unlikely(relation
== float_relation_unordered
)) {
538 update_fpscr(GETPC());
539 } else if (relation
== float_relation_equal
) {
546 void helper_fcmp_gt_FT(float32 t0
, float32 t1
)
550 set_float_exception_flags(0, &env
->fp_status
);
551 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
552 if (unlikely(relation
== float_relation_unordered
)) {
553 update_fpscr(GETPC());
554 } else if (relation
== float_relation_greater
) {
561 void helper_fcmp_gt_DT(float64 t0
, float64 t1
)
565 set_float_exception_flags(0, &env
->fp_status
);
566 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
567 if (unlikely(relation
== float_relation_unordered
)) {
568 update_fpscr(GETPC());
569 } else if (relation
== float_relation_greater
) {
576 float64
helper_fcnvsd_FT_DT(float32 t0
)
579 set_float_exception_flags(0, &env
->fp_status
);
580 ret
= float32_to_float64(t0
, &env
->fp_status
);
581 update_fpscr(GETPC());
585 float32
helper_fcnvds_DT_FT(float64 t0
)
588 set_float_exception_flags(0, &env
->fp_status
);
589 ret
= float64_to_float32(t0
, &env
->fp_status
);
590 update_fpscr(GETPC());
594 float32
helper_fdiv_FT(float32 t0
, float32 t1
)
596 set_float_exception_flags(0, &env
->fp_status
);
597 t0
= float32_div(t0
, t1
, &env
->fp_status
);
598 update_fpscr(GETPC());
602 float64
helper_fdiv_DT(float64 t0
, float64 t1
)
604 set_float_exception_flags(0, &env
->fp_status
);
605 t0
= float64_div(t0
, t1
, &env
->fp_status
);
606 update_fpscr(GETPC());
610 float32
helper_float_FT(uint32_t t0
)
613 set_float_exception_flags(0, &env
->fp_status
);
614 ret
= int32_to_float32(t0
, &env
->fp_status
);
615 update_fpscr(GETPC());
619 float64
helper_float_DT(uint32_t t0
)
622 set_float_exception_flags(0, &env
->fp_status
);
623 ret
= int32_to_float64(t0
, &env
->fp_status
);
624 update_fpscr(GETPC());
628 float32
helper_fmac_FT(float32 t0
, float32 t1
, float32 t2
)
630 set_float_exception_flags(0, &env
->fp_status
);
631 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
632 t0
= float32_add(t0
, t2
, &env
->fp_status
);
633 update_fpscr(GETPC());
637 float32
helper_fmul_FT(float32 t0
, float32 t1
)
639 set_float_exception_flags(0, &env
->fp_status
);
640 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
641 update_fpscr(GETPC());
645 float64
helper_fmul_DT(float64 t0
, float64 t1
)
647 set_float_exception_flags(0, &env
->fp_status
);
648 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
649 update_fpscr(GETPC());
653 float32
helper_fneg_T(float32 t0
)
655 return float32_chs(t0
);
658 float32
helper_fsqrt_FT(float32 t0
)
660 set_float_exception_flags(0, &env
->fp_status
);
661 t0
= float32_sqrt(t0
, &env
->fp_status
);
662 update_fpscr(GETPC());
666 float64
helper_fsqrt_DT(float64 t0
)
668 set_float_exception_flags(0, &env
->fp_status
);
669 t0
= float64_sqrt(t0
, &env
->fp_status
);
670 update_fpscr(GETPC());
674 float32
helper_fsub_FT(float32 t0
, float32 t1
)
676 set_float_exception_flags(0, &env
->fp_status
);
677 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
678 update_fpscr(GETPC());
682 float64
helper_fsub_DT(float64 t0
, float64 t1
)
684 set_float_exception_flags(0, &env
->fp_status
);
685 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
686 update_fpscr(GETPC());
690 uint32_t helper_ftrc_FT(float32 t0
)
693 set_float_exception_flags(0, &env
->fp_status
);
694 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
695 update_fpscr(GETPC());
699 uint32_t helper_ftrc_DT(float64 t0
)
702 set_float_exception_flags(0, &env
->fp_status
);
703 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
704 update_fpscr(GETPC());
708 void helper_fipr(uint32_t m
, uint32_t n
)
713 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
715 set_float_exception_flags(0, &env
->fp_status
);
717 for (i
= 0 ; i
< 4 ; i
++) {
718 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
719 env
->fregs
[bank
+ n
+ i
],
721 r
= float32_add(r
, p
, &env
->fp_status
);
723 update_fpscr(GETPC());
725 env
->fregs
[bank
+ n
+ 3] = r
;
728 void helper_ftrv(uint32_t n
)
730 int bank_matrix
, bank_vector
;
735 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
736 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
737 set_float_exception_flags(0, &env
->fp_status
);
738 for (i
= 0 ; i
< 4 ; i
++) {
740 for (j
= 0 ; j
< 4 ; j
++) {
741 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
742 env
->fregs
[bank_vector
+ j
],
744 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
747 update_fpscr(GETPC());
749 for (i
= 0 ; i
< 4 ; i
++) {
750 env
->fregs
[bank_vector
+ i
] = r
[i
];