xref: /qemu/target/avr/cpu.h (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
1c8c0d267SMichael Rolnik /*
2c8c0d267SMichael Rolnik  * QEMU AVR CPU
3c8c0d267SMichael Rolnik  *
4c8c0d267SMichael Rolnik  * Copyright (c) 2016-2020 Michael Rolnik
5c8c0d267SMichael Rolnik  *
6c8c0d267SMichael Rolnik  * This library is free software; you can redistribute it and/or
7c8c0d267SMichael Rolnik  * modify it under the terms of the GNU Lesser General Public
8c8c0d267SMichael Rolnik  * License as published by the Free Software Foundation; either
9c8c0d267SMichael Rolnik  * version 2.1 of the License, or (at your option) any later version.
10c8c0d267SMichael Rolnik  *
11c8c0d267SMichael Rolnik  * This library is distributed in the hope that it will be useful,
12c8c0d267SMichael Rolnik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c8c0d267SMichael Rolnik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c8c0d267SMichael Rolnik  * Lesser General Public License for more details.
15c8c0d267SMichael Rolnik  *
16c8c0d267SMichael Rolnik  * You should have received a copy of the GNU Lesser General Public
17c8c0d267SMichael Rolnik  * License along with this library; if not, see
18c8c0d267SMichael Rolnik  * <http://www.gnu.org/licenses/lgpl-2.1.html>
19c8c0d267SMichael Rolnik  */
20c8c0d267SMichael Rolnik 
21c8c0d267SMichael Rolnik #ifndef QEMU_AVR_CPU_H
22c8c0d267SMichael Rolnik #define QEMU_AVR_CPU_H
23c8c0d267SMichael Rolnik 
24f1c671f9SMichael Rolnik #include "cpu-qom.h"
25*d97c3b06SPierrick Bouvier #include "exec/cpu-common.h"
26c8c0d267SMichael Rolnik #include "exec/cpu-defs.h"
2722a7c2f2SPierrick Bouvier #include "exec/cpu-interrupt.h"
288be545baSRichard Henderson #include "system/memory.h"
29c8c0d267SMichael Rolnik 
30f1c671f9SMichael Rolnik #ifdef CONFIG_USER_ONLY
31f1c671f9SMichael Rolnik #error "AVR 8-bit does not support user mode"
32f1c671f9SMichael Rolnik #endif
33f1c671f9SMichael Rolnik 
34f1c671f9SMichael Rolnik #define CPU_RESOLVING_TYPE TYPE_AVR_CPU
35f1c671f9SMichael Rolnik 
36c8c0d267SMichael Rolnik /*
37c8c0d267SMichael Rolnik  * AVR has two memory spaces, data & code.
38c8c0d267SMichael Rolnik  * e.g. both have 0 address
39c8c0d267SMichael Rolnik  * ST/LD instructions access data space
40c8c0d267SMichael Rolnik  * LPM/SPM and instruction fetching access code memory space
41c8c0d267SMichael Rolnik  */
42c8c0d267SMichael Rolnik #define MMU_CODE_IDX 0
43c8c0d267SMichael Rolnik #define MMU_DATA_IDX 1
44c8c0d267SMichael Rolnik 
45c8c0d267SMichael Rolnik #define EXCP_RESET 1
46c8c0d267SMichael Rolnik #define EXCP_INT(n) (EXCP_RESET + (n) + 1)
47c8c0d267SMichael Rolnik 
48c8c0d267SMichael Rolnik /* Number of CPU registers */
49c8c0d267SMichael Rolnik #define NUMBER_OF_CPU_REGISTERS 32
50c8c0d267SMichael Rolnik 
51a2860ff9SRichard Henderson /* CPU registers mapped into i/o ports 0x38-0x3f. */
52a2860ff9SRichard Henderson #define REG_38_RAMPD  0
53a2860ff9SRichard Henderson #define REG_38_RAMPX  1
54a2860ff9SRichard Henderson #define REG_38_RAMPY  2
55a2860ff9SRichard Henderson #define REG_38_RAMPZ  3
56a2860ff9SRichard Henderson #define REG_38_EIDN   4
57a2860ff9SRichard Henderson #define REG_38_SPL    5
58a2860ff9SRichard Henderson #define REG_38_SPH    6
59a2860ff9SRichard Henderson #define REG_38_SREG   7
60a2860ff9SRichard Henderson 
61c8c0d267SMichael Rolnik /*
62c8c0d267SMichael Rolnik  * Offsets of AVR memory regions in host memory space.
63c8c0d267SMichael Rolnik  *
64c8c0d267SMichael Rolnik  * This is needed because the AVR has separate code and data address
65c8c0d267SMichael Rolnik  * spaces that both have start from zero but have to go somewhere in
66c8c0d267SMichael Rolnik  * host memory.
67c8c0d267SMichael Rolnik  *
68c8c0d267SMichael Rolnik  * It's also useful to know where some things are, like the IO registers.
69c8c0d267SMichael Rolnik  */
70c8c0d267SMichael Rolnik /* Flash program memory */
71c8c0d267SMichael Rolnik #define OFFSET_CODE 0x00000000
72c8c0d267SMichael Rolnik /* CPU registers, IO registers, and SRAM */
73c8c0d267SMichael Rolnik #define OFFSET_DATA 0x00800000
74c8c0d267SMichael Rolnik /*
75c8c0d267SMichael Rolnik  * IO registers, including status register, stack pointer, and memory
76c8c0d267SMichael Rolnik  * mapped peripherals, mapped just after CPU registers
77c8c0d267SMichael Rolnik  */
78c8c0d267SMichael Rolnik #define OFFSET_IO_REGISTERS (OFFSET_DATA + NUMBER_OF_CPU_REGISTERS)
79c8c0d267SMichael Rolnik 
8025a08409SMichael Rolnik typedef enum AVRFeature {
8125a08409SMichael Rolnik     AVR_FEATURE_SRAM,
8225a08409SMichael Rolnik 
8325a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_PC,
8425a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_PC,
8525a08409SMichael Rolnik     AVR_FEATURE_3_BYTE_PC,
8625a08409SMichael Rolnik 
8725a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_SP,
8825a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_SP,
8925a08409SMichael Rolnik 
9025a08409SMichael Rolnik     AVR_FEATURE_BREAK,
9125a08409SMichael Rolnik     AVR_FEATURE_DES,
9225a08409SMichael Rolnik     AVR_FEATURE_RMW, /* Read Modify Write - XCH LAC LAS LAT */
9325a08409SMichael Rolnik 
9425a08409SMichael Rolnik     AVR_FEATURE_EIJMP_EICALL,
9525a08409SMichael Rolnik     AVR_FEATURE_IJMP_ICALL,
9625a08409SMichael Rolnik     AVR_FEATURE_JMP_CALL,
9725a08409SMichael Rolnik 
9825a08409SMichael Rolnik     AVR_FEATURE_ADIW_SBIW,
9925a08409SMichael Rolnik 
10025a08409SMichael Rolnik     AVR_FEATURE_SPM,
10125a08409SMichael Rolnik     AVR_FEATURE_SPMX,
10225a08409SMichael Rolnik 
10325a08409SMichael Rolnik     AVR_FEATURE_ELPMX,
10425a08409SMichael Rolnik     AVR_FEATURE_ELPM,
10525a08409SMichael Rolnik     AVR_FEATURE_LPMX,
10625a08409SMichael Rolnik     AVR_FEATURE_LPM,
10725a08409SMichael Rolnik 
10825a08409SMichael Rolnik     AVR_FEATURE_MOVW,
10925a08409SMichael Rolnik     AVR_FEATURE_MUL,
11025a08409SMichael Rolnik     AVR_FEATURE_RAMPD,
11125a08409SMichael Rolnik     AVR_FEATURE_RAMPX,
11225a08409SMichael Rolnik     AVR_FEATURE_RAMPY,
11325a08409SMichael Rolnik     AVR_FEATURE_RAMPZ,
11425a08409SMichael Rolnik } AVRFeature;
11525a08409SMichael Rolnik 
1161ea4a06aSPhilippe Mathieu-Daudé typedef struct CPUArchState {
117f1c671f9SMichael Rolnik     uint32_t pc_w; /* 0x003fffff up to 22 bits */
118f1c671f9SMichael Rolnik 
119f1c671f9SMichael Rolnik     uint32_t sregC; /* 0x00000001 1 bit */
120f1c671f9SMichael Rolnik     uint32_t sregZ; /* 0x00000001 1 bit */
121f1c671f9SMichael Rolnik     uint32_t sregN; /* 0x00000001 1 bit */
122f1c671f9SMichael Rolnik     uint32_t sregV; /* 0x00000001 1 bit */
123f1c671f9SMichael Rolnik     uint32_t sregS; /* 0x00000001 1 bit */
124f1c671f9SMichael Rolnik     uint32_t sregH; /* 0x00000001 1 bit */
125f1c671f9SMichael Rolnik     uint32_t sregT; /* 0x00000001 1 bit */
126f1c671f9SMichael Rolnik     uint32_t sregI; /* 0x00000001 1 bit */
127f1c671f9SMichael Rolnik 
128f1c671f9SMichael Rolnik     uint32_t rampD; /* 0x00ff0000 8 bits */
129f1c671f9SMichael Rolnik     uint32_t rampX; /* 0x00ff0000 8 bits */
130f1c671f9SMichael Rolnik     uint32_t rampY; /* 0x00ff0000 8 bits */
131f1c671f9SMichael Rolnik     uint32_t rampZ; /* 0x00ff0000 8 bits */
132f1c671f9SMichael Rolnik     uint32_t eind; /* 0x00ff0000 8 bits */
133f1c671f9SMichael Rolnik 
134f1c671f9SMichael Rolnik     uint32_t r[NUMBER_OF_CPU_REGISTERS]; /* 8 bits each */
135f1c671f9SMichael Rolnik     uint32_t sp; /* 16 bits */
136f1c671f9SMichael Rolnik 
137f1c671f9SMichael Rolnik     uint32_t skip; /* if set skip instruction */
138f1c671f9SMichael Rolnik 
139f1c671f9SMichael Rolnik     uint64_t intsrc; /* interrupt sources */
140f1c671f9SMichael Rolnik     bool fullacc; /* CPU/MEM if true MEM only otherwise */
141f1c671f9SMichael Rolnik 
142f1c671f9SMichael Rolnik     uint64_t features;
1431ea4a06aSPhilippe Mathieu-Daudé } CPUAVRState;
144f1c671f9SMichael Rolnik 
145f1c671f9SMichael Rolnik /**
146f1c671f9SMichael Rolnik  *  AVRCPU:
147f1c671f9SMichael Rolnik  *  @env: #CPUAVRState
148f1c671f9SMichael Rolnik  *
149f1c671f9SMichael Rolnik  *  A AVR CPU.
150f1c671f9SMichael Rolnik  */
151b36e239eSPhilippe Mathieu-Daudé struct ArchCPU {
152f1c671f9SMichael Rolnik     CPUState parent_obj;
153f1c671f9SMichael Rolnik 
154f1c671f9SMichael Rolnik     CPUAVRState env;
155235948bfSGihun Nam 
156204a7bd8SRichard Henderson     MemoryRegion cpu_reg1;
157204a7bd8SRichard Henderson     MemoryRegion cpu_reg2;
158204a7bd8SRichard Henderson 
159235948bfSGihun Nam     /* Initial value of stack pointer */
160235948bfSGihun Nam     uint32_t init_sp;
1619295b1aaSPhilippe Mathieu-Daudé };
162f1c671f9SMichael Rolnik 
1639348028eSPhilippe Mathieu-Daudé /**
1649348028eSPhilippe Mathieu-Daudé  *  AVRCPUClass:
1659348028eSPhilippe Mathieu-Daudé  *  @parent_realize: The parent class' realize handler.
1669348028eSPhilippe Mathieu-Daudé  *  @parent_phases: The parent class' reset phase handlers.
1679348028eSPhilippe Mathieu-Daudé  *
1689348028eSPhilippe Mathieu-Daudé  *  A AVR CPU model.
1699348028eSPhilippe Mathieu-Daudé  */
1709348028eSPhilippe Mathieu-Daudé struct AVRCPUClass {
1719348028eSPhilippe Mathieu-Daudé     CPUClass parent_class;
1729348028eSPhilippe Mathieu-Daudé 
1739348028eSPhilippe Mathieu-Daudé     DeviceRealize parent_realize;
1749348028eSPhilippe Mathieu-Daudé     ResettablePhases parent_phases;
1759348028eSPhilippe Mathieu-Daudé };
1769348028eSPhilippe Mathieu-Daudé 
1773fa28dd6SMichael Rolnik extern const struct VMStateDescription vms_avr_cpu;
1783fa28dd6SMichael Rolnik 
179f1c671f9SMichael Rolnik void avr_cpu_do_interrupt(CPUState *cpu);
180f1c671f9SMichael Rolnik bool avr_cpu_exec_interrupt(CPUState *cpu, int int_req);
181f1c671f9SMichael Rolnik hwaddr avr_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
18212b35405SMichael Rolnik int avr_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
18312b35405SMichael Rolnik int avr_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
1849d8caa67SMichael Rolnik int avr_print_insn(bfd_vma addr, disassemble_info *info);
185e64cb6c2SRichard Henderson vaddr avr_cpu_gdb_adjust_breakpoint(CPUState *cpu, vaddr addr);
186f1c671f9SMichael Rolnik 
avr_feature(CPUAVRState * env,AVRFeature feature)18725a08409SMichael Rolnik static inline int avr_feature(CPUAVRState *env, AVRFeature feature)
18825a08409SMichael Rolnik {
18925a08409SMichael Rolnik     return (env->features & (1U << feature)) != 0;
19025a08409SMichael Rolnik }
19125a08409SMichael Rolnik 
set_avr_feature(CPUAVRState * env,int feature)19225a08409SMichael Rolnik static inline void set_avr_feature(CPUAVRState *env, int feature)
19325a08409SMichael Rolnik {
19425a08409SMichael Rolnik     env->features |= (1U << feature);
19525a08409SMichael Rolnik }
19625a08409SMichael Rolnik 
197f1c671f9SMichael Rolnik void avr_cpu_tcg_init(void);
198e4a8e093SRichard Henderson void avr_cpu_translate_code(CPUState *cs, TranslationBlock *tb,
199e4a8e093SRichard Henderson                             int *max_insns, vaddr pc, void *host_pc);
200f1c671f9SMichael Rolnik 
201f1c671f9SMichael Rolnik int cpu_avr_exec(CPUState *cpu);
202f1c671f9SMichael Rolnik 
203f1c671f9SMichael Rolnik enum {
204f1c671f9SMichael Rolnik     TB_FLAGS_FULL_ACCESS = 1,
205f1c671f9SMichael Rolnik     TB_FLAGS_SKIP = 2,
206f1c671f9SMichael Rolnik };
207f1c671f9SMichael Rolnik 
cpu_interrupts_enabled(CPUAVRState * env)208f1c671f9SMichael Rolnik static inline int cpu_interrupts_enabled(CPUAVRState *env)
209f1c671f9SMichael Rolnik {
210f1c671f9SMichael Rolnik     return env->sregI != 0;
211f1c671f9SMichael Rolnik }
212f1c671f9SMichael Rolnik 
cpu_get_sreg(CPUAVRState * env)213f1c671f9SMichael Rolnik static inline uint8_t cpu_get_sreg(CPUAVRState *env)
214f1c671f9SMichael Rolnik {
21566997c42SMarkus Armbruster     return (env->sregC) << 0
216f1c671f9SMichael Rolnik          | (env->sregZ) << 1
217f1c671f9SMichael Rolnik          | (env->sregN) << 2
218f1c671f9SMichael Rolnik          | (env->sregV) << 3
219f1c671f9SMichael Rolnik          | (env->sregS) << 4
220f1c671f9SMichael Rolnik          | (env->sregH) << 5
221f1c671f9SMichael Rolnik          | (env->sregT) << 6
222f1c671f9SMichael Rolnik          | (env->sregI) << 7;
223f1c671f9SMichael Rolnik }
224f1c671f9SMichael Rolnik 
cpu_set_sreg(CPUAVRState * env,uint8_t sreg)225f1c671f9SMichael Rolnik static inline void cpu_set_sreg(CPUAVRState *env, uint8_t sreg)
226f1c671f9SMichael Rolnik {
227f1c671f9SMichael Rolnik     env->sregC = (sreg >> 0) & 0x01;
228f1c671f9SMichael Rolnik     env->sregZ = (sreg >> 1) & 0x01;
229f1c671f9SMichael Rolnik     env->sregN = (sreg >> 2) & 0x01;
230f1c671f9SMichael Rolnik     env->sregV = (sreg >> 3) & 0x01;
231f1c671f9SMichael Rolnik     env->sregS = (sreg >> 4) & 0x01;
232f1c671f9SMichael Rolnik     env->sregH = (sreg >> 5) & 0x01;
233f1c671f9SMichael Rolnik     env->sregT = (sreg >> 6) & 0x01;
234f1c671f9SMichael Rolnik     env->sregI = (sreg >> 7) & 0x01;
235f1c671f9SMichael Rolnik }
236f1c671f9SMichael Rolnik 
237f1c671f9SMichael Rolnik bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
238f1c671f9SMichael Rolnik                       MMUAccessType access_type, int mmu_idx,
239f1c671f9SMichael Rolnik                       bool probe, uintptr_t retaddr);
240f1c671f9SMichael Rolnik 
241204a7bd8SRichard Henderson extern const MemoryRegionOps avr_cpu_reg1;
242204a7bd8SRichard Henderson extern const MemoryRegionOps avr_cpu_reg2;
243204a7bd8SRichard Henderson 
244ea9cea93SMarkus Armbruster #endif /* QEMU_AVR_CPU_H */
245