sim_memory.h Source File

yasimavr: sim_memory.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_memory.h
Go to the documentation of this file.
1/*
2 * sim_memory.h
3 *
4 * Copyright 2022-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_MEMORY_H__
25#define __YASIMAVR_MEMORY_H__
26
27#include "sim_types.h"
28#include "sim_signal.h"
29
31
32//=======================================================================================
33
43
44public:
45
46 explicit NonVolatileMemory(size_t size);
49
50 size_t size() const;
51
52 bool programmed(size_t pos) const;
53 bytes_view_t programmed(size_t base, size_t len) const;
54
55 unsigned char operator[](size_t pos) const;
56
57 bytes_view_t view() const;
58 bytes_view_t view(size_t base, size_t len) const;
59
60 bool program(const bytes_view_t& mem_block, size_t base = 0);
61
62 void erase();
63 void erase(size_t base, size_t size);
64 void erase(const bytes_view_t& buf, size_t base);
65
66 int read(size_t pos) const;
67 size_t readinto(unsigned char* buf, size_t base, size_t len) const;
68 void write(unsigned char v, size_t pos);
69 void write(const bytes_view_t& buf, size_t base);
70
71 void spm_write(unsigned char v, size_t pos);
72 void spm_write(const bytes_view_t& buf, const bytes_view_t& bufset, size_t base);
73
74 NonVolatileMemory& operator=(const NonVolatileMemory& other);
75
76private:
77
78 size_t m_size;
79 unsigned char* m_memory;
80 unsigned char* m_tag;
81
82};
83
87inline size_t NonVolatileMemory::size() const
88{
89 return m_size;
90}
91
97inline bool NonVolatileMemory::programmed(size_t pos) const
98{
99 return m_tag[pos];
100}
101
107inline uint8_t NonVolatileMemory::operator[](size_t pos) const
108{
109 return m_memory[pos];
110}
111
112
113//=======================================================================================
114
125
126public:
127
130 Access_Read = 0x01,
131 Access_Write = 0x02,
132 };
133
141
142 MemorySectionManager(flash_addr_t page_count, flash_addr_t page_size, unsigned int section_count);
143
144 flash_addr_t page_count() const;
145 flash_addr_t page_size() const;
146 unsigned int section_count() const;
147
148 unsigned int current_section() const;
149
150 //Management of section boundaries
151 void set_section_limits(const std::vector<flash_addr_t>& limits);
152 flash_addr_t section_start(unsigned int section) const;
153 flash_addr_t section_end(unsigned int section) const;
154 flash_addr_t section_size(unsigned int section) const;
155
156 //Conversion page/address to section
157 unsigned int page_to_section(flash_addr_t page) const;
158 unsigned int address_to_section(flash_addr_t addr) const;
159
160 //Access control flags management
161 void set_access_flags(unsigned int src, unsigned int dst, uint8_t flags);
162 void set_access_flags(unsigned int section, uint8_t flags);
163 uint8_t access_flags(unsigned int section_src, unsigned int section_dst) const;
164 uint8_t access_flags(unsigned int section) const;
165 bool can_read(flash_addr_t addr) const;
166 bool can_write(flash_addr_t addr) const;
167 uint8_t address_access_flags(flash_addr_t addr) const;
168
169 //Address fetch management (setting the current section)
170 void set_fetch_allowed(unsigned int section, bool allowed);
171 bool fetch_address(flash_addr_t addr);
172
173 Signal& signal();
174
175private:
176
177 static const uint8_t ACCESS_FLAGS_MASK = 0x3F;
178 static const uint8_t FETCH_ALLOWED = 0x40;
179 static const uint8_t CURRENT_SECTION = 0x80;
180
181 flash_addr_t m_page_count;
182 flash_addr_t m_page_size;
183 unsigned int m_section_count;
184 unsigned int m_current_section;
185 std::vector<flash_addr_t> m_limits;
186 std::vector<uint8_t> m_flags;
187 std::vector<uint8_t> m_pages;
188 Signal m_signal;
189
190 void update_current_section(flash_addr_t src);
191 void invalidate_page_access_map();
192
193};
194
197{
198 return m_page_count;
199}
200
203{
204 return m_page_size;
205}
206
208inline unsigned int MemorySectionManager::section_count() const
209{
210 return m_section_count;
211}
212
214inline unsigned int MemorySectionManager::current_section() const
215{
216 return m_current_section;
217}
218
220inline flash_addr_t MemorySectionManager::section_start(unsigned int index) const
221{
222 return m_limits[index];
223}
224
226inline flash_addr_t MemorySectionManager::section_end(unsigned int index) const
227{
228 if (m_limits[index] < m_page_count)
229 return m_limits[index + 1] - 1;
230 else
231 return m_page_count;
232}
233
235inline flash_addr_t MemorySectionManager::section_size(unsigned int index) const
236{
237 return m_limits[index + 1] - m_limits[index];
238}
239
242{
243 return page_to_section(addr / m_page_size);
244}
245
251inline uint8_t MemorySectionManager::access_flags(unsigned int section_src, unsigned int section_dst) const
252{
253 return m_flags[section_src * m_section_count + section_dst] & ACCESS_FLAGS_MASK;
254}
255
257inline uint8_t MemorySectionManager::access_flags(unsigned int section) const
258{
259 return m_flags[section * (m_section_count + 1)] & ACCESS_FLAGS_MASK;
260}
261
264{
265 return m_pages[addr / m_page_size] & Access_Read;
266}
267
270{
271 return m_pages[addr / m_page_size] & Access_Write;
272}
273
276{
277 return m_pages[addr / m_page_size] & ACCESS_FLAGS_MASK;
278}
279
282{
283 return m_signal;
284}
285
286
288
289#endif //__YASIMAVR_MEMORY_H__
Memory section management.
Definition sim_memory.h:124
unsigned int current_section() const
Return the section containing the current address.
Definition sim_memory.h:214
flash_addr_t page_size() const
Getter for the page size in bytes. (as given to the constructor)
Definition sim_memory.h:202
bool can_write(flash_addr_t addr) const
Return the write access flag for a given address.
Definition sim_memory.h:269
uint8_t address_access_flags(flash_addr_t addr) const
Return the access flags for a given address.
Definition sim_memory.h:275
uint8_t access_flags(unsigned int section_src, unsigned int section_dst) const
Definition sim_memory.h:251
AccessFlag
Generic Read/Write access flags.
Definition sim_memory.h:129
@ Access_Read
Definition sim_memory.h:130
@ Access_Write
Definition sim_memory.h:131
Signal & signal()
Getter for the signal of the section manager.
Definition sim_memory.h:281
bool can_read(flash_addr_t addr) const
Return the read access flag for a given address.
Definition sim_memory.h:263
unsigned int page_to_section(flash_addr_t page) const
Definition sim_memory.cpp:351
flash_addr_t section_end(unsigned int section) const
Return the section end address.
Definition sim_memory.h:226
flash_addr_t section_size(unsigned int section) const
Return the size in bytes of a section.
Definition sim_memory.h:235
unsigned int address_to_section(flash_addr_t addr) const
Return the section index containing the given memory address.
Definition sim_memory.h:241
flash_addr_t section_start(unsigned int section) const
Return the section start address.
Definition sim_memory.h:220
unsigned int section_count() const
Getter for the number of sections.
Definition sim_memory.h:208
SignalId
SignalID raised by the section manager.
Definition sim_memory.h:135
@ Signal_Leave
Raised when the current address leaves a section. data is set to the section index (integer)
Definition sim_memory.h:137
@ Signal_Enter
Raised when the current address enters a section. data is set to the section index (integer)
Definition sim_memory.h:139
flash_addr_t page_count() const
Getter for the page count. (as given to the constructor)
Definition sim_memory.h:196
Non-volatile memory model.
Definition sim_memory.h:42
bool programmed(size_t pos) const
Definition sim_memory.h:97
unsigned char operator[](size_t pos) const
Definition sim_memory.h:107
size_t size() const
Definition sim_memory.h:87
Signalling framework class.
Definition sim_signal.h:97
#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
std::span< const uint8_t > bytes_view_t
Definition sim_types.h:52