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

Super FX game skeleton — a game loop at 60 fps while the GSU renders. More...

#include <snes.h>
#include <snes/superfx.h>
#include <snes/snesmod.h>
#include "soundbank.h"
#include "res/square.inc"
#include "res/sight.inc"

Macros

#define COL_BEAM   12
#define COL_INNER   14
#define COL_OUTER   15
 Colour of each group of edges, an index in BG palette 0.
#define COL_STRUT   13
#define EDGE_TABLE   ((u8 FAR *)0x708040)
 The edge table: 34 x (point, point, colour).
#define HOLO_X   128
 Where the hologram turns, in the 256x128 bitmap: above the projector of the square, whose top is at bitmap line 96 (screen line 144: the bitmap starts at screen line 48).
#define HOLO_Y   48
#define LETTERBOX_BOTTOM   40
 Bottom band: at least one line keeps force blank across line 0.
#define LETTERBOX_TOP   40
 Letterbox bands (lines): the NMI moves frames in VBlank + top band.
#define NUM_EDGES   34
 Number of edges the GSU draws: 32 of the hologram, 2 beams.
#define NUM_TURNED   8
 Number of vertices that turn (the outer cube); the inner cube's are points 8-15.
#define POINT_X   ((u8 FAR *)0x708000)
 Game Pak RAM past the two framebuffers, as the CPU sees it (bank $70): the points' x, their y, the edge table. The renderer reads the same three at $8000, $8020 and $8040 (gsu_cube.sfx). The CPU writes them only while it owns the RAM: before the first job, or after gsuWait(). The pointers are FAR: a plain pointer reads and writes bank $00, and an indexed store through ((u8 *)0x708040)[i] would land in ROM at $00:8040.
#define POINT_Y   ((u8 FAR *)0x708020)
 The points' y.
#define PT_CENTRE   18
 Point 18: the hologram's centre, where the beams meet.
#define PT_RIM_L   16
 Points 16 and 17: the projector's rim, where the beams start.
#define PT_RIM_R   17
#define RIM_Y   96
 The projector's rim, where the two beams start: x 108 and 148.
#define SIGHT_TILE   256
 The sight's first tile: the second name table (256 + 0).
#define VRAM_BG1_A   0x0000
 VRAM word addresses. BG1's two char blocks take $0000-$3FFF (16 KB each), the square's 541 tiles $4000-$61CF. BG2's tiles run into the OBJ name base ($6000), so the sight lives in the second name table: tile 256, at $7000. The two 32x32 maps sit above it, clear of the sight's tiles.
#define VRAM_BG1_B   0x2000
#define VRAM_BG1_MAP   0x7800
#define VRAM_BG2   0x4000
#define VRAM_BG2_MAP   0x7C00
#define VRAM_OBJ   0x6000
#define VRAM_SIGHT   0x7000

Functions

int main (void)
static void prepareFrame (u8 angle_y, u8 angle_x, u8 bob)
 Turn the hologram to the given angles, bob it, write its points.
void rotateVertex (u16 idx)
 Rotate one outer vertex, write its projection and the inner cube's matching point (idx + 8) to Game Pak RAM, clamped to the bitmap.
static void startJob (void)
 Start the renderer from the code cache; returns at once.
static void writeEdgeTable (void)
 Write the edge table and the projector's rim to Game Pak RAM, once: they never move.

Variables

static const s16 cvx [] = {-24, 24, 24,-24,-24, 24, 24,-24}
 The data cube's vertices, x. The cube inside it is the same at half the size, so only these 8 are turned: the inner vertex i + 8 is the outer vertex i's offset halved, a shift instead of four multiplies.
static const s16 cvy [] = {-24,-24, 24, 24,-24,-24, 24, 24}
 The data cube's vertices, y.
static const s16 cvz [] = {-24,-24,-24,-24, 24, 24, 24, 24}
 The data cube's vertices, z.
