-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (113 loc) · 3.63 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
instruction_encoding = {
"add": 0,
"adi": 1,
"sub": 2,
"sui": 3,
"and": 4,
"ani": 5,
"or": 6,
"ori": 7,
"mov": 8,
"mvi": 9,
"load": 10,
"store": 11,
"hlt": 15
}
register_encoding = {
"r1": 0,
"r2": 1,
"r3": 2,
"r4": 3,
"r5": 4,
"r6": 5,
"r7": 6,
"r8": 7
}
def convert_line(line,index):
if(line.find('#') >= 0): line = line[:line.index('#')] #ignore commands
line = line.replace('\n','').strip().lower()
if len(line) == 0: return None #Empty Line
tokens = line.split()
if len(tokens) > 4 : raise Exception("POSSIBLE ILLEGAL HARDWARE INSTRUCTION AT LINE: ", index)
#PREDEFINING THE LABELS
try:
opcode = instruction_encoding[ tokens[0] ]
except:
raise Exception("INVALID OPCODE AT LINE: ",index)
ra = 0
rb = 0
ry = 0
im = 0
# 3 REGISTER FORMAT
if opcode in [0,2,4,6]:
if len(tokens) != 4: raise Exception("INVALID INSTRUCTION FORMAT AT: ", index)
try:
ry = register_encoding[ tokens[1] ]
ra = register_encoding[ tokens[2] ]
rb = register_encoding[ tokens[3] ]
except:
raise Exception("INVALID REGISTER AT LINE: ", index)
# 2 REGISTER 1 CONSTANT FORMAT
elif opcode in [1,3,5,7,10,11]:
if len(tokens) != 4: raise Exception("INVALID INSTRUCTION FORMAT AT: ", index)
try:
ry = register_encoding[ tokens[1] ]
ra = register_encoding[ tokens[2] ]
im = int(tokens[3])
except:
raise Exception("INVALID REGISTER AT LINE: ", index)
# 2 REGISTER 1 CONSTANT FORMAT
elif opcode in [1,3,5,7,10,11]:
if len(tokens) != 4: raise Exception("INVALID INSTRUCTION FORMAT AT: ", index)
try:
ry = register_encoding[ tokens[1] ]
ra = register_encoding[ tokens[2] ]
im = int(tokens[3])
except:
raise Exception("INVALID REGISTER AT LINE: ", index)
# 2 REGISTER FORMAT (MOV)
elif opcode == 8:
if len(tokens) != 3: raise Exception("INVALID INSTRUCTION FORMAT AT: ", index)
try:
ry = register_encoding[ tokens[1] ]
ra = register_encoding[ tokens[2] ]
except:
raise Exception("INVALID REGISTER AT LINE: ", index)
# 1 REGISTER 1 CONSTANT FORMAT (MVI)
elif opcode == 9:
if len(tokens) != 3: raise Exception("INVALID INSTRUCTION FORMAT AT: ", index)
try:
ry = register_encoding[ tokens[1] ]
im = int(tokens[2])
except:
raise Exception("INVALID REGISTER AT LINE: ", index)
# HALT OP
elif opcode == 15:
pass
else:
raise Exception("Unknown Hardware Instruction at line: ", index)
return merge_hex(opcode,ry,ra,rb,im)
def merge_hex(opcode,ry,ra,rb,im):
return hex(opcode%0x10)[2:] + hex(ry%0x10)[2:] + hex(ra%0x10)[2:] + hex(rb%0x10)[2:] + '{0:0{1}X}'.format(im%0x10000,4)
if __name__ == '__main__':
try:
input_filename = sys.argv[1]
except IndexError:
print('Usage: command inputfile outputfile \n\r')
sys.exit()
try:
output_filename = sys.argv[2]
except IndexError:
output_filename = 'memimg'
input = open(input_filename)
output = open(output_filename, mode = 'w')
output.write('v2.0 raw\n')
for i,line in enumerate(input):
if len(line.strip()) > 0:
if line.strip()[0] in ['#','\n']: continue
hexcode = convert_line(line, i)
if hexcode is not None:
output.write(hexcode+' ')
input.close()
output.close()