sim_wire.h Source File

yasimavr: sim_wire.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_wire.h
Go to the documentation of this file.
1/*
2 * sim_wire.h
3 *
4 * Copyright 2024 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_WIRE_H__
25#define __YASIMAVR_WIRE_H__
26
27#include "sim_types.h"
28#include "sim_signal.h"
29
31
32
33//=======================================================================================
34
56
57public:
58
63 enum StateEnum {
64 //The enum values are partially a bitset (using StateFlag values) :
65 //bit 0 indicates that it is a stable digital state if set
66 //bit 1 is the boolean value (only if bit 0 is set too)
67 //bit 2 indicates a 'driver' state if set or 'weak' if clear
68 //'Weak' states
69 State_Floating = 0x00,
70 State_PullUp = 0x03,
71 State_PullDown = 0x01,
72 //'Driver' states
73 State_Analog = 0x04,
74 State_High = 0x07,
75 State_Low = 0x05,
76 //Special states
77 State_Shorted = 0x80,
78 State_Error = 0x90
79 };
80
81
83
84 public:
85
86 inline state_t() : m_value(State_Floating), m_level(0.5) {}
87 constexpr state_t(StateEnum s, double v = 0.0) : m_value(s), m_level(normalised_level(s, v)) {}
88 state_t(const state_t& other) = default;
89
90 inline bool is_digital() const { return m_value & 0x01; }
91 inline bool is_driven() const { return m_value & 0x04; }
92
93 inline bool digital_value() const { return (m_value & 0x01) ? (m_value & 0x02) : (m_level > 0.5); }
94
95 std::string to_string() const;
96
97 inline bool operator==(StateEnum s) const { return m_value == s; }
98 inline bool operator!=(StateEnum s) const { return m_value != s; }
99 inline bool operator==(const state_t& s) const
100 {
101 return (s.m_value == m_value) && ((m_value != State_Analog) || (s.m_level == m_level));
102 }
103 inline bool operator!=(const state_t& s) const { return !(s == *this); }
104
105 inline StateEnum value() const { return m_value; }
106 inline double level() const { return m_level; }
107
108 state_t& operator=(const state_t& other) = default;
109
110 private:
111
112 StateEnum m_value;
113 double m_level;
114
115 constexpr static double normalised_level(StateEnum s, double v)
116 {
117 //If the state is analog, trim the voltage to the correct range
118 if (s == State_Analog) {
119 if (v < 0.0) return 0.0;
120 if (v > 1.0) return 1.0;
121 else return v;
122 }
123 //If the state is digital, ensure the voltage level is consistent with the
124 //digital level
125 else if (s & 0x01) {
126 return (s & 0x02) ? 1.0 : 0.0;
127 }
128 //Other cases : force to the default value
129 else {
130 return 0.5;
131 }
132 }
133
134 };
135
147 static StateEnum char2state(char c);
148
149
154 enum SignalId {
159 Signal_StateChange = 0,
160
166
173 };
174
175
176 Wire();
177 Wire(Wire& other);
178 Wire(const Wire& other);
179 virtual ~Wire();
180
181 DataSignal& signal();
182
183 const state_t& state() const;
184 bool digital_state() const;
185 double voltage() const;
186
187 void set_state(const state_t& state);
188 void set_state(StateEnum state, double v = 0.0);
189 void set_state(char state, double v = 0.0);
190
191 void attach(Wire& other);
192 void detach();
193 bool attached(const Wire& other) const;
194 bool attached() const;
195 std::vector<Wire*> siblings() const;
196
197 Wire& operator=(Wire& other);
198 Wire& operator=(const Wire& other);
199
200 static state_t resolve_two_states(const state_t& a, const state_t& b);
201
202protected:
203
204 void auto_resolve_state();
205
206 virtual void notify_digital_state(bool state);
207 virtual state_t state_for_resolution() const;
208
209private:
210
211 state_t m_state;
212 state_t m_resolved_state;
213 Wire* m_primary;
214 std::vector<Wire*> m_secondaries;
215 DataSignal m_signal;
216
217 void resolve_state();
218 void set_resolved_state(const state_t& s);
219 void notify_resolved_state();
220
221};
222
226inline const Wire::state_t& Wire::state() const
227{
228 return m_resolved_state;
229};
230
234inline double Wire::voltage() const
235{
236 return m_resolved_state.level();
237}
238
244inline void Wire::set_state(StateEnum state, double level)
245{
246 set_state(state_t(state, level));
247}
248
255inline void Wire::set_state(char state, double level)
256{
258}
259
264{
265 return m_signal;
266}
267
268
270
271#endif //__YASIMAVR_WIRE_H__
Definition sim_signal.h:137
Definition sim_wire.h:82
bool operator!=(const state_t &s) const
Definition sim_wire.h:103
bool operator!=(StateEnum s) const
Definition sim_wire.h:98
double level() const
Definition sim_wire.h:106
bool is_driven() const
Definition sim_wire.h:91
bool digital_value() const
Definition sim_wire.h:93
state_t & operator=(const state_t &other)=default
bool operator==(StateEnum s) const
Definition sim_wire.h:97
constexpr state_t(StateEnum s, double v=0.0)
Definition sim_wire.h:87
state_t(const state_t &other)=default
StateEnum value() const
Definition sim_wire.h:105
bool is_digital() const
Definition sim_wire.h:90
bool operator==(const state_t &s) const
Definition sim_wire.h:99
state_t()
Definition sim_wire.h:86
General Purpose wire model.
Definition sim_wire.h:55
void set_state(const state_t &state)
Definition sim_wire.cpp:254
const state_t & state() const
Definition sim_wire.h:226
static StateEnum char2state(char c)
Definition sim_wire.cpp:65
double voltage() const
Definition sim_wire.h:234
StateEnum
Definition sim_wire.h:63
SignalId
Definition sim_wire.h:154
@ Signal_VoltageChange
Definition sim_wire.h:172
@ Signal_DigitalChange
Definition sim_wire.h:165
DataSignal & signal()
Definition sim_wire.h:263
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59