Performance: The Critical Path and the Cycle Time
The single-cycle core runs one instruction in each clock cycle. So its speed is the length of that cycle. This page finds what sets the length, in the lab and in the real core.
You Will Be Able To
- Add up the delays on the path of each kind of instruction.
- Name the critical path of a single-cycle datapath, and derive the shortest clock period from it.
- Read a logic-depth report of a real core, and say where its longest path starts and ends.
- Lesson
- S2.6
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
So far, logic was instant: a value changed, and its result was there. Real gates take time. A load must pass the instruction memory, the decoder, the ALU, the data memory and the load unit. Only then may the next clock edge come, and every other instruction waits as long.
Before You Start
You need the full datapath of S2.21, and which blocks each kind of instruction uses.
You need to know that a gate takes a little time to change its output.
| Question | |
|---|---|
| 1 | What is the critical path of a circuit? |
| 2 | Which kind of instruction has the longest path in the datapath of the lab? |
| 3 | How does the critical path set the clock period? |
| 4 | What happens when the clock is faster than the critical path allows? |
| 5 | When does a faster block make the core faster? |
The Idea: The Slowest Instruction Sets the Clock
Every block of logic has a propagation delay. That is the time from a change at its inputs to the right value at its outputs. This page calls it the delay, and counts it in levels of logic. A level is one simple gate that a signal passes on its way.
After a clock edge, the new PC starts a wave of changes. It runs through the instruction memory, the decoder, the ALU and on. A chain of blocks from one clocked element to the next is a timing path. This page calls it a path. Its delay is the sum of the delays on it.
At the next clock edge, the register file and the PC take whatever value is on their inputs. If the wave has not arrived, they take an old value. Nothing reports that. The program just goes wrong.
So the clock period must be longer than the longest path. The shortest period that works is the cycle time. That longest path has a name: the critical path.
Here is the cost of a single-cycle core. It has one clock period for every instruction. An add needs a short path, and a load a long one. The period must fit the load. So the add waits too.
New Words
| Word | Meaning |
|---|---|
| Propagation delay | The time from a change at the inputs of a block to the right value at its outputs. Called the delay for short. |
| Level of logic | One simple gate on the way of a signal. Used here as the unit of delay. |
| Logic depth | The largest number of levels between the inputs and the outputs of a block. |
| Timing path | A chain of logic from the output of one clocked element to the input of the next. This page calls it a path. |
| Critical path | The path with the largest delay in the whole circuit. |
| Cycle time | The shortest clock period that a core can run with. A clock period is the time from one rising clock edge to the next. |
| Clock frequency | The number of clock periods in one second. A longer period means a lower frequency. |
| Yosys | The synthesis tool of this site. It turns a description of logic into gates, and can report the logic depth. |
One Picture: Four Paths Through One Cycle
- Clock / reset
- Memory
- Control / FSM
- Processor
- An arithmetic instruction: 96 levels
The word arrives after 20 levels. The decoder needs 11 more, the register read 10: count 11. The ALU adds 65. In all: 20 plus 11 plus 65.
- A load: 123 levels
A load goes the same way to the ALU: 96 levels. Then come the data memory, 20, and the load unit, 7. No other instruction has so far to go.
- A store: 96 levels
The address and the value must be at the memory when the edge comes. The address takes 96 levels. The write itself happens at the edge.
- A branch: 88 levels
Two things run side by side. The compare is done after 20 plus 10 plus 19, which is 49. The target needs 20 plus 11 plus 57, which is 88. The slower one counts.
Read the diagram as text
- Clock edge: the PC changes: time 0 of every cycle
- Instruction memory: 20 levels
- Decoder, beside the register read: 11 levels and 10 levels: the slower counts
- ALU: 65 levels
- Branch compare and target adder: 19 levels and 57 levels, side by side
- Data memory: 20 levels
- Load unit: 7 levels
- Next clock edge: the register file and the PC take their values
- Clock edge: the PC changes → Instruction memory: the address
- Instruction memory → Decoder, beside the register read: the word
- Decoder, beside the register read → ALU: operands and control
- ALU → Next clock edge: the result, for rd
- ALU → Data memory: the address
- Data memory → Load unit: the word
- Load unit → Next clock edge: the loaded value, for rd
- Decoder, beside the register read → Branch compare and target adder: operands and the immediate
- Branch compare and target adder → Next clock edge: the next PC
The delays are those of the lab. The multiplexers are counted as nothing.
| Block | Levels | Where the number comes from |
|---|---|---|
| Instruction memory | 20 | a choice of this page |
| Decoder | 11 | measured on the decoder of ap_rv32 |
| Register file, read | 10 | measured on its register file |
| ALU | 65 | measured on its ALU |
| Branch compare | 19 | measured on its branch compare |
| A 32-bit adder | 57 | measured: for the target, and for the PC plus 4 |
| Data memory | 20 | a choice of this page |
| Load unit | 7 | measured on its load unit |
Measured means: the synthesis tool Yosys built the block from its real source and counted its levels.
The Smallest Example, Worked by Hand
Add up the path of lw x5, 0(x10). Blocks in a row add. Blocks side by side do not: the slower one counts.
| Block | Its levels | Levels since the clock edge |
|---|---|---|
| Instruction memory | 20 | 20 |
| Decoder, beside the register read | 11, beside 10 | 31 |
| ALU | 65 | 96 |
| Data memory | 20 | 116 |
| Load unit | 7 | 123 |
The loaded value is ready 123 levels after the clock edge.
An add leaves this path after the ALU. Its result is ready after 96 levels.
| Kind | Path | Levels |
|---|---|---|
| Arithmetic | 20 + 11 + 65 | 96 |
| Load | 20 + 11 + 65 + 20 + 7 | 123 |
| Store | 20 + 11 + 65 | 96 |
| Branch, jal | 20 + 11 + 57 | 88 |
The critical path is that of the load. The clock period must be longer than 123 levels.
The Rules
Rule 1: A path starts at the output of a clocked element and ends at the input of one.
Here the clocked elements are the PC, the register file and the memories.
Rule 2: Delays in a row add. Of delays side by side, the largest counts.
The decoder and the register read work side by side.
Rule 3: The critical path is the longest path of the whole circuit.
It does not matter how seldom a program uses it.
Rule 4: The clock period must be longer than the critical path.
In the lab, one level longer is enough.
Rule 5: In a single-cycle core, every instruction takes one full period.
A short instruction finishes early and waits.
Rule 6: A faster block shortens the period only if it lies on the critical path.
And then only until another path becomes the longest.
Sources for These Rules
The idea that the slowest instruction sets the clock of a single-cycle core is as textbooks commonly describe it. The plan of this track points to Patterson and Hennessy, sections 4.5 and 4.14. It also points to Harris and Harris, sections 7.2 and 7.4. Those places are not checked. The depths on this page are measured: the pinned Yosys of this site, with its command ltp, on the RTL of ap_rv32. A depth counts levels of simple gates. It is not a time. This run did not ask the tool for speed. A tool that is asked builds shallower adders: that remark is common practice, not measured here. The delays of the two memories in the lab, 20 levels each, are a choice of this page.
Now Run It
The lab at the end of this page is the datapath of S2.21 with a delay behind each block. It runs the three test programs of S2.5 at seven clock periods, and says which still give the right result.
period 140 | arithmetic pass | memory pass | loop pass
period 124 | arithmetic pass | memory pass | loop pass
period 123 | arithmetic pass | memory FAIL | loop pass
period 110 | arithmetic pass | memory FAIL | loop pass
period 97 | arithmetic pass | memory FAIL | loop pass
period 96 | arithmetic FAIL | memory FAIL | loop FAIL
period 80 | arithmetic FAIL | memory FAIL | loop FAILRule 4 at work: at a period of 124, everything passes. At 123, the memory program fails. Its lw needs 123 levels, and the period must be longer.
Rule 3 at work: the memory program has one load among seven instructions. That one load sets the limit.
Now read the columns of the other two programs. They hold no load. They still pass at 97, and fail at 96: the path of an add.
So a core that ran only those two programs could have a period of 97. With loads, it needs 124. Rule 5: every add in every program then takes 124 as well.
Nothing in the lab reports a clock that is too fast. A register simply takes a value that is not ready. The only sign is a wrong result.
The Report of a Real Tool
The lab is a model with round numbers. Here is a measurement. The synthesis tool Yosys built each block of ap_rv32 from simple gates, and then the whole single-cycle core. For each it reports the cells and the logic depth.
ap_rv32_regfile | 4520 cells | depth 10
ap_rv32_decode | 130 cells | depth 11
ap_rv32_imm_gen | 79 cells | depth 5
ap_rv32_alu | 1243 cells | depth 65
ap_rv32_branch | 178 cells | depth 19
ap_rv32_load_unit | 129 cells | depth 7
adder32 | 172 cells | depth 57
ap_rv32_sc | 9910 cells | depth 122The rows above the adder are blocks of the core, and the lab takes its delays from five of them. The seventh row is a plain 32-bit adder. The last row is the whole core: 9,910 cells, and a longest path of 122 levels.
The tool also lists that longest path. These are the named wires on it, with the level at which each is reached:
level 0 | imem_rdata[1]
level 4 | alu_a_select[1]
level 11 | imm[1]
level 12 | alu_operand_b[1]
level 61 | dmem_addr[0]
level 69 | trace_trap
level 70 | retire
level 122 | u_csr.minstret_next[63]
flip-flop | u_csr.minstret_q[63]Read it from the top. The path starts at the instruction word. It runs through the decoder and the operand select into the ALU. At level 61, the ALU result leaves as the address of the data memory.
Then comes a part that no picture of this module shows. The core checks whether this instruction must trap, for example for a misaligned address. That decides the wire named retire: the instruction completes.
And retire feeds a counter of 64 bits, which counts the completed instructions. Adding 1 to a 64-bit number takes the last 52 levels. So the longest path of the real core ends in a counter, not in the register file.
Two things are missing from this report. The memories are outside the core, so their delay is not counted. And a level is not a time. The timing track of this site turns levels into nanoseconds.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| One clock period for every instruction | A period that changes with the instruction | One simple clock. Every instruction is as slow as the slowest. |
| The whole instruction in one cycle | Several short cycles for each instruction | No state between the steps, and no control for it. A long critical path: the next lesson compares. |
| The decoder beside the register read | Decode first, then read | The slower of the two counts, not their sum. The fields rs1 and rs2 must be at fixed places. |
| A target adder beside the ALU | The ALU works out the target | A branch needs 88 levels and not more than a load. A second adder. |
| Count levels first | Go straight to times | Levels need no cell library and no layout. They ignore wires and the sizes of gates. |
The Trap: The Faster Block That Changed Nothing
A team wants a faster core. One engineer rebuilds the branch path. The compare drops from 19 levels to 5, and both adders from 57 to 20. The work is good. Here is the same run again, with the new blocks.
period 140 | arithmetic pass | memory pass | loop pass
period 124 | arithmetic pass | memory pass | loop pass
period 123 | arithmetic pass | memory FAIL | loop pass
period 110 | arithmetic pass | memory FAIL | loop pass
period 97 | arithmetic pass | memory FAIL | loop pass
period 96 | arithmetic FAIL | memory FAIL | loop FAIL
period 80 | arithmetic FAIL | memory FAIL | loop FAILNot one line has changed. The shortest period is still 124, because the path of a load is still 123. The branch path was never the critical path. Rule 6.
A second engineer halves the ALU instead: 33 levels in place of 65.
period 124 | arithmetic pass | memory pass | loop pass
period 96 | arithmetic pass | memory pass | loop pass
period 92 | arithmetic pass | memory pass | loop pass
period 91 | arithmetic pass | memory FAIL | loop pass
period 85 | arithmetic pass | memory FAIL | loop FAIL
period 84 | arithmetic pass | memory FAIL | loop FAILNow the memory program passes down to 92. The ALU lies on the critical path, so the whole gain of 32 levels arrives.
Look at the loop program in this second run. It fails at 85, although its arithmetic now needs only 64 levels. Its branch needs 88. With the ALU out of the way, the target adder is the next limit.
So the order of work matters. Find the critical path first. Shorten it. Then find the new one.
At Scale
This section looks ahead. A real core has millions of paths, and a tool checks every one.
Times, not levels
A tool adds the real delays of gates and wires, and checks every path against the clock. The timing track of this site covers it.
Paths that no picture shows
Counters, trap checks and debug logic often hold the longest path. The report of the real core above is an example.
Cutting the path
Put a register in the middle of a long path, and each half needs only half the period. That is a pipeline: S2.7.
A memory takes a whole cycle
A real SRAM answers at the next clock edge. Then a load cannot fit into one cycle at all: S2.16 and S3.
Try It
Run the lab. Find the shortest period at which each program passes.
Set T_DMEM to 40. Work out the new critical path by hand first. Then change the list of periods and check.
Set T_ALU to 20. Which path is the critical one now? Work it out, then check with the lab.
period 140 | arithmetic pass | memory pass | loop pass period 124 | arithmetic pass | memory pass | loop pass period 123 | arithmetic pass | memory FAIL | loop pass period 110 | arithmetic pass | memory FAIL | loop pass period 97 | arithmetic pass | memory FAIL | loop pass period 96 | arithmetic FAIL | memory FAIL | loop FAIL period 80 | arithmetic FAIL | memory FAIL | loop FAILRead included file: ap_rv32_pkg.sv
// ap_rv32_pkg: the named constants of the ap_rv32 core.
// Opcodes, function fields, control-signal values, trap causes and CSR addresses.
// Used by both builds (ap_rv32_sc and ap_rv32). Taught in S2.2 and S2.5.
// Blueprint: docs/soc/ap_rv32-blueprint.md sections 4 (instructions), 5 (CSRs) and 7 (traps).
package ap_rv32_pkg;
// ---- Opcodes: instruction bits 6:0
localparam logic [6:0] OPCODE_LUI = 7'b0110111;
localparam logic [6:0] OPCODE_AUIPC = 7'b0010111;
localparam logic [6:0] OPCODE_JAL = 7'b1101111;
localparam logic [6:0] OPCODE_JALR = 7'b1100111;
localparam logic [6:0] OPCODE_BRANCH = 7'b1100011;
localparam logic [6:0] OPCODE_LOAD = 7'b0000011;
localparam logic [6:0] OPCODE_STORE = 7'b0100011;
localparam logic [6:0] OPCODE_OP_IMM = 7'b0010011;
localparam logic [6:0] OPCODE_OP = 7'b0110011;
localparam logic [6:0] OPCODE_MISC_MEM = 7'b0001111;
localparam logic [6:0] OPCODE_SYSTEM = 7'b1110011;
// ---- funct3 (instruction bits 14:12) of the branches
localparam logic [2:0] FUNCT3_BEQ = 3'b000;
localparam logic [2:0] FUNCT3_BNE = 3'b001;
localparam logic [2:0] FUNCT3_BLT = 3'b100;
localparam logic [2:0] FUNCT3_BGE = 3'b101;
localparam logic [2:0] FUNCT3_BLTU = 3'b110;
localparam logic [2:0] FUNCT3_BGEU = 3'b111;
// ---- funct3 of the arithmetic instructions (OP and OP_IMM) that the decoder must tell apart.
// All eight values are the low three bits of alu_operation, below.
localparam logic [2:0] FUNCT3_ADD_SUB = 3'b000;
localparam logic [2:0] FUNCT3_SLL = 3'b001;
localparam logic [2:0] FUNCT3_SRL_SRA = 3'b101;
// ---- funct3 of JALR and FENCE. Loads and stores: bits 1:0 are mem_size, bit 2 is unsigned.
// SYSTEM: 000 is ecall, ebreak, mret, wfi; else bits 1:0 are csr_operation, bit 2 is immediate.
localparam logic [2:0] FUNCT3_JALR = 3'b000;
localparam logic [2:0] FUNCT3_FENCE = 3'b000;
// ---- funct7 (instruction bits 31:25)
localparam logic [6:0] FUNCT7_BASE = 7'b0000000;
localparam logic [6:0] FUNCT7_ALT = 7'b0100000; // sub, sra, srai
// ---- Instructions matched on all 32 bits
localparam logic [31:0] INSTR_ECALL = 32'h0000_0073;
localparam logic [31:0] INSTR_EBREAK = 32'h0010_0073;
localparam logic [31:0] INSTR_MRET = 32'h3020_0073;
localparam logic [31:0] INSTR_WFI = 32'h1050_0073;
// ---- imm_select: which immediate format the instruction carries
localparam logic [2:0] IMM_I = 3'd0;
localparam logic [2:0] IMM_S = 3'd1;
localparam logic [2:0] IMM_B = 3'd2;
localparam logic [2:0] IMM_U = 3'd3;
localparam logic [2:0] IMM_J = 3'd4;
// ---- alu_operation: bit 3 is instruction bit 30, bits 2:0 are funct3
localparam logic [3:0] ALU_ADD = 4'b0000;
localparam logic [3:0] ALU_SUB = 4'b1000;
localparam logic [3:0] ALU_SLL = 4'b0001;
localparam logic [3:0] ALU_SLT = 4'b0010;
localparam logic [3:0] ALU_SLTU = 4'b0011;
localparam logic [3:0] ALU_XOR = 4'b0100;
localparam logic [3:0] ALU_SRL = 4'b0101;
localparam logic [3:0] ALU_SRA = 4'b1101;
localparam logic [3:0] ALU_OR = 4'b0110;
localparam logic [3:0] ALU_AND = 4'b0111;
// ---- alu_a_select and alu_b_select: the two ALU operands
localparam logic [1:0] ALU_A_RS1 = 2'd0;
localparam logic [1:0] ALU_A_PC = 2'd1;
localparam logic [1:0] ALU_A_ZERO = 2'd2;
localparam logic ALU_B_RS2 = 1'b0;
localparam logic ALU_B_IMM = 1'b1;
// ---- result_select: the value written to rd
localparam logic [1:0] RESULT_ALU = 2'd0;
localparam logic [1:0] RESULT_MEM = 2'd1;
localparam logic [1:0] RESULT_PC_PLUS_4 = 2'd2;
localparam logic [1:0] RESULT_CSR = 2'd3;
// ---- mem_size: bytes moved by a load or store (as AHB hsize)
localparam logic [1:0] SIZE_BYTE = 2'd0;
localparam logic [1:0] SIZE_HALF = 2'd1;
localparam logic [1:0] SIZE_WORD = 2'd2;
// ---- csr_operation: what a CSR instruction does with its operand
localparam logic [1:0] CSR_OPERATION_NONE = 2'd0; // not a CSR instruction
localparam logic [1:0] CSR_OPERATION_WRITE = 2'd1;
localparam logic [1:0] CSR_OPERATION_SET = 2'd2;
localparam logic [1:0] CSR_OPERATION_CLEAR = 2'd3;
// ---- Trap causes: the value written to mcause (bit 31 marks an interrupt)
localparam logic [31:0] CAUSE_INSTR_ADDR_MISALIGNED = 32'd0;
localparam logic [31:0] CAUSE_INSTR_ACCESS_FAULT = 32'd1;
localparam logic [31:0] CAUSE_ILLEGAL_INSTRUCTION = 32'd2;
localparam logic [31:0] CAUSE_BREAKPOINT = 32'd3;
localparam logic [31:0] CAUSE_LOAD_ADDR_MISALIGNED = 32'd4;
localparam logic [31:0] CAUSE_LOAD_ACCESS_FAULT = 32'd5;
localparam logic [31:0] CAUSE_STORE_ADDR_MISALIGNED = 32'd6;
localparam logic [31:0] CAUSE_STORE_ACCESS_FAULT = 32'd7;
localparam logic [31:0] CAUSE_ECALL_FROM_M = 32'd11;
localparam logic [31:0] CAUSE_IRQ_SOFTWARE = 32'h8000_0003;
localparam logic [31:0] CAUSE_IRQ_TIMER = 32'h8000_0007;
localparam logic [31:0] CAUSE_IRQ_EXTERNAL = 32'h8000_000b;
// ---- Bit positions in mstatus, and in mie and mip
localparam int MSTATUS_MIE_BIT = 3;
localparam int MSTATUS_MPIE_BIT = 7;
localparam int IRQ_SOFTWARE_BIT = 3;
localparam int IRQ_TIMER_BIT = 7;
localparam int IRQ_EXTERNAL_BIT = 11;
// ---- CSR addresses (blueprint 5.1)
localparam logic [11:0] CSR_MSTATUS = 12'h300;
localparam logic [11:0] CSR_MISA = 12'h301;
localparam logic [11:0] CSR_MIE = 12'h304;
localparam logic [11:0] CSR_MTVEC = 12'h305;
localparam logic [11:0] CSR_MSTATUSH = 12'h310;
localparam logic [11:0] CSR_MSCRATCH = 12'h340;
localparam logic [11:0] CSR_MEPC = 12'h341;
localparam logic [11:0] CSR_MCAUSE = 12'h342;
localparam logic [11:0] CSR_MTVAL = 12'h343;
localparam logic [11:0] CSR_MIP = 12'h344;
localparam logic [11:0] CSR_MCYCLE = 12'hb00;
localparam logic [11:0] CSR_MINSTRET = 12'hb02;
localparam logic [11:0] CSR_MCYCLEH = 12'hb80;
localparam logic [11:0] CSR_MINSTRETH = 12'hb82;
localparam logic [11:0] CSR_MVENDORID = 12'hf11;
localparam logic [11:0] CSR_MARCHID = 12'hf12;
localparam logic [11:0] CSR_MIMPID = 12'hf13;
localparam logic [11:0] CSR_MHARTID = 12'hf14;
localparam logic [11:0] CSR_MCONFIGPTR = 12'hf15;
// Performance monitors 3 to 31 exist and read 0: first and last address of each range
localparam logic [11:0] CSR_MHPMEVENT3 = 12'h323;
localparam logic [11:0] CSR_MHPMEVENT31 = 12'h33f;
localparam logic [11:0] CSR_MHPMCOUNTER3 = 12'hb03;
localparam logic [11:0] CSR_MHPMCOUNTER31 = 12'hb1f;
localparam logic [11:0] CSR_MHPMCOUNTER3H = 12'hb83;
localparam logic [11:0] CSR_MHPMCOUNTER31H = 12'hb9f;
// Address bits 11:10 of a read-only CSR
localparam logic [1:0] CSR_READ_ONLY = 2'b11;
// ---- CSR values and write masks (a 1 marks a bit that software can change)
localparam logic [31:0] MSTATUS_RESET = 32'h0000_1800; // MPP = 11: machine mode only
localparam logic [31:0] MSTATUS_WRITE_MASK = 32'h0000_0088; // MIE and MPIE
localparam logic [31:0] MISA_VALUE = 32'h4000_0100; // 32 bits, extension I
localparam logic [31:0] MIMPID_VALUE = 32'h0001_0000; // ap_rv32 version 1.0
localparam logic [31:0] MIE_WRITE_MASK = 32'h0000_0888; // MSIE, MTIE and MEIE
localparam logic [31:0] MTVEC_WRITE_MASK = 32'hffff_fffd; // BASE and MODE bit 0
localparam logic [31:0] MEPC_WRITE_MASK = 32'hffff_fffc; // instructions are 4-byte aligned
localparam logic [31:0] ALL_BITS = 32'hffff_ffff;
// ---- Data bits that a byte and a half cover, from bit 0
localparam logic [31:0] BYTE_BITS = 32'h0000_00ff;
localparam logic [31:0] HALF_BITS = 32'h0000_ffff;
// ---- Other
localparam logic [1:0] PRIV_MACHINE = 2'b11; // the only privilege level in v1.0
localparam int REGISTER_COUNT = 32;
endpackage
Read included file: ap_rv32_regfile.sv
// ap_rv32_regfile: the register file.
// 31 registers, x1 to x31, and x0 which always reads 0. Two read ports, one write port.
// A read is combinational; a write happens at the clock edge. All registers reset to 0.
// Used by both builds. Taught in S2.4. Blueprint: sections 2.3 and 6.
module ap_rv32_regfile (
input logic clk, // clock
input logic rst_n, // reset, active low
input logic [4:0] rs1_addr, // first register to read
output logic [31:0] rs1_data, // its value
input logic [4:0] rs2_addr, // second register to read
output logic [31:0] rs2_data, // its value
input logic write_enable, // write rd at this clock edge
input logic [4:0] rd_addr, // register to write; a write to x0 is discarded
input logic [31:0] rd_data // value to write
);
import ap_rv32_pkg::*;
logic [31:0] registers_q [1:REGISTER_COUNT-1]; // x1 to x31; x0 has no storage
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
for (int i = 1; i < REGISTER_COUNT; i++) registers_q[i] <= '0;
end else if (write_enable && rd_addr != '0) begin
registers_q[rd_addr] <= rd_data;
end
end
assign rs1_data = (rs1_addr == '0) ? '0 : registers_q[rs1_addr];
assign rs2_data = (rs2_addr == '0) ? '0 : registers_q[rs2_addr];
endmodule
Read included file: ap_rv32_alu.sv
// ap_rv32_alu: the arithmetic and logic unit.
// Add, subtract, shifts, compares and bitwise logic on two 32-bit operands.
// Used by both builds. Taught in S2.4. Blueprint: section 2.3.
module ap_rv32_alu (
input logic [3:0] alu_operation, // operation: one of the ALU_* constants
input logic [31:0] operand_a, // first operand
input logic [31:0] operand_b, // second operand; shifts use only its bits 4:0
output logic [31:0] result // result of the operation
);
import ap_rv32_pkg::*;
logic [4:0] shift_amount; // number of bit positions to shift by
assign shift_amount = operand_b[4:0];
always_comb begin
result = '0;
unique case (alu_operation)
ALU_ADD: result = operand_a + operand_b;
ALU_SUB: result = operand_a - operand_b;
ALU_SLL: result = operand_a << shift_amount;
ALU_SLT: result = {31'b0, $signed(operand_a) < $signed(operand_b)};
ALU_SLTU: result = {31'b0, operand_a < operand_b};
ALU_XOR: result = operand_a ^ operand_b;
ALU_SRL: result = operand_a >> shift_amount;
ALU_SRA: result = $signed(operand_a) >>> shift_amount;
ALU_OR: result = operand_a | operand_b;
ALU_AND: result = operand_a & operand_b;
default: result = '0;
endcase
end
endmodule
Read included file: ap_rv32_branch.sv
// ap_rv32_branch: the branch compare.
// Decides whether the condition of a branch instruction is true.
// Used by both builds. Taught in S2.4 and S2.10. Blueprint: section 2.3.
module ap_rv32_branch (
input logic [2:0] funct3, // funct3 of the branch instruction: which condition
input logic [31:0] rs1_data, // first value compared
input logic [31:0] rs2_data, // second value compared
output logic condition // the condition is true: the branch is taken
);
import ap_rv32_pkg::*;
always_comb begin
condition = 1'b0;
unique case (funct3)
FUNCT3_BEQ: condition = (rs1_data == rs2_data);
FUNCT3_BNE: condition = (rs1_data != rs2_data);
FUNCT3_BLT: condition = ($signed(rs1_data) < $signed(rs2_data));
FUNCT3_BGE: condition = ($signed(rs1_data) >= $signed(rs2_data));
FUNCT3_BLTU: condition = (rs1_data < rs2_data);
FUNCT3_BGEU: condition = (rs1_data >= rs2_data);
default: condition = 1'b0;
endcase
end
endmodule
Read included file: ap_rv32_load_unit.sv
// ap_rv32_load_unit: the load unit.
// Picks the addressed byte or half out of the word that the memory returned, and extends it to
// 32 bits with the sign bit (lb, lh) or with zeros (lbu, lhu).
// Used by both builds. Taught in S2.4. Blueprint: section 2.3; misalignment rule in section 7.1.
module ap_rv32_load_unit (
input logic [31:0] mem_rdata, // word from the memory, every byte on its own lane
input logic [1:0] addr_low, // bits 1:0 of the load address: the lane of the first byte
input logic [1:0] mem_size, // SIZE_BYTE, SIZE_HALF or SIZE_WORD
input logic load_unsigned, // 1: extend with zeros; 0: extend with the sign bit
output logic [31:0] load_data, // value for rd
output logic misaligned // the address is not a multiple of the access size
);
import ap_rv32_pkg::*;
logic [7:0] byte_data; // the addressed byte
logic [15:0] half_data; // the addressed half
logic sign; // bit that fills the upper part of load_data
assign byte_data = mem_rdata[{addr_low, 3'b000} +: 8];
assign half_data = addr_low[1] ? mem_rdata[31:16] : mem_rdata[15:0];
always_comb begin
load_data = mem_rdata;
sign = 1'b0;
misaligned = 1'b0;
unique case (mem_size)
SIZE_BYTE: begin
sign = byte_data[7] & ~load_unsigned;
load_data = {{24{sign}}, byte_data};
end
SIZE_HALF: begin
sign = half_data[15] & ~load_unsigned;
load_data = {{16{sign}}, half_data};
misaligned = addr_low[0];
end
default: begin
misaligned = (addr_low != '0);
end
endcase
end
endmodule
Check Yourself
Common Mistakes
Wrong Add the delays of blocks that work side by side.
They work at the same time. The path is as long as the slower one.
Fix Add blocks in a row. Take the largest of blocks side by side.
Wrong Set the clock by the most common instruction.
One load in a thousand instructions still needs its full path.
Fix Set the clock by the longest path, however seldom it is used.
Wrong Speed up a block without asking where the critical path is.
A faster block off the critical path changes nothing.
Fix Find the critical path first, and work there.
Remember
- The delay of a path is the sum of the blocks in a row. Of blocks side by side, the slower one counts.
- The critical path is the longest path. The clock period must be longer than it.
- In a single-cycle core every instruction takes the full period, so the slowest instruction slows all of them.
- A faster block helps only on the critical path. In the real core, that path ends in a counter that no datapath picture shows.