SILICON
Loading...
Searching...
No Matches
gates.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#include <array>
20#include <memory>
21#include <utility>
22
23#include <core/component.hpp>
24#include <core/wire.hpp>
25
26namespace SILICON::core {
27
38class Gate : public Component {
39private:
40 void initializeProperties(bool enableBitwiseProperties);
41 void configureIO(int size, bool bitwise);
42 std::size_t scalarInputCount = 2;
43
44public:
50 Gate(const std::vector<Wire_ptr>& inputs, Wire_ptr output);
51
55 Gate();
56
57protected:
62 explicit Gate(bool enableBitwiseProperties);
63
70 Gate(const std::vector<Wire_ptr>& inputs, Wire_ptr output,
71 bool enableBitwiseProperties);
72
77 int setSize(int width);
78};
79
80class AndGate : public Gate {
81public:
82 static constexpr std::string_view Type = "AndGate";
83 std::string_view typeName() const override { return Type; }
84 ComponentMetadata metadata() const override
85 {
86 return {"AND Gate", "Outputs HIGH only when every input is HIGH.",
88 }
89
90 AndGate() = default;
91 AndGate(const std::vector<Wire_ptr>& inputs, Wire_ptr output);
92 void simulate(SILICON::simulation::Simulator& sim) override;
93 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
94};
95
96class OrGate : public Gate {
97public:
98 static constexpr std::string_view Type = "OrGate";
99 std::string_view typeName() const override { return Type; }
101 {
102 return {"OR Gate", "Outputs HIGH when at least one input is HIGH.",
104 }
105
106 OrGate() = default;
107 OrGate(const std::vector<Wire_ptr>& inputs, Wire_ptr output);
108 void simulate(SILICON::simulation::Simulator& sim) override;
109 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
110};
111
112class NotGate : public Gate {
113private:
115
116public:
117 static constexpr std::string_view Type = "NotGate";
118 std::string_view typeName() const override { return Type; }
120 {
121 return {"NOT Gate", "Inverts an input signal or bus.", ComponentCategory::Gates};
122 }
123
124 NotGate();
125 NotGate(Wire_ptr input, Wire_ptr output);
126 void simulate(SILICON::simulation::Simulator& sim) override;
127 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
128};
129
130class NandGate : public Gate {
131public:
132 static constexpr std::string_view Type = "NandGate";
133 std::string_view typeName() const override { return Type; }
135 {
136 return {"NAND Gate", "Outputs the inverse of an AND operation.",
138 }
139
140 NandGate() = default;
141 NandGate(const std::vector<Wire_ptr>& inputs, Wire_ptr output);
142 void simulate(SILICON::simulation::Simulator& sim) override;
143 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
144};
145
146class NorGate : public Gate {
147public:
148 static constexpr std::string_view Type = "NorGate";
149 std::string_view typeName() const override { return Type; }
151 {
152 return {"NOR Gate", "Outputs the inverse of an OR operation.",
154 }
155
156 NorGate() = default;
157 NorGate(const std::vector<Wire_ptr>& inputs, Wire_ptr output);
158 void simulate(SILICON::simulation::Simulator& sim) override;
159 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
160};
161
162class XorGate : public Gate {
163public:
164 static constexpr std::string_view Type = "XorGate";
165 std::string_view typeName() const override { return Type; }
167 {
168 return {"XOR Gate", "Outputs HIGH when exactly one input is HIGH.",
170 }
171
172 XorGate() = default;
173 XorGate(const std::array<Wire_ptr, 2>& inputs, Wire_ptr output);
174 void simulate(SILICON::simulation::Simulator& sim) override;
175 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
176};
177
178} // namespace SILICON::core
static constexpr std::string_view Type
Definition gates.hpp:82
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:84
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.
Definition gates.hpp:83
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:191
std::vector< Bus > inputs
Input buses connected to this component.
void initializeProperties(bool enableBitwiseProperties)
Definition gates.cpp:112
int setSize(int width)
Resizes every input bus and the output bus to the same width.
Definition gates.cpp:166
Gate(const std::vector< Wire_ptr > &inputs, Wire_ptr output)
Constructs a gate with scalar I/O and bitwise properties enabled.
Definition gates.cpp:94
Gate()
Constructs an empty gate instance for factory/serialization usage.
Definition gates.cpp:87
void configureIO(int size, bool bitwise)
Definition gates.cpp:144
std::size_t scalarInputCount
Definition gates.hpp:42
static constexpr std::string_view Type
Definition gates.hpp:132
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:134
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.
Definition gates.hpp:133
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:259
static constexpr std::string_view Type
Definition gates.hpp:148
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:150
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.
Definition gates.hpp:149
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:275
static constexpr std::string_view Type
Definition gates.hpp:117
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:119
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.
Definition gates.hpp:118
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:232
void initializeNotProperties()
Definition gates.cpp:220
static constexpr std::string_view Type
Definition gates.hpp:98
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:100
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.
Definition gates.hpp:99
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:204
static constexpr std::string_view Type
Definition gates.hpp:164
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
Definition gates.hpp:166
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.
Definition gates.hpp:165
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition gates.cpp:288
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