Files
risc-v-cpu/pipelined-cpu/srcs/rtl/cpu21_bpb_8.v
T
Anccend cb0cb433ac 源文件收敛到 pipelined-cpu/ 一份:删除根目录 rtl/board/testbench/tools,补充回归脚本
- 现在只有一份源文件,就在 pipelined-cpu/ 下:
    srcs/rtl(13 个处理器 RTL)、srcs/board、srcs/constrs、srcs/tb、srcs/rom、
    tools/(rv_asm.py、hex2vh.py、run_regress.ps1)、programs/(汇编源码与期望值)。
  仓库根目录的 rtl/、board/、testbench/、tools/ 已删除(删除前逐个文件比对过 MD5,
  与工程目录里的副本完全一致),说明文档仍放在最外层
  cpu21_riscv_redirect_int_bpb说明.md。
- .gitignore 改为:忽略 pipelined-cpu/ 下的工程生成物(*.xpr、*.runs、*.sim、*.cache…),
  只跟踪 srcs/、tools/、programs/、create_project.tcl;_tmp/、_vivado/ 等本地临时目录
  继续忽略。
- 新增 tools/run_regress.ps1:一键回归脚本。编译 srcs/rtl/ → 跑 muldiv/isa/lui/mmio
  四个指令测试并与 programs/*.expected 逐值比对 → 跑两个流水基线 → 跑板级顶层
  (-d BOARD_ROM_FAST)检查跑马灯,最后汇总 PASS/FAIL 并返回失败数作为退出码。
  编译与仿真的中间文件写在 %TEMP%\cpu21_regress,不污染工程目录。实测全部通过。
- 新增 programs/board_demo_fast.asm:快速版演示程序(延时初值 64,约 2.6 µs/步),
  对应 srcs/board/cpu21_board_rom_fast.vh;仿真时用 -d BOARD_ROM_FAST 才会选到它。
- 两个测试平台增加 srcs/rom 探测路径,工程目录内直接 Run Simulation 即可加载镜像。
- 文档:目录结构一节按新布局重写;新增“回归测试”一节(脚本用法 + 期望基线数字);
  上板章节的生成/仿真命令改为相对工程目录(python tools/rv_asm.py programs/…、
  xvlog -d BOARD_ROM_FAST srcs/rtl/*.v …);tools 里的用法示例同步更新;
  “工程分享”一节去掉已不存在的副本同步说明。
- 清理:删除本地 _tmp/(约 57 MB)、_vivado/、.Xil/ 临时目录。
2026-09-13 19:09:36 +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