Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added SR Latch module #9917

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions boolean_algebra/sr_latch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
SR latch (this is a cross-coupled NOR implementation;
for a cross-coupled NAND, just complement the inputs before applying them):
is a simple memory element that stores 1 bit of information
State table:
| Input 1(set pin) | Input 2(reset pin) | q (not q) |
| 0 | 0 | no change |
| 0 | 1 | 0 1 |
| 1 | 0 | 1 0 |
| 1 | 1 | undefined |
Note: get_current_state() return value of [q,!q]
"""


class SrLatch:
"""
Example:
>>> sr_latch = SrLatch(True)
>>> sr_latch.get_current_state()
[True, False]
>>> sr_latch.set_current_state(False,True)
>>> sr_latch.get_current_state()
[False, True]
>>> sr_latch.set_current_state(False,False)
>>> sr_latch.get_current_state()
[False, True]
>>> sr_latch.set_current_state(True,True)
Traceback (most recent call last):
...
ValueError: undefined state.
"""

def __init__(self, initial_state: bool) -> None:
self.__initial_state = initial_state

def get_current_state(self) -> list:
return [self.__initial_state, not self.__initial_state]

def set_current_state(self, set_pin: bool, reset_pin: bool) -> None:
if set_pin and reset_pin:
raise ValueError("undefined state.")
elif not set_pin and reset_pin:
self.__initial_state = False
elif set_pin and not reset_pin:
self.__initial_state = True


if __name__ == "__main__":
from doctest import testmod

testmod()