This is where your blocks become a processor. In exercise E07 you write cvr32_core, the top-level module, and connect your PC, decoder, immediate generator, register file and ALU into a single-cycle core that executes every ALU instruction, plus lui and auipc. It cannot touch data memory or branch yet, but it runs real machine code: a test program of 35 instructions whose every register write is compared with a reference core. This article explains what to connect, follows the first instructions of that program through the datapath cycle by cycle, and shows how to read the test's messages.

10 min read | Beginner to Intermediate

What You'll Learn

  • Connect the fetch, decode, register file and ALU blocks into a single-cycle datapath
  • Build the operand multiplexers and the write-back path from the decoder's control signals
  • Trace an instruction through the datapath in one cycle, with real values
  • Read a core test's message and find the cycle and the wire that caused it

Before You Start

From here on your Verilog work is mostly wiring: instances, connections by name and multiplexers. The Verilog Tutorial covers module instantiation. The Datapath: ALU, Multiplexers and Control describes the datapath this article builds, and Arithmetic, Logic and Immediates the instructions it executes.

Pass E01 to E06 first: this exercise uses your ALU (E01 and E02), register file (E03), immediate generator (E04), decoder (E05) and PC (E06). It is your first core exercise, so read the rules for them in Build a RISC-V Core: Section Overview: you submit one file with all those modules followed by cvr32_core, and the register file instance must be named u_rf.

What This Core Runs

The first core implements 21 instructions: the ten register-register ALU instructions, the nine register-immediate ones, lui and auipc. These are the instructions that read registers, compute, and write a register, and nothing else. They need no data memory and never change the order of execution, so the PC always moves on by 4. That makes the core short to write and easy to check: each cycle, exactly one register is written with one value, and you can compare that value with what the instruction should produce.

The ports are the full core ports from The Core's Specification. The data-memory outputs exist but are not used yet: drive dmem_addr, dmem_wdata, dmem_wstrb, dmem_we and dmem_re with 0. Tie your PC module's redirect to 0.

The Datapath

Here is what to build, with the parts new in this exercise in green:

The single-cycle core for ALU instructions: the PC drives the instruction memory; the instruction goes to the decoder, the immediate generator and the register file instance u_rf; operand multiplexer A chooses rs1 or the PC, operand multiplexer B chooses rs2 or the immediate; the ALU result is written back to rd.
  1. Fetch. Your PC drives imem_addr, and imem_rdata is the instruction of this cycle.
  2. Decode. The instruction goes to three modules at once: the decoder, the immediate generator and the register file's read addresses (rs1 and rs2 come from the decoder, which takes them straight from the instruction bits).
  3. Operand A. A three-way multiplexer driven by a_sel: the register value rs1 (0), the PC (1, for auipc) or zero (2, for lui).
  4. Operand B. A two-way multiplexer driven by b_imm: the register value rs2 (0) or the immediate (1).
  5. Execute. The ALU computes with alu_op from the decoder.
  6. Write-back. The ALU result goes to the register file's write data, the destination register rd to its write address, and reg_write to its write enable. The write happens at the clock edge that ends the cycle, at the same moment the PC moves on.

Everything between the PC and the register file's write port is combinational. When the PC changes at a clock edge, the new instruction appears, the decoder and the immediate generator settle, the register file outputs change, the multiplexers and the ALU follow, and by the end of the cycle the result is waiting at the register file's input. The clock period of a single-cycle core must be long enough for that whole chain; Single-Cycle and Multi-Cycle Processors discusses what that costs.

The multiplexers are a few lines each: a conditional expression or a case on the select signal. Declare a logic signal for every connection between modules, give them clear names (rs1_val, alu_a, alu_y and so on), and connect the instances by port name. With named connections, a mistake shows up as a wrong name instead of a silently swapped pair of wires.

One Instruction, Cycle by Cycle

The test program starts like this (the listing shows the registers by their ABI names: ra is x1, sp is x2, gp is x3):

   0:	123450b7          	lui	ra,0x12345
   4:	67808093          	addi	ra,ra,1656
   8:	fff00113          	li	sp,-1
   c:	00500193          	li	gp,5

These are the values in the first two cycles of a correct core, after reset:

SignalCycle 1: lui ra, 0x12345Cycle 2: addi ra, ra, 1656
PC0x0000_00000x0000_0004
instruction0x1234_50B70x6780_8093
a_sel, operand A2, zero0, rs1 = x1 = 0x1234_5000
b_imm, operand B1, imm = 0x1234_50001, imm = 0x0000_0678
alu_op, resultADD, 0x1234_5000ADD, 0x1234_5678
register write at the end of the cyclex1 = 0x1234_5000x1 = 0x1234_5678

Notice cycle 2: the addi reads x1, which the lui wrote at the edge between the two cycles, so the register file already returns the new value. In a single-cycle core every instruction finishes before the next one starts, so this always works. In a pipeline it does not, and that is what A Data Hazard and Its Forwarding Path is about.

