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(void *retaddr
)
31 pc
= (unsigned long) retaddr
;
34 /* the PC is inside the translated code. It means that we have
35 a virtual CPU fault */
36 cpu_restore_state(tb
, env
, pc
);
41 #ifndef CONFIG_USER_ONLY
42 #include "softmmu_exec.h"
44 #define MMUSUFFIX _mmu
47 #include "softmmu_template.h"
50 #include "softmmu_template.h"
53 #include "softmmu_template.h"
56 #include "softmmu_template.h"
58 void tlb_fill(target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
63 /* XXX: hack to restore env in all cases, even if not called from
67 ret
= cpu_sh4_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
69 /* now we have a real cpu fault */
70 cpu_restore_state_from_retaddr(retaddr
);
78 void helper_ldtlb(void)
80 #ifdef CONFIG_USER_ONLY
82 cpu_abort(env
, "Unhandled ldtlb");
88 static inline void raise_exception(int index
, void *retaddr
)
90 env
->exception_index
= index
;
91 cpu_restore_state_from_retaddr(retaddr
);
95 void helper_raise_illegal_instruction(void)
97 raise_exception(0x180, GETPC());
100 void helper_raise_slot_illegal_instruction(void)
102 raise_exception(0x1a0, GETPC());
105 void helper_raise_fpu_disable(void)
107 raise_exception(0x800, GETPC());
110 void helper_raise_slot_fpu_disable(void)
112 raise_exception(0x820, GETPC());
115 void helper_debug(void)
117 env
->exception_index
= EXCP_DEBUG
;
121 void helper_sleep(uint32_t next_pc
)
125 env
->exception_index
= EXCP_HLT
;
130 void helper_trapa(uint32_t tra
)
133 raise_exception(0x160, GETPC());
136 void helper_movcal(uint32_t address
, uint32_t value
)
138 if (cpu_sh4_is_cached (env
, address
))
140 memory_content
*r
= malloc (sizeof(memory_content
));
141 r
->address
= address
;
145 *(env
->movcal_backup_tail
) = r
;
146 env
->movcal_backup_tail
= &(r
->next
);
150 void helper_discard_movcal_backup(void)
152 memory_content
*current
= env
->movcal_backup
;
156 memory_content
*next
= current
->next
;
158 env
->movcal_backup
= current
= next
;
160 env
->movcal_backup_tail
= &(env
->movcal_backup
);
164 void helper_ocbi(uint32_t address
)
166 memory_content
**current
= &(env
->movcal_backup
);
169 uint32_t a
= (*current
)->address
;
170 if ((a
& ~0x1F) == (address
& ~0x1F))
172 memory_content
*next
= (*current
)->next
;
173 stl(a
, (*current
)->value
);
177 env
->movcal_backup_tail
= current
;
187 uint32_t helper_addc(uint32_t arg0
, uint32_t arg1
)
193 arg1
= tmp1
+ (env
->sr
& 1);
203 uint32_t helper_addv(uint32_t arg0
, uint32_t arg1
)
205 uint32_t dest
, src
, ans
;
207 if ((int32_t) arg1
>= 0)
211 if ((int32_t) arg0
>= 0)
217 if ((int32_t) arg1
>= 0)
222 if (src
== 0 || src
== 2) {
232 #define T (env->sr & SR_T)
233 #define Q (env->sr & SR_Q ? 1 : 0)
234 #define M (env->sr & SR_M ? 1 : 0)
235 #define SETT env->sr |= SR_T
236 #define CLRT env->sr &= ~SR_T
237 #define SETQ env->sr |= SR_Q
238 #define CLRQ env->sr &= ~SR_Q
239 #define SETM env->sr |= SR_M
240 #define CLRM env->sr &= ~SR_M
242 uint32_t helper_div1(uint32_t arg0
, uint32_t arg1
)
245 uint8_t old_q
, tmp1
= 0xff;
247 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
249 if ((0x80000000 & arg1
) != 0)
346 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
350 void helper_macl(uint32_t arg0
, uint32_t arg1
)
354 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
355 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
356 env
->mach
= (res
>> 32) & 0xffffffff;
357 env
->macl
= res
& 0xffffffff;
358 if (env
->sr
& SR_S
) {
360 env
->mach
|= 0xffff0000;
362 env
->mach
&= 0x00007fff;
366 void helper_macw(uint32_t arg0
, uint32_t arg1
)
370 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
371 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
372 env
->mach
= (res
>> 32) & 0xffffffff;
373 env
->macl
= res
& 0xffffffff;
374 if (env
->sr
& SR_S
) {
375 if (res
< -0x80000000) {
377 env
->macl
= 0x80000000;
378 } else if (res
> 0x000000007fffffff) {
380 env
->macl
= 0x7fffffff;
385 uint32_t helper_subc(uint32_t arg0
, uint32_t arg1
)
391 arg1
= tmp1
- (env
->sr
& SR_T
);
401 uint32_t helper_subv(uint32_t arg0
, uint32_t arg1
)
403 int32_t dest
, src
, ans
;
405 if ((int32_t) arg1
>= 0)
409 if ((int32_t) arg0
>= 0)
415 if ((int32_t) arg1
>= 0)
430 static inline void set_t(void)
435 static inline void clr_t(void)
440 void helper_ld_fpscr(uint32_t val
)
442 env
->fpscr
= val
& FPSCR_MASK
;
443 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
444 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
446 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
448 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
451 static void update_fpscr(void *retaddr
)
453 int xcpt
, cause
, enable
;
455 xcpt
= get_float_exception_flags(&env
->fp_status
);
457 /* Clear the flag entries */
458 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
460 if (unlikely(xcpt
)) {
461 if (xcpt
& float_flag_invalid
) {
462 env
->fpscr
|= FPSCR_FLAG_V
;
464 if (xcpt
& float_flag_divbyzero
) {
465 env
->fpscr
|= FPSCR_FLAG_Z
;
467 if (xcpt
& float_flag_overflow
) {
468 env
->fpscr
|= FPSCR_FLAG_O
;
470 if (xcpt
& float_flag_underflow
) {
471 env
->fpscr
|= FPSCR_FLAG_U
;
473 if (xcpt
& float_flag_inexact
) {
474 env
->fpscr
|= FPSCR_FLAG_I
;
477 /* Accumulate in cause entries */
478 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
479 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
481 /* Generate an exception if enabled */
482 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
483 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
484 if (cause
& enable
) {
485 cpu_restore_state_from_retaddr(retaddr
);
486 env
->exception_index
= 0x120;
492 float32
helper_fabs_FT(float32 t0
)
494 return float32_abs(t0
);
497 float64
helper_fabs_DT(float64 t0
)
499 return float64_abs(t0
);
502 float32
helper_fadd_FT(float32 t0
, float32 t1
)
504 set_float_exception_flags(0, &env
->fp_status
);
505 t0
= float32_add(t0
, t1
, &env
->fp_status
);
506 update_fpscr(GETPC());
510 float64
helper_fadd_DT(float64 t0
, float64 t1
)
512 set_float_exception_flags(0, &env
->fp_status
);
513 t0
= float64_add(t0
, t1
, &env
->fp_status
);
514 update_fpscr(GETPC());
518 void helper_fcmp_eq_FT(float32 t0
, float32 t1
)
522 set_float_exception_flags(0, &env
->fp_status
);
523 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
524 if (unlikely(relation
== float_relation_unordered
)) {
525 update_fpscr(GETPC());
526 } else if (relation
== float_relation_equal
) {
533 void helper_fcmp_eq_DT(float64 t0
, float64 t1
)
537 set_float_exception_flags(0, &env
->fp_status
);
538 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
539 if (unlikely(relation
== float_relation_unordered
)) {
540 update_fpscr(GETPC());
541 } else if (relation
== float_relation_equal
) {
548 void helper_fcmp_gt_FT(float32 t0
, float32 t1
)
552 set_float_exception_flags(0, &env
->fp_status
);
553 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
554 if (unlikely(relation
== float_relation_unordered
)) {
555 update_fpscr(GETPC());
556 } else if (relation
== float_relation_greater
) {
563 void helper_fcmp_gt_DT(float64 t0
, float64 t1
)
567 set_float_exception_flags(0, &env
->fp_status
);
568 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
569 if (unlikely(relation
== float_relation_unordered
)) {
570 update_fpscr(GETPC());
571 } else if (relation
== float_relation_greater
) {
578 float64
helper_fcnvsd_FT_DT(float32 t0
)
581 set_float_exception_flags(0, &env
->fp_status
);
582 ret
= float32_to_float64(t0
, &env
->fp_status
);
583 update_fpscr(GETPC());
587 float32
helper_fcnvds_DT_FT(float64 t0
)
590 set_float_exception_flags(0, &env
->fp_status
);
591 ret
= float64_to_float32(t0
, &env
->fp_status
);
592 update_fpscr(GETPC());
596 float32
helper_fdiv_FT(float32 t0
, float32 t1
)
598 set_float_exception_flags(0, &env
->fp_status
);
599 t0
= float32_div(t0
, t1
, &env
->fp_status
);
600 update_fpscr(GETPC());
604 float64
helper_fdiv_DT(float64 t0
, float64 t1
)
606 set_float_exception_flags(0, &env
->fp_status
);
607 t0
= float64_div(t0
, t1
, &env
->fp_status
);
608 update_fpscr(GETPC());
612 float32
helper_float_FT(uint32_t t0
)
615 set_float_exception_flags(0, &env
->fp_status
);
616 ret
= int32_to_float32(t0
, &env
->fp_status
);
617 update_fpscr(GETPC());
621 float64
helper_float_DT(uint32_t t0
)
624 set_float_exception_flags(0, &env
->fp_status
);
625 ret
= int32_to_float64(t0
, &env
->fp_status
);
626 update_fpscr(GETPC());
630 float32
helper_fmac_FT(float32 t0
, float32 t1
, float32 t2
)
632 set_float_exception_flags(0, &env
->fp_status
);
633 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
634 t0
= float32_add(t0
, t2
, &env
->fp_status
);
635 update_fpscr(GETPC());
639 float32
helper_fmul_FT(float32 t0
, float32 t1
)
641 set_float_exception_flags(0, &env
->fp_status
);
642 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
643 update_fpscr(GETPC());
647 float64
helper_fmul_DT(float64 t0
, float64 t1
)
649 set_float_exception_flags(0, &env
->fp_status
);
650 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
651 update_fpscr(GETPC());
655 float32
helper_fneg_T(float32 t0
)
657 return float32_chs(t0
);
660 float32
helper_fsqrt_FT(float32 t0
)
662 set_float_exception_flags(0, &env
->fp_status
);
663 t0
= float32_sqrt(t0
, &env
->fp_status
);
664 update_fpscr(GETPC());
668 float64
helper_fsqrt_DT(float64 t0
)
670 set_float_exception_flags(0, &env
->fp_status
);
671 t0
= float64_sqrt(t0
, &env
->fp_status
);
672 update_fpscr(GETPC());
676 float32
helper_fsub_FT(float32 t0
, float32 t1
)
678 set_float_exception_flags(0, &env
->fp_status
);
679 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
680 update_fpscr(GETPC());
684 float64
helper_fsub_DT(float64 t0
, float64 t1
)
686 set_float_exception_flags(0, &env
->fp_status
);
687 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
688 update_fpscr(GETPC());
692 uint32_t helper_ftrc_FT(float32 t0
)
695 set_float_exception_flags(0, &env
->fp_status
);
696 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
697 update_fpscr(GETPC());
701 uint32_t helper_ftrc_DT(float64 t0
)
704 set_float_exception_flags(0, &env
->fp_status
);
705 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
706 update_fpscr(GETPC());
710 void helper_fipr(uint32_t m
, uint32_t n
)
715 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
717 set_float_exception_flags(0, &env
->fp_status
);
719 for (i
= 0 ; i
< 4 ; i
++) {
720 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
721 env
->fregs
[bank
+ n
+ i
],
723 r
= float32_add(r
, p
, &env
->fp_status
);
725 update_fpscr(GETPC());
727 env
->fregs
[bank
+ n
+ 3] = r
;
730 void helper_ftrv(uint32_t n
)
732 int bank_matrix
, bank_vector
;
737 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
738 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
739 set_float_exception_flags(0, &env
->fp_status
);
740 for (i
= 0 ; i
< 4 ; i
++) {
742 for (j
= 0 ; j
< 4 ; j
++) {
743 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
744 env
->fregs
[bank_vector
+ j
],
746 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
749 update_fpscr(GETPC());
751 for (i
= 0 ; i
< 4 ; i
++) {
752 env
->fregs
[bank_vector
+ i
] = r
[i
];