2. Nintendo Entertainment System (NES)

The console that saved the video game industry

The Nintendo Entertainment System launched in Japan in 1983 (as the Famicom) and in North America in 1985. It sold over 60 million units worldwide and single-handedly revived the console market after the crash of 1983. Super Mario Bros., The Legend of Zelda, Metroid, Mega Man, Castlevania — the NES library defined what video games could be.

From a hardware perspective, the NES is a fascinating machine. It shares the same CPU family as the Commodore 64 (the 6502), but wraps it in a completely different architecture — one designed from the ground up for scrolling tile-based games. The graphics hardware doesn't work with pixels or bitmaps. It works with tiles, nametables, and pattern tables — a vocabulary that will show up again in the Game Boy and SNES.

The System at a Glance

NES Specifications

CPURicoh 2A03 @ 1.79 MHz (NTSC) / 1.66 MHz (PAL)
CPU RAM2 KB internal
GraphicsPPU (2C02) — 256x240, 52 colors, 64 sprites, scrolling nametables
PPU RAM2 KB VRAM + 256 bytes OAM + 32 bytes palette
SoundAPU (built into CPU) — 2 pulse, 1 triangle, 1 noise, 1 DMC (sample)
CartridgePRG ROM (code) + CHR ROM/RAM (graphics) + optional mapper chip
Video outputNTSC or PAL composite

The Ricoh 2A03 CPU

The NES CPU is a Ricoh 2A03, which is a MOS 6502 core with one omission and one addition. The omission: no decimal mode — the SED instruction exists but does nothing. The addition: the APU (audio hardware) is integrated directly onto the CPU die.

If you've read the C64 article, you already know the 6502. The same registers (A, X, Y, SP, PC, P), the same addressing modes, the same instruction set. The only thing you lose is BCD arithmetic, which games never needed anyway.

The 2A03 runs at 1.79 MHz (NTSC) — nearly twice the C64's clock speed. But the NES has only 2 KB of RAM compared to the C64's 64 KB. Those 2 KB are mapped at $0000-$07FF and mirrored through $1FFF (reading $0800 gives you the same byte as $0000).

Zero Page and Stack

Just like the C64, zero page ($0000-$00FF) is your fast-access workspace and the stack lives at $0100-$01FF. With only 2 KB total, you need to be disciplined about memory usage. Many NES games use zero page for all their active variables and the remaining 1.5 KB for level data, animation state, and buffers.

Memory Map

The 2A03's 16-bit address bus gives it a 64 KB address space, but most of it is cartridge-controlled.

CPU Memory Map

$FFFF ┌─────────────────────┐
      │                     │
      │   PRG ROM upper     │ Cartridge program ROM
      │   (16 KB)           │ (or mapper-switched bank)
      │                     │
$C000 ├─────────────────────┤
      │                     │
      │   PRG ROM lower     │ Cartridge program ROM
      │   (16 KB)           │ (or mapper-switched bank)
      │                     │
$8000 ├─────────────────────┤
      │   Cartridge space   │ Mapper registers, save RAM
$6000 ├─────────────────────┤ ($6000-$7FFF often battery-backed SRAM)
      │   Expansion ROM     │ Rarely used
$4020 ├─────────────────────┤
      │   APU & I/O         │ $4000-$4017: APU, controller, OAM DMA
$4000 ├─────────────────────┤
      │   PPU registers     │ $2000-$2007, mirrored through $3FFF
$2000 ├─────────────────────┤
      │   RAM mirrors       │ $0000-$07FF mirrored 3 times
$0800 ├─────────────────────┤
      │   Internal RAM      │ 2 KB: zero page, stack, general purpose
$0000 └─────────────────────┘

The key insight: the NES has almost no onboard memory. The cartridge is the system. A cartridge provides the program ROM, the graphics data, and often additional RAM or custom hardware. This is why NES cartridges vary so wildly in capability — the mapper chip on the cartridge board can add bank switching, extra RAM, scanline counters, and even additional sound channels.

PPU Registers

The CPU communicates with the PPU through only 8 registers, mapped at $2000-$2007 (mirrored every 8 bytes through $3FFF):

PPU Registers (CPU side)

$2000PPUCTRLNMI enable, sprite size, pattern table select, nametable select, VRAM increment
$2001PPUMASKColor emphasis, sprite/background enable, left-column clipping
$2002PPUSTATUSVBlank flag, sprite 0 hit, sprite overflow (read-only)
$2003OAMADDROAM address for write (write-only)
$2004OAMDATAOAM data read/write
$2005PPUSCROLLScroll position (write x, then y — two writes)
$2006PPUADDRVRAM address (write high, then low — two writes)
$2007PPUDATAVRAM data read/write

