2011-02-27 09:45:50 +00:00
|
|
|
|
//http://nesdev.parodius.com/bbs/viewtopic.php?p=4571&sid=db4c7e35316cc5d734606dd02f11dccb
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using BizHawk.Emulation.CPUs.M6502;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.Emulation.Consoles.Nintendo
|
|
|
|
|
{
|
|
|
|
|
partial class NES
|
|
|
|
|
{
|
2011-02-28 09:13:27 +00:00
|
|
|
|
public partial class PPU
|
2011-02-27 09:45:50 +00:00
|
|
|
|
{
|
2012-10-31 18:25:46 +00:00
|
|
|
|
// this only handles region differences within the PPU
|
|
|
|
|
int preNMIlines;
|
|
|
|
|
int postNMIlines;
|
|
|
|
|
bool chopdot;
|
|
|
|
|
public enum Region { NTSC, PAL, Dendy, RGB };
|
|
|
|
|
Region _region;
|
|
|
|
|
public Region region { set { _region = value; SyncRegion(); } get { return _region; } }
|
|
|
|
|
void SyncRegion()
|
|
|
|
|
{
|
|
|
|
|
switch (region)
|
|
|
|
|
{
|
|
|
|
|
case Region.NTSC:
|
|
|
|
|
preNMIlines = 1; postNMIlines = 20; chopdot = true; break;
|
|
|
|
|
case Region.PAL:
|
|
|
|
|
preNMIlines = 1; postNMIlines = 70; chopdot = false; break;
|
|
|
|
|
case Region.Dendy:
|
|
|
|
|
preNMIlines = 51; postNMIlines = 20; chopdot = false; break;
|
|
|
|
|
case Region.RGB:
|
|
|
|
|
preNMIlines = 1; postNMIlines = 20; chopdot = false; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-10 07:43:48 +00:00
|
|
|
|
public class DebugCallback
|
|
|
|
|
{
|
|
|
|
|
public int Scanline;
|
|
|
|
|
//public int Dot; //not supported
|
|
|
|
|
public Action Callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DebugCallback NTViewCallback;
|
|
|
|
|
public DebugCallback PPUViewCallback;
|
|
|
|
|
|
2011-03-28 16:43:15 +00:00
|
|
|
|
|
2011-06-10 07:43:48 +00:00
|
|
|
|
//when the ppu issues a write it goes through here and into the game board
|
2011-03-06 02:36:49 +00:00
|
|
|
|
public void ppubus_write(int addr, byte value)
|
2011-02-27 09:45:50 +00:00
|
|
|
|
{
|
2011-06-06 10:27:42 +00:00
|
|
|
|
nes.board.AddressPPU(addr);
|
2011-02-27 09:45:50 +00:00
|
|
|
|
nes.board.WritePPU(addr, value);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 06:16:20 +00:00
|
|
|
|
//when the ppu issues a read it goes through here and into the game board
|
2011-06-07 07:14:34 +00:00
|
|
|
|
public byte ppubus_read(int addr, bool ppu)
|
2011-02-27 09:45:50 +00:00
|
|
|
|
{
|
2011-06-10 01:14:52 +00:00
|
|
|
|
//hardware doesnt touch the bus when the PPU is disabled
|
2011-06-07 07:14:34 +00:00
|
|
|
|
if (!reg_2001.PPUON && ppu)
|
|
|
|
|
return 0xFF;
|
|
|
|
|
|
2011-06-06 10:27:42 +00:00
|
|
|
|
nes.board.AddressPPU(addr);
|
2011-08-03 01:42:51 +00:00
|
|
|
|
return nes.board.ReadPPU(addr);
|
2011-02-27 09:45:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-13 19:35:50 +00:00
|
|
|
|
//debug tools peek into the ppu through this
|
|
|
|
|
public byte ppubus_peek(int addr)
|
|
|
|
|
{
|
|
|
|
|
return nes.board.PeekPPU(addr);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-09 19:45:07 +00:00
|
|
|
|
public enum PPUPHASE
|
|
|
|
|
{
|
2011-02-27 09:45:50 +00:00
|
|
|
|
VBL, BG, OBJ
|
|
|
|
|
};
|
2011-06-09 19:45:07 +00:00
|
|
|
|
public PPUPHASE ppuphase;
|
2011-02-27 09:45:50 +00:00
|
|
|
|
|
|
|
|
|
NES nes;
|
|
|
|
|
public PPU(NES nes)
|
|
|
|
|
{
|
|
|
|
|
this.nes = nes;
|
2012-03-18 03:46:06 +00:00
|
|
|
|
|
|
|
|
|
OAM = new byte[0x100];
|
|
|
|
|
PALRAM = new byte[0x20];
|
|
|
|
|
|
|
|
|
|
//power-up palette verified by blargg's power_up_palette test.
|
|
|
|
|
//he speculates that these may differ depending on the system tested..
|
|
|
|
|
//and I don't see why the ppu would waste any effort setting these..
|
|
|
|
|
//but for the sake of uniformity, we'll do it.
|
|
|
|
|
Array.Copy(new byte[] {
|
|
|
|
|
0x09,0x01,0x00,0x01,0x00,0x02,0x02,0x0D,0x08,0x10,0x08,0x24,0x00,0x00,0x04,0x2C,
|
|
|
|
|
0x09,0x01,0x34,0x03,0x00,0x04,0x00,0x14,0x08,0x3A,0x00,0x02,0x00,0x20,0x2C,0x08
|
|
|
|
|
}, PALRAM, 0x20);
|
|
|
|
|
|
2011-02-27 09:45:50 +00:00
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-01 09:32:12 +00:00
|
|
|
|
//state
|
2011-02-27 09:45:50 +00:00
|
|
|
|
int ppudead; //measured in frames
|
|
|
|
|
bool idleSynch;
|
2011-06-09 19:45:07 +00:00
|
|
|
|
int NMI_PendingInstructions;
|
|
|
|
|
byte PPUGenLatch;
|
|
|
|
|
bool vtoggle;
|
|
|
|
|
byte VRAMBuffer;
|
2011-08-28 19:07:33 +00:00
|
|
|
|
public byte[] OAM;
|
2011-06-09 19:45:07 +00:00
|
|
|
|
public byte[] PALRAM;
|
2011-02-27 09:45:50 +00:00
|
|
|
|
|
2011-04-17 22:51:53 +00:00
|
|
|
|
public void SyncState(Serializer ser)
|
2011-03-01 09:32:12 +00:00
|
|
|
|
{
|
2011-06-09 19:45:07 +00:00
|
|
|
|
byte temp8;
|
|
|
|
|
|
2011-04-17 22:51:53 +00:00
|
|
|
|
ser.Sync("ppudead", ref ppudead);
|
|
|
|
|
ser.Sync("idleSynch", ref idleSynch);
|
2011-06-09 19:45:07 +00:00
|
|
|
|
ser.Sync("NMI_PendingInstructions", ref NMI_PendingInstructions);
|
2011-04-17 22:51:53 +00:00
|
|
|
|
ser.Sync("PPUGenLatch", ref PPUGenLatch);
|
|
|
|
|
ser.Sync("vtoggle", ref vtoggle);
|
|
|
|
|
ser.Sync("VRAMBuffer", ref VRAMBuffer);
|
2012-03-25 09:25:27 +00:00
|
|
|
|
ser.Sync("ppu_addr_temp", ref ppu_addr_temp);
|
2011-06-09 19:45:07 +00:00
|
|
|
|
|
|
|
|
|
ser.Sync("OAM", ref OAM, false);
|
|
|
|
|
ser.Sync("PALRAM", ref PALRAM, false);
|
|
|
|
|
|
|
|
|
|
ser.Sync("Reg2002_objoverflow", ref Reg2002_objoverflow);
|
|
|
|
|
ser.Sync("Reg2002_objhit", ref Reg2002_objhit);
|
|
|
|
|
ser.Sync("Reg2002_vblank_active", ref Reg2002_vblank_active);
|
|
|
|
|
ser.Sync("Reg2002_vblank_active_pending", ref Reg2002_vblank_active_pending);
|
|
|
|
|
ser.Sync("Reg2002_vblank_clear_pending", ref Reg2002_vblank_clear_pending);
|
2011-04-17 22:51:53 +00:00
|
|
|
|
ppur.SyncState(ser);
|
2011-06-09 19:45:07 +00:00
|
|
|
|
temp8 = reg_2000.Value; ser.Sync("reg_2000.Value", ref temp8); reg_2000.Value = temp8;
|
|
|
|
|
temp8 = reg_2001.Value; ser.Sync("reg_2001.Value", ref temp8); reg_2001.Value = temp8;
|
|
|
|
|
ser.Sync("reg_2003", ref reg_2003);
|
2011-04-17 22:51:53 +00:00
|
|
|
|
|
2011-06-09 19:45:07 +00:00
|
|
|
|
//don't sync framebuffer into binary (rewind) states
|
2011-04-17 22:51:53 +00:00
|
|
|
|
if(ser.IsText)
|
|
|
|
|
ser.Sync("xbuf", ref xbuf, false);
|
2011-03-01 09:32:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 09:45:50 +00:00
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
regs_reset();
|
|
|
|
|
ppudead = 2;
|
|
|
|
|
idleSynch = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TriggerNMI()
|
|
|
|
|
{
|
|
|
|
|
nes.cpu.NMI = true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 07:14:34 +00:00
|
|
|
|
//this gets called once after each cpu instruction executes.
|
|
|
|
|
//anything that needs to happen at instruction granularity can get checked here
|
|
|
|
|
//to save having to check it at ppu cycle granularity
|
2012-03-15 21:28:37 +00:00
|
|
|
|
public void PostCpuInstructionOne()
|
2011-06-06 10:27:42 +00:00
|
|
|
|
{
|
2011-06-07 07:14:34 +00:00
|
|
|
|
if (NMI_PendingInstructions > 0)
|
2011-06-06 10:27:42 +00:00
|
|
|
|
{
|
2011-06-07 07:14:34 +00:00
|
|
|
|
NMI_PendingInstructions--;
|
|
|
|
|
if (NMI_PendingInstructions <= 0)
|
2011-06-06 10:27:42 +00:00
|
|
|
|
{
|
|
|
|
|
TriggerNMI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 09:45:50 +00:00
|
|
|
|
void runppu(int x)
|
|
|
|
|
{
|
2011-06-07 07:14:34 +00:00
|
|
|
|
//run one ppu cycle at a time so we can interact with the ppu and clockPPU at high granularity
|
|
|
|
|
for (int i = 0; i < x; i++)
|
2011-06-06 10:27:42 +00:00
|
|
|
|
{
|
2011-06-09 19:45:07 +00:00
|
|
|
|
ppur.status.cycle++;
|
|
|
|
|
|
2011-06-07 07:14:34 +00:00
|
|
|
|
//might not actually run a cpu cycle if there are none to be run right now
|
2012-03-15 21:28:37 +00:00
|
|
|
|
nes.RunCpuOne();
|
2011-06-07 07:14:34 +00:00
|
|
|
|
|
2011-06-06 10:27:42 +00:00
|
|
|
|
if (Reg2002_vblank_active_pending)
|
2011-06-07 07:14:34 +00:00
|
|
|
|
{
|
2012-08-23 01:28:33 +00:00
|
|
|
|
//if (Reg2002_vblank_active_pending)
|
2011-06-07 07:14:34 +00:00
|
|
|
|
Reg2002_vblank_active = 1;
|
|
|
|
|
Reg2002_vblank_active_pending = false;
|
|
|
|
|
}
|
2011-06-06 10:27:42 +00:00
|
|
|
|
|
2011-06-07 07:14:34 +00:00
|
|
|
|
if (Reg2002_vblank_clear_pending)
|
|
|
|
|
{
|
|
|
|
|
Reg2002_vblank_active = 0;
|
|
|
|
|
Reg2002_vblank_clear_pending = false;
|
|
|
|
|
}
|
2011-06-06 10:27:42 +00:00
|
|
|
|
|
2011-06-07 07:14:34 +00:00
|
|
|
|
nes.board.ClockPPU();
|
|
|
|
|
}
|
2011-02-27 09:45:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//hack
|
2012-10-31 18:25:46 +00:00
|
|
|
|
//public bool PAL = false;
|
2012-08-23 01:28:33 +00:00
|
|
|
|
//bool SPRITELIMIT = true;
|
2011-02-27 09:45:50 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|