ati-vga: Implement DDC and EDID info from monitor
[qemu/ar7.git] / target / microblaze / op_helper.c
blob18677ddfca5b02c3ab650cdb28c2f994330c8eb6
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 void helper_put(uint32_t id, uint32_t ctrl, uint32_t data)
33 int test = ctrl & STREAM_TEST;
34 int atomic = ctrl & STREAM_ATOMIC;
35 int control = ctrl & STREAM_CONTROL;
36 int nonblock = ctrl & STREAM_NONBLOCK;
37 int exception = ctrl & STREAM_EXCEPTION;
39 qemu_log_mask(LOG_UNIMP, "Unhandled stream put to stream-id=%d data=%x %s%s%s%s%s\n",
40 id, data,
41 test ? "t" : "",
42 nonblock ? "n" : "",
43 exception ? "e" : "",
44 control ? "c" : "",
45 atomic ? "a" : "");
48 uint32_t helper_get(uint32_t id, uint32_t ctrl)
50 int test = ctrl & STREAM_TEST;
51 int atomic = ctrl & STREAM_ATOMIC;
52 int control = ctrl & STREAM_CONTROL;
53 int nonblock = ctrl & STREAM_NONBLOCK;
54 int exception = ctrl & STREAM_EXCEPTION;
56 qemu_log_mask(LOG_UNIMP, "Unhandled stream get from stream-id=%d %s%s%s%s%s\n",
57 id,
58 test ? "t" : "",
59 nonblock ? "n" : "",
60 exception ? "e" : "",
61 control ? "c" : "",
62 atomic ? "a" : "");
63 return 0xdead0000 | id;
66 void helper_raise_exception(CPUMBState *env, uint32_t index)
68 CPUState *cs = env_cpu(env);
70 cs->exception_index = index;
71 cpu_loop_exit(cs);
74 void helper_debug(CPUMBState *env)
76 int i;
78 qemu_log("PC=%" PRIx64 "\n", env->sregs[SR_PC]);
79 qemu_log("rmsr=%" PRIx64 " resr=%" PRIx64 " rear=%" PRIx64 " "
80 "debug[%x] imm=%x iflags=%x\n",
81 env->sregs[SR_MSR], env->sregs[SR_ESR], env->sregs[SR_EAR],
82 env->debug, env->imm, env->iflags);
83 qemu_log("btaken=%d btarget=%" PRIx64 " mode=%s(saved=%s) eip=%d ie=%d\n",
84 env->btaken, env->btarget,
85 (env->sregs[SR_MSR] & MSR_UM) ? "user" : "kernel",
86 (env->sregs[SR_MSR] & MSR_UMS) ? "user" : "kernel",
87 (bool)(env->sregs[SR_MSR] & MSR_EIP),
88 (bool)(env->sregs[SR_MSR] & MSR_IE));
89 for (i = 0; i < 32; i++) {
90 qemu_log("r%2.2d=%8.8x ", i, env->regs[i]);
91 if ((i + 1) % 4 == 0)
92 qemu_log("\n");
94 qemu_log("\n\n");
97 static inline uint32_t compute_carry(uint32_t a, uint32_t b, uint32_t cin)
99 uint32_t cout = 0;
101 if ((b == ~0) && cin)
102 cout = 1;
103 else if ((~0 - a) < (b + cin))
104 cout = 1;
105 return cout;
108 uint32_t helper_cmp(uint32_t a, uint32_t b)
110 uint32_t t;
112 t = b + ~a + 1;
113 if ((b & 0x80000000) ^ (a & 0x80000000))
114 t = (t & 0x7fffffff) | (b & 0x80000000);
115 return t;
118 uint32_t helper_cmpu(uint32_t a, uint32_t b)
120 uint32_t t;
122 t = b + ~a + 1;
123 if ((b & 0x80000000) ^ (a & 0x80000000))
124 t = (t & 0x7fffffff) | (a & 0x80000000);
125 return t;
128 uint32_t helper_carry(uint32_t a, uint32_t b, uint32_t cf)
130 return compute_carry(a, b, cf);
133 static inline int div_prepare(CPUMBState *env, uint32_t a, uint32_t b)
135 if (b == 0) {
136 env->sregs[SR_MSR] |= MSR_DZ;
138 if ((env->sregs[SR_MSR] & MSR_EE)
139 && !(env->pvr.regs[2] & PVR2_DIV_ZERO_EXC_MASK)) {
140 env->sregs[SR_ESR] = ESR_EC_DIVZERO;
141 helper_raise_exception(env, EXCP_HW_EXCP);
143 return 0;
145 env->sregs[SR_MSR] &= ~MSR_DZ;
146 return 1;
149 uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
151 if (!div_prepare(env, a, b)) {
152 return 0;
154 return (int32_t)a / (int32_t)b;
157 uint32_t helper_divu(CPUMBState *env, uint32_t a, uint32_t b)
159 if (!div_prepare(env, a, b)) {
160 return 0;
162 return a / b;
165 /* raise FPU exception. */
166 static void raise_fpu_exception(CPUMBState *env)
168 env->sregs[SR_ESR] = ESR_EC_FPU;
169 helper_raise_exception(env, EXCP_HW_EXCP);
172 static void update_fpu_flags(CPUMBState *env, int flags)
174 int raise = 0;
176 if (flags & float_flag_invalid) {
177 env->sregs[SR_FSR] |= FSR_IO;
178 raise = 1;
180 if (flags & float_flag_divbyzero) {
181 env->sregs[SR_FSR] |= FSR_DZ;
182 raise = 1;
184 if (flags & float_flag_overflow) {
185 env->sregs[SR_FSR] |= FSR_OF;
186 raise = 1;
188 if (flags & float_flag_underflow) {
189 env->sregs[SR_FSR] |= FSR_UF;
190 raise = 1;
192 if (raise
193 && (env->pvr.regs[2] & PVR2_FPU_EXC_MASK)
194 && (env->sregs[SR_MSR] & MSR_EE)) {
195 raise_fpu_exception(env);
199 uint32_t helper_fadd(CPUMBState *env, uint32_t a, uint32_t b)
201 CPU_FloatU fd, fa, fb;
202 int flags;
204 set_float_exception_flags(0, &env->fp_status);
205 fa.l = a;
206 fb.l = b;
207 fd.f = float32_add(fa.f, fb.f, &env->fp_status);
209 flags = get_float_exception_flags(&env->fp_status);
210 update_fpu_flags(env, flags);
211 return fd.l;
214 uint32_t helper_frsub(CPUMBState *env, uint32_t a, uint32_t b)
216 CPU_FloatU fd, fa, fb;
217 int flags;
219 set_float_exception_flags(0, &env->fp_status);
220 fa.l = a;
221 fb.l = b;
222 fd.f = float32_sub(fb.f, fa.f, &env->fp_status);
223 flags = get_float_exception_flags(&env->fp_status);
224 update_fpu_flags(env, flags);
225 return fd.l;
228 uint32_t helper_fmul(CPUMBState *env, uint32_t a, uint32_t b)
230 CPU_FloatU fd, fa, fb;
231 int flags;
233 set_float_exception_flags(0, &env->fp_status);
234 fa.l = a;
235 fb.l = b;
236 fd.f = float32_mul(fa.f, fb.f, &env->fp_status);
237 flags = get_float_exception_flags(&env->fp_status);
238 update_fpu_flags(env, flags);
240 return fd.l;
243 uint32_t helper_fdiv(CPUMBState *env, uint32_t a, uint32_t b)
245 CPU_FloatU fd, fa, fb;
246 int flags;
248 set_float_exception_flags(0, &env->fp_status);
249 fa.l = a;
250 fb.l = b;
251 fd.f = float32_div(fb.f, fa.f, &env->fp_status);
252 flags = get_float_exception_flags(&env->fp_status);
253 update_fpu_flags(env, flags);
255 return fd.l;
258 uint32_t helper_fcmp_un(CPUMBState *env, uint32_t a, uint32_t b)
260 CPU_FloatU fa, fb;
261 uint32_t r = 0;
263 fa.l = a;
264 fb.l = b;
266 if (float32_is_signaling_nan(fa.f, &env->fp_status) ||
267 float32_is_signaling_nan(fb.f, &env->fp_status)) {
268 update_fpu_flags(env, float_flag_invalid);
269 r = 1;
272 if (float32_is_quiet_nan(fa.f, &env->fp_status) ||
273 float32_is_quiet_nan(fb.f, &env->fp_status)) {
274 r = 1;
277 return r;
280 uint32_t helper_fcmp_lt(CPUMBState *env, uint32_t a, uint32_t b)
282 CPU_FloatU fa, fb;
283 int r;
284 int flags;
286 set_float_exception_flags(0, &env->fp_status);
287 fa.l = a;
288 fb.l = b;
289 r = float32_lt(fb.f, fa.f, &env->fp_status);
290 flags = get_float_exception_flags(&env->fp_status);
291 update_fpu_flags(env, flags & float_flag_invalid);
293 return r;
296 uint32_t helper_fcmp_eq(CPUMBState *env, uint32_t a, uint32_t b)
298 CPU_FloatU fa, fb;
299 int flags;
300 int r;
302 set_float_exception_flags(0, &env->fp_status);
303 fa.l = a;
304 fb.l = b;
305 r = float32_eq_quiet(fa.f, fb.f, &env->fp_status);
306 flags = get_float_exception_flags(&env->fp_status);
307 update_fpu_flags(env, flags & float_flag_invalid);
309 return r;
312 uint32_t helper_fcmp_le(CPUMBState *env, uint32_t a, uint32_t b)
314 CPU_FloatU fa, fb;
315 int flags;
316 int r;
318 fa.l = a;
319 fb.l = b;
320 set_float_exception_flags(0, &env->fp_status);
321 r = float32_le(fa.f, fb.f, &env->fp_status);
322 flags = get_float_exception_flags(&env->fp_status);
323 update_fpu_flags(env, flags & float_flag_invalid);
326 return r;
329 uint32_t helper_fcmp_gt(CPUMBState *env, uint32_t a, uint32_t b)
331 CPU_FloatU fa, fb;
332 int flags, r;
334 fa.l = a;
335 fb.l = b;
336 set_float_exception_flags(0, &env->fp_status);
337 r = float32_lt(fa.f, fb.f, &env->fp_status);
338 flags = get_float_exception_flags(&env->fp_status);
339 update_fpu_flags(env, flags & float_flag_invalid);
340 return r;
343 uint32_t helper_fcmp_ne(CPUMBState *env, uint32_t a, uint32_t b)
345 CPU_FloatU fa, fb;
346 int flags, r;
348 fa.l = a;
349 fb.l = b;
350 set_float_exception_flags(0, &env->fp_status);
351 r = !float32_eq_quiet(fa.f, fb.f, &env->fp_status);
352 flags = get_float_exception_flags(&env->fp_status);
353 update_fpu_flags(env, flags & float_flag_invalid);
355 return r;
358 uint32_t helper_fcmp_ge(CPUMBState *env, uint32_t a, uint32_t b)
360 CPU_FloatU fa, fb;
361 int flags, r;
363 fa.l = a;
364 fb.l = b;
365 set_float_exception_flags(0, &env->fp_status);
366 r = !float32_lt(fa.f, fb.f, &env->fp_status);
367 flags = get_float_exception_flags(&env->fp_status);
368 update_fpu_flags(env, flags & float_flag_invalid);
370 return r;
373 uint32_t helper_flt(CPUMBState *env, uint32_t a)
375 CPU_FloatU fd, fa;
377 fa.l = a;
378 fd.f = int32_to_float32(fa.l, &env->fp_status);
379 return fd.l;
382 uint32_t helper_fint(CPUMBState *env, uint32_t a)
384 CPU_FloatU fa;
385 uint32_t r;
386 int flags;
388 set_float_exception_flags(0, &env->fp_status);
389 fa.l = a;
390 r = float32_to_int32(fa.f, &env->fp_status);
391 flags = get_float_exception_flags(&env->fp_status);
392 update_fpu_flags(env, flags);
394 return r;
397 uint32_t helper_fsqrt(CPUMBState *env, uint32_t a)
399 CPU_FloatU fd, fa;
400 int flags;
402 set_float_exception_flags(0, &env->fp_status);
403 fa.l = a;
404 fd.l = float32_sqrt(fa.f, &env->fp_status);
405 flags = get_float_exception_flags(&env->fp_status);
406 update_fpu_flags(env, flags);
408 return fd.l;
411 uint32_t helper_pcmpbf(uint32_t a, uint32_t b)
413 unsigned int i;
414 uint32_t mask = 0xff000000;
416 for (i = 0; i < 4; i++) {
417 if ((a & mask) == (b & mask))
418 return i + 1;
419 mask >>= 8;
421 return 0;
424 void helper_memalign(CPUMBState *env, target_ulong addr,
425 uint32_t dr, uint32_t wr,
426 uint32_t mask)
428 if (addr & mask) {
429 qemu_log_mask(CPU_LOG_INT,
430 "unaligned access addr=" TARGET_FMT_lx
431 " mask=%x, wr=%d dr=r%d\n",
432 addr, mask, wr, dr);
433 env->sregs[SR_EAR] = addr;
434 env->sregs[SR_ESR] = ESR_EC_UNALIGNED_DATA | (wr << 10) \
435 | (dr & 31) << 5;
436 if (mask == 3) {
437 env->sregs[SR_ESR] |= 1 << 11;
439 if (!(env->sregs[SR_MSR] & MSR_EE)) {
440 return;
442 helper_raise_exception(env, EXCP_HW_EXCP);
446 void helper_stackprot(CPUMBState *env, target_ulong addr)
448 if (addr < env->slr || addr > env->shr) {
449 qemu_log_mask(CPU_LOG_INT, "Stack protector violation at "
450 TARGET_FMT_lx " %x %x\n",
451 addr, env->slr, env->shr);
452 env->sregs[SR_EAR] = addr;
453 env->sregs[SR_ESR] = ESR_EC_STACKPROT;
454 helper_raise_exception(env, EXCP_HW_EXCP);
458 #if !defined(CONFIG_USER_ONLY)
459 /* Writes/reads to the MMU's special regs end up here. */
460 uint32_t helper_mmu_read(CPUMBState *env, uint32_t ext, uint32_t rn)
462 return mmu_read(env, ext, rn);
465 void helper_mmu_write(CPUMBState *env, uint32_t ext, uint32_t rn, uint32_t v)
467 mmu_write(env, ext, rn, v);
470 void mb_cpu_transaction_failed(CPUState *cs, hwaddr physaddr, vaddr addr,
471 unsigned size, MMUAccessType access_type,
472 int mmu_idx, MemTxAttrs attrs,
473 MemTxResult response, uintptr_t retaddr)
475 MicroBlazeCPU *cpu;
476 CPUMBState *env;
477 qemu_log_mask(CPU_LOG_INT, "Transaction failed: vaddr 0x%" VADDR_PRIx
478 " physaddr 0x" TARGET_FMT_plx " size %d access type %s\n",
479 addr, physaddr, size,
480 access_type == MMU_INST_FETCH ? "INST_FETCH" :
481 (access_type == MMU_DATA_LOAD ? "DATA_LOAD" : "DATA_STORE"));
482 cpu = MICROBLAZE_CPU(cs);
483 env = &cpu->env;
485 cpu_restore_state(cs, retaddr, true);
486 if (!(env->sregs[SR_MSR] & MSR_EE)) {
487 return;
490 env->sregs[SR_EAR] = addr;
491 if (access_type == MMU_INST_FETCH) {
492 if ((env->pvr.regs[2] & PVR2_IOPB_BUS_EXC_MASK)) {
493 env->sregs[SR_ESR] = ESR_EC_INSN_BUS;
494 helper_raise_exception(env, EXCP_HW_EXCP);
496 } else {
497 if ((env->pvr.regs[2] & PVR2_DOPB_BUS_EXC_MASK)) {
498 env->sregs[SR_ESR] = ESR_EC_DATA_BUS;
499 helper_raise_exception(env, EXCP_HW_EXCP);
503 #endif