From d3dcc02364fbf6c0909e52515bd85808639882ec Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:20:29 +0200 Subject: [PATCH 01/22] Added test. Tests not imported from main. --- tests/Multiplexers_tests/__init__.py | 0 tests/Multiplexers_tests/test_Mux2to1.py | 46 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/Multiplexers_tests/__init__.py create mode 100644 tests/Multiplexers_tests/test_Mux2to1.py diff --git a/tests/Multiplexers_tests/__init__.py b/tests/Multiplexers_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Multiplexers_tests/test_Mux2to1.py b/tests/Multiplexers_tests/test_Mux2to1.py new file mode 100644 index 0000000..fd41d91 --- /dev/null +++ b/tests/Multiplexers_tests/test_Mux2to1.py @@ -0,0 +1,46 @@ +from CTools import NotTruthValue, NonExistingInput +from CTools.Multiplexers import Mux2to1 + + +def test_Mux2to1(): + errors = [] + mux = Mux2to1() + + if mux.get_output(): + errors.append("Base get output test not passed!") + + if mux.set_set(True).get_output(): + errors.append("False, False with set to True test not passed!") + + if mux.set_input(1, True).set_set(False).get_output(): + errors.append("False, True with set to False test not passed!") + + if not mux.set_set(True).get_output(): + errors.append("False, True with set to True test not passed!") + + if not mux.set_input(0, True).set_input(1, False).set_set(False).get_output(): + errors.append("True, False with set to False test not passed!") + + if mux.set_set(True).get_output(): + errors.append("True, False with set to True test not passed!") + + if not mux.set_input(1, True).set_set(False).get_output(): + errors.append("True, True with set to False test not passed!") + + if not mux.set_set(True).get_output(): + errors.append("True, True with set to True test not passed!") + + try: + mux.set_set(1) + errors.append("Set to not truth test not passed!") + except NotTruthValue: + pass + + try: + mux.set_input(-1, True) + errors.append("Input to not existing input test not passed!") + except NonExistingInput: + pass + + assert not errors, "errors occured:\n{}".format("\n".join(errors)) + \ No newline at end of file From afc7d1cb3fe55b2a53ac04e0010f80a8c58afcb7 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:22:14 +0200 Subject: [PATCH 02/22] Added Mux4to1 import to __init__ --- CTools/Multiplexers/Mux4to1.py | 0 CTools/Multiplexers/__init__.py | 1 + 2 files changed, 1 insertion(+) create mode 100644 CTools/Multiplexers/Mux4to1.py diff --git a/CTools/Multiplexers/Mux4to1.py b/CTools/Multiplexers/Mux4to1.py new file mode 100644 index 0000000..e69de29 diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py index 38604cd..695961b 100644 --- a/CTools/Multiplexers/__init__.py +++ b/CTools/Multiplexers/__init__.py @@ -5,3 +5,4 @@ """ from CTools.Multiplexers.Mux2to1 import * +from CTools.Multiplexers.Mux4to1 import * From 8288f77b02de8777049fb9c51f05a82c68953168 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:33:32 +0200 Subject: [PATCH 03/22] Added new Exception to Multiplexers Added the NonExistingControlSignal Exception --- CTools/Exceptions/MultiplexersExceptions.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/CTools/Exceptions/MultiplexersExceptions.py index 620e939..5daf742 100644 --- a/CTools/Exceptions/MultiplexersExceptions.py +++ b/CTools/Exceptions/MultiplexersExceptions.py @@ -22,3 +22,15 @@ def __init__(self, requestedInput=None): else: self.msg = "Input requested does not exist." super().__init__(self.msg) + + +class NonExistingControlSignal(MultiplexerException): + """ + NonExistingControlSignal is raised when a Multiplexer/Demultiplexer doesn't have the set control signal asked for. + """ + def __init__(self, requestedInput=None): + if requestedInput is not None: + self.msg = "Set control signal " + requestedInput + " does not exist." + else: + self.msg = "Set control signal does not exist." + super().__init__(self.msg) From 9b009555375bd86c7dd05a3879dc6ca6c1303995 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:35:35 +0200 Subject: [PATCH 04/22] Fix to Mux2to1.py --- CTools/Multiplexers/Mux2to1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CTools/Multiplexers/Mux2to1.py b/CTools/Multiplexers/Mux2to1.py index 29065d3..4ffa437 100644 --- a/CTools/Multiplexers/Mux2to1.py +++ b/CTools/Multiplexers/Mux2to1.py @@ -80,6 +80,7 @@ def set_set(self, value): raise NotTruthValue self.set = value + self.output = self.__calculate_output() return self def __calculate_output(self): @@ -89,4 +90,4 @@ def __calculate_output(self): :return: Value of the output signal. :rtype: bool """ - return (self.input[0] and not(self.set)) or (self.input[1] and self.set) + return (self.input[0] and not self.set) or (self.input[1] and self.set) From 6f4ec5b364a5e05c7b8668f05c802ce486059688 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:51:18 +0200 Subject: [PATCH 05/22] Implemented 4 to 1 Multiplexer --- CTools/Multiplexers/Mux4to1.py | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/CTools/Multiplexers/Mux4to1.py b/CTools/Multiplexers/Mux4to1.py index e69de29..9cd2757 100644 --- a/CTools/Multiplexers/Mux4to1.py +++ b/CTools/Multiplexers/Mux4to1.py @@ -0,0 +1,47 @@ +from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal +from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue + + +class Mux4to1: + def __init__(self): + self.input = [False] * 4 + self.set = [False] * 2 + self.output = self.__calculate_output() + + def get_input(self, num): + return self.input[num] + + def get_set(self, setNum): + return self.set[setNum] + + def get_output(self): + return self.output + + def set_input(self, num, value): + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + + if type(value) is not bool: + raise NotTruthValue + + self.input[num] = value + self.output = self.__calculate_output() + return self + + def set_set(self, setNum, value): + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal(setNum) + + if type(value) is not bool: + raise NotTruthValue + + self.set[setNum] = value + self.output = self.__calculate_output() + return self + + def __calculate_output(self): + first_minterm = not self.set[0] and not self.set[1] and self.input[0] + second_minterm = self.set[0] and not self.set[1] and self.input[1] + third_minterm = not self.set[0] and self.set[1] and self.input[2] + fourth_minterm = self.set[0] and self.set[1] and self.input[3] + return first_minterm or second_minterm or third_minterm or fourth_minterm From f8a7c158e4e909f9b81cdeb4ffbc3052e63da9a1 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 17:01:14 +0200 Subject: [PATCH 06/22] Fixed typo in Mux2to1 --- CTools/Multiplexers/Mux2to1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CTools/Multiplexers/Mux2to1.py b/CTools/Multiplexers/Mux2to1.py index 4ffa437..4114ced 100644 --- a/CTools/Multiplexers/Mux2to1.py +++ b/CTools/Multiplexers/Mux2to1.py @@ -20,7 +20,7 @@ def get_input(self, num): :param num: Number of the input you want to get, either 0 or 1. :type num: int - :raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :raises NonExistingInput: Raised when a Multiplexer/Demultiplexer doesn't have the input asked for. :return: Value of the desired input (num). :rtype: bool """ From 25c736aa3486f6cee0fe6b79b9e85c4f302d1fe0 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 17:10:09 +0200 Subject: [PATCH 07/22] Implemented 4 to 1 Multiplexer --- CTools/Multiplexers/Mux4to1.py | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/CTools/Multiplexers/Mux4to1.py b/CTools/Multiplexers/Mux4to1.py index 9cd2757..063c9fc 100644 --- a/CTools/Multiplexers/Mux4to1.py +++ b/CTools/Multiplexers/Mux4to1.py @@ -3,21 +3,67 @@ class Mux4to1: + """ + 4 to 1 Multiplexer. Takes two bits as a Set input. + """ def __init__(self): + """ + Mux4to1 class constructor method. + """ self.input = [False] * 4 self.set = [False] * 2 self.output = self.__calculate_output() def get_input(self, num): + """ + Method get_input is used to get the value of input[num] + + :param num: Number of the input you want to get. It is a number from 0 to 3. + :type num: int + :raises NonExistingInput: Raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :return: Value of the desired input (num). + :rtype: bool + """ + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + return self.input[num] def get_set(self, setNum): + """ + Method get_set gets the value of the set[setNum] control signal. + + :param setNum: Number of the set control signal you want to get, either 0 or 1. + :type setNum: int + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :return: Value of the desired set control signal (setNum). + :rtype: bool + """ + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal + return self.set[setNum] def get_output(self): + """ + Method get_output gets the output of the 2 to 1 Multiplexer. + + :return: Value of the output. + :rtype: bool + """ return self.output def set_input(self, num, value): + """ + Method set_input sets a certain input to the desired value, either True or False. + + :param num: Number of the input selected. + :type num: int + :param value: Desired value of the input. + :type value: bool + :raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :raises NotTruthValue: Raised when value's type is not bool. + """ if num >= len(self.input) or num < 0: raise NonExistingInput(num) @@ -29,6 +75,16 @@ def set_input(self, num, value): return self def set_set(self, setNum, value): + """ + Method set_set sets the value of the set control signal to the desired truth value. + + :param setNum: Number of the set control signal selected. + :type setNum: int + :param value: Desired value of the set control signal. + :type value: bool + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :raises NotTruthValue: Raised when value's type is not bool. + """ if setNum >= len(self.set) or setNum < 0: raise NonExistingControlSignal(setNum) @@ -40,6 +96,12 @@ def set_set(self, setNum, value): return self def __calculate_output(self): + """ + Private method __calculate_output calculates the output signal using the 4-to-1 mux formula. + + :return: Value of the output signal. + :rtype: bool + """ first_minterm = not self.set[0] and not self.set[1] and self.input[0] second_minterm = self.set[0] and not self.set[1] and self.input[1] third_minterm = not self.set[0] and self.set[1] and self.input[2] From ebfe7c746e1b5e9c52f4c4888fe4afe110e61a8c Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 18:19:00 +0200 Subject: [PATCH 08/22] Fixed Exceptions --- CTools/Exceptions/MultiplexersExceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/CTools/Exceptions/MultiplexersExceptions.py index 5daf742..5785411 100644 --- a/CTools/Exceptions/MultiplexersExceptions.py +++ b/CTools/Exceptions/MultiplexersExceptions.py @@ -30,7 +30,7 @@ class NonExistingControlSignal(MultiplexerException): """ def __init__(self, requestedInput=None): if requestedInput is not None: - self.msg = "Set control signal " + requestedInput + " does not exist." + self.msg = "Set control signal " + str(requestedInput) + " does not exist." else: self.msg = "Set control signal does not exist." super().__init__(self.msg) From 45dae163010b708d54aa77f1496d81c58b416f68 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 18:20:04 +0200 Subject: [PATCH 09/22] Fixed Exceptions --- CTools/Exceptions/MultiplexersExceptions.py | 2 +- tests/Multiplexers_tests/test_Mux2to1.py | 1 - tests/Multiplexers_tests/test_Mux4to1.py | 63 +++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/Multiplexers_tests/test_Mux4to1.py diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/CTools/Exceptions/MultiplexersExceptions.py index 5785411..4a17289 100644 --- a/CTools/Exceptions/MultiplexersExceptions.py +++ b/CTools/Exceptions/MultiplexersExceptions.py @@ -18,7 +18,7 @@ class NonExistingInput(MultiplexerException): """ def __init__(self, requestedInput=None): if requestedInput is not None: - self.msg = "Input " + requestedInput + " does not exist." + self.msg = "Input " + str(requestedInput) + " does not exist." else: self.msg = "Input requested does not exist." super().__init__(self.msg) diff --git a/tests/Multiplexers_tests/test_Mux2to1.py b/tests/Multiplexers_tests/test_Mux2to1.py index fd41d91..0139c3e 100644 --- a/tests/Multiplexers_tests/test_Mux2to1.py +++ b/tests/Multiplexers_tests/test_Mux2to1.py @@ -43,4 +43,3 @@ def test_Mux2to1(): pass assert not errors, "errors occured:\n{}".format("\n".join(errors)) - \ No newline at end of file diff --git a/tests/Multiplexers_tests/test_Mux4to1.py b/tests/Multiplexers_tests/test_Mux4to1.py new file mode 100644 index 0000000..83b7746 --- /dev/null +++ b/tests/Multiplexers_tests/test_Mux4to1.py @@ -0,0 +1,63 @@ +from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput +from CTools.Multiplexers import Mux4to1 + + +def to_bool(value): + if value == 0: + return False + else: + return True + + +def get_expected(val): + if val[0] == 0 and val[1] == 0: + return 3 + elif val[0] == 0 and val[1] == 1: + return 2 + elif val[0] == 1 and val[1] == 0: + return 1 + elif val[0] == 1 and val[1] == 1: + return 0 + + +def test_Mux4to1(): + errors = [] + mux = Mux4to1() + inputs = [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), + (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), + (1, 1, 1, 0), (1, 1, 1, 1)] + set_values = [(0, 0), (0, 1), (1, 0), (1, 1)] + for val in inputs: + mux.set_input(0, to_bool(val[3])).set_input(1, to_bool(val[2])).set_input(2, to_bool(val[1]))\ + .set_input(3, to_bool(val[0])) + for set_val in set_values: + mux.set_set(0, to_bool(set_val[1])).set_set(1, to_bool(set_val[0])) + expected = val[get_expected(set_val)] + output = mux.get_output() + if output != expected: + to_append = "{}".format(str(to_bool(val[3]))) + ", {}".format(str(to_bool(val[2]))) + \ + ", {}".format(str(to_bool(val[1]))) + ", {}".format(str(to_bool(val[0]))) + \ + ", set signals {}".format(str(to_bool(set_val[1]))) + \ + ", {}".format(str(to_bool(set_val[0]))) + " test not passed!" + + errors.append(to_append) + + try: + mux.set_set(2, False) + errors.append("Not existing set input test not passed!") + except NonExistingControlSignal: + pass + + try: + mux.set_set(0, 5) + errors.append("Not truth value to set test not passed!") + except NotTruthValue: + pass + + try: + mux.set_input(16, False) + errors.append("Not existing input test not passed!") + except NonExistingInput: + pass + + assert not errors, "errors occured:\n{}".format("\n".join(errors)) From ac5848d29f4a2ce8e5babdf85a62a64852078cb2 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 18:20:35 +0200 Subject: [PATCH 10/22] Fixed typo in test --- tests/Multiplexers_tests/test_Mux4to1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Multiplexers_tests/test_Mux4to1.py b/tests/Multiplexers_tests/test_Mux4to1.py index 83b7746..e9cba7c 100644 --- a/tests/Multiplexers_tests/test_Mux4to1.py +++ b/tests/Multiplexers_tests/test_Mux4to1.py @@ -60,4 +60,4 @@ def test_Mux4to1(): except NonExistingInput: pass - assert not errors, "errors occured:\n{}".format("\n".join(errors)) + assert not errors, "errors occurred:\n{}".format("\n".join(errors)) From d00efc7a69aad61dded72a190fe7944cef6ffc3c Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 19:14:36 +0200 Subject: [PATCH 11/22] Implemented Mux8to1 --- CTools/Multiplexers/Mux8to1.py | 114 +++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 CTools/Multiplexers/Mux8to1.py diff --git a/CTools/Multiplexers/Mux8to1.py b/CTools/Multiplexers/Mux8to1.py new file mode 100644 index 0000000..115a226 --- /dev/null +++ b/CTools/Multiplexers/Mux8to1.py @@ -0,0 +1,114 @@ +from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal +from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue + + +class Mux8to1: + """ + 8 to 1 Multiplexeer. Takes three bits as Set input. + """ + def __init__(self): + """ + Mux8to1 class constructor method. + """ + self.input = [False] * 8 + self.set = [False] * 3 + self.output = self.__calculate_output() + + def get_input(self, num): + """ + Method get_input is used to get the value of input[num] + + :param num: Number of the input you want to get. It is a number from 0 to 7. + :type num: int + :raises NonExistingInput: Raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :return: Value of the desired input (num). + :rtype: bool + """ + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + + return self.input[num] + + def get_set(self, setNum): + """ + Method get_set gets the value of the set[setNum] control signal. + + :param setNum: Number of the set control signal you want to get, ranges from 0 to 3. + :type setNum: int + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :return: Value of the desired set control signal (setNum). + :rtype: bool + """ + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal + + return self.set[setNum] + + def get_output(self): + """ + Method get_output gets the output of the 8 to 1 Multiplexer. + + :return: Value of the output. + :rtype: bool + """ + return self.output + + def set_input(self, num, value): + """ + Method set_input sets a certain input to the desired value, either True or False. + + :param num: Number of the input selected. + :type num: int + :param value: Desired value of the input. + :type value: bool + :raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :raises NotTruthValue: Raised when value's type is not bool. + """ + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + + if type(value) is not bool: + raise NotTruthValue + + self.input[num] = value + self.output = self.__calculate_output() + return self + + def set_set(self, setNum, value): + """ + Method set_set sets the value of the set control signal to the desired truth value. + + :param setNum: Number of the set control signal selected. + :type setNum: int + :param value: Desired value of the set control signal. + :type value: bool + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :raises NotTruthValue: Raised when value's type is not bool. + """ + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal(setNum) + + if type(value) is not bool: + raise NotTruthValue + + self.set[setNum] = value + self.output = self.__calculate_output() + return self + + def __calculate_output(self): + """ + Private method __calculate_output calculates the output signal using the 8-to-1 mux formula. + + :return: Value of the output signal. + :rtype: bool + """ + first = not self.set[2] and not self.set[1] and not self.set[0] and self.input[0] + second = not self.set[2] and not self.set[1] and self.set[0] and self.input[1] + third = not self.set[2] and self.set[1] and not self.set[0] and self.input[2] + fourth = not self.set[2] and self.set[1] and self.set[0] and self.input[3] + fifth = self.set[2] and not self.set[1] and not self.set[0] and self.input[4] + sixth = self.set[2] and not self.set[1] and self.set[0] and self.input[5] + seventh = self.set[2] and self.set[1] and not self.set[0] and self.input[6] + eight = self.set[2] and self.set[1] and self.set[0] and self.input[7] + + return first or second or third or fourth or fifth or sixth or seventh or eight From e37191cbd9d3bd249b663d3ea55723449f879eef Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 19:15:12 +0200 Subject: [PATCH 12/22] Fixed typo in line 49 in Mux4to1.py --- CTools/Multiplexers/Mux4to1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CTools/Multiplexers/Mux4to1.py b/CTools/Multiplexers/Mux4to1.py index 063c9fc..9473b88 100644 --- a/CTools/Multiplexers/Mux4to1.py +++ b/CTools/Multiplexers/Mux4to1.py @@ -46,7 +46,7 @@ def get_set(self, setNum): def get_output(self): """ - Method get_output gets the output of the 2 to 1 Multiplexer. + Method get_output gets the output of the 4 to 1 Multiplexer. :return: Value of the output. :rtype: bool From c15b54a2a29b085aeefa04983520479c7c2c1c62 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Thu, 23 Jun 2022 19:46:29 +0200 Subject: [PATCH 13/22] Tested 8 to 1 Multiplexer --- CTools/Multiplexers/__init__.py | 1 + tests/Multiplexers_tests/test_Mux8to1.py | 76 ++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tests/Multiplexers_tests/test_Mux8to1.py diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py index 695961b..1404323 100644 --- a/CTools/Multiplexers/__init__.py +++ b/CTools/Multiplexers/__init__.py @@ -6,3 +6,4 @@ from CTools.Multiplexers.Mux2to1 import * from CTools.Multiplexers.Mux4to1 import * +from CTools.Multiplexers.Mux8to1 import * diff --git a/tests/Multiplexers_tests/test_Mux8to1.py b/tests/Multiplexers_tests/test_Mux8to1.py new file mode 100644 index 0000000..84d9e1c --- /dev/null +++ b/tests/Multiplexers_tests/test_Mux8to1.py @@ -0,0 +1,76 @@ +import itertools + +from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput +from CTools.Multiplexers import Mux8to1 + + +def generate_inputs(num): + vals = [] + for i in itertools.product([False, True], repeat=num): + vals.append(i) + + return vals + + +def get_expected(val): + if val[0] == 0 and val[1] == 0 and val[2] == 0: + return 7 + if val[0] == 0 and val[1] == 0 and val[2] == 1: + return 6 + if val[0] == 0 and val[1] == 1 and val[2] == 0: + return 5 + if val[0] == 0 and val[1] == 1 and val[2] == 1: + return 4 + if val[0] == 1 and val[1] == 0 and val[2] == 0: + return 3 + if val[0] == 1 and val[1] == 0 and val[2] == 1: + return 2 + if val[0] == 1 and val[1] == 1 and val[2] == 0: + return 1 + if val[0] == 1 and val[1] == 1 and val[2] == 1: + return 0 + + +def test_Mux8to1(): + errors = [] + mux = Mux8to1() + inputs = generate_inputs(8) + set_inputs = generate_inputs(3) + + for val in inputs: + mux.set_input(0, val[7]).set_input(1, val[6]).set_input(2, val[5]).set_input(3, val[4]).set_input(4, val[3])\ + .set_input(5, val[2]).set_input(6, val[1]).set_input(7, val[0]) + + for set_val in set_inputs: + mux.set_set(0, set_val[2]).set_set(1, set_val[1]).set_set(2, set_val[0]) + expected = val[get_expected(set_val)] + output = mux.get_output() + + if output != expected: + to_append = "{}".format(str(val[7])) + ", {}".format(str(val[6])) + ", {}".format(str(val[5])) + \ + ", {}".format(str(val[4])) + ", {}".format(str(val[3])) + ", {}".format(str(val[2])) + \ + ", {}".format(str(val[1])) + ", {}".format(str(val[0])) +\ + ", set signals {}".format(str(set_val[2])) + ", {}".format(str(set_val[1])) + \ + ", {}".format(str(set_val[0])) + \ + " test not passed!" + errors.append(to_append) + + try: + mux.set_set(5, False) + errors.append("Not existing set input test not passed!") + except NonExistingControlSignal: + pass + + try: + mux.set_set(0, 5) + errors.append("Not truth value to set test not passed!") + except NotTruthValue: + pass + + try: + mux.set_input(16, False) + errors.append("Not existing input test not passed!") + except NonExistingInput: + pass + + assert not errors, "errors occurred:\n{}".format("\n".join(errors)) From 94cea4b984c4778c58c79da81814e96813bf8865 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 00:32:51 +0200 Subject: [PATCH 14/22] Implemented Mux16to1 --- CTools/Multiplexers/Mux16to1.py | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 CTools/Multiplexers/Mux16to1.py diff --git a/CTools/Multiplexers/Mux16to1.py b/CTools/Multiplexers/Mux16to1.py new file mode 100644 index 0000000..51cec8d --- /dev/null +++ b/CTools/Multiplexers/Mux16to1.py @@ -0,0 +1,67 @@ +from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal +from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue + + +class Mux16to1: + def __init__(self): + self.input = [False] * 16 + self.set = [False] * 4 + self.output = self.__calculate_output() + + def get_input(self, num): + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + + return self.input[num] + + def get_set(self, setNum): + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal + + return self.set[setNum] + + def get_output(self): + return self.output + + def set_input(self, num, value): + if num >= len(self.input) or num < 0: + raise NonExistingInput(num) + + if type(value) is not bool: + raise NotTruthValue + + self.input[num] = value + self.output = self.__calculate_output() + return self + + def set_set(self, setNum, value): + if setNum >= len(self.set) or setNum < 0: + raise NonExistingControlSignal(setNum) + + if type(value) is not bool: + raise NotTruthValue + + self.set[setNum] = value + self.output = self.__calculate_output() + return self + + def __calculate_output(self): + first = not self.set[3] and not self.set[2] and not self.set[1] and not self.set[0] and self.input[0] + second = not self.set[3] and not self.set[2] and not self.set[1] and self.set[0] and self.input[1] + third = not self.set[3] and not self.set[2] and self.set[1] and not self.set[0] and self.input[2] + fourth = not self.set[3] and not self.set[2] and self.set[1] and self.set[0] and self.input[3] + fifth = not self.set[3] and self.set[2] and not self.set[1] and not self.set[0] and self.input[4] + sixth = not self.set[3] and self.set[2] and not self.set[1] and self.set[0] and self.input[5] + seventh = not self.set[3] and self.set[2] and self.set[1] and not self.set[0] and self.input[6] + eight = not self.set[3] and self.set[2] and self.set[1] and self.set[0] and self.input[7] + ninth = self.set[3] and not self.set[2] and not self.set[1] and not self.set[0] and self.input[8] + tenth = self.set[3] and not self.set[2] and not self.set[1] and self.set[0] and self.input[9] + eleventh = self.set[3] and not self.set[2] and self.set[1] and not self.set[0] and self.input[10] + twelfth = self.set[3] and not self.set[2] and self.set[1] and self.set[0] and self.input[11] + thirteenth = self.set[3] and self.set[2] and not self.set[1] and not self.set[0] and self.input[12] + fourteenth = self.set[3] and self.set[2] and not self.set[1] and self.set[0] and self.input[13] + fifteenth = self.set[3] and self.set[2] and self.set[1] and not self.set[0] and self.input[14] + sixteenth = self.set[3] and self.set[2] and self.set[1] and self.set[0] and self.input[15] + + return first or second or third or fourth or fifth or sixth or seventh or eight or ninth or tenth or eleventh \ + or twelfth or thirteenth or fourteenth or fifteenth or sixteenth From 24e9f513299c86c5411274c2b30c3d2dd574052f Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 00:33:28 +0200 Subject: [PATCH 15/22] Fixed a typo in Mux8to1.py --- CTools/Multiplexers/Mux8to1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CTools/Multiplexers/Mux8to1.py b/CTools/Multiplexers/Mux8to1.py index 115a226..46b3389 100644 --- a/CTools/Multiplexers/Mux8to1.py +++ b/CTools/Multiplexers/Mux8to1.py @@ -4,7 +4,7 @@ class Mux8to1: """ - 8 to 1 Multiplexeer. Takes three bits as Set input. + 8 to 1 Multiplexer. Takes three bits as Set input. """ def __init__(self): """ From 237d0fd31ed40965d86caffe72a300d1f2190d29 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 00:38:16 +0200 Subject: [PATCH 16/22] Added comments for Mux16to1.py --- CTools/Multiplexers/Mux16to1.py | 58 ++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/CTools/Multiplexers/Mux16to1.py b/CTools/Multiplexers/Mux16to1.py index 51cec8d..3e135f1 100644 --- a/CTools/Multiplexers/Mux16to1.py +++ b/CTools/Multiplexers/Mux16to1.py @@ -3,27 +3,67 @@ class Mux16to1: + """ + 16 to 1 Multiplexer. Takes four bits as Set input. + """ def __init__(self): + """ + Mux16to1 class constructor method. + """ self.input = [False] * 16 self.set = [False] * 4 self.output = self.__calculate_output() def get_input(self, num): + """ + Method get_input is used to get the value of input[num] + + :param num: Number of the input you want to get. It is a number from 0 to 15. + :type num: int + :raises NonExistingInput: Raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :return: Value of the desired input (num). + :rtype: bool + """ if num >= len(self.input) or num < 0: raise NonExistingInput(num) return self.input[num] def get_set(self, setNum): + """ + Method get_set gets the value of the set[setNum] control signal. + + :param setNum: Number of the set control signal you want to get, ranges from 0 to 4. + :type setNum: int + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :return: Value of the desired set control signal (setNum). + :rtype: bool + """ if setNum >= len(self.set) or setNum < 0: raise NonExistingControlSignal return self.set[setNum] def get_output(self): + """ + Method get_output gets the output of the 16 to 1 Multiplexer. + + :return: Value of the output. + :rtype: bool + """ return self.output def set_input(self, num, value): + """ + Method set_input sets a certain input to the desired value, either True or False. + + :param num: Number of the input selected. + :type num: int + :param value: Desired value of the input. + :type value: bool + :raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for. + :raises NotTruthValue: Raised when value's type is not bool. + """ if num >= len(self.input) or num < 0: raise NonExistingInput(num) @@ -35,6 +75,16 @@ def set_input(self, num, value): return self def set_set(self, setNum, value): + """ + Method set_set sets the value of the set control signal to the desired truth value. + + :param setNum: Number of the set control signal selected. + :type setNum: int + :param value: Desired value of the set control signal. + :type value: bool + :raises NonExistingControlSignal: Raised when a Multiplexer/Demultiplexer doesn't have setNum control signal. + :raises NotTruthValue: Raised when value's type is not bool. + """ if setNum >= len(self.set) or setNum < 0: raise NonExistingControlSignal(setNum) @@ -46,6 +96,12 @@ def set_set(self, setNum, value): return self def __calculate_output(self): + """ + Private method __calculate_output calculates the output signal using the 16-to-1 mux formula. + + :return: Value of the output signal. + :rtype: bool + """ first = not self.set[3] and not self.set[2] and not self.set[1] and not self.set[0] and self.input[0] second = not self.set[3] and not self.set[2] and not self.set[1] and self.set[0] and self.input[1] third = not self.set[3] and not self.set[2] and self.set[1] and not self.set[0] and self.input[2] @@ -64,4 +120,4 @@ def __calculate_output(self): sixteenth = self.set[3] and self.set[2] and self.set[1] and self.set[0] and self.input[15] return first or second or third or fourth or fifth or sixth or seventh or eight or ninth or tenth or eleventh \ - or twelfth or thirteenth or fourteenth or fifteenth or sixteenth + or twelfth or thirteenth or fourteenth or fifteenth or sixteenth \ No newline at end of file From 3dfa1a0b3faa85bb55e79f6137d4a7723ce73d16 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 00:40:50 +0200 Subject: [PATCH 17/22] Added import in __init__ of Multiplexers package --- CTools/Multiplexers/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py index 1404323..8365d73 100644 --- a/CTools/Multiplexers/__init__.py +++ b/CTools/Multiplexers/__init__.py @@ -7,3 +7,4 @@ from CTools.Multiplexers.Mux2to1 import * from CTools.Multiplexers.Mux4to1 import * from CTools.Multiplexers.Mux8to1 import * +from CTools.Multiplexers.Mux16to1 import * From 70cb801bb252f1e6e4afb8bee97c847bdb3296c5 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 00:56:04 +0200 Subject: [PATCH 18/22] Implemented Mux16to1 test --- CTools/Multiplexers/__init__.py | 1 + tests/Multiplexers_tests/test_Mux16to1.py | 97 +++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/Multiplexers_tests/test_Mux16to1.py diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py index 1404323..8365d73 100644 --- a/CTools/Multiplexers/__init__.py +++ b/CTools/Multiplexers/__init__.py @@ -7,3 +7,4 @@ from CTools.Multiplexers.Mux2to1 import * from CTools.Multiplexers.Mux4to1 import * from CTools.Multiplexers.Mux8to1 import * +from CTools.Multiplexers.Mux16to1 import * diff --git a/tests/Multiplexers_tests/test_Mux16to1.py b/tests/Multiplexers_tests/test_Mux16to1.py new file mode 100644 index 0000000..107d347 --- /dev/null +++ b/tests/Multiplexers_tests/test_Mux16to1.py @@ -0,0 +1,97 @@ +import itertools + +from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput +from CTools.Multiplexers import Mux16to1 + + +def generate_inputs(num): + vals = [] + for i in itertools.product([False, True], repeat=num): + vals.append(i) + + return vals + + +def get_expected(val): + if val[0] == 0 and val[1] == 0 and val[2] == 0 and val[3] == 0: + return 15 + if val[0] == 0 and val[1] == 0 and val[2] == 0 and val[3] == 1: + return 14 + if val[0] == 0 and val[1] == 0 and val[2] == 1 and val[3] == 0: + return 13 + if val[0] == 0 and val[1] == 0 and val[2] == 1 and val[3] == 1: + return 12 + if val[0] == 0 and val[1] == 1 and val[2] == 0 and val[3] == 0: + return 11 + if val[0] == 0 and val[1] == 1 and val[2] == 0 and val[3] == 1: + return 10 + if val[0] == 0 and val[1] == 1 and val[2] == 1 and val[3] == 0: + return 9 + if val[0] == 0 and val[1] == 1 and val[2] == 1 and val[3] == 1: + return 8 + if val[0] == 1 and val[1] == 0 and val[2] == 0 and val[3] == 0: + return 7 + if val[0] == 1 and val[1] == 0 and val[2] == 0 and val[3] == 1: + return 6 + if val[0] == 1 and val[1] == 0 and val[2] == 1 and val[3] == 0: + return 5 + if val[0] == 1 and val[1] == 0 and val[2] == 1 and val[3] == 1: + return 4 + if val[0] == 1 and val[1] == 1 and val[2] == 0 and val[3] == 0: + return 3 + if val[0] == 1 and val[1] == 1 and val[2] == 0 and val[3] == 1: + return 2 + if val[0] == 1 and val[1] == 1 and val[2] == 1 and val[3] == 0: + return 1 + if val[0] == 1 and val[1] == 1 and val[2] == 1 and val[3] == 1: + return 0 + + +def test_Mux16to1(): + errors = [] + mux = Mux16to1() + inputs = generate_inputs(16) + set_inputs = generate_inputs(4) + + for val in inputs: + mux.set_input(0, val[15]).set_input(1, val[14]).set_input(2, val[13]).set_input(3, val[12])\ + .set_input(4, val[11]).set_input(5, val[10]).set_input(6, val[9]).set_input(7, val[8])\ + .set_input(8, val[7]).set_input(9, val[6]).set_input(10, val[5]).set_input(11, val[4])\ + .set_input(12, val[3]).set_input(13, val[2]).set_input(14, val[1]).set_input(15, val[0]) + + for set_val in set_inputs: + mux.set_set(0, set_val[3]).set_set(1, set_val[2]).set_set(2, set_val[1]).set_set(3, set_val[0]) + expected = val[get_expected(set_val)] + output = mux.get_output() + + if output != expected: + to_append = "{}".format(str(val[15])) + ", {}".format(str(val[14])) + \ + ", {}".format(str(val[13])) + ", {}".format(str(val[12])) + ", {}".format(str(val[11])) + \ + ", {}".format(str(val[10])) + ", {}".format(str(val[9])) + ", {}".format(str(val[8])) + \ + ", {}".format(str(val[7])) + ", {}".format(str(val[6])) + ", {}".format(str(val[5])) + \ + ", {}".format(str(val[4])) + ", {}".format(str(val[3])) + ", {}".format(str(val[2])) + \ + ", {}".format(str(val[1])) + ", {}".format(str(val[0])) +\ + ", set signals {}".format(str(set_val[3])) + ", {}".format(str(set_val[2])) + \ + ", {}".format(str(set_val[1])) + ", {}".format(str(set_val[0])) + \ + " test not passed!" + errors.append(to_append) + + try: + mux.set_set(5, False) + errors.append("Not existing set input test not passed!") + except NonExistingControlSignal: + pass + + try: + mux.set_set(0, 5) + errors.append("Not truth value to set test not passed!") + except NotTruthValue: + pass + + try: + mux.set_input(16, False) + errors.append("Not existing input test not passed!") + except NonExistingInput: + pass + + assert not errors, "errors occurred:\n{}".format("\n".join(errors)) From 30dc6250b48a791c4e52f1d6f84e841132059d56 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 01:32:45 +0200 Subject: [PATCH 19/22] Added Multiplexer Module to index + fixed ortography --- README.md | 136 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index d273d96..89f40b3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PyCircTools -PyCircTools is a python package wich contains tools to build circuits using python 3. It is a work in progress, and will be updated frequently to add more +PyCircTools is a python package which contains tools to build circuits using python 3. It is a work in progress, and will be updated frequently to add more modules. ## Table of contents @@ -31,6 +31,23 @@ modules. * [2.5.1. Attributes](#nor-attributes) * [2.5.2. Constructor](#nor-constructor) * [2.5.3. Methods](#nor-methods) +- [3. Multiplexers Module](#multiplexers) + + [3.1. 2-to-1 Multiplexer](#2-1-mux) + * [3.1.1. Attributes](#2-1-attributes) + * [3.1.2. Constructor](#2-1-constructor) + * [3.1.3. Methods](#2-1-methods) + + [3.1. 4-to-1 Multiplexer](#4-1-mux) + * [3.1.1. Attributes](#4-1-attributes) + * [3.1.2. Constructor](#4-1-constructor) + * [3.1.3. Methods](#4-1-methods) + + [3.1. 8-to-1 Multiplexer](#8-1-mux) + * [3.1.1. Attributes](#8-1-attributes) + * [3.1.2. Constructor](#8-1-constructor) + * [3.1.3. Methods](#8-1-methods) + + [3.1. 16-to-1 Multiplexer](#16-1-mux) + * [3.1.1. Attributes](#16-1-attributes) + * [3.1.2. Constructor](#16-1-constructor) + * [3.1.3. Methods](#16-1-methods) - [Exceptions](#exceptions) + [PyCircTools Exceptions](#ctools-exceptions) * [NotTruthValue](#not-truth-value) @@ -57,13 +74,13 @@ at the start of your code, and substitute subpackage with the package you want, ## Logic Gates Module Here is an in depth explanation for the PyCircTools.LogicGates module. LogicGates adds implementation for common logic gates used in circuit design. It allows -to create gates with as many inputs as the user decides (except for the NOT gate). For further reference about each of the gates, check it's section in +to create gates with as many inputs as the user decides (except for the NOT gate). For further reference about each of the gates, check its section in this README. ### NOT gate -The **NOT gate** is a simple logic gate wich simply inverts the input. It implements the logical negation (¬) and has one input and one output. The NOT +The **NOT gate** is a simple logic gate which simply inverts the input. It implements the logical negation (¬) and has one input and one output. The NOT gate symbol and truth table is shown below.

