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/ 下。
77 lines
3.2 KiB
Verilog
77 lines
3.2 KiB
Verilog
`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
|