Skip to content

Commit

Permalink
Chapter 6 Technical Review Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davecom committed Dec 2, 2024
1 parent e1c2c22 commit 4c5730b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Chip8/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def draw_sprite(self, x: int, y: int, height: int):
old_bit = self.display_buffer[px, py] & 1
if new_bit & old_bit: # If both set, flip white -> black
flipped_black = True
# Chip 8 draws by XORing, which flips everything
# Chip 8 draws by XORing
new_pixel = new_bit ^ old_bit
self.display_buffer[px, py] = WHITE if new_pixel else BLACK
# set flipped flag for collision detection
Expand Down
61 changes: 23 additions & 38 deletions NESEmulator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,31 @@ def run(rom: ROM, name: str):
if (ppu.scanline == 241) and (ppu.cycle == 2) and ppu.generate_nmi:
cpu.trigger_NMI()
ticks += new_ticks
# Handle keyboard events as joypad changes

for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
match event.key:
case pygame.K_LEFT:
cpu.joypad1.left = True
case pygame.K_RIGHT:
cpu.joypad1.right = True
case pygame.K_UP:
cpu.joypad1.up = True
case pygame.K_DOWN:
cpu.joypad1.down = True
case pygame.K_x:
cpu.joypad1.a = True
case pygame.K_z:
cpu.joypad1.b = True
case pygame.K_s:
cpu.joypad1.start = True
case pygame.K_a:
cpu.joypad1.select = True
elif event.type == pygame.KEYUP:
match event.key:
case pygame.K_LEFT:
cpu.joypad1.left = False
case pygame.K_RIGHT:
cpu.joypad1.right = False
case pygame.K_UP:
cpu.joypad1.up = False
case pygame.K_DOWN:
cpu.joypad1.down = False
case pygame.K_x:
cpu.joypad1.a = False
case pygame.K_z:
cpu.joypad1.b = False
case pygame.K_s:
cpu.joypad1.start = False
case pygame.K_a:
cpu.joypad1.select = False
elif event.type == pygame.QUIT:
if event.type == pygame.QUIT:
sys.exit()
# Handle keyboard events as joypad changes
if event.type not in {pygame.KEYDOWN, pygame.KEYUP}:
continue
is_keydown = event.type == pygame.KEYDOWN
match event.key:
case pygame.K_LEFT:
cpu.joypad1.left = is_keydown
case pygame.K_RIGHT:
cpu.joypad1.right = is_keydown
case pygame.K_UP:
cpu.joypad1.up = is_keydown
case pygame.K_DOWN:
cpu.joypad1.down = is_keydown
case pygame.K_x:
cpu.joypad1.a = is_keydown
case pygame.K_z:
cpu.joypad1.b = is_keydown
case pygame.K_s:
cpu.joypad1.start = is_keydown
case pygame.K_a:
cpu.joypad1.select = is_keydown


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions NESEmulator/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@ def step(self):

if not self.jumped:
self.PC += instruction.length
elif instruction.type in [InstructionType.BCC, InstructionType.BCS,
elif instruction.type in {InstructionType.BCC, InstructionType.BCS,
InstructionType.BEQ, InstructionType.BMI,
InstructionType.BNE, InstructionType.BPL,
InstructionType.BVC, InstructionType.BVS]:
InstructionType.BVC, InstructionType.BVS}:
# branch instructions are +1 ticks if they succeeded
self.cpu_ticks += 1
self.cpu_ticks += instruction.ticks
Expand Down
4 changes: 2 additions & 2 deletions NESEmulator/ppu.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def read_register(self, address: int) -> int:
if address == 0x2002:
self.addr_write_latch = False
current = self.status
self.status &= 0b01111111 # clear vblank on read to $2002
self.status &= 0b01111111 # clear vblank on read to 0x2002
return current
elif address == 0x2004:
return self.spr[self.spr_address]
Expand All @@ -191,7 +191,7 @@ def read_register(self, address: int) -> int:
else:
value = self.read_memory(self.addr)
self.buffer2007 = self.read_memory(self.addr - 0x1000)
# every read to $2007 there is an increment
# every read to 0x2007 there is an increment
self.addr += self.address_increment
return value
else:
Expand Down
18 changes: 9 additions & 9 deletions tests/test_nesemulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_blargg_instr_test_v5_basics(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/01-basics.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -70,7 +70,7 @@ def test_blargg_instr_test_v5_implied(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/02-implied.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -84,7 +84,7 @@ def test_blargg_instr_test_v5_branches(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/10-branches.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -98,7 +98,7 @@ def test_blargg_instr_test_v5_stack(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/11-stack.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -112,7 +112,7 @@ def test_blargg_instr_test_v5_jmp_jsr(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/12-jmp_jsr.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -126,7 +126,7 @@ def test_blargg_instr_test_v5_rts(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/13-rts.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -140,7 +140,7 @@ def test_blargg_instr_test_v5_rti(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/14-rti.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -154,7 +154,7 @@ def test_blargg_instr_test_v5_brk(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/15-brk.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand All @@ -168,7 +168,7 @@ def test_blargg_instr_test_v5_special(self):
rom = ROM("../NESEmulator/Tests/instr_test-v5/rom_singles/16-special.nes")
ppu = PPU(rom)
cpu = CPU(ppu, rom)
# Tests run as long as $6000 is 80, and then $6000 is result code; 0 means success
# Tests run as long as 0x6000 is 80, and then 0x6000 is result code; 0 means success
rom.prg_ram[0] = 0x80
while rom.prg_ram[0] == 0x80: # go until first unofficial opcode test
cpu.step()
Expand Down

0 comments on commit 4c5730b

Please sign in to comment.