-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
58 lines (41 loc) · 1.32 KB
/
memory.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
#!/usr/bin/python3
class Memory:
def __init__(self):
self._memory = [0 for _ in range(4096)]
def _check_address(self, address):
if not 0 <= address <= 4095:
raise Exception("address " + address +
" was outside of the memory")
def store_value(self, address, value):
self._check_address(address)
self._memory[address] = value
def get_value(self, address):
self._check_address(address)
return self._memory[address]
class Stack:
def __init__(self):
self._memory = [0 for _ in range(16)]
self._stack_pointer = 0
@property
def length(self):
return self._stack_pointer
def push(self, value):
if self._stack_pointer == 15:
raise Exception("Stack is full")
self._memory[self._stack_pointer] = value
self._stack_pointer += 1
def pop(self):
if self._stack_pointer == 0:
raise Exception("Stack is empty")
self._stack_pointer -= 1
return self._memory[self._stack_pointer]
class Register:
def __init__(self, bits):
self._bits = bits
self._value = 0
@property
def value(self):
return self._value % 2**self._bits
@value.setter
def value(self, argument):
self._value = argument