NOT gate

@@ -84,7 +101,7 @@ The NOT gate has the following attributes: The constructor of the NOT gate has the following format:

Not()

-Wich doesn't take any parameters and returns a NOT gate with its Input set to True and has its output calculated by the not gate calculate output method) +Which doesn't take any parameters and returns a NOT gate with its Input set to True and has its output calculated by the not gate calculate output method) #### Methods @@ -98,11 +115,11 @@ Gets the input of the gate. It returns a bool containing the requested value. Gets the output of the gate. It returns a bool containing the requested value. - **set_input(bool _value_):** -Sets the input of the gate to the truth value _'value'_, wich is passed as a parameter. +Sets the input of the gate to the truth value _'value'_, which is passed as a parameter. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is: +Private method which calculates the output of the gate. This output is: ``` not(input) ``` @@ -110,7 +127,7 @@ not(input) ### AND gate -The **AND gate** is a logic gate with two or more inputs that implements the logical conjunction (^). It's output is True only when all of the inputs are True. If any of them is set to False, the output willthen be False. Below you can find the AND truth table. +The **AND gate** is a logic gate with two or more inputs that implements the logical conjunction (^). It's output is True only when all the inputs are True. If any of them is set to False, the output will then be False. Below you can find the AND truth table.

AND gate

