Loading...
Searching...
No Matches
console.h File Reference

SNES Console Initialization and Core Functions. More...

#include <snes/types.h>

Go to the source code of this file.

Functions

void consoleInit (void)
 Initialize SNES hardware.
 
void consoleInitEx (u16 options)
 Initialize console with options.
 
u8 getBrightness (void)
 Get current brightness.
 
u16 getFrameCount (void)
 Get frame counter.
 
u8 getRegion (void)
 Get system region.
 
u8 isInVBlank (void)
 Check if currently in VBlank.
 
u8 isPAL (void)
 Check if PAL system.
 
u16 rand (void)
 Get random 16-bit number.
 
void resetFrameCount (void)
 Reset frame counter.
 
void setBrightness (u8 brightness)
 Set screen brightness.
 
void setScreenOff (void)
 Disable screen display (blank)
 
void setScreenOn (void)
 Enable screen display.
 
void srand (u16 seed)
 Seed random number generator.
 
void WaitForVBlank (void)
 Wait for next VBlank period.
 

Detailed Description

SNES Console Initialization and Core Functions.

Provides functions for initializing SNES hardware and core functionality like VBlank synchronization.

Basic Usage

#include <snes.h>
int main(void) {
// Initialize hardware
// Set up your game...
// Main game loop
while (1) {
// Wait for VBlank (required for stable graphics)
// Update game logic
// Handle input
// Update sprites/backgrounds
}
}
int main(void)
Entry point — initialize audio, display controls, run transport loop.
Definition main.c:37
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
OpenSNES Master Header.

Attribution

Originally from: PVSnesLib (https://github.com/alekmaul/pvsneslib) Author: Alekmaul License: zlib (compatible with MIT) Modifications:

  • Renamed functions for clarity
  • Added comprehensive documentation
Author
OpenSNES Team

Function Documentation

◆ consoleInit()

void consoleInit ( void  )

Initialize SNES hardware.

Must be called at the start of your program. Performs:

  • PPU initialization (screen blank, registers cleared)
  • CPU register setup
  • Work RAM clearing
  • Default palette loading
  • VBlank interrupt setup

After calling, screen is blanked (black). Call setScreenOn() to enable display after you've set up your graphics.

int main(void) {
// Load graphics...
// Set up sprites...
setScreenOn(); // Now show the screen
while (1) {
}
}
void setScreenOn(void)
Enable screen display.

◆ consoleInitEx()

void consoleInitEx ( u16  options)

Initialize console with options.

Advanced initialization with configuration options.

Parameters
optionsInitialization flags (reserved for future use)
Note
For most games, use consoleInit() instead.

◆ getBrightness()

u8 getBrightness ( void  )

Get current brightness.

Returns
Current brightness level (0-15)

◆ getFrameCount()

u16 getFrameCount ( void  )

Get frame counter.

Returns the number of VBlanks since initialization. Wraps at 65535.

Returns
Frame count
// Simple animation timing
u8 anim_frame = (getFrameCount() / 8) % 4;
u16 getFrameCount(void)
Get frame counter.
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:46

◆ getRegion()

u8 getRegion ( void  )

Get system region.

Returns
0 = NTSC, 1 = PAL

◆ isInVBlank()

u8 isInVBlank ( void  )

Check if currently in VBlank.

Returns
TRUE if in VBlank, FALSE if in active display

◆ isPAL()

u8 isPAL ( void  )

Check if PAL system.

Returns
TRUE if PAL (50Hz), FALSE if NTSC (60Hz)
if (isPAL()) {
// Adjust game speed for 50Hz
}
u8 isPAL(void)
Check if PAL system.

◆ rand()

u16 rand ( void  )

Get random 16-bit number.

Returns a pseudo-random number using a linear feedback shift register.

Returns
Random value 0-65535
u16 enemy_x = rand() % 256;
static s16 enemy_x[4]
X positions of the four static enemy sprites.
Definition main.c:62
u16 rand(void)
Get random 16-bit number.
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:52

◆ resetFrameCount()

void resetFrameCount ( void  )

Reset frame counter.

Sets frame counter to 0. Useful for timing game events.

◆ setBrightness()

void setBrightness ( u8  brightness)

Set screen brightness.

Parameters
brightnessBrightness level (0-15, 0=black, 15=full)
// Fade in effect
for (u8 i = 0; i <= 15; i++) {
}
void setBrightness(u8 brightness)
Set screen brightness.
static u8 i
Definition main.c:156

◆ setScreenOff()

void setScreenOff ( void  )

Disable screen display (blank)

Turns off the display. Use during major VRAM updates that can't complete during VBlank.

// ... massive VRAM update ...
void setScreenOff(void)
Disable screen display (blank)

◆ setScreenOn()

void setScreenOn ( void  )

Enable screen display.

Turns on the display after initialization or a screen blank. Sets full brightness (15).

// ... load graphics ...
setScreenOn(); // Display is now visible

◆ srand()

void srand ( u16  seed)

Seed random number generator.

Parameters
seedInitial seed value
// Seed from player input timing for variety
void srand(u16 seed)
Seed random number generator.

◆ WaitForVBlank()

void WaitForVBlank ( void  )

Wait for next VBlank period.

Blocks until the PPU enters Vertical Blank. This is essential for:

  • Stable graphics (prevents tearing)
  • Safe VRAM/OAM/CGRAM updates
  • Consistent game timing

Call once per frame in your main loop.

while (1) {
// Update game logic (can happen during active display)
updatePlayer();
checkCollisions();
// Wait for VBlank
// Now safe to update graphics
updateOAM();
scrollBackground();
}
Note
VBlank occurs ~60 times/second (NTSC) or ~50 times/second (PAL)