OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

HDMA-driven backdrop gradients: a direct table, and an indirect one. More...

#include <snes.h>
#include <snes/hdma.h>
#include "res/logo.inc"

Macros

#define BAND_LINES   7
#define NBANDS   32
 Bands of the indirect gradient, and scanlines per band (32 x 7 = 224).

Functions

static void buildIndirect (void)
 Build the indirect gradient's payloads and pointer table, once.
void disableGradient (void)
 Disable the HDMA gradient and restore a flat backdrop color.
void enableGradient (void)
 Enable the HDMA backdrop color gradient effect.
void enableIndirect (void)
 Switch channel 6 to the indirect gradient.
int main (void)
 Entry point – HDMA-driven per-scanline backdrop color gradient.

Variables

static u8 band_data [32][4]
 The bands' payloads: [CGADD][CGADD][color_lo][color_hi] each.
u8 hdmaGradientList []
 4bpp tile data for the background layer.
static u8 itable [32 *3+1]
 The indirect table: [count][ptr_lo][ptr_hi] per band + terminator.
u16 pad0
u8 pada
u8 padb
u8 padx

Detailed Description

HDMA-driven backdrop gradients: a direct table, and an indirect one.

Uses HDMA to rewrite CGRAM color 0 (the backdrop color) on every scanline group, creating a smooth vertical color gradient behind the background layer. HDMA channel 6 is configured in mode 3 (2REG_2X), which writes 4 bytes per entry to consecutive register pairs: two bytes to CGADD ($2121) to select color index 0, then two bytes to CGDATA ($2122) to write the 15-bit BGR color value. A pre-built HDMA table in ROM defines the gradient colors for each scanline group: the city's sky at dusk, behind the OpenSNES logo. This technique was widely used in SNES games for sky gradients, underwater tinting, and lava glow effects without consuming any CPU time.

Press X for the same effect through INDIRECT HDMA: the table holds [count][pointer] entries and the PPU fetches each band's 4-byte payload through the pointer ($43x7 selects its bank). The payloads live in a WRAM array the CPU can rewrite freely — the layout of dynamic per-scanline palettes. The indirect gradient is krom (Peter Lemon)'s "Red Space Indirect HDMA" (32 red bands of 7 lines), ported to C.

SNES Concepts
  • HDMA mode 3 (2REG_2X) targeting CGADD + CGDATA
  • Per-scanline CGRAM color modification (color 0 = backdrop)
  • HDMA table format: [count] [CGADD_lo] [CGADD_hi] [color_lo] [color_hi]
  • Runtime HDMA enable/disable toggling
  • INDIRECT HDMA (DMAP bit 6): [count][ptr16] entries, the payload's bank in $43x7, which hdmaSetupIndirect() writes; a count of 7 fetches the payload once and holds it for 7 lines
  • A C pointer's bank for $43x7: (u8)((u32)(void *)data >> 16)
What to Observe
  • A dusk gradient (night, violet, magenta, amber) behind Kit and the wordmark
  • Press A to disable the gradient (flat backdrop color returns)
  • Press B to re-enable the gradient effect
  • Press X for the indirect gradient: 32 red bands, brightest at the top
Modules Used
console, dma, background, sprite, hdma, input
See also
hdma.h, background.h, input.h, video.h

Macro Definition Documentation

◆ BAND_LINES

#define BAND_LINES   7

◆ NBANDS

#define NBANDS   32

Bands of the indirect gradient, and scanlines per band (32 x 7 = 224).

Function Documentation

◆ buildIndirect()

void buildIndirect ( void )
static

Build the indirect gradient's payloads and pointer table, once.

A red ramp $1F..$00 (BGR555: red is bits 0-4, so the high byte stays 0); each band's entry points at its payload and holds it for BAND_LINES lines.

◆ disableGradient()

void disableGradient ( void )

Disable the HDMA gradient and restore a flat backdrop color.

Stops all HDMA channels. After stopping, CGRAM color 0 retains the last value written by HDMA (typically the color at the bottom of the gradient). The next VBlank's palette reload (if any) would restore the original color.

◆ enableGradient()

void enableGradient ( void )

Enable the HDMA backdrop color gradient effect.

Configures HDMA channel 6 in mode 3 (2REG_2X) targeting CGADD ($2121). In this mode, HDMA writes 4 bytes per scanline to two consecutive register pairs: the first two bytes go to $2121 (CGADD, selecting palette index 0), and the next two bytes go to $2122 (CGDATA, writing the 15-bit BGR color).

The net effect: on every scanline group boundary, CGRAM color 0 (the backdrop/transparent color) is overwritten with a new gradient color. Since CGRAM persists until the next write, each color covers its group of scanlines, producing a vertical gradient behind all BG layers.

This technique costs zero CPU time – HDMA runs entirely via DMA hardware.

◆ enableIndirect()

void enableIndirect ( void )

Switch channel 6 to the indirect gradient.

Same channel, same registers (2REG_2X to CGADD); the table is the pointer table, and the payloads' bank goes in $43x7.

◆ main()

int main ( void )

Entry point – HDMA-driven per-scanline backdrop color gradient.

Loads a background and enables an HDMA-driven vertical color gradient on CGRAM color 0 (the backdrop). The A button disables the gradient (returning to a flat backdrop), and the B button re-enables it. Edge-detection flags (pada/padb) prevent repeated toggles while a button is held down.

Returns
Does not return (infinite loop).

Variable Documentation

◆ band_data

u8 band_data[32][4]
static

The bands' payloads: [CGADD][CGADD][color_lo][color_hi] each.

◆ hdmaGradientList

u8 hdmaGradientList[]
extern

4bpp tile data for the background layer.

Tilemap data (32x32 tile grid) for the background layer.

15-bit BGR palette for the background (up to 16 colors).

Pre-built HDMA table for the backdrop color gradient (defined in data.asm).

This table is consumed by HDMA channel 6 in mode 3 (2REG_2X), targeting CGADD ($2121). Each entry is 5 bytes:

  • Byte 0: scanline count (with bit 7 = repeat flag)
  • Bytes 1-2: written to CGADD (always 0x0000 to select color index 0)
  • Bytes 3-4: written to CGDATA (15-bit BGR color for this scanline group)

The table ends with a 0x00 terminator byte. HDMA processes this table automatically every frame without CPU intervention, rewriting the backdrop color at each scanline group boundary to produce a smooth vertical gradient.

◆ itable

u8 itable[32 *3+1]
static

The indirect table: [count][ptr_lo][ptr_hi] per band + terminator.

◆ pad0

u16 pad0

Current joypad button state

◆ pada

u8 pada

Edge-detection flag for A button (prevents repeat triggers while held)

◆ padb

u8 padb

Edge-detection flag for B button

◆ padx

u8 padx

Edge-detection flag for X button