-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '14-create-multiplexers-package'
- Loading branch information
Showing
10 changed files
with
1,000 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
PyCircTools Multiplexer module Exceptions. | ||
""" | ||
|
||
|
||
class MultiplexerException(Exception): | ||
""" | ||
MultiplexerException is the parent Exception class for all PyCircTools.Multiplexers related Exceptions | ||
""" | ||
def __init__(self, msg="Multiplexer Exception"): | ||
self.msg = msg | ||
super().__init__(self.msg) | ||
|
||
|
||
class NonExistingInput(MultiplexerException): | ||
""" | ||
NonExistingInput is raised when a Multiplexer/Demultiplexer doesn't have the input asked for. | ||
""" | ||
def __init__(self, requestedInput=None): | ||
if requestedInput is not None: | ||
self.msg = "Input " + str(requestedInput) + " does not exist." | ||
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 " + str(requestedInput) + " does not exist." | ||
else: | ||
self.msg = "Set control signal does not exist." | ||
super().__init__(self.msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal | ||
from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue | ||
|
||
|
||
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) | ||
|
||
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 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] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from CTools.Exceptions.MultiplexersExceptions import NonExistingInput | ||
from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue | ||
|
||
|
||
class Mux2to1: | ||
""" | ||
2 to 1 Multiplexer. Takes one Set input. | ||
""" | ||
def __init__(self): | ||
""" | ||
Mux2to1 class constructor method. | ||
""" | ||
self.input = [False, False] | ||
self.set = False | ||
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, either 0 or 1. | ||
: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): | ||
""" | ||
Method get_set gets the value of the set control signal. | ||
:return: Value of the set control signal. | ||
:rtype: bool | ||
""" | ||
return self.set | ||
|
||
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) | ||
|
||
if type(value) is not bool: | ||
raise NotTruthValue | ||
|
||
self.input[num] = value | ||
self.output = self.__calculate_output() | ||
return self | ||
|
||
def set_set(self, value): | ||
""" | ||
Method set_set sets the value of the set control signal to the desired truth value. | ||
:param value: Desired value of the set control signal. | ||
:type value: bool | ||
:raises NotTruthValue: Raised when value's type is not bool. | ||
""" | ||
if type(value) is not bool: | ||
raise NotTruthValue | ||
|
||
self.set = value | ||
self.output = self.__calculate_output() | ||
return self | ||
|
||
def __calculate_output(self): | ||
""" | ||
Private method __calculate_output calculates the output signal using the 2-to-1 mux formula. | ||
:return: Value of the output signal. | ||
:rtype: bool | ||
""" | ||
return (self.input[0] and not self.set) or (self.input[1] and self.set) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal | ||
from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue | ||
|
||
|
||
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 4 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 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] | ||
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 |
Oops, something went wrong.