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

Static 16-color background display in Mode 1 (4bpp). More...

#include <snes.h>
#include <snes/asset.h>
#include <snes/lzss.h>
#include "res/street.inc"
#include "res/street_lz.inc"

Macros

#define MAP_ADDR   0x7800
 VRAM word address of the tilemap, after both copies of the tiles.
#define TILES_LZ   0x4000
#define TILES_PLAIN   0x0000
 VRAM word address of the plain tiles (bgLoad) and of the decompressed copy.

Functions

int main (void)
 Entry point – load a 4bpp tileset and display a static Mode 1 image.

Detailed Description

Static 16-color background display in Mode 1 (4bpp).

Displays a single full-screen tiled background using Mode 1, the most commonly used SNES video mode. Mode 1 provides two 4bpp layers (BG1 and BG2, each up to 16 colors) plus one 2bpp layer (BG3, 4 colors). This example uses only BG1: a rainy street at night, 16 colours, converted from a PNG by the build (opensnes-tileset and the settings file beside it).

The same picture is also linked a second time, LZ77-compressed by the build (lz = true in res/street_lz.png.toml: 28256 bytes of tiles become 18816), and decompressed at boot straight into VRAM by lzssDecodeVram(). A and B point BG1 at one copy or the other: the screen does not change, and that is the lesson — same tiles, two thirds of the ROM. A detailed picture compresses less than a flat one: LZ77 finds repeats, and every tile of a painting is a little different.

This began as the PVSnesLib "Mode1" and "Mode1LZ77" examples.

SNES Concepts
  • Mode 1: BG1/BG2 are 4bpp (16 colors each), BG3 is 2bpp (4 colors)
  • bgInitTileSet() for combined tile + palette loading with VRAM address configuration
  • dmaCopyVram() for tilemap data transfer to a separate VRAM region
  • VRAM layout: tiles at $0000, the decompressed copy at $4000, tilemap at $7800 (each region in its own range)
  • Force blank (setScreenOff) during VRAM setup to avoid mid-frame writes
  • LZ77-compressed tiles: lzssDecodeVram() decodes from ROM into VRAM
  • bgSetGfxPtr(): a background's character base is one register, switched at any time
What to Observe
  • A static full-screen image rendered as 8x8 tiles on BG1: 883 distinct tiles, close to the 1024 a tilemap entry can number
  • Only BG1 is enabled; BG2 and BG3 are unused
  • The image remains stationary (no scrolling)
  • A shows BG1 from the decompressed copy at VRAM $4000, B from the plain one at $0000: the picture is the same pixel for pixel
Modules Used
console, sprite, dma, background, asset, lzss, input
See also
background.h, lzss.h, dma.h, video.h

Macro Definition Documentation

◆ MAP_ADDR

#define MAP_ADDR   0x7800

VRAM word address of the tilemap, after both copies of the tiles.

◆ TILES_LZ

#define TILES_LZ   0x4000

◆ TILES_PLAIN

#define TILES_PLAIN   0x0000

VRAM word address of the plain tiles (bgLoad) and of the decompressed copy.

Function Documentation

◆ main()

int main ( void )

Entry point – load a 4bpp tileset and display a static Mode 1 image.

Demonstrates the typed asset bundle: tileset, palette, tilemap, color depth and map size all travel together in a single BgAsset. A single bgLoad() does the same work as a manual bgSetMapPtr + bgInitTileSet + dmaCopyVram triple, with the color-mode / size pairing locked at declaration time.

The picture has 883 distinct tiles (28 KB at 4bpp), and the VRAM holds it twice, so the layout is tight: the plain tiles at $0000, the decompressed copy at $4000 (a character base is a multiple of $1000 words), and the tilemap after both at $7800.

Returns
Never returns (infinite loop).