Changed BasicRegister.__init__() to accept decue instead of a list.
Inherited Counter from BasicRegister.
This commit is contained in:
parent
f1d348616a
commit
c8fb25025c
|
@ -1,4 +1,6 @@
|
|||
from collections import deque
|
||||
from typing import Tuple, List, Any
|
||||
|
||||
from typing_extensions import Self
|
||||
from lib.prettytable import PrettyTable
|
||||
|
||||
|
@ -10,8 +12,8 @@ class BasicRegister:
|
|||
:param deque[bool] memory: The bits stored inside the register.
|
||||
"""
|
||||
|
||||
def __init__(self, memory: list[bool]):
|
||||
self.memory: deque[bool] = deque(memory)
|
||||
def __init__(self, memory: deque[bool]):
|
||||
self.memory: deque[bool] = memory
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
@ -33,18 +35,18 @@ class BasicRegister:
|
|||
"""
|
||||
current_memory_size: int = len(self.memory)
|
||||
return BasicRegister(
|
||||
[False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-resulting_size:]
|
||||
deque([False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-resulting_size:])
|
||||
)
|
||||
|
||||
def negate(self):
|
||||
"""
|
||||
Performs a logical negation on the register.
|
||||
Performs logical negation on the register.
|
||||
"""
|
||||
self.memory = deque([not value for value in self.memory])
|
||||
|
||||
def left_shift(self, shift_in_value: bool = False, bits_shifted: int = 1) -> deque[bool]:
|
||||
"""
|
||||
Shifts the register to the left by a specified number of steps
|
||||
Shifts the register to the left by a specified number of steps.
|
||||
|
||||
:param bool shift_in_value: The value that shifts inside the freed space.
|
||||
:param int bits_shifted: The number of bits by which the register is shifted.
|
||||
|
@ -71,7 +73,7 @@ class BasicRegister:
|
|||
return shifted_bits
|
||||
|
||||
|
||||
class Counter:
|
||||
class Counter(BasicRegister):
|
||||
"""
|
||||
The Counter represents a hardware register specifically designed for countdowns.
|
||||
|
||||
|
@ -79,7 +81,9 @@ class Counter:
|
|||
"""
|
||||
|
||||
def __init__(self, value: int):
|
||||
self.memory: deque[bool] = deque([i == "1" for i in bin(value)[2:]])
|
||||
# memory: deque[bool] = deque([i == "1" for i in bin(value)[2:]])
|
||||
super().__init__(deque([i == "1" for i in bin(value)[2:]]))
|
||||
# self.memory: deque[bool] = deque([i == "1" for i in bin(value)[2:]])
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "".join([str(int(value)) for value in self.memory])
|
||||
|
@ -91,13 +95,13 @@ class Counter:
|
|||
return len(self.memory)
|
||||
|
||||
def decrement(self):
|
||||
self.memory = binary_subtraction(self, BasicRegister([False] * (len(self.memory) - 1) + [True])).memory
|
||||
self.memory = binary_subtraction(self, BasicRegister(deque([False] * (len(self.memory) - 1) + [True]))).memory
|
||||
|
||||
def non_zero(self) -> bool:
|
||||
return any(self.memory)
|
||||
|
||||
|
||||
def get_memory(variable_name: str) -> list[bool]:
|
||||
def get_memory(variable_name: str) -> deque[bool]:
|
||||
"""
|
||||
Reads user input to be used as a memory array.
|
||||
|
||||
|
@ -110,7 +114,7 @@ def get_memory(variable_name: str) -> list[bool]:
|
|||
input_chars: list[str] = list(input(f"Enter {variable_name}: "))
|
||||
|
||||
if all(character in ["0", "1"] for character in input_chars):
|
||||
return [True if character == "1" else False for character in input_chars]
|
||||
return deque([True if character == "1" else False for character in input_chars])
|
||||
else:
|
||||
print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!")
|
||||
|
||||
|
@ -125,7 +129,7 @@ def binary_sum_with_carry(first_term: BasicRegister, second_term: BasicRegister)
|
|||
:return: Register containing the sum and the carry-out bit.
|
||||
:rtype: tuple[BasicRegister, int]
|
||||
"""
|
||||
result_term = BasicRegister([False] * len(first_term))
|
||||
result_term = BasicRegister(deque([False] * len(first_term)))
|
||||
|
||||
carry = False
|
||||
for i in range(len(first_term) - 1, -1, -1):
|
||||
|
@ -166,34 +170,12 @@ def binary_subtraction(minuend: BasicRegister, subtrahend: BasicRegister) -> Bas
|
|||
difference, final_carry = binary_sum_with_carry(minuend, subtrahend)
|
||||
|
||||
if final_carry:
|
||||
return binary_sum(difference, BasicRegister([False] * (len(difference) - 1) + [True]))
|
||||
return binary_sum(difference, BasicRegister(deque([False] * (len(difference) - 1) + [True])))
|
||||
else:
|
||||
difference.negate()
|
||||
return difference
|
||||
|
||||
|
||||
# def align_registers(first_register: BasicRegister, second_register: BasicRegister) \
|
||||
# -> tuple[BasicRegister, BasicRegister]:
|
||||
# """
|
||||
# Aligns two registers by the length of the bigger one.
|
||||
#
|
||||
# :param BasicRegister first_register:
|
||||
# :param BasicRegister second_register:
|
||||
#
|
||||
# :return:
|
||||
# :rtype: tuple[BasicRegister, BasicRegister]
|
||||
# """
|
||||
# first_size = len(first_register)
|
||||
# second_size = len(second_register)
|
||||
#
|
||||
# required_size = max(first_size, second_size)
|
||||
#
|
||||
# if first_size != second_size:
|
||||
# return first_register.adjusted_by_size(required_size), second_register.adjusted_by_size(required_size)
|
||||
# else:
|
||||
# return first_register, second_register
|
||||
|
||||
|
||||
def align_registers(*registers: BasicRegister) -> tuple[BasicRegister, ...]:
|
||||
"""
|
||||
Aligns registers by the length of the bigger one.
|
||||
|
@ -214,12 +196,13 @@ def format_device_state_table(table) -> str:
|
|||
for block in table[1:]:
|
||||
for line in block[:-1]:
|
||||
pt.add_row(line)
|
||||
pt.add_row(block[-1], divider = True)
|
||||
pt.add_row(block[-1], divider=True)
|
||||
|
||||
return pt.get_string()
|
||||
|
||||
|
||||
def binary_multiplication_method_1(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
def binary_multiplication_method_1(first_term: BasicRegister, second_term: BasicRegister) \
|
||||
-> tuple[BasicRegister, list[list[str]]]:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using first method.
|
||||
|
||||
|
@ -232,7 +215,7 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
|
|||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * n)
|
||||
rg1 = BasicRegister(deque([False] * n))
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister(second_term.memory)
|
||||
ct = Counter(n)
|
||||
|
@ -255,13 +238,15 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
|
|||
rg1.right_shift()
|
||||
ct.decrement()
|
||||
|
||||
print(ct.memory)
|
||||
# print(ct.memory)
|
||||
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "RG2 := RG1[1].r(RG2)\nRG1 := 0.r(RG1)\nCT := CT - 1"])))
|
||||
|
||||
return BasicRegister(list(rg1.memory) + list(rg2.memory)), data_table
|
||||
return BasicRegister(rg1.memory + rg2.memory), data_table
|
||||
|
||||
def binary_multiplication_method_2(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
|
||||
def binary_multiplication_method_2(first_term: BasicRegister, second_term: BasicRegister) \
|
||||
-> tuple[BasicRegister, list[list[str]]]:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using second method.
|
||||
|
||||
|
@ -274,14 +259,13 @@ def binary_multiplication_method_2(first_term: BasicRegister, second_term: Basic
|
|||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * (2*n))
|
||||
rg1 = BasicRegister(deque([False] * (2*n)))
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister([False] * n + list(second_term.memory))
|
||||
rg3 = BasicRegister(deque([False] * n + list(second_term.memory)))
|
||||
|
||||
i = 0
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"], []]
|
||||
|
||||
data_table.append([])
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "-"])))
|
||||
|
||||
while any(rg2.memory):
|
||||
|
@ -299,7 +283,9 @@ def binary_multiplication_method_2(first_term: BasicRegister, second_term: Basic
|
|||
|
||||
return rg1, data_table
|
||||
|
||||
def binary_multiplication_method_3(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
|
||||
def binary_multiplication_method_3(first_term: BasicRegister, second_term: BasicRegister) \
|
||||
-> tuple[BasicRegister, list[list[str]]]:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using third method.
|
||||
|
||||
|
@ -314,9 +300,9 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
|
|||
|
||||
data_table = [["iter", "RG2", "RG1", "RG3", "CT", "MicroOperations"]]
|
||||
|
||||
rg1 = BasicRegister([False] * n)
|
||||
rg2 = BasicRegister(list(first_term.memory) + [False])
|
||||
rg3 = BasicRegister([False] * (n+1) + list(second_term.memory))
|
||||
rg1 = BasicRegister(deque([False] * n))
|
||||
rg2 = BasicRegister(first_term.memory + deque([False]))
|
||||
rg3 = BasicRegister(deque([False] * (n+1)) + second_term.memory)
|
||||
ct = Counter(n)
|
||||
|
||||
i = 0
|
||||
|
@ -329,8 +315,8 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
|
|||
|
||||
if rg2.memory[0]:
|
||||
result: list[bool] = list(binary_sum(BasicRegister(rg2.memory + rg1.memory), rg3).memory)
|
||||
rg2 = BasicRegister(result[:n+1])
|
||||
rg1 = BasicRegister(result[n+1:])
|
||||
rg2 = BasicRegister(deque(result[:n+1]))
|
||||
rg1 = BasicRegister(deque(result[n+1:]))
|
||||
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := RG2.RG1 + RG3"])))
|
||||
|
||||
rg2.left_shift(rg1.memory[0])
|
||||
|
@ -339,9 +325,11 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
|
|||
|
||||
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := l(RG2.RG1).0\nCT := CT - 1"])))
|
||||
|
||||
return BasicRegister((list(rg2.memory) + list(rg1.memory))[:-1]), data_table
|
||||
return BasicRegister(deque(list(rg2.memory + rg1.memory)[:-1])), data_table
|
||||
|
||||
def binary_multiplication_method_4(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
||||
|
||||
def binary_multiplication_method_4(first_term: BasicRegister, second_term: BasicRegister) \
|
||||
-> tuple[BasicRegister, list[list[str]]]:
|
||||
"""
|
||||
Multiplies two terms containing binary numbers using fourth method.
|
||||
|
||||
|
@ -354,9 +342,9 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
|
|||
first_term, second_term = align_registers(first_term, second_term)
|
||||
n: int = len(first_term)
|
||||
|
||||
rg1 = BasicRegister([False] * (2*n+1))
|
||||
rg1 = BasicRegister(deque([False] * (2*n+1)))
|
||||
rg2 = BasicRegister(first_term.memory)
|
||||
rg3 = BasicRegister([False] + list(second_term.memory) + [False] * n)
|
||||
rg3 = BasicRegister(deque([False]) + second_term.memory + deque([False] * n))
|
||||
|
||||
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
|
||||
|
||||
|
@ -377,4 +365,4 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
|
|||
|
||||
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG2 := l(RG2).0\nRG3 := 0.r(RG3)"])))
|
||||
|
||||
return BasicRegister(list(rg1.memory)[:-1]), data_table
|
||||
return BasicRegister(deque(list(rg1.memory)[:-1])), data_table
|
||||
|
|
Loading…
Reference in New Issue