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

HiROM memory mapping mode with 64KB bank access. More...

#include <snes.h>
#include <snes/console.h>
#include <snes/dma.h>
#include <snes/input.h>

Macros

#define FONT_SIZE   (11 * 16)
 Total font data size in bytes (11 characters x 16 bytes per 2bpp tile)
 
#define TILE_B   9
 
#define TILE_D   6
 
#define TILE_E   7
 
#define TILE_H   1
 
#define TILE_I   2
 
#define TILE_L   8
 
#define TILE_M   5
 
#define TILE_O   4
 
#define TILE_PLUS   10
 
#define TILE_R   3
 
#define TILE_SPACE   0
 Tile index for space (blank tile)
 
#define TILEMAP_ADDR   0x0400
 VRAM word address for the BG1 tilemap (32x32 = 2KB)
 
#define TILES_ADDR   0x0000
 VRAM word address for the tile character data.
 

Functions

static void clear_tilemap (void)
 Clear the entire BG1 tilemap to blank (space) tiles.
 
int main (void)
 Main entry point – HiROM mode validation demo.
 
static void write_tile (u8 x, u8 y, u8 tile)
 Write a single tile entry to the BG1 tilemap via direct VRAM registers.
 

Variables

static const u8 font_tiles []
 Inline 2bpp font tile data (11 characters, 16 bytes per tile).
 
static const u8 init_palette []
 Initial 2-color palette (dark blue background, white text).
 

Detailed Description

HiROM memory mapping mode with 64KB bank access.

Demonstrates building an OpenSNES ROM in HiROM mode, where each ROM bank provides 64KB of contiguous address space ($0000-$FFFF) instead of LoROM's 32KB ($8000-$FFFF). HiROM is selected by setting USE_HIROM := 1 in the Makefile, which switches the ROM header, memory map, and linker configuration.

HiROM is preferred for games with large contiguous data (streaming tilesets, large lookup tables) because it avoids the 32KB bank boundary that LoROM imposes. The tradeoff is that mirror-region access patterns differ, and some addressing modes behave differently.

This example uses inline 2bpp font tiles (no external assets) and direct VRAM register writes to display text, confirming that the library, ROM header, and memory map are all correctly configured for HiROM operation. Pressing A changes the background color as a simple input validation.

SNES Concepts
  • HiROM vs LoROM memory mapping (64KB vs 32KB banks)
  • ROM header configuration for HiROM (USE_HIROM flag)
  • Mode 0 with 2bpp inline font tiles
  • Direct VRAM register writes for tilemap text rendering
  • CGRAM palette writes for runtime color changes
What to Observe
  • "HIROM MODE" and "+ LIB" text displayed on a dark blue background
  • Hold A to change the background color to light blue
  • Release A to restore the dark blue background
  • If the text appears garbled, the HiROM configuration has an issue
Modules Used
console, dma, input, sprite, background
See also
console.h, dma.h, input.h

Macro Definition Documentation

◆ FONT_SIZE

#define FONT_SIZE   (11 * 16)

Total font data size in bytes (11 characters x 16 bytes per 2bpp tile)

◆ TILE_B

#define TILE_B   9

Tile index for letter B

◆ TILE_D

#define TILE_D   6

Tile index for letter D

◆ TILE_E

#define TILE_E   7

Tile index for letter E

◆ TILE_H

#define TILE_H   1

Tile index for letter H

◆ TILE_I

#define TILE_I   2

Tile index for letter I

◆ TILE_L

#define TILE_L   8

Tile index for letter L

◆ TILE_M

#define TILE_M   5

Tile index for letter M

◆ TILE_O

#define TILE_O   4

Tile index for letter O

◆ TILE_PLUS

#define TILE_PLUS   10

Tile index for plus sign (+)

◆ TILE_R

#define TILE_R   3

Tile index for letter R

◆ TILE_SPACE

#define TILE_SPACE   0

Tile index for space (blank tile)

◆ TILEMAP_ADDR

#define TILEMAP_ADDR   0x0400

VRAM word address for the BG1 tilemap (32x32 = 2KB)

◆ TILES_ADDR

#define TILES_ADDR   0x0000

VRAM word address for the tile character data.

Function Documentation

◆ clear_tilemap()

static void clear_tilemap ( void  )
static

Clear the entire BG1 tilemap to blank (space) tiles.

Writes 1024 tilemap entries (32x32) with tile index 0 (space) and zero attributes. Must be called during force blank or VBlank.

◆ main()

int main ( void  )

Main entry point – HiROM mode validation demo.

Verifies that the HiROM memory map, ROM header, and library all work correctly by displaying text and responding to input. The display is set up with inline 2bpp font tiles written directly to VRAM registers, proving that code and data addresses resolve properly in HiROM's 64KB-per-bank layout.

Holding the A button changes the backdrop color as a simple demonstration that the library's input system works in HiROM mode.

Returns
0 (never reached – infinite game loop)

< Tile index for letter H

< Tile index for letter I

< Tile index for letter R

< Tile index for letter O

< Tile index for letter M

< Tile index for letter M

< Tile index for letter O

< Tile index for letter D

< Tile index for letter E

< Tile index for plus sign (+)

< Tile index for letter L

< Tile index for letter I

< Tile index for letter B

◆ write_tile()

static void write_tile ( u8  x,
u8  y,
u8  tile 
)
static

Write a single tile entry to the BG1 tilemap via direct VRAM registers.

Sets the VRAM address to the tilemap position (row * 32 + column) and writes the tile index as a 16-bit tilemap entry (low = tile number, high = attributes). VRAM writes only succeed during VBlank or forced blank – calling this during active display will silently fail.

Parameters
xColumn position in the 32-wide tilemap (0-31)
yRow position in the 32-tall tilemap (0-31)
tileTile character index to write

Variable Documentation

◆ font_tiles

const u8 font_tiles[]
static

Inline 2bpp font tile data (11 characters, 16 bytes per tile).

Each 2bpp tile is 8x8 pixels stored as 16 bytes: 8 rows of 2 bytes each (one byte per bitplane). Only 2 colors are used: color 0 (transparent/background) and color 1 (text). This avoids needing an external font asset file – the entire character set is embedded in the C source.

◆ init_palette

const u8 init_palette[]
static
Initial value:
= {
0x00, 0x28,
0xFF, 0x7F,
}

Initial 2-color palette (dark blue background, white text).

SNES CGRAM stores BGR555 colors as 2 bytes each (little-endian). Color 0 = backdrop (dark blue), Color 1 = text foreground (white).