SILICON
Loading...
Searching...
No Matches
wire.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 <atomic>
20#include <compare>
21#include <cstdint>
22#include <format>
23#include <memory>
24#include <set>
25#include <string>
26#include <vector>
27
28namespace SILICON::core {
29
30/* --- State values ---------------------------------------------------------------------
31 * Each wire could hold one of four states. UNKNOWN can be HIGH or LOW, for example
32 * HIGH || UNKNOWN == HIGH, cause (HIGH || HIGH) == (HIGH || LOW) == HIGH.
33 *
34 * UNKNOWN may also be assigned by users to model an indeterminate input. ERROR is
35 * reserved for simulation failures and conflicting drivers. */
36
37enum class State : unsigned char {
38 LOW = '0',
39 HIGH = '1',
40 UNKNOWN = 'X',
41 ERROR = 'E',
42};
43
44State operator&&(const State& a, const State& b);
45State operator||(const State& a, const State& b);
46State operator^(const State& a, const State& b);
47State operator!(const State& a);
48
49std::string to_str(State s);
50
51class Component;
52using Component_weakPtr = std::weak_ptr<Component>;
53using Component_ptr = std::shared_ptr<Component>;
54using Component_set = std::set<Component_ptr, std::owner_less<Component_ptr>>;
55
56class Wire {
57private:
58 static inline std::atomic<uint64_t> nextId{0};
59
61 std::atomic<State> currentState;
63 uint64_t id;
64
65public:
66 Wire();
67 explicit Wire(State s);
68 Wire(uint64_t id, State s);
69
76 Wire(const Wire& other);
77
83 Wire& operator=(const Wire& other);
84
89 [[nodiscard]] State getCurrentState() const;
90 [[nodiscard]] uint64_t getId() const { return id; }
91
92 void forceSetCurrentState(State newState);
93
102
108 void forceSetCurrentState(State newState, const Component_weakPtr& authorizedBy);
109 void setCurrentState(State newState, const Component_weakPtr& requestedBy);
110
111 static void safeSetCurrentState(const std::weak_ptr<Wire>& w, State newState,
112 const Component_weakPtr& requestedBy);
113 static State safeGetCurrentState(const std::weak_ptr<Wire>& w);
114};
115
116using Wire_ptr = std::shared_ptr<Wire>;
117
118enum class Signedness : bool { UNSIGNED = false, SIGNED = true };
119
120using BusValue = std::vector<State>;
121
122BusValue operator+(const BusValue& a, const BusValue& b);
123[[nodiscard]] BusValue twosComplement(const BusValue& n);
124
125[[nodiscard]] std::partial_ordering compare(const BusValue& lhs, const BusValue& rhs,
126 Signedness signedness = Signedness::UNSIGNED);
127
128[[nodiscard]] BusValue shift(const BusValue& v, ptrdiff_t amount,
129 Signedness signedness = Signedness::UNSIGNED);
130
131[[nodiscard]] BusValue multiply(const BusValue& a, const BusValue& b,
132 Signedness signedness = Signedness::UNSIGNED);
133
134[[nodiscard]] std::pair<BusValue, BusValue>
135divide(const BusValue& a, const BusValue& b,
136 Signedness signedness = Signedness::UNSIGNED);
137
138class Bus {
139private:
140 std::vector<Wire_ptr> busData;
141
142public:
143 Bus() = default;
144 explicit Bus(unsigned short size);
145 explicit Bus(std::vector<Wire_ptr> busData);
146 Bus(std::initializer_list<Wire> initList);
147 Bus(std::initializer_list<Wire_ptr> initList);
148
149 void setSize(unsigned short size);
150
151 bool forceSetCurrentValue(const BusValue& value);
152
159 [[nodiscard]] bool forceSetCurrentValue(const BusValue& value,
160 const Component_weakPtr& authorizedBy);
161 [[nodiscard]] bool setCurrentValue(const BusValue& value,
162 const Component_weakPtr& requestedBy);
163
164 [[nodiscard]] BusValue getCurrentValue() const;
165 [[nodiscard]] bool isInErrorState() const;
166 [[nodiscard]] bool hasUnknowns() const;
167 [[nodiscard]] bool sharesWireWith(const Bus& other) const;
168
169 const Wire_ptr& operator[](const unsigned short index) const
170 { return this->busData.at(index); }
171
172 Wire_ptr& operator[](const unsigned short index) { return this->busData.at(index); }
173
174 explicit operator std::vector<Wire_ptr>() const { return this->busData; }
175 explicit operator std::vector<Wire_ptr>() { return this->busData; }
176
177 auto begin() { return this->busData.begin(); }
178 auto end() { return this->busData.end(); }
179
180 [[nodiscard]] auto begin() const { return this->busData.begin(); }
181 [[nodiscard]] auto end() const { return this->busData.end(); }
182
183 [[nodiscard]] auto size() { return this->busData.size(); }
184 [[nodiscard]] auto size() const { return this->busData.size(); }
185
186 bool operator==(const Bus& other) const;
187 std::strong_ordering operator<=>(const Bus& other) const;
188};
189
190} // namespace SILICON::core
bool setCurrentValue(const BusValue &value, const Component_weakPtr &requestedBy)
Definition wire.cpp:516
bool hasUnknowns() const
Definition wire.cpp:545
auto size() const
Definition wire.hpp:184
const Wire_ptr & operator[](const unsigned short index) const
Definition wire.hpp:169
bool forceSetCurrentValue(const BusValue &value)
Definition wire.cpp:487
bool operator==(const Bus &other) const
Definition wire.cpp:582
Wire_ptr & operator[](const unsigned short index)
Definition wire.hpp:172
std::vector< Wire_ptr > busData
Definition wire.hpp:140
bool isInErrorState() const
Definition wire.cpp:538
bool sharesWireWith(const Bus &other) const
Definition wire.cpp:552
void setSize(unsigned short size)
Definition wire.cpp:463
auto begin() const
Definition wire.hpp:180
auto end() const
Definition wire.hpp:181
std::strong_ordering operator<=>(const Bus &other) const
Definition wire.cpp:566
BusValue getCurrentValue() const
Definition wire.cpp:530
State getCurrentState() const
Reads the current state safely while simulation may update it.
Definition wire.cpp:194
static State safeGetCurrentState(const std::weak_ptr< Wire > &w)
Definition wire.cpp:245
void clearAuthorizedComponent()
Clears the component currently authorized to drive this wire.
Definition wire.cpp:206
uint64_t getId() const
Definition wire.hpp:90
static std::atomic< uint64_t > nextId
Definition wire.hpp:58
Component_weakPtr authorizedComponent
Definition wire.hpp:62
void setCurrentState(State newState, const Component_weakPtr &requestedBy)
Definition wire.cpp:218
static void safeSetCurrentState(const std::weak_ptr< Wire > &w, State newState, const Component_weakPtr &requestedBy)
Definition wire.cpp:237
std::atomic< State > currentState
State shared between the simulation worker and UI readers.
Definition wire.hpp:61
Wire & operator=(const Wire &other)
Assigns state, authorization, and identifier from another wire.
Definition wire.cpp:183
void forceSetCurrentState(State newState)
Definition wire.cpp:201
std::shared_ptr< Component > Component_ptr
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
BusValue shift(const BusValue &v, const ptrdiff_t amount, const Signedness signedness)
Definition wire.cpp:367
State operator!(const State &a)
Definition wire.cpp:55
BusValue operator+(const BusValue &a, const BusValue &b)
Definition wire.cpp:251
std::partial_ordering compare(const BusValue &lhs, const BusValue &rhs, const Signedness signedness)
Definition wire.cpp:289
State operator||(const State &a, const State &b)
Definition wire.cpp:44
std::pair< BusValue, BusValue > divide(const BusValue &a, const BusValue &b, Signedness signedness)
Definition wire.cpp:425
std::set< Component_ptr, std::owner_less< Component_ptr > > Component_set
Definition wire.hpp:54
State operator&&(const State &a, const State &b)
Definition wire.cpp:33
State operator^(const State &a, const State &b)
Definition wire.cpp:66
BusValue twosComplement(const BusValue &n)
Definition wire.cpp:272
std::weak_ptr< Component > Component_weakPtr
Definition wire.hpp:52
std::string to_str(State s)
Definition wire.cpp:77
BusValue multiply(const BusValue &a, const BusValue &b, const Signedness signedness)
Definition wire.cpp:388
std::vector< State > BusValue
Definition wire.hpp:120