SILICON
Loading...
Searching...
No Matches
fstTraceWriter.hpp
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#pragma once
19
20#include <cstddef>
21#include <cstdint>
22#include <optional>
23#include <span>
24#include <string>
25#include <string_view>
26#include <unordered_map>
27#include <vector>
28
29#include <core/fstWrapper.hpp>
31
32namespace SILICON::waveform::fst {
33
34using namespace SILICON::core;
35
44class TraceWriter final {
45public:
49 struct TraceSignal {
53 std::string name;
54
58 std::size_t width = 1;
59 };
60
64 struct Options {
68 std::string topScopeName;
69
73 int timescale = -9;
74
78 std::string version = std::string("Generated by SILICON ") + SILICON_VERSION;
79 };
80
81 TraceWriter(std::string_view fileName, const std::vector<TraceSignal>& traceSignals);
82
94 TraceWriter(std::string_view fileName, const std::vector<TraceSignal>& traceSignals,
95 Options options);
96
97 TraceWriter(const TraceWriter&) = delete;
99
100 TraceWriter(TraceWriter&&) noexcept = default;
101 TraceWriter& operator=(TraceWriter&&) noexcept = default;
102
113 void emitSnapshot(uint64_t time, std::span<const std::string> values);
114
118 void flush();
119
123 [[nodiscard]] std::optional<fstHandle> handleForSignal(std::string_view name) const;
124
128 [[nodiscard]] std::size_t signalCount() const { return registeredSignals.size(); }
129
130private:
132 std::string name;
133 std::size_t valueIndex;
134 std::size_t width;
135 fstHandle handle;
136 };
137
138 [[nodiscard]] static std::string normalizeValue(std::string_view value,
139 std::size_t width);
140
141 std::vector<RegisteredSignal> registeredSignals;
142 std::unordered_map<std::string, fstHandle, TransparentStringHash,
146};
147
148} // namespace SILICON::waveform::fst
Stateful writer for waveform value emission.
std::size_t signalCount() const
Number of non-zero-width signals registered in the FST hierarchy.
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.
TraceWriter(TraceWriter &&) noexcept=default
std::vector< RegisteredSignal > registeredSignals
std::unordered_map< std::string, fstHandle, TransparentStringHash, TransparentStringEqual > handlesBySignalName
TraceWriter & operator=(const TraceWriter &)=delete
TraceWriter(const TraceWriter &)=delete
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)
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.
FST file metadata and hierarchy options.
Static description of a signal to register in the FST hierarchy.