SILICON
Loading...
Searching...
No Matches
arithmetic.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <array>
21#include <string>
22#include <string_view>
23
24#include <core/component.hpp>
25#include <core/wire.hpp>
26
27namespace SILICON::extra {
28using namespace SILICON::core;
29
34class Extender : public Component {
35public:
36 static constexpr std::string_view Type = "Extender";
37 static constexpr std::string_view SIGNED_MODE = "signed";
38 static constexpr std::string_view UNSIGNED_MODE = "unsigned";
39
40 std::string_view typeName() const override { return Type; }
41 ComponentMetadata metadata() const override
42 {
43 return {"Extender", "Changes a bus width using signed or unsigned extension.",
45 }
46
47 Extender();
48 Extender(Bus in, Bus out, std::string mode = std::string(UNSIGNED_MODE));
49
50 int setInputSize(int width);
51 int setOutputSize(int width);
52
53 void simulate(SILICON::simulation::Simulator& sim) override;
54 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
55};
56
61class Complementer : public Component {
62public:
63 static constexpr std::string_view Type = "Complementer";
64
65 std::string_view typeName() const override { return Type; }
66 ComponentMetadata metadata() const override
67 {
68 return {"Two's complement", "Gives the two's complement of a binary number",
70 }
71
73 Complementer(Bus in, Bus out);
74 int setSize(int width);
75
76 void simulate(SILICON::simulation::Simulator& sim) override;
77 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
78};
79
87class HalfAdder : public Component {
88public:
90 static constexpr std::string_view Type = "HalfAdder";
91
96 std::string_view typeName() const override { return Type; }
97 ComponentMetadata metadata() const override
98 {
99 return {"Half Adder", "Adds two one-bit inputs and outputs sum and carry.",
101 }
102
104 enum class Inputs : unsigned int {
106 A = 0,
108 B = 1,
109 };
110
112 enum class Outputs : unsigned int {
114 Sum = 0,
116 Cout = 1,
117 };
118
120 HalfAdder();
121
128 HalfAdder(std::array<Wire_ptr, 2> inputs, Wire_ptr sum, Wire_ptr cout);
129
134 void simulate(SILICON::simulation::Simulator& sim) override;
135 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
136};
137
145class FullAdder : public Component {
146public:
148 static constexpr std::string_view Type = "FullAdder";
149
154 std::string_view typeName() const override { return Type; }
156 {
157 return {"Full Adder",
158 "Adds two one-bit inputs plus carry-in and outputs sum and carry.",
160 }
161
163 enum class Inputs : unsigned int {
165 A = 0,
167 B = 1,
169 Cin = 2,
170 };
171
173 enum class Outputs : unsigned int {
175 Sum = 0,
177 Cout = 1,
178 };
179
181 FullAdder();
182
190 FullAdder(std::array<Wire_ptr, 2> inputs, Wire_ptr cin, Wire_ptr sum, Wire_ptr cout);
191
196 void simulate(SILICON::simulation::Simulator& sim) override;
197 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
198};
199
208class AdderNBits : public Component {
209public:
211 static constexpr std::string_view Type = "AdderNBits";
212
217 std::string_view typeName() const override { return Type; }
219 {
220 return {"N-bit Adder", "Adds two fixed-width buses and outputs sum and carry.",
222 }
223
225 enum class Inputs : unsigned int {
227 A = 0,
229 B = 1,
230 };
231
233 enum class Outputs : unsigned int {
235 Sum = 0,
237 Cout = 1,
238 };
239
241 AdderNBits();
242
249 AdderNBits(std::array<Bus, 2> inputs, Bus sum, Wire_ptr cout);
250
255 void simulate(SILICON::simulation::Simulator& sim) override;
256 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
257
263 int setSize(int width);
264};
265
267class Shifter : public Component {
268public:
269 static constexpr std::string_view Type = "Shifter";
270 static constexpr std::string_view LEFT_MODE = "left";
271 static constexpr std::string_view RIGHT_MODE = "right";
272
273 enum class Inputs : unsigned int { Value = 0, Amount = 1 };
274 enum class Outputs : unsigned int { Result = 0 };
275
276 std::string_view typeName() const override { return Type; }
278 {
279 return {"Shifter", "Shifts a bus left or right by a variable amount.",
281 }
282
283 Shifter();
284 Shifter(Bus value, Bus amount, Bus result);
285
286 int setSize(int width);
287 int setAmountSize(int width);
288 void simulate(SILICON::simulation::Simulator& sim) override;
289 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
290};
291
293class Multiplier : public Component {
294public:
295 static constexpr std::string_view Type = "Multiplier";
296 enum class Inputs : unsigned int { A = 0, B = 1 };
297 enum class Outputs : unsigned int { Product = 0 };
298
299 std::string_view typeName() const override { return Type; }
301 {
302 return {"Multiplier", "Multiplies two fixed-width signed or unsigned buses.",
304 }
305
306 Multiplier();
307 Multiplier(std::array<Bus, 2> inputs, Bus product);
308 int setSize(int width);
309 void simulate(SILICON::simulation::Simulator& sim) override;
310 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
311};
312
316class Divider : public Component {
317public:
318 static constexpr std::string_view Type = "Divider";
319 enum class Inputs : unsigned int { A = 0, B = 1 };
320 enum class Outputs : unsigned int { Div = 0, Mod = 1 };
321
322 std::string_view typeName() const override { return Type; }
324 {
325 return {"Divider",
326 "Divides two fixed-width buses and outputs quotient and remainder.",
328 }
329
330 Divider();
331 Divider(std::array<Bus, 2> inputs, Bus quotient, Bus remainder);
332 int setSize(int width);
333 void simulate(SILICON::simulation::Simulator& sim) override;
334 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
335};
336
337class Comparator : public Component {
338
339public:
340 static constexpr std::string_view Type = "Comparator";
341
342 std::string_view typeName() const override { return Type; }
344 {
345 return {"Comparator", "Returns the result of a comparison between two values",
347 }
348
349 Comparator();
350 Comparator(std::array<Bus, 2> inputBuses, Wire_ptr output);
351
352 void simulate(SILICON::simulation::Simulator& sim) override;
353 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
354
355 int setSize(int width);
356 std::string setMode(std::string mode);
357
358private:
359 uint64_t propagationDelay = 0;
360};
361
362} // namespace SILICON::extra
const std::vector< Bus > & inputBuses() const
Gets all input buses without copying.
std::vector< Bus > inputs
Input buses connected to this component.
static constexpr std::string_view Type
Serialization/type-registry identifier.
Outputs
Output bus indices for AdderNBits.
@ Sum
Sum bus, least-significant bit first.
Inputs
Input bus indices for AdderNBits.
@ A
First addend bus, least-significant bit first.
@ B
Second addend bus, least-significant bit first.
int setSize(int width)
Resizes both inputs, the sum output, and the carry output.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the N-bit adder.
AdderNBits()
Constructs an unconnected N-bit adder.
static constexpr std::string_view Type
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
std::string setMode(std::string mode)
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
static constexpr std::string_view Type
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
static constexpr std::string_view Type
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
static constexpr std::string_view Type
int setOutputSize(int width)
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
static constexpr std::string_view UNSIGNED_MODE
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
int setInputSize(int width)
static constexpr std::string_view SIGNED_MODE
static constexpr std::string_view Type
Serialization/type-registry identifier.
Outputs
Output bus indices for FullAdder.
Inputs
Input bus indices for FullAdder.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the full-adder truth table.
FullAdder()
Constructs an unconnected full adder.
static constexpr std::string_view Type
Serialization/type-registry identifier.
Outputs
Output bus indices for HalfAdder.
Inputs
Input bus indices for HalfAdder.
HalfAdder()
Constructs an unconnected half adder.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the half-adder truth table.
static constexpr std::string_view Type
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
static constexpr std::string_view Type
static constexpr std::string_view RIGHT_MODE
static constexpr std::string_view LEFT_MODE
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Gets the component type name.
int setAmountSize(int width)
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
Mutable module-building interface passed to component serializers.
Definition netlist.hpp:174
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116