logicgate/
lib.rs

1///逻辑门
2pub trait LogicGate {
3    fn get_result(&self) -> Option<bool>;
4}
5
6
7
8///与非门 全为true则为false
9/// # Examples
10/// ```
11/// use logicgate::Nand;
12/// use logicgate::LogicGate;
13/// let a = Nand{input1: &Some(true), input2: &Some(false)};
14/// assert_eq!(a.get_result(),Some(true))
15/// ```
16#[derive(PartialEq)]
17pub struct Nand<'a> {
18    pub input1: &'a Option<bool>,
19    pub input2: &'a Option<bool>,
20}
21
22
23impl<'a> LogicGate for Nand<'a> {
24
25    fn get_result(&self) -> Option<bool> {
26	if *self.input1 == Some(true){
27	    if *self.input2 == Some(true) {
28		return Some(false);
29	    }
30	}
31	Some(true)
32    }
33}
34
35///非门 反转真值表
36///非门由一个输入一致的与非门组成
37/// # Examples
38/// ```
39/// use logicgate::Not;
40/// use logicgate::LogicGate;
41/// let a = Not{input: &Some(true)};
42/// let b = Not{input: &Some(false)};
43/// assert_eq!(a.get_result(),Some(false));
44/// assert_eq!(b.get_result(),Some(true));
45/// ```
46pub struct Not<'a> {
47    pub input: &'a Option<bool>,
48}
49
50
51impl<'a> LogicGate for Not<'a> {
52    fn get_result(&self) -> Option<bool> {
53	let a = Nand{input1: &self.input, input2: &self.input};
54	a.get_result()
55    }
56}
57///或门 有true则为true
58///或门由三个与非门组成
59/// # Examples
60/// ```
61/// use logicgate::Or;
62/// use logicgate::LogicGate;
63/// let a = Or{input1: &Some(true),input2: &Some(false)};
64/// let b = Or{input1: &Some(false),input2: &Some(false)};
65/// assert_eq!(a.get_result(),Some(true));
66/// assert_eq!(b.get_result(),Some(false));
67/// ```
68pub struct Or<'a> {
69    pub input1: &'a Option<bool>,
70    pub input2: &'a Option<bool>,
71}
72
73impl<'a> LogicGate for Or<'a> {
74    fn get_result(&self) -> Option<bool> {
75	let a = Nand{input1: self.input1, input2: self.input1};
76	let b = Nand{input1: self.input2, input2: self.input2};
77	let c = Nand{input1: &a.get_result(),input2: &b.get_result()};
78	c.get_result()
79    }
80}
81
82///或非门 有true则为Some(false)
83///或非门由四个与非门组成
84/// # Examples
85/// ```
86/// use logicgate::Nor;
87/// use logicgate::LogicGate;
88/// let a = Nor{input1: &Some(true),input2: &Some(false)};
89/// let b = Nor{input1: &Some(false),input2: &Some(false)};
90/// assert_eq!(a.get_result(),Some(false));
91/// assert_eq!(b.get_result(),Some(true));
92/// ```
93pub struct Nor<'a>{
94    pub input1: &'a Option<bool>,
95    pub input2: &'a Option<bool>,
96}
97
98impl<'a> LogicGate for Nor<'a> {
99    fn get_result(&self) -> Option<bool> {
100	let a = Nand{input1: self.input1, input2: self.input1};
101	let b = Nand{input1: self.input2, input2: self.input2};
102	let c = Nand{input1: &a.get_result(),input2: &b.get_result()};
103	let d = Nand{input1: &c.get_result(),input2: &c.get_result()};
104	d.get_result()
105    }
106}
107///与门 全为true则为true
108///与门由两个与非门组成
109/// # Examples
110/// ```
111/// use logicgate::And;
112/// use logicgate::LogicGate;
113/// let a = And{input1: &Some(true),input2: &Some(false)};
114/// let b = And{input1: &Some(true),input2: &Some(true)};
115/// assert_eq!(a.get_result(),Some(false));
116/// assert_eq!(b.get_result(),Some(true));
117/// ```
118pub struct And<'a> {
119    pub input1: &'a Option<bool>,
120    pub input2: &'a Option<bool>,
121}
122
123impl<'a> LogicGate for And<'a> {
124    fn get_result(&self) -> Option<bool> {
125	let a = Nand{input1: self.input1, input2: self.input2};
126	let b = Nand{input1: &a.get_result(), input2: &a.get_result()};
127	b.get_result()
128    }
129}
130///高电平 输出true
131/// # Examples
132/// ```
133/// use logicgate::HighLevel;
134/// use logicgate::LogicGate;
135/// let a = HighLevel{};
136/// assert_eq!(a.get_result(),Some(true));
137/// ```
138pub struct HighLevel{
139}
140impl LogicGate for HighLevel {
141    fn get_result(&self) -> Option<bool> {
142	Some(true)
143    }
144}
145
146///低电平 输出Some(false)
147/// # Examples
148/// ```
149/// use logicgate::LowLevel;
150/// use logicgate::LogicGate;
151/// let a = LowLevel{};
152/// assert_eq!(a.get_result(),Some(false));
153/// ```
154pub struct LowLevel{
155}
156impl LogicGate for LowLevel {
157    fn get_result(&self) -> Option<bool> {
158	Some(false)
159    }
160}
161
162///异或门 输入不一样true
163/// # Examples
164/// ```
165/// use logicgate::Xor;
166/// use logicgate::LogicGate;
167/// let a = Xor{input1:& Some(true),input2:& Some(false)};
168/// let b = Xor{input1:& Some(true),input2:& Some(true)};
169/// let c = Xor{input1:& Some(false),input2:& Some(false)};
170/// assert_eq!(a.get_result(),Some(true));
171/// assert_eq!(b.get_result(),Some(false));
172/// assert_eq!(c.get_result(),Some(false));
173/// ```
174pub struct Xor<'a>{
175    pub input1: &'a Option<bool>,
176    pub input2: &'a Option<bool>,
177}
178impl<'a> LogicGate for Xor<'a>{
179    fn get_result(&self) ->Option<bool> {
180	let a:And = And{input1:self.input1,input2:self.input2};
181	let b:Nor = Nor{input1:self.input1,input2:self.input2};
182	let c:Nor = Nor{input1:&a.get_result(),input2:&b.get_result()};
183	c.get_result()
184    }
185}
186
187
188///三路或门
189/// # Examples
190/// ```
191/// use logicgate::ThreeOr;
192/// use logicgate::LogicGate;
193/// let a = ThreeOr{input1:& Some(true),input2:& Some(false),input3:& Some(false)};
194/// let b = ThreeOr{input1:& Some(false),input2:& Some(true),input3:& Some(false)};
195/// let c = ThreeOr{input1:& Some(false),input2:& Some(false),input3:& Some(true)};
196/// let d = ThreeOr{input1:& Some(false),input2:& Some(false),input3:& Some(false)};
197/// assert_eq!(a.get_result(),Some(true));
198/// assert_eq!(b.get_result(),Some(true));
199/// assert_eq!(c.get_result(),Some(true));
200/// assert_eq!(d.get_result(),Some(false));
201/// ```
202pub struct ThreeOr<'a>{
203    pub input1: &'a Option<bool>,
204    pub input2: &'a Option<bool>,
205    pub input3: &'a Option<bool>,
206}
207
208impl<'a> LogicGate for ThreeOr<'a>{
209    fn get_result(&self) ->Option<bool> {
210	let a:Or = Or{input1:self.input1,input2:self.input2};
211	let b:Or = Or{input1:self.input2,input2:self.input3};
212	let c:Or = Or{input1:&a.get_result(),input2:&b.get_result()};
213	c.get_result()
214    }
215}
216
217
218///三路与门
219/// # Examples
220/// ```
221/// use logicgate::ThreeAnd;
222/// use logicgate::LogicGate;
223/// let a = ThreeAnd{input1:& Some(true),input2:& Some(false),input3:& Some(false)};
224/// let b = ThreeAnd{input1:& Some(false),input2:& Some(true),input3:& Some(false)};
225/// let c = ThreeAnd{input1:& Some(false),input2:& Some(false),input3:& Some(true)};
226/// let d = ThreeAnd{input1:& Some(false),input2:& Some(false),input3:& Some(false)};
227/// let e = ThreeAnd{input1:& Some(true),input2:& Some(true),input3:& Some(true)};
228/// assert_eq!(a.get_result(),Some(false));
229/// assert_eq!(b.get_result(),Some(false));
230/// assert_eq!(c.get_result(),Some(false));
231/// assert_eq!(d.get_result(),Some(false));
232/// assert_eq!(e.get_result(),Some(true));
233/// ```
234pub struct ThreeAnd<'a>{
235    pub input1: &'a Option<bool>,
236    pub input2: &'a Option<bool>,
237    pub input3: &'a Option<bool>,
238}
239
240impl<'a> LogicGate for ThreeAnd<'a>{
241    fn get_result(&self) ->Option<bool> {
242	let a:And = And{input1:self.input1,input2:self.input2};
243	let b:And = And{input1:self.input2,input2:self.input3};
244	let c:And = And{input1:&a.get_result(),input2:&b.get_result()};
245	c.get_result()
246    }
247}
248///同或门
249///相同则为Some(true)
250/// # Examples
251/// ```
252/// use logicgate::Xnor;
253/// use logicgate::LogicGate;
254/// let a = Xnor{input1:&Some(true),input2:&Some(false)};
255/// let b = Xnor{input1:&Some(true),input2:&Some(true)};
256/// let c = Xnor{input1:&Some(false),input2:&Some(false)};
257/// assert_eq!(a.get_result(),Some(false));
258/// assert_eq!(b.get_result(),Some(true));
259/// assert_eq!(c.get_result(),Some(true));
260/// ```
261pub struct Xnor<'a>{
262    pub input1: &'a Option<bool>,
263    pub input2: &'a Option<bool>,
264}
265
266impl<'a> LogicGate for Xnor<'a>{
267    fn get_result(&self) ->Option<bool> {
268	let a:Xor = Xor{input1:self.input1,input2:self.input2};
269	let b:Not = Not{input: &a.get_result()};
270	b.get_result()
271    }
272}
273
274use std::time::Duration;
275///延迟线
276///# Examples
277///```
278/// use logicgate::DelayLine;
279/// let a = DelayLine{delay: 20,input: &Some(true)};
280/// 
281/// a.get_result();
282///```
283
284pub struct DelayLine<'a> {
285    /// 延迟时间,单位为毫秒
286    pub delay: u64,
287    pub input: &'a Option<bool>,
288}
289
290impl<'a> DelayLine<'a> {
291    pub fn get_result(&self) -> Option<bool> {
292        std::thread::sleep(Duration::from_millis(self.delay));
293        *self.input
294    }
295}
296
297///半加器
298///输出一个包含低位结果与进位的元组
299/// # Examples
300///```
301///use logicgate::HalfAdder;
302///let a:HalfAdder = HalfAdder{input1:&Some(true),input2:&Some(false)};
303///assert_eq!(a.get_result(),(Some(true),Some(false)));
304///let a:HalfAdder = HalfAdder{input1:&Some(true),input2:&Some(true)};
305///assert_eq!(a.get_result(),(Some(false),Some(true)));
306///```
307pub struct HalfAdder<'a> {
308    pub input1: &'a Option<bool>,
309    pub input2: &'a Option<bool>,
310}
311
312
313
314impl<'a> HalfAdder<'a>{
315    ///返回(sum,carry)
316    pub fn get_result(&self) -> (Option<bool>,Option<bool>) {
317	let a:Xor = Xor{input1: self.input1,input2:self.input2};
318	let b:And = And{input1: self.input1,input2:self.input2};
319	(a.get_result(),b.get_result())
320    }
321}
322
323///全加器
324/// # Examples
325///```
326///use logicgate::FullAdder;
327///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(false),input3:&Some(false)};
328///assert_eq!(a.get_result(),(Some(true),Some(false)));
329///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(true),input3:&Some(false)};
330///assert_eq!(a.get_result(),(Some(false),Some(true)));
331///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(true),input3:&Some(true)};
332///assert_eq!(a.get_result(),(Some(true),Some(true)));
333///let a:FullAdder = FullAdder{input1:&Some(false),input2:&Some(false),input3:&Some(false)};
334///assert_eq!(a.get_result(),(Some(false),Some(false)));
335///```
336pub struct FullAdder<'a> {
337    pub input1: &'a Option<bool>,
338    pub input2: &'a Option<bool>,
339    pub input3: &'a Option<bool>,
340}
341
342impl<'a> FullAdder<'a> {
343    ///返回(sum,carry)
344    pub fn get_result(&self) ->(Option<bool>,Option<bool>) {
345	let a: Xor = Xor{input1: self.input1, input2: self.input2};
346	let b: And = And{input1: self.input1, input2: self.input2};
347	let c: Xor = Xor{input1: &a.get_result(), input2: self.input3};
348	let d: And = And{input1: &a.get_result(), input2: self.input3};
349	let e: Or = Or{input1: &b.get_result(),input2: &d.get_result()};
350	(c.get_result(),e.get_result())
351    }
352}
353///一位开关
354/// # Examples
355///```
356///use logicgate::Switch;
357///use crate::logicgate::LogicGate;
358/// let a:Switch = Switch{switch: &Some(true),input:&Some(false)};
359///assert_eq!(a.get_result(),Some(false));
360///```
361pub struct Switch<'a> {
362    pub switch: &'a Option<bool>,
363    pub input: &'a Option<bool>,
364}
365
366impl<'a> LogicGate for Switch<'a> {
367    fn get_result(&self) -> Option<bool> {
368	if *self.switch == Some(true) {
369	    return *self.input;
370	}
371	None
372    }
373}
374
375///八位开关
376/// # Examples
377///```
378///use logicgate::Switch;
379///use crate::logicgate::LogicGate;
380/// let a:Switch = Switch{switch: &Some(true),input:&Some(false)};
381///assert_eq!(a.get_result(),Some(false));
382///```
383pub struct EightSwitch<'a> {
384    pub switch: &'a Option<bool>,
385    pub input: i32,
386}
387
388impl<'a>  EightSwitch<'a> {
389    fn get_result(&self) -> Option<i32> {
390	if *self.switch == Some(true) {
391	    return Some(self.input);
392	}
393	None
394    }
395}
396
397///8位分线器
398/// # Examples
399///```
400///use logicgate::EightBitSplitter;
401///let a:EightBitSplitter = EightBitSplitter{input: 201};
402///let a = a.get_result();
403///assert_eq!(a,(Some(true),Some(false),Some(false),Some(true),Some(false),Some(false),Some(true),Some(true)));
404/// let a:EightBitSplitter = EightBitSplitter{input:108};
405/// let a = a.get_result();
406/// assert_eq!(a,(Some(false),Some(false),Some(true),Some(true),Some(false),Some(true),Some(true),Some(false)));
407///```
408pub struct EightBitSplitter{
409    pub input: i32,
410}
411
412impl EightBitSplitter {
413    pub fn get_result(&self) ->(Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>) {
414	let bit1 = (self.input & 1) != 0;
415        let bit2 = (self.input & 2) != 0;
416        let bit3 = (self.input & 4) != 0;
417        let bit4 = (self.input & 8) != 0;
418        let bit5 = (self.input & 16) != 0;
419        let bit6 = (self.input & 32) != 0;
420        let bit7 = (self.input & 64) != 0;
421        let bit8 = (self.input & 128) != 0;
422
423        (Some(bit1), Some(bit2), Some(bit3), Some(bit4), Some(bit5), Some(bit6), Some(bit7),Some(bit8))
424    }
425}
426
427
428///8位集线器
429/// # Examples
430/// ```
431/// use logicgate::EightBitMux;
432/// let a = EightBitMux{input1:& Some(true),input2:& Some(false),input3:& Some(false),input4:& Some(true),input5:& Some(false),input6:& Some(false),input7:& Some(true),input8:& Some(true)};
433/// assert_eq!(a.get_result(),201);
434/// let a = EightBitMux{input1:& Some(false),input2: & Some(false),input3: & Some(true),input4: & Some(true),input5: & Some(false),input6: & Some(true),input7: & Some(true),input8: & Some(false)};
435/// assert_eq!(a.get_result(),108);
436/// ```
437pub struct EightBitMux<'a> {
438    pub input1: &'a Option<bool>,
439    pub input2: &'a Option<bool>,
440    pub input3: &'a Option<bool>,
441    pub input4: &'a Option<bool>,
442    pub input5: &'a Option<bool>,
443    pub input6: &'a Option<bool>,
444    pub input7: &'a Option<bool>,
445    pub input8: &'a Option<bool>,
446}
447
448impl<'a> EightBitMux<'a> {
449    pub fn get_result(&self) -> i32 {
450        let result = (self.input8.unwrap_or(false) as i32) << 7 |
451                     (self.input7.unwrap_or(false) as i32) << 6 |
452                     (self.input6.unwrap_or(false) as i32) << 5 |
453                     (self.input5.unwrap_or(false) as i32) << 4 |
454                     (self.input4.unwrap_or(false) as i32) << 3 |
455                     (self.input3.unwrap_or(false) as i32) << 2 |
456                     (self.input2.unwrap_or(false) as i32) << 1 |
457                     (self.input1.unwrap_or(false) as i32);
458        result
459    }
460}
461
462///八位加法器
463/// # get 1 bool input and 2 i32 inputs
464/// ## return 1 EightBit Output and Carry bool
465/// # Examples
466/// ```
467///    use logicgate::EightBitAdder;
468///         let adder = EightBitAdder {
469///             input1: &Some(false),
470///             input2: 1_i32,
471///             input3: 1_i32,
472///         };
473///         let result = adder.get_result();
474///
475///         // 预期的加法结果
476///         let expected_output = 2;
477///
478///         // 预期的进位
479///         let expected_carry = Some(false);
480///
481///         // 检查加法器的输出
482///         assert_eq!(result, (expected_output, expected_carry));
483/// ```
484pub struct EightBitAdder<'a>{
485    pub input1: &'a Option<bool>,
486    pub input2: i32,
487    pub input3: i32,
488}
489
490impl<'a> EightBitAdder<'a> {
491    /// 返回 (低八位结果, 进位)
492
493    pub fn get_result(&self) -> (i32, Option<bool>) {
494        // 分割输入
495        let splitter_one = EightBitSplitter { input: self.input2 }.get_result();
496        let splitter_two = EightBitSplitter { input: self.input3 }.get_result();
497        // 逐个全加器进行相加
498        let adder_one = FullAdder { input1: self.input1, input2: & splitter_one.0, input3: & splitter_two.0 }.get_result();
499        let adder_two = FullAdder { input1: & adder_one.1, input2: & splitter_one.1, input3: & splitter_two.1 }.get_result();
500        let adder_three = FullAdder { input1: & adder_two.1, input2: & splitter_one.2, input3: & splitter_two.2 }.get_result();
501        let adder_four = FullAdder { input1: & adder_three.1, input2: & splitter_one.3, input3: & splitter_two.3 }.get_result();
502        let adder_five = FullAdder { input1: & adder_four.1, input2: & splitter_one.4, input3: & splitter_two.4 }.get_result();
503        let adder_six = FullAdder { input1: & adder_five.1, input2: & splitter_one.5, input3: & splitter_two.5 }.get_result();
504        let adder_seven = FullAdder { input1: & adder_six.1, input2: & splitter_one.6, input3: & splitter_two.6 }.get_result();
505        let adder_eight = FullAdder { input1: & adder_seven.1, input2: & splitter_one.7, input3: & splitter_two.7 }.get_result();
506        // 合并全加器的结果
507        let selection = EightBitMux{input1:& adder_one.0, input2:& adder_two.0, input3:& adder_three.0, input4:& adder_four.0, input5:& adder_five.0, input6:& adder_six.0, input7:& adder_seven.0, input8:& adder_eight.0 }.get_result();
508
509        // 返回最终结果及进位
510        (selection, adder_eight.1)
511    }
512}
513
514///八位非
515/// # Examples
516/// ```
517///    use logicgate::EightBitNot;
518///    let a = EightBitNot{input: 80}.get_result();
519///    assert_eq!(a,175);
520/// ```
521pub struct EightBitNot{
522    pub input: i32,
523}
524
525impl EightBitNot {
526    pub fn get_result(&self) -> i32 {
527	let a = EightBitSplitter{input: self.input}.get_result();
528	let b = Not{input:& a.0}.get_result();
529	let c = Not{input:& a.1}.get_result();
530	let d = Not{input:& a.2}.get_result();
531	let e = Not{input:& a.3}.get_result();
532	let f = Not{input:& a.4}.get_result();
533	let g = Not{input:& a.5}.get_result();
534	let h = Not{input:& a.6}.get_result();
535	let i = Not{input:& a.7}.get_result();
536	EightBitMux{input1:& b,input2:& c,input3:& d,input4:& e,input5:& f,input6:& g,input7:& h,input8:& i}.get_result()
537    }
538}
539
540///八位或
541/// # Examples
542/// ```
543///    use logicgate::EightBitOr;
544///    let a = EightBitOr{input1: 80,input2:21}.get_result();
545///    assert_eq!(a,85);
546/// ```
547pub struct EightBitOr{
548    pub input1: i32,
549    pub input2: i32,
550}
551
552impl EightBitOr {
553    pub fn get_result(&self) -> i32 {
554	let a = EightBitSplitter{input: self.input1}.get_result();
555	let b = EightBitSplitter{input: self.input2}.get_result();
556	let c = Or{input1:& a.0,input2:& b.0}.get_result();
557	let d = Or{input1:& a.1,input2:& b.1}.get_result();
558	let e = Or{input1:& a.2,input2:& b.2}.get_result();
559	let f = Or{input1:& a.3,input2:& b.3}.get_result();
560	let g = Or{input1:& a.4,input2:& b.4}.get_result();
561	let h = Or{input1:& a.5,input2:& b.5}.get_result();
562	let i = Or{input1:& a.6,input2:& b.6}.get_result();
563	let j = Or{input1:& a.7,input2:& b.7}.get_result();
564	EightBitMux{input1:& c,input2: & d,input3:& e,input4:& f,input5:& g,input6:& h,input7:& i,input8:& j}.get_result()
565    }
566}
567
568
569///八位数据选择器
570/// # 当input1为false时输出input2, input1为true输出input3
571/// # Examples
572/// ```
573/// use logicgate::DataSelector;
574/// let a = DataSelector{input1:&Some(false),input2:20,input3:10}.get_result();
575/// assert_eq!(a,20);
576/// let a = DataSelector{input1:&Some(true),input2:20,input3:10}.get_result();
577/// assert_eq!(a,10);
578///```
579pub struct DataSelector<'a>{
580    pub input1: &'a Option<bool>,
581    pub input2: i32,
582    pub input3: i32,
583}
584
585impl<'a> DataSelector<'a>{
586    pub fn get_result(&self) -> i32 {
587	if *self.input1 ==Some(true) {
588	    return self.input3;
589	}
590	self.input2
591    }
592}