Skip to content

Commit

Permalink
replace prior Ieee1164 enum construction with associated constants
Browse files Browse the repository at this point in the history
  • Loading branch information
hellow554 committed Dec 5, 2018
1 parent 4ed1ac9 commit 09252d9
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 122 deletions.
4 changes: 2 additions & 2 deletions examples/fulladder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ fn main() {
circuit.add_updater(&xor2);
circuit.add_updater(&or);

const _0: Ieee1164 = Ieee1164::Strong(Ieee1164Value::Zero);
const _1: Ieee1164 = Ieee1164::Strong(Ieee1164Value::One);
const _0: Ieee1164 = Ieee1164::_0; // this helps to keep the
const _1: Ieee1164 = Ieee1164::_1; // matrix clean and short

const VALUES: [[Ieee1164; 5]; 8] = [
[_0, _0, _0, _0, _0],
Expand Down
8 changes: 4 additions & 4 deletions examples/vcd_wire_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use logical::models::{
use logical::{Circuit, Ieee1164, Ieee1164Value, Signal};

fn main() {
let mut val = Ieee1164::Strong(Ieee1164Value::One);
let mut val = Ieee1164::_1;

let xor = XorGate::default();
let mux = Mux::default();
let mut input1 = Switch::new_with_value(val);
let input2 = Switch::new_with_value(Ieee1164::Strong(Ieee1164Value::One));
let mut mux_switch = Switch::new_with_value(Ieee1164::Strong(Ieee1164Value::Zero));
let input2 = Switch::new_with_value(Ieee1164::_1);
let mut mux_switch = Switch::new_with_value(Ieee1164::_0);
let output = Led::default();

let mut sig_input_signal = Signal::new();
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
circuit.tick();
circuit.tick();
circuit.tick();
mux_switch.set_value(Ieee1164::Strong(Ieee1164Value::One));
mux_switch.set_value(Ieee1164::_1);
circuit.tick();

let mut dumper = Vcd::new("VCD Example");
Expand Down
63 changes: 32 additions & 31 deletions src/logicbit/ieee1164.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ impl TryFrom<char> for Ieee1164 {

fn try_from(c: char) -> Result<Self, ()> {
Ok(match c.to_ascii_lowercase() {
'u' => Ieee1164::Uninitialized,
'x' => Ieee1164::Strong(Ieee1164Value::Unknown),
'0' => Ieee1164::Strong(Ieee1164Value::Zero),
'1' => Ieee1164::Strong(Ieee1164Value::One),
'z' => Ieee1164::HighImpedance,
'w' => Ieee1164::Weak(Ieee1164Value::Unknown),
'l' => Ieee1164::Weak(Ieee1164Value::Zero),
'h' => Ieee1164::Weak(Ieee1164Value::One),
'*' | '-' | 'd' => Ieee1164::DontCare,
'u' => Ieee1164::_U,
'x' => Ieee1164::_X,
'0' => Ieee1164::_0,
'1' => Ieee1164::_1,
'z' => Ieee1164::_Z,
'w' => Ieee1164::_W,
'l' => Ieee1164::_L,
'h' => Ieee1164::_H,
'*' | '-' | 'd' => Ieee1164::_D,
_ => return Err(()),
})
}
Expand All @@ -89,15 +89,15 @@ impl TryFrom<char> for Ieee1164 {
impl From<Ieee1164> for char {
fn from(i: Ieee1164) -> Self {
match i {
Ieee1164::Uninitialized => 'U',
Ieee1164::Strong(Ieee1164Value::Unknown) => 'X',
Ieee1164::Strong(Ieee1164Value::Zero) => '0',
Ieee1164::Strong(Ieee1164Value::One) => '1',
Ieee1164::HighImpedance => 'Z',
Ieee1164::Weak(Ieee1164Value::Unknown) => 'W',
Ieee1164::Weak(Ieee1164Value::Zero) => 'L',
Ieee1164::Weak(Ieee1164Value::One) => 'H',
Ieee1164::DontCare => '-',
Ieee1164::_U => 'U',
Ieee1164::_X => 'X',
Ieee1164::_0 => '0',
Ieee1164::_1 => '1',
Ieee1164::_Z => 'Z',
Ieee1164::_W => 'W',
Ieee1164::_L => 'L',
Ieee1164::_H => 'H',
Ieee1164::_D => '-',
}
}
}
Expand Down Expand Up @@ -239,62 +239,62 @@ impl Ieee1164 {
!(self.is_1H() || self.is_0L())
}

/// Checks whether this is either [`Ieee1164::_0`], [`Ieee1164::_1`]
/// Checks whether this is either [`Ieee1164::_0`] or [`Ieee1164::_1`]
pub fn is_01(self) -> bool {
self.is_0() || self.is_1()
}

/// Checks whether this is either [`Ieee1164::_1`], [`Ieee1164::_H`]
/// Checks whether this is either [`Ieee1164::_1`] or [`Ieee1164::_H`]
pub fn is_1H(self) -> bool {
self.is_1() || self.is_H()
}

/// Checks whether this is either [`Ieee1164::_0`], [`Ieee1164::_L`]
/// Checks whether this is either [`Ieee1164::_0`] or [`Ieee1164::_L`]
pub fn is_0L(self) -> bool {
self.is_0() || self.is_L()
}

/// Checks whether this is either [`Ieee1164::_U`]
/// Checks whether this is [`Ieee1164::_U`]
pub fn is_U(self) -> bool {
self == _U
}

/// Checks whether this is either [`Ieee1164::_X`]
/// Checks whether this is [`Ieee1164::_X`]
pub fn is_X(self) -> bool {
self == _X
}

/// Checks whether this is either [`Ieee1164::_0`]
/// Checks whether this is [`Ieee1164::_0`]
pub fn is_0(self) -> bool {
self == _0
}

/// Checks whether this is either [`Ieee1164::_1`]
/// Checks whether this is [`Ieee1164::_1`]
pub fn is_1(self) -> bool {
self == _1
}

/// Checks whether this is either [`Ieee1164::_Z`]
/// Checks whether this is [`Ieee1164::_Z`]
pub fn is_Z(self) -> bool {
self == _Z
}

/// Checks whether this is either [`Ieee1164::_W`]
/// Checks whether this is [`Ieee1164::_W`]
pub fn is_W(self) -> bool {
self == _W
}

/// Checks whether this is either [`Ieee1164::_L`]
/// Checks whether this is [`Ieee1164::_L`]
pub fn is_L(self) -> bool {
self == _L
}

/// Checks whether this is either [`Ieee1164::_H`]
/// Checks whether this is [`Ieee1164::_H`]
pub fn is_H(self) -> bool {
self == _H
}

/// Checks whether this is either [`Ieee1164::_D`]
/// Checks whether this is [`Ieee1164::_D`]
pub fn is_D(self) -> bool {
self == _D
}
Expand Down Expand Up @@ -324,7 +324,8 @@ mod tests {

#[test]
fn not() {
//TODO
let a = Ieee1164::_1;
assert_eq!(Ieee1164::_0, !a);
}

#[test]
Expand Down
36 changes: 18 additions & 18 deletions src/logicbit/logicvector/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,31 @@ impl Index<Ieee1164> for Masks {

fn index(&self, index: Ieee1164) -> &u128 {
match index {
Ieee1164::Uninitialized => &self._U,
Ieee1164::Strong(Ieee1164Value::Unknown) => &self._X,
Ieee1164::Strong(Ieee1164Value::One) => &self._1,
Ieee1164::Strong(Ieee1164Value::Zero) => &self._0,
Ieee1164::Weak(Ieee1164Value::Unknown) => &self._W,
Ieee1164::Weak(Ieee1164Value::One) => &self._H,
Ieee1164::Weak(Ieee1164Value::Zero) => &self._L,
Ieee1164::HighImpedance => &self._Z,
Ieee1164::DontCare => &self._D,
Ieee1164::_U => &self._U,
Ieee1164::_X => &self._X,
Ieee1164::_1 => &self._1,
Ieee1164::_0 => &self._0,
Ieee1164::_W => &self._W,
Ieee1164::_H => &self._H,
Ieee1164::_L => &self._L,
Ieee1164::_Z => &self._Z,
Ieee1164::_D => &self._D,
}
}
}

impl IndexMut<Ieee1164> for Masks {
fn index_mut(&mut self, index: Ieee1164) -> &mut u128 {
match index {
Ieee1164::Uninitialized => &mut self._U,
Ieee1164::Strong(Ieee1164Value::Unknown) => &mut self._X,
Ieee1164::Strong(Ieee1164Value::One) => &mut self._1,
Ieee1164::Strong(Ieee1164Value::Zero) => &mut self._0,
Ieee1164::Weak(Ieee1164Value::Unknown) => &mut self._W,
Ieee1164::Weak(Ieee1164Value::One) => &mut self._H,
Ieee1164::Weak(Ieee1164Value::Zero) => &mut self._L,
Ieee1164::HighImpedance => &mut self._Z,
Ieee1164::DontCare => &mut self._D,
Ieee1164::_U => &mut self._U,
Ieee1164::_X => &mut self._X,
Ieee1164::_1 => &mut self._1,
Ieee1164::_0 => &mut self._0,
Ieee1164::_W => &mut self._W,
Ieee1164::_H => &mut self._H,
Ieee1164::_L => &mut self._L,
Ieee1164::_Z => &mut self._Z,
Ieee1164::_D => &mut self._D,
}
}
}
Expand Down
28 changes: 8 additions & 20 deletions src/models/gates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,20 @@ create_simple_1i1o_gate!(Inverter, inv);

fn weak_buf(a: Ieee1164) -> Ieee1164 {
match a {
Ieee1164::Uninitialized
| Ieee1164::Strong(Ieee1164Value::Unknown)
| Ieee1164::Weak(Ieee1164Value::Unknown)
| Ieee1164::DontCare => Ieee1164::Weak(Ieee1164Value::Unknown),
Ieee1164::Strong(Ieee1164Value::One) | Ieee1164::Weak(Ieee1164Value::One) => Ieee1164::Weak(Ieee1164Value::One),
Ieee1164::Strong(Ieee1164Value::Zero) | Ieee1164::Weak(Ieee1164Value::Zero) => {
Ieee1164::Weak(Ieee1164Value::Zero)
}
Ieee1164::HighImpedance => Ieee1164::HighImpedance,
Ieee1164::_U | Ieee1164::_X | Ieee1164::_W | Ieee1164::_D => Ieee1164::_W,
Ieee1164::_1 | Ieee1164::_H => Ieee1164::_H,
Ieee1164::_0 | Ieee1164::_L => Ieee1164::_L,
Ieee1164::_Z => Ieee1164::_Z,
}
}
create_simple_1i1o_gate!(WeakBuffer, weak_buf);

fn weak_inv(a: Ieee1164) -> Ieee1164 {
match a {
Ieee1164::Uninitialized
| Ieee1164::Strong(Ieee1164Value::Unknown)
| Ieee1164::Weak(Ieee1164Value::Unknown)
| Ieee1164::DontCare => Ieee1164::Weak(Ieee1164Value::Unknown),
Ieee1164::Strong(Ieee1164Value::One) | Ieee1164::Weak(Ieee1164Value::One) => {
Ieee1164::Weak(Ieee1164Value::Zero)
}
Ieee1164::Strong(Ieee1164Value::Zero) | Ieee1164::Weak(Ieee1164Value::Zero) => {
Ieee1164::Weak(Ieee1164Value::One)
}
Ieee1164::HighImpedance => Ieee1164::HighImpedance,
Ieee1164::_U | Ieee1164::_X | Ieee1164::_W | Ieee1164::_D => Ieee1164::_W,
Ieee1164::_1 | Ieee1164::_H => Ieee1164::_L,
Ieee1164::_0 | Ieee1164::_L => Ieee1164::_H,
Ieee1164::_Z => Ieee1164::_Z,
}
}
create_simple_1i1o_gate!(WeakInverter, weak_inv);
2 changes: 1 addition & 1 deletion src/models/gates/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Updateable for Mux {
} else if self.s.value().is_0L() {
self.a.value()
} else {
Ieee1164::Strong(Ieee1164Value::Unknown)
Ieee1164::_X
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/gates/tri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ impl Updateable for TriBuffer {
self.z.replace(if self.s.value().is_1H() {
self.a.value()
} else if self.s.value().is_0L() {
Ieee1164::HighImpedance
Ieee1164::_Z
} else {
Ieee1164::Strong(Ieee1164Value::Unknown)
Ieee1164::_X
});
}
}
Expand Down
21 changes: 0 additions & 21 deletions src/models/inputs/switch.rs

This file was deleted.

14 changes: 0 additions & 14 deletions src/models/outputs/led.rs

This file was deleted.

Empty file.
Empty file.
18 changes: 9 additions & 9 deletions src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ mod tests {
assert_eq!(Ieee1164::Uninitialized, i.value());
}

let val = Ieee1164::Strong(Ieee1164Value::One);
let val = Ieee1164::_1;
p.replace(val);
s.update();
assert_eq!(val, i.value());
Expand All @@ -216,8 +216,8 @@ mod tests {

#[test]
fn signal_after_disconnect() {
let val_a = Ieee1164::Strong(Ieee1164Value::One);
let val_b = Ieee1164::Strong(Ieee1164Value::Zero);
let val_a = Ieee1164::_1;
let val_b = Ieee1164::_0;
let mut p = Port::<_, InOut>::new(val_a);
let o = Port::<_, Input>::default();
let mut s = Signal::new();
Expand All @@ -234,11 +234,11 @@ mod tests {

#[test]
fn signal_multiple_ports() {
let val_a = Ieee1164::Weak(Ieee1164Value::One);
let val_b = Ieee1164::Strong(Ieee1164Value::Zero);
let val_a = Ieee1164::_H;
let val_b = Ieee1164::_0;
let p1 = Port::<_, InOut>::new(val_a);
let p2 = Port::<_, InOut>::new(val_b);
let o = Port::<_, Input>::new(Ieee1164::DontCare);
let o = Port::<_, Input>::new(Ieee1164::_D);
let mut s = Signal::new();

s.connect(&p1).unwrap();
Expand All @@ -252,8 +252,8 @@ mod tests {
fn signal_port_out_of_scope() {
let mut s = Signal::new();

let val_a = Ieee1164::Strong(Ieee1164Value::One);
let val_b = Ieee1164::Strong(Ieee1164Value::Zero);
let val_a = Ieee1164::_1;
let val_b = Ieee1164::_0;

let p1 = Port::<_, Output>::new(val_a);
let o = Port::<_, Input>::default();
Expand All @@ -275,7 +275,7 @@ mod tests {
fn disallow_multiple_connects() {
let mut s = Signal::new();

let val = Ieee1164::Strong(Ieee1164Value::One);
let val = Ieee1164::_1;
let p = Port::<_, InOut>::new(val);
s.connect(&p).unwrap();
s.connect(&p).unwrap();
Expand Down

0 comments on commit 09252d9

Please sign in to comment.