pub fn as_bool(x: f64) -> bool {
x >= 0.5
}
pub fn from_bool(b: bool) -> f64 {
if b {
1.0
} else {
0.0
}
}
pub fn rising_edge(clk: f64, prev_clk: f64) -> bool {
as_bool(clk) && !as_bool(prev_clk)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicOp {
And,
Or,
Xor,
Nand,
Nor,
Xnor,
Not,
}
impl LogicOp {
pub fn from_name(name: &str) -> Option<Self> {
use LogicOp::*;
Some(match name {
"and" => And,
"or" => Or,
"xor" => Xor,
"nand" => Nand,
"nor" => Nor,
"xnor" => Xnor,
"not" => Not,
_ => return None,
})
}
pub fn name(self) -> &'static str {
use LogicOp::*;
match self {
And => "and",
Or => "or",
Xor => "xor",
Nand => "nand",
Nor => "nor",
Xnor => "xnor",
Not => "not",
}
}
pub fn is_unary(self) -> bool {
matches!(self, LogicOp::Not)
}
pub fn call(self, inputs: &[bool]) -> bool {
use LogicOp::*;
match self {
And => inputs.iter().all(|&b| b),
Nand => !inputs.iter().all(|&b| b),
Or => inputs.iter().any(|&b| b),
Nor => !inputs.iter().any(|&b| b),
Xor => inputs.iter().filter(|&&b| b).count() % 2 == 1,
Xnor => inputs.iter().filter(|&&b| b).count() % 2 == 0,
Not => {
assert_eq!(inputs.len(), 1, "LogicOp::Not requires exactly one input");
!inputs[0]
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LatchPriority {
Set,
Reset,
}
impl LatchPriority {
pub fn from_name(name: &str) -> Option<Self> {
match name {
"set" => Some(LatchPriority::Set),
"reset" => Some(LatchPriority::Reset),
_ => None,
}
}
}
pub fn srlatch_next(q: bool, set: bool, reset: bool, priority: LatchPriority) -> bool {
match (set, reset) {
(false, false) => q,
(true, false) => true,
(false, true) => false,
(true, true) => matches!(priority, LatchPriority::Set),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlipFlopKind {
D,
Jk,
T,
}
impl FlipFlopKind {
pub fn from_name(name: &str) -> Option<Self> {
use FlipFlopKind::*;
Some(match name {
"dff" => D,
"jkff" => Jk,
"tff" => T,
_ => return None,
})
}
pub fn name(self) -> &'static str {
use FlipFlopKind::*;
match self {
D => "dff",
Jk => "jkff",
T => "tff",
}
}
pub fn input_count(self) -> usize {
match self {
FlipFlopKind::D | FlipFlopKind::T => 1,
FlipFlopKind::Jk => 2,
}
}
pub fn next_state(self, q: bool, inputs: &[bool]) -> bool {
match self {
FlipFlopKind::D => {
assert_eq!(inputs.len(), 1);
inputs[0]
}
FlipFlopKind::T => {
assert_eq!(inputs.len(), 1);
q ^ inputs[0]
}
FlipFlopKind::Jk => {
assert_eq!(inputs.len(), 2);
let (j, k) = (inputs[0], inputs[1]);
match (j, k) {
(false, false) => q,
(true, false) => true,
(false, true) => false,
(true, true) => !q,
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn as_bool_and_from_bool_round_trip_at_the_gate_binding_threshold() {
assert!(!as_bool(0.4));
assert!(as_bool(0.5));
assert!(as_bool(0.6));
assert_eq!(from_bool(true), 1.0);
assert_eq!(from_bool(false), 0.0);
}
#[test]
fn rising_edge_fires_only_on_the_low_to_high_transition() {
assert!(rising_edge(1.0, 0.0)); assert!(!rising_edge(1.0, 1.0)); assert!(!rising_edge(0.0, 0.0)); assert!(!rising_edge(0.0, 1.0)); }
#[test]
fn and_or_nand_nor_match_the_truth_table() {
assert!(LogicOp::And.call(&[true, true, true]));
assert!(!LogicOp::And.call(&[true, false, true]));
assert!(!LogicOp::Nand.call(&[true, true, true]));
assert!(LogicOp::Nand.call(&[true, false, true]));
assert!(!LogicOp::Or.call(&[false, false, false]));
assert!(LogicOp::Or.call(&[false, true, false]));
assert!(LogicOp::Nor.call(&[false, false, false]));
assert!(!LogicOp::Nor.call(&[false, true, false]));
}
#[test]
fn xor_xnor_generalize_to_odd_parity_past_two_inputs() {
assert!(LogicOp::Xor.call(&[true, false]));
assert!(!LogicOp::Xor.call(&[true, true]));
assert!(!LogicOp::Xor.call(&[true, true, false]));
assert!(LogicOp::Xor.call(&[true, true, true]));
assert!(LogicOp::Xnor.call(&[true, true, false]));
assert!(!LogicOp::Xnor.call(&[true, true, true]));
}
#[test]
fn not_inverts_its_one_input() {
assert!(!LogicOp::Not.call(&[true]));
assert!(LogicOp::Not.call(&[false]));
assert!(LogicOp::Not.is_unary());
assert!(!LogicOp::And.is_unary());
}
#[test]
#[should_panic]
fn not_panics_on_the_wrong_arity() {
LogicOp::Not.call(&[true, false]);
}
#[test]
fn srlatch_holds_sets_resets_and_resolves_the_priority_case() {
assert!(!srlatch_next(false, false, false, LatchPriority::Set)); assert!(srlatch_next(true, false, false, LatchPriority::Set)); assert!(srlatch_next(false, true, false, LatchPriority::Set)); assert!(!srlatch_next(true, false, true, LatchPriority::Set)); assert!(srlatch_next(false, true, true, LatchPriority::Set));
assert!(!srlatch_next(true, true, true, LatchPriority::Reset));
}
#[test]
fn dff_next_state_is_just_d() {
assert!(FlipFlopKind::D.next_state(false, &[true]));
assert!(!FlipFlopKind::D.next_state(true, &[false]));
}
#[test]
fn jkff_matches_the_classic_four_row_table() {
let jk = FlipFlopKind::Jk;
assert!(!jk.next_state(false, &[false, false])); assert!(jk.next_state(true, &[false, false])); assert!(jk.next_state(false, &[true, false])); assert!(!jk.next_state(true, &[false, true])); assert!(jk.next_state(false, &[true, true])); assert!(!jk.next_state(true, &[true, true])); }
#[test]
fn tff_toggles_on_t_and_holds_otherwise() {
let t = FlipFlopKind::T;
assert!(!t.next_state(false, &[false])); assert!(t.next_state(false, &[true])); assert!(!t.next_state(true, &[true])); }
#[test]
fn from_name_matches_conventional_names_and_rejects_unknown() {
assert_eq!(LogicOp::from_name("xor"), Some(LogicOp::Xor));
assert_eq!(LogicOp::from_name("nope"), None);
assert_eq!(FlipFlopKind::from_name("jkff"), Some(FlipFlopKind::Jk));
assert_eq!(
LatchPriority::from_name("reset"),
Some(LatchPriority::Reset)
);
}
}