The Sparta Modeling Framework
Loading...
Searching...
No Matches
SharedData.hpp
Go to the documentation of this file.
1// <SharedData.h> -*- C++ -*-
2
3
10#pragma once
11
12#include <array>
15
16namespace sparta
17{
36 template<class DataT, bool manual_update = false>
38 {
39 // Convenient typedef for internal data type
40 typedef std::array<utils::ValidValue<DataT>, 2> PSNSData;
41
42 // Next state index
43 uint32_t NState_() const {
44 return (current_state_ + 1) & 0x1;
45 }
46
47 // Present state index
48 uint32_t PState_() const {
49 return current_state_;
50 }
51
52 void resetHandler_()
53 {
54 ev_update_.resetHandler(CREATE_SPARTA_HANDLER(SharedData, update_));
55 }
56
57 public:
64 template<typename U = DataT>
65 SharedData(const std::string & name,
66 const Clock * clk,
67 U && init_val = U()) :
68 ev_update_(clk, CREATE_SPARTA_HANDLER(SharedData, update_))
69 {
70 writePS(std::forward<U>(init_val));
71 }
72
73 SharedData(const SharedData& rhs) :
74 ev_update_(rhs.ev_update_),
75 current_state_(rhs.current_state_),
76 data_(rhs.data_)
77 {
78 resetHandler_();
79 }
80
81 SharedData(SharedData&& rhs) :
82 ev_update_(std::move(rhs.ev_update_)),
83 current_state_(std::move(rhs.current_state_)),
84 data_(std::move(rhs.data_))
85 {
86 resetHandler_();
87 }
88
89 SharedData& operator=(const SharedData& rhs)
90 {
91 ev_update_ = rhs.ev_update_;
92 resetHandler_();
93
94 current_state_ = rhs.current_state_;
95 data_ = rhs.data_;
96
97 return *this;
98 }
99
100 SharedData& operator=(SharedData&& rhs)
101 {
102 ev_update_ = std::move(rhs.ev_update_);
103 resetHandler_();
104
105 current_state_ = std::move(rhs.current_state_);
106 data_ = std::move(rhs.data_);
107
108 return *this;
109 }
110
115 void writePS(const DataT & dat) {
116 writePSImpl_(dat);
117 }
118
123 void writePS(DataT && dat) {
124 writePSImpl_(std::move(dat));
125 }
126
131 bool isValid() const {
132 return data_[PState_()].isValid();
133 }
134
140 const DataT & read() const {
142 return data_[PState_()];
143 }
144
150 DataT & access() {
152 return data_[PState_()];
153 }
154
159 void write(const DataT & dat) {
160 writeImpl_(dat);
161 }
162
167 void write(DataT && dat) {
168 writeImpl_(std::move(dat));
169 }
170
175 bool isValidNS() const {
176 return data_[NState_()].isValid();
177 }
178
185 const DataT & readNS() const {
187 return data_[NState_()];
188 }
189
196 DataT & accessNS() {
198 return data_[NState_()];
199 }
200
202 void clear() {
203 data_[PState_()].clearValid();
204 data_[NState_()].clearValid();
205 }
206
208 void clearNS() {
209 data_[NState_()].clearValid();
210 }
211
213 void clearPS() {
214 data_[PState_()].clearValid();
215 }
216
220 void update() {
221 static_assert(manual_update == true,
222 "Cannot call update on a SharedData object if manual_update == false");
223 update_();
224 }
225
226 private:
227 template<typename U>
228 void writePSImpl_(U && dat) {
229 data_[PState_()] = std::forward<U>(dat);
230 }
231
232 template<typename U>
233 void writeImpl_(U && dat) {
234 data_[NState_()] = std::forward<U>(dat);
235 if constexpr (!manual_update) {
236 ev_update_.schedule(1);
237 }
238 }
239
240 void update_()
241 {
242 current_state_ = NState_();
243 clearNS();
244 }
245
246 // Global Event
247 GlobalEvent<SchedulingPhase::Update> ev_update_;
248
249 // Current state
250 uint32_t current_state_ = 0;
251
252 // The PS/NS data
253 PSNSData data_;
254 };
255}
256
File that defines the GlobalEvent class.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define CREATE_SPARTA_HANDLER(clname, meth)
File that defines a ValidValue.
A representation of simulated time.
Definition Clock.hpp:51
Class that allows the writing of data this cycle, but not visable until next cycle.
void clearPS()
Clear Present State valid.
DataT & accessNS()
Get a non-constant reference to the data that will be visible next cycle.
bool isValidNS() const
Is there data for the next cycle?
bool isValid() const
Is there data in the current view.
void update()
Update the SharedData class – move next cycle data to current view. Can only be called on a manually ...
void write(const DataT &dat)
Write data for the next cycle view.
SharedData(const std::string &name, const Clock *clk, U &&init_val=U())
Construct a SharedData item.
void clearNS()
Clear Next State valid.
void write(DataT &&dat)
Write data for the next cycle view.
void writePS(DataT &&dat)
Write data to the current view.
void clear()
Clear both Present State and Next State valids.
const DataT & readNS() const
Get a constant reference to the data that will be visible next cycle.
const DataT & read() const
Get a constant reference to the data visible this cycle.
DataT & access()
Get a non-constant reference to the data visible this cycle.
void writePS(const DataT &dat)
Write data to the current view.
Macros for handling exponential backoff.