VIA Timers (T1, T2)

Each 6522 VIA has two 16-bit timers that decrement at 1 MHz — i.e. one tick = 1 µs = 2 CPU cycles (at 2 MHz CPU clock). Both VIAs (system-via, user-via) have a T1 and a T2, so the BBC has four timers total — though System VIA T1 is often claimed by MOS sound, leaving T2 + both User VIA timers free for user code.

Four modes selected by ACR bits 6 (continuous), 7 (PB7 output):

ACR6ACR7Behaviour
00One-shot. IFR bit 6 set on timeout; no further IRQ until T1C-H rewritten.
01One-shot + PB7 pulse low for the count duration.
10Free-run. IFR bit 6 set at each timeout, counter reloads from latches automatically.
11Free-run + PB7 square wave. No CPU cycles needed.

Loading T1

LDA #lo : STA T1C_L     ; load low latch (no count starts yet)
LDA #hi : STA T1C_H     ; loads high latch AND starts count

Timeout = (N+2) × 1 µs where N is the loaded 16-bit value (NAUG §22.4.10 p397). At N=0, the first timeout is 2 µs (4 CPU cycles). See Anatomy of the +2 below for what the two extra µs actually are.

For continuous (free-run) operation, write both latches via T1L_L/T1L_H (regs 6/7) to update the period of the next cycle without restarting the current one.

Clearing T1 IFR

Any of: read T1C-L, read T1C-H, write T1C-H, or write &40 to IFR. Most efficient: LDA T1C_L (4c).

T2 — simpler

Two modes selected by ACR bit 5:

ACR5Mode
0Interval timer (one-shot only). No free-run mode for T2.
1Pulse counter — T2 decrements once per negative-going pulse on PB6

T2 has no PB7 output, no continuous mode. It’s a one-shot from the moment T2C-H is written. To repeat, the IRQ handler must rewrite T2C-H.

Clearing T2 IFR

Read T2C-L or write T2C-H. LDA T2C_L (4c).

Anatomy of the +2

The (N+2) µs formula applies to both T1 and T2 in interval mode. It is not a magic constant — it decomposes into one µs of startup delay plus one µs of through-zero underflow. Understanding the breakdown matters when you’re synchronising to T2/T1 at single-cycle precision (e.g. hexwab-stable-raster) or polling T2C-L for a specific tick.

Sequence from the CPU’s STA TxC_H to the IRQ firing:

StageWhat happens on the chipCost
1. The writeSTA TxC_H is a SHEILA access → cycle-stretched to align with the 1 MHz Φ2 bus. Register update completes at the end of the stretched bus cycle.4-6 CPU cycles (2-3 µs) — see cycle-stretching
2. Load tickOn the next Φ2 edge after the write, the counter loads N from the latch pair. No decrement on this tick — this is the startup delay.+1 µs
3. CountdownCounter decrements once per Φ2 edge: N, N-1, …, 2, 1, 0.N µs
4. Underflow tickThe 0 → &FFFF transition fires the IFR bit (5 for T2, 6 for T1).+1 µs
5. CPU sees IRQStandard 6502 IRQ entry — finish current instruction, push PC/P, fetch vector.7+ CPU cycles

So the +2 is exactly: +1 dead load tick + 1 underflow tick. Stages 1 and 5 are outside the formula — they’re the cost of getting to and from the timer, not the timer’s own delay.

The WDC W65C22 datasheet (Fig 18) actually shows !IRQ falling at N+1.5 Φ2 cycles. The half-cycle is because the IRQ-out pin is updated on the opposite Φ2 phase from the count itself. For polled or IRQ-driven code on the BBC this rounds to the +2 µs that NAUG quotes; for chip-level emulation (jsbeeb / b2 / beebjit) it matters.

Real-hardware verification

hoglet ran a real-hardware test on Model B and Master 128 (Stardot 16138): write T2C-H = 1, then read T2C-L back in a tight loop. Sequence observed: 1, 0, &FF, &FE, &FD, &FC, .... This confirms:

  • The counter passes through zero (not skipping to &FFFF).
  • The IFR bit fires on the 0 → &FFFF transition (stage 4 above), not when the counter first becomes zero.
  • So you have a 1 µs window at T2C-L = 0 to catch the count via polling before the IRQ trigger.

When you can’t use the formula

The +2 model breaks down in two cases:

  • Re-trigger before timeout. Writing TxC-H again re-runs stage 2 (load tick) — you get one extra µs you wouldn’t get from waiting for the first count to expire and re-arming in the IRQ handler. Useful for extending a wait without losing precision.
  • Reading T1C-L mid-count to clear IFR while continuing. The read doesn’t perturb the count, but the bus-side cycle stretch on the read itself is variable depending on Φ2 phase — see cycle-stretching for the 2c/3c distribution. T2 has the same property.

Practical patterns

Periodic IRQ at < 50 Hz

Use T1 free-run on User VIA (System VIA T1 may be in use by MOS sound). Hook IRQ2V. Loaded value N gives a period of (N+2) µs.

