22#include <initializer_list>
35[[nodiscard]]
inline Json
36directions(std::initializer_list<std::pair<std::string_view, std::string_view>> values)
38 Json result = Json::object();
39 for (
const auto& [port, direction] : values)
40 result[std::string(port)] = direction;
45 const std::size_t index)
48 if (index >= buses.size() || buses[index].size() == 0) {
49 throw std::runtime_error(
50 std::format(
"Cannot export '{}': {} bus {} is missing or empty",
51 component.
typeName(), input ?
"input" :
"output", index));
57 const std::size_t bWidth,
58 const std::size_t yWidth)
68 const std::string_view type,
const Json& input,
const Json& output)
75 Json{{
"A", input}, {
"Y", output}});
79 const std::string_view type,
const Json& lhs,
const Json& rhs,
83 directions({{
"A",
"input"}, {
"B",
"input"}, {
"Y",
"output"}}),
84 Json{{
"A", lhs}, {
"B", rhs}, {
"Y", output}});
88 const std::string_view type,
const Json& lhs,
const Json& rhs,
91 if (lhs.size() != rhs.size() || lhs.size() != output.size())
92 throw std::runtime_error(
"Cannot export fine binary cell with mismatched widths");
94 for (std::size_t bit = 0; bit < output.size(); ++bit) {
95 context.
addCell(std::format(
"{}_{}", suffix, bit), type, Json::object(),
96 directions({{
"A",
"input"}, {
"B",
"input"}, {
"Y",
"output"}}),
97 Json{{
"A", Json::array({lhs.at(bit)})},
98 {
"B", Json::array({rhs.at(bit)})},
99 {
"Y", Json::array({output.at(bit)})}});
108 const std::string_view operation,
const bool invert)
111 const auto& output =
requireBus(component,
false, 0);
113 throw std::runtime_error(
114 std::format(
"Cannot export '{}' without inputs", component.
typeName()));
116 const std::size_t width = output.size();
117 for (std::size_t index = 0; index < inputs.size(); ++index) {
118 if (inputs[index].size() != width) {
119 throw std::runtime_error(
120 std::format(
"Cannot export '{}': input {} width does not match output width",
128 if (invert && inputs.size() == 2) {
129 const auto fineType =
130 operation ==
"$and" ? std::string_view{
"$_NAND_"} : std::string_view{
"$_NOR_"};
131 emitFineBinary(context, operation ==
"$and" ?
"nand" :
"nor", fineType,
132 context.
bits(inputs[0]), context.
bits(inputs[1]),
133 context.
bits(output));
137 Json accumulator = context.
bits(inputs.front());
138 if (inputs.size() == 1) {
139 emitUnary(context, invert ?
"invert" :
"buffer", invert ?
"$not" :
"$pos",
140 accumulator, context.
bits(output));
144 for (std::size_t index = 1; index < inputs.size(); ++index) {
145 const bool writesOutput = index + 1 == inputs.size() && !invert;
147 emitBinary(context, std::format(
"fold_{}", index), operation, accumulator,
148 context.
bits(inputs[index]), next);
149 accumulator = std::move(next);
152 emitUnary(context,
"invert",
"$not", accumulator, context.
bits(output));
161 const std::string_view operation)
164 const auto& output =
requireBus(component,
false, 0);
165 if (inputs.size() < 2 || output.size() != 1) {
166 throw std::runtime_error(std::format(
167 "Cannot export logical '{}': expected at least two inputs and one output bit",
172 for (std::size_t index = 1; index < inputs.size(); ++index) {
173 const bool writesOutput = index + 1 == inputs.size();
175 emitBinary(context, std::format(
"logical_fold_{}", index), operation, accumulator,
177 accumulator = std::move(next);
Base class for all digital circuit components.
const std::vector< Bus > & inputBuses() const
Gets all input buses without copying.
virtual std::string_view typeName() const =0
Gets the component type name.
const std::vector< Bus > & outputBuses() const
Gets all output buses without copying.
Mutable module-building interface passed to component serializers.
Json bits(const core::Bus &bus, std::string_view nullValue="x") const
Convert a Silicon bus to a Yosys least-significant-bit-first vector.
Json allocateBits(std::size_t width)
Allocate module-local temporary signal bits.
static std::string parameter(std::uint64_t value, std::size_t width=32)
Encode an unsigned Yosys parameter as a fixed-width binary string.
void addCell(std::string_view suffix, std::string_view type, Json parameters, Json portDirections, Json connections, Json attributes=Json::object())
Emit one native Yosys cell for the current Silicon component.
void emitGateFold(SerializationContext &context, const Component &component, const std::string_view operation, const bool invert)
Json unsignedParameters(const std::size_t aWidth, const std::size_t bWidth, const std::size_t yWidth)
void emitBinary(SerializationContext &context, const std::string_view suffix, const std::string_view type, const Json &lhs, const Json &rhs, const Json &output)
void emitFineBinary(SerializationContext &context, const std::string_view suffix, const std::string_view type, const Json &lhs, const Json &rhs, const Json &output)
Json directions(std::initializer_list< std::pair< std::string_view, std::string_view > > values)
const Bus & requireBus(const Component &component, const bool input, const std::size_t index)
void emitLogicalGateFold(SerializationContext &context, const Component &component, const std::string_view operation)
void emitUnary(SerializationContext &context, const std::string_view suffix, const std::string_view type, const Json &input, const Json &output)
nlohmann::ordered_json Json