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

Mode 7 perspective with HDMA split-screen (F-Zero style) More...

#include <snes.h>

Functions

void asm_loadSkyData (void)
 Load sky background tiles and tilemap to VRAM via assembly DMA.
 
void asm_setupHdmaPerspective (u16 sx, u16 sy)
 Build and activate the 4-channel HDMA perspective split.
 
int main (void)
 Entry point – F-Zero-style Mode 7 perspective with HDMA split-screen.
 

Variables

u8 ground_map []
 Mode 7 ground tilemap (128x128 entries, one byte per tile index).
 
u8 ground_map_end []
 
u8 ground_pal []
 256-color palette for the ground plane (512 bytes).
 
u8 ground_pal_end []
 
u8 ground_tiles []
 Mode 7 ground tile pixel data (256 tiles, 64 bytes each = 16KB).
 
u8 ground_tiles_end []
 
u16 pad0
 
u16 sx
 
u16 sy
 

Detailed Description

Mode 7 perspective with HDMA split-screen (F-Zero style)

Creates an F-Zero-style pseudo-3D ground plane by combining two BG modes on a single screen using HDMA mid-frame register switching. The top portion (96 scanlines) displays a Mode 3 sky background, while the bottom portion (128 scanlines) renders a Mode 7 ground plane with per-scanline perspective scaling. Four HDMA channels cooperate each frame: one switches BGMODE from Mode 3 to Mode 7 at the split line, another toggles the main screen layer enable (BG2 for sky, BG1 for ground), and two more write per-scanline M7A and M7D values to create the perspective foreshortening effect where distant rows appear more compressed.

SNES Concepts
  • Mid-frame BG mode switching via HDMA (Mode 3 sky + Mode 7 ground)
  • Per-scanline M7A/M7D writes for perspective projection
  • HDMA-driven main screen layer enable toggling
  • Mode 7 ground plane scrolling with D-pad input
  • Multi-channel HDMA coordination (4 channels synchronized)
What to Observe
  • The screen is split: a flat sky image on top and a perspective ground below
  • Press D-pad to scroll the ground plane in any direction
  • Distant ground rows appear more compressed (perspective foreshortening)
Modules Used
console, dma, background, sprite, input, mode7
See also
mode7.h, hdma.h, background.h, input.h

Function Documentation

◆ asm_loadSkyData()

void asm_loadSkyData ( void  )
extern

Load sky background tiles and tilemap to VRAM via assembly DMA.

Loads the Mode 3 sky image (tiles at VRAM $5000, tilemap at VRAM $4000). Uses assembly because the data may span multiple ROM banks (SUPERFREE sections), requiring correct bank byte handling that the C-level dmaCopyVram() cannot provide.

Must be called during forced blank.

◆ asm_setupHdmaPerspective()

void asm_setupHdmaPerspective ( u16  sx,
u16  sy 
)
extern

Build and activate the 4-channel HDMA perspective split.

Configures four HDMA channels that cooperate each frame:

  • Channel A: Switches BGMODE register from Mode 3 (sky) to Mode 7 (ground) at the split scanline (scanline 96).
  • Channel B: Toggles TM (main screen enable) from BG2 (sky) to BG1 (ground).
  • Channel C: Writes per-scanline M7A values for horizontal perspective scaling.
  • Channel D: Writes per-scanline M7D values for vertical perspective scaling.

The perspective tables compress distant rows and expand near rows, creating the illusion of a 3D ground plane receding into the horizon. The scroll offsets (sx, sy) control the camera position on the Mode 7 ground.

Parameters
sxHorizontal scroll offset for the Mode 7 ground plane.
syVertical scroll offset for the Mode 7 ground plane.

◆ main()

int main ( void  )

Entry point – F-Zero-style Mode 7 perspective with HDMA split-screen.

Sets up a dual-mode display: the top 96 scanlines render a Mode 3 sky background (BG2), while the bottom 128 scanlines render a Mode 7 ground plane (BG1) with per-scanline perspective scaling via HDMA. The D-pad scrolls the ground plane. HDMA tables are rebuilt every frame by the assembly helper to reflect the current scroll position.

Returns
Does not return (infinite loop).

Variable Documentation

◆ ground_map

u8 ground_map[]
extern

Mode 7 ground tilemap (128x128 entries, one byte per tile index).

Mode 7 tilemaps are 128x128 bytes. Each byte is a tile index (0-255) referencing the 8bpp tile set. Loaded to VRAM via dmaCopyVramMode7() which handles the interleaved format (map in low bytes, tiles in high bytes).

◆ ground_map_end

u8 ground_map_end[]

◆ ground_pal

u8 ground_pal[]
extern

256-color palette for the ground plane (512 bytes).

◆ ground_pal_end

u8 ground_pal_end[]

◆ ground_tiles

u8 ground_tiles[]
extern

Mode 7 ground tile pixel data (256 tiles, 64 bytes each = 16KB).

Each tile is 8x8 pixels at 8bpp (one byte per pixel, 64 bytes/tile). Written to the high bytes of VRAM words by dmaCopyVramMode7().

◆ ground_tiles_end

u8 ground_tiles_end[]

◆ pad0

u16 pad0

Current joypad button state

◆ sx

u16 sx

Mode 7 ground horizontal scroll offset (camera X)

◆ sy

u16 sy

Mode 7 ground vertical scroll offset (camera Y)