SILICON
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
26
28
29using namespace SILICON::core;
30
31namespace {
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
43TraceWriter::TraceWriter(std::string_view fileName,
44 const std::vector<TraceSignal>& signals)
45 : TraceWriter(fileName, signals, Options{})
46{
47}
48
49TraceWriter::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);
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
79void 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
93{
94 writer.flush();
95}
96
97std::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
106std::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
Typestate phase for static topology construction.
void upScope()
Pops one hierarchy level. Must match a preceding setScope().
void setVersion(std::string_view version)
void setScope(fstScopeType scope_type, std::string_view scope_name, std::string_view scope_comp="")
Pushes a new scope onto the writer hierarchy stack.
fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len, std::string_view name, fstHandle aliasHandle=0)
Creates a waveform variable.
void setPackType(fstWriterPackType typ)
void emitSnapshot(uint64_t time, std::span< const std::string > values)
Emits one timestamped snapshot of already-sampled signal values.
TraceWriter(std::string_view fileName, const std::vector< TraceSignal > &traceSignals)
std::optional< fstHandle > handleForSignal(std::string_view name) const
Returns the libfst handle associated with a registered signal name.
std::vector< RegisteredSignal > registeredSignals
std::unordered_map< std::string, fstHandle, TransparentStringHash, TransparentStringEqual > handlesBySignalName
void flush()
Flushes pending FST data so external tools can read recent snapshots.
static std::string normalizeValue(std::string_view value, std::size_t width)
FST file metadata and hierarchy options.