SILICON
Loading...
Searching...
No Matches
fstWrapper.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 <cassert>
21#include <cstdint>
22#include <memory>
23#include <string>
24#include <string_view>
25#include <utility>
26#include <vector>
27
28#include <fstapi.h>
29
30namespace SILICON::waveform::fst {
31
32// --- Shared types ----------------------------------------------------------------------
33
47 void operator()(fstReaderContext* ctx) const
48 {
49 if (ctx)
50 fstReaderClose(ctx);
51 }
52};
53
60 void operator()(fstWriterContext* ctx) const
61 {
62 if (ctx)
63 fstWriterClose(ctx);
64 }
65};
66
67// =======================================================================================
68// Reader
69// =======================================================================================
70
78class Reader final {
79public:
87 struct EnumTable {
88 std::string name;
89
100 std::vector<std::pair<std::string, std::string>> mapping;
101 };
102
106 struct FstVarNode {
107 std::string name;
108
114 fstHandle handle;
115
119 uint32_t length;
120
124 fstVarDir direction;
125
129 fstVarType type;
130 };
131
139 std::string name;
140 std::string component;
141
145 fstScopeType type;
146
150 std::vector<FstVarNode> vars;
151
155 std::vector<FstScopeNode> children;
156 };
157
166 explicit Reader(std::string_view fileName);
167
168 ~Reader() = default;
169
174 Reader(const Reader&) = delete;
175 Reader& operator=(const Reader&) = delete;
176
180 Reader(Reader&&) noexcept = default;
181 Reader& operator=(Reader&&) noexcept = default;
182
183 [[nodiscard]] const std::string& getFileName() const { return fn; }
184
185 // --- Metadata Accessors --------------------------------------------------------------
186
187 [[nodiscard]] uint64_t getStartTime() const
188 {
189 assert(context);
190 return fstReaderGetStartTime(context.get());
191 }
192
193 [[nodiscard]] uint64_t getEndTime() const
194 {
195 assert(context);
196 return fstReaderGetEndTime(context.get());
197 }
198
199 [[nodiscard]] uint64_t getVarCount() const
200 {
201 assert(context);
202 return fstReaderGetVarCount(context.get());
203 }
204
205 [[nodiscard]] uint64_t getScopeCount() const
206 {
207 assert(context);
208 return fstReaderGetScopeCount(context.get());
209 }
210
218 [[nodiscard]] int8_t getTimescale() const
219 {
220 assert(context);
221 return fstReaderGetTimescale(context.get());
222 }
223
224 [[nodiscard]] std::string getVersion() const;
225 [[nodiscard]] std::string getDate() const;
226
227 // --- Hierarchy Traversal -------------------------------------------------------------
228
246 template <typename Func> void iterateHierarchy(Func&& callback)
247 {
248 assert(context);
249
250 // Rewind hierarchy stream before iteration. libfst internally maintains iteration
251 // state.
252 fstReaderIterateHierRewind(context.get());
253
254 struct fstHier* hier;
255
256 /* The hierarchy is emitted as a flat event stream:
257 * SCOPE
258 * VAR
259 * VAR
260 * SCOPE
261 * VAR
262 * UPSCOPE
263 * UPSCOPE */
264
265 while ((hier = fstReaderIterateHier(context.get())) != nullptr) {
266 callback(hier);
267 }
268 }
269
276 [[nodiscard]] FstScopeNode buildHierarchyTree();
277
278 // --- Trace Data Reading --------------------------------------------------------------
279
283 void setLimitTimeRange(uint64_t start_time, uint64_t end_time)
284 {
285 assert(context);
286 fstReaderSetLimitTimeRange(context.get(), start_time, end_time);
287 }
288
293 {
294 assert(context);
295 fstReaderSetUnlimitedTimeRange(context.get());
296 }
297
302 {
303 assert(context);
304 fstReaderSetFacProcessMaskAll(context.get());
305 }
306
311 {
312 assert(context);
313 fstReaderClrFacProcessMaskAll(context.get());
314 }
315
319 void setFacProcessMask(fstHandle facidx)
320 {
321 assert(context);
322 fstReaderSetFacProcessMask(context.get(), facidx);
323 }
324
328 void clearFacProcessMask(fstHandle facidx)
329 {
330 assert(context);
331 fstReaderClrFacProcessMask(context.get(), facidx);
332 }
333
348 template <typename Func> void readIterateBlocks(Func&& callback)
349 {
350 assert(context);
351
352 // Trampoline for fixed-size values. libfst invokes this for regular scalar/vector
353 // changes.
354 auto tramp = [](void* user_data, uint64_t time, fstHandle facidx,
355 const unsigned char* value) {
356 (*static_cast<std::remove_reference_t<Func>*>(user_data))(
357 time, facidx, std::string_view(reinterpret_cast<const char*>(value)), 0);
358 };
359
360 // Trampoline for variable-length values. Used for dynamically sized payloads.
361 auto trampVarlen = [](void* user_data, uint64_t time, fstHandle facidx,
362 const unsigned char* value, uint32_t len) {
363 (*static_cast<std::remove_reference_t<Func>*>(user_data))(
364 time, facidx, std::string_view(reinterpret_cast<const char*>(value), len), len);
365 };
366
367 // Forward callback state through libfst.
368 fstReaderIterBlocks2(context.get(), tramp, trampVarlen, &callback, nullptr);
369 }
370
377 static EnumTable parseEnumTable(std::string_view enumString);
378
379private:
383 std::string fn;
384
388 std::unique_ptr<fstReaderContext, ReaderDeleter> context;
389};
390
391// =======================================================================================
392// DataWriter
393// =======================================================================================
394
406class DataWriter final {
407public:
408 DataWriter(const DataWriter&) = delete;
409 DataWriter& operator=(const DataWriter&) = delete;
410
411 DataWriter(DataWriter&&) noexcept = default;
412 DataWriter& operator=(DataWriter&&) noexcept = default;
413
414 ~DataWriter() = default;
415
420 inline void emitTimeChange(const uint64_t time)
421 {
422 assert(context);
423 fstWriterEmitTimeChange(context.get(), time);
424 }
425
426 // --- String/Binary Encoded Value Emission --------------------------------------------
427
428 inline void emitValueChange(fstHandle handle, const std::string& val)
429 {
430 assert(context);
431 fstWriterEmitValueChange(context.get(), handle, val.c_str());
432 }
433
439 inline void emitVariableLengthValueChange(fstHandle handle, std::string_view val)
440 {
441 assert(context);
442
443 fstWriterEmitVariableLengthValueChange(context.get(), handle, val.data(),
444 val.length());
445 }
446
447 // --- Native Integer-Oriented Emission APIs -------------------------------------------
448
455 inline void emitValue32(fstHandle handle, uint32_t bits, uint32_t val)
456 {
457 assert(context);
458 fstWriterEmitValueChange32(context.get(), handle, bits, val);
459 }
460
461 inline void emitValue64(fstHandle handle, uint32_t bits, uint64_t val)
462 {
463 assert(context);
464 fstWriterEmitValueChange64(context.get(), handle, bits, val);
465 }
466
467 inline void emitValueVec32(fstHandle handle, uint32_t bits, const uint32_t* val)
468 {
469 assert(context);
470 fstWriterEmitValueChangeVec32(context.get(), handle, bits, val);
471 }
472
473 inline void emitValueVec64(fstHandle handle, uint32_t bits, const uint64_t* val)
474 {
475 assert(context);
476 fstWriterEmitValueChangeVec64(context.get(), handle, bits, val);
477 }
478
479 inline void flush()
480 {
481 assert(context);
482 fstWriterFlushContext(context.get());
483 }
484
485private:
491 friend class HierarchyBuilder;
492
493 explicit DataWriter(std::unique_ptr<fstWriterContext, WriterDeleter> ctx)
494 : context(std::move(ctx))
495 {
496 }
497
498 std::unique_ptr<fstWriterContext, WriterDeleter> context;
499};
500
501// =======================================================================================
502// HierarchyBuilder
503// =======================================================================================
504
520class HierarchyBuilder final {
521public:
522 explicit HierarchyBuilder(std::string_view fileName, int use_compressed_hier = 1);
523
524 ~HierarchyBuilder() = default;
525
528
529 HierarchyBuilder(HierarchyBuilder&&) noexcept = default;
530 HierarchyBuilder& operator=(HierarchyBuilder&&) noexcept = default;
531
532 // --- Global Writer Configuration -----------------------------------------------------
533
534 void setTimeScale(int exponent)
535 {
536 assert(context);
537 fstWriterSetTimescale(context.get(), exponent);
538 }
539
540 void setTimeZero(int64_t tim)
541 {
542 assert(context);
543 fstWriterSetTimezero(context.get(), tim);
544 }
545
546 void setPackType(fstWriterPackType typ)
547 {
548 assert(context);
549 fstWriterSetPackType(context.get(), typ);
550 }
551
552 void setFileType(fstFileType typ)
553 {
554 assert(context);
555 fstWriterSetFileType(context.get(), typ);
556 }
557
558 void setDate(std::string_view date)
559 {
560 assert(context);
561 const std::string dateStorage(date);
562 fstWriterSetDate(context.get(), dateStorage.c_str());
563 }
564
565 void setVersion(std::string_view version)
566 {
567 assert(context);
568 const std::string versionStorage(version);
569 fstWriterSetVersion(context.get(), versionStorage.c_str());
570 }
571
572 // --- Hierarchy Construction ----------------------------------------------------------
573
577 void setScope(fstScopeType scope_type, std::string_view scope_name,
578 std::string_view scope_comp = "");
579
584 void upScope()
585 {
586 assert(context);
587 fstWriterSetUpscope(context.get());
588 }
589
596 fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len,
597 std::string_view name, fstHandle aliasHandle = 0);
598
599 // --- Enum Support --------------------------------------------------------------------
600
609 fstEnumHandle createEnumTable(
610 std::string_view name, unsigned int min_valbits,
611 const std::vector<std::pair<const std::string, const std::string>>& values);
612
616 void emitEnumTableRef(fstEnumHandle handle)
617 {
618 assert(context);
619 fstWriterEmitEnumTableRef(context.get(), handle);
620 }
621
630 DataWriter finish() &&;
631
632private:
633 std::string fn;
634
638 std::unique_ptr<fstWriterContext, WriterDeleter> context;
639};
640
641} // namespace SILICON::waveform::fst
Stateful writer for waveform value emission.
DataWriter(const DataWriter &)=delete
DataWriter(std::unique_ptr< fstWriterContext, WriterDeleter > ctx)
void emitTimeChange(const uint64_t time)
Advances simulation time.
void emitValueVec64(fstHandle handle, uint32_t bits, const uint64_t *val)
void emitValue64(fstHandle handle, uint32_t bits, uint64_t val)
std::unique_ptr< fstWriterContext, WriterDeleter > context
void emitValue32(fstHandle handle, uint32_t bits, uint32_t val)
void emitVariableLengthValueChange(fstHandle handle, std::string_view val)
void emitValueVec32(fstHandle handle, uint32_t bits, const uint32_t *val)
DataWriter & operator=(const DataWriter &)=delete
DataWriter(DataWriter &&) noexcept=default
void emitValueChange(fstHandle handle, const std::string &val)
void upScope()
Pops one hierarchy level. Must match a preceding setScope().
HierarchyBuilder(HierarchyBuilder &&) noexcept=default
HierarchyBuilder(std::string_view fileName, int use_compressed_hier=1)
fstEnumHandle createEnumTable(std::string_view name, unsigned int min_valbits, const std::vector< std::pair< const std::string, const std::string > > &values)
Creates a symbolic enum mapping table.
void setDate(std::string_view date)
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.
std::unique_ptr< fstWriterContext, WriterDeleter > context
fstHandle createVar(fstVarType var_type, fstVarDir var_dir, uint32_t len, std::string_view name, fstHandle aliasHandle=0)
Creates a waveform variable.
DataWriter finish() &&
Final typestate transition.
void emitEnumTableRef(fstEnumHandle handle)
Associates a previously created enum table with subsequent variables.
void setPackType(fstWriterPackType typ)
HierarchyBuilder & operator=(const HierarchyBuilder &)=delete
HierarchyBuilder(const HierarchyBuilder &)=delete
void readIterateBlocks(Func &&callback)
Iterates waveform blocks and forwards value changes to a callback.
const std::string & getFileName() const
void setFacProcessMaskAll()
Enables processing for all signals.
Reader(Reader &&) noexcept=default
std::unique_ptr< fstReaderContext, ReaderDeleter > context
void clearFacProcessMask(fstHandle facidx)
Disables a specific signal for processing.
void setUnlimitedTimeRange()
Removes any previously configured time restrictions.
std::string getVersion() const
void iterateHierarchy(Func &&callback)
Generic hierarchy iterator.
FstScopeNode buildHierarchyTree()
Reconstructs hierarchy into a recursive tree structure.
std::string getDate() const
static EnumTable parseEnumTable(std::string_view enumString)
Parses a textual enum encoding into structured data.
void setFacProcessMask(fstHandle facidx)
Enables a specific signal for processing.
void setLimitTimeRange(uint64_t start_time, uint64_t end_time)
Restricts waveform iteration to a bounded time window.
void clearFacProcessMaskAll()
Disables processing for all signals.
Reader(const Reader &)=delete
Reader & operator=(const Reader &)=delete
Reader(std::string_view fileName)
Opens an FST file for reading.
int8_t getTimescale() const
Returns the exponent used by the timescale.
Custom deleter used with std::unique_ptr for fstReaderContext.
void operator()(fstReaderContext *ctx) const
Decoded enum table extracted from libfst.
std::vector< std::pair< std::string, std::string > > mapping
Recursive hierarchy node representing scopes/modules/packages/etc.
Represents a single waveform variable/signal.
Custom deleter used with std::unique_ptr for fstWriterContext.
void operator()(fstWriterContext *ctx) const