@@ -130,7 +147,7 @@ The AND gate has the following attributes: The constructor of the AND gate has the following format:

And(int inputNumber)

-Wich takes the inputNumber parameter, an integer set to two by default wich can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the and gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. +Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the and gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. #### Methods @@ -144,23 +161,23 @@ Gets the number of inputs of the gate. It returns an int containing the requeste - **set_input(int _num_, bool _value_):** Sets the input _'num'_ to the truth value _'value'_. Both of them are passed as parameters to the method. - **add_input():** -Adds a new input to the gate, wich defaults to False and updates both output and numOfInputs. +Adds a new input to the gate, which defaults to False and updates both output and numOfInputs. - **remove_input():** Removes the last input and updates both output and numOfInputs. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is: +Private method which calculates the output of the gate. This output is: ``` (input_n and input_n+1) ``` -for all of the inputs of the gate. +for all the inputs of the gate. ### NAND gate -The **NAND gate** is a logic gate with two or more inputs wich produces a False output only when all of its inputs are True. Its output is True in any other case. In other words, its output is calculated by negating the conjunction of all inputs of the gate. Below you can find the NAND truth table. +The **NAND gate** is a logic gate with two or more inputs which produces a False output only when all of its inputs are True. Its output is True in any other case. In other words, its output is calculated by negating the conjunction of all inputs of the gate. Below you can find the NAND truth table. -

