Created
January 8, 2022 18:40
-
-
Save evaporei/b65533fa0a3824fa88278c1378c349ff to your computer and use it in GitHub Desktop.
Xor in Rust cause HDL sucks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct In { | |
a: bool, | |
b: bool, | |
} | |
#[derive(Debug, PartialEq)] | |
struct Out { | |
out: bool, | |
} | |
struct Xor; | |
trait Chip { | |
type Input; | |
type Output; | |
fn run(input: Self::Input) -> Self::Output; | |
} | |
impl Chip for Xor { | |
type Input = In; | |
type Output = Out; | |
fn run(input: Self::Input) -> Self::Output { | |
let In { a, b } = input; | |
let not_a = !a; | |
let not_b = !b; | |
let w1 = a && not_b; | |
let w2 = not_a && b; | |
let out = w1 || w2; | |
Out { out } | |
} | |
} | |
fn main() { | |
let test_table = [ | |
(In { a: false, b: false }, Out { out: false }), | |
(In { a: false, b: true }, Out { out: true }), | |
(In { a: true, b: false }, Out { out: true }), | |
(In { a: true, b: true }, Out { out: false }), | |
]; | |
for scenario in test_table { | |
let (input, output) = scenario; | |
assert_eq!(Xor::run(input), output); | |
} | |
println!("Tests passed!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://b1391bd6-da3d-477d-8c01-38cdf774495a.filesusr.com/ugd/44046b_f2c9e41f0b204a34ab78be0ae4953128.pdf
Project 01: https://www.nand2tetris.org/course