23TEST(YosysTest, ImportsAndSimulatesOriginalFp32Multiplier)
25#ifdef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
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>());
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");
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));
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}}})
77 std::uint32_t state = 0x516c0ffeu;
78 const auto next = [&] {
84 for (
int sample = 0; sample < 100; ++sample) {
85 const auto a = next();
86 const auto b = next();
90 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
94TEST(YosysTest, YosysAcceptsEveryBuiltInLowering)
96#ifndef SILICON_TEST_YOSYS_EXECUTABLE
97 GTEST_SKIP() <<
"Yosys executable was not found when tests were configured";
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}) {
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);
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);
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));
182 components.push_back(serialRegister);
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);
191 EXPECT_EQ(verilog.find(
"SILICON_"), std::string::npos);
192 EXPECT_NO_THROW((
void)importVerilog(verilog,
"top"));