NAND gate +

NAND gate

#### Attributes @@ -179,7 +196,7 @@ The NAND gate has the following attributes: The constructor of the NAND gate has the following format:

Nand(int inputNumber)

-Wich takes the inputNumber parameter, an integer set to two by default wich can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the nand gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. +Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the nand gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. #### Methods @@ -193,23 +210,23 @@ Gets the number of inputs of the gate. It returns an int containing the requeste - **set_input(int _num_, bool _value_):** Sets the input _'num'_ to the truth value _'value'_. Both of them are passed as parameters to the method. - **add_input():** -Adds a new input to the gate, wich defaults to False and updates both output and numOfInputs. +Adds a new input to the gate, which defaults to False and updates both output and numOfInputs. - **remove_input():** Removes the last input and updates both output and numOfInputs. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is +Private method which calculates the output of the gate. This output is ``` not(input_n and input_n+1) ``` -for all of the inputs of the gate. +for all the inputs of the gate. ### OR gate -The **OR gate** is a logic gate with two or more inputs wich implements the logical disjunction (∨). It's output is True if any of the inputs is True, and False only when all of the gate's inputs are set to False. Below you can find the OR gate truth table. +The **OR gate** is a logic gate with two or more inputs which implements the logical disjunction (∨). Its output is True if any of the inputs is True, and False only when all the gate's inputs are set to False. Below you can find the OR gate truth table. -