For exactly 50 Hz: N = 19998 (period = 20000 µs). For 100 Hz: N = 9998.

Raster split at a specific scan line

Each scan line on a UK BBC is 64 µs (1/(50 × 312)) — but in non-interlace mode used by all modes except 7, that’s 64 µs per line. Programming T1 with N = 64 × lines_to_wait - 2 from the start of vsync gives an IRQ at that line.

Pattern:

  1. In your vsync handler (System VIA CA1, IFR bit 1), program User VIA T1 in one-shot mode with the desired offset.
  2. T1 fires after (N+2) µs from the program write.
  3. In the T1 IRQ handler, write your raster effect (palette change, mode bit toggle, R12/R13 reposition).
  4. To chain another split, immediately reprogram T1 with the next delta.

Cycle-accurate splits require fast IRQ entry — the 7-cycle IRQ overhead plus your handler’s preamble (PHA/PHX/PHY/LDA IFR/CMP/etc.) easily costs 20+ cycles, so plan accordingly. For exactly-cycle-aligned splits, disable IRQs and busy-wait on LDA T1C_L until the count is near zero.

Audio via PB7

ACR bits 6=1, 7=1 → T1 free-run with PB7 square wave. The frequency on PB7 is 500 kHz / (N+2). For a 1 kHz tone, N = 498.

This gives you an audio channel without going through the SN76489 — useful for sample playback (1-bit pulse-width modulation), or as an extra voice. Output is on the user port pin PB7; connect to amp.

Pulse counting (mouse / tachometer)

Set ACR bit 5 = 1. Load T2 with the number of pulses you want to count. Each negative-going edge on PB6 decrements T2. When it hits zero, T2 IFR fires.

Useful for measuring rotation rate of a quadrature encoder (1 channel into PB6, count edges over a known time window).

Sound and the System VIA T1

MOS uses System VIA T1 to time sound-envelope updates (every 20 ms). If you steal T1, sound queues stop processing — channels keep playing their current note but envelopes don’t update, the next note in a queue won’t trigger.

Flagged 2026-09-07: “every 20 ms” disagrees with interrupts and sound on this wiki (System VIA T1 = the 100 Hz tick) and with a measurement: under the MOS, System VIA T1 fires every 20,000 CPU cycles = 10 ms, exactly half a frame — which is why a &FE44 read taken at a fixed point in a VSync-locked loop is near-constant rather than random (Paradroid, 2026-08-26, jsbeeb; beeb-port-kit-hardware-facts §3). The consequence in the paragraph is right; the period is not. Text kept for the record.

The standard advice: prefer User VIA timers for general-purpose timing. Touch System VIA timers only if you’ve turned off MOS sound (*FX 210,1 or OSBYTE 210 with X=1).

Caveats

  • The latches survive timeouts; the counter reloads from them in free-run mode. If you change the latches via regs 6/7 while free-running, the change takes effect at the next timeout, not immediately.
  • Writing T1C-H or T2C-H clears the relevant IFR bit — useful in handlers.
  • A timer write while IRQs are enabled, with the timer already at a small count, may produce an IRQ between the C-L write and C-H write. Disable IRQs around the load to be safe.

See via-6522 for register layout. See naug-ch08-interrupts (pending) for the IRQ dispatch chain.

Measured in practice — two C64 → BBC ports (2026)

Source: beeb-port-kit-hardware-facts — facts measured during Kieran Connell’s two C64 → BBC ports, Paradroid (BBC Model B, August 2026) and Edge Grinder (BBC Master 128, September 2026). Instrument is jsbeeb driven through an MCP unless the entry says otherwise; b-em and b2 were the cross-checks. Each entry gives the date (or the port’s layer where the write-up carries no date), the emulator or hardware, and the port’s own write-up. Both ports drive a vertical rupture from System VIA T1 with IRQ1V taken outright.

  • T1 in continuous mode, not T2, for raster timing. T2 is one-shot only, so the interval starts when the handler writes T2C-H and every interrupt’s service latency feeds into the next interval. T1 auto-reloads from its latch at underflow, so the period is exact however late the service; restart it at VSync and the fires stay phase-locked. A latch write takes effect one reload later, so intervals are set one fire ahead (Paradroid Layer 3, jsbeeb, after T2 was tried first; layer-3-scroll.md). This refines the “raster split” pattern above, which uses one-shot T1 re-armed per fire.
  • The -2 in N = 64 × lines - 2 above is the (N+2) anatomy at work, and both ports carry it: Edge Grinder’s T1_I1 = 56*SL - 4*SL - 2 and T1_I2 = 40*SL - 2 (SL = 64) give 6,802 and 5,122 cycles, measured with breakpoints on the two handlers (2026-09-02). Paradroid’s T1_TUNE = -4 * SL - 32 adds the ~4-scanline CA1 service latency and a sub-scanline centring term. All on field-timing. Take the phase from the emulator, not from the arithmetic: it is a property of the whole IRQ path and drifted ~6 µs as the handler changed.
  • A &FE44 read at a fixed point in a VSync-locked loop is not a random seed (see the flag above); nor is User VIA T1C-L unless it is free-running — code that had retuned it to continuous mode with a &00FF latch handed the game a counter spanning 0-255 every 257 µs, phase-locked to a deterministic boot: the same value every time (Paradroid, 2026-08-30, jsbeeb).
  • A T1 difference used as a stopwatch is only modular while the counter does not pass zero between the reads. When it does, that sample gains ~65,536 ticks (131,000 cycles), and because the reload point drifts slowly the bad readings come in consecutive runs. Paradroid’s DEBUG_TIME reported 2,903 cycles for code measured at 1,009 by breakpoints (2026-08-26, jsbeeb).
  • T2’s two bytes are read one after the other and the counter can roll between them, so an elapsed reading can come out 256 µs smaller than the mark before it; an unsigned maximum then stores a ~65,000 µs junk value. A frame longer than 65.5 ms wraps the counter outright. Reject a sample whose subtraction borrows (Edge, 2026-09-04, jsbeeb; three readings of three were junk before the fix; performance.md).

