1use std::time::Duration;
2
3use crate::{
4 chip::{ChipBuilder, ChipRunner, ChipSet, Pin, PinId, PinType},
5 generate_chip, State,
6};
7
8#[derive(Debug, Clone, Default)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub struct OrGate {
25 pub vcc: Pin,
26 pub gnd: Pin,
27 pub a: Pin,
28 pub b: Pin,
29 pub ab: Pin,
30 pub c: Pin,
31 pub d: Pin,
32 pub cd: Pin,
33 pub e: Pin,
34 pub f: Pin,
35 pub ef: Pin,
36 pub g: Pin,
37 pub h: Pin,
38 pub gh: Pin,
39}
40
41impl OrGate {
42 pub const VCC: PinId = 14;
43 pub const GND: PinId = 7;
44 pub const A: PinId = 1;
45 pub const B: PinId = 2;
46 pub const AB: PinId = 3;
47 pub const C: PinId = 4;
48 pub const D: PinId = 5;
49 pub const CD: PinId = 6;
50 pub const E: PinId = 13;
51 pub const F: PinId = 12;
52 pub const EF: PinId = 11;
53 pub const G: PinId = 10;
54 pub const H: PinId = 9;
55 pub const GH: PinId = 8;
56}
57
58impl ChipBuilder<ChipSet> for OrGate {
59 fn build() -> ChipSet {
60 ChipSet::OrGate(OrGate {
61 vcc: Pin::from(PinType::Input),
62 gnd: Pin::from(PinType::Output),
63 a: Pin::from(PinType::Input),
64 b: Pin::from(PinType::Input),
65 ab: Pin::from(PinType::Output),
66 c: Pin::from(PinType::Input),
67 d: Pin::from(PinType::Input),
68 cd: Pin::from(PinType::Output),
69 e: Pin::from(PinType::Input),
70 f: Pin::from(PinType::Input),
71 ef: Pin::from(PinType::Output),
72 g: Pin::from(PinType::Input),
73 h: Pin::from(PinType::Input),
74 gh: Pin::from(PinType::Output),
75 })
76 }
77}
78
79generate_chip!(
80 OrGate,
81 vcc: OrGate::VCC,
82 gnd: OrGate::GND,
83 a: OrGate::A,
84 b: OrGate::B,
85 ab: OrGate::AB,
86 c: OrGate::C,
87 d: OrGate::D,
88 cd: OrGate::CD,
89 e: OrGate::E,
90 f: OrGate::F,
91 ef: OrGate::EF,
92 g: OrGate::G,
93 h: OrGate::H,
94 gh: OrGate::GH
95);
96
97impl ChipRunner for OrGate {
98 fn run(&mut self, _: Duration) {
99 if self.vcc.state.as_logic(3.3) == State::High {
100 self.gnd.state = State::Low;
101 self.ab.state =
102 State::from(self.a.state.as_logic(3.3).into() || self.b.state.as_logic(3.3).into());
103 self.cd.state =
104 State::from(self.c.state.as_logic(3.3).into() || self.d.state.as_logic(3.3).into());
105 self.ef.state =
106 State::from(self.e.state.as_logic(3.3).into() || self.f.state.as_logic(3.3).into());
107 self.gh.state =
108 State::from(self.g.state.as_logic(3.3).into() || self.h.state.as_logic(3.3).into());
109 }
110 }
111}
112
113#[derive(Debug, Clone, Default)]
128#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
129pub struct ThreeInputOrGate {
130 pub vcc: Pin,
131 pub gnd: Pin,
132 pub a: Pin,
133 pub b: Pin,
134 pub c: Pin,
135 pub abc: Pin,
136 pub d: Pin,
137 pub e: Pin,
138 pub f: Pin,
139 pub def: Pin,
140 pub g: Pin,
141 pub h: Pin,
142 pub i: Pin,
143 pub ghi: Pin,
144}
145
146impl ThreeInputOrGate {
147 pub const VCC: PinId = 14;
148 pub const GND: PinId = 7;
149 pub const A: PinId = 1;
150 pub const B: PinId = 2;
151 pub const C: PinId = 13;
152 pub const ABC: PinId = 12;
153 pub const D: PinId = 3;
154 pub const E: PinId = 4;
155 pub const F: PinId = 5;
156 pub const DEF: PinId = 6;
157 pub const G: PinId = 11;
158 pub const H: PinId = 10;
159 pub const I: PinId = 9;
160 pub const GHI: PinId = 8;
161}
162
163impl ChipBuilder<ChipSet> for ThreeInputOrGate {
164 fn build() -> ChipSet {
165 ChipSet::ThreeInputOrGate(ThreeInputOrGate {
166 vcc: Pin::from(PinType::Input),
167 gnd: Pin::from(PinType::Output),
168 a: Pin::from(PinType::Input),
169 b: Pin::from(PinType::Input),
170 c: Pin::from(PinType::Input),
171 abc: Pin::from(PinType::Output),
172 d: Pin::from(PinType::Input),
173 e: Pin::from(PinType::Input),
174 f: Pin::from(PinType::Input),
175 def: Pin::from(PinType::Output),
176 g: Pin::from(PinType::Input),
177 h: Pin::from(PinType::Input),
178 i: Pin::from(PinType::Input),
179 ghi: Pin::from(PinType::Output),
180 })
181 }
182}
183
184generate_chip!(
185 ThreeInputOrGate,
186 vcc: ThreeInputOrGate::VCC,
187 gnd: ThreeInputOrGate::GND,
188 a: ThreeInputOrGate::A,
189 b: ThreeInputOrGate::B,
190 c: ThreeInputOrGate::C,
191 abc: ThreeInputOrGate::ABC,
192 d: ThreeInputOrGate::D,
193 e: ThreeInputOrGate::E,
194 f: ThreeInputOrGate::F,
195 def: ThreeInputOrGate::DEF,
196 g: ThreeInputOrGate::G,
197 h: ThreeInputOrGate::H,
198 i: ThreeInputOrGate::I,
199 ghi: ThreeInputOrGate::GHI
200);
201
202impl ChipRunner for ThreeInputOrGate {
203 fn run(&mut self, _: Duration) {
204 if self.vcc.state.as_logic(3.3) == State::High {
205 self.gnd.state = State::Low;
206 self.abc.state = State::from(
207 self.a.state.as_logic(3.3).into()
208 || self.b.state.as_logic(3.3).into()
209 || self.c.state.as_logic(3.3).into(),
210 );
211 self.def.state = State::from(
212 self.d.state.as_logic(3.3).into()
213 || self.e.state.as_logic(3.3).into()
214 || self.f.state.as_logic(3.3).into(),
215 );
216 self.ghi.state = State::from(
217 self.g.state.as_logic(3.3).into()
218 || self.h.state.as_logic(3.3).into()
219 || self.i.state.as_logic(3.3).into(),
220 );
221 }
222 }
223}