70TEST(YosysMuxTest, CompactsRepeatedChoicesAndPreservesSelectionTable)
72#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
73 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
75 auto design = muxTreeDesign();
76 auto&
module = onlyModule(design);
77 module["cells"].clear();
78 const std::array<int, 8> choices{0, 1, 0, 2, 1, 0, 2, 3};
79 Json data = Json::array();
80 for (
const auto choice : choices) {
81 data.push_back(2 + 2 * choice);
82 data.push_back(3 + 2 * choice);
84 module["cells"]["table"] = {
87 {{"WIDTH", SILICON::yosys::SerializationContext::parameter(2)},
88 {"S_WIDTH", SILICON::yosys::SerializationContext::parameter(3)}}},
90 {{"A", data}, {"S", Json::array({10, 11, 12})}, {"Y", Json::array({24, 25})}}}};
91 auto circuit = std::make_shared<Circuit>(importMuxStructure(design));
92 auto mux = findComponent<Multiplexer>(*circuit);
94 ASSERT_EQ(mux->inputBuses().size(), 5);
95 EXPECT_EQ(mux->selectionTableIndices(),
96 (std::vector<unsigned short>{0, 1, 0, 2, 1, 0, 2, 3}));
97 EXPECT_EQ(mux->getPropertyValue<
int>(
"selectionSize"), 3);
98 circuit = nativeRoundTrip(*circuit);
99 mux = findComponent<Multiplexer>(*circuit);
101 ASSERT_EQ(mux->inputBuses().size(), 5);
106 onlyModule(exported).at(
"cells").begin().value().at(
"connections").at(
"A");
107 ASSERT_EQ(packed.size(), 16);
108 EXPECT_EQ(packed[0], packed[4]);
109 EXPECT_EQ(packed[1], packed[5]);
112 const auto s = findNamedComponent<DummyBusInputComponent>(*circuit,
"s");
113 const auto y = findNamedComponent<DummyBusOutputComponent>(*circuit,
"y");
116 for (
unsigned int selection = 0; selection < choices.size(); ++selection) {
118 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
119 EXPECT_EQ(y->inputBuses()[0].getCurrentValue(),
valueFor(2, choices[selection]));
121 for (
const State state : {State::UNKNOWN, State::ERROR}) {
122 simulator.
setBus(s->outputBuses()[0],
BusValue{State::LOW, state, State::HIGH});
123 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
124 EXPECT_EQ(y->inputBuses()[0].getCurrentValue(),
BusValue(2, state));
127#ifdef SILICON_TEST_YOSYS_EXECUTABLE
128 auto restored = std::make_shared<Circuit>(
130 setMuxData(restored);
131 const auto restoredS = findNamedComponent<DummyBusInputComponent>(*restored,
"s");
132 const auto restoredY = findNamedComponent<DummyBusOutputComponent>(*restored,
"y");
133 ASSERT_TRUE(restoredS && restoredY);
135 for (
unsigned int selection = 0; selection < choices.size(); ++selection) {
136 restoredSimulator.
setBus(restoredS->outputBuses()[0],
valueFor(3, selection));
137 ASSERT_EQ(restoredSimulator.
runUntilIdle(), Simulator::RunResult::Completed);
138 EXPECT_EQ(restoredY->inputBuses()[0].getCurrentValue(),
145 Json identical = Json::array();
146 for (
unsigned int lane = 0; lane < choices.size(); ++lane) {
147 identical.push_back(2);
148 identical.push_back(3);
150 module["cells"]["table"]["connections"]["A"] = identical;
151 auto aliased = std::make_shared<Circuit>(importMuxStructure(design));
152 const auto aliasedMux = findComponent<Multiplexer>(*aliased);
153 ASSERT_TRUE(aliasedMux);
154 ASSERT_EQ(aliasedMux->inputBuses().size(), 2);
155 const auto aliasedS = findNamedComponent<DummyBusInputComponent>(*aliased,
"s");
156 const auto aliasedY = findNamedComponent<DummyBusOutputComponent>(*aliased,
"y");
157 ASSERT_TRUE(aliasedS && aliasedY);
160 for (
const State state : {State::UNKNOWN, State::ERROR}) {
161 aliasedSimulator.
setBus(aliasedS->outputBuses()[0],
162 BusValue{state, State::LOW, State::LOW});
163 ASSERT_EQ(aliasedSimulator.
runUntilIdle(), Simulator::RunResult::Completed);
164 EXPECT_EQ(aliasedY->inputBuses()[0].getCurrentValue(),
BusValue(2, state));
168TEST(YosysMuxTest, CombinesBalancedTreesWithRootSelectorPrecedence)
170#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
171 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
173 auto circuit = std::make_shared<Circuit>(importMuxStructure(muxTreeDesign()));
175 const auto mux = findComponent<Multiplexer>(*circuit);
177 EXPECT_EQ(mux->getPropertyValue<
int>(
"selectionSize"), 2);
178 circuit = nativeRoundTrip(*circuit);
180 const auto s = findNamedComponent<DummyBusInputComponent>(*circuit,
"s");
181 const auto y = findNamedComponent<DummyBusOutputComponent>(*circuit,
"y");
184 constexpr std::array states{State::LOW, State::HIGH, State::UNKNOWN, State::ERROR};
185 for (
const auto root : states)
186 for (
const auto child : states) {
187 simulator.
setBus(s->outputBuses()[0],
BusValue{child, root, State::LOW});
188 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
189 const auto expected = root == State::UNKNOWN || root == State::ERROR
191 : child == State::UNKNOWN || child == State::ERROR
193 :
valueFor(2, (root == State::HIGH ? 2 : 0)
194 + (child == State::HIGH ? 1 : 0));
195 EXPECT_EQ(y->inputBuses()[0].getCurrentValue(), expected);
199TEST(YosysMuxTest, KeepsPriorityChainsIndependentSelectorsAndSharedOutputs)
201#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
202 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
204 auto priority = muxTreeDesign();
205 auto&
module = onlyModule(priority);
206 module["cells"].erase("right");
207 module["cells"]["root"]["connections"]["B"] = Json::array({8, 9});
208 auto circuit = std::make_shared<Circuit>(importMuxStructure(priority));
211 const auto s = findNamedComponent<DummyBusInputComponent>(*circuit,
"s");
212 const auto y = findNamedComponent<DummyBusOutputComponent>(*circuit,
"y");
216 simulator.
setBus(s->outputBuses()[0],
217 BusValue{State::UNKNOWN, State::HIGH, State::LOW});
218 ASSERT_EQ(simulator.
runUntilIdle(), Simulator::RunResult::Completed);
219 EXPECT_EQ(y->inputBuses()[0].getCurrentValue(),
valueFor(2, 3));
221 auto independent = muxTreeDesign();
222 onlyModule(independent)[
"cells"][
"right"][
"connections"][
"S"] = Json::array({12});
223 EXPECT_EQ(
componentTypes(importMuxStructure(independent)).count(
"Multiplexer"), 3);
225 auto shared = muxTreeDesign();
226 onlyModule(shared)[
"ports"][
"tap"] = {{
"direction",
"output"},
227 {
"bits", Json::array({20, 21})}};
228 EXPECT_EQ(
componentTypes(importMuxStructure(shared)).count(
"Multiplexer"), 3);
230 auto feedback = muxTreeDesign();
231 onlyModule(feedback)[
"cells"][
"left"][
"connections"][
"A"] = Json::array({24, 25});
232 EXPECT_EQ(
componentTypes(importMuxStructure(feedback)).count(
"Multiplexer"), 3);
235TEST(YosysMuxTest, PassRespectsSelectionKeepAndRepeatedRuns)
237#ifndef SILICON_TEST_YOSYS_PLUGIN_AVAILABLE
238 GTEST_SKIP() <<
"The SILICON Yosys plugin is unavailable";
240 auto source = muxTreeDesign();
242 onlyModule(source)[
"cells"][
"right"][
"connections"][
"A"] = Json::array({2, 3});
243 const auto first = Json::parse(legalizeExpressions(source.dump(),
"silicon_mux"));
244 ASSERT_EQ(onlyModule(first).at(
"cells").size(), 1);
245 EXPECT_EQ(onlyCell(first).at(
"type"),
"SILICON_MUX");
246 EXPECT_EQ(Json::parse(legalizeExpressions(first.dump(),
"silicon_mux")).at(
"modules"),
247 first.at(
"modules"));
248 const auto selected = Json::parse(
249 legalizeExpressions(source.dump(),
"select top/root\nsilicon_mux\nselect *"));
250 EXPECT_EQ(onlyModule(selected).at(
"cells").size(), 3);
253 onlyModule(kept)[
"cells"][
"left"][
"attributes"][
"keep"] =
"1";
254 EXPECT_EQ(
componentTypes(importMuxStructure(kept)).count(
"Multiplexer"), 3);
256 onlyModule(kept)[
"netnames"][
"left_alias"] = {
257 {
"hide_name", 0}, {
"bits", Json::array({20, 21})}, {
"attributes", {{
"keep",
"1"}}}};
258 EXPECT_EQ(
componentTypes(importMuxStructure(kept)).count(
"Multiplexer"), 3);
260 for (
const auto table : {
"0",
"11111111",
"00000000",
"000001x0"}) {
261 auto malformed = first;
262 onlyModule(malformed)[
"cells"][
"root"][
"parameters"][
"TABLE"] = table;
265 auto malformed = first;
266 onlyModule(malformed)[
"cells"][
"root"][
"connections"][
"A"] = Json::array({2, 3});
static std::string parameter(std::uint64_t value, std::size_t width=32)
Encode an unsigned Yosys parameter as a fixed-width binary string.