2 * CRISv10 emulation for qemu: main translation routines.
4 * Copyright (c) 2010 AXIS Communications AB
5 * Written by Edgar E. Iglesias.
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.1 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 "crisv10-decode.h"
24 static const char *regnames_v10[] =
26 "$r0", "$r1", "$r2", "$r3",
27 "$r4", "$r5", "$r6", "$r7",
28 "$r8", "$r9", "$r10", "$r11",
29 "$r12", "$r13", "$sp", "$pc",
32 static const char *pregnames_v10[] =
34 "$bz", "$vr", "$p2", "$p3",
35 "$wz", "$ccr", "$p6-prefix", "$mof",
36 "$dz", "$ibr", "$irp", "$srp",
37 "$bar", "$dccr", "$brp", "$usp",
40 /* We need this table to handle preg-moves with implicit width. */
41 static int preg_sizes_v10[] = {
52 static inline int dec10_size(unsigned int size)
60 static inline void cris_illegal_insn(DisasContext *dc)
62 qemu_log_mask(LOG_GUEST_ERROR, "illegal insn at pc=%x\n", dc->pc);
63 t_gen_raise_exception(EXCP_BREAK);
66 static void gen_store_v10_conditional(DisasContext *dc, TCGv addr, TCGv val,
67 unsigned int size, int mem_index)
69 TCGLabel *l1 = gen_new_label();
70 TCGv taddr = tcg_temp_local_new();
71 TCGv tval = tcg_temp_local_new();
72 TCGv t1 = tcg_temp_local_new();
74 cris_evaluate_flags(dc);
76 tcg_gen_mov_tl(taddr, addr);
77 tcg_gen_mov_tl(tval, val);
79 /* Store only if F flag isn't set */
80 tcg_gen_andi_tl(t1, cpu_PR[PR_CCS], F_FLAG_V10);
81 tcg_gen_brcondi_tl(TCG_COND_NE, t1, 0, l1);
83 tcg_gen_qemu_st8(tval, taddr, mem_index);
84 } else if (size == 2) {
85 tcg_gen_qemu_st16(tval, taddr, mem_index);
87 tcg_gen_qemu_st32(tval, taddr, mem_index);
90 tcg_gen_shri_tl(t1, t1, 1); /* shift F to P position */
91 tcg_gen_or_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], t1); /*P=F*/
97 static void gen_store_v10(DisasContext *dc, TCGv addr, TCGv val,
100 int mem_index = cpu_mmu_index(&dc->cpu->env, false);
102 /* If we get a fault on a delayslot we must keep the jmp state in
103 the cpu-state to be able to re-execute the jmp. */
104 if (dc->delayed_branch == 1) {
105 cris_store_direct_jmp(dc);
108 /* Conditional writes. We only support the kind were X is known
109 at translation time. */
110 if (dc->flagx_known && dc->flags_x) {
111 gen_store_v10_conditional(dc, addr, val, size, mem_index);
116 tcg_gen_qemu_st8(val, addr, mem_index);
117 } else if (size == 2) {
118 tcg_gen_qemu_st16(val, addr, mem_index);
120 tcg_gen_qemu_st32(val, addr, mem_index);
125 /* Prefix flag and register are used to handle the more complex
127 static void cris_set_prefix(DisasContext *dc)
129 dc->clear_prefix = 0;
130 dc->tb_flags |= PFIX_FLAG;
131 tcg_gen_ori_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], PFIX_FLAG);
133 /* prefix insns don't clear the x flag. */
138 static void crisv10_prepare_memaddr(DisasContext *dc,
139 TCGv addr, unsigned int size)
141 if (dc->tb_flags & PFIX_FLAG) {
142 tcg_gen_mov_tl(addr, cpu_PR[PR_PREFIX]);
144 tcg_gen_mov_tl(addr, cpu_R[dc->src]);
148 static unsigned int crisv10_post_memaddr(DisasContext *dc, unsigned int size)
150 unsigned int insn_len = 0;
152 if (dc->tb_flags & PFIX_FLAG) {
153 if (dc->mode == CRISV10_MODE_AUTOINC) {
154 tcg_gen_mov_tl(cpu_R[dc->src], cpu_PR[PR_PREFIX]);
157 if (dc->mode == CRISV10_MODE_AUTOINC) {
159 insn_len += size & ~1;
161 tcg_gen_addi_tl(cpu_R[dc->src], cpu_R[dc->src], size);
168 static int dec10_prep_move_m(CPUCRISState *env, DisasContext *dc,
169 int s_ext, int memsize, TCGv dst)
177 is_imm = rs == 15 && !(dc->tb_flags & PFIX_FLAG);
178 LOG_DIS("rs=%d rd=%d is_imm=%d mode=%d pfix=%d\n",
179 rs, dc->dst, is_imm, dc->mode, dc->tb_flags & PFIX_FLAG);
181 /* Load [$rs] onto T1. */
186 imm = cpu_ldsb_code(env, dc->pc + 2);
188 imm = cpu_ldsw_code(env, dc->pc + 2);
191 imm = cpu_ldub_code(env, dc->pc + 2);
193 imm = cpu_lduw_code(env, dc->pc + 2);
196 imm = cpu_ldl_code(env, dc->pc + 2);
198 tcg_gen_movi_tl(dst, imm);
200 if (dc->mode == CRISV10_MODE_AUTOINC) {
204 tcg_gen_addi_tl(cpu_R[15], cpu_R[15], insn_len);
209 addr = tcg_temp_new();
210 cris_flush_cc_state(dc);
211 crisv10_prepare_memaddr(dc, addr, memsize);
212 gen_load(dc, dst, addr, memsize, 0);
214 t_gen_sext(dst, dst, memsize);
216 t_gen_zext(dst, dst, memsize);
217 insn_len += crisv10_post_memaddr(dc, memsize);
221 if (dc->mode == CRISV10_MODE_INDIRECT && (dc->tb_flags & PFIX_FLAG)) {
227 static unsigned int dec10_quick_imm(DisasContext *dc)
234 imm = dc->ir & ((1 << 6) - 1);
235 simm = (int8_t) (imm << 2);
237 switch (dc->opcode) {
238 case CRISV10_QIMM_BDAP_R0:
239 case CRISV10_QIMM_BDAP_R1:
240 case CRISV10_QIMM_BDAP_R2:
241 case CRISV10_QIMM_BDAP_R3:
242 simm = (int8_t)dc->ir;
243 LOG_DIS("bdap %d $r%d\n", simm, dc->dst);
244 LOG_DIS("pc=%x mode=%x quickimm %d r%d r%d\n",
245 dc->pc, dc->mode, dc->opcode, dc->src, dc->dst);
248 tcg_gen_movi_tl(cpu_PR[PR_PREFIX], dc->pc + 2 + simm);
250 tcg_gen_addi_tl(cpu_PR[PR_PREFIX], cpu_R[dc->dst], simm);
254 case CRISV10_QIMM_MOVEQ:
255 LOG_DIS("moveq %d, $r%d\n", simm, dc->dst);
257 cris_cc_mask(dc, CC_MASK_NZVC);
258 c = tcg_const_tl(simm);
259 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->dst],
260 cpu_R[dc->dst], c, 4);
263 case CRISV10_QIMM_CMPQ:
264 LOG_DIS("cmpq %d, $r%d\n", simm, dc->dst);
266 cris_cc_mask(dc, CC_MASK_NZVC);
267 c = tcg_const_tl(simm);
268 cris_alu(dc, CC_OP_CMP, cpu_R[dc->dst],
269 cpu_R[dc->dst], c, 4);
272 case CRISV10_QIMM_ADDQ:
273 LOG_DIS("addq %d, $r%d\n", imm, dc->dst);
275 cris_cc_mask(dc, CC_MASK_NZVC);
276 c = tcg_const_tl(imm);
277 cris_alu(dc, CC_OP_ADD, cpu_R[dc->dst],
278 cpu_R[dc->dst], c, 4);
281 case CRISV10_QIMM_ANDQ:
282 LOG_DIS("andq %d, $r%d\n", simm, dc->dst);
284 cris_cc_mask(dc, CC_MASK_NZVC);
285 c = tcg_const_tl(simm);
286 cris_alu(dc, CC_OP_AND, cpu_R[dc->dst],
287 cpu_R[dc->dst], c, 4);
290 case CRISV10_QIMM_ASHQ:
291 LOG_DIS("ashq %d, $r%d\n", simm, dc->dst);
293 cris_cc_mask(dc, CC_MASK_NZVC);
296 c = tcg_const_tl(imm);
298 cris_alu(dc, CC_OP_ASR, cpu_R[dc->dst],
299 cpu_R[dc->dst], c, 4);
302 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
303 gen_helper_btst(cpu_PR[PR_CCS], cpu_env, cpu_R[dc->dst],
308 case CRISV10_QIMM_LSHQ:
309 LOG_DIS("lshq %d, $r%d\n", simm, dc->dst);
312 if (imm & (1 << 5)) {
316 cris_cc_mask(dc, CC_MASK_NZVC);
317 c = tcg_const_tl(imm);
318 cris_alu(dc, op, cpu_R[dc->dst],
319 cpu_R[dc->dst], c, 4);
322 case CRISV10_QIMM_SUBQ:
323 LOG_DIS("subq %d, $r%d\n", imm, dc->dst);
325 cris_cc_mask(dc, CC_MASK_NZVC);
326 c = tcg_const_tl(imm);
327 cris_alu(dc, CC_OP_SUB, cpu_R[dc->dst],
328 cpu_R[dc->dst], c, 4);
331 case CRISV10_QIMM_ORQ:
332 LOG_DIS("andq %d, $r%d\n", simm, dc->dst);
334 cris_cc_mask(dc, CC_MASK_NZVC);
335 c = tcg_const_tl(simm);
336 cris_alu(dc, CC_OP_OR, cpu_R[dc->dst],
337 cpu_R[dc->dst], c, 4);
341 case CRISV10_QIMM_BCC_R0:
342 case CRISV10_QIMM_BCC_R1:
343 case CRISV10_QIMM_BCC_R2:
344 case CRISV10_QIMM_BCC_R3:
346 /* bit 0 is a sign bit. */
348 imm |= 0xffffff00; /* sign extend. */
349 imm &= ~1; /* get rid of the sign bit. */
352 LOG_DIS("b%s %d\n", cc_name(dc->cond), imm);
355 cris_prepare_cc_branch(dc, imm, dc->cond);
359 LOG_DIS("pc=%x mode=%x quickimm %d r%d r%d\n",
360 dc->pc, dc->mode, dc->opcode, dc->src, dc->dst);
361 cpu_abort(CPU(dc->cpu), "Unhandled quickimm\n");
367 static unsigned int dec10_setclrf(DisasContext *dc)
370 unsigned int set = ~dc->opcode & 1;
372 flags = EXTRACT_FIELD(dc->ir, 0, 3)
373 | (EXTRACT_FIELD(dc->ir, 12, 15) << 4);
374 LOG_DIS("%s set=%d flags=%x\n", __func__, set, flags);
377 if (flags & X_FLAG) {
380 dc->flags_x = X_FLAG;
385 cris_evaluate_flags (dc);
386 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
387 cris_update_cc_x(dc);
388 tcg_gen_movi_tl(cc_op, dc->cc_op);
391 tcg_gen_ori_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], flags);
393 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS],
394 ~(flags|F_FLAG_V10|P_FLAG_V10));
397 dc->flags_uptodate = 1;
403 static inline void dec10_reg_prep_sext(DisasContext *dc, int size, int sext,
404 TCGv dd, TCGv ds, TCGv sd, TCGv ss)
407 t_gen_sext(dd, sd, size);
408 t_gen_sext(ds, ss, size);
410 t_gen_zext(dd, sd, size);
411 t_gen_zext(ds, ss, size);
415 static void dec10_reg_alu(DisasContext *dc, int op, int size, int sext)
419 t[0] = tcg_temp_new();
420 t[1] = tcg_temp_new();
421 dec10_reg_prep_sext(dc, size, sext,
422 t[0], t[1], cpu_R[dc->dst], cpu_R[dc->src]);
424 if (op == CC_OP_LSL || op == CC_OP_LSR || op == CC_OP_ASR) {
425 tcg_gen_andi_tl(t[1], t[1], 63);
428 assert(dc->dst != 15);
429 cris_alu(dc, op, cpu_R[dc->dst], t[0], t[1], size);
434 static void dec10_reg_bound(DisasContext *dc, int size)
438 t = tcg_temp_local_new();
439 t_gen_zext(t, cpu_R[dc->src], size);
440 cris_alu(dc, CC_OP_BOUND, cpu_R[dc->dst], cpu_R[dc->dst], t, 4);
444 static void dec10_reg_mul(DisasContext *dc, int size, int sext)
446 int op = sext ? CC_OP_MULS : CC_OP_MULU;
449 t[0] = tcg_temp_new();
450 t[1] = tcg_temp_new();
451 dec10_reg_prep_sext(dc, size, sext,
452 t[0], t[1], cpu_R[dc->dst], cpu_R[dc->src]);
454 cris_alu(dc, op, cpu_R[dc->dst], t[0], t[1], 4);
461 static void dec10_reg_movs(DisasContext *dc)
463 int size = (dc->size & 1) + 1;
466 LOG_DIS("movx.%d $r%d, $r%d\n", size, dc->src, dc->dst);
467 cris_cc_mask(dc, CC_MASK_NZVC);
471 t_gen_sext(t, cpu_R[dc->src], size);
473 t_gen_zext(t, cpu_R[dc->src], size);
475 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->dst], cpu_R[dc->dst], t, 4);
479 static void dec10_reg_alux(DisasContext *dc, int op)
481 int size = (dc->size & 1) + 1;
484 LOG_DIS("movx.%d $r%d, $r%d\n", size, dc->src, dc->dst);
485 cris_cc_mask(dc, CC_MASK_NZVC);
489 t_gen_sext(t, cpu_R[dc->src], size);
491 t_gen_zext(t, cpu_R[dc->src], size);
493 cris_alu(dc, op, cpu_R[dc->dst], cpu_R[dc->dst], t, 4);
497 static void dec10_reg_mov_pr(DisasContext *dc)
499 LOG_DIS("move p%d r%d sz=%d\n", dc->dst, dc->src, preg_sizes_v10[dc->dst]);
502 tcg_gen_mov_tl(env_btarget, cpu_PR[dc->dst]);
503 cris_prepare_jmp(dc, JMP_INDIRECT);
506 if (dc->dst == PR_CCS) {
507 cris_evaluate_flags(dc);
509 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->src],
510 cpu_R[dc->src], cpu_PR[dc->dst], preg_sizes_v10[dc->dst]);
513 static void dec10_reg_abs(DisasContext *dc)
517 LOG_DIS("abs $r%u, $r%u\n", dc->src, dc->dst);
519 assert(dc->dst != 15);
521 tcg_gen_sari_tl(t0, cpu_R[dc->src], 31);
522 tcg_gen_xor_tl(cpu_R[dc->dst], cpu_R[dc->src], t0);
523 tcg_gen_sub_tl(t0, cpu_R[dc->dst], t0);
525 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->dst], cpu_R[dc->dst], t0, 4);
529 static void dec10_reg_swap(DisasContext *dc)
533 LOG_DIS("not $r%d, $r%d\n", dc->src, dc->dst);
535 cris_cc_mask(dc, CC_MASK_NZVC);
537 tcg_gen_mov_tl(t0, cpu_R[dc->src]);
539 tcg_gen_not_tl(t0, t0);
546 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->src], cpu_R[dc->src], t0, 4);
550 static void dec10_reg_scc(DisasContext *dc)
554 LOG_DIS("s%s $r%u\n", cc_name(cond), dc->src);
556 gen_tst_cc(dc, cpu_R[dc->src], cond);
557 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_R[dc->src], cpu_R[dc->src], 0);
562 static unsigned int dec10_reg(DisasContext *dc)
565 unsigned int insn_len = 2;
566 unsigned int size = dec10_size(dc->size);
570 switch (dc->opcode) {
571 case CRISV10_REG_MOVE_R:
572 LOG_DIS("move.%d $r%d, $r%d\n", dc->size, dc->src, dc->dst);
573 cris_cc_mask(dc, CC_MASK_NZVC);
574 dec10_reg_alu(dc, CC_OP_MOVE, size, 0);
576 tcg_gen_mov_tl(env_btarget, cpu_R[dc->dst]);
577 cris_prepare_jmp(dc, JMP_INDIRECT);
578 dc->delayed_branch = 1;
581 case CRISV10_REG_MOVX:
582 cris_cc_mask(dc, CC_MASK_NZVC);
585 case CRISV10_REG_ADDX:
586 cris_cc_mask(dc, CC_MASK_NZVC);
587 dec10_reg_alux(dc, CC_OP_ADD);
589 case CRISV10_REG_SUBX:
590 cris_cc_mask(dc, CC_MASK_NZVC);
591 dec10_reg_alux(dc, CC_OP_SUB);
593 case CRISV10_REG_ADD:
594 LOG_DIS("add $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
595 cris_cc_mask(dc, CC_MASK_NZVC);
596 dec10_reg_alu(dc, CC_OP_ADD, size, 0);
598 case CRISV10_REG_SUB:
599 LOG_DIS("sub $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
600 cris_cc_mask(dc, CC_MASK_NZVC);
601 dec10_reg_alu(dc, CC_OP_SUB, size, 0);
603 case CRISV10_REG_CMP:
604 LOG_DIS("cmp $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
605 cris_cc_mask(dc, CC_MASK_NZVC);
606 dec10_reg_alu(dc, CC_OP_CMP, size, 0);
608 case CRISV10_REG_BOUND:
609 LOG_DIS("bound $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
610 cris_cc_mask(dc, CC_MASK_NZVC);
611 dec10_reg_bound(dc, size);
613 case CRISV10_REG_AND:
614 LOG_DIS("and $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
615 cris_cc_mask(dc, CC_MASK_NZVC);
616 dec10_reg_alu(dc, CC_OP_AND, size, 0);
618 case CRISV10_REG_ADDI:
624 LOG_DIS("addi r%d r%d size=%d\n", dc->src, dc->dst, dc->size);
625 tcg_gen_shli_tl(t, cpu_R[dc->dst], dc->size & 3);
626 tcg_gen_add_tl(cpu_R[dc->src], cpu_R[dc->src], t);
629 case CRISV10_REG_LSL:
630 LOG_DIS("lsl $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
631 cris_cc_mask(dc, CC_MASK_NZVC);
632 dec10_reg_alu(dc, CC_OP_LSL, size, 0);
634 case CRISV10_REG_LSR:
635 LOG_DIS("lsr $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
636 cris_cc_mask(dc, CC_MASK_NZVC);
637 dec10_reg_alu(dc, CC_OP_LSR, size, 0);
639 case CRISV10_REG_ASR:
640 LOG_DIS("asr $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
641 cris_cc_mask(dc, CC_MASK_NZVC);
642 dec10_reg_alu(dc, CC_OP_ASR, size, 1);
645 LOG_DIS("or $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
646 cris_cc_mask(dc, CC_MASK_NZVC);
647 dec10_reg_alu(dc, CC_OP_OR, size, 0);
649 case CRISV10_REG_NEG:
650 LOG_DIS("neg $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
651 cris_cc_mask(dc, CC_MASK_NZVC);
652 dec10_reg_alu(dc, CC_OP_NEG, size, 0);
654 case CRISV10_REG_BIAP:
655 LOG_DIS("BIAP pc=%x reg %d r%d r%d size=%d\n", dc->pc,
656 dc->opcode, dc->src, dc->dst, size);
658 case 4: tmp = 2; break;
659 case 2: tmp = 1; break;
660 case 1: tmp = 0; break;
662 cpu_abort(CPU(dc->cpu), "Unhandled BIAP");
667 tcg_gen_shli_tl(t, cpu_R[dc->dst], tmp);
669 tcg_gen_addi_tl(cpu_PR[PR_PREFIX], t, ((dc->pc +2)| 1) + 1);
671 tcg_gen_add_tl(cpu_PR[PR_PREFIX], cpu_R[dc->src], t);
678 LOG_DIS("pc=%x reg %d r%d r%d\n", dc->pc,
679 dc->opcode, dc->src, dc->dst);
680 cpu_abort(CPU(dc->cpu), "Unhandled opcode");
684 switch (dc->opcode) {
685 case CRISV10_REG_MOVX:
686 cris_cc_mask(dc, CC_MASK_NZVC);
689 case CRISV10_REG_ADDX:
690 cris_cc_mask(dc, CC_MASK_NZVC);
691 dec10_reg_alux(dc, CC_OP_ADD);
693 case CRISV10_REG_SUBX:
694 cris_cc_mask(dc, CC_MASK_NZVC);
695 dec10_reg_alux(dc, CC_OP_SUB);
697 case CRISV10_REG_MOVE_SPR_R:
698 cris_evaluate_flags(dc);
700 dec10_reg_mov_pr(dc);
702 case CRISV10_REG_MOVE_R_SPR:
703 LOG_DIS("move r%d p%d\n", dc->src, dc->dst);
704 cris_evaluate_flags(dc);
705 if (dc->src != 11) /* fast for srp. */
706 dc->cpustate_changed = 1;
707 t_gen_mov_preg_TN(dc, dc->dst, cpu_R[dc->src]);
709 case CRISV10_REG_SETF:
710 case CRISV10_REG_CLEARF:
713 case CRISV10_REG_SWAP:
716 case CRISV10_REG_ABS:
717 cris_cc_mask(dc, CC_MASK_NZVC);
721 LOG_DIS("lz $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
722 cris_cc_mask(dc, CC_MASK_NZVC);
723 dec10_reg_alu(dc, CC_OP_LZ, 4, 0);
725 case CRISV10_REG_XOR:
726 LOG_DIS("xor $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
727 cris_cc_mask(dc, CC_MASK_NZVC);
728 dec10_reg_alu(dc, CC_OP_XOR, 4, 0);
730 case CRISV10_REG_BTST:
731 LOG_DIS("btst $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
732 cris_cc_mask(dc, CC_MASK_NZVC);
733 cris_update_cc_op(dc, CC_OP_FLAGS, 4);
734 gen_helper_btst(cpu_PR[PR_CCS], cpu_env, cpu_R[dc->dst],
735 cpu_R[dc->src], cpu_PR[PR_CCS]);
737 case CRISV10_REG_DSTEP:
738 LOG_DIS("dstep $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
739 cris_cc_mask(dc, CC_MASK_NZVC);
740 cris_alu(dc, CC_OP_DSTEP, cpu_R[dc->dst],
741 cpu_R[dc->dst], cpu_R[dc->src], 4);
743 case CRISV10_REG_MSTEP:
744 LOG_DIS("mstep $r%d, $r%d sz=%d\n", dc->src, dc->dst, size);
745 cris_evaluate_flags(dc);
746 cris_cc_mask(dc, CC_MASK_NZVC);
747 cris_alu(dc, CC_OP_MSTEP, cpu_R[dc->dst],
748 cpu_R[dc->dst], cpu_R[dc->src], 4);
750 case CRISV10_REG_SCC:
754 LOG_DIS("pc=%x reg %d r%d r%d\n", dc->pc,
755 dc->opcode, dc->src, dc->dst);
756 cpu_abort(CPU(dc->cpu), "Unhandled opcode");
763 static unsigned int dec10_ind_move_m_r(CPUCRISState *env, DisasContext *dc,
766 unsigned int insn_len = 2;
769 LOG_DIS("%s: move.%d [$r%d], $r%d\n", __func__,
770 size, dc->src, dc->dst);
772 cris_cc_mask(dc, CC_MASK_NZVC);
774 insn_len += dec10_prep_move_m(env, dc, 0, size, t);
775 cris_alu(dc, CC_OP_MOVE, cpu_R[dc->dst], cpu_R[dc->dst], t, size);
777 tcg_gen_mov_tl(env_btarget, cpu_R[dc->dst]);
778 cris_prepare_jmp(dc, JMP_INDIRECT);
779 dc->delayed_branch = 1;
786 static unsigned int dec10_ind_move_r_m(DisasContext *dc, unsigned int size)
788 unsigned int insn_len = 2;
791 LOG_DIS("move.%d $r%d, [$r%d]\n", dc->size, dc->src, dc->dst);
792 addr = tcg_temp_new();
793 crisv10_prepare_memaddr(dc, addr, size);
794 gen_store_v10(dc, addr, cpu_R[dc->dst], size);
795 insn_len += crisv10_post_memaddr(dc, size);
801 static unsigned int dec10_ind_move_m_pr(CPUCRISState *env, DisasContext *dc)
803 unsigned int insn_len = 2, rd = dc->dst;
806 LOG_DIS("move.%d $p%d, [$r%d]\n", dc->size, dc->dst, dc->src);
809 addr = tcg_temp_new();
811 insn_len += dec10_prep_move_m(env, dc, 0, 4, t);
813 tcg_gen_mov_tl(env_btarget, t);
814 cris_prepare_jmp(dc, JMP_INDIRECT);
815 dc->delayed_branch = 1;
817 tcg_gen_mov_tl(cpu_PR[rd], t);
818 dc->cpustate_changed = 1;
825 static unsigned int dec10_ind_move_pr_m(DisasContext *dc)
827 unsigned int insn_len = 2, size = preg_sizes_v10[dc->dst];
830 LOG_DIS("move.%d $p%d, [$r%d]\n", dc->size, dc->dst, dc->src);
832 addr = tcg_temp_new();
833 crisv10_prepare_memaddr(dc, addr, size);
834 if (dc->dst == PR_CCS) {
836 cris_evaluate_flags(dc);
837 tcg_gen_andi_tl(t0, cpu_PR[PR_CCS], ~PFIX_FLAG);
838 gen_store_v10(dc, addr, t0, size);
841 gen_store_v10(dc, addr, cpu_PR[dc->dst], size);
843 insn_len += crisv10_post_memaddr(dc, size);
850 static void dec10_movem_r_m(DisasContext *dc)
852 int i, pfix = dc->tb_flags & PFIX_FLAG;
855 LOG_DIS("%s r%d, [r%d] pi=%d ir=%x\n", __func__,
856 dc->dst, dc->src, dc->postinc, dc->ir);
858 addr = tcg_temp_new();
860 crisv10_prepare_memaddr(dc, addr, 4);
861 tcg_gen_mov_tl(t0, addr);
862 for (i = dc->dst; i >= 0; i--) {
863 if ((pfix && dc->mode == CRISV10_MODE_AUTOINC) && dc->src == i) {
864 gen_store_v10(dc, addr, t0, 4);
866 gen_store_v10(dc, addr, cpu_R[i], 4);
868 tcg_gen_addi_tl(addr, addr, 4);
871 if (pfix && dc->mode == CRISV10_MODE_AUTOINC) {
872 tcg_gen_mov_tl(cpu_R[dc->src], t0);
875 if (!pfix && dc->mode == CRISV10_MODE_AUTOINC) {
876 tcg_gen_mov_tl(cpu_R[dc->src], addr);
882 static void dec10_movem_m_r(DisasContext *dc)
884 int i, pfix = dc->tb_flags & PFIX_FLAG;
887 LOG_DIS("%s [r%d], r%d pi=%d ir=%x\n", __func__,
888 dc->src, dc->dst, dc->postinc, dc->ir);
890 addr = tcg_temp_new();
892 crisv10_prepare_memaddr(dc, addr, 4);
893 tcg_gen_mov_tl(t0, addr);
894 for (i = dc->dst; i >= 0; i--) {
895 gen_load(dc, cpu_R[i], addr, 4, 0);
896 tcg_gen_addi_tl(addr, addr, 4);
899 if (pfix && dc->mode == CRISV10_MODE_AUTOINC) {
900 tcg_gen_mov_tl(cpu_R[dc->src], t0);
903 if (!pfix && dc->mode == CRISV10_MODE_AUTOINC) {
904 tcg_gen_mov_tl(cpu_R[dc->src], addr);
910 static int dec10_ind_alu(CPUCRISState *env, DisasContext *dc,
911 int op, unsigned int size)
917 cris_alu_m_alloc_temps(t);
918 insn_len += dec10_prep_move_m(env, dc, 0, size, t[0]);
919 cris_alu(dc, op, cpu_R[dc->dst], cpu_R[rd], t[0], size);
921 tcg_gen_mov_tl(env_btarget, cpu_R[dc->dst]);
922 cris_prepare_jmp(dc, JMP_INDIRECT);
923 dc->delayed_branch = 1;
927 cris_alu_m_free_temps(t);
932 static int dec10_ind_bound(CPUCRISState *env, DisasContext *dc,
939 t = tcg_temp_local_new();
940 insn_len += dec10_prep_move_m(env, dc, 0, size, t);
941 cris_alu(dc, CC_OP_BOUND, cpu_R[dc->dst], cpu_R[rd], t, 4);
943 tcg_gen_mov_tl(env_btarget, cpu_R[dc->dst]);
944 cris_prepare_jmp(dc, JMP_INDIRECT);
945 dc->delayed_branch = 1;
952 static int dec10_alux_m(CPUCRISState *env, DisasContext *dc, int op)
954 unsigned int size = (dc->size & 1) ? 2 : 1;
955 unsigned int sx = !!(dc->size & 2);
960 LOG_DIS("addx size=%d sx=%d op=%d %d\n", size, sx, dc->src, dc->dst);
964 cris_cc_mask(dc, CC_MASK_NZVC);
965 insn_len += dec10_prep_move_m(env, dc, sx, size, t);
966 cris_alu(dc, op, cpu_R[dc->dst], cpu_R[rd], t, 4);
968 tcg_gen_mov_tl(env_btarget, cpu_R[dc->dst]);
969 cris_prepare_jmp(dc, JMP_INDIRECT);
970 dc->delayed_branch = 1;
977 static int dec10_dip(CPUCRISState *env, DisasContext *dc)
982 LOG_DIS("dip pc=%x opcode=%d r%d r%d\n",
983 dc->pc, dc->opcode, dc->src, dc->dst);
985 imm = cpu_ldl_code(env, dc->pc + 2);
986 tcg_gen_movi_tl(cpu_PR[PR_PREFIX], imm);
989 tcg_gen_addi_tl(cpu_R[15], cpu_R[15], insn_len - 2);
991 gen_load(dc, cpu_PR[PR_PREFIX], cpu_R[dc->src], 4, 0);
993 tcg_gen_addi_tl(cpu_R[dc->src], cpu_R[dc->src], 4);
1000 static int dec10_bdap_m(CPUCRISState *env, DisasContext *dc, int size)
1005 LOG_DIS("bdap_m pc=%x opcode=%d r%d r%d sz=%d\n",
1006 dc->pc, dc->opcode, dc->src, dc->dst, size);
1008 assert(dc->dst != 15);
1010 /* 8bit embedded offset? */
1011 if (!dc->postinc && (dc->ir & (1 << 11))) {
1012 int simm = dc->ir & 0xff;
1014 /* cpu_abort(CPU(dc->cpu), "Unhandled opcode"); */
1015 /* sign extended. */
1016 simm = (int8_t)simm;
1018 tcg_gen_addi_tl(cpu_PR[PR_PREFIX], cpu_R[dc->dst], simm);
1020 cris_set_prefix(dc);
1024 /* Now the rest of the modes are truly indirect. */
1025 insn_len += dec10_prep_move_m(env, dc, 1, size, cpu_PR[PR_PREFIX]);
1026 tcg_gen_add_tl(cpu_PR[PR_PREFIX], cpu_PR[PR_PREFIX], cpu_R[rd]);
1027 cris_set_prefix(dc);
1031 static unsigned int dec10_ind(CPUCRISState *env, DisasContext *dc)
1033 unsigned int insn_len = 2;
1034 unsigned int size = dec10_size(dc->size);
1039 if (dc->size != 3) {
1040 switch (dc->opcode) {
1041 case CRISV10_IND_MOVE_M_R:
1042 return dec10_ind_move_m_r(env, dc, size);
1043 case CRISV10_IND_MOVE_R_M:
1044 return dec10_ind_move_r_m(dc, size);
1045 case CRISV10_IND_CMP:
1046 LOG_DIS("cmp size=%d op=%d %d\n", size, dc->src, dc->dst);
1047 cris_cc_mask(dc, CC_MASK_NZVC);
1048 insn_len += dec10_ind_alu(env, dc, CC_OP_CMP, size);
1050 case CRISV10_IND_TEST:
1051 LOG_DIS("test size=%d op=%d %d\n", size, dc->src, dc->dst);
1053 cris_evaluate_flags(dc);
1054 cris_cc_mask(dc, CC_MASK_NZVC);
1055 cris_alu_m_alloc_temps(t);
1056 insn_len += dec10_prep_move_m(env, dc, 0, size, t[0]);
1057 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], ~3);
1058 c = tcg_const_tl(0);
1059 cris_alu(dc, CC_OP_CMP, cpu_R[dc->dst],
1062 cris_alu_m_free_temps(t);
1064 case CRISV10_IND_ADD:
1065 LOG_DIS("add size=%d op=%d %d\n", size, dc->src, dc->dst);
1066 cris_cc_mask(dc, CC_MASK_NZVC);
1067 insn_len += dec10_ind_alu(env, dc, CC_OP_ADD, size);
1069 case CRISV10_IND_SUB:
1070 LOG_DIS("sub size=%d op=%d %d\n", size, dc->src, dc->dst);
1071 cris_cc_mask(dc, CC_MASK_NZVC);
1072 insn_len += dec10_ind_alu(env, dc, CC_OP_SUB, size);
1074 case CRISV10_IND_BOUND:
1075 LOG_DIS("bound size=%d op=%d %d\n", size, dc->src, dc->dst);
1076 cris_cc_mask(dc, CC_MASK_NZVC);
1077 insn_len += dec10_ind_bound(env, dc, size);
1079 case CRISV10_IND_AND:
1080 LOG_DIS("and size=%d op=%d %d\n", size, dc->src, dc->dst);
1081 cris_cc_mask(dc, CC_MASK_NZVC);
1082 insn_len += dec10_ind_alu(env, dc, CC_OP_AND, size);
1084 case CRISV10_IND_OR:
1085 LOG_DIS("or size=%d op=%d %d\n", size, dc->src, dc->dst);
1086 cris_cc_mask(dc, CC_MASK_NZVC);
1087 insn_len += dec10_ind_alu(env, dc, CC_OP_OR, size);
1089 case CRISV10_IND_MOVX:
1090 insn_len = dec10_alux_m(env, dc, CC_OP_MOVE);
1092 case CRISV10_IND_ADDX:
1093 insn_len = dec10_alux_m(env, dc, CC_OP_ADD);
1095 case CRISV10_IND_SUBX:
1096 insn_len = dec10_alux_m(env, dc, CC_OP_SUB);
1098 case CRISV10_IND_CMPX:
1099 insn_len = dec10_alux_m(env, dc, CC_OP_CMP);
1101 case CRISV10_IND_MUL:
1102 /* This is a reg insn coded in the mem indir space. */
1103 LOG_DIS("mul pc=%x opcode=%d\n", dc->pc, dc->opcode);
1104 cris_cc_mask(dc, CC_MASK_NZVC);
1105 dec10_reg_mul(dc, size, dc->ir & (1 << 10));
1107 case CRISV10_IND_BDAP_M:
1108 insn_len = dec10_bdap_m(env, dc, size);
1114 * Instruction format: ADDC [Rs],Rd
1116 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-+
1117 * |Destination(Rd)| 1 0 0 1 1 0 1 0 | Source(Rs)|
1118 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+--+--+
1120 * Instruction format: ADDC [Rs+],Rd
1122 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-+
1123 * |Destination(Rd)| 1 1 0 1 1 0 1 0 | Source(Rs)|
1124 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+-+
1126 if (dc->opcode == CRISV17_IND_ADDC && dc->size == 2 &&
1127 env->pregs[PR_VR] == 17) {
1128 LOG_DIS("addc op=%d %d\n", dc->src, dc->dst);
1129 cris_cc_mask(dc, CC_MASK_NZVC);
1130 insn_len += dec10_ind_alu(env, dc, CC_OP_ADDC, size);
1134 LOG_DIS("pc=%x var-ind.%d %d r%d r%d\n",
1135 dc->pc, size, dc->opcode, dc->src, dc->dst);
1136 cpu_abort(CPU(dc->cpu), "Unhandled opcode");
1142 switch (dc->opcode) {
1143 case CRISV10_IND_MOVE_M_SPR:
1144 insn_len = dec10_ind_move_m_pr(env, dc);
1146 case CRISV10_IND_MOVE_SPR_M:
1147 insn_len = dec10_ind_move_pr_m(dc);
1149 case CRISV10_IND_JUMP_M:
1150 if (dc->src == 15) {
1151 LOG_DIS("jump.%d %d r%d r%d direct\n", size,
1152 dc->opcode, dc->src, dc->dst);
1153 imm = cpu_ldl_code(env, dc->pc + 2);
1154 if (dc->mode == CRISV10_MODE_AUTOINC)
1157 c = tcg_const_tl(dc->pc + insn_len);
1158 t_gen_mov_preg_TN(dc, dc->dst, c);
1161 cris_prepare_jmp(dc, JMP_DIRECT);
1162 dc->delayed_branch--; /* v10 has no dslot here. */
1164 if (dc->dst == 14) {
1165 LOG_DIS("break %d\n", dc->src);
1166 cris_evaluate_flags(dc);
1167 tcg_gen_movi_tl(env_pc, dc->pc + 2);
1168 c = tcg_const_tl(dc->src + 2);
1169 t_gen_mov_env_TN(trap_vector, c);
1171 t_gen_raise_exception(EXCP_BREAK);
1172 dc->is_jmp = DISAS_UPDATE;
1175 LOG_DIS("%d: jump.%d %d r%d r%d\n", __LINE__, size,
1176 dc->opcode, dc->src, dc->dst);
1177 t[0] = tcg_temp_new();
1178 c = tcg_const_tl(dc->pc + insn_len);
1179 t_gen_mov_preg_TN(dc, dc->dst, c);
1181 crisv10_prepare_memaddr(dc, t[0], size);
1182 gen_load(dc, env_btarget, t[0], 4, 0);
1183 insn_len += crisv10_post_memaddr(dc, size);
1184 cris_prepare_jmp(dc, JMP_INDIRECT);
1185 dc->delayed_branch--; /* v10 has no dslot here. */
1186 tcg_temp_free(t[0]);
1190 case CRISV10_IND_MOVEM_R_M:
1191 LOG_DIS("movem_r_m pc=%x opcode=%d r%d r%d\n",
1192 dc->pc, dc->opcode, dc->dst, dc->src);
1193 dec10_movem_r_m(dc);
1195 case CRISV10_IND_MOVEM_M_R:
1196 LOG_DIS("movem_m_r pc=%x opcode=%d\n", dc->pc, dc->opcode);
1197 dec10_movem_m_r(dc);
1199 case CRISV10_IND_JUMP_R:
1200 LOG_DIS("jmp pc=%x opcode=%d r%d r%d\n",
1201 dc->pc, dc->opcode, dc->dst, dc->src);
1202 tcg_gen_mov_tl(env_btarget, cpu_R[dc->src]);
1203 c = tcg_const_tl(dc->pc + insn_len);
1204 t_gen_mov_preg_TN(dc, dc->dst, c);
1206 cris_prepare_jmp(dc, JMP_INDIRECT);
1207 dc->delayed_branch--; /* v10 has no dslot here. */
1209 case CRISV10_IND_MOVX:
1210 insn_len = dec10_alux_m(env, dc, CC_OP_MOVE);
1212 case CRISV10_IND_ADDX:
1213 insn_len = dec10_alux_m(env, dc, CC_OP_ADD);
1215 case CRISV10_IND_SUBX:
1216 insn_len = dec10_alux_m(env, dc, CC_OP_SUB);
1218 case CRISV10_IND_CMPX:
1219 insn_len = dec10_alux_m(env, dc, CC_OP_CMP);
1221 case CRISV10_IND_DIP:
1222 insn_len = dec10_dip(env, dc);
1224 case CRISV10_IND_BCC_M:
1226 cris_cc_mask(dc, 0);
1227 simm = cpu_ldsw_code(env, dc->pc + 2);
1230 LOG_DIS("bcc_m: b%s %x\n", cc_name(dc->cond), dc->pc + simm);
1231 cris_prepare_cc_branch(dc, simm, dc->cond);
1235 LOG_DIS("ERROR pc=%x opcode=%d\n", dc->pc, dc->opcode);
1236 cpu_abort(CPU(dc->cpu), "Unhandled opcode");
1243 static unsigned int crisv10_decoder(CPUCRISState *env, DisasContext *dc)
1245 unsigned int insn_len = 2;
1247 /* Load a halfword onto the instruction register. */
1248 dc->ir = cpu_lduw_code(env, dc->pc);
1250 /* Now decode it. */
1251 dc->opcode = EXTRACT_FIELD(dc->ir, 6, 9);
1252 dc->mode = EXTRACT_FIELD(dc->ir, 10, 11);
1253 dc->src = EXTRACT_FIELD(dc->ir, 0, 3);
1254 dc->size = EXTRACT_FIELD(dc->ir, 4, 5);
1255 dc->cond = dc->dst = EXTRACT_FIELD(dc->ir, 12, 15);
1256 dc->postinc = EXTRACT_FIELD(dc->ir, 10, 10);
1258 dc->clear_prefix = 1;
1260 /* FIXME: What if this insn insn't 2 in length?? */
1261 if (dc->src == 15 || dc->dst == 15)
1262 tcg_gen_movi_tl(cpu_R[15], dc->pc + 2);
1265 case CRISV10_MODE_QIMMEDIATE:
1266 insn_len = dec10_quick_imm(dc);
1268 case CRISV10_MODE_REG:
1269 insn_len = dec10_reg(dc);
1271 case CRISV10_MODE_AUTOINC:
1272 case CRISV10_MODE_INDIRECT:
1273 insn_len = dec10_ind(env, dc);
1277 if (dc->clear_prefix && dc->tb_flags & PFIX_FLAG) {
1278 dc->tb_flags &= ~PFIX_FLAG;
1279 tcg_gen_andi_tl(cpu_PR[PR_CCS], cpu_PR[PR_CCS], ~PFIX_FLAG);
1280 if (dc->tb_flags != dc->tb->flags) {
1281 dc->cpustate_changed = 1;
1285 /* CRISv10 locks out interrupts on dslots. */
1286 if (dc->delayed_branch == 2) {
1292 void cris_initialize_crisv10_tcg(void)
1296 cc_x = tcg_global_mem_new(cpu_env,
1297 offsetof(CPUCRISState, cc_x), "cc_x");
1298 cc_src = tcg_global_mem_new(cpu_env,
1299 offsetof(CPUCRISState, cc_src), "cc_src");
1300 cc_dest = tcg_global_mem_new(cpu_env,
1301 offsetof(CPUCRISState, cc_dest),
1303 cc_result = tcg_global_mem_new(cpu_env,
1304 offsetof(CPUCRISState, cc_result),
1306 cc_op = tcg_global_mem_new(cpu_env,
1307 offsetof(CPUCRISState, cc_op), "cc_op");
1308 cc_size = tcg_global_mem_new(cpu_env,
1309 offsetof(CPUCRISState, cc_size),
1311 cc_mask = tcg_global_mem_new(cpu_env,
1312 offsetof(CPUCRISState, cc_mask),
1315 env_pc = tcg_global_mem_new(cpu_env,
1316 offsetof(CPUCRISState, pc),
1318 env_btarget = tcg_global_mem_new(cpu_env,
1319 offsetof(CPUCRISState, btarget),
1321 env_btaken = tcg_global_mem_new(cpu_env,
1322 offsetof(CPUCRISState, btaken),
1324 for (i = 0; i < 16; i++) {
1325 cpu_R[i] = tcg_global_mem_new(cpu_env,
1326 offsetof(CPUCRISState, regs[i]),
1329 for (i = 0; i < 16; i++) {
1330 cpu_PR[i] = tcg_global_mem_new(cpu_env,
1331 offsetof(CPUCRISState, pregs[i]),