diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-11-29 00:08:45 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-11-29 00:08:45 +0200 |
| commit | f94b1b6825ea3515b4b16e0832a4844f8342b1ba (patch) | |
| tree | 8e06f6fb4d2c34439502365d4867ebe7d37007d0 | |
| parent | d5d258475df2a94c1c248f2f21f1425d7fc683ba (diff) | |
| download | lgsim-f94b1b6825ea3515b4b16e0832a4844f8342b1ba.tar.xz lgsim-f94b1b6825ea3515b4b16e0832a4844f8342b1ba.zip | |
Changed Added pins to AddGate that represents all the and Gate pins and made input and output reference pins ids and modified the evaluate function accordingly
| -rw-r--r-- | src/gate.rs | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/gate.rs b/src/gate.rs index 1baee11..e6172df 100644 --- a/src/gate.rs +++ b/src/gate.rs @@ -127,46 +127,55 @@ impl PartialEq for NotGate { #[derive(Debug, Clone)] pub struct AndGate { id: usize, - input: Pins, - output: Pin, + pins: HashMap<usize, Pin>, + input: Vec<usize>, + output: usize, } impl AndGate { fn new(input: Vec<PinValue>, id: usize, kind: PinType) -> Self { if input.is_empty() { - let pin_1 = Pin::new(PinType::GateInput, id, 42); - let pin_2 = Pin::new(PinType::GateInput, id, 42); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, pin_2], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![], + output: pin_out.id, } } else if input.len() == 1 { let pin_1 = Pin::new(kind, id, input[0]); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_1.id, pin_1); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, Pin::new(PinType::Undetermined, id, 42)], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![pin_1.id], + output: pin_out.id, } } else { let pin_1 = Pin::new(kind, id, input[0]); let pin_2 = Pin::new(kind, id, input[1]); + let pin_out = Pin::new(PinType::GateOutput, id, 42); + let mut pins = HashMap::new(); + pins.insert(pin_1.id, pin_1); + pins.insert(pin_2.id, pin_2); + pins.insert(pin_out.id, pin_out); Self { id, - input: vec![pin_1, pin_2], - output: Pin::new(PinType::Undetermined, id, 42), + pins, + input: vec![pin_1.id, pin_2.id], + output: pin_out.id, } } } - fn evaluate(&mut self) -> bool { - let res = (self.input[0].val.unwrap() & self.input[1].val.unwrap()) == 1; - self.output.val = Some(res as u8); - self.output.kind = PinType::GateOutput; - println!("{:?}", self); - res + pub fn pin(&self, id: usize) -> &Pin { + self.pins.get(&id).expect("pin should exist") } -} #[derive(Debug, Clone)] pub struct OrGate { |
