From 874d06797f562af48293c546a97c0183c9ecda8d Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Sat, 2 Sep 2017 10:48:23 -0400 Subject: [PATCH] NESHawk: Code Clean up remove redundancies, fix scanline count --- .../Consoles/Nintendo/NES/NES.Core.cs | 1 - .../Consoles/Nintendo/NES/PPU.cs | 8 +- .../Consoles/Nintendo/NES/PPU.regs.cs | 8 +- .../Consoles/Nintendo/NES/PPU.run.cs | 86 +++++++++---------- 4 files changed, 49 insertions(+), 54 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs index 1775dcd523..bea02f1504 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.Core.cs @@ -367,7 +367,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } FrameGo = true; - ppu.ppu_tick_counter = 0; if (ppu.ppudead > 0) { diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs index 14a40e2d5e..1a23d7432d 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.cs @@ -135,7 +135,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //when the ppu issues a write it goes through here and into the game board public void ppubus_write(int addr, byte value) { - if (ppur.status.sl == 241 || !PPUON) + if (ppur.status.sl >= 241 || !PPUON) nes.Board.AddressPPU(addr); nes.Board.WritePPU(addr, value); @@ -257,9 +257,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ser.Sync("do_vbl", ref do_vbl); ser.Sync("do_active_sl", ref do_active_sl); ser.Sync("do_pre_vbl", ref do_pre_vbl); - ser.Sync("ppu_tick_counter", ref ppu_tick_counter); - ser.Sync("scanline_counter", ref scanline_counter); ser.Sync("nmi_destiny", ref nmi_destiny); ser.Sync("yp_shift", ref yp_shift); ser.Sync("sprite_eval_cycle", ref sprite_eval_cycle); @@ -326,7 +324,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //but maybe thats just because a ppu read/write shoves it on the address bus //apparently this shoves it on the address bus, too, or else blargg's mmc3 tests dont pass //ONLY if the ppu is not rendering - if (ppur.status.sl == 241 || !PPUON) + if (ppur.status.sl >= 241 || !PPUON) nes.Board.AddressPPU(ppur.get_2007access()); race_2006 = true; @@ -346,7 +344,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ppur.status.cycle++; is_even_cycle = !is_even_cycle; - if (PPUON && ppur.status.cycle >= 257 && ppur.status.cycle <= 320 && 0 <= ppur.status.sl && ppur.status.sl <= 240) + if (PPUON && ppur.status.cycle >= 257 && ppur.status.cycle <= 320 && ppur.status.sl <= 240) { reg_2003 = 0; } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs index 332e4ab220..28b54729da 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.regs.cs @@ -148,7 +148,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES fh = 0; _fv = _v = _h = _vt = _ht = 0; status.cycle = 0; - status.sl = 241; + status.sl = 0; } public void install_latches() @@ -433,7 +433,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //otherwise we just write this value and move on to the next oam byte value &= 0xE3; } - if (0 <= ppur.status.sl && ppur.status.sl <= 240) + if (ppur.status.rendering) { // don't write to OAM if the screen is on and we are in the active display area // this impacts sprite evaluation @@ -587,7 +587,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ppur.increment2007(ppur.status.rendering && PPUON, reg_2000.vram_incr32 != 0); //see comments in $2006 - if (ppur.status.sl == 241 || !PPUON) + if (ppur.status.sl >= 241 || !PPUON) nes.Board.AddressPPU(ppur.get_2007access()); } byte read_2007() @@ -611,7 +611,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ppur.increment2007(ppur.status.rendering && PPUON, reg_2000.vram_incr32 != 0); //see comments in $2006 - if (ppur.status.sl == 241 || !PPUON) + if (ppur.status.sl >= 241 || !PPUON) nes.Board.AddressPPU(ppur.get_2007access()); // update open bus here diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs index 233aaec96b..402f9ef6a4 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/PPU.run.cs @@ -133,9 +133,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public bool do_vbl; public bool do_active_sl; public bool do_pre_vbl; - public int ppu_tick_counter; - int scanline_counter; bool nmi_destiny; int yp_shift; int sprite_eval_cycle; @@ -156,51 +154,52 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public void ppu_init_frame() { - scanline_counter = 0; - ppu_tick_counter = 0; + ppur.status.sl = 241 + preNMIlines; + ppur.status.cycle = 0; // These things happen at the start of every frame Reg2002_vblank_active_pending = true; ppuphase = PPUPHASE.VBL; - ppur.status.sl = 241; bgdata = new BGDataRecord[34]; } public void TickPPU_VBL() { - if (ppu_tick_counter == 3) + if (ppur.status.cycle == 3 && ppur.status.sl == 241 + preNMIlines) { nmi_destiny = reg_2000.vblank_nmi_gen && Reg2002_vblank_active; } - else if (ppu_tick_counter == 6) + else if (ppur.status.cycle == 6 && ppur.status.sl == 241 + preNMIlines) { if (nmi_destiny) { nes.cpu.NMI = true; } nes.Board.AtVsyncNMI(); } - runppu(); - ppu_tick_counter++; + runppu(); // note cycle ticks inside runppu - if (ppu_tick_counter == postNMIlines * kLineTime) + if (ppur.status.cycle == 341) { - Reg2002_objhit = Reg2002_objoverflow = 0; - Reg2002_vblank_clear_pending = true; - idleSynch ^= true; + ppur.status.cycle = 0; + ppur.status.sl++; + if (ppur.status.sl == 241 + preNMIlines + postNMIlines) + { + Reg2002_objhit = Reg2002_objoverflow = 0; + Reg2002_vblank_clear_pending = true; + idleSynch ^= true; - do_vbl = false; - ppu_tick_counter = 0; + do_vbl = false; + ppur.status.sl = 0; + } } } public void TickPPU_active() { - if (ppu_tick_counter == 0) + if (ppur.status.cycle == 0) { ppur.status.cycle = 0; - ppur.status.sl = scanline_counter; - spr_true_count = 0; soam_index = 0; soam_m_index = 0; @@ -213,12 +212,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES sprite_zero_in_range = false; - yp = scanline_counter - 1; + yp = ppur.status.sl - 1; ppuphase = PPUPHASE.BG; // "If PPUADDR is not less then 8 when rendering starts, the first 8 bytes in OAM are written to from // the current location of PPUADDR" - if (scanline_counter == 0 && PPUON && reg_2003 >= 8 && region == Region.NTSC) + if (ppur.status.sl == 0 && PPUON && reg_2003 >= 8 && region == Region.NTSC) { for (int i = 0; i < 8; i++) { @@ -248,9 +247,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } - if (ppu_tick_counter < 256) + if (ppur.status.cycle < 256) { - if (scanline_counter != 0) + if (ppur.status.sl != 0) { ///////////////////////////////////////////// // Sprite Evaluation End @@ -542,10 +541,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } } } - else if (ppu_tick_counter < 320) + else if (ppur.status.cycle < 320) { // after we are done with the visible part of the frame, we reach sprite transfer to temp OAM tables and such - if (ppu_tick_counter == 256) + if (ppur.status.cycle == 256) { // do the more then 8 sprites stuff here where it is convenient // normally only 8 sprites are allowed, but with a particular setting we can have more then that @@ -625,7 +624,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES runppu(); break; case 1: - if (PPUON && scanline_counter == 0 && ppur.status.cycle == 305) + if (PPUON && ppur.status.sl == 0 && ppur.status.cycle == 305) { ppur.install_latches(); @@ -633,7 +632,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES runppu(); } - else if (PPUON && (scanline_counter != 0) && ppur.status.cycle == 257) + else if (PPUON && (ppur.status.sl != 0) && ppur.status.cycle == 257) { if (target <= 61441 && target > 0 && s == 0) @@ -882,14 +881,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } else { - if (ppu_tick_counter == 320) + if (ppur.status.cycle == 320) { ppuphase = PPUPHASE.BG; xt = 0; xp = 0; } - if (ppu_tick_counter < 336) + if (ppur.status.cycle < 336) { // if scanline is the pre-render line, we just read BG data Read_bgdata(xp, ref bgdata[xt]); @@ -920,7 +919,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES xt++; } } - else if (ppu_tick_counter < 340) + else if (ppur.status.cycle < 340) { runppu(); } @@ -932,23 +931,21 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES // equivelant of half a memory access cycle) before repeating the whole // pixel/scanline rendering process. If the scanline being rendered is the very // first one on every second frame, then this delay simply doesn't exist. - if (scanline_counter == 0 && idleSynch && evenOddDestiny && chopdot) - {} + if (ppur.status.sl == 0 && idleSynch && evenOddDestiny && chopdot) + { ppur.status.cycle++; } // increment cycle without running ppu else { runppu(); } } } - ppu_tick_counter++; - if (ppu_tick_counter == 341) + if (ppur.status.cycle == 341) { - ppu_tick_counter = 0; - scanline_counter++; + ppur.status.cycle = 0; + ppur.status.sl++; - if (scanline_counter == 241) + if (ppur.status.sl == 241) { do_active_sl = false; - ppur.status.sl = 241; } } } @@ -957,12 +954,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { runppu(); - - ppu_tick_counter++; - if (ppu_tick_counter == preNMIlines * kLineTime) + if (ppur.status.cycle == 341) { - ppu_tick_counter = 0; - do_pre_vbl = false; + ppur.status.cycle = 0; + ppur.status.sl++; + if (ppur.status.sl == 241 + preNMIlines) + { + do_pre_vbl = false; + } } } @@ -975,8 +974,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { runppu(); - ppu_tick_counter++; - if (ppu_tick_counter == 241 * kLineTime - 3) + if (ppur.status.cycle == 241 * kLineTime - 3) { ppudead--; }