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.
15 #include "qemu-common.h"
16 #include "host-utils.h"
18 static inline void set_feature(CPUState
*env
, int feature
)
20 env
->features
|= feature
;
28 static const struct uc32_cpu_t uc32_cpu_names
[] = {
29 { UC32_CPUID_UCV2
, "UniCore-II"},
30 { UC32_CPUID_ANY
, "any"},
34 /* return 0 if not found */
35 static uint32_t uc32_cpu_find_by_name(const char *name
)
41 for (i
= 0; uc32_cpu_names
[i
].name
; i
++) {
42 if (strcmp(name
, uc32_cpu_names
[i
].name
) == 0) {
43 id
= uc32_cpu_names
[i
].id
;
50 CPUState
*uc32_cpu_init(const char *cpu_model
)
54 static int inited
= 1;
56 env
= qemu_mallocz(sizeof(CPUState
));
59 id
= uc32_cpu_find_by_name(cpu_model
);
62 set_feature(env
, UC32_HWCAP_CMOV
);
63 set_feature(env
, UC32_HWCAP_UCF64
);
64 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = 0;
65 env
->cp0
.c0_cachetype
= 0x1dd20d2;
66 env
->cp0
.c1_sys
= 0x00090078;
68 case UC32_CPUID_ANY
: /* For userspace emulation. */
69 set_feature(env
, UC32_HWCAP_CMOV
);
70 set_feature(env
, UC32_HWCAP_UCF64
);
73 cpu_abort(env
, "Bad CPU ID: %x\n", id
);
76 env
->cpu_model_str
= cpu_model
;
77 env
->cp0
.c0_cpuid
= id
;
78 env
->uncached_asr
= ASR_MODE_USER
;
83 uc32_translate_init();
91 uint32_t HELPER(clo
)(uint32_t x
)
96 uint32_t HELPER(clz
)(uint32_t x
)
101 void do_interrupt(CPUState
*env
)
103 env
->exception_index
= -1;
106 int uc32_cpu_handle_mmu_fault(CPUState
*env
, target_ulong address
, int rw
,
109 env
->exception_index
= UC32_EXCP_TRAP
;
110 env
->cp0
.c4_faultaddr
= address
;
114 /* These should probably raise undefined insn exceptions. */
115 void HELPER(set_cp
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
117 int op1
= (insn
>> 8) & 0xf;
118 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
122 uint32_t HELPER(get_cp
)(CPUState
*env
, uint32_t insn
)
124 int op1
= (insn
>> 8) & 0xf;
125 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
129 void HELPER(set_cp0
)(CPUState
*env
, uint32_t insn
, uint32_t val
)
131 cpu_abort(env
, "cp0 insn %08x\n", insn
);
134 uint32_t HELPER(get_cp0
)(CPUState
*env
, uint32_t insn
)
136 cpu_abort(env
, "cp0 insn %08x\n", insn
);
140 void switch_mode(CPUState
*env
, int mode
)
142 if (mode
!= ASR_MODE_USER
) {
143 cpu_abort(env
, "Tried to switch out of user mode\n");
147 void HELPER(set_r29_banked
)(CPUState
*env
, uint32_t mode
, uint32_t val
)
149 cpu_abort(env
, "banked r29 write\n");
152 uint32_t HELPER(get_r29_banked
)(CPUState
*env
, uint32_t mode
)
154 cpu_abort(env
, "banked r29 read\n");
158 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
159 Single precition routines have a "s" suffix, double precision a
162 /* Convert host exception flags to f64 form. */
163 static inline int ucf64_exceptbits_from_host(int host_bits
)
167 if (host_bits
& float_flag_invalid
) {
168 target_bits
|= UCF64_FPSCR_FLAG_INVALID
;
170 if (host_bits
& float_flag_divbyzero
) {
171 target_bits
|= UCF64_FPSCR_FLAG_DIVZERO
;
173 if (host_bits
& float_flag_overflow
) {
174 target_bits
|= UCF64_FPSCR_FLAG_OVERFLOW
;
176 if (host_bits
& float_flag_underflow
) {
177 target_bits
|= UCF64_FPSCR_FLAG_UNDERFLOW
;
179 if (host_bits
& float_flag_inexact
) {
180 target_bits
|= UCF64_FPSCR_FLAG_INEXACT
;
185 uint32_t HELPER(ucf64_get_fpscr
)(CPUState
*env
)
190 fpscr
= (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & UCF64_FPSCR_MASK
);
191 i
= get_float_exception_flags(&env
->ucf64
.fp_status
);
192 fpscr
|= ucf64_exceptbits_from_host(i
);
196 /* Convert ucf64 exception flags to target form. */
197 static inline int ucf64_exceptbits_to_host(int target_bits
)
201 if (target_bits
& UCF64_FPSCR_FLAG_INVALID
) {
202 host_bits
|= float_flag_invalid
;
204 if (target_bits
& UCF64_FPSCR_FLAG_DIVZERO
) {
205 host_bits
|= float_flag_divbyzero
;
207 if (target_bits
& UCF64_FPSCR_FLAG_OVERFLOW
) {
208 host_bits
|= float_flag_overflow
;
210 if (target_bits
& UCF64_FPSCR_FLAG_UNDERFLOW
) {
211 host_bits
|= float_flag_underflow
;
213 if (target_bits
& UCF64_FPSCR_FLAG_INEXACT
) {
214 host_bits
|= float_flag_inexact
;
219 void HELPER(ucf64_set_fpscr
)(CPUState
*env
, uint32_t val
)
224 changed
= env
->ucf64
.xregs
[UC32_UCF64_FPSCR
];
225 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (val
& UCF64_FPSCR_MASK
);
228 if (changed
& (UCF64_FPSCR_RND_MASK
)) {
229 i
= UCF64_FPSCR_RND(val
);
232 i
= float_round_nearest_even
;
235 i
= float_round_to_zero
;
241 i
= float_round_down
;
243 default: /* 100 and 101 not implement */
244 cpu_abort(env
, "Unsupported UniCore-F64 round mode");
246 set_float_rounding_mode(i
, &env
->ucf64
.fp_status
);
249 i
= ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val
));
250 set_float_exception_flags(i
, &env
->ucf64
.fp_status
);
253 float32
HELPER(ucf64_adds
)(float32 a
, float32 b
, CPUState
*env
)
255 return float32_add(a
, b
, &env
->ucf64
.fp_status
);
258 float64
HELPER(ucf64_addd
)(float64 a
, float64 b
, CPUState
*env
)
260 return float64_add(a
, b
, &env
->ucf64
.fp_status
);
263 float32
HELPER(ucf64_subs
)(float32 a
, float32 b
, CPUState
*env
)
265 return float32_sub(a
, b
, &env
->ucf64
.fp_status
);
268 float64
HELPER(ucf64_subd
)(float64 a
, float64 b
, CPUState
*env
)
270 return float64_sub(a
, b
, &env
->ucf64
.fp_status
);
273 float32
HELPER(ucf64_muls
)(float32 a
, float32 b
, CPUState
*env
)
275 return float32_mul(a
, b
, &env
->ucf64
.fp_status
);
278 float64
HELPER(ucf64_muld
)(float64 a
, float64 b
, CPUState
*env
)
280 return float64_mul(a
, b
, &env
->ucf64
.fp_status
);
283 float32
HELPER(ucf64_divs
)(float32 a
, float32 b
, CPUState
*env
)
285 return float32_div(a
, b
, &env
->ucf64
.fp_status
);
288 float64
HELPER(ucf64_divd
)(float64 a
, float64 b
, CPUState
*env
)
290 return float64_div(a
, b
, &env
->ucf64
.fp_status
);
293 float32
HELPER(ucf64_negs
)(float32 a
)
295 return float32_chs(a
);
298 float64
HELPER(ucf64_negd
)(float64 a
)
300 return float64_chs(a
);
303 float32
HELPER(ucf64_abss
)(float32 a
)
305 return float32_abs(a
);
308 float64
HELPER(ucf64_absd
)(float64 a
)
310 return float64_abs(a
);
313 /* XXX: check quiet/signaling case */
314 void HELPER(ucf64_cmps
)(float32 a
, float32 b
, uint32_t c
, CPUState
*env
)
317 flag
= float32_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
333 if ((flag
== 0) || (flag
== 2)) {
343 if ((flag
== -1) || (flag
== 2)) {
348 if ((flag
== -1) || (flag
== 0)) {
358 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
359 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
362 void HELPER(ucf64_cmpd
)(float64 a
, float64 b
, uint32_t c
, CPUState
*env
)
365 flag
= float64_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
381 if ((flag
== 0) || (flag
== 2)) {
391 if ((flag
== -1) || (flag
== 2)) {
396 if ((flag
== -1) || (flag
== 0)) {
406 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
407 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
410 /* Helper routines to perform bitwise copies between float and int. */
411 static inline float32
ucf64_itos(uint32_t i
)
422 static inline uint32_t ucf64_stoi(float32 s
)
433 static inline float64
ucf64_itod(uint64_t i
)
444 static inline uint64_t ucf64_dtoi(float64 d
)
455 /* Integer to float conversion. */
456 float32
HELPER(ucf64_si2sf
)(float32 x
, CPUState
*env
)
458 return int32_to_float32(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
461 float64
HELPER(ucf64_si2df
)(float32 x
, CPUState
*env
)
463 return int32_to_float64(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
466 /* Float to integer conversion. */
467 float32
HELPER(ucf64_sf2si
)(float32 x
, CPUState
*env
)
469 return ucf64_itos(float32_to_int32(x
, &env
->ucf64
.fp_status
));
472 float32
HELPER(ucf64_df2si
)(float64 x
, CPUState
*env
)
474 return ucf64_itos(float64_to_int32(x
, &env
->ucf64
.fp_status
));
477 /* floating point conversion */
478 float64
HELPER(ucf64_sf2df
)(float32 x
, CPUState
*env
)
480 return float32_to_float64(x
, &env
->ucf64
.fp_status
);
483 float32
HELPER(ucf64_df2sf
)(float64 x
, CPUState
*env
)
485 return float64_to_float32(x
, &env
->ucf64
.fp_status
);