2023-06-26 00:26:09 +03:00
|
|
|
from collections import deque
|
2023-06-26 21:58:24 +03:00
|
|
|
from typing_extensions import Self
|
2023-06-26 00:26:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BasicRegister:
|
2023-06-26 21:58:24 +03:00
|
|
|
"""
|
|
|
|
The BasicRegister represents a hardware register capable of manipulating multiple bits at a time.
|
2023-06-26 00:26:09 +03:00
|
|
|
|
|
|
|
:param deque[bool] memory: The bits stored inside the register.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, memory: list[bool]):
|
|
|
|
self.memory: deque[bool] = deque(memory)
|
|
|
|
|
2023-06-27 14:07:47 +03:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"Memory: {[int(value) for value in self.memory]}"
|
|
|
|
|
2023-06-26 00:26:09 +03:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"Memory: {[int(value) for value in self.memory]}"
|
|
|
|
|
2023-06-27 14:07:47 +03:00
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self.memory)
|
|
|
|
|
2023-06-26 21:58:24 +03:00
|
|
|
def adjusted_by_size(self, resulting_size: int) -> Self:
|
|
|
|
"""
|
|
|
|
Adjusts a register to a given size.
|
|
|
|
|
|
|
|
:param int resulting_size: The size of the resulting register.
|
|
|
|
|
|
|
|
:return: A register of a specified size.
|
|
|
|
:rtype: BasicRegister
|
|
|
|
"""
|
|
|
|
current_memory_size: int = len(self.memory)
|
|
|
|
return BasicRegister(
|
2023-06-26 22:33:41 +03:00
|
|
|
[False] * max(resulting_size - current_memory_size, 0) + list(self.memory)[-resulting_size:]
|
2023-06-26 21:58:24 +03:00
|
|
|
)
|
|
|
|
|
2023-06-26 00:26:09 +03:00
|
|
|
def reverse(self):
|
|
|
|
self.memory = deque([not value for value in self.memory])
|
|
|
|
|
|
|
|
def left_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
|
|
|
|
self.memory.extend([digit_to_fill] * steps_shifted)
|
|
|
|
shifted_radices: deque[bool] = deque([self.memory.popleft() for _i in range(steps_shifted)])
|
|
|
|
return shifted_radices
|
|
|
|
|
|
|
|
def right_shift(self, digit_to_fill: bool = False, steps_shifted: int = 1) -> deque[bool]:
|
|
|
|
self.memory.extendleft([digit_to_fill] * steps_shifted)
|
|
|
|
shifted_radices: deque[bool] = deque([self.memory.pop() for _i in range(steps_shifted)])
|
|
|
|
return shifted_radices
|
|
|
|
|
|
|
|
|
|
|
|
def get_memory(variable_name: str) -> list[bool]:
|
|
|
|
"""
|
|
|
|
Reads user input to be used as a memory array.
|
|
|
|
|
|
|
|
:param str variable_name: The name to be displayed in the input line.
|
|
|
|
|
|
|
|
:return: A list of boolean values read from user.
|
|
|
|
:rtype: list[bool]
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
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]
|
|
|
|
else:
|
|
|
|
print(f"[ERROR] The {variable_name} may contain only 1-s and 0-s!")
|
2023-06-26 21:05:45 +03:00
|
|
|
|
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
def binary_sum(first_term: BasicRegister, second_term: BasicRegister, return_remainder: bool = False) \
|
2023-06-27 15:34:39 +03:00
|
|
|
-> BasicRegister or tuple[BasicRegister, int]:
|
2023-06-26 21:05:45 +03:00
|
|
|
"""
|
|
|
|
Sums two registers' values.
|
|
|
|
|
2023-06-27 14:18:10 +03:00
|
|
|
:param BasicRegister first_term: First register.
|
|
|
|
:param BasicRegister second_term: Second register.
|
|
|
|
:param bool return_remainder: True to return the tuple, False to return just the register.
|
2023-06-26 21:05:45 +03:00
|
|
|
|
2023-06-27 14:18:10 +03:00
|
|
|
:return: Register containing the sum or the tuple containing the register and carried radix.
|
|
|
|
:rtype: BasicRegister | tuple[BasicRegister, int]
|
2023-06-26 21:05:45 +03:00
|
|
|
"""
|
2023-06-27 15:53:20 +03:00
|
|
|
result = BasicRegister([False] * len(first_term))
|
2023-06-26 21:05:45 +03:00
|
|
|
|
|
|
|
carry = False
|
2023-06-27 15:53:20 +03:00
|
|
|
for i in range(len(first_term) - 1, 0, -1):
|
|
|
|
current_bit_sum = first_term.memory[i] + second_term.memory[i] + carry
|
2023-06-26 21:05:45 +03:00
|
|
|
carry = bool(current_bit_sum & 2)
|
2023-06-27 15:53:20 +03:00
|
|
|
result.memory[i] = bool(current_bit_sum & 1)
|
2023-06-26 21:05:45 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
final_bit_sum = first_term.memory[0] + second_term.memory[0] + carry
|
|
|
|
result.memory[0] = bool(final_bit_sum & 1)
|
2023-06-26 21:05:45 +03:00
|
|
|
|
2023-06-27 14:18:10 +03:00
|
|
|
if return_remainder:
|
2023-06-27 15:34:39 +03:00
|
|
|
final_carry = bool(final_bit_sum & 2)
|
2023-06-27 15:53:20 +03:00
|
|
|
return result, final_carry
|
2023-06-27 14:18:10 +03:00
|
|
|
else:
|
2023-06-27 15:53:20 +03:00
|
|
|
return result
|
2023-06-27 15:34:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
def binary_subtraction_1_complement(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
|
2023-06-27 15:53:20 +03:00
|
|
|
second_term.reverse()
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
result: BasicRegister
|
|
|
|
final_carry: bool
|
|
|
|
result, final_carry = binary_sum(first_term, second_term, True)
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
if final_carry:
|
|
|
|
return binary_sum(result, BasicRegister([True]))
|
|
|
|
else:
|
|
|
|
result.reverse()
|
|
|
|
return result
|
2023-06-27 15:34:39 +03:00
|
|
|
|
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
def align_registers(first_register: BasicRegister, second_register: BasicRegister) \
|
|
|
|
-> tuple[BasicRegister, BasicRegister]:
|
|
|
|
size_a = len(first_register)
|
|
|
|
size_b = len(second_register)
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
required_size = max(size_a, size_b)
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
a = first_register
|
|
|
|
b = second_register
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
if size_a != size_b:
|
|
|
|
a = a.adjusted_by_size(required_size)
|
|
|
|
b = b.adjusted_by_size(required_size)
|
2023-06-27 15:34:39 +03:00
|
|
|
|
2023-06-27 15:53:20 +03:00
|
|
|
return a, b
|