diff --git a/bsnes/sfc/ppu/background.cpp b/bsnes/sfc/ppu/background.cpp index e215fb79..4043132a 100644 --- a/bsnes/sfc/ppu/background.cpp +++ b/bsnes/sfc/ppu/background.cpp @@ -150,6 +150,42 @@ auto PPU::Background::getTile() -> void { } } +auto PPU::Background::getTile(uint x, uint y) -> uint16 { + uint tileHeight = 3 + io.tileSize; + uint tileWidth = !hires() ? tileHeight : 4; + uint screenX = io.screenSize.bit(0) ? 32 << 5 : 0; + uint screenY = io.screenSize.bit(1) ? 32 << 5 + io.screenSize.bit(0) : 0; + uint tileX = x >> tileWidth; + uint tileY = y >> tileHeight; + uint16 offset = (tileY & 0x1f) << 5 | (tileX & 0x1f); + if(tileX & 0x20) offset += screenX; + if(tileY & 0x20) offset += screenY; + uint16 address = latch.screenAddress + offset; + return ppu.vram[address]; +} + +auto PPU::Background::getTileColor() -> uint { + uint color = 0; + + switch(io.mode) { + case Mode::BPP8: + color += data[1] >> 24 & 0x80; + color += data[1] >> 17 & 0x40; + color += data[1] >> 10 & 0x20; + color += data[1] >> 3 & 0x10; + data[1] <<= 1; + case Mode::BPP4: + color += data[0] >> 28 & 0x08; + color += data[0] >> 21 & 0x04; + case Mode::BPP2: + color += data[0] >> 14 & 0x02; + color += data[0] >> 7 & 0x01; + data[0] <<= 1; + } + + return color; +} + auto PPU::Background::run(bool screen) -> void { if(ppu.vcounter() == 0) return; @@ -188,42 +224,6 @@ auto PPU::Background::run(bool screen) -> void { if(!hires() || screen == Screen::Below) if(io.belowEnable) output.below = pixel; } -auto PPU::Background::getTile(uint x, uint y) -> uint16 { - uint tileHeight = 3 + io.tileSize; - uint tileWidth = !hires() ? tileHeight : 4; - uint screenX = io.screenSize.bit(0) ? 32 << 5 : 0; - uint screenY = io.screenSize.bit(1) ? 32 << 5 + io.screenSize.bit(0) : 0; - uint tileX = x >> tileWidth; - uint tileY = y >> tileHeight; - uint16 offset = (tileY & 0x1f) << 5 | (tileX & 0x1f); - if(tileX & 0x20) offset += screenX; - if(tileY & 0x20) offset += screenY; - uint16 address = latch.screenAddress + offset; - return ppu.vram[address]; -} - -auto PPU::Background::getTileColor() -> uint { - uint color = 0; - - switch(io.mode) { - case Mode::BPP8: - color += data[1] >> 24 & 0x80; - color += data[1] >> 17 & 0x40; - color += data[1] >> 10 & 0x20; - color += data[1] >> 3 & 0x10; - data[1] <<= 1; - case Mode::BPP4: - color += data[0] >> 28 & 0x08; - color += data[0] >> 21 & 0x04; - case Mode::BPP2: - color += data[0] >> 14 & 0x02; - color += data[0] >> 7 & 0x01; - data[0] <<= 1; - } - - return color; -} - auto PPU::Background::power() -> void { io = {}; io.tiledataAddress = (random() & 0x0f) << 12; diff --git a/bsnes/sfc/ppu/background.hpp b/bsnes/sfc/ppu/background.hpp index efda7d4b..2869c847 100644 --- a/bsnes/sfc/ppu/background.hpp +++ b/bsnes/sfc/ppu/background.hpp @@ -14,6 +14,7 @@ struct Background { auto getTile() -> void; auto getTile(uint x, uint y) -> uint16; auto getTileColor() -> uint; + alwaysinline auto clip(int n) -> int; auto beginMode7() -> void; auto runMode7() -> void; diff --git a/bsnes/sfc/ppu/ppu.cpp b/bsnes/sfc/ppu/ppu.cpp index c3b41ecc..63c9b3b6 100644 --- a/bsnes/sfc/ppu/ppu.cpp +++ b/bsnes/sfc/ppu/ppu.cpp @@ -61,23 +61,23 @@ auto PPU::Enter() -> void { auto PPU::main() -> void { scanline(); step(28); - bg1.begin(); - bg2.begin(); - bg3.begin(); bg4.begin(); + bg3.begin(); + bg2.begin(); + bg1.begin(); if(vcounter() < vdisp()) { for(int pixel = -7; pixel <= 255; pixel++) { - bg1.run(1); - bg2.run(1); - bg3.run(1); bg4.run(1); + bg3.run(1); + bg2.run(1); + bg1.run(1); step(2); - bg1.run(0); - bg2.run(0); - bg3.run(0); bg4.run(0); + bg3.run(0); + bg2.run(0); + bg1.run(0); if(pixel >= 0) { obj.run(); window.run();