Skip to Content
GuidesDebuggingMeasuring T-states

Measuring T-States

When you optimise a routine, you want to know what it costs in T-states, the CPU’s clock cycles. The simplest measurement takes two breakpoints and the TSP value of the Z80 CPU panel: T-states since the last start after a pause. Every time the paused machine continues, TSP starts counting from zero, so when it stops again, TSP is the cost of what ran in between.

Measure between two breakpoints

Take this program for the ZX Spectrum 48K:

Start: .model Spectrum48 .org #8000 ld a,2; ; (START) call #1601 ; open the channel ld hl,Message ; HL points the the message string NextCh: ld a,(hl) ; get next character or a jr z,Done ; we reached the end rst #10 ; display character inc hl ; next character jr NextCh ; next loop Done: jp #12a9 ; (END) Message: .dm "\a\x0A\x06" ; AT 10, 6 .dm "\p\x04" ; PAPER 4 .dm "Welcome to Klive IDE" .dm "\a\x0B\x08" ; AT 10, 8 .dm "\p\x04" ; PAPER 4 .dm "\b\x01" ; BRIGH 1 .dm "(ZX Spectrum 48)" .db 0x00 ; terminate

Set two breakpoints

Set one on the line marked (START) and one on the line marked (END).

Run to the first

Start a debug run (Ctrl+F5). The machine stops at (START). Open the Z80 CPU panel of the Debug activity.

Continue to the second

Press F5. When the machine stops at (END), TSP shows the T-states the code took from (START) to (END); CLK still shows the total since the machine started.

The same works with steps: after a Step Over, TSP is the cost of that one instruction, or of the whole call it stepped over.

What affects the count

Memory contention. On the ZX Spectrum 48K the CPU and the ULA share the memory at $4000-$7FFF, and the ULA can hold the CPU for a few T-states. Code that touches that range can show a slightly different TSP on each run. The ULA & I/O panel’s LCO shows how many T-states were lost to contention since the last pause.

Interrupts. The frame interrupt arrives every 20 milliseconds (about 69,888 T-states on the 48K). If it arrives while your code runs, the interrupt handler’s T-states are counted in TSP too. Disable interrupts around the code (di … ei) to measure it alone.

Other ways to see timing

  • In the Disassembly view, the row at the execution point says how many T-states its instruction will really take, which for a conditional branch depends on whether it is taken.
  • The Execution History’s Time column gives the position in the frame at which each recorded instruction started.
  • For whole routines over a longer run, the profiler measures where the time goes, per routine and per call.
Last updated on