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.
| Question | |
|---|---|
| 1 | What is an IP block? |
| 2 | Which groups of ports does ap_rv32 have? |
| 3 | What does the reset vector decide? |
| 4 | What is the hart number, and how does software read it? |
| 5 | What 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
| Word | Meaning |
|---|---|
| IP block | A finished design block that is used inside a chip without being changed. |
| Reset vector | The address of the first instruction after reset. On ap_rv32 it is an input, reset_vector. |
| Hart | One processor that executes one stream of instructions. The RISC-V word for it. |
| Hart number | The number of a hart in the chip. An input, hart_id. Software reads it in the CSR mhartid. |
| Tie | To connect an input to a constant value in the chip. |
| Retire | An instruction retires when it is finished and its result is final. |
| Retire trace port | Outputs of the core that describe each retired instruction: its address, its word, and what it changed. |
mimpid | A read-only CSR with the version of the core. ap_rv32 version 1.0 reads 0001_0000. |
misa | A read-only CSR that says which instruction set the core has. |
One Picture: Every Port of the Core
- Clock / reset
- Pads
- Peripheral
- Processor
- Bus
- Power
- Debug
- What every block needs
A clock and a reset. The core has one of each, and every flip-flop uses them.
- 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.
- Its work
Two bus ports. One fetches instructions. The other does the loads and stores. S2.33 looks inside them.
- Events from outside, and rest
Three interrupt inputs come in. Two outputs say that the core sleeps, and that it should be woken.
- 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.
| Class | Ports | Direction | Must the chip connect it? |
|---|---|---|---|
| Clock and reset | 2 | in | yes |
Configuration: reset_vector, hart_id | 2 | in | yes: tied to constants |
| Instruction bus port | 11 | out and in | yes: to the memory with the program |
| Data bus port | 11 | out and in | yes: to memory and devices |
| Interrupt inputs | 3 | in | yes: to their sources, or tied to 0 |
| Sleep outputs | 2 | out | only if the clock is to be gated |
| Retire trace port | 25 | out | no: for a test bench or a trace unit |
| Pipeline view | 20 | out | no: 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
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 100 | lui x8, 0x80000 | x8: the RAM, at 8000_0000 |
| 104 | csrr x5, mhartid | which processor am I? |
| 108 | csrr x6, mimpid | which version of the core? |
| 10c | addi x7, x0, 72 | the letter H |
| 110 | add x7, x7, x5 | H for hart 0, I for hart 1 |
| 114 | sw x7, 0(x8) | the result word |
| 118 | sw x6, 4(x8) | the version word |
| 11c | jal x0, 0x11c | stop |
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.
| Value | Where it comes from | Can software change it? |
|---|---|---|
| The first address, 100 | The input reset_vector, tied by the chip | no |
mhartid, 0 | The input hart_id, tied by the chip | no: the CSR is read-only |
mimpid, 0001_0000 | A constant inside the core | no |
misa, 4000_0100 | A constant inside the core: 32 bits, base instruction set | no: writes are ignored |
The chip gives the core its place. The core tells software what it is.
The Rules
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.
Rule 2: mhartid reads the input hart_id.
A chip with one core ties it to 0. A second core gets 1.
Rule 3: mimpid and misa are constants of the core.
Software reads them to learn the version and the instruction set.
Rule 4: An interrupt input that has no source is tied to 0.
A floating input is an unknown value in the CSR mip.
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.
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.
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=0Each 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.
| Field | Value, in hex | Meaning |
|---|---|---|
trace_order | 5 | the sixth record since reset |
trace_pc | 00000114 | the address of the instruction |
trace_insn | 00742023 | the instruction word: sw x7, 0(x8) |
trace_next_pc | 00000118 | the address executed next |
trace_priv | 3 | the privilege level: machine |
trace_trap | 0 | no trap was taken |
trace_rs1_addr, trace_rs1_rdata | 8, 80000000 | it read x8 |
trace_rs2_addr, trace_rs2_rdata | 7, 00000048 | it read x7 |
trace_rd_addr | 0 | it wrote no register |
trace_mem_addr | 80000000 | the address of the store |
trace_mem_wmask, trace_mem_wdata | f, 00000048 | four bytes written, and their value |
trace_mstatus | 00001800 | mstatus 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The reset vector is an input | A constant inside the core | The same core fits a chip with its boot memory anywhere. One input more to tie right. |
| The hart number is an input | A parameter of the module | Two copies of one built block can differ. 32 wires for one small number. |
| Two bus ports | One port for fetch and data | A fetch and a load in the same cycle. Twice the bus signals. |
| Interrupts as three plain levels | A bus to an interrupt controller | Nothing to decode in the core. Priority among devices is done outside, in the controller of S6. |
| A trace port with every effect of an instruction | Only the PC | A model can be compared with the core record by record. 25 outputs that the chip itself never uses. |
| No parameter changes the behaviour | Options for every chip | One 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.
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=6The 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 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.
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.
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.
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.
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?
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=0Check Yourself
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.