2020-04-05 14:22:34 +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"
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
// CSR寄存器模块
|
2020-04-05 14:22:34 +00:00
|
|
|
module csr_reg(
|
|
|
|
|
|
|
|
input wire clk,
|
|
|
|
input wire rst,
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
// form ex
|
2020-04-25 09:13:12 +00:00
|
|
|
input wire we_i, // ex模块写寄存器标志
|
|
|
|
input wire[`MemAddrBus] raddr_i, // ex模块读寄存器地址
|
|
|
|
input wire[`MemAddrBus] waddr_i, // ex模块写寄存器地址
|
|
|
|
input wire[`RegBus] data_i, // ex模块写寄存器数据
|
2020-04-05 14:22:34 +00:00
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
// from clint
|
2020-04-25 09:13:12 +00:00
|
|
|
input wire clint_we_i, // clint模块写寄存器标志
|
|
|
|
input wire[`MemAddrBus] clint_raddr_i, // clint模块读寄存器地址
|
|
|
|
input wire[`MemAddrBus] clint_waddr_i, // clint模块写寄存器地址
|
|
|
|
input wire[`RegBus] clint_data_i, // clint模块写寄存器数据
|
|
|
|
|
|
|
|
output wire global_int_en_o, // 全局中断使能标志
|
2020-04-11 11:03:49 +00:00
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
// to clint
|
2020-04-25 09:13:12 +00:00
|
|
|
output reg[`RegBus] clint_data_o, // clint模块读寄存器数据
|
|
|
|
output wire[`RegBus] clint_csr_mtvec, // mtvec
|
|
|
|
output wire[`RegBus] clint_csr_mepc, // mepc
|
|
|
|
output wire[`RegBus] clint_csr_mstatus, // mstatus
|
2020-04-18 12:14:37 +00:00
|
|
|
|
|
|
|
// to ex
|
2020-04-25 09:13:12 +00:00
|
|
|
output reg[`RegBus] data_o // ex模块读寄存器数据
|
2020-04-05 14:22:34 +00:00
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2020-04-11 11:03:49 +00:00
|
|
|
reg[`DoubleRegBus] cycle;
|
|
|
|
reg[`RegBus] mtvec;
|
|
|
|
reg[`RegBus] mcause;
|
2020-04-18 03:22:20 +00:00
|
|
|
reg[`RegBus] mepc;
|
2020-04-25 09:13:12 +00:00
|
|
|
reg[`RegBus] mie;
|
|
|
|
reg[`RegBus] mstatus;
|
|
|
|
|
|
|
|
|
|
|
|
assign global_int_en_o = (mstatus[3] == 1'b1)? `True: `False;
|
|
|
|
|
|
|
|
assign clint_csr_mtvec = mtvec;
|
|
|
|
assign clint_csr_mepc = mepc;
|
|
|
|
assign clint_csr_mstatus = mstatus;
|
2020-04-05 14:22:34 +00:00
|
|
|
|
|
|
|
|
2020-04-11 11:03:49 +00:00
|
|
|
// cycle counter
|
2020-04-18 12:14:37 +00:00
|
|
|
// 复位撤销后就一直计数
|
2020-04-05 14:22:34 +00:00
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (rst == `RstEnable) begin
|
2020-04-11 11:03:49 +00:00
|
|
|
cycle <= {`ZeroWord, `ZeroWord};
|
2020-04-05 14:22:34 +00:00
|
|
|
end else begin
|
|
|
|
cycle <= cycle + 1'b1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-11 11:03:49 +00:00
|
|
|
// write reg
|
2020-04-18 12:14:37 +00:00
|
|
|
// 写寄存器操作
|
2020-04-11 11:03:49 +00:00
|
|
|
always @ (posedge clk) begin
|
|
|
|
if (rst == `RstEnable) begin
|
|
|
|
mtvec <= `ZeroWord;
|
|
|
|
mcause <= `ZeroWord;
|
2020-04-18 03:22:20 +00:00
|
|
|
mepc <= `ZeroWord;
|
2020-04-25 09:13:12 +00:00
|
|
|
mie <= `ZeroWord;
|
|
|
|
mstatus <= `ZeroWord;
|
2020-04-11 11:03:49 +00:00
|
|
|
end else begin
|
2020-04-18 12:14:37 +00:00
|
|
|
// 优先响应ex模块的写操作
|
2020-04-11 11:03:49 +00:00
|
|
|
if (we_i == `WriteEnable) begin
|
|
|
|
case (waddr_i[11:0])
|
|
|
|
`CSR_MTVEC: begin
|
|
|
|
mtvec <= data_i;
|
|
|
|
end
|
|
|
|
`CSR_MCAUSE: begin
|
|
|
|
mcause <= data_i;
|
|
|
|
end
|
2020-04-18 03:22:20 +00:00
|
|
|
`CSR_MEPC: begin
|
|
|
|
mepc <= data_i;
|
|
|
|
end
|
2020-04-25 09:13:12 +00:00
|
|
|
`CSR_MIE: begin
|
|
|
|
mie <= data_i;
|
|
|
|
end
|
|
|
|
`CSR_MSTATUS: begin
|
|
|
|
mstatus <= data_i;
|
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
default: begin
|
|
|
|
|
|
|
|
end
|
|
|
|
endcase
|
2020-04-18 12:14:37 +00:00
|
|
|
// clint模块写操作
|
2020-04-11 11:03:49 +00:00
|
|
|
end else if (clint_we_i == `WriteEnable) begin
|
|
|
|
case (clint_waddr_i[11:0])
|
|
|
|
`CSR_MTVEC: begin
|
|
|
|
mtvec <= clint_data_i;
|
|
|
|
end
|
|
|
|
`CSR_MCAUSE: begin
|
|
|
|
mcause <= clint_data_i;
|
|
|
|
end
|
2020-04-18 03:22:20 +00:00
|
|
|
`CSR_MEPC: begin
|
|
|
|
mepc <= clint_data_i;
|
|
|
|
end
|
2020-04-25 09:13:12 +00:00
|
|
|
`CSR_MIE: begin
|
|
|
|
mie <= clint_data_i;
|
|
|
|
end
|
|
|
|
`CSR_MSTATUS: begin
|
|
|
|
mstatus <= clint_data_i;
|
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
default: begin
|
|
|
|
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-05 14:22:34 +00:00
|
|
|
// read reg
|
2020-04-18 12:14:37 +00:00
|
|
|
// ex模块读CSR寄存器
|
2020-04-05 14:22:34 +00:00
|
|
|
always @ (*) begin
|
|
|
|
if (rst == `RstEnable) begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = `ZeroWord;
|
2020-04-05 14:22:34 +00:00
|
|
|
end else begin
|
|
|
|
case (raddr_i[11:0])
|
2020-04-11 11:03:49 +00:00
|
|
|
`CSR_CYCLE: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = cycle[31:0];
|
2020-04-05 14:22:34 +00:00
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
`CSR_CYCLEH: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = cycle[63:32];
|
2020-04-05 14:22:34 +00:00
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
`CSR_MTVEC: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = mtvec;
|
2020-04-11 11:03:49 +00:00
|
|
|
end
|
|
|
|
`CSR_MCAUSE: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = mcause;
|
2020-04-11 11:03:49 +00:00
|
|
|
end
|
2020-04-18 03:22:20 +00:00
|
|
|
`CSR_MEPC: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = mepc;
|
2020-04-18 03:22:20 +00:00
|
|
|
end
|
2020-04-25 09:13:12 +00:00
|
|
|
`CSR_MIE: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = mie;
|
2020-04-25 09:13:12 +00:00
|
|
|
end
|
|
|
|
`CSR_MSTATUS: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = mstatus;
|
2020-04-25 09:13:12 +00:00
|
|
|
end
|
2020-04-05 14:22:34 +00:00
|
|
|
default: begin
|
2020-05-02 03:58:44 +00:00
|
|
|
data_o = `ZeroWord;
|
2020-04-05 14:22:34 +00:00
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-11 11:03:49 +00:00
|
|
|
// read reg
|
2020-04-18 12:14:37 +00:00
|
|
|
// clint模块读CSR寄存器
|
2020-04-11 11:03:49 +00:00
|
|
|
always @ (*) begin
|
|
|
|
if (rst == `RstEnable) begin
|
|
|
|
clint_data_o <= `ZeroWord;
|
|
|
|
end else begin
|
|
|
|
case (clint_raddr_i[11:0])
|
|
|
|
`CSR_CYCLE: begin
|
|
|
|
clint_data_o <= cycle[31:0];
|
|
|
|
end
|
|
|
|
`CSR_CYCLEH: begin
|
|
|
|
clint_data_o <= cycle[63:32];
|
|
|
|
end
|
|
|
|
`CSR_MTVEC: begin
|
|
|
|
clint_data_o <= mtvec;
|
|
|
|
end
|
|
|
|
`CSR_MCAUSE: begin
|
|
|
|
clint_data_o <= mcause;
|
|
|
|
end
|
2020-04-18 03:22:20 +00:00
|
|
|
`CSR_MEPC: begin
|
|
|
|
clint_data_o <= mepc;
|
|
|
|
end
|
2020-04-25 09:13:12 +00:00
|
|
|
`CSR_MIE: begin
|
|
|
|
clint_data_o <= mie;
|
|
|
|
end
|
|
|
|
`CSR_MSTATUS: begin
|
|
|
|
clint_data_o <= mstatus;
|
|
|
|
end
|
2020-04-11 11:03:49 +00:00
|
|
|
default: begin
|
|
|
|
clint_data_o <= `ZeroWord;
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-05 14:22:34 +00:00
|
|
|
endmodule
|