The Core as an IP: Ports, Configuration and the Capstone Step

For fifteen pages you looked inside the processor. A chip designer never does. To them the core is a box with ports, and a short list of things that must be connected right.

You Will Be Able To

  • Name the classes of port of ap_rv32, and say which ones a chip must connect for the core to run.
  • Say what the reset vector and the hart number are, and where a program can read what the core is.
  • Read one record of the retire trace port, and say what the port is for.
Lesson
S2.16
Time
35 min
Runs on
Your browser
Check
6 questions

This is the capstone step of the module. The core that you built in pieces becomes one block of the chip ap_soc.

Before You Start

You need the five-stage core from S2.7 to S2.10, and the trap CSRs from S2.13.

It helps to have met a bus manager, in S0.5 or in the AHB lesson P4.2.

Five questions this page answers
Question
1What is an IP block?
2Which groups of ports does ap_rv32 have?
3What does the reset vector decide?
4What is the hart number, and how does software read it?
5What does one record of the trace port describe?

The Idea: A Box with a Contract

A chip is put together from finished blocks. Each block was designed and tested once, and is then used as it is.

Such a block is an IP block. IP is short for intellectual property. A processor core is the largest IP block of a small chip.

Whoever uses the block reads its ports, and not its inside. So the ports must say everything the chip needs to know.

Some ports do the work: the core fetches instructions and moves data. Some receive events: the interrupts.

Some give the core its place. A core does not know where its program starts. The chip tells it, on an input called the reset vector.

It also does not know which processor of the chip it is. RISC-V calls one processor a hart.

The chip gives each hart a number, the hart number.

And some ports only report. One group of outputs is the trace port.

More exactly it is the retire trace port: it shows one record for every instruction that the core has finished.

The trace port changes nothing in the chip. It is there so that someone can check what the core did.

New Words

New words on this page
WordMeaning
IP blockA finished design block that is used inside a chip without being changed.
Reset vectorThe address of the first instruction after reset. On ap_rv32 it is an input, reset_vector.
HartOne processor that executes one stream of instructions. The RISC-V word for it.
Hart numberThe number of a hart in the chip. An input, hart_id. Software reads it in the CSR mhartid.
TieTo connect an input to a constant value in the chip.
RetireAn instruction retires when it is finished and its result is final.
Retire trace portOutputs of the core that describe each retired instruction: its address, its word, and what it changed.
mimpidA read-only CSR with the version of the core. ap_rv32 version 1.0 reads 0001_0000.
misaA read-only CSR that says which instruction set the core has.

One Picture: Every Port of the Core

The Core as a Box: Eight Classes of PortStep through the picture. The box is ap_rv32 as a chip designer sees it: 76 ports, in eight groups.
  • Clock / reset
  • Pads
  • Peripheral
  • Processor
  • Bus
  • Power
  • Debug
The Core as a Box: Eight Classes of PortStep through the picture. The box is ap_rv32 as a chip designer sees it: 76 ports, in eight groups. component Clock and reset 2 inputs component Configuration 2 ports:reset_vector,hart_id component Interrupts 3 inputs: external,timer, software component ap_rv32 the five-stage corewith its two bus ports component Instruction busport 11 ports with theprefix i_ component Data bus port 11 ports with theprefix d_ component Sleep 2 outputs: asleep, andwake me component Trace and view 25 and 20 outputs,for observation only one clock, one resetwhere to start, whoam Ithree levelsfetchloads and storesasleep, wakeone record for eachinstruction
  1. component Clock and reset2 inputs
    • one clock, one reset → ap_rv32
  2. component ap_rv32the five-stage core with its two bus ports
    • fetch → Instruction bus port
    • loads and stores → Data bus port
    • asleep, wake → Sleep
    • one record for each instruction → Trace and view
  3. component Configuration2 ports: reset_vector, hart_id
    • where to start, who am I → ap_rv32
  4. component Instruction bus port11 ports with the prefix i_
  5. component Data bus port11 ports with the prefix d_
  6. component Interrupts3 inputs: external, timer, software
    • three levels → ap_rv32
  7. component Sleep2 outputs: asleep, and wake me
  8. component Trace and view25 and 20 outputs, for observation only
  1. What every block needs

    A clock and a reset. The core has one of each, and every flip-flop uses them.

  2. Its place in the chip

    Two inputs are tied by the chip and never change: the address of the first instruction, and the number of this processor.

  3. Its work

    Two bus ports. One fetches instructions. The other does the loads and stores. S2.33 looks inside them.

  4. Events from outside, and rest

    Three interrupt inputs come in. Two outputs say that the core sleeps, and that it should be woken.

  5. What it did

    The trace port reports every instruction as it finishes. The view port shows the five stages. No logic of the chip may depend on them.

