A short tune needs a clock and a sequence of notes, not a fresh sound effect every frame.

Use it in a project
COUNT is 0–8 pairs N/U. N=0 is rest, 1–95 is a logical note, and 255 ends immediately. Other note codes or a zero duration on a note/rest are invalid. INDEX counts consumed pairs and must not exceed COUNT. If REMAINING is nonzero, decrement it first; when it reaches zero consume the next pair in the same call. A newly emitted note/rest holds for U service intervals, with REMAINING set to U. EVENT=1 emits NOTE; EVENT=2 is a one-shot stop on count exhaustion or N=255. FINISHED is sticky. Validate only the consumed pair before advancing INDEX. Invalid state returns VALID=0 and no event; an elapsed timer may already reach zero.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: NOTE, EVENT, FINISHED, VALID, INDEX, REMAINING, COUNT, N0, N1, N2, N3, N4, N5, N6, N7, U0, U1, U2, U3, U4, U5, U6, U7. They occupy 23 contiguous bytes. Also reserve the private workspace: T0, T1 (2 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call music_command_stream_init once at the intended reset boundary. It clears inputs as well as outputs and workspace. Set your starting state and configuration afterwards, then call music_command_stream_step whenever the described event occurs. The helper reads no device, waits for no frame and installs no interrupt handler.
| Ownership | Fields |
|---|---|
| Retain between calls | INDEX, REMAINING, FINISHED and NOTE; immutable N/U pairs |
| Supply before service | COUNT and one service tick |
| Read after service | EVENT, NOTE, FINISHED and VALID |
Only consume a pair when its predecessor’s interval has expired. A bounded count prevents reading past the stream; explicit end and exhausted count each emit a stop exactly once.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Sequence Counted Notes and Rests. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
music_command_stream_init:
lda #0
sta NOTE
sta EVENT
sta FINISHED
sta VALID
sta INDEX
sta REMAINING
sta COUNT
sta N0
sta N1
sta N2
sta N3
sta N4
sta N5
sta N6
sta N7
sta U0
sta U1
sta U2
sta U3
sta U4
sta U5
sta U6
sta U7
sta T0
sta T1
rts
music_command_stream_step:
; EVENT = 0
lda #0
sta EVENT
; VALID = 0
lda #0
sta VALID
; if COUNT > 8:
lda COUNT
cmp #8
beq music_command_stream_L5
bcs music_command_stream_L4
music_command_stream_L5:
jmp music_command_stream_L2
music_command_stream_L4:
jmp music_command_stream_L1
music_command_stream_L1:
; return
rts
jmp music_command_stream_L3
music_command_stream_L2:
music_command_stream_L3:
; if INDEX > COUNT:
lda INDEX
cmp COUNT
beq music_command_stream_L10
bcs music_command_stream_L9
music_command_stream_L10:
jmp music_command_stream_L7
music_command_stream_L9:
jmp music_command_stream_L6
music_command_stream_L6:
; return
rts
jmp music_command_stream_L8
music_command_stream_L7:
music_command_stream_L8:
; VALID = 1
lda #1
sta VALID
; if FINISHED != 0:
lda FINISHED
cmp #0
bne music_command_stream_L14
music_command_stream_L15:
jmp music_command_stream_L12
music_command_stream_L14:
jmp music_command_stream_L11
music_command_stream_L11:
; return
rts
jmp music_command_stream_L13
music_command_stream_L12:
music_command_stream_L13:
; if REMAINING != 0:
lda REMAINING
cmp #0
bne music_command_stream_L19
music_command_stream_L20:
jmp music_command_stream_L17
music_command_stream_L19:
jmp music_command_stream_L16
music_command_stream_L16:
; REMAINING = REMAINING - 1
lda REMAINING
sec
sbc #1
sta REMAINING
; if REMAINING != 0:
lda REMAINING
cmp #0
bne music_command_stream_L24
music_command_stream_L25:
jmp music_command_stream_L22
music_command_stream_L24:
jmp music_command_stream_L21
music_command_stream_L21:
; return
rts
jmp music_command_stream_L23
music_command_stream_L22:
music_command_stream_L23:
jmp music_command_stream_L18
music_command_stream_L17:
music_command_stream_L18:
; if INDEX == COUNT:
lda INDEX
cmp COUNT
beq music_command_stream_L29
music_command_stream_L30:
jmp music_command_stream_L27
music_command_stream_L29:
jmp music_command_stream_L26
music_command_stream_L26:
; FINISHED = 1
lda #1
sta FINISHED
; NOTE = 0
lda #0
sta NOTE
; EVENT = 2
lda #2
sta EVENT
; return
rts
jmp music_command_stream_L28
music_command_stream_L27:
music_command_stream_L28:
; T0 = N[INDEX]
ldx INDEX
lda N0,x
sta T0
; T1 = U[INDEX]
ldx INDEX
lda U0,x
sta T1
; if T0 == 255:
lda T0
cmp #255
beq music_command_stream_L34
music_command_stream_L35:
jmp music_command_stream_L32
music_command_stream_L34:
jmp music_command_stream_L31
music_command_stream_L31:
; INDEX = INDEX + 1
lda INDEX
clc
adc #1
sta INDEX
; FINISHED = 1
lda #1
sta FINISHED
; NOTE = 0
lda #0
sta NOTE
; EVENT = 2
lda #2
sta EVENT
; return
rts
jmp music_command_stream_L33
music_command_stream_L32:
music_command_stream_L33:
; if T0 > 95:
lda T0
cmp #95
beq music_command_stream_L40
bcs music_command_stream_L39
music_command_stream_L40:
jmp music_command_stream_L37
music_command_stream_L39:
jmp music_command_stream_L36
music_command_stream_L36:
; VALID = 0
lda #0
sta VALID
; return
rts
jmp music_command_stream_L38
music_command_stream_L37:
music_command_stream_L38:
; if T1 == 0:
lda T1
cmp #0
beq music_command_stream_L44
music_command_stream_L45:
jmp music_command_stream_L42
music_command_stream_L44:
jmp music_command_stream_L41
music_command_stream_L41:
; VALID = 0
lda #0
sta VALID
; return
rts
jmp music_command_stream_L43
music_command_stream_L42:
music_command_stream_L43:
; NOTE = T0
lda T0
sta NOTE
; REMAINING = T1
lda T1
sta REMAINING
; INDEX = INDEX + 1
lda INDEX
clc
adc #1
sta INDEX
; EVENT = 1
lda #1
sta EVENT
rts
component_end:Machine contract
A/X/status may change; Y and decimal mode are preserved. Use CLD for binary arithmetic. A direct JSR needs two return-address stack bytes. The helper makes no nested calls and adds no other stack use. Its named scratch is private workspace, not a pointer, a ROM variable or shared zero-page space that the game may borrow concurrently. The samples use fixed symbol bindings. Scope or rename every label and binding, including the component_end measurement marker, and allocate a separate block for each independent instance. Different helpers can also use common field names, so combining them needs separate namespaces or explicitly renamed bindings. Give each block one mutation owner.
The diagnostic PRG owns screen, font and colour RAM and disables interrupts; it does not return to BASIC. The reusable helper does none of those things.
Adapt the caller
demo.asm supplies the bindings and include order. exercise.inc first runs table cases, then a separate client retaining state between calls. A game can call the public step entry directly and remove the diagnostic packet and display. Do not copy the diagnostic’s hardware takeover into an existing game loop.
Build with make ASM198X=/path/to/asm198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.
Bindings and include order
NOTE=$80
EVENT=$81
FINISHED=$82
VALID=$83
INDEX=$84
REMAINING=$85
COUNT=$86
N0=$87
N1=$88
N2=$89
N3=$8a
N4=$8b
N5=$8c
N6=$8d
N7=$8e
U0=$8f
U1=$90
U2=$91
U3=$92
U4=$93
U5=$94
U6=$95
U7=$96
T0=$97
T1=$98
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=0
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:A client retaining state between calls
preview_rules:
!if CUSTOM {
; COUNT = 2
lda #2
sta COUNT
; N0 = 72
lda #72
sta N0
; U0 = 1
lda #1
sta U0
; N1 = 255
lda #255
sta N1
; U1 = 0
lda #0
sta U1
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
rts
} else {
; COUNT = 3
lda #3
sta COUNT
; N0 = 40
lda #40
sta N0
; U0 = 2
lda #2
sta U0
; N1 = 0
lda #0
sta N1
; U1 = 1
lda #1
sta U1
; N2 = 44
lda #44
sta N2
; U2 = 1
lda #1
sta U2
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
!if BROKEN {
; REMAINING = 0
lda #0
sta REMAINING
}
jsr checked_step
jsr emit_state
}
rtsCustomise it
Play one higher note then an explicit end command, whose duration is ignored. The maintained custom caller changes its inputs or client sequence rather than hiding a different implementation behind the same label. Compare every state of the two clients, not only their final display.
Custom client selection
NOTE=$80
EVENT=$81
FINISHED=$82
VALID=$83
INDEX=$84
REMAINING=$85
COUNT=$86
N0=$87
N1=$88
N2=$89
N3=$8a
N4=$8b
N5=$8c
N6=$8d
N7=$8e
U0=$8f
U1=$90
U2=$91
U3=$92
U4=$93
U5=$94
U6=$95
U7=$96
T0=$97
T1=$98
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=1
BROKEN=0
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:The custom diagnostic row is 00 00 01 01 02 00 02 48. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong client discards each note’s hold time. It advances to a rest or next note one service too early. Retain REMAINING and use one consistent service clock.
The BROKEN branch lives in the sequential client, not the reusable helper. wrong.asm selects it; repaired.asm restores the normal call sequence. The repaired executable matches the normal program (for Spectrum, compare loaded CODE rather than the tape filename).
Select the deliberately wrong client
NOTE=$80
EVENT=$81
FINISHED=$82
VALID=$83
INDEX=$84
REMAINING=$85
COUNT=$86
N0=$87
N1=$88
N2=$89
N3=$8a
N4=$8b
N5=$8c
N6=$8d
N7=$8e
U0=$8f
U1=$90
U2=$91
U3=$92
U4=$93
U5=$94
U6=$95
U7=$96
T0=$97
T1=$98
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=1
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:
Cost and alternatives
The measured helper extent is 292 code bytes; public state is 23 bytes and private scratch is 2 bytes. There are no immutable helper tables. Caller data, the test cases, return-address stack, alignment and display buffers cost additional space. These are assembled sizes, not cycle measurements.
One pair at most per service, with no scan over instant commands. Rich formats can add instrument/loop commands, but need a command budget to stop malformed streams looping forever.
This is a logical sequencer, not an audible chip driver, tracker, tempo converter or general music format. The caller maps note numbers to chip periods and handles rest/stop by silencing its owned voice. SID, APU and Paula have different tuning, envelope and channel rules; Spectrum beeper playback also consumes CPU time. No emitted PCM or audible-timing claim is made by the state diagnostic.
Execution evidence
Four variants run on PAL stock-ROM PRG, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 43 boundary/varied/error table cases, dirty initialisation and the separate retained-state client. An independent mathematical/container model checks every public byte after every call. This is bounded example coverage, not exhaustive correctness for every possible state.
The records retain loaded immutable code, completion, register sentinels, full packets, source/tool/firmware hashes and captures. The public gate validates these retained observations and rejects altered evidence; it does not run restricted firmware. No firmware is distributed. The eight displayed bytes are the first eight public fields, zero-padded when shorter; the packet includes every field.
Where it fits
Logical envelope: Drive the existing logical envelope or the documented machine sound trigger from note events; avoid retriggering on every held tick.