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_x10matches 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.
| Instruction | Hex Value | Operation | Description |
|---|---|---|---|
| ADDI x5, x0, 5 | 0x00500293 | x5 = 5 | Set loop counter to 5 |
| ADDI x6, x0, 0 | 0x00000313 | x6 = 0 | Initialize first Fibonacci number |
| ADDI x7, x0, 1 | 0x00100393 | x7 = 1 | Initialize second Fibonacci number |
| ADDI x28, x0, 0 | 0x00000e13 | x28 = 0 | Initialize loop counter |
| BEQ x28, x5, +24 | 0x005e0c63 | Branch if equal | Check if counter reached 5 |
| ADD x29, x6, x7 | 0x00730eb3 | x29 = x6 + x7 | Calculate next Fibonacci number |
| ADD x6, x7, x0 | 0x00038333 | x6 = x7 | Shift first number |
| ADD x7, x29, x0 | 0x000e83b3 | x7 = x29 | Shift second number |
| ADDI x28, x28, 1 | 0x001e0e13 | x28++ | Increment loop counter |
| JAL x0, -20 | 0xfedf_f06f | Jump back | Loop back to start |
| ADDI x10, x7, 0 | 0x00038513 | x10 = x7 | Store result in x10 |
| ECALL | 0x00000073 | Halt | Stop execution |
Practice
- Open RtlStudio and select Lesson 5.3: Core testbench with a fibonacci loop from the Examples list.
- Confirm that
student/tb/tb_top.svandstudent/rtl/design.svappear in the Explore tree. - Open
student/tb/tb_top.svin the editor. - Locate the four
__TODO__placeholders in theinitialblock wheredut_i.u_imem.memis assigned. - Replace each
__TODO__with the correct 32-bit hexadecimal instruction for the Fibonacci loop initialization. - The instructions must set up the loop counter, initialize Fibonacci registers, and prepare the branch logic.
- Save the file and click Simulation in the left sidebar.
- 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
- Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
- Run Verilator and watch PC, the instruction, and the register write on the waveform.
- Treat a program printout as done only after the ISA step this lesson names matches the wave.
- 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.