The Sparta Modeling Framework
Loading...
Searching...
No Matches
BlockingMemoryIF.hpp
Go to the documentation of this file.
1
7#pragma once
8
10#include "sparta/memory/AddressTypes.hpp"
12
13namespace sparta
14{
15 namespace memory
16 {
17 class TranslationIF;
18 class DMIBlockingMemoryIF;
19
60 {
61 public:
62
65
69
70 BlockingMemoryIF() = delete;
71
81 BlockingMemoryIF(const std::string& desc,
82 addr_t block_size,
83 const DebugMemoryIF::AccessWindow& window,
84 TranslationIF* transif=nullptr) :
85 DebugMemoryIF(desc, block_size, window, transif)
86 { }
87
88 virtual ~BlockingMemoryIF() {}
89
92
96
119 virtual bool tryRead(addr_t addr,
120 addr_t size,
121 uint8_t *buf,
122 const void *in_supplement=nullptr,
123 void *out_supplement=nullptr) {
124 if(__builtin_expect(doesAccessSpan(addr, size), 0)){
125 return false;
126 //throw MemoryReadError(addr, size, "addr is in a different block than addr+size");
127 }
128 if(__builtin_expect(isInAccessWindows(addr, size) == false, 0)){
129 return false;
130 }
131
132 return tryRead_(addr, size, buf, in_supplement, out_supplement);
133 }
134
153 void read(addr_t addr,
154 addr_t size,
155 uint8_t *buf,
156 const void *in_supplement=nullptr,
157 void *out_supplement=nullptr) {
158 if(!tryRead(addr, size, buf, in_supplement, out_supplement)){
159 verifyNoBlockSpan(addr, size);
160 verifyInAccessWindows(addr, size);
161 throw MemoryReadError(addr, size, "Unknown reason");
162 }
163 }
164
186 virtual bool tryWrite(addr_t addr,
187 addr_t size,
188 const uint8_t *buf,
189 const void *in_supplement=nullptr,
190 void *out_supplement=nullptr) {
191 if(__builtin_expect(doesAccessSpan(addr, size), 0)){
192 return false;
193 //throw MemoryWriteError(addr, size, "addr is in a different block than addr+size");
194 }
195 if(__builtin_expect(isInAccessWindows(addr, size) == false, 0)){
196 return false;
197 }
198
199 return tryWrite_(addr, size, buf, in_supplement, out_supplement);
200 }
201
221 void write(addr_t addr,
222 addr_t size,
223 const uint8_t *buf,
224 const void *in_supplement=nullptr,
225 void *out_supplement=nullptr) {
226 if(!tryWrite(addr, size, buf, in_supplement, out_supplement)){
227 verifyNoBlockSpan(addr, size);
228 verifyInAccessWindows(addr, size);
229 throw MemoryReadError(addr, size, "Unkonwn reason");
230 }
231 }
232
239 {
240 return nullptr;
241 }
242
246 virtual void invalidateAllDMI() { return; }
247
250
254 std::string stringize(bool pretty=false) const {
255 (void) pretty;
256 std::stringstream ss;
257 ss << "<BlockingMemoryIF size:0x" << std::hex << total_range_ << " bytes>";
258 return ss.str();
259 }
260
261 protected:
262
266
276 virtual bool tryRead_(addr_t addr,
277 addr_t size,
278 uint8_t *buf,
279 const void *in_supplement,
280 void *out_supplement) = 0;
281
290 virtual bool tryWrite_(addr_t addr,
291 addr_t size,
292 const uint8_t *buf,
293 const void *in_supplement,
294 void *out_supplement) = 0;
295
304 virtual bool tryPoke_(addr_t addr,
305 addr_t size,
306 const uint8_t *buf) override {
307 return tryWrite_(addr, size, buf, nullptr, nullptr);
308 }
309
311 //virtual bool isWriteValid_(addr_t addr, addr_t size) const noexcept = 0;
312 //
314 //virtual bool isReadValid_(addr_t addr, addr_t size) const noexcept = 0;
315
318
319
320 }; // class BlockingMemoryIF
321 } // namespace memory
322} // namespace sparta
323
324
326inline std::ostream& operator<<(std::ostream& out, const sparta::memory::BlockingMemoryIF& mi) {
327 out << mi.stringize();
328 return out;
329}
330
332inline std::ostream& operator<<(std::ostream& out, const sparta::memory::BlockingMemoryIF* mi) {
333 if(nullptr == mi){
334 out << "null";
335 }else{
336 out << mi->stringize();
337 }
338 return out;
339}
std::ostream & operator<<(std::ostream &out, const sparta::memory::BlockingMemoryIF &mi)
BlockingMemoryIF stream operator.
File that contains DebugMemoryIF.
File that contains some exception types related to memory interfaces.
Pure-virtual memory interface which represents a simple, immediately accessible (blocking) address-sp...
std::string stringize(bool pretty=false) const
Render description of this BlockingMemoryIF as a string.
virtual bool tryRead_(addr_t addr, addr_t size, uint8_t *buf, const void *in_supplement, void *out_supplement)=0
Implements tryRead.
virtual bool tryRead(addr_t addr, addr_t size, uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr)
Attempt to read memory of size size at address addr.
virtual void invalidateAllDMI()
Invalidate all DMI's.
virtual bool tryWrite_(addr_t addr, addr_t size, const uint8_t *buf, const void *in_supplement, void *out_supplement)=0
Implements tryWrite.
BlockingMemoryIF(const std::string &desc, addr_t block_size, const DebugMemoryIF::AccessWindow &window, TranslationIF *transif=nullptr)
Construct a blocking memory interface.
virtual DMIBlockingMemoryIF * getDMI(addr_t addr, addr_t size)
Get a DMI blocking interface to access the given address/size.
void read(addr_t addr, addr_t size, uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr)
Attempts to read memory.
virtual bool tryWrite(addr_t addr, addr_t size, const uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr)
Attempt to write memory of size size at address addr.
virtual bool tryPoke_(addr_t addr, addr_t size, const uint8_t *buf) override
Override of DebugMemoryIF::tryPoke_ which forwards the call to tryWrite_.
void write(addr_t addr, addr_t size, const uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr)
Attempts to write memory.
Class that provides a BlockingMemoryIF over a raw pointer.
Memory interface which represents a simple, immediately accessible (blocking) address-space with supp...
bool doesAccessSpan(addr_t addr, addr_t size) const noexcept
Determines if the given address spans block boundaries defined for this interface....
bool isInAccessWindows(addr_t addr, addr_t size) const
Determines if the range [addr, addr+size) is within the access windows for this interface.
addr_t total_range_
Range of addresses from highest accessible to lowest.
void verifyNoBlockSpan(addr_t addr, addr_t size) const
Verifies that the given address does not span block boundaries defined for this interface.
void verifyInAccessWindows(addr_t addr, addr_t size) const
Verifies that the range [addr, addr+size) is within the access windows for this interface.
Error while attempting to read some memory object or interface.
Blocking translation interface with 1:1 translation unless subclassed.
uint64_t addr_t
Type for generic address representation in generic interfaces, errors and printouts within SPARTA.
Macros for handling exponential backoff.
Defines an access window within this interface. Accesses through a memory interface are constrained t...