Skip to content

Camera Dead Zones on NES

A camera need not move every time the player takes a step.

A camera need not move every time the player takes a step. Keep a quiet area on screen, and scroll only when the player pushes beyond it. The camera should also stop at the edge of the world rather than revealing an empty border.

NES shows the working diagnostic row 14 64 28 50 9B 01 01 00.
A native completed caller shows its state as hexadecimal bytes. The field order below explains the row; the full packet retains every call.

Include, initialise and call

Reserve CAMERA, PLAYER, LEFT_EDGE, RIGHT_EDGE, SCROLL_LIMIT, SHIFTED, VALID as 7 contiguous private bytes in that order. Include the helper once. Call camera_deadzone_init at the intended reset boundary: it clears every named byte, including input fields. Set the starting values afterwards, then call camera_deadzone_step for each intended service.

Initialise, establish valid world/view limits and update PLAYER in world coordinates. Service after movement, then pass CAMERA to the renderer’s coordinate conversion or scrolling backend. The normal 40–80 dead zone moves the camera from zero to ten for player 90, then to twenty for player 100.

CAMERA and PLAYER are unsigned world coordinates. LEFT_EDGE/RIGHT_EDGE are permitted screen-relative player thresholds, with LEFT_EDGE ≤ RIGHT_EDGE. SCROLL_LIMIT is the largest camera coordinate; the current CAMERA must already be within 0–SCROLL_LIMIT. Invalid bounds leave CAMERA unchanged and clear SHIFTED/VALID. Otherwise step sets VALID, moves just enough to put the player on the crossed threshold, clamps to 0–SCROLL_LIMIT and sets SHIFTED exactly when CAMERA changes. Player and bounds survive.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Camera Dead Zones. Contiguous private bytes: CAMERA, PLAYER, LEFT_EDGE, RIGHT_EDGE, SCROLL_LIMIT, SHIFTED, VALID.
; init clears all fields; set inputs afterwards. No I/O, waits or IRQ ownership.
; A/X/flags change; Y and decimal mode survive. D=0 for binary arithmetic.
camera_deadzone_init:
    lda #0
    sta CAMERA
    sta PLAYER
    sta LEFT_EDGE
    sta RIGHT_EDGE
    sta SCROLL_LIMIT
    sta SHIFTED
    sta VALID
    rts
camera_deadzone_step:
    lda #0
    sta SHIFTED
    sta VALID
    lda LEFT_EDGE
    cmp RIGHT_EDGE
    bcc camera_check_position
    bne camera_done
camera_check_position:
    lda CAMERA
    cmp SCROLL_LIMIT
    bcc camera_valid
    bne camera_done
camera_valid:
    inc VALID
    lda PLAYER
    sec
    sbc CAMERA
    bcc camera_left
    cmp LEFT_EDGE
    bcc camera_left
    cmp RIGHT_EDGE
    bcc camera_done
    beq camera_done
    lda PLAYER
    sec
    sbc RIGHT_EDGE
    jmp camera_cap
camera_left:
    lda PLAYER
    sec
    sbc LEFT_EDGE
    bcs camera_cap
    lda #0
camera_cap:
    cmp SCROLL_LIMIT
    bcc camera_store
    lda SCROLL_LIMIT
camera_store:
    cmp CAMERA
    beq camera_done
    sta CAMERA
    inc SHIFTED
camera_done:
    rts
component_end:

State, registers and ownership

Seven bytes. CAMERA persists; PLAYER and the three bounds are inputs; SHIFTED and VALID are replaced flags. PLAYER − CAMERA is computed without byte-wrap mistakes, including when the player lies to the left of the camera.

A/X/flags may change; Y and decimal mode survive. Clear decimal mode before binary arithmetic; the caller uses CLD. A direct call needs two return-address stack bytes.

The implementation binds fixed symbols and has one mutation owner. Independent users need separate state blocks and scoped/renamed labels, including routine labels; sharing the same bindings means sharing the state. Do not let an interrupt and the main loop mutate the same block without an explicit ownership policy.

The standalone cartridge owns its background/font and test RAM layout. Its bounded packet, preview and completion marker fit below the end of the NES’s 2 KiB internal RAM. The helper itself owns no PPU, DMA or audio registers.

A caller you can adapt

The buildable program supplies bindings and include order. Its separate client retains state between calls. checked_step calls the public camera_deadzone_step entry and checks register sentinels; emit_state records diagnostics. Your game can call the public entry directly, remove the diagnostics and consume the documented outputs.

