2 * Copyright (C) 2010-2011 GUAN Xue-tao
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.
12 #include "host-utils.h"
14 static inline void set_feature(CPUUniCore32State
*env
, int feature
)
16 env
->features
|= feature
;
24 static const struct uc32_cpu_t uc32_cpu_names
[] = {
25 { UC32_CPUID_UCV2
, "UniCore-II"},
26 { UC32_CPUID_ANY
, "any"},
30 /* return 0 if not found */
31 static uint32_t uc32_cpu_find_by_name(const char *name
)
37 for (i
= 0; uc32_cpu_names
[i
].name
; i
++) {
38 if (strcmp(name
, uc32_cpu_names
[i
].name
) == 0) {
39 id
= uc32_cpu_names
[i
].id
;
46 CPUUniCore32State
*uc32_cpu_init(const char *cpu_model
)
48 CPUUniCore32State
*env
;
50 static int inited
= 1;
52 env
= g_malloc0(sizeof(CPUUniCore32State
));
55 id
= uc32_cpu_find_by_name(cpu_model
);
58 set_feature(env
, UC32_HWCAP_CMOV
);
59 set_feature(env
, UC32_HWCAP_UCF64
);
60 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = 0;
61 env
->cp0
.c0_cachetype
= 0x1dd20d2;
62 env
->cp0
.c1_sys
= 0x00090078;
64 case UC32_CPUID_ANY
: /* For userspace emulation. */
65 set_feature(env
, UC32_HWCAP_CMOV
);
66 set_feature(env
, UC32_HWCAP_UCF64
);
69 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
72 env
->cpu_model_str
= cpu_model
;
73 env
->cp0
.c0_cpuid
= id
;
74 env
->uncached_asr
= ASR_MODE_USER
;
79 uc32_translate_init();
87 uint32_t HELPER(clo
)(uint32_t x
)
92 uint32_t HELPER(clz
)(uint32_t x
)
97 void do_interrupt(CPUUniCore32State
*env
)
99 env
->exception_index
= -1;
102 int uc32_cpu_handle_mmu_fault(CPUUniCore32State
*env
, target_ulong address
, int rw
,
105 env
->exception_index
= UC32_EXCP_TRAP
;
106 env
->cp0
.c4_faultaddr
= address
;
110 /* These should probably raise undefined insn exceptions. */
111 void HELPER(set_cp
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
113 int op1
= (insn
>> 8) & 0xf;
114 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
118 uint32_t HELPER(get_cp
)(CPUUniCore32State
*env
, uint32_t insn
)
120 int op1
= (insn
>> 8) & 0xf;
121 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
125 void HELPER(set_cp0
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
127 cpu_abort(env
, "cp0 insn %08x\n", insn
);
130 uint32_t HELPER(get_cp0
)(CPUUniCore32State
*env
, uint32_t insn
)
132 cpu_abort(env
, "cp0 insn %08x\n", insn
);
136 void switch_mode(CPUUniCore32State
*env
, int mode
)
138 if (mode
!= ASR_MODE_USER
) {
139 cpu_abort(env
, "Tried to switch out of user mode\n");
143 void HELPER(set_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
, uint32_t val
)
145 cpu_abort(env
, "banked r29 write\n");
148 uint32_t HELPER(get_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
)
150 cpu_abort(env
, "banked r29 read\n");
154 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
155 Single precition routines have a "s" suffix, double precision a
158 /* Convert host exception flags to f64 form. */
159 static inline int ucf64_exceptbits_from_host(int host_bits
)
163 if (host_bits
& float_flag_invalid
) {
164 target_bits
|= UCF64_FPSCR_FLAG_INVALID
;
166 if (host_bits
& float_flag_divbyzero
) {
167 target_bits
|= UCF64_FPSCR_FLAG_DIVZERO
;
169 if (host_bits
& float_flag_overflow
) {
170 target_bits
|= UCF64_FPSCR_FLAG_OVERFLOW
;
172 if (host_bits
& float_flag_underflow
) {
173 target_bits
|= UCF64_FPSCR_FLAG_UNDERFLOW
;
175 if (host_bits
& float_flag_inexact
) {
176 target_bits
|= UCF64_FPSCR_FLAG_INEXACT
;
181 uint32_t HELPER(ucf64_get_fpscr
)(CPUUniCore32State
*env
)
186 fpscr
= (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & UCF64_FPSCR_MASK
);
187 i
= get_float_exception_flags(&env
->ucf64
.fp_status
);
188 fpscr
|= ucf64_exceptbits_from_host(i
);
192 /* Convert ucf64 exception flags to target form. */
193 static inline int ucf64_exceptbits_to_host(int target_bits
)
197 if (target_bits
& UCF64_FPSCR_FLAG_INVALID
) {
198 host_bits
|= float_flag_invalid
;
200 if (target_bits
& UCF64_FPSCR_FLAG_DIVZERO
) {
201 host_bits
|= float_flag_divbyzero
;
203 if (target_bits
& UCF64_FPSCR_FLAG_OVERFLOW
) {
204 host_bits
|= float_flag_overflow
;
206 if (target_bits
& UCF64_FPSCR_FLAG_UNDERFLOW
) {
207 host_bits
|= float_flag_underflow
;
209 if (target_bits
& UCF64_FPSCR_FLAG_INEXACT
) {
210 host_bits
|= float_flag_inexact
;
215 void HELPER(ucf64_set_fpscr
)(CPUUniCore32State
*env
, uint32_t val
)
220 changed
= env
->ucf64
.xregs
[UC32_UCF64_FPSCR
];
221 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (val
& UCF64_FPSCR_MASK
);
224 if (changed
& (UCF64_FPSCR_RND_MASK
)) {
225 i
= UCF64_FPSCR_RND(val
);
228 i
= float_round_nearest_even
;
231 i
= float_round_to_zero
;
237 i
= float_round_down
;
239 default: /* 100 and 101 not implement */
240 cpu_abort(env
, "Unsupported UniCore-F64 round mode");
242 set_float_rounding_mode(i
, &env
->ucf64
.fp_status
);
245 i
= ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val
));
246 set_float_exception_flags(i
, &env
->ucf64
.fp_status
);
249 float32
HELPER(ucf64_adds
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
251 return float32_add(a
, b
, &env
->ucf64
.fp_status
);
254 float64
HELPER(ucf64_addd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
256 return float64_add(a
, b
, &env
->ucf64
.fp_status
);
259 float32
HELPER(ucf64_subs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
261 return float32_sub(a
, b
, &env
->ucf64
.fp_status
);
264 float64
HELPER(ucf64_subd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
266 return float64_sub(a
, b
, &env
->ucf64
.fp_status
);
269 float32
HELPER(ucf64_muls
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
271 return float32_mul(a
, b
, &env
->ucf64
.fp_status
);
274 float64
HELPER(ucf64_muld
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
276 return float64_mul(a
, b
, &env
->ucf64
.fp_status
);
279 float32
HELPER(ucf64_divs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
281 return float32_div(a
, b
, &env
->ucf64
.fp_status
);
284 float64
HELPER(ucf64_divd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
286 return float64_div(a
, b
, &env
->ucf64
.fp_status
);
289 float32
HELPER(ucf64_negs
)(float32 a
)
291 return float32_chs(a
);
294 float64
HELPER(ucf64_negd
)(float64 a
)
296 return float64_chs(a
);
299 float32
HELPER(ucf64_abss
)(float32 a
)
301 return float32_abs(a
);
304 float64
HELPER(ucf64_absd
)(float64 a
)
306 return float64_abs(a
);
309 /* XXX: check quiet/signaling case */
310 void HELPER(ucf64_cmps
)(float32 a
, float32 b
, uint32_t c
, CPUUniCore32State
*env
)
313 flag
= float32_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 void HELPER(ucf64_cmpd
)(float64 a
, float64 b
, uint32_t c
, CPUUniCore32State
*env
)
361 flag
= float64_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
377 if ((flag
== 0) || (flag
== 2)) {
387 if ((flag
== -1) || (flag
== 2)) {
392 if ((flag
== -1) || (flag
== 0)) {
402 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
403 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
406 /* Helper routines to perform bitwise copies between float and int. */
407 static inline float32
ucf64_itos(uint32_t i
)
418 static inline uint32_t ucf64_stoi(float32 s
)
429 static inline float64
ucf64_itod(uint64_t i
)
440 static inline uint64_t ucf64_dtoi(float64 d
)
451 /* Integer to float conversion. */
452 float32
HELPER(ucf64_si2sf
)(float32 x
, CPUUniCore32State
*env
)
454 return int32_to_float32(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
457 float64
HELPER(ucf64_si2df
)(float32 x
, CPUUniCore32State
*env
)
459 return int32_to_float64(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
462 /* Float to integer conversion. */
463 float32
HELPER(ucf64_sf2si
)(float32 x
, CPUUniCore32State
*env
)
465 return ucf64_itos(float32_to_int32(x
, &env
->ucf64
.fp_status
));
468 float32
HELPER(ucf64_df2si
)(float64 x
, CPUUniCore32State
*env
)
470 return ucf64_itos(float64_to_int32(x
, &env
->ucf64
.fp_status
));
473 /* floating point conversion */
474 float64
HELPER(ucf64_sf2df
)(float32 x
, CPUUniCore32State
*env
)
476 return float32_to_float64(x
, &env
->ucf64
.fp_status
);
479 float32
HELPER(ucf64_df2sf
)(float64 x
, CPUUniCore32State
*env
)
481 return float64_to_float32(x
, &env
->ucf64
.fp_status
);