PPU: impl NMI interrupt

This commit is contained in:
Andrew Glaze
2024-08-19 22:05:08 -04:00
parent 05927d8e91
commit 830bc5f65a
6 changed files with 180 additions and 38 deletions

View File

@@ -15,17 +15,53 @@ class NesPPU {
var oamAddr: UInt8 = 0
var oamData = [UInt8](repeating: 0, count: 64 * 4)
var scanline: UInt16 = 0
var cycles: Int = 0
var nmiInterrupt: UInt8?
init(_ chrRom: [UInt8], _ mirroring: Mirroring) {
self.chrRom = chrRom
self.mirroring = mirroring
}
func tick(_ cycles: UInt8) -> Bool {
self.cycles += Int(cycles)
if self.cycles >= 341 {
self.cycles = self.cycles - 341
scanline += 1
if scanline == 241 {
if self.ctrl.generateVblankNMI() {
self.status.setVblankStatus(true)
status.setSpriteZeroHit(false)
if ctrl.generateVblankNMI() {
nmiInterrupt = 1
}
}
}
if scanline >= 262 {
scanline = 0
nmiInterrupt = nil
status.setSpriteZeroHit(false)
self.status.resetVblankStatus()
return true
}
}
return false
}
func writeToPPUAddr(_ value: UInt8) {
addr.update(value)
}
func writeToCtrl(_ value: UInt8) {
let beforeNmiStatus = ctrl.generateVblankNMI()
ctrl.rawValue = value
if !beforeNmiStatus && ctrl.generateVblankNMI() && status.isInVblank() {
nmiInterrupt = 1
}
}
func incrememtVramAddr() {

View File

@@ -34,4 +34,8 @@ struct ControlRegister: OptionSet {
32
}
}
func generateVblankNMI() -> Bool {
self.contains(.GENERATE_NMI)
}
}