Loading...
Searching...
No Matches
main.c File Reference

HDMA-driven parallax scrolling with three speed zones. More...

#include <snes.h>

Functions

int main (void)
 Entry point: set up parallax scrolling with three speed zones.
 

Variables

u8 palette []
 16-color palette for the background
 
u8 scroll_table [10]
 HDMA scroll table in RAM (must be in bank $00 WRAM, < $2000).
 
u8 tilemap []
 64x32 tilemap data for the parallax background
 
u8 tiles []
 4bpp background tile data (defined in data.asm, stored in ROM)
 
u8 tiles_end []
 

Detailed Description

HDMA-driven parallax scrolling with three speed zones.

Demonstrates parallax scrolling using HDMA (H-Blank DMA) to override the BG1 horizontal scroll register (BG1HOFS, $210D) at different scanline boundaries. The screen is divided into three horizontal zones, each scrolling at a different speed to create the illusion of depth.

HDMA channel 6 writes a 2-byte scroll value (mode: 2-register) to BG1HOFS at each zone boundary. The scroll table lives in RAM so the main loop can increment each zone's offset at different rates every frame.

Ported from PVSnesLib ParallaxScrolling example.

SNES Concepts
  • HDMA (H-Blank DMA) for per-scanline register writes
  • BG horizontal scroll register (BG1HOFS, $210D)
  • RAM-based HDMA tables for dynamic per-frame updates
  • Parallax depth illusion via differential scroll speeds
What to Observe
  • Three horizontal bands scroll at different speeds automatically
  • Top (sky): slow, middle: medium, bottom (foreground): fast
  • No input required; the effect runs continuously
Modules Used
console, dma, background, sprite, hdma, input, math
See also
hdma.h, background.h, dma.h

Function Documentation

◆ main()

int main ( void  )

Entry point: set up parallax scrolling with three speed zones.

Loads a 64x32 tile background, configures HDMA to split the screen into three horizontal bands, and updates each band's scroll offset at a different rate every frame to create a parallax depth illusion.

Returns
Never returns (infinite loop).

< Enable only BG1 on the main screen

< Zone 1 (sky): 72 scanlines

< Initial horizontal scroll = 1 pixel

< Zone 2 (hills): 88 scanlines

< Initial horizontal scroll = 2 pixels

< Zone 3 (ground): 64 scanlines

< Initial horizontal scroll = 4 pixels

< End-of-table marker

< Sky: +1 pixel/frame (slow)

< Hills: +2 pixels/frame (medium)

< Ground: +4 pixels/frame (fast)

Variable Documentation

◆ palette

u8 palette[]
extern

16-color palette for the background

◆ scroll_table

u8 scroll_table[10]

HDMA scroll table in RAM (must be in bank $00 WRAM, < $2000).

This table defines three horizontal screen zones, each with an independent scroll offset. HDMA channel 6 reads this table during HBlank to override the BG1 horizontal scroll register (BG1HOFS, $210D) at zone boundaries.

Table format (mode: 2-register write to $210D/$210E): [line_count] [scroll_lo] [scroll_hi] – zone entry (3 bytes) ...repeated for each zone... [0x00] – end-of-table marker

The table lives in RAM (not ROM) because scroll values are updated every frame by the main loop. HDMA re-reads the table each frame automatically.

Total: 3 zones x 3 bytes + 1 terminator = 10 bytes.

◆ tilemap

u8 tilemap[]
extern

64x32 tilemap data for the parallax background

◆ tiles

u8 tiles[]
extern

4bpp background tile data (defined in data.asm, stored in ROM)

◆ tiles_end

u8 tiles_end[]