Skip to content

Commit 4c5730b

Browse files
committed
Chapter 6 Technical Review Changes
1 parent e1c2c22 commit 4c5730b

File tree

5 files changed

+37
-52
lines changed

5 files changed

+37
-52
lines changed

Chip8/vm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def draw_sprite(self, x: int, y: int, height: int):
115115
old_bit = self.display_buffer[px, py] & 1
116116
if new_bit & old_bit: # If both set, flip white -> black
117117
flipped_black = True
118-
# Chip 8 draws by XORing, which flips everything
118+
# Chip 8 draws by XORing
119119
new_pixel = new_bit ^ old_bit
120120
self.display_buffer[px, py] = WHITE if new_pixel else BLACK
121121
# set flipped flag for collision detection

NESEmulator/__main__.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,31 @@ def run(rom: ROM, name: str):
4848
if (ppu.scanline == 241) and (ppu.cycle == 2) and ppu.generate_nmi:
4949
cpu.trigger_NMI()
5050
ticks += new_ticks
51-
# Handle keyboard events as joypad changes
51+
5252
for event in pygame.event.get():
53-
if event.type == pygame.KEYDOWN:
54-
match event.key:
55-
case pygame.K_LEFT:
56-
cpu.joypad1.left = True
57-
case pygame.K_RIGHT:
58-
cpu.joypad1.right = True
59-
case pygame.K_UP:
60-
cpu.joypad1.up = True
61-
case pygame.K_DOWN:
62-
cpu.joypad1.down = True
63-
case pygame.K_x:
64-
cpu.joypad1.a = True
65-
case pygame.K_z:
66-
cpu.joypad1.b = True
67-
case pygame.K_s:
68-
cpu.joypad1.start = True
69-
case pygame.K_a:
70-
cpu.joypad1.select = True
71-
elif event.type == pygame.KEYUP:
72-
match event.key:
73-
case pygame.K_LEFT:
74-
cpu.joypad1.left = False
75-
case pygame.K_RIGHT:
76-
cpu.joypad1.right = False
77-
case pygame.K_UP:
78-
cpu.joypad1.up = False
79-
case pygame.K_DOWN:
80-
cpu.joypad1.down = False
81-
case pygame.K_x:
82-
cpu.joypad1.a = False
83-
case pygame.K_z:
84-
cpu.joypad1.b = False
85-
case pygame.K_s:
86-
cpu.joypad1.start = False
87-
case pygame.K_a:
88-
cpu.joypad1.select = False
89-
elif event.type == pygame.QUIT:
53+
if event.type == pygame.QUIT:
9054
sys.exit()
55+
# Handle keyboard events as joypad changes
56+
if event.type not in {pygame.KEYDOWN, pygame.KEYUP}:
57+
continue
58+
is_keydown = event.type == pygame.KEYDOWN
59+
match event.key:
60+
case pygame.K_LEFT:
61+
cpu.joypad1.left = is_keydown
62+
case pygame.K_RIGHT:
63+
cpu.joypad1.right = is_keydown
64+
case pygame.K_UP:
65+
cpu.joypad1.up = is_keydown
66+
case pygame.K_DOWN:
67+
cpu.joypad1.down = is_keydown
68+
case pygame.K_x:
69+
cpu.joypad1.a = is_keydown
70+
case pygame.K_z:
71+
cpu.joypad1.b = is_keydown
72+
case pygame.K_s:
73+
cpu.joypad1.start = is_keydown
74+
case pygame.K_a:
75+
cpu.joypad1.select = is_keydown
9176

9277

9378
if __name__ == "__main__":

NESEmulator/cpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,10 @@ def step(self):
730730

731731
if not self.jumped:
732732
self.PC += instruction.length
733-
elif instruction.type in [InstructionType.BCC, InstructionType.BCS,
733+
elif instruction.type in {InstructionType.BCC, InstructionType.BCS,
734734
InstructionType.BEQ, InstructionType.BMI,
735735
InstructionType.BNE, InstructionType.BPL,
736-
InstructionType.BVC, InstructionType.BVS]:
736+
InstructionType.BVC, InstructionType.BVS}:
737737
# branch instructions are +1 ticks if they succeeded
738738
self.cpu_ticks += 1
739739
self.cpu_ticks += instruction.ticks

