Skip to content
Timer and cycle counter

Timer and cycle counter

For programmers

Two devices for time, both counting in clock cycles of 10 ns: the cycle counter tells how much time went by, and the timer gives an interrupt after a number of cycles.

Cycle counter

A 64-bit counter that counts every cycle since the last reset. At 100 MHz it would take more than 5,000 years to wrap around.

AddressRegisterReadWrite
0xFFFF8100SNAPSHOT0any value: copies the count into the snapshot
0xFFFF8104LOWbits [31:0] of the snapshotignored
0xFFFF8108HIGHbits [63:32] of the snapshotignored

The count changes every cycle, and a program reads it in two halves. If it read the halves of the running count, the low half could wrap around between the two reads, and the two halves wouldn’t belong together. The snapshot holds still, so both halves are from the same moment.

Measuring code

Take a snapshot before and after the code, and subtract the low words:

    write 0xFFFF8100 r0 r0      ; snapshot
    read 0xFFFF8104 r0 r1       ; its low word
    ...                         ; the code to measure
    write 0xFFFF8100 r0 r0
    read 0xFFFF8104 r0 r2
    sub r2 r1 r3                ; the cycles
  • With no code in between, the difference is 9 cycles, which the snapshot accesses take themselves. Subtract that to get the cycles of the code.
  • The low word alone is enough for anything shorter than 42 seconds.
  • Code in SDRAM can take longer the first time it runs, when it’s not in the caches yet. Measuring it twice in a row shows the difference.

Timer

A timer that counts down by one every cycle, and sets its flag when it reaches 0.

AddressRegisterReadWrite
0xFFFF8200COUNTthe cycles left, 0 when the timer is stoppedstarts counting down from this value. 0 stops the timer.
0xFFFF8204CONTROLthe bits belowthe bits below
0xFFFF8208STATUSbit 0: the timer expiredany value: clears bit 0
CONTROL bitMeaning
0periodic: when the count reaches 0, start again from the last value written to COUNT
1interrupt 1 is enabled
  • A write of N to COUNT makes the timer expire N cycles later: bit 0 of STATUS becomes 1 when the count goes from 1 to 0.
  • Without the periodic bit, the timer stops at 0. With it, the count starts again right away, so the timer expires exactly every N cycles. The time a handler takes doesn’t add up over the periods.
  • A write to COUNT while the timer runs starts it again with the new value. A write to CONTROL doesn’t change the count.
  • The longest time is 2^32 - 1 cycles, almost 43 seconds.
  • The flag stays 1 until STATUS is written. If the timer expires again in the same cycle as that write, the flag stays 1.

With bit 1 of CONTROL set, the flag gives interrupt 1. The handler writes STATUS to clear it, as on the I/O devices page.

A tick every millisecond (100,000 cycles):

    load 3 r1
    write 0xFFFF8204 r0 r1      ; CONTROL: periodic, with the interrupt
    load32 100000 r1
    write 0xFFFF8200 r0 r1      ; COUNT: start

And in the handler:

    write 0xFFFF8208 r0 r0      ; STATUS: clear the flag

Waiting for a time without an interrupt: write COUNT, then read STATUS until bit 0 is 1.

Last updated on