Skip to content

Commit

Permalink
Use io_registers directly from PPU (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
kremi151 committed Aug 14, 2020
1 parent b78d060 commit 5152e36
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
40 changes: 40 additions & 0 deletions core/source/emulator/io_registers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ using namespace FunkyBoy;
#define FB_REG_OFFSET_P1 (FB_REG_P1 - 0xFF00)
#define FB_REG_OFFSET_DIV (FB_REG_DIV - 0xFF00)
#define FB_REG_OFFSET_IF (FB_REG_IF - 0xFF00)
#define FB_REG_OFFSET_LCDC (FB_REG_LCDC - 0xFF00)
#define FB_REG_OFFSET_STAT (FB_REG_STAT - 0xFF00)
#define FB_REG_OFFSET_SCY (FB_REG_SCY - 0xFF00)
#define FB_REG_OFFSET_SCX (FB_REG_SCX - 0xFF00)
#define FB_REG_OFFSET_LY (FB_REG_LY - 0xFF00)
#define FB_REG_OFFSET_BGP (FB_REG_BGP - 0xFF00)
#define FB_REG_OFFSET_OBP0 (FB_REG_OBP0 - 0xFF00)
#define FB_REG_OFFSET_OBP1 (FB_REG_OBP1 - 0xFF00)
#define FB_REG_OFFSET_WY (FB_REG_WY - 0xFF00)
#define FB_REG_OFFSET_WX (FB_REG_WX - 0xFF00)

io_registers::io_registers(const io_registers &registers)
: sys_counter_lsb(registers.sys_counter_lsb)
Expand Down Expand Up @@ -160,4 +168,36 @@ u8 &io_registers::getP1() {

u8 &io_registers::getIF() {
return *(hwIO + FB_REG_OFFSET_IF);
}

u8 &io_registers::getLCDC() {
return *(hwIO + FB_REG_OFFSET_LCDC);
}

u8 &io_registers::getSCX() {
return *(hwIO + FB_REG_OFFSET_SCX);
}

u8 &io_registers::getSCY() {
return *(hwIO + FB_REG_OFFSET_SCY);
}

u8 &io_registers::getBGP() {
return *(hwIO + FB_REG_OFFSET_BGP);
}

u8 &io_registers::getOBP0() {
return *(hwIO + FB_REG_OFFSET_OBP0);
}

u8 &io_registers::getOBP1() {
return *(hwIO + FB_REG_OFFSET_OBP1);
}

u8 &io_registers::getWX() {
return *(hwIO + FB_REG_OFFSET_WX);
}

u8 &io_registers::getWY() {
return *(hwIO + FB_REG_OFFSET_WY);
}
8 changes: 8 additions & 0 deletions core/source/emulator/io_registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ namespace FunkyBoy {

u8 &getP1();
u8 &getIF();
u8 &getLCDC();
u8 &getSCX();
u8 &getSCY();
u8 &getBGP();
u8 &getOBP0();
u8 &getOBP1();
u8 &getWY();
u8 &getWX();

friend class CPU;
};
Expand Down
18 changes: 9 additions & 9 deletions core/source/emulator/ppu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ ret_code PPU::doClocks(u8 clocks) {
}

void PPU::renderScanline(u8 ly) {
const u8 lcdc = memory->read8BitsAt(FB_REG_LCDC);
const u8 &lcdc = ioRegisters.getLCDC();
const memory_address tileSetAddr = __fb_lcdc_bgAndWindowTileDataSelect(lcdc);
const bool bgEnabled = __fb_lcdc_isBGEnabled(lcdc);
const bool objEnabled = __fb_lcdc_objEnabled(lcdc);
Expand All @@ -144,15 +144,15 @@ void PPU::renderScanline(u8 ly) {
u8 palette;
u8 colorIndex;
if (bgEnabled) {
const u8 scx = memory->read8BitsAt(FB_REG_SCX);
const u8 scy = memory->read8BitsAt(FB_REG_SCY);
const u8 &scx = ioRegisters.getSCX();
const u8 &scy = ioRegisters.getSCY();
y = ly + scy;
tileOffsetX = scx / 8;
xInTile = scx % 8;
yInTile = y % 8;
memory_address tileMapAddr = __fb_lcdc_bgTileMapDisplaySelect(lcdc);
tileMapAddr += ((y & 255u) / 8) * 32;
palette = memory->read8BitsAt(FB_REG_BGP);
palette = ioRegisters.getBGP();
tile = memory->read8BitsAt(tileMapAddr + tileOffsetX);
for (u8 scanLineX = 0 ; scanLineX < FB_GB_DISPLAY_WIDTH ; scanLineX++) {
tileLine = memory->read16BitsAt(tileSetAddr + (tile * 16) + (yInTile * 2));
Expand All @@ -167,8 +167,8 @@ void PPU::renderScanline(u8 ly) {
}
}
if (objEnabled) {
const u8 objPalette0 = memory->read8BitsAt(FB_REG_OBP0);
const u8 objPalette1 = memory->read8BitsAt(FB_REG_OBP1);
const u8 objPalette0 = ioRegisters.getOBP0();
const u8 objPalette1 = ioRegisters.getOBP1();
const u8 objHeight = __fb_lcdc_objSpriteSize(lcdc);
memory_address objAddr = 0xFE00;
u8 objY, objX, objFlags;
Expand Down Expand Up @@ -219,16 +219,16 @@ void PPU::renderScanline(u8 ly) {
}
}
if (windowEnabled) {
u8 wy = memory->read8BitsAt(FB_REG_WY);
const u8 wy = ioRegisters.getWY();
if (ly >= wy) {
y = ly - wy;
yInTile = y % 8;
xInTile = 0;
tileOffsetX = 0;
u8 wx = memory->read8BitsAt(FB_REG_WX) - 7;
const u8 wx = ioRegisters.getWX() - 7;
memory_address tileMapAddr = __fb_lcdc_windowTileMapDisplaySelect(lcdc);
tileMapAddr += ((y & 255u) / 8) * 32;
palette = memory->read8BitsAt(FB_REG_BGP);
palette = ioRegisters.getBGP();
tile = memory->read8BitsAt(tileMapAddr + tileOffsetX);
for (u8 scanLineX = wx ; scanLineX < FB_GB_DISPLAY_WIDTH ; scanLineX++) {
tileLine = memory->read16BitsAt(tileSetAddr + (tile * 16) + (yInTile * 2));
Expand Down

0 comments on commit 5152e36

Please sign in to comment.