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

SNES Mode 7 Support. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define MODE7_FLIP_H   0x01
 Flip Mode 7 plane horizontally.
 
#define MODE7_FLIP_V   0x02
 Flip Mode 7 plane vertically.
 
#define MODE7_TILE0   0xC0
 Show tile 0 when out of bounds.
 
#define MODE7_TRANSPARENT   0x80
 Show transparent when out of bounds.
 
#define MODE7_WRAP   0x00
 Wrap around when out of bounds (default)
 

Functions

void mode7Init (void)
 Initialize Mode 7.
 
void mode7Rotate (u16 degrees)
 Set rotation in degrees (0-359)
 
void mode7SetAngle (u8 angle)
 Set Mode 7 rotation angle.
 
void mode7SetCenter (s16 x, s16 y)
 Set Mode 7 center point.
 
void mode7SetMatrix (s16 a, s16 b, s16 c, s16 d)
 Set Mode 7 matrix directly.
 
void mode7SetPivot (u8 x, u8 y)
 Set pivot point (screen coordinates)
 
void mode7SetScale (u16 scale_x, u16 scale_y)
 Set Mode 7 scale factors.
 
void mode7SetScroll (s16 x, s16 y)
 Set Mode 7 scroll position.
 
void mode7SetSettings (u8 settings)
 Set Mode 7 settings register.
 
void mode7Transform (u16 degrees, u16 scalePercent)
 Set rotation and scale together.
 

Detailed Description

SNES Mode 7 Support.

Functions for Mode 7 rotation and scaling effects. Mode 7 is the SNES's hardware rotation/scaling mode, famously used in F-Zero, Super Mario Kart, and Pilotwings.

Usage

#include <snes.h>
// Initialize Mode 7
// Set scale (0x0100 = 1.0, 0x0080 = 0.5, 0x0200 = 2.0)
mode7SetScale(0x0100, 0x0100);
// Set rotation angle (0-255, where 256 = 360 degrees)
// In main loop
while (1) {
angle++;
mode7SetAngle(angle);
}
void WaitForVBlank(void)
Wait for next VBlank period.
void mode7SetScale(u16 scale_x, u16 scale_y)
Set Mode 7 scale factors.
void mode7SetAngle(u8 angle)
Set Mode 7 rotation angle.
void mode7Init(void)
Initialize Mode 7.
OpenSNES Master Header.

Mode 7 VRAM Layout

Mode 7 uses a special interleaved VRAM format:

  • Tilemap in low bytes of VRAM words 0x0000-0x3FFF
  • Tile data in high bytes of VRAM words 0x0000-0x3FFF

Use mode7LoadGraphics() or set up DMA manually with interleaved data.

Author
OpenSNES Team

Macro Definition Documentation

◆ MODE7_FLIP_H

#define MODE7_FLIP_H   0x01

Flip Mode 7 plane horizontally.

◆ MODE7_FLIP_V

#define MODE7_FLIP_V   0x02

Flip Mode 7 plane vertically.

◆ MODE7_TILE0

#define MODE7_TILE0   0xC0

Show tile 0 when out of bounds.

◆ MODE7_TRANSPARENT

#define MODE7_TRANSPARENT   0x80

Show transparent when out of bounds.

◆ MODE7_WRAP

#define MODE7_WRAP   0x00

Wrap around when out of bounds (default)

Function Documentation

◆ mode7Init()

void mode7Init ( void  )

Initialize Mode 7.

Sets up default scale (1.0), identity matrix, center point (128,128), and scroll position. Call this before using other Mode 7 functions.

Note
This does NOT set BGMODE to Mode 7. You must do that separately:
REG_M7SEL = 0x00; // No flip, wrap around
REG_TM = TM_BG1; // Enable BG1
#define REG_BGMODE
BG mode and tile size (W)
Definition registers.h:64
#define REG_M7SEL
Mode 7 settings (W)
Definition registers.h:127
#define REG_TM
Main screen designation (W)
Definition registers.h:181
#define TM_BG1
Definition registers.h:439
#define BGMODE_MODE7
Definition registers.h:432

◆ mode7Rotate()

void mode7Rotate ( u16  degrees)

Set rotation in degrees (0-359)

