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 inline void cpu_restore_state_from_retaddr(CPUSH4State
*env
,
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
*env
, target_ulong addr
, int is_write
, int mmu_idx
,
61 ret
= cpu_sh4_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
63 /* now we have a real cpu fault */
64 cpu_restore_state_from_retaddr(env
, retaddr
);
71 void helper_ldtlb(CPUSH4State
*env
)
73 #ifdef CONFIG_USER_ONLY
75 cpu_abort(env
, "Unhandled ldtlb");
81 static inline void QEMU_NORETURN
raise_exception(CPUSH4State
*env
, int index
,
84 env
->exception_index
= index
;
85 cpu_restore_state_from_retaddr(env
, retaddr
);
89 void helper_raise_illegal_instruction(CPUSH4State
*env
)
91 raise_exception(env
, 0x180, 0);
94 void helper_raise_slot_illegal_instruction(CPUSH4State
*env
)
96 raise_exception(env
, 0x1a0, 0);
99 void helper_raise_fpu_disable(CPUSH4State
*env
)
101 raise_exception(env
, 0x800, 0);
104 void helper_raise_slot_fpu_disable(CPUSH4State
*env
)
106 raise_exception(env
, 0x820, 0);
109 void helper_debug(CPUSH4State
*env
)
111 raise_exception(env
, EXCP_DEBUG
, 0);
114 void helper_sleep(CPUSH4State
*env
)
118 raise_exception(env
, EXCP_HLT
, 0);
121 void helper_trapa(CPUSH4State
*env
, uint32_t tra
)
124 raise_exception(env
, 0x160, 0);
127 void helper_movcal(CPUSH4State
*env
, uint32_t address
, uint32_t value
)
129 if (cpu_sh4_is_cached (env
, address
))
131 memory_content
*r
= malloc (sizeof(memory_content
));
132 r
->address
= address
;
136 *(env
->movcal_backup_tail
) = r
;
137 env
->movcal_backup_tail
= &(r
->next
);
141 void helper_discard_movcal_backup(CPUSH4State
*env
)
143 memory_content
*current
= env
->movcal_backup
;
147 memory_content
*next
= current
->next
;
149 env
->movcal_backup
= current
= next
;
151 env
->movcal_backup_tail
= &(env
->movcal_backup
);
155 void helper_ocbi(CPUSH4State
*env
, uint32_t address
)
157 memory_content
**current
= &(env
->movcal_backup
);
160 uint32_t a
= (*current
)->address
;
161 if ((a
& ~0x1F) == (address
& ~0x1F))
163 memory_content
*next
= (*current
)->next
;
164 cpu_stl_data(env
, a
, (*current
)->value
);
168 env
->movcal_backup_tail
= current
;
178 #define T (env->sr & SR_T)
179 #define Q (env->sr & SR_Q ? 1 : 0)
180 #define M (env->sr & SR_M ? 1 : 0)
181 #define SETT env->sr |= SR_T
182 #define CLRT env->sr &= ~SR_T
183 #define SETQ env->sr |= SR_Q
184 #define CLRQ env->sr &= ~SR_Q
185 #define SETM env->sr |= SR_M
186 #define CLRM env->sr &= ~SR_M
188 uint32_t helper_div1(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
191 uint8_t old_q
, tmp1
= 0xff;
193 //printf("div1 arg0=0x%08x arg1=0x%08x M=%d Q=%d T=%d\n", arg0, arg1, M, Q, T);
195 if ((0x80000000 & arg1
) != 0)
292 //printf("Output: arg1=0x%08x M=%d Q=%d T=%d\n", arg1, M, Q, T);
296 void helper_macl(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
300 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
301 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
302 env
->mach
= (res
>> 32) & 0xffffffff;
303 env
->macl
= res
& 0xffffffff;
304 if (env
->sr
& SR_S
) {
306 env
->mach
|= 0xffff0000;
308 env
->mach
&= 0x00007fff;
312 void helper_macw(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
316 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
317 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
318 env
->mach
= (res
>> 32) & 0xffffffff;
319 env
->macl
= res
& 0xffffffff;
320 if (env
->sr
& SR_S
) {
321 if (res
< -0x80000000) {
323 env
->macl
= 0x80000000;
324 } else if (res
> 0x000000007fffffff) {
326 env
->macl
= 0x7fffffff;
331 static inline void set_t(CPUSH4State
*env
)
336 static inline void clr_t(CPUSH4State
*env
)
341 void helper_ld_fpscr(CPUSH4State
*env
, uint32_t val
)
343 env
->fpscr
= val
& FPSCR_MASK
;
344 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
345 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
347 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
349 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
352 static void update_fpscr(CPUSH4State
*env
, uintptr_t retaddr
)
354 int xcpt
, cause
, enable
;
356 xcpt
= get_float_exception_flags(&env
->fp_status
);
358 /* Clear the flag entries */
359 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
361 if (unlikely(xcpt
)) {
362 if (xcpt
& float_flag_invalid
) {
363 env
->fpscr
|= FPSCR_FLAG_V
;
365 if (xcpt
& float_flag_divbyzero
) {
366 env
->fpscr
|= FPSCR_FLAG_Z
;
368 if (xcpt
& float_flag_overflow
) {
369 env
->fpscr
|= FPSCR_FLAG_O
;
371 if (xcpt
& float_flag_underflow
) {
372 env
->fpscr
|= FPSCR_FLAG_U
;
374 if (xcpt
& float_flag_inexact
) {
375 env
->fpscr
|= FPSCR_FLAG_I
;
378 /* Accumulate in cause entries */
379 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
380 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
382 /* Generate an exception if enabled */
383 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
384 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
385 if (cause
& enable
) {
386 raise_exception(env
, 0x120, retaddr
);
391 float32
helper_fabs_FT(float32 t0
)
393 return float32_abs(t0
);
396 float64
helper_fabs_DT(float64 t0
)
398 return float64_abs(t0
);
401 float32
helper_fadd_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
403 set_float_exception_flags(0, &env
->fp_status
);
404 t0
= float32_add(t0
, t1
, &env
->fp_status
);
405 update_fpscr(env
, GETPC());
409 float64
helper_fadd_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
411 set_float_exception_flags(0, &env
->fp_status
);
412 t0
= float64_add(t0
, t1
, &env
->fp_status
);
413 update_fpscr(env
, GETPC());
417 void helper_fcmp_eq_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
421 set_float_exception_flags(0, &env
->fp_status
);
422 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
423 if (unlikely(relation
== float_relation_unordered
)) {
424 update_fpscr(env
, GETPC());
425 } else if (relation
== float_relation_equal
) {
432 void helper_fcmp_eq_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
436 set_float_exception_flags(0, &env
->fp_status
);
437 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
438 if (unlikely(relation
== float_relation_unordered
)) {
439 update_fpscr(env
, GETPC());
440 } else if (relation
== float_relation_equal
) {
447 void helper_fcmp_gt_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
451 set_float_exception_flags(0, &env
->fp_status
);
452 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
453 if (unlikely(relation
== float_relation_unordered
)) {
454 update_fpscr(env
, GETPC());
455 } else if (relation
== float_relation_greater
) {
462 void helper_fcmp_gt_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
466 set_float_exception_flags(0, &env
->fp_status
);
467 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
468 if (unlikely(relation
== float_relation_unordered
)) {
469 update_fpscr(env
, GETPC());
470 } else if (relation
== float_relation_greater
) {
477 float64
helper_fcnvsd_FT_DT(CPUSH4State
*env
, float32 t0
)
480 set_float_exception_flags(0, &env
->fp_status
);
481 ret
= float32_to_float64(t0
, &env
->fp_status
);
482 update_fpscr(env
, GETPC());
486 float32
helper_fcnvds_DT_FT(CPUSH4State
*env
, float64 t0
)
489 set_float_exception_flags(0, &env
->fp_status
);
490 ret
= float64_to_float32(t0
, &env
->fp_status
);
491 update_fpscr(env
, GETPC());
495 float32
helper_fdiv_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
497 set_float_exception_flags(0, &env
->fp_status
);
498 t0
= float32_div(t0
, t1
, &env
->fp_status
);
499 update_fpscr(env
, GETPC());
503 float64
helper_fdiv_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
505 set_float_exception_flags(0, &env
->fp_status
);
506 t0
= float64_div(t0
, t1
, &env
->fp_status
);
507 update_fpscr(env
, GETPC());
511 float32
helper_float_FT(CPUSH4State
*env
, uint32_t t0
)
514 set_float_exception_flags(0, &env
->fp_status
);
515 ret
= int32_to_float32(t0
, &env
->fp_status
);
516 update_fpscr(env
, GETPC());
520 float64
helper_float_DT(CPUSH4State
*env
, uint32_t t0
)
523 set_float_exception_flags(0, &env
->fp_status
);
524 ret
= int32_to_float64(t0
, &env
->fp_status
);
525 update_fpscr(env
, GETPC());
529 float32
helper_fmac_FT(CPUSH4State
*env
, float32 t0
, float32 t1
, float32 t2
)
531 set_float_exception_flags(0, &env
->fp_status
);
532 t0
= float32_muladd(t0
, t1
, t2
, 0, &env
->fp_status
);
533 update_fpscr(env
, GETPC());
537 float32
helper_fmul_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
539 set_float_exception_flags(0, &env
->fp_status
);
540 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
541 update_fpscr(env
, GETPC());
545 float64
helper_fmul_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
547 set_float_exception_flags(0, &env
->fp_status
);
548 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
549 update_fpscr(env
, GETPC());
553 float32
helper_fneg_T(float32 t0
)
555 return float32_chs(t0
);
558 float32
helper_fsqrt_FT(CPUSH4State
*env
, float32 t0
)
560 set_float_exception_flags(0, &env
->fp_status
);
561 t0
= float32_sqrt(t0
, &env
->fp_status
);
562 update_fpscr(env
, GETPC());
566 float64
helper_fsqrt_DT(CPUSH4State
*env
, float64 t0
)
568 set_float_exception_flags(0, &env
->fp_status
);
569 t0
= float64_sqrt(t0
, &env
->fp_status
);
570 update_fpscr(env
, GETPC());
574 float32
helper_fsub_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
576 set_float_exception_flags(0, &env
->fp_status
);
577 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
578 update_fpscr(env
, GETPC());
582 float64
helper_fsub_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
584 set_float_exception_flags(0, &env
->fp_status
);
585 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
586 update_fpscr(env
, GETPC());
590 uint32_t helper_ftrc_FT(CPUSH4State
*env
, float32 t0
)
593 set_float_exception_flags(0, &env
->fp_status
);
594 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
595 update_fpscr(env
, GETPC());
599 uint32_t helper_ftrc_DT(CPUSH4State
*env
, float64 t0
)
602 set_float_exception_flags(0, &env
->fp_status
);
603 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
604 update_fpscr(env
, GETPC());
608 void helper_fipr(CPUSH4State
*env
, uint32_t m
, uint32_t n
)
613 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
615 set_float_exception_flags(0, &env
->fp_status
);
617 for (i
= 0 ; i
< 4 ; i
++) {
618 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
619 env
->fregs
[bank
+ n
+ i
],
621 r
= float32_add(r
, p
, &env
->fp_status
);
623 update_fpscr(env
, GETPC());
625 env
->fregs
[bank
+ n
+ 3] = r
;
628 void helper_ftrv(CPUSH4State
*env
, uint32_t n
)
630 int bank_matrix
, bank_vector
;
635 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
636 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
637 set_float_exception_flags(0, &env
->fp_status
);
638 for (i
= 0 ; i
< 4 ; i
++) {
640 for (j
= 0 ; j
< 4 ; j
++) {
641 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
642 env
->fregs
[bank_vector
+ j
],
644 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
647 update_fpscr(env
, GETPC());
649 for (i
= 0 ; i
< 4 ; i
++) {
650 env
->fregs
[bank_vector
+ i
] = r
[i
];