-
Notifications
You must be signed in to change notification settings - Fork 0
/
slowpu.py
240 lines (172 loc) · 4.77 KB
/
slowpu.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
from tabulate import tabulate
from typing import Union
import pickle
import time
tt0 = time.time()
t0 = time.time()
with open('rom.pic', 'rb') as f:
data = pickle.load(f)
instructions = data[0]
labels = data[1]
def cleanregs(registers):
'''Cleans up the registers'''
for reg in registers.keys():
dat = registers[reg]
# if the data is to long
if len(dat) > 5:
dat = dat[:6]
# if it is to short
if len(dat) < 6:
#remove 0x
dat = dat.replace('0x', '')
datlen = len(dat)
# add the right number of zeros
dat = '0x' + '0' * (4 - datlen) + str(dat)
registers[reg] = dat
return registers
registers = {
'r1': '0x0000',
'r2': '0x0000',
'r3': '0x0000',
'r4': '0x0000',
'r5': '0x0000',
'r6': '0x0000',
'r7': '0x0000',
'p0': '0x0000',
'x8': '0x0000',
'y9': '0x0000',
'cm': '0x0000',
}
disp = []
X_VALUE = 5
Y_VALUE = 4
for y in range(0, Y_VALUE):
disp.append([])
for x in range(0, X_VALUE):
disp[y].append(' ')
# print display
def printdisp(disp: list):
'''Prints out display using tabulate'''
print(tabulate(disp))
def addhex(hexdat: str, value: str) -> str:
'''adds a hex to a hex'''
intdat = int(hexdat, 16)
intdat = intdat + int(value, 16)
return hex(intdat)
def subhex(hexdat: str, value: str) -> str:
'''Subtracts a hex from a hex'''
intdat = int(hexdat, 16)
intdat = intdat - int(value, 16)
return hex(intdat)
def mulhex(hexdat: str, value: str) -> str:
'''Multiplies a hex by a hex'''
intdat = int(hexdat, 16)
intdat = intdat * int(value, 16)
return hex(intdat)
def divhex(hexdat: str, value: str) -> str:
'''Divides a hex by a hex'''
intdat = int(hexdat, 16)
intdat = intdat // int(value, 16)
return hex(intdat)
def toint(hexdat: str) -> int:
'''Converts a hex to an int'''
return int(hexdat, 16)
def printreg():
if toint(registers['p0']) != 0:
print(chr(toint(registers['p0'])), end='')
else:
print()
cmpstore = False
def runtok(tok: list) -> Union[None, int]:
'''Interpret 1 token from a list'''
try:
len(tok[0])
except:
return None
com = tok[0]
reg = tok[1]
dat = tok[2]
if com == 'store':
registers[reg] = registers[dat]
if com == 'load':
registers[reg] = dat
if com == 'add':
registers[reg] = addhex(registers[reg], dat)
if com == 'addr':
first = int(toint(registers[reg]))
second = int(toint(registers[dat]))
registers[reg] = hex(first + second)
if com == 'sub':
registers[reg] = subhex(registers[reg], dat)
if com == 'subr':
first = int(toint(registers[reg]))
second = int(toint(registers[dat]))
registers[reg] = hex(first - second)
if com == 'mul':
registers[reg] = subhex(registers[reg], dat)
if com == 'mulr':
first = int(toint(registers[reg]))
second = int(toint(registers[dat]))
registers[reg] = hex(first * second)
if com == 'div':
registers[reg] = subhex(registers[reg], dat)
if com == 'divr':
first = int(toint(registers[reg]))
second = int(toint(registers[dat]))
registers[reg] = hex(first // second)
if com == 'prt':
printreg()
if com == 'scrn':
y = toint(registers['x8'])
x = int(toint(registers['y9']))
disp[x][y] = 'X' if dat == "0xffff" else " "
if com == 'drw':
printdisp(disp)
if com == 'branch':
pointer = 0
if registers['cm'] == reg:
pointer = labels[dat]
return pointer
else:
pass
if com == 'nbranch':
pointer = 0
if not registers['cm'] == reg:
pointer = labels[dat]
return pointer
else:
pass
if com == 'cmp':
val1 = int(toint(registers[reg]))
val2 = int(toint(registers[dat]))
if val1 > val2:
registers['cm'] = '0x0001'
elif val1 < val2:
registers['cm'] = '0x0000'
else:
registers['cm'] = '0xffff'
if com == 'log':
print(registers[reg])
if com == 'noop':
pass
if com == 'jump':
return labels[reg]
if com == 'halt':
exit()
t1 = time.time()
total = t1-t0
# Timestamping
# print("Init: " + str(total))
global pointer
pointer = 0
while pointer != len(instructions):
lpoint = pointer
val = runtok(instructions[lpoint])
if val == None:
pointer += 1
else:
pointer = val
registers = cleanregs(registers)
# print out registers
print(tabulate(zip(registers.keys(), registers.values())))
input()