Skip to content

Draw Without Repeating from a Bag on C64

Random pieces can arrive in long droughts.

Random pieces can arrive in long droughts. A bag guarantees each of eight choices appears once before a refill.

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

RESET nonzero fills B0–B7 with 0–7 and sets REMAINING=8; it does not draw. A draw requires REMAINING 1–8 and ROLL below it. Return the indexed choice, swap the last live item into its place and shorten the live prefix. CHOICE=255 and VALID=0 reject an empty bag or invalid index. Only the live prefix is meaningful; callers must preserve it.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: REMAINING, ROLL, RESET, CHOICE, VALID, B0, B1, B2, B3, B4, B5, B6, B7. They occupy 13 contiguous bytes. Also reserve the private workspace: EXPR_SAVE (1 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call shuffle_bag_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 shuffle_bag_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 REMAINING and B0–B7
Supply before service ROLL and RESET
Read after service CHOICE and VALID

Only the live prefix matters. Removing one item moves the final live item into its hole, so there is no need to shift all the later entries. This is a bag, not an order-preserving queue.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Draw Without Repeating from a Bag. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
shuffle_bag_init:
    lda #0
    sta REMAINING
    sta ROLL
    sta RESET
    sta CHOICE
    sta VALID
    sta B0
    sta B1
    sta B2
    sta B3
    sta B4
    sta B5
    sta B6
    sta B7
    sta EXPR_SAVE
    rts
shuffle_bag_step:
; CHOICE = 255
    lda #255
    sta CHOICE
; VALID = 0
    lda #0
    sta VALID
; if RESET != 0:
    lda RESET
    cmp #0
    bne shuffle_bag_L4
shuffle_bag_L5:
    jmp shuffle_bag_L2
shuffle_bag_L4:
    jmp shuffle_bag_L1
shuffle_bag_L1:
; B[0] = 0
    lda #0
    sta EXPR_SAVE
    ldx #0
    lda EXPR_SAVE
    sta B0,x
; B[1] = 1
    lda #1
    sta EXPR_SAVE
    ldx #1
    lda EXPR_SAVE
    sta B0,x
; B[2] = 2
    lda #2
    sta EXPR_SAVE
    ldx #2
    lda EXPR_SAVE
    sta B0,x
; B[3] = 3
    lda #3
    sta EXPR_SAVE
    ldx #3
    lda EXPR_SAVE
    sta B0,x
; B[4] = 4
    lda #4
    sta EXPR_SAVE
    ldx #4
    lda EXPR_SAVE
    sta B0,x
; B[5] = 5
    lda #5
    sta EXPR_SAVE
    ldx #5
    lda EXPR_SAVE
    sta B0,x
; B[6] = 6
    lda #6
    sta EXPR_SAVE
    ldx #6
    lda EXPR_SAVE
    sta B0,x
; B[7] = 7
    lda #7
    sta EXPR_SAVE
    ldx #7
    lda EXPR_SAVE
    sta B0,x
; REMAINING = 8
    lda #8
    sta REMAINING
; VALID = 1
    lda #1
    sta VALID
; return
    rts
    jmp shuffle_bag_L3
shuffle_bag_L2:
shuffle_bag_L3:
; if REMAINING == 0:
    lda REMAINING
    cmp #0
    beq shuffle_bag_L9
shuffle_bag_L10:
    jmp shuffle_bag_L7
shuffle_bag_L9:
    jmp shuffle_bag_L6
shuffle_bag_L6:
; return
    rts
    jmp shuffle_bag_L8
shuffle_bag_L7:
shuffle_bag_L8:
; if REMAINING > 8:
    lda REMAINING
    cmp #8
    beq shuffle_bag_L15
    bcs shuffle_bag_L14
shuffle_bag_L15:
    jmp shuffle_bag_L12
shuffle_bag_L14:
    jmp shuffle_bag_L11
shuffle_bag_L11:
; return
    rts
    jmp shuffle_bag_L13
shuffle_bag_L12:
shuffle_bag_L13:
; if ROLL >= REMAINING:
    lda ROLL
    cmp REMAINING
    bcs shuffle_bag_L19
shuffle_bag_L20:
    jmp shuffle_bag_L17
shuffle_bag_L19:
    jmp shuffle_bag_L16
shuffle_bag_L16:
; return
    rts
    jmp shuffle_bag_L18
shuffle_bag_L17:
shuffle_bag_L18:
; CHOICE = B[ROLL]
    ldx ROLL
    lda B0,x
    sta CHOICE
; REMAINING = REMAINING - 1
    lda REMAINING
    sec
    sbc #1
    sta REMAINING
; B[ROLL] = B[REMAINING]
    ldx REMAINING
    lda B0,x
    sta EXPR_SAVE
    ldx ROLL
    lda EXPR_SAVE
    sta B0,x
; VALID = 1
    lda #1
    sta VALID
    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

demo.asm
REMAINING=$80
ROLL=$81
RESET=$82
CHOICE=$83
VALID=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
EXPR_SAVE=$8d
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 {
; RESET = 1
    lda #1
    sta RESET
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 7
    lda #7
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 6
    lda #6
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 5
    lda #5
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 4
    lda #4
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 3
    lda #3
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 2
    lda #2
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 1
    lda #1
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 0
    lda #0
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; RESET = 1
    lda #1
    sta RESET
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
; RESET = 0
    lda #0
    sta RESET
; ROLL = 0
    lda #0
    sta ROLL
!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state

!if BROKEN {
; RESET = 1
    lda #1
    sta RESET
}
    jsr checked_step
    jsr emit_state
}
    rts

Customise it

Draw the last remaining live index each time instead of the first. 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
REMAINING=$80
ROLL=$81
RESET=$82
CHOICE=$83
VALID=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
EXPR_SAVE=$8d
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 00 00 01 00 01 02. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller requests RESET on every call. It continually refills but never draws. Refill once, draw from the live prefix, then decide explicitly what an empty bag should do.

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
REMAINING=$80
ROLL=$81
RESET=$82
CHOICE=$83
VALID=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
EXPR_SAVE=$8d
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 08 00 01 FF 01 00 01 02.
The wrong client completes with 08 00 01 FF 01 00 01 02; its changed inputs and state explain the result.

Cost and alternatives

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

A valid draw takes constant work. Refilling writes eight entries. An order-preserving removal would take more moves but suits a queue rather than a bag.

The helper does not secretly refill or pick a random index. Uniform draws require an unbiased roll in the current range. It protects indices, not arbitrary corruption of bag contents; only reset and this routine should mutate the live prefix.

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

Weighted choice: Independent weighted draws can repeat; a bag changes that promise by removing each chosen item until refill.

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