A processor core on its own can assume that memory answers at once. A core in an SoC cannot: when it loads a word from the SRAM or stores a byte to the UART, the access travels over a bus and comes back several cycles later. The core needs a way to wait. This article explains the ready signals that tell a core a memory has not answered yet, the one rule a pipelined core must follow while it waits, and how the cycle count of a program changes when it does. In exercise S01 you add that rule to the 3-stage core from the Computer Architecture tutorial.
13 min read | Beginner to Intermediate
What You'll Learn
- Explain why a core in an SoC must wait for its memories, and how a ready signal tells it to
- Describe what has to stand still in a 3-stage pipeline while a memory is not ready
- Predict a program's cycle count from its instructions, its taken branches and its wait cycles
- Add waiting to your own core and use it in the SoC instead of the provided core
Before You Start
This section needs Verilog: see the Verilog Tutorial. This exercise starts from the 3-stage core you build in the Computer Architecture tutorial, so it assumes you passed its exercise E16; Splitting the Core into Three Stages and A Control Hazard: Flushing After a Taken Branch describe that core. If you did not build it, read this article for what the core does on a bus and go on to The TCM and the Local Decode: the tests of every other exercise provide a core that already waits. Latency and Bandwidth explains why accesses over a bus take several cycles.
Why a Core Has to Wait
In the Computer Architecture tutorial the core was tested with an ideal memory: an instruction or a data word appeared in the same cycle its address did. That is why the core's imem_ready and dmem_ready inputs could be ignored: they were always 1.
In cvsoc that is only true for the TCM. A load from the SRAM goes out through the manager port, crosses the interconnect, reaches the SRAM and comes back: on the reference design it takes 4 cycles. A store to a peripheral also passes the APB bridge: 6 cycles. During those cycles the core has nothing to put into its register and nowhere to send its next store. It has to stop and wait for the answer, and it has to do so without losing or repeating anything.
The memory tells the core when to wait with a ready signal. While dmem_ready is 0, the data access the core is making has not finished; in the cycle it is 1, the access completes: the load data is in dmem_rdata, or the store has been accepted. imem_ready does the same for instruction fetches. In cvsoc instructions always come from the TCM, so imem_ready is tied to 1, but the core treats both the same way, so it also works with a slow instruction memory. Buses and Transactions calls this pattern a handshake; here it is the simplest form of one.
The Rule: Nothing Moves
The 3-stage pipeline has three places where an instruction can be: F, where it is fetched; X, where it is decoded, its registers are read and the ALU and the branch decision work; and W, where it makes its memory access and writes its result. A load or store waits in W. The simplest correct rule, and the one S01 asks for, is that the whole pipeline stands still while either memory is not ready:
- The PC keeps its value, so the instruction being fetched is fetched again.
- The F/X register keeps the instruction it holds, its PC and its valid bit.
- The X/W register keeps the waiting instruction, with its result, destination register, store data and valid bit. Because the memory's address, write enable, store data and byte enables are all driven from this register, they stay the same too. A memory expects exactly that: the request stays unchanged until it is answered.
- No register is written, and
retireis 0: nothing completes while the pipeline waits.
In the first cycle where both ready signals are 1, everything behaves exactly as in the core you already have: the load data arrives and is written, the store completes, the instruction retires, and all registers move on at the clock edge. Calling the condition hold is natural: hold is 1 when imem_ready is 0 or dmem_ready is 0.
The timing diagram shows a load that waits two cycles. In cycle 2 the load reaches W and asks for its data; dmem_ready is 0, so hold is 1 and at the end of the cycle nothing moves. Cycle 3 looks exactly like cycle 2. In cycle 4 the data arrives, dmem_ready is 1, the load writes its register and retires, and the instructions behind it move on.
What Happens Inside X While the Pipeline Waits
While the pipeline holds, the instruction in X keeps being decoded and computed every cycle, but its results go nowhere, because the X/W register does not load them. Two things are worth checking in your own core.
Forwarding. If the instruction in X needs the register the waiting load will write, the forwarding path in your core selects W's value, which is not valid yet while the load waits. That is harmless: X's result is thrown away in every waiting cycle, and in the cycle the load completes, the forwarded value is the real load data, which X uses and the X/W register captures at that clock edge.
Branches. If the instruction in X is a taken branch, your core raises its redirect signal in every waiting cycle, because the branch is still in X. Nothing special is needed: the PC and the F/X register hold, so the redirect and the flush of the instruction fetched behind the branch happen once, at the clock edge where the pipeline moves on, exactly as without waiting.
Your program counter block from the Computer Architecture tutorial has no enable input: on every clock edge it loads either PC + 4 or the redirect target. You do not need to change it. Think about what to give its redirect and target inputs while the pipeline holds, so that it loads its own value.
Quick Check
Q: A core holds both pipeline registers while waiting but forgets the PC, which keeps loading PC + 4. The core is fetching address P when a load in W waits two cycles. Which instructions does the program lose?
Show Answer
The two at P and P + 4. In the two waiting cycles the PC moves on to P + 4 and P + 8 while the F/X register keeps its old instruction, so nothing fetched there is kept. At the edge where the pipeline moves, the F/X register takes the instruction at P + 8. The S01 tests catch this through the result and the instruction count.
Counting the Cycles
With ideal memories, the 3-stage core needs one cycle per instruction, one extra cycle for every taken branch or jump (the flushed instruction), and two cycles at the start to fill the pipeline:
cycles = instructions + taken branches and jumps + 2
Waiting adds exactly one cycle for every cycle in which a memory is not ready, and nothing else, because a waiting cycle is a cycle in which nothing happens:
cycles = instructions + taken branches and jumps + 2 + wait cycles
The S01 tests check this. Their visible test runs the program hello from the Computer Architecture tutorial on a memory that makes each data access wait 0 to 3 cycles, chosen at random, and makes one fetch in four wait a cycle. On the reference solution it reports:
S01: PASS (hello, 386 cycles, 152 of them waiting)
Without the 152 waiting cycles that leaves 234, and that is exactly the count with ideal memories: hello completes 205 instructions (counting the final store to EXIT) and takes 27 branches and jumps, and 205 + 27 + 2 = 234. If your core does a little work while it waits, or loses a cycle after it, this rule tells you at once: the cycles without the wait cycles no longer match.
The same rule explains the SoC numbers in Build an SoC: What You Will Make. There, hello is a different program with many accesses to the bus, and the core waits in 43.3 percent of its cycles. The core is no slower than before; the memories are.
Where Is This Used?
Every core that sits on a bus has an input like dmem_ready: on AHB it is HREADY, on a core with its own simple memory interface it is often called gnt or rvalid. Stalling the whole pipeline is how small microcontroller cores handle it, because it costs only an enable on each pipeline register. Larger cores keep working while a load is outstanding, letting later independent instructions go ahead; Load-Use Stalls and Control Hazards describes the simpler stall that every pipelined core needs, and the chapters on out-of-order execution describe the rest.
How It Is Tested
The tests run six C programs compiled for the Computer Architecture tutorial (a greeting, an array sum, a memory copy, a bubble sort, a linked-list walk and a CRC) on a 64 KB memory that makes your core wait. Each of the seven tests (the visible one included) picks a maximum wait, from 0 to 6 cycles per data access. Each checks three things:
- the program's result, stored to the EXIT address;
- that
retireis 1 once per instruction and never in a waiting cycle; - that cycles minus wait cycles equals instructions + taken branches and jumps + 2, the count with ideal memories.
One test uses no waits at all: your core must behave exactly as it did in E16.
From S08 on, the tests provide a core. To use your own instead, put this line at the very top of a system exercise's file and paste your core and its blocks into it:
`define CVSOC_OWN_CORE
The tests then leave their core out, and yours runs the SoC.
Common Mistakes
Mistake #1: Holding only the stage that waits
If W waits but F and X move on, the instruction in X is lost: it moves into the X/W register on top of the waiting load. Every register in the pipeline has to hold, and so does the PC.
Mistake #2: Forgetting the PC because its block has no enable
The PC block loads a new value at every clock edge, so it is easy to leave it out of the hold, and then every waiting cycle skips an instruction. It needs no new input: while holding, give it its own value as the target and make it take that target.
Mistake #3: Counting retire while waiting
If retire follows only the valid bit of the X/W register, it stays 1 through every waiting cycle of a load and counts the load several times. Retire, like the register write, needs the hold condition too.
Your Turn in the Lab
Exercise S01: paste your 3-stage core that passed Computer Architecture E16, with all its blocks, and add the hold. Run the visible test first: it prints the cycle count and the number of waiting cycles, so you can check the rule yourself before you submit.
Open exercise S01 in ChipVerify LabSummary
- In an SoC, accesses over the bus take several cycles; the memory says when an access completes with a ready signal,
dmem_readyfor data andimem_readyfor instructions. - While either is 0, nothing in the pipeline moves: the PC, both pipeline registers and their valid bits hold, no register is written and
retireis 0. The memory request stays unchanged because it comes from the X/W register. - A branch in X keeps its redirect while waiting; holding the PC and the F/X valid bit makes the redirect and the flush happen exactly once, when the pipeline moves.
- Each waiting cycle adds exactly one cycle: cycles = instructions + taken branches and jumps + 2 + wait cycles. The visible test's
hellotakes 386 cycles, 152 of them waiting: 234 = 205 + 27 + 2. - With S01 passed, a
`define CVSOC_OWN_COREline lets your own core run the SoC in the system exercises.