Every real processor starts as a specification: a document that says which instructions the core runs, what its ports are, how it talks to memory, what happens at reset and how its parts are divided. This article is that specification for cvr32, the core you build in this section. It lists the 37 instructions the core implements, the modules you will write and the exercises that test them, the port list and timing of the memory interface, the fixed codes that let your modules work together, and the coding rules that keep your Verilog portable across simulators. Everything the hidden tests assume is written down here.

14 min read | Beginner to Intermediate

What You'll Learn

  • List the instructions the core implements and the architectural state it keeps
  • Describe the core's ports, its memory timing and how a program ends and prints
  • Name the modules of the core, the fixed codes between them and the exercise for each
  • Apply the coding rules that make your modules work in every simulator and in synthesis

Before You Start

From here on you write Verilog, so you need to know it: modules and ports, always blocks, blocking and nonblocking assignments, case statements and vectors. The Verilog Tutorial covers all of it. This article introduces the few SystemVerilog words the core uses.

Read Build a RISC-V Core: Section Overview for how the exercises work. From the first half of the tutorial, What an ISA Defines explains why an instruction set is a contract, and The Datapath: ALU, Multiplexers and Control describes the blocks you are about to build.

What a Specification Is For

A processor specification separates two questions: what the core must do, and how it does it. The "what" is fixed by the instruction set, RISC-V RV32I: for every instruction, the manual defines exactly which registers and memory it reads and writes. The "how" is the microarchitecture, the design of the hardware, and it is yours: a single-cycle core and a pipelined core implement the same instructions very differently, and both are correct if every program gives the same results. ISA, Microarchitecture and Implementation explains the distinction.

The specification also fixes the things a test needs in order to check your design without knowing how you built it: module names, ports, and the meaning of the signals between modules. Those are the parts of this article you must follow to the letter.

The Instructions

The core implements 37 of the 40 RV32I instructions:

GroupInstructionsCount
register-register arithmetic and logicadd, sub, sll, slt, sltu, xor, srl, sra, or, and10
register-immediate arithmetic and logicaddi, slti, sltiu, xori, ori, andi, slli, srli, srai9
upper immediateslui, auipc2
loadslb, lh, lw, lbu, lhu5
storessb, sh, sw3
branchesbeq, bne, blt, bge, bltu, bgeu6
jumpsjal, jalr2

The three left out are fence, ecall and ebreak. fence orders memory accesses as other cores or devices see them, which matters only with several cores or caches; ecall and ebreak call an operating system or a debugger, which need the trap hardware that Traps and Precise Exceptions describes. None of the test programs contains them. The seven C programs use 25 different instructions between them; the assembly test programs were written to use the other twelve, so every implemented instruction is tested.

There is no multiply or divide. When a C program multiplies, gcc compiled for rv32i calls a small library routine that multiplies with shifts and adds, which runs on your core like any other code.

Architectural State

The architectural state is what a program can see: everything an instruction can read or change. For this core it is:

  • 32 registers of 32 bits, x0 to x31. Register x0 always reads as zero, and writes to it are ignored. Registers and the Register File explains why.
  • The program counter (PC): the address of the instruction being executed. It is not one of the 32 registers.
  • Memory: byte-addressed and little-endian. Address 0x0000_0040 names one byte, a 32-bit word at that address occupies bytes 0x40 to 0x43, and the byte at the lowest address is the least significant one (Memory, Addresses and Endianness).

Accesses are naturally aligned: words at addresses that are multiples of 4, halfwords at multiples of 2. The compiler produces only aligned accesses, so the core does not need to handle the other case.

Reset

The reset input rst_n is active-low (the "_n" means the core is held in reset while it is 0) and asynchronous (it acts immediately, without waiting for a clock edge). While reset is low, the PC is 0x0000_0000. When reset goes high, the core fetches its first instruction from address 0, where the start-up code is.

Reset does not clear the registers. Real programs never read a register before writing it, and the start-up code sets the stack pointer first. A core that depends on registers starting at zero passes some tests by luck, and the simulators disagree about it: in Icarus Verilog an unwritten register reads as unknown (X), in Verilator as zero.

The Ports and the Memory

This is the top module's port list. It matches the boot template the lab's Software panel uses, so the same core can later run your own C programs in a project:

module cvr32_core (
    input  logic        clk,
    input  logic        rst_n,

    output logic [31:0] imem_addr,     // instruction port
    input  logic [31:0] imem_rdata,
    input  logic        imem_ready,

    output logic [31:0] dmem_addr,     // data port
    output logic [31:0] dmem_wdata,
    output logic [3:0]  dmem_wstrb,
    output logic        dmem_we,
    output logic        dmem_re,
    input  logic [31:0] dmem_rdata,
    input  logic        dmem_ready
);

