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 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 void do_interrupt(CPUUniCore32State
*env
)
50 env
->exception_index
= -1;
53 int uc32_cpu_handle_mmu_fault(CPUUniCore32State
*env
, target_ulong address
, int rw
,
56 env
->exception_index
= UC32_EXCP_TRAP
;
57 env
->cp0
.c4_faultaddr
= address
;
61 /* These should probably raise undefined insn exceptions. */
62 void HELPER(set_cp
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
64 int op1
= (insn
>> 8) & 0xf;
65 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
69 uint32_t HELPER(get_cp
)(CPUUniCore32State
*env
, uint32_t insn
)
71 int op1
= (insn
>> 8) & 0xf;
72 cpu_abort(env
, "cp%i insn %08x\n", op1
, insn
);
76 void HELPER(set_cp0
)(CPUUniCore32State
*env
, uint32_t insn
, uint32_t val
)
78 cpu_abort(env
, "cp0 insn %08x\n", insn
);
81 uint32_t HELPER(get_cp0
)(CPUUniCore32State
*env
, uint32_t insn
)
83 cpu_abort(env
, "cp0 insn %08x\n", insn
);
87 void switch_mode(CPUUniCore32State
*env
, int mode
)
89 if (mode
!= ASR_MODE_USER
) {
90 cpu_abort(env
, "Tried to switch out of user mode\n");
94 void HELPER(set_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
, uint32_t val
)
96 cpu_abort(env
, "banked r29 write\n");
99 uint32_t HELPER(get_r29_banked
)(CPUUniCore32State
*env
, uint32_t mode
)
101 cpu_abort(env
, "banked r29 read\n");
105 /* UniCore-F64 support. We follow the convention used for F64 instrunctions:
106 Single precition routines have a "s" suffix, double precision a
109 /* Convert host exception flags to f64 form. */
110 static inline int ucf64_exceptbits_from_host(int host_bits
)
114 if (host_bits
& float_flag_invalid
) {
115 target_bits
|= UCF64_FPSCR_FLAG_INVALID
;
117 if (host_bits
& float_flag_divbyzero
) {
118 target_bits
|= UCF64_FPSCR_FLAG_DIVZERO
;
120 if (host_bits
& float_flag_overflow
) {
121 target_bits
|= UCF64_FPSCR_FLAG_OVERFLOW
;
123 if (host_bits
& float_flag_underflow
) {
124 target_bits
|= UCF64_FPSCR_FLAG_UNDERFLOW
;
126 if (host_bits
& float_flag_inexact
) {
127 target_bits
|= UCF64_FPSCR_FLAG_INEXACT
;
132 uint32_t HELPER(ucf64_get_fpscr
)(CPUUniCore32State
*env
)
137 fpscr
= (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & UCF64_FPSCR_MASK
);
138 i
= get_float_exception_flags(&env
->ucf64
.fp_status
);
139 fpscr
|= ucf64_exceptbits_from_host(i
);
143 /* Convert ucf64 exception flags to target form. */
144 static inline int ucf64_exceptbits_to_host(int target_bits
)
148 if (target_bits
& UCF64_FPSCR_FLAG_INVALID
) {
149 host_bits
|= float_flag_invalid
;
151 if (target_bits
& UCF64_FPSCR_FLAG_DIVZERO
) {
152 host_bits
|= float_flag_divbyzero
;
154 if (target_bits
& UCF64_FPSCR_FLAG_OVERFLOW
) {
155 host_bits
|= float_flag_overflow
;
157 if (target_bits
& UCF64_FPSCR_FLAG_UNDERFLOW
) {
158 host_bits
|= float_flag_underflow
;
160 if (target_bits
& UCF64_FPSCR_FLAG_INEXACT
) {
161 host_bits
|= float_flag_inexact
;
166 void HELPER(ucf64_set_fpscr
)(CPUUniCore32State
*env
, uint32_t val
)
171 changed
= env
->ucf64
.xregs
[UC32_UCF64_FPSCR
];
172 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (val
& UCF64_FPSCR_MASK
);
175 if (changed
& (UCF64_FPSCR_RND_MASK
)) {
176 i
= UCF64_FPSCR_RND(val
);
179 i
= float_round_nearest_even
;
182 i
= float_round_to_zero
;
188 i
= float_round_down
;
190 default: /* 100 and 101 not implement */
191 cpu_abort(env
, "Unsupported UniCore-F64 round mode");
193 set_float_rounding_mode(i
, &env
->ucf64
.fp_status
);
196 i
= ucf64_exceptbits_to_host(UCF64_FPSCR_TRAPEN(val
));
197 set_float_exception_flags(i
, &env
->ucf64
.fp_status
);
200 float32
HELPER(ucf64_adds
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
202 return float32_add(a
, b
, &env
->ucf64
.fp_status
);
205 float64
HELPER(ucf64_addd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
207 return float64_add(a
, b
, &env
->ucf64
.fp_status
);
210 float32
HELPER(ucf64_subs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
212 return float32_sub(a
, b
, &env
->ucf64
.fp_status
);
215 float64
HELPER(ucf64_subd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
217 return float64_sub(a
, b
, &env
->ucf64
.fp_status
);
220 float32
HELPER(ucf64_muls
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
222 return float32_mul(a
, b
, &env
->ucf64
.fp_status
);
225 float64
HELPER(ucf64_muld
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
227 return float64_mul(a
, b
, &env
->ucf64
.fp_status
);
230 float32
HELPER(ucf64_divs
)(float32 a
, float32 b
, CPUUniCore32State
*env
)
232 return float32_div(a
, b
, &env
->ucf64
.fp_status
);
235 float64
HELPER(ucf64_divd
)(float64 a
, float64 b
, CPUUniCore32State
*env
)
237 return float64_div(a
, b
, &env
->ucf64
.fp_status
);
240 float32
HELPER(ucf64_negs
)(float32 a
)
242 return float32_chs(a
);
245 float64
HELPER(ucf64_negd
)(float64 a
)
247 return float64_chs(a
);
250 float32
HELPER(ucf64_abss
)(float32 a
)
252 return float32_abs(a
);
255 float64
HELPER(ucf64_absd
)(float64 a
)
257 return float64_abs(a
);
260 /* XXX: check quiet/signaling case */
261 void HELPER(ucf64_cmps
)(float32 a
, float32 b
, uint32_t c
, CPUUniCore32State
*env
)
264 flag
= float32_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
280 if ((flag
== 0) || (flag
== 2)) {
290 if ((flag
== -1) || (flag
== 2)) {
295 if ((flag
== -1) || (flag
== 0)) {
305 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
306 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
309 void HELPER(ucf64_cmpd
)(float64 a
, float64 b
, uint32_t c
, CPUUniCore32State
*env
)
312 flag
= float64_compare_quiet(a
, b
, &env
->ucf64
.fp_status
);
328 if ((flag
== 0) || (flag
== 2)) {
338 if ((flag
== -1) || (flag
== 2)) {
343 if ((flag
== -1) || (flag
== 0)) {
353 env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] = (env
->CF
<< 29)
354 | (env
->ucf64
.xregs
[UC32_UCF64_FPSCR
] & 0x0fffffff);
357 /* Helper routines to perform bitwise copies between float and int. */
358 static inline float32
ucf64_itos(uint32_t i
)
369 static inline uint32_t ucf64_stoi(float32 s
)
380 static inline float64
ucf64_itod(uint64_t i
)
391 static inline uint64_t ucf64_dtoi(float64 d
)
402 /* Integer to float conversion. */
403 float32
HELPER(ucf64_si2sf
)(float32 x
, CPUUniCore32State
*env
)
405 return int32_to_float32(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
408 float64
HELPER(ucf64_si2df
)(float32 x
, CPUUniCore32State
*env
)
410 return int32_to_float64(ucf64_stoi(x
), &env
->ucf64
.fp_status
);
413 /* Float to integer conversion. */
414 float32
HELPER(ucf64_sf2si
)(float32 x
, CPUUniCore32State
*env
)
416 return ucf64_itos(float32_to_int32(x
, &env
->ucf64
.fp_status
));
419 float32
HELPER(ucf64_df2si
)(float64 x
, CPUUniCore32State
*env
)
421 return ucf64_itos(float64_to_int32(x
, &env
->ucf64
.fp_status
));
424 /* floating point conversion */
425 float64
HELPER(ucf64_sf2df
)(float32 x
, CPUUniCore32State
*env
)
427 return float32_to_float64(x
, &env
->ucf64
.fp_status
);
430 float32
HELPER(ucf64_df2sf
)(float64 x
, CPUUniCore32State
*env
)
432 return float64_to_float32(x
, &env
->ucf64
.fp_status
);