Replace a delay loop with a machine timer
Online version: https://fpga.labsland.com/altera/computer-organization/lab-03/part-04/
Debugger setup
CPUlator simulatorRISC-V · DE1-SoC Nios V computer, simulated in your browser. No board or sign-in needed.
- Get the sourceDownload
timer_counter.s - Open the simulatorOpen CPUlator
Keep the same two-digit hexadecimal display and button controls. This time the processor polls a timer that counts independently of the instructions in the waiting loop. Find the difference between the running count, mtime, and its compare value, mtimecmp.
Time the previous counter
Download the delay-loop checkpoint. In CPUlator choose File → Load ELF… and press Continue. Start a stopwatch when HEX1–HEX0 first changes and stop it five changes later. Press and release KEY0 to pause the count, then repeat to resume. Keep your elapsed time for comparison on the same computer. Press Stop before loading the timer source.
Configure the comparison
Open timer_counter.s in CPUlator for RISC-V DE1-SoC. The two 64-bit timer registers each occupy two 32-bit words. Draw their addresses starting at 0xFF202100. Read the stored low word of mtimecmp during polling instead of using the starter's fixed immediate value. Complete the eight-bit wrap mask. Compile and load, step once, then continue.
In Devices, open RISC-V Machine Timer and Seven-segment displays. The program writes 100,000,000 timer ticks into mtimecmp. Record the compare value shown by CPUlator and time five display changes. Compare the two methods on the same machine.
Observe and explain
Predict the result of each trial. For the first two, time five changes. For the last two, describe what the digits showed.
| Trial | Ticks in mtimecmp | Result |
|---|---|---|
| Default compare value | ||
| Compare value halved | ||
| Initial byte FE | ||
| Short KEY0 press and release, then a second press |
When testing FE, record each value across FF. When pausing, check whether mtime and the displayed byte behave the same way. Explain why the software reads the high word of mtime before using an unsigned comparison on the low word. Restore the original compare value before submitting.
Adapted from FPGAcademy Computer Organization 24.1, Lab 3, Part IV. LabsLand wrote the assembly and activities. MIT notice
