目录整理:RTL/上板/仿真分开存放,测试 ROM 镜像归入 testbench/rom
- rtl/:12 个可综合的处理器源文件(顶层 cpu21_riscv_redirect_int_bpb.v,其余为各功能单元)。 - board/:板级顶层 cpu21_board_top.v、引脚约束 cpu21_board_top.xdc、片内 ROM 初始化 cpu21_board_rom.vh 与短延时版本 cpu21_board_rom_fast.vh。 - testbench/:保留三个测试平台;ROM 镜像统一放到 testbench/rom/,测试程序源码与期望值 仍在 testbench/programs/(其中的 .hex 是 RARS 原始输出,与 rom/ 里改过显示方式的版本 内容不同,两者都保留)。 - 同步更新:两个测试平台的 ROM 探测路径改为 testbench/rom/(并在头注释里补充说明: PowerShell 会把 -generic_top/--testplusarg 参数里的 "=" 拆开、Verilog 字符串中的 路径要用正斜杠);tools/hex2vh.py 的用法示例指向 board/cpu21_board_top.v; 说明文档新增“目录结构”一节并同步所有路径引用;.gitignore 补充 .Xil/。 - 清理:删除 *.hex.lst 中间文件与空的 .Xil/ 目录。 - 验证:isa_test 24/24、lui_test 4/4、mmio_test 5/5 三项回归通过;两个基线(5560 / 6003 周期)与整理前一致;板级仿真(默认 ROM 首次写入 LED=0001、快速 ROM 跑马灯循环)正常; Vivado 工程源文件路径同步到 rtl/ 与 board/ 后综合通过(LUT 5276、23 个引脚,与整理前 完全一致),因此已有 bit 文件仍然有效。
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 八表项全相联分支目标缓冲(BPB/BTB)。
|
||||
//
|
||||
// tag = PC[11:2],与 Logisim 版 BPB 中的十位 tag 拆分保持一致。
|
||||
// count 为两位饱和计数器:00/01 表示不跳转,10/11 表示跳转。
|
||||
// age 用于近似 LRU 替换:被选中的表项清零,其余表项自增。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_bpb_8 (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
input wire [31:0] predict_pc,
|
||||
input wire predict_enable,
|
||||
output reg predict_hit,
|
||||
output reg predict_taken,
|
||||
output reg [31:0] predict_target,
|
||||
input wire update_enable,
|
||||
input wire update_controlflow,
|
||||
input wire [31:0] update_pc,
|
||||
input wire [31:0] update_target,
|
||||
input wire update_taken
|
||||
);
|
||||
reg valid [0:7];
|
||||
reg [ 9:0] tag [0:7];
|
||||
reg [31:0] target [0:7];
|
||||
reg [ 1:0] count [0:7];
|
||||
reg [ 2:0] age [0:7];
|
||||
|
||||
reg update_found;
|
||||
reg [ 2:0] update_index;
|
||||
reg replace_found;
|
||||
reg [ 2:0] replace_index;
|
||||
reg [ 2:0] max_age;
|
||||
integer p;
|
||||
integer u;
|
||||
integer k;
|
||||
integer selected_index;
|
||||
|
||||
// 预测命中查找:按 tag 全相联匹配,命中则给出目标地址与方向预测。
|
||||
always @* begin
|
||||
predict_hit = 1'b0;
|
||||
predict_taken = 1'b0;
|
||||
predict_target = 32'b0;
|
||||
for (p = 0; p < 8; p = p + 1) begin
|
||||
if (predict_enable && !predict_hit && valid[p] && (tag[p] == predict_pc[11:2])) begin
|
||||
predict_hit = 1'b1;
|
||||
predict_target = target[p];
|
||||
predict_taken = count[p][1];
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// 更新时先找匹配表项;若无匹配则优先使用无效表项,否则淘汰最旧表项。
|
||||
// 下方的 age 自增实现了类似 LRU 的替换策略。
|
||||
always @* begin
|
||||
update_found = 1'b0;
|
||||
update_index = 3'd0;
|
||||
replace_found = 1'b0;
|
||||
replace_index = 3'd0;
|
||||
max_age = 3'd0;
|
||||
|
||||
for (u = 0; u < 8; u = u + 1) begin
|
||||
if (!update_found && valid[u] && (tag[u] == update_pc[11:2])) begin
|
||||
update_found = 1'b1;
|
||||
update_index = u;
|
||||
end
|
||||
end
|
||||
for (u = 0; u < 8; u = u + 1) begin
|
||||
if (!replace_found && !valid[u]) begin
|
||||
replace_found = 1'b1;
|
||||
replace_index = u;
|
||||
end else if (!replace_found && (age[u] >= max_age)) begin
|
||||
replace_index = u;
|
||||
max_age = age[u];
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// 表项状态更新(含替换与两位饱和计数器更新)。
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
for (k = 0; k < 8; k = k + 1) begin
|
||||
valid[k] <= 1'b0;
|
||||
tag[k] <= 10'b0;
|
||||
target[k] <= 32'b0;
|
||||
count[k] <= 2'b01;
|
||||
age[k] <= 3'b0;
|
||||
end
|
||||
end else if (update_enable && update_controlflow) begin
|
||||
selected_index = update_found ? update_index : replace_index;
|
||||
|
||||
valid[selected_index] <= 1'b1;
|
||||
tag[selected_index] <= update_pc[11:2];
|
||||
target[selected_index] <= update_target;
|
||||
age[selected_index] <= 3'b0;
|
||||
|
||||
if (!update_found) begin
|
||||
// 新表项采用较弱的初始状态,避免立刻产生错误预测。
|
||||
count[selected_index] <= update_taken ? 2'b10 : 2'b01;
|
||||
end else if (update_taken) begin
|
||||
// 实际跳转:计数器加一(饱和到 11)。
|
||||
if (count[selected_index] != 2'b11) count[selected_index] <= count[selected_index] + 2'b01;
|
||||
end else begin
|
||||
// 实际不跳转:计数器减一(饱和到 00)。
|
||||
if (count[selected_index] != 2'b00) count[selected_index] <= count[selected_index] - 2'b01;
|
||||
end
|
||||
|
||||
for (k = 0; k < 8; k = k + 1) begin
|
||||
if (valid[k] && (k != selected_index) && (age[k] != 3'b111)) age[k] <= age[k] + 3'b001;
|
||||
end
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,62 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// MMIO:把外设寄存器映射到数据存储器地址空间。
|
||||
//
|
||||
// 目前实现 LED 数据寄存器(LED_ADDR 默认 0x0000_1000,位于 4 KB 数据 RAM 之外):
|
||||
// * 写:sw / sb 命中该地址(按字对齐,即同一 32 位寄存器内的任意字节偏移
|
||||
// 都算命中)时更新 LED 寄存器,led_valid_o 拉高一拍;sb 依据 wstrb_i 只
|
||||
// 修改对应的字节通道(与 RAM 的字节写语义一致)。
|
||||
// * 读:lw 命中该地址时 rdata_hit_o 拉高,顶层据此选择本模块的 rdata_o
|
||||
// 而不是外部 data_rdata_i,从而支持把 LED 值读回。
|
||||
//
|
||||
// 以后要增加外设(拨码开关、UART 等)时只需在这里扩展地址译码,
|
||||
// 处理器核心与流水线时序不需要改动。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_mmio #(
|
||||
parameter [31:0] LED_ADDR = 32'h0000_1000
|
||||
) (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
// MEM 级的访存请求(来自 EX/MEM 流水寄存器)
|
||||
input wire mem_valid_i,
|
||||
input wire mem_write_i,
|
||||
input wire mem_read_i,
|
||||
input wire [31:0] addr_i,
|
||||
input wire [31:0] wdata_i,
|
||||
input wire [ 3:0] wstrb_i,
|
||||
// MMIO 读回
|
||||
output wire [31:0] rdata_o,
|
||||
output wire rdata_hit_o,
|
||||
// LED 外设
|
||||
output reg [31:0] led_data_o,
|
||||
output reg led_valid_o
|
||||
);
|
||||
// 地址译码按“字”对齐:同一 32 位寄存器内的任意字节偏移都算命中,
|
||||
// 具体修改哪几个字节由 wstrb_i 决定(sw 全字、sb 单字节)。
|
||||
wire led_wr = mem_valid_i && mem_write_i && (addr_i[31:2] == LED_ADDR[31:2]);
|
||||
wire led_rd = mem_valid_i && mem_read_i && (addr_i[31:2] == LED_ADDR[31:2]);
|
||||
|
||||
assign rdata_hit_o = led_rd;
|
||||
assign rdata_o = led_data_o;
|
||||
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
led_data_o <= 32'b0;
|
||||
led_valid_o <= 1'b0;
|
||||
end else begin
|
||||
// led_valid_o 只拉高一拍,作为“本周期有 LED 写入”的指示。
|
||||
led_valid_o <= 1'b0;
|
||||
if (led_wr) begin
|
||||
if (wstrb_i[0]) led_data_o[7:0] <= wdata_i[7:0];
|
||||
if (wstrb_i[1]) led_data_o[15:8] <= wdata_i[15:8];
|
||||
if (wstrb_i[2]) led_data_o[23:16] <= wdata_i[23:16];
|
||||
if (wstrb_i[3]) led_data_o[31:24] <= wdata_i[31:24];
|
||||
led_valid_o <= 1'b1;
|
||||
end
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,71 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// ALU:纯组合运算单元。
|
||||
//
|
||||
// result 为主结果;result2 为第二结果(乘法的无符号高 32 位 / DIVU 的余数),
|
||||
// 由译码器用 wb_sel=3 选取,用于无符号高位乘法 mulhu 与课程约定的 remu。
|
||||
// 除法做了除零保护:b==0 时商为 32'hffff_ffff、余数为被除数。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_alu (
|
||||
input wire [ 3:0] op,
|
||||
input wire [31:0] a,
|
||||
input wire [31:0] b,
|
||||
output reg [31:0] result,
|
||||
output reg [31:0] result2
|
||||
);
|
||||
localparam ALU_SLL = 4'd0;
|
||||
localparam ALU_SRA = 4'd1;
|
||||
localparam ALU_SRL = 4'd2;
|
||||
localparam ALU_MUL = 4'd3;
|
||||
localparam ALU_DIVU = 4'd4;
|
||||
localparam ALU_ADD = 4'd5;
|
||||
localparam ALU_SUB = 4'd6;
|
||||
localparam ALU_AND = 4'd7;
|
||||
localparam ALU_OR = 4'd8;
|
||||
localparam ALU_XOR = 4'd9;
|
||||
localparam ALU_NOR = 4'd10;
|
||||
localparam ALU_SLT = 4'd11;
|
||||
localparam ALU_SLTU = 4'd12;
|
||||
localparam ALU_MULH = 4'd13;
|
||||
localparam ALU_MULHSU = 4'd14;
|
||||
localparam ALU_PASSB = 4'd15; // 直接输出 B 端(供 LUI 使用)
|
||||
reg [63:0] mult_result; // 无符号 32x32 乘积
|
||||
reg signed [63:0] mult_ss; // 有符号 x 有符号
|
||||
reg signed [63:0] mult_su; // 有符号 x 无符号
|
||||
|
||||
always @* begin
|
||||
mult_result = a * b;
|
||||
mult_ss = $signed(a) * $signed(b);
|
||||
mult_su = $signed(a) * $signed({32'b0, b});
|
||||
result2 = 32'b0;
|
||||
case (op)
|
||||
ALU_SLL: result = a << b[4:0];
|
||||
ALU_SRA: result = $signed(a) >>> b[4:0];
|
||||
ALU_SRL: result = a >> b[4:0];
|
||||
ALU_MUL: begin
|
||||
result = mult_result[31:0];
|
||||
result2 = mult_result[63:32];
|
||||
end
|
||||
ALU_DIVU: begin
|
||||
result = (b == 32'b0) ? 32'hffff_ffff : a / b;
|
||||
result2 = (b == 32'b0) ? a : a % b;
|
||||
end
|
||||
ALU_ADD: result = a + b;
|
||||
ALU_SUB: result = a - b;
|
||||
ALU_AND: result = a & b;
|
||||
ALU_OR: result = a | b;
|
||||
ALU_XOR: result = a ^ b;
|
||||
ALU_NOR: result = ~(a | b);
|
||||
ALU_MULH: result = mult_ss[63:32];
|
||||
ALU_MULHSU: result = mult_su[63:32];
|
||||
ALU_SLT: result = ($signed(a) < $signed(b)) ? 32'd1 : 32'd0;
|
||||
ALU_SLTU: result = (a < b) ? 32'd1 : 32'd0;
|
||||
ALU_PASSB: result = b;
|
||||
default: result = 32'b0;
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,68 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// EX 级控制流裁决单元。
|
||||
//
|
||||
// 条件分支的实际方向与目标都在 EX 级解析;JAL/JALR 的目标也只在 EX 级
|
||||
// 得到,因此它们总是需要重定向。条件分支仅在预测方向/目标与实际不符时
|
||||
// 才重定向,这样预测正确时不会产生气泡。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_branch_unit (
|
||||
input wire valid_i,
|
||||
input wire [31:0] pc_i,
|
||||
input wire [31:0] imm_i,
|
||||
input wire [31:0] src1_i,
|
||||
input wire [31:0] src2_i,
|
||||
input wire branch_i,
|
||||
input wire beq_i,
|
||||
input wire bne_i,
|
||||
input wire blt_i,
|
||||
input wire bge_i,
|
||||
input wire bltu_i,
|
||||
input wire bgeu_i,
|
||||
input wire jal_i,
|
||||
input wire jalr_i,
|
||||
// IF 级携带过来的预测信息
|
||||
input wire pred_taken_i,
|
||||
input wire [31:0] pred_target_i,
|
||||
// 裁决结果
|
||||
output wire branch_taken_o,
|
||||
output wire controlflow_o,
|
||||
output wire [31:0] target_o,
|
||||
output wire redirect_valid_o,
|
||||
output wire [31:0] redirect_pc_o
|
||||
);
|
||||
// 比较结果预先算好,避免表达式过长被自动换行拆散。
|
||||
wire lt_signed = $signed(src1_i) < $signed(src2_i);
|
||||
wire ge_signed = $signed(src1_i) >= $signed(src2_i);
|
||||
wire lt_unsigned = src1_i < src2_i;
|
||||
wire ge_unsigned = src1_i >= src2_i;
|
||||
|
||||
// 条件分支裁决:beq 相等、bne 不等、blt/bge 有符号比较、bltu/bgeu 无符号比较。
|
||||
assign branch_taken_o = branch_i &&
|
||||
((beq_i && (src1_i == src2_i)) ||
|
||||
(bne_i && (src1_i != src2_i)) ||
|
||||
(blt_i && lt_signed) ||
|
||||
(bge_i && ge_signed) ||
|
||||
(bltu_i && lt_unsigned) ||
|
||||
(bgeu_i && ge_unsigned));
|
||||
|
||||
// 跳转目标:jalr 为 (rs1+imm) 且最低位清零;jal 为 PC+imm。
|
||||
assign target_o = jalr_i ? ((src1_i + imm_i) & 32'hffff_fffe) : (pc_i + imm_i);
|
||||
|
||||
// EX 级存在需要裁决的控制流指令。
|
||||
assign controlflow_o = valid_i && (branch_i || jal_i || jalr_i);
|
||||
|
||||
// 条件分支的预测方向/目标与实际不符,或 JAL/JALR 必须重定向。
|
||||
assign redirect_valid_o = controlflow_o &&
|
||||
(branch_i ?
|
||||
((pred_taken_i != branch_taken_o) ||
|
||||
(pred_taken_i && branch_taken_o &&
|
||||
(pred_target_i != target_o))) : 1'b1);
|
||||
|
||||
// 预测了跳转但实际不跳转时,回到顺序地址继续取指。
|
||||
assign redirect_pc_o = (branch_i && !branch_taken_o) ? (pc_i + 32'd4) : target_o;
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,281 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// ID 级组合译码器。
|
||||
//
|
||||
// 与原始 Logisim 控制器一致:操作码取标准 RISC-V 的 IR[6:2] 五位字段,
|
||||
// 真值表中以十六进制形式存储该字段。
|
||||
// 所有输出在无匹配时保持"无操作/不写回"的默认值。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_decoder (
|
||||
input wire [31:0] ir_i,
|
||||
|
||||
output reg [ 4:0] rs1_idx_o,
|
||||
output reg [ 4:0] rs2_idx_o,
|
||||
output reg [ 4:0] rd_o,
|
||||
output reg [31:0] imm_o,
|
||||
output reg [ 3:0] alu_op_o,
|
||||
output reg [ 2:0] wb_sel_o,
|
||||
output wire [11:0] csr_addr_o,
|
||||
output reg uses_rs1_o,
|
||||
output reg uses_rs2_o,
|
||||
output reg reg_write_o,
|
||||
output reg mem_to_reg_o,
|
||||
output reg mem_write_o,
|
||||
output reg mem_byte_o,
|
||||
output reg alu_src_o,
|
||||
output reg branch_o,
|
||||
output reg beq_o,
|
||||
output reg bne_o,
|
||||
output reg blt_o,
|
||||
output reg bge_o,
|
||||
output reg bltu_o,
|
||||
output reg bgeu_o,
|
||||
output reg jal_o,
|
||||
output reg jalr_o,
|
||||
output reg ecall_o,
|
||||
output reg uret_o,
|
||||
output reg csr_set_o,
|
||||
output reg csr_clear_o,
|
||||
output reg csr_write_o
|
||||
);
|
||||
// CPU21 自定义操作码(取自所提供的真值表)。
|
||||
localparam OP_LOAD = 5'h00;
|
||||
localparam OP_R = 5'h0c;
|
||||
localparam OP_I = 5'h04;
|
||||
localparam OP_STORE = 5'h08;
|
||||
localparam OP_JALR = 5'h19;
|
||||
localparam OP_BRANCH = 5'h18;
|
||||
localparam OP_JAL = 5'h1b;
|
||||
localparam OP_SYS = 5'h1c;
|
||||
// LUI 为扩展指令(标准 RISC-V opcode 0x37 -> IR[6:2]=0x0d)。
|
||||
localparam OP_LUI = 5'h0d;
|
||||
|
||||
localparam ALU_SLL = 4'd0;
|
||||
localparam ALU_SRA = 4'd1;
|
||||
localparam ALU_SRL = 4'd2;
|
||||
localparam ALU_MUL = 4'd3;
|
||||
localparam ALU_DIVU = 4'd4;
|
||||
localparam ALU_ADD = 4'd5;
|
||||
localparam ALU_SUB = 4'd6;
|
||||
localparam ALU_AND = 4'd7;
|
||||
localparam ALU_OR = 4'd8;
|
||||
localparam ALU_XOR = 4'd9;
|
||||
localparam ALU_SLT = 4'd11;
|
||||
localparam ALU_SLTU = 4'd12;
|
||||
localparam ALU_MULH = 4'd13;
|
||||
localparam ALU_MULHSU = 4'd14;
|
||||
localparam ALU_PASSB = 4'd15; // LUI:直接输出 B 端(立即数)
|
||||
|
||||
wire [4:0] opcode = ir_i[6:2];
|
||||
wire [2:0] funct3 = ir_i[14:12];
|
||||
wire [6:0] funct7 = ir_i[31:25];
|
||||
|
||||
// CSR 地址字段在译码时直接旁路输出(供 EX 级读取 CSR)。
|
||||
assign csr_addr_o = ir_i[31:20];
|
||||
|
||||
// 默认控制信号:全部为"无操作/不写回",再由下面的 case 覆盖。
|
||||
always @* begin
|
||||
rs1_idx_o = ir_i[19:15];
|
||||
rs2_idx_o = ir_i[24:20];
|
||||
rd_o = ir_i[11:7];
|
||||
imm_o = {{20{ir_i[31]}}, ir_i[31:20]};
|
||||
alu_op_o = ALU_ADD;
|
||||
wb_sel_o = 3'd0;
|
||||
uses_rs1_o = 1'b0;
|
||||
uses_rs2_o = 1'b0;
|
||||
reg_write_o = 1'b0;
|
||||
mem_to_reg_o = 1'b0;
|
||||
mem_write_o = 1'b0;
|
||||
mem_byte_o = 1'b0;
|
||||
alu_src_o = 1'b0;
|
||||
branch_o = 1'b0;
|
||||
beq_o = 1'b0;
|
||||
bne_o = 1'b0;
|
||||
blt_o = 1'b0;
|
||||
bge_o = 1'b0;
|
||||
bltu_o = 1'b0;
|
||||
bgeu_o = 1'b0;
|
||||
jal_o = 1'b0;
|
||||
jalr_o = 1'b0;
|
||||
ecall_o = 1'b0;
|
||||
uret_o = 1'b0;
|
||||
csr_set_o = 1'b0;
|
||||
csr_clear_o = 1'b0;
|
||||
csr_write_o = 1'b0;
|
||||
|
||||
case (opcode)
|
||||
// R 型运算:由 funct3/funct7 决定具体操作。
|
||||
OP_R: begin
|
||||
uses_rs1_o = 1'b1;
|
||||
uses_rs2_o = 1'b1;
|
||||
reg_write_o = 1'b1;
|
||||
// M 扩展(funct7=0000001):mul/mulh/mulhsu/mulhu,外加课程约定的 remu
|
||||
// (funct3=111 形式)。mulhu 与 remu 都取 ALU 的 result2(wb_sel=3)。
|
||||
if (funct7 == 7'b0000001) begin
|
||||
case (funct3)
|
||||
3'b000: alu_op_o = ALU_MUL; // mul:无符号乘积低 32 位
|
||||
3'b001: alu_op_o = ALU_MULH; // mulh:有符号乘积高 32 位
|
||||
3'b010: alu_op_o = ALU_MULHSU; // mulhsu:有符号 x 无符号高 32 位
|
||||
3'b011: begin // mulhu:无符号乘积高 32 位
|
||||
alu_op_o = ALU_MUL;
|
||||
wb_sel_o = 3'd3;
|
||||
end
|
||||
3'b111: begin // remu(课程约定):取余数
|
||||
alu_op_o = ALU_DIVU;
|
||||
wb_sel_o = 3'd3;
|
||||
end
|
||||
default: reg_write_o = 1'b0;
|
||||
endcase
|
||||
end else begin
|
||||
// 标准 R 型 funct3 译码;add/sub 与 sra/srl 由 funct7[5] 区分。
|
||||
case (funct3)
|
||||
3'b000: alu_op_o = (funct7[5] ? ALU_SUB : ALU_ADD);
|
||||
3'b001: alu_op_o = ALU_SLL;
|
||||
3'b010: alu_op_o = ALU_SLT;
|
||||
3'b011: alu_op_o = ALU_SLTU;
|
||||
3'b100: alu_op_o = ALU_XOR;
|
||||
3'b101: alu_op_o = (funct7[5] ? ALU_SRA : ALU_SRL);
|
||||
3'b110: alu_op_o = ALU_OR;
|
||||
3'b111: alu_op_o = ALU_AND;
|
||||
default: reg_write_o = 1'b0;
|
||||
endcase
|
||||
end
|
||||
end
|
||||
|
||||
// I 型运算(addi/slli/slti/sltiu/xori/srai/srli/ori/andi)。
|
||||
OP_I: begin
|
||||
uses_rs1_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
reg_write_o = 1'b1;
|
||||
case (funct3)
|
||||
3'b000: alu_op_o = ALU_ADD; // addi:立即数加
|
||||
3'b001: alu_op_o = ALU_SLL; // slli:立即数逻辑左移
|
||||
3'b010: alu_op_o = ALU_SLT; // slti:有符号小于置 1
|
||||
3'b011: alu_op_o = ALU_SLTU; // sltiu:无符号小于置 1(立即数符号扩展)
|
||||
3'b100: alu_op_o = ALU_XOR; // xori:立即数异或
|
||||
3'b101:
|
||||
alu_op_o = (funct7[5] ? ALU_SRA : ALU_SRL); // srai/srli:立即数算术/逻辑右移
|
||||
3'b110: alu_op_o = ALU_OR; // ori:立即数或
|
||||
3'b111: alu_op_o = ALU_AND; // andi:立即数与
|
||||
default: reg_write_o = 1'b0;
|
||||
endcase
|
||||
end
|
||||
|
||||
// 加载高位立即数 lui:rd = imm[31:12] << 12。
|
||||
// 不读寄存器;用 ALU_SRC 选立即数,ALU 直通 B 端输出。
|
||||
OP_LUI: begin
|
||||
reg_write_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
alu_op_o = ALU_PASSB;
|
||||
imm_o = {ir_i[31:12], 12'b0};
|
||||
wb_sel_o = 3'd0;
|
||||
end
|
||||
|
||||
// 加载指令,当前仅支持 lw(funct3=010)。
|
||||
OP_LOAD: begin
|
||||
if (funct3 == 3'b010) begin
|
||||
uses_rs1_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
alu_op_o = ALU_ADD;
|
||||
mem_to_reg_o = 1'b1;
|
||||
reg_write_o = 1'b1;
|
||||
wb_sel_o = 3'd1;
|
||||
end
|
||||
end
|
||||
|
||||
// 存储指令:sw(funct3=010)与 sb(funct3=000)。
|
||||
OP_STORE: begin
|
||||
if ((funct3 == 3'b010) || (funct3 == 3'b000)) begin
|
||||
uses_rs1_o = 1'b1;
|
||||
uses_rs2_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
alu_op_o = ALU_ADD;
|
||||
mem_write_o = 1'b1;
|
||||
mem_byte_o = (funct3 == 3'b000); // sb:字节存储
|
||||
imm_o = {{20{ir_i[31]}}, ir_i[31:25], ir_i[11:7]};
|
||||
end
|
||||
end
|
||||
|
||||
// 条件分支:beq(000)/bne(001)/blt(100)/bge(101)/bltu(110)/bgeu(111)。
|
||||
OP_BRANCH: begin
|
||||
if ((funct3 == 3'b000) || (funct3 == 3'b001) || (funct3 == 3'b100) ||
|
||||
(funct3 == 3'b101) || (funct3 == 3'b110) || (funct3 == 3'b111)) begin
|
||||
uses_rs1_o = 1'b1;
|
||||
uses_rs2_o = 1'b1;
|
||||
branch_o = 1'b1;
|
||||
beq_o = (funct3 == 3'b000);
|
||||
bne_o = (funct3 == 3'b001);
|
||||
blt_o = (funct3 == 3'b100);
|
||||
bge_o = (funct3 == 3'b101);
|
||||
bltu_o = (funct3 == 3'b110);
|
||||
bgeu_o = (funct3 == 3'b111);
|
||||
// 有符号比较用 ALU_SLT、无符号比较用 ALU_SLTU,相等/不等用减法;
|
||||
// 分支是否成立由 EX 级的分支裁决单元决定,这里只与真值表保持一致。
|
||||
alu_op_o = (blt_o || bge_o) ? ALU_SLT : ((bltu_o || bgeu_o) ? ALU_SLTU : ALU_SUB);
|
||||
imm_o = {{19{ir_i[31]}}, ir_i[31], ir_i[7], ir_i[30:25], ir_i[11:8], 1'b0};
|
||||
end
|
||||
end
|
||||
|
||||
// 无条件跳转并链接:jal(写回 PC+4)。
|
||||
OP_JAL: begin
|
||||
jal_o = 1'b1;
|
||||
reg_write_o = 1'b1;
|
||||
wb_sel_o = 3'd2;
|
||||
imm_o = {{11{ir_i[31]}}, ir_i[31], ir_i[19:12], ir_i[20], ir_i[30:21], 1'b0};
|
||||
end
|
||||
|
||||
// 寄存器间接跳转并链接:jalr。
|
||||
OP_JALR: begin
|
||||
if (funct3 == 3'b000) begin
|
||||
jalr_o = 1'b1;
|
||||
uses_rs1_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
alu_op_o = ALU_ADD;
|
||||
reg_write_o = 1'b1;
|
||||
wb_sel_o = 3'd2;
|
||||
end
|
||||
end
|
||||
|
||||
// 系统指令:ecall / uret / CSR 读写。
|
||||
OP_SYS: begin
|
||||
// 电路用 IR[21] 区分 URET 与 ecall。
|
||||
if (funct3 == 3'b000) begin
|
||||
if (ir_i[21]) begin
|
||||
uret_o = 1'b1;
|
||||
end else begin
|
||||
ecall_o = 1'b1;
|
||||
// 按文档说明,ecall 读取 a7(rs17) 与 a0(rs10)。
|
||||
uses_rs1_o = 1'b1;
|
||||
uses_rs2_o = 1'b1;
|
||||
rs1_idx_o = 5'd17;
|
||||
rs2_idx_o = 5'd10;
|
||||
end
|
||||
end else if (funct3 == 3'b001) begin
|
||||
csr_write_o = 1'b1; // CSRRW:写 CSR,并把旧值写回 rd
|
||||
uses_rs1_o = 1'b1;
|
||||
alu_src_o = 1'b1;
|
||||
imm_o = {27'b0, ir_i[19:15]};
|
||||
reg_write_o = (rd_o != 5'd0);
|
||||
wb_sel_o = 3'd4;
|
||||
end else if (funct3 == 3'b110) begin
|
||||
csr_set_o = 1'b1; // CSRRSI:置位 CSR 中的指定位
|
||||
imm_o = {27'b0, ir_i[19:15]};
|
||||
reg_write_o = (rd_o != 5'd0);
|
||||
wb_sel_o = 3'd4;
|
||||
end else if (funct3 == 3'b111) begin
|
||||
csr_clear_o = 1'b1; // CSRRCI:清除 CSR 中的指定位
|
||||
imm_o = {27'b0, ir_i[19:15]};
|
||||
reg_write_o = (rd_o != 5'd0);
|
||||
wb_sel_o = 3'd4;
|
||||
end
|
||||
end
|
||||
|
||||
default: begin
|
||||
end
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,47 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// EX 级前递单元。
|
||||
//
|
||||
// 优先级:EX/MEM 级的结果最新 -> 其次 MEM/WB 级写回值 -> 最后是 ID 级
|
||||
// 译码时读到的寄存器值。load 结果在 MEM 级之后才可用,因此 EX/MEM 的
|
||||
// load(mem_to_reg)不参与前递,改由冒险单元插入一个气泡。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_forward_unit (
|
||||
input wire [31:0] rs1_value_i,
|
||||
input wire [31:0] rs2_value_i,
|
||||
input wire [ 4:0] rs1_idx_i,
|
||||
input wire [ 4:0] rs2_idx_i,
|
||||
// EX/MEM 级
|
||||
input wire exmem_valid_i,
|
||||
input wire exmem_reg_write_i,
|
||||
input wire exmem_mem_to_reg_i,
|
||||
input wire [ 4:0] exmem_rd_i,
|
||||
input wire [31:0] exmem_value_i,
|
||||
// MEM/WB 级
|
||||
input wire memwb_valid_i,
|
||||
input wire memwb_reg_write_i,
|
||||
input wire [ 4:0] memwb_rd_i,
|
||||
input wire [31:0] memwb_value_i,
|
||||
// 前递后的操作数
|
||||
output reg [31:0] src1_o,
|
||||
output reg [31:0] src2_o
|
||||
);
|
||||
wire ex_mem_forward_valid = exmem_valid_i && exmem_reg_write_i &&
|
||||
!exmem_mem_to_reg_i && (exmem_rd_i != 5'd0);
|
||||
wire wb_forward_valid = memwb_valid_i && memwb_reg_write_i && (memwb_rd_i != 5'd0);
|
||||
|
||||
always @* begin
|
||||
src1_o = rs1_value_i;
|
||||
src2_o = rs2_value_i;
|
||||
|
||||
if (ex_mem_forward_valid && (exmem_rd_i == rs1_idx_i)) src1_o = exmem_value_i;
|
||||
else if (wb_forward_valid && (memwb_rd_i == rs1_idx_i)) src1_o = memwb_value_i;
|
||||
|
||||
if (ex_mem_forward_valid && (exmem_rd_i == rs2_idx_i)) src2_o = exmem_value_i;
|
||||
else if (wb_forward_valid && (memwb_rd_i == rs2_idx_i)) src2_o = memwb_value_i;
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,43 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 冒险/流水控制单元。
|
||||
//
|
||||
// load-use 冒险:EX 级是 load 且 ID 级指令马上要用它的结果时,插入一个
|
||||
// 气泡(停顿一拍)。其余数据相关由前递网络解决。
|
||||
// 冲刷(flush_younger)优先级高于停顿:中断、URET、ecall 停机、分支
|
||||
// 重定向都会丢弃年轻指令,此时不需要再插气泡。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_hazard_unit (
|
||||
input wire ifid_valid_i,
|
||||
input wire idex_valid_i,
|
||||
input wire idex_mem_to_reg_i,
|
||||
input wire [4:0] idex_rd_i,
|
||||
input wire d_uses_rs1_i,
|
||||
input wire d_uses_rs2_i,
|
||||
input wire [4:0] d_src1_idx_i,
|
||||
input wire [4:0] d_src2_idx_i,
|
||||
// 冲刷来源
|
||||
input wire take_irq_i,
|
||||
input wire uret_redirect_i,
|
||||
input wire halt_event_i,
|
||||
input wire redirect_valid_i,
|
||||
input wire halted_i,
|
||||
// 控制结果
|
||||
output wire load_use_hazard_o,
|
||||
output wire flush_younger_o,
|
||||
output wire pipeline_stall_o
|
||||
);
|
||||
// EX 级为 load,且 ID 级的源寄存器与它的目的寄存器相同。
|
||||
assign load_use_hazard_o = ifid_valid_i && idex_valid_i && idex_mem_to_reg_i &&
|
||||
(idex_rd_i != 5'd0) &&
|
||||
((d_uses_rs1_i && (d_src1_idx_i == idex_rd_i)) ||
|
||||
(d_uses_rs2_i && (d_src2_idx_i == idex_rd_i)));
|
||||
|
||||
assign flush_younger_o = take_irq_i || uret_redirect_i || halt_event_i || redirect_valid_i;
|
||||
|
||||
assign pipeline_stall_o = load_use_hazard_o && !flush_younger_o && !halted_i;
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,188 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 中断控制器 + ustatus/uepc CSR + 嵌套返回栈。
|
||||
//
|
||||
// IRQ3 优先级最高,其次 IRQ2,最后 IRQ1;嵌套请求只有优先级高于当前级别
|
||||
// 才会被接纳。中断入口地址由优先级选出,返回地址保存在 epc_stack 中,
|
||||
// 当前上下文的活动返回 PC 放在 uepc_q(与课程讲义用 CSRRW 保存/恢复
|
||||
// uepc 的流程一致)。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_irq_ctrl #(
|
||||
parameter [31:0] IRQ1_VECTOR = 32'h0000_30ac,
|
||||
parameter [31:0] IRQ2_VECTOR = 32'h0000_31e4,
|
||||
parameter [31:0] IRQ3_VECTOR = 32'h0000_3310,
|
||||
parameter IRQ_STACK_DEPTH = 4,
|
||||
// ustatus 复位值。bit0 为 MIE。课程的中断测试程序只会在中断处理
|
||||
// 程序中设置 MIE,若复位后不立即置位 MIE,主程序将永远无法响应
|
||||
// 第一次中断,因此这里默认打开 MIE。
|
||||
parameter [31:0] USTATUS_INIT = 32'h0000_0001
|
||||
) (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
input wire [ 2:0] irq_i,
|
||||
input wire halted_i,
|
||||
// EX 级正在处理重定向/URET/停机,此时不允许响应中断
|
||||
input wire ex_block_i,
|
||||
// 中断发生时要保存的返回地址(由顶层根据流水线状态给出)
|
||||
input wire [31:0] save_pc_i,
|
||||
// EX 级的 URET
|
||||
input wire uret_i,
|
||||
// EX 级 CSR 指令(CSRRW/CSRRSI/CSRRCI)
|
||||
input wire csr_valid_i,
|
||||
input wire csr_set_i,
|
||||
input wire csr_clear_i,
|
||||
input wire csr_write_i,
|
||||
input wire [11:0] csr_addr_i,
|
||||
input wire [31:0] csr_imm_i,
|
||||
input wire [31:0] csr_wdata_i,
|
||||
// 控制结果
|
||||
output wire take_irq_o,
|
||||
output wire [31:0] vector_o,
|
||||
output wire [31:0] return_pc_o,
|
||||
output wire [ 2:0] pending_o,
|
||||
output wire [ 1:0] level_o,
|
||||
output wire [31:0] uepc_o,
|
||||
output wire [31:0] ustatus_o
|
||||
);
|
||||
reg [31:0] ustatus_q;
|
||||
reg [31:0] uepc_q;
|
||||
reg [ 1:0] irq_current_q;
|
||||
reg [ 2:0] irq_pending_q;
|
||||
reg [ 2:0] irq_sync1_q;
|
||||
reg [ 2:0] irq_sync2_q;
|
||||
reg [ 2:0] irq_prev_q;
|
||||
reg [31:0] epc_stack [0:IRQ_STACK_DEPTH-1];
|
||||
reg [31:0] status_stack [0:IRQ_STACK_DEPTH-1];
|
||||
reg [ 1:0] priority_stack [0:IRQ_STACK_DEPTH-1];
|
||||
reg [ 2:0] irq_depth_q;
|
||||
|
||||
reg irq_selected_valid;
|
||||
reg [ 1:0] irq_selected_level;
|
||||
reg [31:0] irq_vector;
|
||||
reg [ 2:0] irq_pending_d;
|
||||
reg [31:0] ustatus_d;
|
||||
reg [31:0] uepc_d;
|
||||
|
||||
integer s;
|
||||
|
||||
// 上升沿检测:只在 irq_i 由 0 变 1 的那一拍产生中断事件。
|
||||
wire [ 2:0] irq_event = irq_sync2_q & ~irq_prev_q;
|
||||
|
||||
// 优先级仲裁:在当前级别允许的条件下选出优先级最高的中断源。
|
||||
always @* begin
|
||||
irq_selected_valid = 1'b0;
|
||||
irq_selected_level = 2'd0;
|
||||
if (irq_pending_q[2] && (irq_current_q < 2'd3)) begin
|
||||
irq_selected_valid = 1'b1;
|
||||
irq_selected_level = 2'd3;
|
||||
end else if (irq_pending_q[1] && (irq_current_q < 2'd2)) begin
|
||||
irq_selected_valid = 1'b1;
|
||||
irq_selected_level = 2'd2;
|
||||
end else if (irq_pending_q[0] && (irq_current_q < 2'd1)) begin
|
||||
irq_selected_valid = 1'b1;
|
||||
irq_selected_level = 2'd1;
|
||||
end
|
||||
end
|
||||
|
||||
// 依据被选中的优先级选择对应的中断入口地址。
|
||||
always @* begin
|
||||
case (irq_selected_level)
|
||||
2'd1: irq_vector = IRQ1_VECTOR;
|
||||
2'd2: irq_vector = IRQ2_VECTOR;
|
||||
2'd3: irq_vector = IRQ3_VECTOR;
|
||||
default: irq_vector = IRQ1_VECTOR;
|
||||
endcase
|
||||
end
|
||||
|
||||
// 响应中断的条件:有已选中中断、MIE 使能、栈未溢出、未停机,且没有
|
||||
// 正在处理的重定向/URET/停机事件。
|
||||
assign take_irq_o = irq_selected_valid && ustatus_q[0] &&
|
||||
(irq_depth_q < IRQ_STACK_DEPTH) && !halted_i &&
|
||||
!ex_block_i;
|
||||
|
||||
assign vector_o = irq_vector;
|
||||
assign return_pc_o = uepc_q;
|
||||
assign pending_o = irq_pending_q;
|
||||
assign level_o = irq_current_q;
|
||||
assign uepc_o = uepc_q;
|
||||
assign ustatus_o = ustatus_q;
|
||||
|
||||
// 组合逻辑计算下一拍的挂起位与 CSR(ustatus/uepc)取值。
|
||||
always @* begin
|
||||
irq_pending_d = irq_pending_q | irq_event;
|
||||
if (take_irq_o) begin
|
||||
case (irq_selected_level)
|
||||
2'd1: irq_pending_d[0] = 1'b0;
|
||||
2'd2: irq_pending_d[1] = 1'b0;
|
||||
2'd3: irq_pending_d[2] = 1'b0;
|
||||
default: irq_pending_d = irq_pending_d;
|
||||
endcase
|
||||
end
|
||||
|
||||
ustatus_d = ustatus_q;
|
||||
uepc_d = uepc_q;
|
||||
if (csr_valid_i && csr_set_i) begin
|
||||
if (csr_addr_i == 12'h004) ustatus_d = ustatus_q | csr_imm_i;
|
||||
else if (csr_addr_i == 12'h041) uepc_d = uepc_q | csr_imm_i;
|
||||
end else if (csr_valid_i && csr_clear_i) begin
|
||||
if (csr_addr_i == 12'h004) ustatus_d = ustatus_q & ~csr_imm_i;
|
||||
else if (csr_addr_i == 12'h041) uepc_d = uepc_q & ~csr_imm_i;
|
||||
end else if (csr_valid_i && csr_write_i) begin
|
||||
if (csr_addr_i == 12'h004) ustatus_d = csr_wdata_i;
|
||||
else if (csr_addr_i == 12'h041) uepc_d = csr_wdata_i;
|
||||
end
|
||||
|
||||
if (take_irq_o) ustatus_d[0] = 1'b0;
|
||||
else if (uret_i && (irq_depth_q != 0)) ustatus_d = status_stack[irq_depth_q-1'b1];
|
||||
|
||||
if (take_irq_o) uepc_d = save_pc_i;
|
||||
else if (uret_i && (irq_depth_q > 1)) uepc_d = epc_stack[irq_depth_q-2];
|
||||
else if (uret_i && (irq_depth_q == 1)) uepc_d = 32'b0;
|
||||
end
|
||||
|
||||
// 复位时清空中断状态与嵌套栈。
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
ustatus_q <= USTATUS_INIT;
|
||||
uepc_q <= 32'b0;
|
||||
irq_current_q <= 2'b0;
|
||||
irq_pending_q <= 3'b0;
|
||||
irq_sync1_q <= 3'b0;
|
||||
irq_sync2_q <= 3'b0;
|
||||
irq_prev_q <= 3'b0;
|
||||
irq_depth_q <= 3'b0;
|
||||
for (s = 0; s < IRQ_STACK_DEPTH; s = s + 1) begin
|
||||
epc_stack[s] <= 32'b0;
|
||||
status_stack[s] <= 32'b0;
|
||||
priority_stack[s] <= 2'b0;
|
||||
end
|
||||
end else begin
|
||||
// 中断输入打两拍同步,并锁存挂起状态与 CSR。
|
||||
irq_sync1_q <= irq_i;
|
||||
irq_sync2_q <= irq_sync1_q;
|
||||
irq_prev_q <= irq_sync2_q;
|
||||
irq_pending_q <= irq_pending_d;
|
||||
ustatus_q <= ustatus_d;
|
||||
uepc_q <= uepc_d;
|
||||
|
||||
if (take_irq_o) begin
|
||||
if (irq_depth_q < IRQ_STACK_DEPTH) begin
|
||||
epc_stack[irq_depth_q] <= save_pc_i;
|
||||
status_stack[irq_depth_q] <= ustatus_q;
|
||||
priority_stack[irq_depth_q] <= irq_current_q;
|
||||
irq_depth_q <= irq_depth_q + 3'd1;
|
||||
end
|
||||
irq_current_q <= irq_selected_level;
|
||||
end else if (uret_i) begin
|
||||
if (irq_depth_q != 0) begin
|
||||
irq_depth_q <= irq_depth_q - 3'd1;
|
||||
irq_current_q <= priority_stack[irq_depth_q-1'b1];
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,76 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 性能计数器:周期数、停顿/气泡数、条件分支跳转数、无条件跳转数,
|
||||
// 以及分支预测的成功/失败次数。
|
||||
//
|
||||
// 这些计数器只影响可观测的统计量,不参与处理器控制,便于在波形中单独
|
||||
// 观察流水线效率。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_perf_counters (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
input wire inc_cycle_i, // 未停机时周期数 +1
|
||||
input wire stall_i, // load-use 停顿
|
||||
input wire flush_i, // 冲刷年轻指令
|
||||
input wire cond_taken_i, // EX 级条件分支实际跳转
|
||||
input wire cond_valid_i, // EX 级条件分支有效
|
||||
input wire mispredict_i, // EX 级条件分支预测失败
|
||||
input wire uncond_i, // EX 级 JAL/JALR
|
||||
output wire [15:0] cycle_count_o,
|
||||
output wire [15:0] stall_count_o,
|
||||
output wire [15:0] bubble_count_o,
|
||||
output wire [15:0] conditional_taken_count_o,
|
||||
output wire [15:0] unconditional_branch_count_o,
|
||||
output wire [15:0] prediction_success_count_o,
|
||||
output wire [15:0] prediction_failure_count_o
|
||||
);
|
||||
reg [15:0] cycle_count_q;
|
||||
reg [15:0] stall_count_q;
|
||||
reg [15:0] bubble_count_q;
|
||||
reg [15:0] conditional_taken_count_q;
|
||||
reg [15:0] unconditional_branch_count_q;
|
||||
reg [15:0] prediction_success_count_q;
|
||||
reg [15:0] prediction_failure_count_q;
|
||||
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
cycle_count_q <= 16'b0;
|
||||
stall_count_q <= 16'b0;
|
||||
bubble_count_q <= 16'b0;
|
||||
conditional_taken_count_q <= 16'b0;
|
||||
unconditional_branch_count_q <= 16'b0;
|
||||
prediction_success_count_q <= 16'b0;
|
||||
prediction_failure_count_q <= 16'b0;
|
||||
end else begin
|
||||
if (inc_cycle_i) cycle_count_q <= cycle_count_q + 16'd1;
|
||||
|
||||
if (stall_i) begin
|
||||
stall_count_q <= stall_count_q + 16'd1;
|
||||
bubble_count_q <= bubble_count_q + 16'd1;
|
||||
end else if (flush_i) begin
|
||||
// 一次冲刷丢弃 IF/ID 两条年轻指令,记两个气泡。
|
||||
bubble_count_q <= bubble_count_q + 16'd2;
|
||||
end
|
||||
|
||||
if (cond_taken_i) conditional_taken_count_q <= conditional_taken_count_q + 16'd1;
|
||||
if (uncond_i) unconditional_branch_count_q <= unconditional_branch_count_q + 16'd1;
|
||||
|
||||
if (cond_valid_i) begin
|
||||
if (mispredict_i) prediction_failure_count_q <= prediction_failure_count_q + 16'd1;
|
||||
else prediction_success_count_q <= prediction_success_count_q + 16'd1;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assign cycle_count_o = cycle_count_q;
|
||||
assign stall_count_o = stall_count_q;
|
||||
assign bubble_count_o = bubble_count_q;
|
||||
assign conditional_taken_count_o = conditional_taken_count_q;
|
||||
assign unconditional_branch_count_o = unconditional_branch_count_q;
|
||||
assign prediction_success_count_o = prediction_success_count_q;
|
||||
assign prediction_failure_count_o = prediction_failure_count_q;
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,767 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// cpu21-risc-v-4.circ -> 可综合 Verilog 实现(顶层)
|
||||
//
|
||||
// 处理器为五级流水线 IF/ID/EX/MEM/WB。本文件只保留:
|
||||
// * 五级流水寄存器与各级之间的连线;
|
||||
// * 各功能子模块的例化;
|
||||
// * PC / IF-ID / ID-EX 的控制优先级(中断、URET、停机、重定向、停顿、
|
||||
// 正常推进);
|
||||
// * 对外输出与调试信号。
|
||||
//
|
||||
// 各功能单元已拆分为独立文件,便于单独仿真调试:
|
||||
// cpu21_riscv_alu.v 组合 ALU(result2 用于 MUL 高位 / 余数)
|
||||
// cpu21_bpb_8.v 八表项全相联分支目标缓冲
|
||||
// cpu21_riscv_decoder.v ID 级组合译码器
|
||||
// cpu21_riscv_regfile.v 寄存器堆(写优先旁路)
|
||||
// cpu21_riscv_forward_unit.v EX 级前递网络
|
||||
// cpu21_riscv_store_unit.v EX 级存储数据对齐
|
||||
// cpu21_riscv_branch_unit.v EX 级控制流裁决
|
||||
// cpu21_riscv_hazard_unit.v load-use 冒险与冲刷/停顿判定
|
||||
// cpu21_riscv_irq_ctrl.v 中断控制、ustatus/uepc 与嵌套返回栈
|
||||
// cpu21_riscv_perf_counters.v 性能计数器
|
||||
// cpu21_mmio.v MMIO:LED 数据寄存器(可写、可读回)
|
||||
//
|
||||
// 主要特性:
|
||||
// * EX 级前递(forwarding)与 load-use 冒险互锁;
|
||||
// * EX 级控制流裁决与流水线重定向;
|
||||
// * 八表项全相联分支目标缓冲(BPB);
|
||||
// * 三路采样的中断源,带优先级与可嵌套的 URET 返回;
|
||||
// * LED 显示通过 MMIO 完成:向 LED_ADDR(默认 0x0000_1000)存储即写 LED,
|
||||
// 也可以从该地址读回 LED 当前值;
|
||||
// * ecall 仍用于停机:a7==34 的历史“显示”调用保留为无操作,其余(如 a7=10)
|
||||
// 令流水线停机并等待 go_i。
|
||||
//
|
||||
// 存储器接口约定:
|
||||
// instr_addr_o 与 data_addr_o 均为字节地址。data_wstrb_o 是字节写掩码,
|
||||
// 因此外部字宽 RAM 可用 data_addr_o[11:2] 作为字索引,再依据
|
||||
// data_wstrb_o 完成字节合并。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_redirect_int_bpb #(
|
||||
parameter RESET_PC = 32'h0000_0000,
|
||||
parameter IRQ1_VECTOR = 32'h0000_30ac,
|
||||
parameter IRQ2_VECTOR = 32'h0000_31e4,
|
||||
parameter IRQ3_VECTOR = 32'h0000_3310,
|
||||
parameter IRQ_STACK_DEPTH = 4,
|
||||
// MMIO:LED 数据寄存器地址(默认 0x0000_1000,位于 4 KB 数据 RAM 之外)。
|
||||
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
||||
// ustatus 复位值。详见 cpu21_riscv_irq_ctrl 中的说明。
|
||||
parameter [31:0] USTATUS_INIT = 32'h0000_0001
|
||||
) (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
input wire [31:0] instr_i,
|
||||
input wire [31:0] data_rdata_i,
|
||||
input wire [ 2:0] irq_i,
|
||||
input wire go_i,
|
||||
|
||||
output wire [31:0] instr_addr_o,
|
||||
output wire [31:0] data_addr_o,
|
||||
output wire [31:0] data_wdata_o,
|
||||
output wire [ 3:0] data_wstrb_o,
|
||||
output wire data_we_o,
|
||||
|
||||
output wire [31:0] led_data_o,
|
||||
output wire led_valid_o,
|
||||
output wire [31:0] if_pc_o,
|
||||
output wire [31:0] id_pc_o,
|
||||
output wire [31:0] ex_pc_o,
|
||||
output wire [31:0] mem_pc_o,
|
||||
output wire [31:0] wb_pc_o,
|
||||
output wire [31:0] if_ir_o,
|
||||
output wire [31:0] id_ir_o,
|
||||
output wire [31:0] ex_ir_o,
|
||||
output wire [31:0] mem_ir_o,
|
||||
output wire [31:0] wb_ir_o,
|
||||
output wire [31:0] rdin_o,
|
||||
output wire [31:0] mdin_o,
|
||||
output wire reg_write_o,
|
||||
output wire mem_write_o,
|
||||
output wire halted_o,
|
||||
|
||||
output wire [ 2:0] irq_pending_o,
|
||||
output wire [ 1:0] irq_current_level_o,
|
||||
output wire [31:0] uepc_o,
|
||||
output wire [31:0] ustatus_o,
|
||||
output wire bpb_predict_hit_o,
|
||||
output wire bpb_predict_taken_o,
|
||||
output wire bpb_mispredict_o,
|
||||
|
||||
output wire [15:0] cycle_count_o,
|
||||
output wire [15:0] stall_count_o,
|
||||
output wire [15:0] bubble_count_o,
|
||||
output wire [15:0] conditional_taken_count_o,
|
||||
output wire [15:0] unconditional_branch_count_o,
|
||||
output wire [15:0] prediction_success_count_o,
|
||||
output wire [15:0] prediction_failure_count_o
|
||||
);
|
||||
// 顶层只用到少量常量,完整译码表见 cpu21_riscv_decoder.v。
|
||||
localparam OP_BRANCH = 5'h18;
|
||||
localparam ALU_ADD = 4'd5;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 流水线寄存器
|
||||
// -------------------------------------------------------------------------
|
||||
// 当前取指 PC。
|
||||
reg [31:0] pc_q;
|
||||
|
||||
// IF/ID 流水寄存器(含本条指令所携带的分支预测信息)。
|
||||
reg ifid_valid_q;
|
||||
reg [31:0] ifid_pc_q;
|
||||
reg [31:0] ifid_ir_q;
|
||||
reg ifid_pred_taken_q;
|
||||
reg [31:0] ifid_pred_target_q;
|
||||
|
||||
// ID/EX 流水寄存器:保存译码结果与操作数。
|
||||
reg idex_valid_q;
|
||||
reg [31:0] idex_pc_q;
|
||||
reg [31:0] idex_ir_q;
|
||||
reg [31:0] idex_rs1_value_q;
|
||||
reg [31:0] idex_rs2_value_q;
|
||||
reg [4:0] idex_rs1_idx_q;
|
||||
reg [4:0] idex_rs2_idx_q;
|
||||
reg [4:0] idex_rd_q;
|
||||
reg [31:0] idex_imm_q;
|
||||
reg [3:0] idex_aluop_q;
|
||||
reg idex_alu_src_q;
|
||||
reg idex_reg_write_q;
|
||||
reg idex_mem_to_reg_q;
|
||||
reg idex_mem_write_q;
|
||||
reg idex_mem_byte_q;
|
||||
reg [2:0] idex_wb_sel_q;
|
||||
reg [11:0] idex_csr_addr_q;
|
||||
reg idex_branch_q;
|
||||
reg idex_beq_q;
|
||||
reg idex_bne_q;
|
||||
reg idex_blt_q;
|
||||
reg idex_bge_q;
|
||||
reg idex_bltu_q;
|
||||
reg idex_bgeu_q;
|
||||
reg idex_jal_q;
|
||||
reg idex_jalr_q;
|
||||
reg idex_ecall_q;
|
||||
reg idex_uret_q;
|
||||
reg idex_csr_set_q;
|
||||
reg idex_csr_clear_q;
|
||||
reg idex_csr_write_q;
|
||||
reg idex_pred_taken_q;
|
||||
reg [31:0] idex_pred_target_q;
|
||||
|
||||
// EX/MEM 流水寄存器:保存 ALU/存储结果与写回控制。
|
||||
reg exmem_valid_q;
|
||||
reg [31:0] exmem_pc_q;
|
||||
reg [31:0] exmem_ir_q;
|
||||
reg [31:0] exmem_alu_result_q;
|
||||
reg [31:0] exmem_alu_result2_q;
|
||||
reg [31:0] exmem_csr_old_q;
|
||||
reg [31:0] exmem_write_data_q;
|
||||
reg [4:0] exmem_rd_q;
|
||||
reg exmem_reg_write_q;
|
||||
reg exmem_mem_to_reg_q;
|
||||
reg exmem_mem_write_q;
|
||||
reg [3:0] exmem_wstrb_q;
|
||||
reg [2:0] exmem_wb_sel_q;
|
||||
|
||||
// MEM/WB 流水寄存器:保存访存数据与最终写回信息。
|
||||
reg memwb_valid_q;
|
||||
reg [31:0] memwb_pc_q;
|
||||
reg [31:0] memwb_ir_q;
|
||||
reg [31:0] memwb_alu_result_q;
|
||||
reg [31:0] memwb_alu_result2_q;
|
||||
reg [31:0] memwb_csr_old_q;
|
||||
reg [31:0] memwb_mem_data_q;
|
||||
reg [4:0] memwb_rd_q;
|
||||
reg memwb_reg_write_q;
|
||||
reg memwb_mem_to_reg_q;
|
||||
reg [2:0] memwb_wb_sel_q;
|
||||
|
||||
// 停机标志(LED 寄存器已移入 cpu21_mmio.v)。
|
||||
reg halted_q;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// IF 级:分支预测。JAL/JALR 在 EX 级裁决,条件分支则可以直接依据 BPB
|
||||
// 给出的目标地址重定向取指 PC。
|
||||
// -------------------------------------------------------------------------
|
||||
wire [4:0] f_opcode = instr_i[6:2];
|
||||
wire [2:0] f_funct3 = instr_i[14:12];
|
||||
wire f_is_branch = (f_opcode == OP_BRANCH) &&
|
||||
((f_funct3 == 3'b000) || (f_funct3 == 3'b001) ||
|
||||
(f_funct3 == 3'b100) || (f_funct3 == 3'b101) ||
|
||||
(f_funct3 == 3'b110) || (f_funct3 == 3'b111));
|
||||
wire [31:0] bpb_predict_target;
|
||||
wire bpb_predict_hit;
|
||||
wire bpb_predict_taken;
|
||||
wire ex_branch_taken;
|
||||
wire [31:0] ex_target;
|
||||
wire ex_controlflow;
|
||||
wire ex_redirect_valid;
|
||||
wire [31:0] ex_redirect_pc;
|
||||
|
||||
// BPB 更新端口:在 EX 级用条件分支的实际结果训练预测器。
|
||||
wire bpb_update_enable = idex_valid_q && idex_branch_q;
|
||||
wire bpb_update_taken = ex_branch_taken;
|
||||
wire [31:0] bpb_update_target = ex_target;
|
||||
|
||||
// 分支目标缓冲实例。
|
||||
cpu21_bpb_8 u_bpb (
|
||||
.clk (clk),
|
||||
.reset (reset),
|
||||
.predict_pc (pc_q),
|
||||
.predict_enable (f_is_branch),
|
||||
.predict_hit (bpb_predict_hit),
|
||||
.predict_taken (bpb_predict_taken),
|
||||
.predict_target (bpb_predict_target),
|
||||
.update_enable (bpb_update_enable),
|
||||
.update_controlflow(1'b1),
|
||||
.update_pc (idex_pc_q),
|
||||
.update_target (bpb_update_target),
|
||||
.update_taken (bpb_update_taken)
|
||||
);
|
||||
|
||||
// 下一取指 PC:预测跳转命中时使用 BPB 目标地址,否则顺序加 4。
|
||||
wire [31:0] fetch_next_pc =
|
||||
(f_is_branch && bpb_predict_hit && bpb_predict_taken) ?
|
||||
bpb_predict_target : (pc_q + 32'd4);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// ID 级:译码与寄存器堆读取
|
||||
// -------------------------------------------------------------------------
|
||||
wire [4:0] d_src1_idx;
|
||||
wire [4:0] d_src2_idx;
|
||||
wire [4:0] d_rd;
|
||||
wire [31:0] d_imm;
|
||||
wire [3:0] d_aluop;
|
||||
wire [2:0] d_wb_sel;
|
||||
wire [11:0] d_csr_addr;
|
||||
wire d_uses_rs1;
|
||||
wire d_uses_rs2;
|
||||
wire d_reg_write;
|
||||
wire d_mem_to_reg;
|
||||
wire d_mem_write;
|
||||
wire d_mem_byte;
|
||||
wire d_alu_src;
|
||||
wire d_branch;
|
||||
wire d_beq;
|
||||
wire d_bne;
|
||||
wire d_blt;
|
||||
wire d_bge;
|
||||
wire d_bltu;
|
||||
wire d_bgeu;
|
||||
wire d_jal;
|
||||
wire d_jalr;
|
||||
wire d_ecall;
|
||||
wire d_uret;
|
||||
wire d_csr_set;
|
||||
wire d_csr_clear;
|
||||
wire d_csr_write;
|
||||
|
||||
cpu21_riscv_decoder u_decoder (
|
||||
.ir_i (ifid_ir_q),
|
||||
.rs1_idx_o (d_src1_idx),
|
||||
.rs2_idx_o (d_src2_idx),
|
||||
.rd_o (d_rd),
|
||||
.imm_o (d_imm),
|
||||
.alu_op_o (d_aluop),
|
||||
.wb_sel_o (d_wb_sel),
|
||||
.csr_addr_o (d_csr_addr),
|
||||
.uses_rs1_o (d_uses_rs1),
|
||||
.uses_rs2_o (d_uses_rs2),
|
||||
.reg_write_o (d_reg_write),
|
||||
.mem_to_reg_o(d_mem_to_reg),
|
||||
.mem_write_o (d_mem_write),
|
||||
.mem_byte_o (d_mem_byte),
|
||||
.alu_src_o (d_alu_src),
|
||||
.branch_o (d_branch),
|
||||
.beq_o (d_beq),
|
||||
.bne_o (d_bne),
|
||||
.blt_o (d_blt),
|
||||
.bge_o (d_bge),
|
||||
.bltu_o (d_bltu),
|
||||
.bgeu_o (d_bgeu),
|
||||
.jal_o (d_jal),
|
||||
.jalr_o (d_jalr),
|
||||
.ecall_o (d_ecall),
|
||||
.uret_o (d_uret),
|
||||
.csr_set_o (d_csr_set),
|
||||
.csr_clear_o (d_csr_clear),
|
||||
.csr_write_o (d_csr_write)
|
||||
);
|
||||
|
||||
// 写回值选择:wb_sel 0=ALU 结果,1=访存数据,2=PC+4,3=MUL 高位/余数,
|
||||
// 4=CSR 旧值。
|
||||
wire [31:0] wb_value;
|
||||
assign wb_value = (memwb_wb_sel_q == 3'd1) ? memwb_mem_data_q :
|
||||
(memwb_wb_sel_q == 3'd2) ? (memwb_pc_q + 32'd4) :
|
||||
(memwb_wb_sel_q == 3'd3) ? memwb_alu_result2_q :
|
||||
(memwb_wb_sel_q == 3'd4) ? memwb_csr_old_q :
|
||||
memwb_alu_result_q;
|
||||
|
||||
// WB 级写回使能(同时用于寄存器堆的写端口与写优先旁路)。
|
||||
wire memwb_reg_write_en = memwb_valid_q && memwb_reg_write_q;
|
||||
|
||||
// ID 级读寄存器:x0 恒为 0,并对同周期 WB 的结果做写优先旁路。
|
||||
wire [31:0] d_rs1_value;
|
||||
wire [31:0] d_rs2_value;
|
||||
|
||||
cpu21_riscv_regfile u_regfile (
|
||||
.clk (clk),
|
||||
.reset (reset),
|
||||
.rs1_idx_i (d_src1_idx),
|
||||
.rs2_idx_i (d_src2_idx),
|
||||
.rs1_data_o(d_rs1_value),
|
||||
.rs2_data_o(d_rs2_value),
|
||||
.we_i (memwb_reg_write_en),
|
||||
.waddr_i (memwb_rd_q),
|
||||
.wdata_i (wb_value)
|
||||
);
|
||||
|
||||
// 中断/CSR 相关连线(中断控制器实例见下方"中断 / CSR"小节)。
|
||||
wire take_irq;
|
||||
wire [31:0] irq_vector;
|
||||
wire [31:0] ex_return_pc;
|
||||
wire [2:0] irq_pending;
|
||||
wire [1:0] irq_current_level;
|
||||
wire [31:0] uepc_value;
|
||||
wire [31:0] ustatus_value;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// EX 级:前递网络、ALU 运算、存储对齐与控制流裁决
|
||||
// -------------------------------------------------------------------------
|
||||
// EX/MEM 级要前递的值(按 wb_sel 选取)。
|
||||
wire [31:0] exmem_forward_value =
|
||||
(exmem_wb_sel_q == 3'd2) ? (exmem_pc_q + 32'd4) :
|
||||
(exmem_wb_sel_q == 3'd3) ? exmem_alu_result2_q :
|
||||
(exmem_wb_sel_q == 3'd4) ? exmem_csr_old_q :
|
||||
exmem_alu_result_q;
|
||||
|
||||
// 前递优先级:EX/MEM 的结果最新,其次为 MEM/WB 写回值,最后才是
|
||||
// 译码时读到的寄存器值。
|
||||
wire [31:0] ex_src1;
|
||||
wire [31:0] ex_src2;
|
||||
|
||||
cpu21_riscv_forward_unit u_forward_unit (
|
||||
.rs1_value_i (idex_rs1_value_q),
|
||||
.rs2_value_i (idex_rs2_value_q),
|
||||
.rs1_idx_i (idex_rs1_idx_q),
|
||||
.rs2_idx_i (idex_rs2_idx_q),
|
||||
.exmem_valid_i (exmem_valid_q),
|
||||
.exmem_reg_write_i (exmem_reg_write_q),
|
||||
.exmem_mem_to_reg_i(exmem_mem_to_reg_q),
|
||||
.exmem_rd_i (exmem_rd_q),
|
||||
.exmem_value_i (exmem_forward_value),
|
||||
.memwb_valid_i (memwb_valid_q),
|
||||
.memwb_reg_write_i (memwb_reg_write_q),
|
||||
.memwb_rd_i (memwb_rd_q),
|
||||
.memwb_value_i (wb_value),
|
||||
.src1_o (ex_src1),
|
||||
.src2_o (ex_src2)
|
||||
);
|
||||
|
||||
// ALU 的 B 端:立即数或 rs2。
|
||||
wire [31:0] ex_alu_b = idex_alu_src_q ? idex_imm_q : ex_src2;
|
||||
wire [31:0] ex_alu_result;
|
||||
wire [31:0] ex_alu_result2;
|
||||
// 读取 CSR 旧值(ustatus=0x004、uepc=0x041),供 CSR 指令回写 rd。
|
||||
wire [31:0] ex_csr_old_value =
|
||||
(idex_csr_addr_q == 12'h004) ? ustatus_value :
|
||||
(idex_csr_addr_q == 12'h041) ? uepc_value : 32'b0;
|
||||
|
||||
// ALU 实例(纯组合逻辑)。
|
||||
cpu21_riscv_alu u_ex_alu (
|
||||
.op (idex_aluop_q),
|
||||
.a (ex_src1),
|
||||
.b (ex_alu_b),
|
||||
.result (ex_alu_result),
|
||||
.result2(ex_alu_result2)
|
||||
);
|
||||
|
||||
// 控制流裁决:条件分支方向/目标、JAL/JALR 目标、是否需要重定向。
|
||||
cpu21_riscv_branch_unit u_branch_unit (
|
||||
.valid_i (idex_valid_q),
|
||||
.pc_i (idex_pc_q),
|
||||
.imm_i (idex_imm_q),
|
||||
.src1_i (ex_src1),
|
||||
.src2_i (ex_src2),
|
||||
.branch_i (idex_branch_q),
|
||||
.beq_i (idex_beq_q),
|
||||
.bne_i (idex_bne_q),
|
||||
.blt_i (idex_blt_q),
|
||||
.bge_i (idex_bge_q),
|
||||
.bltu_i (idex_bltu_q),
|
||||
.bgeu_i (idex_bgeu_q),
|
||||
.jal_i (idex_jal_q),
|
||||
.jalr_i (idex_jalr_q),
|
||||
.pred_taken_i (idex_pred_taken_q),
|
||||
.pred_target_i (idex_pred_target_q),
|
||||
.branch_taken_o (ex_branch_taken),
|
||||
.controlflow_o (ex_controlflow),
|
||||
.target_o (ex_target),
|
||||
.redirect_valid_o(ex_redirect_valid),
|
||||
.redirect_pc_o (ex_redirect_pc)
|
||||
);
|
||||
|
||||
// 存储数据对齐:依据字节地址低两位生成写数据与字节写掩码。
|
||||
wire [31:0] ex_store_data;
|
||||
wire [ 3:0] ex_store_wstrb;
|
||||
|
||||
cpu21_riscv_store_unit u_store_unit (
|
||||
.addr_i (ex_alu_result),
|
||||
.data_i (ex_src2),
|
||||
.mem_write_i (idex_mem_write_q),
|
||||
.mem_byte_i (idex_mem_byte_q),
|
||||
.store_data_o (ex_store_data),
|
||||
.store_wstrb_o(ex_store_wstrb)
|
||||
);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// MMIO:LED 等外设映射到数据地址空间,在 MEM 级做地址译码。
|
||||
// -------------------------------------------------------------------------
|
||||
wire [31:0] mmio_rdata;
|
||||
wire mmio_rdata_hit;
|
||||
|
||||
cpu21_mmio #(
|
||||
.LED_ADDR(LED_ADDR)
|
||||
) u_mmio (
|
||||
.clk (clk),
|
||||
.reset (reset),
|
||||
.mem_valid_i(exmem_valid_q),
|
||||
.mem_write_i(exmem_mem_write_q),
|
||||
.mem_read_i (exmem_mem_to_reg_q),
|
||||
.addr_i (exmem_alu_result_q),
|
||||
.wdata_i (exmem_write_data_q),
|
||||
.wstrb_i (exmem_wstrb_q),
|
||||
.rdata_o (mmio_rdata),
|
||||
.rdata_hit_o(mmio_rdata_hit),
|
||||
.led_data_o (led_data_o),
|
||||
.led_valid_o(led_valid_o)
|
||||
);
|
||||
|
||||
// 访存读数据:命中 MMIO 时用外设数据,否则用外部存储器数据。
|
||||
wire [31:0] mem_rdata = mmio_rdata_hit ? mmio_rdata : data_rdata_i;
|
||||
|
||||
// ecall 约定:LED 显示已改由 MMIO 存储完成(见上方的 cpu21_mmio 例化)。
|
||||
// a7==34 的历史“显示”调用保留为无操作;其余(例如 a7=10)令处理器停机,
|
||||
// 等待 go_i 重新启动。
|
||||
wire ex_halt_event = idex_valid_q && idex_ecall_q && (ex_src1 != 32'd34);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 中断 / CSR / 嵌套返回栈
|
||||
// -------------------------------------------------------------------------
|
||||
wire ex_uret_redirect = idex_valid_q && idex_uret_q;
|
||||
// EX 级正在重定向/URET/停机时不允许响应中断。
|
||||
wire ex_block_irq = ex_redirect_valid || ex_uret_redirect || ex_halt_event;
|
||||
// 中断发生时要保存的返回地址取"下一个架构 PC":若 EX 是一条预测正确
|
||||
// 且已跳转的分支,则该 PC 就是它的目标地址;若 EX 为空,则 IF/ID 中
|
||||
// 保存的就是尚未执行的最老指令。
|
||||
wire [31:0] irq_save_pc = idex_valid_q ?
|
||||
((idex_branch_q && ex_branch_taken) ? ex_target : (idex_pc_q + 32'd4)) :
|
||||
(ifid_valid_q ? ifid_pc_q : pc_q);
|
||||
|
||||
|
||||
cpu21_riscv_irq_ctrl #(
|
||||
.IRQ1_VECTOR (IRQ1_VECTOR),
|
||||
.IRQ2_VECTOR (IRQ2_VECTOR),
|
||||
.IRQ3_VECTOR (IRQ3_VECTOR),
|
||||
.IRQ_STACK_DEPTH(IRQ_STACK_DEPTH),
|
||||
.USTATUS_INIT (USTATUS_INIT)
|
||||
) u_irq_ctrl (
|
||||
.clk (clk),
|
||||
.reset (reset),
|
||||
.irq_i (irq_i),
|
||||
.halted_i (halted_q),
|
||||
.ex_block_i (ex_block_irq),
|
||||
.save_pc_i (irq_save_pc),
|
||||
.uret_i (ex_uret_redirect),
|
||||
.csr_valid_i(idex_valid_q),
|
||||
.csr_set_i (idex_csr_set_q),
|
||||
.csr_clear_i(idex_csr_clear_q),
|
||||
.csr_write_i(idex_csr_write_q),
|
||||
.csr_addr_i (idex_csr_addr_q),
|
||||
.csr_imm_i (idex_imm_q),
|
||||
.csr_wdata_i(ex_src1),
|
||||
.take_irq_o (take_irq),
|
||||
.vector_o (irq_vector),
|
||||
.return_pc_o(ex_return_pc),
|
||||
.pending_o (irq_pending),
|
||||
.level_o (irq_current_level),
|
||||
.uepc_o (uepc_value),
|
||||
.ustatus_o (ustatus_value)
|
||||
);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 冒险 / 停顿 / 冲刷判定
|
||||
// -------------------------------------------------------------------------
|
||||
wire load_use_hazard;
|
||||
wire flush_younger;
|
||||
wire pipeline_stall;
|
||||
|
||||
cpu21_riscv_hazard_unit u_hazard_unit (
|
||||
.ifid_valid_i (ifid_valid_q),
|
||||
.idex_valid_i (idex_valid_q),
|
||||
.idex_mem_to_reg_i(idex_mem_to_reg_q),
|
||||
.idex_rd_i (idex_rd_q),
|
||||
.d_uses_rs1_i (d_uses_rs1),
|
||||
.d_uses_rs2_i (d_uses_rs2),
|
||||
.d_src1_idx_i (d_src1_idx),
|
||||
.d_src2_idx_i (d_src2_idx),
|
||||
.take_irq_i (take_irq),
|
||||
.uret_redirect_i (ex_uret_redirect),
|
||||
.halt_event_i (ex_halt_event),
|
||||
.redirect_valid_i (ex_redirect_valid),
|
||||
.halted_i (halted_q),
|
||||
.load_use_hazard_o(load_use_hazard),
|
||||
.flush_younger_o (flush_younger),
|
||||
.pipeline_stall_o (pipeline_stall)
|
||||
);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 对外输出与调试信号。
|
||||
// -------------------------------------------------------------------------
|
||||
// 存储器接口:指令地址来自取指 PC;数据地址/写数据/写掩码来自 MEM 级。
|
||||
assign instr_addr_o = pc_q;
|
||||
assign data_addr_o = exmem_alu_result_q;
|
||||
assign data_wdata_o = exmem_write_data_q;
|
||||
assign data_wstrb_o = (exmem_valid_q && exmem_mem_write_q) ? exmem_wstrb_q : 4'b0;
|
||||
assign data_we_o = |data_wstrb_o;
|
||||
|
||||
// LED 输出由 cpu21_mmio 驱动,这里只保留流水线各级 PC/指令回显。
|
||||
assign if_pc_o = pc_q;
|
||||
assign id_pc_o = ifid_valid_q ? ifid_pc_q : 32'b0;
|
||||
assign ex_pc_o = idex_valid_q ? idex_pc_q : 32'b0;
|
||||
assign mem_pc_o = exmem_valid_q ? exmem_pc_q : 32'b0;
|
||||
assign wb_pc_o = memwb_valid_q ? memwb_pc_q : 32'b0;
|
||||
assign if_ir_o = instr_i;
|
||||
assign id_ir_o = ifid_valid_q ? ifid_ir_q : 32'b0;
|
||||
assign ex_ir_o = idex_valid_q ? idex_ir_q : 32'b0;
|
||||
assign mem_ir_o = exmem_valid_q ? exmem_ir_q : 32'b0;
|
||||
assign wb_ir_o = memwb_valid_q ? memwb_ir_q : 32'b0;
|
||||
assign rdin_o = wb_value;
|
||||
assign mdin_o = exmem_write_data_q;
|
||||
assign reg_write_o = memwb_valid_q && memwb_reg_write_q;
|
||||
assign mem_write_o = exmem_valid_q && exmem_mem_write_q;
|
||||
assign halted_o = halted_q;
|
||||
assign irq_pending_o = irq_pending;
|
||||
assign irq_current_level_o = irq_current_level;
|
||||
assign uepc_o = uepc_value;
|
||||
assign ustatus_o = ustatus_value;
|
||||
assign bpb_predict_hit_o = bpb_predict_hit;
|
||||
assign bpb_predict_taken_o = bpb_predict_taken;
|
||||
assign bpb_mispredict_o = ex_redirect_valid && idex_branch_q;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 状态更新。顺序遵循五级流水线:先捕获 MEM/WB、EX/MEM,再处理
|
||||
// ID/EX 与 IF/ID/PC 的控制。
|
||||
// -------------------------------------------------------------------------
|
||||
// 复位时清空所有流水寄存器与 LED/停机标志(寄存器堆、中断状态与
|
||||
// 性能计数器在各自的子模块内复位)。
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
pc_q <= RESET_PC;
|
||||
ifid_valid_q <= 1'b0;
|
||||
ifid_pc_q <= 32'b0;
|
||||
ifid_ir_q <= 32'b0;
|
||||
ifid_pred_taken_q <= 1'b0;
|
||||
ifid_pred_target_q <= 32'b0;
|
||||
|
||||
idex_valid_q <= 1'b0;
|
||||
idex_pc_q <= 32'b0;
|
||||
idex_ir_q <= 32'b0;
|
||||
idex_rs1_value_q <= 32'b0;
|
||||
idex_rs2_value_q <= 32'b0;
|
||||
idex_rs1_idx_q <= 5'b0;
|
||||
idex_rs2_idx_q <= 5'b0;
|
||||
idex_rd_q <= 5'b0;
|
||||
idex_imm_q <= 32'b0;
|
||||
idex_aluop_q <= ALU_ADD;
|
||||
idex_alu_src_q <= 1'b0;
|
||||
idex_reg_write_q <= 1'b0;
|
||||
idex_mem_to_reg_q <= 1'b0;
|
||||
idex_mem_write_q <= 1'b0;
|
||||
idex_mem_byte_q <= 1'b0;
|
||||
idex_wb_sel_q <= 3'b0;
|
||||
idex_csr_addr_q <= 12'b0;
|
||||
idex_branch_q <= 1'b0;
|
||||
idex_beq_q <= 1'b0;
|
||||
idex_bne_q <= 1'b0;
|
||||
idex_blt_q <= 1'b0;
|
||||
idex_bge_q <= 1'b0;
|
||||
idex_bltu_q <= 1'b0;
|
||||
idex_bgeu_q <= 1'b0;
|
||||
idex_jal_q <= 1'b0;
|
||||
idex_jalr_q <= 1'b0;
|
||||
idex_ecall_q <= 1'b0;
|
||||
idex_uret_q <= 1'b0;
|
||||
idex_csr_set_q <= 1'b0;
|
||||
idex_csr_clear_q <= 1'b0;
|
||||
idex_csr_write_q <= 1'b0;
|
||||
idex_pred_taken_q <= 1'b0;
|
||||
idex_pred_target_q <= 32'b0;
|
||||
|
||||
exmem_valid_q <= 1'b0;
|
||||
exmem_pc_q <= 32'b0;
|
||||
exmem_ir_q <= 32'b0;
|
||||
exmem_alu_result_q <= 32'b0;
|
||||
exmem_alu_result2_q <= 32'b0;
|
||||
exmem_csr_old_q <= 32'b0;
|
||||
exmem_write_data_q <= 32'b0;
|
||||
exmem_rd_q <= 5'b0;
|
||||
exmem_reg_write_q <= 1'b0;
|
||||
exmem_mem_to_reg_q <= 1'b0;
|
||||
exmem_mem_write_q <= 1'b0;
|
||||
exmem_wstrb_q <= 4'b0;
|
||||
exmem_wb_sel_q <= 3'b0;
|
||||
|
||||
memwb_valid_q <= 1'b0;
|
||||
memwb_pc_q <= 32'b0;
|
||||
memwb_ir_q <= 32'b0;
|
||||
memwb_alu_result_q <= 32'b0;
|
||||
memwb_alu_result2_q <= 32'b0;
|
||||
memwb_csr_old_q <= 32'b0;
|
||||
memwb_mem_data_q <= 32'b0;
|
||||
memwb_rd_q <= 5'b0;
|
||||
memwb_reg_write_q <= 1'b0;
|
||||
memwb_mem_to_reg_q <= 1'b0;
|
||||
memwb_wb_sel_q <= 3'b0;
|
||||
|
||||
halted_q <= 1'b0;
|
||||
end else begin
|
||||
if (go_i) halted_q <= 1'b0;
|
||||
if (ex_halt_event) halted_q <= 1'b1;
|
||||
|
||||
// MEM/WB 流水寄存器捕获。
|
||||
memwb_valid_q <= exmem_valid_q;
|
||||
memwb_pc_q <= exmem_pc_q;
|
||||
memwb_ir_q <= exmem_ir_q;
|
||||
memwb_alu_result_q <= exmem_alu_result_q;
|
||||
memwb_alu_result2_q <= exmem_alu_result2_q;
|
||||
memwb_csr_old_q <= exmem_csr_old_q;
|
||||
memwb_mem_data_q <= mem_rdata;
|
||||
memwb_rd_q <= exmem_rd_q;
|
||||
memwb_reg_write_q <= exmem_reg_write_q;
|
||||
memwb_mem_to_reg_q <= exmem_mem_to_reg_q;
|
||||
memwb_wb_sel_q <= exmem_wb_sel_q;
|
||||
|
||||
// EX/MEM 流水寄存器捕获。
|
||||
exmem_valid_q <= idex_valid_q;
|
||||
exmem_pc_q <= idex_pc_q;
|
||||
exmem_ir_q <= idex_ir_q;
|
||||
exmem_alu_result_q <= ex_alu_result;
|
||||
exmem_alu_result2_q <= ex_alu_result2;
|
||||
exmem_csr_old_q <= ex_csr_old_value;
|
||||
exmem_write_data_q <= ex_store_data;
|
||||
exmem_rd_q <= idex_rd_q;
|
||||
exmem_reg_write_q <= idex_reg_write_q;
|
||||
exmem_mem_to_reg_q <= idex_mem_to_reg_q;
|
||||
exmem_mem_write_q <= idex_mem_write_q;
|
||||
exmem_wstrb_q <= ex_store_wstrb;
|
||||
exmem_wb_sel_q <= idex_wb_sel_q;
|
||||
|
||||
// 控制优先级:中断 > URET > ecall 停机 > 分支重定向 >
|
||||
// load-use 停顿 > 正常流水推进。
|
||||
if (flush_younger || pipeline_stall || halted_q) begin
|
||||
idex_valid_q <= 1'b0;
|
||||
idex_pred_taken_q <= 1'b0;
|
||||
idex_pred_target_q <= 32'b0;
|
||||
end else begin
|
||||
idex_valid_q <= ifid_valid_q;
|
||||
idex_pc_q <= ifid_pc_q;
|
||||
idex_ir_q <= ifid_ir_q;
|
||||
idex_rs1_value_q <= d_rs1_value;
|
||||
idex_rs2_value_q <= d_rs2_value;
|
||||
idex_rs1_idx_q <= d_src1_idx;
|
||||
idex_rs2_idx_q <= d_src2_idx;
|
||||
idex_rd_q <= d_rd;
|
||||
idex_imm_q <= d_imm;
|
||||
idex_aluop_q <= d_aluop;
|
||||
idex_alu_src_q <= d_alu_src;
|
||||
idex_reg_write_q <= d_reg_write;
|
||||
idex_mem_to_reg_q <= d_mem_to_reg;
|
||||
idex_mem_write_q <= d_mem_write;
|
||||
idex_mem_byte_q <= d_mem_byte;
|
||||
idex_wb_sel_q <= d_wb_sel;
|
||||
idex_csr_addr_q <= d_csr_addr;
|
||||
idex_branch_q <= d_branch;
|
||||
idex_beq_q <= d_beq;
|
||||
idex_bne_q <= d_bne;
|
||||
idex_blt_q <= d_blt;
|
||||
idex_bge_q <= d_bge;
|
||||
idex_bltu_q <= d_bltu;
|
||||
idex_bgeu_q <= d_bgeu;
|
||||
idex_jal_q <= d_jal;
|
||||
idex_jalr_q <= d_jalr;
|
||||
idex_ecall_q <= d_ecall;
|
||||
idex_uret_q <= d_uret;
|
||||
idex_csr_set_q <= d_csr_set;
|
||||
idex_csr_clear_q <= d_csr_clear;
|
||||
idex_csr_write_q <= d_csr_write;
|
||||
idex_pred_taken_q <= ifid_pred_taken_q;
|
||||
idex_pred_target_q <= ifid_pred_target_q;
|
||||
end
|
||||
|
||||
if (take_irq) begin
|
||||
pc_q <= irq_vector;
|
||||
ifid_valid_q <= 1'b0;
|
||||
ifid_pred_taken_q <= 1'b0;
|
||||
ifid_pred_target_q <= 32'b0;
|
||||
end else if (ex_uret_redirect) begin
|
||||
pc_q <= ex_return_pc;
|
||||
ifid_valid_q <= 1'b0;
|
||||
ifid_pred_taken_q <= 1'b0;
|
||||
ifid_pred_target_q <= 32'b0;
|
||||
end else if (ex_halt_event) begin
|
||||
pc_q <= idex_pc_q + 32'd4;
|
||||
ifid_valid_q <= 1'b0;
|
||||
ifid_pred_taken_q <= 1'b0;
|
||||
ifid_pred_target_q <= 32'b0;
|
||||
end else if (ex_redirect_valid) begin
|
||||
pc_q <= ex_redirect_pc;
|
||||
ifid_valid_q <= 1'b0;
|
||||
ifid_pred_taken_q <= 1'b0;
|
||||
ifid_pred_target_q <= 32'b0;
|
||||
end else if (pipeline_stall) begin
|
||||
pc_q <= pc_q;
|
||||
ifid_valid_q <= ifid_valid_q;
|
||||
ifid_pc_q <= ifid_pc_q;
|
||||
ifid_ir_q <= ifid_ir_q;
|
||||
ifid_pred_taken_q <= ifid_pred_taken_q;
|
||||
ifid_pred_target_q <= ifid_pred_target_q;
|
||||
end else if (halted_q) begin
|
||||
pc_q <= pc_q;
|
||||
ifid_valid_q <= 1'b0;
|
||||
end else begin
|
||||
pc_q <= fetch_next_pc;
|
||||
ifid_valid_q <= 1'b1;
|
||||
ifid_pc_q <= pc_q;
|
||||
ifid_ir_q <= instr_i;
|
||||
ifid_pred_taken_q <= f_is_branch && bpb_predict_hit && bpb_predict_taken;
|
||||
ifid_pred_target_q <= bpb_predict_target;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 性能计数器(只影响统计量,不参与控制)。
|
||||
// -------------------------------------------------------------------------
|
||||
cpu21_riscv_perf_counters u_perf_counters (
|
||||
.clk (clk),
|
||||
.reset (reset),
|
||||
.inc_cycle_i (!halted_q),
|
||||
.stall_i (pipeline_stall),
|
||||
.flush_i (flush_younger),
|
||||
.cond_taken_i (idex_valid_q && idex_branch_q && ex_branch_taken),
|
||||
.cond_valid_i (idex_valid_q && idex_branch_q),
|
||||
.mispredict_i (ex_redirect_valid),
|
||||
.uncond_i (idex_valid_q && (idex_jal_q || idex_jalr_q)),
|
||||
.cycle_count_o (cycle_count_o),
|
||||
.stall_count_o (stall_count_o),
|
||||
.bubble_count_o (bubble_count_o),
|
||||
.conditional_taken_count_o (conditional_taken_count_o),
|
||||
.unconditional_branch_count_o(unconditional_branch_count_o),
|
||||
.prediction_success_count_o (prediction_success_count_o),
|
||||
.prediction_failure_count_o (prediction_failure_count_o)
|
||||
);
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,46 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 寄存器堆:32 x 32 位,x0 恒为 0。
|
||||
//
|
||||
// 读端口为组合读,并对同周期的 WB 写回做"写优先"旁路,因此 ID 级可以
|
||||
// 立刻看到正在写回的结果,无需额外的寄存器堆相关冒险处理。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_regfile (
|
||||
input wire clk,
|
||||
input wire reset,
|
||||
// 读端口(组合)
|
||||
input wire [ 4:0] rs1_idx_i,
|
||||
input wire [ 4:0] rs2_idx_i,
|
||||
output wire [31:0] rs1_data_o,
|
||||
output wire [31:0] rs2_data_o,
|
||||
// 写端口(WB 级)
|
||||
input wire we_i,
|
||||
input wire [ 4:0] waddr_i,
|
||||
input wire [31:0] wdata_i
|
||||
);
|
||||
reg [31:0] regs[0:31];
|
||||
|
||||
integer i;
|
||||
|
||||
// 写优先旁路:同周期写回且地址相同的读请求返回新值。
|
||||
wire [31:0] rs1_raw = regs[rs1_idx_i];
|
||||
wire [31:0] rs2_raw = regs[rs2_idx_i];
|
||||
|
||||
assign rs1_data_o = (rs1_idx_i == 5'd0) ? 32'b0 :
|
||||
((we_i && (waddr_i == rs1_idx_i)) ? wdata_i : rs1_raw);
|
||||
assign rs2_data_o = (rs2_idx_i == 5'd0) ? 32'b0 :
|
||||
((we_i && (waddr_i == rs2_idx_i)) ? wdata_i : rs2_raw);
|
||||
|
||||
always @(posedge clk or posedge reset) begin
|
||||
if (reset) begin
|
||||
for (i = 0; i < 32; i = i + 1) regs[i] <= 32'b0;
|
||||
end else begin
|
||||
if (we_i && (waddr_i != 5'd0)) regs[waddr_i] <= wdata_i;
|
||||
regs[0] <= 32'b0;
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,49 @@
|
||||
`timescale 1ns / 1ps
|
||||
`default_nettype none
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// EX 级存储数据对齐单元。
|
||||
//
|
||||
// 依据地址低两位生成写数据与字节写掩码:sw 输出全字 + 1111,sb 把数据的
|
||||
// 低 8 位搬到对应的字节车道,并只置位对应的写掩码。
|
||||
// -----------------------------------------------------------------------------
|
||||
module cpu21_riscv_store_unit (
|
||||
input wire [31:0] addr_i,
|
||||
input wire [31:0] data_i,
|
||||
input wire mem_write_i,
|
||||
input wire mem_byte_i,
|
||||
output reg [31:0] store_data_o,
|
||||
output reg [ 3:0] store_wstrb_o
|
||||
);
|
||||
always @* begin
|
||||
store_data_o = data_i;
|
||||
store_wstrb_o = 4'b0;
|
||||
if (mem_write_i) begin
|
||||
if (mem_byte_i) begin
|
||||
case (addr_i[1:0])
|
||||
2'd0: begin
|
||||
store_data_o = {24'b0, data_i[7:0]};
|
||||
store_wstrb_o = 4'b0001;
|
||||
end
|
||||
2'd1: begin
|
||||
store_data_o = {16'b0, data_i[7:0], 8'b0};
|
||||
store_wstrb_o = 4'b0010;
|
||||
end
|
||||
2'd2: begin
|
||||
store_data_o = {8'b0, data_i[7:0], 16'b0};
|
||||
store_wstrb_o = 4'b0100;
|
||||
end
|
||||
default: begin
|
||||
store_data_o = {data_i[7:0], 24'b0};
|
||||
store_wstrb_o = 4'b1000;
|
||||
end
|
||||
endcase
|
||||
end else begin
|
||||
store_data_o = data_i;
|
||||
store_wstrb_o = 4'b1111;
|
||||
end
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
Reference in New Issue
Block a user