52TEST(YosysTest, CollapsesGateTreesWithFourStateAndBusSemantics)
54#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
55 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
57 for (
const bool logical : {
false,
true})
58 for (
const bool conjunction : {
false,
true}) {
59 SCOPED_TRACE(conjunction);
60 SCOPED_TRACE(logical);
61 auto design = gateTreeDesign(conjunction);
63 auto&
module = onlyModule(design);
64 module["ports"]["y"]["bits"] = nlohmann::json::array({14});
65 for (
auto& cell : module[
"cells"]) {
66 cell[
"type"] = conjunction ?
"$logic_and" :
"$logic_or";
67 cell[
"parameters"][
"Y_WIDTH"] =
69 cell[
"connections"][
"Y"].erase(1);
71 auto& root =
module["cells"]["root"];
72 for (
const auto port : {
"A",
"B"}) {
73 root[
"parameters"][std::string(port) +
"_WIDTH"] =
75 root[
"connections"][port].erase(1);
78 auto circuit = std::make_shared<Circuit>(importGateStructure(design));
79 const auto type = conjunction ?
"AndGate" :
"OrGate";
82 if (component->typeName() == type) {
83 ASSERT_EQ(component->inputBuses().size(), 4);
91#ifdef SILICON_TEST_YOSYS_EXECUTABLE
92 circuit = std::make_shared<Circuit>(importVerilog(
98 std::array<std::shared_ptr<DummyBusInputComponent>, 4> inputs;
99 for (std::size_t i = 0; i < inputs.size(); ++i) {
101 findNamedComponent<DummyBusInputComponent>(*circuit, std::string(1,
'a' + i));
102 ASSERT_TRUE(inputs[i]);
104 const auto output = findNamedComponent<BoundaryIoComponent>(*circuit,
"y");
107 constexpr std::array states{State::LOW, State::HIGH, State::UNKNOWN, State::ERROR};
108 for (
unsigned int pattern = 0; pattern < 256; ++pattern) {
109 BusValue expected(logical ? 1 : 2, conjunction ? State::HIGH : State::LOW);
110 for (std::size_t i = 0; i < inputs.size(); ++i) {
111 const auto index = (pattern >> (2 * i)) & 3;
112 const BusValue value{states[index], states[(index + 1) & 3]};
113 simulator.
setBus(inputs[i]->outputBuses()[0], value);
115 State truth = State::LOW;
116 for (
const auto state : value)
117 truth = truth || state;
118 expected[0] = conjunction ? expected[0] && truth : expected[0] || truth;
120 for (std::size_t bit = 0; bit < value.size(); ++bit)
122 conjunction ? expected[bit] && value[bit] : expected[bit] || value[bit];
124 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
125 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(), expected);
232TEST(YosysTest, LowersCombinationalComponents)
234 auto a = std::make_shared<Wire>();
235 auto b = std::make_shared<Wire>();
236 auto c = std::make_shared<Wire>();
237 auto y = std::make_shared<Wire>();
238 auto carry = std::make_shared<Wire>();
240 EXPECT_EQ(cellTypes(onlyModule(exportComponent(
241 std::make_shared<NandGate>(std::vector<Wire_ptr>{a, b, c}, y)))),
242 (std::multiset<std::string>{
"$and",
"$and",
"$not"}));
243 EXPECT_EQ(cellTypes(onlyModule(exportComponent(std::make_shared<FullAdder>(
244 std::array<Wire_ptr, 2>{a, b}, c, y, carry)))),
245 std::multiset<std::string>{
"SILICON_FULL_ADDER"});
247 auto adder = std::make_shared<AdderNBits>(std::array<Bus, 2>{
Bus(4),
Bus(4)},
Bus(4),
248 std::make_shared<Wire>());
249 EXPECT_EQ(cellTypes(onlyModule(exportComponent(adder))),
250 std::multiset<std::string>{
"SILICON_ADDER"});
252 auto extender = std::make_shared<Extender>(
Bus(3),
Bus(5));
253 EXPECT_EQ(cellTypes(onlyModule(exportComponent(extender))),
254 std::multiset<std::string>{
"$pos"});
256 auto mux = std::make_shared<Multiplexer>(
Bus(4),
Bus(2), std::make_shared<Wire>());
257 EXPECT_EQ(cellTypes(onlyModule(exportComponent(mux))),
258 std::multiset<std::string>{
"$bmux"});
260 auto demux = std::make_shared<Demultiplexer>(
Bus(1),
Bus(2),
Bus(4));
261 EXPECT_EQ(cellTypes(onlyModule(exportComponent(demux))),
262 std::multiset<std::string>{
"$demux"});
264 auto decoder = std::make_shared<Decoder>(
Bus(1),
Bus(2),
Bus(4));
265 EXPECT_EQ(cellTypes(onlyModule(exportComponent(decoder))),
266 std::multiset<std::string>{
"$demux"});
269 std::make_shared<WireSplitter>(
Bus(2), std::vector<Bus>{
Bus(1),
Bus(1)});
270 auto merger = std::make_shared<WireMerger>(std::vector<Bus>{
Bus(1),
Bus(1)},
Bus(2));
271 EXPECT_EQ(cellTypes(onlyModule(exportComponent(splitter))),
272 std::multiset<std::string>{
"$pos"});
273 EXPECT_EQ(cellTypes(onlyModule(exportComponent(merger))),
274 std::multiset<std::string>{
"$pos"});
395TEST(YosysTest, LowersSequentialComponents)
398 std::make_shared<DLatch>(std::make_shared<Wire>(), std::make_shared<Wire>(),
399 std::make_shared<Wire>(), std::make_shared<Wire>());
400 EXPECT_EQ(cellTypes(onlyModule(exportComponent(latch))),
401 std::multiset<std::string>{
"SILICON_DLATCH"});
403 auto dff = std::make_shared<DFlipFlop>(
404 std::make_shared<Wire>(), std::make_shared<Wire>(),
nullptr,
nullptr,
405 std::make_shared<Wire>(), std::make_shared<Wire>());
406 EXPECT_EQ(cellTypes(onlyModule(exportComponent(dff))),
407 std::multiset<std::string>{
"SILICON_DFF"});
409 auto effe = std::make_shared<EFlipFlop>(
410 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
411 nullptr,
nullptr, std::make_shared<Wire>(), std::make_shared<Wire>());
412 EXPECT_EQ(cellTypes(onlyModule(exportComponent(effe))),
413 std::multiset<std::string>{
"SILICON_DFFE"});
415 auto jk = std::make_shared<JKFlipFlop>(
416 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
417 nullptr,
nullptr, std::make_shared<Wire>(), std::make_shared<Wire>());
418 EXPECT_EQ(cellTypes(onlyModule(exportComponent(jk))),
419 std::multiset<std::string>{
"SILICON_JKFF"});
421 EXPECT_EQ(cellTypes(onlyModule(exportComponent(registerWithMode(
true,
false)))),
422 std::multiset<std::string>{
"SILICON_PISO"});
425TEST(YosysTest, CustomTechnologyCellsRoundTripToNativeComponents)
429#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
430 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
438 std::make_shared<DLatch>(std::make_shared<Wire>(), std::make_shared<Wire>(),
439 std::make_shared<Wire>(), std::make_shared<Wire>());
440 const auto latchDesign = exportComponent(latch);
441 const auto& latchCell = onlyCell(latchDesign);
442 EXPECT_EQ(latchCell.at(
"type"),
"SILICON_DLATCH");
443 EXPECT_EQ(latchCell.at(
"parameters").at(
"EN_POLARITY"),
"1");
444 EXPECT_TRUE(findComponent<DLatch>(roundTrip(latch)));
446 auto dff = std::make_shared<DFlipFlop>(
447 std::make_shared<Wire>(), std::make_shared<Wire>(),
nullptr,
nullptr,
448 std::make_shared<Wire>(), std::make_shared<Wire>());
449 dff->setProperty(
"triggerEdge", std::string(
"NET"));
450 const auto dffDesign = exportComponent(dff);
451 const auto& dffCell = onlyCell(dffDesign);
452 EXPECT_EQ(dffCell.at(
"type"),
"SILICON_DFF");
453 EXPECT_EQ(dffCell.at(
"connections").size(), 4);
454 EXPECT_EQ(dffCell.at(
"parameters").at(
"CLK_POLARITY"),
"0");
455 const auto importedDff = roundTrip(dff);
456 ASSERT_TRUE(findComponent<DFlipFlop>(importedDff));
458 findComponent<DFlipFlop>(importedDff)->getPropertyValue<std::string>(
"triggerEdge"),
459 std::optional<std::string>(
"NET"));
461 auto dffsr = std::make_shared<DFlipFlop>(
462 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
463 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>());
464 EXPECT_EQ(onlyCell(exportComponent(dffsr)).at(
"type"),
"SILICON_DFFSR");
465 EXPECT_TRUE(findComponent<DFlipFlop>(roundTrip(dffsr)));
467 auto dffe = std::make_shared<EFlipFlop>(
468 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
469 nullptr,
nullptr, std::make_shared<Wire>(), std::make_shared<Wire>());
470 EXPECT_EQ(onlyCell(exportComponent(dffe)).at(
"type"),
"SILICON_DFFE");
471 EXPECT_TRUE(findComponent<EFlipFlop>(roundTrip(dffe)));
473 auto dffsre = std::make_shared<EFlipFlop>(
474 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
475 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
476 std::make_shared<Wire>());
477 EXPECT_EQ(onlyCell(exportComponent(dffsre)).at(
"type"),
"SILICON_DFFSRE");
478 EXPECT_TRUE(findComponent<EFlipFlop>(roundTrip(dffsre)));
480 auto jkff = std::make_shared<JKFlipFlop>(
481 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
482 nullptr,
nullptr, std::make_shared<Wire>(), std::make_shared<Wire>());
483 EXPECT_EQ(onlyCell(exportComponent(jkff)).at(
"type"),
"SILICON_JKFF");
484 EXPECT_TRUE(findComponent<JKFlipFlop>(roundTrip(jkff)));
486 auto halfAdder = std::make_shared<HalfAdder>(
487 std::array<Wire_ptr, 2>{std::make_shared<Wire>(), std::make_shared<Wire>()},
488 std::make_shared<Wire>(), std::make_shared<Wire>());
489 EXPECT_EQ(onlyCell(exportComponent(halfAdder)).at(
"type"),
"SILICON_HALF_ADDER");
490 EXPECT_TRUE(findComponent<HalfAdder>(roundTrip(halfAdder)));
492 auto fullAdder = std::make_shared<FullAdder>(
493 std::array<Wire_ptr, 2>{std::make_shared<Wire>(), std::make_shared<Wire>()},
494 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>());
495 EXPECT_EQ(onlyCell(exportComponent(fullAdder)).at(
"type"),
"SILICON_FULL_ADDER");
496 EXPECT_TRUE(findComponent<FullAdder>(roundTrip(fullAdder)));
498 auto adder = std::make_shared<AdderNBits>(std::array<Bus, 2>{
Bus(5),
Bus(5)},
Bus(5),
499 std::make_shared<Wire>());
500 const auto adderDesign = exportComponent(adder);
501 const auto& adderCell = onlyCell(adderDesign);
502 EXPECT_EQ(adderCell.at(
"type"),
"SILICON_ADDER");
503 EXPECT_EQ(adderCell.at(
"parameters").at(
"SIGNED"),
"0");
504 EXPECT_FALSE(adderCell.at(
"parameters").contains(
"A_SIGNED"));
505 EXPECT_FALSE(adderCell.at(
"parameters").contains(
"B_SIGNED"));
506 EXPECT_EQ(adderCell.at(
"connections").at(
"A").size(), 5);
507 const auto importedAdder = roundTrip(adder);
508 ASSERT_TRUE(findComponent<AdderNBits>(importedAdder));
509 EXPECT_EQ(findComponent<AdderNBits>(importedAdder)->getPropertyValue<int>(
"size"), 5);
511 for (
const auto& [mode, isSigned, cellType] :
512 std::array<std::tuple<std::string_view, bool, std::string_view>, 3>{
516 auto shifter = std::make_shared<Shifter>(
Bus(5),
Bus(3),
Bus(5));
517 shifter->setProperty(
"mode", std::string(mode));
518 shifter->setProperty(
"signed", isSigned);
519 const auto design = exportComponent(shifter);
520 EXPECT_EQ(onlyCell(design).at(
"type"), cellType);
521 const auto imported = findComponent<Shifter>(roundTrip(shifter));
522 ASSERT_TRUE(imported);
523 EXPECT_EQ(imported->getPropertyValue<std::string>(
"mode"), mode);
524 EXPECT_EQ(imported->getPropertyValue<
bool>(
"signed"), isSigned);
525 EXPECT_EQ(imported->getPropertyValue<
int>(
"size"), 5);
526 EXPECT_EQ(imported->getPropertyValue<
int>(
"amountSize"), 3);
529 struct RegisterMode {
532 std::string_view cellType;
534 static constexpr std::array REGISTER_MODES{
535 RegisterMode{
true,
true,
"SILICON_PIPO"},
536 RegisterMode{
true,
false,
"SILICON_PISO"},
537 RegisterMode{
false,
true,
"SILICON_SIPO"},
538 RegisterMode{
false,
false,
"SILICON_SISO"},
540 for (
const auto& mode : REGISTER_MODES) {
541 SCOPED_TRACE(mode.cellType);
542 const auto reg = registerWithMode(mode.parallelInput, mode.parallelOutput);
543 const auto registerDesign = exportComponent(reg);
544 const auto& registerCell = onlyCell(registerDesign);
545 EXPECT_EQ(registerCell.at(
"type"), mode.cellType);
546 EXPECT_EQ(registerCell.at(
"connections").at(
"DATA").size(),
547 mode.parallelInput ? 4 : 1);
548 EXPECT_EQ(registerCell.at(
"connections").at(
"OUT").size(),
549 mode.parallelOutput ? 4 : 1);
550 EXPECT_EQ(registerCell.at(
"connections").contains(
"LOAD"),
551 mode.parallelInput && !mode.parallelOutput);
552 EXPECT_EQ(registerCell.at(
"parameters").contains(
"LOAD_POLARITY"),
553 mode.parallelInput && !mode.parallelOutput);
554 EXPECT_FALSE(registerCell.at(
"parameters").contains(
"INPUT_PARALLEL"));
555 EXPECT_FALSE(registerCell.at(
"parameters").contains(
"OUTPUT_PARALLEL"));
557 const auto importedCircuit = roundTrip(reg);
558 const auto importedRegister = findComponent<Register>(importedCircuit);
559 ASSERT_TRUE(importedRegister);
560 EXPECT_EQ(importedRegister->getPropertyValue<
int>(
"size"), 4);
561 EXPECT_EQ(importedRegister->getPropertyValue<std::string>(
"inputType"),
564 EXPECT_EQ(importedRegister->getPropertyValue<std::string>(
"outputType"),
570TEST(YosysTest, RejectsMalformedCustomTechnologyCells)
572 auto component = std::make_shared<DFlipFlop>(
573 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>(),
574 std::make_shared<Wire>(), std::make_shared<Wire>(), std::make_shared<Wire>());
575 const auto reject = [](
const nlohmann::json& design) {
579 auto missingPort = exportComponent(component);
580 onlyModule(missingPort)[
"cells"].begin().value()[
"connections"].erase(
"D");
583 auto unexpectedPort = exportComponent(component);
584 onlyModule(unexpectedPort)[
"cells"].begin().value()[
"connections"][
"EXTRA"] =
585 nlohmann::json::array({2});
586 reject(unexpectedPort);
588 auto incorrectWidth = exportComponent(component);
589 onlyModule(incorrectWidth)[
"cells"].begin().value()[
"connections"][
"D"].push_back(3);
590 reject(incorrectWidth);
592 auto invalidBoolean = exportComponent(component);
593 onlyModule(invalidBoolean)[
"cells"].begin().value()[
"parameters"][
"CLK_POLARITY"] =
595 reject(invalidBoolean);
597 auto unsupportedPolarity = exportComponent(component);
598 onlyModule(unsupportedPolarity)[
"cells"].begin().value()[
"parameters"][
"SET_POLARITY"] =
600 reject(unsupportedPolarity);
603 std::make_shared<DLatch>(std::make_shared<Wire>(), std::make_shared<Wire>(),
604 std::make_shared<Wire>(), std::make_shared<Wire>());
605 auto unsupportedLatchPolarity = exportComponent(latch);
606 onlyModule(unsupportedLatchPolarity)[
"cells"].begin().value()[
"parameters"]
607 [
"EN_POLARITY"] =
"0";
608 reject(unsupportedLatchPolarity);
610 auto duplicateDriver = exportComponent(component);
611 auto& duplicateConnections =
612 onlyModule(duplicateDriver)[
"cells"].begin().value()[
"connections"];
613 duplicateConnections[
"QN"] = duplicateConnections[
"Q"];
614 reject(duplicateDriver);
616 auto adder = std::make_shared<AdderNBits>(std::array<Bus, 2>{
Bus(3),
Bus(3)},
Bus(3),
617 std::make_shared<Wire>());
618 auto malformedAdder = exportComponent(adder);
619 onlyModule(malformedAdder)[
"cells"].begin().value()[
"parameters"][
"WIDTH"] =
621 reject(malformedAdder);
623 auto missingRegisterLoad = exportComponent(registerWithMode(
true,
false));
624 onlyModule(missingRegisterLoad)[
"cells"].begin().value()[
"connections"].erase(
"LOAD");
625 reject(missingRegisterLoad);
627 auto unsupportedRegisterPolarity = exportComponent(registerWithMode(
false,
true));
628 onlyModule(unsupportedRegisterPolarity)[
"cells"].begin().value()[
"parameters"]
629 [
"EN_POLARITY"] =
"0";
630 reject(unsupportedRegisterPolarity);
632 auto unexpectedRegisterLoad = exportComponent(registerWithMode(
true,
true));
633 onlyModule(unexpectedRegisterLoad)[
"cells"].begin().value()[
"connections"][
"LOAD"] =
634 nlohmann::json::array({
"0"});
635 reject(unexpectedRegisterLoad);
637 EXPECT_THROW((
void)exportComponent(std::make_shared<HalfAdder>()), std::runtime_error);
647TEST(YosysTest, SharesOneSplitterAcrossIndexedBusConsumers)
654 {{
"attributes", Json::object()},
656 {{
"a", {{
"direction",
"input"}, {
"bits", Json::array({2, 3})}}},
657 {
"whole", {{
"direction",
"output"}, {
"bits", Json::array({2, 3})}}},
658 {
"low", {{
"direction",
"output"}, {
"bits", Json::array({2})}}},
659 {
"low_copy", {{
"direction",
"output"}, {
"bits", Json::array({2})}}},
660 {
"high", {{
"direction",
"output"}, {
"bits", Json::array({3})}}}}},
661 {
"cells", Json::object()},
662 {
"netnames", Json::object()}}}}}};
668 const auto input = findNamedComponent<DummyBusInputComponent>(circuit,
"a");
669 const auto whole = findNamedComponent<DummyBusOutputComponent>(circuit,
"whole");
670 const auto low = findNamedComponent<DummyOutputComponent>(circuit,
"low");
671 const auto lowCopy = findNamedComponent<DummyOutputComponent>(circuit,
"low_copy");
672 const auto high = findNamedComponent<DummyOutputComponent>(circuit,
"high");
673 const auto splitter = findComponent<WireSplitter>(circuit);
674 ASSERT_TRUE(input && whole && low && lowCopy && high && splitter);
675 ASSERT_EQ(splitter->outputBuses().size(), 2);
676 EXPECT_EQ(splitter->getPropertyValue<
int>(
"size"), 2);
677 EXPECT_EQ(splitter->inputBuses()[0], input->outputBuses()[0]);
678 EXPECT_EQ(whole->inputBuses()[0], input->outputBuses()[0]);
679 EXPECT_EQ(low->inputBuses()[0], splitter->outputBuses()[0]);
680 EXPECT_EQ(lowCopy->inputBuses()[0], splitter->outputBuses()[0]);
681 EXPECT_EQ(high->inputBuses()[0], splitter->outputBuses()[1]);
684TEST(YosysTest, SharesOneMergerAcrossIdenticalAssembledBuses)
691 {{
"attributes", Json::object()},
693 {{
"x", {{
"direction",
"input"}, {
"bits", Json::array({2})}}},
694 {
"y", {{
"direction",
"input"}, {
"bits", Json::array({3})}}},
695 {
"q", {{
"direction",
"output"}, {
"bits", Json::array({2, 3})}}},
696 {
"q_copy", {{
"direction",
"output"}, {
"bits", Json::array({2, 3})}}}}},
697 {
"cells", Json::object()},
698 {
"netnames", Json::object()}}}}}};
704 const auto x = findNamedComponent<DummyInputComponent>(circuit,
"x");
705 const auto y = findNamedComponent<DummyInputComponent>(circuit,
"y");
706 const auto q = findNamedComponent<DummyBusOutputComponent>(circuit,
"q");
707 const auto qCopy = findNamedComponent<DummyBusOutputComponent>(circuit,
"q_copy");
708 const auto merger = findComponent<WireMerger>(circuit);
709 ASSERT_TRUE(x && y && q && qCopy && merger);
710 ASSERT_EQ(merger->inputBuses().size(), 2);
711 EXPECT_EQ(merger->getPropertyValue<
int>(
"size"), 2);
712 EXPECT_EQ(merger->inputBuses()[0], x->outputBuses()[0]);
713 EXPECT_EQ(merger->inputBuses()[1], y->outputBuses()[0]);
714 EXPECT_EQ(q->inputBuses()[0], merger->outputBuses()[0]);
715 EXPECT_EQ(qCopy->inputBuses()[0], merger->outputBuses()[0]);
718TEST(YosysTest, NormalizesReorderedPartSelectThroughOneSplitterAndMerger)
725 {{
"attributes", Json::object()},
727 {{
"a", {{
"direction",
"input"}, {
"bits", Json::array({2, 3, 4, 5})}}},
728 {
"q", {{
"direction",
"output"}, {
"bits", Json::array({5, 2})}}}}},
729 {
"cells", Json::object()},
730 {
"netnames", Json::object()}}}}}};
736 const auto input = findNamedComponent<DummyBusInputComponent>(*circuit,
"a");
737 const auto output = findNamedComponent<DummyBusOutputComponent>(*circuit,
"q");
738 const auto splitter = findComponent<WireSplitter>(*circuit);
739 const auto merger = findComponent<WireMerger>(*circuit);
740 ASSERT_TRUE(input && output && splitter && merger);
741 ASSERT_EQ(splitter->outputBuses().size(), 2);
742 ASSERT_EQ(merger->inputBuses().size(), 2);
743 EXPECT_EQ(merger->inputBuses()[0], splitter->outputBuses()[1]);
744 EXPECT_EQ(merger->inputBuses()[1], splitter->outputBuses()[0]);
745 EXPECT_EQ(output->inputBuses()[0], merger->outputBuses()[0]);
747 input->setBusValue(
valueFor(input->outputBuses()[0], 8));
749 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
750 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(),
751 valueFor(output->inputBuses()[0], 1));
754TEST(YosysTest, SharesBusSlicesWithoutReconstructingThemFromIndividualBits)
757 Json ports = Json::object();
758 ports[
"a"] = {{
"direction",
"input"}, {
"bits", Json::array({2, 3, 4, 5, 6, 7, 8, 9})}};
759 ports[
"whole"] = {{
"direction",
"output"}, {
"bits", ports[
"a"][
"bits"]}};
760 ports[
"low"] = {{
"direction",
"output"}, {
"bits", Json::array({2, 3, 4, 5})}};
761 ports[
"low_copy"] = ports[
"low"];
762 ports[
"high"] = {{
"direction",
"output"}, {
"bits", Json::array({6, 7, 8, 9})}};
763 ports[
"overlap"] = {{
"direction",
"output"}, {
"bits", Json::array({4, 5, 6, 7})}};
764 ports[
"tap"] = {{
"direction",
"output"}, {
"bits", Json::array({5})}};
766 {
"modules", {{
"top", {{
"ports", ports}, {
"cells", Json::object()}}}}}};
771 const auto splitter = findComponent<WireSplitter>(*circuit);
772 ASSERT_TRUE(splitter);
773 EXPECT_EQ(splitter->outputSlices(), (std::vector<std::pair<unsigned int, unsigned int>>{
774 {0, 4}, {2, 4}, {3, 1}, {4, 4}}));
775 const auto low = findNamedComponent<DummyBusOutputComponent>(*circuit,
"low");
776 const auto copy = findNamedComponent<DummyBusOutputComponent>(*circuit,
"low_copy");
777 ASSERT_TRUE(low && copy);
778 EXPECT_EQ(low->inputBuses()[0], copy->inputBuses()[0]);
786#ifdef SILICON_TEST_YOSYS_EXECUTABLE
788 circuit = std::make_shared<Circuit>(importVerilog(
792 const auto input = findNamedComponent<DummyBusInputComponent>(*circuit,
"a");
795 for (
unsigned int value = 0;
value < 256; ++
value) {
796 simulator.setBus(input->outputBuses()[0],
valueFor(input->outputBuses()[0], value));
797 ASSERT_EQ(simulator.runUntilIdle(), Simulator::RunResult::Completed);
798 for (
const auto& [name, expected] :
799 std::map<std::string, unsigned int>{{
"low",
value & 15},
800 {
"low_copy",
value & 15},
801 {
"high",
value >> 4},
802 {
"overlap", (
value >> 2) & 15},
804 const auto output = findNamedComponent<DummyBusOutputComponent>(*circuit, name);
806 EXPECT_EQ(output->inputBuses()[0].getCurrentValue(),
807 valueFor(output->inputBuses()[0], expected));
809 const auto bit = findNamedComponent<DummyOutputComponent>(*circuit,
"tap");
811 EXPECT_EQ(bit->inputBuses()[0].getCurrentValue(),
812 valueFor(bit->inputBuses()[0], (value >> 3) & 1));
816TEST(YosysTest, ConcatenatesBusRunsAndPacksLiteralRuns)
820 {
"a", {{
"direction",
"input"}, {
"bits", Json::array({2, 3, 4, 5, 6, 7, 8, 9})}}},
821 {
"b", {{
"direction",
"input"}, {
"bits", Json::array({10, 11, 12, 13})}}},
823 {{
"direction",
"output"},
824 {
"bits", Json::array({6, 7, 8, 9,
"0",
"x",
"1",
"0", 10, 11, 12, 13})}}}};
826 {
"modules", {{
"top", {{
"ports", ports}, {
"cells", Json::object()}}}}}};
830 const auto merger = findComponent<WireMerger>(*circuit);
832 ASSERT_EQ(merger->inputBuses().size(), 3);
833 for (
const auto& input : merger->inputBuses())
834 EXPECT_EQ(input.size(), 4);
839 const auto a = findNamedComponent<DummyBusInputComponent>(*circuit,
"a");
840 const auto b = findNamedComponent<DummyBusInputComponent>(*circuit,
"b");
841 const auto q = findNamedComponent<DummyBusOutputComponent>(*circuit,
"q");
842 ASSERT_TRUE(a && b && q);
843 a->setBusValue(
valueFor(a->outputBuses()[0], 0xa5));
844 b->setBusValue(
valueFor(b->outputBuses()[0], 3));
846 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
847 EXPECT_EQ(q->inputBuses()[0].getCurrentValue(),
busValueFromBits(
"001101X01010"));
849#ifdef SILICON_TEST_YOSYS_EXECUTABLE
850 auto restored = std::make_shared<Circuit>(importVerilog(
852 const auto restoredA = findNamedComponent<DummyBusInputComponent>(*restored,
"a");
853 const auto restoredB = findNamedComponent<DummyBusInputComponent>(*restored,
"b");
854 const auto restoredQ = findNamedComponent<DummyBusOutputComponent>(*restored,
"q");
855 ASSERT_TRUE(restoredA && restoredB && restoredQ);
856 restoredA->setBusValue(
valueFor(restoredA->outputBuses()[0], 0xa5));
857 restoredB->setBusValue(
valueFor(restoredB->outputBuses()[0], 3));
859 ASSERT_EQ(restoredSimulator.
runUntilIdle(), Simulator::RunResult::Completed);
860 EXPECT_EQ(restoredQ->inputBuses()[0].getCurrentValue(),
865TEST(YosysTest, RecognizesSharedSignAndZeroExtensionOfBusSlices)
869 {
"a", {{
"direction",
"input"}, {
"bits", Json::array({2, 3, 4, 5})}}},
870 {
"signed_q", {{
"direction",
"output"}, {
"bits", Json::array({3, 4, 5, 5, 5, 5})}}},
872 {{
"direction",
"output"}, {
"bits", Json::array({3, 4, 5, 5, 5, 5})}}},
874 {{
"direction",
"output"}, {
"bits", Json::array({3, 4, 5,
"0",
"0",
"0"})}}}};
876 {
"modules", {{
"top", {{
"ports", ports}, {
"cells", Json::object()}}}}}};
881 const auto input = findNamedComponent<DummyBusInputComponent>(*circuit,
"a");
882 const auto signedQ = findNamedComponent<DummyBusOutputComponent>(*circuit,
"signed_q");
883 const auto copy = findNamedComponent<DummyBusOutputComponent>(*circuit,
"signed_copy");
884 const auto unsignedQ =
885 findNamedComponent<DummyBusOutputComponent>(*circuit,
"unsigned_q");
886 ASSERT_TRUE(input && signedQ && copy && unsignedQ);
887 EXPECT_EQ(signedQ->inputBuses()[0], copy->inputBuses()[0]);
889 for (
const auto& bits : {
"1010",
"0011",
"X010",
"E010"}) {
891 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
892 const auto value = input->outputBuses()[0].getCurrentValue();
894 unsignedQ->inputBuses()[0].getCurrentValue(),
895 (
BusValue{value[1], value[2], value[3], State::LOW, State::LOW, State::LOW}));
896 EXPECT_EQ(signedQ->inputBuses()[0].getCurrentValue(),
897 (
BusValue{value[1], value[2], value[3], value[3], value[3], value[3]}));
901TEST(YosysTest, ImportsGeneralCombinationalNetlistWithConstants)
906 const Json binaryParameters{
913 const Json unaryParameters{
922 {{
"attributes", Json::object()},
924 {{
"a", {{
"direction",
"input"}, {
"bits", Json::array({2, 3})}}},
925 {
"y", {{
"direction",
"output"}, {
"bits", Json::array({6, 7})}}}}},
929 {
"parameters", binaryParameters},
931 {{
"A", Json::array({2, 3})},
932 {
"B", Json::array({
"1",
"0"})},
933 {
"Y", Json::array({4, 5})}}}}},
936 {
"parameters", unaryParameters},
938 {{
"A", Json::array({4, 5})}, {
"Y", Json::array({6, 7})}}}}}}},
939 {
"netnames", Json::object()}}}}}};
943 (std::multiset<std::string>{
"AndGate",
"ConstantComponent",
944 "DummyBusInputComponent",
945 "DummyBusOutputComponent",
"NotGate"}));
946 auto importedConstant = findComponent<ConstantComponent>(imported);
947 ASSERT_TRUE(importedConstant);
948 EXPECT_EQ(importedConstant->getPropertyValue<
int>(
"size"), 2);
949 EXPECT_EQ(importedConstant->getPropertyValue<
BusValue>(
"value"),
951 const auto importedNot = findComponent<NotGate>(imported);
952 ASSERT_TRUE(importedNot);
953 EXPECT_EQ(importedNot->getPropertyValue<
int>(
"size"), 2);
959 ASSERT_TRUE(findComponent<NotGate>(restored));
960 EXPECT_EQ(findComponent<NotGate>(restored)->getPropertyValue<int>(
"size"), 2);
962 const auto reparsed =
964 EXPECT_TRUE(reparsed.is_object());
967#ifdef SILICON_TEST_YOSYS_EXECUTABLE
968 EXPECT_EQ(validateWithYosys(restored,
"import_constants"), 0);
972 std::shared_ptr<DummyBusInputComponent> inputComponent;
973 std::shared_ptr<DummyBusOutputComponent> outputComponent;
974 for (
const auto vertex :
975 boost::make_iterator_range(boost::vertices(simulated->getGraph()))) {
976 const auto& component = simulated->getGraph()[vertex].component;
977 if (
auto input = std::dynamic_pointer_cast<DummyBusInputComponent>(component))
978 inputComponent = std::move(input);
979 if (
auto output = std::dynamic_pointer_cast<DummyBusOutputComponent>(component))
980 outputComponent = std::move(output);
982 ASSERT_TRUE(inputComponent);
983 ASSERT_TRUE(outputComponent);
984 inputComponent->setBusValue(
valueFor(inputComponent->outputBuses()[0], 3));
986 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
987 EXPECT_EQ(outputComponent->inputBuses()[0].getCurrentValue(),
988 valueFor(outputComponent->inputBuses()[0], 2));
991TEST(YosysTest, ExportsArithmeticCellsAndPreservesComponentProperties)
993 for (
const bool sign : {
false,
true}) {
994 std::vector<Component_ptr> components{
995 std::make_shared<Multiplier>(std::array<Bus, 2>{
Bus(5),
Bus(5)},
Bus(5)),
996 std::make_shared<Divider>(std::array<Bus, 2>{
Bus(5),
Bus(5)},
Bus(5),
Bus(5))};
997 for (
const auto& component : components) {
998 component->setProperty(
"signed", sign);
999 component->setProperty(
"delay", 7);
1000 const auto design = exportComponent(component);
1002 EXPECT_EQ(cellTypes(onlyModule(design)),
1003 std::multiset<std::string>{multiplication ?
"SILICON_MULTIPLIER"
1004 :
"SILICON_DIVIDER"});
1005 for (
const auto& cell : onlyModule(design).at(
"cells")) {
1006 EXPECT_EQ(cell.at(
"parameters").at(
"SIGNED"), sign ?
"1" :
"0");
1007 EXPECT_FALSE(cell.at(
"parameters").contains(
"A_SIGNED"));
1008 EXPECT_FALSE(cell.at(
"parameters").contains(
"B_SIGNED"));
1010 cell.at(
"connections").at(multiplication ?
"PRODUCT" :
"div").size(), 5);
1013 EXPECT_EQ(
componentTypes(imported).count(multiplication ?
"Multiplier" :
"Divider"),
1015 const auto importedComponent =
1017 ? std::static_pointer_cast<Component>(findComponent<Multiplier>(imported))
1018 : std::static_pointer_cast<Component>(findComponent<Divider>(imported));
1019 ASSERT_TRUE(importedComponent);
1020 EXPECT_EQ(importedComponent->getPropertyValue<
bool>(
"signed"), sign);
1021 EXPECT_EQ(importedComponent->getPropertyValue<
int>(
"size"), 5);
1024 const auto circuit = circuitWithBoundaryPorts(component);
1026 const auto restoredComponent =
1028 ? std::static_pointer_cast<Component>(findComponent<Multiplier>(restored))
1029 : std::static_pointer_cast<Component>(findComponent<Divider>(restored));
1030 ASSERT_TRUE(restoredComponent);
1031 EXPECT_EQ(restoredComponent->getPropertyValue<
int>(
"size"), 5);
1032 EXPECT_EQ(restoredComponent->getPropertyValue<
int>(
"delay"), 7);
1033 EXPECT_EQ(restoredComponent->getPropertyValue<
bool>(
"signed"), sign);
1034#ifdef SILICON_TEST_YOSYS_EXECUTABLE
1035 EXPECT_EQ(validateWithYosys(circuit, multiplication ?
"multiplier" :
"divider"), 0);
1068TEST(YosysTest, RejectsCellsThatRequireTechnologyMapping)
1072 std::vector<Json> designs;
1073 for (
const auto type : {
"$mul",
"$div",
"$mod"}) {
1074 auto design = subtractionDesign(3, 3, 3,
false,
false);
1075 onlyModule(design)[
"cells"].begin().value()[
"type"] = type;
1076 designs.push_back(std::move(design));
1078 const auto wire = [] {
return std::make_shared<Wire>(); };
1079 const std::array<std::pair<Component_ptr, const char*>, 4> storage{
1081 std::make_shared<DFlipFlop>(wire(), wire(),
nullptr,
nullptr, wire(), wire()),
1084 std::make_shared<DFlipFlop>(wire(), wire(), wire(), wire(), wire(), wire()),
1086 std::pair{std::make_shared<EFlipFlop>(wire(), wire(), wire(), wire(), wire(),
1089 std::pair{std::make_shared<DLatch>(wire(), wire(), wire(), wire()),
"$dlatch"}};
1090 for (
const auto& [component, type] : storage) {
1091 auto design = exportComponent(component);
1092 auto& cell = onlyModule(design)[
"cells"].begin().value();
1093 cell[
"type"] = type;
1095 cell[
"connections"].erase(
"QN");
1096 cell[
"port_directions"].erase(
"QN");
1097 designs.push_back(std::move(design));
1099 for (
const auto& design : designs) {
1100 SCOPED_TRACE(onlyCell(design).at(
"type"));
1102#ifdef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
1103 EXPECT_THROW((
void)legalizeExpressions(design.dump(),
"silicon_validate"),
1104 std::runtime_error);