Files

67 lines
2.6 KiB
Verilog
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
module int_ctrl (
input wire clk,
input wire resetn,
input wire [31:0] int_en, // 中断使能1有效
input wire [31:0] int_edge, // 触发方式1边沿触发0电平触发
input wire [31:0] int_pol, // 极性选择:电平(1高/0低),边沿(1上升沿/0下降沿)
input wire [31:0] int_in, // 外部输入中断信号
input wire [31:0] int_clr, // 软件中断清除(针对边沿触发)
input wire [31:0] int_set, // 软件中断置位(针对边沿触发)
output wire [31:0] int_out // 最终输出到处理器的高电平有效中断信号
);
// 寄存输入信号以检测边沿
reg [31:0] int_in_d1;
always @(posedge clk or negedge resetn) begin
if (!resetn) begin
int_in_d1 <= 32'b0;
end else begin
int_in_d1 <= int_in;
end
end
// 生成上升沿和下降沿信号
wire [31:0] rising_edge = int_in & ~int_in_d1;
wire [31:0] falling_edge = ~int_in & int_in_d1;
// 边沿触发状态维护寄存器
reg [31:0] edge_int_state;
integer i;
always @(posedge clk or negedge resetn) begin
if (!resetn) begin
edge_int_state <= 32'b0;
end else begin
for (i = 0; i < 32; i = i + 1) begin
// 清除操作优先级最高按照基础SoC惯例写clr信号清零
if (int_clr[i]) begin
edge_int_state[i] <= 1'b0;
end
// 软件置位操作
else if (int_set[i]) begin
edge_int_state[i] <= 1'b1;
end
// 硬件检测到有效边沿并锁存
else if (int_edge[i]) begin
if (int_pol[i] && rising_edge[i]) begin
edge_int_state[i] <= 1'b1; // 上升沿触发
end else if (!int_pol[i] && falling_edge[i]) begin
edge_int_state[i] <= 1'b1; // 下降沿触发
end
end
end
end
end
// 电平触发状态评估
// 如果极性配置为1高电平触发则 int_in 为1时有效
// 如果极性配置为0低电平触发则 int_in 为0时有效
wire [31:0] level_int_state;
assign level_int_state = (int_in & int_pol) | (~int_in & ~int_pol);
// 结合 edge/level 选择,并用 en 屏蔽
assign int_out = int_en & (
(int_edge & edge_int_state) |
(~int_edge & level_int_state)
);
endmodule