Quick Check

Q: The program contains auipc s11, 0 at address 0x6C (s11 is x27). Which operands does the ALU see, and what is written to x27?

Show Answer

a_sel = 1 selects the PC, 0x0000_006C, and b_imm = 1 selects the immediate, 0 (the upper immediate is 0). The ALU adds them, and x27 = 0x0000_006C: the address of the instruction itself. That is how position-independent code finds where it is running.

Writes to x0

The program contains addi x0, x0, 1. The decoder sets reg_write = 1, as for any addi, and the register file ignores the write because the destination is x0. Your core needs no special case: the rule lives in the register file. The test does the same: it compares only writes to registers other than x0. Your core may assert the write enable with address 0 as often as it likes.

How It Is Tested

The test program, alu.S, has 35 instructions and makes 34 register writes (all except the one to x0). It covers every ALU instruction, including SUB, the signed and unsigned comparisons with a negative operand, the shifts by 31 and by 37 (which must shift by 5), the largest and smallest 12-bit immediates, and both upper-immediate instructions. It has no stores, so it cannot report a result through EXIT; the testbench stops when it has seen all 34 writes.

The testbench loads the program into a 64 KB memory at address 0 and releases reset. In every cycle it looks at your register file's write port, u_rf's we, wa and wd, and when we is 1 and wa is not 0, it compares the write with the next one the reference core made. The first difference ends the test with a message that numbers the write. Here are two real ones, from mistakes the test was checked against:

Output
FAIL: register write 1: expected x1 = 0x12345000, got x1 = 0xxxxxxxxx
FAIL: register write 28: expected x27 = 0x0000006c, got x27 = 0x00000000

The first comes from a core whose operand B multiplexer is reversed: for lui it passes rs2 instead of the immediate. A lui has no rs2; bits 24 to 20 are part of its immediate, here register x3, which has never been written, so in Icarus Verilog it reads as unknown, X. The very first write of the program is therefore all X: a strong hint that the core used a register it should not have. The second comes from a core whose operand A multiplexer passes zero instead of the PC for a_sel = 1: the 28th write is the auipc from the Quick Check, and it gets 0 instead of its own address.

To debug a message like this, count the instructions: write 28 is the 28th instruction that writes a register. Find it in the listing, then find the cycle in your waveform where your core executes it (in a single-cycle core, the cycle number equals the instruction number), and look at the operands and control signals of that one cycle.

Where Does This Matter?

Comparing every register write with a reference model, instruction by instruction, is how processor teams verify cores. The reference is usually an instruction set simulator such as Spike for RISC-V, which executes the same program in software, and the comparison reports the first instruction where the hardware differs. Verifying a CPU describes the method; the core tests in this section are a small version of it.

Quick Check

Q: A core passes all 34 writes of the test except that every slli, srli and srai writes the wrong value, while sll, srl and sra are right. Where is the bug most likely, given that the ALU passed E02?

Show Answer

In the operand B path for immediates: the register forms take the shift amount from rs2, the immediate forms from the immediate. With an ALU that is known to be right, the suspects are the B multiplexer, the b_imm signal for op-imm instructions, or the immediate generator for I-type shifts (whose immediate holds the shift amount in its low 5 bits).

Common Mistakes

Mistake #1: Swapping the inputs of a multiplexer

A select signal of 1 must pick what the specification says: the immediate for b_imm, the PC for a_sel = 1. A reversed multiplexer compiles and runs; the test shows it at the first instruction that uses it.

Mistake #2: Renaming the register file instance

The test reads u_core.u_rf.we, wa and wd. With any other instance name the test does not compile, and the message comes from the simulator, not from the test.

Mistake #3: Leaving the unused outputs undriven

Outputs that nothing drives are X in simulation. The data-memory outputs are not used yet, but drive them with 0 so that dmem_we is not X; later exercises connect them for real.

Your Turn in the Lab

The exercise opens with a visible test, testbench.v, next to your design: the program alu.S, run on your core and compared write by write with the reference core, naming the first write that differs. Simulate it as you work and look at the waveform where it fails. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.

Write cvr32_core for E07. Paste your five modules from your project into the submission, then the core. Before submitting, simulate it in your project with the first few instructions of the listing above in a memory, and check the first two cycles against the table.

Open exercise E07 in ChipVerify Lab

Summary

  • The first core connects the PC, decoder, immediate generator, register file (instance u_rf) and ALU; it executes the 21 ALU, lui and auipc instructions in one cycle each.
  • Operand A is rs1, the PC or zero (a_sel); operand B is rs2 or the immediate (b_imm); the ALU result is written to rd at the clock edge when reg_write is 1.
  • Unused data-memory outputs are driven with 0 and redirect is tied to 0 until branches arrive.
  • Writes to x0 need no special case in the core; the register file ignores them and the test does not compare them.
  • The test compares all 34 register writes of a 35-instruction program with a reference core and numbers the first one that differs.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.