Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / target-moxie / translate.c
blob58200c25d3f4656582a6d36d665da420a7e5f0ed
1 /*
2 * Moxie emulation for qemu: main translation routines.
4 * Copyright (c) 2009, 2013 Anthony Green
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* For information on the Moxie architecture, see
21 * http://moxielogic.org/wiki
24 #include "qemu/osdep.h"
26 #include "cpu.h"
27 #include "exec/exec-all.h"
28 #include "disas/disas.h"
29 #include "tcg-op.h"
30 #include "exec/cpu_ldst.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 #include "exec/log.h"
36 /* This is the state at translation time. */
37 typedef struct DisasContext {
38 struct TranslationBlock *tb;
39 target_ulong pc, saved_pc;
40 uint32_t opcode;
41 uint32_t fp_status;
42 /* Routine used to access memory */
43 int memidx;
44 int bstate;
45 target_ulong btarget;
46 int singlestep_enabled;
47 } DisasContext;
49 enum {
50 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
51 * exception condition */
52 BS_STOP = 1, /* We want to stop translation for any reason */
53 BS_BRANCH = 2, /* We reached a branch condition */
54 BS_EXCP = 3, /* We reached an exception condition */
57 static TCGv cpu_pc;
58 static TCGv cpu_gregs[16];
59 static TCGv_env cpu_env;
60 static TCGv cc_a, cc_b;
62 #include "exec/gen-icount.h"
64 #define REG(x) (cpu_gregs[x])
66 /* Extract the signed 10-bit offset from a 16-bit branch
67 instruction. */
68 static int extract_branch_offset(int opcode)
70 return (((signed short)((opcode & ((1 << 10) - 1)) << 6)) >> 6) << 1;
73 void moxie_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
74 int flags)
76 MoxieCPU *cpu = MOXIE_CPU(cs);
77 CPUMoxieState *env = &cpu->env;
78 int i;
79 cpu_fprintf(f, "pc=0x%08x\n", env->pc);
80 cpu_fprintf(f, "$fp=0x%08x $sp=0x%08x $r0=0x%08x $r1=0x%08x\n",
81 env->gregs[0], env->gregs[1], env->gregs[2], env->gregs[3]);
82 for (i = 4; i < 16; i += 4) {
83 cpu_fprintf(f, "$r%d=0x%08x $r%d=0x%08x $r%d=0x%08x $r%d=0x%08x\n",
84 i-2, env->gregs[i], i-1, env->gregs[i + 1],
85 i, env->gregs[i + 2], i+1, env->gregs[i + 3]);
87 for (i = 4; i < 16; i += 4) {
88 cpu_fprintf(f, "sr%d=0x%08x sr%d=0x%08x sr%d=0x%08x sr%d=0x%08x\n",
89 i-2, env->sregs[i], i-1, env->sregs[i + 1],
90 i, env->sregs[i + 2], i+1, env->sregs[i + 3]);
94 void moxie_translate_init(void)
96 int i;
97 static int done_init;
98 static const char * const gregnames[16] = {
99 "$fp", "$sp", "$r0", "$r1",
100 "$r2", "$r3", "$r4", "$r5",
101 "$r6", "$r7", "$r8", "$r9",
102 "$r10", "$r11", "$r12", "$r13"
105 if (done_init) {
106 return;
108 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
109 cpu_pc = tcg_global_mem_new_i32(cpu_env,
110 offsetof(CPUMoxieState, pc), "$pc");
111 for (i = 0; i < 16; i++)
112 cpu_gregs[i] = tcg_global_mem_new_i32(cpu_env,
113 offsetof(CPUMoxieState, gregs[i]),
114 gregnames[i]);
116 cc_a = tcg_global_mem_new_i32(cpu_env,
117 offsetof(CPUMoxieState, cc_a), "cc_a");
118 cc_b = tcg_global_mem_new_i32(cpu_env,
119 offsetof(CPUMoxieState, cc_b), "cc_b");
121 done_init = 1;
124 static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
126 if (unlikely(ctx->singlestep_enabled)) {
127 return false;
130 #ifndef CONFIG_USER_ONLY
131 return (ctx->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
132 #else
133 return true;
134 #endif
137 static inline void gen_goto_tb(CPUMoxieState *env, DisasContext *ctx,
138 int n, target_ulong dest)
140 if (use_goto_tb(ctx, dest)) {
141 tcg_gen_goto_tb(n);
142 tcg_gen_movi_i32(cpu_pc, dest);
143 tcg_gen_exit_tb((uintptr_t)ctx->tb + n);
144 } else {
145 tcg_gen_movi_i32(cpu_pc, dest);
146 if (ctx->singlestep_enabled) {
147 gen_helper_debug(cpu_env);
149 tcg_gen_exit_tb(0);
153 static int decode_opc(MoxieCPU *cpu, DisasContext *ctx)
155 CPUMoxieState *env = &cpu->env;
157 /* Local cache for the instruction opcode. */
158 int opcode;
159 /* Set the default instruction length. */
160 int length = 2;
162 /* Examine the 16-bit opcode. */
163 opcode = ctx->opcode;
165 /* Decode instruction. */
166 if (opcode & (1 << 15)) {
167 if (opcode & (1 << 14)) {
168 /* This is a Form 3 instruction. */
169 int inst = (opcode >> 10 & 0xf);
171 #define BRANCH(cond) \
172 do { \
173 TCGLabel *l1 = gen_new_label(); \
174 tcg_gen_brcond_i32(cond, cc_a, cc_b, l1); \
175 gen_goto_tb(env, ctx, 1, ctx->pc+2); \
176 gen_set_label(l1); \
177 gen_goto_tb(env, ctx, 0, extract_branch_offset(opcode) + ctx->pc+2); \
178 ctx->bstate = BS_BRANCH; \
179 } while (0)
181 switch (inst) {
182 case 0x00: /* beq */
183 BRANCH(TCG_COND_EQ);
184 break;
185 case 0x01: /* bne */
186 BRANCH(TCG_COND_NE);
187 break;
188 case 0x02: /* blt */
189 BRANCH(TCG_COND_LT);
190 break;
191 case 0x03: /* bgt */
192 BRANCH(TCG_COND_GT);
193 break;
194 case 0x04: /* bltu */
195 BRANCH(TCG_COND_LTU);
196 break;
197 case 0x05: /* bgtu */
198 BRANCH(TCG_COND_GTU);
199 break;
200 case 0x06: /* bge */
201 BRANCH(TCG_COND_GE);
202 break;
203 case 0x07: /* ble */
204 BRANCH(TCG_COND_LE);
205 break;
206 case 0x08: /* bgeu */
207 BRANCH(TCG_COND_GEU);
208 break;
209 case 0x09: /* bleu */
210 BRANCH(TCG_COND_LEU);
211 break;
212 default:
214 TCGv temp = tcg_temp_new_i32();
215 tcg_gen_movi_i32(cpu_pc, ctx->pc);
216 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
217 gen_helper_raise_exception(cpu_env, temp);
218 tcg_temp_free_i32(temp);
220 break;
222 } else {
223 /* This is a Form 2 instruction. */
224 int inst = (opcode >> 12 & 0x3);
225 switch (inst) {
226 case 0x00: /* inc */
228 int a = (opcode >> 8) & 0xf;
229 unsigned int v = (opcode & 0xff);
230 tcg_gen_addi_i32(REG(a), REG(a), v);
232 break;
233 case 0x01: /* dec */
235 int a = (opcode >> 8) & 0xf;
236 unsigned int v = (opcode & 0xff);
237 tcg_gen_subi_i32(REG(a), REG(a), v);
239 break;
240 case 0x02: /* gsr */
242 int a = (opcode >> 8) & 0xf;
243 unsigned v = (opcode & 0xff);
244 tcg_gen_ld_i32(REG(a), cpu_env,
245 offsetof(CPUMoxieState, sregs[v]));
247 break;
248 case 0x03: /* ssr */
250 int a = (opcode >> 8) & 0xf;
251 unsigned v = (opcode & 0xff);
252 tcg_gen_st_i32(REG(a), cpu_env,
253 offsetof(CPUMoxieState, sregs[v]));
255 break;
256 default:
258 TCGv temp = tcg_temp_new_i32();
259 tcg_gen_movi_i32(cpu_pc, ctx->pc);
260 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
261 gen_helper_raise_exception(cpu_env, temp);
262 tcg_temp_free_i32(temp);
264 break;
267 } else {
268 /* This is a Form 1 instruction. */
269 int inst = opcode >> 8;
270 switch (inst) {
271 case 0x00: /* nop */
272 break;
273 case 0x01: /* ldi.l (immediate) */
275 int reg = (opcode >> 4) & 0xf;
276 int val = cpu_ldl_code(env, ctx->pc+2);
277 tcg_gen_movi_i32(REG(reg), val);
278 length = 6;
280 break;
281 case 0x02: /* mov (register-to-register) */
283 int dest = (opcode >> 4) & 0xf;
284 int src = opcode & 0xf;
285 tcg_gen_mov_i32(REG(dest), REG(src));
287 break;
288 case 0x03: /* jsra */
290 TCGv t1 = tcg_temp_new_i32();
291 TCGv t2 = tcg_temp_new_i32();
293 tcg_gen_movi_i32(t1, ctx->pc + 6);
295 /* Make space for the static chain and return address. */
296 tcg_gen_subi_i32(t2, REG(1), 8);
297 tcg_gen_mov_i32(REG(1), t2);
298 tcg_gen_qemu_st32(t1, REG(1), ctx->memidx);
300 /* Push the current frame pointer. */
301 tcg_gen_subi_i32(t2, REG(1), 4);
302 tcg_gen_mov_i32(REG(1), t2);
303 tcg_gen_qemu_st32(REG(0), REG(1), ctx->memidx);
305 /* Set the pc and $fp. */
306 tcg_gen_mov_i32(REG(0), REG(1));
308 gen_goto_tb(env, ctx, 0, cpu_ldl_code(env, ctx->pc+2));
310 tcg_temp_free_i32(t1);
311 tcg_temp_free_i32(t2);
313 ctx->bstate = BS_BRANCH;
314 length = 6;
316 break;
317 case 0x04: /* ret */
319 TCGv t1 = tcg_temp_new_i32();
321 /* The new $sp is the old $fp. */
322 tcg_gen_mov_i32(REG(1), REG(0));
324 /* Pop the frame pointer. */
325 tcg_gen_qemu_ld32u(REG(0), REG(1), ctx->memidx);
326 tcg_gen_addi_i32(t1, REG(1), 4);
327 tcg_gen_mov_i32(REG(1), t1);
330 /* Pop the return address and skip over the static chain
331 slot. */
332 tcg_gen_qemu_ld32u(cpu_pc, REG(1), ctx->memidx);
333 tcg_gen_addi_i32(t1, REG(1), 8);
334 tcg_gen_mov_i32(REG(1), t1);
336 tcg_temp_free_i32(t1);
338 /* Jump... */
339 tcg_gen_exit_tb(0);
341 ctx->bstate = BS_BRANCH;
343 break;
344 case 0x05: /* add.l */
346 int a = (opcode >> 4) & 0xf;
347 int b = opcode & 0xf;
349 tcg_gen_add_i32(REG(a), REG(a), REG(b));
351 break;
352 case 0x06: /* push */
354 int a = (opcode >> 4) & 0xf;
355 int b = opcode & 0xf;
357 TCGv t1 = tcg_temp_new_i32();
358 tcg_gen_subi_i32(t1, REG(a), 4);
359 tcg_gen_mov_i32(REG(a), t1);
360 tcg_gen_qemu_st32(REG(b), REG(a), ctx->memidx);
361 tcg_temp_free_i32(t1);
363 break;
364 case 0x07: /* pop */
366 int a = (opcode >> 4) & 0xf;
367 int b = opcode & 0xf;
368 TCGv t1 = tcg_temp_new_i32();
370 tcg_gen_qemu_ld32u(REG(b), REG(a), ctx->memidx);
371 tcg_gen_addi_i32(t1, REG(a), 4);
372 tcg_gen_mov_i32(REG(a), t1);
373 tcg_temp_free_i32(t1);
375 break;
376 case 0x08: /* lda.l */
378 int reg = (opcode >> 4) & 0xf;
380 TCGv ptr = tcg_temp_new_i32();
381 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
382 tcg_gen_qemu_ld32u(REG(reg), ptr, ctx->memidx);
383 tcg_temp_free_i32(ptr);
385 length = 6;
387 break;
388 case 0x09: /* sta.l */
390 int val = (opcode >> 4) & 0xf;
392 TCGv ptr = tcg_temp_new_i32();
393 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
394 tcg_gen_qemu_st32(REG(val), ptr, ctx->memidx);
395 tcg_temp_free_i32(ptr);
397 length = 6;
399 break;
400 case 0x0a: /* ld.l (register indirect) */
402 int src = opcode & 0xf;
403 int dest = (opcode >> 4) & 0xf;
405 tcg_gen_qemu_ld32u(REG(dest), REG(src), ctx->memidx);
407 break;
408 case 0x0b: /* st.l */
410 int dest = (opcode >> 4) & 0xf;
411 int val = opcode & 0xf;
413 tcg_gen_qemu_st32(REG(val), REG(dest), ctx->memidx);
415 break;
416 case 0x0c: /* ldo.l */
418 int a = (opcode >> 4) & 0xf;
419 int b = opcode & 0xf;
421 TCGv t1 = tcg_temp_new_i32();
422 TCGv t2 = tcg_temp_new_i32();
423 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
424 tcg_gen_qemu_ld32u(t2, t1, ctx->memidx);
425 tcg_gen_mov_i32(REG(a), t2);
427 tcg_temp_free_i32(t1);
428 tcg_temp_free_i32(t2);
430 length = 6;
432 break;
433 case 0x0d: /* sto.l */
435 int a = (opcode >> 4) & 0xf;
436 int b = opcode & 0xf;
438 TCGv t1 = tcg_temp_new_i32();
439 TCGv t2 = tcg_temp_new_i32();
440 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
441 tcg_gen_qemu_st32(REG(b), t1, ctx->memidx);
443 tcg_temp_free_i32(t1);
444 tcg_temp_free_i32(t2);
446 length = 6;
448 break;
449 case 0x0e: /* cmp */
451 int a = (opcode >> 4) & 0xf;
452 int b = opcode & 0xf;
454 tcg_gen_mov_i32(cc_a, REG(a));
455 tcg_gen_mov_i32(cc_b, REG(b));
457 break;
458 case 0x19: /* jsr */
460 int fnreg = (opcode >> 4) & 0xf;
462 /* Load the stack pointer into T0. */
463 TCGv t1 = tcg_temp_new_i32();
464 TCGv t2 = tcg_temp_new_i32();
466 tcg_gen_movi_i32(t1, ctx->pc+2);
468 /* Make space for the static chain and return address. */
469 tcg_gen_subi_i32(t2, REG(1), 8);
470 tcg_gen_mov_i32(REG(1), t2);
471 tcg_gen_qemu_st32(t1, REG(1), ctx->memidx);
473 /* Push the current frame pointer. */
474 tcg_gen_subi_i32(t2, REG(1), 4);
475 tcg_gen_mov_i32(REG(1), t2);
476 tcg_gen_qemu_st32(REG(0), REG(1), ctx->memidx);
478 /* Set the pc and $fp. */
479 tcg_gen_mov_i32(REG(0), REG(1));
480 tcg_gen_mov_i32(cpu_pc, REG(fnreg));
481 tcg_temp_free_i32(t1);
482 tcg_temp_free_i32(t2);
483 tcg_gen_exit_tb(0);
484 ctx->bstate = BS_BRANCH;
486 break;
487 case 0x1a: /* jmpa */
489 tcg_gen_movi_i32(cpu_pc, cpu_ldl_code(env, ctx->pc+2));
490 tcg_gen_exit_tb(0);
491 ctx->bstate = BS_BRANCH;
492 length = 6;
494 break;
495 case 0x1b: /* ldi.b (immediate) */
497 int reg = (opcode >> 4) & 0xf;
498 int val = cpu_ldl_code(env, ctx->pc+2);
499 tcg_gen_movi_i32(REG(reg), val);
500 length = 6;
502 break;
503 case 0x1c: /* ld.b (register indirect) */
505 int src = opcode & 0xf;
506 int dest = (opcode >> 4) & 0xf;
508 tcg_gen_qemu_ld8u(REG(dest), REG(src), ctx->memidx);
510 break;
511 case 0x1d: /* lda.b */
513 int reg = (opcode >> 4) & 0xf;
515 TCGv ptr = tcg_temp_new_i32();
516 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
517 tcg_gen_qemu_ld8u(REG(reg), ptr, ctx->memidx);
518 tcg_temp_free_i32(ptr);
520 length = 6;
522 break;
523 case 0x1e: /* st.b */
525 int dest = (opcode >> 4) & 0xf;
526 int val = opcode & 0xf;
528 tcg_gen_qemu_st8(REG(val), REG(dest), ctx->memidx);
530 break;
531 case 0x1f: /* sta.b */
533 int val = (opcode >> 4) & 0xf;
535 TCGv ptr = tcg_temp_new_i32();
536 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
537 tcg_gen_qemu_st8(REG(val), ptr, ctx->memidx);
538 tcg_temp_free_i32(ptr);
540 length = 6;
542 break;
543 case 0x20: /* ldi.s (immediate) */
545 int reg = (opcode >> 4) & 0xf;
546 int val = cpu_ldl_code(env, ctx->pc+2);
547 tcg_gen_movi_i32(REG(reg), val);
548 length = 6;
550 break;
551 case 0x21: /* ld.s (register indirect) */
553 int src = opcode & 0xf;
554 int dest = (opcode >> 4) & 0xf;
556 tcg_gen_qemu_ld16u(REG(dest), REG(src), ctx->memidx);
558 break;
559 case 0x22: /* lda.s */
561 int reg = (opcode >> 4) & 0xf;
563 TCGv ptr = tcg_temp_new_i32();
564 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
565 tcg_gen_qemu_ld16u(REG(reg), ptr, ctx->memidx);
566 tcg_temp_free_i32(ptr);
568 length = 6;
570 break;
571 case 0x23: /* st.s */
573 int dest = (opcode >> 4) & 0xf;
574 int val = opcode & 0xf;
576 tcg_gen_qemu_st16(REG(val), REG(dest), ctx->memidx);
578 break;
579 case 0x24: /* sta.s */
581 int val = (opcode >> 4) & 0xf;
583 TCGv ptr = tcg_temp_new_i32();
584 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
585 tcg_gen_qemu_st16(REG(val), ptr, ctx->memidx);
586 tcg_temp_free_i32(ptr);
588 length = 6;
590 break;
591 case 0x25: /* jmp */
593 int reg = (opcode >> 4) & 0xf;
594 tcg_gen_mov_i32(cpu_pc, REG(reg));
595 tcg_gen_exit_tb(0);
596 ctx->bstate = BS_BRANCH;
598 break;
599 case 0x26: /* and */
601 int a = (opcode >> 4) & 0xf;
602 int b = opcode & 0xf;
604 tcg_gen_and_i32(REG(a), REG(a), REG(b));
606 break;
607 case 0x27: /* lshr */
609 int a = (opcode >> 4) & 0xf;
610 int b = opcode & 0xf;
612 TCGv sv = tcg_temp_new_i32();
613 tcg_gen_andi_i32(sv, REG(b), 0x1f);
614 tcg_gen_shr_i32(REG(a), REG(a), sv);
615 tcg_temp_free_i32(sv);
617 break;
618 case 0x28: /* ashl */
620 int a = (opcode >> 4) & 0xf;
621 int b = opcode & 0xf;
623 TCGv sv = tcg_temp_new_i32();
624 tcg_gen_andi_i32(sv, REG(b), 0x1f);
625 tcg_gen_shl_i32(REG(a), REG(a), sv);
626 tcg_temp_free_i32(sv);
628 break;
629 case 0x29: /* sub.l */
631 int a = (opcode >> 4) & 0xf;
632 int b = opcode & 0xf;
634 tcg_gen_sub_i32(REG(a), REG(a), REG(b));
636 break;
637 case 0x2a: /* neg */
639 int a = (opcode >> 4) & 0xf;
640 int b = opcode & 0xf;
642 tcg_gen_neg_i32(REG(a), REG(b));
644 break;
645 case 0x2b: /* or */
647 int a = (opcode >> 4) & 0xf;
648 int b = opcode & 0xf;
650 tcg_gen_or_i32(REG(a), REG(a), REG(b));
652 break;
653 case 0x2c: /* not */
655 int a = (opcode >> 4) & 0xf;
656 int b = opcode & 0xf;
658 tcg_gen_not_i32(REG(a), REG(b));
660 break;
661 case 0x2d: /* ashr */
663 int a = (opcode >> 4) & 0xf;
664 int b = opcode & 0xf;
666 TCGv sv = tcg_temp_new_i32();
667 tcg_gen_andi_i32(sv, REG(b), 0x1f);
668 tcg_gen_sar_i32(REG(a), REG(a), sv);
669 tcg_temp_free_i32(sv);
671 break;
672 case 0x2e: /* xor */
674 int a = (opcode >> 4) & 0xf;
675 int b = opcode & 0xf;
677 tcg_gen_xor_i32(REG(a), REG(a), REG(b));
679 break;
680 case 0x2f: /* mul.l */
682 int a = (opcode >> 4) & 0xf;
683 int b = opcode & 0xf;
685 tcg_gen_mul_i32(REG(a), REG(a), REG(b));
687 break;
688 case 0x30: /* swi */
690 int val = cpu_ldl_code(env, ctx->pc+2);
692 TCGv temp = tcg_temp_new_i32();
693 tcg_gen_movi_i32(temp, val);
694 tcg_gen_st_i32(temp, cpu_env,
695 offsetof(CPUMoxieState, sregs[3]));
696 tcg_gen_movi_i32(cpu_pc, ctx->pc);
697 tcg_gen_movi_i32(temp, MOXIE_EX_SWI);
698 gen_helper_raise_exception(cpu_env, temp);
699 tcg_temp_free_i32(temp);
701 length = 6;
703 break;
704 case 0x31: /* div.l */
706 int a = (opcode >> 4) & 0xf;
707 int b = opcode & 0xf;
708 tcg_gen_movi_i32(cpu_pc, ctx->pc);
709 gen_helper_div(REG(a), cpu_env, REG(a), REG(b));
711 break;
712 case 0x32: /* udiv.l */
714 int a = (opcode >> 4) & 0xf;
715 int b = opcode & 0xf;
716 tcg_gen_movi_i32(cpu_pc, ctx->pc);
717 gen_helper_udiv(REG(a), cpu_env, REG(a), REG(b));
719 break;
720 case 0x33: /* mod.l */
722 int a = (opcode >> 4) & 0xf;
723 int b = opcode & 0xf;
724 tcg_gen_rem_i32(REG(a), REG(a), REG(b));
726 break;
727 case 0x34: /* umod.l */
729 int a = (opcode >> 4) & 0xf;
730 int b = opcode & 0xf;
731 tcg_gen_remu_i32(REG(a), REG(a), REG(b));
733 break;
734 case 0x35: /* brk */
736 TCGv temp = tcg_temp_new_i32();
737 tcg_gen_movi_i32(cpu_pc, ctx->pc);
738 tcg_gen_movi_i32(temp, MOXIE_EX_BREAK);
739 gen_helper_raise_exception(cpu_env, temp);
740 tcg_temp_free_i32(temp);
742 break;
743 case 0x36: /* ldo.b */
745 int a = (opcode >> 4) & 0xf;
746 int b = opcode & 0xf;
748 TCGv t1 = tcg_temp_new_i32();
749 TCGv t2 = tcg_temp_new_i32();
750 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
751 tcg_gen_qemu_ld8u(t2, t1, ctx->memidx);
752 tcg_gen_mov_i32(REG(a), t2);
754 tcg_temp_free_i32(t1);
755 tcg_temp_free_i32(t2);
757 length = 6;
759 break;
760 case 0x37: /* sto.b */
762 int a = (opcode >> 4) & 0xf;
763 int b = opcode & 0xf;
765 TCGv t1 = tcg_temp_new_i32();
766 TCGv t2 = tcg_temp_new_i32();
767 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
768 tcg_gen_qemu_st8(REG(b), t1, ctx->memidx);
770 tcg_temp_free_i32(t1);
771 tcg_temp_free_i32(t2);
773 length = 6;
775 break;
776 case 0x38: /* ldo.s */
778 int a = (opcode >> 4) & 0xf;
779 int b = opcode & 0xf;
781 TCGv t1 = tcg_temp_new_i32();
782 TCGv t2 = tcg_temp_new_i32();
783 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
784 tcg_gen_qemu_ld16u(t2, t1, ctx->memidx);
785 tcg_gen_mov_i32(REG(a), t2);
787 tcg_temp_free_i32(t1);
788 tcg_temp_free_i32(t2);
790 length = 6;
792 break;
793 case 0x39: /* sto.s */
795 int a = (opcode >> 4) & 0xf;
796 int b = opcode & 0xf;
798 TCGv t1 = tcg_temp_new_i32();
799 TCGv t2 = tcg_temp_new_i32();
800 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
801 tcg_gen_qemu_st16(REG(b), t1, ctx->memidx);
802 tcg_temp_free_i32(t1);
803 tcg_temp_free_i32(t2);
805 length = 6;
807 break;
808 default:
810 TCGv temp = tcg_temp_new_i32();
811 tcg_gen_movi_i32(cpu_pc, ctx->pc);
812 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
813 gen_helper_raise_exception(cpu_env, temp);
814 tcg_temp_free_i32(temp);
816 break;
820 return length;
823 /* generate intermediate code for basic block 'tb'. */
824 void gen_intermediate_code(CPUMoxieState *env, struct TranslationBlock *tb)
826 MoxieCPU *cpu = moxie_env_get_cpu(env);
827 CPUState *cs = CPU(cpu);
828 DisasContext ctx;
829 target_ulong pc_start;
830 int num_insns, max_insns;
832 pc_start = tb->pc;
833 ctx.pc = pc_start;
834 ctx.saved_pc = -1;
835 ctx.tb = tb;
836 ctx.memidx = 0;
837 ctx.singlestep_enabled = 0;
838 ctx.bstate = BS_NONE;
839 num_insns = 0;
840 max_insns = tb->cflags & CF_COUNT_MASK;
841 if (max_insns == 0) {
842 max_insns = CF_COUNT_MASK;
844 if (max_insns > TCG_MAX_INSNS) {
845 max_insns = TCG_MAX_INSNS;
848 gen_tb_start(tb);
849 do {
850 tcg_gen_insn_start(ctx.pc);
851 num_insns++;
853 if (unlikely(cpu_breakpoint_test(cs, ctx.pc, BP_ANY))) {
854 tcg_gen_movi_i32(cpu_pc, ctx.pc);
855 gen_helper_debug(cpu_env);
856 ctx.bstate = BS_EXCP;
857 /* The address covered by the breakpoint must be included in
858 [tb->pc, tb->pc + tb->size) in order to for it to be
859 properly cleared -- thus we increment the PC here so that
860 the logic setting tb->size below does the right thing. */
861 ctx.pc += 2;
862 goto done_generating;
865 ctx.opcode = cpu_lduw_code(env, ctx.pc);
866 ctx.pc += decode_opc(cpu, &ctx);
868 if (num_insns >= max_insns) {
869 break;
871 if (cs->singlestep_enabled) {
872 break;
874 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0) {
875 break;
877 } while (ctx.bstate == BS_NONE && !tcg_op_buf_full());
879 if (cs->singlestep_enabled) {
880 tcg_gen_movi_tl(cpu_pc, ctx.pc);
881 gen_helper_debug(cpu_env);
882 } else {
883 switch (ctx.bstate) {
884 case BS_STOP:
885 case BS_NONE:
886 gen_goto_tb(env, &ctx, 0, ctx.pc);
887 break;
888 case BS_EXCP:
889 tcg_gen_exit_tb(0);
890 break;
891 case BS_BRANCH:
892 default:
893 break;
896 done_generating:
897 gen_tb_end(tb, num_insns);
899 tb->size = ctx.pc - pc_start;
900 tb->icount = num_insns;
903 void restore_state_to_opc(CPUMoxieState *env, TranslationBlock *tb,
904 target_ulong *data)
906 env->pc = data[0];