2021-01-15 04:37:51 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// csrc.sv
|
|
|
|
//
|
|
|
|
// Written: David_Harris@hmc.edu 9 January 2021
|
2021-03-04 16:40:18 +00:00
|
|
|
// Modified:ssanghai@hmc.edu 2nd March
|
|
|
|
// Added a configurable number of counters
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
|
|
|
// Purpose: Counter CSRs
|
|
|
|
// See RISC-V Privileged Mode Specification 20190608 3.1.10-11
|
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
|
|
|
|
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
|
|
|
// is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
|
|
|
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
2021-01-23 15:48:12 +00:00
|
|
|
`include "wally-config.vh"
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
module csrc (
|
2021-02-02 04:44:41 +00:00
|
|
|
input logic clk, reset,
|
2021-03-24 20:56:55 +00:00
|
|
|
input logic StallD, StallE, StallM, StallW,
|
2021-03-23 18:25:51 +00:00
|
|
|
input logic InstrValidW, LoadStallD, CSRMWriteM, BPPredWrongM,
|
2021-03-24 20:56:55 +00:00
|
|
|
input logic [3:0] InstrClassM,
|
2021-02-02 04:44:41 +00:00
|
|
|
input logic [11:0] CSRAdrM,
|
|
|
|
input logic [1:0] PrivilegeModeW,
|
2021-01-23 15:48:12 +00:00
|
|
|
input logic [`XLEN-1:0] CSRWriteValM,
|
2021-02-02 04:44:41 +00:00
|
|
|
input logic [31:0] MCOUNTINHIBIT_REGW, MCOUNTEREN_REGW, SCOUNTEREN_REGW,
|
2021-01-23 15:48:12 +00:00
|
|
|
output logic [`XLEN-1:0] CSRCReadValM,
|
2021-03-04 16:40:18 +00:00
|
|
|
output logic IllegalCSRCAccessM);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
// create Counter arrays to store address of each counter
|
|
|
|
integer MHPMCOUNTER [`COUNTERS:0];
|
|
|
|
integer MHPMCOUNTERH [`COUNTERS:0];
|
|
|
|
integer HPMCOUNTER [`COUNTERS:0];
|
|
|
|
integer HPMCOUNTERH [`COUNTERS:0];
|
|
|
|
integer MHPEVENT [`COUNTERS:0];
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
initial begin
|
|
|
|
integer i;
|
|
|
|
for (i=0; i<= `COUNTERS; i = i+1) begin
|
|
|
|
if (i !==1) begin
|
|
|
|
MHPMCOUNTER[i] = 12'hB00 + i; // not sure this addition is legit
|
2021-03-04 17:59:45 +00:00
|
|
|
MHPMCOUNTERH[i] = 12'hB80 + i;
|
2021-03-04 16:40:18 +00:00
|
|
|
HPMCOUNTER[i] = 12'hC00 + i;
|
|
|
|
HPMCOUNTERH[i] = 12'hC80 + i;
|
|
|
|
MHPEVENT[i] = 12'h320 + i; // MHPEVENT[0] = MCOUNTERINHIBIT
|
|
|
|
end
|
|
|
|
end //end for loop
|
|
|
|
end // end for initial
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
logic [`COUNTERS:0] MCOUNTEN;
|
|
|
|
assign MCOUNTEN[0] = 1'b1;
|
|
|
|
assign MCOUNTEN[1] = 1'b0;
|
2021-03-24 20:56:55 +00:00
|
|
|
assign MCOUNTEN[2] = InstrValidW & ~StallW;
|
|
|
|
assign MCOUNTEN[3] = LoadStallD & ~StallD;
|
|
|
|
assign MCOUNTEN[4] = BPPredWrongM & ~StallM;
|
|
|
|
assign MCOUNTEN[5] = InstrClassM[0] & ~StallM;
|
2021-03-23 18:25:51 +00:00
|
|
|
assign MCOUNTEN[`COUNTERS:6] = 0;
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
genvar j;
|
|
|
|
generate
|
|
|
|
if (`ZCOUNTERS_SUPPORTED) begin
|
|
|
|
logic [`COUNTERS:0][63:0] HPMCOUNTER_REGW;
|
|
|
|
logic [`COUNTERS:0][63:0] HPMCOUNTERPlusM;
|
|
|
|
logic [`COUNTERS:0][`XLEN-1:0] NextHPMCOUNTERM;
|
|
|
|
logic [`COUNTERS:0] WriteHPMCOUNTERM;
|
|
|
|
logic [4:0] CounterNumM;
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 17:59:45 +00:00
|
|
|
assign CounterNumM = CSRAdrM[4:0]; // which counter to read? ***
|
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
for (j=0; j<= `COUNTERS; j = j+1) begin
|
|
|
|
// Write enables
|
|
|
|
if (j !==1) begin
|
|
|
|
assign WriteHPMCOUNTERM[j] = CSRMWriteM && (CSRAdrM == MHPMCOUNTER[j]);
|
|
|
|
// Count Signals
|
|
|
|
assign HPMCOUNTERPlusM[j] = HPMCOUNTER_REGW[j] + {63'b0, MCOUNTEN[j] & ~MCOUNTINHIBIT_REGW[j]};
|
|
|
|
|
|
|
|
assign NextHPMCOUNTERM[j] = WriteHPMCOUNTERM[j] ? CSRWriteValM : HPMCOUNTERPlusM[j][`XLEN-1:0];
|
|
|
|
end
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
// Write / update counters
|
|
|
|
// Only the Machine mode versions of the counter CSRs are writable
|
|
|
|
if (`XLEN==64) begin // 64-bit counters
|
2021-03-24 20:56:55 +00:00
|
|
|
flopenr #(64) HPMCOUNTERreg_j(clk, reset, ~StallW, NextHPMCOUNTERM[j], HPMCOUNTER_REGW[j]);
|
2021-03-04 16:40:18 +00:00
|
|
|
end
|
|
|
|
else begin // 32-bit low and high counters
|
|
|
|
logic [`COUNTERS:0] WriteHPMCOUNTERHM;
|
|
|
|
logic [`COUNTERS:0] [`XLEN-1:0] NextHPMCOUNTERHM;
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
// Write Enables
|
|
|
|
assign WriteHPMCOUNTERHM[j] = CSRMWriteM && (CSRAdrM == MHPMCOUNTERH[j]);
|
|
|
|
assign NextHPMCOUNTERHM[j] = WriteHPMCOUNTERHM[j] ? CSRWriteValM : HPMCOUNTERPlusM[j][63:32];
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
// Counter CSRs
|
2021-03-24 20:56:55 +00:00
|
|
|
flopenr #(32) HPMCOUNTERreg_j(clk, reset, ~StallW, NextHPMCOUNTERM[j], HPMCOUNTER_REGW[j][31:0]);
|
|
|
|
flopenr #(32) HPMCOUNTERHreg_j(clk, reset, ~StallW, NextHPMCOUNTERHM[j], HPMCOUNTER_REGW[j][63:32]);
|
2021-01-15 04:37:51 +00:00
|
|
|
end
|
2021-03-04 17:59:45 +00:00
|
|
|
end // end for
|
|
|
|
|
|
|
|
// eventually move TIME and TIMECMP to the CLINT
|
|
|
|
// run TIME off asynchronous reference clock
|
|
|
|
// synchronize write enable to TIME
|
|
|
|
// four phase handshake to synchronize reads from TIME
|
|
|
|
|
|
|
|
// interrupt on timer compare
|
|
|
|
// ability to disable optional CSRs
|
|
|
|
|
|
|
|
// Read Counters, or cause excepiton if insufficient privilege in light of COUNTEREN flags
|
|
|
|
if (`XLEN==64) begin // 64-bit counter reads
|
|
|
|
always_comb
|
|
|
|
if (PrivilegeModeW == `M_MODE ||
|
|
|
|
MCOUNTEREN_REGW[CounterNumM] && (PrivilegeModeW == `S_MODE || SCOUNTEREN_REGW[CounterNumM])) begin
|
2021-03-04 16:40:18 +00:00
|
|
|
|
2021-03-04 17:59:45 +00:00
|
|
|
if (CSRAdrM[11:5] == MHPMCOUNTER[0][11:5] || CSRAdrM[11:5] == HPMCOUNTER[0][11:5]) begin
|
|
|
|
CSRCReadValM = HPMCOUNTER_REGW[CSRAdrM[4:0]];
|
|
|
|
IllegalCSRCAccessM = 0;
|
2021-03-04 16:40:18 +00:00
|
|
|
end
|
2021-03-04 17:59:45 +00:00
|
|
|
// //case (CSRAdrM)
|
|
|
|
// MHPMCOUNTER[j]: CSRCReadValM = HPMCOUNTER_REGW[j];
|
|
|
|
// HPMCOUNTER[j]: CSRCReadValM = HPMCOUNTER_REGW[j];
|
|
|
|
// default: begin
|
|
|
|
// CSRCReadValM = 0;
|
|
|
|
// IllegalCSRCAccessM = 1;
|
|
|
|
// end
|
|
|
|
// endcase
|
|
|
|
// end
|
2021-03-04 16:40:18 +00:00
|
|
|
else begin
|
|
|
|
IllegalCSRCAccessM = 1; // no privileges for this csr
|
|
|
|
CSRCReadValM = 0;
|
2021-03-04 17:59:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
IllegalCSRCAccessM = 1; // no privileges for this csr
|
|
|
|
CSRCReadValM = 0;
|
2021-01-15 04:37:51 +00:00
|
|
|
end
|
2021-03-04 17:59:45 +00:00
|
|
|
end
|
|
|
|
else begin // 32-bit counter reads
|
|
|
|
always_comb
|
|
|
|
if (PrivilegeModeW == `M_MODE ||
|
|
|
|
MCOUNTEREN_REGW[CounterNumM] && (PrivilegeModeW == `S_MODE || SCOUNTEREN_REGW[CounterNumM])) begin
|
|
|
|
|
|
|
|
if (CSRAdrM[11:5] == MHPMCOUNTER[0][11:5] || CSRAdrM[11:5] == HPMCOUNTER[0][11:5] ||
|
|
|
|
CSRAdrM[11:5] == MHPMCOUNTERH[0][11:5] || CSRAdrM[11:5] == HPMCOUNTERH[0][11:5]) begin
|
|
|
|
CSRCReadValM = HPMCOUNTER_REGW[CSRAdrM[4:0]];
|
|
|
|
IllegalCSRCAccessM = 0;
|
2021-03-04 16:40:18 +00:00
|
|
|
end
|
2021-03-04 17:59:45 +00:00
|
|
|
|
2021-03-04 16:40:18 +00:00
|
|
|
else begin
|
|
|
|
IllegalCSRCAccessM = 1; // no privileges for this csr
|
|
|
|
CSRCReadValM = 0;
|
2021-03-04 17:59:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
// IllegalCSRCAccessM = 0;
|
|
|
|
// case (CSRAdrM)
|
|
|
|
// MHPMCOUNTER[j]: CSRCReadValM = HPMCOUNTER_REGW[j][31:0];
|
|
|
|
// HPMCOUNTER[j]: CSRCReadValM = HPMCOUNTER_REGW[j][31:0];
|
|
|
|
// MHPMCOUNTERH[j]: CSRCReadValM = HPMCOUNTER_REGW[j][63:32];
|
|
|
|
// HPMCOUNTERH[j]: CSRCReadValM = HPMCOUNTER_REGW[j][63:32];
|
|
|
|
// default: begin
|
|
|
|
// CSRCReadValM = 0;
|
|
|
|
// IllegalCSRCAccessM = 1;
|
|
|
|
// end
|
|
|
|
// endcase
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
IllegalCSRCAccessM = 1; // no privileges for this csr
|
|
|
|
CSRCReadValM = 0;
|
|
|
|
end
|
|
|
|
end // 32-bit counter end
|
|
|
|
end // end for big if
|
2021-03-04 16:40:18 +00:00
|
|
|
else begin
|
2021-03-04 17:59:45 +00:00
|
|
|
assign CSRCReadValM = 0;
|
|
|
|
assign IllegalCSRCAccessM = 1;
|
2021-03-04 16:40:18 +00:00
|
|
|
end // end for else
|
|
|
|
endgenerate
|
2021-01-15 04:37:51 +00:00
|
|
|
endmodule
|
|
|
|
|