forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WaveformTracer.cpp
196 lines (179 loc) · 5.25 KB
/
WaveformTracer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "WaveformTracer.h"
#include "Port.h"
#include "BitManip.h"
#include <ctime>
WaveformTracer::WaveformTracer(const std::string &name) :
out(name.c_str(), std::fstream::out),
portsFinalized(false),
currentTime(0) {}
void WaveformTracerPort::update(ticks_t newTime)
{
if (!prev.isClock())
return;
EdgeIterator it = prev.getEdgeIterator(time);
while (it->time <= newTime) {
uint32_t value = it->type == Edge::RISING ? 1 : 0;
parent->seePinsChange(this, value, it->time);
++it;
}
time = newTime;
parent->schedule(this, it->time);
}
void WaveformTracerPort::seePinsChange(const Signal &value, ticks_t newTime)
{
if (value == prev)
return;
parent->runUntil(newTime);
uint32_t prevValue = prev.getValue(newTime);
uint32_t newValue = value.getValue(newTime);
if (prevValue != newValue)
parent->seePinsChange(this, newValue, newTime);
time = newTime;
prev = value;
if (value.isClock()) {
parent->schedule(this, value.getNextEdge(newTime).time);
}
}
void WaveformTracer::add(const std::string &name, Port *port)
{
assert(!portsFinalized);
modules[name].push_back(ports.size());
ports.push_back(WaveformTracerPort(this, port,
makeIdentifier(ports.size())));
}
std::string WaveformTracer::makeIdentifier(unsigned index)
{
std::string identifier;
unsigned offset = '!';
unsigned base = '~' - '!' + 1;
if (index) {
while (index) {
identifier.push_back(offset + (index % base));
index /= base;
}
} else {
identifier.push_back(offset);
}
return identifier;
}
void WaveformTracer::emitDate()
{
time_t rawTime = std::time(0);
if (rawTime == -1)
return;
struct tm *timeinfo = localtime(&rawTime);
char buf[256];
if (std::strftime(buf, 256, "%B %d, %Y %X", timeinfo) == 0)
return;
out << "$date\n";
out << " " << buf << '\n';
out << "$end\n";
}
void WaveformTracer::emitDeclarations()
{
emitDate();
out << "$version\n";
out << " AXE (An XCore Emulator)\n";
out << "$end\n";
out << "$timescale\n";
out << " 100 ps\n";
out << "$end\n";
}
void WaveformTracer::
dumpPortValue(const std::string &identifier, Port *port, uint32_t value)
{
if (port->getPortWidth() == 1) {
// Dumps of value changes to scalar variables shall not have any white space
// between the value and the identifier code.
out << (value & 1);
} else {
// Dumps of value changes to vectors shall not have any white space between
// the base letter and the value digits, but they shall have one white space
// between the value digits and the identifier code.
// The output format for each value is right-justified. Vector values appear
// in the shortest form possible: redundant bit values which result from
// left-extending values to fill a particular vector size are eliminated.
out << 'b';
if (value) {
for (int i = 31 - countLeadingZeros(value); i >= 0; i--) {
out << ((value & (1 << i)) ? '1' : '0');
}
} else {
out << '0';
}
out << ' ';
}
out << identifier;
out << '\n';
}
void WaveformTracer::dumpInitialValues()
{
out << "$dumpvars\n";
for (std::vector<WaveformTracerPort>::iterator it = ports.begin(),
e = ports.end(); it != e; ++it) {
dumpPortValue(it->getIdentifier(), it->getPort(), 0);
}
out << "$end\n";
}
void WaveformTracer::finalizePorts()
{
assert(!portsFinalized);
portsFinalized = true;
emitDeclarations();
for (ModuleMap::iterator it = modules.begin(), e = modules.end(); it != e;
++it) {
const std::string &moduleName = it->first;
if (!moduleName.empty()) {
out << "$scope\n";
out << " module " << moduleName << '\n';
out << "$end\n";
}
const std::vector<unsigned> &modulePorts = it->second;
for (std::vector<unsigned>::const_iterator it = modulePorts.begin(),
e = modulePorts.end(); it != e; ++it) {
WaveformTracerPort &waveformTracerPort = ports[*it];
Port *port = waveformTracerPort.getPort();
port->setTracer(&waveformTracerPort);
out << "$var\n";
out << " wire";
out << ' ' << std::dec << port->getID().width();
out << ' ' << waveformTracerPort.getIdentifier();
out << ' ' << port->getName();
out << '\n';
out << "$end\n";
}
if (!moduleName.empty()) {
out << "$upscope $end\n";
}
}
out << "$enddefinitions $end\n";
dumpInitialValues();
}
void WaveformTracer::schedule(WaveformTracerPort *port, ticks_t time)
{
queue.push(Event(port, time));
}
void WaveformTracer::runUntil(ticks_t time)
{
if (!queue.empty()) {
while (queue.top().time < time) {
Event event = queue.top();
event.port->update(event.time);
queue.pop();
}
}
}
void WaveformTracer::
seePinsChange(WaveformTracerPort *port, uint32_t value, ticks_t time)
{
ticks_t translatedTime = time * (100 / CYCLES_PER_TICK);
if (translatedTime != currentTime) {
out << '#' << translatedTime << '\n';
currentTime = translatedTime;
}
dumpPortValue(port->getIdentifier(), port->getPort(), value);
}