cvw/pipelined/src/cache/cacheway.sv

158 lines
6.6 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,
2021-12-30 15:18:16 +00:00
parameter OFFSETLEN = 5, parameter INDEXLEN = 9, parameter DIRTY_BITS = 1)
(input logic clk,
2021-12-30 15:18:16 +00:00
input logic reset,
input logic [$clog2(NUMLINES)-1:0] RAdr,
2021-12-30 15:18:16 +00:00
input logic [`PA_BITS-1:0] PAdr,
input logic WriteEnable,
input logic VDWriteEnable,
input logic [LINELEN/`XLEN-1:0] WriteWordEnable,
2021-12-30 15:18:16 +00:00
input logic TagWriteEnable,
2022-01-05 04:08:18 +00:00
input logic [LINELEN-1:0] WriteData,
2021-12-30 15:18:16 +00:00
input logic SetValid,
input logic ClearValid,
input logic SetDirty,
input logic ClearDirty,
input logic SelEvict,
input logic VictimWay,
input logic InvalidateAll,
input logic SelFlush,
input logic FlushWay,
2022-01-05 04:08:18 +00:00
output logic [LINELEN-1:0] ReadDataLineWayMasked,
2021-12-30 15:18:16 +00:00
output logic WayHit,
output logic VictimDirtyWay,
output logic [TAGLEN-1:0] VictimTagWay
);
2021-12-30 15:18:16 +00:00
logic [NUMLINES-1:0] ValidBits;
logic [NUMLINES-1:0] DirtyBits;
2022-01-05 04:08:18 +00:00
logic [LINELEN-1:0] ReadDataLineWay;
2021-12-30 15:18:16 +00:00
logic [TAGLEN-1:0] ReadTag;
logic Valid;
logic Dirty;
logic SelectedWay;
logic [TAGLEN-1:0] VicDirtyWay;
logic [TAGLEN-1:0] FlushThisWay;
logic [$clog2(NUMLINES)-1:0] RAdrD;
logic SetValidD, ClearValidD;
logic SetDirtyD, ClearDirtyD;
logic WriteEnableD, VDWriteEnableD;
2022-02-03 16:00:57 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
// Data and Tag Arrays
/////////////////////////////////////////////////////////////////////////////////////////////
// Potential optimization: if byte write enables are available, could remove subwordwrites
/* sram1rw #(.DEPTH(NUMLINES), .WIDTH(LINELEN)) CacheDataMem(
.clk(clk), .Addr(RAdr),
.ReadData(ReadDataLineWay), .WriteData(WriteData),
.WriteEnable(WriteEnable & WriteWordEnable[words])); // *** */
2021-12-30 15:18:16 +00:00
genvar words;
2022-01-05 16:25:08 +00:00
for(words = 0; words < LINELEN/`XLEN; words++) begin: word
sram1rw #(.DEPTH(NUMLINES), .WIDTH(`XLEN))
2022-01-05 16:25:08 +00:00
CacheDataMem(.clk(clk), .Addr(RAdr),
.ReadData(ReadDataLineWay[(words+1)*`XLEN-1:words*`XLEN] ),
.WriteData(WriteData[(words+1)*`XLEN-1:words*`XLEN]),
.WriteEnable(WriteEnable & WriteWordEnable[words]));
end
2022-02-03 16:00:57 +00:00
sram1rw #(.DEPTH(NUMLINES), .WIDTH(TAGLEN)) CacheTagMem(.clk(clk),
.Addr(RAdr), .ReadData(ReadTag),
.WriteData(PAdr[`PA_BITS-1:OFFSETLEN+INDEXLEN]), .WriteEnable(TagWriteEnable));
assign WayHit = Valid & (ReadTag == PAdr[`PA_BITS-1:OFFSETLEN+INDEXLEN]);
2021-09-16 21:56:48 +00:00
assign SelectedWay = SelFlush ? FlushWay :
2021-12-30 15:18:16 +00:00
SelEvict ? VictimWay : WayHit;
2022-01-05 04:08:18 +00:00
assign ReadDataLineWayMasked = SelectedWay ? ReadDataLineWay : '0; // first part of AO mux.
2021-08-25 17:42:05 +00:00
assign VictimDirtyWay = SelFlush ? FlushWay & Dirty & Valid :
2021-12-30 15:18:16 +00:00
VictimWay & Dirty & Valid;
assign VicDirtyWay = VictimWay ? ReadTag : '0;
assign FlushThisWay = FlushWay ? ReadTag : '0;
assign VictimTagWay = SelFlush ? FlushThisWay : VicDirtyWay;
2021-12-30 15:18:16 +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,
if (reset | InvalidateAll) ValidBits <= #1 '0;
else if (SetValidD & (WriteEnableD | VDWriteEnableD)) ValidBits[RAdrD] <= #1 1'b1;
else if (ClearValidD & (WriteEnableD | VDWriteEnableD)) ValidBits[RAdrD] <= #1 1'b0;
end
/* always_ff @(posedge clk) begin // pipeline register; helps timing ***Ross consider further
RAdrD <= #1 RAdr;
SetValidD <= #1 SetValid;
ClearValidD <= #1 ClearValid;
WriteEnableD <= #1 WriteEnable;
VDWriteEnableD <= #1 VDWriteEnable;
end */
flop #($clog2(NUMLINES)) RAdrDelayReg(clk, RAdr, RAdrD);
flop #(4) ValidCtrlDelayReg(clk, {SetValid, ClearValid, WriteEnable, VDWriteEnable},
{SetValidD, ClearValidD, WriteEnableD, VDWriteEnableD});
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-03 16:00:57 +00:00
if (reset) DirtyBits <= #1 {NUMLINES{1'b0}};
else if (SetDirtyD & (WriteEnableD | VDWriteEnableD)) DirtyBits[RAdrD] <= #1 1'b1;
else if (ClearDirtyD & (WriteEnableD | VDWriteEnableD)) DirtyBits[RAdrD] <= #1 1'b0;
end
2022-02-03 16:00:57 +00:00
flop #(2) DirtyCtlDelayReg(clk, {SetDirty, ClearDirty}, {SetDirtyD, ClearDirtyD});
/* always_ff @(posedge clk) begin
2022-01-05 16:25:08 +00:00
SetDirtyD <= SetDirty;
ClearDirtyD <= ClearDirty;
2022-02-03 16:00:57 +00:00
end */
2022-01-05 16:25:08 +00:00
assign Dirty = DirtyBits[RAdrD];
end else begin:dirty
assign Dirty = 1'b0;
end
2022-02-03 15:36:11 +00:00
endmodule // DCacheCacheWays