Skip to content

Keep a Bounded Cell-edit Undo Log on NES

Undo should restore the previous cell value, including when the same cell was edited twice.

Undo should restore the previous cell value, including when the same cell was edited twice.

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

A sixteen-cell board has a four-entry undo stack: J stores cell indices and K their previous values. MODE=0 records then applies one CELL/VALUE edit; MODE=1 undoes the newest edit; MODE=2 clears history without changing the board. Full or empty history returns RESULT=0 with VALID=1 and leaves the board unchanged. Invalid mode, COUNT above four, edit index or popped log index returns VALID=0 without committing changes.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: RESULT, VALID, COUNT, MODE, CELL, VALUE, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15, J0, J1, J2, J3, K0, K1, K2, K3. They occupy 30 contiguous bytes. Also reserve the private workspace: T0, T1, T2, EXPR_SAVE (4 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call bounded_undo_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 bounded_undo_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 COUNT, C0–C15 and J0–J3/K0–K3
Supply before service MODE, CELL and VALUE
Read after service RESULT and VALID

Record the old value before writing the new one. Undo restores that saved value and shortens the stack only after validating the stored cell index.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Keep a Bounded Cell-edit Undo Log. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
bounded_undo_init:
    lda #0
    sta RESULT
    sta VALID
    sta COUNT
    sta MODE
    sta CELL
    sta VALUE
    sta C0
    sta C1
    sta C2
    sta C3
    sta C4
    sta C5
    sta C6
    sta C7
    sta C8
    sta C9
    sta C10
    sta C11
    sta C12
    sta C13
    sta C14
    sta C15
    sta J0
    sta J1
    sta J2
    sta J3
    sta K0
    sta K1
    sta K2
    sta K3
    sta T0
    sta T1
    sta T2
    sta EXPR_SAVE
    rts
bounded_undo_step:
; RESULT = 0
    lda #0
    sta RESULT
; VALID = 0
    lda #0
    sta VALID
; if COUNT > 4:
    lda COUNT
    cmp #4
    beq bounded_undo_L5
    bcs bounded_undo_L4
bounded_undo_L5:
    jmp bounded_undo_L2
bounded_undo_L4:
    jmp bounded_undo_L1
bounded_undo_L1:
; return
    rts
    jmp bounded_undo_L3
bounded_undo_L2:
bounded_undo_L3:
; if MODE >= 3:
    lda MODE
    cmp #3
    bcs bounded_undo_L9
bounded_undo_L10:
    jmp bounded_undo_L7
bounded_undo_L9:
    jmp bounded_undo_L6
bounded_undo_L6:
; return
    rts
    jmp bounded_undo_L8
bounded_undo_L7:
bounded_undo_L8:
; if MODE == 2:
    lda MODE
    cmp #2
    beq bounded_undo_L14
bounded_undo_L15:
    jmp bounded_undo_L12
bounded_undo_L14:
    jmp bounded_undo_L11
bounded_undo_L11:
; COUNT = 0
    lda #0
    sta COUNT
; VALID = 1
    lda #1
    sta VALID
; RESULT = 1
    lda #1
    sta RESULT
; return
    rts
    jmp bounded_undo_L13
bounded_undo_L12:
bounded_undo_L13:
; if MODE == 0:
    lda MODE
    cmp #0
    beq bounded_undo_L19
bounded_undo_L20:
    jmp bounded_undo_L17
bounded_undo_L19:
    jmp bounded_undo_L16
bounded_undo_L16:
; if CELL >= 16:
    lda CELL
    cmp #16
    bcs bounded_undo_L24
bounded_undo_L25:
    jmp bounded_undo_L22
bounded_undo_L24:
    jmp bounded_undo_L21
bounded_undo_L21:
; return
    rts
    jmp bounded_undo_L23
bounded_undo_L22:
bounded_undo_L23:
; VALID = 1
    lda #1
    sta VALID
; if COUNT == 4:
    lda COUNT
    cmp #4
    beq bounded_undo_L29
bounded_undo_L30:
    jmp bounded_undo_L27
bounded_undo_L29:
    jmp bounded_undo_L26
bounded_undo_L26:
; return
    rts
    jmp bounded_undo_L28
bounded_undo_L27:
bounded_undo_L28:
; T0 = C[CELL]
    ldx CELL
    lda C0,x
    sta T0
; J[COUNT] = CELL
    lda CELL
    sta EXPR_SAVE
    ldx COUNT
    lda EXPR_SAVE
    sta J0,x
; K[COUNT] = T0
    lda T0
    sta EXPR_SAVE
    ldx COUNT
    lda EXPR_SAVE
    sta K0,x
; C[CELL] = VALUE
    lda VALUE
    sta EXPR_SAVE
    ldx CELL
    lda EXPR_SAVE
    sta C0,x
; COUNT = COUNT + 1
    lda COUNT
    clc
    adc #1
    sta COUNT
; RESULT = 1
    lda #1
    sta RESULT
; return
    rts
    jmp bounded_undo_L18
bounded_undo_L17:
bounded_undo_L18:
; VALID = 1
    lda #1
    sta VALID
; if COUNT == 0:
    lda COUNT
    cmp #0
    beq bounded_undo_L34
bounded_undo_L35:
    jmp bounded_undo_L32
bounded_undo_L34:
    jmp bounded_undo_L31
bounded_undo_L31:
; return
    rts
    jmp bounded_undo_L33
bounded_undo_L32:
bounded_undo_L33:
; T0 = COUNT - 1
    lda COUNT
    sec
    sbc #1
    sta T0
; T1 = J[T0]
    ldx T0
    lda J0,x
    sta T1
; if T1 >= 16:
    lda T1
    cmp #16
    bcs bounded_undo_L39
bounded_undo_L40:
    jmp bounded_undo_L37
bounded_undo_L39:
    jmp bounded_undo_L36
bounded_undo_L36:
; VALID = 0
    lda #0
    sta VALID
; return
    rts
    jmp bounded_undo_L38
bounded_undo_L37:
bounded_undo_L38:
; T2 = K[T0]
    ldx T0
    lda K0,x
    sta T2
; C[T1] = T2
    lda T2
    sta EXPR_SAVE
    ldx T1
    lda EXPR_SAVE
    sta C0,x
; COUNT = T0
    lda T0
    sta COUNT
; RESULT = 1
    lda #1
    sta RESULT
    rts
component_end:

Machine contract

A/X/status may change; Y is preserved. The 2A03 has no decimal arithmetic mode. 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 NROM caller owns PPU setup and background display, with NMI and DMC disabled. Table and packet sizes are bounded to fit the cartridge caller and internal RAM; the helper owns no PPU registers.

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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
RESULT=$80
VALID=$81
COUNT=$82
MODE=$83
CELL=$84
VALUE=$85
C0=$86
C1=$87
C2=$88
C3=$89
C4=$8a
C5=$8b
C6=$8c
C7=$8d
C8=$8e
C9=$8f
C10=$90
C11=$91
C12=$92
C13=$93
C14=$94
C15=$95
J0=$96
J1=$97
J2=$98
J3=$99
K0=$9a
K1=$9b
K2=$9c
K3=$9d
T0=$9e
T1=$9f
T2=$a0
EXPR_SAVE=$a1
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
; CELL = 0
    lda #0
    sta CELL
; VALUE = 7
    lda #7
    sta VALUE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
; VALUE = 9
    lda #9
    sta VALUE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
; MODE = 1
    lda #1
    sta MODE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
    rts
.else
; CELL = 0
    lda #0
    sta CELL
; VALUE = 7
    lda #7
    sta VALUE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
; CELL = 1
    lda #1
    sta CELL
; VALUE = 9
    lda #9
    sta VALUE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
; MODE = 1
    lda #1
    sta MODE
.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; COUNT = 0
    lda #0
    sta COUNT
.endif
    jsr checked_step
    jsr emit_state
.endif
    rts

Customise it

Edit the same cell twice, then undo only the latest edit. 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
RESULT=$80
VALID=$81
COUNT=$82
MODE=$83
CELL=$84
VALUE=$85
C0=$86
C1=$87
C2=$88
C3=$89
C4=$8a
C5=$8b
C6=$8c
C7=$8d
C8=$8e
C9=$8f
C10=$90
C11=$91
C12=$92
C13=$93
C14=$94
C15=$95
J0=$96
J1=$97
J2=$98
J3=$99
K0=$9a
K1=$9b
K2=$9c
K3=$9d
T0=$9e
T1=$9f
T2=$a0
EXPR_SAVE=$a1
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0

The custom diagnostic row is 01 01 01 01 00 09 07 00. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears COUNT before each operation. Edits lose their history and undo has nothing to restore. Keep the log and its count together until an explicit clear or restart.

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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
RESULT=$80
VALID=$81
COUNT=$82
MODE=$83
CELL=$84
VALUE=$85
C0=$86
C1=$87
C2=$88
C3=$89
C4=$8a
C5=$8b
C6=$8c
C7=$8d
C8=$8e
C9=$8f
C10=$90
C11=$91
C12=$92
C13=$93
C14=$94
C15=$95
J0=$96
J1=$97
J2=$98
J3=$99
K0=$9a
K1=$9b
K2=$9c
K3=$9d
T0=$9e
T1=$9f
T2=$a0
EXPR_SAVE=$a1
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0
NES shows the deliberate error row 00 01 00 01 01 09 07 09.
The wrong client completes with 00 01 00 01 01 09 07 09; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 318 code bytes; public state is 30 bytes and private scratch is 4 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 edit or undo, with eight log bytes for four changes. Whole-state checkpoints are simpler for a tiny state; a larger transaction log needs capacity preflight and explicit grouping.

This is a LIFO edit log, not redo or a full game-state rewind. A move changing several cells needs a grouped transaction or several log entries committed together. Editing an unchanged value still consumes an entry in this deliberately simple contract.

Execution evidence

Four variants run on NTSC NROM, background only, DMC/NMI disabled, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 33 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

Checkpoints: A checkpoint restores one coherent snapshot; an undo log remembers a bounded sequence of individual changes.

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