2 * Copyright (C) 2010-2012 Guan Xuetao
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Contributions from 2012-04-01 on are considered under GPL version 2,
9 * or (at your option) any later version.
15 #include "host-utils.h"
17 CPUUniCore32State
*uc32_cpu_init(const char *cpu_model
)
20 CPUUniCore32State
*env
;
21 static int inited
= 1;
23 if (object_class_by_name(cpu_model
) == NULL
) {
26 cpu
= UNICORE32_CPU(object_new(cpu_model
));
31 uc32_translate_init();
38 uint32_t HELPER(clo
)(uint32_t x
)
43 uint32_t HELPER(clz
)(uint32_t x
)
48 #ifdef CONFIG_USER_ONLY
49 void switch_mode(CPUUniCore32State
*env
, int mode
)
51 if (mode
!= ASR_MODE_USER
) {
52 cpu_abort(env
, "Tried to switch out of user mode\n");
56 void do_interrupt(CPUUniCore32State
*env
)
58 cpu_abort(env
, "NO interrupt in user mode\n");
61 int uc32_cpu_handle_mmu_fault(CPUUniCore32State
*env
, target_ulong address
,
62 int access_type
, int mmu_idx
)
64 cpu_abort(env
, "NO mmu fault in user mode\n");
69 /* These should probably raise undefined insn exceptions. */
70 void HELPER(set_cp
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
72 int op1
= (insn
>> 8) & 0xf;
73 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
77 uint32_t HELPER(get_cp
)(CPUUniCore32State
*env
, uint32_t insn
)
79 int op1
= (insn
>> 8) & 0xf;
80 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
84 void HELPER(set_cp0
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
86 cpu_abort(env
, "cp0 insn %08x\n", insn
);
89 uint32_t HELPER(get_cp0
)(CPUUniCore32State
*env
, uint32_t insn
)
91 cpu_abort(env
, "cp0 insn %08x\n", insn
);
95 void HELPER(set_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
, uint32_t val
)
97 cpu_abort(env
, "banked r29 write\n");
100 uint32_t HELPER(get_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
)
102 cpu_abort(env
, "banked r29 read\n");
106 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
107 Single precition routines have a "s" suffix, double precision a
110 /* Convert host exception flags to f64 form. */
111 static inline int ucf64_exceptbits_from_host(int host_bits
)
115 if (host_bits
& float_flag_invalid
) {
116 target_bits
|= UCF64_FPSCR_FLAG_INVALID
;
118 if (host_bits
& float_flag_divbyzero
) {
119 target_bits
|= UCF64_FPSCR_FLAG_DIVZERO
;
121 if (host_bits
& float_flag_overflow
) {
122 target_bits
|= UCF64_FPSCR_FLAG_OVERFLOW
;
124 if (host_bits
& float_flag_underflow
) {
125 target_bits
|= UCF64_FPSCR_FLAG_UNDERFLOW
;
127 if (host_bits
& float_flag_inexact
) {
128 target_bits
|= UCF64_FPSCR_FLAG_INEXACT
;
133 uint32_t HELPER(ucf64_get_fpscr
)(CPUUniCore32State
*env
)
138 fpscr
= (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & UCF64_FPSCR_MASK
);
139 i
= get_float_exception_flags(&env
->ucf64
.fp_status
);
140 fpscr
|= ucf64_exceptbits_from_host(i
);
144 /* Convert ucf64 exception flags to target form. */
145 static inline int ucf64_exceptbits_to_host(int target_bits
)
149 if (target_bits
& UCF64_FPSCR_FLAG_INVALID
) {
150 host_bits
|= float_flag_invalid
;
152 if (target_bits
& UCF64_FPSCR_FLAG_DIVZERO
) {
153 host_bits
|= float_flag_divbyzero
;
155 if (target_bits
& UCF64_FPSCR_FLAG_OVERFLOW
) {
156 host_bits
|= float_flag_overflow
;
158 if (target_bits
& UCF64_FPSCR_FLAG_UNDERFLOW
) {
159 host_bits
|= float_flag_underflow
;
161 if (target_bits
& UCF64_FPSCR_FLAG_INEXACT
) {
162 host_bits
|= float_flag_inexact
;
167 void HELPER(ucf64_set_fpscr
)(CPUUniCore32State
*env
, uint32_t val
)
172 changed
= env
->ucf64
.xregs
[UC32_UCF64_FPSCR
];
173 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (val
& UCF64_FPSCR_MASK
);
176 if (changed
& (UCF64_FPSCR_RND_MASK
)) {
177 i
= UCF64_FPSCR_RND(val
);
180 i
= float_round_nearest_even
;
183 i
= float_round_to_zero
;
189 i
= float_round_down
;
191 default: /* 100 and 101 not implement */
192 cpu_abort(env
, "Unsupported UniCore-F64 round mode");
194 set_float_rounding_mode(i
, &env
->ucf64
.fp_status
);
197 i
= ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val
));
198 set_float_exception_flags(i
, &env
->ucf64
.fp_status
);
201 float32
HELPER(ucf64_adds
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
203 return float32_add(a
, b
, &env
->ucf64
.fp_status
);
206 float64
HELPER(ucf64_addd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
208 return float64_add(a
, b
, &env
->ucf64
.fp_status
);
211 float32
HELPER(ucf64_subs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
213 return float32_sub(a
, b
, &env
->ucf64
.fp_status
);
216 float64
HELPER(ucf64_subd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
218 return float64_sub(a
, b
, &env
->ucf64
.fp_status
);
221 float32
HELPER(ucf64_muls
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
223 return float32_mul(a
, b
, &env
->ucf64
.fp_status
);
226 float64
HELPER(ucf64_muld
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
228 return float64_mul(a
, b
, &env
->ucf64
.fp_status
);
231 float32
HELPER(ucf64_divs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
233 return float32_div(a
, b
, &env
->ucf64
.fp_status
);
236 float64
HELPER(ucf64_divd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
238 return float64_div(a
, b
, &env
->ucf64
.fp_status
);
241 float32
HELPER(ucf64_negs
)(float32 a
)
243 return float32_chs(a
);
246 float64
HELPER(ucf64_negd
)(float64 a
)
248 return float64_chs(a
);
251 float32
HELPER(ucf64_abss
)(float32 a
)
253 return float32_abs(a
);
256 float64
HELPER(ucf64_absd
)(float64 a
)
258 return float64_abs(a
);
261 /* XXX: check quiet/signaling case */
262 void HELPER(ucf64_cmps
)(float32 a
, float32 b
, uint32_t c
, CPUUniCore32State
*env
)
265 flag
= float32_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
281 if ((flag
== 0) || (flag
== 2)) {
291 if ((flag
== -1) || (flag
== 2)) {
296 if ((flag
== -1) || (flag
== 0)) {
306 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
307 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
310 void HELPER(ucf64_cmpd
)(float64 a
, float64 b
, uint32_t c
, CPUUniCore32State
*env
)
313 flag
= float64_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
329 if ((flag
== 0) || (flag
== 2)) {
339 if ((flag
== -1) || (flag
== 2)) {
344 if ((flag
== -1) || (flag
== 0)) {
354 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
355 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
358 /* Helper routines to perform bitwise copies between float and int. */
359 static inline float32
ucf64_itos(uint32_t i
)
370 static inline uint32_t ucf64_stoi(float32 s
)
381 static inline float64
ucf64_itod(uint64_t i
)
392 static inline uint64_t ucf64_dtoi(float64 d
)
403 /* Integer to float conversion. */
404 float32
HELPER(ucf64_si2sf
)(float32 x
, CPUUniCore32State
*env
)
406 return int32_to_float32(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
409 float64
HELPER(ucf64_si2df
)(float32 x
, CPUUniCore32State
*env
)
411 return int32_to_float64(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
414 /* Float to integer conversion. */
415 float32
HELPER(ucf64_sf2si
)(float32 x
, CPUUniCore32State
*env
)
417 return ucf64_itos(float32_to_int32(x
, &env
->ucf64
.fp_status
));
420 float32
HELPER(ucf64_df2si
)(float64 x
, CPUUniCore32State
*env
)
422 return ucf64_itos(float64_to_int32(x
, &env
->ucf64
.fp_status
));
425 /* floating point conversion */
426 float64
HELPER(ucf64_sf2df
)(float32 x
, CPUUniCore32State
*env
)
428 return float32_to_float64(x
, &env
->ucf64
.fp_status
);
431 float32
HELPER(ucf64_df2sf
)(float64 x
, CPUUniCore32State
*env
)
433 return float64_to_float32(x
, &env
->ucf64
.fp_status
);