SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
fstTraceWriter.cpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2026. Giulio Cocconi
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include "
fstTraceWriter.hpp
"
19
20
#include <cstdint>
21
#include <limits>
22
#include <stdexcept>
23
#include <utility>
24
25
#include <
utils/ranges_wrapper.hpp
>
26
27
namespace
SILICON::waveform::fst
{
28
29
using namespace
SILICON::core
;
30
31
namespace
{
32
33
uint32_t checkedFstWidth(
const
std::size_t width)
34
{
35
if
(width > std::numeric_limits<uint32_t>::max())
36
throw
std::invalid_argument(
"FST signal width exceeds libfst limit"
);
37
38
return
static_cast<
uint32_t
>
(width);
39
}
40
41
}
// namespace
42
43
TraceWriter::TraceWriter
(std::string_view fileName,
44
const
std::vector<TraceSignal>& signals)
45
:
TraceWriter
(fileName, signals,
Options
{})
46
{
47
}
48
49
TraceWriter::TraceWriter
(std::string_view fileName,
50
const
std::vector<TraceSignal>& traceSignals,
Options
options)
51
:
writer
([&] {
52
HierarchyBuilder
builder(fileName);
53
builder.
setTimeScale
(options.
timescale
);
54
builder.
setVersion
(options.
version
);
55
builder.
setPackType
(FST_WR_PT_LZ4);
56
builder.
setScope
(FST_ST_VCD_MODULE,
57
options.
topScopeName
.empty() ?
"SILICON"
: options.
topScopeName
);
58
59
registeredSignals
.reserve(traceSignals.size());
60
handlesBySignalName
.reserve(traceSignals.size());
61
62
for
(
const
auto
& [index, signal] : traceSignals | SILICON::views::enumerate) {
63
if
(signal.width == 0)
64
continue
;
65
66
const
auto
handle = builder.
createVar
(FST_VT_VCD_WIRE, FST_VD_IMPLICIT,
67
checkedFstWidth(signal.width), signal.name);
68
registeredSignals
.push_back(
RegisteredSignal
{
69
signal.name,
static_cast<
std::size_t
>
(index), signal.width, handle});
70
handlesBySignalName
.try_emplace(signal.name, handle);
71
}
72
73
builder.
upScope
();
74
return
std::move(builder).finish();
75
}())
76
{
77
}
78
79
void
TraceWriter::emitSnapshot
(
const
uint64_t time,
80
const
std::span<const std::string> values)
81
{
82
writer
.emitTimeChange(time);
83
84
for
(
const
auto
& signal :
registeredSignals
) {
85
const
std::string_view value = signal.valueIndex < values.size()
86
? std::string_view(values[signal.valueIndex])
87
: std::string_view{};
88
writer
.emitValueChange(signal.handle,
normalizeValue
(value, signal.width));
89
}
90
}
91
92
void
TraceWriter::flush
()
93
{
94
writer
.flush();
95
}
96
97
std::optional<fstHandle>
TraceWriter::handleForSignal
(std::string_view name)
const
98
{
99
const
auto
it =
handlesBySignalName
.find(name);
100
if
(it ==
handlesBySignalName
.end())
101
return
std::nullopt;
102
103
return
it->second;
104
}
105
106
std::string
TraceWriter::normalizeValue
(std::string_view value,
const
std::size_t width)
107
{
108
std::string normalized(value);
109
if
(normalized.empty())
110
normalized =
"x"
;
111
112
for
(
char
& ch : normalized) {
113
if
(ch !=
'0'
&& ch !=
'1'
&& ch !=
'x'
&& ch !=
'z'
)
114
ch =
'x'
;
115
}
116
117
if
(normalized.size() < width)
118
normalized.insert(normalized.begin(), width - normalized.size(),
'x'
);
119
else
if
(normalized.size() > width)
120
normalized.erase(0, normalized.size() - width);
121
122
return
normalized;
123
}
124
125
}
// namespace SILICON::waveform::fst
SILICON::waveform::fst::HierarchyBuilder
Typestate phase for static topology construction.
Definition
fstWrapper.hpp:520
SILICON::waveform::fst::HierarchyBuilder::upScope
void upScope()
Pops one hierarchy level. Must match a preceding setScope().
Definition
fstWrapper.hpp:584
SILICON::waveform::fst::HierarchyBuilder::setTimeScale
void setTimeScale(int exponent)
Definition
fstWrapper.hpp:534
SILICON::waveform::fst::HierarchyBuilder::setVersion
void setVersion(std::string_view version)
Definition
fstWrapper.hpp:565
SILICON::waveform::fst::HierarchyBuilder::setScope
void setScope(fstScopeType scope_type, std::string_view scope_name, std::string_view scope_comp="")
Pushes a new scope onto the writer hierarchy stack.
Definition
fstWrapper.cpp:135
SILICON::waveform::fst::HierarchyBuilder::createVar
fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len, std::string_view name, fstHandle aliasHandle=0)
Creates a waveform variable.
Definition
fstWrapper.cpp:144
SILICON::waveform::fst::HierarchyBuilder::setPackType
void setPackType(fstWriterPackType typ)
Definition
fstWrapper.hpp:546
SILICON::waveform::fst::TraceWriter::emitSnapshot
void emitSnapshot(uint64_t time, std::span< const std::string > values)
Emits one timestamped snapshot of already-sampled signal values.
Definition
fstTraceWriter.cpp:79
SILICON::waveform::fst::TraceWriter::TraceWriter
TraceWriter(std::string_view fileName, const std::vector< TraceSignal > &traceSignals)
Definition
fstTraceWriter.cpp:43
SILICON::waveform::fst::TraceWriter::handleForSignal
std::optional< fstHandle > handleForSignal(std::string_view name) const
Returns the libfst handle associated with a registered signal name.
Definition
fstTraceWriter.cpp:97
SILICON::waveform::fst::TraceWriter::registeredSignals
std::vector< RegisteredSignal > registeredSignals
Definition
fstTraceWriter.hpp:141
SILICON::waveform::fst::TraceWriter::handlesBySignalName
std::unordered_map< std::string, fstHandle, TransparentStringHash, TransparentStringEqual > handlesBySignalName
Definition
fstTraceWriter.hpp:144
SILICON::waveform::fst::TraceWriter::writer
DataWriter writer
Definition
fstTraceWriter.hpp:145
SILICON::waveform::fst::TraceWriter::flush
void flush()
Flushes pending FST data so external tools can read recent snapshots.
Definition
fstTraceWriter.cpp:92
SILICON::waveform::fst::TraceWriter::normalizeValue
static std::string normalizeValue(std::string_view value, std::size_t width)
Definition
fstTraceWriter.cpp:106
fstTraceWriter.hpp
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::waveform::fst
Definition
fstTraceWriter.cpp:27
ranges_wrapper.hpp
SILICON::waveform::fst::TraceWriter::Options
FST file metadata and hierarchy options.
Definition
fstTraceWriter.hpp:64
SILICON::waveform::fst::TraceWriter::Options::topScopeName
std::string topScopeName
Definition
fstTraceWriter.hpp:68
SILICON::waveform::fst::TraceWriter::Options::version
std::string version
Definition
fstTraceWriter.hpp:78
SILICON::waveform::fst::TraceWriter::Options::timescale
int timescale
Definition
fstTraceWriter.hpp:73
SILICON::waveform::fst::TraceWriter::RegisteredSignal
Definition
fstTraceWriter.hpp:131
src
core
fstTraceWriter.cpp
Generated by
1.17.0