-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRtlSimulator.cpp
79 lines (65 loc) · 2.27 KB
/
RtlSimulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by unknown on 2024/11/5.
//
#include "RtlSimulator.h"
#include "Exceptions.h"
#include <iostream>
void RtlSimulatorEndSymbol::propagate(std::size_t pos, const CircuitData &data) {
inputDataVec[pos] = std::make_pair(data, inputDataVec[pos].second);
outputData = calculateOutput();
}
RtlSimulatorEndSymbol::RtlSimulatorEndSymbol(std::shared_ptr<ModuleIOPort> port) :
CircuitSymbolWire(port->getIdentifier(), port->getSlicing()) {
}
RtlSimulator::RtlSimulator(const RtlModule &module) : rtlModule(module) {
for (auto &port: module.ioPorts) {
if (port->getPortDirection() == PortDirection::Input) {
inputPorts.push_back(port);
} else if (port->getPortDirection() == PortDirection::Output) {
auto endSym = std::make_shared<RtlSimulatorEndSymbol>(port);
endSym->registerInput(port, port->getSlicing());
outputSymbols.push_back(endSym);
}
}
for (auto &sym: module.circuitSymbols) {
if (sym->getIdentifier().starts_with("__hwconst_")) {
constantSymbols.push_back(sym);
}
}
}
const decltype(RtlSimulator::inputPorts) &RtlSimulator::getInputPorts() const {
return inputPorts;
}
void RtlSimulator::poke(std::string idName, const CircuitData &data) {
std::shared_ptr<ModuleIOPort> port = nullptr;
for (auto &inPort: inputPorts) {
if (inPort->getIdentifier() == idName) {
port = inPort;
break;
}
}
if (port == nullptr) {
throw CircuitException("No such an input port");
}
port->propagate(0, data);
for (auto &constSym: constantSymbols) {
constSym->propagate(0, CircuitData{PortSlicingAST{0, 0}});
}
for (auto ®: rtlModule.registers) {
reg->propagateStoredData();
}
}
CircuitData RtlSimulator::peek(std::string idName) {
for (auto &outPort: outputSymbols) {
if (outPort->getIdentifier() == idName) {
return outPort->getOutputData();
}
}
throw CircuitException("No such an output port");
}
void RtlSimulator::printOutcome() {
for (auto &outputSym: outputSymbols) {
auto outputData = outputSym->getOutputData();
std::cout << outputSym->getIdentifier() << " output: " << outputData.toString() << std::endl;
}
}