Skip to content

Commit

Permalink
add d flip flop, d latch, fix t flip flop
Browse files Browse the repository at this point in the history
  • Loading branch information
sparshg committed Oct 4, 2023
1 parent 16642df commit dbaf5c4
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/simulator/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@ lazy_static! {
),
);

builtins.insert(
"D Latch",
Builtin::new(
Module::new_builtin(
"D Latch",
Category::Latch,
2,
1,
Decoration::Label("D L".to_string()),
),
|input, instance| {
if input & 0b10 > 0 {
instance.set_bytes(input);
}
instance.bytes()
},
),
);

builtins.insert(
"SR Nand Latch",
Builtin::new(
Expand Down Expand Up @@ -304,13 +323,27 @@ lazy_static! {
),
);

builtins.insert(
"D Flip-Flop",
Builtin::new(
Module::new_builtin(
"D Flip-Flop",
Category::FlipFlop,
2,
1,
Decoration::Label("D".to_string()),
),
d_flip_flop,
),
);

builtins.insert(
"T Flip-Flop",
Builtin::new(
Module::new_builtin(
"T Flip-Flop",
Category::FlipFlop,
1,
2,
1,
Decoration::Label("T".to_string()),
),
Expand Down Expand Up @@ -363,10 +396,18 @@ fn sr_nand_latch(input: u128, instance: &mut Block) -> u128 {
instance.bytes() | (((instance.bytes() == 0) as u128) << 1)
}

fn d_flip_flop(input: u128, instance: &mut Block) -> u128 {
if input & 0b10 > 0 && instance.bytes() & 0b10 == 0 {
instance.set_bytes(input);
}
instance.set_bytes(instance.bytes() & !0b10 | (input & 0b10));
instance.bytes() & 1
}

fn t_flip_flop(input: u128, instance: &mut Block) -> u128 {
if input & 1 > 0 && instance.bytes() & 0b10 == 0 {
if input == 0b11 && instance.bytes() & 0b10 == 0 {
instance.set_bytes(instance.bytes() ^ 1);
}
instance.set_bytes((instance.bytes() & !0b10) | (input << 1));
instance.set_bytes((instance.bytes() & !0b10) | (input & 0b10));
instance.bytes() & 1
}

0 comments on commit dbaf5c4

Please sign in to comment.