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

Random number generation demo. More...

#include <snes.h>
#include <snes/text.h>
#include <snes/gameloop.h>

Functions

int main (void)
 Entry point — gameLoopRun never returns.
 
static void on_init (void)
 One-time setup. Static labels + first random + screen on.
 
static void on_update (void)
 Per-frame: read input, regenerate on A, re-seed on B, print value.
 

Variables

u16 current_value
 Current random value to display.
 

Detailed Description

Random number generation demo.

Generates and displays random 16-bit numbers on button press. Demonstrates the LFSR-based pseudo-random number generator and how to seed it for varied sequences.

Every game needs randomness — enemy spawn positions, item drops, damage variance, shuffle order. The SNES has no hardware RNG, so we use a software PRNG seeded from the frame counter at game start.

Ported from PVSnesLib "random" example by alekmaul.

SNES Concepts
  • rand() — 16-bit LFSR pseudo-random number generator
  • srand() — seed the PRNG (use frame_count for unpredictable sequences)
  • Text module for hex and decimal display
  • padPressed() for single-press button detection
What to Observe
  • Press any button to generate a new random number
  • Both hex and decimal values are shown
  • Press B to re-seed the PRNG from the frame counter
  • After re-seeding, the sequence changes unpredictably
Modules Used
console, sprite, dma, background, text, input
See also
console.h (rand, srand)

Function Documentation

◆ main()

int main ( void  )

Entry point — gameLoopRun never returns.

◆ on_init()

static void on_init ( void  )
static

One-time setup. Static labels + first random + screen on.

◆ on_update()

static void on_update ( void  )
static

Per-frame: read input, regenerate on A, re-seed on B, print value.

The framework calls this once per VBlank — padPressed() reads the NMI-updated joypad buffer and returns only the bits that just transitioned (single-press detection), so holding the button down keeps producing the same number until release.

Variable Documentation

◆ current_value

u16 current_value

Current random value to display.