Pokémon mini Development Documentation

Sound

Single-channel square-wave with adjustable pulse-width. Sound is generated by output from PTM_C or PTM5 in 8-bit mode.

3 levels of volume: 0%, 50%, 100%

VOL1~0 Volume level
0 0 0%
0 1 50%
1 0 50%
1 1 100%

Minimal example

From here.

#include "pm.h"

int main(void)
{
	// Initialize PTM_C in 16-bit mode
	TMR3_CTRL = 0x0082;
	TMR1_OSC |= 0x20;
	TMR3_SCALE = 0x08;
	TMR3_OSC = 0;

	// Initialize volume
	AUD_CTRL &= 0xFC; // this is the default
	AUD_VOL = 2; // 50% volume

	// Generate square wave at A4, ~440 Hz
	TMR3_PRE = 4544;
	TMR3_PVT = 2272; // half of PRE = 50% pulse width

	// Enable timer
	TMR3_CTRL_L = 0x04;

	// Do nothing
	for (;;) {}
}

MINLIB audio

For more details and a description of the format, see its main page

Every commercial game uses MINLIB to generate its audio. MINLIB defines an instruction format which instructs the system to play certain notes, use certain effects, or perform loops. It does this by using PTM3 to control how long a tick is (a preset of 255, roughly 16.32 ms) and every time PTM3 underflows it decrements a counter; when this counter reaches 0 it reads the next instruction.

Instruction reference

Formulas

Preset = (Timer_Frequency / Sound_Frequency) - 1
Sound_Frequency = Timer_Frequency / (Preset + 1)
Pivot = Preset * Pulse_Width
Pulse_Width = Pivot / Preset

# In MINLIB it uses a lookup table of preset values per note
# and then calculates the pivot using a lookup table of PWs.
# Here, Cx is the x from the most recent Cx instruction.
Widths = [2, 4, 8, 16, 32]  # most common
Pivot = Preset - (Preset / Widths[Cx])

# When it applies a 9x effect, it uses these formulas to
# adjust the frequency every tick.
# Here,
#   9x_y is byte y (0-based) of the last 9x instruction.
#   Note_Length is the length of the currently play note
#   Ticks_Remaining for the currently playing note
# This ensures the Pulse_Width stays the same.
if Ticks_Remaining < (Note_Length - 9x_0):
  # - if counter & 9x_1 else +
  Preset ±= (Preset >> 8) * 9x_2
  Pivot ±= (Pivot >> 8) * 9x_2