Convenience function that converts degrees to the internal 0-255 format.

Parameters
degreesRotation angle in degrees (0-359)
mode7Rotate(45); // Rotate 45 degrees
mode7Rotate(180); // Rotate 180 degrees
void mode7Rotate(u16 degrees)
Set rotation in degrees (0-359)

◆ mode7SetAngle()

void mode7SetAngle ( u8  angle)

Set Mode 7 rotation angle.

Sets the rotation angle and recalculates the transformation matrix. The angle is 0-255 where 256 would equal 360 degrees (wraps at 256).

Parameters
angleRotation angle (0-255)

This function:

  1. Looks up sin/cos from table
  2. Multiplies by current scale using hardware multiplier
  3. Writes the 4 matrix values (M7A, M7B, M7C, M7D) to PPU

◆ mode7SetCenter()

void mode7SetCenter ( s16  x,
s16  y 
)

Set Mode 7 center point.

Sets the center of rotation/scaling. Default is (128, 128).

Parameters
xCenter X coordinate (13-bit signed, -4096 to 4095)
yCenter Y coordinate (13-bit signed, -4096 to 4095)

◆ mode7SetMatrix()

void mode7SetMatrix ( s16  a,
s16  b,
s16  c,
s16  d 
)

Set Mode 7 matrix directly.

For advanced users who need direct control over the transformation matrix. Bypasses the angle/scale system.

The matrix maps screen (x,y) to Mode 7 plane (X,Y): X = A*(x-cx) + B*(y-cy) + sx + cx Y = C*(x-cx) + D*(y-cy) + sy + cy

Parameters
aMatrix A (1.7.8 fixed point)
bMatrix B (1.7.8 fixed point)
cMatrix C (1.7.8 fixed point)
dMatrix D (1.7.8 fixed point)

◆ mode7SetPivot()

void mode7SetPivot ( u8  x,
u8  y 
)

Set pivot point (screen coordinates)

Sets the rotation center using screen coordinates (0-255). The pivot point is where the Mode 7 plane appears to rotate around.

Parameters
xScreen X coordinate (0-255)
yScreen Y coordinate (0-223)
mode7SetPivot(128, 112); // Center of screen
mode7SetPivot(0, 0); // Top-left corner
void mode7SetPivot(u8 x, u8 y)
Set pivot point (screen coordinates)

◆ mode7SetScale()

void mode7SetScale ( u16  scale_x,
u16  scale_y 
)

Set Mode 7 scale factors.

Sets the X and Y scale for Mode 7 transformation. Scale is in 8.8 fixed point format:

  • 0x0100 = 1.0 (normal size)
  • 0x0080 = 0.5 (zoomed in / larger)
  • 0x0200 = 2.0 (zoomed out / smaller)
Parameters
scale_xHorizontal scale (8.8 fixed point)
scale_yVertical scale (8.8 fixed point)
Note
Call mode7SetAngle() after changing scale to update the matrix.

◆ mode7SetScroll()

void mode7SetScroll ( s16  x,
s16  y 
)

Set Mode 7 scroll position.

Sets the scroll offset for the Mode 7 plane.

Parameters
xHorizontal scroll (13-bit signed)
yVertical scroll (13-bit signed)

◆ mode7SetSettings()

void mode7SetSettings ( u8  settings)

Set Mode 7 settings register.

Controls flipping and out-of-bounds behavior.

Parameters
settingsM7SEL value: Bit 1: Flip vertically Bit 0: Flip horizontally Bits 7-6: Out of bounds behavior (0=wrap, 0x80=transparent, 0xC0=tile 0)

◆ mode7Transform()

void mode7Transform ( u16  degrees,
u16  scalePercent 
)

Set rotation and scale together.

Combined transformation with rotation in degrees and percentage-based scaling.

Parameters
degreesRotation angle in degrees (0-359)
scalePercentScale as percentage (100 = normal, 50 = half, 200 = double)
mode7Transform(45, 100); // 45 degree rotation, normal scale
mode7Transform(0, 50); // No rotation, 2x zoom in
mode7Transform(90, 200); // 90 degrees, 0.5x zoom out
void mode7Transform(u16 degrees, u16 scalePercent)
Set rotation and scale together.