OR gate +

OR gate

#### Attributes @@ -228,7 +245,7 @@ The OR gate has the following attributes: The constructor of the OR gate has the following format:

Or(int inputNumber)

-Wich takes the inputNumber parameter, an integer set to two by default wich can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the or gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. +Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the or gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. #### Methods @@ -242,23 +259,23 @@ Gets the number of inputs of the gate. It returns an int containing the requeste - **set_input(int _num_, bool _value_):** Sets the input _'num'_ to the truth value _'value'_. Both of them are passed as parameters to the method. - **add_input():** -Adds a new input to the gate, wich defaults to False and updates both output and numOfInputs. +Adds a new input to the gate, which defaults to False and updates both output and numOfInputs. - **remove_input():** Removes the last input and updates both output and numOfInputs. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is +Private method which calculates the output of the gate. This output is ``` (input_n or input_n+1) ``` -for all of the inputs of the gate. +for all the inputs of the gate. ### XOR gate The **XOR gate**is a logic gate with two or more inputs whose output is True when the number of True inputs is odd. In any other case, the output value is False. Below you can find the OR gate truth table. -

XOR gate +

XOR gate

#### Attributes @@ -277,7 +294,7 @@ The XOR gate has the following attributes: The constructor of the XOR gate has the following format:

Xor(int inputNumber)

