-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
191 lines (131 loc) · 6.07 KB
/
commands.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
#!/usr/bin/python3
import random
from display import Point
class Command:
class BaseCommand:
def __init__(self, interpreter):
self.interpreter = interpreter
class Clear(BaseCommand):
def execute_command(self):
self.interpreter.display.clear()
class Call(BaseCommand):
def execute_command(self, new_address):
self.interpreter.stack.push(self.interpreter.instruction_pointer)
self.interpreter.instruction_pointer = new_address
class Return(BaseCommand):
def execute_command(self):
address = self.interpreter.stack.pop()
self.interpreter.instruction_pointer = address
class Jump(BaseCommand):
def execute_command(self, address):
self.interpreter.instruction_pointer = address
class PassIfEqual(BaseCommand):
def execute_command(self, first_number, second_number):
if first_number == second_number:
self.interpreter.instruction_pointer += 2
class PassIfNotEqual(BaseCommand):
def execute_command(self, first_number, second_number):
if first_number != second_number:
self.interpreter.instruction_pointer += 2
class Move(BaseCommand):
def execute_command(self, register, value):
register.value = value
class Add(BaseCommand):
def execute_command(self, register, value):
self.interpreter.V[0xf].value = int(register.value + value > 0xff)
register.value += value
class Or(BaseCommand):
def execute_command(self, register, value):
register.value = register.value | value
class And(BaseCommand):
def execute_command(self, register, value):
register.value = register.value & value
class Xor(BaseCommand):
def execute_command(self, register, value):
register.value = register.value ^ value
class Sub(BaseCommand):
def execute_command(self, register, value):
self.interpreter.V[0xf].value = int(register.value > value)
register.value = register.value - value
class SubN(BaseCommand):
def execute_command(self, register, value):
self.interpreter.V[0xf].value = int(value > register.value)
register.value = value - register.value
class ShiftRight(BaseCommand):
def execute_command(self, register, value):
self.interpreter.V[0xf].value = register.value & 1
register.value = register.value >> 1
class ShiftLeft(BaseCommand):
def execute_command(self, register, value):
self.interpreter.V[0xf].value = int(register.value & 0x80 != 0)
register.value = register.value << 1
class Random(BaseCommand):
def execute_command(self, register, value):
rand = random.randint(0, 255)
register.value = rand & value
class Draw(BaseCommand):
def _get_bits(self, number):
bits = list(map(int, bin(number)[2:]))
bits = [0]*(8 - len(bits)) + bits
return tuple(bits)
def execute_command(self, x, y, lines):
self.interpreter.need_redraw = True
self.interpreter.V[0xf].value = 0
for line in range(lines):
memory_data = self.interpreter.memory\
.get_value(self.interpreter.I.value + line)
bits = self._get_bits(memory_data)
for bit_number, bit in enumerate(bits):
pixel_pos = Point(x + bit_number, y + line)
current_bit = self.interpreter.display.get_pixel(pixel_pos)
if bit == 1 and current_bit == 1:
self.interpreter.V[0xf].value = 1
self.interpreter.display.set_pixel(pixel_pos,
bit ^ current_bit)
class CheckPushed(BaseCommand):
def execute_command(self, value):
key_code = self.interpreter.get_key_code()
if key_code == value:
self.interpreter.instruction_pointer += 2
class CheckNotPushed(BaseCommand):
def execute_command(self, value):
key_code = self.interpreter.get_key_code()
if key_code != value:
self.interpreter.instruction_pointer += 2
class SetDelayTimer(BaseCommand):
def execute_command(self, value):
self.interpreter.delay_timer.ticks = value
class WaitPushing(BaseCommand):
def execute_command(self, register):
key_code = self.interpreter.get_key_code()
if key_code is None:
self.interpreter.instruction_pointer -= 2
else:
register.value = key_code
class SetSoundTimer(BaseCommand):
def execute_command(self, value):
self.interpreter.sound_timer.ticks = value
class SetSymbolLocation(BaseCommand):
def execute_command(self, character_code):
self.interpreter.I.value = character_code * 5
class StoreDecimalToMemory(BaseCommand):
def _get_decimal_representation(self, value):
decimal = list(map(int, str(value)))
return tuple([0] * (3 - len(decimal)) + decimal)
def execute_command(self, value):
decimal = self._get_decimal_representation(value)
for i, number in enumerate(decimal):
self.interpreter.memory\
.store_value(self.interpreter.I.value + i, number)
class StoreRegisters(BaseCommand):
def execute_command(self, register_number):
for i in range(register_number + 1):
value = self.interpreter.V[i].value
self.interpreter.memory\
.store_value(self.interpreter.I.value + i, value)
class LoadRegisters(BaseCommand):
def execute_command(self, register_number):
for i in range(register_number + 1):
value = self.interpreter.memory\
.get_value(self.interpreter.I.value + i)
self.interpreter.V[i].value = value