🎨

GBA Basics

TypeQuiz 4 Material

Notes

GBA - Game Boy Advance emulator

GBA graphics

Pixel: 16 bit value

Each color is allotted 5 bits, intensity from 0 - 31

Bit 15 is unused

Video Buffer

Video buffer is a one dimensional array that tells GBA hardware the value to display on the screen

Indexing into a specific (row, col) coordinate

Offset Calculation:

unit16_t videobuffer[];

videoBuffer(row, col) = videobuffer + (row * WIDTH + col)

WIDTH is the length of each row(the width of the screen)

Can use Macro to simplify this calculation

GBA Draw Cycle

GBA alternates between: VDraw and VBlank

VDraw:

  • GBA copies one row of pixels at a time from the video buffer to the screen
  • VDraw is not instantaneous. halfway through VDraw, only half the scanlines have been drawn

VBlank: Nothing Happens

Tearing

the video buffer is updated during VDraw, causing the top half of the screen to show the old image and the bottom half to show the new image.

If we update the video buffer during VBlank, no tearing will happen, because the screen is not updated during VBlank.

Synchronizing Drawing and Logic

The GBA exposes the scanline counter as a memory- mapped device register; it can be read using the macro defined below.

The scanline counter indicates the current row of pixels being drawn at the screen (screen height: 160)

#define SCANLINECOUNTER *(volatile unsigned short *)0x4000006

Implementing waitForVBlank

SCANLINECOUNTER > 160 —> thee GBA is in VBlank

Two issues:

Use waitForVBlank()

However, VBlank cannot fully eliminate tearing — Only about 1600 pixels updates can be drawn during a single VBlank phase. Filling the entire screen (38400 pixels) takes several cycles, guaranteeing tearing.

Direct Memory Access(DMA)

DMA optimizes large array copy, can drawing without tearing(drawing is array copy)

DMA is not connected to the CPU. However, when DMA operates, it takes cycles from the CPU and writes to specific memory locations, then returns control to the CPU once finished.

There are 12 DMA registers and 4 channels. Each channel has a source, destination, and control register.

Channel 3 - General Purpose Register

DMA Source and Destination Registers

Both Source and Destination Registers contain short pointers.

Destination Register:

Source Register:

DMA Control Registers

The control register sets:

Iteratee through destination/source

Use DMA in code

DMA_Controller struct with the 3 registers:

typedef struct

{

const volatile void *src;

const volatile void *dst;

u32. cnt;

}

DMA[3] is used to access channel 3

#define DMA ((volatile DMA_CONTROLLER *) 0x040000B0)

GBA Program as a State Machine

Buttons

In this register, bits 15–10 are unused, and each button is assigned a

single bit.

The key states are low-active, meaning their bit is cleared when the button is pressed and set when it is released.

Questions & Answers



GBA Basics

buffer to the screen.

the scanlines have been drawn.

because the screen is not updated during VBlank