xref: /qemu/target/avr/cpu.h (revision 22a7c2f239229b2ee9fcbac03cb598d9aebb9196)
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"
25c8c0d267SMichael Rolnik #include "exec/cpu-defs.h"
26*22a7c2f2SPierrick Bouvier #include "exec/cpu-interrupt.h"
278be545baSRichard Henderson #include "system/memory.h"
28c8c0d267SMichael Rolnik 
29f1c671f9SMichael Rolnik #ifdef CONFIG_USER_ONLY
30f1c671f9SMichael Rolnik #error "AVR 8-bit does not support user mode"
31f1c671f9SMichael Rolnik #endif
32f1c671f9SMichael Rolnik 
33f1c671f9SMichael Rolnik #define CPU_RESOLVING_TYPE TYPE_AVR_CPU
34f1c671f9SMichael Rolnik 
35c8c0d267SMichael Rolnik /*
36c8c0d267SMichael Rolnik  * AVR has two memory spaces, data & code.
37c8c0d267SMichael Rolnik  * e.g. both have 0 address
38c8c0d267SMichael Rolnik  * ST/LD instructions access data space
39c8c0d267SMichael Rolnik  * LPM/SPM and instruction fetching access code memory space
40c8c0d267SMichael Rolnik  */
41c8c0d267SMichael Rolnik #define MMU_CODE_IDX 0
42c8c0d267SMichael Rolnik #define MMU_DATA_IDX 1
43c8c0d267SMichael Rolnik 
44c8c0d267SMichael Rolnik #define EXCP_RESET 1
45c8c0d267SMichael Rolnik #define EXCP_INT(n) (EXCP_RESET + (n) + 1)
46c8c0d267SMichael Rolnik 
47c8c0d267SMichael Rolnik /* Number of CPU registers */
48c8c0d267SMichael Rolnik #define NUMBER_OF_CPU_REGISTERS 32
49c8c0d267SMichael Rolnik 
50a2860ff9SRichard Henderson /* CPU registers mapped into i/o ports 0x38-0x3f. */
51a2860ff9SRichard Henderson #define REG_38_RAMPD  0
52a2860ff9SRichard Henderson #define REG_38_RAMPX  1
53a2860ff9SRichard Henderson #define REG_38_RAMPY  2
54a2860ff9SRichard Henderson #define REG_38_RAMPZ  3
55a2860ff9SRichard Henderson #define REG_38_EIDN   4
56a2860ff9SRichard Henderson #define REG_38_SPL    5
57a2860ff9SRichard Henderson #define REG_38_SPH    6
58a2860ff9SRichard Henderson #define REG_38_SREG   7
59a2860ff9SRichard Henderson 
60c8c0d267SMichael Rolnik /*
61c8c0d267SMichael Rolnik  * Offsets of AVR memory regions in host memory space.
62c8c0d267SMichael Rolnik  *
63c8c0d267SMichael Rolnik  * This is needed because the AVR has separate code and data address
64c8c0d267SMichael Rolnik  * spaces that both have start from zero but have to go somewhere in
65c8c0d267SMichael Rolnik  * host memory.
66c8c0d267SMichael Rolnik  *
67c8c0d267SMichael Rolnik  * It's also useful to know where some things are, like the IO registers.
68c8c0d267SMichael Rolnik  */
69c8c0d267SMichael Rolnik /* Flash program memory */
70c8c0d267SMichael Rolnik #define OFFSET_CODE 0x00000000
71c8c0d267SMichael Rolnik /* CPU registers, IO registers, and SRAM */
72c8c0d267SMichael Rolnik #define OFFSET_DATA 0x00800000
73c8c0d267SMichael Rolnik /*
74c8c0d267SMichael Rolnik  * IO registers, including status register, stack pointer, and memory
75c8c0d267SMichael Rolnik  * mapped peripherals, mapped just after CPU registers
76c8c0d267SMichael Rolnik  */
77c8c0d267SMichael Rolnik #define OFFSET_IO_REGISTERS (OFFSET_DATA + NUMBER_OF_CPU_REGISTERS)
78c8c0d267SMichael Rolnik 
7925a08409SMichael Rolnik typedef enum AVRFeature {
8025a08409SMichael Rolnik     AVR_FEATURE_SRAM,
8125a08409SMichael Rolnik 
8225a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_PC,
8325a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_PC,
8425a08409SMichael Rolnik     AVR_FEATURE_3_BYTE_PC,
8525a08409SMichael Rolnik 
8625a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_SP,
8725a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_SP,
8825a08409SMichael Rolnik 
8925a08409SMichael Rolnik     AVR_FEATURE_BREAK,
9025a08409SMichael Rolnik     AVR_FEATURE_DES,
9125a08409SMichael Rolnik     AVR_FEATURE_RMW, /* Read Modify Write - XCH LAC LAS LAT */
9225a08409SMichael Rolnik 
9325a08409SMichael Rolnik     AVR_FEATURE_EIJMP_EICALL,
9425a08409SMichael Rolnik     AVR_FEATURE_IJMP_ICALL,
9525a08409SMichael Rolnik     AVR_FEATURE_JMP_CALL,
9625a08409SMichael Rolnik 
9725a08409SMichael Rolnik     AVR_FEATURE_ADIW_SBIW,
9825a08409SMichael Rolnik 
9925a08409SMichael Rolnik     AVR_FEATURE_SPM,
10025a08409SMichael Rolnik     AVR_FEATURE_SPMX,
10125a08409SMichael Rolnik 
10225a08409SMichael Rolnik     AVR_FEATURE_ELPMX,
10325a08409SMichael Rolnik     AVR_FEATURE_ELPM,
10425a08409SMichael Rolnik     AVR_FEATURE_LPMX,
10525a08409SMichael Rolnik     AVR_FEATURE_LPM,
10625a08409SMichael Rolnik 
10725a08409SMichael Rolnik     AVR_FEATURE_MOVW,
10825a08409SMichael Rolnik     AVR_FEATURE_MUL,
10925a08409SMichael Rolnik     AVR_FEATURE_RAMPD,
11025a08409SMichael Rolnik     AVR_FEATURE_RAMPX,
11125a08409SMichael Rolnik     AVR_FEATURE_RAMPY,
11225a08409SMichael Rolnik     AVR_FEATURE_RAMPZ,
11325a08409SMichael Rolnik } AVRFeature;
11425a08409SMichael Rolnik 
1151ea4a06aSPhilippe Mathieu-Daudé typedef struct CPUArchState {
116f1c671f9SMichael Rolnik     uint32_t pc_w; /* 0x003fffff up to 22 bits */
117f1c671f9SMichael Rolnik 
118f1c671f9SMichael Rolnik     uint32_t sregC; /* 0x00000001 1 bit */
119f1c671f9SMichael Rolnik     uint32_t sregZ; /* 0x00000001 1 bit */
120f1c671f9SMichael Rolnik     uint32_t sregN; /* 0x00000001 1 bit */
121f1c671f9SMichael Rolnik     uint32_t sregV; /* 0x00000001 1 bit */
122f1c671f9SMichael Rolnik     uint32_t sregS; /* 0x00000001 1 bit */
123f1c671f9SMichael Rolnik     uint32_t sregH; /* 0x00000001 1 bit */
124f1c671f9SMichael Rolnik     uint32_t sregT; /* 0x00000001 1 bit */
125f1c671f9SMichael Rolnik     uint32_t sregI; /* 0x00000001 1 bit */
126f1c671f9SMichael Rolnik 
127f1c671f9SMichael Rolnik     uint32_t rampD; /* 0x00ff0000 8 bits */
128f1c671f9SMichael Rolnik     uint32_t rampX; /* 0x00ff0000 8 bits */
129f1c671f9SMichael Rolnik     uint32_t rampY; /* 0x00ff0000 8 bits */
130f1c671f9SMichael Rolnik     uint32_t rampZ; /* 0x00ff0000 8 bits */
131f1c671f9SMichael Rolnik     uint32_t eind; /* 0x00ff0000 8 bits */
132f1c671f9SMichael Rolnik 
133f1c671f9SMichael Rolnik     uint32_t r[NUMBER_OF_CPU_REGISTERS]; /* 8 bits each */
134f1c671f9SMichael Rolnik     uint32_t sp; /* 16 bits */
135f1c671f9SMichael Rolnik 
136f1c671f9SMichael Rolnik     uint32_t skip; /* if set skip instruction */
137f1c671f9SMichael Rolnik 
138f1c671f9SMichael Rolnik     uint64_t intsrc; /* interrupt sources */
139f1c671f9SMichael Rolnik     bool fullacc; /* CPU/MEM if true MEM only otherwise */
140f1c671f9SMichael Rolnik 
141f1c671f9SMichael Rolnik     uint64_t features;
1421ea4a06aSPhilippe Mathieu-Daudé } CPUAVRState;
143f1c671f9SMichael Rolnik 
144f1c671f9SMichael Rolnik /**
145f1c671f9SMichael Rolnik  *  AVRCPU:
146f1c671f9SMichael Rolnik  *  @env: #CPUAVRState
147f1c671f9SMichael Rolnik  *
148f1c671f9SMichael Rolnik  *  A AVR CPU.
149f1c671f9SMichael Rolnik  */
150b36e239eSPhilippe Mathieu-Daudé struct ArchCPU {
151f1c671f9SMichael Rolnik     CPUState parent_obj;
152f1c671f9SMichael Rolnik 
153f1c671f9SMichael Rolnik     CPUAVRState env;
154235948bfSGihun Nam 
155204a7bd8SRichard Henderson     MemoryRegion cpu_reg1;
156204a7bd8SRichard Henderson     MemoryRegion cpu_reg2;
157204a7bd8SRichard Henderson 
158235948bfSGihun Nam     /* Initial value of stack pointer */
159235948bfSGihun Nam     uint32_t init_sp;
1609295b1aaSPhilippe Mathieu-Daudé };
161f1c671f9SMichael Rolnik 
1629348028eSPhilippe Mathieu-Daudé /**
1639348028eSPhilippe Mathieu-Daudé  *  AVRCPUClass:
1649348028eSPhilippe Mathieu-Daudé  *  @parent_realize: The parent class' realize handler.
1659348028eSPhilippe Mathieu-Daudé  *  @parent_phases: The parent class' reset phase handlers.
1669348028eSPhilippe Mathieu-Daudé  *
1679348028eSPhilippe Mathieu-Daudé  *  A AVR CPU model.
1689348028eSPhilippe Mathieu-Daudé  */
1699348028eSPhilippe Mathieu-Daudé struct AVRCPUClass {
1709348028eSPhilippe Mathieu-Daudé     CPUClass parent_class;
1719348028eSPhilippe Mathieu-Daudé 
1729348028eSPhilippe Mathieu-Daudé     DeviceRealize parent_realize;
1739348028eSPhilippe Mathieu-Daudé     ResettablePhases parent_phases;
1749348028eSPhilippe Mathieu-Daudé };
1759348028eSPhilippe Mathieu-Daudé 
1763fa28dd6SMichael Rolnik extern const struct VMStateDescription vms_avr_cpu;
1773fa28dd6SMichael Rolnik 
178f1c671f9SMichael Rolnik void avr_cpu_do_interrupt(CPUState *cpu);
179f1c671f9SMichael Rolnik bool avr_cpu_exec_interrupt(CPUState *cpu, int int_req);
180f1c671f9SMichael Rolnik hwaddr avr_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
18112b35405SMichael Rolnik int avr_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
18212b35405SMichael Rolnik int avr_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
1839d8caa67SMichael Rolnik int avr_print_insn(bfd_vma addr, disassemble_info *info);
184e64cb6c2SRichard Henderson vaddr avr_cpu_gdb_adjust_breakpoint(CPUState *cpu, vaddr addr);
185f1c671f9SMichael Rolnik 
18625a08409SMichael Rolnik static inline int avr_feature(CPUAVRState *env, AVRFeature feature)
18725a08409SMichael Rolnik {
18825a08409SMichael Rolnik     return (env->features & (1U << feature)) != 0;
18925a08409SMichael Rolnik }
19025a08409SMichael Rolnik 
19125a08409SMichael Rolnik static inline void set_avr_feature(CPUAVRState *env, int feature)
19225a08409SMichael Rolnik {
19325a08409SMichael Rolnik     env->features |= (1U << feature);
19425a08409SMichael Rolnik }
19525a08409SMichael Rolnik 
196f1c671f9SMichael Rolnik void avr_cpu_tcg_init(void);
197e4a8e093SRichard Henderson void avr_cpu_translate_code(CPUState *cs, TranslationBlock *tb,
198e4a8e093SRichard Henderson                             int *max_insns, vaddr pc, void *host_pc);
199f1c671f9SMichael Rolnik 
200f1c671f9SMichael Rolnik int cpu_avr_exec(CPUState *cpu);
201f1c671f9SMichael Rolnik 
202f1c671f9SMichael Rolnik enum {
203f1c671f9SMichael Rolnik     TB_FLAGS_FULL_ACCESS = 1,
204f1c671f9SMichael Rolnik     TB_FLAGS_SKIP = 2,
205f1c671f9SMichael Rolnik };
206f1c671f9SMichael Rolnik 
207bb5de525SAnton Johansson static inline void cpu_get_tb_cpu_state(CPUAVRState *env, vaddr *pc,
208bb5de525SAnton Johansson                                         uint64_t *cs_base, uint32_t *pflags)
209f1c671f9SMichael Rolnik {
210f1c671f9SMichael Rolnik     uint32_t flags = 0;
211f1c671f9SMichael Rolnik 
212f1c671f9SMichael Rolnik     *pc = env->pc_w * 2;
213f1c671f9SMichael Rolnik     *cs_base = 0;
214f1c671f9SMichael Rolnik 
215f1c671f9SMichael Rolnik     if (env->fullacc) {
216f1c671f9SMichael Rolnik         flags |= TB_FLAGS_FULL_ACCESS;
217f1c671f9SMichael Rolnik     }
218f1c671f9SMichael Rolnik     if (env->skip) {
219f1c671f9SMichael Rolnik         flags |= TB_FLAGS_SKIP;
220f1c671f9SMichael Rolnik     }
221f1c671f9SMichael Rolnik 
222f1c671f9SMichael Rolnik     *pflags = flags;
223f1c671f9SMichael Rolnik }
224f1c671f9SMichael Rolnik 
225f1c671f9SMichael Rolnik static inline int cpu_interrupts_enabled(CPUAVRState *env)
226f1c671f9SMichael Rolnik {
227f1c671f9SMichael Rolnik     return env->sregI != 0;
228f1c671f9SMichael Rolnik }
229f1c671f9SMichael Rolnik 
230f1c671f9SMichael Rolnik static inline uint8_t cpu_get_sreg(CPUAVRState *env)
231f1c671f9SMichael Rolnik {
23266997c42SMarkus Armbruster     return (env->sregC) << 0
233f1c671f9SMichael Rolnik          | (env->sregZ) << 1
234f1c671f9SMichael Rolnik          | (env->sregN) << 2
235f1c671f9SMichael Rolnik          | (env->sregV) << 3
236f1c671f9SMichael Rolnik          | (env->sregS) << 4
237f1c671f9SMichael Rolnik          | (env->sregH) << 5
238f1c671f9SMichael Rolnik          | (env->sregT) << 6
239f1c671f9SMichael Rolnik          | (env->sregI) << 7;
240f1c671f9SMichael Rolnik }
241f1c671f9SMichael Rolnik 
242f1c671f9SMichael Rolnik static inline void cpu_set_sreg(CPUAVRState *env, uint8_t sreg)
243f1c671f9SMichael Rolnik {
244f1c671f9SMichael Rolnik     env->sregC = (sreg >> 0) & 0x01;
245f1c671f9SMichael Rolnik     env->sregZ = (sreg >> 1) & 0x01;
246f1c671f9SMichael Rolnik     env->sregN = (sreg >> 2) & 0x01;
247f1c671f9SMichael Rolnik     env->sregV = (sreg >> 3) & 0x01;
248f1c671f9SMichael Rolnik     env->sregS = (sreg >> 4) & 0x01;
249f1c671f9SMichael Rolnik     env->sregH = (sreg >> 5) & 0x01;
250f1c671f9SMichael Rolnik     env->sregT = (sreg >> 6) & 0x01;
251f1c671f9SMichael Rolnik     env->sregI = (sreg >> 7) & 0x01;
252f1c671f9SMichael Rolnik }
253f1c671f9SMichael Rolnik 
254f1c671f9SMichael Rolnik bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
255f1c671f9SMichael Rolnik                       MMUAccessType access_type, int mmu_idx,
256f1c671f9SMichael Rolnik                       bool probe, uintptr_t retaddr);
257f1c671f9SMichael Rolnik 
258204a7bd8SRichard Henderson extern const MemoryRegionOps avr_cpu_reg1;
259204a7bd8SRichard Henderson extern const MemoryRegionOps avr_cpu_reg2;
260204a7bd8SRichard Henderson 
261f1c671f9SMichael Rolnik #include "exec/cpu-all.h"
262f1c671f9SMichael Rolnik 
263ea9cea93SMarkus Armbruster #endif /* QEMU_AVR_CPU_H */
264