-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
98 lines (82 loc) · 4.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sys
def binary_to_hex(bin):
bin_to_hex = {
"0000" : "0",
"0001" : "1",
"0010" : "2",
"0011" : "3",
"0100" : "4",
"0101" : "5",
"0110" : "6",
"0111" : "7",
"1000" : "8",
"1001" : "9",
"1010" : "A",
"1011" : "B",
"1100" : "C",
"1101" : "D",
"1110" : "E",
"1111" : "F"
}
hex = bin_to_hex[bin[0:4]] + bin_to_hex[bin[4:8]] + bin_to_hex[bin[8:12]] + bin_to_hex[bin[12:16]]
return hex
input_file = open(sys.argv[1], "r")
for instruction in input_file.readlines():
instruction_splitted = instruction.split(" ")
instruction_splitted = [e for elem in instruction_splitted for e in elem.split("(")]
instruction_splitted = [elem for elem in instruction_splitted if elem != ""]
instruction_splitted = [elem.upper() for elem in instruction_splitted]
dec_to_hex = ["000", "001", "010", "011", "100", "101", "110", "111"]
dec_to_hex_signed = {
"0": "000",
"1": "001",
"2": "010",
"3": "011",
"-1": "111",
"-2": "110",
"-3": "101",
"-4": "100"
}
instruction_decoded = ""
decoded = False
match (instruction_splitted[0]):
case "MVRV":
instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "011" + dec_to_hex[int(instruction_splitted[3])]
decoded = True
case "MVVR":
instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "010" + dec_to_hex[int(instruction_splitted[3])]
decoded = True
case "MULV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "001" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "MULHV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "010" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "MULHUV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "011" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "ADDV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "100" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "SUBV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "101" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "SHAV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "110" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "SHLV":
instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "111" + dec_to_hex[int(instruction_splitted[3][1])]
decoded = True
case "STV":
instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[2][1])] + dec_to_hex[int(instruction_splitted[1][1])] + "001" + dec_to_hex_signed[instruction_splitted[1]]
decoded = True
case "LDV":
instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "010" + dec_to_hex_signed[instruction_splitted[2]]
decoded = True
if decoded:
print(".word 0x" + str(binary_to_hex(instruction_decoded)))
else:
if instruction[-1] != "" and instruction[-1] == "\n":
print(instruction[:-1])
else:
print(instruction)