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 void helper_ldtlb(CPUSH4State
*env
)
46 #ifdef CONFIG_USER_ONLY
47 SuperHCPU
*cpu
= sh_env_get_cpu(env
);
50 cpu_abort(CPU(cpu
), "Unhandled ldtlb");
56 static inline void QEMU_NORETURN
raise_exception(CPUSH4State
*env
, int index
,
59 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
61 cs
->exception_index
= index
;
63 cpu_restore_state(cs
, retaddr
);
68 void helper_raise_illegal_instruction(CPUSH4State
*env
)
70 raise_exception(env
, 0x180, 0);
73 void helper_raise_slot_illegal_instruction(CPUSH4State
*env
)
75 raise_exception(env
, 0x1a0, 0);
78 void helper_raise_fpu_disable(CPUSH4State
*env
)
80 raise_exception(env
, 0x800, 0);
83 void helper_raise_slot_fpu_disable(CPUSH4State
*env
)
85 raise_exception(env
, 0x820, 0);
88 void helper_debug(CPUSH4State
*env
)
90 raise_exception(env
, EXCP_DEBUG
, 0);
93 void helper_sleep(CPUSH4State
*env
)
95 CPUState
*cs
= CPU(sh_env_get_cpu(env
));
99 raise_exception(env
, EXCP_HLT
, 0);
102 void helper_trapa(CPUSH4State
*env
, uint32_t tra
)
105 raise_exception(env
, 0x160, 0);
108 void helper_movcal(CPUSH4State
*env
, uint32_t address
, uint32_t value
)
110 if (cpu_sh4_is_cached (env
, address
))
112 memory_content
*r
= g_new(memory_content
, 1);
114 r
->address
= address
;
118 *(env
->movcal_backup_tail
) = r
;
119 env
->movcal_backup_tail
= &(r
->next
);
123 void helper_discard_movcal_backup(CPUSH4State
*env
)
125 memory_content
*current
= env
->movcal_backup
;
129 memory_content
*next
= current
->next
;
131 env
->movcal_backup
= current
= next
;
133 env
->movcal_backup_tail
= &(env
->movcal_backup
);
137 void helper_ocbi(CPUSH4State
*env
, uint32_t address
)
139 memory_content
**current
= &(env
->movcal_backup
);
142 uint32_t a
= (*current
)->address
;
143 if ((a
& ~0x1F) == (address
& ~0x1F))
145 memory_content
*next
= (*current
)->next
;
146 cpu_stl_data(env
, a
, (*current
)->value
);
150 env
->movcal_backup_tail
= current
;
160 void helper_macl(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
164 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
165 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
166 env
->mach
= (res
>> 32) & 0xffffffff;
167 env
->macl
= res
& 0xffffffff;
168 if (env
->sr
& (1u << SR_S
)) {
170 env
->mach
|= 0xffff0000;
172 env
->mach
&= 0x00007fff;
176 void helper_macw(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
180 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
181 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
182 env
->mach
= (res
>> 32) & 0xffffffff;
183 env
->macl
= res
& 0xffffffff;
184 if (env
->sr
& (1u << SR_S
)) {
185 if (res
< -0x80000000) {
187 env
->macl
= 0x80000000;
188 } else if (res
> 0x000000007fffffff) {
190 env
->macl
= 0x7fffffff;
195 void helper_ld_fpscr(CPUSH4State
*env
, uint32_t val
)
197 env
->fpscr
= val
& FPSCR_MASK
;
198 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
199 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
201 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
203 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
206 static void update_fpscr(CPUSH4State
*env
, uintptr_t retaddr
)
208 int xcpt
, cause
, enable
;
210 xcpt
= get_float_exception_flags(&env
->fp_status
);
212 /* Clear the flag entries */
213 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
215 if (unlikely(xcpt
)) {
216 if (xcpt
& float_flag_invalid
) {
217 env
->fpscr
|= FPSCR_FLAG_V
;
219 if (xcpt
& float_flag_divbyzero
) {
220 env
->fpscr
|= FPSCR_FLAG_Z
;
222 if (xcpt
& float_flag_overflow
) {
223 env
->fpscr
|= FPSCR_FLAG_O
;
225 if (xcpt
& float_flag_underflow
) {
226 env
->fpscr
|= FPSCR_FLAG_U
;
228 if (xcpt
& float_flag_inexact
) {
229 env
->fpscr
|= FPSCR_FLAG_I
;
232 /* Accumulate in cause entries */
233 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
234 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
236 /* Generate an exception if enabled */
237 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
238 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
239 if (cause
& enable
) {
240 raise_exception(env
, 0x120, retaddr
);
245 float32
helper_fabs_FT(float32 t0
)
247 return float32_abs(t0
);
250 float64
helper_fabs_DT(float64 t0
)
252 return float64_abs(t0
);
255 float32
helper_fadd_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
257 set_float_exception_flags(0, &env
->fp_status
);
258 t0
= float32_add(t0
, t1
, &env
->fp_status
);
259 update_fpscr(env
, GETPC());
263 float64
helper_fadd_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
265 set_float_exception_flags(0, &env
->fp_status
);
266 t0
= float64_add(t0
, t1
, &env
->fp_status
);
267 update_fpscr(env
, GETPC());
271 void helper_fcmp_eq_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
275 set_float_exception_flags(0, &env
->fp_status
);
276 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
277 if (unlikely(relation
== float_relation_unordered
)) {
278 update_fpscr(env
, GETPC());
280 env
->sr_t
= (relation
== float_relation_equal
);
284 void helper_fcmp_eq_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
288 set_float_exception_flags(0, &env
->fp_status
);
289 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
290 if (unlikely(relation
== float_relation_unordered
)) {
291 update_fpscr(env
, GETPC());
293 env
->sr_t
= (relation
== float_relation_equal
);
297 void helper_fcmp_gt_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
301 set_float_exception_flags(0, &env
->fp_status
);
302 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
303 if (unlikely(relation
== float_relation_unordered
)) {
304 update_fpscr(env
, GETPC());
306 env
->sr_t
= (relation
== float_relation_greater
);
310 void helper_fcmp_gt_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
314 set_float_exception_flags(0, &env
->fp_status
);
315 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
316 if (unlikely(relation
== float_relation_unordered
)) {
317 update_fpscr(env
, GETPC());
319 env
->sr_t
= (relation
== float_relation_greater
);
323 float64
helper_fcnvsd_FT_DT(CPUSH4State
*env
, float32 t0
)
326 set_float_exception_flags(0, &env
->fp_status
);
327 ret
= float32_to_float64(t0
, &env
->fp_status
);
328 update_fpscr(env
, GETPC());
332 float32
helper_fcnvds_DT_FT(CPUSH4State
*env
, float64 t0
)
335 set_float_exception_flags(0, &env
->fp_status
);
336 ret
= float64_to_float32(t0
, &env
->fp_status
);
337 update_fpscr(env
, GETPC());
341 float32
helper_fdiv_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
343 set_float_exception_flags(0, &env
->fp_status
);
344 t0
= float32_div(t0
, t1
, &env
->fp_status
);
345 update_fpscr(env
, GETPC());
349 float64
helper_fdiv_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
351 set_float_exception_flags(0, &env
->fp_status
);
352 t0
= float64_div(t0
, t1
, &env
->fp_status
);
353 update_fpscr(env
, GETPC());
357 float32
helper_float_FT(CPUSH4State
*env
, uint32_t t0
)
360 set_float_exception_flags(0, &env
->fp_status
);
361 ret
= int32_to_float32(t0
, &env
->fp_status
);
362 update_fpscr(env
, GETPC());
366 float64
helper_float_DT(CPUSH4State
*env
, uint32_t t0
)
369 set_float_exception_flags(0, &env
->fp_status
);
370 ret
= int32_to_float64(t0
, &env
->fp_status
);
371 update_fpscr(env
, GETPC());
375 float32
helper_fmac_FT(CPUSH4State
*env
, float32 t0
, float32 t1
, float32 t2
)
377 set_float_exception_flags(0, &env
->fp_status
);
378 t0
= float32_muladd(t0
, t1
, t2
, 0, &env
->fp_status
);
379 update_fpscr(env
, GETPC());
383 float32
helper_fmul_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
385 set_float_exception_flags(0, &env
->fp_status
);
386 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
387 update_fpscr(env
, GETPC());
391 float64
helper_fmul_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
393 set_float_exception_flags(0, &env
->fp_status
);
394 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
395 update_fpscr(env
, GETPC());
399 float32
helper_fneg_T(float32 t0
)
401 return float32_chs(t0
);
404 float32
helper_fsqrt_FT(CPUSH4State
*env
, float32 t0
)
406 set_float_exception_flags(0, &env
->fp_status
);
407 t0
= float32_sqrt(t0
, &env
->fp_status
);
408 update_fpscr(env
, GETPC());
412 float64
helper_fsqrt_DT(CPUSH4State
*env
, float64 t0
)
414 set_float_exception_flags(0, &env
->fp_status
);
415 t0
= float64_sqrt(t0
, &env
->fp_status
);
416 update_fpscr(env
, GETPC());
420 float32
helper_fsub_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
422 set_float_exception_flags(0, &env
->fp_status
);
423 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
424 update_fpscr(env
, GETPC());
428 float64
helper_fsub_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
430 set_float_exception_flags(0, &env
->fp_status
);
431 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
432 update_fpscr(env
, GETPC());
436 uint32_t helper_ftrc_FT(CPUSH4State
*env
, float32 t0
)
439 set_float_exception_flags(0, &env
->fp_status
);
440 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
441 update_fpscr(env
, GETPC());
445 uint32_t helper_ftrc_DT(CPUSH4State
*env
, float64 t0
)
448 set_float_exception_flags(0, &env
->fp_status
);
449 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
450 update_fpscr(env
, GETPC());
454 void helper_fipr(CPUSH4State
*env
, uint32_t m
, uint32_t n
)
459 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
461 set_float_exception_flags(0, &env
->fp_status
);
463 for (i
= 0 ; i
< 4 ; i
++) {
464 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
465 env
->fregs
[bank
+ n
+ i
],
467 r
= float32_add(r
, p
, &env
->fp_status
);
469 update_fpscr(env
, GETPC());
471 env
->fregs
[bank
+ n
+ 3] = r
;
474 void helper_ftrv(CPUSH4State
*env
, uint32_t n
)
476 int bank_matrix
, bank_vector
;
481 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
482 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
483 set_float_exception_flags(0, &env
->fp_status
);
484 for (i
= 0 ; i
< 4 ; i
++) {
486 for (j
= 0 ; j
< 4 ; j
++) {
487 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
488 env
->fregs
[bank_vector
+ j
],
490 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
493 update_fpscr(env
, GETPC());
495 for (i
= 0 ; i
< 4 ; i
++) {
496 env
->fregs
[bank_vector
+ i
] = r
[i
];