The Latch

Several PPU registers use a two-write latch. Writing to $2005 twice sets the X scroll then the Y scroll. Writing to $2006 twice sets the high byte then low byte of the VRAM address. Reading $2002 resets the latch. This is a constant source of bugs in NES programming — if you lose track of the latch state, your writes go to the wrong place.

The PPU — Picture Processing Unit

The PPU is where the NES diverges most dramatically from the C64. Instead of a character-mode display with a bitmap option, the PPU is a tile engine with hardware scrolling built in.

The Rendering Pipeline

Every frame, the PPU renders 240 visible scanlines (plus 22 lines of VBlank). For each scanline, it composites two layers:

  1. Background — a scrollable grid of 8x8 tiles drawn from a nametable
  2. Sprites — up to 64 independently positioned objects (8x8 or 8x16), with a maximum of 8 per scanline

The PPU has its own 16 KB address space, completely separate from the CPU's. The CPU cannot see PPU memory directly — it must read and write through the $2006/$2007 register pair.

PPU Memory Map

PPU Address Space (16 KB)

$3FFF ┌─────────────────────┐
      │   Mirrors           │ $3000-$3EFF mirrors $2000-$2EFF
$3F00 ├─────────────────────┤
      │   Palette RAM (32B) │ Background + sprite palettes
$3F00 ├─────────────────────┤
$2FFF │                     │
      │   Nametables (2 KB) │ 4 logical nametables (2 physical)
      │                     │ Each: 960 tile indices + 64 attribute bytes
$2000 ├─────────────────────┤
      │                     │
      │   Pattern tables    │ 8 KB: 512 tiles (256 left + 256 right)
      │   (CHR ROM/RAM)     │ Each tile: 16 bytes (8x8 pixels, 2 bits/pixel)
      │                     │
$0000 └─────────────────────┘

Pattern Tables (CHR)

Pattern tables hold the actual tile graphics. Each tile is 8x8 pixels with 2 bits per pixel, stored as two 8-byte bit planes:

Tile Format — Two Bit Planes

Plane 0 (low bits):      Plane 1 (high bits):     Combined (2-bit values):
.#.#.#..  = $54          ..####..  = $3C           01 02 01 02 01 02 00 00
.#.#.#..  = $54          ..####..  = $3C           01 02 01 02 01 02 00 00
........  = $00          ########  = $FF           02 02 02 02 02 02 02 02
........  = $00          ########  = $FF           02 02 02 02 02 02 02 02
#.#.#.#.  = $AA          ########  = $FF           03 02 03 02 03 02 03 02
#.#.#.#.  = $AA          ########  = $FF           03 02 03 02 03 02 03 02
........  = $00          ..####..  = $3C           02 02 02 02 02 02 00 00
........  = $00          ..####..  = $3C           02 02 02 02 02 02 00 00

Total: 16 bytes per tile (8 bytes plane 0 + 8 bytes plane 1)

The 2-bit pixel value (0-3) indexes into a 4-color palette. Value 0 is always transparent for sprites and the shared background color for backgrounds.

Palettes

The PPU has 32 bytes of palette RAM at $3F00-$3F1F:

  • $3F00-$3F0F: 4 background palettes (4 colors each)
  • $3F10-$3F1F: 4 sprite palettes (4 colors each)

Each palette entry is an index into the NES's master color table of 52 unique colors. The NES palette cannot be reprogrammed — you choose from the fixed set.

; Set background palette 0 to: sky blue, dark blue, white, red
    LDA $2002       ; reset the PPU latch
    LDA #$3F
    STA $2006       ; VRAM address high byte
    LDA #$00
    STA $2006       ; VRAM address low byte ($3F00)

    LDA #$21        ; sky blue
    STA $2007
    LDA #$02        ; dark blue
    STA $2007
    LDA #$30        ; white
    STA $2007
    LDA #$16        ; red
    STA $2007

Nametables

A nametable is a 32x30 grid of tile indices — 960 bytes that describe one screen's worth of background. The PPU has space for 4 logical nametables arranged in a 2x2 grid, but only 2 KB of physical VRAM — so two nametables are real and two are mirrors. The cartridge wiring determines the mirroring arrangement:

Mirroring Layout Used for
Horizontal A A / B B Vertical scrolling (up/down)
Vertical A B / A B Horizontal scrolling (left/right)
Single-screen A A / A A Special cases
Four-screen A B / C D Requires extra VRAM on cartridge

Nametable Mirroring

