fpu/softfloat: re-factor int/uint to float
[qemu/ar7.git] / target / microblaze / op_helper.c
blob1b4fe796e739b83af7802636bfc7b3eb86630da0
1 /*
2 * Microblaze helper routines.
4 * Copyright (c) 2009 Edgar E. Iglesias <edgar.iglesias@gmail.com>.
5 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "fpu/softfloat.h"
29 #define D(x)
31 #if !defined(CONFIG_USER_ONLY)
33 /* Try to fill the TLB and return an exception if error. If retaddr is
34 * NULL, it means that the function was called in C code (i.e. not
35 * from generated code or from helper.c)
37 void tlb_fill(CPUState *cs, target_ulong addr, int size,
38 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
40 int ret;
42 ret = mb_cpu_handle_mmu_fault(cs, addr, size, access_type, mmu_idx);
43 if (unlikely(ret)) {
44 /* now we have a real cpu fault */
45 cpu_loop_exit_restore(cs, retaddr);
48 #endif
50 void helper_put(uint32_t id, uint32_t ctrl, uint32_t data)
52 int test = ctrl & STREAM_TEST;
53 int atomic = ctrl & STREAM_ATOMIC;
54 int control = ctrl & STREAM_CONTROL;
55 int nonblock = ctrl & STREAM_NONBLOCK;
56 int exception = ctrl & STREAM_EXCEPTION;
58 qemu_log_mask(LOG_UNIMP, "Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
59 id, data,
60 test ? "t" : "",
61 nonblock ? "n" : "",
62 exception ? "e" : "",
63 control ? "c" : "",
64 atomic ? "a" : "");
67 uint32_t helper_get(uint32_t id, uint32_t ctrl)
69 int test = ctrl & STREAM_TEST;
70 int atomic = ctrl & STREAM_ATOMIC;
71 int control = ctrl & STREAM_CONTROL;
72 int nonblock = ctrl & STREAM_NONBLOCK;
73 int exception = ctrl & STREAM_EXCEPTION;
75 qemu_log_mask(LOG_UNIMP, "Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
76 id,
77 test ? "t" : "",
78 nonblock ? "n" : "",
79 exception ? "e" : "",
80 control ? "c" : "",
81 atomic ? "a" : "");
82 return 0xdead0000 | id;
85 void helper_raise_exception(CPUMBState *env, uint32_t index)
87 CPUState *cs = CPU(mb_env_get_cpu(env));
89 cs->exception_index = index;
90 cpu_loop_exit(cs);
93 void helper_debug(CPUMBState *env)
95 int i;
97 qemu_log("PC=%8.8x\n", env->sregs[SR_PC]);
98 qemu_log("rmsr=%x resr=%x rear=%x debug[%x] imm=%x iflags=%x\n",
99 env->sregs[SR_MSR], env->sregs[SR_ESR], env->sregs[SR_EAR],
100 env->debug, env->imm, env->iflags);
101 qemu_log("btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
102 env->btaken, env->btarget,
103 (env->sregs[SR_MSR] & MSR_UM) ? "user" : "kernel",
104 (env->sregs[SR_MSR] & MSR_UMS) ? "user" : "kernel",
105 (env->sregs[SR_MSR] & MSR_EIP),
106 (env->sregs[SR_MSR] & MSR_IE));
107 for (i = 0; i < 32; i++) {
108 qemu_log("r%2.2d=%8.8x ", i, env->regs[i]);
109 if ((i + 1) % 4 == 0)
110 qemu_log("\n");
112 qemu_log("\n\n");
115 static inline uint32_t compute_carry(uint32_t a, uint32_t b, uint32_t cin)
117 uint32_t cout = 0;
119 if ((b == ~0) && cin)
120 cout = 1;
121 else if ((~0 - a) < (b + cin))
122 cout = 1;
123 return cout;
126 uint32_t helper_cmp(uint32_t a, uint32_t b)
128 uint32_t t;
130 t = b + ~a + 1;
131 if ((b & 0x80000000) ^ (a & 0x80000000))
132 t = (t & 0x7fffffff) | (b & 0x80000000);
133 return t;
136 uint32_t helper_cmpu(uint32_t a, uint32_t b)
138 uint32_t t;
140 t = b + ~a + 1;
141 if ((b & 0x80000000) ^ (a & 0x80000000))
142 t = (t & 0x7fffffff) | (a & 0x80000000);
143 return t;
146 uint32_t helper_carry(uint32_t a, uint32_t b, uint32_t cf)
148 return compute_carry(a, b, cf);
151 static inline int div_prepare(CPUMBState *env, uint32_t a, uint32_t b)
153 if (b == 0) {
154 env->sregs[SR_MSR] |= MSR_DZ;
156 if ((env->sregs[SR_MSR] & MSR_EE)
157 && !(env->pvr.regs[2] & PVR2_DIV_ZERO_EXC_MASK)) {
158 env->sregs[SR_ESR] = ESR_EC_DIVZERO;
159 helper_raise_exception(env, EXCP_HW_EXCP);
161 return 0;
163 env->sregs[SR_MSR] &= ~MSR_DZ;
164 return 1;
167 uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
169 if (!div_prepare(env, a, b)) {
170 return 0;
172 return (int32_t)a / (int32_t)b;
175 uint32_t helper_divu(CPUMBState *env, uint32_t a, uint32_t b)
177 if (!div_prepare(env, a, b)) {
178 return 0;
180 return a / b;
183 /* raise FPU exception. */
184 static void raise_fpu_exception(CPUMBState *env)
186 env->sregs[SR_ESR] = ESR_EC_FPU;
187 helper_raise_exception(env, EXCP_HW_EXCP);
190 static void update_fpu_flags(CPUMBState *env, int flags)
192 int raise = 0;
194 if (flags & float_flag_invalid) {
195 env->sregs[SR_FSR] |= FSR_IO;
196 raise = 1;
198 if (flags & float_flag_divbyzero) {
199 env->sregs[SR_FSR] |= FSR_DZ;
200 raise = 1;
202 if (flags & float_flag_overflow) {
203 env->sregs[SR_FSR] |= FSR_OF;
204 raise = 1;
206 if (flags & float_flag_underflow) {
207 env->sregs[SR_FSR] |= FSR_UF;
208 raise = 1;
210 if (raise
211 && (env->pvr.regs[2] & PVR2_FPU_EXC_MASK)
212 && (env->sregs[SR_MSR] & MSR_EE)) {
213 raise_fpu_exception(env);
217 uint32_t helper_fadd(CPUMBState *env, uint32_t a, uint32_t b)
219 CPU_FloatU fd, fa, fb;
220 int flags;
222 set_float_exception_flags(0, &env->fp_status);
223 fa.l = a;
224 fb.l = b;
225 fd.f = float32_add(fa.f, fb.f, &env->fp_status);
227 flags = get_float_exception_flags(&env->fp_status);
228 update_fpu_flags(env, flags);
229 return fd.l;
232 uint32_t helper_frsub(CPUMBState *env, uint32_t a, uint32_t b)
234 CPU_FloatU fd, fa, fb;
235 int flags;
237 set_float_exception_flags(0, &env->fp_status);
238 fa.l = a;
239 fb.l = b;
240 fd.f = float32_sub(fb.f, fa.f, &env->fp_status);
241 flags = get_float_exception_flags(&env->fp_status);
242 update_fpu_flags(env, flags);
243 return fd.l;
246 uint32_t helper_fmul(CPUMBState *env, uint32_t a, uint32_t b)
248 CPU_FloatU fd, fa, fb;
249 int flags;
251 set_float_exception_flags(0, &env->fp_status);
252 fa.l = a;
253 fb.l = b;
254 fd.f = float32_mul(fa.f, fb.f, &env->fp_status);
255 flags = get_float_exception_flags(&env->fp_status);
256 update_fpu_flags(env, flags);
258 return fd.l;
261 uint32_t helper_fdiv(CPUMBState *env, uint32_t a, uint32_t b)
263 CPU_FloatU fd, fa, fb;
264 int flags;
266 set_float_exception_flags(0, &env->fp_status);
267 fa.l = a;
268 fb.l = b;
269 fd.f = float32_div(fb.f, fa.f, &env->fp_status);
270 flags = get_float_exception_flags(&env->fp_status);
271 update_fpu_flags(env, flags);
273 return fd.l;
276 uint32_t helper_fcmp_un(CPUMBState *env, uint32_t a, uint32_t b)
278 CPU_FloatU fa, fb;
279 uint32_t r = 0;
281 fa.l = a;
282 fb.l = b;
284 if (float32_is_signaling_nan(fa.f, &env->fp_status) ||
285 float32_is_signaling_nan(fb.f, &env->fp_status)) {
286 update_fpu_flags(env, float_flag_invalid);
287 r = 1;
290 if (float32_is_quiet_nan(fa.f, &env->fp_status) ||
291 float32_is_quiet_nan(fb.f, &env->fp_status)) {
292 r = 1;
295 return r;
298 uint32_t helper_fcmp_lt(CPUMBState *env, uint32_t a, uint32_t b)
300 CPU_FloatU fa, fb;
301 int r;
302 int flags;
304 set_float_exception_flags(0, &env->fp_status);
305 fa.l = a;
306 fb.l = b;
307 r = float32_lt(fb.f, fa.f, &env->fp_status);
308 flags = get_float_exception_flags(&env->fp_status);
309 update_fpu_flags(env, flags & float_flag_invalid);
311 return r;
314 uint32_t helper_fcmp_eq(CPUMBState *env, uint32_t a, uint32_t b)
316 CPU_FloatU fa, fb;
317 int flags;
318 int r;
320 set_float_exception_flags(0, &env->fp_status);
321 fa.l = a;
322 fb.l = b;
323 r = float32_eq_quiet(fa.f, fb.f, &env->fp_status);
324 flags = get_float_exception_flags(&env->fp_status);
325 update_fpu_flags(env, flags & float_flag_invalid);
327 return r;
330 uint32_t helper_fcmp_le(CPUMBState *env, uint32_t a, uint32_t b)
332 CPU_FloatU fa, fb;
333 int flags;
334 int r;
336 fa.l = a;
337 fb.l = b;
338 set_float_exception_flags(0, &env->fp_status);
339 r = float32_le(fa.f, fb.f, &env->fp_status);
340 flags = get_float_exception_flags(&env->fp_status);
341 update_fpu_flags(env, flags & float_flag_invalid);
344 return r;
347 uint32_t helper_fcmp_gt(CPUMBState *env, uint32_t a, uint32_t b)
349 CPU_FloatU fa, fb;
350 int flags, r;
352 fa.l = a;
353 fb.l = b;
354 set_float_exception_flags(0, &env->fp_status);
355 r = float32_lt(fa.f, fb.f, &env->fp_status);
356 flags = get_float_exception_flags(&env->fp_status);
357 update_fpu_flags(env, flags & float_flag_invalid);
358 return r;
361 uint32_t helper_fcmp_ne(CPUMBState *env, uint32_t a, uint32_t b)
363 CPU_FloatU fa, fb;
364 int flags, r;
366 fa.l = a;
367 fb.l = b;
368 set_float_exception_flags(0, &env->fp_status);
369 r = !float32_eq_quiet(fa.f, fb.f, &env->fp_status);
370 flags = get_float_exception_flags(&env->fp_status);
371 update_fpu_flags(env, flags & float_flag_invalid);
373 return r;
376 uint32_t helper_fcmp_ge(CPUMBState *env, uint32_t a, uint32_t b)
378 CPU_FloatU fa, fb;
379 int flags, r;
381 fa.l = a;
382 fb.l = b;
383 set_float_exception_flags(0, &env->fp_status);
384 r = !float32_lt(fa.f, fb.f, &env->fp_status);
385 flags = get_float_exception_flags(&env->fp_status);
386 update_fpu_flags(env, flags & float_flag_invalid);
388 return r;
391 uint32_t helper_flt(CPUMBState *env, uint32_t a)
393 CPU_FloatU fd, fa;
395 fa.l = a;
396 fd.f = int32_to_float32(fa.l, &env->fp_status);
397 return fd.l;
400 uint32_t helper_fint(CPUMBState *env, uint32_t a)
402 CPU_FloatU fa;
403 uint32_t r;
404 int flags;
406 set_float_exception_flags(0, &env->fp_status);
407 fa.l = a;
408 r = float32_to_int32(fa.f, &env->fp_status);
409 flags = get_float_exception_flags(&env->fp_status);
410 update_fpu_flags(env, flags);
412 return r;
415 uint32_t helper_fsqrt(CPUMBState *env, uint32_t a)
417 CPU_FloatU fd, fa;
418 int flags;
420 set_float_exception_flags(0, &env->fp_status);
421 fa.l = a;
422 fd.l = float32_sqrt(fa.f, &env->fp_status);
423 flags = get_float_exception_flags(&env->fp_status);
424 update_fpu_flags(env, flags);
426 return fd.l;
429 uint32_t helper_pcmpbf(uint32_t a, uint32_t b)
431 unsigned int i;
432 uint32_t mask = 0xff000000;
434 for (i = 0; i < 4; i++) {
435 if ((a & mask) == (b & mask))
436 return i + 1;
437 mask >>= 8;
439 return 0;
442 void helper_memalign(CPUMBState *env, uint32_t addr, uint32_t dr, uint32_t wr,
443 uint32_t mask)
445 if (addr & mask) {
446 qemu_log_mask(CPU_LOG_INT,
447 "unaligned access addr=%x mask=%x, wr=%d dr=r%d\n",
448 addr, mask, wr, dr);
449 env->sregs[SR_EAR] = addr;
450 env->sregs[SR_ESR] = ESR_EC_UNALIGNED_DATA | (wr << 10) \
451 | (dr & 31) << 5;
452 if (mask == 3) {
453 env->sregs[SR_ESR] |= 1 << 11;
455 if (!(env->sregs[SR_MSR] & MSR_EE)) {
456 return;
458 helper_raise_exception(env, EXCP_HW_EXCP);
462 void helper_stackprot(CPUMBState *env, uint32_t addr)
464 if (addr < env->slr || addr > env->shr) {
465 qemu_log_mask(CPU_LOG_INT, "Stack protector violation at %x %x %x\n",
466 addr, env->slr, env->shr);
467 env->sregs[SR_EAR] = addr;
468 env->sregs[SR_ESR] = ESR_EC_STACKPROT;
469 helper_raise_exception(env, EXCP_HW_EXCP);
473 #if !defined(CONFIG_USER_ONLY)
474 /* Writes/reads to the MMU's special regs end up here. */
475 uint32_t helper_mmu_read(CPUMBState *env, uint32_t rn)
477 return mmu_read(env, rn);
480 void helper_mmu_write(CPUMBState *env, uint32_t rn, uint32_t v)
482 mmu_write(env, rn, v);
485 void mb_cpu_unassigned_access(CPUState *cs, hwaddr addr,
486 bool is_write, bool is_exec, int is_asi,
487 unsigned size)
489 MicroBlazeCPU *cpu;
490 CPUMBState *env;
492 qemu_log_mask(CPU_LOG_INT, "Unassigned " TARGET_FMT_plx " wr=%d exe=%d\n",
493 addr, is_write ? 1 : 0, is_exec ? 1 : 0);
494 if (cs == NULL) {
495 return;
497 cpu = MICROBLAZE_CPU(cs);
498 env = &cpu->env;
499 if (!(env->sregs[SR_MSR] & MSR_EE)) {
500 return;
503 env->sregs[SR_EAR] = addr;
504 if (is_exec) {
505 if ((env->pvr.regs[2] & PVR2_IOPB_BUS_EXC_MASK)) {
506 env->sregs[SR_ESR] = ESR_EC_INSN_BUS;
507 helper_raise_exception(env, EXCP_HW_EXCP);
509 } else {
510 if ((env->pvr.regs[2] & PVR2_DOPB_BUS_EXC_MASK)) {
511 env->sregs[SR_ESR] = ESR_EC_DATA_BUS;
512 helper_raise_exception(env, EXCP_HW_EXCP);
516 #endif