Read the diagram as text
  • Clock and reset: 2 inputs
  • Configuration: 2 ports: reset_vector, hart_id
  • Interrupts: 3 inputs: external, timer, software
  • ap_rv32: the five-stage core with its two bus ports
  • Instruction bus port: 11 ports with the prefix i_
  • Data bus port: 11 ports with the prefix d_
  • Sleep: 2 outputs: asleep, and wake me
  • Trace and view: 25 and 20 outputs, for observation only
  • Clock and reset → ap_rv32: one clock, one reset
  • Configuration → ap_rv32: where to start, who am I
  • Interrupts → ap_rv32: three levels
  • ap_rv32 → Instruction bus port: fetch
  • ap_rv32 → Data bus port: loads and stores
  • ap_rv32 → Sleep: asleep, wake
  • ap_rv32 → Trace and view: one record for each instruction

Three groups make the core run, two tell it where it is, one lets it rest, and two only let you watch.

The eight classes, counted on the real top level
ClassPortsDirectionMust the chip connect it?
Clock and reset2inyes
Configuration: reset_vector, hart_id2inyes: tied to constants
Instruction bus port11out and inyes: to the memory with the program
Data bus port11out and inyes: to memory and devices
Interrupt inputs3inyes: to their sources, or tied to 0
Sleep outputs2outonly if the clock is to be gated
Retire trace port25outno: for a test bench or a trace unit
Pipeline view20outno: for drawing the five stages

Together 76 ports. A debug request is planned as a ninth class. It is not built in version 1.0.

The Smallest Example, Worked by Hand

The hello program
Address, in hexInstructionWhat it is for
100lui x8, 0x80000x8: the RAM, at 8000_0000
104csrr x5, mhartidwhich processor am I?
108csrr x6, mimpidwhich version of the core?
10caddi x7, x0, 72the letter H
110add x7, x7, x5H for hart 0, I for hart 1
114sw x7, 0(x8)the result word
118sw x6, 4(x8)the version word
11cjal x0, 0x11cstop

It is placed at 100 in hex. It asks the core who it is, and writes two words to the RAM at 8000_0000.

Suppose the chip ties reset_vector to 0000_0100 and hart_id to 0. What does the program do?

After reset the core fetches at 100. The second row reads mhartid: it reads the tied value, 0.

The third row reads mimpid. That value is built into the core: 0001_0000, which means version 1.0.

The program adds the hart number to 72, the code of the letter H. On hart 0 the result word is 48 in hex.

Who decides what
ValueWhere it comes fromCan software change it?
The first address, 100The input reset_vector, tied by the chipno
mhartid, 0The input hart_id, tied by the chipno: the CSR is read-only
mimpid, 0001_0000A constant inside the coreno
misa, 4000_0100A constant inside the core: 32 bits, base instruction setno: writes are ignored

The chip gives the core its place. The core tells software what it is.

The Rules

  1. Rule 1: The first instruction is fetched from the address on reset_vector.

    The chip ties it to where its boot memory is. In ap_soc that is 0000_0000.

  2. Rule 2: mhartid reads the input hart_id.

    A chip with one core ties it to 0. A second core gets 1.

  3. Rule 3: mimpid and misa are constants of the core.

    Software reads them to learn the version and the instruction set.

  4. Rule 4: An interrupt input that has no source is tied to 0.

    A floating input is an unknown value in the CSR mip.

  5. Rule 5: The trace port shows one record for each instruction that retires, and for each trap.

    Records come in program order. Each has a number that counts up from 0.

  6. Rule 6: Nothing in the chip may depend on the trace port or the view port.

    They are for observation. The chip must work the same with them left open.

Sources for These Rules

