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

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
; 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:
ld a,0
ld (REMAINING),a
ld (ROLL),a
ld (RESET),a
ld (CHOICE),a
ld (VALID),a
ld (B0),a
ld (B1),a
ld (B2),a
ld (B3),a
ld (B4),a
ld (B5),a
ld (B6),a
ld (B7),a
ld (EXPR_SAVE),a
ret
shuffle_bag_step:
; CHOICE = 255
ld a,255
ld (CHOICE),a
; VALID = 0
ld a,0
ld (VALID),a
; if RESET != 0:
ld a,(RESET)
cp 0
jp nz,shuffle_bag_L1
jp shuffle_bag_L2
shuffle_bag_L1:
; B[0] = 0
ld a,0
ld c,a
ld a,0
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[1] = 1
ld a,1
ld c,a
ld a,1
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[2] = 2
ld a,2
ld c,a
ld a,2
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[3] = 3
ld a,3
ld c,a
ld a,3
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[4] = 4
ld a,4
ld c,a
ld a,4
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[5] = 5
ld a,5
ld c,a
ld a,5
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[6] = 6
ld a,6
ld c,a
ld a,6
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; B[7] = 7
ld a,7
ld c,a
ld a,7
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; REMAINING = 8
ld a,8
ld (REMAINING),a
; VALID = 1
ld a,1
ld (VALID),a
; return
ret
jp shuffle_bag_L3
shuffle_bag_L2:
shuffle_bag_L3:
; if REMAINING == 0:
ld a,(REMAINING)
cp 0
jp z,shuffle_bag_L4
jp shuffle_bag_L5
shuffle_bag_L4:
; return
ret
jp shuffle_bag_L6
shuffle_bag_L5:
shuffle_bag_L6:
; if REMAINING > 8:
ld a,(REMAINING)
cp 8
jp z,shuffle_bag_L8
jp nc,shuffle_bag_L7
jp shuffle_bag_L8
shuffle_bag_L7:
; return
ret
jp shuffle_bag_L9
shuffle_bag_L8:
shuffle_bag_L9:
; if ROLL >= REMAINING:
ld a,(ROLL)
ld b,a
ld a,(REMAINING)
ld c,a
ld a,b
cp c
jp nc,shuffle_bag_L10
jp shuffle_bag_L11
shuffle_bag_L10:
; return
ret
jp shuffle_bag_L12
shuffle_bag_L11:
shuffle_bag_L12:
; CHOICE = B[ROLL]
ld a,(ROLL)
ld e,a
ld d,0
ld hl,B0
add hl,de
ld a,(hl)
ld (CHOICE),a
; REMAINING = REMAINING - 1
ld a,(REMAINING)
sub 1
ld (REMAINING),a
; B[ROLL] = B[REMAINING]
ld a,(REMAINING)
ld e,a
ld d,0
ld hl,B0
add hl,de
ld a,(hl)
ld c,a
ld a,(ROLL)
ld e,a
ld d,0
ld hl,B0
add hl,de
ld (hl),c
; VALID = 1
ld a,1
ld (VALID),a
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. Scope or rename routine and field labels and allocate a separate block for each independent instance. 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
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:
REMAINING: defb 0
ROLL: defb 0
RESET: defb 0
CHOICE: defb 0
VALID: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
B4: defb 0
B5: defb 0
B6: defb 0
B7: defb 0
EXPR_SAVE: 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 startA client retaining state between calls
preview_rules:
if CUSTOM
; RESET = 1
ld a,1
ld (RESET),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 7
ld a,7
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 6
ld a,6
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 5
ld a,5
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 4
ld a,4
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 3
ld a,3
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 2
ld a,2
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 1
ld a,1
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 0
ld a,0
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
ret
else
; RESET = 1
ld a,1
ld (RESET),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
; RESET = 0
ld a,0
ld (RESET),a
; ROLL = 0
ld a,0
ld (ROLL),a
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
if BROKEN
; RESET = 1
ld a,1
ld (RESET),a
endif
call checked_step
call emit_state
endif
retCustomise 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
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:
REMAINING: defb 0
ROLL: defb 0
RESET: defb 0
CHOICE: defb 0
VALID: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
B4: defb 0
B5: defb 0
B6: defb 0
B7: defb 0
EXPR_SAVE: 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 startThe 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
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:
REMAINING: defb 0
ROLL: defb 0
RESET: defb 0
CHOICE: defb 0
VALID: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
B4: defb 0
B5: defb 0
B6: defb 0
B7: defb 0
EXPR_SAVE: 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
Cost and alternatives
The measured helper extent is 288 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 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
Weighted choice: Independent weighted draws can repeat; a bag changes that promise by removing each chosen item until refill.