Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OMFG I FIXED SKY SHARK #85

Merged
merged 9 commits into from
Nov 14, 2024
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ So far, the following mappers and associated games have been tested in this emul
- Gyruss
- Tiger Heli
- 007
- Sky Shark (HUD is jumpy)
- Sky Shark
- Marble Madness (text box glitches a little at start of level)
- Wheel of Fortune

Expand Down
1 change: 1 addition & 0 deletions happiNESs/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum AddressBitMask: Address {
case coarseY = 0b0000_0011_1110_0000
case coarseX = 0b0000_0000_0001_1111

case bit14 = 0b0100_0000_0000_0000
case highByte = 0b0011_1111_0000_0000
case lowByte = 0b0000_0000_1111_1111

Expand Down
8 changes: 8 additions & 0 deletions happiNESs/PPU+IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ extension PPU {

mutating public func updateAddress(byte: UInt8) {
if !self.wRegister {
// ACHTUNG! Per the following excerpt in the NESDev wiki for PPUADDR, we
// need to _explicitly_ reset bit 14 here:
//
// "However, bit 14 of the internal t register that holds the data written
// to PPUADDR is forced to 0 when writing the PPUADDR high byte"
//
// https://www.nesdev.org/wiki/PPU_registers#PPUADDR_-_VRAM_address_($2006_write)
self.nextSharedAddress[.highByte] = byte
self.nextSharedAddress[.bit14] = 0
} else {
self.nextSharedAddress[.lowByte] = byte
self.currentSharedAddress = self.nextSharedAddress
Expand Down
8 changes: 4 additions & 4 deletions happiNESs/PPU+caching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ extension PPU {
}

private func getSpriteData(for oamIndex: Int) -> UInt32 {
let tileY = Int(self.oamRegister.data[oamIndex]) + 1
let tileY = Int(self.oamRegister.data[oamIndex])
let attributeByte = self.oamRegister.data[oamIndex + 2]

let flipVertical = ((attributeByte >> 7) & 1) == 1
Expand Down Expand Up @@ -285,7 +285,7 @@ extension PPU {
// ACHTUNG! Note that the value in OAM is one less than the actual Y value!
//
// https://www.nesdev.org/wiki/PPU_OAM#Byte_0
let tileY = Int(self.oamRegister.data[oamIndex]) + 1
let tileY = Int(self.oamRegister.data[oamIndex])

let spritePixelY = self.scanline - tileY
// The sprite height property takes into account whether or not all
Expand Down Expand Up @@ -328,7 +328,7 @@ extension PPU {
// Reset coarse X
self.currentSharedAddress[.coarseX] = 0b0_0000
// Toggle horizontal nametable
self.currentSharedAddress[.nametable] ^= 0b01
self.currentSharedAddress[.nametableX] ^= 0b1
} else {
// Just increment coarse X
self.currentSharedAddress[.coarseX] += 0b0_0001
Expand All @@ -344,7 +344,7 @@ extension PPU {
// Reset coarse Y
self.currentSharedAddress[.coarseY] = 0b0_0000
// Toggle vertical nametable
self.currentSharedAddress[.nametable] ^= 0b10
self.currentSharedAddress[.nametableY] ^= 0b1
} else if self.currentSharedAddress[.coarseY] == 0b1_1111 {
// ACHTUNG! How would we ever get to this branch?
//
Expand Down
8 changes: 8 additions & 0 deletions happiNESs/PPU+rendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ extension PPU {
}

private func getCurrentBackgroundTileColor() -> NESColor? {
if self.maskRegister[.showBackground] == false {
return nil
}

let tileData = self.currentTileData
let pixelData = tileData >> ((7 - self.currentFineX) * 4)
let colorIndex = Int(pixelData & 0x0F)
return colorIndex.isMultiple(of: 4) ? nil : NESColor.systemPalette[Int(self.paletteTable[colorIndex])]
}

private func getCurrentSpriteColor() -> (color: NESColor, index: Int, backgroundPriority: Bool)? {
if self.maskRegister[.showSprites] == false {
return nil
}

// Note that `currentSprites` is ordered from left to right by the OAM index,
// with the first (zeroth) element being the so-called sprite zero. Furthermore,
// the strategy here is to find the first sprite whose pixels intersect with the
Expand Down
11 changes: 7 additions & 4 deletions happiNESs/PPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ public struct PPU {
self.cycles >= 1 && self.cycles <= Self.width
}
var isIncrementVerticalScrollCycle: Bool {
self.cycles == Self.width + 1
self.cycles == Self.width
}
var isCopyHorizontalScrollCycle: Bool {
self.cycles == Self.width + 2
self.cycles == Self.width + 1
}
var isCacheSpritesCycle: Bool {
self.cycles == Self.width + 1
}
var isCopyVerticalScrollCycle: Bool {
self.cycles >= 280 && self.cycles <= 304
Expand Down Expand Up @@ -161,7 +164,7 @@ public struct PPU {
self.cacheBackgroundTiles()

// TODO: Revisit this because sprites should be cached for the _next_ line
if self.isVisibleLine && self.cycles == 0 {
if self.isVisibleLine && self.isCacheSpritesCycle {
self.cacheSprites()
}
}
Expand Down Expand Up @@ -239,7 +242,7 @@ public struct PPU {
self.handleNmiState()
self.handleRendering()
self.handleCaching()
redrawScreen = redrawScreen || self.handleVerticalBlank()
redrawScreen = self.handleVerticalBlank() || redrawScreen
self.handleFrameCounts()
}

Expand Down
1 change: 1 addition & 0 deletions happiNESs/Register.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension Register {
(self & (1 << flag.bitIndex)) > 0
}
set {
self &= ~(1 << flag.bitIndex)
self |= (newValue ? 1 : 0) << flag.bitIndex
}
}
Expand Down
2 changes: 1 addition & 1 deletion happiNESsApp/Speaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Speaker {
engine.attach(sourceNode)
engine.connect(sourceNode, to: mainMixer, format: inputFormat)
engine.connect(mainMixer, to: output, format: outputFormat)
mainMixer.outputVolume = 0.5
mainMixer.outputVolume = 1.0

try engine.start()
}
Expand Down