Skip to content

Commit

Permalink
Merge pull request #36 from sparshg/flipflop
Browse files Browse the repository at this point in the history
Add D-Latch, D-Flip Flop, and fix T-Flip Flop
  • Loading branch information
Spydr06 authored Oct 4, 2023
2 parents 58e8951 + 9514f6f commit 8559ea5
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 69 deletions.
153 changes: 90 additions & 63 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
- [Basic Modules](#basic-modules)
- [Input/Output Modules](#inputoutput-modules)
- [Gate Modules](#gate-modules)
- [Combinational Modules](#combinational-modules)
- [Latch Modules](#latch-modules)
- [Flip Flops](#flip-flops)
- [3. Custom Modules](#3-custom-modules)
- [Creating a new module](#creating-a-new-module)
- [Exporting Modules](#exporting-modules)
Expand Down Expand Up @@ -104,131 +106,156 @@ LogicRs features several builtin modules available for every project. Here is a

<img style="float: right;" src="./assets/modules/and.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

- **`Nand`**: inverse of `And`:

<img style="float: right;" src="./assets/modules/nand.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

- **`Nor`**: inverse of `Or`:

<img style="float: right;" src="./assets/modules/nor.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |

- **`Not`**: outputs the inverse of the input signal:

<img style="float: right;" src="./assets/modules/not.png"/>

| Input | Output |
|-------|--------|
| ----- | ------ |
| 0 | 1 |
| 1 | 0 |

- **`Or`**: outputs `1` if either A or B or both inputs are `1`:

<img style="float: right;" src="./assets/modules/or.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

- **`Xnor`**: inverse of `Xor`:

<img style="float: right;" src="./assets/modules/xnor.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

- **`Xor`**: outputs `1` if either the A *or* (strictly) B input is `1`:

<img style="float: right;" src="./assets/modules/xor.png"/>

| A | B | Output |
|---|---|--------|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| A | B | Output |
| --- | --- | ------ |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

### Combinational Modules

- **`Mux`**: A 2:1 multiplexer, selecting either the A or B input based on the S input:

<img style="float: right;" src="./assets/modules/mux.png"/>

| S | Output |
|---|--------|
| 0 | A |
| 1 | B |
| S | Output |
| --- | ------ |
| 0 | A |
| 1 | B |

- **`Demux`**: A 1:2 demultiplexer, selecting either the O0 or O1 output based on the S input:

<img style="float: right;" src="./assets/modules/demux.png"/>

| S | O1 | O0 |
|---|----|----|
| 0 | 0 | A |
| 1 | A | 0 |
| S | O1 | O0 |
| --- | --- | --- |
| 0 | 0 | A |
| 1 | A | 0 |

### Latch Modules

- **`JK Latch`**: Latch similar to the `SR Latch`, but toggeling its value when both inputs A and B are `1`:

<img style="float: right;" src="./assets/modules/jk-latch.png"/>

| A | B | Action |
|---|---|-----------|
| 0 | 0 | No change |
| 0 | 1 | Reset |
| 1 | 0 | Set |
| 1 | 1 | Toggle |
| A | B | Action |
| --- | --- | --------- |
| 0 | 0 | No change |
| 0 | 1 | Reset |
| 1 | 0 | Set |
| 1 | 1 | Toggle |

- **`SR Latch`**: Latch used to store a value:

<img style="float: right;" src="./assets/modules/sr-latch.png"/>

| A | B | Action |
|---|---|-------------|
| 0 | 0 | No change |
| 0 | 1 | Reset |
| 1 | 0 | Set |
| 1 | 1 | Not allowed |
| A | B | Action |
| --- | --- | ----------- |
| 0 | 0 | No change |
| 0 | 1 | Reset |
| 1 | 0 | Set |
| 1 | 1 | Not allowed |

- **`SR Nand Latch`**:

<img style="float: right;" src="./assets/modules/sr-nand-latch.png"/>

| A | B | Action |
|---|---|-------------|
| 0 | 0 | Not allowed |
| 0 | 1 | Store 1 |
| 1 | 0 | Store 0 |
| 1 | 1 | No change |
| A | B | Action |
| --- | --- | ----------- |
| 0 | 0 | Not allowed |
| 0 | 1 | Store 1 |
| 1 | 0 | Store 0 |
| 1 | 1 | No change |


### Flip Flops

- **`D Flip Flop`**: A positive edge triggered flip flop storing D value:

<img style="float: right;" src="./assets/modules/d-flip-flop.png"/>

| D | Q(t) | Q(t+1) |
| --- | ---- | ------ |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |

- **`T Flip Flop`**: A positive edge triggered flip flop toggeling its value:

<img style="float: right;" src="./assets/modules/t-flip-flop.png"/>

| T | Q(t) | Q(t+1) |
| --- | ---- | ------ |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |

> **Note**
> Feel free to submit pull-requests for more module implementations
Expand Down
Binary file added assets/modules/d-flip-flop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/modules/t-flip-flop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/4-bit-counter.lrsproj
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"modules":{},"main_plot":{"blocks":{"5354188438588814584":{"id":5354188438588814584,"name":"Lamp","position":[375,225],"size":[75,75],"unique":false,"passthrough":true,"inputs":[10440987279344356270],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":true},"color":null},"17585780629157557337":{"id":17585780629157557337,"name":"T Flip-Flop","position":[125,25],"size":[110,75],"unique":false,"passthrough":true,"inputs":[895923654972995817],"outputs":[9271995660626457038],"state":{"Direct":2},"output_state":0,"decoration":{"Label":"T"},"color":null},"7846093778520356818":{"id":7846093778520356818,"name":"Lamp","position":[675,225],"size":[75,75],"unique":false,"passthrough":true,"inputs":[3134920317199890587],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":true},"color":null},"17325373593975782801":{"id":17325373593975782801,"name":"T Flip-Flop","position":[275,25],"size":[110,75],"unique":false,"passthrough":true,"inputs":[9271995660626457038],"outputs":[8096273090428741690],"state":{"Direct":0},"output_state":0,"decoration":{"Label":"T"},"color":null},"7433714147185165618":{"id":7433714147185165618,"name":"T Flip-Flop","position":[425,25],"size":[110,75],"unique":false,"passthrough":true,"inputs":[8096273090428741690],"outputs":[6273669149933044936],"state":{"Direct":0},"output_state":0,"decoration":{"Label":"T"},"color":null},"3596810306868589121":{"id":3596810306868589121,"name":"Not","position":[125,125],"size":[75,75],"unique":false,"passthrough":true,"inputs":[895923654972995817],"outputs":[14744021733325809919],"state":{"Direct":0},"output_state":340282366920938463463374607431768211454,"decoration":{"NotLabel":"1"},"color":null},"12911774693485193003":{"id":12911774693485193003,"name":"Not","position":[25,25],"size":[75,75],"unique":false,"passthrough":true,"inputs":[895923654972995817],"outputs":[895923654972995817],"state":{"Direct":0},"output_state":340282366920938463463374607431768211455,"decoration":{"NotLabel":"1"},"color":null},"10497699952113116465":{"id":10497699952113116465,"name":"Not","position":[425,125],"size":[75,75],"unique":false,"passthrough":true,"inputs":[8096273090428741690],"outputs":[7588132761653207674],"state":{"Direct":0},"output_state":340282366920938463463374607431768211455,"decoration":{"NotLabel":"1"},"color":null},"16190443890770760703":{"id":16190443890770760703,"name":"Lamp","position":[525,225],"size":[75,75],"unique":false,"passthrough":true,"inputs":[7588132761653207674],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":true},"color":null},"11961212448952297802":{"id":11961212448952297802,"name":"Not","position":[575,125],"size":[75,75],"unique":false,"passthrough":true,"inputs":[6273669149933044936],"outputs":[3134920317199890587],"state":{"Direct":0},"output_state":340282366920938463463374607431768211455,"decoration":{"NotLabel":"1"},"color":null},"13998638750705030997":{"id":13998638750705030997,"name":"Lamp","position":[225,225],"size":[75,75],"unique":false,"passthrough":true,"inputs":[14744021733325809919],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":false},"color":null},"2861656550823996258":{"id":2861656550823996258,"name":"Not","position":[275,125],"size":[75,75],"unique":false,"passthrough":true,"inputs":[9271995660626457038],"outputs":[10440987279344356270],"state":{"Direct":0},"output_state":340282366920938463463374607431768211455,"decoration":{"NotLabel":"1"},"color":null}},"connections":{"9271995660626457038":{"id":9271995660626457038,"active":false,"origin":{"Output":[17585780629157557337,0]},"segments":{"4695115420018433688":{"Block":[2861656550823996258,0]},"13156344502743620961":{"Block":[17325373593975782801,0]}}},"3134920317199890587":{"id":3134920317199890587,"active":true,"origin":{"Output":[11961212448952297802,0]},"segments":{"10612020911719657597":{"Block":[7846093778520356818,0]}}},"7588132761653207674":{"id":7588132761653207674,"active":true,"origin":{"Output":[10497699952113116465,0]},"segments":{"18078718082634527518":{"Block":[16190443890770760703,0]}}},"10440987279344356270":{"id":10440987279344356270,"active":true,"origin":{"Output":[2861656550823996258,0]},"segments":{"4827306972552525760":{"Block":[5354188438588814584,0]}}},"6273669149933044936":{"id":6273669149933044936,"active":false,"origin":{"Output":[7433714147185165618,0]},"segments":{"4117633801655392617":{"Block":[11961212448952297802,0]}}},"895923654972995817":{"id":895923654972995817,"active":true,"origin":{"Output":[12911774693485193003,0]},"segments":{"9702505521668695002":{"Block":[12911774693485193003,0]},"7187239749178552577":{"Block":[3596810306868589121,0]},"8965177135727669501":{"Block":[17585780629157557337,0]}}},"14744021733325809919":{"id":14744021733325809919,"active":false,"origin":{"Output":[3596810306868589121,0]},"segments":{"14019196343756079993":{"Block":[13998638750705030997,0]}}},"8096273090428741690":{"id":8096273090428741690,"active":false,"origin":{"Output":[17325373593975782801,0]},"segments":{"6814540146988114983":{"Block":[7433714147185165618,0]},"2534036242052040424":{"Block":[10497699952113116465,0]}}}},"states":[{"blocks":{},"connections":{}}]},"tps":10}
{"modules":{},"main_plot":{"blocks":{"3310398795976558300":{"id":3310398795976558300,"name":"High","position":[271,-7],"size":[75,75],"unique":false,"passthrough":true,"inputs":[],"outputs":[4672615323058003302],"state":{"Direct":0},"output_state":340282366920938463463374607431768211455,"decoration":{"Label":"1"},"color":null},"18193944025558943703":{"id":18193944025558943703,"name":"Lamp","position":[489,252],"size":[75,75],"unique":false,"passthrough":true,"inputs":[17115819071537323242],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":false},"color":null},"13764986653674854796":{"id":13764986653674854796,"name":"T Flip-Flop","position":[522,118],"size":[110,100],"unique":false,"passthrough":true,"inputs":[4672615323058003302,13779025196202125909],"outputs":[5266926683341780562,null],"state":{"Direct":2},"output_state":340282366920938463463374607431768211450,"decoration":{"Label":"T"},"color":null},"15743551746034192670":{"id":15743551746034192670,"name":"T Flip-Flop","position":[233,118],"size":[110,100],"unique":false,"passthrough":true,"inputs":[4672615323058003302,11679388001501554237],"outputs":[7635165452054149733,3895035867946493813],"state":{"Direct":2},"output_state":340282366920938463463374607431768211450,"decoration":{"Label":"T"},"color":null},"2185657683087893488":{"id":2185657683087893488,"name":"T Flip-Flop","position":[90,118],"size":[110,100],"unique":false,"passthrough":true,"inputs":[4672615323058003302,9789689151405107917],"outputs":[9676552777124640418,11679388001501554237],"state":{"Direct":0},"output_state":340282366920938463463374607431768211454,"decoration":{"Label":"T"},"color":null},"8129519016779213416":{"id":8129519016779213416,"name":"Button","position":[11,251],"size":[75,75],"unique":false,"passthrough":true,"inputs":[],"outputs":[9789689151405107917],"state":{"Direct":0},"output_state":0,"decoration":{"Button":false},"color":null},"3757203818844583619":{"id":3757203818844583619,"name":"Lamp","position":[345,251],"size":[75,75],"unique":false,"passthrough":true,"inputs":[7635165452054149733],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":false},"color":null},"3553480486059670029":{"id":3553480486059670029,"name":"T Flip-Flop","position":[379,118],"size":[110,100],"unique":false,"passthrough":true,"inputs":[4672615323058003302,3895035867946493813],"outputs":[17115819071537323242,13779025196202125909],"state":{"Direct":2},"output_state":340282366920938463463374607431768211450,"decoration":{"Label":"T"},"color":null},"16642788480495327288":{"id":16642788480495327288,"name":"Lamp","position":[633,250],"size":[75,75],"unique":false,"passthrough":true,"inputs":[5266926683341780562],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":false},"color":null},"4986905588854076751":{"id":4986905588854076751,"name":"Lamp","position":[200,250],"size":[75,75],"unique":false,"passthrough":true,"inputs":[9676552777124640418],"outputs":[],"state":{"Direct":0},"output_state":0,"decoration":{"Lamp":false},"color":null}},"connections":{"11679388001501554237":{"id":11679388001501554237,"active":true,"origin":{"Output":[2185657683087893488,1]},"segments":{"1959939874093137484":{"Block":[15743551746034192670,1]}}},"7635165452054149733":{"id":7635165452054149733,"active":false,"origin":{"Output":[15743551746034192670,0]},"segments":{"2823241741726666062":{"Block":[3757203818844583619,0]}}},"5266926683341780562":{"id":5266926683341780562,"active":false,"origin":{"Output":[13764986653674854796,0]},"segments":{"16401291941952202028":{"Block":[16642788480495327288,0]}}},"4672615323058003302":{"id":4672615323058003302,"active":true,"origin":{"Output":[3310398795976558300,0]},"segments":{"11400273655492572696":{"Block":[13764986653674854796,0]},"5093895033105929308":{"Block":[3553480486059670029,0]},"4700516162004995735":{"Block":[2185657683087893488,0]},"15591164720030407252":{"Block":[15743551746034192670,0]}}},"3895035867946493813":{"id":3895035867946493813,"active":true,"origin":{"Output":[15743551746034192670,1]},"segments":{"4266020639005941764":{"Block":[3553480486059670029,1]}}},"9789689151405107917":{"id":9789689151405107917,"active":false,"origin":{"Output":[8129519016779213416,0]},"segments":{"10163194800539781641":{"Block":[2185657683087893488,1]}}},"9676552777124640418":{"id":9676552777124640418,"active":false,"origin":{"Output":[2185657683087893488,0]},"segments":{"6387114459403000980":{"Block":[4986905588854076751,0]}}},"13779025196202125909":{"id":13779025196202125909,"active":true,"origin":{"Output":[3553480486059670029,1]},"segments":{"18124619394754504424":{"Block":[13764986653674854796,1]}}},"17115819071537323242":{"id":17115819071537323242,"active":false,"origin":{"Output":[3553480486059670029,0]},"segments":{"14049412917095013675":{"Block":[18193944025558943703,0]}}}},"states":[{"blocks":{},"connections":{}}]},"tps":10}
51 changes: 46 additions & 5 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,14 +323,28 @@ lazy_static! {
),
);

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

builtins.insert(
"T Flip-Flop",
Builtin::new(
Module::new_builtin(
"T Flip-Flop",
Category::FlipFlop,
1,
1,
2,
2,
Decoration::Label("T".to_string()),
),
t_flip_flop,
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 | !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.bytes() & 1
instance.set_bytes((instance.bytes() & !0b10) | (input & 0b10));
instance.bytes() & 1 | !instance.bytes() << 1
}

0 comments on commit 8559ea5

Please sign in to comment.