-Wich takes the inputNumber parameter, an integer set to two by default wich can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the xor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. +Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the xor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. #### Methods @@ -291,21 +308,21 @@ Gets the number of inputs of the gate. It returns an int containing the requeste - **set_input(int _num_, bool _value_):** Sets the input _'num'_ to the truth value _'value'_. Both of them are passed as parameters to the method. - **add_input():** -Adds a new input to the gate, wich defaults to False and updates both output and numOfInputs. +Adds a new input to the gate, which defaults to False and updates both output and numOfInputs. - **remove_input():** Removes the last input and updates both output and numOfInputs. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is +Private method which calculates the output of the gate. This output is ``` [(input_n and not(input_n+1)) or (not(input_n) and input_n+1)] ``` -for all of the inputs of the gate. +for all the inputs of the gate. ### NOR gate -The **NOR gate** is a logic gate wich can take two or more inputs. Its output is True only when all of the inputs are False. If any of the inputs is True, the output will be False. In other words, its output is calculated by negating the disjunction of all inputs of the gate. Below you can find the NOR gate truth table. +The **NOR gate** is a logic gate which can take two or more inputs. Its output is True only when all the inputs are False. If any of the inputs is True, the output will be False. In other words, its output is calculated by negating the disjunction of all inputs of the gate. Below you can find the NOR gate truth table.

