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

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
; 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:
move.b #0,d0
move.b d0,LEVEL
move.b d0,PHASE
move.b d0,PREVIOUS
move.b d0,VALID
move.b d0,GATE
move.b d0,ATTACK
move.b d0,DECAY
move.b d0,SUSTAIN
move.b d0,RELEASE
move.b d0,T0
rts
logical_envelope_step:
; VALID = 0
move.b #0,d0
move.b d0,VALID
; if LEVEL > 15:
move.b LEVEL,d0
cmp.b #15,d0
bhi logical_envelope_L1
jmp logical_envelope_L2
logical_envelope_L1:
; return
rts
jmp logical_envelope_L3
logical_envelope_L2:
logical_envelope_L3:
; if PHASE > 4:
move.b PHASE,d0
cmp.b #4,d0
bhi logical_envelope_L4
jmp logical_envelope_L5
logical_envelope_L4:
; return
rts
jmp logical_envelope_L6
logical_envelope_L5:
logical_envelope_L6:
; if ATTACK == 0:
move.b ATTACK,d0
cmp.b #0,d0
beq logical_envelope_L7
jmp logical_envelope_L8
logical_envelope_L7:
; return
rts
jmp logical_envelope_L9
logical_envelope_L8:
logical_envelope_L9:
; if ATTACK > 15:
move.b ATTACK,d0
cmp.b #15,d0
bhi logical_envelope_L10
jmp logical_envelope_L11
logical_envelope_L10:
; return
rts
jmp logical_envelope_L12
logical_envelope_L11:
logical_envelope_L12:
; if DECAY == 0:
move.b DECAY,d0
cmp.b #0,d0
beq logical_envelope_L13
jmp logical_envelope_L14
logical_envelope_L13:
; return
rts
jmp logical_envelope_L15
logical_envelope_L14:
logical_envelope_L15:
; if DECAY > 15:
move.b DECAY,d0
cmp.b #15,d0
bhi logical_envelope_L16
jmp logical_envelope_L17
logical_envelope_L16:
; return
rts
jmp logical_envelope_L18
logical_envelope_L17:
logical_envelope_L18:
; if RELEASE == 0:
move.b RELEASE,d0
cmp.b #0,d0
beq logical_envelope_L19
jmp logical_envelope_L20
logical_envelope_L19:
; return
rts
jmp logical_envelope_L21
logical_envelope_L20:
logical_envelope_L21:
; if RELEASE > 15:
move.b RELEASE,d0
cmp.b #15,d0
bhi logical_envelope_L22
jmp logical_envelope_L23
logical_envelope_L22:
; return
rts
jmp logical_envelope_L24
logical_envelope_L23:
logical_envelope_L24:
; if SUSTAIN > 15:
move.b SUSTAIN,d0
cmp.b #15,d0
bhi logical_envelope_L25
jmp logical_envelope_L26
logical_envelope_L25:
; return
rts
jmp logical_envelope_L27
logical_envelope_L26:
logical_envelope_L27:
; VALID = 1
move.b #1,d0
move.b d0,VALID
; if GATE != 0:
move.b GATE,d0
cmp.b #0,d0
bne logical_envelope_L28
jmp logical_envelope_L29
logical_envelope_L28:
; if PREVIOUS == 0:
move.b PREVIOUS,d0
cmp.b #0,d0
beq logical_envelope_L31
jmp logical_envelope_L32
logical_envelope_L31:
; LEVEL = 0
move.b #0,d0
move.b d0,LEVEL
; PHASE = 1
move.b #1,d0
move.b d0,PHASE
jmp logical_envelope_L33
logical_envelope_L32:
logical_envelope_L33:
; PREVIOUS = 1
move.b #1,d0
move.b d0,PREVIOUS
jmp logical_envelope_L30
logical_envelope_L29:
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
; if PHASE != 0:
move.b PHASE,d0
cmp.b #0,d0
bne logical_envelope_L34
jmp logical_envelope_L35
logical_envelope_L34:
; PHASE = 4
move.b #4,d0
move.b d0,PHASE
jmp logical_envelope_L36
logical_envelope_L35:
logical_envelope_L36:
logical_envelope_L30:
; if PHASE == 0:
move.b PHASE,d0
cmp.b #0,d0
beq logical_envelope_L37
jmp logical_envelope_L38
logical_envelope_L37:
; LEVEL = 0
move.b #0,d0
move.b d0,LEVEL
; return
rts
jmp logical_envelope_L39
logical_envelope_L38:
logical_envelope_L39:
; if PHASE == 1:
move.b PHASE,d0
cmp.b #1,d0
beq logical_envelope_L40
jmp logical_envelope_L41
logical_envelope_L40:
; T0 = 15 - LEVEL
move.b #15,d0
move.b LEVEL,d1
sub.b d1,d0
move.b d0,T0
; if ATTACK >= T0:
move.b ATTACK,d0
move.b T0,d1
cmp.b d1,d0
bcc logical_envelope_L43
jmp logical_envelope_L44
logical_envelope_L43:
; LEVEL = 15
move.b #15,d0
move.b d0,LEVEL
; PHASE = 2
move.b #2,d0
move.b d0,PHASE
jmp logical_envelope_L45
logical_envelope_L44:
; LEVEL = LEVEL + ATTACK
move.b LEVEL,d0
move.b ATTACK,d1
add.b d1,d0
move.b d0,LEVEL
logical_envelope_L45:
; return
rts
jmp logical_envelope_L42
logical_envelope_L41:
logical_envelope_L42:
; if PHASE == 2:
move.b PHASE,d0
cmp.b #2,d0
beq logical_envelope_L46
jmp logical_envelope_L47
logical_envelope_L46:
; if LEVEL <= SUSTAIN:
move.b LEVEL,d0
move.b SUSTAIN,d1
cmp.b d1,d0
bls logical_envelope_L49
jmp logical_envelope_L50
logical_envelope_L49:
; LEVEL = SUSTAIN
move.b SUSTAIN,d0
move.b d0,LEVEL
; PHASE = 3
move.b #3,d0
move.b d0,PHASE
; return
rts
jmp logical_envelope_L51
logical_envelope_L50:
logical_envelope_L51:
; T0 = LEVEL - SUSTAIN
move.b LEVEL,d0
move.b SUSTAIN,d1
sub.b d1,d0
move.b d0,T0
; if DECAY >= T0:
move.b DECAY,d0
move.b T0,d1
cmp.b d1,d0
bcc logical_envelope_L52
jmp logical_envelope_L53
logical_envelope_L52:
; LEVEL = SUSTAIN
move.b SUSTAIN,d0
move.b d0,LEVEL
; PHASE = 3
move.b #3,d0
move.b d0,PHASE
jmp logical_envelope_L54
logical_envelope_L53:
; LEVEL = LEVEL - DECAY
move.b LEVEL,d0
move.b DECAY,d1
sub.b d1,d0
move.b d0,LEVEL
logical_envelope_L54:
; return
rts
jmp logical_envelope_L48
logical_envelope_L47:
logical_envelope_L48:
; if PHASE == 3:
move.b PHASE,d0
cmp.b #3,d0
beq logical_envelope_L55
jmp logical_envelope_L56
logical_envelope_L55:
; LEVEL = SUSTAIN
move.b SUSTAIN,d0
move.b d0,LEVEL
; return
rts
jmp logical_envelope_L57
logical_envelope_L56:
logical_envelope_L57:
; if RELEASE >= LEVEL:
move.b RELEASE,d0
move.b LEVEL,d1
cmp.b d1,d0
bcc logical_envelope_L58
jmp logical_envelope_L59
logical_envelope_L58:
; LEVEL = 0
move.b #0,d0
move.b d0,LEVEL
; PHASE = 0
move.b #0,d0
move.b d0,PHASE
jmp logical_envelope_L60
logical_envelope_L59:
; LEVEL = LEVEL - RELEASE
move.b LEVEL,d0
move.b RELEASE,d1
sub.b d1,d0
move.b d0,LEVEL
logical_envelope_L60:
rts
component_end:Machine contract
D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 are preserved. A direct BSR/JSR needs four 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 A500 OCS diagnostic cold-boots an OFS disk, owns the Copper and bitplane and disables interrupt/DMA sources. The helper does not take over hardware or allocate Chip RAM.
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 and BUILD198X=/path/to/build198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.
Bindings and include order
section code,code_c
CUSTOM = 0
BROKEN = 0
COYOTE_STEPS = 4
BUFFER_STEPS = 4
TEXT_CAP = 8
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
even
include "data.inc"
include "cases.inc"
even
include "display.inc"
include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
LEVEL: dc.b 0
PHASE: dc.b 0
PREVIOUS: dc.b 0
VALID: dc.b 0
GATE: dc.b 0
ATTACK: dc.b 0
DECAY: dc.b 0
SUSTAIN: dc.b 0
RELEASE: dc.b 0
T0: dc.b 0
even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
even
signature: dc.b "198XWORLDKIT000",0
even
copperlist:
dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
dc.w $180,$012,$182,$fff
copbpl:
dc.w $0e0,0,$0e2,0
dc.w $ffff,$fffe
plane: dcb.b 10240,0A client retaining state between calls
preview_rules:
ifne CUSTOM
; GATE = 1
move.b #1,d0
move.b d0,GATE
; ATTACK = 15
move.b #15,d0
move.b d0,ATTACK
; DECAY = 4
move.b #4,d0
move.b d0,DECAY
; SUSTAIN = 4
move.b #4,d0
move.b d0,SUSTAIN
; RELEASE = 4
move.b #4,d0
move.b d0,RELEASE
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
; GATE = 0
move.b #0,d0
move.b d0,GATE
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
rts
else
; GATE = 1
move.b #1,d0
move.b d0,GATE
; ATTACK = 3
move.b #3,d0
move.b d0,ATTACK
; DECAY = 2
move.b #2,d0
move.b d0,DECAY
; SUSTAIN = 6
move.b #6,d0
move.b d0,SUSTAIN
; RELEASE = 4
move.b #4,d0
move.b d0,RELEASE
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; PREVIOUS = 0
move.b #0,d0
move.b d0,PREVIOUS
endif
bsr checked_step
bsr emit_state
endif
rtsCustomise 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
section code,code_c
CUSTOM = 1
BROKEN = 0
COYOTE_STEPS = 2
BUFFER_STEPS = 2
TEXT_CAP = 4
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 49
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
even
include "data.inc"
include "cases.inc"
even
include "display.inc"
include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
LEVEL: dc.b 0
PHASE: dc.b 0
PREVIOUS: dc.b 0
VALID: dc.b 0
GATE: dc.b 0
ATTACK: dc.b 0
DECAY: dc.b 0
SUSTAIN: dc.b 0
RELEASE: dc.b 0
T0: dc.b 0
even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
even
signature: dc.b "198XWORLDKIT000",0
even
copperlist:
dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
dc.w $180,$012,$182,$fff
copbpl:
dc.w $0e0,0,$0e2,0
dc.w $ffff,$fffe
plane: dcb.b 10240,0The 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
section code,code_c
CUSTOM = 0
BROKEN = 1
COYOTE_STEPS = 4
BUFFER_STEPS = 4
TEXT_CAP = 8
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
even
include "data.inc"
include "cases.inc"
even
include "display.inc"
include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
LEVEL: dc.b 0
PHASE: dc.b 0
PREVIOUS: dc.b 0
VALID: dc.b 0
GATE: dc.b 0
ATTACK: dc.b 0
DECAY: dc.b 0
SUSTAIN: dc.b 0
RELEASE: dc.b 0
T0: dc.b 0
even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
even
signature: dc.b "198XWORLDKIT000",0
even
copperlist:
dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
dc.w $180,$012,$182,$fff
copbpl:
dc.w $0e0,0,$0e2,0
dc.w $ffff,$fffe
plane: dcb.b 10240,0
Cost and alternatives
The measured helper extent is 700 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.