Skip to content

Commit ddaba6b

Browse files
authored
Add store subset of register operation. (#36)
1 parent e49245f commit ddaba6b

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

chip8/cpu.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ def misc_routines(self):
255255
Will execute one of the routines specified in misc_routines.
256256
"""
257257
operation = self.operand & 0x00FF
258-
try:
259-
self.misc_routine_lookup[operation]()
260-
except KeyError:
261-
raise UnknownOpCodeException(self.operand)
258+
sub_operation = self.operand & 0x000F
259+
if sub_operation == 0x2:
260+
self.store_subset_regs_in_memory()
261+
else:
262+
try:
263+
self.misc_routine_lookup[operation]()
264+
except KeyError:
265+
raise UnknownOpCodeException(self.operand)
262266

263267
def clear_return(self):
264268
"""
@@ -837,6 +841,32 @@ def set_bitplane(self):
837841
self.bitplane = (self.operand & 0x0F00) >> 8
838842
self.last_op = f"BITPLANE {self.bitplane:01X}"
839843

844+
def store_subset_regs_in_memory(self):
845+
"""
846+
Fxy2 - STORSUB [I], Vx, Vy
847+
848+
Store a subset of registers from x to y in memory starting at index.
849+
The x and y calculation is as follows:
850+
851+
Bits: 15-12 11-8 7-4 3-0
852+
F x y 2
853+
854+
If x is larger than y, then they will be stored in reverse order.
855+
"""
856+
x = (self.operand & 0x0F00) >> 8
857+
y = (self.operand & 0x00F0) >> 4
858+
pointer = 0
859+
if y >= x:
860+
for z in range(x, y+1):
861+
self.memory[self.index + pointer] = self.v[z]
862+
pointer += 1
863+
else:
864+
for z in range(x, y-1, -1):
865+
self.memory[self.index + pointer] = self.v[z]
866+
pointer += 1
867+
868+
self.last_op = f"STORSUB [I], {x:01X}, {y:01X}"
869+
840870
def move_delay_timer_into_reg(self):
841871
"""
842872
Fx07 - LOAD Vx, DELAY

test/test_chip8cpu.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,47 @@ def test_wait_for_keypress_sets_awaiting_keypress(self):
914914
self.assertEqual(1, self.cpu.keypress_register)
915915
self.assertTrue(self.cpu.awaiting_keypress)
916916

917+
def test_store_subset_regs_one_two(self):
918+
self.cpu.v[1] = 5
919+
self.cpu.v[2] = 6
920+
self.cpu.index = 0x5000
921+
self.cpu.operand = 0xF122
922+
self.cpu.store_subset_regs_in_memory()
923+
self.assertEqual(5, self.cpu.memory[0x5000])
924+
self.assertEqual(6, self.cpu.memory[0x5001])
925+
926+
def test_store_subset_regs_one_one(self):
927+
self.cpu.v[1] = 5
928+
self.cpu.v[2] = 6
929+
self.cpu.index = 0x5000
930+
self.cpu.operand = 0xF112
931+
self.cpu.store_subset_regs_in_memory()
932+
self.assertEqual(5, self.cpu.memory[0x5000])
933+
self.assertEqual(0, self.cpu.memory[0x5001])
934+
935+
def test_store_subset_regs_three_one(self):
936+
self.cpu.v[1] = 5
937+
self.cpu.v[2] = 6
938+
self.cpu.v[3] = 7
939+
self.cpu.index = 0x5000
940+
self.cpu.operand = 0xF312
941+
self.cpu.store_subset_regs_in_memory()
942+
self.assertEqual(7, self.cpu.memory[0x5000])
943+
self.assertEqual(6, self.cpu.memory[0x5001])
944+
self.assertEqual(5, self.cpu.memory[0x5002])
945+
946+
def test_store_subset_regs_integration(self):
947+
self.cpu.v[1] = 5
948+
self.cpu.v[2] = 6
949+
self.cpu.v[3] = 7
950+
self.cpu.index = 0x5000
951+
self.cpu.memory[0x0200] = 0xF3
952+
self.cpu.memory[0x0201] = 0x12
953+
self.cpu.execute_instruction()
954+
self.assertEqual(7, self.cpu.memory[0x5000])
955+
self.assertEqual(6, self.cpu.memory[0x5001])
956+
self.assertEqual(5, self.cpu.memory[0x5002])
957+
917958
# M A I N #####################################################################
918959

919960

0 commit comments

Comments
 (0)