There are two memory ports, one for fetching instructions and one for loads and stores, both connected to the same 64 KB memory starting at address 0. Two ports let the core fetch an instruction and access data in the same cycle, which a single-cycle core must do.

The cvr32_core box contains the modules cvr32_pc, cvr32_decode, cvr32_imm, cvr32_regfile with instance name u_rf, cvr32_alu, cvr32_branch and cvr32_lsu, plus your own wiring and the retire output. The memory on the left connects through the instruction and data ports, and the testbench on the right watches the EXIT and CONSOLE stores and the retire output.

The memory's timing is what makes a single-cycle core possible:

  • Reads answer in the same cycle. The word at imem_addr appears on imem_rdata without waiting for a clock edge, and the same for dmem_addr and dmem_rdata. The memory uses address bits 15 to 2, so it always returns the whole aligned word that contains the address.
  • Writes happen at the clock edge. When dmem_we is 1 at a rising edge of clk, the memory writes the bytes of dmem_wdata whose bits in dmem_wstrb are 1. Bit 0 of dmem_wstrb enables bits 7 to 0 of the word, bit 1 enables bits 15 to 8, and so on. This per-byte control is how sb changes one byte without touching its neighbours.
  • dmem_re marks a load. This memory does not need it, but real memories do, so the core drives it. The two ready inputs are always 1 here and the core can ignore them.
Illustration of three clock cycles. In each, the instruction address and the instruction appear in the same cycle. In cycle 2 a load's address and data appear in the same cycle. In cycle 3 a store sets dmem_we and byte enable 0010, and the memory writes the byte at the clock edge that ends the cycle.

Real on-chip memories are slower: most return read data one cycle after the address. A memory that answers in the same cycle is an idealization that keeps the first core simple. The pipelined core in the last chapter takes advantage of it too, and that article explains what a one-cycle memory would change.

Quick Check

Q: A program executes sh a2, 2(a3) with a3 = 0x0000_1000. What are dmem_addr, the word the memory changes, and dmem_wstrb?

Show Answer

dmem_addr is 0x0000_1002. The memory uses bits 15 to 2, so it changes the word at 0x0000_1000. A halfword at byte offset 2 occupies the upper two bytes of that word, so dmem_wstrb is 1100, and the halfword must be on bits 31 to 16 of dmem_wdata.

Ending a Program and Printing

Two addresses are not memory. A store to 0xF000_0000 (EXIT) ends the program: the testbench takes the stored value as the program's result and stops the simulation. A store to 0xF000_0004 (CONSOLE) prints bits 7 to 0 of the stored value as a character. Your core does nothing special for either; it just performs the store, and the testbench watches the data port.

From exercise E12 on, the core has one more output, retire: 1 in every cycle in which an instruction completes. The testbench counts those cycles to know how many instructions ran. In a single-cycle core every cycle after reset completes an instruction; in the pipeline it is not so simple, which is what the last chapter is about.

The Modules

The core is divided into seven modules plus the top level that connects them. Each has a fixed name and port list, given in its exercise:

ModuleJobExercise
cvr32_aluarithmetic and logic on two 32-bit operandsE01, E02
cvr32_regfilethe 32 registers, two reads and one write per cycleE03
cvr32_immthe immediate of an instruction, sign-extended to 32 bitsE04
cvr32_decoderegister numbers and control signals from an instructionE05
cvr32_pcthe program counter and the choice of the next PCE06
cvr32_lsubyte enables and data placement for stores, extension for loadsE08
cvr32_branchwhether a branch condition holdsE10
cvr32_corethe top level: your wiring, multiplexers and write-backE07, E09, E11, E12; pipelined in E13, E15, E16

The pipeline adds one more small module, cvr32_fwd, in E14. Inside cvr32_core, the register file must be an instance named u_rf, because the core tests watch its write port to compare your core's register writes with the reference.

The Codes Between Modules

Modules that you write separately must agree on what their signals mean. Three encodings are fixed. The ALU operation, a 4-bit code from the decoder to the ALU:

Code0123456789
OperationADDSUBSLLSLTSLTUXORSRLSRAORAND

The ALU's first operand, a_sel: 0 selects rs1, 1 the PC (for auipc) and 2 the value zero (for lui, so the ALU adds the immediate to zero). And the value written to the destination register, wb_sel: 0 the ALU result, 1 the loaded data, 2 PC + 4 (the return address that jal and jalr save). The second operand is chosen by a 1-bit b_imm: the immediate or rs2. The Decoder and Immediate Generator gives the value of every control signal for every kind of instruction.

