-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.py
272 lines (223 loc) · 8.89 KB
/
machine.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
#!/usr/bin/python3
# pylint: disable=missing-function-docstring # чтобы не быть Капитаном Очевидностью
# pylint: disable=invalid-name # сохраним традиционные наименования сигналов
# pylint: disable=consider-using-f-string # избыточный синтаксис
import logging
import sys
from isa import Opcode, read_code
class DataPath:
def __init__(self, data_memory_size: int, input_buffer: list):
assert data_memory_size > 0, "Data_memory size should be non-zero"
self.data_memory_size = data_memory_size
self.data_memory = [0] * data_memory_size
self.da = 0 # data address
self.acc = 0 # accumulator
self.dr = 0 # data register
self.sr = 0 # storage register (stores instruction's argument)
self.input_buffer = input_buffer
self.output_buffer = []
self.flags = {"zero": 1, "sign": 0}
def latch_sr(self, arg: int):
self.sr = arg
def latch_dr(self, sel_memory: bool):
if sel_memory:
self.dr = self.data_memory[self.da]
else:
self.dr = self.sr
def latch_acc(self, sel: Opcode):
assert sel in [Opcode.READ, Opcode.LOAD, Opcode.LOAD_CONST,
Opcode.LOAD_INDIRECT, Opcode.ADD, Opcode.ADD_CONST,
Opcode.SUB], 'Internal error'
if sel is Opcode.READ:
if len(self.input_buffer) == 0:
raise EOFError()
symbol = self.input_buffer.pop(0)
symbol_code = ord(symbol)
self.acc = symbol_code
logging.debug('input: %s', repr(symbol))
elif sel in [Opcode.LOAD, Opcode.LOAD_CONST, Opcode.LOAD_INDIRECT]:
self.acc = self.dr
elif sel in [Opcode.ADD, Opcode.ADD_CONST, Opcode.SUB]:
if sel is Opcode.SUB:
self.acc -= self.dr
else:
self.acc += self.dr
if self.acc > 2**31 - 1:
self.acc -= 2**32
elif self.acc < -2**31:
self.acc += 2**32
self.flags = {"zero": 1 if self.acc == 0 else 0,
"sign": 1 if self.acc < 0 else 0}
def latch_da(self, sel_sr: bool):
if sel_sr:
self.da = self.sr
else:
self.da = self.dr
def output(self, sel: Opcode):
assert sel in [Opcode.PRINT, Opcode.PRINT_INT], 'Internal error'
if sel is Opcode.PRINT:
symbol = chr(self.acc)
else:
symbol = str(self.acc)
logging.debug('output: %s << %s', repr(
''.join(self.output_buffer)), repr(symbol))
self.output_buffer.append(symbol)
def wr(self):
self.data_memory[self.da] = self.acc
class ControlUnit:
def __init__(self, program: list, data_path: DataPath):
self.program = program
self.ip = 0 # instruction pointer
self.data_path = data_path
self._tick = 0
def tick(self):
self._tick += 1
def current_tick(self):
return self._tick
def latch_ip(self, sel_next: bool):
if sel_next:
self.ip += 1
else:
instr = self.program[self.ip]
assert 'arg' in instr, "internal error"
self.ip = instr["arg"]
def decode_and_execute_instruction(self):
instr = self.program[self.ip]
opcode = instr["opcode"]
arg = instr["arg"]
if arg != "":
self.data_path.latch_sr(arg)
if opcode is Opcode.LOAD:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.latch_dr(sel_memory=True)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.LOAD_CONST:
self.latch_ip(sel_next=True)
self.data_path.latch_dr(sel_memory=False)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.LOAD_INDIRECT:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.latch_dr(sel_memory=True)
self.tick()
self.data_path.latch_da(sel_sr=False)
self.tick()
self.data_path.latch_dr(sel_memory=True)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.STORE:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.wr()
self.tick()
elif opcode is Opcode.STORE_INDIRECT:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.latch_dr(sel_memory=True)
self.tick()
self.data_path.latch_da(sel_sr=False)
self.data_path.wr()
self.tick()
elif opcode is Opcode.ADD:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.latch_dr(sel_memory=True)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.ADD_CONST:
self.latch_ip(sel_next=True)
self.data_path.latch_dr(sel_memory=False)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.SUB:
self.latch_ip(sel_next=True)
self.data_path.latch_da(sel_sr=True)
self.data_path.latch_dr(sel_memory=True)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode in [Opcode.PRINT, Opcode.PRINT_INT]:
self.latch_ip(sel_next=True)
self.data_path.output(opcode)
self.tick()
elif opcode is Opcode.READ:
self.latch_ip(sel_next=True)
self.data_path.latch_acc(opcode)
self.tick()
elif opcode is Opcode.JMP:
self.latch_ip(sel_next=False)
self.tick()
elif opcode in [Opcode.JE, Opcode.JNE]:
expected_value = 1 if opcode is Opcode.JE else 0
if self.data_path.flags['zero'] == expected_value:
self.latch_ip(sel_next=False)
else:
self.latch_ip(sel_next=True)
self.tick()
elif opcode in [Opcode.JL, Opcode.JG]:
expected_value = 1 if opcode is Opcode.JL else 0
if self.data_path.flags['zero'] == 0 and self.data_path.flags['sign'] == expected_value:
self.latch_ip(sel_next=False)
else:
self.latch_ip(sel_next=True)
self.tick()
elif opcode is Opcode.HLT:
raise StopIteration()
def __repr__(self):
state = "{{TICK: {}, PC: {}, ADDR: {}, OUT: {}, ACC: {}}}".format(
self._tick,
self.ip,
self.data_path.da,
self.data_path.data_memory[self.data_path.da],
self.data_path.acc,
)
instr = self.program[self.ip]
instr_string = "{{OPCODE: {}, ARG: {}, POS: {}}}".format(
instr['opcode'].value,
instr['arg'],
instr['term'].pos
)
return "{}, {}".format(state, instr_string)
def simulation(code, input_tokens, data_memory_size, limit, debug_level):
data_path = DataPath(data_memory_size, input_tokens)
control_unit = ControlUnit(code, data_path)
instr_counter = 0
logging.debug('%s', control_unit)
try:
while True:
assert limit > instr_counter, "Too long execution, increase limit!"
control_unit.decode_and_execute_instruction()
instr_counter += 1
if instr_counter < debug_level:
logging.debug('%s', control_unit)
elif instr_counter == debug_level:
logging.debug('...')
except EOFError:
logging.warning('Input buffer is empty!')
except StopIteration:
pass
out = ''.join(data_path.output_buffer)
logging.info(f'output_buffer: {out}')
return ''.join(data_path.output_buffer), instr_counter, control_unit.current_tick()
def main(args):
assert len(args) == 2, "Wrong arguments: machine.py <code_file> <input_file>"
code_file, input_file = args
code = read_code(code_file)
with open(input_file, encoding="utf-8") as file:
input_text = file.read()
input_token = []
for char in input_text:
input_token.append(char)
input_token.append('\0')
output, instr_counter, ticks = simulation(code,
input_tokens=input_token,
data_memory_size=1000,
limit=10000000,
debug_level=50)
print(''.join(output))
print("instr_counter: ", instr_counter, "ticks:", ticks)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
main(sys.argv[1:])