The ports, their widths and their meanings are those of the real core: tools/soc/ap_rv32/rtl/ap_rv32.sv and docs/soc/ap_rv32-blueprint.md, section 3. The port counts on this page are read from that file. The identity CSRs and their values are in the blueprint, section 5.1. Their addresses and meanings come from the RISC-V privileged specification, which is not in our library: not checked against it. The signal names of the bus ports are those of AHB-Lite (Arm IHI 0033C, section 2.2), which the P4 lessons teach. The trace port is our own design. Its fields follow a public interface for processor verification where one exists; that relation is not checked here. Every row of the lab was also given by the real core, with the same two inputs.

Now Run It

The lab at the end of this page is a model of the core. It takes the two configuration values as the real core takes its inputs.

Printed by the lab on Icarus. The real core ap_rv32 ran the same program with reset_vector at 0000_0100 and hart_id at 0. Every row agrees with its trace.
0 | 00000100 | lui x8, 0x80000 | x8 = 80000000
1 | 00000104 | csrr x5, mhartid | x5 = 00000000
2 | 00000108 | csrr x6, mimpid | x6 = 00010000
3 | 0000010c | addi x7, x0, 72 | x7 = 00000048
4 | 00000110 | add x7, x7, x5 | x7 = 00000048
5 | 00000114 | sw x7, 0(x8) | mem[80000000] = 00000048
6 | 00000118 | sw x6, 4(x8) | mem[80000004] = 00010000
7 | 0000011c | jal x0, 0x11c | the program stops here
result word=00000048 version word=00010000 instructions=7 traps=0

Each row begins with a number. It is the record number of the trace port: the first instruction is record 0.

Rule 1 at work: row 0 has the address 00000100. On the real core, the first fetch was at that address too.

Rules 2 and 3 at work: rows 1 and 2. x5 reads 0, the hart number. x6 reads 00010000, the version.

Row 5 is the store of the result word. Here is the same instruction as the real core reported it on its trace port.

Record 5 of the real trace port, field by field
FieldValue, in hexMeaning
trace_order5the sixth record since reset
trace_pc00000114the address of the instruction
trace_insn00742023the instruction word: sw x7, 0(x8)
trace_next_pc00000118the address executed next
trace_priv3the privilege level: machine
trace_trap0no trap was taken
trace_rs1_addr, trace_rs1_rdata8, 80000000it read x8
trace_rs2_addr, trace_rs2_rdata7, 00000048it read x7
trace_rd_addr0it wrote no register
trace_mem_addr80000000the address of the store
trace_mem_wmask, trace_mem_wdataf, 00000048four bytes written, and their value
trace_mstatus00001800mstatus after this record

Recorded from the real core. The record has 24 fields; the others are 0 here.

Rule 5 at work: with these fields, a test bench can follow the whole program without looking inside the core.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
The reset vector is an inputA constant inside the coreThe same core fits a chip with its boot memory anywhere. One input more to tie right.
The hart number is an inputA parameter of the moduleTwo copies of one built block can differ. 32 wires for one small number.
Two bus portsOne port for fetch and dataA fetch and a load in the same cycle. Twice the bus signals.
Interrupts as three plain levelsA bus to an interrupt controllerNothing to decode in the core. Priority among devices is done outside, in the controller of S6.
A trace port with every effect of an instructionOnly the PCA model can be compared with the core record by record. 25 outputs that the chip itself never uses.
No parameter changes the behaviourOptions for every chipOne design to verify. Less choice.

This is the capstone step. A chip that wants this core must do five things.

It gives the core a clock and a reset. It ties two inputs. It connects two bus ports. And it wires or ties three interrupt lines.

Everything else is optional. That short list is what "the core as an IP" means.

The Trap: A Reset Vector from Another Chip

The program above was placed at 100. Suppose the chip ties reset_vector to 0, because the last chip had its program there.

The program at 100, started at address 0. The lab stops after 6 steps. The real core, with reset_vector at 0, gave the same 6 records.
0 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
1 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
2 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
3 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
4 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
5 | 00000000 | .word 0x00000000 | TRAP mcause=2 mepc=00000000 mtval=00000000, next is 00000000
still running after 0 instructions and 6 traps
result word=00000000 version word=00000000 instructions=0 traps=6

The core fetches at 0. The memory there holds zeros. A word of zeros is no instruction: a trap, cause 2.

No handler was installed, so mtvec is 0. The trap goes to address 0, and meets the same zeros.

The core is alive. Its clock runs and its bus is busy. But no row of the program ever runs, and nothing reports a fault.

So a reset vector is checked against the memory map of the chip it is in, and not against the last one.

At Scale