NESEmulator/ppu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def read_register(self, address: int) -> int:
180180
if address == 0x2002:
181181
self.addr_write_latch = False
182182
current = self.status
183-
self.status &= 0b01111111 # clear vblank on read to $2002
183+
self.status &= 0b01111111 # clear vblank on read to 0x2002
184184
return current
185185
elif address == 0x2004:
186186
return self.spr[self.spr_address]
@@ -191,7 +191,7 @@ def read_register(self, address: int) -> int:
191191
else:
192192
value = self.read_memory(self.addr)
193193
self.buffer2007 = self.read_memory(self.addr - 0x1000)
194-
# every read to $2007 there is an increment
194+
# every read to 0x2007 there is an increment
195195
self.addr += self.address_increment
196196
return value
197197
else:

tests/test_nesemulator.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_blargg_instr_test_v5_basics(self):
5656
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/01-basics.nes")
5757
ppu = PPU(rom)
5858
cpu = CPU(ppu, rom)
59-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
59+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
6060
rom.prg_ram[0] = 0x80
6161
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
6262
cpu.step()
@@ -70,7 +70,7 @@ def test_blargg_instr_test_v5_implied(self):
7070
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/02-implied.nes")
7171
ppu = PPU(rom)
7272
cpu = CPU(ppu, rom)
73-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
73+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
7474
rom.prg_ram[0] = 0x80
7575
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
7676
cpu.step()
@@ -84,7 +84,7 @@ def test_blargg_instr_test_v5_branches(self):
8484
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/10-branches.nes")
8585
ppu = PPU(rom)
8686
cpu = CPU(ppu, rom)
87-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
87+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
8888
rom.prg_ram[0] = 0x80
8989
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
9090
cpu.step()
@@ -98,7 +98,7 @@ def test_blargg_instr_test_v5_stack(self):
9898
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/11-stack.nes")
9999
ppu = PPU(rom)
100100
cpu = CPU(ppu, rom)
101-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
101+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
102102
rom.prg_ram[0] = 0x80
103103
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
104104
cpu.step()
@@ -112,7 +112,7 @@ def test_blargg_instr_test_v5_jmp_jsr(self):
112112
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/12-jmp_jsr.nes")
113113
ppu = PPU(rom)
114114
cpu = CPU(ppu, rom)
115-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
115+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
116116
rom.prg_ram[0] = 0x80
117117
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
118118
cpu.step()
@@ -126,7 +126,7 @@ def test_blargg_instr_test_v5_rts(self):
126126
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/13-rts.nes")
127127
ppu = PPU(rom)
128128
cpu = CPU(ppu, rom)
129-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
129+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
130130
rom.prg_ram[0] = 0x80
131131
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
132132
cpu.step()
@@ -140,7 +140,7 @@ def test_blargg_instr_test_v5_rti(self):
140140
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/14-rti.nes")
141141
ppu = PPU(rom)
142142
cpu = CPU(ppu, rom)
143-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
143+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
144144
rom.prg_ram[0] = 0x80
145145
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
146146
cpu.step()
@@ -154,7 +154,7 @@ def test_blargg_instr_test_v5_brk(self):
154154
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/15-brk.nes")
155155
ppu = PPU(rom)
156156
cpu = CPU(ppu, rom)
157-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
157+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
158158
rom.prg_ram[0] = 0x80
159159
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
160160
cpu.step()
@@ -168,7 +168,7 @@ def test_blargg_instr_test_v5_special(self):
168168
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/16-special.nes")
169169
ppu = PPU(rom)
170170
cpu = CPU(ppu, rom)
171-
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
171+
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
172172
rom.prg_ram[0] = 0x80
173173
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
174174
cpu.step()

0 commit comments

Comments
 (0)