Thirty updates from fifty display ticks should be spaced out, not rounded down to zero.

Use it in a project
NUMERATOR must be at most DENOMINATOR, DENOMINATOR must be nonzero and PHASE must be below it. Add the numerator to the retained phase; return FIRED=1 on a crossing and subtract the denominator. At most one event per call. Invalid input returns FIRED=0 and VALID=0 without changing PHASE.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: PHASE, NUMERATOR, DENOMINATOR, FIRED, VALID. They occupy 5 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 rational_tick_divider_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 rational_tick_divider_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 | PHASE |
| Supply before service | NUMERATOR and DENOMINATOR |
| Read after service | FIRED and VALID |
Think of PHASE as stored credit. Crossing one denominator spends that much credit and emits an event. The implementation tests the distance to the threshold before adding, so an intermediate byte cannot wrap.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Divide Ticks Without Losing the Remainder. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
rational_tick_divider_init:
lda #0
sta PHASE
sta NUMERATOR
sta DENOMINATOR
sta FIRED
sta VALID
sta T0
rts
rational_tick_divider_step:
; FIRED = 0
lda #0
sta FIRED
; VALID = 0
lda #0
sta VALID
; if DENOMINATOR == 0:
lda DENOMINATOR
cmp #0
beq rational_tick_divider_L4
rational_tick_divider_L5:
jmp rational_tick_divider_L2
rational_tick_divider_L4:
jmp rational_tick_divider_L1
rational_tick_divider_L1:
; return
rts
jmp rational_tick_divider_L3
rational_tick_divider_L2:
rational_tick_divider_L3:
; if NUMERATOR > DENOMINATOR:
lda NUMERATOR
cmp DENOMINATOR
beq rational_tick_divider_L10
bcs rational_tick_divider_L9
rational_tick_divider_L10:
jmp rational_tick_divider_L7
rational_tick_divider_L9:
jmp rational_tick_divider_L6
rational_tick_divider_L6:
; return
rts
jmp rational_tick_divider_L8
rational_tick_divider_L7:
rational_tick_divider_L8:
; if PHASE >= DENOMINATOR:
lda PHASE
cmp DENOMINATOR
bcs rational_tick_divider_L14
rational_tick_divider_L15:
jmp rational_tick_divider_L12
rational_tick_divider_L14:
jmp rational_tick_divider_L11
rational_tick_divider_L11:
; return
rts
jmp rational_tick_divider_L13
rational_tick_divider_L12:
rational_tick_divider_L13:
; VALID = 1
lda #1
sta VALID
; T0 = DENOMINATOR - NUMERATOR
lda DENOMINATOR
sec
sbc NUMERATOR
sta T0
; if PHASE >= T0:
lda PHASE
cmp T0
bcs rational_tick_divider_L19
rational_tick_divider_L20:
jmp rational_tick_divider_L17
rational_tick_divider_L19:
jmp rational_tick_divider_L16
rational_tick_divider_L16:
; PHASE = PHASE - T0
lda PHASE
sec
sbc T0
sta PHASE
; FIRED = 1
lda #1
sta FIRED
jmp rational_tick_divider_L18
rational_tick_divider_L17:
; PHASE = PHASE + NUMERATOR
lda PHASE
clc
adc NUMERATOR
sta PHASE
rational_tick_divider_L18:
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. Scope or rename routine and field labels and allocate a separate block for each independent instance. 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
PHASE=$80
NUMERATOR=$81
DENOMINATOR=$82
FIRED=$83
VALID=$84
T0=$85
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 {
; NUMERATOR = 1
lda #1
sta NUMERATOR
; DENOMINATOR = 3
lda #3
sta DENOMINATOR
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
rts
} else {
; NUMERATOR = 3
lda #3
sta NUMERATOR
; DENOMINATOR = 5
lda #5
sta DENOMINATOR
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
!if BROKEN {
; PHASE = 0
lda #0
sta PHASE
}
jsr checked_step
jsr emit_state
}
rtsCustomise it
Use one event per three calls instead of three per five. 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
PHASE=$80
NUMERATOR=$81
DENOMINATOR=$82
FIRED=$83
VALID=$84
T0=$85
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 01 01 03 00 01 00 00 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller throws away PHASE before each tick. A fraction smaller than one never accumulates into an event. Keep the remainder even on ticks that return FIRED=0.
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
PHASE=$80
NUMERATOR=$81
DENOMINATOR=$82
FIRED=$83
VALID=$84
T0=$85
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 118 code bytes; public state is 5 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 with no division instruction or table. Allowing a numerator larger than the denominator would require multiple events or a reported event count.
The subtract-before-add arrangement avoids an overflowing byte even when the denominator is 255. This is scheduling, not interpolation or an unlimited catch-up loop. A caller choosing frames rather than real time inherits that machine’s refresh rate.
Execution evidence
Four variants run on PAL stock-ROM PRG, 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
Animation timeline: Use the emitted tick to service an animation when its logical rate differs from the display rate.