From f9efe23b0b723ebcb25c9aee4b31c13427b4705b Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Tue, 20 Aug 2024 23:36:04 -0400 Subject: [PATCH] PPU: background rendering --- Sources/CPU/CPU.swift | 5 +++-- Sources/CPU/Trace.swift | 2 +- Sources/Data/Bus.swift | 2 +- Sources/PPU/NesPPU.swift | 10 ++++++++-- Sources/PPU/Registers/ControlRegister.swift | 4 ++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Sources/CPU/CPU.swift b/Sources/CPU/CPU.swift index fec359f..8992687 100644 --- a/Sources/CPU/CPU.swift +++ b/Sources/CPU/CPU.swift @@ -157,9 +157,10 @@ class CPU { func run(onCycle: @escaping () -> (), onComplete: @escaping () -> ()) { let opcodes = OPCODES_MAP while true { - if let _nmi = bus.pollNMI() { - + if bus.pollNMI() != nil { + print(programCounter) interrupt(.NMI) + print(programCounter) } processOpcodes(onCycle: onCycle, opcodes: opcodes) { onComplete() diff --git a/Sources/CPU/Trace.swift b/Sources/CPU/Trace.swift index ed5f0ad..dd39b85 100644 --- a/Sources/CPU/Trace.swift +++ b/Sources/CPU/Trace.swift @@ -9,7 +9,7 @@ func dumpCpuState(_ cpu: CPU) -> String { { let (addr, _) = cpu.getAbsoluteAddress(opcode.mode, addr: cpu.programCounter + 1) - return (addr, cpu.memRead(addr)) + return (addr, 0)//cpu.memRead(addr)) }() } diff --git a/Sources/Data/Bus.swift b/Sources/Data/Bus.swift index 583acc4..d999c2c 100644 --- a/Sources/Data/Bus.swift +++ b/Sources/Data/Bus.swift @@ -32,7 +32,7 @@ class Bus { } func pollNMI() -> UInt8? { - ppu.nmiInterrupt + ppu.pollNMI() } } diff --git a/Sources/PPU/NesPPU.swift b/Sources/PPU/NesPPU.swift index 5672871..0c17616 100644 --- a/Sources/PPU/NesPPU.swift +++ b/Sources/PPU/NesPPU.swift @@ -18,7 +18,7 @@ class NesPPU { var scanline: UInt16 = 0 var cycles: Int = 0 - var nmiInterrupt: UInt8? + var nmiInterrupt: UInt8? = nil init(_ chrRom: [UInt8], _ mirroring: Mirroring) { self.chrRom = chrRom @@ -52,6 +52,12 @@ class NesPPU { return false } + func pollNMI() -> UInt8? { + let tmp = self.nmiInterrupt + self.nmiInterrupt = nil + return tmp + } + func writeToPPUAddr(_ value: UInt8) { addr.update(value) } @@ -110,7 +116,7 @@ class NesPPU { func writeToData(_ data: UInt8) { let addr = addr.get() - //print("\(addr): \(data)") + print("\(addr): \(data)") switch addr { case 0...0x1fff: print("Attempt to write to chr rom space \(addr)!") diff --git a/Sources/PPU/Registers/ControlRegister.swift b/Sources/PPU/Registers/ControlRegister.swift index 48e236b..0010757 100644 --- a/Sources/PPU/Registers/ControlRegister.swift +++ b/Sources/PPU/Registers/ControlRegister.swift @@ -28,7 +28,7 @@ struct ControlRegister: OptionSet { static let GENERATE_NMI = ControlRegister(rawValue: 0b10000000) func vramAddrIncrement() -> UInt8 { - if self.contains(.VRAM_ADD_INCREMENT) { + if !self.contains(.VRAM_ADD_INCREMENT) { 1 } else { 32 @@ -40,7 +40,7 @@ struct ControlRegister: OptionSet { } func backgroundPatternAddr() -> Int { - if self.contains(.BACKROUND_PATTERN_ADDR) { + if !self.contains(.BACKROUND_PATTERN_ADDR) { 0 } else { 0x1000