Mesen/Core/PPU.h

255 lines
4.8 KiB
C
Raw Normal View History

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,
Mask = 0x01,
2014-06-14 14:12:10 +00:00
Status = 0x02,
SpriteAddr = 0x03,
SpriteData = 0x04,
ScrollOffsets = 0x05,
VideoMemoryAddr = 0x06,
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;
uint8_t Mask;
2014-06-14 14:12:10 +00:00
uint8_t Status;
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;
uint16_t HighBitShift;
uint16_t LowBitShift;
};
struct TileInfo
{
uint8_t LowByte;
uint8_t HighByte;
uint32_t PaletteOffset;
uint16_t TileAddr; //used by HD ppu
uint8_t OffsetY; //used by HD ppu
};
struct SpriteInfo : TileInfo
{
bool HorizontalMirror;
bool BackgroundPriority;
uint8_t SpriteX;
bool VerticalMirror; //used by HD ppu
2014-06-14 14:12:10 +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
{
protected:
static PPU* Instance;
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;
int32_t _frameCount;
2015-07-22 03:05:27 +00:00
uint8_t _memoryReadBuffer;
uint8_t _paletteRAM[0x20];
uint8_t _spriteRAM[0x100];
2014-06-21 13:00:10 +00:00
uint8_t _secondarySpriteRAM[0x20];
2014-06-14 15:27:55 +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;
2014-06-15 13:35:17 +00:00
PPUControlFlags _flags;
PPUStatusFlags _statusFlags;
2014-06-14 22:20:56 +00:00
uint16_t _intensifyColorBits;
uint8_t _paletteRamMask;
SpriteInfo *_lastSprite; //used by HD ppu
TileInfo _currentTile;
TileInfo _nextTile;
TileInfo _previousTile;
SpriteInfo _spriteTiles[8];
uint32_t _spriteCount = 0;
uint32_t _secondaryOAMAddr = 0;
bool _sprite0Visible = false;
uint32_t _spriteIndex = 0;
2015-12-29 01:07:27 +00:00
uint16_t _spriteDmaCounter = 0;
uint16_t _spriteDmaAddr = 0;
uint8_t _oamCopybuffer;
bool _writeOAMData;
uint32_t _overflowCounter;
bool _sprite0Added;
2014-06-17 22:16:49 +00:00
void UpdateStatusFlag();
void SetControlRegister(uint8_t value);
void SetMaskRegister(uint8_t value);
2014-06-17 22:16:49 +00:00
bool IsRenderingEnabled();
void UpdateVideoRamAddr();
2014-06-17 22:16:49 +00:00
void IncVerticalScrolling();
void IncHorizontalScrolling();
uint16_t GetNameTableAddr();
2014-06-17 22:16:49 +00:00
uint16_t GetAttributeAddr();
void ProcessPreVBlankScanline();
2014-06-17 22:16:49 +00:00
void ProcessPrerenderScanline();
void ProcessVisibleScanline();
2014-06-14 22:20:56 +00:00
void CopyOAMData();
2014-06-17 22:16:49 +00:00
void BeginVBlank();
void EndVBlank();
2014-06-14 22:20:56 +00:00
uint32_t GetBGPaletteEntry(uint32_t paletteOffset, uint32_t pixel);
uint32_t GetSpritePaletteEntry(uint32_t paletteOffset, uint32_t pixel);
void WritePaletteRAM(uint16_t addr, uint8_t value);
void LoadTileInfo();
void LoadSpriteTileInfo();
void ShiftTileRegisters();
void InitializeShiftRegisters();
void LoadNextTile();
uint32_t GetPixelColor(uint32_t &paletteOffset);
virtual void DrawPixel();
virtual void SendFrame();
2014-06-14 15:27:55 +00:00
PPURegisters GetRegisterID(uint16_t addr)
{
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);
virtual ~PPU();
2014-06-14 22:20:56 +00:00
void Reset();
PPUDebugState GetState();
void GetMemoryRanges(MemoryRanges &ranges)
2014-06-16 01:45:36 +00:00
{
ranges.AddHandler(MemoryOperation::Read, 0x2000, 0x3FFF);
ranges.AddHandler(MemoryOperation::Read, 0x4014);
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
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
void Exec();
2015-07-21 22:18:20 +00:00
static void ExecStatic();
static uint32_t GetFrameCount()
{
return PPU::Instance->_frameCount;
}
static uint32_t GetFrameCycle()
{
return ((PPU::Instance->_scanline + 1) * 341) + PPU::Instance->_cycle;
}
static PPUControlFlags GetControlFlags()
{
return PPU::Instance->_flags;
}
static uint32_t GetCurrentCycle()
{
return PPU::Instance->_cycle;
}
static uint32_t GetCurrentScanline()
{
return PPU::Instance->_scanline;
}
uint8_t* GetSpriteRam()
{
return _spriteRAM;
}
uint8_t* GetSecondarySpriteRam()
{
return _secondarySpriteRAM;
}
};