README expanded.
This commit is contained in:
parent
9f7ec834b2
commit
054dc803d1
257
README.md
257
README.md
|
@ -1,3 +1,258 @@
|
|||
# binaryCalculatorPrototype
|
||||
This is a Python language prototype for a binary calculator to be used in Computer Arithmetics lab works for first-year students studying Computer Engineering at KPI.
|
||||
|
||||
This is a Python language prototype for a binary calculator to be used in Computer Arithmetics lab works for first-year students studying Computer Engineering at KPI.
|
||||
# Requirements
|
||||
The user must have installed:
|
||||
|
||||
- python 3 (for the calculator itself);
|
||||
- git (to clone the repository for installation);
|
||||
|
||||
# Installation
|
||||
To install the calculator just clone the repository locally:
|
||||
|
||||
```
|
||||
git clone -b master http://10.1.1.1:3000/Rhinemann/binaryCalculatorPrototype.git
|
||||
```
|
||||
|
||||
# User instructions
|
||||
Start the calculator using the following command:
|
||||
|
||||
```
|
||||
python3 main.py
|
||||
```
|
||||
|
||||
After that you must input the binary number as your first and second operands, as such:
|
||||
|
||||
```
|
||||
Enter first operand: 110101
|
||||
Enter second operand: 110
|
||||
```
|
||||
|
||||
Note that you can't input any digit other than 0 or 1 into the operands:
|
||||
|
||||
```
|
||||
Enter first operand: 234123
|
||||
[ERROR] The first operand may contain only 1-s and 0-s!
|
||||
Enter first operand: 12314
|
||||
[ERROR] The first operand may contain only 1-s and 0-s!
|
||||
Enter first operand: 1234123
|
||||
[ERROR] The first operand may contain only 1-s and 0-s!
|
||||
```
|
||||
|
||||
After properly inputting the operands properly, you will be presented with such prompt:
|
||||
|
||||
```
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) >
|
||||
```
|
||||
|
||||
`(110101 000110)` are the operands you have input. You may now choose the operations performed on the operands as such:
|
||||
|
||||
```
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) > a
|
||||
|
||||
Sum: 111011
|
||||
Carry: 0
|
||||
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) > s
|
||||
|
||||
Subtraction: 101111
|
||||
Carry: 1
|
||||
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) > m
|
||||
|
||||
Choose method to use (1-4):
|
||||
(110101 000110) m > 1
|
||||
|
||||
Multiplication (method 1):
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| iter | RG1 | RG2 | RG3 | CT | MicroOperations |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 0 | 000000 | 110101 | 000110 | 110 | - |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 1 | 000110 | 110101 | 000110 | 110 | RG1 := RG1 + RG3 |
|
||||
| 1 | 000011 | 011010 | 000110 | 101 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 2 | 000001 | 101101 | 000110 | 100 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 3 | 000111 | 101101 | 000110 | 100 | RG1 := RG1 + RG3 |
|
||||
| 3 | 000011 | 110110 | 000110 | 011 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 4 | 000001 | 111011 | 000110 | 010 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 5 | 000111 | 111011 | 000110 | 010 | RG1 := RG1 + RG3 |
|
||||
| 5 | 000011 | 111101 | 000110 | 001 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 6 | 001001 | 111101 | 000110 | 001 | RG1 := RG1 + RG3 |
|
||||
| 6 | 000100 | 111110 | 000110 | 000 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
Result: 000100111110
|
||||
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) > d
|
||||
|
||||
Choose method to use (1-2):
|
||||
(110101 000110) d > 1
|
||||
|
||||
Division (method 1):
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| iter | RG3 | RG2 | RG1 | MicroOperations |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 0 | 1111111 | 00110101 | 00000110 | - |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 1 | 1111111 | 00101111 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 1 | 1111111 | 01011110 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 2 | 1111111 | 01011000 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 2 | 1111111 | 10110000 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 3 | 1111111 | 10110110 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 3 | 1111110 | 01101100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 4 | 1111110 | 01100110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 4 | 1111101 | 11001100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 5 | 1111101 | 11010010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 5 | 1111010 | 10100100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 6 | 1111010 | 10101010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 6 | 1110100 | 01010100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 7 | 1110100 | 01001110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 7 | 1101001 | 10011100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 8 | 1101001 | 10100010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 8 | 1010010 | 01000100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 9 | 1010010 | 00111110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 9 | 0100101 | 01111100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
Result: 100101
|
||||
```
|
||||
|
||||
The results of the operations will be displayed, and you will get prompted for the next operation to perform. **Note** the results of previous operations don't impact the operands, therefore you can't plug your previous results into the calculator without restarting the program!
|
||||
|
||||
Also, as a quality of life feature, you can chain multiple operations in one prompt as such:
|
||||
|
||||
```
|
||||
Choose the operation:
|
||||
[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit
|
||||
(110101 000110) > asm1d1
|
||||
|
||||
Sum: 111011
|
||||
Carry: 0
|
||||
|
||||
Subtraction: 101111
|
||||
Carry: 1
|
||||
|
||||
Multiplication (method 1):
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| iter | RG1 | RG2 | RG3 | CT | MicroOperations |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 0 | 000000 | 110101 | 000110 | 110 | - |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 1 | 000110 | 110101 | 000110 | 110 | RG1 := RG1 + RG3 |
|
||||
| 1 | 000011 | 011010 | 000110 | 101 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 2 | 000001 | 101101 | 000110 | 100 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 3 | 000111 | 101101 | 000110 | 100 | RG1 := RG1 + RG3 |
|
||||
| 3 | 000011 | 110110 | 000110 | 011 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 4 | 000001 | 111011 | 000110 | 010 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 5 | 000111 | 111011 | 000110 | 010 | RG1 := RG1 + RG3 |
|
||||
| 5 | 000011 | 111101 | 000110 | 001 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
| 6 | 001001 | 111101 | 000110 | 001 | RG1 := RG1 + RG3 |
|
||||
| 6 | 000100 | 111110 | 000110 | 000 | RG2 := RG1[1].r(RG2) |
|
||||
| | | | | | RG1 := 0.r(RG1) |
|
||||
| | | | | | CT := CT - 1 |
|
||||
+------+--------+--------+--------+-----+----------------------+
|
||||
Result: 000100111110
|
||||
|
||||
Division (method 1):
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| iter | RG3 | RG2 | RG1 | MicroOperations |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 0 | 1111111 | 00110101 | 00000110 | - |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 1 | 1111111 | 00101111 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 1 | 1111111 | 01011110 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 2 | 1111111 | 01011000 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 2 | 1111111 | 10110000 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 3 | 1111111 | 10110110 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 3 | 1111110 | 01101100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 4 | 1111110 | 01100110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 4 | 1111101 | 11001100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 5 | 1111101 | 11010010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 5 | 1111010 | 10100100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 6 | 1111010 | 10101010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 6 | 1110100 | 01010100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 7 | 1110100 | 01001110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 7 | 1101001 | 10011100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 8 | 1101001 | 10100010 | 00000110 | RG2 := RG2 + RG1 |
|
||||
| 8 | 1010010 | 01000100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
| 9 | 1010010 | 00111110 | 00000110 | RG2 := RG2 - RG1 |
|
||||
| 9 | 0100101 | 01111100 | 00000110 | RG3 := l(RG3).!RG2[8] |
|
||||
| | | | | RG2 := l(RG2).0 |
|
||||
+------+---------+----------+----------+-----------------------+
|
||||
Result: 100101
|
||||
```
|
||||
|
||||
So that multiple operations are performed on the same operands.
|
|
@ -2,7 +2,7 @@ from collections import deque
|
|||
from typing import Tuple, List, Any
|
||||
|
||||
from typing_extensions import Self
|
||||
from lib.prettytable import PrettyTable
|
||||
from prettytable import PrettyTable
|
||||
|
||||
|
||||
class BasicRegister:
|
||||
|
|
2534
lib/prettytable.py
2534
lib/prettytable.py
File diff suppressed because it is too large
Load Diff
138
main.py
138
main.py
|
@ -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,44 +92,27 @@ 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":
|
||||
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("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)
|
||||
# TODO live-swapping of registers!!!
|
||||
|
||||
input_handler(reg1, reg2)
|
||||
|
|
Loading…
Reference in New Issue