Skip to content

Limit Hits to an Attack Window on C64

A sword can overlap the same enemy for several frames without dealing damage on every frame.

A sword can overlap the same enemy for several frames without dealing damage on every frame.

C64 shows the working diagnostic row 00 01 02 03 02 05 01 01.
A completed native caller displays 00 01 02 03 02 05 01 01. 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

START_FRAME..END_FRAME is a half-open active interval, with START_FRAME strictly below END_FRAME. FRAME is the caller’s current attack frame. TARGET is 0–7. A nonzero NEW_ATTACK clears the retained SEEN bitset before testing contact. In the active interval, nonzero CONTACT emits HIT=1 once per target and records its bit. Outside the interval HIT=0. Invalid interval or target clears HIT/VALID and preserves SEEN.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: HIT, VALID, SEEN, FRAME, START_FRAME, END_FRAME, TARGET, CONTACT, NEW_ATTACK. They occupy 9 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 attack_hit_window_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 attack_hit_window_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 SEEN
Supply before service frame interval, TARGET, CONTACT and NEW_ATTACK
Read after service HIT, SEEN and VALID

The active interval decides when damage is permitted; the bitset decides whether this attack already affected that target. They answer different questions and are checked independently.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Limit Hits to an Attack Window. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
attack_hit_window_init:
    lda #0
    sta HIT
    sta VALID
    sta SEEN
    sta FRAME
    sta START_FRAME
    sta END_FRAME
    sta TARGET
    sta CONTACT
    sta NEW_ATTACK
    sta T0
    sta T1
    rts
attack_hit_window_step:
; HIT = 0
    lda #0
    sta HIT
; VALID = 0
    lda #0
    sta VALID
; if START_FRAME >= END_FRAME:
    lda START_FRAME
    cmp END_FRAME
    bcs attack_hit_window_L4
attack_hit_window_L5:
    jmp attack_hit_window_L2
attack_hit_window_L4:
    jmp attack_hit_window_L1
attack_hit_window_L1:
; return
    rts
    jmp attack_hit_window_L3
attack_hit_window_L2:
attack_hit_window_L3:
; if TARGET >= 8:
    lda TARGET
    cmp #8
    bcs attack_hit_window_L9
attack_hit_window_L10:
    jmp attack_hit_window_L7
attack_hit_window_L9:
    jmp attack_hit_window_L6
attack_hit_window_L6:
; return
    rts
    jmp attack_hit_window_L8
attack_hit_window_L7:
attack_hit_window_L8:
; VALID = 1
    lda #1
    sta VALID
; if NEW_ATTACK != 0:
    lda NEW_ATTACK
    cmp #0
    bne attack_hit_window_L14
attack_hit_window_L15:
    jmp attack_hit_window_L12
attack_hit_window_L14:
    jmp attack_hit_window_L11
attack_hit_window_L11:
; SEEN = 0
    lda #0
    sta SEEN
    jmp attack_hit_window_L13
attack_hit_window_L12:
attack_hit_window_L13:
; if FRAME < START_FRAME:
    lda FRAME
    cmp START_FRAME
    bcc attack_hit_window_L19
attack_hit_window_L20:
    jmp attack_hit_window_L17
attack_hit_window_L19:
    jmp attack_hit_window_L16
attack_hit_window_L16:
; return
    rts
    jmp attack_hit_window_L18
attack_hit_window_L17:
attack_hit_window_L18:
; if FRAME >= END_FRAME:
    lda FRAME
    cmp END_FRAME
    bcs attack_hit_window_L24
attack_hit_window_L25:
    jmp attack_hit_window_L22
attack_hit_window_L24:
    jmp attack_hit_window_L21
attack_hit_window_L21:
; return
    rts
    jmp attack_hit_window_L23
attack_hit_window_L22:
attack_hit_window_L23:
; if CONTACT == 0:
    lda CONTACT
    cmp #0
    beq attack_hit_window_L29
attack_hit_window_L30:
    jmp attack_hit_window_L27
attack_hit_window_L29:
    jmp attack_hit_window_L26
attack_hit_window_L26:
; return
    rts
    jmp attack_hit_window_L28
attack_hit_window_L27:
attack_hit_window_L28:
; T0 = 1
    lda #1
    sta T0
