2014-06-14 15:27:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-06-14 14:12:10 +00:00
|
|
|
#include "stdafx.h"
|
2014-06-26 01:52:37 +00:00
|
|
|
#include "Snapshotable.h"
|
2014-06-16 01:45:36 +00:00
|
|
|
#include "MemoryManager.h"
|
2014-06-14 14:12:10 +00:00
|
|
|
|
2015-07-22 03:05:27 +00:00
|
|
|
enum class NesModel;
|
|
|
|
|
2014-06-14 14:12:10 +00:00
|
|
|
enum PPURegisters
|
|
|
|
{
|
|
|
|
Control = 0x00,
|
2014-06-22 04:37:45 +00:00
|
|
|
Mask = 0x01,
|
2014-06-14 14:12:10 +00:00
|
|
|
Status = 0x02,
|
|
|
|
SpriteAddr = 0x03,
|
|
|
|
SpriteData = 0x04,
|
|
|
|
ScrollOffsets = 0x05,
|
|
|
|
VideoMemoryAddr = 0x06,
|
2014-06-21 01:48:55 +00:00
|
|
|
VideoMemoryData = 0x07,
|
|
|
|
SpriteDMA = 0x4014,
|
2014-06-14 14:12:10 +00:00
|
|
|
};
|
|
|
|
|
2014-06-14 22:20:56 +00:00
|
|
|
struct PPUControlFlags
|
|
|
|
{
|
|
|
|
bool VerticalWrite;
|
|
|
|
uint16_t SpritePatternAddr;
|
|
|
|
uint16_t BackgroundPatternAddr;
|
|
|
|
bool LargeSprites;
|
|
|
|
bool VBlank;
|
|
|
|
|
|
|
|
bool Grayscale;
|
|
|
|
bool BackgroundMask;
|
|
|
|
bool SpriteMask;
|
|
|
|
bool BackgroundEnabled;
|
|
|
|
bool SpritesEnabled;
|
|
|
|
bool IntensifyRed;
|
|
|
|
bool IntensifyGreen;
|
|
|
|
bool IntensifyBlue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PPUStatusFlags
|
|
|
|
{
|
|
|
|
bool SpriteOverflow;
|
|
|
|
bool Sprite0Hit;
|
|
|
|
bool VerticalBlank;
|
|
|
|
};
|
|
|
|
|
2014-06-14 14:12:10 +00:00
|
|
|
struct PPUState
|
|
|
|
{
|
2014-06-14 22:20:56 +00:00
|
|
|
uint8_t Control;
|
2014-06-22 04:37:45 +00:00
|
|
|
uint8_t Mask;
|
2014-06-14 14:12:10 +00:00
|
|
|
uint8_t Status;
|
2014-06-21 01:48:55 +00:00
|
|
|
uint32_t SpriteRamAddr;
|
2014-06-14 14:12:10 +00:00
|
|
|
uint16_t VideoRamAddr;
|
2014-06-17 22:16:49 +00:00
|
|
|
uint8_t XScroll;
|
|
|
|
uint16_t TmpVideoRamAddr;
|
|
|
|
bool WriteToggle;
|
2014-06-19 02:54:23 +00:00
|
|
|
|
|
|
|
uint16_t HighBitShift;
|
|
|
|
uint16_t LowBitShift;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TileInfo
|
2014-06-21 16:42:40 +00:00
|
|
|
{
|
|
|
|
uint8_t LowByte;
|
|
|
|
uint8_t HighByte;
|
2014-06-22 20:22:10 +00:00
|
|
|
uint32_t PaletteOffset;
|
2015-08-15 01:50:14 +00:00
|
|
|
|
|
|
|
uint16_t TileAddr; //used by HD ppu
|
|
|
|
uint8_t OffsetY; //used by HD ppu
|
2014-06-21 16:42:40 +00:00
|
|
|
};
|
|
|
|
|
2015-08-15 01:50:14 +00:00
|
|
|
struct SpriteInfo : TileInfo
|
2014-06-19 02:54:23 +00:00
|
|
|
{
|
2014-06-21 01:48:55 +00:00
|
|
|
bool HorizontalMirror;
|
2014-06-21 16:42:40 +00:00
|
|
|
bool BackgroundPriority;
|
2015-08-15 01:50:14 +00:00
|
|
|
uint8_t SpriteX;
|
2015-08-15 14:40:27 +00:00
|
|
|
|
|
|
|
bool VerticalMirror; //used by HD ppu
|
2014-06-14 14:12:10 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
struct PPUDebugState
|
|
|
|
{
|
|
|
|
PPUControlFlags ControlFlags;
|
|
|
|
PPUStatusFlags StatusFlags;
|
|
|
|
PPUState State;
|
|
|
|
int32_t Scanline;
|
|
|
|
uint32_t Cycle;
|
|
|
|
};
|
|
|
|
|
2014-06-26 01:52:37 +00:00
|
|
|
class PPU : public IMemoryHandler, public Snapshotable
|
2014-06-14 14:12:10 +00:00
|
|
|
{
|
2015-08-15 01:50:14 +00:00
|
|
|
protected:
|
2014-06-25 21:30:35 +00:00
|
|
|
static PPU* Instance;
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2014-06-16 01:45:36 +00:00
|
|
|
MemoryManager *_memoryManager;
|
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
PPUState _state;
|
2015-07-22 03:05:27 +00:00
|
|
|
int32_t _scanline;
|
|
|
|
uint32_t _cycle;
|
2016-01-03 01:36:22 +00:00
|
|
|
uint32_t _frameCount;
|
2015-07-22 03:05:27 +00:00
|
|
|
uint8_t _memoryReadBuffer;
|
|
|
|
|
2015-08-06 01:43:53 +00:00
|
|
|
uint8_t _paletteRAM[0x20];
|
2014-06-21 02:31:48 +00:00
|
|
|
|
2014-06-15 15:25:29 +00:00
|
|
|
uint8_t _spriteRAM[0x100];
|
2014-06-21 13:00:10 +00:00
|
|
|
uint8_t _secondarySpriteRAM[0x20];
|
2014-06-14 15:27:55 +00:00
|
|
|
|
2015-08-31 01:04:21 +00:00
|
|
|
uint16_t *_currentOutputBuffer;
|
|
|
|
uint16_t *_outputBuffers[2];
|
2015-07-21 22:18:20 +00:00
|
|
|
|
2015-07-22 03:05:27 +00:00
|
|
|
NesModel _nesModel;
|
|
|
|
uint16_t _vblankEnd;
|
2016-01-02 23:02:56 +00:00
|
|
|
bool _skipTick;
|
2015-07-22 03:05:27 +00:00
|
|
|
|
2014-06-15 13:35:17 +00:00
|
|
|
PPUControlFlags _flags;
|
|
|
|
PPUStatusFlags _statusFlags;
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2015-07-23 02:08:28 +00:00
|
|
|
uint16_t _intensifyColorBits;
|
|
|
|
uint8_t _paletteRamMask;
|
|
|
|
|
2015-08-15 01:50:14 +00:00
|
|
|
SpriteInfo *_lastSprite; //used by HD ppu
|
|
|
|
|
2014-06-19 02:54:23 +00:00
|
|
|
TileInfo _currentTile;
|
|
|
|
TileInfo _nextTile;
|
2014-06-19 23:58:15 +00:00
|
|
|
TileInfo _previousTile;
|
2014-06-21 01:48:55 +00:00
|
|
|
|
2014-06-21 16:42:40 +00:00
|
|
|
SpriteInfo _spriteTiles[8];
|
2014-06-22 05:54:35 +00:00
|
|
|
uint32_t _spriteCount = 0;
|
|
|
|
uint32_t _secondaryOAMAddr = 0;
|
2014-06-21 16:42:40 +00:00
|
|
|
bool _sprite0Visible = false;
|
2014-07-01 22:05:54 +00:00
|
|
|
|
2015-12-29 18:23:38 +00:00
|
|
|
uint32_t _spriteIndex = 0;
|
|
|
|
|
2016-01-03 01:36:22 +00:00
|
|
|
uint8_t _openBus = 0;
|
|
|
|
int32_t _openBusDecayStamp[8];
|
2016-01-03 22:20:18 +00:00
|
|
|
uint32_t _ignoreVramRead = 0;
|
2016-01-03 01:36:22 +00:00
|
|
|
|
2015-12-29 01:07:27 +00:00
|
|
|
uint16_t _spriteDmaCounter = 0;
|
|
|
|
uint16_t _spriteDmaAddr = 0;
|
|
|
|
|
2014-07-01 22:05:54 +00:00
|
|
|
uint8_t _oamCopybuffer;
|
2016-01-03 16:44:20 +00:00
|
|
|
bool _spriteInRange;
|
2014-07-01 22:05:54 +00:00
|
|
|
bool _sprite0Added;
|
2016-01-03 16:44:20 +00:00
|
|
|
uint8_t _spriteAddrH;
|
|
|
|
uint8_t _spriteAddrL;
|
|
|
|
bool _oamCopyDone;
|
|
|
|
|
2014-06-17 22:16:49 +00:00
|
|
|
void UpdateStatusFlag();
|
|
|
|
|
2014-06-22 04:37:45 +00:00
|
|
|
void SetControlRegister(uint8_t value);
|
|
|
|
void SetMaskRegister(uint8_t value);
|
2014-06-17 22:16:49 +00:00
|
|
|
|
|
|
|
bool IsRenderingEnabled();
|
|
|
|
|
2016-01-03 01:36:22 +00:00
|
|
|
void SetOpenBus(uint8_t mask, uint8_t value);
|
|
|
|
uint8_t ApplyOpenBus(uint8_t mask, uint8_t value);
|
|
|
|
|
2014-06-22 04:37:45 +00:00
|
|
|
void UpdateVideoRamAddr();
|
2014-06-17 22:16:49 +00:00
|
|
|
void IncVerticalScrolling();
|
|
|
|
void IncHorizontalScrolling();
|
2014-06-19 23:58:15 +00:00
|
|
|
uint16_t GetNameTableAddr();
|
2014-06-17 22:16:49 +00:00
|
|
|
uint16_t GetAttributeAddr();
|
|
|
|
|
2014-06-21 01:48:55 +00:00
|
|
|
void ProcessPreVBlankScanline();
|
2014-06-17 22:16:49 +00:00
|
|
|
void ProcessPrerenderScanline();
|
|
|
|
void ProcessVisibleScanline();
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2014-06-21 01:48:55 +00:00
|
|
|
void CopyOAMData();
|
|
|
|
|
2014-06-17 22:16:49 +00:00
|
|
|
void BeginVBlank();
|
|
|
|
void EndVBlank();
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2014-06-22 20:22:10 +00:00
|
|
|
uint32_t GetBGPaletteEntry(uint32_t paletteOffset, uint32_t pixel);
|
|
|
|
uint32_t GetSpritePaletteEntry(uint32_t paletteOffset, uint32_t pixel);
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2014-06-21 02:31:48 +00:00
|
|
|
void WritePaletteRAM(uint16_t addr, uint8_t value);
|
|
|
|
|
2016-01-03 20:35:40 +00:00
|
|
|
void LoadTileInfo(bool updatePreviousCurrent);
|
2015-12-29 18:23:38 +00:00
|
|
|
void LoadSpriteTileInfo();
|
2014-06-19 23:58:15 +00:00
|
|
|
void ShiftTileRegisters();
|
|
|
|
void InitializeShiftRegisters();
|
|
|
|
void LoadNextTile();
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2015-08-15 01:50:14 +00:00
|
|
|
uint32_t GetPixelColor(uint32_t &paletteOffset);
|
|
|
|
virtual void DrawPixel();
|
|
|
|
virtual void SendFrame();
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
PPURegisters GetRegisterID(uint16_t addr)
|
|
|
|
{
|
2014-06-21 01:48:55 +00:00
|
|
|
if(addr == 0x4014) {
|
|
|
|
return PPURegisters::SpriteDMA;
|
|
|
|
} else {
|
|
|
|
return (PPURegisters)(addr & 0x07);
|
|
|
|
}
|
2014-06-14 14:12:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 01:52:37 +00:00
|
|
|
void StreamState(bool saving);
|
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
public:
|
2015-07-24 03:16:31 +00:00
|
|
|
static const uint32_t ScreenWidth = 256;
|
|
|
|
static const uint32_t ScreenHeight = 240;
|
|
|
|
static const uint32_t PixelCount = 256*240;
|
|
|
|
static const uint32_t OutputBufferSize = 256*240*2;
|
|
|
|
|
2014-06-16 01:45:36 +00:00
|
|
|
PPU(MemoryManager *memoryManager);
|
2015-08-15 01:50:14 +00:00
|
|
|
virtual ~PPU();
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2014-06-23 17:52:53 +00:00
|
|
|
void Reset();
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
PPUDebugState GetState();
|
|
|
|
|
2014-06-25 16:22:48 +00:00
|
|
|
void GetMemoryRanges(MemoryRanges &ranges)
|
2014-06-16 01:45:36 +00:00
|
|
|
{
|
2015-07-30 02:10:34 +00:00
|
|
|
ranges.AddHandler(MemoryOperation::Read, 0x2000, 0x3FFF);
|
|
|
|
ranges.AddHandler(MemoryOperation::Write, 0x2000, 0x3FFF);
|
|
|
|
ranges.AddHandler(MemoryOperation::Write, 0x4014);
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
2014-06-14 14:12:10 +00:00
|
|
|
|
2015-08-06 01:43:53 +00:00
|
|
|
uint8_t ReadPaletteRAM(uint16_t addr);
|
|
|
|
|
2014-06-16 01:45:36 +00:00
|
|
|
uint8_t ReadRAM(uint16_t addr);
|
|
|
|
void WriteRAM(uint16_t addr, uint8_t value);
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2015-07-22 03:05:27 +00:00
|
|
|
void SetNesModel(NesModel model);
|
2015-12-26 22:11:00 +00:00
|
|
|
|
2015-07-05 02:21:14 +00:00
|
|
|
void Exec();
|
2015-07-21 22:18:20 +00:00
|
|
|
static void ExecStatic();
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2015-07-11 01:07:24 +00:00
|
|
|
static uint32_t GetFrameCount()
|
2014-06-19 02:54:23 +00:00
|
|
|
{
|
2015-07-11 01:07:24 +00:00
|
|
|
return PPU::Instance->_frameCount;
|
2014-06-19 02:54:23 +00:00
|
|
|
}
|
2014-06-25 21:30:35 +00:00
|
|
|
|
2014-06-26 20:41:07 +00:00
|
|
|
static uint32_t GetFrameCycle()
|
|
|
|
{
|
|
|
|
return ((PPU::Instance->_scanline + 1) * 341) + PPU::Instance->_cycle;
|
|
|
|
}
|
|
|
|
|
2015-07-30 02:10:34 +00:00
|
|
|
static PPUControlFlags GetControlFlags()
|
|
|
|
{
|
|
|
|
return PPU::Instance->_flags;
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:30:35 +00:00
|
|
|
static uint32_t GetCurrentCycle()
|
|
|
|
{
|
|
|
|
return PPU::Instance->_cycle;
|
|
|
|
}
|
2014-06-26 20:41:07 +00:00
|
|
|
|
|
|
|
static uint32_t GetCurrentScanline()
|
|
|
|
{
|
|
|
|
return PPU::Instance->_scanline;
|
|
|
|
}
|
2015-08-06 01:43:53 +00:00
|
|
|
|
2015-08-06 00:40:10 +00:00
|
|
|
uint8_t* GetSpriteRam()
|
|
|
|
{
|
|
|
|
return _spriteRAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* GetSecondarySpriteRam()
|
|
|
|
{
|
|
|
|
return _secondarySpriteRAM;
|
|
|
|
}
|
2014-06-19 02:54:23 +00:00
|
|
|
};
|