-
Notifications
You must be signed in to change notification settings - Fork 1
/
px.py
441 lines (378 loc) · 11.9 KB
/
px.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env python3
from nitrox import instruction
@instruction
class nop: # 1cy
operands = ''
encoding = '0000 0000 0000 0000'
def emulate():
pass
@instruction
class seg: # 1cy
operands = '{seg}'
encoding = '0000 0000 0000 0ss1'
def emulate(state, segment):
state.segment = segment
@instruction
class wait_load: # 3cy, used to wait for loads
operands = ''
encoding = '0000 0000 1xxx xxxx'
@instruction
class wait_other: # 5cy, used to wait for register writes (e.g. to use getdcr)
operands = ''
encoding = '0000 0001 1xxx xxxx'
@instruction
class jz: # taken: 5cy, not taken: 1cy
operands = '{addr}'
encoding = 'aa11 0aaa aaaa aaaa'
def emulate(state, addr):
if state.result == 0:
state.pc = (state.segment << 13) | addr
@instruction
class jnz: # same latencies as jz
operands = '{addr}'
encoding = 'aa01 0aaa aaaa aaaa'
def emulate(state, addr):
if state.result != 0:
state.pc = (state.segment << 13) | addr
@instruction
class js: # same latencies as jz
operands = '{addr}'
encoding = 'aa01 1aaa aaaa aaaa'
def emulate(state, addr):
if state.result & 0x8000:
state.pc = (state.segment << 13) | addr
@instruction
class call: # call + ret: 6-8cy
operands = '{addr_}'
encoding = 'aa11 1aaa aaaa aaaa'
def emulate(state, addr):
assert len(state.call_stack) < 32
state.call_stack.append(state.pc)
state.pc = (state.segment << 13) | addr
@instruction
class ret: # call + ret 0: 6cy, call + ret 1: 8cy
operands = '{imm1}'
encoding = '0000 0010 ixxx xxxx'
def emulate(state, imm1): # TODO: what does imm1 do?
state.pc = state.call_stack.pop()
state.segment = state.pc >> 13
@instruction
class push: # push to stack (overwriting the oldest entry on overflow), needs one wait state before ret works
operands = 'r{src}'
encoding = '0000 0011 xxxx 0sss'
def emulate(state, src):
state.call_stack[state.stack_ptr] = state.main_reg[src]
state.stack_ptr = (state.stack_ptr + 1) & 31
@instruction
class pushf:
operands = '{imm1}'
encoding = '0000 0011 ixxx 1xxx'
def emulate(state, imm1):
value = 1 - state.zero_flag
if imm1 and state.sign_flag:
value |= 0x8000
state.call_stack[state.stack_ptr] = value
state.stack_ptr = (state.stack_ptr + 1) & 31
@instruction
class emit_lo:
operands = 'r{reg}'
encoding = '0000 0101 xxxx xrrr'
@instruction
class emit_hi:
operands = 'r{reg}'
encoding = '0000 0110 xxxx xrrr'
@instruction
class emit:
operands = '0x{imm8:02x} ; {imm8}'
encoding = '0000 0111 iiii iiii'
@instruction
class li: # load immediate (1cy)
operands = 'r{dst}, 0x{imm8:02x} ; {imm8}'
encoding = '0.00 1ddd iiii iiii'
def emulate(state, dst, imm8):
state.main_reg[dst] = imm8
@instruction
class and_: # bitwise and (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '0.10 0ddd 00rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] & state.main_regs[rhs]
@instruction
class or_: # bitwise or (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '0.10 0ddd 01rr rlll'
def emulate(state, dst, lhs, rhs):
state.main_reg[dst] = state.main_reg[lhs] | state.main_regs[rhs]
@instruction
class add: # addition (1cy)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '0.10 0ddd 10rr rlll'
def emulate(state, dst, lhs, rhs):
result = state.main_reg[lhs] + state.main_regs[rhs]
state.carry_flag = result > 0xFFFF
state.main_reg[dst] = result & 0xFFFF
@instruction
class sub: # subtraction
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '0.10 0ddd 11rr rlll'
def emulate(state, dst, lhs, rhs):
result = state.main_reg[lhs] + (~state.main_regs[rhs] & 0xFFFF) + 1
state.carry_flag = result >> 16
state.main_reg[dst] = result & 0xFFFF
@instruction
class shli: # shift left by immediate (1cy)
operands = 'r{dst}, r{lhs}, {imm4}'
encoding = 'i.10 1ddd 00ii illl'
def emulate(state, dst, lhs, imm4):
state.main_reg[dst] = (state.main_reg[lhs] << imm4) & 0xFFFF
@instruction
class shri: # logical shift right by immediate (1cy)
operands = 'r{dst}, r{lhs}, {imm4}'
encoding = 'i.10 1ddd 01ii illl'
def emulate(state, dst, lhs, imm4):
state.main_reg[dst] = state.main_reg[lhs] >> imm4
@instruction
class la: # load A temp register (16 bits, 1cy)
operands = 'a{dst}, r{src}'
encoding = '0x10 1ddd 1000 0sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class lb: # load B temp register (16 bits, 1cy)
operands = 'b{dst}, r{src}'
encoding = '0x10 1ddd 1000 1sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class lc: # load C temp register (16 bits, 1cy)
operands = 'c{dst}, r{src}'
encoding = '1x10 1ddd 1000 0sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class ld: # load D temp register (16 bits, 1cy)
operands = 'd{dst}, r{src}'
encoding = '1x10 1ddd 1000 1sss'
def emulate(state, dst, src):
state.temp_reg[dst] = state.main_reg[src]
@instruction
class la_hi: # load A temp register (high 8 bits)
operands = 'a{dst}, r{src}'
encoding = '0x10 1ddd 1001 0sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class lb_hi: # load B temp register (high 8 bits)
operands = 'b{dst}, r{src}'
encoding = '0x10 1ddd 1001 1sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class lc_hi: # load C temp register (high 8 bits)
operands = 'c{dst}, r{src}'
encoding = '1x10 1ddd 1001 0sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class ld_hi: # load D temp register (high 8 bits)
operands = 'd{dst}, r{src}'
encoding = '1x10 1ddd 1001 1sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF
state.addr_reg[dst] |= (state.main_reg[src] & 0xFF) << 8
@instruction
class la_lo: # load A temp register (low 8 bits)
operands = 'a{dst}, r{src}'
encoding = '0x10 1ddd 1010 0sss'
def emulate(state, dst, src):
state.addr_reg[dst] &= 0xFF00
state.addr_reg[dst] |= state.main_reg[src] & 0xFF
@instruction
class lb_lo: # load B temp register (low 8 bits)
operands = 'b{dst}, r{src}'
encoding = '0x10 1ddd 1010 1sss'
def emulate(state, dst, src):
state.tmp_reg[dst+8] &= 0xFF00
state.tmp_reg[dst+8] |= state.main_reg[src] & 0xFF
@instruction
class lc_lo: # load C temp register (low 8 bits)
operands = 'c{dst}, r{src}'
encoding = '1x10 1ddd 1010 0sss'
def emulate(state, dst, src):
state.tmp_reg[dst+16] &= 0xFF00
state.tmp_reg[dst+16] |= state.main_reg[src] & 0xFF
@instruction
class ld_lo: # load D temp register (low 8 bits)
operands = 'd{dst}, r{src}'
encoding = '1x10 1ddd 1010 1sss'
def emulate(state, dst, src):
state.tmp_reg[dst+24] &= 0xFF00
state.tmp_reg[dst+24] |= state.main_reg[src] & 0xFF
@instruction
class load: # read SRAM
operands = 'r{dst}, r{src}'
encoding = '0.10 1ddd 1011 0sss'
def emulate(state, dst, src):
tmp = state.main_reg[src]
result = tmp + 1
state.carry_flag = result >> 16
state.main_reg[src] = result & 0xFFFF
state.update_flags(state.main_reg[src])
state.main_reg[dst] = state.memory[tmp]
@instruction
class addc:
operands = 'r{dst}, r{src}'
encoding = '1.10 1ddd 1011 0sss'
def emulate(state, dst, src):
result = state.main_reg[src] + state.carry_flag
state.carry_flag = result >> 16
state.main_reg[dst] = result & 0xFFFF
@instruction
class align8: # 1cy
operands = 'r{dst}, r{src}'
encoding = '0.10 1ddd 1011 1sss'
def emulate(state, dst, src):
state.main_reg[dst] = (state.main_reg[src] + 7) & (-7 & 0xFFFF)
@instruction
class align16: # 1cy
operands = 'r{dst}, r{src}'
encoding = '1.10 1ddd 1011 1sss'
def emulate(state, dst, src):
state.main_reg[dst] = (state.main_reg[src] + 15) & (-15 & 0xFFFF)
@instruction
class sa: # store A temp register to GPR (16 bits)
operands = 'r{dst}, a{src}'
encoding = '0.10 1ddd 1100 0sss'
@instruction
class sb:
operands = 'r{dst}, b{src}'
encoding = '0.10 1ddd 1100 1sss'
@instruction
class sc:
operands = 'r{dst}, c{src}'
encoding = '1.10 1ddd 1100 0sss'
@instruction
class sd:
operands = 'r{dst}, d{src}'
encoding = '1.10 1ddd 1100 1sss'
@instruction
class sa_hi:
operands = 'r{dst}, a{src}'
encoding = '0.10 1ddd 1101 0sss'
@instruction
class sb_hi:
operands = 'r{dst}, b{src}'
encoding = '0.10 1ddd 1101 1sss'
@instruction
class sc_hi:
operands = 'r{dst}, c{src}'
encoding = '1.10 1ddd 1101 0sss'
@instruction
class sd_hi:
operands = 'r{dst}, d{src}'
encoding = '1.10 1ddd 1101 1sss'
@instruction
class sa_lo:
operands = 'r{dst}, a{src}'
encoding = '0.10 1ddd 1110 0sss'
@instruction
class sb_lo:
operands = 'r{dst}, b{src}'
encoding = '0.10 1ddd 1110 1sss'
@instruction
class sc_lo:
operands = 'r{dst}, c{src}'
encoding = '1.10 1ddd 1110 0sss'
@instruction
class sd_lo:
operands = 'r{dst}, d{src}'
encoding = '1.10 1ddd 1110 1sss'
@instruction
class not_:
operands = 'r{dst}, r{src}'
encoding = '0.10 1ddd 1111 0sss'
def emulate(state, dst, src):
state.main_reg[dst] = ~state.main_reg[src] & 0xFFFF
@instruction
class align4:
operands = 'r{dst}, r{src}'
encoding = '1.10 1ddd 1111 0sss'
def emulate(state, dst, src):
state.main_reg[dst] = (state.main_reg[src] + 3) & (-3 & 0xFFFF)
@instruction
class getdcr: # 1cy, read direct communication register
# after writing the DCR, it takes 4 cycles until the new value can be read
operands = 'r{dst}'
encoding = '0.10 1ddd 1111 1xxx'
def emulate(state, dst):
state.main_reg[dst] = (state.hw_reg[7] << 8) | state.hw_reg[8]
@instruction
class getcore:
operands = 'r{dst}'
encoding = '1.10 1ddd 1111 1xx0'
def emulate(state, dst):
state.main_reg[dst] = state.core_id
@instruction
class pop: # pop item from stack or zero if stack is empty
operands = 'r{dst}'
encoding = '1.10 1ddd 1111 1xx1'
def emulate(state, dst):
state.stack_ptr = (state.stack_ptr - 1) & 31
state.main_reg[dst] = state.call_stack[state.stack_ptr]
state.call_stack[state.stack_ptr] = 0
@instruction
class setreg: # 1cy, load lower 8 bits from GPR into accelerator register
operands = '0x{imm4:x}, r{src}'
encoding = '0100 0000 0iii isss'
def emulate(state, dst, src):
state.hw_reg[dst] = state.main_reg[src] & 0xFF
@instruction
class setregi: # load 8-bit immediate into accelerator register
operands = '0x{imm4:x}, 0x{jmm8:02x} ; {jmm8}'
encoding = '1i00 0jjj jjjj jiii'
def emulate(state, dst, imm8):
state.hw_reg[dst] = imm8
@instruction
class lis: # load immediate shifted (flags are set according to the whole 16-bit register)
operands = 'r{dst}, 0x{imm8:02x} ; {imm8}'
encoding = '1.00 1iii iiii iddd'
def emulate(state, dst, imm8):
state.main_reg[dst] = (imm8 << 8) | (state.main_regs[dst] & 0xFF)
@instruction
class andi: # bitwise and with 3-bit immediate
operands = 'r{dst}, r{lhs}, {imm3_minus_one}'
encoding = '1.10 0ddd 00ii illl'
def emulate(state, dst, lhs, imm3_minus_one):
state.main_reg[dst] = state.main_reg[lhs] & imm3_minus_one
@instruction
class store: # write SRAM (4096 bytes)
operands = 'r{dst}, r{lhs}, r{rhs}'
encoding = '1.10 0ddd 01rr rlll'
def emulate(state, dst, lhs, rhs):
state.memory[state.main_reg[lhs] & 0x7FF] = state.main_reg[rhs]
result = state.main_reg[lhs] + 1
state.carry_flag = result >> 16
state.main_reg[dst] = result & 0xFFFF
@instruction
class addi: # add 3-bit immediate (set carry on unsigned overflow)
operands = 'r{dst}, r{lhs}, {imm3_minus_one}'
encoding = '1.10 0ddd 10ii illl'
def emulate(state, dst, lhs, imm3_minus_one):
result = state.main_reg[lhs] + imm3_minus_one
state.carry_flag = result > 0xFFFF
state.main_reg[dst] = result & 0xFFFF
@instruction
class subi: # subtract 3-bit immediate (set carry on signed overflow)
operands = 'r{dst}, r{lhs}, {imm3_minus_one}'
encoding = '1.10 0ddd 11ii illl'
def emulate(state, dst, lhs, imm3_minus_one):
result = state.main_reg[lhs] + (~imm3_minus_one & 0xFFFF) + 1
state.carry_flag = result >> 16
state.main_reg[dst] = result & 0xFFFF
#@instruction
#class dw:
# operands = '0x{imm16:04x}'
# encoding = 'iiii iiii iiii iiii'