Skip to content

Commit cc34898

Browse files
authored
Additional unit tests for new code (#45)
* Unit tests for drawing commands. * Fix sprite drawing in extended mode.
1 parent b802ca0 commit cc34898

File tree

2 files changed

+380
-9
lines changed

2 files changed

+380
-9
lines changed

chip8/cpu.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,30 +867,34 @@ def draw_sprite(self):
867867

868868
if num_bytes == 0:
869869
if self.bitplane == 3:
870-
self.draw_extended(x_pos, y_pos, 1)
871-
self.draw_extended(x_pos, y_pos, 2)
870+
self.draw_extended(x_pos, y_pos, 1, index=self.index)
871+
self.draw_extended(x_pos, y_pos, 2, index=self.index + 32)
872872
else:
873873
self.draw_extended(x_pos, y_pos, self.bitplane)
874874
self.last_op = f"DRAWEX"
875875
else:
876876
if self.bitplane == 3:
877-
self.draw_normal(x_pos, y_pos, num_bytes, 1)
878-
self.draw_normal(x_pos, y_pos, num_bytes, 2)
877+
self.draw_normal(x_pos, y_pos, num_bytes, 1, index=self.index)
878+
self.draw_normal(x_pos, y_pos, num_bytes, 2, index=self.index + num_bytes)
879879
else:
880880
self.draw_normal(x_pos, y_pos, num_bytes, self.bitplane)
881881
self.last_op = f"DRAW V{x_source:01X}, V{y_source:01X}"
882882

883-
def draw_normal(self, x_pos, y_pos, num_bytes, bitplane):
883+
def draw_normal(self, x_pos, y_pos, num_bytes, bitplane, index=None):
884884
"""
885885
Draws a sprite on the screen while in NORMAL mode.
886886
887887
:param x_pos: the X position of the sprite
888888
:param y_pos: the Y position of the sprite
889889
:param num_bytes: the number of bytes to draw
890890
:param bitplane: the bitplane to draw to
891+
:param index: the memory index in memory where byte the pattern is stored
891892
"""
893+
if not index:
894+
index = self.index
895+
892896
for y_index in range(num_bytes):
893-
color_byte = self.memory[self.index + y_index]
897+
color_byte = self.memory[index + y_index]
894898
y_coord = y_pos + y_index
895899
if not self.clip_quirks or (self.clip_quirks and y_coord < self.screen.get_height()):
896900
y_coord = y_coord % self.screen.get_height()
@@ -906,7 +910,7 @@ def draw_normal(self, x_pos, y_pos, num_bytes, bitplane):
906910
mask = mask >> 1
907911
self.screen.update()
908912

909-
def draw_extended(self, x_pos, y_pos, bitplane):
913+
def draw_extended(self, x_pos, y_pos, bitplane, index=None):
910914
"""
911915
Draws a sprite on the screen while in EXTENDED mode. Sprites in this
912916
mode are assumed to be 16x16 pixels. This means that two bytes will
@@ -916,10 +920,14 @@ def draw_extended(self, x_pos, y_pos, bitplane):
916920
:param x_pos: the X position of the sprite
917921
:param y_pos: the Y position of the sprite
918922
:param bitplane: the bitplane to draw to
923+
:param index: the memory index in memory where byte the pattern is stored
919924
"""
925+
if not index:
926+
index = self.index
927+
920928
for y_index in range(16):
921929
for x_byte in range(2):
922-
color_byte = self.memory[self.index + (y_index * 2) + x_byte]
930+
color_byte = self.memory[index + (y_index * 2) + x_byte]
923931
y_coord = y_pos + y_index
924932
if y_coord < self.screen.get_height():
925933
y_coord = y_coord % self.screen.get_height()

0 commit comments

Comments
 (0)