21TEST(YosysToolTest, RejectsMissingConfiguredExecutable)
23 const auto missing = std::filesystem::temp_directory_path()
24 /
"silicon_yosys_executable_that_does_not_exist";
28 .technologyLibraryDirectory = std::nullopt});
29 FAIL() <<
"Expected a missing Yosys executable to be rejected";
30 }
catch (
const std::runtime_error& error) {
31 EXPECT_NE(std::string(error.what()).find(
"executable-validation phase"),
33 EXPECT_NE(std::string(error.what()).find(missing.string()), std::string::npos);
38TEST(YosysToolTest, ReportsProcessCreationFailure)
41 std::filesystem::temp_directory_path()
42 / std::format(
"silicon_yosys_non_executable_{}",
43 std::chrono::steady_clock::now().time_since_epoch().count());
45 std::ofstream file(path);
46 ASSERT_TRUE(file.good());
47 file <<
"not an executable";
49 std::filesystem::permissions(path, std::filesystem::perms::owner_read,
50 std::filesystem::perm_options::replace);
56 .technologyLibraryDirectory = std::nullopt});
57 }
catch (
const std::runtime_error& error) {
58 message = error.what();
60 std::filesystem::remove(path);
62 EXPECT_NE(message.find(
"process-creation phase"), std::string::npos);
63 EXPECT_NE(message.find(
"'yosys' logs"), std::string::npos);
66TEST(YosysToolTest, ReportsPathDiscoveryFailure)
70 const PathEnvironmentGuard emptyPath;
73 }
catch (
const std::runtime_error& error) {
74 message = error.what();
77 EXPECT_NE(message.find(
"executable-discovery phase"), std::string::npos);
78 EXPECT_NE(message.find(
"PATH"), std::string::npos);
82#ifdef SILICON_TEST_YOSYS_EXECUTABLE
84TEST(YosysToolTest, RunsScriptsWithPathDiscoveryAndExplicitSelection)
86 YosysLogCapture logCapture;
88 EXPECT_FALSE(discovered.standardOutput.empty());
91 .executable = std::filesystem::path(SILICON_TEST_YOSYS_EXECUTABLE),
92 .technologyLibraryDirectory = std::nullopt};
94 EXPECT_FALSE(explicitResult.standardOutput.empty());
95 EXPECT_EQ(explicitResult.standardError, discovered.standardError);
96 EXPECT_NE(logCapture.text().find(
"stdout:"), std::string::npos);
99TEST(YosysToolTest, LogsCapturedDiagnosticsAndReferencesLogs)
101 YosysLogCapture logCapture;
105 }
catch (
const std::runtime_error& error) {
106 message = error.what();
109 EXPECT_NE(message.find(
"script-execution phase"), std::string::npos);
110 EXPECT_NE(message.find(
"exit status"), std::string::npos);
111 EXPECT_NE(message.find(
"'yosys' logs"), std::string::npos);
112 EXPECT_EQ(message.find(
"stdout:"), std::string::npos);
113 EXPECT_EQ(message.find(
"stderr:"), std::string::npos);
115 const auto logs = logCapture.text();
116 EXPECT_NE(logs.find(
"stderr:"), std::string::npos);
117 EXPECT_NE(logs.find(
"this_command_does_not_exist"), std::string::npos);
118 EXPECT_NE(logs.find(
"Command failed with exit status"), std::string::npos);
121TEST(YosysToolTest, RejectsInvalidMultiSourceInputs)
125 const std::array unsafe{
126 SourceFile{.path =
"../top.v", .contents =
"module top; endmodule"}};
129 const std::array duplicate{
130 SourceFile{.path =
"code/top.v", .contents =
"module top; endmodule"},
131 SourceFile{.path =
"code/top.v", .contents =
"module other; endmodule"}};
133 std::invalid_argument);
135 const std::array SOURCES{
136 SourceFile{.path =
"code/helper.v", .contents =
"module helper; endmodule"}};
138 std::invalid_argument);
141TEST(YosysToolTest, RejectsImplicitNets)
143 YosysLogCapture logCapture;
147 "module top(input [7:0] a, output [7:0] y); assign y = a + B; endmodule");
148 }
catch (
const std::runtime_error& error) {
149 message = error.what();
152 EXPECT_NE(message.find(
"script-execution phase"), std::string::npos);
153 EXPECT_NE(logCapture.text().find(
"Identifier `\\B' is implicitly declared"),
157TEST(YosysToolTest, ResolvesTransitiveProjectIncludesWithoutParsingUnrelatedFiles)
161 constexpr std::array SOURCES{
162 SourceFile{.path =
"code/top.v", .contents = R
"(`include "helper.v"
163module top(input a, output y);
164 helper child(.a(a), .y(y));
167 SourceFile{.path = "code/helper.v", .contents = R
"(`include "defs.v"
168module helper(input a, output y);
169 assign y = `APPLY(a);
172 SourceFile{.path = "code/defs.v", .contents =
"`define APPLY(signal) ~signal\n"},
174 .contents =
"this is deliberately invalid Verilog\n"}};
177 const auto design = SILICON::yosys::Json::parse(designJson);
178 ASSERT_TRUE(design.at(
"modules").contains(
"top"));
179 ASSERT_TRUE(design.at(
"modules").contains(
"helper"));
180 EXPECT_FALSE(design.at(
"modules").contains(
"unrelated"));
183 EXPECT_EQ(graph.modules(), (std::vector<std::string>{
"helper",
"top"}));
184 EXPECT_EQ(graph.dependenciesOf(
"top"), (std::vector<std::string>{
"helper"}));
186 const auto& helperCells = design.at(
"modules").at(
"helper").at(
"cells");
187 ASSERT_EQ(helperCells.size(), 1);
188 EXPECT_EQ(helperCells.begin().value().at(
"type"),
"$not");
191TEST(YosysToolTest, ReportsMissingProjectIncludeThroughYosysDiagnostics)
194 .path =
"code/top.v", .contents =
"`include \"missing.v\"\n"}};
195 YosysLogCapture logCapture;
199 }
catch (
const std::runtime_error& error) {
200 message = error.what();
203 EXPECT_NE(message.find(
"script-execution phase"), std::string::npos);
204 EXPECT_NE(logCapture.text().find(
"missing.v"), std::string::npos);
207TEST(YosysToolTest, BuildsDependencyGraphFromMultiModuleVerilog)
209 constexpr std::string_view SOURCE = R
"(
210 module leaf(input a, output reg y);
213 module helper(input a, output y0, output y1);
214 leaf first(.a(a), .y(y0));
215 leaf second(.a(a), .y(y1));
217 module unused(input a, output y);
230 helper helper_instance(.a(a), .y0(y0), .y1(y1));
231 leaf leaf_instance(.a(a), .y(y2));
232 assign and_y = a & b;
239 const auto design = SILICON::yosys::Json::parse(designJson);
241 std::set<std::string> topCellTypes;
242 for (
const auto& cell : design.at(
"modules").at(
"top").at(
"cells"))
243 topCellTypes.insert(cell.at(
"type").get<std::string>());
244 EXPECT_TRUE(topCellTypes.contains(
"$and"));
245 EXPECT_TRUE(topCellTypes.contains(
"$or"));
246 EXPECT_TRUE(topCellTypes.contains(
"$not"));
250 EXPECT_EQ(graph.modules(),
251 (std::vector<std::string>{
"helper",
"leaf",
"top",
"unused"}));
252 EXPECT_EQ(graph.dependenciesOf(
"top"), (std::vector<std::string>{
"helper",
"leaf"}));
253 EXPECT_EQ(graph.dependenciesOf(
"helper"), (std::vector<std::string>{
"leaf"}));
254 EXPECT_EQ(graph.dependenciesOf(
"leaf"), std::vector<std::string>{});
255 EXPECT_EQ(graph.dependenciesOf(
"unused"), std::vector<std::string>{});
261TEST(YosysToolTest, ResolvesParameterizedInstancesBeforeJsonExport)
263 constexpr std::string_view SOURCE = R
"(
264 module leaf #(parameter WIDTH = 1)(input [WIDTH-1:0] a, output [WIDTH-1:0] y);
267 module helper #(parameter WIDTH = 1)(input [WIDTH-1:0] a, output [WIDTH-1:0] y);
268 leaf #(.WIDTH(WIDTH)) child(.a(a), .y(y));
270 module top(input [7:0] a, output [7:0] y, output [3:0] z);
271 helper #(.WIDTH(8)) wide(.a(a), .y(y));
272 helper #(.WIDTH(4)) narrow(.a(a[3:0]), .y(z));
274 module unused(input a, output y);
280 const auto design = SILICON::yosys::Json::parse(designJson);
281 const auto& modules = design.at(
"modules");
282 EXPECT_TRUE(modules.contains(
"unused"));
284 for (
const auto& [instance, expectedWidth] :
285 std::array{std::pair{
"wide", 8}, std::pair{
"narrow", 4}}) {
286 const auto& cell = modules.at(
"top").at(
"cells").at(instance);
287 EXPECT_TRUE(cell.at(
"parameters").empty());
288 const auto& helper = modules.at(cell.at(
"type").get<std::string>());
289 EXPECT_EQ(helper.at(
"ports").at(
"a").at(
"bits").size(), expectedWidth);
290 const auto& child = helper.at(
"cells").at(
"child");
291 EXPECT_TRUE(child.at(
"parameters").empty());
292 const auto& leaf = modules.at(child.at(
"type").get<std::string>());
293 EXPECT_EQ(leaf.at(
"ports").at(
"y").at(
"bits").size(), expectedWidth);
298 for (
const auto& module : graph.dependencyOrder({
"top"}))
302TEST(YosysToolTest, ImportsCombinationalVerilogPreservingHelperHierarchy)
304 constexpr std::string_view SOURCE = R
"(
305 module helper(input a, input b, output y);
308 module unused(input a, output y);
311 module selected(input a, input b, output y);
312 helper instance(.a(a), .b(b), .y(y));
316 const Circuit circuit = importVerilog(SOURCE,
"selected");
320 (std::multiset<std::string>{
"Subcircuit",
"DummyInputComponent",
321 "DummyInputComponent",
"DummyOutputComponent"}));
322 EXPECT_THROW((
void)importVerilog(SOURCE,
"missing"), std::runtime_error);
325TEST(YosysToolTest, ImportsLogicalOperatorsWithVectorTruthSemantics)
327 const auto verify = [](
const std::string_view expression,
328 const std::string_view yosysCell,
329 const std::string_view siliconComponent,
const auto& expected) {
330 const auto SOURCE = std::format(
"module top(input [2:0] a, input [2:0] b, output y); "
331 "assign y = {}; endmodule",
334 const auto hierarchicalJson =
336 EXPECT_NE(hierarchicalJson.find(yosysCell), std::string::npos);
338 const std::array circuits{
340 std::make_shared<Circuit>(importVerilog(SOURCE,
"top")),
342 for (
const auto& circuit : circuits) {
343 EXPECT_EQ(
componentTypes(*circuit).count(std::string(siliconComponent)), 1);
346 for (
unsigned int a = 0; a < 8; ++a) {
347 for (
unsigned int b = 0; b < 8; ++b) {
348 SCOPED_TRACE(std::format(
"{} with a={} b={}", expression, a, b));
349 EXPECT_EQ(evaluateBinaryCircuit(circuit, a, b),
350 valueFor(1, expected(a != 0, b != 0)));
356 verify(
"a && b",
"$logic_and",
"AndGate",
357 [](
const bool a,
const bool b) {
return a && b; });
358 verify(
"a || b",
"$logic_or",
"OrGate",
359 [](
const bool a,
const bool b) {
return a || b; });
362TEST(YosysToolTest, ImportsLogicalNotAsOneGate)
364 constexpr std::string_view SOURCE =
365 "module top(input [2:0] a, output y); assign y = !a; endmodule";
366 auto circuit = std::make_shared<Circuit>(importVerilog(SOURCE,
"top"));
371 ASSERT_TRUE(findComponent<NotGate>(*circuit));
372 EXPECT_EQ(findComponent<NotGate>(*circuit)->getPropertyValue<
int>(
"size"), 1);
374 EXPECT_NE(serialized.find(
"$logic_not"), std::string::npos);
376 const auto input = findNamedComponent<DummyBusInputComponent>(*circuit,
"a");
377 const auto output = findNamedComponent<DummyOutputComponent>(*circuit,
"y");
381 input->setBusValue(
valueFor(input->outputBuses()[0], 0));
383 ASSERT_EQ(simulator.runUntilIdle(), Simulator::RunResult::Completed);
384 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(),
valueFor(1, 1));
387 simulator.setBus(input->outputBuses()[0],
valueFor(input->outputBuses()[0], 2)),
388 Simulator::RunResult::Completed);
389 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(),
valueFor(1, 0));
392TEST(YosysToolTest, KeepsAluLogicalOperationsAndVectorNotCompact)
394 constexpr std::string_view SOURCE = R
"(
397 input [7:0] OperandA,
399 output reg [7:0] result
403 3'b000: result = OperandA + B;
404 3'b001: result = OperandA - B;
405 3'b100: result = OperandA && B;
406 3'b101: result = OperandA || B;
407 3'b110: result = ~OperandA;
408 3'b111: result = OperandA ^ OperandA;
415 const Circuit circuit = importVerilog(SOURCE,
"alu");
417 EXPECT_EQ(types.count(
"AndGate"), 1);
418 EXPECT_EQ(types.count(
"OrGate"), 1);
419 EXPECT_EQ(types.count(
"NotGate"), 1);
420 EXPECT_EQ(types.count(
"Multiplexer"), 1);
421 EXPECT_LT(types.size(), 25);
422 ASSERT_TRUE(findComponent<NotGate>(circuit));
423 EXPECT_EQ(findComponent<NotGate>(circuit)->getPropertyValue<int>(
"size"), 8);
426TEST(YosysToolTest, ImportsOnlyASingleDiscoveredModule)
428 const auto discover = [](
const std::string_view SOURCE) -> std::string {
431 if (modules.size() != 1)
432 throw std::runtime_error(
"Verilog source must declare exactly one module");
433 return modules.front();
436 const std::string_view SOURCE =
437 "module sole(input a, output y); assign y = ~a; endmodule";
438 const auto top = discover(SOURCE);
439 const auto circuit = importVerilog(SOURCE, top);
440 EXPECT_EQ(top,
"sole");
442 EXPECT_THROW((
void)discover(
""), std::runtime_error);
443 EXPECT_THROW((
void)discover(
"module first; endmodule module second; endmodule"),
447TEST(YosysToolTest, ImportsZeroExtendedOutputAsUnsignedExtender)
449 constexpr std::string_view SOURCE = R
"(
450 module a(output [7:0] bus_out, input in);
451 assign bus_out = { 7'h00, in };
455 const Circuit circuit = importVerilog(SOURCE,
"a");
456 const auto extender = findComponent<Extender>(circuit);
457 ASSERT_TRUE(extender);
458 EXPECT_EQ(extender->getPropertyValue<
int>(
"inSize"), 1);
459 EXPECT_EQ(extender->getPropertyValue<
int>(
"outSize"), 8);
460 EXPECT_EQ(extender->getPropertyValue<std::string>(
"mode"),
462 EXPECT_FALSE(findComponent<ConstantComponent>(circuit));
465TEST(YosysToolTest, RaisesSharedConstantEqualityComparisonsToDecoder)
467 #ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
468 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
470 constexpr std::string_view SOURCE = R
"(
471 module top(input [2:0] select, output [1:0] matches);
472 assign matches[0] = select == 3'd1;
473 assign matches[1] = 3'd6 == select;
477 auto circuit = std::make_shared<Circuit>(importVerilog(SOURCE,
"top"));
478 auto decoder = findComponent<Decoder>(*circuit);
479 ASSERT_TRUE(decoder);
480 EXPECT_EQ(decoder->getPropertyValue<
int>(
"selectionSize"), 3);
485 simulator.setBus(decoder->inputBuses()[1],
valueFor(decoder->inputBuses()[1], 1)),
486 Simulator::RunResult::Completed);
487 EXPECT_EQ(decoder->outputBuses()[0][1]->getCurrentState(), State::HIGH);
488 EXPECT_EQ(decoder->outputBuses()[0][6]->getCurrentState(), State::LOW);
491 simulator.setBus(decoder->inputBuses()[1],
valueFor(decoder->inputBuses()[1], 6)),
492 Simulator::RunResult::Completed);
493 EXPECT_EQ(decoder->outputBuses()[0][1]->getCurrentState(), State::LOW);
494 EXPECT_EQ(decoder->outputBuses()[0][6]->getCurrentState(), State::HIGH);
497 simulator.setBus(decoder->inputBuses()[1],
valueFor(decoder->inputBuses()[1], 3)),
498 Simulator::RunResult::Completed);
499 EXPECT_EQ(decoder->outputBuses()[0][1]->getCurrentState(), State::LOW);
500 EXPECT_EQ(decoder->outputBuses()[0][6]->getCurrentState(), State::LOW);
504TEST(YosysToolTest, LegalizationAcceptsTechnologyMapPathWithSpaces)
506#ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
507 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
509 const auto directory = std::filesystem::temp_directory_path()
510 / std::format(
"silicon technology map {}",
511 std::chrono::steady_clock::now().time_since_epoch().count());
512 std::filesystem::create_directories(directory);
516 { std::ofstream file(directory /
"silicon_cells.v"); file <<
"// empty library\n"; }
517 { std::ofstream file(directory /
"silicon_techmap.v"); file <<
"// empty map\n"; }
519 .executable = std::nullopt,
520 .technologyLibraryDirectory = directory};
522 "module top(input a, output y); assign y = a; endmodule", options);
526 std::filesystem::remove_all(directory);
529 std::filesystem::remove_all(directory);
533TEST(YosysToolTest, PmgenMatchesSelectedPmuxWithUnselectedPredicates)
535 #ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
536 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
538 constexpr std::string_view SOURCE = R
"(
558 EXPECT_NO_THROW(runPluginScript(SOURCE,
559 "hierarchy -check -top top\n"
562 "select top/t:$pmux\n"
563 "silicon_pmux_bmux\n"
564 "select -assert-count 1 top/t:$bmux\n"
565 "select -assert-count 0 top/t:$pmux\n",
570TEST(YosysToolTest, PmgenLeavesInvalidGroupsAndRaisesValidSignedGroup)
572 #ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
573 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
575 constexpr std::string_view SOURCE = R
"(
579 input signed [2:0] signed_select,
582 assign matches[0] = first == 3'd1;
583 assign matches[1] = first == 3'd3;
584 assign matches[2] = first == 3'd3;
585 assign matches[3] = second == 3'd2;
586 assign matches[4] = signed_select == 3'sd1;
587 assign matches[5] = signed_select == 3'sd2;
591 EXPECT_NO_THROW(runPluginScript(SOURCE,
592 "hierarchy -check -top top\n"
594 "silicon_eq_decoder\n"
595 "select -assert-count 4 top/t:$eq\n"
596 "select -assert-count 1 top/t:$demux\n",
597 "invalid_eq_groups"));
601TEST(YosysToolTest, FoldsPrivateClockedRomAddressInPlugin)
603 #ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
604 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
606 constexpr std::string_view SOURCE = R
"(
607 module top(input clk, input en, input [1:0] addr, output [7:0] data);
608 reg [1:0] saved_addr;
611 rom[0] = 8'h12; rom[1] = 8'h34;
612 rom[2] = 8'h56; rom[3] = 8'h78;
614 always @(posedge clk) if (en) saved_addr <= addr;
615 assign data = rom[saved_addr];
618 EXPECT_NO_THROW(runPluginScript(SOURCE,
619 "hierarchy -check -top top\n"
624 "select -assert-count 1 top/t:$dffe\n"
625 "select -assert-count 1 top/t:$mux\n"
626 "silicon_memrd_address\n"
627 "select -assert-count 0 top/t:$dffe\n"
628 "select -assert-count 0 top/t:$mux\n"
629 "select -assert-count 1 top/t:$mem_v2\n",
630 "private_rom_address"));
634TEST(YosysToolTest, KeepsRomAddressRegisterWithExternalConsumer)
636 #ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
637 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
639 constexpr std::string_view SOURCE = R
"(
640 module top(input clk, input en, input [1:0] addr,
641 output [1:0] saved, output [7:0] data);
642 reg [1:0] saved_addr;
645 rom[0] = 8'h12; rom[1] = 8'h34;
646 rom[2] = 8'h56; rom[3] = 8'h78;
648 always @(posedge clk) if (en) saved_addr <= addr;
649 assign saved = saved_addr;
650 assign data = rom[saved_addr];
653 EXPECT_NO_THROW(runPluginScript(SOURCE,
654 "hierarchy -check -top top\n"
659 "silicon_memrd_address\n"
660 "select -assert-count 1 top/t:$dffe\n"
661 "select -assert-count 1 top/t:$mux\n",
662 "shared_rom_address"));
666TEST(YosysToolTest, KeepsRomAddressRegisterWithResetOrInitialization)
668 #ifndef SILICON_TEST_YOSYS_PLUGIN_PATH
669 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
671 constexpr std::string_view SOURCE = R
"(
672 module top(input clk, input en, input rst, input [1:0] addr,
674 reg [1:0] saved_addr;
677 rom[0] = 8'h12; rom[1] = 8'h34;
678 rom[2] = 8'h56; rom[3] = 8'h78;
680 always @(posedge clk) if (en) saved_addr <= addr;
681 assign data = rom[saved_addr];
685 constexpr std::array cases{
686 std::pair{
"read_arst",
"connect -port rom RD_ARST rst\n"
687 "setparam -set RD_ARST_VALUE 8'h00 rom\n"},
688 std::pair{
"read_srst",
"connect -port rom RD_SRST rst\n"
689 "setparam -set RD_SRST_VALUE 8'h00 rom\n"},
690 std::pair{
"read_init",
"setparam -set RD_INIT_VALUE 8'h00 rom\n"},
691 std::pair{
"address_init",
"setattr -set init 2'bx1 saved_addr\n"}};
693 for (
const auto& [name, change] : cases) {
695 EXPECT_NO_THROW(runPluginScript(
697 std::format(
"hierarchy -check -top top\n"
702 "select -assert-count 1 top/t:$dffe\n"
703 "select -assert-count 1 top/t:$mux\n"
707 "silicon_memrd_address\n"
708 "select -assert-count 1 top/t:$dffe\n"
709 "select -assert-count 1 top/t:$mux\n"
710 "select -assert-count 1 top/t:$mem_v2\n",
717TEST(YosysToolTest, LowersPriorityMuxCellsBeforeImport)
719 constexpr std::string_view SOURCE = R
"(
721 input [1:0] fallback,
740 const Circuit circuit = importVerilog(SOURCE,
"top");
744TEST(YosysToolTest, ImportsSequentialVerilog)
746 constexpr std::string_view SOURCE = R
"(
747 module storage(input d, input clk, output reg q);
748 always @(posedge clk)
752 const Circuit circuit = importVerilog(SOURCE,
"storage");
756TEST(YosysToolTest, FoldsSparseCaseIntoOneWideMultiplexer)
758 #ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
759 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
761 constexpr std::string_view SOURCE = R
"(
779 const Circuit circuit = importVerilog(SOURCE,
"top");
785TEST(YosysToolTest, KeepsSynchronousResetAsScalarMuxAndDFlipFlop)
787 constexpr std::string_view SOURCE = R
"(
794 always @(posedge clk) begin
803 const Circuit circuit = importVerilog(SOURCE,
"sdff_test");
808 const auto mux = findComponent<Multiplexer>(circuit);
810 ASSERT_EQ(
mux->inputBuses().size(), 3);
811 EXPECT_EQ(
mux->inputBuses()[0].size(), 1);
812 EXPECT_EQ(
mux->inputBuses()[1].size(), 1);
813 EXPECT_EQ(
mux->inputBuses()[2].size(), 1);
816TEST(YosysToolTest, FoldsExhaustiveCaseIntoOneWideMultiplexer)
818 #ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
819 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
821 constexpr std::string_view SOURCE = R
"(
828 output reg [3:0] bus_out
832 2'h0: bus_out = bus_1;
833 2'h1: bus_out = bus_2;
834 2'h2: bus_out = bus_3;
835 2'h3: bus_out = bus_4;
836 default: bus_out = 4'hx;
842 const Circuit circuit = importVerilog(SOURCE,
"mux");
847 const auto mux = findComponent<Multiplexer>(circuit);
849 EXPECT_EQ(
mux->getPropertyValue<
int>(
"selectionSize"), 2);
850 EXPECT_EQ(
mux->getPropertyValue<
int>(
"busSize"), 4);
851 EXPECT_EQ(
mux->inputBuses().size(), 5);
854TEST(YosysToolTest, ImportsCaseLiteralLanesAsSizedConstants)
856 #ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
857 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
859 constexpr std::string_view SOURCE = R
"(
860 module my_mux(input [1:0] a, input [3:0] b, c, output reg [3:0] o);
861 always @(a, b, c) begin
872 const Circuit circuit = importVerilog(SOURCE,
"my_mux");
878 std::set<BusValue> values;
880 if (
auto constant = std::dynamic_pointer_cast<ConstantComponent>(component)) {
881 EXPECT_EQ(constant->getPropertyValue<
int>(
"size"), 4);
882 values.insert(*constant->getPropertyValue<
BusValue>(
"value"));
889TEST(YosysTest, LegalizesScalarActiveHighNativeDlatch)
894 std::make_shared<DLatch>(std::make_shared<Wire>(), std::make_shared<Wire>(),
895 std::make_shared<Wire>(), std::make_shared<Wire>());
896 auto design = nlohmann::json::parse(
898 auto& cell = onlyModule(design)[
"cells"].begin().value();
899 cell[
"type"] =
"$dlatch";
901 cell[
"connections"].erase(
"QN");
902 cell[
"port_directions"].erase(
"QN");
std::multiset< std::string > componentTypes(const Circuit &circuit)
std::vector< Component_ptr > componentsIn(const Circuit &circuit)
Represents a digital circuit as a directed graph of components.
Executes event-driven simulation of a Circuit.
ModuleNameList modules() const
Returns every declared module name.
Mutable module-building interface passed to component serializers.
static std::string parameter(std::uint64_t value, std::size_t width=32)
Encode an unsigned Yosys parameter as a fixed-width binary string.
std::optional< Mux > mux(Cell *cell, SigMap &signals)
BusValue busValueFromBits(const std::string_view bits)
Strictly decodes an MSB-first raw string containing 0, 1, X, or E.
std::vector< State > BusValue
std::string read(const std::string_view source, const yosys::ToolOptions &options)
core::SourceFile SourceFile
std::string elaborateHierarchy(const std::string_view json, const ToolOptions &options)
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.
ScriptResult runScript(const std::string_view script, const ToolOptions &options)
ModuleDependencyGraph moduleDependencyGraph(const std::string_view json)
Build a module dependency graph from a Yosys write_json document.
std::string serialize(const Circuit &circuit, const std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
std::string readVerilog(const std::string_view source, const ToolOptions &options)
BusValue valueFor(const Bus &bus, const std::uint64_t value)