Mirroring is a hardware decision, not a software one. It's determined by how the cartridge's address lines are wired. Horizontal mirroring gives you two nametables stacked vertically — perfect for games that scroll up and down. Vertical mirroring gives you two nametables side by side — perfect for side-scrollers like Super Mario Bros. Mapper chips can switch mirroring dynamically.

Attribute Tables

Each nametable has a 64-byte attribute table at the end that assigns palettes to the background tiles. But here's the constraint: palette assignments work on 16x16 pixel blocks (2x2 tiles), not individual tiles. Each attribute byte covers a 32x32 pixel area (4x4 tiles), packed as four 2-bit palette selectors:

; Attribute byte layout for a 32x32 pixel block:
;
;   +-------+-------+
;   | Tiles | Tiles |
;   | 0,0   | 2,0   |  bits 0-1 = top-left     (2x2 tiles)
;   | 1,0   | 3,0   |  bits 2-3 = top-right    (2x2 tiles)
;   +-------+-------+  bits 4-5 = bottom-left  (2x2 tiles)
;   | Tiles | Tiles |  bits 6-7 = bottom-right (2x2 tiles)
;   | 0,2   | 2,2   |
;   | 1,2   | 3,2   |
;   +-------+-------+
;
; Example: top-left uses palette 0, top-right palette 1,
;          bottom-left palette 2, bottom-right palette 3:
;   .byte %11_10_01_00   ; = $E4

This coarse palette assignment is why NES backgrounds have that characteristic blocky color look — you can't change palette every tile, only every 2x2 group.

Scrolling

The PPU has hardware scrolling via $2005 (PPUSCROLL). You write the X scroll followed by the Y scroll, and the PPU handles the rest — compositing from two nametables seamlessly.

; Set scroll to (120, 0) — scroll 120 pixels to the right
    LDA $2002       ; reset latch
    LDA #120
    STA $2005       ; X scroll
    LDA #0
    STA $2005       ; Y scroll

For scrolling past 256 pixels (one nametable width), you also need to set the base nametable in PPUCTRL ($2000 bits 0-1). Multi-directional scrolling requires careful nametable management — updating the off-screen column or row of tiles before they scroll into view.

Sprites

The NES supports 64 sprites stored in OAM (Object Attribute Memory) — 256 bytes of dedicated memory inside the PPU, separate from VRAM.

OAM Format

Each sprite is 4 bytes:

OAM Entry (4 bytes per sprite)

Byte 0Y position (top of sprite, minus 1)
Byte 1Tile index (from pattern table)
Byte 2Attributes: palette (bits 0-1), priority (bit 5), flip H (bit 6), flip V (bit 7)
Byte 3X position (left of sprite)

OAM DMA

Writing 256 bytes to OAM through $2004 one byte at a time would be painfully slow. Instead, you use OAM DMA: write a page number to $4014 and the hardware copies 256 bytes from that CPU RAM page to OAM in 513 cycles.

; Copy sprite data from $0200-$02FF to OAM
    LDA #$02
    STA $4014       ; triggers DMA: CPU page $02 → OAM
                    ; CPU is halted for 513 cycles during transfer

Most NES games keep a shadow copy of OAM in CPU RAM (typically at $0200) and DMA it to the PPU every frame during VBlank.

Sprite Limitations

  • 8 sprites per scanline — if more than 8 sprites share a scanline, the PPU drops the extras. Games work around this by cycling sprite priority each frame, causing the "flickering" effect seen in many NES games.
  • 64 sprites total — 256 bytes of OAM, 4 bytes each.
  • No sprite scaling or rotation — but you can flip horizontally and vertically.
  • Sprite 0 hit — the PPU sets a flag in $2002 when sprite 0's non-transparent pixel overlaps a non-transparent background pixel. Games use this for split-screen effects (status bars) by polling for the hit, then changing scroll values mid-frame.

The APU — Audio Processing Unit

The APU is built into the 2A03 CPU and provides 5 sound channels, mapped at $4000-$4017:

APU Channels

Pulse 1$4000-$4003Square wave with variable duty cycle (12.5%, 25%, 50%, 75%), volume envelope, sweep
Pulse 2$4004-$4007Same as Pulse 1
Triangle$4008-$400BTriangle wave, no volume control (always full volume), linear counter
Noise$400C-$400FPseudo-random noise, 16 frequency presets, short or long sequence
DMC$4010-$4013Delta modulation channel — plays 1-bit delta-encoded samples from ROM

Playing a Note