NOR gate

@@ -326,7 +343,7 @@ The NOR gate has the following attributes: The constructor of the NOR gate has the following format:

Nor(int inputNumber)

-Wich takes the inputNumber parameter, an integer set to two by default wich can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the Nor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. +Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the Nor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber. #### Methods @@ -340,21 +357,72 @@ Gets the number of inputs of the gate. It returns an int containing the requeste - **set_input(int _num_, bool _value_):** Sets the input _'num'_ to the truth value _'value'_. Both of them are passed as parameters to the method. - **add_input():** -Adds a new input to the gate, wich defaults to False and updates both output and numOfInputs. +Adds a new input to the gate, which defaults to False and updates both output and numOfInputs. - **remove_input():** Removes the last input and updates both output and numOfInputs. - **__calculate_output():** -Private method wich calculates the output of the gate. This output is +Private method which calculates the output of the gate. This output is ``` not(input_n or input_n+1) ``` -for all of the inputs of the gate. +for all the inputs of the gate. + + +## Multiplexers Module + + +### 2-to-1 Multiplexer + + +#### Attributes + + +#### Constructor + + +#### Methods + + +### 4-to-1 Multiplexer + + +#### Attributes + + +#### Constructor + + +#### Methods + + +### 8-to-1 Multiplexer + + +#### Attributes + + +#### Constructor + + +#### Methods + + +### 16-to-1 Multiplexer + + +#### Attributes + + +#### Constructor + + +#### Methods ## Exceptions -CTools implements the following Exceptions. They are divided depending on wich module they are intended to use. +CTools implements the following Exceptions. They are divided depending on which module they are intended to use. ### PyCircTools Exceptions From c369027700a398139794c80227c46399a6b8970b Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 02:17:09 +0200 Subject: [PATCH 20/22] Fixed index and added 2-to-1 Mux documentation --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 89f40b3..454222b 100644 --- a/README.md +++ b/README.md @@ -36,18 +36,18 @@ modules. * [3.1.1. Attributes](#2-1-attributes) * [3.1.2. Constructor](#2-1-constructor) * [3.1.3. Methods](#2-1-methods) - + [3.1. 4-to-1 Multiplexer](#4-1-mux) - * [3.1.1. Attributes](#4-1-attributes) - * [3.1.2. Constructor](#4-1-constructor) - * [3.1.3. Methods](#4-1-methods) - + [3.1. 8-to-1 Multiplexer](#8-1-mux) - * [3.1.1. Attributes](#8-1-attributes) - * [3.1.2. Constructor](#8-1-constructor) - * [3.1.3. Methods](#8-1-methods) - + [3.1. 16-to-1 Multiplexer](#16-1-mux) - * [3.1.1. Attributes](#16-1-attributes) - * [3.1.2. Constructor](#16-1-constructor) - * [3.1.3. Methods](#16-1-methods) + + [3.2. 4-to-1 Multiplexer](#4-1-mux) + * [3.2.1. Attributes](#4-1-attributes) + * [3.2.2. Constructor](#4-1-constructor) + * [3.2.3. Methods](#4-1-methods) + + [3.3. 8-to-1 Multiplexer](#8-1-mux) + * [3.3.1. Attributes](#8-1-attributes) + * [3.3.2. Constructor](#8-1-constructor) + * [3.3.3. Methods](#8-1-methods) + + [3.4. 16-to-1 Multiplexer](#16-1-mux) + * [3.4.1. Attributes](#16-1-attributes) + * [3.4.2. Constructor](#16-1-constructor) + * [3.4.3. Methods](#16-1-methods) - [Exceptions](#exceptions) + [PyCircTools Exceptions](#ctools-exceptions) * [NotTruthValue](#not-truth-value) @@ -371,18 +371,59 @@ for all the inputs of the gate. ## Multiplexers Module +Here is an in depth explanation for the PyCircTools.Multiplexers module. Multiplexers adds implementation for multiplexers ranging from 2-to-1 to 16-to-1. +For further reference about each of the multiplexers, check its section in this README. + ### 2-to-1 Multiplexer +The **2-to-1** Multiplexer adds the implementation for this circuit element. Below you will find a picture depicting this mux and its equation. + +

2-to-1 Multiplexer

+ #### Attributes +The 2-to-1 Multiplexer has the following attributes: + +| Name | Description | Type | +| - | - | - | +| **input** | Input of the multiplexer | list[bool] | +| **set** | Set control signals of the multiplexer | bool | +| **output** | Output of the multiplexer | bool | + #### Constructor +The constructor of the 2-to-1 Multiplexer has the following format:
+ +

Mux2to1()

