2020-03-29 15:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2020 Blue Liang, liangkangnan@163.com
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
`include "defines.v"
|
|
|
|
|
|
|
|
|
|
// core local interruptor module
|
2020-04-18 03:21:09 +00:00
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٲ<EFBFBD>ģ<EFBFBD><EFBFBD>
|
2020-03-29 15:19:14 +00:00
|
|
|
|
module clint(
|
|
|
|
|
|
|
|
|
|
input wire clk,
|
2020-10-23 13:26:18 +00:00
|
|
|
|
input wire rst_n,
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// from core
|
2020-10-23 13:26:18 +00:00
|
|
|
|
input wire[`INT_WIDTH-1:0] int_flag_i, // <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
|
2020-04-18 03:21:09 +00:00
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// from exu
|
|
|
|
|
input wire inst_ecall_i, // ecallָ<EFBFBD><EFBFBD>
|
|
|
|
|
input wire inst_ebreak_i, // ebreakָ<EFBFBD><EFBFBD>
|
|
|
|
|
input wire inst_mret_i, // mretָ<EFBFBD><EFBFBD>
|
|
|
|
|
input wire[31:0] inst_addr_i, // ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2020-07-25 08:23:45 +00:00
|
|
|
|
input wire jump_flag_i,
|
2020-04-18 03:21:09 +00:00
|
|
|
|
|
|
|
|
|
// from csr_reg
|
2020-10-23 13:26:18 +00:00
|
|
|
|
input wire[31:0] csr_mtvec_i, // mtvec<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
input wire[31:0] csr_mepc_i, // mepc<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
input wire[31:0] csr_mstatus_i, // mstatus<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-18 03:21:09 +00:00
|
|
|
|
// to csr_reg
|
2020-10-23 13:26:18 +00:00
|
|
|
|
output reg csr_we_o, // дCSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|
|
|
|
output reg[31:0] csr_waddr_o, // дCSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
output reg[31:0] csr_wdata_o, // дCSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-04-11 11:03:49 +00:00
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// to pipe_ctrl
|
|
|
|
|
output wire stall_flag_o, // <EFBFBD><EFBFBD>ˮ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD>־
|
|
|
|
|
output wire[31:0] int_addr_o, // <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ַ
|
|
|
|
|
output wire int_assert_o // <EFBFBD>жϱ<EFBFBD>־
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// <EFBFBD>ж<EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-08-15 08:05:06 +00:00
|
|
|
|
localparam S_INT_IDLE = 4'b0001;
|
|
|
|
|
localparam S_INT_SYNC_ASSERT = 4'b0010;
|
|
|
|
|
localparam S_INT_ASYNC_ASSERT = 4'b0100;
|
|
|
|
|
localparam S_INT_MRET = 4'b1000;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// дCSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
localparam S_CSR_IDLE = 5'b00001;
|
|
|
|
|
localparam S_CSR_MSTATUS = 5'b00010;
|
|
|
|
|
localparam S_CSR_MEPC = 5'b00100;
|
|
|
|
|
localparam S_CSR_MSTATUS_MRET = 5'b01000;
|
|
|
|
|
localparam S_CSR_MCAUSE = 5'b10000;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-25 09:03:13 +00:00
|
|
|
|
reg[3:0] int_state;
|
|
|
|
|
reg[4:0] csr_state;
|
2020-10-23 13:26:18 +00:00
|
|
|
|
reg[31:0] inst_addr;
|
2020-04-25 09:03:13 +00:00
|
|
|
|
reg[31:0] cause;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
wire global_int_en = csr_mstatus_i[3];
|
|
|
|
|
|
|
|
|
|
assign stall_flag_o = ((int_state != S_INT_IDLE) | (csr_state != S_CSR_IDLE))? 1'b1: 1'b0;
|
2020-04-25 09:03:13 +00:00
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
wire pc_state_jump_flag;
|
|
|
|
|
gen_rst_0_dff #(1) pc_state_dff(clk, rst_n, jump_flag_i, pc_state_jump_flag);
|
|
|
|
|
|
|
|
|
|
wire if_state_jump_flag;
|
|
|
|
|
gen_rst_0_dff #(1) if_state_dff(clk, rst_n, pc_state_jump_flag, if_state_jump_flag);
|
|
|
|
|
|
|
|
|
|
wire id_state_jump_flag;
|
|
|
|
|
gen_rst_0_dff #(1) id_state_dff(clk, rst_n, if_state_jump_flag, id_state_jump_flag);
|
|
|
|
|
|
|
|
|
|
wire ex_state_jump_flag;
|
|
|
|
|
gen_rst_0_dff #(1) ex_state_dff(clk, rst_n, id_state_jump_flag, ex_state_jump_flag);
|
|
|
|
|
|
|
|
|
|
wire[3:0] state_jump_flag = {pc_state_jump_flag, if_state_jump_flag, id_state_jump_flag, ex_state_jump_flag};
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˮ<EFBFBD><EFBFBD>û<EFBFBD>г<EFBFBD>ˢ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ж<EFBFBD>
|
|
|
|
|
wire inst_addr_valid = (~(|state_jump_flag)) | ex_state_jump_flag;
|
2020-04-25 09:03:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <EFBFBD>ж<EFBFBD><EFBFBD>ٲ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
always @ (*) begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// ͬ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
if (inst_ecall_i | inst_ebreak_i) begin
|
|
|
|
|
int_state = S_INT_SYNC_ASSERT;
|
|
|
|
|
// <EFBFBD>첽<EFBFBD>ж<EFBFBD>
|
|
|
|
|
end else if ((int_flag_i != `INT_NONE) & global_int_en & inst_addr_valid) begin
|
|
|
|
|
int_state = S_INT_ASYNC_ASSERT;
|
|
|
|
|
// <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
end else if (inst_mret_i) begin
|
|
|
|
|
int_state = S_INT_MRET;
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>Ӧ
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end else begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
int_state = S_INT_IDLE;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// дCSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>л<EFBFBD>
|
2020-10-23 13:26:18 +00:00
|
|
|
|
always @ (posedge clk or negedge rst_n) begin
|
|
|
|
|
if (!rst_n) begin
|
2020-04-25 09:03:13 +00:00
|
|
|
|
csr_state <= S_CSR_IDLE;
|
2020-10-23 13:26:18 +00:00
|
|
|
|
cause <= 32'h0;
|
|
|
|
|
inst_addr <= 32'h0;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end else begin
|
2020-04-25 09:03:13 +00:00
|
|
|
|
case (csr_state)
|
|
|
|
|
S_CSR_IDLE: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
case (int_state)
|
|
|
|
|
// ͬ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
S_INT_SYNC_ASSERT: begin
|
|
|
|
|
csr_state <= S_CSR_MEPC;
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ὣ<EFBFBD>жϷ<EFBFBD><EFBFBD>ص<EFBFBD>ַ<EFBFBD><EFBFBD>4
|
2020-07-25 14:15:03 +00:00
|
|
|
|
inst_addr <= inst_addr_i;
|
2020-10-23 13:26:18 +00:00
|
|
|
|
cause <= inst_ebreak_i? 32'd3:
|
|
|
|
|
inst_ecall_i? 32'd11:
|
|
|
|
|
32'd10;
|
2020-07-25 14:15:03 +00:00
|
|
|
|
end
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// <EFBFBD>첽<EFBFBD>ж<EFBFBD>
|
|
|
|
|
S_INT_ASYNC_ASSERT: begin
|
|
|
|
|
csr_state <= S_CSR_MEPC;
|
2020-07-25 08:23:45 +00:00
|
|
|
|
inst_addr <= inst_addr_i;
|
2020-10-23 13:26:18 +00:00
|
|
|
|
// <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
cause <= 32'h80000004;
|
|
|
|
|
end
|
|
|
|
|
// <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
S_INT_MRET: begin
|
|
|
|
|
csr_state <= S_CSR_MSTATUS_MRET;
|
2020-07-25 08:23:45 +00:00
|
|
|
|
end
|
2020-10-23 13:26:18 +00:00
|
|
|
|
endcase
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
2020-04-25 09:03:13 +00:00
|
|
|
|
S_CSR_MEPC: begin
|
|
|
|
|
csr_state <= S_CSR_MSTATUS;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
2020-04-25 09:03:13 +00:00
|
|
|
|
S_CSR_MSTATUS: begin
|
2020-07-25 08:23:45 +00:00
|
|
|
|
csr_state <= S_CSR_MCAUSE;
|
|
|
|
|
end
|
|
|
|
|
S_CSR_MCAUSE: begin
|
2020-04-25 09:03:13 +00:00
|
|
|
|
csr_state <= S_CSR_IDLE;
|
|
|
|
|
end
|
|
|
|
|
S_CSR_MSTATUS_MRET: begin
|
|
|
|
|
csr_state <= S_CSR_IDLE;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
|
|
|
|
default: begin
|
2020-04-25 09:03:13 +00:00
|
|
|
|
csr_state <= S_CSR_IDLE;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>ź<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>CSR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
2020-10-23 13:26:18 +00:00
|
|
|
|
always @ (posedge clk or negedge rst_n) begin
|
|
|
|
|
if (!rst_n) begin
|
|
|
|
|
csr_we_o <= 1'b0;
|
|
|
|
|
csr_waddr_o <= 32'h0;
|
|
|
|
|
csr_wdata_o <= 32'h0;
|
2020-04-11 11:03:49 +00:00
|
|
|
|
end else begin
|
2020-04-25 09:03:13 +00:00
|
|
|
|
case (csr_state)
|
|
|
|
|
// <EFBFBD><EFBFBD>mepc<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>ǰָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
S_CSR_MEPC: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
csr_we_o <= 1'b1;
|
|
|
|
|
csr_waddr_o <= {20'h0, `CSR_MEPC};
|
|
|
|
|
csr_wdata_o <= inst_addr;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// д<EFBFBD>жϲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>
|
|
|
|
|
S_CSR_MCAUSE: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
csr_we_o <= 1'b1;
|
|
|
|
|
csr_waddr_o <= {20'h0, `CSR_MCAUSE};
|
|
|
|
|
csr_wdata_o <= cause;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// <EFBFBD>ر<EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
S_CSR_MSTATUS: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
csr_we_o <= 1'b1;
|
|
|
|
|
csr_waddr_o <= {20'h0, `CSR_MSTATUS};
|
|
|
|
|
csr_wdata_o <= {csr_mstatus_i[31:4], 1'b0, csr_mstatus_i[2:0]};
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
2020-04-25 09:03:13 +00:00
|
|
|
|
// <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
S_CSR_MSTATUS_MRET: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
csr_we_o <= 1'b1;
|
|
|
|
|
csr_waddr_o <= {20'h0, `CSR_MSTATUS};
|
|
|
|
|
csr_wdata_o <= {csr_mstatus_i[31:4], csr_mstatus_i[7], csr_mstatus_i[2:0]};
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
|
|
|
|
default: begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
csr_we_o <= 1'b0;
|
|
|
|
|
csr_waddr_o <= 32'h0;
|
|
|
|
|
csr_wdata_o <= 32'h0;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end
|
|
|
|
|
endcase
|
2020-04-11 11:03:49 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
reg in_int_context;
|
|
|
|
|
|
|
|
|
|
always @ (posedge clk or negedge rst_n) begin
|
|
|
|
|
if (!rst_n) begin
|
|
|
|
|
in_int_context <= 1'b0;
|
2020-04-18 03:21:09 +00:00
|
|
|
|
end else begin
|
2020-10-23 13:26:18 +00:00
|
|
|
|
if (csr_state == S_CSR_MSTATUS_MRET) begin
|
|
|
|
|
in_int_context <= 1'b0;
|
|
|
|
|
end else if (csr_state != S_CSR_IDLE) begin
|
|
|
|
|
in_int_context <= 1'b1;
|
|
|
|
|
end
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-23 13:26:18 +00:00
|
|
|
|
assign int_assert_o = (csr_state == S_CSR_MCAUSE) | (csr_state == S_CSR_MSTATUS_MRET);
|
|
|
|
|
assign int_addr_o = (csr_state == S_CSR_MCAUSE)? csr_mtvec_i:
|
|
|
|
|
(csr_state == S_CSR_MSTATUS_MRET)? csr_mepc_i:
|
|
|
|
|
32'h0;
|
|
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
|
endmodule
|