From 7a4c5afce486ca3f039c296c888083831b62f25c Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Mon, 28 Sep 2020 21:41:56 -0400 Subject: [PATCH] GBHawk: fix x-scroll latch and add some notes --- .../CPUs/LR35902/Execute.cs | 2 +- .../Consoles/Nintendo/GBHawk/GBC_GB_PPU.cs | 25 +++-------- .../Consoles/Nintendo/GBHawk/GBC_PPU.cs | 45 +++++++++---------- .../Consoles/Nintendo/GBHawk/GBHawk.cs | 2 + .../Consoles/Nintendo/GBHawk/GB_PPU.cs | 27 ++++------- .../Consoles/Nintendo/GBHawk/HW_Registers.cs | 1 + 6 files changed, 39 insertions(+), 63 deletions(-) diff --git a/src/BizHawk.Emulation.Cores/CPUs/LR35902/Execute.cs b/src/BizHawk.Emulation.Cores/CPUs/LR35902/Execute.cs index 32b68d8323..544bf124a0 100644 --- a/src/BizHawk.Emulation.Cores/CPUs/LR35902/Execute.cs +++ b/src/BizHawk.Emulation.Cores/CPUs/LR35902/Execute.cs @@ -5,7 +5,7 @@ namespace BizHawk.Emulation.Cores.Components.LR35902 public ulong TotalExecutedCycles; private int EI_pending; - private bool interrupts_enabled; + public bool interrupts_enabled; // variables for executing instructions public int instr_pntr = 0; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_GB_PPU.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_GB_PPU.cs index 662c00f84a..a9c80825a1 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_GB_PPU.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_GB_PPU.cs @@ -163,9 +163,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk scroll_x = value; break; case 0xFF44: // LY - // writing to LY has no effect on GBC (zen intergalactic ninja does this on initialization) - //LY = 0; /*reset*/ - //LY_read = 0; + // writing to LY has no effect, confirmed by gambatte test roms break; case 0xFF45: // LYC // tests indicate that latching writes to LYC should take place 4 cycles after the write @@ -597,15 +595,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } else if (!rendering_complete) { - if (cycle == 85) - { - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; - } - // render the screen and handle hblank render(cycle - 85); } @@ -658,12 +647,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk OAM_access_write = false; VRAM_access_read = false; VRAM_access_write = false; - - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; } // render the screen and handle hblank @@ -1119,7 +1102,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { // here we set up rendering pre_render = false; - + + // x scroll is latched here + x_tile = scroll_x >> 3; + render_offset = scroll_offset = scroll_x % 8; + render_counter = 0; latch_counter = 0; read_case = 0; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_PPU.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_PPU.cs index 739bbd1acd..f3cf3e619d 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_PPU.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBC_PPU.cs @@ -159,9 +159,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk scroll_x = value; break; case 0xFF44: // LY - // writing to LY has no effect on GBC (zen intergalactic ninja does this on initialization) - //LY = 0; /*reset*/ - //LY_read = 0; + // writing to LY has no effect, confirmed by gambatte test roms break; case 0xFF45: // LYC // tests indicate that latching writes to LYC should take place 4 cycles after the write @@ -587,15 +585,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } else if (!rendering_complete) { - if (cycle == 85) - { - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; - } - // render the screen and handle hblank render(cycle - 85); } @@ -649,12 +638,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk OAM_access_write = false; VRAM_access_write = false; VRAM_access_read = false; - - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; } // render the screen and handle hblank @@ -1074,7 +1057,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { // here we set up rendering pre_render = false; - + + // calculate the column number of the tile to start with + x_tile = scroll_x >> 3; + render_offset = scroll_offset = scroll_x % 8; + render_counter = 0; latch_counter = 0; read_case = 0; @@ -1238,14 +1225,24 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk VRAM_access_read = true; VRAM_access_write = true; read_case = 18; - + + if (Core.double_speed) + { + STAT &= 0xFC; + STAT |= 0x00; + if (STAT.Bit(3)) { HBL_INT = true; } + // the CPU has to be able to see the transition from mode 3 to mode 0 to start HDMA + } } else { - STAT &= 0xFC; - STAT |= 0x00; - if (STAT.Bit(3)) { HBL_INT = true; } - // the CPU has to be able to see the transition from mode 3 to mode 0 to start HDMA + if (!Core.double_speed) + { + STAT &= 0xFC; + STAT |= 0x00; + if (STAT.Bit(3)) { HBL_INT = true; } + // the CPU has to be able to see the transition from mode 3 to mode 0 to start HDMA + } } } break; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs index 2e090ff0e3..7489d4110d 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.cs @@ -8,6 +8,8 @@ using BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy; using System.Runtime.InteropServices; // TODO: mode1_disableint_gbc.gbc behaves differently between GBC and GBA, why? +// TODO: oam_dma_start.gb does not behave as expected but test still passes through lucky coincidences / test deficiency +// TODO: LYC interrupt behaves differently in GBC and GB compat mode namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GB_PPU.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GB_PPU.cs index 507203a40d..dc13349b27 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GB_PPU.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GB_PPU.cs @@ -54,6 +54,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk case 0xFF41: // STAT // writing to STAT during mode 0 or 1 causes a STAT IRQ // this appears to be a glitchy LYC compare + if (!value.Bit(6)) { LYC_INT = false; } + if (LCDC.Bit(7)) { if (((STAT & 3) == 0) || ((STAT & 3) == 1)) @@ -73,7 +75,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } STAT = (byte)((value & 0xF8) | (STAT & 7) | 0x80); - //if (!STAT.Bit(6)) { LYC_INT = false; } if (!STAT.Bit(4)) { VBL_INT = false; } break; case 0xFF42: // SCY @@ -83,8 +84,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk scroll_x = value; break; case 0xFF44: // LY - // writing to LY has no effect on GBC, not sure about GB (zen intergalactic ninja does this on initialization) - LY = 0; /*reset*/ + // writing to LY has no effect, confirmed by gambatte test roms break; case 0xFF45: // LYC LYC = value; @@ -298,15 +298,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } else if (!rendering_complete) { - if (cycle == 85) - { - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; - } - // render the screen and handle hblank render(cycle - 85); } @@ -361,12 +352,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk OAM_INT = false; OAM_access_write = false; VRAM_access_write = false; - - // x-scroll is expected to be latched one cycle later - // this is fine since nothing has started in the rendering until the second cycle - // calculate the column number of the tile to start with - x_tile = scroll_x >> 3; - render_offset = scroll_offset = scroll_x % 8; } // render the screen and handle hblank @@ -733,7 +718,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { // here we set up rendering pre_render = false; - + + // x scroll is latched here + x_tile = scroll_x >> 3; + render_offset = scroll_offset = scroll_x % 8; + render_counter = 0; latch_counter = 0; read_case = 0; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/HW_Registers.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/HW_Registers.cs index 001fc0ee25..328fd3006f 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/HW_Registers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/HW_Registers.cs @@ -37,6 +37,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk // Interrupt flags case 0xFF0F: + // TODO: Maybe some PPU bits are immediately visible, see 10spritesPrLine_10xposA7_m0irq_2_dmg08_cgb04c_out2.gbc ret = REG_FF0F_OLD; break;