xref: /qemu/target/avr/cpu.h (revision 9295b1aa92d3efb3c08b71ee751fbfd83ea02f4d)
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"
26c8c0d267SMichael Rolnik 
27f1c671f9SMichael Rolnik #ifdef CONFIG_USER_ONLY
28f1c671f9SMichael Rolnik #error "AVR 8-bit does not support user mode"
29f1c671f9SMichael Rolnik #endif
30f1c671f9SMichael Rolnik 
31f1c671f9SMichael Rolnik #define AVR_CPU_TYPE_SUFFIX "-" TYPE_AVR_CPU
32f1c671f9SMichael Rolnik #define AVR_CPU_TYPE_NAME(name) (name AVR_CPU_TYPE_SUFFIX)
33f1c671f9SMichael Rolnik #define CPU_RESOLVING_TYPE TYPE_AVR_CPU
34f1c671f9SMichael Rolnik 
35c8c0d267SMichael Rolnik #define TCG_GUEST_DEFAULT_MO 0
36c8c0d267SMichael Rolnik 
37c8c0d267SMichael Rolnik /*
38c8c0d267SMichael Rolnik  * AVR has two memory spaces, data & code.
39c8c0d267SMichael Rolnik  * e.g. both have 0 address
40c8c0d267SMichael Rolnik  * ST/LD instructions access data space
41c8c0d267SMichael Rolnik  * LPM/SPM and instruction fetching access code memory space
42c8c0d267SMichael Rolnik  */
43c8c0d267SMichael Rolnik #define MMU_CODE_IDX 0
44c8c0d267SMichael Rolnik #define MMU_DATA_IDX 1
45c8c0d267SMichael Rolnik 
46c8c0d267SMichael Rolnik #define EXCP_RESET 1
47c8c0d267SMichael Rolnik #define EXCP_INT(n) (EXCP_RESET + (n) + 1)
48c8c0d267SMichael Rolnik 
49c8c0d267SMichael Rolnik /* Number of CPU registers */
50c8c0d267SMichael Rolnik #define NUMBER_OF_CPU_REGISTERS 32
51c8c0d267SMichael Rolnik /* Number of IO registers accessible by ld/st/in/out */
52c8c0d267SMichael Rolnik #define NUMBER_OF_IO_REGISTERS 64
53c8c0d267SMichael Rolnik 
54c8c0d267SMichael Rolnik /*
55c8c0d267SMichael Rolnik  * Offsets of AVR memory regions in host memory space.
56c8c0d267SMichael Rolnik  *
57c8c0d267SMichael Rolnik  * This is needed because the AVR has separate code and data address
58c8c0d267SMichael Rolnik  * spaces that both have start from zero but have to go somewhere in
59c8c0d267SMichael Rolnik  * host memory.
60c8c0d267SMichael Rolnik  *
61c8c0d267SMichael Rolnik  * It's also useful to know where some things are, like the IO registers.
62c8c0d267SMichael Rolnik  */
63c8c0d267SMichael Rolnik /* Flash program memory */
64c8c0d267SMichael Rolnik #define OFFSET_CODE 0x00000000
65c8c0d267SMichael Rolnik /* CPU registers, IO registers, and SRAM */
66c8c0d267SMichael Rolnik #define OFFSET_DATA 0x00800000
67c8c0d267SMichael Rolnik /* CPU registers specifically, these are mapped at the start of data */
68c8c0d267SMichael Rolnik #define OFFSET_CPU_REGISTERS OFFSET_DATA
69c8c0d267SMichael Rolnik /*
70c8c0d267SMichael Rolnik  * IO registers, including status register, stack pointer, and memory
71c8c0d267SMichael Rolnik  * mapped peripherals, mapped just after CPU registers
72c8c0d267SMichael Rolnik  */
73c8c0d267SMichael Rolnik #define OFFSET_IO_REGISTERS (OFFSET_DATA + NUMBER_OF_CPU_REGISTERS)
74c8c0d267SMichael Rolnik 
7525a08409SMichael Rolnik typedef enum AVRFeature {
7625a08409SMichael Rolnik     AVR_FEATURE_SRAM,
7725a08409SMichael Rolnik 
7825a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_PC,
7925a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_PC,
8025a08409SMichael Rolnik     AVR_FEATURE_3_BYTE_PC,
8125a08409SMichael Rolnik 
8225a08409SMichael Rolnik     AVR_FEATURE_1_BYTE_SP,
8325a08409SMichael Rolnik     AVR_FEATURE_2_BYTE_SP,
8425a08409SMichael Rolnik 
8525a08409SMichael Rolnik     AVR_FEATURE_BREAK,
8625a08409SMichael Rolnik     AVR_FEATURE_DES,
8725a08409SMichael Rolnik     AVR_FEATURE_RMW, /* Read Modify Write - XCH LAC LAS LAT */
8825a08409SMichael Rolnik 
8925a08409SMichael Rolnik     AVR_FEATURE_EIJMP_EICALL,
9025a08409SMichael Rolnik     AVR_FEATURE_IJMP_ICALL,
9125a08409SMichael Rolnik     AVR_FEATURE_JMP_CALL,
9225a08409SMichael Rolnik 
9325a08409SMichael Rolnik     AVR_FEATURE_ADIW_SBIW,
9425a08409SMichael Rolnik 
9525a08409SMichael Rolnik     AVR_FEATURE_SPM,
9625a08409SMichael Rolnik     AVR_FEATURE_SPMX,
9725a08409SMichael Rolnik 
9825a08409SMichael Rolnik     AVR_FEATURE_ELPMX,
9925a08409SMichael Rolnik     AVR_FEATURE_ELPM,
10025a08409SMichael Rolnik     AVR_FEATURE_LPMX,
10125a08409SMichael Rolnik     AVR_FEATURE_LPM,
10225a08409SMichael Rolnik 
10325a08409SMichael Rolnik     AVR_FEATURE_MOVW,
10425a08409SMichael Rolnik     AVR_FEATURE_MUL,
10525a08409SMichael Rolnik     AVR_FEATURE_RAMPD,
10625a08409SMichael Rolnik     AVR_FEATURE_RAMPX,
10725a08409SMichael Rolnik     AVR_FEATURE_RAMPY,
10825a08409SMichael Rolnik     AVR_FEATURE_RAMPZ,
10925a08409SMichael Rolnik } AVRFeature;
11025a08409SMichael Rolnik 
1111ea4a06aSPhilippe Mathieu-Daudé typedef struct CPUArchState {
112f1c671f9SMichael Rolnik     uint32_t pc_w; /* 0x003fffff up to 22 bits */
113f1c671f9SMichael Rolnik 
114f1c671f9SMichael Rolnik     uint32_t sregC; /* 0x00000001 1 bit */
115f1c671f9SMichael Rolnik     uint32_t sregZ; /* 0x00000001 1 bit */
116f1c671f9SMichael Rolnik     uint32_t sregN; /* 0x00000001 1 bit */
117f1c671f9SMichael Rolnik     uint32_t sregV; /* 0x00000001 1 bit */
118f1c671f9SMichael Rolnik     uint32_t sregS; /* 0x00000001 1 bit */
119f1c671f9SMichael Rolnik     uint32_t sregH; /* 0x00000001 1 bit */
120f1c671f9SMichael Rolnik     uint32_t sregT; /* 0x00000001 1 bit */
121f1c671f9SMichael Rolnik     uint32_t sregI; /* 0x00000001 1 bit */
122f1c671f9SMichael Rolnik 
123f1c671f9SMichael Rolnik     uint32_t rampD; /* 0x00ff0000 8 bits */
124f1c671f9SMichael Rolnik     uint32_t rampX; /* 0x00ff0000 8 bits */
125f1c671f9SMichael Rolnik     uint32_t rampY; /* 0x00ff0000 8 bits */
126f1c671f9SMichael Rolnik     uint32_t rampZ; /* 0x00ff0000 8 bits */
127f1c671f9SMichael Rolnik     uint32_t eind; /* 0x00ff0000 8 bits */
128f1c671f9SMichael Rolnik 
129f1c671f9SMichael Rolnik     uint32_t r[NUMBER_OF_CPU_REGISTERS]; /* 8 bits each */
130f1c671f9SMichael Rolnik     uint32_t sp; /* 16 bits */
131f1c671f9SMichael Rolnik 
132f1c671f9SMichael Rolnik     uint32_t skip; /* if set skip instruction */
133f1c671f9SMichael Rolnik 
134f1c671f9SMichael Rolnik     uint64_t intsrc; /* interrupt sources */
135f1c671f9SMichael Rolnik     bool fullacc; /* CPU/MEM if true MEM only otherwise */
136f1c671f9SMichael Rolnik 
137f1c671f9SMichael Rolnik     uint64_t features;
1381ea4a06aSPhilippe Mathieu-Daudé } CPUAVRState;
139f1c671f9SMichael Rolnik 
140f1c671f9SMichael Rolnik /**
141f1c671f9SMichael Rolnik  *  AVRCPU:
142f1c671f9SMichael Rolnik  *  @env: #CPUAVRState
143f1c671f9SMichael Rolnik  *
144f1c671f9SMichael Rolnik  *  A AVR CPU.
145f1c671f9SMichael Rolnik  */
146*9295b1aaSPhilippe Mathieu-Daudé struct AVRCPU {
147f1c671f9SMichael Rolnik     /*< private >*/
148f1c671f9SMichael Rolnik     CPUState parent_obj;
149f1c671f9SMichael Rolnik     /*< public >*/
150f1c671f9SMichael Rolnik 
151f1c671f9SMichael Rolnik     CPUNegativeOffsetState neg;
152f1c671f9SMichael Rolnik     CPUAVRState env;
153*9295b1aaSPhilippe Mathieu-Daudé };
154f1c671f9SMichael Rolnik 
1553fa28dd6SMichael Rolnik extern const struct VMStateDescription vms_avr_cpu;
1563fa28dd6SMichael Rolnik 
157f1c671f9SMichael Rolnik void avr_cpu_do_interrupt(CPUState *cpu);
158f1c671f9SMichael Rolnik bool avr_cpu_exec_interrupt(CPUState *cpu, int int_req);
159f1c671f9SMichael Rolnik hwaddr avr_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
16012b35405SMichael Rolnik int avr_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
16112b35405SMichael Rolnik int avr_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
1629d8caa67SMichael Rolnik int avr_print_insn(bfd_vma addr, disassemble_info *info);
163e64cb6c2SRichard Henderson vaddr avr_cpu_gdb_adjust_breakpoint(CPUState *cpu, vaddr addr);
164f1c671f9SMichael Rolnik 
16525a08409SMichael Rolnik static inline int avr_feature(CPUAVRState *env, AVRFeature feature)
16625a08409SMichael Rolnik {
16725a08409SMichael Rolnik     return (env->features & (1U << feature)) != 0;
16825a08409SMichael Rolnik }
16925a08409SMichael Rolnik 
17025a08409SMichael Rolnik static inline void set_avr_feature(CPUAVRState *env, int feature)
17125a08409SMichael Rolnik {
17225a08409SMichael Rolnik     env->features |= (1U << feature);
17325a08409SMichael Rolnik }
17425a08409SMichael Rolnik 
175f1c671f9SMichael Rolnik #define cpu_list avr_cpu_list
176f1c671f9SMichael Rolnik #define cpu_mmu_index avr_cpu_mmu_index
177f1c671f9SMichael Rolnik 
178f1c671f9SMichael Rolnik static inline int avr_cpu_mmu_index(CPUAVRState *env, bool ifetch)
179f1c671f9SMichael Rolnik {
180f1c671f9SMichael Rolnik     return ifetch ? MMU_CODE_IDX : MMU_DATA_IDX;
181f1c671f9SMichael Rolnik }
182f1c671f9SMichael Rolnik 
183f1c671f9SMichael Rolnik void avr_cpu_tcg_init(void);
184f1c671f9SMichael Rolnik 
185f1c671f9SMichael Rolnik void avr_cpu_list(void);
186f1c671f9SMichael Rolnik int cpu_avr_exec(CPUState *cpu);
187f1c671f9SMichael Rolnik int avr_cpu_memory_rw_debug(CPUState *cs, vaddr address, uint8_t *buf,
188f1c671f9SMichael Rolnik                             int len, bool is_write);
189f1c671f9SMichael Rolnik 
190f1c671f9SMichael Rolnik enum {
191f1c671f9SMichael Rolnik     TB_FLAGS_FULL_ACCESS = 1,
192f1c671f9SMichael Rolnik     TB_FLAGS_SKIP = 2,
193f1c671f9SMichael Rolnik };
194f1c671f9SMichael Rolnik 
195f1c671f9SMichael Rolnik static inline void cpu_get_tb_cpu_state(CPUAVRState *env, target_ulong *pc,
196f1c671f9SMichael Rolnik                                         target_ulong *cs_base, uint32_t *pflags)
197f1c671f9SMichael Rolnik {
198f1c671f9SMichael Rolnik     uint32_t flags = 0;
199f1c671f9SMichael Rolnik 
200f1c671f9SMichael Rolnik     *pc = env->pc_w * 2;
201f1c671f9SMichael Rolnik     *cs_base = 0;
202f1c671f9SMichael Rolnik 
203f1c671f9SMichael Rolnik     if (env->fullacc) {
204f1c671f9SMichael Rolnik         flags |= TB_FLAGS_FULL_ACCESS;
205f1c671f9SMichael Rolnik     }
206f1c671f9SMichael Rolnik     if (env->skip) {
207f1c671f9SMichael Rolnik         flags |= TB_FLAGS_SKIP;
208f1c671f9SMichael Rolnik     }
209f1c671f9SMichael Rolnik 
210f1c671f9SMichael Rolnik     *pflags = flags;
211f1c671f9SMichael Rolnik }
212f1c671f9SMichael Rolnik 
213f1c671f9SMichael Rolnik static inline int cpu_interrupts_enabled(CPUAVRState *env)
214f1c671f9SMichael Rolnik {
215f1c671f9SMichael Rolnik     return env->sregI != 0;
216f1c671f9SMichael Rolnik }
217f1c671f9SMichael Rolnik 
218f1c671f9SMichael Rolnik static inline uint8_t cpu_get_sreg(CPUAVRState *env)
219f1c671f9SMichael Rolnik {
220f1c671f9SMichael Rolnik     uint8_t sreg;
221f1c671f9SMichael Rolnik     sreg = (env->sregC) << 0
222f1c671f9SMichael Rolnik          | (env->sregZ) << 1
223f1c671f9SMichael Rolnik          | (env->sregN) << 2
224f1c671f9SMichael Rolnik          | (env->sregV) << 3
225f1c671f9SMichael Rolnik          | (env->sregS) << 4
226f1c671f9SMichael Rolnik          | (env->sregH) << 5
227f1c671f9SMichael Rolnik          | (env->sregT) << 6
228f1c671f9SMichael Rolnik          | (env->sregI) << 7;
229f1c671f9SMichael Rolnik     return sreg;
230f1c671f9SMichael Rolnik }
231f1c671f9SMichael Rolnik 
232f1c671f9SMichael Rolnik static inline void cpu_set_sreg(CPUAVRState *env, uint8_t sreg)
233f1c671f9SMichael Rolnik {
234f1c671f9SMichael Rolnik     env->sregC = (sreg >> 0) & 0x01;
235f1c671f9SMichael Rolnik     env->sregZ = (sreg >> 1) & 0x01;
236f1c671f9SMichael Rolnik     env->sregN = (sreg >> 2) & 0x01;
237f1c671f9SMichael Rolnik     env->sregV = (sreg >> 3) & 0x01;
238f1c671f9SMichael Rolnik     env->sregS = (sreg >> 4) & 0x01;
239f1c671f9SMichael Rolnik     env->sregH = (sreg >> 5) & 0x01;
240f1c671f9SMichael Rolnik     env->sregT = (sreg >> 6) & 0x01;
241f1c671f9SMichael Rolnik     env->sregI = (sreg >> 7) & 0x01;
242f1c671f9SMichael Rolnik }
243f1c671f9SMichael Rolnik 
244f1c671f9SMichael Rolnik bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
245f1c671f9SMichael Rolnik                       MMUAccessType access_type, int mmu_idx,
246f1c671f9SMichael Rolnik                       bool probe, uintptr_t retaddr);
247f1c671f9SMichael Rolnik 
248f1c671f9SMichael Rolnik #include "exec/cpu-all.h"
249f1c671f9SMichael Rolnik 
250c8c0d267SMichael Rolnik #endif /* !defined (QEMU_AVR_CPU_H) */
251