Skip to content

Service a Logical Attack–Decay–Sustain–Release Envelope on Spectrum

A sound can swell, settle and fade instead of switching between full volume and silence.

A sound can swell, settle and fade instead of switching between full volume and silence.

Spectrum shows the working diagnostic row 06 03 01 01 01 03 02 06.
A completed native caller displays 06 03 01 01 01 03 02 06. The public field order below identifies these bytes; the capture is a state diagnostic, not a recording of a finished game.

Use it in a project

LEVEL is 0–15; PHASE is 0 off, 1 attack, 2 decay, 3 sustain or 4 release. ATTACK, DECAY and RELEASE are per-service steps 1–15; SUSTAIN is 0–15. A rising GATE restarts from zero. A low gate sends any active phase to release. Return a clamped logical level and VALID=1; invalid parameters or state return VALID=0 without changing envelope state.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: LEVEL, PHASE, PREVIOUS, VALID, GATE, ATTACK, DECAY, SUSTAIN, RELEASE. They occupy 9 contiguous bytes. Also reserve the private workspace: T0 (1 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call logical_envelope_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 logical_envelope_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 LEVEL, PHASE and PREVIOUS
Supply before service GATE, ATTACK, DECAY, SUSTAIN and RELEASE
Read after service LEVEL, PHASE and VALID

Gate edges choose a phase; each phase moves towards its own bound. Clamp before changing phase so a large step cannot wrap below silence or past the peak.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Service a Logical Attack–Decay–Sustain–Release Envelope. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
logical_envelope_init:
    ld a,0
    ld (LEVEL),a
    ld (PHASE),a
    ld (PREVIOUS),a
    ld (VALID),a
    ld (GATE),a
    ld (ATTACK),a
    ld (DECAY),a
    ld (SUSTAIN),a
    ld (RELEASE),a
    ld (T0),a
    ret
logical_envelope_step:
; VALID = 0
    ld a,0
    ld (VALID),a
; if LEVEL > 15:
    ld a,(LEVEL)
    cp 15
    jp z,logical_envelope_L2
    jp nc,logical_envelope_L1
    jp logical_envelope_L2
logical_envelope_L1:
; return
    ret
    jp logical_envelope_L3
logical_envelope_L2:
logical_envelope_L3:
; if PHASE > 4:
    ld a,(PHASE)
    cp 4
    jp z,logical_envelope_L5
    jp nc,logical_envelope_L4
    jp logical_envelope_L5
logical_envelope_L4:
; return
    ret
    jp logical_envelope_L6
logical_envelope_L5:
logical_envelope_L6:
; if ATTACK == 0:
    ld a,(ATTACK)
    cp 0
    jp z,logical_envelope_L7
    jp logical_envelope_L8
logical_envelope_L7:
; return
    ret
    jp logical_envelope_L9
logical_envelope_L8:
logical_envelope_L9:
; if ATTACK > 15:
    ld a,(ATTACK)
    cp 15
    jp z,logical_envelope_L11
    jp nc,logical_envelope_L10
    jp logical_envelope_L11
logical_envelope_L10:
; return
    ret
    jp logical_envelope_L12
logical_envelope_L11:
logical_envelope_L12:
; if DECAY == 0:
    ld a,(DECAY)
    cp 0
    jp z,logical_envelope_L13
    jp logical_envelope_L14
logical_envelope_L13:
; return
    ret
    jp logical_envelope_L15
logical_envelope_L14:
logical_envelope_L15:
; if DECAY > 15:
    ld a,(DECAY)
    cp 15
    jp z,logical_envelope_L17
    jp nc,logical_envelope_L16
    jp logical_envelope_L17
logical_envelope_L16:
; return
    ret
    jp logical_envelope_L18
logical_envelope_L17:
logical_envelope_L18:
; if RELEASE == 0:
    ld a,(RELEASE)
    cp 0
    jp z,logical_envelope_L19
    jp logical_envelope_L20
logical_envelope_L19:
; return
    ret
    jp logical_envelope_L21
logical_envelope_L20:
logical_envelope_L21:
; if RELEASE > 15:
    ld a,(RELEASE)
    cp 15
    jp z,logical_envelope_L23
    jp nc,logical_envelope_L22
    jp logical_envelope_L23
logical_envelope_L22:
; return
    ret
    jp logical_envelope_L24
logical_envelope_L23:
logical_envelope_L24:
; if SUSTAIN > 15:
    ld a,(SUSTAIN)
    cp 15
    jp z,logical_envelope_L26
    jp nc,logical_envelope_L25
    jp logical_envelope_L26
logical_envelope_L25:
; return
    ret
    jp logical_envelope_L27
logical_envelope_L26:
logical_envelope_L27:
; VALID = 1
    ld a,1
    ld (VALID),a
; if GATE != 0:
    ld a,(GATE)
    cp 0
    jp nz,logical_envelope_L28
    jp logical_envelope_L29
logical_envelope_L28:
; if PREVIOUS == 0:
    ld a,(PREVIOUS)
    cp 0
    jp z,logical_envelope_L31
    jp logical_envelope_L32
logical_envelope_L31:
; LEVEL = 0
    ld a,0
    ld (LEVEL),a
; PHASE = 1
    ld a,1
    ld (PHASE),a
    jp logical_envelope_L33
logical_envelope_L32:
logical_envelope_L33:
; PREVIOUS = 1
    ld a,1
    ld (PREVIOUS),a
    jp logical_envelope_L30
logical_envelope_L29:
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
; if PHASE != 0:
    ld a,(PHASE)
    cp 0
    jp nz,logical_envelope_L34
    jp logical_envelope_L35
logical_envelope_L34:
; PHASE = 4
    ld a,4
    ld (PHASE),a
    jp logical_envelope_L36
logical_envelope_L35:
logical_envelope_L36:
logical_envelope_L30:
; if PHASE == 0:
    ld a,(PHASE)
    cp 0
    jp z,logical_envelope_L37
    jp logical_envelope_L38
logical_envelope_L37:
; LEVEL = 0
    ld a,0
    ld (LEVEL),a
; return
    ret
    jp logical_envelope_L39
logical_envelope_L38:
logical_envelope_L39:
; if PHASE == 1:
    ld a,(PHASE)
    cp 1
    jp z,logical_envelope_L40
    jp logical_envelope_L41
logical_envelope_L40:
; T0 = 15 - LEVEL
    ld a,15
    ld b,a
    ld a,(LEVEL)
    ld c,a
    ld a,b
    sub c
    ld (T0),a
; if ATTACK >= T0:
    ld a,(ATTACK)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    cp c
    jp nc,logical_envelope_L43
    jp logical_envelope_L44
logical_envelope_L43:
; LEVEL = 15
    ld a,15
    ld (LEVEL),a
; PHASE = 2
    ld a,2
    ld (PHASE),a
    jp logical_envelope_L45
logical_envelope_L44:
; LEVEL = LEVEL + ATTACK
    ld a,(LEVEL)
    ld b,a
    ld a,(ATTACK)
    ld c,a
    ld a,b
    add a,c
    ld (LEVEL),a
logical_envelope_L45:
; return
    ret
    jp logical_envelope_L42
logical_envelope_L41:
logical_envelope_L42:
; if PHASE == 2:
    ld a,(PHASE)
    cp 2
    jp z,logical_envelope_L46
    jp logical_envelope_L47
logical_envelope_L46:
; if LEVEL <= SUSTAIN:
    ld a,(LEVEL)
    ld b,a
    ld a,(SUSTAIN)
    ld c,a
    ld a,b
    cp c
    jp c,logical_envelope_L49
    jp z,logical_envelope_L49
    jp logical_envelope_L50
logical_envelope_L49:
; LEVEL = SUSTAIN
    ld a,(SUSTAIN)
    ld (LEVEL),a
; PHASE = 3
    ld a,3
    ld (PHASE),a
; return
    ret
    jp logical_envelope_L51
logical_envelope_L50:
logical_envelope_L51:
; T0 = LEVEL - SUSTAIN
    ld a,(LEVEL)
    ld b,a
    ld a,(SUSTAIN)
    ld c,a
    ld a,b
    sub c
    ld (T0),a
; if DECAY >= T0:
    ld a,(DECAY)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    cp c
    jp nc,logical_envelope_L52
    jp logical_envelope_L53
logical_envelope_L52:
; LEVEL = SUSTAIN
    ld a,(SUSTAIN)
    ld (LEVEL),a
; PHASE = 3
    ld a,3
    ld (PHASE),a
    jp logical_envelope_L54
logical_envelope_L53:
; LEVEL = LEVEL - DECAY
    ld a,(LEVEL)
    ld b,a
    ld a,(DECAY)
    ld c,a
    ld a,b
    sub c
    ld (LEVEL),a
logical_envelope_L54:
; return
    ret
    jp logical_envelope_L48
logical_envelope_L47:
logical_envelope_L48:
; if PHASE == 3:
    ld a,(PHASE)
    cp 3
    jp z,logical_envelope_L55
    jp logical_envelope_L56
logical_envelope_L55:
; LEVEL = SUSTAIN
    ld a,(SUSTAIN)
    ld (LEVEL),a
; return
    ret
    jp logical_envelope_L57
logical_envelope_L56:
logical_envelope_L57:
; if RELEASE >= LEVEL:
    ld a,(RELEASE)
    ld b,a
    ld a,(LEVEL)
    ld c,a
    ld a,b
    cp c
    jp nc,logical_envelope_L58
    jp logical_envelope_L59
logical_envelope_L58:
; LEVEL = 0
    ld a,0
    ld (LEVEL),a
; PHASE = 0
    ld a,0
    ld (PHASE),a
    jp logical_envelope_L60
logical_envelope_L59:
; LEVEL = LEVEL - RELEASE
    ld a,(LEVEL)
    ld b,a
    ld a,(RELEASE)
    ld c,a
    ld a,b
    sub c
    ld (LEVEL),a
logical_envelope_L60:
    ret
component_end:

Machine contract

AF/BC/DE/HL may change; IX/IY are preserved. A direct CALL 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 48K diagnostic boots through ROM tape loading and USR, disables interrupts and owns the screen. The helper performs no port or screen writes.

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

demo.asm
    org $8000
CUSTOM equ 0
BROKEN equ 0
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
LEVEL: defb 0
PHASE: defb 0
PREVIOUS: defb 0
VALID: defb 0
GATE: defb 0
ATTACK: defb 0
DECAY: defb 0
SUSTAIN: defb 0
RELEASE: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    if CUSTOM
; GATE = 1
    ld a,1
    ld (GATE),a
; ATTACK = 15
    ld a,15
    ld (ATTACK),a
; DECAY = 4
    ld a,4
    ld (DECAY),a
; SUSTAIN = 4
    ld a,4
    ld (SUSTAIN),a
; RELEASE = 4
    ld a,4
    ld (RELEASE),a
    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state
; GATE = 0
    ld a,0
    ld (GATE),a
    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state
    ret
    else
; GATE = 1
    ld a,1
    ld (GATE),a
; ATTACK = 3
    ld a,3
    ld (ATTACK),a
; DECAY = 2
    ld a,2
    ld (DECAY),a
; SUSTAIN = 6
    ld a,6
    ld (SUSTAIN),a
; RELEASE = 4
    ld a,4
    ld (RELEASE),a
    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; PREVIOUS = 0
    ld a,0
    ld (PREVIOUS),a
    endif
    call checked_step
    call emit_state
    endif
    ret

Customise it

Use a fast attack and release the note before the default held sequence finishes. 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

custom.asm
    org $8000
CUSTOM equ 1
BROKEN equ 0
COYOTE_STEPS equ 2
BUFFER_STEPS equ 2
TEXT_CAP equ 4
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 49
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
LEVEL: defb 0
PHASE: defb 0
PREVIOUS: defb 0
VALID: defb 0
GATE: defb 0
ATTACK: defb 0
DECAY: defb 0
SUSTAIN: defb 0
RELEASE: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start

The custom diagnostic row is 00 00 00 01 00 0F 04 04. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears PREVIOUS each service. A held note continually retriggers its attack and never develops the intended shape. Retain the gate edge state independently of note servicing.

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

wrong.asm
    org $8000
CUSTOM equ 0
BROKEN equ 1
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
LEVEL: defb 0
PHASE: defb 0
PREVIOUS: defb 0
VALID: defb 0
GATE: defb 0
ATTACK: defb 0
DECAY: defb 0
SUSTAIN: defb 0
RELEASE: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start
Spectrum shows the deliberate error row 03 01 01 01 01 03 02 06.
The wrong client completes with 03 01 01 01 01 03 02 06; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 521 code bytes; public state is 9 bytes and private scratch is 1 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.

Constant work per service. A hardware envelope can save CPU when its behaviour fits the sound. Software envelopes offer a common control shape but need scheduled updates and machine-specific output mapping.

This is a software control envelope with linear steps per service, not an emulation of a chip’s envelope rates or curves. A backend maps LEVEL to an owned voice or mix gain. Do not overwrite shared hardware volume or a hardware-envelope mode without a deliberate audio ownership policy.

Execution evidence

Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 64 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

Priority arbitration: Voice ownership and effect priority decide which sound receives an envelope; the envelope alone does not allocate audio hardware.

The Craft · Maths for Games · Source and native evidence.