2021-05-03 17:03:17 +00:00
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2021-06-04 18:36:06 +00:00
|
|
|
module ICacheMem #(parameter NUMLINES=512, parameter BLOCKLEN = 256)
|
|
|
|
(
|
|
|
|
// Pipeline stuff
|
2021-06-04 18:45:08 +00:00
|
|
|
input logic clk,
|
|
|
|
input logic reset,
|
2021-06-04 18:36:06 +00:00
|
|
|
// If flush is high, invalidate the entire cache
|
2021-06-04 18:45:08 +00:00
|
|
|
input logic flush,
|
|
|
|
|
2021-07-07 22:52:16 +00:00
|
|
|
input logic [`PA_BITS-1:0] PCTagF, // physical address
|
|
|
|
input logic [`PA_BITS-1:0] PCNextIndexF, // virtual address
|
2021-06-04 18:45:08 +00:00
|
|
|
input logic WriteEnable,
|
|
|
|
input logic [BLOCKLEN-1:0] WriteLine,
|
|
|
|
output logic [BLOCKLEN-1:0] ReadLineF,
|
|
|
|
output logic HitF
|
2021-06-04 18:36:06 +00:00
|
|
|
);
|
2021-05-03 17:03:17 +00:00
|
|
|
|
2021-06-04 18:45:08 +00:00
|
|
|
// divide the address bus into sections; tag, index, and offset
|
2021-06-04 18:36:06 +00:00
|
|
|
localparam BLOCKBYTELEN = BLOCKLEN/8;
|
|
|
|
localparam OFFSETLEN = $clog2(BLOCKBYTELEN);
|
|
|
|
localparam INDEXLEN = $clog2(NUMLINES);
|
2021-06-04 18:45:08 +00:00
|
|
|
// *** BUG. `XLEN needs to be replaced with the virtual address width, S32, S39, or S48
|
2021-06-18 17:02:59 +00:00
|
|
|
localparam TAGLEN = `PA_BITS - OFFSETLEN - INDEXLEN;
|
2021-05-03 17:03:17 +00:00
|
|
|
|
2021-06-04 18:45:08 +00:00
|
|
|
logic [TAGLEN-1:0] LookupTag;
|
|
|
|
logic [NUMLINES-1:0] ValidOut;
|
|
|
|
logic DataValidBit;
|
2021-05-03 17:03:17 +00:00
|
|
|
|
2021-06-04 18:36:06 +00:00
|
|
|
// Depth is number of bits in one "word" of the memory, width is number of such words
|
|
|
|
sram1rw #(.DEPTH(BLOCKLEN), .WIDTH(NUMLINES))
|
|
|
|
cachemem (.*,
|
|
|
|
.Addr(PCNextIndexF[INDEXLEN+OFFSETLEN-1:OFFSETLEN]),
|
2021-06-04 18:45:08 +00:00
|
|
|
.ReadData(ReadLineF),
|
2021-06-04 18:36:06 +00:00
|
|
|
.WriteData(WriteLine)
|
|
|
|
);
|
2021-06-04 18:45:08 +00:00
|
|
|
|
2021-06-04 18:36:06 +00:00
|
|
|
sram1rw #(.DEPTH(TAGLEN), .WIDTH(NUMLINES))
|
|
|
|
cachetags (.*,
|
|
|
|
.Addr(PCNextIndexF[INDEXLEN+OFFSETLEN-1:OFFSETLEN]),
|
2021-06-04 18:45:08 +00:00
|
|
|
.ReadData(LookupTag),
|
2021-06-18 17:02:59 +00:00
|
|
|
.WriteData(PCTagF[`PA_BITS-1:INDEXLEN+OFFSETLEN])
|
2021-06-04 18:36:06 +00:00
|
|
|
);
|
2021-05-03 17:03:17 +00:00
|
|
|
|
2021-06-04 18:36:06 +00:00
|
|
|
// Correctly handle the valid bits
|
|
|
|
always_ff @(posedge clk, posedge reset) begin
|
|
|
|
if (reset) begin
|
|
|
|
ValidOut <= {NUMLINES{1'b0}};
|
|
|
|
end else if (flush) begin
|
|
|
|
ValidOut <= {NUMLINES{1'b0}};
|
|
|
|
end else begin
|
|
|
|
if (WriteEnable) begin
|
|
|
|
ValidOut[PCNextIndexF[INDEXLEN+OFFSETLEN-1:OFFSETLEN]] <= 1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
DataValidBit <= ValidOut[PCNextIndexF[INDEXLEN+OFFSETLEN-1:OFFSETLEN]];
|
|
|
|
end
|
2021-06-18 17:02:59 +00:00
|
|
|
assign HitF = DataValidBit && (LookupTag == PCTagF[`PA_BITS-1:INDEXLEN+OFFSETLEN]);
|
2021-05-03 17:03:17 +00:00
|
|
|
endmodule
|