liveBook · Manning

Zero-page memory

As explained in Classic Game Programming on the NES: Make your own retro video game

Zero-page memory is the first 256 bytes of 6502 address space, whose locations can be specified with a one-byte address.

Zero-page memory is the first 256 bytes of 6502 RAM. A location there uses a one-byte address rather than the two-byte address required elsewhere in the 64K address space, so zero-page instructions execute faster than normal memory instructions. It fills the gap between the 6502’s few, fastest registers and its larger, slower RAM. section 3.2 section 3.2.4

Treat zero page as a compact area for program variables. It holds data that must persist outside A, X, and Y registers, while remaining efficient to load and store. On the NES, zero-page declarations reserve RAM only: code belongs in ROM, not in this RAM area. section 3.3.2 section 5.6

Choose the addressing form by the location and operation needed:

  • Load from a zero-page location by omitting # before an address from $00 through $FF. For example, lda $05 reads the byte stored at $05; lda #$05 instead loads the literal value $05. section 3.3.1
  • Store a register value with sta, stx, or sty and a zero-page destination, such as sta $06, stx $07, or sty $08. section 3.3.2
  • Use X or Y indexing where supported to calculate a zero-page location, such as lda $01,X or sta $01,X. section 3.3.1 section 3.3.2
  • Use two zero-page bytes as an address when indirect indexed addressing is needed, such as lda ($05),Y or sta ($05),Y. section 3.3.1 section 3.3.2

Reserve game variables

Place variables in a ZEROPAGE segment with .res. A one-byte nmi_ready flag can control whether rendering proceeds, gamepad can hold controller input, and d_x and d_y can store ball velocity. These labels reserve RAM locations that later instructions can access with zero-page forms. section 5.6 section 5.2.2

Listing 5.6 Our zero-page memory allocations (example.s)
;*****************************************************************
; 6502 Zero Page Memory (256 bytes)
;*****************************************************************
  
.segment "ZEROPAGE"
  
nmi_ready:        .res 1     #1
                             #1
gamepad:        .res 1       #2
  
d_x:            .res 1       #3
d_y:            .res 1       #4

This allocation listing shows labeled one-byte variables reserved in the ZEROPAGE segment. section 5.6

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Defines one-byte zero-page addressing and demonstrates direct, indexed, and indirect loads and stores without processor internals.
Examples
Demonstrates loading and storing bytes at $05–$08, indexed accesses, indirect addresses, and variables nmi_ready, gamepad, d_x, and d_y.
What it assumes
Teaches zero-page access from basic registers and memory concepts while using 6502 assembly syntax.
What you can do after
Apply it via 6502 load and store instructions to reserve and access fast RAM variables.
How it connects
Connects registers, RAM, addressing modes, indirect indexed addressing, assembler segments, and NES controller input.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.