-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
251 lines (225 loc) · 8.12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
r = ["00" for _ in range(16)]
add = ["00" for _ in range(256)]
options = {
"writememory": "step",
"endwhenterminatecode": "true",
"ouputputdisplaytype": "string"
}
def readoptions(options):
optionfile = open("settings.txt")
option = optionfile.read().lower()
optionfile.close()
option = option.split("\n")
for x in option:
if x.count(" ") > 0:
#TODO check if Valid Value
x = x.split()[0]
key = x.split("=")[0]
value = x.split("=")[1]
options[key] = value
del optionfile,option,key,value,x
def writememorytotext():
memoryfile = open("memory.txt","w")
x = 00
while True:
address = hex(x).upper() if x > 15 else hex(x).replace("0x","0x0").upper()
memoryfile.write(f"{address} {add[x]}{add[x+1]}\n")
x += 2
if x >= 256:
memoryfile.close()
print("Writing from Memory Successful")
break
del memoryfile,x,address
def writeregistrytotext():
registryfile = open("registry.txt","w")
x = 00
while True:
address = hex(x).upper() if x > 15 else hex(x).replace("0x","0x0").upper()
registryfile.write(f"{address} {r[x]}\n")
x += 1
if x >= 16:
registryfile.close()
print("Writing from Registry Successful")
break
del registryfile,x,address
def writeinstructionstomemory():
instructionsfile = open("instructions.txt",encoding="utf-8")
instructions = instructionsfile.read()
instructionsfile.close()
#Remove Comments
l = instructions.split("\n")
x = 0
c = []
for i in l:
i = i.replace(" "," ") #Tab to Space
if i == "" or i[0] =="#" or i[0] == "/":
c.append(i)
elif i.count(" ") != 1:
l[x] = f"{i.split()[0]} {i.split()[1]}"
x+=1
j = ""
for j in c:
l.remove(j)
del c,j,i,x
#Insert to Memory
for i in l:
ins = i.split()
if len(ins[1]) == 2:
address = int(ins[0], base=16)
add[address] = ins[1]
if len(ins[1]) == 4:
address = int(ins[0], base=16)
add[address] = ins[1][0] + ins[1][1]
add[address + 1] = ins[1][2] + ins[1][3]
del i,address,ins,instructions,instructionsfile,l
counter = 00
con = True
def executeinstruction(code):
opcode = code[0]
if opcode == "0":
return ["SKP"]
elif opcode == "1":
#Load From Memory
address = int(code[2], base=16) * 16 + int(code[3], base=16)
radd = int(code[1], base=16)
r[radd] = add[address]
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "2":
#Load Value
value = code[2]+ code[3]
radd = int(code[1], base=16)
r[radd] = value
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "3":
#Store or Display
if code[2] == "0" and code[3] == "0":
#Display
outputfile = open("output.txt","a")
radd = int(code[1], base=16)
text = r[radd]
if options["ouputputdisplaytype"] == "string":
text = str(bytes.fromhex(text)).replace("b'","").replace("'","")
outputfile.write(f"{text}\n")
outputfile.close()
print("Writing from Display Successful")
return ["CON"]
#Store
address = int(code[2], base=16) * 16 + int(code[3], base=16)
radd = int(code[1], base=16)
add[address] = r[radd]
if options["writememory"] == "step": writememorytotext()
elif opcode == "4":
#Copy contents
orad = int(code[2], base=16)
nrad = int(code[3], base=16)
r[nrad] = r[orad]
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "5":
#Add (twos complement)
s = int(code[2], base=16)
t = int(code[3], base=16)
s = int(r[s],base=16)
t = int(r[t],base=16)
res = s + t
if res > 255: res -= 256
radd = int(code[1], base=16)
r[radd] = hex(res).replace("0x","").upper() if len(hex(res).replace("0x","").upper()) > 1 else f"0{hex(res).replace('0x','').upper()}"
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "6":
#Add (floating point)
s = int(code[2], base=16)
t = int(code[3], base=16)
s = int(r[s],base=16)
t = int(r[t],base=16)
radd = int(code[1], base=16)
res = s + t
if res > 255: res -= 256
r[radd] = hex(res).replace("0x","").upper() if len(hex(res).replace("0x","").upper()) > 1 else f"0{hex(res).replace('0x','').upper()}"
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "7":
#OR
s = int(code[2], base=16)
t = int(code[3], base=16)
s = int(r[s],base=16)
t = int(r[t],base=16)
radd = int(code[1], base=16)
result = s | t
result = hex(result).replace("0x","").upper() if len(hex(result).replace("0x","").upper()) > 1 else f"0{hex(result).replace('0x','').upper()}"
r[radd] = result
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "8":
#AND
s = int(code[2], base=16)
t = int(code[3], base=16)
s = int(r[s],base=16)
t = int(r[t],base=16)
radd = int(code[1], base=16)
result = s & t
result = hex(result).replace("0x","").upper() if len(hex(result).replace("0x","").upper()) > 1 else f"0{hex(result).replace('0x','').upper()}"
r[radd] = result
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "9":
#XOR
s = int(code[2], base=16)
t = int(code[3], base=16)
s = int(r[s],base=16)
t = int(r[t],base=16)
radd = int(code[1], base=16)
result = s ^ t
result = hex(result).replace("0x","").upper() if len(hex(result).replace("0x","").upper()) > 1 else f"0{hex(result).replace('0x','').upper()}"
r[radd] = result
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "A":
#ROTATE
t = int(code[3], base=16)
radd = int(code[1], base=16)
num = int(r[radd],base=16)
bnum = bin(num).replace("0b","")
while len(bnum) < 8:
bnum = "0" + bnum
for _ in range(t):
bnum = bnum[7] + bnum[:-1]
num = int(bnum,base=2)
num = hex(num).replace("0x","").upper() if len(hex(num).replace("0x","").upper()) > 1 else f"0{hex(num).replace('0x','').upper()}"
r[radd] = num
if options["writememory"] == "step": writeregistrytotext()
elif opcode == "B":
#Jump
address = int(code[2], base=16) * 16 + int(code[3], base=16)
radd = int(code[1], base=16)
if r[0] == r[radd] or code[1] == "0":
return ["JMP",address]
elif opcode == "C":
if code[1] == code[2] == code[3] == "0":
#Terminate
return ["TER"]
elif code[2] == code[3] == "0":
return ["SKP"]
else:
return ["SKP"] #TODO Learn move of what to do here
print("ERROR EXECUTING INSTRUCTION")
exit(-3)
return ["CON"]
print("Starting Up")
print("Reading Config")
readoptions(options)
print("Done")
print("Adding Instructions")
writeinstructionstomemory()
writememorytotext()
print("Instructions Added")
print("Starting to Execute")
while counter < 255:
ocounter = counter
ret = executeinstruction(add[counter] + add[counter + 1])
if options["writememory"] == "step" and ret[0] != "SKP": input(f"Ran instructions in address {hex(counter).upper().replace('0X','')}, Press Enter to Continue:\n")
if ret[0] == "TER" and options["endwhenterminatecode"] == "true": con = False
elif ret[0] == "JMP": counter = ret[1]
if not con: break
counter = counter + 2 if ocounter == counter else counter
print("Execute Finished")
print("Writing Final Memory to File")
writememorytotext()
print("Writing Final Registry to File")
writeregistrytotext()
print("Finished")