RTL Studio Open IDE

5.3 · RISC-V CLASS

Core testbench with a fibonacci loop

Preload a Fibonacci loop into the RV32I core's instruction memory and verify the computed result in debug_x10.

Video coming soon. This lesson page stays at the same URL when the lecture publishes.

What you practice

Core testbench with a fibonacci loop (5.3). Preload a Fibonacci loop into the RV32I core's instruction memory and verify the computed result in debug_x10. It is in Core TB: fibonacci loop — Part 5 · Core Capstone of the RISC-V RV32I Core Masterclass.

The RV32I core is Verilog you simulate in the browser IDE. Verilator runs the program; the waveform shows PC, the instruction, and the register write.

What you will learn

You will preload a complete Fibonacci loop into the instruction memory and verify the core computes the correct result. This lesson demonstrates how to use the testbench to drive a real algorithm and check the output register.

Learning objectives

  • Preload instruction memory with a Fibonacci loop using RV32I opcodes.
  • Understand how the testbench waits for the core to halt on an ECALL instruction.
  • Verify the debug output register debug_x10 matches the expected Fibonacci value.

Theory

The Fibonacci sequence is a classic algorithm for testing control flow and arithmetic logic. A simple loop calculates the next number by adding the two previous numbers. The loop continues until a counter reaches a target value. Finally, the result is stored in a specific register for verification. In this lesson, we compute fib(6), which equals 8. The testbench preloads the machine code instructions directly into the u_imem.mem array. Each instruction is a 32-bit hexadecimal value representing the RV32I opcode. The core fetches these instructions sequentially from address 0. The loop uses a branch instruction to repeat the addition steps. An ECALL instruction at the end triggers the halt condition. The testbench monitors the halted signal to know when execution is complete. It then reads the debug_x10 signal to check the final result.

InstructionHex ValueOperationDescription
ADDI x5, x0, 50x00500293x5 = 5Set loop counter to 5
ADDI x6, x0, 00x00000313x6 = 0Initialize first Fibonacci number
ADDI x7, x0, 10x00100393x7 = 1Initialize second Fibonacci number
ADDI x28, x0, 00x00000e13x28 = 0Initialize loop counter
BEQ x28, x5, +240x005e0c63Branch if equalCheck if counter reached 5
ADD x29, x6, x70x00730eb3x29 = x6 + x7Calculate next Fibonacci number
ADD x6, x7, x00x00038333x6 = x7Shift first number
ADD x7, x29, x00x000e83b3x7 = x29Shift second number
ADDI x28, x28, 10x001e0e13x28++Increment loop counter
JAL x0, -200xfedf_f06fJump backLoop back to start
ADDI x10, x7, 00x00038513x10 = x7Store result in x10
ECALL0x00000073HaltStop execution

Practice

  1. Open RtlStudio and select Lesson 5.3: Core testbench with a fibonacci loop from the Examples list.
  2. Confirm that student/tb/tb_top.sv and student/rtl/design.sv appear in the Explore tree.
  3. Open student/tb/tb_top.sv in the editor.
  4. Locate the four __TODO__ placeholders in the initial block where dut_i.u_imem.mem is assigned.
  5. Replace each __TODO__ with the correct 32-bit hexadecimal instruction for the Fibonacci loop initialization.
  6. The instructions must set up the loop counter, initialize Fibonacci registers, and prepare the branch logic.
  7. Save the file and click Simulation in the left sidebar.
  8. Click Run and wait for the simulation to complete.

Summary

You practiced preloading a Fibonacci loop into instruction memory and verifying the RV32I core's control flow execution. The lesson demonstrated how branches and arithmetic operations interact to compute the correct result before halting. You confirmed that the x10 register holds the expected Fibonacci value upon simulation completion.

© 2026 RtlStudio. Licensed under CC BY-NC-ND 4.0. Commercial use and modifications by third parties are not permitted.

Run it in the Verilog IDE

  1. Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
  2. Run Verilator and watch PC, the instruction, and the register write on the waveform.
  3. Treat a program printout as done only after the ISA step this lesson names matches the wave.
  4. Change the RTL in the editor, rerun Verilator, and read the waveform again in the same tab.

The lesson is done when the simulation matches Core testbench with a fibonacci loop. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.