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

Eight-direction scrolling over a map of 1,433 distinct characters. More...

#include <snes.h>
#include <snes/tilering.h>
#include <snes/vramqueue.h>
#include "res/archipelago.inc"

Classes

struct  Leg
 One leg: where the camera goes, and how fast (pixels a frame on each axis). More...

Macros

#define CAM_MAX_X   (archipelago_WIDTH * 8 - 256)
 Camera limits: the map less one screen.
#define CAM_MAX_Y   (archipelago_HEIGHT * 8 - 224)
 Camera limits: the map less one screen (224 lines).
#define CHR_VRAM   0x0000
 The map (opensnes-tilering, from res/archipelago.png and its settings file).
#define LEGS   (sizeof(course) / sizeof(course[0]))
 Legs in the course.
#define MAP_VRAM   0x3000
 BG1 tilemap, 64x32 entries (4 KB), above the slots.
#define RING_SLOTS   640
 Slots in the ring: the worst screen (389) and room to keep characters for a return.

Functions

static u16 approach (u16 v, u16 target, u16 step)
 Move v toward target by at most step.
int main (void)
 Entry point — the archipelago, flown over.
static u16 scanline (void)
 The scanline now, from the PPU's latched counter.

Variables

u16 camx
 Camera position in map pixels.
u16 camy
FAR u16 char_of [640]
 The character of each slot.
static const Leg course []
 The course, from the bottom-left corner and back to it.
u16 leg
 The leg being flown.
u16 manual
 1 while the D-pad drives the camera
FAR u8 refs [640]
 Cells of the window showing each slot.
TileRing ring
 The ring's state (plain RAM).
FAR u16 slot_of [archipelago_CHARS]
 The slot of each map character.
FAR u8 staging [(((33)+(29)) *32)]
 The frame's new characters, copied from ROM, sent by one or two DMAs.
u16 tr_cost_55
u16 tr_cost_7
 The worst frame the module predicts for the course's fastest legs (7 px, and 5 + 5).
u16 tr_direct
 1 = direct mode (no staging buffer); a test pokes it before main
u16 tr_frames
 Frames of the first lap, and laps completed.
u16 tr_laps
u16 tr_max_chars
 Most characters one frame queued (first lap).
u16 tr_max_cost
 Most vramQueueCost() before a flush, in bytes (first lap; x 8 = master cycles).
u16 tr_max_entries
 Most queue entries one frame noted (first lap).
u16 tr_max_lines
 The latest scanline a flush ended on, counted from the start of VBlank (line 225).
u16 tr_max_resident
 Most slots in use at once (first lap).
u16 tr_refused
u16 tr_starved
 Characters that found no free slot, transfers the queue refused: 0 and 0.
u16 tr_total_chars
 Characters loaded over the first lap.
static const TileRingMap world = archipelago_TILERING_MAP
 What the converter wrote, in one structure.

Detailed Description

Eight-direction scrolling over a map of 1,433 distinct characters.

An archipelago of 1280x896 pixels, 160x112 characters, drawn with 1,433 distinct 8x8 characters once mirrored copies are merged: more than the 1,024 a tilemap entry can name, so the whole set never fits VRAM. The screen never shows more than 389 at once (the converter measured every position), and the tilering module keeps a ring of 640 character slots in VRAM: as the camera moves it loads the characters of the column and the row that come into view, and frees the slots of those that leave.

The camera flies a fixed course on its own — up, across, down at a diagonal, at 3 to 7 pixels a frame — so that a test sees the same frames every time; the D-pad takes over (B held: fast), START gives the course back. The figures the course produced are kept in tr_* variables for the test (testing/manifests/scrolling_tile_ring*.toml, testing/tile_ring_budget.py).

The map is invented: devtools/art_assets/tile_ring.py draws it from a fixed seed.

SNES Concepts
  • A tilemap entry names 1,024 characters: a large map needs streaming
  • A ring of character slots, freed by reference count, refilled in sequence
  • A 64x32 tilemap as a window that follows the camera on both axes
  • VRAM transfers noted during the frame, sent in VBlank (vramqueue)
  • The VBlank budget: what one frame of scrolling costs, NTSC and PAL
What to Observe
  • The map scrolls in all eight directions without a wrong character
  • Nothing to see of the ring itself: that is the point
