NESHawk: Code Clean up

remove redundancies, fix scanline count
This commit is contained in:
alyosha-tas 2017-09-02 10:48:23 -04:00 committed by GitHub
parent d0fdc75a1e
commit 874d06797f
4 changed files with 49 additions and 54 deletions

View File

@ -367,7 +367,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
} }
FrameGo = true; FrameGo = true;
ppu.ppu_tick_counter = 0;
if (ppu.ppudead > 0) if (ppu.ppudead > 0)
{ {

View File

@ -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 //when the ppu issues a write it goes through here and into the game board
public void ppubus_write(int addr, byte value) 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.AddressPPU(addr);
nes.Board.WritePPU(addr, value); 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_vbl", ref do_vbl);
ser.Sync("do_active_sl", ref do_active_sl); ser.Sync("do_active_sl", ref do_active_sl);
ser.Sync("do_pre_vbl", ref do_pre_vbl); 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("nmi_destiny", ref nmi_destiny);
ser.Sync("yp_shift", ref yp_shift); ser.Sync("yp_shift", ref yp_shift);
ser.Sync("sprite_eval_cycle", ref sprite_eval_cycle); 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 //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 //apparently this shoves it on the address bus, too, or else blargg's mmc3 tests dont pass
//ONLY if the ppu is not rendering //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()); nes.Board.AddressPPU(ppur.get_2007access());
race_2006 = true; race_2006 = true;
@ -346,7 +344,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
ppur.status.cycle++; ppur.status.cycle++;
is_even_cycle = !is_even_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; reg_2003 = 0;
} }

View File

@ -148,7 +148,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
fh = 0; fh = 0;
_fv = _v = _h = _vt = _ht = 0; _fv = _v = _h = _vt = _ht = 0;
status.cycle = 0; status.cycle = 0;
status.sl = 241; status.sl = 0;
} }
public void install_latches() 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 //otherwise we just write this value and move on to the next oam byte
value &= 0xE3; 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 // don't write to OAM if the screen is on and we are in the active display area
// this impacts sprite evaluation // 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); ppur.increment2007(ppur.status.rendering && PPUON, reg_2000.vram_incr32 != 0);
//see comments in $2006 //see comments in $2006
if (ppur.status.sl == 241 || !PPUON) if (ppur.status.sl >= 241 || !PPUON)
nes.Board.AddressPPU(ppur.get_2007access()); nes.Board.AddressPPU(ppur.get_2007access());
} }
byte read_2007() byte read_2007()
@ -611,7 +611,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
ppur.increment2007(ppur.status.rendering && PPUON, reg_2000.vram_incr32 != 0); ppur.increment2007(ppur.status.rendering && PPUON, reg_2000.vram_incr32 != 0);
//see comments in $2006 //see comments in $2006
if (ppur.status.sl == 241 || !PPUON) if (ppur.status.sl >= 241 || !PPUON)
nes.Board.AddressPPU(ppur.get_2007access()); nes.Board.AddressPPU(ppur.get_2007access());
// update open bus here // update open bus here

View File

