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
first_irq:
    lda #<second_irq
    sta $0314
    inc $d012        ; Next line
    ; ... acknowledge IRQ
    rti

; Second IRQ - stabilised
second_irq:
    ; NOP slide for consistent entry point
    nop : nop : nop : nop : nop : nop : nop
    ; Now cycle-exact
    ; ... do stable raster 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 writes
Raster barsColour changes per line
Split screensMode changes mid-frame
FLD effectsFlexible Line Distance
Tech-techMulti-colour text

Historical Development

EraAdvancement
1986Basic double-IRQ discovered
1987Refined techniques
1990sDemo scene mastery
TodayStill essential for C64 effects

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