@@ -81,7 +81,8 @@ def __init__(self, program_data: bytes):
8181 # ours can just be unlimited and expand/contract as needed
8282 self .stack = []
8383 # Graphics buffer for the screen - 64 x 32 pixels
84- self .display_buffer = np .zeros ((SCREEN_WIDTH , SCREEN_HEIGHT ), dtype = np .uint32 )
84+ self .display_buffer = np .zeros ((SCREEN_WIDTH , SCREEN_HEIGHT ),
85+ dtype = np .uint32 )
8586 self .needs_redraw = False
8687 # Timers - really simple registers that count down to 0 at 60 hertz
8788 self .delay_timer = 0
@@ -100,9 +101,9 @@ def decrement_timers(self):
100101 def play_sound (self ) -> bool :
101102 return self .sound_timer > 0
102103
103- # Draw a sprite at *x*, *y* using data at *i* and with a height of *height*
104+ # Draw a sprite at *x*, *y* using data at *i* with a height of *height*
104105 def draw_sprite (self , x : int , y : int , height : int ):
105- flipped_black = False # did drawing this flip any pixels from white to black ?
106+ flipped_black = False # did drawing this flip any pixels?
106107 for row in range (0 , height ):
107108 row_bits = self .ram [self .i + row ]
108109 for col in range (0 , SPRITE_WIDTH ):
@@ -112,11 +113,13 @@ def draw_sprite(self, x: int, y: int, height: int):
112113 continue # Ignore off-screen pixels
113114 new_bit = (row_bits >> (7 - col )) & 1
114115 old_bit = self .display_buffer [px , py ] & 1
115- if new_bit & old_bit : # If both are set, they will get flipped white -> black
116+ if new_bit & old_bit : # If both set, flip white -> black
116117 flipped_black = True
117- new_pixel = new_bit ^ old_bit # Chip 8 draws by XORing, which flips everything
118+ # Chip 8 draws by XORing, which flips everything
119+ new_pixel = new_bit ^ old_bit
118120 self .display_buffer [px , py ] = WHITE if new_pixel else BLACK
119- self .v [0xF ] = 1 if flipped_black else 0 # set flipped flag for collision detection
121+ # set flipped flag for collision detection
122+ self .v [0xF ] = 1 if flipped_black else 0
120123
121124 def step (self ):
122125 # we look at the opcode in terms of its nibbles (4 bit pieces)
@@ -139,7 +142,7 @@ def step(self):
139142 self .pc = self .stack .pop ()
140143 jumped = True
141144 case (0x0 , n1 , n2 , n3 ): # call program
142- self .pc = concat_nibbles (n1 , n2 , n3 ) # go to program start
145+ self .pc = concat_nibbles (n1 , n2 , n3 ) # go to start
143146 # clear registers
144147 self .delay_timer = 0
145148 self .sound_timer = 0
@@ -153,7 +156,7 @@ def step(self):
153156 self .pc = concat_nibbles (n1 , n2 , n3 )
154157 jumped = True
155158 case (0x2 , n1 , n2 , n3 ): # call subroutine
156- self .stack .append (self .pc + 2 ) # return location onto the stack
159+ self .stack .append (self .pc + 2 ) # put return place on stack
157160 self .pc = concat_nibbles (n1 , n2 , n3 ) # goto subroutine
158161 jumped = True
159162 case (0x3 , x , _, _): # conditional skip v[x] equal last2
@@ -261,7 +264,8 @@ def step(self):
261264 for r in range (0 , x + 1 ):
262265 self .v [r ] = self .ram [self .i + r ]
263266 case _:
264- print (f"Unknown opcode { (hex (first ), hex (second ), hex (third ), hex (fourth ))} !" )
267+ print (f"Unknown opcode { (hex (first ), hex (second ),
268+ hex (third ), hex (fourth ))} !" )
265269
266270 if not jumped :
267271 self .pc += 2 # increment program counter
0 commit comments