NESEmulator/NES Emulator/ppu.h

219 lines
3 KiB
C
Raw Permalink Normal View History

2021-10-23 17:30:02 +00:00
#ifndef _PPU_H_
#define _PPU_H_
#include "types.h"
2021-10-23 18:36:38 +00:00
#include "SDL.h"
2021-10-23 17:30:02 +00:00
struct Bus;
2021-10-28 14:57:37 +00:00
struct Pixel
{
Byte r;
Byte g;
Byte b;
};
struct FIFO16
{
union
{
struct
{
Byte lo;
Byte hi;
};
Word data;
};
};
2021-10-29 20:33:34 +00:00
union OAMEntry
{
struct
{
Byte y;
Byte tile;
Byte attr;
Byte x;
};
DWord raw;
};
2021-10-23 17:30:02 +00:00
struct PPU
{
2021-10-28 14:57:37 +00:00
////////////////////////////////////////
/// REGISTERS ///
////////////////////////////////////////
2021-10-23 17:30:02 +00:00
union
{
struct
{
Byte nametable : 2;
Byte increment : 1;
Byte spriteTile : 1;
Byte bgTile : 1;
Byte spriteHeight : 1;
Byte master : 1;
Byte nmiEnable : 1;
};
Byte raw;
2021-10-23 18:16:29 +00:00
} ppuCtrl;
2021-10-23 17:30:02 +00:00
union
{
struct
{
Byte greyscale : 1;
Byte bgLeftColumn : 1;
Byte spriteLeftColumn : 1;
Byte bgEnable : 1;
Byte spriteEnable : 1;
Byte colorEmphasis : 3;
};
Byte raw;
2021-10-23 18:16:29 +00:00
} ppuMask;
2021-10-23 17:30:02 +00:00
union
{
struct
{
Byte padding : 5;
Byte overflow : 1;
Byte spriteZeroHit : 1;
Byte vBlank : 1;
};
Byte raw;
2021-10-23 18:16:29 +00:00
} ppuStatus;
2021-10-23 17:30:02 +00:00
Byte oamaddr;
Byte oamdata;
2021-10-23 18:16:29 +00:00
2021-10-28 14:57:37 +00:00
////////////////////////////////////////
/// PSEUDO REGISTERS ///
////////////////////////////////////////
2021-10-23 18:16:29 +00:00
Byte scrollX, scrollY;
Byte scrollWriteTarget;
union
{
struct
{
Byte lo;
Byte hi;
};
Word raw;
} ppuAddress;
Byte ppuAddressWriteTarget;
2021-10-30 17:42:17 +00:00
Byte ppuReadLatch;
2021-10-23 18:16:29 +00:00
2021-10-23 17:30:02 +00:00
Byte oamdma;
2021-10-28 14:57:37 +00:00
////////////////////////////////////////
/// VRAM ///
////////////////////////////////////////
2021-10-24 11:47:11 +00:00
Byte* nameTables[2];
SDL_Texture* nameTableTextures[2];
2021-10-29 15:34:17 +00:00
SDL_Texture* renderedNameTableTextures[2];
2021-10-24 11:47:11 +00:00
Byte mirroring;
2021-10-23 17:30:02 +00:00
Byte* paletteIndexes;
2021-10-28 14:57:37 +00:00
////////////////////////////////////////
/// PHASE TRACKERS ///
////////////////////////////////////////
enum
{
Visible,
PostRender,
VBlank,
PreRender,
VerticalPhaseSize
} verticalPhase;
enum
{
Idle,
Fetching,
SpriteFetching,
NextLineFetching,
Unknown,
HorizontalPhaseSize
} horizontalPhase;
Byte remainingCycles;
enum
{
Nametable,
Attribute,
PatternLow,
PatternHigh,
FetchingPhaseSize
} fetchingPhase;
////////////////////////////////////////
/// TILE DATA ///
////////////////////////////////////////
struct
{
Byte x;
Byte y;
} nametablePos;
Byte tilePosY;
struct
{
Byte nametable;
Byte attribute;
union
{
struct {
Byte lo;
Byte hi;
};
Word raw;
} tile;
} tileData;
struct FIFO16 loPatternFIFO;
struct FIFO16 hiPatternFIFO;
2021-10-29 20:33:34 +00:00
union OAMEntry* oam;
2021-10-23 17:30:02 +00:00
Word x, y;
2021-10-28 14:57:37 +00:00
struct Pixel* pixels;
SDL_Texture* screen;
2021-10-23 17:30:02 +00:00
struct Bus* bus;
};
struct PPU* createPPU(struct Bus* parent);
void destroyPPU(struct PPU* ppu);
Byte ppuRead(struct PPU* ppu, Word addr);
void ppuWrite(struct PPU* ppu, Word addr, Byte val);
2021-10-23 18:36:38 +00:00
int tickPPU(struct PPU* ppu);
2021-10-23 17:30:02 +00:00
2021-10-24 11:47:11 +00:00
SDL_Texture* getNameTableTexture(struct PPU* ppu, int index);
2021-10-28 14:57:37 +00:00
SDL_Texture* getScreenTexture(struct PPU* ppu);
2021-10-29 15:34:17 +00:00
SDL_Texture* getRenderedNameTableTexture(struct PPU* ppu, int index);
2021-10-24 11:47:11 +00:00
2021-10-29 20:33:34 +00:00
#endif // _PPU_H_