This lab1 core, 1 program, 2 words of result
ap_socthe core, two memories, a bus fabric and a dozen devices

This section looks ahead. One core, a ROM and a RAM are the smallest system. The rest of the track builds the chip around this box.

  1. The bus ports

    The 22 bus signals follow AHB-Lite. S2.33 shows what the core puts on them, and how a simple memory port becomes a bus manager.

  2. A second core

    A chip with two harts builds the core twice and ties hart_id to 0 and 1. S9.1 starts the second one.

  3. Verification lives on the trace port

    A model of the instruction set runs beside the core, and every record is compared. The list of illegal instructions of S2.22 and the causes of S2.29 are its test plan.

  4. The debug port

    A debugger must stop the core and read its registers. Those ports are planned for a later version, and S8.1 says what they need.

Try It

Run the lab. Find the two rows that read a CSR, and say where each value comes from.

Set hart_value to 1 and run again. Which row changes first, and what is the result word?

Set reset_pc to 104 in hex, one instruction too far. Which register is never written, and what address does the first store use?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 0 | 00000100 | lui x8, 0x80000 | x8 = 80000000 1 | 00000104 | csrr x5, mhartid | x5 = 00000000 2 | 00000108 | csrr x6, mimpid | x6 = 00010000 3 | 0000010c | addi x7, x0, 72 | x7 = 00000048 4 | 00000110 | add x7, x7, x5 | x7 = 00000048 5 | 00000114 | sw x7, 0(x8) | mem[80000000] = 00000048 6 | 00000118 | sw x6, 4(x8) | mem[80000004] = 00010000 7 | 0000011c | jal x0, 0x11c | the program stops here result word=00000048 version word=00010000 instructions=7 traps=0

Check Yourself

