diff --git a/main.py b/main.py index 9295868..a2e131f 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,89 @@ 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(first_register: bu.BasicRegister, second_register: bu.BasicRegister) -> str: + global operation, method + + if not operation: + return f"({first_register} {second_register})" + elif not method: + return f"({first_register} {second_register}) {operation}" + else: + return f"({first_register} {second_register}) {operation}/{method}" + 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() @@ -10,43 +92,26 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg while True: 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": - match input("Choose method to use (1-2):\n>>> "): - case "1": - result, data_table = bu.binary_division_method_1(first_register, second_register) - print(f"Division:\n{bu.format_device_state_table(data_table)}\nResult: {result}") - case "2": - result, data_table = bu.binary_division_method_2(first_register, second_register) - print(f"Division:\n{bu.format_device_state_table(data_table)}\nResult: {result}") - case "q": - exit() - case _: - print("Not an available operation, try again.") + if operation == "": + raw_user_input = input(f"Choose the operation:\n" \ + f"[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n" \ + f"{get_prompt_text(first_register, second_register)} > ") + elif operation == "m": + raw_user_input = input(f"Choose method to use (1-4):\n" \ + f"{get_prompt_text(first_register, second_register)} > ") + elif operation == "d": + raw_user_input = input(f"Choose method to use (1-2):\n" \ + f"{get_prompt_text(first_register, second_register)} > ") + + 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")) + reg1: bu.BasicRegister = bu.BasicRegister(bu.get_memory("memory")) reg2: bu.BasicRegister = bu.BasicRegister(bu.get_memory("more memory")) - input_handler(reg, reg2) + input_handler(reg1, reg2)