Fix: symbols eliminated by --gc-sections still trigger warnings for gnu.warning.SYM
[binutils-gdb.git] / sim / avr / interp.c
blobeef4ad96ec2fa0ff99fdf42f7cb3eab0b1386327
1 /* Simulator for Atmel's AVR core.
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 Written by Tristan Gingold, AdaCore.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
21 #include "defs.h"
23 #include <string.h>
25 #include "bfd.h"
26 #include "libiberty.h"
27 #include "sim/sim.h"
29 #include "sim-main.h"
30 #include "sim-base.h"
31 #include "sim-options.h"
32 #include "sim-signal.h"
33 #include "avr-sim.h"
35 /* As AVR is a 8/16 bits processor, define handy types. */
36 typedef unsigned short int word;
37 typedef signed short int sword;
38 typedef unsigned char byte;
39 typedef signed char sbyte;
41 /* Max size of I space (which is always flash on avr). */
42 #define MAX_AVR_FLASH (128 * 1024)
43 #define PC_MASK (MAX_AVR_FLASH - 1)
45 /* Mac size of D space. */
46 #define MAX_AVR_SRAM (64 * 1024)
47 #define SRAM_MASK (MAX_AVR_SRAM - 1)
49 /* D space offset in ELF file. */
50 #define SRAM_VADDR 0x800000
52 /* Simulator specific ports. */
53 #define STDIO_PORT 0x52
54 #define EXIT_PORT 0x4F
55 #define ABORT_PORT 0x49
57 /* GDB defined register numbers. */
58 #define AVR_SREG_REGNUM 32
59 #define AVR_SP_REGNUM 33
60 #define AVR_PC_REGNUM 34
62 /* Memory mapped registers. */
63 #define SREG 0x5F
64 #define REG_SP 0x5D
65 #define EIND 0x5C
66 #define RAMPZ 0x5B
68 #define REGX 0x1a
69 #define REGY 0x1c
70 #define REGZ 0x1e
71 #define REGZ_LO 0x1e
72 #define REGZ_HI 0x1f
74 /* Sreg (status) bits. */
75 #define SREG_I 0x80
76 #define SREG_T 0x40
77 #define SREG_H 0x20
78 #define SREG_S 0x10
79 #define SREG_V 0x08
80 #define SREG_N 0x04
81 #define SREG_Z 0x02
82 #define SREG_C 0x01
84 /* In order to speed up emulation we use a simple approach:
85 a code is associated with each instruction. The pre-decoding occurs
86 usually once when the instruction is first seen.
87 This works well because I&D spaces are separated.
89 Missing opcodes: sleep, spm, wdr (as they are mmcu dependent).
91 enum avr_opcode
93 /* Opcode not yet decoded. */
94 OP_unknown,
95 OP_bad,
97 OP_nop,
99 OP_rjmp,
100 OP_rcall,
101 OP_ret,
102 OP_reti,
104 OP_break,
106 OP_brbs,
107 OP_brbc,
109 OP_bset,
110 OP_bclr,
112 OP_bld,
113 OP_bst,
115 OP_sbrc,
116 OP_sbrs,
118 OP_eor,
119 OP_and,
120 OP_andi,
121 OP_or,
122 OP_ori,
123 OP_com,
124 OP_swap,
125 OP_neg,
127 OP_out,
128 OP_in,
129 OP_cbi,
130 OP_sbi,
132 OP_sbic,
133 OP_sbis,
135 OP_ldi,
136 OP_cpse,
137 OP_cp,
138 OP_cpi,
139 OP_cpc,
140 OP_sub,
141 OP_sbc,
142 OP_sbiw,
143 OP_adiw,
144 OP_add,
145 OP_adc,
146 OP_subi,
147 OP_sbci,
148 OP_inc,
149 OP_dec,
150 OP_lsr,
151 OP_ror,
152 OP_asr,
154 OP_mul,
155 OP_muls,
156 OP_mulsu,
157 OP_fmul,
158 OP_fmuls,
159 OP_fmulsu,
161 OP_mov,
162 OP_movw,
164 OP_push,
165 OP_pop,
167 OP_st_X,
168 OP_st_dec_X,
169 OP_st_X_inc,
170 OP_st_Y_inc,
171 OP_st_dec_Y,
172 OP_st_Z_inc,
173 OP_st_dec_Z,
174 OP_std_Y,
175 OP_std_Z,
176 OP_ldd_Y,
177 OP_ldd_Z,
178 OP_ld_Z_inc,
179 OP_ld_dec_Z,
180 OP_ld_Y_inc,
181 OP_ld_dec_Y,
182 OP_ld_X,
183 OP_ld_X_inc,
184 OP_ld_dec_X,
186 OP_lpm,
187 OP_lpm_Z,
188 OP_lpm_inc_Z,
189 OP_elpm,
190 OP_elpm_Z,
191 OP_elpm_inc_Z,
193 OP_ijmp,
194 OP_icall,
196 OP_eijmp,
197 OP_eicall,
199 /* 2 words opcodes. */
200 #define OP_2words OP_jmp
201 OP_jmp,
202 OP_call,
203 OP_sts,
204 OP_lds
207 struct avr_insn_cell
209 /* The insn (16 bits). */
210 word op;
212 /* Pre-decoding code. */
213 enum avr_opcode code : 8;
214 /* One byte of additional information. */
215 byte r;
218 /* I&D memories. */
219 /* TODO: Should be moved to SIM_CPU. */
220 static struct avr_insn_cell flash[MAX_AVR_FLASH];
221 static byte sram[MAX_AVR_SRAM];
223 /* Sign extend a value. */
224 static int sign_ext (word val, int nb_bits)
226 if (val & (1 << (nb_bits - 1)))
227 return val | -(1 << nb_bits);
228 return val;
231 /* Insn field extractors. */
233 /* Extract xxxx_xxxRx_xxxx_RRRR. */
234 static inline byte get_r (word op)
236 return (op & 0xf) | ((op >> 5) & 0x10);
239 /* Extract xxxx_xxxxx_xxxx_RRRR. */
240 static inline byte get_r16 (word op)
242 return 16 + (op & 0xf);
245 /* Extract xxxx_xxxxx_xxxx_xRRR. */
246 static inline byte get_r16_23 (word op)
248 return 16 + (op & 0x7);
251 /* Extract xxxx_xxxD_DDDD_xxxx. */
252 static inline byte get_d (word op)
254 return (op >> 4) & 0x1f;
257 /* Extract xxxx_xxxx_DDDD_xxxx. */
258 static inline byte get_d16 (word op)
260 return 16 + ((op >> 4) & 0x0f);
263 /* Extract xxxx_xxxx_xDDD_xxxx. */
264 static inline byte get_d16_23 (word op)
266 return 16 + ((op >> 4) & 0x07);
269 /* Extract xxxx_xAAx_xxxx_AAAA. */
270 static inline byte get_A (word op)
272 return (op & 0x0f) | ((op & 0x600) >> 5);
275 /* Extract xxxx_xxxx_AAAA_Axxx. */
276 static inline byte get_biA (word op)
278 return (op >> 3) & 0x1f;
281 /* Extract xxxx_KKKK_xxxx_KKKK. */
282 static inline byte get_K (word op)
284 return (op & 0xf) | ((op & 0xf00) >> 4);
287 /* Extract xxxx_xxKK_KKKK_Kxxx. */
288 static inline int get_k (word op)
290 return sign_ext ((op & 0x3f8) >> 3, 7);
293 /* Extract xxxx_xxxx_xxDD_xxxx. */
294 static inline byte get_d24 (word op)
296 return 24 + ((op >> 3) & 6);
299 /* Extract xxxx_xxxx_KKxx_KKKK. */
300 static inline byte get_k6 (word op)
302 return (op & 0xf) | ((op >> 2) & 0x30);
305 /* Extract xxQx_QQxx_xxxx_xQQQ. */
306 static inline byte get_q (word op)
308 return (op & 7) | ((op >> 7) & 0x18)| ((op >> 8) & 0x20);
311 /* Extract xxxx_xxxx_xxxx_xBBB. */
312 static inline byte get_b (word op)
314 return (op & 7);
317 /* AVR is little endian. */
318 static inline word
319 read_word (unsigned int addr)
321 return sram[addr] | (sram[addr + 1] << 8);
324 static inline void
325 write_word (unsigned int addr, word w)
327 sram[addr] = w;
328 sram[addr + 1] = w >> 8;
331 static inline word
332 read_word_post_inc (unsigned int addr)
334 word v = read_word (addr);
335 write_word (addr, v + 1);
336 return v;
339 static inline word
340 read_word_pre_dec (unsigned int addr)
342 word v = read_word (addr) - 1;
343 write_word (addr, v);
344 return v;
347 static void
348 update_flags_logic (byte res)
350 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
351 if (res == 0)
352 sram[SREG] |= SREG_Z;
353 if (res & 0x80)
354 sram[SREG] |= SREG_N | SREG_S;
357 static void
358 update_flags_add (byte r, byte a, byte b)
360 byte carry;
362 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
363 if (r & 0x80)
364 sram[SREG] |= SREG_N;
365 carry = (a & b) | (a & ~r) | (b & ~r);
366 if (carry & 0x08)
367 sram[SREG] |= SREG_H;
368 if (carry & 0x80)
369 sram[SREG] |= SREG_C;
370 if (((a & b & ~r) | (~a & ~b & r)) & 0x80)
371 sram[SREG] |= SREG_V;
372 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
373 sram[SREG] |= SREG_S;
374 if (r == 0)
375 sram[SREG] |= SREG_Z;
378 static void update_flags_sub (byte r, byte a, byte b)
380 byte carry;
382 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
383 if (r & 0x80)
384 sram[SREG] |= SREG_N;
385 carry = (~a & b) | (b & r) | (r & ~a);
386 if (carry & 0x08)
387 sram[SREG] |= SREG_H;
388 if (carry & 0x80)
389 sram[SREG] |= SREG_C;
390 if (((a & ~b & ~r) | (~a & b & r)) & 0x80)
391 sram[SREG] |= SREG_V;
392 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
393 sram[SREG] |= SREG_S;
394 /* Note: Z is not set. */
397 static enum avr_opcode
398 decode (unsigned int pc)
400 word op1 = flash[pc].op;
402 switch ((op1 >> 12) & 0x0f)
404 case 0x0:
405 switch ((op1 >> 10) & 0x3)
407 case 0x0:
408 switch ((op1 >> 8) & 0x3)
410 case 0x0:
411 if (op1 == 0)
412 return OP_nop;
413 break;
414 case 0x1:
415 return OP_movw;
416 case 0x2:
417 return OP_muls;
418 case 0x3:
419 if (op1 & 0x80)
421 if (op1 & 0x08)
422 return OP_fmulsu;
423 else
424 return OP_fmuls;
426 else
428 if (op1 & 0x08)
429 return OP_fmul;
430 else
431 return OP_mulsu;
434 break;
435 case 0x1:
436 return OP_cpc;
437 case 0x2:
438 flash[pc].r = SREG_C;
439 return OP_sbc;
440 case 0x3:
441 flash[pc].r = 0;
442 return OP_add;
444 break;
445 case 0x1:
446 switch ((op1 >> 10) & 0x3)
448 case 0x0:
449 return OP_cpse;
450 case 0x1:
451 return OP_cp;
452 case 0x2:
453 flash[pc].r = 0;
454 return OP_sub;
455 case 0x3:
456 flash[pc].r = SREG_C;
457 return OP_adc;
459 break;
460 case 0x2:
461 switch ((op1 >> 10) & 0x3)
463 case 0x0:
464 return OP_and;
465 case 0x1:
466 return OP_eor;
467 case 0x2:
468 return OP_or;
469 case 0x3:
470 return OP_mov;
472 break;
473 case 0x3:
474 return OP_cpi;
475 case 0x4:
476 return OP_sbci;
477 case 0x5:
478 return OP_subi;
479 case 0x6:
480 return OP_ori;
481 case 0x7:
482 return OP_andi;
483 case 0x8:
484 case 0xa:
485 if (op1 & 0x0200)
487 if (op1 & 0x0008)
489 flash[pc].r = get_q (op1);
490 return OP_std_Y;
492 else
494 flash[pc].r = get_q (op1);
495 return OP_std_Z;
498 else
500 if (op1 & 0x0008)
502 flash[pc].r = get_q (op1);
503 return OP_ldd_Y;
505 else
507 flash[pc].r = get_q (op1);
508 return OP_ldd_Z;
511 break;
512 case 0x9: /* 9xxx */
513 switch ((op1 >> 8) & 0xf)
515 case 0x0:
516 case 0x1:
517 switch ((op1 >> 0) & 0xf)
519 case 0x0:
520 return OP_lds;
521 case 0x1:
522 return OP_ld_Z_inc;
523 case 0x2:
524 return OP_ld_dec_Z;
525 case 0x4:
526 return OP_lpm_Z;
527 case 0x5:
528 return OP_lpm_inc_Z;
529 case 0x6:
530 return OP_elpm_Z;
531 case 0x7:
532 return OP_elpm_inc_Z;
533 case 0x9:
534 return OP_ld_Y_inc;
535 case 0xa:
536 return OP_ld_dec_Y;
537 case 0xc:
538 return OP_ld_X;
539 case 0xd:
540 return OP_ld_X_inc;
541 case 0xe:
542 return OP_ld_dec_X;
543 case 0xf:
544 return OP_pop;
546 break;
547 case 0x2:
548 case 0x3:
549 switch ((op1 >> 0) & 0xf)
551 case 0x0:
552 return OP_sts;
553 case 0x1:
554 return OP_st_Z_inc;
555 case 0x2:
556 return OP_st_dec_Z;
557 case 0x9:
558 return OP_st_Y_inc;
559 case 0xa:
560 return OP_st_dec_Y;
561 case 0xc:
562 return OP_st_X;
563 case 0xd:
564 return OP_st_X_inc;
565 case 0xe:
566 return OP_st_dec_X;
567 case 0xf:
568 return OP_push;
570 break;
571 case 0x4:
572 case 0x5:
573 switch (op1 & 0xf)
575 case 0x0:
576 return OP_com;
577 case 0x1:
578 return OP_neg;
579 case 0x2:
580 return OP_swap;
581 case 0x3:
582 return OP_inc;
583 case 0x5:
584 flash[pc].r = 0x80;
585 return OP_asr;
586 case 0x6:
587 flash[pc].r = 0;
588 return OP_lsr;
589 case 0x7:
590 return OP_ror;
591 case 0x8: /* 9[45]x8 */
592 switch ((op1 >> 4) & 0x1f)
594 case 0x00:
595 case 0x01:
596 case 0x02:
597 case 0x03:
598 case 0x04:
599 case 0x05:
600 case 0x06:
601 case 0x07:
602 return OP_bset;
603 case 0x08:
604 case 0x09:
605 case 0x0a:
606 case 0x0b:
607 case 0x0c:
608 case 0x0d:
609 case 0x0e:
610 case 0x0f:
611 return OP_bclr;
612 case 0x10:
613 return OP_ret;
614 case 0x11:
615 return OP_reti;
616 case 0x19:
617 return OP_break;
618 case 0x1c:
619 return OP_lpm;
620 case 0x1d:
621 return OP_elpm;
622 default:
623 break;
625 break;
626 case 0x9: /* 9[45]x9 */
627 switch ((op1 >> 4) & 0x1f)
629 case 0x00:
630 return OP_ijmp;
631 case 0x01:
632 return OP_eijmp;
633 case 0x10:
634 return OP_icall;
635 case 0x11:
636 return OP_eicall;
637 default:
638 break;
640 break;
641 case 0xa:
642 return OP_dec;
643 case 0xc:
644 case 0xd:
645 flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
646 return OP_jmp;
647 case 0xe:
648 case 0xf:
649 flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
650 return OP_call;
652 break;
653 case 0x6:
654 return OP_adiw;
655 case 0x7:
656 return OP_sbiw;
657 case 0x8:
658 return OP_cbi;
659 case 0x9:
660 return OP_sbic;
661 case 0xa:
662 return OP_sbi;
663 case 0xb:
664 return OP_sbis;
665 case 0xc:
666 case 0xd:
667 case 0xe:
668 case 0xf:
669 return OP_mul;
671 break;
672 case 0xb:
673 flash[pc].r = get_A (op1);
674 if (((op1 >> 11) & 1) == 0)
675 return OP_in;
676 else
677 return OP_out;
678 case 0xc:
679 return OP_rjmp;
680 case 0xd:
681 return OP_rcall;
682 case 0xe:
683 return OP_ldi;
684 case 0xf:
685 switch ((op1 >> 9) & 7)
687 case 0:
688 case 1:
689 flash[pc].r = 1 << (op1 & 7);
690 return OP_brbs;
691 case 2:
692 case 3:
693 flash[pc].r = 1 << (op1 & 7);
694 return OP_brbc;
695 case 4:
696 if ((op1 & 8) == 0)
698 flash[pc].r = 1 << (op1 & 7);
699 return OP_bld;
701 break;
702 case 5:
703 if ((op1 & 8) == 0)
705 flash[pc].r = 1 << (op1 & 7);
706 return OP_bst;
708 break;
709 case 6:
710 if ((op1 & 8) == 0)
712 flash[pc].r = 1 << (op1 & 7);
713 return OP_sbrc;
715 break;
716 case 7:
717 if ((op1 & 8) == 0)
719 flash[pc].r = 1 << (op1 & 7);
720 return OP_sbrs;
722 break;
726 return OP_bad;
729 static void
730 do_call (SIM_CPU *cpu, unsigned int npc)
732 const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
733 struct avr_sim_cpu *avr_cpu = AVR_SIM_CPU (cpu);
734 unsigned int sp = read_word (REG_SP);
736 /* Big endian! */
737 sram[sp--] = avr_cpu->pc;
738 sram[sp--] = avr_cpu->pc >> 8;
739 if (state->avr_pc22)
741 sram[sp--] = avr_cpu->pc >> 16;
742 avr_cpu->cycles++;
744 write_word (REG_SP, sp);
745 avr_cpu->pc = npc & PC_MASK;
746 avr_cpu->cycles += 3;
749 static int
750 get_insn_length (unsigned int p)
752 if (flash[p].code == OP_unknown)
753 flash[p].code = decode(p);
754 if (flash[p].code >= OP_2words)
755 return 2;
756 else
757 return 1;
760 static unsigned int
761 get_z (void)
763 return (sram[RAMPZ] << 16) | (sram[REGZ_HI] << 8) | sram[REGZ_LO];
766 static unsigned char
767 get_lpm (unsigned int addr)
769 word w;
771 w = flash[(addr >> 1) & PC_MASK].op;
772 if (addr & 1)
773 w >>= 8;
774 return w;
777 static void
778 gen_mul (SIM_CPU *cpu, unsigned int res)
780 struct avr_sim_cpu *avr_cpu = AVR_SIM_CPU (cpu);
782 write_word (0, res);
783 sram[SREG] &= ~(SREG_Z | SREG_C);
784 if (res == 0)
785 sram[SREG] |= SREG_Z;
786 if (res & 0x8000)
787 sram[SREG] |= SREG_C;
788 avr_cpu->cycles++;
791 static void
792 step_once (SIM_CPU *cpu)
794 struct avr_sim_cpu *avr_cpu = AVR_SIM_CPU (cpu);
795 unsigned int ipc;
797 int code;
798 word op;
799 byte res;
800 byte r, d, vd;
802 again:
803 code = flash[avr_cpu->pc].code;
804 op = flash[avr_cpu->pc].op;
806 #if 0
807 if (tracing && code != OP_unknown)
809 if (verbose > 0) {
810 int flags;
811 int i;
813 sim_cb_eprintf (callback, "R00-07:");
814 for (i = 0; i < 8; i++)
815 sim_cb_eprintf (callback, " %02x", sram[i]);
816 sim_cb_eprintf (callback, " -");
817 for (i = 8; i < 16; i++)
818 sim_cb_eprintf (callback, " %02x", sram[i]);
819 sim_cb_eprintf (callback, " SP: %02x %02x",
820 sram[REG_SP + 1], sram[REG_SP]);
821 sim_cb_eprintf (callback, "\n");
822 sim_cb_eprintf (callback, "R16-31:");
823 for (i = 16; i < 24; i++)
824 sim_cb_eprintf (callback, " %02x", sram[i]);
825 sim_cb_eprintf (callback, " -");
826 for (i = 24; i < 32; i++)
827 sim_cb_eprintf (callback, " %02x", sram[i]);
828 sim_cb_eprintf (callback, " ");
829 flags = sram[SREG];
830 for (i = 0; i < 8; i++)
831 sim_cb_eprintf (callback, "%c",
832 flags & (0x80 >> i) ? "ITHSVNZC"[i] : '-');
833 sim_cb_eprintf (callback, "\n");
836 if (!tracing)
837 sim_cb_eprintf (callback, "%06x: %04x\n", 2 * avr_cpu->pc, flash[avr_cpu->pc].op);
838 else
840 sim_cb_eprintf (callback, "pc=0x%06x insn=0x%04x code=%d r=%d\n",
841 2 * avr_cpu->pc, flash[avr_cpu->pc].op, code, flash[avr_cpu->pc].r);
842 disassemble_insn (CPU_STATE (cpu), avr_cpu->pc);
843 sim_cb_eprintf (callback, "\n");
846 #endif
848 ipc = avr_cpu->pc;
849 avr_cpu->pc = (avr_cpu->pc + 1) & PC_MASK;
850 avr_cpu->cycles++;
852 switch (code)
854 case OP_unknown:
855 flash[ipc].code = decode(ipc);
856 avr_cpu->pc = ipc;
857 avr_cpu->cycles--;
858 goto again;
860 case OP_nop:
861 break;
863 case OP_jmp:
864 /* 2 words instruction, but we don't care about the pc. */
865 avr_cpu->pc = ((flash[ipc].r << 16) | flash[ipc + 1].op) & PC_MASK;
866 avr_cpu->cycles += 2;
867 break;
869 case OP_eijmp:
870 avr_cpu->pc = ((sram[EIND] << 16) | read_word (REGZ)) & PC_MASK;
871 avr_cpu->cycles += 2;
872 break;
874 case OP_ijmp:
875 avr_cpu->pc = read_word (REGZ) & PC_MASK;
876 avr_cpu->cycles += 1;
877 break;
879 case OP_call:
880 /* 2 words instruction. */
881 avr_cpu->pc++;
882 do_call (cpu, (flash[ipc].r << 16) | flash[ipc + 1].op);
883 break;
885 case OP_eicall:
886 do_call (cpu, (sram[EIND] << 16) | read_word (REGZ));
887 break;
889 case OP_icall:
890 do_call (cpu, read_word (REGZ));
891 break;
893 case OP_rcall:
894 do_call (cpu, avr_cpu->pc + sign_ext (op & 0xfff, 12));
895 break;
897 case OP_reti:
898 sram[SREG] |= SREG_I;
899 /* Fall through */
900 case OP_ret:
902 const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
903 unsigned int sp = read_word (REG_SP);
904 if (state->avr_pc22)
906 avr_cpu->pc = sram[++sp] << 16;
907 avr_cpu->cycles++;
909 else
910 avr_cpu->pc = 0;
911 avr_cpu->pc |= sram[++sp] << 8;
912 avr_cpu->pc |= sram[++sp];
913 write_word (REG_SP, sp);
915 avr_cpu->cycles += 3;
916 break;
918 case OP_break:
919 /* Stop on this address. */
920 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, ipc, sim_stopped, SIM_SIGTRAP);
921 break;
923 case OP_bld:
924 d = get_d (op);
925 r = flash[ipc].r;
926 if (sram[SREG] & SREG_T)
927 sram[d] |= r;
928 else
929 sram[d] &= ~r;
930 break;
932 case OP_bst:
933 if (sram[get_d (op)] & flash[ipc].r)
934 sram[SREG] |= SREG_T;
935 else
936 sram[SREG] &= ~SREG_T;
937 break;
939 case OP_sbrc:
940 case OP_sbrs:
941 if (((sram[get_d (op)] & flash[ipc].r) == 0) ^ ((op & 0x0200) != 0))
943 int l = get_insn_length (avr_cpu->pc);
944 avr_cpu->pc += l;
945 avr_cpu->cycles += l;
947 break;
949 case OP_push:
951 unsigned int sp = read_word (REG_SP);
952 sram[sp--] = sram[get_d (op)];
953 write_word (REG_SP, sp);
955 avr_cpu->cycles++;
956 break;
958 case OP_pop:
960 unsigned int sp = read_word (REG_SP);
961 sram[get_d (op)] = sram[++sp];
962 write_word (REG_SP, sp);
964 avr_cpu->cycles++;
965 break;
967 case OP_bclr:
968 sram[SREG] &= ~(1 << ((op >> 4) & 0x7));
969 break;
971 case OP_bset:
972 sram[SREG] |= 1 << ((op >> 4) & 0x7);
973 break;
975 case OP_rjmp:
976 avr_cpu->pc = (avr_cpu->pc + sign_ext (op & 0xfff, 12)) & PC_MASK;
977 avr_cpu->cycles++;
978 break;
980 case OP_eor:
981 d = get_d (op);
982 res = sram[d] ^ sram[get_r (op)];
983 sram[d] = res;
984 update_flags_logic (res);
985 break;
987 case OP_and:
988 d = get_d (op);
989 res = sram[d] & sram[get_r (op)];
990 sram[d] = res;
991 update_flags_logic (res);
992 break;
994 case OP_andi:
995 d = get_d16 (op);
996 res = sram[d] & get_K (op);
997 sram[d] = res;
998 update_flags_logic (res);
999 break;
1001 case OP_or:
1002 d = get_d (op);
1003 res = sram[d] | sram[get_r (op)];
1004 sram[d] = res;
1005 update_flags_logic (res);
1006 break;
1008 case OP_ori:
1009 d = get_d16 (op);
1010 res = sram[d] | get_K (op);
1011 sram[d] = res;
1012 update_flags_logic (res);
1013 break;
1015 case OP_com:
1016 d = get_d (op);
1017 res = ~sram[d];
1018 sram[d] = res;
1019 update_flags_logic (res);
1020 sram[SREG] |= SREG_C;
1021 break;
1023 case OP_swap:
1024 d = get_d (op);
1025 vd = sram[d];
1026 sram[d] = (vd >> 4) | (vd << 4);
1027 break;
1029 case OP_neg:
1030 d = get_d (op);
1031 vd = sram[d];
1032 res = -vd;
1033 sram[d] = res;
1034 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1035 if (res == 0)
1036 sram[SREG] |= SREG_Z;
1037 else
1038 sram[SREG] |= SREG_C;
1039 if (res == 0x80)
1040 sram[SREG] |= SREG_V | SREG_N;
1041 else if (res & 0x80)
1042 sram[SREG] |= SREG_N | SREG_S;
1043 if ((res | vd) & 0x08)
1044 sram[SREG] |= SREG_H;
1045 break;
1047 case OP_inc:
1048 d = get_d (op);
1049 res = sram[d] + 1;
1050 sram[d] = res;
1051 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
1052 if (res == 0x80)
1053 sram[SREG] |= SREG_V | SREG_N;
1054 else if (res & 0x80)
1055 sram[SREG] |= SREG_N | SREG_S;
1056 else if (res == 0)
1057 sram[SREG] |= SREG_Z;
1058 break;
1060 case OP_dec:
1061 d = get_d (op);
1062 res = sram[d] - 1;
1063 sram[d] = res;
1064 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
1065 if (res == 0x7f)
1066 sram[SREG] |= SREG_V | SREG_S;
1067 else if (res & 0x80)
1068 sram[SREG] |= SREG_N | SREG_S;
1069 else if (res == 0)
1070 sram[SREG] |= SREG_Z;
1071 break;
1073 case OP_lsr:
1074 case OP_asr:
1075 d = get_d (op);
1076 vd = sram[d];
1077 res = (vd >> 1) | (vd & flash[ipc].r);
1078 sram[d] = res;
1079 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1080 if (vd & 1)
1081 sram[SREG] |= SREG_C | SREG_S;
1082 if (res & 0x80)
1083 sram[SREG] |= SREG_N;
1084 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
1085 sram[SREG] |= SREG_V;
1086 if (res == 0)
1087 sram[SREG] |= SREG_Z;
1088 break;
1090 case OP_ror:
1091 d = get_d (op);
1092 vd = sram[d];
1093 res = vd >> 1 | (sram[SREG] << 7);
1094 sram[d] = res;
1095 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1096 if (vd & 1)
1097 sram[SREG] |= SREG_C | SREG_S;
1098 if (res & 0x80)
1099 sram[SREG] |= SREG_N;
1100 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
1101 sram[SREG] |= SREG_V;
1102 if (res == 0)
1103 sram[SREG] |= SREG_Z;
1104 break;
1106 case OP_mul:
1107 gen_mul (cpu, (word)sram[get_r (op)] * (word)sram[get_d (op)]);
1108 break;
1110 case OP_muls:
1111 gen_mul (cpu, (sword)(sbyte)sram[get_r16 (op)]
1112 * (sword)(sbyte)sram[get_d16 (op)]);
1113 break;
1115 case OP_mulsu:
1116 gen_mul (cpu, (sword)(word)sram[get_r16_23 (op)]
1117 * (sword)(sbyte)sram[get_d16_23 (op)]);
1118 break;
1120 case OP_fmul:
1121 gen_mul (cpu, ((word)sram[get_r16_23 (op)]
1122 * (word)sram[get_d16_23 (op)]) << 1);
1123 break;
1125 case OP_fmuls:
1126 gen_mul (cpu, ((sword)(sbyte)sram[get_r16_23 (op)]
1127 * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
1128 break;
1130 case OP_fmulsu:
1131 gen_mul (cpu, ((sword)(word)sram[get_r16_23 (op)]
1132 * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
1133 break;
1135 case OP_adc:
1136 case OP_add:
1137 r = sram[get_r (op)];
1138 d = get_d (op);
1139 vd = sram[d];
1140 res = r + vd + (sram[SREG] & flash[ipc].r);
1141 sram[d] = res;
1142 update_flags_add (res, vd, r);
1143 break;
1145 case OP_sub:
1146 d = get_d (op);
1147 vd = sram[d];
1148 r = sram[get_r (op)];
1149 res = vd - r;
1150 sram[d] = res;
1151 update_flags_sub (res, vd, r);
1152 if (res == 0)
1153 sram[SREG] |= SREG_Z;
1154 break;
1156 case OP_sbc:
1158 byte old = sram[SREG];
1159 d = get_d (op);
1160 vd = sram[d];
1161 r = sram[get_r (op)];
1162 res = vd - r - (old & SREG_C);
1163 sram[d] = res;
1164 update_flags_sub (res, vd, r);
1165 if (res == 0 && (old & SREG_Z))
1166 sram[SREG] |= SREG_Z;
1168 break;
1170 case OP_subi:
1171 d = get_d16 (op);
1172 vd = sram[d];
1173 r = get_K (op);
1174 res = vd - r;
1175 sram[d] = res;
1176 update_flags_sub (res, vd, r);
1177 if (res == 0)
1178 sram[SREG] |= SREG_Z;
1179 break;
1181 case OP_sbci:
1183 byte old = sram[SREG];
1185 d = get_d16 (op);
1186 vd = sram[d];
1187 r = get_K (op);
1188 res = vd - r - (old & SREG_C);
1189 sram[d] = res;
1190 update_flags_sub (res, vd, r);
1191 if (res == 0 && (old & SREG_Z))
1192 sram[SREG] |= SREG_Z;
1194 break;
1196 case OP_mov:
1197 sram[get_d (op)] = sram[get_r (op)];
1198 break;
1200 case OP_movw:
1201 d = (op & 0xf0) >> 3;
1202 r = (op & 0x0f) << 1;
1203 sram[d] = sram[r];
1204 sram[d + 1] = sram[r + 1];
1205 break;
1207 case OP_out:
1208 d = get_A (op) + 0x20;
1209 res = sram[get_d (op)];
1210 sram[d] = res;
1211 if (d == STDIO_PORT)
1212 putchar (res);
1213 else if (d == EXIT_PORT)
1214 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, avr_cpu->pc, sim_exited, 0);
1215 else if (d == ABORT_PORT)
1216 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, avr_cpu->pc, sim_exited, 1);
1217 break;
1219 case OP_in:
1220 d = get_A (op) + 0x20;
1221 sram[get_d (op)] = sram[d];
1222 break;
1224 case OP_cbi:
1225 d = get_biA (op) + 0x20;
1226 sram[d] &= ~(1 << get_b(op));
1227 break;
1229 case OP_sbi:
1230 d = get_biA (op) + 0x20;
1231 sram[d] |= 1 << get_b(op);
1232 break;
1234 case OP_sbic:
1235 if (!(sram[get_biA (op) + 0x20] & 1 << get_b(op)))
1237 int l = get_insn_length (avr_cpu->pc);
1238 avr_cpu->pc += l;
1239 avr_cpu->cycles += l;
1241 break;
1243 case OP_sbis:
1244 if (sram[get_biA (op) + 0x20] & 1 << get_b(op))
1246 int l = get_insn_length (avr_cpu->pc);
1247 avr_cpu->pc += l;
1248 avr_cpu->cycles += l;
1250 break;
1252 case OP_ldi:
1253 res = get_K (op);
1254 d = get_d16 (op);
1255 sram[d] = res;
1256 break;
1258 case OP_lds:
1259 sram[get_d (op)] = sram[flash[avr_cpu->pc].op];
1260 avr_cpu->pc++;
1261 avr_cpu->cycles++;
1262 break;
1264 case OP_sts:
1265 sram[flash[avr_cpu->pc].op] = sram[get_d (op)];
1266 avr_cpu->pc++;
1267 avr_cpu->cycles++;
1268 break;
1270 case OP_cpse:
1271 if (sram[get_r (op)] == sram[get_d (op)])
1273 int l = get_insn_length (avr_cpu->pc);
1274 avr_cpu->pc += l;
1275 avr_cpu->cycles += l;
1277 break;
1279 case OP_cp:
1280 r = sram[get_r (op)];
1281 d = sram[get_d (op)];
1282 res = d - r;
1283 update_flags_sub (res, d, r);
1284 if (res == 0)
1285 sram[SREG] |= SREG_Z;
1286 break;
1288 case OP_cpi:
1289 r = get_K (op);
1290 d = sram[get_d16 (op)];
1291 res = d - r;
1292 update_flags_sub (res, d, r);
1293 if (res == 0)
1294 sram[SREG] |= SREG_Z;
1295 break;
1297 case OP_cpc:
1299 byte old = sram[SREG];
1300 d = sram[get_d (op)];
1301 r = sram[get_r (op)];
1302 res = d - r - (old & SREG_C);
1303 update_flags_sub (res, d, r);
1304 if (res == 0 && (old & SREG_Z))
1305 sram[SREG] |= SREG_Z;
1307 break;
1309 case OP_brbc:
1310 if (!(sram[SREG] & flash[ipc].r))
1312 avr_cpu->pc = (avr_cpu->pc + get_k (op)) & PC_MASK;
1313 avr_cpu->cycles++;
1315 break;
1317 case OP_brbs:
1318 if (sram[SREG] & flash[ipc].r)
1320 avr_cpu->pc = (avr_cpu->pc + get_k (op)) & PC_MASK;
1321 avr_cpu->cycles++;
1323 break;
1325 case OP_lpm:
1326 sram[0] = get_lpm (read_word (REGZ));
1327 avr_cpu->cycles += 2;
1328 break;
1330 case OP_lpm_Z:
1331 sram[get_d (op)] = get_lpm (read_word (REGZ));
1332 avr_cpu->cycles += 2;
1333 break;
1335 case OP_lpm_inc_Z:
1336 sram[get_d (op)] = get_lpm (read_word_post_inc (REGZ));
1337 avr_cpu->cycles += 2;
1338 break;
1340 case OP_elpm:
1341 sram[0] = get_lpm (get_z ());
1342 avr_cpu->cycles += 2;
1343 break;
1345 case OP_elpm_Z:
1346 sram[get_d (op)] = get_lpm (get_z ());
1347 avr_cpu->cycles += 2;
1348 break;
1350 case OP_elpm_inc_Z:
1352 unsigned int z = get_z ();
1354 sram[get_d (op)] = get_lpm (z);
1355 z++;
1356 sram[REGZ_LO] = z;
1357 sram[REGZ_HI] = z >> 8;
1358 sram[RAMPZ] = z >> 16;
1360 avr_cpu->cycles += 2;
1361 break;
1363 case OP_ld_Z_inc:
1364 sram[get_d (op)] = sram[read_word_post_inc (REGZ) & SRAM_MASK];
1365 avr_cpu->cycles++;
1366 break;
1368 case OP_ld_dec_Z:
1369 sram[get_d (op)] = sram[read_word_pre_dec (REGZ) & SRAM_MASK];
1370 avr_cpu->cycles++;
1371 break;
1373 case OP_ld_X_inc:
1374 sram[get_d (op)] = sram[read_word_post_inc (REGX) & SRAM_MASK];
1375 avr_cpu->cycles++;
1376 break;
1378 case OP_ld_dec_X:
1379 sram[get_d (op)] = sram[read_word_pre_dec (REGX) & SRAM_MASK];
1380 avr_cpu->cycles++;
1381 break;
1383 case OP_ld_Y_inc:
1384 sram[get_d (op)] = sram[read_word_post_inc (REGY) & SRAM_MASK];
1385 avr_cpu->cycles++;
1386 break;
1388 case OP_ld_dec_Y:
1389 sram[get_d (op)] = sram[read_word_pre_dec (REGY) & SRAM_MASK];
1390 avr_cpu->cycles++;
1391 break;
1393 case OP_st_X:
1394 sram[read_word (REGX) & SRAM_MASK] = sram[get_d (op)];
1395 avr_cpu->cycles++;
1396 break;
1398 case OP_st_X_inc:
1399 sram[read_word_post_inc (REGX) & SRAM_MASK] = sram[get_d (op)];
1400 avr_cpu->cycles++;
1401 break;
1403 case OP_st_dec_X:
1404 sram[read_word_pre_dec (REGX) & SRAM_MASK] = sram[get_d (op)];
1405 avr_cpu->cycles++;
1406 break;
1408 case OP_st_Z_inc:
1409 sram[read_word_post_inc (REGZ) & SRAM_MASK] = sram[get_d (op)];
1410 avr_cpu->cycles++;
1411 break;
1413 case OP_st_dec_Z:
1414 sram[read_word_pre_dec (REGZ) & SRAM_MASK] = sram[get_d (op)];
1415 avr_cpu->cycles++;
1416 break;
1418 case OP_st_Y_inc:
1419 sram[read_word_post_inc (REGY) & SRAM_MASK] = sram[get_d (op)];
1420 avr_cpu->cycles++;
1421 break;
1423 case OP_st_dec_Y:
1424 sram[read_word_pre_dec (REGY) & SRAM_MASK] = sram[get_d (op)];
1425 avr_cpu->cycles++;
1426 break;
1428 case OP_std_Y:
1429 sram[read_word (REGY) + flash[ipc].r] = sram[get_d (op)];
1430 avr_cpu->cycles++;
1431 break;
1433 case OP_std_Z:
1434 sram[read_word (REGZ) + flash[ipc].r] = sram[get_d (op)];
1435 avr_cpu->cycles++;
1436 break;
1438 case OP_ldd_Z:
1439 sram[get_d (op)] = sram[read_word (REGZ) + flash[ipc].r];
1440 avr_cpu->cycles++;
1441 break;
1443 case OP_ldd_Y:
1444 sram[get_d (op)] = sram[read_word (REGY) + flash[ipc].r];
1445 avr_cpu->cycles++;
1446 break;
1448 case OP_ld_X:
1449 sram[get_d (op)] = sram[read_word (REGX) & SRAM_MASK];
1450 avr_cpu->cycles++;
1451 break;
1453 case OP_sbiw:
1455 word wk = get_k6 (op);
1456 word wres;
1457 word wr;
1459 d = get_d24 (op);
1460 wr = read_word (d);
1461 wres = wr - wk;
1463 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1464 if (wres == 0)
1465 sram[SREG] |= SREG_Z;
1466 if (wres & 0x8000)
1467 sram[SREG] |= SREG_N;
1468 if (wres & ~wr & 0x8000)
1469 sram[SREG] |= SREG_C;
1470 if (~wres & wr & 0x8000)
1471 sram[SREG] |= SREG_V;
1472 if (((~wres & wr) ^ wres) & 0x8000)
1473 sram[SREG] |= SREG_S;
1474 write_word (d, wres);
1476 avr_cpu->cycles++;
1477 break;
1479 case OP_adiw:
1481 word wk = get_k6 (op);
1482 word wres;
1483 word wr;
1485 d = get_d24 (op);
1486 wr = read_word (d);
1487 wres = wr + wk;
1489 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1490 if (wres == 0)
1491 sram[SREG] |= SREG_Z;
1492 if (wres & 0x8000)
1493 sram[SREG] |= SREG_N;
1494 if (~wres & wr & 0x8000)
1495 sram[SREG] |= SREG_C;
1496 if (wres & ~wr & 0x8000)
1497 sram[SREG] |= SREG_V;
1498 if (((wres & ~wr) ^ wres) & 0x8000)
1499 sram[SREG] |= SREG_S;
1500 write_word (d, wres);
1502 avr_cpu->cycles++;
1503 break;
1505 case OP_bad:
1506 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, avr_cpu->pc, sim_signalled, SIM_SIGILL);
1508 default:
1509 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, avr_cpu->pc, sim_signalled, SIM_SIGILL);
1513 void
1514 sim_engine_run (SIM_DESC sd,
1515 int next_cpu_nr, /* ignore */
1516 int nr_cpus, /* ignore */
1517 int siggnal) /* ignore */
1519 SIM_CPU *cpu;
1521 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1523 cpu = STATE_CPU (sd, 0);
1525 while (1)
1527 step_once (cpu);
1528 if (sim_events_tick (sd))
1529 sim_events_process (sd);
1533 uint64_t
1534 sim_write (SIM_DESC sd, uint64_t addr, const void *buffer, uint64_t size)
1536 int osize = size;
1538 if (addr >= 0 && addr < SRAM_VADDR)
1540 const unsigned char *data = buffer;
1541 while (size > 0 && addr < (MAX_AVR_FLASH << 1))
1543 word val = flash[addr >> 1].op;
1545 if (addr & 1)
1546 val = (val & 0xff) | (data[0] << 8);
1547 else
1548 val = (val & 0xff00) | data[0];
1550 flash[addr >> 1].op = val;
1551 flash[addr >> 1].code = OP_unknown;
1552 addr++;
1553 data++;
1554 size--;
1556 return osize - size;
1558 else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
1560 addr -= SRAM_VADDR;
1561 if (addr + size > MAX_AVR_SRAM)
1562 size = MAX_AVR_SRAM - addr;
1563 memcpy (sram + addr, buffer, size);
1564 return size;
1566 else
1567 return 0;
1570 uint64_t
1571 sim_read (SIM_DESC sd, uint64_t addr, void *buffer, uint64_t size)
1573 int osize = size;
1575 if (addr >= 0 && addr < SRAM_VADDR)
1577 unsigned char *data = buffer;
1578 while (size > 0 && addr < (MAX_AVR_FLASH << 1))
1580 word val = flash[addr >> 1].op;
1582 if (addr & 1)
1583 val >>= 8;
1585 *data++ = val;
1586 addr++;
1587 size--;
1589 return osize - size;
1591 else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
1593 addr -= SRAM_VADDR;
1594 if (addr + size > MAX_AVR_SRAM)
1595 size = MAX_AVR_SRAM - addr;
1596 memcpy (buffer, sram + addr, size);
1597 return size;
1599 else
1601 /* Avoid errors. */
1602 memset (buffer, 0, size);
1603 return size;
1607 static int
1608 avr_reg_store (SIM_CPU *cpu, int rn, const void *buf, int length)
1610 struct avr_sim_cpu *avr_cpu = AVR_SIM_CPU (cpu);
1611 const unsigned char *memory = buf;
1613 if (rn < 32 && length == 1)
1615 sram[rn] = *memory;
1616 return 1;
1618 if (rn == AVR_SREG_REGNUM && length == 1)
1620 sram[SREG] = *memory;
1621 return 1;
1623 if (rn == AVR_SP_REGNUM && length == 2)
1625 sram[REG_SP] = memory[0];
1626 sram[REG_SP + 1] = memory[1];
1627 return 2;
1629 if (rn == AVR_PC_REGNUM && length == 4)
1631 avr_cpu->pc = (memory[0] >> 1) | (memory[1] << 7)
1632 | (memory[2] << 15) | (memory[3] << 23);
1633 avr_cpu->pc &= PC_MASK;
1634 return 4;
1636 return 0;
1639 static int
1640 avr_reg_fetch (SIM_CPU *cpu, int rn, void *buf, int length)
1642 struct avr_sim_cpu *avr_cpu = AVR_SIM_CPU (cpu);
1643 unsigned char *memory = buf;
1645 if (rn < 32 && length == 1)
1647 *memory = sram[rn];
1648 return 1;
1650 if (rn == AVR_SREG_REGNUM && length == 1)
1652 *memory = sram[SREG];
1653 return 1;
1655 if (rn == AVR_SP_REGNUM && length == 2)
1657 memory[0] = sram[REG_SP];
1658 memory[1] = sram[REG_SP + 1];
1659 return 2;
1661 if (rn == AVR_PC_REGNUM && length == 4)
1663 memory[0] = avr_cpu->pc << 1;
1664 memory[1] = avr_cpu->pc >> 7;
1665 memory[2] = avr_cpu->pc >> 15;
1666 memory[3] = avr_cpu->pc >> 23;
1667 return 4;
1669 return 0;
1672 static sim_cia
1673 avr_pc_get (sim_cpu *cpu)
1675 return AVR_SIM_CPU (cpu)->pc;
1678 static void
1679 avr_pc_set (sim_cpu *cpu, sim_cia pc)
1681 AVR_SIM_CPU (cpu)->pc = pc;
1684 static void
1685 free_state (SIM_DESC sd)
1687 if (STATE_MODULES (sd) != NULL)
1688 sim_module_uninstall (sd);
1689 sim_cpu_free_all (sd);
1690 sim_state_free (sd);
1693 SIM_DESC
1694 sim_open (SIM_OPEN_KIND kind, host_callback *cb,
1695 struct bfd *abfd, char * const *argv)
1697 int i;
1698 SIM_DESC sd = sim_state_alloc_extra (kind, cb, sizeof (struct avr_sim_state));
1699 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1701 /* Set default options before parsing user options. */
1702 current_alignment = STRICT_ALIGNMENT;
1703 current_target_byte_order = BFD_ENDIAN_LITTLE;
1705 /* The cpu data is kept in a separately allocated chunk of memory. */
1706 if (sim_cpu_alloc_all_extra (sd, 0, sizeof (struct avr_sim_cpu))
1707 != SIM_RC_OK)
1709 free_state (sd);
1710 return 0;
1713 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1715 free_state (sd);
1716 return 0;
1719 /* The parser will print an error message for us, so we silently return. */
1720 if (sim_parse_args (sd, argv) != SIM_RC_OK)
1722 free_state (sd);
1723 return 0;
1726 /* Check for/establish the a reference program image. */
1727 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
1729 free_state (sd);
1730 return 0;
1733 /* Configure/verify the target byte order and other runtime
1734 configuration options. */
1735 if (sim_config (sd) != SIM_RC_OK)
1737 sim_module_uninstall (sd);
1738 return 0;
1741 if (sim_post_argv_init (sd) != SIM_RC_OK)
1743 /* Uninstall the modules to avoid memory leaks,
1744 file descriptor leaks, etc. */
1745 sim_module_uninstall (sd);
1746 return 0;
1749 /* CPU specific initialization. */
1750 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1752 SIM_CPU *cpu = STATE_CPU (sd, i);
1754 CPU_REG_FETCH (cpu) = avr_reg_fetch;
1755 CPU_REG_STORE (cpu) = avr_reg_store;
1756 CPU_PC_FETCH (cpu) = avr_pc_get;
1757 CPU_PC_STORE (cpu) = avr_pc_set;
1760 /* Clear all the memory. */
1761 memset (sram, 0, sizeof (sram));
1762 memset (flash, 0, sizeof (flash));
1764 return sd;
1767 SIM_RC
1768 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
1769 char * const *argv, char * const *env)
1771 struct avr_sim_state *state = AVR_SIM_STATE (sd);
1772 SIM_CPU *cpu = STATE_CPU (sd, 0);
1773 bfd_vma addr;
1775 /* Set the PC. */
1776 if (abfd != NULL)
1777 addr = bfd_get_start_address (abfd);
1778 else
1779 addr = 0;
1780 sim_pc_set (cpu, addr);
1782 if (abfd != NULL)
1783 state->avr_pc22 = (bfd_get_mach (abfd) >= bfd_mach_avr6);
1785 return SIM_RC_OK;