1 /* Simulator for Atmel's AVR core.
2 Copyright (C) 2009-2022 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"
34 /* As AVR is a 8/16 bits processor, define handy types. */
35 typedef unsigned short int word;
36 typedef signed short int sword;
37 typedef unsigned char byte;
38 typedef signed char sbyte;
40 /* Max size of I space (which is always flash on avr). */
41 #define MAX_AVR_FLASH (128 * 1024)
42 #define PC_MASK (MAX_AVR_FLASH - 1)
44 /* Mac size of D space. */
45 #define MAX_AVR_SRAM (64 * 1024)
46 #define SRAM_MASK (MAX_AVR_SRAM - 1)
48 /* D space offset in ELF file. */
49 #define SRAM_VADDR 0x800000
51 /* Simulator specific ports. */
52 #define STDIO_PORT 0x52
53 #define EXIT_PORT 0x4F
54 #define ABORT_PORT 0x49
56 /* GDB defined register numbers. */
57 #define AVR_SREG_REGNUM 32
58 #define AVR_SP_REGNUM 33
59 #define AVR_PC_REGNUM 34
61 /* Memory mapped registers. */
73 /* Sreg (status) bits. */
83 /* In order to speed up emulation we use a simple approach:
84 a code is associated with each instruction. The pre-decoding occurs
85 usually once when the instruction is first seen.
86 This works well because I&D spaces are separated.
88 Missing opcodes: sleep, spm, wdr (as they are mmcu dependent).
92 /* Opcode not yet decoded. */
198 /* 2 words opcodes. */
199 #define OP_2words OP_jmp
208 /* The insn (16 bits). */
211 /* Pre-decoding code. */
212 enum avr_opcode code : 8;
213 /* One byte of additional information. */
218 /* TODO: Should be moved to SIM_CPU. */
219 static struct avr_insn_cell flash[MAX_AVR_FLASH];
220 static byte sram[MAX_AVR_SRAM];
222 /* Sign extend a value. */
223 static int sign_ext (word val, int nb_bits)
225 if (val & (1 << (nb_bits - 1)))
226 return val | -(1 << nb_bits);
230 /* Insn field extractors. */
232 /* Extract xxxx_xxxRx_xxxx_RRRR. */
233 static inline byte get_r (word op)
235 return (op & 0xf) | ((op >> 5) & 0x10);
238 /* Extract xxxx_xxxxx_xxxx_RRRR. */
239 static inline byte get_r16 (word op)
241 return 16 + (op & 0xf);
244 /* Extract xxxx_xxxxx_xxxx_xRRR. */
245 static inline byte get_r16_23 (word op)
247 return 16 + (op & 0x7);
250 /* Extract xxxx_xxxD_DDDD_xxxx. */
251 static inline byte get_d (word op)
253 return (op >> 4) & 0x1f;
256 /* Extract xxxx_xxxx_DDDD_xxxx. */
257 static inline byte get_d16 (word op)
259 return 16 + ((op >> 4) & 0x0f);
262 /* Extract xxxx_xxxx_xDDD_xxxx. */
263 static inline byte get_d16_23 (word op)
265 return 16 + ((op >> 4) & 0x07);
268 /* Extract xxxx_xAAx_xxxx_AAAA. */
269 static inline byte get_A (word op)
271 return (op & 0x0f) | ((op & 0x600) >> 5);
274 /* Extract xxxx_xxxx_AAAA_Axxx. */
275 static inline byte get_biA (word op)
277 return (op >> 3) & 0x1f;
280 /* Extract xxxx_KKKK_xxxx_KKKK. */
281 static inline byte get_K (word op)
283 return (op & 0xf) | ((op & 0xf00) >> 4);
286 /* Extract xxxx_xxKK_KKKK_Kxxx. */
287 static inline int get_k (word op)
289 return sign_ext ((op & 0x3f8) >> 3, 7);
292 /* Extract xxxx_xxxx_xxDD_xxxx. */
293 static inline byte get_d24 (word op)
295 return 24 + ((op >> 3) & 6);
298 /* Extract xxxx_xxxx_KKxx_KKKK. */
299 static inline byte get_k6 (word op)
301 return (op & 0xf) | ((op >> 2) & 0x30);
304 /* Extract xxQx_QQxx_xxxx_xQQQ. */
305 static inline byte get_q (word op)
307 return (op & 7) | ((op >> 7) & 0x18)| ((op >> 8) & 0x20);
310 /* Extract xxxx_xxxx_xxxx_xBBB. */
311 static inline byte get_b (word op)
316 /* AVR is little endian. */
318 read_word (unsigned int addr)
320 return sram[addr] | (sram[addr + 1] << 8);
324 write_word (unsigned int addr, word w)
327 sram[addr + 1] = w >> 8;
331 read_word_post_inc (unsigned int addr)
333 word v = read_word (addr);
334 write_word (addr, v + 1);
339 read_word_pre_dec (unsigned int addr)
341 word v = read_word (addr) - 1;
342 write_word (addr, v);
347 update_flags_logic (byte res)
349 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
351 sram[SREG] |= SREG_Z;
353 sram[SREG] |= SREG_N | SREG_S;
357 update_flags_add (byte r, byte a, byte b)
361 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
363 sram[SREG] |= SREG_N;
364 carry = (a & b) | (a & ~r) | (b & ~r);
366 sram[SREG] |= SREG_H;
368 sram[SREG] |= SREG_C;
369 if (((a & b & ~r) | (~a & ~b & r)) & 0x80)
370 sram[SREG] |= SREG_V;
371 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
372 sram[SREG] |= SREG_S;
374 sram[SREG] |= SREG_Z;
377 static void update_flags_sub (byte r, byte a, byte b)
381 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
383 sram[SREG] |= SREG_N;
384 carry = (~a & b) | (b & r) | (r & ~a);
386 sram[SREG] |= SREG_H;
388 sram[SREG] |= SREG_C;
389 if (((a & ~b & ~r) | (~a & b & r)) & 0x80)
390 sram[SREG] |= SREG_V;
391 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
392 sram[SREG] |= SREG_S;
393 /* Note: Z is not set. */
396 static enum avr_opcode
397 decode (unsigned int pc)
399 word op1 = flash[pc].op;
401 switch ((op1 >> 12) & 0x0f)
404 switch ((op1 >> 10) & 0x3)
407 switch ((op1 >> 8) & 0x3)
437 flash[pc].r = SREG_C;
445 switch ((op1 >> 10) & 0x3)
455 flash[pc].r = SREG_C;
460 switch ((op1 >> 10) & 0x3)
488 flash[pc].r = get_q (op1);
493 flash[pc].r = get_q (op1);
501 flash[pc].r = get_q (op1);
506 flash[pc].r = get_q (op1);
512 switch ((op1 >> 8) & 0xf)
516 switch ((op1 >> 0) & 0xf)
531 return OP_elpm_inc_Z;
548 switch ((op1 >> 0) & 0xf)
590 case 0x8: /* 9[45]x8 */
591 switch ((op1 >> 4) & 0x1f)
625 case 0x9: /* 9[45]x9 */
626 switch ((op1 >> 4) & 0x1f)
644 flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
648 flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
672 flash[pc].r = get_A (op1);
673 if (((op1 >> 11) & 1) == 0)
684 switch ((op1 >> 9) & 7)
688 flash[pc].r = 1 << (op1 & 7);
692 flash[pc].r = 1 << (op1 & 7);
697 flash[pc].r = 1 << (op1 & 7);
704 flash[pc].r = 1 << (op1 & 7);
711 flash[pc].r = 1 << (op1 & 7);
718 flash[pc].r = 1 << (op1 & 7);
729 do_call (SIM_CPU *cpu, unsigned int npc)
731 const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
732 unsigned int sp = read_word (REG_SP);
735 sram[sp--] = cpu->pc;
736 sram[sp--] = cpu->pc >> 8;
739 sram[sp--] = cpu->pc >> 16;
742 write_word (REG_SP, sp);
743 cpu->pc = npc & PC_MASK;
748 get_insn_length (unsigned int p)
750 if (flash[p].code == OP_unknown)
751 flash[p].code = decode(p);
752 if (flash[p].code >= OP_2words)
761 return (sram[RAMPZ] << 16) | (sram[REGZ_HI] << 8) | sram[REGZ_LO];
765 get_lpm (unsigned int addr)
769 w = flash[(addr >> 1) & PC_MASK].op;
776 gen_mul (SIM_CPU *cpu, unsigned int res)
779 sram[SREG] &= ~(SREG_Z | SREG_C);
781 sram[SREG] |= SREG_Z;
783 sram[SREG] |= SREG_C;
788 step_once (SIM_CPU *cpu)
798 code = flash[cpu->pc].code;
799 op = flash[cpu->pc].op;
802 if (tracing && code != OP_unknown)
808 sim_cb_eprintf (callback, "R00-07:");
809 for (i = 0; i < 8; i++)
810 sim_cb_eprintf (callback, " %02x", sram[i]);
811 sim_cb_eprintf (callback, " -");
812 for (i = 8; i < 16; i++)
813 sim_cb_eprintf (callback, " %02x", sram[i]);
814 sim_cb_eprintf (callback, " SP: %02x %02x",
815 sram[REG_SP + 1], sram[REG_SP]);
816 sim_cb_eprintf (callback, "\n");
817 sim_cb_eprintf (callback, "R16-31:");
818 for (i = 16; i < 24; i++)
819 sim_cb_eprintf (callback, " %02x", sram[i]);
820 sim_cb_eprintf (callback, " -");
821 for (i = 24; i < 32; i++)
822 sim_cb_eprintf (callback, " %02x", sram[i]);
823 sim_cb_eprintf (callback, " ");
825 for (i = 0; i < 8; i++)
826 sim_cb_eprintf (callback, "%c",
827 flags & (0x80 >> i) ? "ITHSVNZC"[i] : '-');
828 sim_cb_eprintf (callback, "\n");
832 sim_cb_eprintf (callback, "%06x: %04x\n", 2 * cpu->pc, flash[cpu->pc].op);
835 sim_cb_eprintf (callback, "pc=0x%06x insn=0x%04x code=%d r=%d\n",
836 2 * cpu->pc, flash[cpu->pc].op, code, flash[cpu->pc].r);
837 disassemble_insn (CPU_STATE (cpu), cpu->pc);
838 sim_cb_eprintf (callback, "\n");
844 cpu->pc = (cpu->pc + 1) & PC_MASK;
850 flash[ipc].code = decode(ipc);
859 /* 2 words instruction, but we don't care about the pc. */
860 cpu->pc = ((flash[ipc].r << 16) | flash[ipc + 1].op) & PC_MASK;
865 cpu->pc = ((sram[EIND] << 16) | read_word (REGZ)) & PC_MASK;
870 cpu->pc = read_word (REGZ) & PC_MASK;
875 /* 2 words instruction. */
877 do_call (cpu, (flash[ipc].r << 16) | flash[ipc + 1].op);
881 do_call (cpu, (sram[EIND] << 16) | read_word (REGZ));
885 do_call (cpu, read_word (REGZ));
889 do_call (cpu, cpu->pc + sign_ext (op & 0xfff, 12));
893 sram[SREG] |= SREG_I;
897 const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
898 unsigned int sp = read_word (REG_SP);
901 cpu->pc = sram[++sp] << 16;
906 cpu->pc |= sram[++sp] << 8;
907 cpu->pc |= sram[++sp];
908 write_word (REG_SP, sp);
914 /* Stop on this address. */
915 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, ipc, sim_stopped, SIM_SIGTRAP);
921 if (sram[SREG] & SREG_T)
928 if (sram[get_d (op)] & flash[ipc].r)
929 sram[SREG] |= SREG_T;
931 sram[SREG] &= ~SREG_T;
936 if (((sram[get_d (op)] & flash[ipc].r) == 0) ^ ((op & 0x0200) != 0))
938 int l = get_insn_length (cpu->pc);
946 unsigned int sp = read_word (REG_SP);
947 sram[sp--] = sram[get_d (op)];
948 write_word (REG_SP, sp);
955 unsigned int sp = read_word (REG_SP);
956 sram[get_d (op)] = sram[++sp];
957 write_word (REG_SP, sp);
963 sram[SREG] &= ~(1 << ((op >> 4) & 0x7));
967 sram[SREG] |= 1 << ((op >> 4) & 0x7);
971 cpu->pc = (cpu->pc + sign_ext (op & 0xfff, 12)) & PC_MASK;
977 res = sram[d] ^ sram[get_r (op)];
979 update_flags_logic (res);
984 res = sram[d] & sram[get_r (op)];
986 update_flags_logic (res);
991 res = sram[d] & get_K (op);
993 update_flags_logic (res);
998 res = sram[d] | sram[get_r (op)];
1000 update_flags_logic (res);
1005 res = sram[d] | get_K (op);
1007 update_flags_logic (res);
1014 update_flags_logic (res);
1015 sram[SREG] |= SREG_C;
1021 sram[d] = (vd >> 4) | (vd << 4);
1029 sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1031 sram[SREG] |= SREG_Z;
1033 sram[SREG] |= SREG_C;
1035 sram[SREG] |= SREG_V | SREG_N;
1036 else if (res & 0x80)
1037 sram[SREG] |= SREG_N | SREG_S;
1038 if ((res | vd) & 0x08)
1039 sram[SREG] |= SREG_H;
1046 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
1048 sram[SREG] |= SREG_V | SREG_N;
1049 else if (res & 0x80)
1050 sram[SREG] |= SREG_N | SREG_S;
1052 sram[SREG] |= SREG_Z;
1059 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
1061 sram[SREG] |= SREG_V | SREG_S;
1062 else if (res & 0x80)
1063 sram[SREG] |= SREG_N | SREG_S;
1065 sram[SREG] |= SREG_Z;
1072 res = (vd >> 1) | (vd & flash[ipc].r);
1074 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1076 sram[SREG] |= SREG_C | SREG_S;
1078 sram[SREG] |= SREG_N;
1079 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
1080 sram[SREG] |= SREG_V;
1082 sram[SREG] |= SREG_Z;
1088 res = vd >> 1 | (sram[SREG] << 7);
1090 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1092 sram[SREG] |= SREG_C | SREG_S;
1094 sram[SREG] |= SREG_N;
1095 if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
1096 sram[SREG] |= SREG_V;
1098 sram[SREG] |= SREG_Z;
1102 gen_mul (cpu, (word)sram[get_r (op)] * (word)sram[get_d (op)]);
1106 gen_mul (cpu, (sword)(sbyte)sram[get_r16 (op)]
1107 * (sword)(sbyte)sram[get_d16 (op)]);
1111 gen_mul (cpu, (sword)(word)sram[get_r16_23 (op)]
1112 * (sword)(sbyte)sram[get_d16_23 (op)]);
1116 gen_mul (cpu, ((word)sram[get_r16_23 (op)]
1117 * (word)sram[get_d16_23 (op)]) << 1);
1121 gen_mul (cpu, ((sword)(sbyte)sram[get_r16_23 (op)]
1122 * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
1126 gen_mul (cpu, ((sword)(word)sram[get_r16_23 (op)]
1127 * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
1132 r = sram[get_r (op)];
1135 res = r + vd + (sram[SREG] & flash[ipc].r);
1137 update_flags_add (res, vd, r);
1143 r = sram[get_r (op)];
1146 update_flags_sub (res, vd, r);
1148 sram[SREG] |= SREG_Z;
1153 byte old = sram[SREG];
1156 r = sram[get_r (op)];
1157 res = vd - r - (old & SREG_C);
1159 update_flags_sub (res, vd, r);
1160 if (res == 0 && (old & SREG_Z))
1161 sram[SREG] |= SREG_Z;
1171 update_flags_sub (res, vd, r);
1173 sram[SREG] |= SREG_Z;
1178 byte old = sram[SREG];
1183 res = vd - r - (old & SREG_C);
1185 update_flags_sub (res, vd, r);
1186 if (res == 0 && (old & SREG_Z))
1187 sram[SREG] |= SREG_Z;
1192 sram[get_d (op)] = sram[get_r (op)];
1196 d = (op & 0xf0) >> 3;
1197 r = (op & 0x0f) << 1;
1199 sram[d + 1] = sram[r + 1];
1203 d = get_A (op) + 0x20;
1204 res = sram[get_d (op)];
1206 if (d == STDIO_PORT)
1208 else if (d == EXIT_PORT)
1209 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_exited, 0);
1210 else if (d == ABORT_PORT)
1211 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_exited, 1);
1215 d = get_A (op) + 0x20;
1216 sram[get_d (op)] = sram[d];
1220 d = get_biA (op) + 0x20;
1221 sram[d] &= ~(1 << get_b(op));
1225 d = get_biA (op) + 0x20;
1226 sram[d] |= 1 << get_b(op);
1230 if (!(sram[get_biA (op) + 0x20] & 1 << get_b(op)))
1232 int l = get_insn_length (cpu->pc);
1239 if (sram[get_biA (op) + 0x20] & 1 << get_b(op))
1241 int l = get_insn_length (cpu->pc);
1254 sram[get_d (op)] = sram[flash[cpu->pc].op];
1260 sram[flash[cpu->pc].op] = sram[get_d (op)];
1266 if (sram[get_r (op)] == sram[get_d (op)])
1268 int l = get_insn_length (cpu->pc);
1275 r = sram[get_r (op)];
1276 d = sram[get_d (op)];
1278 update_flags_sub (res, d, r);
1280 sram[SREG] |= SREG_Z;
1285 d = sram[get_d16 (op)];
1287 update_flags_sub (res, d, r);
1289 sram[SREG] |= SREG_Z;
1294 byte old = sram[SREG];
1295 d = sram[get_d (op)];
1296 r = sram[get_r (op)];
1297 res = d - r - (old & SREG_C);
1298 update_flags_sub (res, d, r);
1299 if (res == 0 && (old & SREG_Z))
1300 sram[SREG] |= SREG_Z;
1305 if (!(sram[SREG] & flash[ipc].r))
1307 cpu->pc = (cpu->pc + get_k (op)) & PC_MASK;
1313 if (sram[SREG] & flash[ipc].r)
1315 cpu->pc = (cpu->pc + get_k (op)) & PC_MASK;
1321 sram[0] = get_lpm (read_word (REGZ));
1326 sram[get_d (op)] = get_lpm (read_word (REGZ));
1331 sram[get_d (op)] = get_lpm (read_word_post_inc (REGZ));
1336 sram[0] = get_lpm (get_z ());
1341 sram[get_d (op)] = get_lpm (get_z ());
1347 unsigned int z = get_z ();
1349 sram[get_d (op)] = get_lpm (z);
1352 sram[REGZ_HI] = z >> 8;
1353 sram[RAMPZ] = z >> 16;
1359 sram[get_d (op)] = sram[read_word_post_inc (REGZ) & SRAM_MASK];
1364 sram[get_d (op)] = sram[read_word_pre_dec (REGZ) & SRAM_MASK];
1369 sram[get_d (op)] = sram[read_word_post_inc (REGX) & SRAM_MASK];
1374 sram[get_d (op)] = sram[read_word_pre_dec (REGX) & SRAM_MASK];
1379 sram[get_d (op)] = sram[read_word_post_inc (REGY) & SRAM_MASK];
1384 sram[get_d (op)] = sram[read_word_pre_dec (REGY) & SRAM_MASK];
1389 sram[read_word (REGX) & SRAM_MASK] = sram[get_d (op)];
1394 sram[read_word_post_inc (REGX) & SRAM_MASK] = sram[get_d (op)];
1399 sram[read_word_pre_dec (REGX) & SRAM_MASK] = sram[get_d (op)];
1404 sram[read_word_post_inc (REGZ) & SRAM_MASK] = sram[get_d (op)];
1409 sram[read_word_pre_dec (REGZ) & SRAM_MASK] = sram[get_d (op)];
1414 sram[read_word_post_inc (REGY) & SRAM_MASK] = sram[get_d (op)];
1419 sram[read_word_pre_dec (REGY) & SRAM_MASK] = sram[get_d (op)];
1424 sram[read_word (REGY) + flash[ipc].r] = sram[get_d (op)];
1429 sram[read_word (REGZ) + flash[ipc].r] = sram[get_d (op)];
1434 sram[get_d (op)] = sram[read_word (REGZ) + flash[ipc].r];
1439 sram[get_d (op)] = sram[read_word (REGY) + flash[ipc].r];
1444 sram[get_d (op)] = sram[read_word (REGX) & SRAM_MASK];
1450 word wk = get_k6 (op);
1458 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1460 sram[SREG] |= SREG_Z;
1462 sram[SREG] |= SREG_N;
1463 if (wres & ~wr & 0x8000)
1464 sram[SREG] |= SREG_C;
1465 if (~wres & wr & 0x8000)
1466 sram[SREG] |= SREG_V;
1467 if (((~wres & wr) ^ wres) & 0x8000)
1468 sram[SREG] |= SREG_S;
1469 write_word (d, wres);
1476 word wk = get_k6 (op);
1484 sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
1486 sram[SREG] |= SREG_Z;
1488 sram[SREG] |= SREG_N;
1489 if (~wres & wr & 0x8000)
1490 sram[SREG] |= SREG_C;
1491 if (wres & ~wr & 0x8000)
1492 sram[SREG] |= SREG_V;
1493 if (((wres & ~wr) ^ wres) & 0x8000)
1494 sram[SREG] |= SREG_S;
1495 write_word (d, wres);
1501 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_signalled, SIM_SIGILL);
1504 sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_signalled, SIM_SIGILL);
1509 sim_engine_run (SIM_DESC sd,
1510 int next_cpu_nr, /* ignore */
1511 int nr_cpus, /* ignore */
1512 int siggnal) /* ignore */
1516 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1518 cpu = STATE_CPU (sd, 0);
1523 if (sim_events_tick (sd))
1524 sim_events_process (sd);
1529 sim_write (SIM_DESC sd, SIM_ADDR addr, const void *buffer, int size)
1533 if (addr >= 0 && addr < SRAM_VADDR)
1535 const unsigned char *data = buffer;
1536 while (size > 0 && addr < (MAX_AVR_FLASH << 1))
1538 word val = flash[addr >> 1].op;
1541 val = (val & 0xff) | (data[0] << 8);
1543 val = (val & 0xff00) | data[0];
1545 flash[addr >> 1].op = val;
1546 flash[addr >> 1].code = OP_unknown;
1551 return osize - size;
1553 else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
1556 if (addr + size > MAX_AVR_SRAM)
1557 size = MAX_AVR_SRAM - addr;
1558 memcpy (sram + addr, buffer, size);
1566 sim_read (SIM_DESC sd, SIM_ADDR addr, void *buffer, int size)
1570 if (addr >= 0 && addr < SRAM_VADDR)
1572 unsigned char *data = buffer;
1573 while (size > 0 && addr < (MAX_AVR_FLASH << 1))
1575 word val = flash[addr >> 1].op;
1584 return osize - size;
1586 else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
1589 if (addr + size > MAX_AVR_SRAM)
1590 size = MAX_AVR_SRAM - addr;
1591 memcpy (buffer, sram + addr, size);
1597 memset (buffer, 0, size);
1603 avr_reg_store (SIM_CPU *cpu, int rn, const void *buf, int length)
1605 const unsigned char *memory = buf;
1607 if (rn < 32 && length == 1)
1612 if (rn == AVR_SREG_REGNUM && length == 1)
1614 sram[SREG] = *memory;
1617 if (rn == AVR_SP_REGNUM && length == 2)
1619 sram[REG_SP] = memory[0];
1620 sram[REG_SP + 1] = memory[1];
1623 if (rn == AVR_PC_REGNUM && length == 4)
1625 cpu->pc = (memory[0] >> 1) | (memory[1] << 7)
1626 | (memory[2] << 15) | (memory[3] << 23);
1634 avr_reg_fetch (SIM_CPU *cpu, int rn, void *buf, int length)
1636 unsigned char *memory = buf;
1638 if (rn < 32 && length == 1)
1643 if (rn == AVR_SREG_REGNUM && length == 1)
1645 *memory = sram[SREG];
1648 if (rn == AVR_SP_REGNUM && length == 2)
1650 memory[0] = sram[REG_SP];
1651 memory[1] = sram[REG_SP + 1];
1654 if (rn == AVR_PC_REGNUM && length == 4)
1656 memory[0] = cpu->pc << 1;
1657 memory[1] = cpu->pc >> 7;
1658 memory[2] = cpu->pc >> 15;
1659 memory[3] = cpu->pc >> 23;
1666 avr_pc_get (sim_cpu *cpu)
1672 avr_pc_set (sim_cpu *cpu, sim_cia pc)
1678 free_state (SIM_DESC sd)
1680 if (STATE_MODULES (sd) != NULL)
1681 sim_module_uninstall (sd);
1682 sim_cpu_free_all (sd);
1683 sim_state_free (sd);
1687 sim_open (SIM_OPEN_KIND kind, host_callback *cb,
1688 struct bfd *abfd, char * const *argv)
1691 SIM_DESC sd = sim_state_alloc_extra (kind, cb, sizeof (struct avr_sim_state));
1692 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1694 /* Set default options before parsing user options. */
1695 current_alignment = STRICT_ALIGNMENT;
1696 current_target_byte_order = BFD_ENDIAN_LITTLE;
1698 /* The cpu data is kept in a separately allocated chunk of memory. */
1699 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
1705 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1711 /* The parser will print an error message for us, so we silently return. */
1712 if (sim_parse_args (sd, argv) != SIM_RC_OK)
1718 /* Check for/establish the a reference program image. */
1719 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
1725 /* Configure/verify the target byte order and other runtime
1726 configuration options. */
1727 if (sim_config (sd) != SIM_RC_OK)
1729 sim_module_uninstall (sd);
1733 if (sim_post_argv_init (sd) != SIM_RC_OK)
1735 /* Uninstall the modules to avoid memory leaks,
1736 file descriptor leaks, etc. */
1737 sim_module_uninstall (sd);
1741 /* CPU specific initialization. */
1742 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1744 SIM_CPU *cpu = STATE_CPU (sd, i);
1746 CPU_REG_FETCH (cpu) = avr_reg_fetch;
1747 CPU_REG_STORE (cpu) = avr_reg_store;
1748 CPU_PC_FETCH (cpu) = avr_pc_get;
1749 CPU_PC_STORE (cpu) = avr_pc_set;
1752 /* Clear all the memory. */
1753 memset (sram, 0, sizeof (sram));
1754 memset (flash, 0, sizeof (flash));
1760 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
1761 char * const *argv, char * const *env)
1763 struct avr_sim_state *state = AVR_SIM_STATE (sd);
1764 SIM_CPU *cpu = STATE_CPU (sd, 0);
1769 addr = bfd_get_start_address (abfd);
1772 sim_pc_set (cpu, addr);
1775 state->avr_pc22 = (bfd_get_mach (abfd) >= bfd_mach_avr6);