-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtual_CPU.py
362 lines (286 loc) · 12.7 KB
/
Virtual_CPU.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
from typing import Callable, Optional, Union
from inspect import getfullargspec
MAX_MEM: int = 1000
MIN_MEM: int = 100
MAX_REGISTERS: int = 20
ABSTRACT_CLASS_ERROR: str = "This is an abstract class that is not meant to be instantiated."
class Operand:
def get_value(self) -> None:
raise NotImplementedError(ABSTRACT_CLASS_ERROR)
def set_value(self, value) -> None:
raise NotImplementedError(ABSTRACT_CLASS_ERROR)
class ReadOnlyOperand(Operand):
def __init__(self, value) -> None:
self.value: any = value
def set_value(self, value) -> None:
raise RuntimeError('This class is Read-Only')
def get_value(self) -> any:
return self.value
class Memory:
def __init__(self, size: int) -> None:
self.size: int = size
self.mem: list[Union[str, int, float]] = [0] * size # allocate memory and initialize to 0
def set_mem(self, location: int, value: any) -> None:
if location < 0:
raise RuntimeError('location (' + str(location) + ') is less than 0')
elif location > (self.size - 1):
raise RuntimeError('location (' + str(location) + ') is greater than memory size (' + str(self.size) + ')')
else:
self.mem[location] = value
def get_mem(self, location: int) -> Union[str, int, float]:
if location < 0:
raise RuntimeError('location (' + str(location) + ') is less than 0')
elif location > (self.size - 1):
raise RuntimeError('location (' + str(location) + ') is greater than memory size (' + str(self.size) + ')')
else:
return self.mem[location]
class MemoryOperand(Operand):
def __init__(self, memory_object, location) -> None:
self.location = location
self.memory_object = memory_object
def get_value(self):
return self.memory_object.get_mem(self.location)
def set_value(self, value) -> None:
self.memory_object.set_mem(self.location, value)
class Literal(ReadOnlyOperand):
def __init__(self, value) -> None:
super().__init__(value)
def set_value(self, value) -> None:
raise RuntimeError(f'This class is Read-Only: you tried to set the value of a literal to "{value}" but it '
f'cannot be changed from "{self.value}"...')
def get_value(self) -> any:
return self.value
class Register(Operand):
def __init__(self, value: int = 0) -> None:
self.value: int = value
def set_value(self, value) -> None:
self.value = value
def get_value(self) -> int:
return self.value
class StackPointer(Operand):
def __init__(self, value) -> None:
self.value = value
self.maxVal = value
def pop(self) -> None:
self.value += 1
if self.value > self.maxVal:
raise RuntimeError("Stack mismatch: max value is " + str(self.maxVal) + ", SP is " + str(self.value))
def push(self) -> None:
self.value -= 1
if self.value < 0:
raise RuntimeError("Stack overflow: stack pointer is " + str(self.value))
def get_value(self) -> any:
return self.value
def set_value(self, value) -> None:
self.value = value
class Flag:
def __init__(self, value=False) -> None:
self.value = value
def set_true(self) -> None:
self.value = True
def set_false(self) -> None:
self.value = False
def get_value(self) -> any:
return self.value
class Instructions:
def __init__(self, cpu) -> None:
self.cpu = cpu
def inc(self, op1) -> None:
op1.set_value(op1.get_value() + 1)
def dec(self, op1) -> None:
op1.set_value(op1.get_value() - 1)
def cmp(self, op1, op2) -> None:
val = op1.get_value() - op2.get_value()
if 0 == val:
self.cpu.EqualFlag.set_true()
else:
self.cpu.EqualFlag.set_false()
if 0 < val:
self.cpu.GreaterFlag.set_true()
else:
self.cpu.GreaterFlag.set_false()
def out(self, op1) -> None:
print(op1.get_value())
def add(self, op1, op2) -> None:
op1.set_value(op1.get_value() + op2.get_value())
def sub(self, op1, op2) -> None:
op1.set_value(op1.get_value() - op2.get_value())
def jmp(self, op1) -> None:
self.cpu.IC.set_value(op1.get_value())
def je(self, op1) -> None:
if self.cpu.EqualFlag.get_value():
self.cpu.IC.set_value(op1.get_value())
def jne(self, op1) -> None:
if not self.cpu.EqualFlag.get_value():
self.cpu.IC.set_value(op1.get_value())
def jg(self, op1) -> None:
if self.cpu.GreaterFlag.get_value() and (not self.cpu.EqualFlag.get_value()):
self.cpu.IC.set_value(op1.get_value())
def jl(self, op1) -> None:
if (not self.cpu.GreaterFlag.get_value()) and (not self.cpu.EqualFlag.get_value()):
self.cpu.IC.set_value(op1.get_value())
def jle(self, op1) -> None:
if (not self.cpu.GreaterFlag.get_value()) or (self.cpu.EqualFlag.get_value()):
self.cpu.IC.set_value(op1.get_value())
def jge(self, op1) -> None:
if (self.cpu.GreaterFlag.get_value()) or (self.cpu.EqualFlag.get_value()):
self.cpu.IC.set_value(op1.get_value())
def mul(self, op1, op2) -> None:
op1.set_value(op1.get_value() * op2.get_value())
def div(self, op1, op2) -> None:
op1.set_value(op1.get_value() // op2.get_value())
def push(self, from_operand: Operand) -> None:
self.cpu.SP.push()
self.cpu.stack.set_mem(self.cpu.SP.get_value(), from_operand.get_value())
def pop(self, to_operand: Operand) -> None:
to_operand.set_value(self.cpu.stack.get_mem(self.cpu.SP.get_value()))
self.cpu.SP.pop()
def save(self, to_address: Operand, value_to_set: Operand) -> None:
self.cpu.set_mem(to_address.get_value(), value_to_set.get_value())
def load(self, from_address: Operand, to_operator: Operand) -> None:
to_operator.set_value(self.cpu.get_mem(from_address.get_value()))
def call(self, function_address: Operand) -> None:
self.push(Literal(self.cpu.IC.get_value() + 1))
self.jmp(function_address)
def ret(self) -> None:
self.pop(self.cpu.IC)
def set(self, to_operand: Operand, from_operand: Operand) -> None:
to_operand.set_value(from_operand.get_value())
def end(self) -> bool:
return False
def nop(self) -> None:
pass
class CPU:
def __init__(self, registers: int, memory: Memory, stack_memory: Memory, debug_print: bool = False) -> None:
self.memory: Memory = memory
self.stack: Memory = stack_memory
self.registers: list[Register] = [Register() for _ in range(registers)]
self.IC: Register = Register(0)
self.instructions: Instructions = Instructions(self)
self.EqualFlag: Flag = Flag()
self.GreaterFlag: Flag = Flag()
self.SP: StackPointer = StackPointer(stack_memory.size - 1)
self.labels: dict[str, int] = {}
self.debug_print: bool = debug_print
def execute_program(self, entry_point=0) -> None:
print('Starting Program at Entry Point ' + str(entry_point))
self.IC.set_value(entry_point)
ic_val: int = self.IC.get_value()
while self.execute_instruction():
if self.IC.get_value() == ic_val:
self.IC.set_value(self.IC.get_value() + 1)
ic_val: int = self.IC.get_value()
print('Program Ended')
def execute_instruction(self) -> bool:
instruction_string: str = self.memory.get_mem(self.IC.get_value())
if self.debug_print:
print(instruction_string)
if instruction_string is not None:
return self.decode_instruction(instruction_string)
else:
return False
def decode_instruction(self, instruction_string: str) -> bool:
split_instruction: list[str] = instruction_string.split(' ', 1)
instruction_name: str = split_instruction[0]
try:
instruction_function: Callable = getattr(self.instructions, instruction_name)
except AttributeError:
raise RuntimeError(f'You\'ve attempted to call a nonexistent instruction: "{instruction_name}"...weird...')
else:
num_operands: int = len(getfullargspec(instruction_function).args) - 1
if num_operands == 2:
operands: list[str] = split_instruction[1].split(', ', 1)
op1: Operand = self.decode_operand(operands[0])
op2: Operand = self.decode_operand(operands[1])
result: Optional[bool] = instruction_function(op1, op2)
elif num_operands == 1:
op1: Operand = self.decode_operand(split_instruction[1])
result: Optional[bool] = instruction_function(op1)
else:
result: Optional[bool] = instruction_function()
if result is None: # result is None; this is normal (all instructions besides "end" return nothing)
return True
elif not result: # result is False; the program has halted
return False
else: # shouldn't happen
print(f'Weird: result from {instruction_name} was {result}. It should only either be None or False, though')
return True
def decode_operand(self, op: str) -> Operand:
# Registers start with R
# Literal Numbers start with #
# Literal Strings start with "
# Memory starts with M
# Stack Pointer = SP
# Instruction Counter = IC
if op in self.labels:
label_location: int = self.labels[op]
return Literal(label_location)
if op == 'SP':
return self.SP
if op == 'IC':
return self.IC
op_type = op[:1]
op_value = op[1:]
if op_type == 'R':
register_number: int
try:
register_number = int(op_value)
except ValueError:
raise RuntimeError(f'Uh what? You want me to convert "{op_value}" to an int? That\'s not possible...')
if register_number < 0 or register_number > (len(self.registers) - 1):
raise RuntimeError(f'You tried to access register number "{register_number}" but that\'s invalid.')
return self.registers[register_number]
if op_type == '#':
literal_value: Union[int, float]
try:
literal_value = int(op_value)
except ValueError:
try:
literal_value = float(op_value)
except ValueError:
raise RuntimeError(f'I expected "{op_value}" to be an int or float! what am I supposed to do here?')
return Literal(literal_value)
if op_type == '"':
return Literal(op_value[:-1])
if op_type == 'M':
return MemoryOperand(self.memory, int(op_value))
raise RuntimeError(f'Can\'t decode operand "{op}". There is no label with that name. I have no clue what '
f'you\'re trying to say.')
class Computer:
def __init__(self, registers=10, memory=256):
self.memory: Memory = Memory(memory)
self.stackMem: Memory = Memory(memory)
self.processor: CPU = CPU(registers, self.memory, self.stackMem)
def load_program(self, program: str, entry_point: int = 0):
i: int = entry_point
for string in program.splitlines():
set_string: str = string
if set_string[:1] != ';': # line is commented out
split_instruction: list[str] = string.split(' ', 1)
label_name: str = split_instruction[0]
if label_name[-1:] == ':': # line starts with a label
self.processor.labels[label_name[:-1]] = i # store the address of this label in a dict
set_string = split_instruction[1]
self.memory.set_mem(i, set_string)
i += 1
def start(self, entry_point=0):
self.processor.execute_program(entry_point)
def run_program(self, program: str, entry_point: int = 0):
self.load_program(program, entry_point)
self.start(entry_point)
# TODO: Add a way to label memory addresses
if __name__ == '__main__':
PC: Computer = Computer()
program_str: str = '''\
out "Let's begin...
set R0, #10
loop: out R0
dec R0
cmp R0, #0
jge loop
test: #123
out "Okay, it's over!
end
;commented out
'''
PC.run_program(program_str)