; T1 = TARGET
    lda TARGET
    sta T1
; while T1 != 0:
attack_hit_window_L31:
    lda T1
    cmp #0
    bne attack_hit_window_L34
attack_hit_window_L35:
    jmp attack_hit_window_L33
attack_hit_window_L34:
    jmp attack_hit_window_L32
attack_hit_window_L32:
; T0 = T0 << 1
    lda T0
    asl
    sta T0
; T1 = T1 - 1
    lda T1
    sec
    sbc #1
    sta T1
    jmp attack_hit_window_L31
attack_hit_window_L33:
; T1 = SEEN & T0
    lda SEEN
    and T0
    sta T1
; if T1 == 0:
    lda T1
    cmp #0
    beq attack_hit_window_L39
attack_hit_window_L40:
    jmp attack_hit_window_L37
attack_hit_window_L39:
    jmp attack_hit_window_L36
attack_hit_window_L36:
; SEEN = SEEN | T0
    lda SEEN
    ora T0
    sta SEEN
; HIT = 1
    lda #1
    sta HIT
    jmp attack_hit_window_L38
attack_hit_window_L37:
attack_hit_window_L38:
    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

demo.asm
HIT=$80
VALID=$81
SEEN=$82
FRAME=$83
START_FRAME=$84
END_FRAME=$85
TARGET=$86
CONTACT=$87
NEW_ATTACK=$88
T0=$89
T1=$8a
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

exercise.inc, excerpt
preview_rules:
!if CUSTOM {
; START_FRAME = 1
    lda #1
    sta START_FRAME
; END_FRAME = 3
    lda #3
    sta END_FRAME
; FRAME = 1
    lda #1
    sta FRAME
; TARGET = 7
    lda #7
    sta TARGET
; CONTACT = 1
    lda #1
    sta CONTACT
; NEW_ATTACK = 1
    lda #1
    sta NEW_ATTACK
!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state
; NEW_ATTACK = 0
    lda #0
    sta NEW_ATTACK
; TARGET = 0
    lda #0
    sta TARGET
!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state
; FRAME = 3
    lda #3
    sta FRAME
!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; START_FRAME = 2
    lda #2
    sta START_FRAME
; END_FRAME = 5
    lda #5
    sta END_FRAME
; FRAME = 2
    lda #2
    sta FRAME
; TARGET = 1
    lda #1
    sta TARGET
; CONTACT = 1
    lda #1
    sta CONTACT
; NEW_ATTACK = 1
    lda #1
    sta NEW_ATTACK
!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state
; NEW_ATTACK = 0
    lda #0
    sta NEW_ATTACK
; FRAME = 3
    lda #3
    sta FRAME
!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; SEEN = 0
    lda #0
    sta SEEN
}
    jsr checked_step
    jsr emit_state
}
    rts

Customise it

Strike target seven, then a different target, and stop exactly at the exclusive end frame. 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
HIT=$80
VALID=$81
SEEN=$82
FRAME=$83
START_FRAME=$84
END_FRAME=$85
TARGET=$86
CONTACT=$87
NEW_ATTACK=$88
T0=$89
T1=$8a
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 01 81 03 01 03 00 01. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong client clears the hit history every service. The same target is hit repeatedly while the attack remains active. Reset SEEN once per new attack, not once per collision candidate.

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
HIT=$80
VALID=$81
SEEN=$82
FRAME=$83
START_FRAME=$84
END_FRAME=$85
TARGET=$86
CONTACT=$87
NEW_ATTACK=$88
T0=$89
T1=$8a
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:
C64 shows the deliberate error row 01 01 02 03 02 05 01 01.
The wrong client completes with 01 01 02 03 02 05 01 01; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 203 code bytes; public state is 9 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.

At most seven shifts and one bitset update. Larger target sets need more bits or handles; per-attack storage grows with the identity space.

Eight stable target IDs are available per attack. Slot reuse during one attack requires generation-aware identity or clearing the relevant bit under an explicit policy. The caller owns animation, overlap, faction rules and actual damage. Do not set NEW_ATTACK on every candidate in one frame.

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

Damage rules: After this attack grants a hit, apply the target’s invulnerability and health rules separately.

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