Modules Used
console, background, input, tilering, vramqueue
See also
tilering.h, vramqueue.h

Macro Definition Documentation

◆ CAM_MAX_X

#define CAM_MAX_X   (archipelago_WIDTH * 8 - 256)

Camera limits: the map less one screen.

◆ CAM_MAX_Y

#define CAM_MAX_Y   (archipelago_HEIGHT * 8 - 224)

Camera limits: the map less one screen (224 lines).

◆ CHR_VRAM

#define CHR_VRAM   0x0000

The map (opensnes-tilering, from res/archipelago.png and its settings file).

BG1 characters: the ring's 640 slots, $0000-$27FF (20 KB)

◆ LEGS

#define LEGS   (sizeof(course) / sizeof(course[0]))

Legs in the course.

◆ MAP_VRAM

#define MAP_VRAM   0x3000

BG1 tilemap, 64x32 entries (4 KB), above the slots.

◆ RING_SLOTS

#define RING_SLOTS   640

Slots in the ring: the worst screen (389) and room to keep characters for a return.

Function Documentation

◆ approach()

u16 approach ( u16 v,
u16 target,
u16 step )
static

Move v toward target by at most step.

Parameters
vCurrent value
targetWhere it goes
stepMost it moves
Returns
The new value

◆ main()

int main ( void )

Entry point — the archipelago, flown over.

Returns
Never returns

◆ scanline()

u16 scanline ( void )
static

The scanline now, from the PPU's latched counter.

Returns
0-261 (NTSC) or 0-311 (PAL)

Variable Documentation

◆ camx

u16 camx

Camera position in map pixels.

◆ camy

u16 camy

◆ char_of

FAR u16 char_of[640]

The character of each slot.

◆ course

const Leg course[]
static
Initial value:
= {
{ 0, 0, 7 },
{ (archipelago_WIDTH * 8 - 256) , 0, 7 },
{ 352, (archipelago_HEIGHT * 8 - 224) , 5 },
{ 0, (archipelago_HEIGHT * 8 - 224) , 7 },
{ (archipelago_HEIGHT * 8 - 224) , 0, 5 },
{ (archipelago_WIDTH * 8 - 256) , 0, 7 },
{ (archipelago_WIDTH * 8 - 256) , (archipelago_HEIGHT * 8 - 224) , 7 },
{ 0, (archipelago_HEIGHT * 8 - 224) , 3 },
}

The course, from the bottom-left corner and back to it.

◆ leg

u16 leg

The leg being flown.

◆ manual

u16 manual

1 while the D-pad drives the camera

◆ refs

FAR u8 refs[640]

Cells of the window showing each slot.

◆ ring

TileRing ring

The ring's state (plain RAM).

◆ slot_of

FAR u16 slot_of[archipelago_CHARS]

The slot of each map character.

◆ staging

FAR u8 staging[(((33)+(29)) *32)]

The frame's new characters, copied from ROM, sent by one or two DMAs.

◆ tr_cost_55

u16 tr_cost_55

◆ tr_cost_7

u16 tr_cost_7

The worst frame the module predicts for the course's fastest legs (7 px, and 5 + 5).

◆ tr_direct

u16 tr_direct

1 = direct mode (no staging buffer); a test pokes it before main

◆ tr_frames

u16 tr_frames

Frames of the first lap, and laps completed.

◆ tr_laps

u16 tr_laps

◆ tr_max_chars

u16 tr_max_chars

Most characters one frame queued (first lap).

◆ tr_max_cost

u16 tr_max_cost

Most vramQueueCost() before a flush, in bytes (first lap; x 8 = master cycles).

◆ tr_max_entries

u16 tr_max_entries

Most queue entries one frame noted (first lap).

◆ tr_max_lines

u16 tr_max_lines

The latest scanline a flush ended on, counted from the start of VBlank (line 225).

◆ tr_max_resident

u16 tr_max_resident

Most slots in use at once (first lap).

◆ tr_refused

u16 tr_refused

◆ tr_starved

u16 tr_starved

Characters that found no free slot, transfers the queue refused: 0 and 0.

◆ tr_total_chars

u16 tr_total_chars

Characters loaded over the first lap.

◆ world

const TileRingMap world = archipelago_TILERING_MAP
static

What the converter wrote, in one structure.