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.
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 static inline void set_feature(CPUUniCore32State
*env
, int feature
)
19 env
->features
|= feature
;
27 static const struct uc32_cpu_t uc32_cpu_names
[] = {
28 { UC32_CPUID_UCV2
, "UniCore-II"},
29 { UC32_CPUID_ANY
, "any"},
33 /* return 0 if not found */
34 static uint32_t uc32_cpu_find_by_name(const char *name
)
40 for (i
= 0; uc32_cpu_names
[i
].name
; i
++) {
41 if (strcmp(name
, uc32_cpu_names
[i
].name
) == 0) {
42 id
= uc32_cpu_names
[i
].id
;
49 CPUUniCore32State
*uc32_cpu_init(const char *cpu_model
)
51 CPUUniCore32State
*env
;
53 static int inited
= 1;
55 env
= g_malloc0(sizeof(CPUUniCore32State
));
58 id
= uc32_cpu_find_by_name(cpu_model
);
61 set_feature(env
, UC32_HWCAP_CMOV
);
62 set_feature(env
, UC32_HWCAP_UCF64
);
63 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = 0;
64 env
->cp0
.c0_cachetype
= 0x1dd20d2;
65 env
->cp0
.c1_sys
= 0x00090078;
67 case UC32_CPUID_ANY
: /* For userspace emulation. */
68 set_feature(env
, UC32_HWCAP_CMOV
);
69 set_feature(env
, UC32_HWCAP_UCF64
);
72 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
75 env
->cpu_model_str
= cpu_model
;
76 env
->cp0
.c0_cpuid
= id
;
77 env
->uncached_asr
= ASR_MODE_USER
;
82 uc32_translate_init();
90 uint32_t HELPER(clo
)(uint32_t x
)
95 uint32_t HELPER(clz
)(uint32_t x
)
100 void do_interrupt(CPUUniCore32State
*env
)
102 env
->exception_index
= -1;
105 int uc32_cpu_handle_mmu_fault(CPUUniCore32State
*env
, target_ulong address
, int rw
,
108 env
->exception_index
= UC32_EXCP_TRAP
;
109 env
->cp0
.c4_faultaddr
= address
;
113 /* These should probably raise undefined insn exceptions. */
114 void HELPER(set_cp
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
116 int op1
= (insn
>> 8) & 0xf;
117 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
121 uint32_t HELPER(get_cp
)(CPUUniCore32State
*env
, uint32_t insn
)
123 int op1
= (insn
>> 8) & 0xf;
124 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
128 void HELPER(set_cp0
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
130 cpu_abort(env
, "cp0 insn %08x\n", insn
);
133 uint32_t HELPER(get_cp0
)(CPUUniCore32State
*env
, uint32_t insn
)
135 cpu_abort(env
, "cp0 insn %08x\n", insn
);
139 void switch_mode(CPUUniCore32State
*env
, int mode
)
141 if (mode
!= ASR_MODE_USER
) {
142 cpu_abort(env
, "Tried to switch out of user mode\n");
146 void HELPER(set_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
, uint32_t val
)
148 cpu_abort(env
, "banked r29 write\n");
151 uint32_t HELPER(get_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
)
153 cpu_abort(env
, "banked r29 read\n");
157 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
158 Single precition routines have a "s" suffix, double precision a
161 /* Convert host exception flags to f64 form. */
162 static inline int ucf64_exceptbits_from_host(int host_bits
)
166 if (host_bits
& float_flag_invalid
) {
167 target_bits
|= UCF64_FPSCR_FLAG_INVALID
;
169 if (host_bits
& float_flag_divbyzero
) {
170 target_bits
|= UCF64_FPSCR_FLAG_DIVZERO
;
172 if (host_bits
& float_flag_overflow
) {
173 target_bits
|= UCF64_FPSCR_FLAG_OVERFLOW
;
175 if (host_bits
& float_flag_underflow
) {
176 target_bits
|= UCF64_FPSCR_FLAG_UNDERFLOW
;
178 if (host_bits
& float_flag_inexact
) {
179 target_bits
|= UCF64_FPSCR_FLAG_INEXACT
;
184 uint32_t HELPER(ucf64_get_fpscr
)(CPUUniCore32State
*env
)
189 fpscr
= (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & UCF64_FPSCR_MASK
);
190 i
= get_float_exception_flags(&env
->ucf64
.fp_status
);
191 fpscr
|= ucf64_exceptbits_from_host(i
);
195 /* Convert ucf64 exception flags to target form. */
196 static inline int ucf64_exceptbits_to_host(int target_bits
)
200 if (target_bits
& UCF64_FPSCR_FLAG_INVALID
) {
201 host_bits
|= float_flag_invalid
;
203 if (target_bits
& UCF64_FPSCR_FLAG_DIVZERO
) {
204 host_bits
|= float_flag_divbyzero
;
206 if (target_bits
& UCF64_FPSCR_FLAG_OVERFLOW
) {
207 host_bits
|= float_flag_overflow
;
209 if (target_bits
& UCF64_FPSCR_FLAG_UNDERFLOW
) {
210 host_bits
|= float_flag_underflow
;
212 if (target_bits
& UCF64_FPSCR_FLAG_INEXACT
) {
213 host_bits
|= float_flag_inexact
;
218 void HELPER(ucf64_set_fpscr
)(CPUUniCore32State
*env
, uint32_t val
)
223 changed
= env
->ucf64
.xregs
[UC32_UCF64_FPSCR
];
224 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (val
& UCF64_FPSCR_MASK
);
227 if (changed
& (UCF64_FPSCR_RND_MASK
)) {
228 i
= UCF64_FPSCR_RND(val
);
231 i
= float_round_nearest_even
;
234 i
= float_round_to_zero
;
240 i
= float_round_down
;
242 default: /* 100 and 101 not implement */
243 cpu_abort(env
, "Unsupported UniCore-F64 round mode");
245 set_float_rounding_mode(i
, &env
->ucf64
.fp_status
);
248 i
= ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val
));
249 set_float_exception_flags(i
, &env
->ucf64
.fp_status
);
252 float32
HELPER(ucf64_adds
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
254 return float32_add(a
, b
, &env
->ucf64
.fp_status
);
257 float64
HELPER(ucf64_addd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
259 return float64_add(a
, b
, &env
->ucf64
.fp_status
);
262 float32
HELPER(ucf64_subs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
264 return float32_sub(a
, b
, &env
->ucf64
.fp_status
);
267 float64
HELPER(ucf64_subd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
269 return float64_sub(a
, b
, &env
->ucf64
.fp_status
);
272 float32
HELPER(ucf64_muls
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
274 return float32_mul(a
, b
, &env
->ucf64
.fp_status
);
277 float64
HELPER(ucf64_muld
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
279 return float64_mul(a
, b
, &env
->ucf64
.fp_status
);
282 float32
HELPER(ucf64_divs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
284 return float32_div(a
, b
, &env
->ucf64
.fp_status
);
287 float64
HELPER(ucf64_divd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
289 return float64_div(a
, b
, &env
->ucf64
.fp_status
);
292 float32
HELPER(ucf64_negs
)(float32 a
)
294 return float32_chs(a
);
297 float64
HELPER(ucf64_negd
)(float64 a
)
299 return float64_chs(a
);
302 float32
HELPER(ucf64_abss
)(float32 a
)
304 return float32_abs(a
);
307 float64
HELPER(ucf64_absd
)(float64 a
)
309 return float64_abs(a
);
312 /* XXX: check quiet/signaling case */
313 void HELPER(ucf64_cmps
)(float32 a
, float32 b
, uint32_t c
, CPUUniCore32State
*env
)
316 flag
= float32_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
332 if ((flag
== 0) || (flag
== 2)) {
342 if ((flag
== -1) || (flag
== 2)) {
347 if ((flag
== -1) || (flag
== 0)) {
357 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
358 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
361 void HELPER(ucf64_cmpd
)(float64 a
, float64 b
, uint32_t c
, CPUUniCore32State
*env
)
364 flag
= float64_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
380 if ((flag
== 0) || (flag
== 2)) {
390 if ((flag
== -1) || (flag
== 2)) {
395 if ((flag
== -1) || (flag
== 0)) {
405 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
406 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
409 /* Helper routines to perform bitwise copies between float and int. */
410 static inline float32
ucf64_itos(uint32_t i
)
421 static inline uint32_t ucf64_stoi(float32 s
)
432 static inline float64
ucf64_itod(uint64_t i
)
443 static inline uint64_t ucf64_dtoi(float64 d
)
454 /* Integer to float conversion. */
455 float32
HELPER(ucf64_si2sf
)(float32 x
, CPUUniCore32State
*env
)
457 return int32_to_float32(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
460 float64
HELPER(ucf64_si2df
)(float32 x
, CPUUniCore32State
*env
)
462 return int32_to_float64(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
465 /* Float to integer conversion. */
466 float32
HELPER(ucf64_sf2si
)(float32 x
, CPUUniCore32State
*env
)
468 return ucf64_itos(float32_to_int32(x
, &env
->ucf64
.fp_status
));
471 float32
HELPER(ucf64_df2si
)(float64 x
, CPUUniCore32State
*env
)
473 return ucf64_itos(float64_to_int32(x
, &env
->ucf64
.fp_status
));
476 /* floating point conversion */
477 float64
HELPER(ucf64_sf2df
)(float32 x
, CPUUniCore32State
*env
)
479 return float32_to_float64(x
, &env
->ucf64
.fp_status
);
482 float32
HELPER(ucf64_df2sf
)(float64 x
, CPUUniCore32State
*env
)
484 return float64_to_float32(x
, &env
->ucf64
.fp_status
);