Software needs to know what time it is: to measure how long something took, to wait a fixed time, or to be woken up after one. The timer gives it that, with a counter that increases every clock cycle and a compare value that raises an interrupt line when the counter reaches it. This article explains the timer's 64-bit counter and compare register, the way RISC-V defines them, why a 64-bit value read through a 32-bit bus needs care, and the exact cycle in which the interrupt line changes. In exercise S15 you write the timer.

7 min read | Beginner to Intermediate

What You'll Learn

  • Describe a free-running 64-bit time counter and a compare register
  • Expose 64-bit registers as pairs of 32-bit registers on APB
  • Generate an interrupt request combinationally from MTIME >= CMP
  • Read a 64-bit counter safely from a 32-bit core

Before You Start

This section needs Verilog: see the Verilog Tutorial. Timers and Watchdogs explains timers in real chips, and GPIO explains the APB rules a peripheral follows. No earlier exercise is needed.

MTIME and CMP

The timer has two 64-bit values:

  • MTIME counts clock cycles. It is 0 after reset and increases by one at every rising clock edge, forever. Software can read it but not write it.
  • CMP is a compare value that software writes. It is all ones after reset.

The output irq is 1 while MTIME is greater than or equal to CMP, comparing all 64 bits as unsigned numbers. Because CMP starts at the largest possible value, irq is 0 after reset, and stays 0 until software writes a smaller CMP.

These are the names RISC-V uses: the privileged architecture defines a machine timer with a memory-mapped mtime and mtimecmp, and a timer interrupt that is pending whenever mtime is greater than or equal to mtimecmp. Your timer is that machine timer, minus the part where the core takes the interrupt.

64 Bits Through a 32-Bit Bus

Why 64 bits? A 32-bit counter at 100 MHz wraps around after about 43 seconds, which is far too soon for a system clock; a 64-bit one would take nearly 6,000 years. But APB carries 32 bits per transfer, so each 64-bit value appears as two 32-bit registers:

OffsetRegisterAccessMeaning
0x000MTIME_LOread onlybits [31:0] of MTIME
0x004MTIME_HIread onlybits [63:32] of MTIME
0x008CMP_LOread/writebits [31:0] of CMP
0x00CCMP_HIread/writebits [63:32] of CMP

Writing CMP_LO changes only the low half of CMP, and CMP_HI only the high half. Writes to MTIME are ignored. The ports are the usual APB peripheral ports plus irq:

module cvsoc_timer (
    input  logic        clk,
    input  logic        rst_n,
    input  logic        psel,
    input  logic        penable,
    input  logic        pwrite,
    input  logic [11:0] paddr,
    input  logic [31:0] pwdata,
    output logic [31:0] prdata,
    output logic        pready,
    output logic        pslverr,
    output logic        irq         // 1 while MTIME >= CMP
);

Splitting a 64-bit counter has a known trap on the software side. Between reading MTIME_LO and MTIME_HI, the counter keeps running, and if the low half wraps from 0xFFFF_FFFF to 0 in between, the two halves come from different moments and the result is off by about 232. The usual fix is to read HI, then LO, then HI again, and start over if the two HI values differ; the RISC-V specification shows exactly this loop for reading its 64-bit cycle counter on a 32-bit core. The hardware does not need to do anything about it; the driver does.

Quick Check

Q: A program reads MTIME_LO and gets 0xFFFF_FFFE, then reads MTIME_HI six cycles later and gets 0x0000_0001. Before the first read, MTIME_HI was 0. What value does the program assemble, and what was the real time at the first read?

Show Answer

It assembles 0x0000_0001_FFFF_FFFE. The real value at the first read was 0x0000_0000_FFFF_FFFE: the low half wrapped between the two reads and the high half went from 0 to 1. The program is about 232 cycles ahead of the truth, which is why the read-HI, LO, HI pattern exists.

The Interrupt Line

The comparison is combinational, straight from the two registers: no extra flip-flop, no extra cycle. With CMP = 105, irq is 0 while MTIME is 101 to 104, and becomes 1 in the cycle in which MTIME holds 105:

Waveform: MTIME counts 101 to 108; with CMP = 105, irq is 0 while MTIME is below 105 and becomes 1 in the cycle MTIME equals 105, staying 1 afterwards.

It stays 1 until software writes a CMP larger than MTIME; then it falls at once. This "level" behavior is what RISC-V specifies: the interrupt stays pending as long as the condition holds, and the handler clears it by moving CMP into the future.

In cvsoc the line goes to the top level as timer_irq and stops there: the core in this section has no interrupts, so programs use the timer by reading MTIME, the way hello measures its own run. It reads MTIME_LO before and after its main work and prints the difference: cycles 0000181e, 6,174 cycles. Interrupt Controllers and Traps and Precise Exceptions explain what a core with interrupts would do with the line.

Where Is This Used?

Every RISC-V system has a machine timer like this one, usually inside a block called the CLINT or ACLINT, shared by all cores. Operating systems use it as their tick: the scheduler sets CMP to "now plus one time slice", the interrupt arrives when the slice is over, and the handler sets the next one. A watchdog, described in Timers and Watchdogs, is the same idea pointed the other way: a counter that resets the chip unless software keeps pushing its deadline back.

How It Is Tested

The S15 tests drive APB transfers and count clock edges:

  • the reset values: irq 0, CMP all ones, MTIME small, MTIME_HI 0;
  • MTIME read at known distances: a read takes three cycles in the test (SETUP, ACCESS and one idle cycle), so two reads in a row differ by exactly 3, and reads 25 cycles further apart by 28;
  • a write to MTIME_LO, which must be ignored;
  • CMP written and read back, and irq 0 while CMP_HI = 1;
  • CMP set to a value a few cycles ahead of MTIME, and irq checked at every clock edge around the cycle where MTIME reaches it; the hidden test repeats that for several values.

Common Mistakes

Mistake #1: Comparing with > instead of >=

With "greater than", irq rises one cycle late, when MTIME is CMP + 1. A one-cycle difference sounds harmless, but the test checks the exact cycle, and so does software that sets CMP to "now" to raise the interrupt at once.

Mistake #2: Resetting CMP to 0

With CMP = 0 after reset, MTIME >= CMP holds from the first cycle, and irq is 1 before software has set anything up. A core with interrupts would take a timer interrupt as its first act. All ones keeps the interrupt quiet until it is wanted.

Mistake #3: Mixing up the halves

Returning the low half for MTIME_HI, or writing CMP_LO from a CMP_HI write, passes every test that only uses small values. The tests read MTIME_HI and set CMP_HI to 1 for that reason.

Your Turn in the Lab

Exercise S15: write cvsoc_timer with the ports above. Save it in your lab project for S16.

Open exercise S15 in ChipVerify Lab

Summary

  • MTIME is a 64-bit counter that starts at 0 and counts every clock cycle; CMP is a 64-bit compare value that resets to all ones.
  • Each appears as two 32-bit registers on APB; MTIME is read only, and the CMP halves are written separately.
  • irq = MTIME >= CMP, combinational, so it rises in the very cycle MTIME reaches CMP and falls as soon as CMP is moved ahead.
  • Software reads a 64-bit counter through a 32-bit bus as HI, LO, HI to avoid a wrap between the halves.
  • In cvsoc the line stops at the top level; hello uses the timer to measure its own run, 6,174 cycles.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.