-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisa.py
141 lines (114 loc) · 3.18 KB
/
isa.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
from __future__ import annotations
import json
import struct
from enum import Enum
class OpcodeParamType(str, Enum):
CONST = "const"
ADDR = "addr"
UNDEFINED = "undefined"
ADDR_REL = "addr_rel"
class OpcodeParam:
def __init__(self, param_type: OpcodeParamType, value: any):
self.param_type = param_type
self.value = value
def __str__(self):
return f"({self.param_type}, {self.value})"
class OpcodeType(str, Enum):
DROP = "drop"
MUL = "mul"
DIV = "div"
SUB = "sub"
ADD = "add"
MOD = "mod"
SWAP = "swap"
OVER = "over"
DUP = "dup"
EQ = "eq"
GR = "gr"
LS = "ls"
DI = "di"
EI = "ei"
OMIT = "omit"
READ = "read"
# not used in source code, compile-generated
STORE = "store"
LOAD = "load"
PUSH = "push"
RPOP = "rpop" # move from return stack to data stack
POP = "pop" # move from data stack to return stack
JMP = "jmp"
ZJMP = "zjmp"
CALL = "call"
RET = "ret"
HALT = "halt"
def __str__(self):
return str(self.value)
def get_number_of_opcode_type(opcode: OpcodeType) -> int:
return list(OpcodeType).index(opcode)
def get_opcode_by_number(number: int) -> OpcodeType:
return list(OpcodeType)[number]
class Opcode:
def __init__(self, opcode_type: OpcodeType, params: list[OpcodeParam]):
self.opcode_type = opcode_type
self.params = params
class TermType(Enum):
(
# Term --> Opcode
DI,
EI,
DUP,
ADD,
SUB,
MUL,
DIV,
MOD,
OMIT,
SWAP,
DROP,
OVER,
EQ,
LS,
GR,
READ,
# Term !-> Opcode
VARIABLE,
ALLOT,
STORE,
LOAD,
IF,
ELSE,
THEN,
PRINT,
DEF,
RET,
DEF_INTR,
DO,
LOOP,
BEGIN,
UNTIL,
LOOP_CNT,
CALL,
STRING,
ENTRYPOINT,
) = range(35)
def read_binary_code(filename: str):
with open(filename, "rb") as file:
binary_data = file.read()
decoded_values = []
# Расшифровка бинарных данных
for i in range(0, len(binary_data), 4): # Предполагается, что каждое значение кодируется 4 байтами
struct_bytes = binary_data[i : i + 4]
decoded_value = struct.unpack("<I", struct_bytes)[0]
# Извлечение значений из битовых полей
opcode_number = decoded_value >> 27
index = (decoded_value >> 12) & 0x7FFF # Маска для извлечения 15 бит адреса
arg = decoded_value & 0xFFF # Маска для извлечения 12 бит аргумента
tuple_decode = {"index": index, "command": get_opcode_by_number(opcode_number).value, "arg": arg}
decoded_values.append(tuple_decode)
return decoded_values
def write_code(filename: str, code: bytes):
with open(filename, "wb") as file:
file.write(code)
def read_code(source_path: str) -> list:
with open(source_path, encoding="utf-8") as file:
return json.loads(file.read())