-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv16alpha_test.py
executable file
·392 lines (336 loc) · 7.56 KB
/
v16alpha_test.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/python3
from processor import *
from v16alpha.v16alpha import V16alpha
from v16alpha.v16alpha_asm import *
from v16alpha.v16alpha_util import *
def testBasicAndReset(p, verbose=False):
if (verbose):
print("="*nbEqSigns+" BasicAndReset "+"="*nbEqSigns)
if verbose:
print(p.err)
print(p.register)
asm="""\
STORE 21 RINO
# cannot write 0xCF, have to compute it
PUSH 157
POP RINT
ADD 50
DSPR RINT RINO"""
loadProgram(p, asm, verbose)
run(p, verbose)
if verbose:
printByteArray(p.program, groupBytesBy=3, name="Program data")
print(p.register)
print(p.io)
print("{:indic}".format(p.pinset))
assert p.program[21] == 0xCF
assert p.err.value == 9
reset(p,verbose)
assert p.program == bytearray([0xFF]*2**V16alpha.INSTRUCTION_ADDRESSING_SIZE*V16alpha.INSTRUCTION_SIZE)
assert p.pinset.state == [False]*32
assert p.err.value == 0
if verbose:
printByteArray(p.program, groupBytesBy=3, name="Program data")
print(p.register)
print("{:indic}".format(p.pinset))
print("Cycles :",p.cycleCount)
def testConstantAndStaticLabels(p, verbose=False):
if (verbose):
print("="*nbEqSigns+" ConstantAndStaticLabels "+"="*nbEqSigns)
# by hand
asm1 ="""
STORE 5 RINT
STORE RINT RCNT
PUSH 1 # these stack ops will be skipped
POP RINO
PUSH 1
POP RINO
END
"""
# with constants
asm2 ="""
:CONST lol 5
STORE !lol RINT
STORE RINT RCNT
PUSH 1
POP RINO
PUSH 1
POP RINO
END
"""
# with static label
asm3 ="""
STORE !endline RINT
STORE RINT RCNT
PUSH 1
POP RINO
PUSH 1
:endline: POP RINO
END
"""
prog1 = assemble(asm1)
prog2 = assemble(asm2)
prog3 = assemble(asm3)
assert prog1 == prog2 and prog2 == prog3
loadProgram(p, asm3, verbose)
run(p, verbose)
def testDynamicJump(p, verbose=False):
if (verbose):
print("="*nbEqSigns+" DynamicJump "+"="*nbEqSigns)
asm="""
STORE 1 RIOB
LABEL 0
store 0x08 rioa
store 2 RIOB
JUMP 14
LABEL 121 # will be skipped
store 0x0f rioa # if RIOA ends with 1111 skipping didn't work
store 3 RIOB
END
0xFF
0xFF
0xFF
LABEL 14 # jump target
store 4 RIOB
END
LABEL 23 # won't get executed
store 5 RIOB
END
"""
loadProgram(p, asm, verbose)
run(p, verbose, safety=100)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) == 0b01001000000001000000000000001000)
def testArithmetic(p, verbose=False):
if (verbose):
print("="*nbEqSigns+" Arithmetic "+"="*nbEqSigns)
if verbose:
print("-"*nbEqSigns+" AND "+"-"*nbEqSigns)
asm="""
STORE 0b01010101 RINT
AND 0b00001111
# 00000101
STORE RINT RIOB
END
"""
loadProgram(p, asm, verbose)
run(p, verbose, safety=100)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x00FF0000 == 0b00001010000000000000000)
reset(p, verbose)
if verbose:
print("-"*nbEqSigns+" OR "+"-"*nbEqSigns)
asm="""
STORE 0b01010101 RINT
OR 0b00001111
# 01011111
STORE RINT RIOB
END
"""
loadProgram(p, asm, verbose)
run(p, verbose, safety=100)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x00FF0000 == 0b010111110000000000000000)
reset(p, verbose)
if verbose:
print("-"*nbEqSigns+" XOR "+"-"*nbEqSigns)
asm="""
STORE 0b01010101 RINT
XOR 0b00001111
# 01011010
STORE RINT RIOB
END
"""
loadProgram(p, asm, verbose)
run(p, verbose, safety=100)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x00FF0000 == 0b010110100000000000000000)
def testConditionals(p, verbose=True):
if (verbose):
print("="*nbEqSigns+" Conditionals "+"="*nbEqSigns)
asm="""
STORE 0 RIOB
STORE 50 RINT
IF RINT = 0 # false
END
IF RINT >= 0 # true
STORE RINT RIOB
if rint lt riob # false
STORE 100 RIOB
ifle rint riob # true
store 0b01010101 riob
if riob /= 0x55 # false
store 0 riob
if riob > 0 # true
END
store 0 riob
end
"""
loadProgram(p, asm, verbose)
run(p, verbose, safety=100)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x00FF0000 == 0b010101010000000000000000)
def testLinstructions(p, verbose=False):
if (verbose):
print("="*nbEqSigns+" L-instructions "+"="*nbEqSigns)
asm="""
LSTORE 257
STORE RINT RIOA
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x0101)
reset(p, verbose)
asm="""
LSTORE 0x0FFF
LADD 0xF000
STORE RINT RIOA
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0xFFFF)
reset(p, verbose)
asm="""
Store 0b01010101 Rint
LXOR 0xFFFF
STORE RINT RIOA
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0xFFAA)
def testBitShift(p, verbose=False):
if (verbose):
printHeader("=", "Bit Shifting")
asm="""
LSTORE 0xAAAA
STORE RINT RIOA
RIOA >> 1
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print(p.register)
print(p.ioa)
print(p.pinset)
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x5555)
reset(p, verbose)
asm="""
LSTORE 0x00FF
STORE RINT RIOA
LSHIFT RIOA 4
RIOA LSHIFT 2
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print(p.register)
print(p.ioa)
print(p.pinset)
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x3FC0)
reset(p, verbose)
asm="""
STORE 100 RIOB
LSTORE 0x0FF0
STORE RINT RIOA
RIOA RSHIFT RIOB
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print("{:indic}".format(p.pinset))
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x0000)
def testIOAsubRegs(p,verbose):
if (verbose):
printHeader("=", "I/O A sub-registers")
asm="""
LSTORE 0
STORE RINT RIOA
STORE 0x99 RINT
STORE RINT RIOAL
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print(p.ioa)
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x9900)
reset(p, verbose)
asm="""
LSTORE 0
STORE RINT RIOA
STORE 0x55 RINT
STORE RINT RIOAL
ADD 0x55
STORE RINT RIOAR
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print(p.ioa)
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x55AA)
reset(p, verbose)
asm="""
STORE 0x55 RINTL
ADD 0x55
STORE RINT RIOA
END
"""
loadProgram(p,asm,verbose)
run(p,verbose)
if verbose:
print(p.ioa)
assert(getIntFromBoolList(p.pinset.state) & 0x0000FFFF == 0x5555)
if __name__ == '__main__' :
verbose = False
from sys import argv
if "-h" in argv:
print("""\
-v verbose
-t execute this test in particular ("test" at the start is optional)
-l list all available tests and abort""")
if "-v" in argv:
verbose = True
p = V16alpha()
funcs = []
for i in locals().copy():
if i[:4] == "test":
funcs.append(i)
if "-l" in argv:
for i in funcs:
print(i)
exit()
if "-t" in argv:
n = argv[argv.index("-t")+1]
if n[:4] != "test":
n="test"+n
funcs = [n]
for f in funcs:
try:
if verbose:
print("\n\n\n")
locals()[f](p, verbose)
status = "OK"
except Exception as e:
if verbose:
from traceback import print_exc ; print_exc(limit=-1,chain=False)
status = "FAIL"
print(" {:^4} {}".format(status, f[4:]))
reset(p)