7 Commits

2 changed files with 218 additions and 41 deletions
+118 -11
View File
@@ -1,5 +1,5 @@
import copy
from collections import deque
from typing import Tuple, List, Any
from typing_extensions import Self
from lib.prettytable import PrettyTable
@@ -163,6 +163,7 @@ def binary_subtraction(minuend: BasicRegister, subtrahend: BasicRegister) -> Bas
:return: Register containing the difference.
:rtype: BasicRegister
"""
subtrahend = BasicRegister(subtrahend.memory)
subtrahend.negate()
difference: BasicRegister
@@ -176,6 +177,29 @@ def binary_subtraction(minuend: BasicRegister, subtrahend: BasicRegister) -> Bas
return difference
def binary_subtraction_second_complement(minuend: BasicRegister, subtrahend: BasicRegister) \
-> tuple[BasicRegister, bool]:
"""
Subtracts the second term from the first in binary using seconds' complement.
:param BasicRegister minuend: Register to subtract from.
:param BasicRegister subtrahend: Register to subtract by.
:return: Register containing the difference.
:rtype: BasicRegister
"""
subtrahend = BasicRegister(subtrahend.memory)
subtrahend.negate()
subtrahend = binary_sum(*align_registers(subtrahend, BasicRegister([True])))
difference: BasicRegister
final_carry: bool
difference, final_carry = binary_sum_with_carry(minuend, subtrahend)
return difference, final_carry
def align_registers(*registers: BasicRegister) -> tuple[BasicRegister, ...]:
"""
Aligns registers by the length of the bigger one.
@@ -212,12 +236,12 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * n))
rg2 = BasicRegister(first_term.memory)
rg3 = BasicRegister(second_term.memory)
rg2 = copy.copy(first_term)
rg3 = copy.copy(second_term)
ct = Counter(n)
data_table = [["iter", "RG1", "RG2", "RG3", "CT", "MicroOperations"]]
@@ -238,8 +262,6 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
rg1.right_shift()
ct.decrement()
# 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(rg1.memory + rg2.memory), data_table
@@ -256,11 +278,11 @@ def binary_multiplication_method_2(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * (2*n)))
rg2 = BasicRegister(first_term.memory)
rg2 = copy.copy(first_term)
rg3 = BasicRegister(deque([False] * n + list(second_term.memory)))
i = 0
@@ -295,7 +317,7 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
data_table = [["iter", "RG2", "RG1", "RG3", "CT", "MicroOperations"]]
@@ -339,11 +361,11 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
:return: Register containing the product.
:rtype: BasicRegister
"""
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
rg1 = BasicRegister(deque([False] * (2*n+1)))
rg2 = BasicRegister(first_term.memory)
rg2 = copy.copy(first_term)
rg3 = BasicRegister(deque([False]) + second_term.memory + deque([False] * n))
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
@@ -366,3 +388,88 @@ 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(deque(list(rg1.memory)[:-1])), data_table
def binary_division_method_1(first_term: BasicRegister, second_term: BasicRegister) \
-> tuple[BasicRegister, list[list[str]]]:
"""
Divides first term by the second term containing binary numbers using first method.
:param: BasicRegister first_term: Register being divided.
:param: BasicRegister second_term: Register being divided by.
:return: Register containing the division result.
:rtype: BasicRegister
"""
n: int = len(first_term)
rg1 = BasicRegister(deque([False, False]) + second_term.memory)
rg2 = BasicRegister(deque([False, False]) + first_term.memory)
rg3 = BasicRegister(deque([True] * (n+1)))
data_table = [["iter", "RG3", "RG2", "RG1", "MicroOperations"]]
i = 0
data_table.append([])
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "-"])))
while rg3.memory[0]:
i += 1
data_table.append([])
if rg2.memory[0]:
rg2 = binary_sum(rg2, rg1)
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "RG2 := RG2 + RG1"])))
else:
rg2, _ = binary_subtraction_second_complement(rg2, rg1)
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "RG2 := RG2 - RG1"])))
rg3.left_shift(not rg2.memory[0])
rg2.left_shift()
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, f"RG3 := l(RG3).!RG2[{n+2}]\nRG2 := l(RG2).0"])))
return BasicRegister(deque(list(rg3.memory)[1:])), data_table
def binary_division_method_2(first_term: BasicRegister, second_term: BasicRegister) \
-> tuple[BasicRegister, list[list[str]]]:
"""
Divides first term by the second term containing binary numbers using second method.
:param: BasicRegister first_term: Register being divided.
:param: BasicRegister second_term: Register being divided by.
:return: Register containing the division result.
:rtype: BasicRegister
"""
n: int = len(first_term)
rg1 = BasicRegister(deque([False]) + second_term.memory + deque([False]*n))
rg2 = BasicRegister(deque([False]) + first_term.memory + deque([False]*n))
rg3 = BasicRegister(deque([True] * (n+1)))
data_table = [["iter", "RG3", "RG2", "RG1", "MicroOperations"]]
i = 0
carry = False
data_table.append([])
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "-"])))
while rg3.memory[0]:
i += 1
data_table.append([])
if rg2.memory[0]:
rg2, carry = binary_sum_with_carry(rg2, rg1)
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "RG2 := RG2 + RG1"])))
else:
rg2, carry = binary_subtraction_second_complement(rg2, rg1)
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, "RG2 := RG2 - RG1"])))
rg3.left_shift(carry)
rg1.right_shift()
data_table[-1].append(list(map(str, [i, rg3, rg2, rg1, f"RG3 := l(RG3).SM[p]\nRG1 := 0.r(RG1)"])))
return BasicRegister(deque(list(rg3.memory)[1:])), data_table
+100 -30
View File
@@ -1,7 +1,90 @@
import bitutilities as bu
operation = ""
method = ""
user_input = []
def process_command(symbol):
global operation, method
if symbol.lower() in "asmdq":
operation = symbol
elif operation == "m" and symbol in "1234":
method = symbol
elif operation == "d" and symbol in "12":
method = symbol
elif symbol in " ;:":
pass
else:
print(f"Error: unexpected instruction '{symbol}', skipping")
def perform_operation(first_register: bu.BasicRegister, second_register: bu.BasicRegister):
global operation, method
match operation:
case "a":
result, carry = bu.binary_sum_with_carry(first_register, second_register)
print(f"\nSum: {result}\nCarry: {int(carry)}")
operation, method = "", ""
case "s":
result, carry = bu.binary_subtraction_second_complement(first_register, second_register)
print(f"\nSubtraction: {result}\nCarry: {int(carry)}")
operation, method = "", ""
case "m":
match method:
case "1":
result, data_table = bu.binary_multiplication_method_1(first_register, second_register)
print(f"\nMultiplication (method 1):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case "2":
result, data_table = bu.binary_multiplication_method_2(first_register, second_register)
print(f"\nMultiplication (method 2):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case "3":
result, data_table = bu.binary_multiplication_method_3(first_register, second_register)
print(f"\nMultiplication (method 3):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case "4":
result, data_table = bu.binary_multiplication_method_4(first_register, second_register)
print(f"\nMultiplication (method 4):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case _:
pass
case "d":
match method:
case "1":
result, data_table = bu.binary_division_method_1(first_register, second_register)
print(f"\nDivision (method 1):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case "2":
result, data_table = bu.binary_division_method_2(first_register, second_register)
print(f"\nDivision (method 2):\n{bu.format_device_state_table(data_table)}\nResult: {result}")
operation, method = "", ""
case _:
pass
case "q":
exit(0)
case _:
pass
def get_prompt_text(operation: any, method: any) -> str:
response = "({} {})"
if operation:
response += " {}"
if operation and method:
response += "/{}"
return response
def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicRegister):
global user_input, operation
first_register, second_register = bu.align_registers(first_register, second_register)
print()
@@ -9,38 +92,25 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg
print(second_register)
while True:
prompt_text: str = get_prompt_text(operation, method).format(first_register, second_register, operation, method)
print()
match input("Choose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n>>> "):
case "a":
print(f"Sum:\n{bu.binary_sum(first_register, second_register)}")
case "s":
print(f"Subtraction:\n{bu.binary_subtraction(first_register, second_register)}")
case "m":
match input("Choose method to use (1-4):\n>>> "):
case "1":
result, data_table = bu.binary_multiplication_method_1(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
case "2":
result, data_table = bu.binary_multiplication_method_2(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
case "3":
result, data_table = bu.binary_multiplication_method_3(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
case "4":
result, data_table = bu.binary_multiplication_method_4(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result}")
case _:
print("Such method does not exist, try again.")
case "d":
pass
case "q":
exit()
case _:
print("Not an available operation, try again.")
if operation == "":
raw_user_input = input("Choose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, "
"[q]uit\n" + prompt_text + " > ")
elif operation == "m":
raw_user_input = input("Choose method to use (1-4):\n" + prompt_text + " > ")
elif operation == "d":
raw_user_input = input("Choose method to use (1-2):\n" + prompt_text + " > ")
user_input = list(raw_user_input)
for symbol in user_input:
process_command(symbol)
perform_operation(first_register, second_register)
if __name__ == '__main__':
reg: bu.BasicRegister = bu.BasicRegister(bu.get_memory("memory"))
reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("more memory"))
reg1: bu.BasicRegister = bu.BasicRegister(bu.get_memory("first operand"))
reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("second operand"))
input_handler(reg, reg2)
input_handler(reg1, reg2)