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
, int is_write
, int mmu_idx
,
32 ret
= superh_cpu_handle_mmu_fault(cs
, addr
, is_write
, 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
= malloc (sizeof(memory_content
));
113 r
->address
= address
;
117 *(env
->movcal_backup_tail
) = r
;
118 env
->movcal_backup_tail
= &(r
->next
);
122 void helper_discard_movcal_backup(CPUSH4State
*env
)
124 memory_content
*current
= env
->movcal_backup
;
128 memory_content
*next
= current
->next
;
130 env
->movcal_backup
= current
= next
;
132 env
->movcal_backup_tail
= &(env
->movcal_backup
);
136 void helper_ocbi(CPUSH4State
*env
, uint32_t address
)
138 memory_content
**current
= &(env
->movcal_backup
);
141 uint32_t a
= (*current
)->address
;
142 if ((a
& ~0x1F) == (address
& ~0x1F))
144 memory_content
*next
= (*current
)->next
;
145 cpu_stl_data(env
, a
, (*current
)->value
);
149 env
->movcal_backup_tail
= current
;
159 void helper_macl(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
163 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
164 res
+= (int64_t) (int32_t) arg0
*(int64_t) (int32_t) arg1
;
165 env
->mach
= (res
>> 32) & 0xffffffff;
166 env
->macl
= res
& 0xffffffff;
167 if (env
->sr
& (1u << SR_S
)) {
169 env
->mach
|= 0xffff0000;
171 env
->mach
&= 0x00007fff;
175 void helper_macw(CPUSH4State
*env
, uint32_t arg0
, uint32_t arg1
)
179 res
= ((uint64_t) env
->mach
<< 32) | env
->macl
;
180 res
+= (int64_t) (int16_t) arg0
*(int64_t) (int16_t) arg1
;
181 env
->mach
= (res
>> 32) & 0xffffffff;
182 env
->macl
= res
& 0xffffffff;
183 if (env
->sr
& (1u << SR_S
)) {
184 if (res
< -0x80000000) {
186 env
->macl
= 0x80000000;
187 } else if (res
> 0x000000007fffffff) {
189 env
->macl
= 0x7fffffff;
194 void helper_ld_fpscr(CPUSH4State
*env
, uint32_t val
)
196 env
->fpscr
= val
& FPSCR_MASK
;
197 if ((val
& FPSCR_RM_MASK
) == FPSCR_RM_ZERO
) {
198 set_float_rounding_mode(float_round_to_zero
, &env
->fp_status
);
200 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
202 set_flush_to_zero((val
& FPSCR_DN
) != 0, &env
->fp_status
);
205 static void update_fpscr(CPUSH4State
*env
, uintptr_t retaddr
)
207 int xcpt
, cause
, enable
;
209 xcpt
= get_float_exception_flags(&env
->fp_status
);
211 /* Clear the flag entries */
212 env
->fpscr
&= ~FPSCR_FLAG_MASK
;
214 if (unlikely(xcpt
)) {
215 if (xcpt
& float_flag_invalid
) {
216 env
->fpscr
|= FPSCR_FLAG_V
;
218 if (xcpt
& float_flag_divbyzero
) {
219 env
->fpscr
|= FPSCR_FLAG_Z
;
221 if (xcpt
& float_flag_overflow
) {
222 env
->fpscr
|= FPSCR_FLAG_O
;
224 if (xcpt
& float_flag_underflow
) {
225 env
->fpscr
|= FPSCR_FLAG_U
;
227 if (xcpt
& float_flag_inexact
) {
228 env
->fpscr
|= FPSCR_FLAG_I
;
231 /* Accumulate in cause entries */
232 env
->fpscr
|= (env
->fpscr
& FPSCR_FLAG_MASK
)
233 << (FPSCR_CAUSE_SHIFT
- FPSCR_FLAG_SHIFT
);
235 /* Generate an exception if enabled */
236 cause
= (env
->fpscr
& FPSCR_CAUSE_MASK
) >> FPSCR_CAUSE_SHIFT
;
237 enable
= (env
->fpscr
& FPSCR_ENABLE_MASK
) >> FPSCR_ENABLE_SHIFT
;
238 if (cause
& enable
) {
239 raise_exception(env
, 0x120, retaddr
);
244 float32
helper_fabs_FT(float32 t0
)
246 return float32_abs(t0
);
249 float64
helper_fabs_DT(float64 t0
)
251 return float64_abs(t0
);
254 float32
helper_fadd_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
256 set_float_exception_flags(0, &env
->fp_status
);
257 t0
= float32_add(t0
, t1
, &env
->fp_status
);
258 update_fpscr(env
, GETPC());
262 float64
helper_fadd_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
264 set_float_exception_flags(0, &env
->fp_status
);
265 t0
= float64_add(t0
, t1
, &env
->fp_status
);
266 update_fpscr(env
, GETPC());
270 void helper_fcmp_eq_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
274 set_float_exception_flags(0, &env
->fp_status
);
275 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
276 if (unlikely(relation
== float_relation_unordered
)) {
277 update_fpscr(env
, GETPC());
279 env
->sr_t
= (relation
== float_relation_equal
);
283 void helper_fcmp_eq_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
287 set_float_exception_flags(0, &env
->fp_status
);
288 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
289 if (unlikely(relation
== float_relation_unordered
)) {
290 update_fpscr(env
, GETPC());
292 env
->sr_t
= (relation
== float_relation_equal
);
296 void helper_fcmp_gt_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
300 set_float_exception_flags(0, &env
->fp_status
);
301 relation
= float32_compare(t0
, t1
, &env
->fp_status
);
302 if (unlikely(relation
== float_relation_unordered
)) {
303 update_fpscr(env
, GETPC());
305 env
->sr_t
= (relation
== float_relation_greater
);
309 void helper_fcmp_gt_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
313 set_float_exception_flags(0, &env
->fp_status
);
314 relation
= float64_compare(t0
, t1
, &env
->fp_status
);
315 if (unlikely(relation
== float_relation_unordered
)) {
316 update_fpscr(env
, GETPC());
318 env
->sr_t
= (relation
== float_relation_greater
);
322 float64
helper_fcnvsd_FT_DT(CPUSH4State
*env
, float32 t0
)
325 set_float_exception_flags(0, &env
->fp_status
);
326 ret
= float32_to_float64(t0
, &env
->fp_status
);
327 update_fpscr(env
, GETPC());
331 float32
helper_fcnvds_DT_FT(CPUSH4State
*env
, float64 t0
)
334 set_float_exception_flags(0, &env
->fp_status
);
335 ret
= float64_to_float32(t0
, &env
->fp_status
);
336 update_fpscr(env
, GETPC());
340 float32
helper_fdiv_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
342 set_float_exception_flags(0, &env
->fp_status
);
343 t0
= float32_div(t0
, t1
, &env
->fp_status
);
344 update_fpscr(env
, GETPC());
348 float64
helper_fdiv_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
350 set_float_exception_flags(0, &env
->fp_status
);
351 t0
= float64_div(t0
, t1
, &env
->fp_status
);
352 update_fpscr(env
, GETPC());
356 float32
helper_float_FT(CPUSH4State
*env
, uint32_t t0
)
359 set_float_exception_flags(0, &env
->fp_status
);
360 ret
= int32_to_float32(t0
, &env
->fp_status
);
361 update_fpscr(env
, GETPC());
365 float64
helper_float_DT(CPUSH4State
*env
, uint32_t t0
)
368 set_float_exception_flags(0, &env
->fp_status
);
369 ret
= int32_to_float64(t0
, &env
->fp_status
);
370 update_fpscr(env
, GETPC());
374 float32
helper_fmac_FT(CPUSH4State
*env
, float32 t0
, float32 t1
, float32 t2
)
376 set_float_exception_flags(0, &env
->fp_status
);
377 t0
= float32_muladd(t0
, t1
, t2
, 0, &env
->fp_status
);
378 update_fpscr(env
, GETPC());
382 float32
helper_fmul_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
384 set_float_exception_flags(0, &env
->fp_status
);
385 t0
= float32_mul(t0
, t1
, &env
->fp_status
);
386 update_fpscr(env
, GETPC());
390 float64
helper_fmul_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
392 set_float_exception_flags(0, &env
->fp_status
);
393 t0
= float64_mul(t0
, t1
, &env
->fp_status
);
394 update_fpscr(env
, GETPC());
398 float32
helper_fneg_T(float32 t0
)
400 return float32_chs(t0
);
403 float32
helper_fsqrt_FT(CPUSH4State
*env
, float32 t0
)
405 set_float_exception_flags(0, &env
->fp_status
);
406 t0
= float32_sqrt(t0
, &env
->fp_status
);
407 update_fpscr(env
, GETPC());
411 float64
helper_fsqrt_DT(CPUSH4State
*env
, float64 t0
)
413 set_float_exception_flags(0, &env
->fp_status
);
414 t0
= float64_sqrt(t0
, &env
->fp_status
);
415 update_fpscr(env
, GETPC());
419 float32
helper_fsub_FT(CPUSH4State
*env
, float32 t0
, float32 t1
)
421 set_float_exception_flags(0, &env
->fp_status
);
422 t0
= float32_sub(t0
, t1
, &env
->fp_status
);
423 update_fpscr(env
, GETPC());
427 float64
helper_fsub_DT(CPUSH4State
*env
, float64 t0
, float64 t1
)
429 set_float_exception_flags(0, &env
->fp_status
);
430 t0
= float64_sub(t0
, t1
, &env
->fp_status
);
431 update_fpscr(env
, GETPC());
435 uint32_t helper_ftrc_FT(CPUSH4State
*env
, float32 t0
)
438 set_float_exception_flags(0, &env
->fp_status
);
439 ret
= float32_to_int32_round_to_zero(t0
, &env
->fp_status
);
440 update_fpscr(env
, GETPC());
444 uint32_t helper_ftrc_DT(CPUSH4State
*env
, float64 t0
)
447 set_float_exception_flags(0, &env
->fp_status
);
448 ret
= float64_to_int32_round_to_zero(t0
, &env
->fp_status
);
449 update_fpscr(env
, GETPC());
453 void helper_fipr(CPUSH4State
*env
, uint32_t m
, uint32_t n
)
458 bank
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
460 set_float_exception_flags(0, &env
->fp_status
);
462 for (i
= 0 ; i
< 4 ; i
++) {
463 p
= float32_mul(env
->fregs
[bank
+ m
+ i
],
464 env
->fregs
[bank
+ n
+ i
],
466 r
= float32_add(r
, p
, &env
->fp_status
);
468 update_fpscr(env
, GETPC());
470 env
->fregs
[bank
+ n
+ 3] = r
;
473 void helper_ftrv(CPUSH4State
*env
, uint32_t n
)
475 int bank_matrix
, bank_vector
;
480 bank_matrix
= (env
->sr
& FPSCR_FR
) ? 0 : 16;
481 bank_vector
= (env
->sr
& FPSCR_FR
) ? 16 : 0;
482 set_float_exception_flags(0, &env
->fp_status
);
483 for (i
= 0 ; i
< 4 ; i
++) {
485 for (j
= 0 ; j
< 4 ; j
++) {
486 p
= float32_mul(env
->fregs
[bank_matrix
+ 4 * j
+ i
],
487 env
->fregs
[bank_vector
+ j
],
489 r
[i
] = float32_add(r
[i
], p
, &env
->fp_status
);
492 update_fpscr(env
, GETPC());
494 for (i
= 0 ; i
< 4 ; i
++) {
495 env
->fregs
[bank_vector
+ i
] = r
[i
];