Begin changes to direct-mapped cache

This commit is contained in:
Jarred Allen 2021-04-01 13:55:21 -04:00
parent 6cda818f09
commit 8dc57a7706

View File

@ -26,7 +26,7 @@
`include "wally-config.vh" `include "wally-config.vh"
module rodirectmappedmem #(parameter LINESIZE = 256, parameter NUMLINES = 512, parameter WORDSIZE = `XLEN) ( module rodirectmappedmem #(parameter NUMLINES=512, parameter LINESIZE = 256, parameter WORDSIZE = `XLEN) (
// Pipeline stuff // Pipeline stuff
input logic clk, input logic clk,
input logic reset, input logic reset,
@ -44,50 +44,76 @@ module rodirectmappedmem #(parameter LINESIZE = 256, parameter NUMLINES = 512, p
output logic DataValid output logic DataValid
); );
localparam integer SETWIDTH = $clog2(NUMLINES); // Various compile-time constants
localparam integer OFFSETWIDTH = $clog2(LINESIZE/8); localparam integer WORDWIDTH = $clog2(WORDSIZE);
localparam integer TAGWIDTH = `XLEN-SETWIDTH-OFFSETWIDTH; localparam integer LINEWIDTH = $clog2(LINESIZE/8);
localparam integer OFFSETWIDTH = $clog2(LINESIZE) - WORDWIDTH;
localparam integer SETWIDTH = $clog2(NUMLINES);
localparam integer TAGWIDTH = $clog2(`XLEN) - $clog2(LINESIZE) - SETWIDTH;
logic [NUMLINES-1:0][WORDSIZE-1:0] LineOutputs; // Machinery to read from and write to the correct addresses in memory
logic [NUMLINES-1:0] ValidOutputs; logic [`XLEN-1:0] ReadPAdr;
logic [NUMLINES-1:0][TAGWIDTH-1:0] TagOutputs; logic [OFFSETWIDTH-1:0] ReadOffset, WriteOffset;
logic [OFFSETWIDTH-1:0] WordSelect; logic [SETWIDTH-1:0] ReadSet, WriteSet;
logic [`XLEN-1:0] ReadPAdr; logic [TAGWIDTH-1:0] ReadTag, WriteTag;
logic [SETWIDTH-1:0] ReadSet, WriteSet;
logic [TAGWIDTH-1:0] ReadTag, WriteTag;
// Swizzle bits to get the offset, set, and tag out of the read and write addresses // Machinery to check if a given read is valid and is the desired value
logic [TAGWIDTH-1:0] DataTag;
logic [NUMLINES-1:0] ValidOut, NextValidOut;
// Assign the read and write addresses in cache memory
always_comb begin always_comb begin
// Read address assign ReadOffset = ReadLowerAdr[WORDWIDTH+OFFSETWIDTH-1:WORDWIDTH];
assign WordSelect = ReadLowerAdr[OFFSETWIDTH-1:0];
assign ReadPAdr = {ReadUpperPAdr, ReadLowerAdr}; assign ReadPAdr = {ReadUpperPAdr, ReadLowerAdr};
assign ReadSet = ReadPAdr[SETWIDTH+OFFSETWIDTH-1:OFFSETWIDTH]; assign ReadSet = ReadPAdr[LINEWIDTH+SETWIDTH-1:LINEWIDTH];
assign ReadTag = ReadPAdr[`XLEN-1:SETWIDTH+OFFSETWIDTH]; assign ReadTag = ReadPAdr[`XLEN-1:LINEWIDTH+SETWIDTH];
// Write address
assign WriteSet = WritePAdr[SETWIDTH+OFFSETWIDTH-1:OFFSETWIDTH]; assign WriteOffset = WritePAdr[WORDWIDTH+OFFSETWIDTH-1:WORDWIDTH];
assign WriteTag = WritePAdr[`XLEN-1:SETWIDTH+OFFSETWIDTH]; assign WriteSet = WritePAdr[LINEWIDTH+SETWIDTH-1:LINEWIDTH];
assign WriteTag = WritePAdr[`XLEN-1:LINEWIDTH+SETWIDTH];
end end
genvar i; SRAM2P1R1W #(.Depth(OFFSETWIDTH), .Width(WORDSIZE)) cachemem (
generate .*,
for (i=0; i < NUMLINES; i++) begin .RA1(ReadOffset),
rocacheline #(LINESIZE, TAGWIDTH, WORDSIZE) lines ( .RD1(DataWord),
.*, .REN1(1'b1),
.WriteEnable(WriteEnable & (WriteSet == i)), .WA1(WriteOffset),
.WriteData(WriteLine), .WD1(WriteSet),
.WriteTag(WriteTag), .WEN1(WriteEnable),
.DataWord(LineOutputs[i]), .BitWEN1(0)
.DataTag(TagOutputs[i]), );
.DataValid(ValidOutputs[i])
);
end
endgenerate
// Get the data and valid out of the lines SRAM2P1R1W #(.Depth(OFFSETWIDTH), .Width(TAGWIDTH)) cachetags (
.*,
.RA1(ReadOffset),
.RD1(DataTag),
.REN1(1'b1),
.WA1(WriteOffset),
.WD1(WriteTag),
.WEN1(WriteEnable),
.BitWEN1(0)
);
// Correctly handle the valid bits
always_comb begin always_comb begin
assign DataWord = LineOutputs[ReadSet]; if (WriteEnable) begin
assign DataValid = ValidOutputs[ReadSet] & (TagOutputs[ReadSet] == ReadTag); assign NextValidOut = {NextValidOut[NUMLINES-1:WriteSet+1], 1'b1, NextValidOut[WriteSet-1:0]};
end else begin
assign NextValidOut = ValidOut;
end
end
always_ff @(posedge clk, reset, flush) begin
if (reset || flush) begin
ValidOut <= {NUMLINES{1'b0}};
end else begin
ValidOut <= NextValidOut;
end
end
// Determine if the line coming out is valid and matches the desired data
always_comb begin
assign DataValid = ValidOut[ReadSet] && (DataTag == ReadTag);
end end
endmodule endmodule