PPU

class PPU

This class represents the Picture Processing Unit of the GBA Functions in here are mostly inline utilities to make code inside renderers understandable.  There’s also the rendering timing idle.  (the real rendering is in the functions below)

See Also

Memory CPU MainWindow Tile4 Tile8 OAMSprites Palettes

Summary
PPUThis class represents the Picture Processing Unit of the GBA Functions in here are mostly inline utilities to make code inside renderers understandable.
Variables
mMemoryA pointer to the memory to read LCD registers, vram, oam and pram.
mCpuA pointer to the cpu for interrupts.
palettesA reinterpretation of pram to make things understandable.
spritesA reinterpretation of oam to make things understandable.
mBGOrderObsolete.
mCurrentPixelObsolete.
mHCOUNTKeeps track of Horizontal Count (which x is currently theoretically drawn).
Functions
CheckVMatchGenerates an interrupt if VCOUNT is equal to LYC.
PPUBuilds a PPU.
Variables
buffer_nulBackdrop buffer.
buffer_bg0Background 0 buffer.
buffer_bg1Background 1 buffer.
buffer_bg2Background 2 buffer.
buffer_bg3Background 3 buffer.
buffer_objSprites layer buffer.
buffer_obj_prioritySprites priorities buffer.
Functions
GetObjTilesMemoryUnimplemented.
GetMemoryThe memory of the GBA associated to the PPU.
GetVRamThe VRAM of the GBA associated to the PPU.
GetIORamThe IORam of the GBA associated to the PPU.
GetPalettesThe Palettes of the GBA associated to the PPU.
GetSpritesThe Sprites of the GBA associated to the PPU.
GetDISPSTATThe DISPSTAT register contains the LYC (vertical match register) and things like HBLANK flag, VBLANK flag, ....
GetDISPCNTThe DISPCNT register contains information about rendering (e.g.
GetBLDYEvyComputes Evy factor for backgrounds effects (from BLDY register).
GetSelectedSecondFrameFor Render modes 4-5.
GetFirstFrameThe first frame in rendering modes 3-4-5.
GetSecondFrameThe second frame in rendering modes 3-4-5.
GetBGModeContained in DISPCNT.
ClearHBlankClears HBLANK Flag.
ClearVBlankClears VBlank Flag.
GetVBlankTrue if VBlank Flag is set.
HBlankGenerates a HBlank interrupt if the environment allows that.
VBlankGenerates a VBlank interrupt.
DrawIdlePerforms PPU idle operations.
pack_rgbPacks 3 channels into a 16 bit RGBA_5_5_5_1 format integer.
render_sprite_rotscaleRenders a sprite (into sprites buffer) that has rotation/scale enabled.
render_sprite_fixedRenders a sprite (into sprites buffer) that has rotation/scale disabled.
render_spriteRenders a sprite (into sprites buffer).
render_objspritesRenders all the sprites of the ppu into its sprites buffer PPU.buffer_obj.
render_bg_textmode_tile4Renders a background with 16/16 palettes/color mode.
render_bg_textmode_tile8Renders a background with 256/1 colors/palette mode.
render_bg_textmodeRenders a background in text mode.
render_mode0Renders a background with rendering mode 0.
render_mode1UNIMPLEMENTED.
render_mode2UNIMPLEMENTED.
render_mode3UNIMPLEMENTED.
render_mode4Renders a background with rendering mode 4.
render_mode5UNIMPLEMENTED.
Variables
PPU_renderersPointers to the render functions.

Variables

mMemory

Memory* mMemory

A pointer to the memory to read LCD registers, vram, oam and pram.

mCpu

CPU* mCpu

A pointer to the cpu for interrupts.

palettes

Palettes* palettes

A reinterpretation of pram to make things understandable.

sprites

OAMSprites* sprites

A reinterpretation of oam to make things understandable.

mBGOrder

int mBGOrder[4]

Obsolete.  In the first renderers it contained background priorities.

mCurrentPixel

int mCurrentPixel

Obsolete.  In the first renderers pixels were computed singularly every 4 cycles.

mHCOUNT

int mHCOUNT

Keeps track of Horizontal Count (which x is currently theoretically drawn).

Functions

CheckVMatch

inline void CheckVMatch()

Generates an interrupt if VCOUNT is equal to LYC.

PPU

PPU(Memory *mem,
CPU *cpu,
Palettes *pal,
OAMSprites *spr)

Builds a PPU.

Parameters

memThe memory.
cpuThe cpu.
palThe pram.
sprThe oam.

Variables

buffer_nul

uint16_t buffer_nul[38400]

Backdrop buffer.

buffer_bg0

uint16_t buffer_bg0[38400]

Background 0 buffer.

buffer_bg1

uint16_t buffer_bg1[38400]

Background 1 buffer.

buffer_bg2

uint16_t buffer_bg2[38400]

Background 2 buffer.

buffer_bg3

uint16_t buffer_bg3[38400]

Background 3 buffer.

buffer_obj

uint16_t buffer_obj[38400]

Sprites layer buffer.

buffer_obj_priority

uint8_t buffer_obj_priority[38400]

Sprites priorities buffer.

Functions

GetObjTilesMemory

inline void* GetObjTilesMemory()

Unimplemented.  Useless anyway.

GetMemory

inline Memory* GetMemory()

Returns

The memory of the GBA associated to the PPU.

GetVRam

inline int8_t* GetVRam()

Returns

The VRAM of the GBA associated to the PPU.

GetIORam

inline ASICRegisters* GetIORam()

Returns

The IORam of the GBA associated to the PPU.

GetPalettes

inline Palettes* GetPalettes()

Returns

The Palettes of the GBA associated to the PPU.

GetSprites

inline OAMSprites* GetSprites()

Returns

The Sprites of the GBA associated to the PPU.

GetDISPSTAT

inline uint16_t GetDISPSTAT()

The DISPSTAT register contains the LYC (vertical match register) and things like HBLANK flag, VBLANK flag, ....

Returns

The DISPSTAT register value.

GetDISPCNT

inline uint16_t GetDISPCNT()

The DISPCNT register contains information about rendering (e.g. which backgrounds are enabled, if mosaic is enabled, if sprites are enabled, if in swap chained modes the back buffer becomes the front, if OAM tiles are 1D or 2D, ...)

Returns

The DISPCNT register value.

GetBLDYEvy

inline float GetBLDYEvy()

Computes Evy factor for backgrounds effects (from BLDY register).

Returns

Brigthness/darkness effect factor

GetSelectedSecondFrame

inline bool GetSelectedSecondFrame()

For Render modes 4-5.  These render modes use vram to store two full screen frames.  Then something similar to today’s swapchains swaps between frames This flag determines if the first or second frame has to be used (false=first).  The information is inside DISPCNT.

Returns

True if the second frame is selected, False otherwise.

GetFirstFrame

inline unsigned char* GetFirstFrame()

Returns

The first frame in rendering modes 3-4-5.  In mode 4-5 a frame is 40KB (really 37.5KB in mode4).  (1 byte per color (palette)) In mode 3 a frame is 75KB (2 bytes per color, raw description (RGBA 5-5-5-1))

GetSecondFrame

inline unsigned char* GetSecondFrame()

Returns

The second frame in rendering modes 3-4-5.  Mode 3 hasn’t a second frame.  In mode 4-5 a frame is 40KB (really 37.5KB in mode4).  (1 byte per color (palette)) In mode 3 a frame is 75KB (2 bytes per color, raw description (RGBA 5-5-5-1))

GetBGMode

inline int GetBGMode()

Contained in DISPCNT.

Returns

Current Render Mode.

ClearHBlank

inline void ClearHBlank()

Clears HBLANK Flag.

ClearVBlank

inline void ClearVBlank()

Clears VBlank Flag.

GetVBlank

inline bool GetVBlank()

Returns

True if VBlank Flag is set.

HBlank

inline void HBlank()

Generates a HBlank interrupt if the environment allows that.

VBlank

inline void VBlank()

Generates a VBlank interrupt.

DrawIdle

inline void DrawIdle(int count)

Performs PPU idle operations.

A pixel has a draw time of 4 cycles (238.xx ns (approx)) The screen is 240x160 with a vertical draw period of 197120 cycles and a vblanking of 83776, we have a total of 280896 cycles each frame, with 16.78Mhz it means we have 59.59ns per cycle and thus a wait time of 16.73859264 ms per frame -> 59.7421792 Frames Per Second (60 FPS approx).

Parameters

countNumber of cycles since last call.

pack_rgb

inline uint16_t pack_rgb(int r,
int g,
int b)

Packs 3 channels into a 16 bit RGBA_5_5_5_1 format integer.  Unpacking is defined as macro.  See unpack_rgb(p, r, g, b).

Parameters

rRed channel.
gGreen channel.
bBlue channel.

Returns

The packed 16bit value.

render_sprite_rotscale

inline void render_sprite_rotscale(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)

Renders a sprite (into sprites buffer) that has rotation/scale enabled.

Parameters

ppuThe ppu used for rendering.
spriteThe sprite that has to be rendered.
tilesMemPointer to the tiles memory.

render_sprite_fixed

inline void render_sprite_fixed(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)

Renders a sprite (into sprites buffer) that has rotation/scale disabled.

Parameters

ppuThe ppu used for rendering.
spriteThe sprite that has to be rendered.
tilesMemPointer to the tiles memory.

render_sprite

inline void render_sprite(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)

Renders a sprite (into sprites buffer).

Parameters

ppuThe ppu used for rendering.
spriteThe sprite that has to be rendered.
tilesMemPointer to the tiles memory.

render_objsprites

inline void render_objsprites(PPU *ppu,
void *tilesMem)

Renders all the sprites of the ppu into its sprites buffer PPU.buffer_obj.

Parameters

ppuThe ppu used for rendering.
tilesMemPointer to the tiles memory.

render_bg_textmode_tile4

inline void render_bg_textmode_tile4(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)

Renders a background with 16/16 palettes/color mode.  Text Mode (no rotation).

Parameters

ppuThe ppu used for rendering.
bgcntBackground control register value.
bghofsBackground horizontal scroll.
bgvofsBackground vertical scroll.
outBufferOutput buffer.

render_bg_textmode_tile8

inline void render_bg_textmode_tile8(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)

Renders a background with 256/1 colors/palette mode.  Text Mode (no rotation)

Parameters

ppuThe ppu used for rendering.
bgcntBackground control register value.
bghofsBackground horizontal scroll.
bgvofsBackground vertical scroll.
outBufferOutput buffer.

render_bg_textmode

inline void render_bg_textmode(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)

Renders a background in text mode.

Parameters

ppuThe ppu used for rendering.
bgcntBackground control register value.
bghofsBackground horizontal scroll.
bgvofsBackground vertical scroll.
outBufferOutput buffer.

render_mode0

void render_mode0(PPU *ppu,
unsigned char *outBuffer)

Renders a background with rendering mode 0.

Rendering mode 0 supports all backgrounds, 1024 tiles, 16/16 and 256/1 colors mode, Scrolling, Flip, Mosaic, and Alpha blending effects.

It doesn’t support rotation/scaling.

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

render_mode1

void render_mode1(PPU *ppu,
unsigned char *outBuffer)

UNIMPLEMENTED.  Renders a background with rendering mode 1.

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

render_mode2

void render_mode2(PPU *ppu,
unsigned char *outBuffer)

UNIMPLEMENTED.  Renders a background with rendering mode 2.

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

render_mode3

void render_mode3(PPU *ppu,
unsigned char *outBuffer)

UNIMPLEMENTED.  Renders a background with rendering mode 3.

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

render_mode4

void render_mode4(PPU *ppu,
unsigned char *outBuffer)

Renders a background with rendering mode 4.

In render mode 4 there’s only a big frame, 32768 colors available, rotation/scaling/mosaic/brightness/darkness effects available.  The frame size is fullscreen (240x160).

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

render_mode5

void render_mode5(PPU *ppu,
unsigned char *outBuffer)

UNIMPLEMENTED.  Renders a background with rendering mode 5.

Parameters

ppuThe ppu used for rendering.
outBufferOutput buffer.

Variables

PPU_renderers

extern void (*PPU_renderers[6])(PPU*,unsigned char*)

Pointers to the render functions.

class PPU
This class represents the Picture Processing Unit of the GBA Functions in here are mostly inline utilities to make code inside renderers understandable.
Memory* mMemory
A pointer to the memory to read LCD registers, vram, oam and pram.
CPU* mCpu
A pointer to the cpu for interrupts.
Palettes* palettes
A reinterpretation of pram to make things understandable.
OAMSprites* sprites
A reinterpretation of oam to make things understandable.
int mBGOrder[4]
Obsolete.
int mCurrentPixel
Obsolete.
int mHCOUNT
Keeps track of Horizontal Count (which x is currently theoretically drawn).
inline void CheckVMatch()
Generates an interrupt if VCOUNT is equal to LYC.
PPU(Memory *mem,
CPU *cpu,
Palettes *pal,
OAMSprites *spr)
Builds a PPU.
uint16_t buffer_nul[38400]
Backdrop buffer.
uint16_t buffer_bg0[38400]
Background 0 buffer.
uint16_t buffer_bg1[38400]
Background 1 buffer.
uint16_t buffer_bg2[38400]
Background 2 buffer.
uint16_t buffer_bg3[38400]
Background 3 buffer.
uint16_t buffer_obj[38400]
Sprites layer buffer.
uint8_t buffer_obj_priority[38400]
Sprites priorities buffer.
inline void* GetObjTilesMemory()
Unimplemented.
inline Memory* GetMemory()
The memory of the GBA associated to the PPU.
inline int8_t* GetVRam()
The VRAM of the GBA associated to the PPU.
inline ASICRegisters* GetIORam()
The IORam of the GBA associated to the PPU.
inline Palettes* GetPalettes()
The Palettes of the GBA associated to the PPU.
inline OAMSprites* GetSprites()
The Sprites of the GBA associated to the PPU.
inline uint16_t GetDISPSTAT()
The DISPSTAT register contains the LYC (vertical match register) and things like HBLANK flag, VBLANK flag, ....
inline uint16_t GetDISPCNT()
The DISPCNT register contains information about rendering (e.g.
inline float GetBLDYEvy()
Computes Evy factor for backgrounds effects (from BLDY register).
inline bool GetSelectedSecondFrame()
For Render modes 4-5.
inline unsigned char* GetFirstFrame()
The first frame in rendering modes 3-4-5.
inline unsigned char* GetSecondFrame()
The second frame in rendering modes 3-4-5.
inline int GetBGMode()
Contained in DISPCNT.
inline void ClearHBlank()
Clears HBLANK Flag.
inline void ClearVBlank()
Clears VBlank Flag.
inline bool GetVBlank()
True if VBlank Flag is set.
inline void HBlank()
Generates a HBlank interrupt if the environment allows that.
inline void VBlank()
Generates a VBlank interrupt.
inline void DrawIdle(int count)
Performs PPU idle operations.
inline uint16_t pack_rgb(int r,
int g,
int b)
Packs 3 channels into a 16 bit RGBA_5_5_5_1 format integer.
inline void render_sprite_rotscale(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)
Renders a sprite (into sprites buffer) that has rotation/scale enabled.
inline void render_sprite_fixed(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)
Renders a sprite (into sprites buffer) that has rotation/scale disabled.
inline void render_sprite(PPU *ppu,
OAMSprite *sprite,
void *tilesMem)
Renders a sprite (into sprites buffer).
inline void render_objsprites(PPU *ppu,
void *tilesMem)
Renders all the sprites of the ppu into its sprites buffer PPU.buffer_obj.
inline void render_bg_textmode_tile4(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)
Renders a background with 16/16 palettes/color mode.
inline void render_bg_textmode_tile8(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)
Renders a background with 256/1 colors/palette mode.
inline void render_bg_textmode(PPU *ppu,
uint16_t bgcnt,
uint16_t bghofs,
uint16_t bgvofs,
uint16_t *outBuffer)
Renders a background in text mode.
void render_mode0(PPU *ppu,
unsigned char *outBuffer)
Renders a background with rendering mode 0.
void render_mode1(PPU *ppu,
unsigned char *outBuffer)
UNIMPLEMENTED.
void render_mode2(PPU *ppu,
unsigned char *outBuffer)
UNIMPLEMENTED.
void render_mode3(PPU *ppu,
unsigned char *outBuffer)
UNIMPLEMENTED.
void render_mode4(PPU *ppu,
unsigned char *outBuffer)
Renders a background with rendering mode 4.
void render_mode5(PPU *ppu,
unsigned char *outBuffer)
UNIMPLEMENTED.
extern void (*PPU_renderers[6])(PPU*,unsigned char*)
Pointers to the render functions.
struct Memory
Represents the entire address space of the GBA.
class CPU
This class represents an ARM7TDMI CPU running at 16.78MHZ with 32 bit registers and two execution modes (ARM32bit, THUMB16bit).
class MainWindow
Represents an openGL window that displays the GBA video output.
struct Tile4
32 bytes Tile, 4 bit depth.
struct Tile8
64 bytes Tile, 8 bit depth.
class OAMSprites
Builds and handles all OAMSprites and OAMTransformGroups contained in OAM memory.
class Palettes
Represents the full set of palettes.
Close