1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use super::super::State;
use super::{Pin, PinType, Chip};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
pub struct GateOr {
pin: [Rc<RefCell<Pin>>; 14],
}
impl Default for GateOr {
fn default() -> Self {
Self::new()
}
}
impl GateOr {
pub const A: u8 = 1;
pub const B: u8 = 2;
pub const A_OR_B: u8 = 3;
pub const C: u8 = 4;
pub const D: u8 = 5;
pub const C_OR_D: u8 = 6;
pub const E: u8 = 13;
pub const F: u8 = 12;
pub const E_OR_F: u8 = 11;
pub const G: u8 = 10;
pub const H: u8 = 9;
pub const G_OR_H: u8 = 8;
pub const VCC: u8 = 14;
pub const GND: u8 = 7;
pub fn new() -> Self {
GateOr {
pin: [
Rc::new(RefCell::new(Pin::new(1, PinType::Input))),
Rc::new(RefCell::new(Pin::new(2, PinType::Input))),
Rc::new(RefCell::new(Pin::new(3, PinType::Output))),
Rc::new(RefCell::new(Pin::new(4, PinType::Input))),
Rc::new(RefCell::new(Pin::new(5, PinType::Input))),
Rc::new(RefCell::new(Pin::new(6, PinType::Output))),
Rc::new(RefCell::new(Pin::new(7, PinType::Input))),
Rc::new(RefCell::new(Pin::new(8, PinType::Output))),
Rc::new(RefCell::new(Pin::new(9, PinType::Input))),
Rc::new(RefCell::new(Pin::new(10, PinType::Input))),
Rc::new(RefCell::new(Pin::new(11, PinType::Output))),
Rc::new(RefCell::new(Pin::new(12, PinType::Input))),
Rc::new(RefCell::new(Pin::new(13, PinType::Input))),
Rc::new(RefCell::new(Pin::new(14, PinType::Input)))
]
}
}
}
impl Chip for GateOr {
fn get_pin_qty(&self) -> u8 {
14
}
fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
if pin > 0 && pin <= 14 {
Ok(self.pin[pin as usize-1].clone())
} else {
Err("Pin out of bounds")
}
}
fn run(&mut self, _: std::time::Duration) {
if self.pin[6].borrow().state == State::Low && self.pin[13].borrow().state == State::High {
self.pin[2].borrow_mut().state = if self.pin[0].borrow().state == State::High || self.pin[1].borrow().state == State::High {State::High} else {State::Low};
self.pin[5].borrow_mut().state = if self.pin[3].borrow().state == State::High || self.pin[4].borrow().state == State::High {State::High} else {State::Low};
self.pin[10].borrow_mut().state = if self.pin[11].borrow().state == State::High || self.pin[12].borrow().state == State::High {State::High} else {State::Low};
self.pin[7].borrow_mut().state = if self.pin[8].borrow().state == State::High || self.pin[9].borrow().state == State::High {State::High} else {State::Low};
} else {
for i in 0..14 {
self.pin[i].borrow_mut().state = State::Low
}
}
}
}
#[derive(Debug)]
pub struct GateAnd {
pin: [Rc<RefCell<Pin>>; 14],
}
impl Default for GateAnd {
fn default() -> Self {
Self::new()
}
}
impl GateAnd {
pub const A: u8 = 1;
pub const B: u8 = 2;
pub const A_AND_B: u8 = 3;
pub const C: u8 = 4;
pub const D: u8 = 5;
pub const C_AND_D: u8 = 6;
pub const E: u8 = 13;
pub const F: u8 = 12;
pub const E_AND_F: u8 = 11;
pub const G: u8 = 10;
pub const H: u8 = 9;
pub const G_AND_H: u8 = 8;
pub const VCC: u8 = 14;
pub const GND: u8 = 7;
pub fn new() -> Self {
GateAnd {
pin: [
Rc::new(RefCell::new(Pin::new(1, PinType::Input))),
Rc::new(RefCell::new(Pin::new(2, PinType::Input))),
Rc::new(RefCell::new(Pin::new(3, PinType::Output))),
Rc::new(RefCell::new(Pin::new(4, PinType::Input))),
Rc::new(RefCell::new(Pin::new(5, PinType::Input))),
Rc::new(RefCell::new(Pin::new(6, PinType::Output))),
Rc::new(RefCell::new(Pin::new(7, PinType::Input))),
Rc::new(RefCell::new(Pin::new(8, PinType::Output))),
Rc::new(RefCell::new(Pin::new(9, PinType::Input))),
Rc::new(RefCell::new(Pin::new(10, PinType::Input))),
Rc::new(RefCell::new(Pin::new(11, PinType::Output))),
Rc::new(RefCell::new(Pin::new(12, PinType::Input))),
Rc::new(RefCell::new(Pin::new(13, PinType::Input))),
Rc::new(RefCell::new(Pin::new(14, PinType::Input)))
]
}
}
}
impl Chip for GateAnd {
fn get_pin_qty(&self) -> u8 {
14
}
fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
if pin > 0 && pin <= 14 {
Ok(self.pin[pin as usize-1].clone())
} else {
Err("Pin out of bounds")
}
}
fn run(&mut self, _: std::time::Duration) {
if self.pin[6].borrow().state == State::Low && self.pin[13].borrow().state == State::High {
self.pin[2].borrow_mut().state = if self.pin[0].borrow().state == State::High && self.pin[1].borrow().state == State::High {State::High} else {State::Low};
self.pin[5].borrow_mut().state = if self.pin[3].borrow().state == State::High && self.pin[4].borrow().state == State::High {State::High} else {State::Low};
self.pin[10].borrow_mut().state = if self.pin[11].borrow().state == State::High && self.pin[12].borrow().state == State::High {State::High} else {State::Low};
self.pin[7].borrow_mut().state = if self.pin[8].borrow().state == State::High && self.pin[9].borrow().state == State::High {State::High} else {State::Low};
} else {
for i in 0..14 {
self.pin[i].borrow_mut().state = State::Undefined
}
}
}
}
#[derive(Debug)]
pub struct GateNot {
pin: [Rc<RefCell<Pin>>; 14],
}
impl Default for GateNot {
fn default() -> Self {
Self::new()
}
}
impl GateNot {
pub const A: u8 = 1;
pub const NOT_A: u8 = 2;
pub const B: u8 = 3;
pub const NOT_B: u8 = 4;
pub const C: u8 = 5;
pub const NOT_C: u8 = 6;
pub const D: u8 = 13;
pub const NOT_D: u8 = 12;
pub const E: u8 = 11;
pub const NOT_E: u8 = 10;
pub const F: u8 = 9;
pub const NOT_F: u8 = 8;
pub const VCC: u8 = 14;
pub const GND: u8 = 7;
pub fn new() -> Self {
GateNot {
pin: [
Rc::new(RefCell::new(Pin::new(1, PinType::Input))),
Rc::new(RefCell::new(Pin::new(2, PinType::Output))),
Rc::new(RefCell::new(Pin::new(3, PinType::Input))),
Rc::new(RefCell::new(Pin::new(4, PinType::Output))),
Rc::new(RefCell::new(Pin::new(5, PinType::Input))),
Rc::new(RefCell::new(Pin::new(6, PinType::Output))),
Rc::new(RefCell::new(Pin::new(7, PinType::Input))),
Rc::new(RefCell::new(Pin::new(8, PinType::Output))),
Rc::new(RefCell::new(Pin::new(9, PinType::Input))),
Rc::new(RefCell::new(Pin::new(10, PinType::Output))),
Rc::new(RefCell::new(Pin::new(11, PinType::Input))),
Rc::new(RefCell::new(Pin::new(12, PinType::Output))),
Rc::new(RefCell::new(Pin::new(13, PinType::Input))),
Rc::new(RefCell::new(Pin::new(14, PinType::Input)))
]
}
}
}
impl Chip for GateNot {
fn get_pin_qty(&self) -> u8 {
14
}
fn get_pin(&mut self, pin: u8) -> Result<Rc<RefCell<Pin>>, &str> {
if pin > 0 && pin <= 14 {
Ok(self.pin[pin as usize-1].clone())
} else {
Err("Pin out of bounds")
}
}
fn run(&mut self, _: std::time::Duration) {
if self.pin[6].borrow().state == State::Low && self.pin[13].borrow().state == State::High {
self.pin[1].borrow_mut().state = if self.pin[0].borrow().state == State::High {State::Low} else {State::High};
self.pin[3].borrow_mut().state = if self.pin[2].borrow().state == State::High {State::Low} else {State::High};
self.pin[5].borrow_mut().state = if self.pin[4].borrow().state == State::High {State::Low} else {State::High};
self.pin[11].borrow_mut().state = if self.pin[12].borrow().state == State::High {State::Low} else {State::High};
self.pin[9].borrow_mut().state = if self.pin[10].borrow().state == State::High {State::Low} else {State::High};
self.pin[7].borrow_mut().state = if self.pin[8].borrow().state == State::High {State::Low} else {State::High};
} else {
for i in 0..14 {
self.pin[i].borrow_mut().state = State::Undefined
}
}
}
}