- 现在只有一份源文件,就在 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/ 临时目录。
189 lines
6.8 KiB
Verilog
189 lines
6.8 KiB
Verilog
`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
|