+Which doesn't take any parameters and returns a 2-to-1 Multiplexer with its two inputs set to False, its set control signal set to False and output calculated by the 2-to-1 Mux gate calculate output method + #### Methods +The 2-to-1 Multiplexer has the following methods: + +- **get_input(int _num_):** +Gets the value of the input _num_. It returns the boolean value in input[num]. + +- **get_set():** +Gets the value of the set control signal. It returns a bool with the requested value. + +- **get_output():** +Gets the output of the mux. It returns a bool containing the requested value. + +- **set_input(int _num_, bool _value_):** +Sets the input[num] of the mux to the truth value _'value'_, which is passed as a parameter. + +- **set_set(bool _value_):** +Sets the value of the set control signal to _'value'_. + + +- **__calculate_output():** +Private method which calculates the output of the mux. This output is showed in 2-to-1 Multiplexer chapter. + ### 4-to-1 Multiplexer From f5fcd3503242e4d2a4016f591945a327c3de10b7 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 18:00:25 +0200 Subject: [PATCH 21/22] Added documentation for Multiplexers Have to add documentation for Exceptions as well --- README.md | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/README.md b/README.md index 454222b..b422ac0 100644 --- a/README.md +++ b/README.md @@ -392,6 +392,8 @@ The 2-to-1 Multiplexer has the following attributes: | **set** | Set control signals of the multiplexer | bool | | **output** | Output of the multiplexer | bool | +Note that for this multiplexer, the number of inputs is two and the number of control signals is one. + #### Constructor @@ -427,39 +429,155 @@ Private method which calculates the output of the mux. This output is showed in ### 4-to-1 Multiplexer +The **4-to-1** Multiplexer adds the implementation for this circuit element. Below you will find a picture depicting this mux and its equation. + +

4-to-1 Multiplexer

+ #### Attributes +The 4-to-1 Multiplexer has the following attributes: + +| Name | Description | Type | +| - | - | - | +| **input** | Input of the multiplexer | list[bool] | +| **set** | Set control signals of the multiplexer | list[bool] | +| **output** | Output of the multiplexer | bool | + +Note that for this multiplexer, the number of inputs is four and the number of control signals is two. + #### Constructor +The constructor of the 4-to-1 Multiplexer has the following format:
+ +

Mux4to1()

+Which doesn't take any parameters and returns a 4-to-1 Multiplexer with its four inputs set to False, its set control signals set to False and output calculated by the 4-to-1 Mux gate calculate output method + #### Methods +The 4-to-1 Multiplexer has the following methods: + +- **get_input(int _num_):** +Gets the value of the input _num_. It returns the boolean value in input[num]. + +- **get_set(int _setNum_):** +Gets the value of the set control signal _setNum_. It returns a bool with the requested value. + +- **get_output():** +Gets the output of the mux. It returns a bool containing the requested value. + +- **set_input(int _num_, bool _value_):** +Sets the input[num] of the mux to the truth value _'value'_, which is passed as a parameter. + +- **set_set(int _setNum_, bool _value_):** +Sets the value of the set control signal set[setNum] to _'value'_. Both are passed as parameters. + + +- **__calculate_output():** +Private method which calculates the output of the mux. This output is showed in 4-to-1 Multiplexer chapter. + ### 8-to-1 Multiplexer +The **8-to-1** Multiplexer adds the implementation for this circuit element. It has 8 inputs and the output value is selected by the input number in the set signal. + #### Attributes +The 8-to-1 Multiplexer has the following attributes: + +| Name | Description | Type | +| - | - | - | +| **input** | Input of the multiplexer | list[bool] | +| **set** | Set control signals of the multiplexer | list[bool] | +| **output** | Output of the multiplexer | bool | + +Note that for this multiplexer, the number of inputs is eight and the number of control signals is three. + #### Constructor +The constructor of the 8-to-1 Multiplexer has the following format:
+ +

Mux8to1()

+Which doesn't take any parameters and returns an 8-to-1 Multiplexer with its four inputs set to False, its set control signals set to False and output calculated by the 8-to-1 Mux gate calculate output method + #### Methods +The 8-to-1 Multiplexer has the following methods: + +- **get_input(int _num_):** +Gets the value of the input _num_. It returns the boolean value in input[num]. + +- **get_set(int _setNum_):** +Gets the value of the set control signal _setNum_. It returns a bool with the requested value. + +- **get_output():** +Gets the output of the mux. It returns a bool containing the requested value. + +- **set_input(int _num_, bool _value_):** +Sets the input[num] of the mux to the truth value _'value'_, which is passed as a parameter. + +- **set_set(int _setNum_, bool _value_):** +Sets the value of the set control signal set[setNum] to _'value'_. Both are passed as parameters. + + +- **__calculate_output():** +Private method which calculates the output of the mux. This output is showed in 8-to-1 Multiplexer chapter. + ### 16-to-1 Multiplexer +The **16-to-1** Multiplexer adds the implementation for this circuit element. It has 16 inputs and the output value is selected by the input number in the set signal. + #### Attributes +The 16-to-1 Multiplexer has the following attributes: + +| Name | Description | Type | +| - | - | - | +| **input** | Input of the multiplexer | list[bool] | +| **set** | Set control signals of the multiplexer | list[bool] | +| **output** | Output of the multiplexer | bool | + +Note that for this multiplexer, the number of inputs is sixteen and the number of control signals is four. + #### Constructor +The constructor of the 8-to-1 Multiplexer has the following format:
+ +

Mux16to1()

+Which doesn't take any parameters and returns a 16-to-1 Multiplexer with its sixteen inputs set to False, its set control signals set to False and output calculated by the 16-to-1 Mux gate calculate output method + #### Methods +The 16-to-1 Multiplexer has the following methods: + +- **get_input(int _num_):** +Gets the value of the input _num_. It returns the boolean value in input[num]. + +- **get_set(int _setNum_):** +Gets the value of the set control signal _setNum_. It returns a bool with the requested value. + +- **get_output():** +Gets the output of the mux. It returns a bool containing the requested value. + +- **set_input(int _num_, bool _value_):** +Sets the input[num] of the mux to the truth value _'value'_, which is passed as a parameter. + +- **set_set(int _setNum_, bool _value_):** +Sets the value of the set control signal set[setNum] to _'value'_. Both are passed as parameters. + + +- **__calculate_output():** +Private method which calculates the output of the mux. This output is showed in 16-to-1 Multiplexer chapter. + ## Exceptions @@ -492,6 +610,8 @@ of inputs of any logic gate. NotAnInput is raised when the selected input does not exist. + + ## About From 21ee31641a171ff317c294b8dc9a0b686cae019f Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Fri, 24 Jun 2022 19:09:59 +0200 Subject: [PATCH 22/22] Added Exceptions.MultiplexersExceptions documentation --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index b422ac0..a62d928 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,9 @@ modules. + [Logic gates Exceptions](#logic-gates-exceptions) * [NonPositiveInput](#non-positive-input) * [NotAnInput](#not-an-input) + + [Multiplexer](#multiplexers-exceptions) + * [NonExistingInput](#non-existing-input) + * [NonExistingControlSignal](#non-existing-control-signal) - [About](#about) @@ -610,7 +613,20 @@ of inputs of any logic gate. NotAnInput is raised when the selected input does not exist. + +### Multiplexers Exceptions +These are the exceptions used in the PyCircTools.Multiplexers module. An explanation of each one follows below. + + +#### NonExistingInput + +NonExistingInput is raised when a Multiplexer/Demultiplexer doesn't have the input asked for. It can take an int as an input. This input _'requestedInput'_ is the number which caused the exception. + + +#### NonExistingControlSignal + +NonExistingControlSignal is raised when a Multiplexer/Demultiplexer doesn't have the set control signal asked for. ## About