@ -133,9 +133,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public bool do_vbl; public bool do_vbl;
public bool do_active_sl; public bool do_active_sl;
public bool do_pre_vbl; public bool do_pre_vbl;
public int ppu_tick_counter;
int scanline_counter;
bool nmi_destiny; bool nmi_destiny;
int yp_shift; int yp_shift;
int sprite_eval_cycle; int sprite_eval_cycle;
@ -156,51 +154,52 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public void ppu_init_frame() public void ppu_init_frame()
{ {
scanline_counter = 0; ppur.status.sl = 241 + preNMIlines;
ppu_tick_counter = 0; ppur.status.cycle = 0;
// These things happen at the start of every frame // These things happen at the start of every frame
Reg2002_vblank_active_pending = true; Reg2002_vblank_active_pending = true;
ppuphase = PPUPHASE.VBL; ppuphase = PPUPHASE.VBL;
ppur.status.sl = 241;
bgdata = new BGDataRecord[34]; bgdata = new BGDataRecord[34];
} }
public void TickPPU_VBL() 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; 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; } if (nmi_destiny) { nes.cpu.NMI = true; }
nes.Board.AtVsyncNMI(); nes.Board.AtVsyncNMI();
} }
runppu(); runppu(); // note cycle ticks inside runppu
ppu_tick_counter++;
if (ppu_tick_counter == postNMIlines * kLineTime) if (ppur.status.cycle == 341)
{ {
Reg2002_objhit = Reg2002_objoverflow = 0; ppur.status.cycle = 0;
Reg2002_vblank_clear_pending = true; ppur.status.sl++;
idleSynch ^= true; if (ppur.status.sl == 241 + preNMIlines + postNMIlines)
{
Reg2002_objhit = Reg2002_objoverflow = 0;
Reg2002_vblank_clear_pending = true;
idleSynch ^= true;
do_vbl = false; do_vbl = false;
ppu_tick_counter = 0; ppur.status.sl = 0;
}
} }
} }
public void TickPPU_active() public void TickPPU_active()
{ {
if (ppu_tick_counter == 0) if (ppur.status.cycle == 0)
{ {
ppur.status.cycle = 0; ppur.status.cycle = 0;
ppur.status.sl = scanline_counter;
spr_true_count = 0; spr_true_count = 0;
soam_index = 0; soam_index = 0;
soam_m_index = 0; soam_m_index = 0;
@ -213,12 +212,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
sprite_zero_in_range = false; sprite_zero_in_range = false;
yp = scanline_counter - 1; yp = ppur.status.sl - 1;
ppuphase = PPUPHASE.BG; ppuphase = PPUPHASE.BG;
// "If PPUADDR is not less then 8 when rendering starts, the first 8 bytes in OAM are written to from // "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" // 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++) 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 // 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 // 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 // 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 // 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(); runppu();
break; break;
case 1: case 1:
if (PPUON && scanline_counter == 0 && ppur.status.cycle == 305) if (PPUON && ppur.status.sl == 0 && ppur.status.cycle == 305)
{ {
ppur.install_latches(); ppur.install_latches();
@ -633,7 +632,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
runppu(); 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) if (target <= 61441 && target > 0 && s == 0)
@ -882,14 +881,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
} }
else else
{ {
if (ppu_tick_counter == 320) if (ppur.status.cycle == 320)
{ {
ppuphase = PPUPHASE.BG; ppuphase = PPUPHASE.BG;
xt = 0; xt = 0;
xp = 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 // if scanline is the pre-render line, we just read BG data
Read_bgdata(xp, ref bgdata[xt]); Read_bgdata(xp, ref bgdata[xt]);
@ -920,7 +919,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
xt++; xt++;
} }
} }
else if (ppu_tick_counter < 340) else if (ppur.status.cycle < 340)
{ {
runppu(); runppu();
} }
@ -932,23 +931,21 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
// equivelant of half a memory access cycle) before repeating the whole // equivelant of half a memory access cycle) before repeating the whole
// pixel/scanline rendering process. If the scanline being rendered is the very // 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. // 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 else
{ runppu(); } { runppu(); }
} }
} }
ppu_tick_counter++; if (ppur.status.cycle == 341)
if (ppu_tick_counter == 341)
{ {
ppu_tick_counter = 0; ppur.status.cycle = 0;
scanline_counter++; ppur.status.sl++;
if (scanline_counter == 241) if (ppur.status.sl == 241)
{ {
do_active_sl = false; do_active_sl = false;
ppur.status.sl = 241;
} }
} }
} }
@ -957,12 +954,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
{ {
runppu(); runppu();
if (ppur.status.cycle == 341)
ppu_tick_counter++;
if (ppu_tick_counter == preNMIlines * kLineTime)
{ {
ppu_tick_counter = 0; ppur.status.cycle = 0;
do_pre_vbl = false; ppur.status.sl++;
if (ppur.status.sl == 241 + preNMIlines)
{
do_pre_vbl = false;
}
} }
} }
@ -975,8 +974,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
{ {
runppu(); runppu();
ppu_tick_counter++; if (ppur.status.cycle == 241 * kLineTime - 3)
if (ppu_tick_counter == 241 * kLineTime - 3)
{ {
ppudead--; ppudead--;
} }