2021-01-15 04:37:51 +00:00
///////////////////////////////////////////
// csri.sv
//
// Written: David_Harris@hmc.edu 9 January 2021
// Modified:
//
// Purpose: Interrupt Control & Status Registers (IP, EI)
// See RISC-V Privileged Mode Specification 20190608 & 20210108 draft
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
2022-01-07 12:58:40 +00:00
// MIT LICENSE
// 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:
2021-01-15 04:37:51 +00:00
//
2022-01-07 12:58:40 +00:00
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
2021-01-15 04:37:51 +00:00
//
2022-01-07 12:58:40 +00:00
// 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-15 04:37:51 +00:00
2021-01-23 15:48:12 +00:00
`include " wally-config.vh "
2021-01-15 04:37:51 +00:00
2021-01-23 15:48:12 +00:00
module csri # ( parameter
2021-01-15 04:37:51 +00:00
// Machine CSRs
MIE = 12 'h304 ,
MIP = 12 'h344 ,
SIE = 12 'h104 ,
SIP = 12 'h144 ) (
2021-02-02 04:44:41 +00:00
input logic clk , reset ,
2022-01-20 22:39:54 +00:00
input logic InstrValidNotFlushedM , StallW ,
2021-02-02 04:44:41 +00:00
input logic CSRMWriteM , CSRSWriteM ,
input logic [ 11 : 0 ] CSRAdrM ,
2022-03-25 00:08:10 +00:00
input logic ExtIntM , ExtIntS , TimerIntM , SwIntM ,
2021-06-11 03:47:32 +00:00
input logic [ `XLEN - 1 : 0 ] MIDELEG_REGW ,
2021-02-02 04:44:41 +00:00
output logic [ 11 : 0 ] MIP_REGW , MIE_REGW , SIP_REGW , SIE_REGW ,
2021-01-23 15:48:12 +00:00
input logic [ `XLEN - 1 : 0 ] CSRWriteValM
2021-01-15 04:37:51 +00:00
) ;
2021-04-15 13:05:53 +00:00
logic [ 9 : 0 ] IP_REGW_writeable ;
2021-01-15 04:37:51 +00:00
logic [ 11 : 0 ] IntInM , IP_REGW , IE_REGW ;
logic [ 11 : 0 ] MIP_WRITE_MASK , SIP_WRITE_MASK ;
logic WriteMIPM , WriteMIEM , WriteSIPM , WriteSIEM ;
// Determine which interrupts need to be set
// assumes no N-mode user interrupts
always_comb begin
2021-08-22 21:02:40 +00:00
IntInM = 0 ;
2022-03-25 00:08:10 +00:00
IntInM [ 11 ] = ExtIntM ; // MEIP
2022-03-25 18:10:31 +00:00
IntInM [ 9 ] = ( ExtIntM & MIDELEG_REGW [ 9 ] ) ; // SEIP
2022-03-25 00:08:10 +00:00
IntInM [ 7 ] = TimerIntM ; // MTIP
IntInM [ 5 ] = TimerIntM & MIDELEG_REGW [ 5 ] ; // STIP
IntInM [ 3 ] = SwIntM ; // MSIP
IntInM [ 1 ] = SwIntM & MIDELEG_REGW [ 1 ] ; // SSIP
2021-01-15 04:37:51 +00:00
end
// Interrupt Write Enables
2022-01-18 23:19:33 +00:00
assign WriteMIPM = CSRMWriteM & ( CSRAdrM = = MIP ) & InstrValidNotFlushedM ;
assign WriteMIEM = CSRMWriteM & ( CSRAdrM = = MIE ) & InstrValidNotFlushedM ;
assign WriteSIPM = CSRSWriteM & ( CSRAdrM = = SIP ) & InstrValidNotFlushedM ;
assign WriteSIEM = CSRSWriteM & ( CSRAdrM = = SIE ) & InstrValidNotFlushedM ;
2021-01-15 04:37:51 +00:00
2022-01-05 14:35:25 +00:00
// Interrupt Pending and Enable Registers
// MEIP, MTIP, MSIP are read-only
// SEIP, STIP, SSIP is writable in MIP if S mode exists
// SSIP is writable in SIP if S mode exists
if ( `S_SUPPORTED ) begin : mask
assign MIP_WRITE_MASK = 12 'h222 ; // SEIP, STIP, SSIP are writable in MIP (20210108-draft 3.1.9)
assign SIP_WRITE_MASK = 12 'h002 ; // SSIP is writable in SIP (privileged 20210108-draft 4.1.3)
end else begin : mask
assign MIP_WRITE_MASK = 12 'h000 ;
assign SIP_WRITE_MASK = 12 'h000 ;
end
2022-03-25 00:08:10 +00:00
always @ ( posedge clk )
2022-01-05 14:35:25 +00:00
if ( reset ) IP_REGW_writeable < = 10 'b0 ;
2022-03-25 00:08:10 +00:00
else if ( WriteMIPM ) IP_REGW_writeable < = ( CSRWriteValM [ 9 : 0 ] & MIP_WRITE_MASK [ 9 : 0 ] ) | { 1 'b0 , IntInM [ 8 : 0 ] } ; // MTIP unclearable
else if ( WriteSIPM ) IP_REGW_writeable < = ( CSRWriteValM [ 9 : 0 ] & SIP_WRITE_MASK [ 9 : 0 ] ) | { 1 'b0 , IntInM [ 8 : 0 ] } ; // MTIP unclearable
else IP_REGW_writeable < = IP_REGW_writeable | { 1 'b0 , IntInM [ 8 : 0 ] } ; // *** check this turns off interrupts properly even when MIDELEG changes
always @ ( posedge clk )
2022-01-05 14:35:25 +00:00
if ( reset ) IE_REGW < = 12 'b0 ;
else if ( WriteMIEM ) IE_REGW < = ( CSRWriteValM [ 11 : 0 ] & 12 'hAAA ) ; // MIE controls M and S fields
else if ( WriteSIEM ) IE_REGW < = ( CSRWriteValM [ 11 : 0 ] & 12 'h222 ) | ( IE_REGW & 12 'h888 ) ; // only S fields
2021-01-15 04:37:51 +00:00
// restricted views of registers
2022-03-25 00:08:10 +00:00
// Add ExtIntM read-only signal
2022-03-29 02:26:42 +00:00
assign IP_REGW = { ExtIntM , 1 'b0 , ExtIntS , 1 'b0 , IntInM [ 7 ] , 7 'b0 } | { 2 'b0 , IP_REGW_writeable [ 9 ] , 3 'b0 , IP_REGW_writeable [ 5 ] , 3 'b0 , IP_REGW_writeable [ 1 ] , 1 'b0 } ; // *** This is just to force the Machine level bits of IP to be unwriteable and to only come from intInM. PLEASE CHANGE ME!!!
2022-01-05 14:35:25 +00:00
// Machine Mode
2022-03-23 03:04:06 +00:00
assign MIP_REGW = IP_REGW ;
assign MIE_REGW = IE_REGW ;
2021-01-15 04:37:51 +00:00
2022-03-23 03:04:06 +00:00
// Supervisor mode
if ( `S_SUPPORTED ) begin
assign SIP_REGW = IP_REGW & MIDELEG_REGW [ 11 : 0 ] & 'h222 ; // only delegated interrupts visible
assign SIE_REGW = IE_REGW & MIDELEG_REGW [ 11 : 0 ] & 'h222 ;
end else begin
assign SIP_REGW = 12 'b0 ;
assign SIE_REGW = 12 'b0 ;
2022-01-05 14:35:25 +00:00
end
2022-03-23 03:04:06 +00:00
2021-01-15 04:37:51 +00:00
endmodule