static const u8 edges [34 *3]
 The edge table the GSU walks: point, point, colour. Written to Game Pak RAM once; each job then only moves the points.
s16 g_cax
s16 g_cay
s16 g_holo_y
 The hologram's centre line this frame (it bobs).
s16 g_sax
s16 g_say
 Sines and cosines of the two rotation angles.
const u8 gsu_cube []
const u8 gsu_cube_end []
static const u16 holo_pal [16]
 BG palette 0: the GSU bitmap colours. 0 is the backdrop, the square's darkest ink (#080810), seen wherever no BG has a pixel.
u16 r_game_frames
 Game-loop frames (one per WaitForVBlank).
u16 r_jobs
 GSU jobs started.
u16 r_music_pos
 The module's pattern position, sampled every frame.
u16 r_ship_x
 Sight position (the game's state): its top-left corner.
u16 r_ship_y
static const s8 sin_tab [256]
 Sine table, 256 steps per turn, amplitude 127.

Detailed Description

Super FX game skeleton — a game loop at 60 fps while the GSU renders.

The shape of a Super FX game: the CPU runs the game every frame (here, a sight steered with the D-pad, and music), the GSU renders the 3D view on its own clock (here, a hologram turning above a square of the city at night: a data cube, a smaller cube inside it, the struts between them and the projector's beams), and the NMI moves each finished frame to VRAM. Nothing waits for anything else.

  • The renderer runs from the GSU's code cache (gsuCacheLoad / gsuStartCached): the ROM stays the CPU's, so the game loop runs during the job.
  • Two framebuffers in Game Pak RAM, two char blocks in VRAM: gsuPresent() queues the frame the job just drew, the next job draws in the other buffer, and the NMI moves the queued one a piece per VBlank, taking Game Pak RAM from the GSU for each piece. BG1 switches to the new block only once all of it has landed.
  • A 40 + 40 line letterbox (gsuSetupHdmaBlanking) lengthens the blank the NMI transfers in: a 16 KB frame lands in two VBlanks, 30 fps.
SNES Concepts
  • Super FX code cache: a job that leaves the ROM to the CPU
  • Game Pak RAM time-sharing (SCMR RAN) between the GSU and the NMI's DMA
  • Double buffering by BG1 char base (BG12NBA), swapped after a whole frame
  • HDMA letterbox on INIDISP to extend the VBlank transfer window
  • A GSU bitmap BG over a picture BG: the GSU clears to colour 0, which is transparent, so BG2's square shows wherever the hologram is not
  • OBJ layer over both
  • SNESMOD music fed every frame (snesmodProcess) during GSU work
  • The CPU projects, the GSU draws: 19 points a job in Game Pak RAM, joined by the GSU through an edge table written once at boot
  • Cartridge RAM from C through FAR pointers ($70:8000)
What to Observe
  • The hologram turns and bobs at 30 fps above the projector in the middle of the square; the sight follows the D-pad at 60 fps, whatever the GSU is doing, and the music never stutters.
  • No torn or half-drawn hologram: a frame is shown only once complete.
Modules Used
console, sprite, dma, background, asset, input, superfx, snesmod
See also
superfx.h (gsuPresent, gsuStartCached), docs/tutorials/superfx_advanced.md

Macro Definition Documentation

◆ COL_BEAM

#define COL_BEAM   12

◆ COL_INNER

#define COL_INNER   14

◆ COL_OUTER

#define COL_OUTER   15

Colour of each group of edges, an index in BG palette 0.

◆ COL_STRUT

#define COL_STRUT   13

◆ EDGE_TABLE

#define EDGE_TABLE   ((u8 FAR *)0x708040)

The edge table: 34 x (point, point, colour).

◆ HOLO_X

#define HOLO_X   128

Where the hologram turns, in the 256x128 bitmap: above the projector of the square, whose top is at bitmap line 96 (screen line 144: the bitmap starts at screen line 48).

◆ HOLO_Y

#define HOLO_Y   48

◆ LETTERBOX_BOTTOM

#define LETTERBOX_BOTTOM   40

Bottom band: at least one line keeps force blank across line 0.

◆ LETTERBOX_TOP

#define LETTERBOX_TOP   40

Letterbox bands (lines): the NMI moves frames in VBlank + top band.

◆ NUM_EDGES

#define NUM_EDGES   34

Number of edges the GSU draws: 32 of the hologram, 2 beams.

◆ NUM_TURNED

#define NUM_TURNED   8

Number of vertices that turn (the outer cube); the inner cube's are points 8-15.

◆ POINT_X

#define POINT_X   ((u8 FAR *)0x708000)

Game Pak RAM past the two framebuffers, as the CPU sees it (bank $70): the points' x, their y, the edge table. The renderer reads the same three at $8000, $8020 and $8040 (gsu_cube.sfx). The CPU writes them only while it owns the RAM: before the first job, or after gsuWait(). The pointers are FAR: a plain pointer reads and writes bank $00, and an indexed store through ((u8 *)0x708040)[i] would land in ROM at $00:8040.

◆ POINT_Y

#define POINT_Y   ((u8 FAR *)0x708020)

The points' y.

◆ PT_CENTRE

#define PT_CENTRE   18

Point 18: the hologram's centre, where the beams meet.

◆ PT_RIM_L

#define PT_RIM_L   16

Points 16 and 17: the projector's rim, where the beams start.

◆ PT_RIM_R

#define PT_RIM_R   17

◆ RIM_Y

#define RIM_Y   96

The projector's rim, where the two beams start: x 108 and 148.

◆ SIGHT_TILE

#define SIGHT_TILE   256

The sight's first tile: the second name table (256 + 0).

◆ VRAM_BG1_A

#define VRAM_BG1_A   0x0000

VRAM word addresses. BG1's two char blocks take $0000-$3FFF (16 KB each), the square's 541 tiles $4000-$61CF. BG2's tiles run into the OBJ name base ($6000), so the sight lives in the second name table: tile 256, at $7000. The two 32x32 maps sit above it, clear of the sight's tiles.

◆ VRAM_BG1_B

#define VRAM_BG1_B   0x2000

◆ VRAM_BG1_MAP

#define VRAM_BG1_MAP   0x7800

◆ VRAM_BG2

#define VRAM_BG2   0x4000

◆ VRAM_BG2_MAP

#define VRAM_BG2_MAP   0x7C00

◆ VRAM_OBJ

#define VRAM_OBJ   0x6000

◆ VRAM_SIGHT

#define VRAM_SIGHT   0x7000

Function Documentation

◆ main()

int main ( void )

◆ prepareFrame()

void prepareFrame ( u8 angle_y,
u8 angle_x,
u8 bob )
static

Turn the hologram to the given angles, bob it, write its points.

◆ rotateVertex()

void rotateVertex ( u16 idx)

Rotate one outer vertex, write its projection and the inner cube's matching point (idx + 8) to Game Pak RAM, clamped to the bitmap.

◆ startJob()

void startJob ( void )
static

Start the renderer from the code cache; returns at once.

◆ writeEdgeTable()

void writeEdgeTable ( void )
static

Write the edge table and the projector's rim to Game Pak RAM, once: they never move.

Variable Documentation

◆ cvx

const s16 cvx[] = {-24, 24, 24,-24,-24, 24, 24,-24}
static

The data cube's vertices, x. The cube inside it is the same at half the size, so only these 8 are turned: the inner vertex i + 8 is the outer vertex i's offset halved, a shift instead of four multiplies.

◆ cvy

const s16 cvy[] = {-24,-24, 24, 24,-24,-24, 24, 24}
static

The data cube's vertices, y.

◆ cvz

const s16 cvz[] = {-24,-24,-24,-24, 24, 24, 24, 24}
static

The data cube's vertices, z.

◆ edges

const u8 edges[34 *3]
static
Initial value:
= {
0,1, 15 , 1,2, 15 , 2,3, 15 , 3,0, 15 ,
4,5, 15 , 5,6, 15 , 6,7, 15 , 7,4, 15 ,
0,4, 15 , 1,5, 15 , 2,6, 15 , 3,7, 15 ,
8,9, 14 , 9,10, 14 , 10,11, 14 , 11,8, 14 ,
12,13, 14 , 13,14, 14 , 14,15, 14 , 15,12, 14 ,
8,12, 14 , 9,13, 14 , 10,14, 14 , 11,15, 14 ,
0,8, 13 , 1,9, 13 , 2,10, 13 , 3,11, 13 ,
4,12, 13 , 5,13, 13 , 6,14, 13 , 7,15, 13 ,
16 , 18 , 12 , 17 , 18 , 12 ,
}

The edge table the GSU walks: point, point, colour. Written to Game Pak RAM once; each job then only moves the points.

◆ g_cax

s16 g_cax

◆ g_cay

s16 g_cay

◆ g_holo_y

s16 g_holo_y

The hologram's centre line this frame (it bobs).

◆ g_sax

s16 g_sax

◆ g_say

s16 g_say

Sines and cosines of the two rotation angles.

◆ gsu_cube

const u8 gsu_cube[]
extern

◆ gsu_cube_end

const u8 gsu_cube_end[]

the cache-resident renderer

◆ holo_pal

const u16 holo_pal[16]
static
Initial value:
= {
RGB(1,1,2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
RGB(4,10,11),
RGB(9,20,20),
RGB(28,10,18),
RGB(20,31,31),
}
#define RGB(r, g, b)
Create RGB color value.
Definition video.h:92

BG palette 0: the GSU bitmap colours. 0 is the backdrop, the square's darkest ink (#080810), seen wherever no BG has a pixel.

◆ r_game_frames

u16 r_game_frames

Game-loop frames (one per WaitForVBlank).

◆ r_jobs

u16 r_jobs

GSU jobs started.

◆ r_music_pos

u16 r_music_pos

The module's pattern position, sampled every frame.

◆ r_ship_x

u16 r_ship_x

Sight position (the game's state): its top-left corner.

◆ r_ship_y

u16 r_ship_y

◆ sin_tab

const s8 sin_tab[256]
static
Initial value:
= {
0, 3, 6, 9, 12, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
49, 51, 54, 57, 60, 63, 65, 68, 71, 73, 76, 78, 81, 83, 85, 88,
90, 92, 94, 96, 98, 100, 102, 104, 106, 107, 109, 111, 112, 113, 115, 116,
117, 118, 120, 121, 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127,
127, 127, 127, 127, 126, 126, 126, 125, 125, 124, 123, 122, 122, 121, 120, 118,
117, 116, 115, 113, 112, 111, 109, 107, 106, 104, 102, 100, 98, 96, 94, 92,
90, 88, 85, 83, 81, 78, 76, 73, 71, 68, 65, 63, 60, 57, 54, 51,
49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 12, 9, 6, 3,
0, -3, -6, -9, -12, -16, -19, -22, -25, -28, -31, -34, -37, -40, -43, -46,
-49, -51, -54, -57, -60, -63, -65, -68, -71, -73, -76, -78, -81, -83, -85, -88,
-90, -92, -94, -96, -98,-100,-102,-104,-106,-107,-109,-111,-112,-113,-115,-116,
-117,-118,-120,-121,-122,-122,-123,-124,-125,-125,-126,-126,-126,-127,-127,-127,
-127,-127,-127,-127,-126,-126,-126,-125,-125,-124,-123,-122,-122,-121,-120,-118,
-117,-116,-115,-113,-112,-111,-109,-107,-106,-104,-102,-100, -98, -96, -94, -92,
-90, -88, -85, -83, -81, -78, -76, -73, -71, -68, -65, -63, -60, -57, -54, -51,
-49, -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -12, -9, -6, -3,
}

Sine table, 256 steps per turn, amplitude 127.