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/>.
19 #include "qemu/osdep.h"
21 #include "exec/helper-proto.h"
22 #include "exec/exec-all.h"
23 #include "exec/cpu_ldst.h"
25 #ifndef CONFIG_USER_ONLY
27 void tlb_fill(CPUState
*cs
, target_ulong addr
, MMUAccessType access_type
,
28 int mmu_idx
, uintptr_t retaddr
)
32 ret
= superh_cpu_handle_mmu_fault(cs
, addr
, access_type
, mmu_idx
);
34 /* now we have a real cpu fault */
36 cpu_restore_state(cs
, retaddr
);
44 #ifdef CONFIG_USER_ONLY
45 void QEMU_NORETURN
helper_ldtlb(CPUSH4State
*env
)
47 void helper_ldtlb(CPUSH4State
*env
)
50 #ifdef CONFIG_USER_ONLY
51 SuperHCPU
*cpu
= sh_env_get_cpu(env
);
54 cpu_abort(CPU(cpu
), "Unhandled ldtlb");
60 static inline void QEMU_NORETURN
raise_exception(CPUSH4State
*env
, int index
,
63 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
65 cs
->exception_index
= index
;
67 cpu_restore_state(cs
, retaddr
);
72 void QEMU_NORETURN
helper_raise_illegal_instruction(CPUSH4State
*env
)
74 raise_exception(env
, 0x180, 0);
77 void QEMU_NORETURN
helper_raise_slot_illegal_instruction(CPUSH4State
*env
)
79 raise_exception(env
, 0x1a0, 0);
82 void QEMU_NORETURN
helper_raise_fpu_disable(CPUSH4State
*env
)
84 raise_exception(env
, 0x800, 0);
87 void QEMU_NORETURN
helper_raise_slot_fpu_disable(CPUSH4State
*env
)
89 raise_exception(env
, 0x820, 0);
92 void QEMU_NORETURN
helper_debug(CPUSH4State
*env
)
94 raise_exception(env
, EXCP_DEBUG
, 0);
97 void QEMU_NORETURN
helper_sleep(CPUSH4State
*env
)
99 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
103 raise_exception(env
, EXCP_HLT
, 0);
106 void QEMU_NORETURN
helper_trapa(CPUSH4State
*env
, uint32_t tra
)
109 raise_exception(env
, 0x160, 0);
112 void helper_movcal(CPUSH4State
*env
, uint32_t address
, uint32_t value
)
114 if (cpu_sh4_is_cached (env
, address
))
116 memory_content
*r
= g_new(memory_content
, 1);
118 r
->address
= address
;
122 *(env
->movcal_backup_tail
) = r
;
123 env
->movcal_backup_tail
= &(r
->next
);
127 void helper_discard_movcal_backup(CPUSH4State
*env
)
129 memory_content
*current
= env
->movcal_backup
;
133 memory_content
*next
= current
->next
;
135 env
->movcal_backup
= current
= next
;
137 env
->movcal_backup_tail
= &(env
->movcal_backup
);
141 void helper_ocbi(CPUSH4State
*env
, uint32_t address
)
143 memory_content
**current
= &(env
->movcal_backup
);
146 uint32_t a
= (*current
)->address
;
147 if ((a
& ~0x1F) == (address
& ~0x1F))
149 memory_content
*next
= (*current
)->next
;
150 cpu_stl_data(env
, a
, (*current
)->value
);
154 env
->movcal_backup_tail
= current
;
164 void helper_macl(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
168 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
169 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
170 env
->mach
= (res
>> 32) & 0xffffffff;
171 env
->macl
= res
& 0xffffffff;
172 if (env
->sr
& (1u << SR_S
)) {
174 env
->mach
|= 0xffff0000;
176 env
->mach
&= 0x00007fff;
180 void helper_macw(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
184 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
185 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
186 env
->mach
= (res
>> 32) & 0xffffffff;
187 env
->macl
= res
& 0xffffffff;
188 if (env
->sr
& (1u << SR_S
)) {
189 if (res
< -0x80000000) {
191 env
->macl
= 0x80000000;
192 } else if (res
> 0x000000007fffffff) {
194 env
->macl
= 0x7fffffff;
199 void helper_ld_fpscr(CPUSH4State
*env
, uint32_t val
)
201 env
->fpscr
= val
& FPSCR_MASK
;
202 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
203 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
205 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
207 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
210 static void update_fpscr(CPUSH4State
*env
, uintptr_t retaddr
)
212 int xcpt
, cause
, enable
;
214 xcpt
= get_float_exception_flags(&env
->fp_status
);
216 /* Clear the flag entries */
217 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
219 if (unlikely(xcpt
)) {
220 if (xcpt
& float_flag_invalid
) {
221 env
->fpscr
|= FPSCR_FLAG_V
;
223 if (xcpt
& float_flag_divbyzero
) {
224 env
->fpscr
|= FPSCR_FLAG_Z
;
226 if (xcpt
& float_flag_overflow
) {
227 env
->fpscr
|= FPSCR_FLAG_O
;
229 if (xcpt
& float_flag_underflow
) {
230 env
->fpscr
|= FPSCR_FLAG_U
;
232 if (xcpt
& float_flag_inexact
) {
233 env
->fpscr
|= FPSCR_FLAG_I
;
236 /* Accumulate in cause entries */
237 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
238 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
240 /* Generate an exception if enabled */
241 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
242 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
243 if (cause
& enable
) {
244 raise_exception(env
, 0x120, retaddr
);
249 float32
helper_fabs_FT(float32 t0
)
251 return float32_abs(t0
);
254 float64
helper_fabs_DT(float64 t0
)
256 return float64_abs(t0
);
259 float32
helper_fadd_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
261 set_float_exception_flags(0, &env
->fp_status
);
262 t0
= float32_add(t0
, t1
, &env
->fp_status
);
263 update_fpscr(env
, GETPC());
267 float64
helper_fadd_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
269 set_float_exception_flags(0, &env
->fp_status
);
270 t0
= float64_add(t0
, t1
, &env
->fp_status
);
271 update_fpscr(env
, GETPC());
275 void helper_fcmp_eq_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
279 set_float_exception_flags(0, &env
->fp_status
);
280 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
281 if (unlikely(relation
== float_relation_unordered
)) {
282 update_fpscr(env
, GETPC());
284 env
->sr_t
= (relation
== float_relation_equal
);
288 void helper_fcmp_eq_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
292 set_float_exception_flags(0, &env
->fp_status
);
293 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
294 if (unlikely(relation
== float_relation_unordered
)) {
295 update_fpscr(env
, GETPC());
297 env
->sr_t
= (relation
== float_relation_equal
);
301 void helper_fcmp_gt_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
305 set_float_exception_flags(0, &env
->fp_status
);
306 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
307 if (unlikely(relation
== float_relation_unordered
)) {
308 update_fpscr(env
, GETPC());
310 env
->sr_t
= (relation
== float_relation_greater
);
314 void helper_fcmp_gt_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
318 set_float_exception_flags(0, &env
->fp_status
);
319 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
320 if (unlikely(relation
== float_relation_unordered
)) {
321 update_fpscr(env
, GETPC());
323 env
->sr_t
= (relation
== float_relation_greater
);
327 float64
helper_fcnvsd_FT_DT(CPUSH4State
*env
, float32 t0
)
330 set_float_exception_flags(0, &env
->fp_status
);
331 ret
= float32_to_float64(t0
, &env
->fp_status
);
332 update_fpscr(env
, GETPC());
336 float32
helper_fcnvds_DT_FT(CPUSH4State
*env
, float64 t0
)
339 set_float_exception_flags(0, &env
->fp_status
);
340 ret
= float64_to_float32(t0
, &env
->fp_status
);
341 update_fpscr(env
, GETPC());
345 float32
helper_fdiv_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
347 set_float_exception_flags(0, &env
->fp_status
);
348 t0
= float32_div(t0
, t1
, &env
->fp_status
);
349 update_fpscr(env
, GETPC());
353 float64
helper_fdiv_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
355 set_float_exception_flags(0, &env
->fp_status
);
356 t0
= float64_div(t0
, t1
, &env
->fp_status
);
357 update_fpscr(env
, GETPC());
361 float32
helper_float_FT(CPUSH4State
*env
, uint32_t t0
)
364 set_float_exception_flags(0, &env
->fp_status
);
365 ret
= int32_to_float32(t0
, &env
->fp_status
);
366 update_fpscr(env
, GETPC());
370 float64
helper_float_DT(CPUSH4State
*env
, uint32_t t0
)
373 set_float_exception_flags(0, &env
->fp_status
);
374 ret
= int32_to_float64(t0
, &env
->fp_status
);
375 update_fpscr(env
, GETPC());
379 float32
helper_fmac_FT(CPUSH4State
*env
, float32 t0
, float32 t1
, float32 t2
)
381 set_float_exception_flags(0, &env
->fp_status
);
382 t0
= float32_muladd(t0
, t1
, t2
, 0, &env
->fp_status
);
383 update_fpscr(env
, GETPC());
387 float32
helper_fmul_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
389 set_float_exception_flags(0, &env
->fp_status
);
390 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
391 update_fpscr(env
, GETPC());
395 float64
helper_fmul_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
397 set_float_exception_flags(0, &env
->fp_status
);
398 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
399 update_fpscr(env
, GETPC());
403 float32
helper_fneg_T(float32 t0
)
405 return float32_chs(t0
);
408 float32
helper_fsqrt_FT(CPUSH4State
*env
, float32 t0
)
410 set_float_exception_flags(0, &env
->fp_status
);
411 t0
= float32_sqrt(t0
, &env
->fp_status
);
412 update_fpscr(env
, GETPC());
416 float64
helper_fsqrt_DT(CPUSH4State
*env
, float64 t0
)
418 set_float_exception_flags(0, &env
->fp_status
);
419 t0
= float64_sqrt(t0
, &env
->fp_status
);
420 update_fpscr(env
, GETPC());
424 float32
helper_fsub_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
426 set_float_exception_flags(0, &env
->fp_status
);
427 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
428 update_fpscr(env
, GETPC());
432 float64
helper_fsub_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
434 set_float_exception_flags(0, &env
->fp_status
);
435 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
436 update_fpscr(env
, GETPC());
440 uint32_t helper_ftrc_FT(CPUSH4State
*env
, float32 t0
)
443 set_float_exception_flags(0, &env
->fp_status
);
444 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
445 update_fpscr(env
, GETPC());
449 uint32_t helper_ftrc_DT(CPUSH4State
*env
, float64 t0
)
452 set_float_exception_flags(0, &env
->fp_status
);
453 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
454 update_fpscr(env
, GETPC());
458 void helper_fipr(CPUSH4State
*env
, uint32_t m
, uint32_t n
)
463 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
465 set_float_exception_flags(0, &env
->fp_status
);
467 for (i
= 0 ; i
< 4 ; i
++) {
468 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
469 env
->fregs
[bank
+ n
+ i
],
471 r
= float32_add(r
, p
, &env
->fp_status
);
473 update_fpscr(env
, GETPC());
475 env
->fregs
[bank
+ n
+ 3] = r
;
478 void helper_ftrv(CPUSH4State
*env
, uint32_t n
)
480 int bank_matrix
, bank_vector
;
485 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
486 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
487 set_float_exception_flags(0, &env
->fp_status
);
488 for (i
= 0 ; i
< 4 ; i
++) {
490 for (j
= 0 ; j
< 4 ; j
++) {
491 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
492 env
->fregs
[bank_vector
+ j
],
494 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
497 update_fpscr(env
, GETPC());
499 for (i
= 0 ; i
< 4 ; i
++) {
500 env
->fregs
[bank_vector
+ i
] = r
[i
];