SILICON
Loading...
Searching...
No Matches
siliconWaveform.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 "tests.hpp"
19
20#include <core/fstWrapper.hpp>
22
23#include <algorithm>
24#include <cstdio>
25#include <map>
26#include <string>
27#include <vector>
28
29using namespace SILICON::core;
30using namespace SILICON::waveform;
31using namespace SILICON::waveform::fst;
32
33namespace {
34
35struct FileCleanup {
36 std::string filename;
37 ~FileCleanup() { std::remove(filename.c_str()); }
38};
39
40} // namespace
41
42TEST(SiliconWaveformTest, AppendsAndReplacesLatestTimestamp)
43{
44 Trace trace;
45 resetTrace(trace, {{"a", 1}, {"bus", 4}}, 1);
46
47 appendSnapshot(trace, 0, {busValueFromBits("0"), busValueFromBits("0011")});
48 appendSnapshot(trace, 0, {busValueFromBits("1"), busValueFromBits("0101")});
49 appendSnapshot(trace, 4, {busValueFromBits("0"), busValueFromBits("1111")});
50
51 ASSERT_EQ(trace.samples.size(), 2);
52 EXPECT_EQ(trace.samples[0].time, 0);
53 EXPECT_EQ(trace.samples[0].values,
54 (std::vector<BusValue>{busValueFromBits("1"), busValueFromBits("0101")}));
55 EXPECT_EQ(trace.samples[1].time, 4);
56 EXPECT_EQ(trace.samples[1].values,
57 (std::vector<BusValue>{busValueFromBits("0"), busValueFromBits("1111")}));
58}
59
60TEST(SiliconWaveformTest, RebuildsAndAppliesEditIntervals)
61{
62 Trace trace;
63 resetTrace(trace, {{"in", 1}, {"bus", 4}, {"out", 1}}, 2);
64
65 rebuildEditableTrace(trace, 10);
66 ASSERT_EQ(trace.samples.size(), 2);
67 EXPECT_EQ(trace.samples[0].values,
68 (std::vector<BusValue>{busValueFromBits("0"), busValueFromBits("0000")}));
69 EXPECT_EQ(trace.samples[1].time, 10);
70
71 applyEditInterval(trace, 10, 1, 2, 5, busValueFromBits("1010"));
72
73 ASSERT_EQ(trace.samples.size(), 4);
74 EXPECT_EQ(trace.samples[0].time, 0);
75 EXPECT_EQ(trace.samples[0].values,
76 (std::vector<BusValue>{busValueFromBits("0"), busValueFromBits("0000")}));
77 EXPECT_EQ(trace.samples[1].time, 2);
78 EXPECT_EQ(trace.samples[1].values,
79 (std::vector<BusValue>{busValueFromBits("0"), busValueFromBits("1010")}));
80 EXPECT_EQ(trace.samples[2].time, 5);
81 EXPECT_EQ(trace.samples[2].values,
82 (std::vector<BusValue>{busValueFromBits("0"), busValueFromBits("0000")}));
83 EXPECT_EQ(trace.samples[3].time, 10);
84}
85
86TEST(SiliconWaveformTest, PreservesFourStateValues)
87{
88 Trace trace;
89 resetTrace(trace, {{"in", 4}}, 1);
90 const auto value = busValueFromBits("10XE");
91 appendSnapshot(trace, 0, {value});
92 EXPECT_EQ(trace.samples.front().values.front(), value);
93}
94
95TEST(SiliconWaveformTest, WritesFstTrace)
96{
97 const std::string filename = "test_silicon_waveform.fst";
98 FileCleanup cleanup{filename};
99
100 Trace trace;
101 resetTrace(trace, {{"clk", 1}, {"data", 4}}, 1);
102 appendSnapshot(trace, 0, {busValueFromBits("0"), busValueFromBits("0011")});
103 appendSnapshot(trace, 5, {busValueFromBits("1"), busValueFromBits("1010")});
104
105 writeTrace(filename, trace);
106
107 Reader reader(filename);
108 const auto hierarchy = reader.buildHierarchyTree();
109 ASSERT_EQ(hierarchy.name, "Waveform");
110 ASSERT_EQ(hierarchy.vars.size(), 2);
111 EXPECT_EQ(hierarchy.vars[0].name, "clk");
112 EXPECT_EQ(hierarchy.vars[0].length, 1);
113 EXPECT_EQ(hierarchy.vars[1].name, "data");
114 EXPECT_EQ(hierarchy.vars[1].length, 4);
115
116 std::map<fstHandle, std::string> namesByHandle;
117 for (const auto& var : hierarchy.vars)
118 namesByHandle.emplace(var.handle, var.name);
119
120 std::vector<std::tuple<uint64_t, std::string, std::string>> events;
121 reader.setFacProcessMaskAll();
122 reader.readIterateBlocks(
123 [&](uint64_t time, fstHandle handle, std::string_view value, uint32_t len) {
124 events.emplace_back(time, namesByHandle.at(handle), std::string(value));
125 });
126
127 std::ranges::sort(events);
128
129 EXPECT_EQ(events, (std::vector<std::tuple<uint64_t, std::string, std::string>>{
130 {0, "clk", "0"},
131 {0, "data", "0011"},
132 {5, "clk", "1"},
133 {5, "data", "1010"},
134 }));
135}
High-level zero-overhead wrapper around libfst reader APIs.
void readIterateBlocks(Func &&callback)
Iterates waveform blocks and forwards value changes to a callback.
void setFacProcessMaskAll()
Enables processing for all signals.
FstScopeNode buildHierarchyTree()
Reconstructs hierarchy into a recursive tree structure.
BusValue busValueFromBits(const std::string_view bits)
Strictly decodes an MSB-first raw string containing 0, 1, X, or E.
void writeTrace(std::string_view fileName, const Trace &trace)
void appendSnapshot(Trace &trace, const uint64_t time, std::vector< core::BusValue > values)
void applyEditInterval(Trace &trace, uint64_t duration, int signalIndex, uint64_t startTime, uint64_t endTime, const core::BusValue &rawValue)
void rebuildEditableTrace(Trace &trace, const uint64_t duration)
void resetTrace(Trace &trace, std::vector< Signal > signalDefinitions, const int inputCount)
std::string filename
std::vector< Sample > samples
TEST(SiliconWaveformTest, AppendsAndReplacesLatestTimestamp)