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 CPU_RESOLVING_TYPE TYPE_AVR_CPU 32f1c671f9SMichael Rolnik 33c8c0d267SMichael Rolnik #define TCG_GUEST_DEFAULT_MO 0 34c8c0d267SMichael 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 /* Number of IO registers accessible by ld/st/in/out */ 50c8c0d267SMichael Rolnik #define NUMBER_OF_IO_REGISTERS 64 51c8c0d267SMichael Rolnik 52c8c0d267SMichael Rolnik /* 53c8c0d267SMichael Rolnik * Offsets of AVR memory regions in host memory space. 54c8c0d267SMichael Rolnik * 55c8c0d267SMichael Rolnik * This is needed because the AVR has separate code and data address 56c8c0d267SMichael Rolnik * spaces that both have start from zero but have to go somewhere in 57c8c0d267SMichael Rolnik * host memory. 58c8c0d267SMichael Rolnik * 59c8c0d267SMichael Rolnik * It's also useful to know where some things are, like the IO registers. 60c8c0d267SMichael Rolnik */ 61c8c0d267SMichael Rolnik /* Flash program memory */ 62c8c0d267SMichael Rolnik #define OFFSET_CODE 0x00000000 63c8c0d267SMichael Rolnik /* CPU registers, IO registers, and SRAM */ 64c8c0d267SMichael Rolnik #define OFFSET_DATA 0x00800000 65c8c0d267SMichael Rolnik /* CPU registers specifically, these are mapped at the start of data */ 66c8c0d267SMichael Rolnik #define OFFSET_CPU_REGISTERS OFFSET_DATA 67c8c0d267SMichael Rolnik /* 68c8c0d267SMichael Rolnik * IO registers, including status register, stack pointer, and memory 69c8c0d267SMichael Rolnik * mapped peripherals, mapped just after CPU registers 70c8c0d267SMichael Rolnik */ 71c8c0d267SMichael Rolnik #define OFFSET_IO_REGISTERS (OFFSET_DATA + NUMBER_OF_CPU_REGISTERS) 72c8c0d267SMichael Rolnik 7325a08409SMichael Rolnik typedef enum AVRFeature { 7425a08409SMichael Rolnik AVR_FEATURE_SRAM, 7525a08409SMichael Rolnik 7625a08409SMichael Rolnik AVR_FEATURE_1_BYTE_PC, 7725a08409SMichael Rolnik AVR_FEATURE_2_BYTE_PC, 7825a08409SMichael Rolnik AVR_FEATURE_3_BYTE_PC, 7925a08409SMichael Rolnik 8025a08409SMichael Rolnik AVR_FEATURE_1_BYTE_SP, 8125a08409SMichael Rolnik AVR_FEATURE_2_BYTE_SP, 8225a08409SMichael Rolnik 8325a08409SMichael Rolnik AVR_FEATURE_BREAK, 8425a08409SMichael Rolnik AVR_FEATURE_DES, 8525a08409SMichael Rolnik AVR_FEATURE_RMW, /* Read Modify Write - XCH LAC LAS LAT */ 8625a08409SMichael Rolnik 8725a08409SMichael Rolnik AVR_FEATURE_EIJMP_EICALL, 8825a08409SMichael Rolnik AVR_FEATURE_IJMP_ICALL, 8925a08409SMichael Rolnik AVR_FEATURE_JMP_CALL, 9025a08409SMichael Rolnik 9125a08409SMichael Rolnik AVR_FEATURE_ADIW_SBIW, 9225a08409SMichael Rolnik 9325a08409SMichael Rolnik AVR_FEATURE_SPM, 9425a08409SMichael Rolnik AVR_FEATURE_SPMX, 9525a08409SMichael Rolnik 9625a08409SMichael Rolnik AVR_FEATURE_ELPMX, 9725a08409SMichael Rolnik AVR_FEATURE_ELPM, 9825a08409SMichael Rolnik AVR_FEATURE_LPMX, 9925a08409SMichael Rolnik AVR_FEATURE_LPM, 10025a08409SMichael Rolnik 10125a08409SMichael Rolnik AVR_FEATURE_MOVW, 10225a08409SMichael Rolnik AVR_FEATURE_MUL, 10325a08409SMichael Rolnik AVR_FEATURE_RAMPD, 10425a08409SMichael Rolnik AVR_FEATURE_RAMPX, 10525a08409SMichael Rolnik AVR_FEATURE_RAMPY, 10625a08409SMichael Rolnik AVR_FEATURE_RAMPZ, 10725a08409SMichael Rolnik } AVRFeature; 10825a08409SMichael Rolnik 1091ea4a06aSPhilippe Mathieu-Daudé typedef struct CPUArchState { 110f1c671f9SMichael Rolnik uint32_t pc_w; /* 0x003fffff up to 22 bits */ 111f1c671f9SMichael Rolnik 112f1c671f9SMichael Rolnik uint32_t sregC; /* 0x00000001 1 bit */ 113f1c671f9SMichael Rolnik uint32_t sregZ; /* 0x00000001 1 bit */ 114f1c671f9SMichael Rolnik uint32_t sregN; /* 0x00000001 1 bit */ 115f1c671f9SMichael Rolnik uint32_t sregV; /* 0x00000001 1 bit */ 116f1c671f9SMichael Rolnik uint32_t sregS; /* 0x00000001 1 bit */ 117f1c671f9SMichael Rolnik uint32_t sregH; /* 0x00000001 1 bit */ 118f1c671f9SMichael Rolnik uint32_t sregT; /* 0x00000001 1 bit */ 119f1c671f9SMichael Rolnik uint32_t sregI; /* 0x00000001 1 bit */ 120f1c671f9SMichael Rolnik 121f1c671f9SMichael Rolnik uint32_t rampD; /* 0x00ff0000 8 bits */ 122f1c671f9SMichael Rolnik uint32_t rampX; /* 0x00ff0000 8 bits */ 123f1c671f9SMichael Rolnik uint32_t rampY; /* 0x00ff0000 8 bits */ 124f1c671f9SMichael Rolnik uint32_t rampZ; /* 0x00ff0000 8 bits */ 125f1c671f9SMichael Rolnik uint32_t eind; /* 0x00ff0000 8 bits */ 126f1c671f9SMichael Rolnik 127f1c671f9SMichael Rolnik uint32_t r[NUMBER_OF_CPU_REGISTERS]; /* 8 bits each */ 128f1c671f9SMichael Rolnik uint32_t sp; /* 16 bits */ 129f1c671f9SMichael Rolnik 130f1c671f9SMichael Rolnik uint32_t skip; /* if set skip instruction */ 131f1c671f9SMichael Rolnik 132f1c671f9SMichael Rolnik uint64_t intsrc; /* interrupt sources */ 133f1c671f9SMichael Rolnik bool fullacc; /* CPU/MEM if true MEM only otherwise */ 134f1c671f9SMichael Rolnik 135f1c671f9SMichael Rolnik uint64_t features; 1361ea4a06aSPhilippe Mathieu-Daudé } CPUAVRState; 137f1c671f9SMichael Rolnik 138f1c671f9SMichael Rolnik /** 139f1c671f9SMichael Rolnik * AVRCPU: 140f1c671f9SMichael Rolnik * @env: #CPUAVRState 141f1c671f9SMichael Rolnik * 142f1c671f9SMichael Rolnik * A AVR CPU. 143f1c671f9SMichael Rolnik */ 144b36e239eSPhilippe Mathieu-Daudé struct ArchCPU { 145f1c671f9SMichael Rolnik CPUState parent_obj; 146f1c671f9SMichael Rolnik 147f1c671f9SMichael Rolnik CPUAVRState env; 1489295b1aaSPhilippe Mathieu-Daudé }; 149f1c671f9SMichael Rolnik 150*9348028eSPhilippe Mathieu-Daudé /** 151*9348028eSPhilippe Mathieu-Daudé * AVRCPUClass: 152*9348028eSPhilippe Mathieu-Daudé * @parent_realize: The parent class' realize handler. 153*9348028eSPhilippe Mathieu-Daudé * @parent_phases: The parent class' reset phase handlers. 154*9348028eSPhilippe Mathieu-Daudé * 155*9348028eSPhilippe Mathieu-Daudé * A AVR CPU model. 156*9348028eSPhilippe Mathieu-Daudé */ 157*9348028eSPhilippe Mathieu-Daudé struct AVRCPUClass { 158*9348028eSPhilippe Mathieu-Daudé CPUClass parent_class; 159*9348028eSPhilippe Mathieu-Daudé 160*9348028eSPhilippe Mathieu-Daudé DeviceRealize parent_realize; 161*9348028eSPhilippe Mathieu-Daudé ResettablePhases parent_phases; 162*9348028eSPhilippe Mathieu-Daudé }; 163*9348028eSPhilippe Mathieu-Daudé 1643fa28dd6SMichael Rolnik extern const struct VMStateDescription vms_avr_cpu; 1653fa28dd6SMichael Rolnik 166f1c671f9SMichael Rolnik void avr_cpu_do_interrupt(CPUState *cpu); 167f1c671f9SMichael Rolnik bool avr_cpu_exec_interrupt(CPUState *cpu, int int_req); 168f1c671f9SMichael Rolnik hwaddr avr_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); 16912b35405SMichael Rolnik int avr_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg); 17012b35405SMichael Rolnik int avr_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg); 1719d8caa67SMichael Rolnik int avr_print_insn(bfd_vma addr, disassemble_info *info); 172e64cb6c2SRichard Henderson vaddr avr_cpu_gdb_adjust_breakpoint(CPUState *cpu, vaddr addr); 173f1c671f9SMichael Rolnik 17425a08409SMichael Rolnik static inline int avr_feature(CPUAVRState *env, AVRFeature feature) 17525a08409SMichael Rolnik { 17625a08409SMichael Rolnik return (env->features & (1U << feature)) != 0; 17725a08409SMichael Rolnik } 17825a08409SMichael Rolnik 17925a08409SMichael Rolnik static inline void set_avr_feature(CPUAVRState *env, int feature) 18025a08409SMichael Rolnik { 18125a08409SMichael Rolnik env->features |= (1U << feature); 18225a08409SMichael Rolnik } 18325a08409SMichael Rolnik 184f1c671f9SMichael Rolnik #define cpu_list avr_cpu_list 185f1c671f9SMichael Rolnik #define cpu_mmu_index avr_cpu_mmu_index 186f1c671f9SMichael Rolnik 187f1c671f9SMichael Rolnik static inline int avr_cpu_mmu_index(CPUAVRState *env, bool ifetch) 188f1c671f9SMichael Rolnik { 189f1c671f9SMichael Rolnik return ifetch ? MMU_CODE_IDX : MMU_DATA_IDX; 190f1c671f9SMichael Rolnik } 191f1c671f9SMichael Rolnik 192f1c671f9SMichael Rolnik void avr_cpu_tcg_init(void); 193f1c671f9SMichael Rolnik 194f1c671f9SMichael Rolnik void avr_cpu_list(void); 195f1c671f9SMichael Rolnik int cpu_avr_exec(CPUState *cpu); 196f1c671f9SMichael Rolnik 197f1c671f9SMichael Rolnik enum { 198f1c671f9SMichael Rolnik TB_FLAGS_FULL_ACCESS = 1, 199f1c671f9SMichael Rolnik TB_FLAGS_SKIP = 2, 200f1c671f9SMichael Rolnik }; 201f1c671f9SMichael Rolnik 202bb5de525SAnton Johansson static inline void cpu_get_tb_cpu_state(CPUAVRState *env, vaddr *pc, 203bb5de525SAnton Johansson uint64_t *cs_base, uint32_t *pflags) 204f1c671f9SMichael Rolnik { 205f1c671f9SMichael Rolnik uint32_t flags = 0; 206f1c671f9SMichael Rolnik 207f1c671f9SMichael Rolnik *pc = env->pc_w * 2; 208f1c671f9SMichael Rolnik *cs_base = 0; 209f1c671f9SMichael Rolnik 210f1c671f9SMichael Rolnik if (env->fullacc) { 211f1c671f9SMichael Rolnik flags |= TB_FLAGS_FULL_ACCESS; 212f1c671f9SMichael Rolnik } 213f1c671f9SMichael Rolnik if (env->skip) { 214f1c671f9SMichael Rolnik flags |= TB_FLAGS_SKIP; 215f1c671f9SMichael Rolnik } 216f1c671f9SMichael Rolnik 217f1c671f9SMichael Rolnik *pflags = flags; 218f1c671f9SMichael Rolnik } 219f1c671f9SMichael Rolnik 220f1c671f9SMichael Rolnik static inline int cpu_interrupts_enabled(CPUAVRState *env) 221f1c671f9SMichael Rolnik { 222f1c671f9SMichael Rolnik return env->sregI != 0; 223f1c671f9SMichael Rolnik } 224f1c671f9SMichael Rolnik 225f1c671f9SMichael Rolnik static inline uint8_t cpu_get_sreg(CPUAVRState *env) 226f1c671f9SMichael Rolnik { 22766997c42SMarkus Armbruster return (env->sregC) << 0 228f1c671f9SMichael Rolnik | (env->sregZ) << 1 229f1c671f9SMichael Rolnik | (env->sregN) << 2 230f1c671f9SMichael Rolnik | (env->sregV) << 3 231f1c671f9SMichael Rolnik | (env->sregS) << 4 232f1c671f9SMichael Rolnik | (env->sregH) << 5 233f1c671f9SMichael Rolnik | (env->sregT) << 6 234f1c671f9SMichael Rolnik | (env->sregI) << 7; 235f1c671f9SMichael Rolnik } 236f1c671f9SMichael Rolnik 237f1c671f9SMichael Rolnik static inline void cpu_set_sreg(CPUAVRState *env, uint8_t sreg) 238f1c671f9SMichael Rolnik { 239f1c671f9SMichael Rolnik env->sregC = (sreg >> 0) & 0x01; 240f1c671f9SMichael Rolnik env->sregZ = (sreg >> 1) & 0x01; 241f1c671f9SMichael Rolnik env->sregN = (sreg >> 2) & 0x01; 242f1c671f9SMichael Rolnik env->sregV = (sreg >> 3) & 0x01; 243f1c671f9SMichael Rolnik env->sregS = (sreg >> 4) & 0x01; 244f1c671f9SMichael Rolnik env->sregH = (sreg >> 5) & 0x01; 245f1c671f9SMichael Rolnik env->sregT = (sreg >> 6) & 0x01; 246f1c671f9SMichael Rolnik env->sregI = (sreg >> 7) & 0x01; 247f1c671f9SMichael Rolnik } 248f1c671f9SMichael Rolnik 249f1c671f9SMichael Rolnik bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size, 250f1c671f9SMichael Rolnik MMUAccessType access_type, int mmu_idx, 251f1c671f9SMichael Rolnik bool probe, uintptr_t retaddr); 252f1c671f9SMichael Rolnik 253f1c671f9SMichael Rolnik #include "exec/cpu-all.h" 254f1c671f9SMichael Rolnik 255ea9cea93SMarkus Armbruster #endif /* QEMU_AVR_CPU_H */ 256