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
// Copyright © 2022
// Author: Antonio Caggiano <info@antoniocaggiano.eu>
// SPDX-License-Identifier: MIT

use super::{Nand, Not, Signal, Unit};

/// The AND gate is built using a NOT and a NAND gate
///
/// | a | b |out|
/// |---|---|---|
/// | 0 | 0 | 0 |
/// | 0 | 1 | 0 |
/// | 1 | 0 | 0 |
/// | 1 | 1 | 1 |
#[derive(Default)]
pub struct And {
    a: Signal,
    b: Signal,
    out: Signal,

    not: Not,
    nand: Nand,
}

impl And {
    /// Carries out the simulation and returns `a AND b`
    pub fn sim(&mut self, a: Signal, b: Signal) -> Signal {
        self.a = a;
        self.b = b;
        self.eval();
        self.out
    }
}

impl Unit for And {
    fn eval(&mut self) {
        self.out = self.not.sim(self.nand.sim(self.a, self.b));
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn run() {
        let mut and = And::default();

        and.sim(Signal::LO, Signal::LO);
        assert_eq!(and.out, Signal::LO);

        and.sim(Signal::HI, Signal::LO);
        assert_eq!(and.out, Signal::LO);

        and.sim(Signal::LO, Signal::HI);
        assert_eq!(and.out, Signal::LO);

        and.sim(Signal::HI, Signal::HI);
        assert_eq!(and.out, Signal::HI);
    }
}