Measured in practice — later projects (1942, Stryker’s Run, Isometrikum, Square Roots, phX — 2026)

From beeb-port-kit-hardware-facts-2026-11 §3, §8 (all jsbeeb unless stated; the CPC demos are emulator-only so far):

  • Re-arming T1 from a constant drifts; chaining its latch does not. Square Roots restarted T1 at its first fire each field from a constant and drifted 70 µs a field — and one late entry shifts it against the CRTC for good. Lock T1 once at install and have each fire load the latch for the interval to the next: it held (2026-11-08; square-roots/docs/layer-3-display.md; kit gotchas class 10). Same principle as “set intervals one fire ahead” above.
  • One T1 chain, two latches, a 0-14-cycle slide, for a 10-minute demo (phX, Master): its draw is entered at the same beam position (C0 0) every one of 16,302 fields, every field 39,936 cycles, VSync once a field; only fields with disc NMIs enter late (0-35 cycles behind the loading card, 0-67 across all four load windows), because an NMI can delay the IRQ beyond what the slide absorbs (2026-11-15, verify_frame.mjs; phX/docs/design-backbone.md).
  • A fire’s first CRTC write lands ~4 lines after its nominal line with no correction (the CA1 flag’s timing, the IRQ path, the slide): Isometrikum’s T1_SYNC_TUNE = -3*SL, applied to the next field, puts every write 1 line + 5 characters after its line — fire 0’s R4 at 3:9 in 6,384 of 6,399 fields; the rest up to 54 characters late, each because a bass interrupt was already running (MODE 2, R1 = 96, 2026-11-07). Consistent with the ~4-scanline CA1 latency above.
  • A stable-raster NOP slide of 80 µs held one write position in 2,994 of 2,999 fields; 64 µs split the writes between two positions 2 cycles apart. Page-aligned (ASSERT HI(slide) = HI(slide_end)), entered by a self-modified low byte from loaded - T1CL. Only worth it where a write window is narrow: Square Roots’ windows were whole 512 µs rows and its slide was removed (Isometrikum 2026-11-07, Square Roots 2026-11-08). See hexwab-stable-raster.
  • A frame need not be 312 lines. Square Roots’ CPC frame is 304 (51.4 Hz), so the BBC runs a T1 field of 19,456 µs (304 × 64) with R4 = 37 — 312 would make the demo 2.7% slower. A harness that counts jsbeeb’s 312-line frames drifts against it and must sample at the port’s own tick (verify_music differed every 39th tick until it did) (2026-11-08; kit gotchas class 5).
  • A User VIA T1 software-bass toggle is only as punctual as the interrupts in front of it: Isometrikum’s bass, 1.95 interrupts a field at 152 cycles, toggled within 8 cycles of its period 85.5% of the time with two raster fires a field, 77.5% with five (worst ±4,700 cycles). Nesting the bass (CLI after each fire’s CRTC writes) is the unbuilt cure. See sn76489.
  • A User VIA T1 palette split chained to the MOS cost 1,165 cycles a field in service call 5 (every paged ROM offered the interrupt) before IRQ2V. Service your own timers and RTI yourself (Stryker’s Run decision 15, 2026-10-16, Master MOS 3.20). See interrupts.
  • A one-row CRTC frame driven by User VIA T1 firing three times a field, each fire re-armed from T2’s count is how Stryker’s Run splits its HUD from the scrolling frame (2026-10-23, jsbeeb 2.10.2; seen right on b2). The CRTC side is on vertical-rupture.
  • The MOS’s tick is not one interrupt entry per T1 flag: counting IRQ entries with System VIA T1’s flag up gave 3,192 for 1,176 ticks from BASIC (nesting, re-entry). Place ticks on a 20,000-cycle grid (kit sfxcapture.mjs, 2026-10-25).

This wiki is curated by Claude following the LLM-Wiki methodology — a human curates source documents, the LLM compiles structured cross-linked markdown. Content may contain errors, omissions, or stale claims. For authoritative information refer to the original source documents in the bbc-documents GitHub archive.