4 * Copyright (c) 2019-2020 Michael Rolnik
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but 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 Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
22 #include "qemu/qemu-print.h"
25 #include "exec/exec-all.h"
26 #include "tcg/tcg-op.h"
27 #include "exec/cpu_ldst.h"
28 #include "exec/helper-proto.h"
29 #include "exec/helper-gen.h"
31 #include "exec/translator.h"
32 #include "exec/gen-icount.h"
35 * Define if you want a BREAK instruction translated to a breakpoint
36 * Active debugging connection is assumed
38 * https://github.com/seharris/qemu-avr-tests/tree/master/instruction-tests
41 #undef BREAKPOINT_ON_BREAK
54 static TCGv cpu_rampD
;
55 static TCGv cpu_rampX
;
56 static TCGv cpu_rampY
;
57 static TCGv cpu_rampZ
;
59 static TCGv cpu_r
[NUMBER_OF_CPU_REGISTERS
];
65 static const char reg_names
[NUMBER_OF_CPU_REGISTERS
][8] = {
66 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
67 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
68 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
69 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
71 #define REG(x) (cpu_r[x])
74 DISAS_EXIT
= DISAS_TARGET_0
, /* We want return to the cpu main loop. */
75 DISAS_LOOKUP
= DISAS_TARGET_1
, /* We have a variable condition exit. */
76 DISAS_CHAIN
= DISAS_TARGET_2
, /* We have a single condition exit. */
79 typedef struct DisasContext DisasContext
;
81 /* This is the state at translation time. */
91 /* Routine used to access memory */
97 * some AVR instructions can make the following instruction to be skipped
98 * Let's name those instructions
99 * A - instruction that can skip the next one
100 * B - instruction that can be skipped. this depends on execution of A
101 * there are two scenarios
102 * 1. A and B belong to the same translation block
103 * 2. A is the last instruction in the translation block and B is the last
105 * following variables are used to simplify the skipping logic, they are
106 * used in the following manner (sketch)
108 * TCGLabel *skip_label = NULL;
109 * if (ctx.skip_cond != TCG_COND_NEVER) {
110 * skip_label = gen_new_label();
111 * tcg_gen_brcond_tl(skip_cond, skip_var0, skip_var1, skip_label);
114 * if (free_skip_var0) {
115 * tcg_temp_free(skip_var0);
116 * free_skip_var0 = false;
122 * gen_set_label(skip_label);
131 void avr_cpu_tcg_init(void)
135 #define AVR_REG_OFFS(x) offsetof(CPUAVRState, x)
136 cpu_pc
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(pc_w
), "pc");
137 cpu_Cf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregC
), "Cf");
138 cpu_Zf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregZ
), "Zf");
139 cpu_Nf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregN
), "Nf");
140 cpu_Vf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregV
), "Vf");
141 cpu_Sf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregS
), "Sf");
142 cpu_Hf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregH
), "Hf");
143 cpu_Tf
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregT
), "Tf");
144 cpu_If
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sregI
), "If");
145 cpu_rampD
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(rampD
), "rampD");
146 cpu_rampX
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(rampX
), "rampX");
147 cpu_rampY
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(rampY
), "rampY");
148 cpu_rampZ
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(rampZ
), "rampZ");
149 cpu_eind
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(eind
), "eind");
150 cpu_sp
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(sp
), "sp");
151 cpu_skip
= tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(skip
), "skip");
153 for (i
= 0; i
< NUMBER_OF_CPU_REGISTERS
; i
++) {
154 cpu_r
[i
] = tcg_global_mem_new_i32(cpu_env
, AVR_REG_OFFS(r
[i
]),
160 static int to_regs_16_31_by_one(DisasContext
*ctx
, int indx
)
162 return 16 + (indx
% 16);
165 static int to_regs_16_23_by_one(DisasContext
*ctx
, int indx
)
167 return 16 + (indx
% 8);
170 static int to_regs_24_30_by_two(DisasContext
*ctx
, int indx
)
172 return 24 + (indx
% 4) * 2;
175 static int to_regs_00_30_by_two(DisasContext
*ctx
, int indx
)
177 return (indx
% 16) * 2;
180 static uint16_t next_word(DisasContext
*ctx
)
182 return cpu_lduw_code(ctx
->env
, ctx
->npc
++ * 2);
185 static int append_16(DisasContext
*ctx
, int x
)
187 return x
<< 16 | next_word(ctx
);
190 static bool avr_have_feature(DisasContext
*ctx
, int feature
)
192 if (!avr_feature(ctx
->env
, feature
)) {
193 gen_helper_unsupported(cpu_env
);
194 ctx
->bstate
= DISAS_NORETURN
;
200 static bool decode_insn(DisasContext
*ctx
, uint16_t insn
);
201 #include "decode_insn.inc.c"
204 * Arithmetic Instructions
208 * Utility functions for updating status registers:
219 static void gen_add_CHf(TCGv R
, TCGv Rd
, TCGv Rr
)
221 TCGv t1
= tcg_temp_new_i32();
222 TCGv t2
= tcg_temp_new_i32();
223 TCGv t3
= tcg_temp_new_i32();
225 tcg_gen_and_tl(t1
, Rd
, Rr
); /* t1 = Rd & Rr */
226 tcg_gen_andc_tl(t2
, Rd
, R
); /* t2 = Rd & ~R */
227 tcg_gen_andc_tl(t3
, Rr
, R
); /* t3 = Rr & ~R */
228 tcg_gen_or_tl(t1
, t1
, t2
); /* t1 = t1 | t2 | t3 */
229 tcg_gen_or_tl(t1
, t1
, t3
);
231 tcg_gen_shri_tl(cpu_Cf
, t1
, 7); /* Cf = t1(7) */
232 tcg_gen_shri_tl(cpu_Hf
, t1
, 3); /* Hf = t1(3) */
233 tcg_gen_andi_tl(cpu_Hf
, cpu_Hf
, 1);
235 tcg_temp_free_i32(t3
);
236 tcg_temp_free_i32(t2
);
237 tcg_temp_free_i32(t1
);
240 static void gen_add_Vf(TCGv R
, TCGv Rd
, TCGv Rr
)
242 TCGv t1
= tcg_temp_new_i32();
243 TCGv t2
= tcg_temp_new_i32();
245 /* t1 = Rd & Rr & ~R | ~Rd & ~Rr & R */
246 /* = (Rd ^ R) & ~(Rd ^ Rr) */
247 tcg_gen_xor_tl(t1
, Rd
, R
);
248 tcg_gen_xor_tl(t2
, Rd
, Rr
);
249 tcg_gen_andc_tl(t1
, t1
, t2
);
251 tcg_gen_shri_tl(cpu_Vf
, t1
, 7); /* Vf = t1(7) */
253 tcg_temp_free_i32(t2
);
254 tcg_temp_free_i32(t1
);
257 static void gen_sub_CHf(TCGv R
, TCGv Rd
, TCGv Rr
)
259 TCGv t1
= tcg_temp_new_i32();
260 TCGv t2
= tcg_temp_new_i32();
261 TCGv t3
= tcg_temp_new_i32();
263 tcg_gen_not_tl(t1
, Rd
); /* t1 = ~Rd */
264 tcg_gen_and_tl(t2
, t1
, Rr
); /* t2 = ~Rd & Rr */
265 tcg_gen_or_tl(t3
, t1
, Rr
); /* t3 = (~Rd | Rr) & R */
266 tcg_gen_and_tl(t3
, t3
, R
);
267 tcg_gen_or_tl(t2
, t2
, t3
); /* t2 = ~Rd & Rr | ~Rd & R | R & Rr */
269 tcg_gen_shri_tl(cpu_Cf
, t2
, 7); /* Cf = t2(7) */
270 tcg_gen_shri_tl(cpu_Hf
, t2
, 3); /* Hf = t2(3) */
271 tcg_gen_andi_tl(cpu_Hf
, cpu_Hf
, 1);
273 tcg_temp_free_i32(t3
);
274 tcg_temp_free_i32(t2
);
275 tcg_temp_free_i32(t1
);
278 static void gen_sub_Vf(TCGv R
, TCGv Rd
, TCGv Rr
)
280 TCGv t1
= tcg_temp_new_i32();
281 TCGv t2
= tcg_temp_new_i32();
283 /* t1 = Rd & ~Rr & ~R | ~Rd & Rr & R */
284 /* = (Rd ^ R) & (Rd ^ R) */
285 tcg_gen_xor_tl(t1
, Rd
, R
);
286 tcg_gen_xor_tl(t2
, Rd
, Rr
);
287 tcg_gen_and_tl(t1
, t1
, t2
);
289 tcg_gen_shri_tl(cpu_Vf
, t1
, 7); /* Vf = t1(7) */
291 tcg_temp_free_i32(t2
);
292 tcg_temp_free_i32(t1
);
295 static void gen_NSf(TCGv R
)
297 tcg_gen_shri_tl(cpu_Nf
, R
, 7); /* Nf = R(7) */
298 tcg_gen_xor_tl(cpu_Sf
, cpu_Nf
, cpu_Vf
); /* Sf = Nf ^ Vf */
301 static void gen_ZNSf(TCGv R
)
303 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
305 /* update status register */
306 tcg_gen_shri_tl(cpu_Nf
, R
, 7); /* Nf = R(7) */
307 tcg_gen_xor_tl(cpu_Sf
, cpu_Nf
, cpu_Vf
); /* Sf = Nf ^ Vf */
311 * Adds two registers without the C Flag and places the result in the
312 * destination register Rd.
314 static bool trans_ADD(DisasContext
*ctx
, arg_ADD
*a
)
316 TCGv Rd
= cpu_r
[a
->rd
];
317 TCGv Rr
= cpu_r
[a
->rr
];
318 TCGv R
= tcg_temp_new_i32();
320 tcg_gen_add_tl(R
, Rd
, Rr
); /* Rd = Rd + Rr */
321 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
323 /* update status register */
324 gen_add_CHf(R
, Rd
, Rr
);
325 gen_add_Vf(R
, Rd
, Rr
);
328 /* update output registers */
329 tcg_gen_mov_tl(Rd
, R
);
331 tcg_temp_free_i32(R
);
337 * Adds two registers and the contents of the C Flag and places the result in
338 * the destination register Rd.
340 static bool trans_ADC(DisasContext
*ctx
, arg_ADC
*a
)
342 TCGv Rd
= cpu_r
[a
->rd
];
343 TCGv Rr
= cpu_r
[a
->rr
];
344 TCGv R
= tcg_temp_new_i32();
346 tcg_gen_add_tl(R
, Rd
, Rr
); /* R = Rd + Rr + Cf */
347 tcg_gen_add_tl(R
, R
, cpu_Cf
);
348 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
350 /* update status register */
351 gen_add_CHf(R
, Rd
, Rr
);
352 gen_add_Vf(R
, Rd
, Rr
);
355 /* update output registers */
356 tcg_gen_mov_tl(Rd
, R
);
358 tcg_temp_free_i32(R
);
364 * Adds an immediate value (0 - 63) to a register pair and places the result
365 * in the register pair. This instruction operates on the upper four register
366 * pairs, and is well suited for operations on the pointer registers. This
367 * instruction is not available in all devices. Refer to the device specific
368 * instruction set summary.
370 static bool trans_ADIW(DisasContext
*ctx
, arg_ADIW
*a
)
372 if (!avr_have_feature(ctx
, AVR_FEATURE_ADIW_SBIW
)) {
376 TCGv RdL
= cpu_r
[a
->rd
];
377 TCGv RdH
= cpu_r
[a
->rd
+ 1];
379 TCGv R
= tcg_temp_new_i32();
380 TCGv Rd
= tcg_temp_new_i32();
382 tcg_gen_deposit_tl(Rd
, RdL
, RdH
, 8, 8); /* Rd = RdH:RdL */
383 tcg_gen_addi_tl(R
, Rd
, Imm
); /* R = Rd + Imm */
384 tcg_gen_andi_tl(R
, R
, 0xffff); /* make it 16 bits */
386 /* update status register */
387 tcg_gen_andc_tl(cpu_Cf
, Rd
, R
); /* Cf = Rd & ~R */
388 tcg_gen_shri_tl(cpu_Cf
, cpu_Cf
, 15);
389 tcg_gen_andc_tl(cpu_Vf
, R
, Rd
); /* Vf = R & ~Rd */
390 tcg_gen_shri_tl(cpu_Vf
, cpu_Vf
, 15);
391 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
392 tcg_gen_shri_tl(cpu_Nf
, R
, 15); /* Nf = R(15) */
393 tcg_gen_xor_tl(cpu_Sf
, cpu_Nf
, cpu_Vf
);/* Sf = Nf ^ Vf */
395 /* update output registers */
396 tcg_gen_andi_tl(RdL
, R
, 0xff);
397 tcg_gen_shri_tl(RdH
, R
, 8);
399 tcg_temp_free_i32(Rd
);
400 tcg_temp_free_i32(R
);
406 * Subtracts two registers and places the result in the destination
409 static bool trans_SUB(DisasContext
*ctx
, arg_SUB
*a
)
411 TCGv Rd
= cpu_r
[a
->rd
];
412 TCGv Rr
= cpu_r
[a
->rr
];
413 TCGv R
= tcg_temp_new_i32();
415 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr */
416 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
418 /* update status register */
419 tcg_gen_andc_tl(cpu_Cf
, Rd
, R
); /* Cf = Rd & ~R */
420 gen_sub_CHf(R
, Rd
, Rr
);
421 gen_sub_Vf(R
, Rd
, Rr
);
424 /* update output registers */
425 tcg_gen_mov_tl(Rd
, R
);
427 tcg_temp_free_i32(R
);
433 * Subtracts a register and a constant and places the result in the
434 * destination register Rd. This instruction is working on Register R16 to R31
435 * and is very well suited for operations on the X, Y, and Z-pointers.
437 static bool trans_SUBI(DisasContext
*ctx
, arg_SUBI
*a
)
439 TCGv Rd
= cpu_r
[a
->rd
];
440 TCGv Rr
= tcg_const_i32(a
->imm
);
441 TCGv R
= tcg_temp_new_i32();
443 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Imm */
444 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
446 /* update status register */
447 gen_sub_CHf(R
, Rd
, Rr
);
448 gen_sub_Vf(R
, Rd
, Rr
);
451 /* update output registers */
452 tcg_gen_mov_tl(Rd
, R
);
454 tcg_temp_free_i32(R
);
455 tcg_temp_free_i32(Rr
);
461 * Subtracts two registers and subtracts with the C Flag and places the
462 * result in the destination register Rd.
464 static bool trans_SBC(DisasContext
*ctx
, arg_SBC
*a
)
466 TCGv Rd
= cpu_r
[a
->rd
];
467 TCGv Rr
= cpu_r
[a
->rr
];
468 TCGv R
= tcg_temp_new_i32();
469 TCGv zero
= tcg_const_i32(0);
471 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr - Cf */
472 tcg_gen_sub_tl(R
, R
, cpu_Cf
);
473 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
475 /* update status register */
476 gen_sub_CHf(R
, Rd
, Rr
);
477 gen_sub_Vf(R
, Rd
, Rr
);
481 * Previous value remains unchanged when the result is zero;
484 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_Zf
, R
, zero
, cpu_Zf
, zero
);
486 /* update output registers */
487 tcg_gen_mov_tl(Rd
, R
);
489 tcg_temp_free_i32(zero
);
490 tcg_temp_free_i32(R
);
496 * SBCI -- Subtract Immediate with Carry
498 static bool trans_SBCI(DisasContext
*ctx
, arg_SBCI
*a
)
500 TCGv Rd
= cpu_r
[a
->rd
];
501 TCGv Rr
= tcg_const_i32(a
->imm
);
502 TCGv R
= tcg_temp_new_i32();
503 TCGv zero
= tcg_const_i32(0);
505 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr - Cf */
506 tcg_gen_sub_tl(R
, R
, cpu_Cf
);
507 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
509 /* update status register */
510 gen_sub_CHf(R
, Rd
, Rr
);
511 gen_sub_Vf(R
, Rd
, Rr
);
515 * Previous value remains unchanged when the result is zero;
518 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_Zf
, R
, zero
, cpu_Zf
, zero
);
520 /* update output registers */
521 tcg_gen_mov_tl(Rd
, R
);
523 tcg_temp_free_i32(zero
);
524 tcg_temp_free_i32(R
);
525 tcg_temp_free_i32(Rr
);
531 * Subtracts an immediate value (0-63) from a register pair and places the
532 * result in the register pair. This instruction operates on the upper four
533 * register pairs, and is well suited for operations on the Pointer Registers.
534 * This instruction is not available in all devices. Refer to the device
535 * specific instruction set summary.
537 static bool trans_SBIW(DisasContext
*ctx
, arg_SBIW
*a
)
539 if (!avr_have_feature(ctx
, AVR_FEATURE_ADIW_SBIW
)) {
543 TCGv RdL
= cpu_r
[a
->rd
];
544 TCGv RdH
= cpu_r
[a
->rd
+ 1];
546 TCGv R
= tcg_temp_new_i32();
547 TCGv Rd
= tcg_temp_new_i32();
549 tcg_gen_deposit_tl(Rd
, RdL
, RdH
, 8, 8); /* Rd = RdH:RdL */
550 tcg_gen_subi_tl(R
, Rd
, Imm
); /* R = Rd - Imm */
551 tcg_gen_andi_tl(R
, R
, 0xffff); /* make it 16 bits */
553 /* update status register */
554 tcg_gen_andc_tl(cpu_Cf
, R
, Rd
);
555 tcg_gen_shri_tl(cpu_Cf
, cpu_Cf
, 15); /* Cf = R & ~Rd */
556 tcg_gen_andc_tl(cpu_Vf
, Rd
, R
);
557 tcg_gen_shri_tl(cpu_Vf
, cpu_Vf
, 15); /* Vf = Rd & ~R */
558 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
559 tcg_gen_shri_tl(cpu_Nf
, R
, 15); /* Nf = R(15) */
560 tcg_gen_xor_tl(cpu_Sf
, cpu_Nf
, cpu_Vf
); /* Sf = Nf ^ Vf */
562 /* update output registers */
563 tcg_gen_andi_tl(RdL
, R
, 0xff);
564 tcg_gen_shri_tl(RdH
, R
, 8);
566 tcg_temp_free_i32(Rd
);
567 tcg_temp_free_i32(R
);
573 * Performs the logical AND between the contents of register Rd and register
574 * Rr and places the result in the destination register Rd.
576 static bool trans_AND(DisasContext
*ctx
, arg_AND
*a
)
578 TCGv Rd
= cpu_r
[a
->rd
];
579 TCGv Rr
= cpu_r
[a
->rr
];
580 TCGv R
= tcg_temp_new_i32();
582 tcg_gen_and_tl(R
, Rd
, Rr
); /* Rd = Rd and Rr */
584 /* update status register */
585 tcg_gen_movi_tl(cpu_Vf
, 0); /* Vf = 0 */
586 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
589 /* update output registers */
590 tcg_gen_mov_tl(Rd
, R
);
592 tcg_temp_free_i32(R
);
598 * Performs the logical AND between the contents of register Rd and a constant
599 * and places the result in the destination register Rd.
601 static bool trans_ANDI(DisasContext
*ctx
, arg_ANDI
*a
)
603 TCGv Rd
= cpu_r
[a
->rd
];
606 tcg_gen_andi_tl(Rd
, Rd
, Imm
); /* Rd = Rd & Imm */
608 /* update status register */
609 tcg_gen_movi_tl(cpu_Vf
, 0x00); /* Vf = 0 */
616 * Performs the logical OR between the contents of register Rd and register
617 * Rr and places the result in the destination register Rd.
619 static bool trans_OR(DisasContext
*ctx
, arg_OR
*a
)
621 TCGv Rd
= cpu_r
[a
->rd
];
622 TCGv Rr
= cpu_r
[a
->rr
];
623 TCGv R
= tcg_temp_new_i32();
625 tcg_gen_or_tl(R
, Rd
, Rr
);
627 /* update status register */
628 tcg_gen_movi_tl(cpu_Vf
, 0);
631 /* update output registers */
632 tcg_gen_mov_tl(Rd
, R
);
634 tcg_temp_free_i32(R
);
640 * Performs the logical OR between the contents of register Rd and a
641 * constant and places the result in the destination register Rd.
643 static bool trans_ORI(DisasContext
*ctx
, arg_ORI
*a
)
645 TCGv Rd
= cpu_r
[a
->rd
];
648 tcg_gen_ori_tl(Rd
, Rd
, Imm
); /* Rd = Rd | Imm */
650 /* update status register */
651 tcg_gen_movi_tl(cpu_Vf
, 0x00); /* Vf = 0 */
658 * Performs the logical EOR between the contents of register Rd and
659 * register Rr and places the result in the destination register Rd.
661 static bool trans_EOR(DisasContext
*ctx
, arg_EOR
*a
)
663 TCGv Rd
= cpu_r
[a
->rd
];
664 TCGv Rr
= cpu_r
[a
->rr
];
666 tcg_gen_xor_tl(Rd
, Rd
, Rr
);
668 /* update status register */
669 tcg_gen_movi_tl(cpu_Vf
, 0);
676 * Clears the specified bits in register Rd. Performs the logical AND
677 * between the contents of register Rd and the complement of the constant mask
678 * K. The result will be placed in register Rd.
680 static bool trans_COM(DisasContext
*ctx
, arg_COM
*a
)
682 TCGv Rd
= cpu_r
[a
->rd
];
683 TCGv R
= tcg_temp_new_i32();
685 tcg_gen_xori_tl(Rd
, Rd
, 0xff);
687 /* update status register */
688 tcg_gen_movi_tl(cpu_Cf
, 1); /* Cf = 1 */
689 tcg_gen_movi_tl(cpu_Vf
, 0); /* Vf = 0 */
692 tcg_temp_free_i32(R
);
698 * Replaces the contents of register Rd with its two's complement; the
699 * value $80 is left unchanged.
701 static bool trans_NEG(DisasContext
*ctx
, arg_NEG
*a
)
703 TCGv Rd
= cpu_r
[a
->rd
];
704 TCGv t0
= tcg_const_i32(0);
705 TCGv R
= tcg_temp_new_i32();
707 tcg_gen_sub_tl(R
, t0
, Rd
); /* R = 0 - Rd */
708 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
710 /* update status register */
711 gen_sub_CHf(R
, t0
, Rd
);
712 gen_sub_Vf(R
, t0
, Rd
);
715 /* update output registers */
716 tcg_gen_mov_tl(Rd
, R
);
718 tcg_temp_free_i32(t0
);
719 tcg_temp_free_i32(R
);
725 * Adds one -1- to the contents of register Rd and places the result in the
726 * destination register Rd. The C Flag in SREG is not affected by the
727 * operation, thus allowing the INC instruction to be used on a loop counter in
728 * multiple-precision computations. When operating on unsigned numbers, only
729 * BREQ and BRNE branches can be expected to perform consistently. When
730 * operating on two's complement values, all signed branches are available.
732 static bool trans_INC(DisasContext
*ctx
, arg_INC
*a
)
734 TCGv Rd
= cpu_r
[a
->rd
];
736 tcg_gen_addi_tl(Rd
, Rd
, 1);
737 tcg_gen_andi_tl(Rd
, Rd
, 0xff);
739 /* update status register */
740 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Vf
, Rd
, 0x80); /* Vf = Rd == 0x80 */
747 * Subtracts one -1- from the contents of register Rd and places the result
748 * in the destination register Rd. The C Flag in SREG is not affected by the
749 * operation, thus allowing the DEC instruction to be used on a loop counter in
750 * multiple-precision computations. When operating on unsigned values, only
751 * BREQ and BRNE branches can be expected to perform consistently. When
752 * operating on two's complement values, all signed branches are available.
754 static bool trans_DEC(DisasContext
*ctx
, arg_DEC
*a
)
756 TCGv Rd
= cpu_r
[a
->rd
];
758 tcg_gen_subi_tl(Rd
, Rd
, 1); /* Rd = Rd - 1 */
759 tcg_gen_andi_tl(Rd
, Rd
, 0xff); /* make it 8 bits */
761 /* update status register */
762 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Vf
, Rd
, 0x7f); /* Vf = Rd == 0x7f */
769 * This instruction performs 8-bit x 8-bit -> 16-bit unsigned multiplication.
771 static bool trans_MUL(DisasContext
*ctx
, arg_MUL
*a
)
773 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
779 TCGv Rd
= cpu_r
[a
->rd
];
780 TCGv Rr
= cpu_r
[a
->rr
];
781 TCGv R
= tcg_temp_new_i32();
783 tcg_gen_mul_tl(R
, Rd
, Rr
); /* R = Rd * Rr */
784 tcg_gen_andi_tl(R0
, R
, 0xff);
785 tcg_gen_shri_tl(R1
, R
, 8);
787 /* update status register */
788 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
789 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
791 tcg_temp_free_i32(R
);
797 * This instruction performs 8-bit x 8-bit -> 16-bit signed multiplication.
799 static bool trans_MULS(DisasContext
*ctx
, arg_MULS
*a
)
801 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
807 TCGv Rd
= cpu_r
[a
->rd
];
808 TCGv Rr
= cpu_r
[a
->rr
];
809 TCGv R
= tcg_temp_new_i32();
810 TCGv t0
= tcg_temp_new_i32();
811 TCGv t1
= tcg_temp_new_i32();
813 tcg_gen_ext8s_tl(t0
, Rd
); /* make Rd full 32 bit signed */
814 tcg_gen_ext8s_tl(t1
, Rr
); /* make Rr full 32 bit signed */
815 tcg_gen_mul_tl(R
, t0
, t1
); /* R = Rd * Rr */
816 tcg_gen_andi_tl(R
, R
, 0xffff); /* make it 16 bits */
817 tcg_gen_andi_tl(R0
, R
, 0xff);
818 tcg_gen_shri_tl(R1
, R
, 8);
820 /* update status register */
821 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
822 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
824 tcg_temp_free_i32(t1
);
825 tcg_temp_free_i32(t0
);
826 tcg_temp_free_i32(R
);
832 * This instruction performs 8-bit x 8-bit -> 16-bit multiplication of a
833 * signed and an unsigned number.
835 static bool trans_MULSU(DisasContext
*ctx
, arg_MULSU
*a
)
837 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
843 TCGv Rd
= cpu_r
[a
->rd
];
844 TCGv Rr
= cpu_r
[a
->rr
];
845 TCGv R
= tcg_temp_new_i32();
846 TCGv t0
= tcg_temp_new_i32();
848 tcg_gen_ext8s_tl(t0
, Rd
); /* make Rd full 32 bit signed */
849 tcg_gen_mul_tl(R
, t0
, Rr
); /* R = Rd * Rr */
850 tcg_gen_andi_tl(R
, R
, 0xffff); /* make R 16 bits */
851 tcg_gen_andi_tl(R0
, R
, 0xff);
852 tcg_gen_shri_tl(R1
, R
, 8);
854 /* update status register */
855 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
856 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
858 tcg_temp_free_i32(t0
);
859 tcg_temp_free_i32(R
);
865 * This instruction performs 8-bit x 8-bit -> 16-bit unsigned
866 * multiplication and shifts the result one bit left.
868 static bool trans_FMUL(DisasContext
*ctx
, arg_FMUL
*a
)
870 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
876 TCGv Rd
= cpu_r
[a
->rd
];
877 TCGv Rr
= cpu_r
[a
->rr
];
878 TCGv R
= tcg_temp_new_i32();
880 tcg_gen_mul_tl(R
, Rd
, Rr
); /* R = Rd * Rr */
882 /* update status register */
883 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
884 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
886 /* update output registers */
887 tcg_gen_shli_tl(R
, R
, 1);
888 tcg_gen_andi_tl(R0
, R
, 0xff);
889 tcg_gen_shri_tl(R1
, R
, 8);
890 tcg_gen_andi_tl(R1
, R1
, 0xff);
893 tcg_temp_free_i32(R
);
899 * This instruction performs 8-bit x 8-bit -> 16-bit signed multiplication
900 * and shifts the result one bit left.
902 static bool trans_FMULS(DisasContext
*ctx
, arg_FMULS
*a
)
904 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
910 TCGv Rd
= cpu_r
[a
->rd
];
911 TCGv Rr
= cpu_r
[a
->rr
];
912 TCGv R
= tcg_temp_new_i32();
913 TCGv t0
= tcg_temp_new_i32();
914 TCGv t1
= tcg_temp_new_i32();
916 tcg_gen_ext8s_tl(t0
, Rd
); /* make Rd full 32 bit signed */
917 tcg_gen_ext8s_tl(t1
, Rr
); /* make Rr full 32 bit signed */
918 tcg_gen_mul_tl(R
, t0
, t1
); /* R = Rd * Rr */
919 tcg_gen_andi_tl(R
, R
, 0xffff); /* make it 16 bits */
921 /* update status register */
922 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
923 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
925 /* update output registers */
926 tcg_gen_shli_tl(R
, R
, 1);
927 tcg_gen_andi_tl(R0
, R
, 0xff);
928 tcg_gen_shri_tl(R1
, R
, 8);
929 tcg_gen_andi_tl(R1
, R1
, 0xff);
931 tcg_temp_free_i32(t1
);
932 tcg_temp_free_i32(t0
);
933 tcg_temp_free_i32(R
);
939 * This instruction performs 8-bit x 8-bit -> 16-bit signed multiplication
940 * and shifts the result one bit left.
942 static bool trans_FMULSU(DisasContext
*ctx
, arg_FMULSU
*a
)
944 if (!avr_have_feature(ctx
, AVR_FEATURE_MUL
)) {
950 TCGv Rd
= cpu_r
[a
->rd
];
951 TCGv Rr
= cpu_r
[a
->rr
];
952 TCGv R
= tcg_temp_new_i32();
953 TCGv t0
= tcg_temp_new_i32();
955 tcg_gen_ext8s_tl(t0
, Rd
); /* make Rd full 32 bit signed */
956 tcg_gen_mul_tl(R
, t0
, Rr
); /* R = Rd * Rr */
957 tcg_gen_andi_tl(R
, R
, 0xffff); /* make it 16 bits */
959 /* update status register */
960 tcg_gen_shri_tl(cpu_Cf
, R
, 15); /* Cf = R(15) */
961 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
963 /* update output registers */
964 tcg_gen_shli_tl(R
, R
, 1);
965 tcg_gen_andi_tl(R0
, R
, 0xff);
966 tcg_gen_shri_tl(R1
, R
, 8);
967 tcg_gen_andi_tl(R1
, R1
, 0xff);
969 tcg_temp_free_i32(t0
);
970 tcg_temp_free_i32(R
);
976 * The module is an instruction set extension to the AVR CPU, performing
977 * DES iterations. The 64-bit data block (plaintext or ciphertext) is placed in
978 * the CPU register file, registers R0-R7, where LSB of data is placed in LSB
979 * of R0 and MSB of data is placed in MSB of R7. The full 64-bit key (including
980 * parity bits) is placed in registers R8- R15, organized in the register file
981 * with LSB of key in LSB of R8 and MSB of key in MSB of R15. Executing one DES
982 * instruction performs one round in the DES algorithm. Sixteen rounds must be
983 * executed in increasing order to form the correct DES ciphertext or
984 * plaintext. Intermediate results are stored in the register file (R0-R15)
985 * after each DES instruction. The instruction's operand (K) determines which
986 * round is executed, and the half carry flag (H) determines whether encryption
987 * or decryption is performed. The DES algorithm is described in
988 * "Specifications for the Data Encryption Standard" (Federal Information
989 * Processing Standards Publication 46). Intermediate results in this
990 * implementation differ from the standard because the initial permutation and
991 * the inverse initial permutation are performed each iteration. This does not
992 * affect the result in the final ciphertext or plaintext, but reduces
995 static bool trans_DES(DisasContext
*ctx
, arg_DES
*a
)
998 if (!avr_have_feature(ctx
, AVR_FEATURE_DES
)) {
1002 qemu_log_mask(LOG_UNIMP
, "%s: not implemented\n", __func__
);
1008 * Branch Instructions
1010 static void gen_jmp_ez(DisasContext
*ctx
)
1012 tcg_gen_deposit_tl(cpu_pc
, cpu_r
[30], cpu_r
[31], 8, 8);
1013 tcg_gen_or_tl(cpu_pc
, cpu_pc
, cpu_eind
);
1014 ctx
->bstate
= DISAS_LOOKUP
;
1017 static void gen_jmp_z(DisasContext
*ctx
)
1019 tcg_gen_deposit_tl(cpu_pc
, cpu_r
[30], cpu_r
[31], 8, 8);
1020 ctx
->bstate
= DISAS_LOOKUP
;
1023 static void gen_push_ret(DisasContext
*ctx
, int ret
)
1025 if (avr_feature(ctx
->env
, AVR_FEATURE_1_BYTE_PC
)) {
1027 TCGv t0
= tcg_const_i32((ret
& 0x0000ff));
1029 tcg_gen_qemu_st_tl(t0
, cpu_sp
, MMU_DATA_IDX
, MO_UB
);
1030 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 1);
1032 tcg_temp_free_i32(t0
);
1033 } else if (avr_feature(ctx
->env
, AVR_FEATURE_2_BYTE_PC
)) {
1035 TCGv t0
= tcg_const_i32((ret
& 0x00ffff));
1037 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 1);
1038 tcg_gen_qemu_st_tl(t0
, cpu_sp
, MMU_DATA_IDX
, MO_BEUW
);
1039 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 1);
1041 tcg_temp_free_i32(t0
);
1043 } else if (avr_feature(ctx
->env
, AVR_FEATURE_3_BYTE_PC
)) {
1045 TCGv lo
= tcg_const_i32((ret
& 0x0000ff));
1046 TCGv hi
= tcg_const_i32((ret
& 0xffff00) >> 8);
1048 tcg_gen_qemu_st_tl(lo
, cpu_sp
, MMU_DATA_IDX
, MO_UB
);
1049 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 2);
1050 tcg_gen_qemu_st_tl(hi
, cpu_sp
, MMU_DATA_IDX
, MO_BEUW
);
1051 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 1);
1053 tcg_temp_free_i32(lo
);
1054 tcg_temp_free_i32(hi
);
1058 static void gen_pop_ret(DisasContext
*ctx
, TCGv ret
)
1060 if (avr_feature(ctx
->env
, AVR_FEATURE_1_BYTE_PC
)) {
1061 tcg_gen_addi_tl(cpu_sp
, cpu_sp
, 1);
1062 tcg_gen_qemu_ld_tl(ret
, cpu_sp
, MMU_DATA_IDX
, MO_UB
);
1063 } else if (avr_feature(ctx
->env
, AVR_FEATURE_2_BYTE_PC
)) {
1064 tcg_gen_addi_tl(cpu_sp
, cpu_sp
, 1);
1065 tcg_gen_qemu_ld_tl(ret
, cpu_sp
, MMU_DATA_IDX
, MO_BEUW
);
1066 tcg_gen_addi_tl(cpu_sp
, cpu_sp
, 1);
1067 } else if (avr_feature(ctx
->env
, AVR_FEATURE_3_BYTE_PC
)) {
1068 TCGv lo
= tcg_temp_new_i32();
1069 TCGv hi
= tcg_temp_new_i32();
1071 tcg_gen_addi_tl(cpu_sp
, cpu_sp
, 1);
1072 tcg_gen_qemu_ld_tl(hi
, cpu_sp
, MMU_DATA_IDX
, MO_BEUW
);
1074 tcg_gen_addi_tl(cpu_sp
, cpu_sp
, 2);
1075 tcg_gen_qemu_ld_tl(lo
, cpu_sp
, MMU_DATA_IDX
, MO_UB
);
1077 tcg_gen_deposit_tl(ret
, lo
, hi
, 8, 16);
1079 tcg_temp_free_i32(lo
);
1080 tcg_temp_free_i32(hi
);
1084 static void gen_goto_tb(DisasContext
*ctx
, int n
, target_ulong dest
)
1086 TranslationBlock
*tb
= ctx
->tb
;
1088 if (ctx
->singlestep
== 0) {
1090 tcg_gen_movi_i32(cpu_pc
, dest
);
1091 tcg_gen_exit_tb(tb
, n
);
1093 tcg_gen_movi_i32(cpu_pc
, dest
);
1094 gen_helper_debug(cpu_env
);
1095 tcg_gen_exit_tb(NULL
, 0);
1097 ctx
->bstate
= DISAS_NORETURN
;
1101 * Relative jump to an address within PC - 2K +1 and PC + 2K (words). For
1102 * AVR microcontrollers with Program memory not exceeding 4K words (8KB) this
1103 * instruction can address the entire memory from every address location. See
1106 static bool trans_RJMP(DisasContext
*ctx
, arg_RJMP
*a
)
1108 int dst
= ctx
->npc
+ a
->imm
;
1110 gen_goto_tb(ctx
, 0, dst
);
1116 * Indirect jump to the address pointed to by the Z (16 bits) Pointer
1117 * Register in the Register File. The Z-pointer Register is 16 bits wide and
1118 * allows jump within the lowest 64K words (128KB) section of Program memory.
1119 * This instruction is not available in all devices. Refer to the device
1120 * specific instruction set summary.
1122 static bool trans_IJMP(DisasContext
*ctx
, arg_IJMP
*a
)
1124 if (!avr_have_feature(ctx
, AVR_FEATURE_IJMP_ICALL
)) {
1134 * Indirect jump to the address pointed to by the Z (16 bits) Pointer
1135 * Register in the Register File and the EIND Register in the I/O space. This
1136 * instruction allows for indirect jumps to the entire 4M (words) Program
1137 * memory space. See also IJMP. This instruction is not available in all
1138 * devices. Refer to the device specific instruction set summary.
1140 static bool trans_EIJMP(DisasContext
*ctx
, arg_EIJMP
*a
)
1142 if (!avr_have_feature(ctx
, AVR_FEATURE_EIJMP_EICALL
)) {
1151 * Jump to an address within the entire 4M (words) Program memory. See also
1152 * RJMP. This instruction is not available in all devices. Refer to the device
1153 * specific instruction set summary.0
1155 static bool trans_JMP(DisasContext
*ctx
, arg_JMP
*a
)
1157 if (!avr_have_feature(ctx
, AVR_FEATURE_JMP_CALL
)) {
1161 gen_goto_tb(ctx
, 0, a
->imm
);
1167 * Relative call to an address within PC - 2K + 1 and PC + 2K (words). The
1168 * return address (the instruction after the RCALL) is stored onto the Stack.
1169 * See also CALL. For AVR microcontrollers with Program memory not exceeding 4K
1170 * words (8KB) this instruction can address the entire memory from every
1171 * address location. The Stack Pointer uses a post-decrement scheme during
1174 static bool trans_RCALL(DisasContext
*ctx
, arg_RCALL
*a
)
1177 int dst
= ctx
->npc
+ a
->imm
;
1179 gen_push_ret(ctx
, ret
);
1180 gen_goto_tb(ctx
, 0, dst
);
1186 * Calls to a subroutine within the entire 4M (words) Program memory. The
1187 * return address (to the instruction after the CALL) will be stored onto the
1188 * Stack. See also RCALL. The Stack Pointer uses a post-decrement scheme during
1189 * CALL. This instruction is not available in all devices. Refer to the device
1190 * specific instruction set summary.
1192 static bool trans_ICALL(DisasContext
*ctx
, arg_ICALL
*a
)
1194 if (!avr_have_feature(ctx
, AVR_FEATURE_IJMP_ICALL
)) {
1200 gen_push_ret(ctx
, ret
);
1207 * Indirect call of a subroutine pointed to by the Z (16 bits) Pointer
1208 * Register in the Register File and the EIND Register in the I/O space. This
1209 * instruction allows for indirect calls to the entire 4M (words) Program
1210 * memory space. See also ICALL. The Stack Pointer uses a post-decrement scheme
1211 * during EICALL. This instruction is not available in all devices. Refer to
1212 * the device specific instruction set summary.
1214 static bool trans_EICALL(DisasContext
*ctx
, arg_EICALL
*a
)
1216 if (!avr_have_feature(ctx
, AVR_FEATURE_EIJMP_EICALL
)) {
1222 gen_push_ret(ctx
, ret
);
1228 * Calls to a subroutine within the entire Program memory. The return
1229 * address (to the instruction after the CALL) will be stored onto the Stack.
1230 * (See also RCALL). The Stack Pointer uses a post-decrement scheme during
1231 * CALL. This instruction is not available in all devices. Refer to the device
1232 * specific instruction set summary.
1234 static bool trans_CALL(DisasContext
*ctx
, arg_CALL
*a
)
1236 if (!avr_have_feature(ctx
, AVR_FEATURE_JMP_CALL
)) {
1243 gen_push_ret(ctx
, ret
);
1244 gen_goto_tb(ctx
, 0, Imm
);
1250 * Returns from subroutine. The return address is loaded from the STACK.
1251 * The Stack Pointer uses a preincrement scheme during RET.
1253 static bool trans_RET(DisasContext
*ctx
, arg_RET
*a
)
1255 gen_pop_ret(ctx
, cpu_pc
);
1257 ctx
->bstate
= DISAS_LOOKUP
;
1262 * Returns from interrupt. The return address is loaded from the STACK and
1263 * the Global Interrupt Flag is set. Note that the Status Register is not
1264 * automatically stored when entering an interrupt routine, and it is not
1265 * restored when returning from an interrupt routine. This must be handled by
1266 * the application program. The Stack Pointer uses a pre-increment scheme
1269 static bool trans_RETI(DisasContext
*ctx
, arg_RETI
*a
)
1271 gen_pop_ret(ctx
, cpu_pc
);
1272 tcg_gen_movi_tl(cpu_If
, 1);
1274 /* Need to return to main loop to re-evaluate interrupts. */
1275 ctx
->bstate
= DISAS_EXIT
;
1280 * This instruction performs a compare between two registers Rd and Rr, and
1281 * skips the next instruction if Rd = Rr.
1283 static bool trans_CPSE(DisasContext
*ctx
, arg_CPSE
*a
)
1285 ctx
->skip_cond
= TCG_COND_EQ
;
1286 ctx
->skip_var0
= cpu_r
[a
->rd
];
1287 ctx
->skip_var1
= cpu_r
[a
->rr
];
1292 * This instruction performs a compare between two registers Rd and Rr.
1293 * None of the registers are changed. All conditional branches can be used
1294 * after this instruction.
1296 static bool trans_CP(DisasContext
*ctx
, arg_CP
*a
)
1298 TCGv Rd
= cpu_r
[a
->rd
];
1299 TCGv Rr
= cpu_r
[a
->rr
];
1300 TCGv R
= tcg_temp_new_i32();
1302 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr */
1303 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
1305 /* update status register */
1306 gen_sub_CHf(R
, Rd
, Rr
);
1307 gen_sub_Vf(R
, Rd
, Rr
);
1310 tcg_temp_free_i32(R
);
1316 * This instruction performs a compare between two registers Rd and Rr and
1317 * also takes into account the previous carry. None of the registers are
1318 * changed. All conditional branches can be used after this instruction.
1320 static bool trans_CPC(DisasContext
*ctx
, arg_CPC
*a
)
1322 TCGv Rd
= cpu_r
[a
->rd
];
1323 TCGv Rr
= cpu_r
[a
->rr
];
1324 TCGv R
= tcg_temp_new_i32();
1325 TCGv zero
= tcg_const_i32(0);
1327 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr - Cf */
1328 tcg_gen_sub_tl(R
, R
, cpu_Cf
);
1329 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
1330 /* update status register */
1331 gen_sub_CHf(R
, Rd
, Rr
);
1332 gen_sub_Vf(R
, Rd
, Rr
);
1336 * Previous value remains unchanged when the result is zero;
1337 * cleared otherwise.
1339 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_Zf
, R
, zero
, cpu_Zf
, zero
);
1341 tcg_temp_free_i32(zero
);
1342 tcg_temp_free_i32(R
);
1348 * This instruction performs a compare between register Rd and a constant.
1349 * The register is not changed. All conditional branches can be used after this
1352 static bool trans_CPI(DisasContext
*ctx
, arg_CPI
*a
)
1354 TCGv Rd
= cpu_r
[a
->rd
];
1356 TCGv Rr
= tcg_const_i32(Imm
);
1357 TCGv R
= tcg_temp_new_i32();
1359 tcg_gen_sub_tl(R
, Rd
, Rr
); /* R = Rd - Rr */
1360 tcg_gen_andi_tl(R
, R
, 0xff); /* make it 8 bits */
1362 /* update status register */
1363 gen_sub_CHf(R
, Rd
, Rr
);
1364 gen_sub_Vf(R
, Rd
, Rr
);
1367 tcg_temp_free_i32(R
);
1368 tcg_temp_free_i32(Rr
);
1374 * This instruction tests a single bit in a register and skips the next
1375 * instruction if the bit is cleared.
1377 static bool trans_SBRC(DisasContext
*ctx
, arg_SBRC
*a
)
1379 TCGv Rr
= cpu_r
[a
->rr
];
1381 ctx
->skip_cond
= TCG_COND_EQ
;
1382 ctx
->skip_var0
= tcg_temp_new();
1383 ctx
->free_skip_var0
= true;
1385 tcg_gen_andi_tl(ctx
->skip_var0
, Rr
, 1 << a
->bit
);
1390 * This instruction tests a single bit in a register and skips the next
1391 * instruction if the bit is set.
1393 static bool trans_SBRS(DisasContext
*ctx
, arg_SBRS
*a
)
1395 TCGv Rr
= cpu_r
[a
->rr
];
1397 ctx
->skip_cond
= TCG_COND_NE
;
1398 ctx
->skip_var0
= tcg_temp_new();
1399 ctx
->free_skip_var0
= true;
1401 tcg_gen_andi_tl(ctx
->skip_var0
, Rr
, 1 << a
->bit
);
1406 * This instruction tests a single bit in an I/O Register and skips the
1407 * next instruction if the bit is cleared. This instruction operates on the
1408 * lower 32 I/O Registers -- addresses 0-31.
1410 static bool trans_SBIC(DisasContext
*ctx
, arg_SBIC
*a
)
1412 TCGv temp
= tcg_const_i32(a
->reg
);
1414 gen_helper_inb(temp
, cpu_env
, temp
);
1415 tcg_gen_andi_tl(temp
, temp
, 1 << a
->bit
);
1416 ctx
->skip_cond
= TCG_COND_EQ
;
1417 ctx
->skip_var0
= temp
;
1418 ctx
->free_skip_var0
= true;
1424 * This instruction tests a single bit in an I/O Register and skips the
1425 * next instruction if the bit is set. This instruction operates on the lower
1426 * 32 I/O Registers -- addresses 0-31.
1428 static bool trans_SBIS(DisasContext
*ctx
, arg_SBIS
*a
)
1430 TCGv temp
= tcg_const_i32(a
->reg
);
1432 gen_helper_inb(temp
, cpu_env
, temp
);
1433 tcg_gen_andi_tl(temp
, temp
, 1 << a
->bit
);
1434 ctx
->skip_cond
= TCG_COND_NE
;
1435 ctx
->skip_var0
= temp
;
1436 ctx
->free_skip_var0
= true;
1442 * Conditional relative branch. Tests a single bit in SREG and branches
1443 * relatively to PC if the bit is cleared. This instruction branches relatively
1444 * to PC in either direction (PC - 63 < = destination <= PC + 64). The
1445 * parameter k is the offset from PC and is represented in two's complement
1448 static bool trans_BRBC(DisasContext
*ctx
, arg_BRBC
*a
)
1450 TCGLabel
*not_taken
= gen_new_label();
1480 g_assert_not_reached();
1483 tcg_gen_brcondi_i32(TCG_COND_NE
, var
, 0, not_taken
);
1484 gen_goto_tb(ctx
, 0, ctx
->npc
+ a
->imm
);
1485 gen_set_label(not_taken
);
1487 ctx
->bstate
= DISAS_CHAIN
;
1492 * Conditional relative branch. Tests a single bit in SREG and branches
1493 * relatively to PC if the bit is set. This instruction branches relatively to
1494 * PC in either direction (PC - 63 < = destination <= PC + 64). The parameter k
1495 * is the offset from PC and is represented in two's complement form.
1497 static bool trans_BRBS(DisasContext
*ctx
, arg_BRBS
*a
)
1499 TCGLabel
*not_taken
= gen_new_label();
1529 g_assert_not_reached();
1532 tcg_gen_brcondi_i32(TCG_COND_EQ
, var
, 0, not_taken
);
1533 gen_goto_tb(ctx
, 0, ctx
->npc
+ a
->imm
);
1534 gen_set_label(not_taken
);
1536 ctx
->bstate
= DISAS_CHAIN
;
1541 * Data Transfer Instructions
1545 * in the gen_set_addr & gen_get_addr functions
1546 * H assumed to be in 0x00ff0000 format
1547 * M assumed to be in 0x000000ff format
1548 * L assumed to be in 0x000000ff format
1550 static void gen_set_addr(TCGv addr
, TCGv H
, TCGv M
, TCGv L
)
1553 tcg_gen_andi_tl(L
, addr
, 0x000000ff);
1555 tcg_gen_andi_tl(M
, addr
, 0x0000ff00);
1556 tcg_gen_shri_tl(M
, M
, 8);
1558 tcg_gen_andi_tl(H
, addr
, 0x00ff0000);
1561 static void gen_set_xaddr(TCGv addr
)
1563 gen_set_addr(addr
, cpu_rampX
, cpu_r
[27], cpu_r
[26]);
1566 static void gen_set_yaddr(TCGv addr
)
1568 gen_set_addr(addr
, cpu_rampY
, cpu_r
[29], cpu_r
[28]);
1571 static void gen_set_zaddr(TCGv addr
)
1573 gen_set_addr(addr
, cpu_rampZ
, cpu_r
[31], cpu_r
[30]);
1576 static TCGv
gen_get_addr(TCGv H
, TCGv M
, TCGv L
)
1578 TCGv addr
= tcg_temp_new_i32();
1580 tcg_gen_deposit_tl(addr
, M
, H
, 8, 8);
1581 tcg_gen_deposit_tl(addr
, L
, addr
, 8, 16);
1586 static TCGv
gen_get_xaddr(void)
1588 return gen_get_addr(cpu_rampX
, cpu_r
[27], cpu_r
[26]);
1591 static TCGv
gen_get_yaddr(void)
1593 return gen_get_addr(cpu_rampY
, cpu_r
[29], cpu_r
[28]);
1596 static TCGv
gen_get_zaddr(void)
1598 return gen_get_addr(cpu_rampZ
, cpu_r
[31], cpu_r
[30]);
1602 * Load one byte indirect from data space to register and stores an clear
1603 * the bits in data space specified by the register. The instruction can only
1604 * be used towards internal SRAM. The data location is pointed to by the Z (16
1605 * bits) Pointer Register in the Register File. Memory access is limited to the
1606 * current data segment of 64KB. To access another data segment in devices with
1607 * more than 64KB data space, the RAMPZ in register in the I/O area has to be
1608 * changed. The Z-pointer Register is left unchanged by the operation. This
1609 * instruction is especially suited for clearing status bits stored in SRAM.
1611 static void gen_data_store(DisasContext
*ctx
, TCGv data
, TCGv addr
)
1613 if (ctx
->tb
->flags
& TB_FLAGS_FULL_ACCESS
) {
1614 gen_helper_fullwr(cpu_env
, data
, addr
);
1616 tcg_gen_qemu_st8(data
, addr
, MMU_DATA_IDX
); /* mem[addr] = data */
1620 static void gen_data_load(DisasContext
*ctx
, TCGv data
, TCGv addr
)
1622 if (ctx
->tb
->flags
& TB_FLAGS_FULL_ACCESS
) {
1623 gen_helper_fullrd(data
, cpu_env
, addr
);
1625 tcg_gen_qemu_ld8u(data
, addr
, MMU_DATA_IDX
); /* data = mem[addr] */
1630 * This instruction makes a copy of one register into another. The source
1631 * register Rr is left unchanged, while the destination register Rd is loaded
1632 * with a copy of Rr.
1634 static bool trans_MOV(DisasContext
*ctx
, arg_MOV
*a
)
1636 TCGv Rd
= cpu_r
[a
->rd
];
1637 TCGv Rr
= cpu_r
[a
->rr
];
1639 tcg_gen_mov_tl(Rd
, Rr
);
1645 * This instruction makes a copy of one register pair into another register
1646 * pair. The source register pair Rr+1:Rr is left unchanged, while the
1647 * destination register pair Rd+1:Rd is loaded with a copy of Rr + 1:Rr. This
1648 * instruction is not available in all devices. Refer to the device specific
1649 * instruction set summary.
1651 static bool trans_MOVW(DisasContext
*ctx
, arg_MOVW
*a
)
1653 if (!avr_have_feature(ctx
, AVR_FEATURE_MOVW
)) {
1657 TCGv RdL
= cpu_r
[a
->rd
];
1658 TCGv RdH
= cpu_r
[a
->rd
+ 1];
1659 TCGv RrL
= cpu_r
[a
->rr
];
1660 TCGv RrH
= cpu_r
[a
->rr
+ 1];
1662 tcg_gen_mov_tl(RdH
, RrH
);
1663 tcg_gen_mov_tl(RdL
, RrL
);
1669 * Loads an 8 bit constant directly to register 16 to 31.
1671 static bool trans_LDI(DisasContext
*ctx
, arg_LDI
*a
)
1673 TCGv Rd
= cpu_r
[a
->rd
];
1676 tcg_gen_movi_tl(Rd
, imm
);
1682 * Loads one byte from the data space to a register. For parts with SRAM,
1683 * the data space consists of the Register File, I/O memory and internal SRAM
1684 * (and external SRAM if applicable). For parts without SRAM, the data space
1685 * consists of the register file only. The EEPROM has a separate address space.
1686 * A 16-bit address must be supplied. Memory access is limited to the current
1687 * data segment of 64KB. The LDS instruction uses the RAMPD Register to access
1688 * memory above 64KB. To access another data segment in devices with more than
1689 * 64KB data space, the RAMPD in register in the I/O area has to be changed.
1690 * This instruction is not available in all devices. Refer to the device
1691 * specific instruction set summary.
1693 static bool trans_LDS(DisasContext
*ctx
, arg_LDS
*a
)
1695 TCGv Rd
= cpu_r
[a
->rd
];
1696 TCGv addr
= tcg_temp_new_i32();
1698 a
->imm
= next_word(ctx
);
1700 tcg_gen_mov_tl(addr
, H
); /* addr = H:M:L */
1701 tcg_gen_shli_tl(addr
, addr
, 16);
1702 tcg_gen_ori_tl(addr
, addr
, a
->imm
);
1704 gen_data_load(ctx
, Rd
, addr
);
1706 tcg_temp_free_i32(addr
);
1712 * Loads one byte indirect from the data space to a register. For parts
1713 * with SRAM, the data space consists of the Register File, I/O memory and
1714 * internal SRAM (and external SRAM if applicable). For parts without SRAM, the
1715 * data space consists of the Register File only. In some parts the Flash
1716 * Memory has been mapped to the data space and can be read using this command.
1717 * The EEPROM has a separate address space. The data location is pointed to by
1718 * the X (16 bits) Pointer Register in the Register File. Memory access is
1719 * limited to the current data segment of 64KB. To access another data segment
1720 * in devices with more than 64KB data space, the RAMPX in register in the I/O
1721 * area has to be changed. The X-pointer Register can either be left unchanged
1722 * by the operation, or it can be post-incremented or predecremented. These
1723 * features are especially suited for accessing arrays, tables, and Stack
1724 * Pointer usage of the X-pointer Register. Note that only the low byte of the
1725 * X-pointer is updated in devices with no more than 256 bytes data space. For
1726 * such devices, the high byte of the pointer is not used by this instruction
1727 * and can be used for other purposes. The RAMPX Register in the I/O area is
1728 * updated in parts with more than 64KB data space or more than 64KB Program
1729 * memory, and the increment/decrement is added to the entire 24-bit address on
1730 * such devices. Not all variants of this instruction is available in all
1731 * devices. Refer to the device specific instruction set summary. In the
1732 * Reduced Core tinyAVR the LD instruction can be used to achieve the same
1733 * operation as LPM since the program memory is mapped to the data memory
1736 static bool trans_LDX1(DisasContext
*ctx
, arg_LDX1
*a
)
1738 TCGv Rd
= cpu_r
[a
->rd
];
1739 TCGv addr
= gen_get_xaddr();
1741 gen_data_load(ctx
, Rd
, addr
);
1743 tcg_temp_free_i32(addr
);
1748 static bool trans_LDX2(DisasContext
*ctx
, arg_LDX2
*a
)
1750 TCGv Rd
= cpu_r
[a
->rd
];
1751 TCGv addr
= gen_get_xaddr();
1753 gen_data_load(ctx
, Rd
, addr
);
1754 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
1756 gen_set_xaddr(addr
);
1758 tcg_temp_free_i32(addr
);
1763 static bool trans_LDX3(DisasContext
*ctx
, arg_LDX3
*a
)
1765 TCGv Rd
= cpu_r
[a
->rd
];
1766 TCGv addr
= gen_get_xaddr();
1768 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
1769 gen_data_load(ctx
, Rd
, addr
);
1770 gen_set_xaddr(addr
);
1772 tcg_temp_free_i32(addr
);
1778 * Loads one byte indirect with or without displacement from the data space
1779 * to a register. For parts with SRAM, the data space consists of the Register
1780 * File, I/O memory and internal SRAM (and external SRAM if applicable). For
1781 * parts without SRAM, the data space consists of the Register File only. In
1782 * some parts the Flash Memory has been mapped to the data space and can be
1783 * read using this command. The EEPROM has a separate address space. The data
1784 * location is pointed to by the Y (16 bits) Pointer Register in the Register
1785 * File. Memory access is limited to the current data segment of 64KB. To
1786 * access another data segment in devices with more than 64KB data space, the
1787 * RAMPY in register in the I/O area has to be changed. The Y-pointer Register
1788 * can either be left unchanged by the operation, or it can be post-incremented
1789 * or predecremented. These features are especially suited for accessing
1790 * arrays, tables, and Stack Pointer usage of the Y-pointer Register. Note that
1791 * only the low byte of the Y-pointer is updated in devices with no more than
1792 * 256 bytes data space. For such devices, the high byte of the pointer is not
1793 * used by this instruction and can be used for other purposes. The RAMPY
1794 * Register in the I/O area is updated in parts with more than 64KB data space
1795 * or more than 64KB Program memory, and the increment/decrement/displacement
1796 * is added to the entire 24-bit address on such devices. Not all variants of
1797 * this instruction is available in all devices. Refer to the device specific
1798 * instruction set summary. In the Reduced Core tinyAVR the LD instruction can
1799 * be used to achieve the same operation as LPM since the program memory is
1800 * mapped to the data memory space.
1802 static bool trans_LDY2(DisasContext
*ctx
, arg_LDY2
*a
)
1804 TCGv Rd
= cpu_r
[a
->rd
];
1805 TCGv addr
= gen_get_yaddr();
1807 gen_data_load(ctx
, Rd
, addr
);
1808 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
1810 gen_set_yaddr(addr
);
1812 tcg_temp_free_i32(addr
);
1817 static bool trans_LDY3(DisasContext
*ctx
, arg_LDY3
*a
)
1819 TCGv Rd
= cpu_r
[a
->rd
];
1820 TCGv addr
= gen_get_yaddr();
1822 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
1823 gen_data_load(ctx
, Rd
, addr
);
1824 gen_set_yaddr(addr
);
1826 tcg_temp_free_i32(addr
);
1831 static bool trans_LDDY(DisasContext
*ctx
, arg_LDDY
*a
)
1833 TCGv Rd
= cpu_r
[a
->rd
];
1834 TCGv addr
= gen_get_yaddr();
1836 tcg_gen_addi_tl(addr
, addr
, a
->imm
); /* addr = addr + q */
1837 gen_data_load(ctx
, Rd
, addr
);
1839 tcg_temp_free_i32(addr
);
1845 * Loads one byte indirect with or without displacement from the data space
1846 * to a register. For parts with SRAM, the data space consists of the Register
1847 * File, I/O memory and internal SRAM (and external SRAM if applicable). For
1848 * parts without SRAM, the data space consists of the Register File only. In
1849 * some parts the Flash Memory has been mapped to the data space and can be
1850 * read using this command. The EEPROM has a separate address space. The data
1851 * location is pointed to by the Z (16 bits) Pointer Register in the Register
1852 * File. Memory access is limited to the current data segment of 64KB. To
1853 * access another data segment in devices with more than 64KB data space, the
1854 * RAMPZ in register in the I/O area has to be changed. The Z-pointer Register
1855 * can either be left unchanged by the operation, or it can be post-incremented
1856 * or predecremented. These features are especially suited for Stack Pointer
1857 * usage of the Z-pointer Register, however because the Z-pointer Register can
1858 * be used for indirect subroutine calls, indirect jumps and table lookup, it
1859 * is often more convenient to use the X or Y-pointer as a dedicated Stack
1860 * Pointer. Note that only the low byte of the Z-pointer is updated in devices
1861 * with no more than 256 bytes data space. For such devices, the high byte of
1862 * the pointer is not used by this instruction and can be used for other
1863 * purposes. The RAMPZ Register in the I/O area is updated in parts with more
1864 * than 64KB data space or more than 64KB Program memory, and the
1865 * increment/decrement/displacement is added to the entire 24-bit address on
1866 * such devices. Not all variants of this instruction is available in all
1867 * devices. Refer to the device specific instruction set summary. In the
1868 * Reduced Core tinyAVR the LD instruction can be used to achieve the same
1869 * operation as LPM since the program memory is mapped to the data memory
1870 * space. For using the Z-pointer for table lookup in Program memory see the
1871 * LPM and ELPM instructions.
1873 static bool trans_LDZ2(DisasContext
*ctx
, arg_LDZ2
*a
)
1875 TCGv Rd
= cpu_r
[a
->rd
];
1876 TCGv addr
= gen_get_zaddr();
1878 gen_data_load(ctx
, Rd
, addr
);
1879 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
1881 gen_set_zaddr(addr
);
1883 tcg_temp_free_i32(addr
);
1888 static bool trans_LDZ3(DisasContext
*ctx
, arg_LDZ3
*a
)
1890 TCGv Rd
= cpu_r
[a
->rd
];
1891 TCGv addr
= gen_get_zaddr();
1893 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
1894 gen_data_load(ctx
, Rd
, addr
);
1896 gen_set_zaddr(addr
);
1898 tcg_temp_free_i32(addr
);
1903 static bool trans_LDDZ(DisasContext
*ctx
, arg_LDDZ
*a
)
1905 TCGv Rd
= cpu_r
[a
->rd
];
1906 TCGv addr
= gen_get_zaddr();
1908 tcg_gen_addi_tl(addr
, addr
, a
->imm
); /* addr = addr + q */
1909 gen_data_load(ctx
, Rd
, addr
);
1911 tcg_temp_free_i32(addr
);
1917 * Stores one byte from a Register to the data space. For parts with SRAM,
1918 * the data space consists of the Register File, I/O memory and internal SRAM
1919 * (and external SRAM if applicable). For parts without SRAM, the data space
1920 * consists of the Register File only. The EEPROM has a separate address space.
1921 * A 16-bit address must be supplied. Memory access is limited to the current
1922 * data segment of 64KB. The STS instruction uses the RAMPD Register to access
1923 * memory above 64KB. To access another data segment in devices with more than
1924 * 64KB data space, the RAMPD in register in the I/O area has to be changed.
1925 * This instruction is not available in all devices. Refer to the device
1926 * specific instruction set summary.
1928 static bool trans_STS(DisasContext
*ctx
, arg_STS
*a
)
1930 TCGv Rd
= cpu_r
[a
->rd
];
1931 TCGv addr
= tcg_temp_new_i32();
1933 a
->imm
= next_word(ctx
);
1935 tcg_gen_mov_tl(addr
, H
); /* addr = H:M:L */
1936 tcg_gen_shli_tl(addr
, addr
, 16);
1937 tcg_gen_ori_tl(addr
, addr
, a
->imm
);
1938 gen_data_store(ctx
, Rd
, addr
);
1940 tcg_temp_free_i32(addr
);
1946 * Stores one byte indirect from a register to data space. For parts with SRAM,
1947 * the data space consists of the Register File, I/O memory, and internal SRAM
1948 * (and external SRAM if applicable). For parts without SRAM, the data space
1949 * consists of the Register File only. The EEPROM has a separate address space.
1951 * The data location is pointed to by the X (16 bits) Pointer Register in the
1952 * Register File. Memory access is limited to the current data segment of 64KB.
1953 * To access another data segment in devices with more than 64KB data space, the
1954 * RAMPX in register in the I/O area has to be changed.
1956 * The X-pointer Register can either be left unchanged by the operation, or it
1957 * can be post-incremented or pre-decremented. These features are especially
1958 * suited for accessing arrays, tables, and Stack Pointer usage of the
1959 * X-pointer Register. Note that only the low byte of the X-pointer is updated
1960 * in devices with no more than 256 bytes data space. For such devices, the high
1961 * byte of the pointer is not used by this instruction and can be used for other
1962 * purposes. The RAMPX Register in the I/O area is updated in parts with more
1963 * than 64KB data space or more than 64KB Program memory, and the increment /
1964 * decrement is added to the entire 24-bit address on such devices.
1966 static bool trans_STX1(DisasContext
*ctx
, arg_STX1
*a
)
1968 TCGv Rd
= cpu_r
[a
->rr
];
1969 TCGv addr
= gen_get_xaddr();
1971 gen_data_store(ctx
, Rd
, addr
);
1973 tcg_temp_free_i32(addr
);
1978 static bool trans_STX2(DisasContext
*ctx
, arg_STX2
*a
)
1980 TCGv Rd
= cpu_r
[a
->rr
];
1981 TCGv addr
= gen_get_xaddr();
1983 gen_data_store(ctx
, Rd
, addr
);
1984 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
1985 gen_set_xaddr(addr
);
1987 tcg_temp_free_i32(addr
);
1992 static bool trans_STX3(DisasContext
*ctx
, arg_STX3
*a
)
1994 TCGv Rd
= cpu_r
[a
->rr
];
1995 TCGv addr
= gen_get_xaddr();
1997 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
1998 gen_data_store(ctx
, Rd
, addr
);
1999 gen_set_xaddr(addr
);
2001 tcg_temp_free_i32(addr
);
2007 * Stores one byte indirect with or without displacement from a register to data
2008 * space. For parts with SRAM, the data space consists of the Register File, I/O
2009 * memory, and internal SRAM (and external SRAM if applicable). For parts
2010 * without SRAM, the data space consists of the Register File only. The EEPROM
2011 * has a separate address space.
2013 * The data location is pointed to by the Y (16 bits) Pointer Register in the
2014 * Register File. Memory access is limited to the current data segment of 64KB.
2015 * To access another data segment in devices with more than 64KB data space, the
2016 * RAMPY in register in the I/O area has to be changed.
2018 * The Y-pointer Register can either be left unchanged by the operation, or it
2019 * can be post-incremented or pre-decremented. These features are especially
2020 * suited for accessing arrays, tables, and Stack Pointer usage of the Y-pointer
2021 * Register. Note that only the low byte of the Y-pointer is updated in devices
2022 * with no more than 256 bytes data space. For such devices, the high byte of
2023 * the pointer is not used by this instruction and can be used for other
2024 * purposes. The RAMPY Register in the I/O area is updated in parts with more
2025 * than 64KB data space or more than 64KB Program memory, and the increment /
2026 * decrement / displacement is added to the entire 24-bit address on such
2029 static bool trans_STY2(DisasContext
*ctx
, arg_STY2
*a
)
2031 TCGv Rd
= cpu_r
[a
->rd
];
2032 TCGv addr
= gen_get_yaddr();
2034 gen_data_store(ctx
, Rd
, addr
);
2035 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
2036 gen_set_yaddr(addr
);
2038 tcg_temp_free_i32(addr
);
2043 static bool trans_STY3(DisasContext
*ctx
, arg_STY3
*a
)
2045 TCGv Rd
= cpu_r
[a
->rd
];
2046 TCGv addr
= gen_get_yaddr();
2048 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
2049 gen_data_store(ctx
, Rd
, addr
);
2050 gen_set_yaddr(addr
);
2052 tcg_temp_free_i32(addr
);
2057 static bool trans_STDY(DisasContext
*ctx
, arg_STDY
*a
)
2059 TCGv Rd
= cpu_r
[a
->rd
];
2060 TCGv addr
= gen_get_yaddr();
2062 tcg_gen_addi_tl(addr
, addr
, a
->imm
); /* addr = addr + q */
2063 gen_data_store(ctx
, Rd
, addr
);
2065 tcg_temp_free_i32(addr
);
2071 * Stores one byte indirect with or without displacement from a register to data
2072 * space. For parts with SRAM, the data space consists of the Register File, I/O
2073 * memory, and internal SRAM (and external SRAM if applicable). For parts
2074 * without SRAM, the data space consists of the Register File only. The EEPROM
2075 * has a separate address space.
2077 * The data location is pointed to by the Y (16 bits) Pointer Register in the
2078 * Register File. Memory access is limited to the current data segment of 64KB.
2079 * To access another data segment in devices with more than 64KB data space, the
2080 * RAMPY in register in the I/O area has to be changed.
2082 * The Y-pointer Register can either be left unchanged by the operation, or it
2083 * can be post-incremented or pre-decremented. These features are especially
2084 * suited for accessing arrays, tables, and Stack Pointer usage of the Y-pointer
2085 * Register. Note that only the low byte of the Y-pointer is updated in devices
2086 * with no more than 256 bytes data space. For such devices, the high byte of
2087 * the pointer is not used by this instruction and can be used for other
2088 * purposes. The RAMPY Register in the I/O area is updated in parts with more
2089 * than 64KB data space or more than 64KB Program memory, and the increment /
2090 * decrement / displacement is added to the entire 24-bit address on such
2093 static bool trans_STZ2(DisasContext
*ctx
, arg_STZ2
*a
)
2095 TCGv Rd
= cpu_r
[a
->rd
];
2096 TCGv addr
= gen_get_zaddr();
2098 gen_data_store(ctx
, Rd
, addr
);
2099 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
2101 gen_set_zaddr(addr
);
2103 tcg_temp_free_i32(addr
);
2108 static bool trans_STZ3(DisasContext
*ctx
, arg_STZ3
*a
)
2110 TCGv Rd
= cpu_r
[a
->rd
];
2111 TCGv addr
= gen_get_zaddr();
2113 tcg_gen_subi_tl(addr
, addr
, 1); /* addr = addr - 1 */
2114 gen_data_store(ctx
, Rd
, addr
);
2116 gen_set_zaddr(addr
);
2118 tcg_temp_free_i32(addr
);
2123 static bool trans_STDZ(DisasContext
*ctx
, arg_STDZ
*a
)
2125 TCGv Rd
= cpu_r
[a
->rd
];
2126 TCGv addr
= gen_get_zaddr();
2128 tcg_gen_addi_tl(addr
, addr
, a
->imm
); /* addr = addr + q */
2129 gen_data_store(ctx
, Rd
, addr
);
2131 tcg_temp_free_i32(addr
);
2137 * Loads one byte pointed to by the Z-register into the destination
2138 * register Rd. This instruction features a 100% space effective constant
2139 * initialization or constant data fetch. The Program memory is organized in
2140 * 16-bit words while the Z-pointer is a byte address. Thus, the least
2141 * significant bit of the Z-pointer selects either low byte (ZLSB = 0) or high
2142 * byte (ZLSB = 1). This instruction can address the first 64KB (32K words) of
2143 * Program memory. The Zpointer Register can either be left unchanged by the
2144 * operation, or it can be incremented. The incrementation does not apply to
2145 * the RAMPZ Register.
2147 * Devices with Self-Programming capability can use the LPM instruction to read
2148 * the Fuse and Lock bit values.
2150 static bool trans_LPM1(DisasContext
*ctx
, arg_LPM1
*a
)
2152 if (!avr_have_feature(ctx
, AVR_FEATURE_LPM
)) {
2157 TCGv addr
= tcg_temp_new_i32();
2161 tcg_gen_shli_tl(addr
, H
, 8); /* addr = H:L */
2162 tcg_gen_or_tl(addr
, addr
, L
);
2163 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2165 tcg_temp_free_i32(addr
);
2170 static bool trans_LPM2(DisasContext
*ctx
, arg_LPM2
*a
)
2172 if (!avr_have_feature(ctx
, AVR_FEATURE_LPM
)) {
2176 TCGv Rd
= cpu_r
[a
->rd
];
2177 TCGv addr
= tcg_temp_new_i32();
2181 tcg_gen_shli_tl(addr
, H
, 8); /* addr = H:L */
2182 tcg_gen_or_tl(addr
, addr
, L
);
2183 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2185 tcg_temp_free_i32(addr
);
2190 static bool trans_LPMX(DisasContext
*ctx
, arg_LPMX
*a
)
2192 if (!avr_have_feature(ctx
, AVR_FEATURE_LPMX
)) {
2196 TCGv Rd
= cpu_r
[a
->rd
];
2197 TCGv addr
= tcg_temp_new_i32();
2201 tcg_gen_shli_tl(addr
, H
, 8); /* addr = H:L */
2202 tcg_gen_or_tl(addr
, addr
, L
);
2203 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2204 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
2205 tcg_gen_andi_tl(L
, addr
, 0xff);
2206 tcg_gen_shri_tl(addr
, addr
, 8);
2207 tcg_gen_andi_tl(H
, addr
, 0xff);
2209 tcg_temp_free_i32(addr
);
2215 * Loads one byte pointed to by the Z-register and the RAMPZ Register in
2216 * the I/O space, and places this byte in the destination register Rd. This
2217 * instruction features a 100% space effective constant initialization or
2218 * constant data fetch. The Program memory is organized in 16-bit words while
2219 * the Z-pointer is a byte address. Thus, the least significant bit of the
2220 * Z-pointer selects either low byte (ZLSB = 0) or high byte (ZLSB = 1). This
2221 * instruction can address the entire Program memory space. The Z-pointer
2222 * Register can either be left unchanged by the operation, or it can be
2223 * incremented. The incrementation applies to the entire 24-bit concatenation
2224 * of the RAMPZ and Z-pointer Registers.
2226 * Devices with Self-Programming capability can use the ELPM instruction to
2227 * read the Fuse and Lock bit value.
2229 static bool trans_ELPM1(DisasContext
*ctx
, arg_ELPM1
*a
)
2231 if (!avr_have_feature(ctx
, AVR_FEATURE_ELPM
)) {
2236 TCGv addr
= gen_get_zaddr();
2238 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2240 tcg_temp_free_i32(addr
);
2245 static bool trans_ELPM2(DisasContext
*ctx
, arg_ELPM2
*a
)
2247 if (!avr_have_feature(ctx
, AVR_FEATURE_ELPM
)) {
2251 TCGv Rd
= cpu_r
[a
->rd
];
2252 TCGv addr
= gen_get_zaddr();
2254 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2256 tcg_temp_free_i32(addr
);
2261 static bool trans_ELPMX(DisasContext
*ctx
, arg_ELPMX
*a
)
2263 if (!avr_have_feature(ctx
, AVR_FEATURE_ELPMX
)) {
2267 TCGv Rd
= cpu_r
[a
->rd
];
2268 TCGv addr
= gen_get_zaddr();
2270 tcg_gen_qemu_ld8u(Rd
, addr
, MMU_CODE_IDX
); /* Rd = mem[addr] */
2271 tcg_gen_addi_tl(addr
, addr
, 1); /* addr = addr + 1 */
2272 gen_set_zaddr(addr
);
2274 tcg_temp_free_i32(addr
);
2280 * SPM can be used to erase a page in the Program memory, to write a page
2281 * in the Program memory (that is already erased), and to set Boot Loader Lock
2282 * bits. In some devices, the Program memory can be written one word at a time,
2283 * in other devices an entire page can be programmed simultaneously after first
2284 * filling a temporary page buffer. In all cases, the Program memory must be
2285 * erased one page at a time. When erasing the Program memory, the RAMPZ and
2286 * Z-register are used as page address. When writing the Program memory, the
2287 * RAMPZ and Z-register are used as page or word address, and the R1:R0
2288 * register pair is used as data(1). When setting the Boot Loader Lock bits,
2289 * the R1:R0 register pair is used as data. Refer to the device documentation
2290 * for detailed description of SPM usage. This instruction can address the
2291 * entire Program memory.
2293 * The SPM instruction is not available in all devices. Refer to the device
2294 * specific instruction set summary.
2296 * Note: 1. R1 determines the instruction high byte, and R0 determines the
2297 * instruction low byte.
2299 static bool trans_SPM(DisasContext
*ctx
, arg_SPM
*a
)
2302 if (!avr_have_feature(ctx
, AVR_FEATURE_SPM
)) {
2309 static bool trans_SPMX(DisasContext
*ctx
, arg_SPMX
*a
)
2312 if (!avr_have_feature(ctx
, AVR_FEATURE_SPMX
)) {
2320 * Loads data from the I/O Space (Ports, Timers, Configuration Registers,
2321 * etc.) into register Rd in the Register File.
2323 static bool trans_IN(DisasContext
*ctx
, arg_IN
*a
)
2325 TCGv Rd
= cpu_r
[a
->rd
];
2326 TCGv port
= tcg_const_i32(a
->imm
);
2328 gen_helper_inb(Rd
, cpu_env
, port
);
2330 tcg_temp_free_i32(port
);
2336 * Stores data from register Rr in the Register File to I/O Space (Ports,
2337 * Timers, Configuration Registers, etc.).
2339 static bool trans_OUT(DisasContext
*ctx
, arg_OUT
*a
)
2341 TCGv Rd
= cpu_r
[a
->rd
];
2342 TCGv port
= tcg_const_i32(a
->imm
);
2344 gen_helper_outb(cpu_env
, port
, Rd
);
2346 tcg_temp_free_i32(port
);
2352 * This instruction stores the contents of register Rr on the STACK. The
2353 * Stack Pointer is post-decremented by 1 after the PUSH. This instruction is
2354 * not available in all devices. Refer to the device specific instruction set
2357 static bool trans_PUSH(DisasContext
*ctx
, arg_PUSH
*a
)
2359 TCGv Rd
= cpu_r
[a
->rd
];
2361 gen_data_store(ctx
, Rd
, cpu_sp
);
2362 tcg_gen_subi_tl(cpu_sp
, cpu_sp
, 1);
2368 * This instruction loads register Rd with a byte from the STACK. The Stack
2369 * Pointer is pre-incremented by 1 before the POP. This instruction is not
2370 * available in all devices. Refer to the device specific instruction set
2373 static bool trans_POP(DisasContext
*ctx
, arg_POP
*a
)
2376 * Using a temp to work around some strange behaviour:
2377 * tcg_gen_addi_tl(cpu_sp, cpu_sp, 1);
2378 * gen_data_load(ctx, Rd, cpu_sp);
2379 * seems to cause the add to happen twice.
2380 * This doesn't happen if either the add or the load is removed.
2382 TCGv t1
= tcg_temp_new_i32();
2383 TCGv Rd
= cpu_r
[a
->rd
];
2385 tcg_gen_addi_tl(t1
, cpu_sp
, 1);
2386 gen_data_load(ctx
, Rd
, t1
);
2387 tcg_gen_mov_tl(cpu_sp
, t1
);
2393 * Exchanges one byte indirect between register and data space. The data
2394 * location is pointed to by the Z (16 bits) Pointer Register in the Register
2395 * File. Memory access is limited to the current data segment of 64KB. To
2396 * access another data segment in devices with more than 64KB data space, the
2397 * RAMPZ in register in the I/O area has to be changed.
2399 * The Z-pointer Register is left unchanged by the operation. This instruction
2400 * is especially suited for writing/reading status bits stored in SRAM.
2402 static bool trans_XCH(DisasContext
*ctx
, arg_XCH
*a
)
2404 if (!avr_have_feature(ctx
, AVR_FEATURE_RMW
)) {
2408 TCGv Rd
= cpu_r
[a
->rd
];
2409 TCGv t0
= tcg_temp_new_i32();
2410 TCGv addr
= gen_get_zaddr();
2412 gen_data_load(ctx
, t0
, addr
);
2413 gen_data_store(ctx
, Rd
, addr
);
2414 tcg_gen_mov_tl(Rd
, t0
);
2416 tcg_temp_free_i32(t0
);
2417 tcg_temp_free_i32(addr
);
2423 * Load one byte indirect from data space to register and set bits in data
2424 * space specified by the register. The instruction can only be used towards
2425 * internal SRAM. The data location is pointed to by the Z (16 bits) Pointer
2426 * Register in the Register File. Memory access is limited to the current data
2427 * segment of 64KB. To access another data segment in devices with more than
2428 * 64KB data space, the RAMPZ in register in the I/O area has to be changed.
2430 * The Z-pointer Register is left unchanged by the operation. This instruction
2431 * is especially suited for setting status bits stored in SRAM.
2433 static bool trans_LAS(DisasContext
*ctx
, arg_LAS
*a
)
2435 if (!avr_have_feature(ctx
, AVR_FEATURE_RMW
)) {
2439 TCGv Rr
= cpu_r
[a
->rd
];
2440 TCGv addr
= gen_get_zaddr();
2441 TCGv t0
= tcg_temp_new_i32();
2442 TCGv t1
= tcg_temp_new_i32();
2444 gen_data_load(ctx
, t0
, addr
); /* t0 = mem[addr] */
2445 tcg_gen_or_tl(t1
, t0
, Rr
);
2446 tcg_gen_mov_tl(Rr
, t0
); /* Rr = t0 */
2447 gen_data_store(ctx
, t1
, addr
); /* mem[addr] = t1 */
2449 tcg_temp_free_i32(t1
);
2450 tcg_temp_free_i32(t0
);
2451 tcg_temp_free_i32(addr
);
2457 * Load one byte indirect from data space to register and stores and clear
2458 * the bits in data space specified by the register. The instruction can
2459 * only be used towards internal SRAM. The data location is pointed to by
2460 * the Z (16 bits) Pointer Register in the Register File. Memory access is
2461 * limited to the current data segment of 64KB. To access another data
2462 * segment in devices with more than 64KB data space, the RAMPZ in register
2463 * in the I/O area has to be changed.
2465 * The Z-pointer Register is left unchanged by the operation. This instruction
2466 * is especially suited for clearing status bits stored in SRAM.
2468 static bool trans_LAC(DisasContext
*ctx
, arg_LAC
*a
)
2470 if (!avr_have_feature(ctx
, AVR_FEATURE_RMW
)) {
2474 TCGv Rr
= cpu_r
[a
->rd
];
2475 TCGv addr
= gen_get_zaddr();
2476 TCGv t0
= tcg_temp_new_i32();
2477 TCGv t1
= tcg_temp_new_i32();
2479 gen_data_load(ctx
, t0
, addr
); /* t0 = mem[addr] */
2480 tcg_gen_andc_tl(t1
, t0
, Rr
); /* t1 = t0 & (0xff - Rr) = t0 & ~Rr */
2481 tcg_gen_mov_tl(Rr
, t0
); /* Rr = t0 */
2482 gen_data_store(ctx
, t1
, addr
); /* mem[addr] = t1 */
2484 tcg_temp_free_i32(t1
);
2485 tcg_temp_free_i32(t0
);
2486 tcg_temp_free_i32(addr
);
2493 * Load one byte indirect from data space to register and toggles bits in
2494 * the data space specified by the register. The instruction can only be used
2495 * towards SRAM. The data location is pointed to by the Z (16 bits) Pointer
2496 * Register in the Register File. Memory access is limited to the current data
2497 * segment of 64KB. To access another data segment in devices with more than
2498 * 64KB data space, the RAMPZ in register in the I/O area has to be changed.
2500 * The Z-pointer Register is left unchanged by the operation. This instruction
2501 * is especially suited for changing status bits stored in SRAM.
2503 static bool trans_LAT(DisasContext
*ctx
, arg_LAT
*a
)
2505 if (!avr_have_feature(ctx
, AVR_FEATURE_RMW
)) {
2509 TCGv Rd
= cpu_r
[a
->rd
];
2510 TCGv addr
= gen_get_zaddr();
2511 TCGv t0
= tcg_temp_new_i32();
2512 TCGv t1
= tcg_temp_new_i32();
2514 gen_data_load(ctx
, t0
, addr
); /* t0 = mem[addr] */
2515 tcg_gen_xor_tl(t1
, t0
, Rd
);
2516 tcg_gen_mov_tl(Rd
, t0
); /* Rd = t0 */
2517 gen_data_store(ctx
, t1
, addr
); /* mem[addr] = t1 */
2519 tcg_temp_free_i32(t1
);
2520 tcg_temp_free_i32(t0
);
2521 tcg_temp_free_i32(addr
);
2527 * Bit and Bit-test Instructions
2529 static void gen_rshift_ZNVSf(TCGv R
)
2531 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, R
, 0); /* Zf = R == 0 */
2532 tcg_gen_shri_tl(cpu_Nf
, R
, 7); /* Nf = R(7) */
2533 tcg_gen_xor_tl(cpu_Vf
, cpu_Nf
, cpu_Cf
);
2534 tcg_gen_xor_tl(cpu_Sf
, cpu_Nf
, cpu_Vf
); /* Sf = Nf ^ Vf */
2538 * Shifts all bits in Rd one place to the right. Bit 7 is cleared. Bit 0 is
2539 * loaded into the C Flag of the SREG. This operation effectively divides an
2540 * unsigned value by two. The C Flag can be used to round the result.
2542 static bool trans_LSR(DisasContext
*ctx
, arg_LSR
*a
)
2544 TCGv Rd
= cpu_r
[a
->rd
];
2546 tcg_gen_andi_tl(cpu_Cf
, Rd
, 1);
2547 tcg_gen_shri_tl(Rd
, Rd
, 1);
2549 /* update status register */
2550 tcg_gen_setcondi_tl(TCG_COND_EQ
, cpu_Zf
, Rd
, 0); /* Zf = Rd == 0 */
2551 tcg_gen_movi_tl(cpu_Nf
, 0);
2552 tcg_gen_mov_tl(cpu_Vf
, cpu_Cf
);
2553 tcg_gen_mov_tl(cpu_Sf
, cpu_Vf
);
2559 * Shifts all bits in Rd one place to the right. The C Flag is shifted into
2560 * bit 7 of Rd. Bit 0 is shifted into the C Flag. This operation, combined
2561 * with ASR, effectively divides multi-byte signed values by two. Combined with
2562 * LSR it effectively divides multi-byte unsigned values by two. The Carry Flag
2563 * can be used to round the result.
2565 static bool trans_ROR(DisasContext
*ctx
, arg_ROR
*a
)
2567 TCGv Rd
= cpu_r
[a
->rd
];
2568 TCGv t0
= tcg_temp_new_i32();
2570 tcg_gen_shli_tl(t0
, cpu_Cf
, 7);
2572 /* update status register */
2573 tcg_gen_andi_tl(cpu_Cf
, Rd
, 1);
2575 /* update output register */
2576 tcg_gen_shri_tl(Rd
, Rd
, 1);
2577 tcg_gen_or_tl(Rd
, Rd
, t0
);
2579 /* update status register */
2580 gen_rshift_ZNVSf(Rd
);
2582 tcg_temp_free_i32(t0
);
2588 * Shifts all bits in Rd one place to the right. Bit 7 is held constant. Bit 0
2589 * is loaded into the C Flag of the SREG. This operation effectively divides a
2590 * signed value by two without changing its sign. The Carry Flag can be used to
2593 static bool trans_ASR(DisasContext
*ctx
, arg_ASR
*a
)
2595 TCGv Rd
= cpu_r
[a
->rd
];
2596 TCGv t0
= tcg_temp_new_i32();
2598 /* update status register */
2599 tcg_gen_andi_tl(cpu_Cf
, Rd
, 1); /* Cf = Rd(0) */
2601 /* update output register */
2602 tcg_gen_andi_tl(t0
, Rd
, 0x80); /* Rd = (Rd & 0x80) | (Rd >> 1) */
2603 tcg_gen_shri_tl(Rd
, Rd
, 1);
2604 tcg_gen_or_tl(Rd
, Rd
, t0
);
2606 /* update status register */
2607 gen_rshift_ZNVSf(Rd
);
2609 tcg_temp_free_i32(t0
);
2615 * Swaps high and low nibbles in a register.
2617 static bool trans_SWAP(DisasContext
*ctx
, arg_SWAP
*a
)
2619 TCGv Rd
= cpu_r
[a
->rd
];
2620 TCGv t0
= tcg_temp_new_i32();
2621 TCGv t1
= tcg_temp_new_i32();
2623 tcg_gen_andi_tl(t0
, Rd
, 0x0f);
2624 tcg_gen_shli_tl(t0
, t0
, 4);
2625 tcg_gen_andi_tl(t1
, Rd
, 0xf0);
2626 tcg_gen_shri_tl(t1
, t1
, 4);
2627 tcg_gen_or_tl(Rd
, t0
, t1
);
2629 tcg_temp_free_i32(t1
);
2630 tcg_temp_free_i32(t0
);
2636 * Sets a specified bit in an I/O Register. This instruction operates on
2637 * the lower 32 I/O Registers -- addresses 0-31.
2639 static bool trans_SBI(DisasContext
*ctx
, arg_SBI
*a
)
2641 TCGv data
= tcg_temp_new_i32();
2642 TCGv port
= tcg_const_i32(a
->reg
);
2644 gen_helper_inb(data
, cpu_env
, port
);
2645 tcg_gen_ori_tl(data
, data
, 1 << a
->bit
);
2646 gen_helper_outb(cpu_env
, port
, data
);
2648 tcg_temp_free_i32(port
);
2649 tcg_temp_free_i32(data
);
2655 * Clears a specified bit in an I/O Register. This instruction operates on
2656 * the lower 32 I/O Registers -- addresses 0-31.
2658 static bool trans_CBI(DisasContext
*ctx
, arg_CBI
*a
)
2660 TCGv data
= tcg_temp_new_i32();
2661 TCGv port
= tcg_const_i32(a
->reg
);
2663 gen_helper_inb(data
, cpu_env
, port
);
2664 tcg_gen_andi_tl(data
, data
, ~(1 << a
->bit
));
2665 gen_helper_outb(cpu_env
, port
, data
);
2667 tcg_temp_free_i32(data
);
2668 tcg_temp_free_i32(port
);
2674 * Stores bit b from Rd to the T Flag in SREG (Status Register).
2676 static bool trans_BST(DisasContext
*ctx
, arg_BST
*a
)
2678 TCGv Rd
= cpu_r
[a
->rd
];
2680 tcg_gen_andi_tl(cpu_Tf
, Rd
, 1 << a
->bit
);
2681 tcg_gen_shri_tl(cpu_Tf
, cpu_Tf
, a
->bit
);
2687 * Copies the T Flag in the SREG (Status Register) to bit b in register Rd.
2689 static bool trans_BLD(DisasContext
*ctx
, arg_BLD
*a
)
2691 TCGv Rd
= cpu_r
[a
->rd
];
2692 TCGv t1
= tcg_temp_new_i32();
2694 tcg_gen_andi_tl(Rd
, Rd
, ~(1u << a
->bit
)); /* clear bit */
2695 tcg_gen_shli_tl(t1
, cpu_Tf
, a
->bit
); /* create mask */
2696 tcg_gen_or_tl(Rd
, Rd
, t1
);
2698 tcg_temp_free_i32(t1
);
2704 * Sets a single Flag or bit in SREG.
2706 static bool trans_BSET(DisasContext
*ctx
, arg_BSET
*a
)
2710 tcg_gen_movi_tl(cpu_Cf
, 0x01);
2713 tcg_gen_movi_tl(cpu_Zf
, 0x01);
2716 tcg_gen_movi_tl(cpu_Nf
, 0x01);
2719 tcg_gen_movi_tl(cpu_Vf
, 0x01);
2722 tcg_gen_movi_tl(cpu_Sf
, 0x01);
2725 tcg_gen_movi_tl(cpu_Hf
, 0x01);
2728 tcg_gen_movi_tl(cpu_Tf
, 0x01);
2731 tcg_gen_movi_tl(cpu_If
, 0x01);
2739 * Clears a single Flag in SREG.
2741 static bool trans_BCLR(DisasContext
*ctx
, arg_BCLR
*a
)
2745 tcg_gen_movi_tl(cpu_Cf
, 0x00);
2748 tcg_gen_movi_tl(cpu_Zf
, 0x00);
2751 tcg_gen_movi_tl(cpu_Nf
, 0x00);
2754 tcg_gen_movi_tl(cpu_Vf
, 0x00);
2757 tcg_gen_movi_tl(cpu_Sf
, 0x00);
2760 tcg_gen_movi_tl(cpu_Hf
, 0x00);
2763 tcg_gen_movi_tl(cpu_Tf
, 0x00);
2766 tcg_gen_movi_tl(cpu_If
, 0x00);
2774 * MCU Control Instructions
2778 * The BREAK instruction is used by the On-chip Debug system, and is
2779 * normally not used in the application software. When the BREAK instruction is
2780 * executed, the AVR CPU is set in the Stopped Mode. This gives the On-chip
2781 * Debugger access to internal resources. If any Lock bits are set, or either
2782 * the JTAGEN or OCDEN Fuses are unprogrammed, the CPU will treat the BREAK
2783 * instruction as a NOP and will not enter the Stopped mode. This instruction
2784 * is not available in all devices. Refer to the device specific instruction
2787 static bool trans_BREAK(DisasContext
*ctx
, arg_BREAK
*a
)
2789 if (!avr_have_feature(ctx
, AVR_FEATURE_BREAK
)) {
2793 #ifdef BREAKPOINT_ON_BREAK
2794 tcg_gen_movi_tl(cpu_pc
, ctx
->npc
- 1);
2795 gen_helper_debug(cpu_env
);
2796 ctx
->bstate
= DISAS_EXIT
;
2805 * This instruction performs a single cycle No Operation.
2807 static bool trans_NOP(DisasContext
*ctx
, arg_NOP
*a
)
2816 * This instruction sets the circuit in sleep mode defined by the MCU
2819 static bool trans_SLEEP(DisasContext
*ctx
, arg_SLEEP
*a
)
2821 gen_helper_sleep(cpu_env
);
2822 ctx
->bstate
= DISAS_NORETURN
;
2827 * This instruction resets the Watchdog Timer. This instruction must be
2828 * executed within a limited time given by the WD prescaler. See the Watchdog
2829 * Timer hardware specification.
2831 static bool trans_WDR(DisasContext
*ctx
, arg_WDR
*a
)
2833 gen_helper_wdr(cpu_env
);
2839 * Core translation mechanism functions:
2842 * - canonicalize_skip()
2843 * - gen_intermediate_code()
2844 * - restore_state_to_opc()
2847 static void translate(DisasContext
*ctx
)
2849 uint32_t opcode
= next_word(ctx
);
2851 if (!decode_insn(ctx
, opcode
)) {
2852 gen_helper_unsupported(cpu_env
);
2853 ctx
->bstate
= DISAS_NORETURN
;
2857 /* Standardize the cpu_skip condition to NE. */
2858 static bool canonicalize_skip(DisasContext
*ctx
)
2860 switch (ctx
->skip_cond
) {
2861 case TCG_COND_NEVER
:
2862 /* Normal case: cpu_skip is known to be false. */
2865 case TCG_COND_ALWAYS
:
2867 * Breakpoint case: cpu_skip is known to be true, via TB_FLAGS_SKIP.
2868 * The breakpoint is on the instruction being skipped, at the start
2869 * of the TranslationBlock. No need to update.
2874 if (ctx
->skip_var1
== NULL
) {
2875 tcg_gen_mov_tl(cpu_skip
, ctx
->skip_var0
);
2877 tcg_gen_xor_tl(cpu_skip
, ctx
->skip_var0
, ctx
->skip_var1
);
2878 ctx
->skip_var1
= NULL
;
2883 /* Convert to a NE condition vs 0. */
2884 if (ctx
->skip_var1
== NULL
) {
2885 tcg_gen_setcondi_tl(ctx
->skip_cond
, cpu_skip
, ctx
->skip_var0
, 0);
2887 tcg_gen_setcond_tl(ctx
->skip_cond
, cpu_skip
,
2888 ctx
->skip_var0
, ctx
->skip_var1
);
2889 ctx
->skip_var1
= NULL
;
2891 ctx
->skip_cond
= TCG_COND_NE
;
2894 if (ctx
->free_skip_var0
) {
2895 tcg_temp_free(ctx
->skip_var0
);
2896 ctx
->free_skip_var0
= false;
2898 ctx
->skip_var0
= cpu_skip
;
2902 void gen_intermediate_code(CPUState
*cs
, TranslationBlock
*tb
, int max_insns
)
2904 CPUAVRState
*env
= cs
->env_ptr
;
2905 DisasContext ctx
= {
2910 .bstate
= DISAS_NEXT
,
2911 .skip_cond
= TCG_COND_NEVER
,
2912 .singlestep
= cs
->singlestep_enabled
,
2914 target_ulong pc_start
= tb
->pc
/ 2;
2917 if (tb
->flags
& TB_FLAGS_FULL_ACCESS
) {
2919 * This flag is set by ST/LD instruction we will regenerate it ONLY
2920 * with mem/cpu memory access instead of mem access
2924 if (ctx
.singlestep
) {
2931 if (tb
->flags
& TB_FLAGS_SKIP
) {
2932 ctx
.skip_cond
= TCG_COND_ALWAYS
;
2933 ctx
.skip_var0
= cpu_skip
;
2937 TCGLabel
*skip_label
= NULL
;
2939 /* translate current instruction */
2940 tcg_gen_insn_start(ctx
.npc
);
2944 * this is due to some strange GDB behavior
2945 * let's assume main has address 0x100
2946 * b main - sets breakpoint at address 0x00000100 (code)
2947 * b *0x100 - sets breakpoint at address 0x00800100 (data)
2949 if (unlikely(!ctx
.singlestep
&&
2950 (cpu_breakpoint_test(cs
, OFFSET_CODE
+ ctx
.npc
* 2, BP_ANY
) ||
2951 cpu_breakpoint_test(cs
, OFFSET_DATA
+ ctx
.npc
* 2, BP_ANY
)))) {
2952 canonicalize_skip(&ctx
);
2953 tcg_gen_movi_tl(cpu_pc
, ctx
.npc
);
2954 gen_helper_debug(cpu_env
);
2955 goto done_generating
;
2958 /* Conditionally skip the next instruction, if indicated. */
2959 if (ctx
.skip_cond
!= TCG_COND_NEVER
) {
2960 skip_label
= gen_new_label();
2961 if (ctx
.skip_var0
== cpu_skip
) {
2963 * Copy cpu_skip so that we may zero it before the branch.
2964 * This ensures that cpu_skip is non-zero after the label
2965 * if and only if the skipped insn itself sets a skip.
2967 ctx
.free_skip_var0
= true;
2968 ctx
.skip_var0
= tcg_temp_new();
2969 tcg_gen_mov_tl(ctx
.skip_var0
, cpu_skip
);
2970 tcg_gen_movi_tl(cpu_skip
, 0);
2972 if (ctx
.skip_var1
== NULL
) {
2973 tcg_gen_brcondi_tl(ctx
.skip_cond
, ctx
.skip_var0
, 0, skip_label
);
2975 tcg_gen_brcond_tl(ctx
.skip_cond
, ctx
.skip_var0
,
2976 ctx
.skip_var1
, skip_label
);
2977 ctx
.skip_var1
= NULL
;
2979 if (ctx
.free_skip_var0
) {
2980 tcg_temp_free(ctx
.skip_var0
);
2981 ctx
.free_skip_var0
= false;
2983 ctx
.skip_cond
= TCG_COND_NEVER
;
2984 ctx
.skip_var0
= NULL
;
2990 canonicalize_skip(&ctx
);
2991 gen_set_label(skip_label
);
2992 if (ctx
.bstate
== DISAS_NORETURN
) {
2993 ctx
.bstate
= DISAS_CHAIN
;
2996 } while (ctx
.bstate
== DISAS_NEXT
2997 && num_insns
< max_insns
2998 && (ctx
.npc
- pc_start
) * 2 < TARGET_PAGE_SIZE
- 4
2999 && !tcg_op_buf_full());
3001 if (tb
->cflags
& CF_LAST_IO
) {
3005 bool nonconst_skip
= canonicalize_skip(&ctx
);
3007 switch (ctx
.bstate
) {
3008 case DISAS_NORETURN
:
3009 assert(!nonconst_skip
);
3012 case DISAS_TOO_MANY
:
3014 if (!nonconst_skip
) {
3015 /* Note gen_goto_tb checks singlestep. */
3016 gen_goto_tb(&ctx
, 1, ctx
.npc
);
3019 tcg_gen_movi_tl(cpu_pc
, ctx
.npc
);
3022 if (!ctx
.singlestep
) {
3023 tcg_gen_lookup_and_goto_ptr();
3028 if (ctx
.singlestep
) {
3029 gen_helper_debug(cpu_env
);
3031 tcg_gen_exit_tb(NULL
, 0);
3035 g_assert_not_reached();
3039 gen_tb_end(tb
, num_insns
);
3041 tb
->size
= (ctx
.npc
- pc_start
) * 2;
3042 tb
->icount
= num_insns
;
3045 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)
3046 && qemu_log_in_addr_range(tb
->pc
)) {
3048 fd
= qemu_log_lock();
3049 qemu_log("IN: %s\n", lookup_symbol(tb
->pc
));
3050 log_target_disas(cs
, tb
->pc
, tb
->size
);
3052 qemu_log_unlock(fd
);
3057 void restore_state_to_opc(CPUAVRState
*env
, TranslationBlock
*tb
,
3060 env
->pc_w
= data
[0];