SILICON
Toggle main menu visibility
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
27
namespace
SILICON::extra
{
28
using namespace
SILICON::core;
29
34
class
Extender
:
public
Component
{
35
public
:
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."
,
44
ComponentCategory::Arithmetic
};
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
61
class
Complementer
:
public
Component
{
62
public
:
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"
,
69
ComponentCategory::Arithmetic
};
70
}
71
72
Complementer
();
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
87
class
HalfAdder
:
public
Component
{
88
public
:
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."
,
100
ComponentCategory::Arithmetic
};
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
145
class
FullAdder
:
public
Component
{
146
public
:
148
static
constexpr
std::string_view
Type
=
"FullAdder"
;
149
154
std::string_view
typeName
()
const override
{
return
Type
; }
155
ComponentMetadata
metadata
()
const override
156
{
157
return
{
"Full Adder"
,
158
"Adds two one-bit inputs plus carry-in and outputs sum and carry."
,
159
ComponentCategory::Arithmetic
};
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
208
class
AdderNBits
:
public
Component
{
209
public
:
211
static
constexpr
std::string_view
Type
=
"AdderNBits"
;
212
217
std::string_view
typeName
()
const override
{
return
Type
; }
218
ComponentMetadata
metadata
()
const override
219
{
220
return
{
"N-bit Adder"
,
"Adds two fixed-width buses and outputs sum and carry."
,
221
ComponentCategory::Arithmetic
};
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
267
class
Shifter
:
public
Component
{
268
public
:
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
; }
277
ComponentMetadata
metadata
()
const override
278
{
279
return
{
"Shifter"
,
"Shifts a bus left or right by a variable amount."
,
280
ComponentCategory::Arithmetic
};
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
293
class
Multiplier
:
public
Component
{
294
public
:
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
; }
300
ComponentMetadata
metadata
()
const override
301
{
302
return
{
"Multiplier"
,
"Multiplies two fixed-width signed or unsigned buses."
,
303
ComponentCategory::Arithmetic
};
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
316
class
Divider
:
public
Component
{
317
public
:
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
; }
323
ComponentMetadata
metadata
()
const override
324
{
325
return
{
"Divider"
,
326
"Divides two fixed-width buses and outputs quotient and remainder."
,
327
ComponentCategory::Arithmetic
};
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
337
class
Comparator
:
public
Component
{
338
339
public
:
340
static
constexpr
std::string_view
Type
=
"Comparator"
;
341
342
std::string_view
typeName
()
const override
{
return
Type
; }
343
ComponentMetadata
metadata
()
const override
344
{
345
return
{
"Comparator"
,
"Returns the result of a comparison between two values"
,
346
ComponentCategory::Arithmetic
};
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
358
private
:
359
uint64_t
propagationDelay
= 0;
360
};
361
362
}
// namespace SILICON::extra
SILICON::core::Bus
Definition
wire.hpp:138
SILICON::core::Component::inputBuses
const std::vector< Bus > & inputBuses() const
Gets all input buses without copying.
Definition
component.hpp:577
SILICON::core::Component::inputs
std::vector< Bus > inputs
Input buses connected to this component.
Definition
component.hpp:213
SILICON::core::Component::Component
Component()=default
SILICON::extra::AdderNBits::Type
static constexpr std::string_view Type
Serialization/type-registry identifier.
Definition
arithmetic.hpp:211
SILICON::extra::AdderNBits::Outputs
Outputs
Output bus indices for AdderNBits.
Definition
arithmetic.hpp:233
SILICON::extra::AdderNBits::Outputs::Cout
@ Cout
Carry-out bit.
Definition
arithmetic.hpp:237
SILICON::extra::AdderNBits::Outputs::Sum
@ Sum
Sum bus, least-significant bit first.
Definition
arithmetic.hpp:235
SILICON::extra::AdderNBits::Inputs
Inputs
Input bus indices for AdderNBits.
Definition
arithmetic.hpp:225
SILICON::extra::AdderNBits::Inputs::A
@ A
First addend bus, least-significant bit first.
Definition
arithmetic.hpp:227
SILICON::extra::AdderNBits::Inputs::B
@ B
Second addend bus, least-significant bit first.
Definition
arithmetic.hpp:229
SILICON::extra::AdderNBits::setSize
int setSize(int width)
Resizes both inputs, the sum output, and the carry output.
Definition
arithmetic.cpp:263
SILICON::extra::AdderNBits::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:218
SILICON::extra::AdderNBits::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:378
SILICON::extra::AdderNBits::typeName
std::string_view typeName() const override
Returns the component type name.
Definition
arithmetic.hpp:217
SILICON::extra::AdderNBits::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the N-bit adder.
Definition
arithmetic.cpp:286
SILICON::extra::AdderNBits::AdderNBits
AdderNBits()
Constructs an unconnected N-bit adder.
Definition
arithmetic.cpp:232
SILICON::extra::Comparator::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:340
SILICON::extra::Comparator::setSize
int setSize(int width)
Definition
arithmetic.cpp:596
SILICON::extra::Comparator::propagationDelay
uint64_t propagationDelay
Definition
arithmetic.hpp:359
SILICON::extra::Comparator::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:343
SILICON::extra::Comparator::setMode
std::string setMode(std::string mode)
SILICON::extra::Comparator::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:476
SILICON::extra::Comparator::Comparator
Comparator()
Definition
arithmetic.cpp:515
SILICON::extra::Comparator::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:342
SILICON::extra::Comparator::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:549
SILICON::extra::Complementer::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:63
SILICON::extra::Complementer::setSize
int setSize(int width)
Definition
arithmetic.cpp:139
SILICON::extra::Complementer::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:66
SILICON::extra::Complementer::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:321
SILICON::extra::Complementer::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:65
SILICON::extra::Complementer::Complementer
Complementer()
Definition
arithmetic.cpp:109
SILICON::extra::Complementer::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:159
SILICON::extra::Divider::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:318
SILICON::extra::Divider::Outputs
Outputs
Definition
arithmetic.hpp:320
SILICON::extra::Divider::Outputs::Div
@ Div
Definition
arithmetic.hpp:320
SILICON::extra::Divider::Outputs::Mod
@ Mod
Definition
arithmetic.hpp:320
SILICON::extra::Divider::Inputs
Inputs
Definition
arithmetic.hpp:319
SILICON::extra::Divider::Inputs::A
@ A
Definition
arithmetic.hpp:319
SILICON::extra::Divider::Inputs::B
@ B
Definition
arithmetic.hpp:319
SILICON::extra::Divider::setSize
int setSize(int width)
Definition
arithmetic.cpp:473
SILICON::extra::Divider::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:323
SILICON::extra::Divider::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:424
SILICON::extra::Divider::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:322
SILICON::extra::Divider::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:492
SILICON::extra::Divider::Divider
Divider()
Definition
arithmetic.cpp:448
SILICON::extra::Extender::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:36
SILICON::extra::Extender::setOutputSize
int setOutputSize(int width)
Definition
arithmetic.cpp:80
SILICON::extra::Extender::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:41
SILICON::extra::Extender::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:294
SILICON::extra::Extender::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:40
SILICON::extra::Extender::UNSIGNED_MODE
static constexpr std::string_view UNSIGNED_MODE
Definition
arithmetic.hpp:38
SILICON::extra::Extender::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:93
SILICON::extra::Extender::Extender
Extender()
Definition
arithmetic.cpp:34
SILICON::extra::Extender::setInputSize
int setInputSize(int width)
Definition
arithmetic.cpp:67
SILICON::extra::Extender::SIGNED_MODE
static constexpr std::string_view SIGNED_MODE
Definition
arithmetic.hpp:37
SILICON::extra::FullAdder::Type
static constexpr std::string_view Type
Serialization/type-registry identifier.
Definition
arithmetic.hpp:148
SILICON::extra::FullAdder::Outputs
Outputs
Output bus indices for FullAdder.
Definition
arithmetic.hpp:173
SILICON::extra::FullAdder::Outputs::Cout
@ Cout
Carry-out bit.
Definition
arithmetic.hpp:177
SILICON::extra::FullAdder::Outputs::Sum
@ Sum
Sum bit.
Definition
arithmetic.hpp:175
SILICON::extra::FullAdder::Inputs
Inputs
Input bus indices for FullAdder.
Definition
arithmetic.hpp:163
SILICON::extra::FullAdder::Inputs::A
@ A
First addend bit.
Definition
arithmetic.hpp:165
SILICON::extra::FullAdder::Inputs::B
@ B
Second addend bit.
Definition
arithmetic.hpp:167
SILICON::extra::FullAdder::Inputs::Cin
@ Cin
Carry-in bit.
Definition
arithmetic.hpp:169
SILICON::extra::FullAdder::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:155
SILICON::extra::FullAdder::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:359
SILICON::extra::FullAdder::typeName
std::string_view typeName() const override
Returns the component type name.
Definition
arithmetic.hpp:154
SILICON::extra::FullAdder::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the full-adder truth table.
Definition
arithmetic.cpp:216
SILICON::extra::FullAdder::FullAdder
FullAdder()
Constructs an unconnected full adder.
Definition
arithmetic.cpp:198
SILICON::extra::HalfAdder::Type
static constexpr std::string_view Type
Serialization/type-registry identifier.
Definition
arithmetic.hpp:90
SILICON::extra::HalfAdder::Outputs
Outputs
Output bus indices for HalfAdder.
Definition
arithmetic.hpp:112
SILICON::extra::HalfAdder::Outputs::Cout
@ Cout
Carry-out bit.
Definition
arithmetic.hpp:116
SILICON::extra::HalfAdder::Outputs::Sum
@ Sum
Sum bit.
Definition
arithmetic.hpp:114
SILICON::extra::HalfAdder::Inputs
Inputs
Input bus indices for HalfAdder.
Definition
arithmetic.hpp:104
SILICON::extra::HalfAdder::Inputs::A
@ A
First addend bit.
Definition
arithmetic.hpp:106
SILICON::extra::HalfAdder::Inputs::B
@ B
Second addend bit.
Definition
arithmetic.hpp:108
SILICON::extra::HalfAdder::HalfAdder
HalfAdder()
Constructs an unconnected half adder.
Definition
arithmetic.cpp:169
SILICON::extra::HalfAdder::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:97
SILICON::extra::HalfAdder::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:344
SILICON::extra::HalfAdder::typeName
std::string_view typeName() const override
Returns the component type name.
Definition
arithmetic.hpp:96
SILICON::extra::HalfAdder::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Evaluates the half-adder truth table.
Definition
arithmetic.cpp:185
SILICON::extra::Multiplier::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:295
SILICON::extra::Multiplier::Outputs
Outputs
Definition
arithmetic.hpp:297
SILICON::extra::Multiplier::Outputs::Product
@ Product
Definition
arithmetic.hpp:297
SILICON::extra::Multiplier::Inputs
Inputs
Definition
arithmetic.hpp:296
SILICON::extra::Multiplier::Inputs::A
@ A
Definition
arithmetic.hpp:296
SILICON::extra::Multiplier::Inputs::B
@ B
Definition
arithmetic.hpp:296
SILICON::extra::Multiplier::setSize
int setSize(int width)
Definition
arithmetic.cpp:443
SILICON::extra::Multiplier::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:300
SILICON::extra::Multiplier::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:405
SILICON::extra::Multiplier::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:299
SILICON::extra::Multiplier::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:478
SILICON::extra::Multiplier::Multiplier
Multiplier()
Definition
arithmetic.cpp:418
SILICON::extra::Shifter::Type
static constexpr std::string_view Type
Definition
arithmetic.hpp:269
SILICON::extra::Shifter::Outputs
Outputs
Definition
arithmetic.hpp:274
SILICON::extra::Shifter::Outputs::Result
@ Result
Definition
arithmetic.hpp:274
SILICON::extra::Shifter::Inputs
Inputs
Definition
arithmetic.hpp:273
SILICON::extra::Shifter::Inputs::Value
@ Value
Definition
arithmetic.hpp:273
SILICON::extra::Shifter::Inputs::Amount
@ Amount
Definition
arithmetic.hpp:273
SILICON::extra::Shifter::RIGHT_MODE
static constexpr std::string_view RIGHT_MODE
Definition
arithmetic.hpp:271
SILICON::extra::Shifter::LEFT_MODE
static constexpr std::string_view LEFT_MODE
Definition
arithmetic.hpp:270
SILICON::extra::Shifter::setSize
int setSize(int width)
Definition
arithmetic.cpp:339
SILICON::extra::Shifter::Shifter
Shifter()
Definition
arithmetic.cpp:306
SILICON::extra::Shifter::metadata
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition
arithmetic.hpp:277
SILICON::extra::Shifter::serializeYosys
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
Definition
components.cpp:448
SILICON::extra::Shifter::typeName
std::string_view typeName() const override
Gets the component type name.
Definition
arithmetic.hpp:276
SILICON::extra::Shifter::setAmountSize
int setAmountSize(int width)
Definition
arithmetic.cpp:356
SILICON::extra::Shifter::simulate
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition
arithmetic.cpp:368
SILICON::simulation::Simulator
Executes event-driven simulation of a Circuit.
Definition
simulator.hpp:74
SILICON::yosys::SerializationContext
Mutable module-building interface passed to component serializers.
Definition
netlist.hpp:174
component.hpp
SILICON::core::Wire_ptr
std::shared_ptr< Wire > Wire_ptr
Definition
wire.hpp:116
SILICON::core::ComponentCategory::Arithmetic
@ Arithmetic
Definition
component.hpp:50
SILICON::extra
Definition
components.cpp:288
SILICON::core::ComponentMetadata
Definition
component.hpp:72
wire.hpp
src
extraComponents
arithmetic.hpp
Generated by
1.17.0