2021-05-03 21:37:42 +00:00
///////////////////////////////////////////
// pmpadrdec.sv
//
// Written: tfleming@hmc.edu 28 April 2021
// Modified:
//
// Purpose: Address decoder for the PMP checker. Decides whether a given address
// falls within the PMP range for each address-matching mode
// (top-of-range/TOR, naturally aligned four-byte region/NA4, and
// naturally aligned power-of-two region/NAPOT), then selects the
// output based on which mode is input.
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-05-03 21:37:42 +00:00
//
2023-01-10 19:35:20 +00:00
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-05-03 21:37:42 +00:00
//
2023-01-10 19:35:20 +00:00
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-05-03 21:37:42 +00:00
//
2023-01-10 19:35:20 +00:00
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
// may obtain a copy of the License at
2021-05-03 21:37:42 +00:00
//
2023-01-10 19:35:20 +00:00
// https://solderpad.org/licenses/SHL-2.1/
//
// Unless required by applicable law or agreed to in writing, any work 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.
2022-01-07 12:58:40 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////
2021-05-03 21:37:42 +00:00
`include " wally-config.vh "
module pmpadrdec (
2021-06-24 23:59:29 +00:00
input logic [ `PA_BITS - 1 : 0 ] PhysicalAddress ,
2021-07-03 07:29:33 +00:00
input logic [ 7 : 0 ] PMPCfg ,
input logic [ `XLEN - 1 : 0 ] PMPAdr ,
input logic PAgePMPAdrIn ,
output logic PAgePMPAdrOut ,
output logic Match , Active ,
output logic L , X , W , R
2021-05-03 21:37:42 +00:00
) ;
2021-07-03 07:29:33 +00:00
2021-05-03 21:37:42 +00:00
localparam TOR = 2 'b01 ;
localparam NA4 = 2 'b10 ;
localparam NAPOT = 2 'b11 ;
2021-06-23 06:31:50 +00:00
logic TORMatch , NAMatch ;
2021-07-03 07:29:33 +00:00
logic PAltPMPAdr ;
2021-06-23 06:31:50 +00:00
logic [ `PA_BITS - 1 : 0 ] CurrentAdrFull ;
2021-07-03 07:29:33 +00:00
logic [ 1 : 0 ] AdrMode ;
assign AdrMode = PMPCfg [ 4 : 3 ] ;
2021-05-04 05:56:05 +00:00
2021-07-03 06:25:31 +00:00
// The two lsb of the physical address don't matter for this checking.
// The following code includes them, but hardwires the PMP checker lsbs to 00
// and masks them later. Logic synthesis should optimize away these bottom bits.
2021-06-23 06:31:50 +00:00
2021-05-03 21:37:42 +00:00
// Top-of-range (TOR)
2021-06-23 06:31:50 +00:00
// Append two implicit trailing 0's to PMPAdr value
2021-07-03 07:29:33 +00:00
assign CurrentAdrFull = { PMPAdr [ `PA_BITS - 3 : 0 ] , 2 'b00 } ;
assign PAltPMPAdr = { 1 'b0 , PhysicalAddress } < { 1 'b0 , CurrentAdrFull } ; // unsigned comparison
assign PAgePMPAdrOut = ~ PAltPMPAdr ;
2022-01-02 21:47:21 +00:00
assign TORMatch = PAgePMPAdrIn & PAltPMPAdr ;
2021-05-03 21:37:42 +00:00
2021-06-23 06:31:50 +00:00
// Naturally aligned regions
2021-10-08 22:33:18 +00:00
logic [ `PA_BITS - 1 : 0 ] NAMask , NABase ;
2021-07-19 19:06:14 +00:00
2021-07-23 16:57:58 +00:00
assign NAMask [ 1 : 0 ] = { 2 'b11 } ;
2021-10-08 22:33:18 +00:00
assign NAMask [ `PA_BITS - 1 : 2 ] = ( PMPAdr [ `PA_BITS - 3 : 0 ] + { { ( `PA_BITS - 3 ) { 1 'b0 } } , ( AdrMode = = NAPOT ) } ) ^ PMPAdr [ `PA_BITS - 3 : 0 ] ;
2022-01-05 14:35:25 +00:00
// form a mask where the bottom k bits are 1, corresponding to a size of 2^k bytes for this memory region.
2021-10-08 22:33:18 +00:00
// This assumes we're using at least an NA4 region, but works for any size NAPOT region.
assign NABase = { ( PMPAdr [ `PA_BITS - 3 : 0 ] & ~ NAMask [ `PA_BITS - 1 : 2 ] ) , 2 'b00 } ; // base physical address of the pmp.
assign NAMatch = & ( ( NABase ~ ^ PhysicalAddress ) | NAMask ) ; // check if upper bits of base address match, ignore lower bits correspoonding to inside the memory range
2021-06-23 06:31:50 +00:00
2021-05-03 21:37:42 +00:00
assign Match = ( AdrMode = = TOR ) ? TORMatch :
2022-01-02 21:47:21 +00:00
( AdrMode = = NA4 | AdrMode = = NAPOT ) ? NAMatch :
2021-05-03 21:37:42 +00:00
0 ;
2022-07-06 00:02:01 +00:00
assign L = PMPCfg [ 7 ] ;
assign X = PMPCfg [ 2 ] ;
assign W = PMPCfg [ 1 ] ;
assign R = PMPCfg [ 0 ] ;
2021-07-03 07:29:33 +00:00
assign Active = | PMPCfg [ 4 : 3 ] ;
endmodule
2021-05-03 21:37:42 +00:00