Where Does This Matter?

In industry the specification is the contract between the people who design a core, the people who verify it and the people who write its software, often in different teams and countries. An interface written down once, with fixed signal names and encodings, is what lets the verification team build its testbench before the design is finished, which is exactly how the tests for this section were written before you start. Writing a Microarchitecture Spec covers how such documents are written.

Coding Rules

The exercises run on Icarus Verilog and Verilator, and the last chapter synthesizes your core with Yosys. Code that follows these rules works in all three.

Use these SystemVerilog words:

  • logic for every signal. It replaces Verilog's wire and reg: a logic can be driven by an assign or by an always block, so you never have to decide between the two.
  • always_ff @(posedge clk) for registers (flip-flops). Write them with nonblocking assignments (<=). For the asynchronous reset, write always_ff @(posedge clk or negedge rst_n).
  • always_comb for combinational logic, with blocking assignments (=). If Icarus warns about "constant selects" inside it, write always @(*) instead, which means the same.
  • localparam for named constants, such as the ALU codes.

And keep to these rules:

  • In combinational logic, give every output a value in every path, for example with a default before a case. Otherwise the tools infer a latch, a storage element you did not intend, and the simulators and synthesis may disagree.
  • Keep the reset logic and the data registers in separate always_ff blocks when only some registers are reset. A block that resets some of its registers and not others can be rejected by Yosys.
  • Do not use SystemVerilog interfaces, modports or the streaming operators ({<<{...}}). Not every open-source tool handles them, and the core does not need them.
  • Do not write `timescale or use delays (#5) in the design. Timing belongs to the testbench.

Quick Check

Q: A decoder's always_comb block sets mem_write = 1 inside the case item for stores, and nowhere else. What goes wrong?

Show Answer

For every other instruction the block does not assign mem_write, so it keeps its last value: a latch. After the first store, mem_write stays 1 and every later instruction writes memory. Set mem_write = 0 (and every other output) as a default at the top of the block, then override it in the store case.

Setting Up Your Project

Create one project in ChipVerify Lab for your core, with one file per module: cvr32_alu.sv, cvr32_regfile.sv and so on, and cvr32_core.sv. Each time you pass a block exercise, copy your solution into its file. For a core exercise, paste the files you need into the submission, in any order, followed by your cvr32_core.

Your project is also the place to experiment. You can write your own small testbench for a block, simulate it and look at the waveform before you submit. The hidden tests are thorough, but a waveform you made yourself is the fastest way to understand why one failed.

Common Mistakes

Mistake #1: Making the core depend on registers starting at zero

Reset sets only the PC. Logic that works because a register happened to start at zero passes in one simulator and fails in another. If a register must have a value after reset, such as a valid bit in the pipeline, reset it explicitly.

Mistake #2: Treating dmem_addr as a word address

All addresses in the core are byte addresses: the PC, imem_addr and dmem_addr. Consecutive instructions are 4 apart, and the byte offset within a word is dmem_addr[1:0], which the load/store unit needs.

Mistake #3: Inventing your own control encodings

A decoder that numbers the ALU operations differently from the table above can be perfectly logical and still fail every test, because the ALU you wrote earlier, and the tests, use the fixed codes.

Your Turn in the Lab

This article has no graded exercise. Set up the project you will use for the rest of the section:

  1. Create a project named, for example, my-cvr32.
  2. Add the eight files listed in the module table, each with just its module name, the port list from its exercise and endmodule. The exercises' starters contain the port lists.
  3. Open exercise E01 and read its statement. You will write it after The ALU.
Open the course Computer Architecture: Build a RISC-V Core in ChipVerify Lab

Summary

  • The core implements 37 RV32I instructions, everything except fence, ecall and ebreak; its state is 32 registers with x0 fixed at zero, the PC and byte-addressed little-endian memory.
  • Reset is active-low and asynchronous and sets only the PC, to 0x0000_0000.
  • The core has an instruction port and a data port to a 64 KB memory that answers reads in the same cycle and writes the enabled bytes at the clock edge; stores to 0xF000_0000 and 0xF000_0004 end the program and print.
  • The core is divided into seven named modules plus cvr32_core, with fixed port lists, fixed ALU codes and operand and write-back selects, and the register file instance u_rf.
  • Write logic, always_ff and always_comb, give every combinational output a value in every path, and avoid interfaces, streaming operators and delays.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.