The Sparta Modeling Framework
Loading...
Searching...
No Matches
ExampleInst.hpp
1// <ExampleInst.h> -*- C++ -*-
2
3
4#pragma once
5
6#include "sparta/decode/DecoderBase.hpp"
7#include "sparta/memory/AddressTypes.hpp"
9#include "sparta/pairs/SpartaKeyPairs.hpp"
10#include "sparta/simulation/State.hpp"
12#include "sparta/utils/SpartaSharedPointerAllocator.hpp"
13
14#include <cstdlib>
15#include <ostream>
16#include <map>
17
18namespace core_example
19{
25 // Forward declaration of the Pair Definition class is must as we are friending it.
26 class ExampleInstPairDef;
27
29 public:
30
31 // The modeler needs to alias a type called "SpartaPairDefinitionType" to the Pair Definition class of itself
33
34 enum class Status : std::uint16_t{
35 FETCHED = 0,
36 __FIRST = FETCHED,
37 DECODED,
38 RENAMED,
39 SCHEDULED,
40 COMPLETED,
41 RETIRED,
42 __LAST
43 };
44
45 enum class TargetUnit : std::uint16_t{
46 ALU0,
47 ALU1,
48 FPU,
49 BR,
50 LSU,
51 ROB, // Instructions that go right to retire
52 N_TARGET_UNITS
53 };
54
55 struct StaticInfo {
56 sparta::decode::DecoderBase decode_base;
57 TargetUnit unit;
58 uint32_t execute_time;
59 bool is_store_inst;
60 };
61
63
64 ExampleInst(const sparta::decode::DecoderBase & static_inst,
65 TargetUnit unit,
66 uint32_t execute_time,
67 bool isStore,
68 const sparta::Clock * clk,
69 Status state) :
70 static_inst_(static_inst),
71 unit_(unit),
72 execute_time_(execute_time),
73 isStoreInst_(isStore),
74 status_("inst_status", clk, state),
75 status_state_(state) {}
76
77 ExampleInst(const StaticInfo & info,
78 const sparta::Clock * clk,
79 Status state = Status::FETCHED) :
80 ExampleInst(info.decode_base,
81 info.unit,
82 info.execute_time,
83 info.is_store_inst,
84 clk,
85 state)
86 {}
87
88 const sparta::decode::DecoderBase & getStaticInst() const {
89 return static_inst_;
90 }
91
92 const Status & getStatus() const {
93 return status_state_.getEnumValue();
94 }
95
96 bool getCompletedStatus() const {
97 return getStatus() == core_example::ExampleInst::Status::COMPLETED;
98 }
99
100 void setStatus(Status status) {
101 status_state_.setValue(status);
102 status_.write(status);
103 if(getStatus() == core_example::ExampleInst::Status::COMPLETED) {
104 if(ev_retire_ != 0) {
105 ev_retire_->schedule();
106 }
107 }
108 }
109
110 const TargetUnit& getUnit() const {
111 return unit_;
112 }
113
114 void setLast(bool last, sparta::Scheduleable * rob_retire_event) {
115 ev_retire_ = rob_retire_event;
116 is_last_ = last;
117
118 if(status_.isValidNS() && status_.readNS() == core_example::ExampleInst::Status::COMPLETED) {
119 ev_retire_->schedule();
120 }
121 }
122
123 void setVAdr(uint64_t vaddr) {
124 vaddr_ = vaddr;
125 }
126
127 void setUniqueID(uint64_t uid) {
128 unique_id_ = uid;
129 }
130
131 // This is a function which will be added in the SPARTA_ADDPAIRs API.
132 uint64_t getUniqueID() const {
133 return unique_id_;
134 }
135
136 void setSpeculative(bool spec) {
137 is_speculative_ = spec;
138 }
139
140 const char* getMnemonic() const { return static_inst_.mnemonic; }
141 uint32_t getOpCode() const { return static_inst_.encoding; }
142 uint64_t getVAdr() const { return vaddr_; }
143 uint64_t getRAdr() const { return vaddr_ | 0x3000; } // faked
144 uint64_t getParentId() const { return 0; }
145 uint32_t getExecuteTime() const { return execute_time_; }
146 bool isSpeculative() const { return is_speculative_; }
147 bool isStoreInst() const { return isStoreInst_; }
148
149 private:
150
151 const sparta::decode::DecoderBase static_inst_;
152 TargetUnit unit_;
153 const uint32_t execute_time_ = 0;
154 bool isStoreInst_ = false;
155 sparta::memory::addr_t vaddr_ = 0;
156 bool is_last_ = false;
157 uint64_t unique_id_ = 0; // Supplied by Fetch
158 bool is_speculative_ = false; // Is this instruction soon to be flushed?
159 sparta::Scheduleable * ev_retire_ = nullptr;
160 InstStatus status_;
161 sparta::State<Status> status_state_;
162 };
163
164 extern sparta::SpartaSharedPointerAllocator<ExampleInst> example_inst_allocator;
165
166 inline std::ostream & operator<<(std::ostream & os, const ExampleInst & inst) {
167 os << inst.getMnemonic();
168 return os;
169 }
170
171 typedef sparta::SpartaSharedPointer<ExampleInst> ExampleInstPtr;
172 inline std::ostream & operator<<(std::ostream & os, const ExampleInstPtr & inst) {
173 os << *inst;
174 return os;
175 }
176
177 inline std::ostream & operator<<(std::ostream & os, const ExampleInst::TargetUnit & unit) {
178 switch(unit) {
179 case ExampleInst::TargetUnit::ALU0:
180 os << "ALU0";
181 break;
182 case ExampleInst::TargetUnit::ALU1:
183 os << "ALU1";
184 break;
185 case ExampleInst::TargetUnit::FPU:
186 os << "FPU";
187 break;
188 case ExampleInst::TargetUnit::BR:
189 os << "BR";
190 break;
191 case ExampleInst::TargetUnit::LSU:
192 os << "LSU";
193 break;
194 case ExampleInst::TargetUnit::ROB:
195 os << "ROB";
196 break;
197 case ExampleInst::TargetUnit::N_TARGET_UNITS:
198 throw sparta::SpartaException("N_TARGET_UNITS cannot be a valid enum state.");
199 }
200 return os;
201 }
202
203 inline std::ostream & operator<<(std::ostream & os, const ExampleInst::Status & status) {
204 switch(status) {
205 case ExampleInst::Status::FETCHED:
206 os << "FETCHED";
207 break;
208 case ExampleInst::Status::DECODED:
209 os << "DECODED";
210 break;
211 case ExampleInst::Status::RENAMED:
212 os << "RENAMED";
213 break;
214 case ExampleInst::Status::SCHEDULED:
215 os << "SCHEDULED";
216 break;
217 case ExampleInst::Status::COMPLETED:
218 os << "COMPLETED";
219 break;
220 case ExampleInst::Status::RETIRED:
221 os << "RETIRED";
222 break;
223 case ExampleInst::Status::__LAST:
224 throw sparta::SpartaException("__LAST cannot be a valid enum state.");
225 }
226 return os;
227 }
228
233 // This is the definition of the PairDefinition class of ExampleInst.
234 // This PairDefinition class could be named anything but it needs to
235 // inherit publicly from sparta::PairDefinition templatized on the actual class ExampleInst.
236 class ExampleInstPairDef : public sparta::PairDefinition<ExampleInst>{
237 public:
238
239 // The SPARTA_ADDPAIRs APIs must be called during the construction of the PairDefinition class
240 ExampleInstPairDef() : PairDefinition<ExampleInst>(){
241 SPARTA_INVOKE_PAIRS(ExampleInst);
242 }
243 SPARTA_REGISTER_PAIRS(SPARTA_ADDPAIR("DID", &ExampleInst::getUniqueID),
244 SPARTA_ADDPAIR("uid", &ExampleInst::getUniqueID),
245 SPARTA_ADDPAIR("mnemonic", &ExampleInst::getMnemonic),
246 SPARTA_ADDPAIR("complete", &ExampleInst::getCompletedStatus),
247 SPARTA_ADDPAIR("unit", &ExampleInst::getUnit),
248 SPARTA_ADDPAIR("latency", &ExampleInst::getExecuteTime),
249 SPARTA_ADDPAIR("raddr", &ExampleInst::getRAdr, std::ios::hex),
250 SPARTA_ADDPAIR("vaddr", &ExampleInst::getVAdr, std::ios::hex))
251 };
252}
Defines the SharedData class.
Defines the SpartaSharedPointer class used for garbage collection.
Pair Definition class of the Example instruction that flows through the example/CoreModel.
Example instruction that flows through the example/CoreModel.
A representation of simulated time.
Definition Clock.hpp:51
A class that defines the basic scheduling interface to the Scheduler. Not intended to be used by mode...
void schedule()
Schedule this event with its pre-set delay using the pre-set Clock.
bool isValidNS() const
Is there data for the next cycle?
void write(const DataT &dat)
Write data for the next cycle view.
const DataT & readNS() const
Get a constant reference to the data that will be visible next cycle.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
A memory allocator complementing SpartaSharedPointer that reuses old memory.
Used for garbage collection, will delete the object it points to when all objects are finished using ...
The State class for watching transition between enum states.
Definition State.hpp:123
void setValue(const EnumTValueType &val)
Set a new enum value explicit and fire observers.
Definition State.hpp:518
uint64_t addr_t
Type for generic address representation in generic interfaces, errors and printouts within SPARTA.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo