-
Notifications
You must be signed in to change notification settings - Fork 0
/
iType.py
91 lines (79 loc) · 2.73 KB
/
iType.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
from getRegisterCode import getRegisterCode
from printColor import printColor
from writeToFile import writeToFile
def iType2Args(inst, reg1, reg2):
l = ["SW", "LW"]
if inst.upper() not in l:
raise Exception(" Wrong instruction format")
if inst.upper() == "LI":
regCode2 = getRegisterCode(reg1)
printColor(regCode2, "red")
writeToFile(regCode2)
binaddr = ""
if int(reg2) >= 0:
binaddr += bin(int(reg2)).replace("0b", "")
binaddr = binaddr.zfill(21)
else:
num = 65535 + int(reg2)
num = num + 1
num = str(num)
binaddr += bin(int(num)).replace("0b", "")
binaddr = binaddr.zfill(21)
printColor(binaddr + "\n", "green")
writeToFile(binaddr + "\n")
else:
l = ["SW", "LW"]
if inst.upper() not in l:
raise Exception(" Wrong instruction format")
index = reg2.index("(")
offset = reg2[0:index]
offset = int(offset)
if offset % 4:
raise Exception(" Boundary is not word aligned")
binaddr = ""
binaddr += bin(offset).replace("0b", "")
binaddr = binaddr.zfill(16)
index2 = reg2.index(")")
secondReg = reg2[(index + 1) : index2]
regCode2 = getRegisterCode(secondReg)
printColor(regCode2, "red")
writeToFile(regCode2)
regCode1 = getRegisterCode(reg1)
printColor(regCode1, "blue")
writeToFile(regCode1)
printColor(binaddr + "\n", "green")
writeToFile(binaddr + "\n")
def iType3Args(inst, reg1, reg2, dest):
set1 = ["ADDI", "SUBI", "DIVI", "SLTI", "ORI", "ANDI", "XORI"]
set2 = ["BEQ", "BNE", "BLT", "BGT"]
binaddr = ""
if inst.upper() in set1:
if int(dest) >= 0:
# binaddr += bin(int(dest, 16)).replace("0b", "")
binaddr += bin(int(dest)).replace("0b", "")
binaddr = binaddr.zfill(16)
else:
num = 65535 + int(dest)
num = num + 1
# print(num)
num = str(num)
# print(num)
binaddr += bin(int(num)).replace("0b", "")
binaddr = binaddr.zfill(16)
elif inst.upper() in set2:
for i in range(6, len(dest)):
binaddr += "{0:04b}".format(int(dest[i], 16))
binaddr = binaddr[0:-2]
binaddr = "00" + binaddr
regCode2 = getRegisterCode(reg2)
regCode1 = getRegisterCode(reg1)
if inst.upper() in set2:
temp = regCode2
regCode2 = regCode1
regCode1 = temp
printColor(regCode2, "blue")
writeToFile(regCode2)
printColor(regCode1, "green")
writeToFile(regCode1)
printColor(binaddr + "\n", "red")
writeToFile(binaddr + "\n")