cvw/pipelined/src/cache/cacheway.sv

183 lines
8.5 KiB
Systemverilog
Raw Normal View History

///////////////////////////////////////////
2022-02-03 15:36:11 +00:00
// cacheway
//
// Written: ross1728@gmail.com July 07, 2021
// Implements the data, tag, valid, dirty, and replacement bits.
//
// Purpose: Storage and read/write access to data cache data, tag valid, dirty, and replacement.
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
// 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:
//
// 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.
////////////////////////////////////////////////////////////////////////////////////////////////
`include "wally-config.vh"
2022-01-05 04:08:18 +00:00
module cacheway #(parameter NUMLINES=512, parameter LINELEN = 256, TAGLEN = 26,
2022-02-03 16:33:01 +00:00
parameter OFFSETLEN = 5, parameter INDEXLEN = 9, parameter DIRTY_BITS = 1) (
input logic clk,
input logic reset,
input logic [$clog2(NUMLINES)-1:0] RAdr,
input logic [`PA_BITS-1:0] PAdr,
2022-02-10 16:43:37 +00:00
input logic [LINELEN-1:0] CacheWriteData,
2022-07-09 00:26:45 +00:00
input logic FStore2,
2022-02-10 16:50:17 +00:00
input logic SetValidWay,
input logic ClearValidWay,
input logic SetDirtyWay,
input logic ClearDirtyWay,
input logic SelEvict,
input logic SelFlush,
2022-02-13 21:47:27 +00:00
input logic VictimWay,
2022-02-10 16:50:17 +00:00
input logic FlushWay,
2022-02-13 21:47:27 +00:00
input logic Invalidate,
2022-03-11 00:44:50 +00:00
input logic [(`XLEN-1)/8:0] ByteMask,
2022-02-10 16:50:17 +00:00
output logic [LINELEN-1:0] ReadDataLineWay,
2022-02-13 21:47:27 +00:00
output logic HitWay,
2022-02-10 17:40:10 +00:00
output logic VictimDirtyWay,
output logic [TAGLEN-1:0] VictimTagWay);
localparam integer WORDSPERLINE = LINELEN/`XLEN;
localparam integer BYTESPERLINE = LINELEN/8;
2022-02-08 23:52:09 +00:00
localparam LOGWPL = $clog2(WORDSPERLINE);
localparam LOGXLENBYTES = $clog2(`XLEN/8);
localparam integer BYTESPERWORD = `XLEN/8;
2022-02-08 23:52:09 +00:00
2022-02-12 05:10:58 +00:00
logic [NUMLINES-1:0] ValidBits;
logic [NUMLINES-1:0] DirtyBits;
logic [LINELEN-1:0] ReadDataLine;
logic [TAGLEN-1:0] ReadTag;
logic Valid;
logic Dirty;
logic SelData;
logic SelTag;
logic [$clog2(NUMLINES)-1:0] RAdrD;
logic [2**LOGWPL-1:0] MemPAdrDecoded;
logic [WORDSPERLINE-1:0] SelectedWriteWordEn;
2022-03-11 00:44:50 +00:00
logic [(`XLEN-1)/8:0] FinalByteMask;
2022-02-08 23:52:09 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
// Write Enable demux
/////////////////////////////////////////////////////////////////////////////////////////////
if(`LLEN>`XLEN)begin
logic [2**LOGWPL-1:0] MemPAdrDecodedtmp;
onehotdecoder #(LOGWPL) adrdec(
.bin(PAdr[LOGWPL+LOGXLENBYTES-1:LOGXLENBYTES]), .decoded(MemPAdrDecodedtmp));
2022-07-09 00:26:45 +00:00
assign MemPAdrDecoded = MemPAdrDecodedtmp|{MemPAdrDecodedtmp[2**LOGWPL-2:0]&{2**LOGWPL-1{FStore2}}, 1'b0};
end else
onehotdecoder #(LOGWPL) adrdec(
.bin(PAdr[LOGWPL+LOGXLENBYTES-1:LOGXLENBYTES]), .decoded(MemPAdrDecoded));
2022-02-08 23:52:09 +00:00
// If writing the whole line set all write enables to 1, else only set the correct word.
2022-02-13 21:53:01 +00:00
assign SelectedWriteWordEn = SetValidWay ? '1 : SetDirtyWay ? MemPAdrDecoded : '0; // OR-AND
2022-03-11 00:44:50 +00:00
assign FinalByteMask = SetValidWay ? '1 : ByteMask; // OR
2022-02-03 16:00:57 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
2022-02-03 16:52:22 +00:00
// Tag Array
2022-02-03 16:00:57 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
2022-02-10 16:43:37 +00:00
sram1p1rw #(.DEPTH(NUMLINES), .WIDTH(TAGLEN)) CacheTagMem(.clk,
2022-03-11 00:44:50 +00:00
.Adr(RAdr), .ReadData(ReadTag), .ByteMask('1),
2022-02-13 21:53:01 +00:00
.CacheWriteData(PAdr[`PA_BITS-1:OFFSETLEN+INDEXLEN]), .WriteEnable(SetValidWay));
2022-02-03 16:52:22 +00:00
// AND portion of distributed tag multiplexer
2022-02-13 18:38:39 +00:00
mux2 #(1) seltagmux(VictimWay, FlushWay, SelFlush, SelTag);
2022-02-10 17:40:10 +00:00
assign VictimTagWay = SelTag ? ReadTag : '0; // AND part of AOMux
assign VictimDirtyWay = SelTag & Dirty & Valid;
2022-02-13 21:47:27 +00:00
assign HitWay = Valid & (ReadTag == PAdr[`PA_BITS-1:OFFSETLEN+INDEXLEN]);
2022-02-03 16:52:22 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
// Data Array
/////////////////////////////////////////////////////////////////////////////////////////////
2022-07-10 12:47:34 +00:00
// *** instantiate one larger RAM, not one per RAM. Expand byte mask
2021-12-30 15:18:16 +00:00
genvar words;
logic [BYTESPERLINE-1:0] ReplicatedByteMask, SRAMLineByteMask, WordByteEnabled;
assign ReplicatedByteMask = {{WORDSPERLINE}{FinalByteMask}};
for(words = 0; words < WORDSPERLINE; words++)
assign WordByteEnabled[BYTESPERWORD*(words+1)-1:BYTESPERWORD*(words)] = {{BYTESPERWORD}{SelectedWriteWordEn[words]}};
assign SRAMLineByteMask = ReplicatedByteMask & WordByteEnabled;
localparam integer SRAMLEN = 256;
localparam integer NUMSRAM = LINELEN/SRAMLEN;
localparam integer SRAMLENINBYTES = SRAMLEN/8;
localparam integer LOGNUMSRAM = $clog2(NUMSRAM);
for(words = 0; words < NUMSRAM; words++) begin: word
sram1p1rw #(.DEPTH(NUMLINES), .WIDTH(SRAMLEN)) CacheDataMem(.clk, .Adr(RAdr),
.ReadData(ReadDataLine[SRAMLEN*(words+1)-1:SRAMLEN*words]),
.CacheWriteData(CacheWriteData[SRAMLEN*(words+1)-1:SRAMLEN*words]),
.WriteEnable(1'b1), .ByteMask(SRAMLineByteMask[SRAMLENINBYTES*(words+1)-1:SRAMLENINBYTES*words]));
end
/* -----\/----- EXCLUDED -----\/-----
for(words = 0; words < 1; words++) begin: word
sram1p1rw #(.DEPTH(NUMLINES), .WIDTH(LINELEN)) CacheDataMem(.clk, .Adr(RAdr),
.ReadData(ReadDataLine),
.CacheWriteData(CacheWriteData),
.WriteEnable(1'b1), .ByteMask(SRAMLineByteMask));
end
-----/\----- EXCLUDED -----/\----- */
/* -----\/----- EXCLUDED -----\/-----
for(words = 0; words < WORDSPERLINE; words++) begin: word
2022-02-10 16:43:37 +00:00
sram1p1rw #(.DEPTH(NUMLINES), .WIDTH(`XLEN)) CacheDataMem(.clk, .Adr(RAdr),
2022-02-04 20:18:10 +00:00
.ReadData(ReadDataLine[(words+1)*`XLEN-1:words*`XLEN] ),
2022-02-10 16:43:37 +00:00
.CacheWriteData(CacheWriteData[(words+1)*`XLEN-1:words*`XLEN]),
2022-03-11 00:44:50 +00:00
.WriteEnable(SelectedWriteWordEn[words]), .ByteMask(FinalByteMask));
2022-01-05 16:25:08 +00:00
end
-----/\----- EXCLUDED -----/\----- */
2022-02-03 16:52:22 +00:00
// AND portion of distributed read multiplexers
2022-02-13 21:47:27 +00:00
mux3 #(1) selecteddatamux(HitWay, VictimWay, FlushWay, {SelFlush, SelEvict}, SelData);
2022-02-10 16:50:17 +00:00
assign ReadDataLineWay = SelData ? ReadDataLine : '0; // AND part of AO mux.
2022-02-03 16:33:01 +00:00
2022-02-03 16:00:57 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
// Valid Bits
/////////////////////////////////////////////////////////////////////////////////////////////
2022-02-03 16:00:57 +00:00
always_ff @(posedge clk) begin // Valid bit array,
2022-02-13 21:47:27 +00:00
if (reset | Invalidate) ValidBits <= #1 '0;
2022-02-12 05:10:58 +00:00
else if (SetValidWay) ValidBits[RAdr] <= #1 1'b1;
else if (ClearValidWay) ValidBits[RAdr] <= #1 1'b0;
2022-02-03 16:00:57 +00:00
end
flop #($clog2(NUMLINES)) RAdrDelayReg(clk, RAdr, RAdrD);
2022-02-04 20:18:10 +00:00
assign Valid = ValidBits[RAdrD];
2022-02-03 16:00:57 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
// Dirty Bits
/////////////////////////////////////////////////////////////////////////////////////////////
2022-01-05 16:25:08 +00:00
// Dirty bits
2022-02-03 16:00:57 +00:00
if (DIRTY_BITS) begin:dirty
2022-01-05 16:25:08 +00:00
always_ff @(posedge clk) begin
2022-02-12 05:10:58 +00:00
if (reset) DirtyBits <= #1 {NUMLINES{1'b0}};
else if (SetDirtyWay) DirtyBits[RAdr] <= #1 1'b1;
2022-02-10 16:50:17 +00:00
else if (ClearDirtyWay) DirtyBits[RAdr] <= #1 1'b0;
end
2022-02-04 20:18:10 +00:00
assign Dirty = DirtyBits[RAdrD];
2022-02-03 16:07:55 +00:00
end else assign Dirty = 1'b0;
2022-02-03 16:33:01 +00:00
endmodule