Bindings, buildable caller and include order

demo.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
CAMERA=$80
PLAYER=$81
LEFT_EDGE=$82
RIGHT_EDGE=$83
SCROLL_LIMIT=$84
SHIFTED=$85
VALID=$86
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 separate client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
    lda #0
    sta CAMERA
    lda #90
    sta PLAYER
    lda #60
    sta LEFT_EDGE
    lda #60
    sta RIGHT_EDGE
    lda #155
    sta SCROLL_LIMIT
.if BROKEN
    lda PLAYER
    sec
    sbc CAMERA
    sta PLAYER
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #100
    sta PLAYER
.if BROKEN
    lda PLAYER
    sec
    sbc CAMERA
    sta PLAYER
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
.else
    lda #0
    sta CAMERA
    lda #90
    sta PLAYER
    lda #40
    sta LEFT_EDGE
    lda #80
    sta RIGHT_EDGE
    lda #155
    sta SCROLL_LIMIT
.if BROKEN
    lda PLAYER
    sec
    sbc CAMERA
    sta PLAYER
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #100
    sta PLAYER
.if BROKEN
    lda PLAYER
    sec
    sbc CAMERA
    sta PLAYER
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
.endif
    rts

The display fields are CAMERA, PLAYER, LEFT_EDGE, RIGHT_EDGE, SCROLL_LIMIT, SHIFTED, VALID; remaining display fields are zero. Each is one hexadecimal byte. The normal row is 14 64 28 50 9B 01 01 00. This is a completed state observation, not a recording of an actual character jumping, a camera scrolling or an asset being displayed.

Make it your own

The custom caller sets both thresholds to 60. The dead zone has zero width, so the camera tracks the player at that screen position until a world edge prevents it. The two calls finish at camera 40 instead of 20.

Maintained custom configuration or caller

custom.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
CAMERA=$80
PLAYER=$81
LEFT_EDGE=$82
RIGHT_EDGE=$83
SCROLL_LIMIT=$84
SHIFTED=$85
VALID=$86
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 row is 28 64 3C 3C 9B 01 01 00. These records qualify the named builds and inputs; another layout, format or input domain needs explicit bounds and fresh execution evidence.

Common mistake and repair

The wrong client subtracts CAMERA from PLAYER before calling the world-coordinate API. The second request supplies screen position 90 in place of world position 100, leaving the camera at ten. Keep world and screen coordinates separate and subtract the camera only in the rendering path.

Select the deliberately wrong client

wrong.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
CAMERA=$80
PLAYER=$81
LEFT_EDGE=$82
RIGHT_EDGE=$83
SCROLL_LIMIT=$84
SHIFTED=$85
VALID=$86
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 fault row 0A 5A 28 50 9B 00 01 00.
The wrong client completes with 0A 5A 28 50 9B 00 01 00. Its code and the explanation above identify the cause; the capture observes the result.

The BROKEN branch is visible in the client. repaired.asm restores the normal selection; its executable matches demo exactly. Spectrum tape filenames may differ while loaded CODE remains identical. The fault is a labelled experiment, not a suggested alternative.

Cost, limits and verification

The measured layout has 90 code bytes, 0 immutable data bytes and 7 private state bytes. Alignment, stack usage, diagnostic tables and display machinery are additional. The extents come from the loaded program’s symbols, not from counting source lines.

A relative-position comparison and one clamped correction, with no map copy or display transfer. C64/Amiga hardware capabilities are not reduced to this logical byte coordinate; their renderers may need wider and differently split scroll values.

The byte world is a bounded example, not a universal camera range. Wider maps need wider coordinates; smoothing adds fractional state and a time-step policy. Hardware coarse/fine scrolling, exposed map columns and parallax remain separate display-specific work. If bounds shrink, first move CAMERA into the new valid interval.

Four native runs use NTSC mapper-0 NES cartridge, CHR-ROM diagnostic font, NMI/DMC disabled; Asm198x 0.0.72 and Emu198x 0.44.2. Each checks 64 boundary/varied/error cases, dirty initialisation and every state of a separate sequential client against an independent integer model. This is not exhaustive coverage of every possible byte combination. Source/tool/firmware hashes, immutable loaded code/data, native startup/completion, preserved-register sentinels, helper/data sizes, screenshot bytes/pixels and exact repair are checked. Deliberately altered observations must fail. Firmware is hashed, never distributed.

Related technique · Maths for Games · The Craft.

Source and native evidence.