Envelope format:

<127 is a number of repeats of previous output value
127 is end of an envelope, next byte is new offset in envelope
128..255 is output value + 192 (it is in -64..63 range)

Envelopes can't be longer than 255 bytes


Stream format:

%00nnnnnn is a note (0..59 are octaves 1-5, 63 note stop)
%01iiiii0 is an instrument number (0 is default, silence)
%10rrrrrr is a number of empty rows (up to 63)
%11eeeeee is a special tag or effect
   eeeeee $01..19 speed
%11111110 is end of the stream, two next bytes are new pointer
%11111111 is a reference (next two bytes of absolute address, and number of rows)

No octaves on Noise channel, it is always 0..15; no instruments and octaves on DPCM channel, it is always one octave


Sound effect format:

<16 is a remapped register number followed by a byte of data
>=16 is a number of frames to skip before reading next block of data - 16 (16 if next frame is not empty)
255 is end of the effect

Sound effect data can't be longer than 255 bytes

Registers are remapped:

$4000 00
$4002 01
$4003 02
$4004 03
$4006 04
$4007 05
$4008 06
$400a 07
$400b 08
$400c 09
$400e 0a


RAM usage:

$00 frame counter, used to count time between rows
$01 current song speed
$02 instruments offset LSB
$03 instruments offset MSB
$04 previous MSB of Pulse 1
$05 previous MSB of Pulse 2

$06 channel 1 repeat counter (counts empty rows)
$07 channel 1 current note
$08 channel 1 current instrument
$09 channel 1 current duty
$0a channel 1 pointer LSB
$0b channel 1 pointer MSB
$0c channel 1 return LSB
$0d channel 1 return MSB
$0e channel 1 reference length counter (counts rows before return)

$0f channel 2 repeat counter
$10 channel 2 current note
$11 channel 2 current instrument
$12 channel 2 current duty
$13 channel 2 pointer LSB
$14 channel 2 pointer MSB
$15 channel 2 return LSB
$16 channel 2 return MSB
$17 channel 2 reference length counter

$18 channel 3 repeat counter
$19 channel 3 current note
$1a channel 3 current instrument
$1b channel 3 current duty
$1c channel 3 pointer LSB
$1d channel 3 pointer MSB
$1e channel 3 return LSB
$1f channel 3 return MSB
$20 channel 3 reference length counter

$21 channel 4 repeat counter
$22 channel 4 current note
$23 channel 4 current instrument
$24 channel 4 current duty
$25 channel 4 pointer LSB
$26 channel 4 pointer MSB
$27 channel 4 return LSB
$28 channel 4 return MSB
$29 channel 4 reference length counter

$2a channel 5 repeat counter
$2b channel 5 current note
$2c channel 5 current instrument
$2d channel 5 current duty
$2e channel 5 pointer LSB
$2f channel 5 pointer MSB
$30 channel 5 return LSB
$31 channel 5 return MSB
$32 channel 5 reference length counter

$33 channel 1 volume envelope output
$34 channel 1 volume envelope repeat counter
$35 channel 1 volume envelope address LSB
$36 channel 1 volume envelope address MSB
$37 channel 1 volume envelope pointer
$38 channel 1 arpeggio envelope output
$39 channel 1 arpeggio envelope repeat counter
$3a channel 1 arpeggio envelope address LSB
$3b channel 1 arpeggio envelope address MSB
$3c channel 1 arpeggio envelope pointer
$3d channel 1 pitch envelope output
$3e channel 1 pitch envelope repeat counter
$3f channel 1 pitch envelope address LSB
$40 channel 1 pitch envelope address MSB
$41 channel 1 pitch envelope pointer

$42 channel 2 volume envelope output
$43 channel 2 volume envelope repeat counter
$44 channel 2 volume envelope address LSB
$45 channel 2 volume envelope address MSB
$46 channel 2 volume envelope pointer
$47 channel 2 arpeggio envelope output
$48 channel 2 arpeggio envelope repeat counter
$49 channel 2 arpeggio envelope address LSB
$4a channel 2 arpeggio envelope address MSB
$4b channel 2 arpeggio envelope pointer
$4c channel 2 pitch envelope output
$4d channel 2 pitch envelope repeat counter
$4e channel 2 pitch envelope address LSB
$4f channel 2 pitch envelope address MSB
$50 channel 2 pitch envelope pointer

$51 channel 3 volume envelope output
$52 channel 3 volume envelope repeat counter
$53 channel 3 volume envelope address LSB
$54 channel 3 volume envelope address MSB
$55 channel 3 volume envelope pointer
$56 channel 3 arpeggio envelope output
$57 channel 3 arpeggio envelope repeat counter
$58 channel 3 arpeggio envelope address LSB
$59 channel 3 arpeggio envelope address MSB
$5a channel 3 arpeggio envelope pointer
$5b channel 3 pitch envelope output
$5c channel 3 pitch envelope repeat counter
$5d channel 3 pitch envelope address LSB
$5e channel 3 pitch envelope address MSB
$5f channel 3 pitch envelope pointer

$60 channel 4 volume envelope output
$61 channel 4 volume envelope repeat counter
$62 channel 4 volume envelope address LSB
$63 channel 4 volume envelope address MSB
$64 channel 4 volume envelope pointer
$65 channel 4 arpeggio envelope output
$66 channel 4 arpeggio envelope repeat counter
$67 channel 4 arpeggio envelope address LSB
$68 channel 4 arpeggio envelope address MSB
$69 channel 4 arpeggio envelope pointer

$6a DPCM table pointer LSB
$6b DPCM table pointer LSB
$6c DPCM sound effect active
$6d Sound effects data LSB
$6e Sound effects data MSB
$6f PAL adjust counter

$70 Main output buffer (11 bytes)

$7c Sound effect stream 1 repeat counter
$7d Sound effect stream 1 pointer LSB
$7e Sound effect stream 1 pointer MSB
$7f Sound effect stream 1 offset
$80 Sound effect stream 1 output buffer (11 bytes)

$8b Sound effect stream 2 repeat counter
$8c Sound effect stream 2 pointer LSB
$8d Sound effect stream 2 pointer MSB
$8e Sound effect stream 2 offset
$8f Sound effect stream 2 output buffer (11 bytes)

$9a Sound effect stream 3 repeat counter
$9b Sound effect stream 3 pointer LSB
$9c Sound effect stream 3 pointer MSB
$9d Sound effect stream 3 offset
$9e Sound effect stream 3 output buffer (11 bytes)

$a9 Sound effect stream 4 repeat counter
$aa Sound effect stream 4 pointer LSB
$ab Sound effect stream 4 pointer MSB
$ac Sound effect stream 4 offset
$ad Sound effect stream 4 output buffer (11 bytes)