The Sparta Modeling Framework
Loading...
Searching...
No Matches
DMIBlockingMemoryIF.hpp
1
2#pragma once
3
4#include <string.h>
6
7namespace sparta {
8 namespace memory {
9
51 {
52 public:
68 DMIBlockingMemoryIF(void * raw_pointer,
69 addr_t start_addr,
70 addr_t size) :
71 BlockingMemoryIF("DMI", size, {start_addr, start_addr + size}),
72 start_addr_(start_addr),
73 raw_pointer_(raw_pointer)
74 { }
75
84 void * getRawDataPtr() {
85 sparta_assert(isValid(), "This DMI pointer is invalid " << this);
86 return raw_pointer_;
87 }
88
90 bool isValid() const { return valid_; }
91
93 void clearValid() { valid_ = false; }
94
99 bool tryRead(addr_t addr,
100 addr_t size,
101 uint8_t *buf,
102 const void *in_supplement=nullptr,
103 void *out_supplement=nullptr) override final
104 {
105 if(false == isValid()) { return false; }
106 return BlockingMemoryIF::tryRead(addr, size, buf, in_supplement, out_supplement);
107 }
108
113 bool tryWrite(addr_t addr,
114 addr_t size,
115 const uint8_t *buf,
116 const void *in_supplement=nullptr,
117 void *out_supplement=nullptr) override final
118 {
119 if(false == isValid()) { return false; }
120 return BlockingMemoryIF::tryWrite(addr, size, buf, in_supplement, out_supplement);
121 }
122
123 private:
124
126 DMIBlockingMemoryIF * getDMI(addr_t, addr_t) override final {
127 sparta_assert(false, "You cannot get a DMI interface from a DMI interface!");
128 return nullptr;
129 }
130
131 void *computeHostAddress_(const addr_t addr) const
132 {
133 return (uint8_t *)raw_pointer_ + (addr - start_addr_);
134 }
135
136 // When this method is called, access windowing is alraedy
137 // checked as well as address spanning
138 bool tryRead_(addr_t addr,
139 addr_t size,
140 uint8_t *buf,
141 const void *,
142 void *) noexcept override
143 {
144 ::memcpy(buf, computeHostAddress_(addr), size);
145 return true;
146 }
147
148 // When this method is called, access windowing is alraedy
149 // checked as well as address spanning
150 bool tryWrite_(addr_t addr,
151 addr_t size,
152 const uint8_t *buf,
153 const void *,
154 void *) noexcept override
155 {
156 ::memcpy(computeHostAddress_(addr), buf, size);
157 return true;
158 }
159
160 // When this method is called, access windowing is alraedy
161 // checked as well as address spanning
162 bool tryPeek_(addr_t addr, addr_t size, uint8_t *buf) const noexcept override
163 {
164 ::memcpy(buf, computeHostAddress_(addr), size);
165 return true;
166 }
167
168 // When this method is called, access windowing is alraedy
169 // checked as well as address spanning
170 bool tryPoke_(addr_t addr, addr_t size, const uint8_t *buf) noexcept override
171 {
172 ::memcpy(computeHostAddress_(addr), buf, size);
173 return true;
174 }
175
176 addr_t start_addr_;
177 void * raw_pointer_ = nullptr;
178 bool valid_ = true;
179 };
180
181 } // namespace memory
182} // namespace sparta
File that contains BlockingMemoryIF.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Pure-virtual memory interface which represents a simple, immediately accessible (blocking) address-sp...
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 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.
Class that provides a BlockingMemoryIF over a raw pointer.
bool tryWrite(addr_t addr, addr_t size, const uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr) override final
Override of sparta::BlockingMemoryIF::tryWrite.
bool tryRead(addr_t addr, addr_t size, uint8_t *buf, const void *in_supplement=nullptr, void *out_supplement=nullptr) override final
Override of sparta::BlockingMemoryIF::tryRead.
void clearValid()
Typically called by the creator of the DMI Mem IF.
DMIBlockingMemoryIF(void *raw_pointer, addr_t start_addr, addr_t size)
Wraps a raw pointer and provides BlockingMemoryIF to it.
void * getRawDataPtr()
Get a raw pointer to the memory held (dangerous)
uint64_t addr_t
Type for generic address representation in generic interfaces, errors and printouts within SPARTA.
Macros for handling exponential backoff.