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. */
26 #include "libiberty.h"
31 #include "sim-options.h"
32 #include "sim-signal.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. */
74 /* Sreg (status) bits. */
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).
93 /* Opcode not yet decoded. */
199 /* 2 words opcodes. */
200 #define OP_2words OP_jmp
209 /* The insn (16 bits). */
212 /* Pre-decoding code. */
213 enum avr_opcode code
: 8;
214 /* One byte of additional information. */
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
);
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
)
317 /* AVR is little endian. */
319 read_word (unsigned int addr
)
321 return sram
[addr
] | (sram
[addr
+ 1] << 8);
325 write_word (unsigned int addr
, word w
)
328 sram
[addr
+ 1] = w
>> 8;
332 read_word_post_inc (unsigned int addr
)
334 word v
= read_word (addr
);
335 write_word (addr
, v
+ 1);
340 read_word_pre_dec (unsigned int addr
)
342 word v
= read_word (addr
) - 1;
343 write_word (addr
, v
);
348 update_flags_logic (byte res
)
350 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
);
352 sram
[SREG
] |= SREG_Z
;
354 sram
[SREG
] |= SREG_N
| SREG_S
;
358 update_flags_add (byte r
, byte a
, byte b
)
362 sram
[SREG
] &= ~(SREG_H
| SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
364 sram
[SREG
] |= SREG_N
;
365 carry
= (a
& b
) | (a
& ~r
) | (b
& ~r
);
367 sram
[SREG
] |= SREG_H
;
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
;
375 sram
[SREG
] |= SREG_Z
;
378 static void update_flags_sub (byte r
, byte a
, byte b
)
382 sram
[SREG
] &= ~(SREG_H
| SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
384 sram
[SREG
] |= SREG_N
;
385 carry
= (~a
& b
) | (b
& r
) | (r
& ~a
);
387 sram
[SREG
] |= SREG_H
;
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)
405 switch ((op1
>> 10) & 0x3)
408 switch ((op1
>> 8) & 0x3)
438 flash
[pc
].r
= SREG_C
;
446 switch ((op1
>> 10) & 0x3)
456 flash
[pc
].r
= SREG_C
;
461 switch ((op1
>> 10) & 0x3)
489 flash
[pc
].r
= get_q (op1
);
494 flash
[pc
].r
= get_q (op1
);
502 flash
[pc
].r
= get_q (op1
);
507 flash
[pc
].r
= get_q (op1
);
513 switch ((op1
>> 8) & 0xf)
517 switch ((op1
>> 0) & 0xf)
532 return OP_elpm_inc_Z
;
549 switch ((op1
>> 0) & 0xf)
591 case 0x8: /* 9[45]x8 */
592 switch ((op1
>> 4) & 0x1f)
626 case 0x9: /* 9[45]x9 */
627 switch ((op1
>> 4) & 0x1f)
645 flash
[pc
].r
= ((op1
& 0x1f0) >> 3) | (op1
& 1);
649 flash
[pc
].r
= ((op1
& 0x1f0) >> 3) | (op1
& 1);
673 flash
[pc
].r
= get_A (op1
);
674 if (((op1
>> 11) & 1) == 0)
685 switch ((op1
>> 9) & 7)
689 flash
[pc
].r
= 1 << (op1
& 7);
693 flash
[pc
].r
= 1 << (op1
& 7);
698 flash
[pc
].r
= 1 << (op1
& 7);
705 flash
[pc
].r
= 1 << (op1
& 7);
712 flash
[pc
].r
= 1 << (op1
& 7);
719 flash
[pc
].r
= 1 << (op1
& 7);
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
);
737 sram
[sp
--] = avr_cpu
->pc
;
738 sram
[sp
--] = avr_cpu
->pc
>> 8;
741 sram
[sp
--] = avr_cpu
->pc
>> 16;
744 write_word (REG_SP
, sp
);
745 avr_cpu
->pc
= npc
& PC_MASK
;
746 avr_cpu
->cycles
+= 3;
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
)
763 return (sram
[RAMPZ
] << 16) | (sram
[REGZ_HI
] << 8) | sram
[REGZ_LO
];
767 get_lpm (unsigned int addr
)
771 w
= flash
[(addr
>> 1) & PC_MASK
].op
;
778 gen_mul (SIM_CPU
*cpu
, unsigned int res
)
780 struct avr_sim_cpu
*avr_cpu
= AVR_SIM_CPU (cpu
);
783 sram
[SREG
] &= ~(SREG_Z
| SREG_C
);
785 sram
[SREG
] |= SREG_Z
;
787 sram
[SREG
] |= SREG_C
;
792 step_once (SIM_CPU
*cpu
)
794 struct avr_sim_cpu
*avr_cpu
= AVR_SIM_CPU (cpu
);
803 code
= flash
[avr_cpu
->pc
].code
;
804 op
= flash
[avr_cpu
->pc
].op
;
807 if (tracing
&& code
!= OP_unknown
)
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
, " ");
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");
837 sim_cb_eprintf (callback
, "%06x: %04x\n", 2 * avr_cpu
->pc
, flash
[avr_cpu
->pc
].op
);
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");
849 avr_cpu
->pc
= (avr_cpu
->pc
+ 1) & PC_MASK
;
855 flash
[ipc
].code
= decode(ipc
);
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;
870 avr_cpu
->pc
= ((sram
[EIND
] << 16) | read_word (REGZ
)) & PC_MASK
;
871 avr_cpu
->cycles
+= 2;
875 avr_cpu
->pc
= read_word (REGZ
) & PC_MASK
;
876 avr_cpu
->cycles
+= 1;
880 /* 2 words instruction. */
882 do_call (cpu
, (flash
[ipc
].r
<< 16) | flash
[ipc
+ 1].op
);
886 do_call (cpu
, (sram
[EIND
] << 16) | read_word (REGZ
));
890 do_call (cpu
, read_word (REGZ
));
894 do_call (cpu
, avr_cpu
->pc
+ sign_ext (op
& 0xfff, 12));
898 sram
[SREG
] |= SREG_I
;
902 const struct avr_sim_state
*state
= AVR_SIM_STATE (CPU_STATE (cpu
));
903 unsigned int sp
= read_word (REG_SP
);
906 avr_cpu
->pc
= sram
[++sp
] << 16;
911 avr_cpu
->pc
|= sram
[++sp
] << 8;
912 avr_cpu
->pc
|= sram
[++sp
];
913 write_word (REG_SP
, sp
);
915 avr_cpu
->cycles
+= 3;
919 /* Stop on this address. */
920 sim_engine_halt (CPU_STATE (cpu
), cpu
, NULL
, ipc
, sim_stopped
, SIM_SIGTRAP
);
926 if (sram
[SREG
] & SREG_T
)
933 if (sram
[get_d (op
)] & flash
[ipc
].r
)
934 sram
[SREG
] |= SREG_T
;
936 sram
[SREG
] &= ~SREG_T
;
941 if (((sram
[get_d (op
)] & flash
[ipc
].r
) == 0) ^ ((op
& 0x0200) != 0))
943 int l
= get_insn_length (avr_cpu
->pc
);
945 avr_cpu
->cycles
+= l
;
951 unsigned int sp
= read_word (REG_SP
);
952 sram
[sp
--] = sram
[get_d (op
)];
953 write_word (REG_SP
, sp
);
960 unsigned int sp
= read_word (REG_SP
);
961 sram
[get_d (op
)] = sram
[++sp
];
962 write_word (REG_SP
, sp
);
968 sram
[SREG
] &= ~(1 << ((op
>> 4) & 0x7));
972 sram
[SREG
] |= 1 << ((op
>> 4) & 0x7);
976 avr_cpu
->pc
= (avr_cpu
->pc
+ sign_ext (op
& 0xfff, 12)) & PC_MASK
;
982 res
= sram
[d
] ^ sram
[get_r (op
)];
984 update_flags_logic (res
);
989 res
= sram
[d
] & sram
[get_r (op
)];
991 update_flags_logic (res
);
996 res
= sram
[d
] & get_K (op
);
998 update_flags_logic (res
);
1003 res
= sram
[d
] | sram
[get_r (op
)];
1005 update_flags_logic (res
);
1010 res
= sram
[d
] | get_K (op
);
1012 update_flags_logic (res
);
1019 update_flags_logic (res
);
1020 sram
[SREG
] |= SREG_C
;
1026 sram
[d
] = (vd
>> 4) | (vd
<< 4);
1034 sram
[SREG
] &= ~(SREG_H
| SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
1036 sram
[SREG
] |= SREG_Z
;
1038 sram
[SREG
] |= SREG_C
;
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
;
1051 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
);
1053 sram
[SREG
] |= SREG_V
| SREG_N
;
1054 else if (res
& 0x80)
1055 sram
[SREG
] |= SREG_N
| SREG_S
;
1057 sram
[SREG
] |= SREG_Z
;
1064 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
);
1066 sram
[SREG
] |= SREG_V
| SREG_S
;
1067 else if (res
& 0x80)
1068 sram
[SREG
] |= SREG_N
| SREG_S
;
1070 sram
[SREG
] |= SREG_Z
;
1077 res
= (vd
>> 1) | (vd
& flash
[ipc
].r
);
1079 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
1081 sram
[SREG
] |= SREG_C
| SREG_S
;
1083 sram
[SREG
] |= SREG_N
;
1084 if (!(sram
[SREG
] & SREG_N
) ^ !(sram
[SREG
] & SREG_C
))
1085 sram
[SREG
] |= SREG_V
;
1087 sram
[SREG
] |= SREG_Z
;
1093 res
= vd
>> 1 | (sram
[SREG
] << 7);
1095 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
1097 sram
[SREG
] |= SREG_C
| SREG_S
;
1099 sram
[SREG
] |= SREG_N
;
1100 if (!(sram
[SREG
] & SREG_N
) ^ !(sram
[SREG
] & SREG_C
))
1101 sram
[SREG
] |= SREG_V
;
1103 sram
[SREG
] |= SREG_Z
;
1107 gen_mul (cpu
, (word
)sram
[get_r (op
)] * (word
)sram
[get_d (op
)]);
1111 gen_mul (cpu
, (sword
)(sbyte
)sram
[get_r16 (op
)]
1112 * (sword
)(sbyte
)sram
[get_d16 (op
)]);
1116 gen_mul (cpu
, (sword
)(word
)sram
[get_r16_23 (op
)]
1117 * (sword
)(sbyte
)sram
[get_d16_23 (op
)]);
1121 gen_mul (cpu
, ((word
)sram
[get_r16_23 (op
)]
1122 * (word
)sram
[get_d16_23 (op
)]) << 1);
1126 gen_mul (cpu
, ((sword
)(sbyte
)sram
[get_r16_23 (op
)]
1127 * (sword
)(sbyte
)sram
[get_d16_23 (op
)]) << 1);
1131 gen_mul (cpu
, ((sword
)(word
)sram
[get_r16_23 (op
)]
1132 * (sword
)(sbyte
)sram
[get_d16_23 (op
)]) << 1);
1137 r
= sram
[get_r (op
)];
1140 res
= r
+ vd
+ (sram
[SREG
] & flash
[ipc
].r
);
1142 update_flags_add (res
, vd
, r
);
1148 r
= sram
[get_r (op
)];
1151 update_flags_sub (res
, vd
, r
);
1153 sram
[SREG
] |= SREG_Z
;
1158 byte old
= sram
[SREG
];
1161 r
= sram
[get_r (op
)];
1162 res
= vd
- r
- (old
& SREG_C
);
1164 update_flags_sub (res
, vd
, r
);
1165 if (res
== 0 && (old
& SREG_Z
))
1166 sram
[SREG
] |= SREG_Z
;
1176 update_flags_sub (res
, vd
, r
);
1178 sram
[SREG
] |= SREG_Z
;
1183 byte old
= sram
[SREG
];
1188 res
= vd
- r
- (old
& SREG_C
);
1190 update_flags_sub (res
, vd
, r
);
1191 if (res
== 0 && (old
& SREG_Z
))
1192 sram
[SREG
] |= SREG_Z
;
1197 sram
[get_d (op
)] = sram
[get_r (op
)];
1201 d
= (op
& 0xf0) >> 3;
1202 r
= (op
& 0x0f) << 1;
1204 sram
[d
+ 1] = sram
[r
+ 1];
1208 d
= get_A (op
) + 0x20;
1209 res
= sram
[get_d (op
)];
1211 if (d
== STDIO_PORT
)
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);
1220 d
= get_A (op
) + 0x20;
1221 sram
[get_d (op
)] = sram
[d
];
1225 d
= get_biA (op
) + 0x20;
1226 sram
[d
] &= ~(1 << get_b(op
));
1230 d
= get_biA (op
) + 0x20;
1231 sram
[d
] |= 1 << get_b(op
);
1235 if (!(sram
[get_biA (op
) + 0x20] & 1 << get_b(op
)))
1237 int l
= get_insn_length (avr_cpu
->pc
);
1239 avr_cpu
->cycles
+= l
;
1244 if (sram
[get_biA (op
) + 0x20] & 1 << get_b(op
))
1246 int l
= get_insn_length (avr_cpu
->pc
);
1248 avr_cpu
->cycles
+= l
;
1259 sram
[get_d (op
)] = sram
[flash
[avr_cpu
->pc
].op
];
1265 sram
[flash
[avr_cpu
->pc
].op
] = sram
[get_d (op
)];
1271 if (sram
[get_r (op
)] == sram
[get_d (op
)])
1273 int l
= get_insn_length (avr_cpu
->pc
);
1275 avr_cpu
->cycles
+= l
;
1280 r
= sram
[get_r (op
)];
1281 d
= sram
[get_d (op
)];
1283 update_flags_sub (res
, d
, r
);
1285 sram
[SREG
] |= SREG_Z
;
1290 d
= sram
[get_d16 (op
)];
1292 update_flags_sub (res
, d
, r
);
1294 sram
[SREG
] |= SREG_Z
;
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
;
1310 if (!(sram
[SREG
] & flash
[ipc
].r
))
1312 avr_cpu
->pc
= (avr_cpu
->pc
+ get_k (op
)) & PC_MASK
;
1318 if (sram
[SREG
] & flash
[ipc
].r
)
1320 avr_cpu
->pc
= (avr_cpu
->pc
+ get_k (op
)) & PC_MASK
;
1326 sram
[0] = get_lpm (read_word (REGZ
));
1327 avr_cpu
->cycles
+= 2;
1331 sram
[get_d (op
)] = get_lpm (read_word (REGZ
));
1332 avr_cpu
->cycles
+= 2;
1336 sram
[get_d (op
)] = get_lpm (read_word_post_inc (REGZ
));
1337 avr_cpu
->cycles
+= 2;
1341 sram
[0] = get_lpm (get_z ());
1342 avr_cpu
->cycles
+= 2;
1346 sram
[get_d (op
)] = get_lpm (get_z ());
1347 avr_cpu
->cycles
+= 2;
1352 unsigned int z
= get_z ();
1354 sram
[get_d (op
)] = get_lpm (z
);
1357 sram
[REGZ_HI
] = z
>> 8;
1358 sram
[RAMPZ
] = z
>> 16;
1360 avr_cpu
->cycles
+= 2;
1364 sram
[get_d (op
)] = sram
[read_word_post_inc (REGZ
) & SRAM_MASK
];
1369 sram
[get_d (op
)] = sram
[read_word_pre_dec (REGZ
) & SRAM_MASK
];
1374 sram
[get_d (op
)] = sram
[read_word_post_inc (REGX
) & SRAM_MASK
];
1379 sram
[get_d (op
)] = sram
[read_word_pre_dec (REGX
) & SRAM_MASK
];
1384 sram
[get_d (op
)] = sram
[read_word_post_inc (REGY
) & SRAM_MASK
];
1389 sram
[get_d (op
)] = sram
[read_word_pre_dec (REGY
) & SRAM_MASK
];
1394 sram
[read_word (REGX
) & SRAM_MASK
] = sram
[get_d (op
)];
1399 sram
[read_word_post_inc (REGX
) & SRAM_MASK
] = sram
[get_d (op
)];
1404 sram
[read_word_pre_dec (REGX
) & SRAM_MASK
] = sram
[get_d (op
)];
1409 sram
[read_word_post_inc (REGZ
) & SRAM_MASK
] = sram
[get_d (op
)];
1414 sram
[read_word_pre_dec (REGZ
) & SRAM_MASK
] = sram
[get_d (op
)];
1419 sram
[read_word_post_inc (REGY
) & SRAM_MASK
] = sram
[get_d (op
)];
1424 sram
[read_word_pre_dec (REGY
) & SRAM_MASK
] = sram
[get_d (op
)];
1429 sram
[read_word (REGY
) + flash
[ipc
].r
] = sram
[get_d (op
)];
1434 sram
[read_word (REGZ
) + flash
[ipc
].r
] = sram
[get_d (op
)];
1439 sram
[get_d (op
)] = sram
[read_word (REGZ
) + flash
[ipc
].r
];
1444 sram
[get_d (op
)] = sram
[read_word (REGY
) + flash
[ipc
].r
];
1449 sram
[get_d (op
)] = sram
[read_word (REGX
) & SRAM_MASK
];
1455 word wk
= get_k6 (op
);
1463 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
1465 sram
[SREG
] |= SREG_Z
;
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
);
1481 word wk
= get_k6 (op
);
1489 sram
[SREG
] &= ~(SREG_S
| SREG_V
| SREG_N
| SREG_Z
| SREG_C
);
1491 sram
[SREG
] |= SREG_Z
;
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
);
1506 sim_engine_halt (CPU_STATE (cpu
), cpu
, NULL
, avr_cpu
->pc
, sim_signalled
, SIM_SIGILL
);
1509 sim_engine_halt (CPU_STATE (cpu
), cpu
, NULL
, avr_cpu
->pc
, sim_signalled
, SIM_SIGILL
);
1514 sim_engine_run (SIM_DESC sd
,
1515 int next_cpu_nr
, /* ignore */
1516 int nr_cpus
, /* ignore */
1517 int siggnal
) /* ignore */
1521 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
1523 cpu
= STATE_CPU (sd
, 0);
1528 if (sim_events_tick (sd
))
1529 sim_events_process (sd
);
1534 sim_write (SIM_DESC sd
, uint64_t addr
, const void *buffer
, uint64_t 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
;
1546 val
= (val
& 0xff) | (data
[0] << 8);
1548 val
= (val
& 0xff00) | data
[0];
1550 flash
[addr
>> 1].op
= val
;
1551 flash
[addr
>> 1].code
= OP_unknown
;
1556 return osize
- size
;
1558 else if (addr
>= SRAM_VADDR
&& addr
< SRAM_VADDR
+ MAX_AVR_SRAM
)
1561 if (addr
+ size
> MAX_AVR_SRAM
)
1562 size
= MAX_AVR_SRAM
- addr
;
1563 memcpy (sram
+ addr
, buffer
, size
);
1571 sim_read (SIM_DESC sd
, uint64_t addr
, void *buffer
, uint64_t 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
;
1589 return osize
- size
;
1591 else if (addr
>= SRAM_VADDR
&& addr
< SRAM_VADDR
+ MAX_AVR_SRAM
)
1594 if (addr
+ size
> MAX_AVR_SRAM
)
1595 size
= MAX_AVR_SRAM
- addr
;
1596 memcpy (buffer
, sram
+ addr
, size
);
1602 memset (buffer
, 0, size
);
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)
1618 if (rn
== AVR_SREG_REGNUM
&& length
== 1)
1620 sram
[SREG
] = *memory
;
1623 if (rn
== AVR_SP_REGNUM
&& length
== 2)
1625 sram
[REG_SP
] = memory
[0];
1626 sram
[REG_SP
+ 1] = memory
[1];
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
;
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)
1650 if (rn
== AVR_SREG_REGNUM
&& length
== 1)
1652 *memory
= sram
[SREG
];
1655 if (rn
== AVR_SP_REGNUM
&& length
== 2)
1657 memory
[0] = sram
[REG_SP
];
1658 memory
[1] = sram
[REG_SP
+ 1];
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;
1673 avr_pc_get (sim_cpu
*cpu
)
1675 return AVR_SIM_CPU (cpu
)->pc
;
1679 avr_pc_set (sim_cpu
*cpu
, sim_cia pc
)
1681 AVR_SIM_CPU (cpu
)->pc
= pc
;
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
);
1694 sim_open (SIM_OPEN_KIND kind
, host_callback
*cb
,
1695 struct bfd
*abfd
, char * const *argv
)
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
))
1713 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
1719 /* The parser will print an error message for us, so we silently return. */
1720 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
1726 /* Check for/establish the a reference program image. */
1727 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
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
);
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
);
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
));
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);
1777 addr
= bfd_get_start_address (abfd
);
1780 sim_pc_set (cpu
, addr
);
1783 state
->avr_pc22
= (bfd_get_mach (abfd
) >= bfd_mach_avr6
);