; Play a note on Pulse 1 — duty 50%, volume 15

    ; Duty cycle + volume envelope
    LDA #%10111111   ; duty=50% (bits 6-7), constant volume (bit 4),
    STA $4000        ; volume=15 (bits 0-3)

    ; Frequency (timer value for ~440 Hz / A4)
    ; Timer = CPU_clock / (16 * frequency) - 1
    ; 1789773 / (16 * 440) - 1 ≈ 253
    LDA #$FD         ; timer low byte (253)
    STA $4002
    LDA #$00         ; timer high byte (bits 0-2) + length counter
    ORA #%00001000   ; length counter load (bit 3 triggers reload)
    STA $4003

    ; Enable Pulse 1 channel
    LDA #%00000001
    STA $4015        ; APU status — bit 0 enables Pulse 1

Duty Cycles

The two pulse channels can select from four duty cycles, which changes the timbre of the sound:

Pulse Duty Cycles

12.5%:  _#______  thin, reedy
25%:    _##_____  bright, punchy (most common)
50%:    _####___  hollow, flute-like
75%:    #__#####  same as 25% but inverted phase

Sweep and Envelope

Each pulse channel has a hardware sweep unit that can automatically bend the pitch up or down over time — used for sound effects like Mario's jump and coin sounds. The volume envelope can automatically decay the volume from 15 to 0 at a configurable rate, without any CPU involvement.

NMI and the Game Loop

The PPU generates a NMI (Non-Maskable Interrupt) at the start of every VBlank period — the ~20 scanlines after the visible frame when the PPU isn't drawing. This is the only safe time to update VRAM (nametables, palettes, scroll position).

A typical NES game loop:

; Main game loop — runs once per frame via NMI

NMI_handler:
    PHA              ; save registers
    TXA
    PHA
    TYA
    PHA

    ; === VRAM UPDATES (time-critical) ===
    ; Update nametable tiles for scrolling
    ; Update palettes if needed
    ; Set scroll position
    LDA #$00
    STA $2005        ; X scroll
    STA $2005        ; Y scroll

    ; DMA sprite data to OAM
    LDA #$02
    STA $4014

    ; === GAME LOGIC (can run past VBlank) ===
    JSR read_controllers
    JSR update_game_state
    JSR update_sprites

    PLA              ; restore registers
    TAY
    PLA
    TAX
    PLA
    RTI              ; return from interrupt

The VBlank Budget

You have approximately 2,270 CPU cycles of VBlank time per frame (NTSC). That's enough to update about 160 bytes of VRAM. If you need to update more — like filling a new column of tiles for scrolling — you need to spread the work across multiple frames, or use clever techniques like updating during rendering with timed code.

Controllers

Reading the NES controller is a serial protocol. You strobe the controller latch by writing to $4016, then read $4016 eight times — each read returns one button in order: A, B, Select, Start, Up, Down, Left, Right.

; Read controller 1 into zero-page variable "buttons"
    LDA #$01
    STA $4016        ; strobe: latch button states
    LDA #$00
    STA $4016        ; release strobe

    LDX #8
read_loop:
    LDA $4016        ; read one button (bit 0)
    LSR              ; shift into carry
    ROL buttons      ; rotate carry into buttons variable
    DEX
    BNE read_loop
    ; buttons now has: A B Sel Sta Up Dn Le Ri (bit 7 to bit 0)

Mapper Chips

The base NES has limited ROM space: 32 KB for program code and 8 KB for graphics. Early games like Donkey Kong fit in this, but larger games need mapper chips — custom hardware on the cartridge that adds bank switching, extra RAM, and sometimes additional features.

Mapper Name Features Used by
0 NROM No mapper — 32K PRG, 8K CHR Donkey Kong, Mario Bros.
1 MMC1 PRG/CHR bank switching, mirroring control Zelda, Metroid, Mega Man 2
2 UxROM PRG bank switching (16K switchable + 16K fixed) Castlevania, Contra, DuckTales
4 MMC3 Fine-grained banking, scanline counter, IRQ Super Mario Bros. 3, Kirby
5 MMC5 1MB PRG, 1MB CHR, extra sound, split-screen Castlevania III

The Mapper Is Part of the System

Unlike the C64 where bank switching is a CPU feature, NES bank switching is cartridge hardware. Different games have different capabilities depending on their mapper. Super Mario Bros. 3 (MMC3) can do things that Donkey Kong (NROM) physically cannot — like scanline-counted IRQs for a status bar, or swapping individual 1 KB CHR banks for animated tiles.

Bank Switching Example (UxROM / Mapper 2)

UxROM is one of the simplest mappers. The upper 16 KB ($C000-$FFFF) is fixed to the last bank. The lower 16 KB ($8000-$BFFF) can be switched by writing the bank number to any address in $8000-$FFFF:

