SILICON
Loading...
Searching...
No Matches
yosys_verilog.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
19
20#include <bit>
21#include <cmath>
22
23TEST(YosysTest, ImportsAndSimulatesOriginalFp32Multiplier)
24{
25#ifdef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
26 const auto path =
27 std::filesystem::path(__FILE__).parent_path() / "fixtures/fp32_multiplier.v";
28 std::ifstream file(path);
29 ASSERT_TRUE(file.good());
30 const std::string source((std::istreambuf_iterator<char>(file)),
31 std::istreambuf_iterator<char>());
32 const auto circuit =
33 std::make_shared<Circuit>(importVerilog(source, "fp32_multiplier"));
34 const auto inputA = findNamedComponent<DummyBusInputComponent>(*circuit, "a");
35 const auto inputB = findNamedComponent<DummyBusInputComponent>(*circuit, "b");
36 const auto output = findNamedComponent<DummyBusOutputComponent>(*circuit, "result");
37 ASSERT_TRUE(inputA);
38 ASSERT_TRUE(inputB);
39 ASSERT_TRUE(output);
40 Simulator simulator(circuit);
41
42 const auto check = [&](std::uint32_t a, std::uint32_t b) {
43 SCOPED_TRACE(std::format("a={:08x} b={:08x}", a, b));
44 const float product = std::bit_cast<float>(a) * std::bit_cast<float>(b);
45 const std::uint32_t expected =
46 std::isnan(product) ? 0x7fc00000u : std::bit_cast<std::uint32_t>(product);
47 ASSERT_EQ(simulator.setBus(inputA->outputBuses()[0], valueFor(32, a), inputA),
48 Simulator::RunResult::Completed);
49 ASSERT_EQ(simulator.setBus(inputB->outputBuses()[0], valueFor(32, b), inputB),
50 Simulator::RunResult::Completed);
51 ASSERT_EQ(simulator.runUntilIdle(), Simulator::RunResult::Completed);
52 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(), valueFor(32, expected));
53 };
54 // Include zeros, subnormals, rounding ties, normalization carries, overflow,
55 // infinities and NaNs, in addition to arbitrary operand bit patterns.
56 for (const auto [a, b] : std::array<std::pair<std::uint32_t, std::uint32_t>, 18>{
57 {{0x3f800000u, 0x3f800000u},
58 {0x3fc00000u, 0x40000000u},
59 {0xbfc00000u, 0x40000000u},
60 {0x3fffffffu, 0x3fffffffu},
61 {0x3fb504f3u, 0x3fb504f3u},
62 {0x00000001u, 0x3f800000u},
63 {0x00800000u, 0x3f000000u},
64 {0x007fffffu, 0x3f800001u},
65 {0x00000001u, 0x3f000000u},
66 {0x00000003u, 0x3f000000u},
67 {0x7f7fffffu, 0x40000000u},
68 {0x80000000u, 0x3f800000u},
69 {0x00000000u, 0x7f800000u},
70 {0xff800000u, 0x40000000u},
71 {0x7fc12345u, 0x3f800000u},
72 {0x7f812345u, 0x3f800000u},
73 {0x00000001u, 0x00000001u},
74 {0x00800000u, 0x00800000u}}})
75 check(a, b);
76
77 std::uint32_t state = 0x516c0ffeu;
78 const auto next = [&] {
79 state ^= state << 13;
80 state ^= state >> 17;
81 state ^= state << 5;
82 return state;
83 };
84 for (int sample = 0; sample < 100; ++sample) {
85 const auto a = next();
86 const auto b = next();
87 check(a, b);
88 }
89#else
90 GTEST_SKIP() << "The SILICON Yosys plugin is unavailable";
91#endif
92}
93
94TEST(YosysTest, YosysAcceptsEveryBuiltInLowering)
95{
96#ifndef SILICON_TEST_YOSYS_EXECUTABLE
97 GTEST_SKIP() << "Yosys executable was not found when tests were configured";
98#else
99 std::vector<Component_ptr> components;
100 components.push_back(std::make_shared<AndGate>(
101 std::vector<Wire_ptr>{std::make_shared<Wire>(), std::make_shared<Wire>(),
102 std::make_shared<Wire>()},
103 std::make_shared<Wire>()));
104 components.push_back(std::make_shared<OrGate>(
105 std::vector<Wire_ptr>{std::make_shared<Wire>(), std::make_shared<Wire>()},
106 std::make_shared<Wire>()));
107 components.push_back(
108 std::make_shared<NotGate>(std::make_shared<Wire>(), std::make_shared<Wire>()));
109 components.push_back(std::make_shared<NandGate>(
110 std::vector<Wire_ptr>{std::make_shared<Wire>(), std::make_shared<Wire>()},
111 std::make_shared<Wire>()));
112 components.push_back(std::make_shared<NorGate>(
113 std::vector<Wire_ptr>{std::make_shared<Wire>(), std::make_shared<Wire>()},
114 std::make_shared<Wire>()));
115 components.push_back(std::make_shared<XorGate>(
116 std::array<Wire_ptr, 2>{std::make_shared<Wire>(), std::make_shared<Wire>()},
117 std::make_shared<Wire>()));
118 components.push_back(std::make_shared<Extender>(Bus(3), Bus(5)));
119 components.push_back(std::make_shared<Complementer>(Bus(4), Bus(4)));
120 for (const bool sign : {false, true}) {
121 auto multiplier =
122 std::make_shared<Multiplier>(std::array<Bus, 2>{Bus(4), Bus(4)}, Bus(4));
123 multiplier->setProperty("signed", sign);
124 components.push_back(multiplier);
125 auto divider =
126 std::make_shared<Divider>(std::array<Bus, 2>{Bus(4), Bus(4)}, Bus(4), Bus(4));
127 divider->setProperty("signed", sign);
128 components.push_back(divider);
129 }
130 components.push_back(std::make_shared<HalfAdder>(
131 std::array<Wire_ptr, 2>{std::make_shared<Wire>(), std::make_shared<Wire>()},
132 std::make_shared<Wire>(), std::make_shared<Wire>()));
133 components.push_back(std::make_shared<FullAdder>(
134 std::array<Wire_ptr, 2>{std::make_shared<Wire>(), std::make_shared<Wire>()},
135 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>()));
136 components.push_back(std::make_shared<AdderNBits>(std::array<Bus, 2>{Bus(4), Bus(4)},
137 Bus(4), std::make_shared<Wire>()));
138 components.push_back(std::make_shared<Comparator>(std::array<Bus, 2>{Bus(4), Bus(4)},
139 std::make_shared<Wire>()));
140 components.push_back(
141 std::make_shared<Multiplexer>(Bus(4), Bus(2), std::make_shared<Wire>()));
142 auto busMux = std::make_shared<Multiplexer>(Bus(4), Bus(2), std::make_shared<Wire>());
143 busMux->setProperty("busSize", 2);
144 components.push_back(busMux);
145 components.push_back(std::make_shared<Demultiplexer>(Bus(1), Bus(2), Bus(4)));
146 auto busDemux = std::make_shared<Demultiplexer>(Bus(1), Bus(2), Bus(4));
147 busDemux->setProperty("busSize", 2);
148 components.push_back(busDemux);
149 components.push_back(std::make_shared<Decoder>(Bus(1), Bus(2), Bus(4)));
150 components.push_back(
151 std::make_shared<WireSplitter>(Bus(2), std::vector<Bus>{Bus(1), Bus(1)}));
152 components.push_back(
153 std::make_shared<WireMerger>(std::vector<Bus>{Bus(1), Bus(1)}, Bus(2)));
154 components.push_back(std::make_shared<DFlipFlop>(
155 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
156 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>()));
157 components.push_back(std::make_shared<EFlipFlop>(
158 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
159 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
160 std::make_shared<Wire>()));
161 components.push_back(
162 std::make_shared<DLatch>(std::make_shared<Wire>(), std::make_shared<Wire>(),
163 std::make_shared<Wire>(), std::make_shared<Wire>()));
164 components.push_back(std::make_shared<JKFlipFlop>(
165 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
166 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
167 std::make_shared<Wire>()));
168 components.push_back(std::make_shared<Register>(Bus(4), std::make_shared<Wire>(),
169 std::make_shared<Wire>(),
170 std::make_shared<Wire>(), Bus(4)));
171 components.push_back(std::make_shared<Register>(Bus(1), std::make_shared<Wire>(),
172 std::make_shared<Wire>(),
173 std::make_shared<Wire>(), Bus(4)));
174 components.push_back(std::make_shared<Register>(Bus(4), std::make_shared<Wire>(),
175 std::make_shared<Wire>(),
176 std::make_shared<Wire>(), Bus(1)));
177 auto serialRegister = std::make_shared<Register>(Bus(2), std::make_shared<Wire>(),
178 std::make_shared<Wire>(),
179 std::make_shared<Wire>(), Bus(2));
180 serialRegister->setProperty("inputType", std::string(Register::SERIAL_TYPE));
181 serialRegister->setProperty("outputType", std::string(Register::SERIAL_TYPE));
182 components.push_back(serialRegister);
183
184 for (std::size_t index = 0; index < components.size(); ++index) {
185 SCOPED_TRACE(std::format("component {} ({})", index, components[index]->typeName()));
186 const auto circuit = circuitWithBoundaryPorts(components[index]);
187 EXPECT_EQ(validateWithYosys(circuit, std::format("component_{}", index)), 0);
188 EXPECT_NO_THROW(
189 (void)SILICON::yosys::deserialize(legalizeExpressions(SILICON::yosys::serialize(circuit, "top"))));
190 const auto verilog = SILICON::verilog::write(circuit, "top");
191 EXPECT_EQ(verilog.find("SILICON_"), std::string::npos);
192 EXPECT_NO_THROW((void)importVerilog(verilog, "top"));
193 }
194#endif
195}
196
197TEST(YosysTest, ExportsSubcircuitsAsHierarchicalModules)
198{
199 auto registry = ComponentRegistry::empty();
200 registerAllComponents(registry);
202 SILICON::project::ProjectCircuitResolver resolver{project, registry};
206
207 {
208 auto instance = std::make_shared<SubcircuitComponent>(&resolver);
209 instance->setProperty("slug", std::string("and_child"));
210 auto circuit = circuitWithBoundaryPorts(instance);
211
212 const auto json = nlohmann::json::parse(SILICON::yosys::serialize(circuit, "top"));
213 ASSERT_TRUE(json.at("modules").contains("top"));
214 ASSERT_TRUE(json.at("modules").contains("and_child"));
215 EXPECT_TRUE(std::ranges::any_of(
216 json.at("modules").at("top").at("cells"),
217 [](const auto& cell) { return cell.at("type") == "and_child"; }));
218#ifdef SILICON_TEST_YOSYS_EXECUTABLE
219 EXPECT_EQ(validateWithYosys(circuit, "hierarchy"), 0);
220 const auto verilog = SILICON::verilog::write(circuit, "top");
221 EXPECT_NE(verilog.find("module and_child"), std::string::npos);
222 EXPECT_NO_THROW((void)importVerilog(verilog, "top"));
223#endif
224 }
225}
226
227TEST(YosysTest, YosysReadJsonAcceptsExport)
228{
229#ifndef SILICON_TEST_YOSYS_EXECUTABLE
230 GTEST_SKIP() << "Yosys executable was not found when tests were configured";
231#else
232 auto a = std::make_shared<Wire>();
233 auto b = std::make_shared<Wire>();
234 auto y = std::make_shared<Wire>();
235 Circuit circuit(
237 std::make_shared<DummyInputComponent>(Bus{a}, "a"),
238 std::make_shared<DummyInputComponent>(Bus{b}, "b"),
239 std::make_shared<AndGate>(std::vector<Wire_ptr>{a, b}, y),
240 std::make_shared<DummyOutputComponent>(Bus{y}, "y"),
241 },
242 false);
243 EXPECT_EQ(validateWithYosys(circuit, "simple"), 0);
244#endif
245}
Represents a digital circuit as a directed graph of components.
Definition circuit.hpp:125
static ComponentRegistry empty()
static constexpr std::string_view SERIAL_TYPE
Definition register.hpp:33
void upsertDocument(Document document)
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
RunResult setBus(Bus bus, BusValue value, CancellationCheck isCancelled={})
Forces a bus to a specific value and propagates the change through the circuit.
RunResult runUntilIdle(CancellationCheck isCancelled={})
Runs pending timed events until no further propagation is scheduled.
void registerAllComponents(ComponentRegistry &registry)
std::set< Component_ptr, std::owner_less< Component_ptr > > Component_set
Definition wire.hpp:54
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
std::string write(const std::string_view yosysJson, const yosys::ToolOptions &options)
Definition verilog.cpp:494
Circuit deserialize(const std::string_view json, const std::optional< std::string_view > moduleName)
Deserialize one supported Yosys write_json module into a Silicon circuit.
std::string serialize(const Circuit &circuit, const std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
Definition netlist.cpp:443
std::string andSubcircuitDocument()
BusValue valueFor(const Bus &bus, const std::uint64_t value)
Definition tests.hpp:11
TEST(YosysTest, ImportsAndSimulatesOriginalFp32Multiplier)