Predict the outputThe hello program runs on a core whose hart_id input is tied to 1. Everything else is the same. What does the last line say?
// The same program on a core whose hart_id input is 1. What does the last line say?
// The program is the task at the top. Each row is one instruction: i(kind, a, b, c).
// The comment beside a row is its address and the same instruction in assembly.
// A model beside the core. The real ap_rv32 takes the same two values on its inputs reset_vector and hart_id.
module tb;
  task automatic the_program();
    at('h100);
    i(LUI,    8,  0,  'h80000);   // 100: lui x8, 0x80000      x8: the RAM, at 8000_0000
    i(CSRR,   5,  0,  MHARTID);   // 104: csrr x5, mhartid      which processor am I?
    i(CSRR,   6,  0,  MIMPID);    // 108: csrr x6, mimpid      which version of the core?
    i(ADDI,   7,  0,  72);        // 10c: addi x7, x0, 72      the letter H
    i(ADD,    7,  7,  5);         // 110: add x7, x7, x5      H for hart 0, I for hart 1
    i(SW,     7,  8,  0);         // 114: sw x7, 0(x8)      the result word
    i(SW,     6,  8,  4);         // 118: sw x6, 4(x8)      the version word
    i(JAL,    0,  0,  'h11c);     // 11c: jal x0, 0x11c      stop
  endtask

  // ---- From here down: a small model of a RISC-V processor with traps, as test code. ----
  // It is a model beside the real core ap_rv32, not a part of it.
  localparam int LUI = 0, ADDI = 1, ANDI = 2, ORI = 3, SLLI = 4, SRLI = 5, ADD = 6, SUB = 7, AND = 8, OR = 9, LW = 10, SW = 11,
                 BEQ = 12, BNE = 13, BLT = 14, BGE = 15, JAL = 16, JALR = 17, CSRR = 18, CSRW = 19, CSRS = 20, CSRC = 21, MRET = 22, ECALL = 23, EBREAK = 24, WORD = 25;
  localparam int MSTATUS = 'h300, MISA = 'h301, MTVEC = 'h305, MSCRATCH = 'h340, MEPC = 'h341, MCAUSE = 'h342, MTVAL = 'h343, MIP = 'h344, MIMPID = 'hf13, MHARTID = 'hf14;
  int kind [0:1023], fa [0:1023], fb [0:1023];   // the program: one row for each instruction
  logic [31:0] fc [0:1023];
  int here = 0;                                   // the address of the next row
  task automatic at(input int address);           // the rows that follow start at this address
    here = address;
  endtask
  task automatic i(input int k, input int a, input int b, input logic [31:0] c);   // add one row to the program
    if (here < 4096) begin kind[here >> 2] = k; fa[here >> 2] = a; fb[here >> 2] = b; fc[here >> 2] = c; end
    here += 4;
  endtask
  logic [31:0] x [0:31];             // the 32 registers
  logic [31:0] pc;                   // the address of the next instruction
  logic [31:0] mem [0:16383];        // 64 KiB of data memory, from address 8000_0000 up
  logic [31:0] mstatus, mtvec, mscratch, mepc, mcause, mtval;   // the machine CSRs of this model
  logic [1:0]  level;                // the privilege level: 3 is machine (the only one here)
  localparam bit USER = 0;           // 1: the model has a user level. ap_rv32 v1.0 has none
  logic [31:0] reset_pc = 0;         // the address of the first instruction: the input reset_vector of a core
  logic [31:0] hart_value = 0;       // what mhartid reads: the input hart_id of a core
  logic [31:0] mip_value = 0;        // what mip reads: the three interrupt inputs of a core, at bits 11, 7 and 3
  int   steps, traps;
  bit   stopped;
  function automatic string cn(input logic [11:0] a);   // the name of a CSR
    case (a)
      MSTATUS: return "mstatus";  MTVEC: return "mtvec";  MSCRATCH: return "mscratch";  MEPC: return "mepc";
      MCAUSE: return "mcause";  MTVAL: return "mtval";  MHARTID: return "mhartid";
      MISA: return "misa";  MIP: return "mip";  MIMPID: return "mimpid";
      default: return $sformatf("0x%0h", a);
    endcase
  endfunction
  function automatic string nm(input int k);            // the name of a kind of instruction
    case (k)
      0: return "lui"; 1: return "addi"; 2: return "andi"; 3: return "ori"; 4: return "slli"; 5: return "srli"; 
      6: return "add"; 7: return "sub"; 8: return "and"; 9: return "or"; 10: return "lw"; 11: return "sw"; 
      12: return "beq"; 13: return "bne"; 14: return "blt"; 15: return "bge"; 16: return "jal"; 17: return "jalr"; 
      18: return "csrr"; 19: return "csrw"; 20: return "csrs"; 21: return "csrc"; 22: return "mret"; 23: return "ecall"; 
      24: return "ebreak"; 25: return "word"; 
      default: return "?";
    endcase
  endfunction
  function automatic bit csr_exists(input logic [11:0] a);
    return a == MSTATUS || a == MTVEC || a == MSCRATCH || a == MEPC || a == MCAUSE || a == MTVAL || a == MHARTID || a == MISA || a == MIP || a == MIMPID;
  endfunction
  function automatic logic [31:0] csr_value(input logic [11:0] a);
    case (a)
      MSTATUS: return mstatus;  MTVEC: return mtvec;  MSCRATCH: return mscratch;  MEPC: return mepc;
      MCAUSE: return mcause;  MTVAL: return mtval;
      MISA: return 32'h4000_0100;  MIMPID: return 32'h0001_0000;   // what this core is: 32 bits, base set I; version 1.0
      MIP: return mip_value;
      default: return hart_value;                       // mhartid
    endcase
  endfunction
  task automatic csr_store(input logic [11:0] a, input logic [31:0] v);   // each CSR keeps only its writable bits
    case (a)
      MSTATUS: begin
        mstatus[3] = v[3];  mstatus[7] = v[7];                             // MIE and MPIE
        if (USER && (v[12:11] == 2'b00 || v[12:11] == 2'b11)) mstatus[12:11] = v[12:11];   // MPP: user or machine
      end
      MTVEC:    mtvec = v & 32'hffff_fffd;                                 // bit 1 always reads 0
      MSCRATCH: mscratch = v;
      MEPC:     mepc = v & 32'hffff_fffc;                                  // an instruction address: bits 1:0 read 0
      MCAUSE:   mcause = v;
      MTVAL:    mtval = v;
      default: ;
    endcase
  endtask
  // The entry of a trap: what the processor does by itself, in one step.
  task automatic take_trap(input logic [31:0] cause, input logic [31:0] tval, input string text);
    string lv;
    lv = $sformatf("%0d | ", steps + traps);
    mepc = pc & 32'hffff_fffc;  mcause = cause;  mtval = tval;
    mstatus[7] = mstatus[3];  mstatus[3] = 1'b0;                           // MPIE takes MIE, MIE closes
    if (USER) begin mstatus[12:11] = level;  level = 2'd3; end             // MPP takes the level, the level is machine
    $display("%s%08h | %s | TRAP mcause=%0d mepc=%08h mtval=%08h, next is %08h", lv, pc, text, cause, mepc, mtval, mtvec & 32'hffff_fffc);
    pc = mtvec & 32'hffff_fffc;  traps++;
  endtask
  // One instruction: read the row at the PC, do what it says or take a trap, and print one line.
  task automatic step();
    int k, a, b;
    logic [31:0] c, r1, r2, res, next, ad, word;
    string text, effect, lv;
    bit writes, take, done;
    lv = $sformatf("%0d | ", steps + traps);
    done = 0;  writes = 1;  res = 0;  effect = "";  next = pc + 4;
    if (pc >= 32'h1000) begin take_trap(1, pc, "(no memory here)");  done = 1;  k = WORD;  a = 0;  b = 0;  c = 0; end
    else begin k = kind[pc >> 2];  a = fa[pc >> 2];  b = fb[pc >> 2];  c = fc[pc >> 2]; end
    r1 = x[b[4:0]];  r2 = x[c[4:0]];
    if (!done) case (k)
      LUI:  begin res = c << 12;  text = $sformatf("lui x%0d, 0x%0h", a, c); end
      ADDI: res = r1 + c;
      ANDI: res = r1 & c;
      ORI:  res = r1 | c;
      SLLI: res = r1 << c[4:0];
      SRLI: res = r1 >> c[4:0];
      ADD:  res = r1 + r2;
      SUB:  res = r1 - r2;
      AND:  res = r1 & r2;
      OR:   res = r1 | r2;
      LW, SW: begin
        ad = r1 + c;
        text = $sformatf("%s x%0d, %0d(x%0d)", nm(k), a, $signed(c), b);
        if (ad[1:0] != 0) begin take_trap(k == LW ? 4 : 6, ad, text);  done = 1; end
        else if (ad < 32'h8000_0000 || ad > 32'h8000_fffc) begin take_trap(k == LW ? 5 : 7, ad, text);  done = 1; end
        else if (k == LW) begin res = mem[ad[15:2]];  effect = $sformatf(" from mem[%08h]", ad); end
        else begin mem[ad[15:2]] = x[a[4:0]];  writes = 0;  effect = $sformatf("mem[%08h] = %08h", ad, x[a[4:0]]); end
      end
      BEQ, BNE, BLT, BGE: begin
        r1 = x[a[4:0]];  r2 = x[b[4:0]];  writes = 0;
        case (k)
          BEQ: take = (r1 == r2);
          BNE: take = (r1 != r2);
          BLT: take = ($signed(r1) < $signed(r2));
          default: take = ($signed(r1) >= $signed(r2));
        endcase
        text = $sformatf("%s x%0d, x%0d, 0x%0h", nm(k), a, b, c);
        if (take && c[1:0] != 0) begin take_trap(0, c, text);  done = 1; end
        else if (take) begin next = c;  effect = $sformatf("taken: next is %08h", next); end
        else effect = "not taken";
      end
      JAL, JALR: begin
        res = pc + 4;
        if (k == JAL) begin next = c;  text = $sformatf("jal x%0d, 0x%0h", a, c); end
        else begin next = (r1 + c) & ~32'd1;  text = $sformatf("jalr x%0d, %0d(x%0d)", a, $signed(c), b); end
        if (next[1:0] != 0) begin take_trap(0, next, text);  done = 1; end
        else if (k == JAL && next == pc) begin
          $display("%s%08h | %s | the program stops here", lv, pc, text);
          stopped = 1;  done = 1;
        end
      end
      CSRR, CSRW, CSRS, CSRC: begin
        word = {c[11:0], (k == CSRR) ? 5'd0 : a[4:0], (k == CSRW) ? 3'd1 : (k == CSRC) ? 3'd3 : 3'd2, (k == CSRR) ? a[4:0] : 5'd0, 7'h73};
        if (k == CSRR) text = $sformatf("csrr x%0d, %s", a, cn(c[11:0]));
        else text = $sformatf("%s %s, x%0d", nm(k), cn(c[11:0]), a);
        // Illegal: the CSR does not exist, or it is read-only and would be written, or the level is too low.
        if (!csr_exists(c[11:0]) || (k != CSRR && c[11:10] == 2'b11) || level < c[9:8]) begin take_trap(2, word, text);  done = 1; end
        else if (k == CSRR) res = csr_value(c[11:0]);
        else begin
          writes = 0;
          case (k)
            CSRW:    csr_store(c[11:0], x[a[4:0]]);
            CSRS:    csr_store(c[11:0], csr_value(c[11:0]) | x[a[4:0]]);
            default: csr_store(c[11:0], csr_value(c[11:0]) & ~x[a[4:0]]);
          endcase
          effect = $sformatf("%s = %08h", cn(c[11:0]), csr_value(c[11:0]));
        end
      end
      MRET: begin
        text = "mret";
        if (level != 2'd3) begin take_trap(2, 32'h3020_0073, text);  done = 1; end
        else begin
          writes = 0;  next = mepc;
          mstatus[3] = mstatus[7];  mstatus[7] = 1'b1;                     // MIE takes MPIE back
          if (USER) begin level = mstatus[12:11];  mstatus[12:11] = 2'b00; end
          effect = $sformatf("next is %08h, MIE=%0d", next, mstatus[3]);
        end
      end
      ECALL:  begin take_trap((level == 2'd3) ? 11 : 8, 0, "ecall");  done = 1; end
      EBREAK: begin take_trap(3, pc, "ebreak");  done = 1; end
      default: begin take_trap(2, c, $sformatf(".word 0x%08h", c));  done = 1; end
    endcase
    if (k >= ADDI && k <= SRLI) text = $sformatf("%s x%0d, x%0d, %0d", nm(k), a, b, $signed(c));
    if (k >= ADD && k <= OR)    text = $sformatf("%s x%0d, x%0d, x%0d", nm(k), a, b, c[4:0]);
    if (!done) begin
      if (writes) begin
        if (a[4:0] != 0) x[a[4:0]] = res;                    // register 0 is never written
        effect = {$sformatf("x%0d = %08h", a, x[a[4:0]]), effect};
        if (k == JAL || k == JALR) effect = {effect, $sformatf(", next is %08h", next)};
      end
      $display("%s%08h | %s | %s", lv, pc, text, effect);
      pc = next;  steps++;
    end
  endtask
  task automatic power_on();
    for (int n = 0; n < 32; n++) x[n] = 0;
    for (int n = 0; n < 16384; n++) mem[n] = 0;
    mstatus = 32'h0000_1800;  mtvec = 0;  mscratch = 0;  mepc = 0;  mcause = 0;  mtval = 0;   // the values after reset
    level = 2'd3;  pc = reset_pc;  steps = 0;  traps = 0;  stopped = 0;
  endtask
  task automatic run();
    while (!stopped && steps + traps < 40) step();
  endtask
  initial begin
    for (int n = 0; n < 1024; n++) begin kind[n] = WORD; fa[n] = 0; fb[n] = 0; fc[n] = 0; end   // empty memory reads as 0
    the_program();
    reset_pc = 32'h00000100;      // the input reset_vector of the core
    hart_value = 32'd1;             // the input hart_id of the core
    power_on();
    run();
    if (!stopped) $display("still running after %0d instructions and %0d traps", steps, traps);
    $display("result word=%08h version word=%08h instructions=%0d traps=%0d", mem[0], mem[1], steps, traps);
  end
endmodule
ConceptWhat is an IP block?
ConceptWhich groups of ports must a chip connect for ap_rv32 to run a program?
ConceptWhat does the reset vector decide?
ConceptHow does software read the hart number?
ConceptWhat does one record of the retire trace port describe?

Common Mistakes

Wrong Copy the reset vector from the last chip.

The boot memory may be at another address. The core then fetches where no program is.

Fix Tie reset_vector from the memory map of this chip.

Wrong Tie hart_id to 0 on every core of a chip.

All harts then believe they are the first.

Fix Give each core its own number.

Wrong Use a trace output to drive logic of the chip.

The port is for observation. A chip built without it must behave the same.

Fix Connect it to a test bench or a trace unit only.

Remember

  • ap_rv32 has 76 ports in eight classes. Five classes must be connected for it to run.
  • reset_vector gives the first address, and hart_id the number that mhartid reads. Both are tied by the chip.
  • mimpid and misa tell software what the core is.
  • The retire trace port reports each instruction with everything it changed. No chip logic may depend on it.

Practice

Report a problem with this page