sim_core.h Source File

yasimavr: sim_core.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_core.h
Go to the documentation of this file.
1/*
2 * sim_core.h
3 *
4 * Copyright 2021-2026 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_CORE_H__
25#define __YASIMAVR_CORE_H__
26
27#include "sim_types.h"
28#include "sim_pin.h"
29#include "sim_config.h"
30#include "sim_memory.h"
31#include <vector>
32#include <string>
33#include <map>
34
36
37class IORegister;
38class Device;
39class Firmware;
42
43
44//=======================================================================================
45
46//Break opcode, inserted in the program to implement breakpoints
47#define AVR_BREAK_OPCODE 0x9598
48
49//Definition of the bit flags for the SREG register
50enum {
51 SREG_C,// = 0x01,
52 SREG_Z,// = 0x02,
53 SREG_N,// = 0x04,
54 SREG_V,// = 0x08,
55 SREG_S,// = 0x10,
56 SREG_H,// = 0x20,
57 SREG_T,// = 0x40,
58 SREG_I,// = 0x80,
59};
60
61enum {
62 // 16 bits register pairs
67 // stack pointer, in IO register space
68 R_SPL = 0x3d, R_SPH,
69 // real SREG, in IO register space
70 R_SREG = 0x3f,
71};
72
82 uint8_t instr[4];
84 uint8_t instr_len;
85};
86
87
88//=======================================================================================
89
97
98 friend class Device;
99 friend class DeviceDebugProbe;
100
101public:
102
109 enum NVM {
113 NVM_ArchDefined = NVM_CommonCount,
114 NVM_GetCount = 0xFFFF,
115 };
116
117 explicit Core(const CoreConfiguration& config);
118 virtual ~Core();
119
120 const CoreConfiguration& config() const;
121
122 bool init(Device& device);
123
124 void reset();
125
126 int exec_cycle();
127
128 IORegister* get_ioreg(reg_addr_t addr);
129
130 //Peripheral access to the I/O registers
131 uint8_t ioctl_read_ioreg(reg_addr_t addr);
132 void ioctl_write_ioreg(reg_addr_t addr, bitmask_t bm, uint8_t value);
133
134 void start_interrupt_inhibit(unsigned int count);
135
136 void set_console_register(reg_addr_t addr);
137
138 void set_direct_LPM_enabled(bool enabled);
139
140 //Disable copy semantics
141 Core(const Core&) = delete;
142 Core& operator=(const Core&) = delete;
143
144protected:
145
151 uint8_t m_regs[32];
153 std::vector<IORegister*> m_ioregs;
155 uint8_t* m_sram;
167
168 //CPU access to I/O registers in I/O address space
169 uint8_t cpu_read_ioreg(reg_addr_t addr);
170 void cpu_write_ioreg(reg_addr_t addr, uint8_t value);
171
172 //CPU access to the general 32 registers
173 uint8_t cpu_read_gpreg(uint8_t reg);
174 void cpu_write_gpreg(uint8_t reg, uint8_t value);
175
189 virtual uint8_t cpu_read_data(mem_addr_t data_addr) = 0;
190
203 virtual void cpu_write_data(mem_addr_t data_addr, uint8_t value) = 0;
204
205 int16_t cpu_read_flash(flash_addr_t pgm_addr);
206
207 inline bool use_extended_addressing() const
208 {
210 }
211
212 //===== Debugging management (used by DeviceDebugProbe) =====
213
214 //Debug probe access to memory data in blocks
225 virtual void dbg_read_data(mem_addr_t start, uint8_t* buf, mem_addr_t len) = 0;
226
237 virtual void dbg_write_data(mem_addr_t start, const uint8_t* buf, mem_addr_t len) = 0;
238
239 //Breakpoint management
240 void dbg_insert_breakpoint(breakpoint_t& bp);
241 void dbg_remove_breakpoint(breakpoint_t& bp);
242
243 static bool data_space_map(mem_addr_t addr, mem_addr_t len,
244 mem_addr_t blockstart, mem_addr_t blockend,
245 mem_addr_t* bufofs, mem_addr_t* blockofs,
246 mem_addr_t* result_len);
247
248private:
249
250 //Status register variable
251 uint8_t m_sreg[8];
252 //Direct pointer to the interrupt controller. We don't use the ctlreq framework for performance
253 InterruptController* m_intrctl;
254
255 reg_addr_t m_reg_console;
256 std::string m_console_buffer;
257
258 //Boolean to indicate if the direct mode for the LPM instruction is enabled
259 bool m_direct_LPM;
260
261 //Helpers for managing the SREG register
262 uint8_t read_sreg();
263 void write_sreg(uint8_t value);
264
265 //Helpers for managing the stack
266 uint16_t read_sp();
267 void write_sp(uint16_t sp);
268 void cpu_push_flash_addr(flash_addr_t addr);
269 flash_addr_t cpu_pop_flash_addr();
270
271 //Main instruction interpreter
272 cycle_count_t run_instruction();
273
274 //Called by a RETI instruction
275 void exec_reti();
276
277};
278
279inline const CoreConfiguration& Core::config() const
280{
281 return m_config;
282}
283
285{
286 m_reg_console = addr;
287}
288
289
290inline void Core::set_direct_LPM_enabled(bool enabled)
291{
292 m_direct_LPM = enabled;
293}
294
295
297
298#endif //__YASIMAVR_CORE_H__
AVR core generic model.
Definition sim_core.h:96
uint8_t * m_sram
Pointer to the array representing the device RAM memory.
Definition sim_core.h:155
Core & operator=(const Core &)=delete
virtual uint8_t cpu_read_data(mem_addr_t data_addr)=0
virtual void cpu_write_data(mem_addr_t data_addr, uint8_t value)=0
void set_console_register(reg_addr_t addr)
Definition sim_core.h:284
NonVolatileMemory m_fuses
Non-volatile memory model for the fuse bits.
Definition sim_core.h:159
const CoreConfiguration & m_config
Reference to the configuration structure, set at construction.
Definition sim_core.h:147
NVM
Definition sim_core.h:109
@ NVM_Fuses
Definition sim_core.h:111
@ NVM_CommonCount
Definition sim_core.h:112
@ NVM_Flash
Definition sim_core.h:110
virtual void dbg_read_data(mem_addr_t start, uint8_t *buf, mem_addr_t len)=0
bool use_extended_addressing() const
Definition sim_core.h:207
Device * m_device
Pointer to the device, set by init()
Definition sim_core.h:149
std::vector< IORegister * > m_ioregs
Array of the I/O registers.
Definition sim_core.h:153
const CoreConfiguration & config() const
Definition sim_core.h:279
DeviceDebugProbe * m_debug_probe
Pointer to the generic debug probe.
Definition sim_core.h:165
virtual void dbg_write_data(mem_addr_t start, const uint8_t *buf, mem_addr_t len)=0
unsigned int m_int_inhib_counter
Counter to inhibit interrupts for a given number of instructions.
Definition sim_core.h:163
Core(const Core &)=delete
void set_direct_LPM_enabled(bool enabled)
Definition sim_core.h:290
MemorySectionManager * m_section_manager
Definition sim_core.h:166
flash_addr_t m_pc
Program Counter register, expressed in bytes (unlike the actual device PC)
Definition sim_core.h:161
NonVolatileMemory m_flash
Non-volatile memory model for the flash.
Definition sim_core.h:157
Definition sim_debug.h:42
Basic AVR device model.
Definition sim_device.h:61
Definition sim_firmware.h:53
Definition sim_ioreg.h:116
Generic interrupt controller.
Definition sim_interrupt.h:75
Memory section management.
Definition sim_memory.h:124
Non-volatile memory model.
Definition sim_memory.h:42
Representation of a I/O register address, with validity state.
Definition sim_types.h:60
@ SREG_Z
Definition sim_core.h:52
@ SREG_V
Definition sim_core.h:54
@ SREG_H
Definition sim_core.h:56
@ SREG_C
Definition sim_core.h:51
@ SREG_T
Definition sim_core.h:57
@ SREG_N
Definition sim_core.h:53
@ SREG_S
Definition sim_core.h:55
@ SREG_I
Definition sim_core.h:58
@ R_ZL
Definition sim_core.h:63
@ R_YL
Definition sim_core.h:63
@ R_YH
Definition sim_core.h:63
@ R_XL
Definition sim_core.h:63
@ R_Z
Definition sim_core.h:66
@ R_Y
Definition sim_core.h:65
@ R_SPH
Definition sim_core.h:68
@ R_SREG
Definition sim_core.h:70
@ R_SPL
Definition sim_core.h:68
@ R_X
Definition sim_core.h:64
@ R_XH
Definition sim_core.h:63
@ R_ZH
Definition sim_core.h:63
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
unsigned long flash_addr_t
Definition sim_types.h:42
unsigned long mem_addr_t
Definition sim_types.h:41
YASIMAVR_BEGIN_NAMESPACE typedef long long cycle_count_t
Definition sim_types.h:40
Definition sim_config.h:37
uint32_t attributes
Definition sim_config.h:46
@ ExtendedAddressing
Definition sim_config.h:41
Bit mask structure for bitwise operations on 8-bits registers.
Definition sim_types.h:86
Breakpoint structure.
Definition sim_core.h:78
flash_addr_t addr
Address in code space of the breakpoint (where the BREAK instruction is inserted)
Definition sim_core.h:80
uint8_t instr_len
Length in bytes of the instruction replaced.
Definition sim_core.h:84
uint8_t instr[4]
Instruction replaced by the BREAK (up to 32-bits long)
Definition sim_core.h:82