Skip to content

The Vertical Blank

Poll the Amiga’s beam position to pace a small colour update, distinguish a frame from vertical blanking, and discover why a late loop can miss its turn.

  • Commodore Amiga
  • Assembly

So far, we have run a small job and then held a still screen. Now we want a job that repeats at a steady pace. Our ordinary PAL display refreshes at roughly 50 Hz, giving us about 20 ms between successive frames. Those are useful round numbers; exact timing depends on the machine and display mode.

The video hardware scans across the display, line by line. During vertical blanking, the picture is blanked while the beam returns towards the top. Blanking occupies only part of the frame. We do not have 20 ms of blanking in which to change anything we like.

We will watch the beam counter for line zero and change one colour value. This is polling: the CPU repeatedly reads a register until a condition is true. It is not an interrupt handler. The sample disables custom-chip interrupts during setup; later, we can learn how an interrupt lets hardware request our attention.

What you’ll see by the end

About twelve seconds of the unchanged program running in Emu198x as a PAL Amiga A500. Each update adds $0011 to the Copper-list colour value. There is no sound.

Press play to watch the background change over time. The packed RGB addition produces both small shifts and larger jumps when colour components wrap. The clip contains full-screen colour changes; we can pause it at any point. Its important result is the repeated update, which a still image cannot show.

Wait for the beam, then work

heartbeat.asm
;──────────────────────────────────────────────────────────────
; Meet the Machine (Amiga) - Unit 9: The Vertical Blank
;
; A game runs in step with the screen: 50 times a second the beam finishes a
; frame and returns to the top - the vertical blank. We wait for it each frame
; and do one small job: nudge the colour. The screen cycles all by itself.
;──────────────────────────────────────────────────────────────

CUSTOM      equ $dff000
VPOSR       equ $004            ; beam position (line number in bits 8-16)
DMACON      equ $096
INTENA      equ $09a
INTREQ      equ $09c
COP1LC      equ $080
COPJMP1     equ $088
BPLCON0     equ $100
COLOR00     equ $180

            section code,code_c

start:
            lea     CUSTOM,a5
            move.w  #$7fff,INTENA(a5)
            move.w  #$7fff,INTREQ(a5)
            move.w  #$7fff,DMACON(a5)
            lea     copperlist,a0
            move.l  a0,COP1LC(a5)
            move.w  d0,COPJMP1(a5)
            move.w  #$8280,DMACON(a5)

mainloop:
            ; --- wait for the top of the frame (the vertical blank) ---
.wait:      move.l  VPOSR(a5),d0
            and.l   #$1ff00,d0          ; isolate the beam's line number
            bne.s   .wait               ; loop until it's line 0

            ; --- one frame's work: nudge the colour ---
            add.w   #$0011,colourval

            ; --- wait for the beam to leave the top, so we bump once per frame ---
.leave:     move.l  VPOSR(a5),d0
            and.l   #$1ff00,d0
            beq.s   .leave

            bra.s   mainloop

copperlist:
            dc.w    BPLCON0,$0200
            dc.w    COLOR00
colourval:
            dc.w    $0000
            dc.w    $ffff,$fffe

The loop has three parts:

Code
.wait:  move.l  VPOSR(a5),d0
        and.l   #$1ff00,d0          ; isolate the beam's line number
        bne.s   .wait               ; loop until it's line 0

        add.w   #$0011,colourval    ; one frame's work: nudge the colour

.leave: move.l  VPOSR(a5),d0
        and.l   #$1ff00,d0
        beq.s   .leave              ; wait for the beam to leave the top

The long-word read starts at VPOSR ($DFF004) and also reads the adjacent VHPOSR ($DFF006). Together they contain the vertical position: its high bit comes from VPOSR, and its lower eight bits from VHPOSR. The mask $1FF00 keeps those nine bits in place. We only test whether they are zero, so we do not need to shift them down.

The first loop waits for line zero, a position near the start of the frame. That is our chosen timing marker, not a test for the whole blanking interval.

The add.w changes colourval, a word in our Copper list. The Copper reads that word when it reaches the COLOR00 instruction. Changing the list is therefore different from the CPU writing the colour register directly: the visible result depends on when the Copper consumes the value.

The second wait prevents another colour bump while the beam is still on line zero. With this small workload, we can return to .wait in time for the next frame. That gives us the intended once-per-frame update.

Code
line 0                                         next line 0
  |---------------- one whole frame ----------------|
  |-- blanking --| ... visible scan ...              |
  wait → small job → leave line 0 → wait again

This is a schematic, not a scale drawing. Waiting for a frame boundary does not give every graphics operation a whole frame of safe access. Each operation has its own deadline: for example, a Copper-list value must be ready before the Copper reads it.

What if the job runs late?

Before running anything, predict this: our job takes so long that the next line zero has already passed when we return to .wait. Does the colour update twice to catch up, or wait?

Compare your prediction

It waits for a later line zero. The display keeps scanning, but our program has missed an update opportunity. The loop has no elapsed-frame counter and performs no catch-up updates. If we used its iterations as a clock, that clock would run slow whenever work missed a turn.

The .leave loop prevents repeated work during one visit to line zero. It cannot guarantee that we visit every frame. A late update may hold an old colour for longer; it does not automatically mean the picture is corrupted.

Assemble, master, and run

Code
make

The background cycles through colour after colour, smoothly, forever.

Try this: change the speed

The step is $0011. Make it bigger — $0044, $0111 — and the colour races; make it $0001 and it crawls. This changes the amount added per update, not the update frequency. The value is packed RGB: carries between its colour components can produce jumps rather than a smooth trip around a colour wheel.

Try this: count the frames

Add a counter. Before the loop, moveq #0,d2; inside, after the colour bump, addq.w #1,d2. Predict its value after 50 completed updates, then inspect d2 in the emulator. It should be 50.

If every frame receives one update, that is roughly a second on this PAL setup. The counter measures completed loop updates, however, not elapsed time independently. Missed updates break that equivalence; an ordinary NTSC display also has a different refresh rate. We will need a more deliberate clock when consistent timing becomes a game requirement.

If it doesn’t work

  • The colour flickers wildly instead of drifting. The second wait — the .leave loop — is missing or wrong, so the colour bumps many times per frame. Both waits matter: one for the beam to arrive, one for it to go.
  • The screen sits on one colour. The first wait never finishes, or the add is outside the loop. Check the bra.s mainloop at the bottom sends control back to the top of the loop.
  • Nothing shows at all. The Copper DMA isn’t on — the harness enables it with $8280 to DMACON. Leave that line as shown.

What you’ve learnt

We can pace a small job by polling the beam position. Waiting for line zero and then leaving it prevents repeated updates on that line, provided we keep the work short enough to return for the next frame. A frame is longer than its blanking interval, and counting updates is only a reliable measure of time when none are missed.

What’s next

The loop turns; now let’s make it listen. Next — Reading the Player — we test the mouse button every frame and paint the screen to match. Wire a person’s hand into that heartbeat and the program stops being a demo and starts being a game.

Hardware reference

Commodore-Amiga’s Amiga Hardware Reference Manual, second edition, describes the beam-position bits in chapter 7, Table 7-5 (printed pp. 210–211), and the combined long-word read in chapter 8 (p. 233). Chapter 2, “Location Registers” (pp. 20–21), describes restarting the Copper list. See the manual scan and the beam-counter section.

Run it here

This unit's program, built for you.

Program
heartbeat.adf
Machine
Commodore Amiga 500 + A501 trapdoor (OCS PAL)

Download heartbeat.adf