; Switch to PRG bank 3
    LDA #3
    STA $8000        ; any write to $8000-$FFFF selects the bank
                     ; (the address doesn't matter, just the value)

Putting It All Together — NES Program Structure

An NES ROM has a standard structure. Here's the skeleton of a complete program using the common iNES header format:

; iNES header (16 bytes)
    .byte "NES", $1A       ; magic number
    .byte 2                ; 2 x 16KB PRG ROM = 32KB
    .byte 1                ; 1 x 8KB CHR ROM = 8KB
    .byte %00000001        ; mapper 0 (NROM), vertical mirroring
    .byte 0,0,0,0,0,0,0,0 ; padding

; ---- PRG ROM bank 0 ($8000-$BFFF) ----
    .org $8000

RESET:
    SEI                    ; disable IRQs
    CLD                    ; disable decimal mode (not that it works)
    LDX #$FF
    TXS                    ; set up stack

    ; Wait for PPU to stabilize (2 VBlanks)
    BIT $2002
vwait1:
    BIT $2002
    BPL vwait1
vwait2:
    BIT $2002
    BPL vwait2

    ; Initialize PPU
    LDA #%10000000         ; enable NMI, use pattern table 0
    STA $2000
    LDA #%00011110         ; enable sprites + background
    STA $2001

    ; Clear RAM
    LDA #0
    LDX #0
clear_ram:
    STA $0000,X
    STA $0100,X
    STA $0200,X
    STA $0300,X
    STA $0400,X
    STA $0500,X
    STA $0600,X
    STA $0700,X
    INX
    BNE clear_ram

    ; Load palettes, nametables, sprites...
    ; (game-specific setup)

forever:
    JMP forever            ; main loop does nothing — NMI drives the game

NMI:
    ; Per-frame update: OAM DMA, VRAM writes, game logic
    LDA #$02
    STA $4014              ; sprite DMA
    JSR game_logic
    RTI

IRQ:
    RTI                    ; unused in mapper 0

; ---- Vectors (at $FFFA) ----
    .org $FFFA
    .word NMI              ; NMI vector
    .word RESET            ; Reset vector
    .word IRQ              ; IRQ vector

; ---- CHR ROM (8 KB) ----
    ; 512 tiles of 16 bytes each
    ; Usually created with a tile editor, included as binary data

NES vs. C64 — Key Differences

C64 NES
RAM 64 KB 2 KB (+ cartridge)
Graphics model Character/bitmap modes Tile-based with hardware scroll
Color assignment Per-character Per 2x2 tile block (attribute table)
Sprites 8 (24x21) 64 (8x8 or 8x16), 8 per scanline
Sound 3-voice analog synth (SID) 5 channels (2 pulse, tri, noise, DMC)
Expansion Cartridges optional Cartridge is the system
Programming model Direct memory access PPU accessed through register port

Key Takeaways

  • The NES uses a 6502-family CPU (Ricoh 2A03) — same instruction set as the C64, minus decimal mode, plus integrated audio.
  • The PPU is a tile engine with hardware scrolling. Graphics are composed from 8x8 tiles, not individual pixels.
  • The CPU and PPU have separate address spaces. All VRAM access goes through the register pair at $2006/$2007.
  • With only 2 KB of CPU RAM, every byte matters. Zero page is your lifeline.
  • Nametable mirroring (horizontal vs. vertical) is a hardware feature controlled by cartridge wiring, not software.
  • The attribute table assigns palettes to 2x2 tile blocks — the source of the NES's characteristic blocky color areas.
  • Mapper chips on the cartridge extend the system: bank switching, scanline counters, extra RAM, and more.
  • VBlank is your only safe window for VRAM updates — about 2,270 cycles per frame. Budget carefully.

Tools and Emulators

Tool Purpose
FCEUX Feature-rich NES emulator with PPU viewer, nametable viewer, and debugger.
Mesen Highly accurate NES emulator with excellent debugging tools.
ca65 (cc65 suite) Cross-assembler for 6502. The standard toolchain for NES homebrew.
ASM6 Simple, fast 6502 assembler popular in the NES scene.
NES Screen Tool Tile and nametable editor — design backgrounds visually.
YY-CHR Tile/sprite editor for viewing and editing CHR ROM data.
NESDev Wiki Comprehensive reference for NES hardware — the definitive resource.

The NES took the 6502 and built a tile-based console around it. Next up, its direct rival — the Sega Master System — takes a completely different CPU, the Zilog Z80, and builds its own tile engine with a different set of trade-offs.