Skip to content
Techniques & Technology

Stable Raster

Cycle-exact timing

The double-IRQ technique that achieves cycle-exact synchronisation with the VIC-II raster beam, enabling perfect mid-screen effects on the Commodore 64.

commodore-64 c64vic-iirasterinterruptstimingdemo-scene

Overview

Stable raster is the Commodore 64 technique for achieving cycle-exact synchronisation with the VIC-II's raster beam. Because the CPU and VIC-II share the bus, interrupt timing has inherent jitter (0-7 cycles). The stable raster technique eliminates this jitter, enabling perfectly timed mid-screen effects.

Fast Facts

AspectDetail
ProblemIRQ timing varies by 0-7 cycles
SolutionDouble-IRQ with self-modifying code
PrecisionCycle-exact (0 jitter)
Used forBorder removal, split screens, colour bars

The Jitter Problem

When a raster interrupt fires, the CPU might be mid-instruction:

Instruction LengthJitter
2 cycles0-2 cycle variation
3 cycles0-3 cycle variation
7 cycles0-7 cycle variation

This jitter causes visible glitches in timing-sensitive effects.

The Double-IRQ Solution

The technique uses two interrupts:

IRQPurpose
First IRQSets up second IRQ one line later
Second IRQTriggers inside a long NOP slide

Step by Step

  1. First IRQ fires with unknown jitter
  2. Set second IRQ for next raster line
  3. Execute NOP slide (many 2-cycle NOPs)
  4. Second IRQ fires inside NOP slide
  5. Jitter is now only 0-1 cycles
  6. Measure exact cycle position
  7. Compensate with calculated delay

Code Structure

; First IRQ - unstable, sets up second one line later
first_irq:
    lda #<second_irq
    sta $0314           ; Point CINV at second handler
    lda #>second_irq
    sta $0315
    inc $d012           ; Next IRQ one line down
    lda #$01
    sta $d019           ; Acknowledge
    tsx
    cli                 ; Re-enable IRQs (so second can fire)
    nop : nop : nop : nop : nop : nop : nop : nop
    ; ... long NOP slide ...
    ; The second IRQ will fire somewhere inside this slide,
    ; at one of two cycle positions (jitter is now 0 or 1).

; Second IRQ - stabilised
second_irq:
    ; Read $d012 to compensate for the remaining 0-1 cycle jitter
    lda $d012
    cmp $d012           ; If line just changed, branch to align
    beq @aligned
@aligned:
    ; Now cycle-exact — perform the effect
    sta $d020           ; Example: change border colour
    ; ... acknowledge and exit ...

The standard idiom is "first IRQ enables nested IRQ, NOP slide gives the second IRQ a known entry point, then a CMP+BEQ trick eliminates the last cycle of jitter." This is the backbone of every cycle-exact C64 effect.

Why It Works

MechanismEffect
NOP slideAll instructions same length (2 cycles)
Maximum jitter = 1Only 0 or 1 cycle variation
Cycle countingDetermine exact position
Branch compensationConditional delay to align

Applications

EffectRequirement
Border removalExact $D011 / $D016 bit-3 writes at cycle-precise raster positions
Raster barsColour changes per scanline
Split screensMode changes mid-frame (text/bitmap split, multicolour switch)
FLD (Flexible Line Distance)Manipulate $D011 YSCROLL to space rendered lines vertically — produces the "stretchy screen" effect
FLI (Flexible Line Interpretation)Per-line $D018 change combined with forced badlines — yields 8 colours per 8×8 cell
Tech-techPer-line $D016 XSCROLL change so text undulates in a sine wave

Historical Development

EraAdvancement
1986Basic double-IRQ discovered
1987Refined techniques
1990sDemo scene mastery — Crest's Wonderland series, Censor Design's productions
TodayStill essential for C64 effects; modern toolchains (KickAssembler, Acme) treat stable raster as a standard preamble

Crest's Wonderland XII (1996) is the canonical reference demo for stable raster — every effect in it relies on cycle-exact raster timing.

Performance Cost

FactorImpact
CPU timeTwo IRQs per stable point
Code spaceNOP slides, handler code
ComplexityRequires deep understanding

Modern Relevance

Understanding stable raster teaches:

  • Interrupt handling fundamentals
  • Cycle-exact timing
  • Hardware synchronisation
  • Why modern GPUs use VSync

See Also