Files
risc-v-cpu/cpu21_bpb_8.v
T
Anccend 266ec6a049 重构:按功能拆分流水线模块、修复 BLT 指令、注释中文化
1) 模块拆分(便于单独仿真调试)
   把原来单文件的三模块结构按功能拆成 10 个独立文件,顶层只保留五级流水
   寄存器、子模块例化、PC/IF-ID/ID-EX 控制优先级与对外输出:
     cpu21_riscv_alu.v            组合 ALU(result2 用于 MUL 高位/余数)
     cpu21_bpb_8.v                8 项全相联分支目标缓冲
     cpu21_riscv_decoder.v        ID 级组合译码器
     cpu21_riscv_regfile.v        寄存器堆(写优先旁路)
     cpu21_riscv_forward_unit.v   EX 级前递网络
     cpu21_riscv_store_unit.v     EX 级存储数据对齐(sw/sb)
     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  性能计数器
   已把上述文件加入 Vivado 工程 sources_1;端口、时序与行为经 xsim 对同一
   ROM 逐周期回归验证,统计量完全一致。

2) 修复 BLT 指令(真值表第 28 行:opcode IR[6:2]=0x18、funct3=100、ALU_OP=SLT)
   此前 funct3=100 未译码,blt 被当作空指令执行,于是
   `blt s1,zero,loop` 的循环只执行一次就顺序落到退出代码并停机
   (现象:只输出第一个值后就不再运行)。
     - cpu21_riscv_decoder.v:新增 blt_o,OP_BRANCH 接受 funct3=100,
       并按真值表给出 ALU_SLT(beq/bne 仍为 SUB,bltu 仍为 SLTU)
     - cpu21_riscv_branch_unit.v:新增 blt_i,用有符号比较
       $signed(src1) < $signed(src2) 裁决
     - cpu21_riscv_redirect_int_bpb.v:新增 idex_blt_q 流水寄存器,
       IF 级 f_is_branch 纳入 funct3=100,使 blt 也参与 BPB 预测
   验证:blt 小程序正确输出 -3/-2/-1 后停机(cond_taken=2);
   benchmark 第 [4] 段完整输出 fffffff1..ffffffff;
   中断测试程序的中断入口与停留周期与改动前一致。

3) 注释中文化
   - cpu21_riscv_redirect_int_bpb.v 及全部新增子模块使用中文注释;
   - testbench/tb_cpu21_riscv_redirect_int_bpb.v 与新增的 tb_no_intr.v
     注释全部译为中文。
   - testbench/tb_no_intr.v:与中断测试平台同框架但不注入 irq 脉冲的对照
     测试平台(修复其 $dumpvars 引用了不存在的模块名,并改用 tb_no_intr.vcd
     避免与另一个 TB 的波形文件互相覆盖)。

4) 其他
   - testbench/tb_cpu21_riscv_redirect_int_bpb.v 的默认 ROM_FILE 改为
     cpu21_riscv_redirect_int_bpb_rom.hex(中断测试程序);
   - cpu21_riscv_redirect_int_bpb说明.md 增补"文件与模块划分"章节与 BLT 说明;
   - 真值表.txt 移动到 testbench/programs/ 下。
2026-09-12 18:48:31 +08:00

118 lines
4.0 KiB
Verilog

`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