-
Notifications
You must be signed in to change notification settings - Fork 1
/
knightdecode.py
executable file
·864 lines (769 loc) · 24.7 KB
/
knightdecode.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#!/usr/bin/env python
#
# A derivitive port of:
# https://github.com/oriansj/stage0/blob/master/vm_decode.c
#
# Copyright (C) 2019 Mark Jenkins <[email protected]>
# This file is part of knightpies
#
# knightpies is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# knightpies is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with knightpies. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division # prevent use of "/" in the old way
from sys import stderr, stdin, stdout, exc_info, exit
from time import sleep
from array import array
import knightinstructions
from pythoncompat import print_func, init_array_itemsize_8, \
get_binary_mode_stdout, COMPAT_FALSE, COMPAT_TRUE
from constants import \
EXIT_FAILURE, \
IP, REG, MEM, HALTED, EXCEPT, PERF_COUNT, TAPE1FILENAME, TAPE2FILENAME, \
OP, RAW, CURIP, NEXTIP, RESTOF, INVALID, \
RAW_XOP, XOP, RAW_IMMEDIATE, IMMEDIATE, I_REGISTERS, HAL_CODE, \
ARRAY_TYPE_UNSIGNED_CHAR, ARRAY_TYPE_UNSIGNED_SHORT, \
ARRAY_TYPE_UNSIGNED_INT, ARRAY_TYPE_UNSIGNED_INT_LONG, \
ARRAY_TYPE_UNSIGNED_LONG_LONG, \
HALT_OP, HAL_CODE_OP, \
HAL_CODE_FGETC, HAL_CODE_FPUTC, HAL_CODE_FOPEN_WRITE, HAL_CODE_FCLOSE
from knightdecodeutil import outside_of_world, OutsideOfWorldException
NUM_REGISTERS = 16
SIZE_UNSIGNED_CHAR = 1
MIN_SIZE_UNSIGNED_SHORT = 2
MIN_SIZE_UNSIGNED_INT = 4
MIN_INSTRUCTION_LEN = 4
DEBUG = COMPAT_FALSE
OUTSIDE_WORLD_ERROR = "READ Instruction outside of World"
class InstructionNotImplemented(Exception):
pass
def grow_memory(vm, size):
while len(vm[MEM])<size:
vm[MEM].append(0)
def create_vm(size, registersize=32,
tapefile1="tape_01", tapefile2="tape_02",
stdin=stdin, stdout=None):
if stdout==None:
stdout = get_binary_mode_stdout()
instruction_pointer = 0
if registersize==32:
registers = array(ARRAY_TYPE_UNSIGNED_INT)
assert registers.itemsize == MIN_SIZE_UNSIGNED_INT # 4
elif registersize==64:
registers = init_array_itemsize_8()
if None==registers: # 64//8==8
raise Exception("64 bit register size not available "
"on this platform")
elif registersize==16:
# allocate registers, assert unsigned short is the size we think it is
registers = array(ARRAY_TYPE_UNSIGNED_SHORT)
assert registers.itemsize == MIN_SIZE_UNSIGNED_SHORT # 2
else:
raise Exception("%d bit register size not available on this platform"
% registersize
)
for i in range(NUM_REGISTERS):
registers.append(0)
amount_of_ram = size
# allocate memory, assert unsigned char is the size we think it is
memory = array(ARRAY_TYPE_UNSIGNED_CHAR)
assert memory.itemsize == SIZE_UNSIGNED_CHAR # 1
halted = COMPAT_FALSE
exception = COMPAT_FALSE
performance_counter = 0
vm = (instruction_pointer, registers, memory,
halted, exception, performance_counter,
tapefile1, tapefile2, [None,None, stdin, stdout])
grow_memory(vm, size)
return vm
def unpack_byte(a):
table = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F']
assert len(table)==16
return (table[a // 16], table[a % 16])
def read_instruction(vm):
current_ip = vm[IP]
next_ip = current_ip+MIN_INSTRUCTION_LEN
# Why current_ip+MIN_INSTRUCTION_LEN-1 and not just current_ip ?
# If the end of memory isn't MIN_INSTRUCTION_LEN byte aligned, than
# current_ip may be in bounds but the last byte of it might not be
outside_of_world(vm[MEM], next_ip-1, OUTSIDE_WORLD_ERROR)
instruction_bytes = vm[MEM][current_ip:current_ip+MIN_INSTRUCTION_LEN]
opcode = unpack_byte(instruction_bytes[0])
return (opcode, # OP
instruction_bytes, # RAW
current_ip, # CURIP
next_ip, # NEXTIP
[unpack_byte(a) for a in instruction_bytes[1:]], # RESTOF
COMPAT_FALSE # INVALID
)
def halt_vm(vm):
# recontruct vm tuple with vm[HALTED] = COMPAT_TRUE
return vm[0:HALTED] + (COMPAT_TRUE,) + vm[HALTED+1:]
def increment_vm_perf_count(vm):
return vm[0:PERF_COUNT] + (vm[PERF_COUNT]+1,) + vm[PERF_COUNT+1:]
def invalidate_instruction(i):
return i[0:INVALID] + (COMPAT_TRUE,) + i[INVALID+1:]
def illegal_instruction(vm, current_instruction):
print_func("Invalid instruction was recieved at address:%08X" %
current_instruction[CURIP],
file=stderr)
print_func("After %d instructions" % vm[PERF_COUNT], file=stderr)
print_func("Unable to execute the following instruction:\n\t%s" %
string_unpacked_instruction(current_instruction),
file=stderr)
current_instruction = invalidate_instruction(current_instruction)
vm = halt_vm(vm)
if DEBUG:
print_func("Computer Program has Halted", file=stderr)
#if TRACE: # TODO
# record_trace("HALT") # TODO
# print_traces() # TODO
exit(EXIT_FAILURE)
def string_unpacked_instruction(i):
return (''.join(i[OP]) +
''.join(''.join(rpair)
for rpair in i[RESTOF]) )
def vm_with_new_ip(vm, new_ip):
return vm[0:IP] + (new_ip,) + vm[IP+1:]
def make_eval_instruction_for_registersize(registersizebits):
EVAL_TABLE = make_eval_tables_for_register_size(registersizebits)
def eval_instruction(vm, current_instruction, halt_print=COMPAT_TRUE):
vm = increment_vm_perf_count(vm)
if DEBUG:
print_func("Executing: %s" %
string_unpacked_instruction(current_instruction),
file=stderr)
sleep(1)
raw0 = current_instruction[RAW][0]
if raw0 == 0: # Deal with NOPs
if [0,0,0,0]==current_instruction[RAW].tolist():
#if TRACE: # TODO
# record_trace("NOP") # TODO
return vm_with_new_ip(vm, current_instruction[NEXTIP])
illegal_instruction(vm, current_instruction)
elif raw0 in DECODE_TABLE:
assert raw0 in EVAL_TABLE
current_instruction = DECODE_TABLE[raw0](vm, current_instruction)
return vm_with_new_ip(vm,
EVAL_TABLE[raw0](vm, current_instruction) )
elif raw0 == HALT_OP: # Deal with HALT
vm = halt_vm(vm)
if halt_print:
print_func(
"Computer Program has Halted\nAfter Executing %d "
"instructions" % vm[PERF_COUNT],
file=stderr)
# if TRACE: # TODO
# record_trace("HALT") # TODO
# print_traces() # TODO
return vm
else:
illegal_instruction(vm, current_instruction)
# we shouldn't make it this far, other branches call exit()
assert COMPAT_FALSE
return None
return eval_instruction
def decode_4OP(vm, c):
raw_xop = c[RAW][1]
xop = (c[RESTOF][0][0], c[RESTOF][0][1])
raw_immediate = 0
i_registers = (
c[RAW][2]//16,
c[RAW][2]%16,
c[RAW][3]//16,
c[RAW][3]%16,
)
return c + (raw_xop, # RAW_XOP
xop, # XOP
raw_immediate, # RAW_IMMEDIATE
(), # IMMEDIATE
i_registers, # I_REGISTERS
None, # HAL_CODE
)
def decode_3OP(vm, c):
raw_xop = c[RAW][1]*0x10 + c[RAW][2]//16
xop = (c[RESTOF][0][0], c[RESTOF][0][1], c[RESTOF][1][0])
assert len(xop) == 3
raw_immediate = 0
i_registers = (
c[RAW][2]%16,
c[RAW][3]//16,
c[RAW][3]%16,
)
return c + (raw_xop, # RAW_XOP
xop, # XOP
raw_immediate, # RAW_IMMEDIATE
(), # IMMEDIATE
i_registers, # I_REGISTERS
None, # HAL_CODE
)
def decode_2OP(vm, c):
raw_xop = c[RAW][1]*0x100 + c[RAW][2]
xop = tuple([x
for r in c[RESTOF][0:2]
for x in r ])
assert len(xop) == 4
raw_immediate = 0
i_registers = (
c[RAW][3]//16,
c[RAW][3]%16,
)
return c + (raw_xop, # RAW_XOP
xop, # XOP
raw_immediate, # RAW_IMMEDIATE
(), # IMMEDIATE
i_registers, # I_REGISTERS
None, # HAL_CODE
)
def decode_1OP(vm, c):
raw_xop = c[RAW][1]*0x1000 + c[RAW][2]*0x10 + c[RAW][3]//16
xop = tuple([x
for r in c[RESTOF][0:2]
for x in r ]) + (c[RESTOF][2][0],)
assert len(xop) == 5
raw_immediate = 0
i_registers = (c[RAW][3]%16,)
return c + (raw_xop, # RAW_XOP
xop, # XOP
raw_immediate, # RAW_IMMEDIATE
(), # IMMEDIATE
i_registers, # I_REGISTERS
None, # HAL_CODE
)
def decode_2OPI(vm, c):
next_ip = c[NEXTIP]
raw_immediate = vm[MEM][next_ip]
next_ip+=1
hold = vm[MEM][next_ip]
next_ip+=1
raw_immediate = raw_immediate*0x100 + hold
immediate = tuple([x
for r in c[RESTOF][1:]
for x in r] )
assert len(immediate) == 4
i_registers = (c[RAW][3]//16, c[RAW][3]%16)
return c[0:NEXTIP] + (next_ip,) + c[NEXTIP+1:] + (
None, # RAW_XOP
None, # XOP
raw_immediate, # RAW_IMMEDIATE
immediate, # IMMEDIATE
i_registers, # I_REGISTERS
None, # HAL_CODE
)
def decode_1OPI(vm, c):
next_ip = c[NEXTIP]
outside_of_world(vm[MEM], next_ip+2-1, OUTSIDE_WORLD_ERROR)
raw_immediate = vm[MEM][next_ip]
next_ip+=1
hold = vm[MEM][next_ip]
next_ip+=1
raw_immediate = raw_immediate*0x100 + hold
immediate = tuple([x
for r in c[RESTOF][1:]
for x in r] )
assert len(immediate) == 4
hal_code = 0
raw_xop = c[RAW][3]//16
xop = (c[RESTOF][2][1],)
i_registers = (c[RAW][3]%16,)
return c[0:NEXTIP] + (next_ip,) + c[NEXTIP+1:] + (
raw_xop, # RAW_XOP
xop,
raw_immediate, # RAW_IMMEDIATE
immediate, # IMMEDIATE
i_registers, # I_REGISTERS
hal_code, # HAL_CODE
)
def decode_0OPI(vm, c):
raw_immediate = c[RAW][2]*0x100 + c[RAW][3]
immediate = tuple([x
for r in c[RESTOF][1:]
for x in r] )
assert len(immediate)==4
hal_code = 0
raw_xop = c[RAW][1]
xop = c[RESTOF][0]
assert len(xop)==2
return c + (
raw_xop, # RAW_XOP
xop, # XOP
raw_immediate, # RAW_IMMEDIATE
immediate, # IMMEDIATE
(), # I_REGISTERS
hal_code, # HAL_CODE
)
def decode_HALCODE(vm, c):
return c + (
None, # RAW_XOP
None, # XOP
None, # RAW_IMEDIATE
None, # IMMEDIATE
(), # I_REGISTERS
c[RAW][1]*0x10000 + c[RAW][2]*0x100 + c[RAW][3] # HAL_CODE
)
EVAL_4OP_INT_TABLE_STRING = {
0x00: "ADD_CI",
0x01: "ADD_CO",
0x02: "ADD_CIO",
0x03: "ADDU_CI",
0x04: "ADDU_CO",
0x05: "ADDU_CIO",
0x06: "SUB_BI",
0x07: "SUB_BO",
0x08: "SUB_BIO",
0x09: "SUBU_BI",
0x0A: "SUBU_BO",
0x0B: "SUBU_BIO",
0x0C: "MULTIPLY",
0x0D: "MULTIPLYU",
0x0E: "DIVIDE",
0x0F: "DIVIDEU",
0x10: "MUX",
0x11: "NMUX",
0x12: "SORT",
0x13: "SORTU",
}
EVAL_3OP_INT_TABLE_STRING = {
0x000: "ADD",
0x001: "ADDU",
0x002: "SUB",
0x003: "SUBU",
0x004: "CMP",
0x005: "CMPU",
0x006: "MUL",
0x007: "MULH",
0x008: "MULU",
0x009: "MULUH",
0x00A: "DIV",
0x00B: "MOD",
0x00C: "DIVU",
0x00D: "MODU",
0x010: "MAX",
0x011: "MAXU",
0x012: "MIN",
0x013: "MINU",
0x020: "AND",
0x021: "OR",
0x022: "XOR",
0x023: "NAND",
0x024: "NOR",
0x025: "XNOR",
0x026: "MPQ",
0x027: "LPQ",
0x028: "CPQ",
0x029: "BPQ",
0x030: "SAL",
0x031: "SAR",
0x032: "SL0",
0x033: "SR0",
0x034: "SL1",
0x035: "SR1",
0x036: "ROL",
0x037: "ROR",
0x038: "LOADX",
0x03A: "LOADXU8",
0x03B: "LOADX16",
0x03C: "LOADXU16",
0x03D: "LOADX32",
0x03E: "LOADXU32",
0x048: "STOREX",
0x049: "STOREX8",
0x04A: "STOREX16",
0x04B: "STOREX32",
0x050: "CMPJUMP_G",
0x051: "CMPJUMP_GE",
0x052: "CMPJUMP_E",
0x053: "CMPJUMP_NE",
0x054: "CMPJUMP_LE",
0x055: "CMPJUMP_L",
0x060: "CMPJUMPU_G",
0x061: "CMPJUMPU_GE",
0x064: "CMPJUMPU_LE",
0x065: "CMPJUMPU_L",
}
# using a dictionary type instead of set/fozenset or sets for python 2.2
# compatibility
EVAL_3OP_INT_ILLEGAL = {
0x014: None,
0x015: None,
0x016: None,
0x017: None,
0x018: None,
0x019: None,
0x01A: None,
0x01B: None,
}
EVAL_2OP_INT_TABLE_STRING = {
0x0000: "NEG",
0x0001: "ABS",
0x0002: "NABS",
0x0003: "SWAP",
0x0004: "COPY",
0x0005: "MOVE",
0x0006: "NOT",
0x0100: "BRANCH",
0x0101: "CALL",
0x0200: "PUSHR",
0x0201: "PUSH8",
0x0202: "PUSH16",
0x0203: "PUSH32",
0x0280: "POPR",
0x0281: "POP8",
0x0282: "POPU8",
0x0283: "POP16",
0x0284: "POPU16",
0x0285: "POP32",
0x0286: "POPU32",
0x0300: "CMPSKIP_G",
0x0301: "CMPSKIP_GE",
0x0302: "CMPSKIP_E",
0x0304: "CMPSKIP_NE",
0x0305: "CMPSKIP_LE",
0x0305: "CMPSKIP_L",
0x0380: "CMPSKIPU_G",
0x0381: "CMPSKIPU_GE",
0x0384: "CMPSKIPU_LE",
0x0385: "CMPSKIPU_L",
}
EVAL_1OP_INT_TABLE_STRING = {
0x00000: "READPC",
0x00001: "READSCID",
0x00002: "FALSE",
0x00003: "TRUE",
0x01000: "JSR_COROUTINE",
0x01001: "RET",
0x02000: "PUSHPC",
0x02001: "POPPC",
}
EVAL_2OPI_INT_TABLE_STRING = {
0x0E: "ADDI",
0x0F: "ADDUI",
0x10: "SUBI",
0x11: "SUBUI",
0x12: "CMPI",
0x13: "LOAD",
0x14: "LOAD8",
0x15: "LOADU8",
0x16: "LOAD16",
0x17: "LOADU16",
0x18: "LOAD32",
0x19: "LOADU32",
0x1F: "CMPUI",
0x20: "STORE",
0x21: "STORE8",
0x22: "STORE16",
0x23: "STORE32",
0xB0: "ANDI",
0xB1: "ORI",
0xB2: "XORI",
0xB3: "NANDI",
0xB4: "NORI",
0xB5: "XNORI",
0xC0: "CMPJUMPI_G",
0xC1: "CMPJUMPI_GE",
0xC2: "CMPJUMPI_E",
0xC3: "CMPJUMPI_NE",
0xC4: "CMPJUMPI_LE",
0xC5: "CMPJUMPI_L",
0xD0: "CMPJUMPUI_G",
0xD1: "CMPJUMPUI_GE",
0xD4: "CMPJUMPUI_LE",
0xD5: "CMPJUMPUI_L",
}
EVAL_1OPI_INT_TABLE_STRING = {
0x2C0: "JUMP_C",
0x2C1: "JUMP_B",
0x2C2: "JUMP_O",
0x2C3: "JUMP_G",
0x2C4: "JUMP_GE",
0x2C5: "JUMP_E",
0x2C6: "JUMP_NE",
0x2C7: "JUMP_LE",
0x2C8: "JUMP_L",
0x2C9: "JUMP_Z",
0x2CA: "JUMP_NZ",
0x2CB: "JUMP_P",
0x2CC: "JUMP_NP",
0x2D0: "CALLI",
0x2D1: "LOADI",
0x2D2: "LOADUI",
0x2D3: "SALI",
0x2D4: "SARI",
0x2D5: "SL0I",
0x2D6: "SR0I",
0x2D7: "SL1I",
0x2D8: "SR1I",
0x2E0: "LOADR",
0x2E1: "LOADR8",
0x2E2: "LOADRU8",
0x2E3: "LOADR16",
0x2E4: "LOADRU16",
0x2E5: "LOADR32",
0x2E6: "LOADRU32",
0x2F0: "STORER",
0x2F1: "STORER8",
0x2F2: "STORER16",
0x2F3: "STORER32",
0xA00: "CMPSKIPI_G",
0xA01: "CMPSKIPI_GE",
0xA02: "CMPSKIPI_E",
0xA03: "CMPSKIPI_NE",
0xA04: "CMPSKIPI_LE",
0xA05: "CMPSKIPI_L",
0xA10: "CMPSKIPUI_G",
0xA11: "CMPSKIPUI_GE",
0xA14: "CMPSKIPUI_LE",
0xA15: "CMPSKIPUI_L",
}
def eval_N_OP_int(vm, c, n, lookup_val, lookup_table,
immediate=COMPAT_FALSE, illegal_table=None):
next_ip = None
if immediate:
name = "ILLEGAL_%dOPI" % n
else:
name = "ILLEGAL_%dOP" % n
if lookup_val in lookup_table:
instruction_func, instruction_str = lookup_table[lookup_val]
if DEBUG:
name = instruction_str
#elif TRACE: # TODO
# record_trace(instruction_str) # TODO
next_ip = instruction_func(vm, c)
# not sure why zome XOP are matched explicitly for illegal whereas
# others fall into default when the handling is the same
# some explicitly illegal XOPs in eval_3OP_int probably just exist
# to reserve them for future use
# elif illegal_table!=None and raw_xop in illegal_table:
# illegal_instruction(vm, c)
else:
illegal_instruction(vm, c)
if DEBUG:
print_func(
("# %s" + " reg%d"*n) % ( (name,) + c[I_REGISTERS][0:n] ),
end="",
sep="",
) # print_func
if immediate:
print_func(" %d" % c[RAW_IMMEDIATE] )
else:
print_func()
return next_ip
def get_instruction_module_for_registersize_bits(registersizebits):
# defer the imports to here to only import what's needed and
# in case the user opts for unoptimized version
if registersizebits==64:
import knightinstructions64
return knightinstructions64
elif registersizebits==32:
import knightinstructions32
return knightinstructions32
elif registersizebits==16:
import knightinstructions16
return knightinstructions16
else:
return knightinstructions
def make_eval_tables_for_register_size(registersizebits):
knightmodule = get_instruction_module_for_registersize_bits(
registersizebits)
def lookup_instruction_and_debug_str(x, replace_underscore=COMPAT_TRUE):
table_key, instruction_str = x
if replace_underscore:
instruction_str_debug = instruction_str.replace("_", ".")
else:
instruction_str_debug = instruction_str
return (table_key,
(getattr(knightmodule, instruction_str),
instruction_str_debug
) # inner tuple
) # outer tuple
def lookup_instruction_and_debug_str_no_sub(x):
return lookup_instruction_and_debug_str(
x, replace_underscore=COMPAT_FALSE)
EVAL_4OP_INT_TABLE = dict( map(
lookup_instruction_and_debug_str,
EVAL_4OP_INT_TABLE_STRING.items() ) # map
) # dict
EVAL_2OP_INT_TABLE = dict( map(
lookup_instruction_and_debug_str,
EVAL_2OP_INT_TABLE_STRING.items() ) # map
) # dict
EVAL_3OP_INT_TABLE = dict( map(
lookup_instruction_and_debug_str,
EVAL_3OP_INT_TABLE_STRING.items() ) # map
) # dict
EVAL_1OP_INT_TABLE = dict( map(
lookup_instruction_and_debug_str_no_sub,
EVAL_1OP_INT_TABLE_STRING.items() ) # map
) # dict
EVAL_2OPI_INT_TABLE = dict( map(
lookup_instruction_and_debug_str,
EVAL_2OPI_INT_TABLE_STRING.items() ) # map
) # dict
EVAL_1OPI_INT_TABLE = dict( map(
lookup_instruction_and_debug_str,
EVAL_1OPI_INT_TABLE_STRING.items() ) # map
) # dict
def eval_4OP_Int(vm, c):
return eval_N_OP_int(vm, c, 4, c[RAW_XOP], EVAL_4OP_INT_TABLE)
def eval_3OP_Int(vm, c):
return eval_N_OP_int(vm, c, 3,
c[RAW_XOP], EVAL_3OP_INT_TABLE,
illegal_table=EVAL_3OP_INT_ILLEGAL)
def eval_2OP_Int(vm, c):
return eval_N_OP_int(vm, c, 2, c[RAW_XOP], EVAL_2OP_INT_TABLE)
def eval_1OP_Int(vm, c):
return eval_N_OP_int(vm, c, 1, c[RAW_XOP], EVAL_1OP_INT_TABLE)
def eval_2OPI_Int(vm, c):
return eval_N_OP_int(vm, c, 2, c[RAW][2], EVAL_2OPI_INT_TABLE,
immediate=COMPAT_TRUE)
def eval_Integer_1OPI(vm, c):
return eval_N_OP_int(vm, c, 2,
c[RAW][2]*16 + c[RAW_XOP], EVAL_1OPI_INT_TABLE,
immediate=COMPAT_TRUE)
def eval_Integer_0OPI(vm, c):
next_ip = None
name = "ILLEGAL_0OPI"
if c[RAW_XOP] == 0x00: # JUMP
if DEBUG:
name = "JUMP"
#elif TRACE: # TODO
# record_trace("JUMP") # TODO
next_ip = knightmodule.JUMP(vm, c)
else:
illegal_instruction(vm, c)
if DEBUG:
print_func( "# %s %d\n" % (name, c[RAW_IMMEDIATE]) )
return next_ip
EVAL_TABLE = {
0x01: eval_4OP_Int,
0x05: eval_3OP_Int,
0x09: eval_2OP_Int,
0x0D: eval_1OP_Int,
0xE1: eval_2OPI_Int,
0xE0: eval_Integer_1OPI,
0x3C: eval_Integer_0OPI,
HAL_CODE_OP: eval_HALCODE,
}
return EVAL_TABLE
HAL_CODES_TABLE_STRING = {
0x100000: "FOPEN_READ",
0x100001: "FOPEN_WRITE",
HAL_CODE_FCLOSE: "FCLOSE",
0x100003: "REWIND",
0x100004: "FSEEK",
HAL_CODE_FGETC: "FGETC",
HAL_CODE_FPUTC: "FPUTC",
HAL_CODE_FOPEN_WRITE: "FOPEN_WRITE",
0x110000: "HAL_MEM",
}
def hal_code_table_entry(x):
table_key, instruction_str = x
return (table_key,
(getattr(knightinstructions, "vm_" + instruction_str),
instruction_str
) # inner tuple
) # outer tuple
HAL_CODES_TABLE = dict( map( hal_code_table_entry,
HAL_CODES_TABLE_STRING.items() ) # map
) # dict
def eval_HALCODE(vm, c):
next_ip = None
name = "ILLEGAL_HALCODE"
# POSIX MODE instructions not implemented
if c[HAL_CODE] in HAL_CODES_TABLE:
instruction_func, instruction_str = HAL_CODES_TABLE[c[HAL_CODE]]
if DEBUG:
name = instruction_str
#elif TRACE: # TODO
# record_trace(instruction_str) # TODO
instruction_func(vm)
next_ip = c[NEXTIP]
else:
print_func("Invalid HALCODE", file=stderr)
print_func("Computer Program has Halted", file=stderr)
illegal_instruction(vm, c)
if DEBUG:
print_func("# %s" % name)
return next_ip
DECODE_TABLE = {
0x01: decode_4OP,
0x05: decode_3OP,
0x09: decode_2OP,
0x0D: decode_1OP,
0xE1: decode_2OPI,
0xE0: decode_1OPI,
0x3C: decode_0OPI,
0x42: decode_HALCODE,
}
assert \
(tuple(sorted(DECODE_TABLE.keys())) ==
tuple(sorted(make_eval_tables_for_register_size(0).keys()))
) # end expression
EVAL_INSTRUCTION_FOR_REGISTER_SIZES = {}
def get_eval_instruction_for_register_size(regsize_bytes):
global EVAL_INSTRUCTION_FOR_REGISTER_SIZES
if regsize_bytes not in EVAL_INSTRUCTION_FOR_REGISTER_SIZES:
EVAL_INSTRUCTION_FOR_REGISTER_SIZES[regsize_bytes] = \
make_eval_instruction_for_registersize(regsize_bytes*8)
return EVAL_INSTRUCTION_FOR_REGISTER_SIZES[regsize_bytes]
def eval_instruction(vm, current_instruction,
optimize=COMPAT_TRUE, halt_print=COMPAT_TRUE):
if optimize:
return get_eval_instruction_for_register_size(
vm[REG].itemsize)(vm, current_instruction, halt_print=halt_print)
else:
return get_eval_instruction_for_register_size(0)(
vm, current_instruction, halt_print=halt_print)
def make_read_and_eval_for_registersize(registersizebits):
global EVAL_INSTRUCTION_FOR_REGISTER_SIZES
eval_instruction_specific_bit = get_eval_instruction_for_register_size(
registersizebits//8)
def read_and_eval(vm, halt_print=COMPAT_TRUE):
try:
c = read_instruction(vm)
vm = eval_instruction_specific_bit(vm, c, halt_print=halt_print)
if vm==None or vm[IP]==None:
raise InstructionNotImplemented(c)
return vm
except OutsideOfWorldException:
e = exc_info()[1] # to remain backwards and forwards compatible
print_func(
"Invalid state reached after: %d instructions" % vm[PERF_COUNT],
file=stderr)
print_func(
"%d: %s" % (e.args[1], e.args[0]),
file=stderr )
# if TRACE: TODO
# pass # TODO
exit(EXIT_FAILURE)
return read_and_eval
READ_AND_EVAL_TABLE = {}
def get_read_and_eval_for_register_size(regsize_bytes):
global READ_AND_EVAL_TABLE
if regsize_bytes not in READ_AND_EVAL_TABLE:
READ_AND_EVAL_TABLE[regsize_bytes] = \
make_read_and_eval_for_registersize(regsize_bytes*8)
return READ_AND_EVAL_TABLE[regsize_bytes]
def read_and_eval(vm, optimize=COMPAT_TRUE, halt_print=COMPAT_TRUE):
if optimize:
return get_read_and_eval_for_register_size(vm[REG].itemsize)(
vm, halt_print=halt_print)
else:
# this forces the generic version
return get_read_and_eval_for_register_size(0)(
vm, halt_print=halt_print)
if __name__ == "__main__":
vm = create_vm(2**16) # (64*1024)
print_func( "vm created %d bytes" % len(vm[MEM]) )
instruction = read_instruction(vm)
print_func( "instruction opcode unpacked (0x0%s, 0x0%s)" %
instruction[OP] )