2021-06-23 05:41:00 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// lsu.sv
|
|
|
|
//
|
|
|
|
// Written: David_Harris@hmc.edu 9 January 2021
|
|
|
|
// Modified:
|
|
|
|
//
|
|
|
|
// Purpose: Load/Store Unit
|
|
|
|
// Top level of the memory-stage hart logic
|
|
|
|
// Contains data cache, DTLB, subword read/write datapath, interface to external bus
|
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2021-07-06 15:41:36 +00:00
|
|
|
module lsu
|
|
|
|
(
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic clk, reset,
|
|
|
|
input logic StallM, FlushM, StallW, FlushW,
|
|
|
|
output logic LSUStall,
|
2021-07-06 15:41:36 +00:00
|
|
|
// Memory Stage
|
|
|
|
|
|
|
|
// connected to cpu (controls)
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [1:0] MemRWM,
|
|
|
|
input logic [2:0] Funct3M,
|
|
|
|
input logic [6:0] Funct7M,
|
|
|
|
input logic [1:0] AtomicM,
|
|
|
|
input logic ExceptionM,
|
|
|
|
input logic PendingInterruptM,
|
|
|
|
input logic FlushDCacheM,
|
|
|
|
output logic CommittedM,
|
|
|
|
output logic SquashSCW,
|
|
|
|
output logic DCacheMiss,
|
|
|
|
output logic DCacheAccess,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// address and write data
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [`XLEN-1:0] IEUAdrE,
|
2021-12-20 16:03:19 +00:00
|
|
|
(* mark_debug = "true" *)output logic [`XLEN-1:0] IEUAdrM,
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [`XLEN-1:0] WriteDataM,
|
2021-07-22 19:51:14 +00:00
|
|
|
output logic [`XLEN-1:0] ReadDataM,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// cpu privilege
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [1:0] PrivilegeModeW,
|
|
|
|
input logic DTLBFlushM,
|
2021-07-06 15:41:36 +00:00
|
|
|
// faults
|
2021-12-20 03:34:40 +00:00
|
|
|
output logic DTLBLoadPageFaultM, DTLBStorePageFaultM,
|
|
|
|
output logic LoadMisalignedFaultM, LoadAccessFaultM,
|
2021-07-06 15:41:36 +00:00
|
|
|
// cpu hazard unit (trap)
|
2021-12-20 03:34:40 +00:00
|
|
|
output logic StoreMisalignedFaultM, StoreAccessFaultM,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// connect to ahb
|
2021-12-27 22:45:49 +00:00
|
|
|
(* mark_debug = "true" *) output logic [`PA_BITS-1:0] DCtoAHBPAdrM,
|
2021-12-20 03:34:40 +00:00
|
|
|
output logic DCtoAHBReadM,
|
|
|
|
output logic DCtoAHBWriteM,
|
|
|
|
input logic DCfromAHBAck,
|
2021-12-27 22:45:49 +00:00
|
|
|
(* mark_debug = "true" *) input logic [`XLEN-1:0] DCfromAHBReadData,
|
2021-07-13 22:24:59 +00:00
|
|
|
output logic [`XLEN-1:0] DCtoAHBWriteData,
|
2021-12-20 03:34:40 +00:00
|
|
|
output logic [2:0] DCtoAHBSizeM,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// mmu management
|
|
|
|
|
|
|
|
// page table walker
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [`XLEN-1:0] SATP_REGW, // from csr
|
|
|
|
input logic STATUS_MXR, STATUS_SUM, STATUS_MPRV,
|
|
|
|
input logic [1:0] STATUS_MPP,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
input logic [`XLEN-1:0] PCF,
|
|
|
|
input logic ITLBMissF,
|
2021-07-17 19:01:01 +00:00
|
|
|
output logic [`XLEN-1:0] PTE,
|
2021-12-20 03:34:40 +00:00
|
|
|
output logic [1:0] PageType,
|
|
|
|
output logic ITLBWriteF,
|
|
|
|
output logic WalkerInstrPageFaultF,
|
|
|
|
output logic WalkerLoadPageFaultM,
|
|
|
|
output logic WalkerStorePageFaultM,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
input var logic [7:0] PMPCFG_ARRAY_REGW[`PMP_ENTRIES-1:0],
|
|
|
|
input var logic [`XLEN-1:0] PMPADDR_ARRAY_REGW[`PMP_ENTRIES-1:0] // *** this one especially has a large note attached to it in pmpchecker.
|
2021-07-06 15:41:36 +00:00
|
|
|
);
|
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
logic DTLBPageFaultM;
|
2021-07-06 15:41:36 +00:00
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
logic [`PA_BITS-1:0] MemPAdrM; // from mmu to dcache
|
2021-12-20 16:03:56 +00:00
|
|
|
logic [`XLEN+1:0] IEUAdrExtM;
|
2021-12-20 03:34:40 +00:00
|
|
|
logic DTLBMissM;
|
|
|
|
logic DTLBWriteM;
|
|
|
|
logic HPTWStall;
|
2021-12-20 04:21:03 +00:00
|
|
|
logic [`PA_BITS-1:0] HPTWAdr;
|
2021-12-20 03:34:40 +00:00
|
|
|
logic HPTWRead;
|
|
|
|
logic [1:0] MemRWMtoDCache;
|
|
|
|
logic [1:0] MemRWMtoLRSC;
|
|
|
|
logic [2:0] Funct3MtoDCache;
|
|
|
|
logic [1:0] AtomicMtoDCache;
|
|
|
|
logic [`PA_BITS-1:0] MemPAdrNoTranslate;
|
|
|
|
logic [11:0] MemAdrE, MemAdrE_RENAME;
|
2021-12-20 04:21:03 +00:00
|
|
|
logic CPUBusy;
|
2021-12-20 03:34:40 +00:00
|
|
|
logic MemReadM;
|
|
|
|
logic DataMisalignedM;
|
|
|
|
logic DCacheStall;
|
|
|
|
|
|
|
|
logic CacheableM;
|
|
|
|
logic CacheableMtoDCache;
|
2021-12-20 04:24:07 +00:00
|
|
|
logic SelHPTW;
|
2021-12-20 04:00:28 +00:00
|
|
|
logic [2:0] HPTWSize;
|
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
|
|
|
|
logic CommittedMfromDCache;
|
2021-12-28 00:12:59 +00:00
|
|
|
logic CommittedMfromBus;
|
2021-12-20 03:34:40 +00:00
|
|
|
logic PendingInterruptMtoDCache;
|
|
|
|
logic WalkerPageFaultM;
|
|
|
|
|
|
|
|
logic AnyCPUReqM;
|
|
|
|
logic MemAfterIWalkDone;
|
2021-12-28 00:12:59 +00:00
|
|
|
logic BusStall;
|
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
|
|
|
|
typedef enum {STATE_T0_READY,
|
|
|
|
STATE_T0_REPLAY,
|
|
|
|
STATE_T0_FAULT_REPLAY,
|
|
|
|
STATE_T3_DTLB_MISS,
|
|
|
|
STATE_T4_ITLB_MISS,
|
|
|
|
STATE_T5_ITLB_MISS,
|
|
|
|
STATE_T7_DITLB_MISS} statetype;
|
2021-07-26 04:14:28 +00:00
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
statetype InterlockCurrState, InterlockNextState;
|
2021-12-20 03:34:40 +00:00
|
|
|
logic InterlockStall;
|
|
|
|
logic SelReplayCPURequest;
|
|
|
|
logic WalkerInstrPageFaultRaw;
|
|
|
|
logic IgnoreRequest;
|
2021-12-19 19:55:57 +00:00
|
|
|
|
2021-07-26 04:14:28 +00:00
|
|
|
assign AnyCPUReqM = (|MemRWM) | (|AtomicM);
|
2021-12-14 20:46:29 +00:00
|
|
|
|
2021-12-19 19:55:57 +00:00
|
|
|
always_ff @(posedge clk)
|
2021-12-28 00:12:59 +00:00
|
|
|
if (reset) InterlockCurrState <= #1 STATE_T0_READY;
|
|
|
|
else InterlockCurrState <= #1 InterlockNextState;
|
2021-12-19 19:55:57 +00:00
|
|
|
|
|
|
|
always_comb begin
|
2021-12-28 00:12:59 +00:00
|
|
|
case(InterlockCurrState)
|
|
|
|
STATE_T0_READY: if(~ITLBMissF & DTLBMissM & AnyCPUReqM) InterlockNextState = STATE_T3_DTLB_MISS;
|
|
|
|
else if(ITLBMissF & ~DTLBMissM & ~AnyCPUReqM) InterlockNextState = STATE_T4_ITLB_MISS;
|
|
|
|
else if(ITLBMissF & ~DTLBMissM & AnyCPUReqM) InterlockNextState = STATE_T5_ITLB_MISS;
|
|
|
|
else if(ITLBMissF & DTLBMissM & AnyCPUReqM) InterlockNextState = STATE_T7_DITLB_MISS;
|
|
|
|
else InterlockNextState = STATE_T0_READY;
|
|
|
|
STATE_T0_REPLAY: if(DCacheStall) InterlockNextState = STATE_T0_REPLAY;
|
|
|
|
else InterlockNextState = STATE_T0_READY;
|
|
|
|
STATE_T3_DTLB_MISS: if(WalkerLoadPageFaultM | WalkerStorePageFaultM) InterlockNextState = STATE_T0_READY;
|
|
|
|
else if(DTLBWriteM) InterlockNextState = STATE_T0_REPLAY;
|
|
|
|
else InterlockNextState = STATE_T3_DTLB_MISS;
|
|
|
|
STATE_T4_ITLB_MISS: if(WalkerInstrPageFaultRaw | ITLBWriteF) InterlockNextState = STATE_T0_READY;
|
|
|
|
else InterlockNextState = STATE_T4_ITLB_MISS;
|
|
|
|
STATE_T5_ITLB_MISS: if(ITLBWriteF) InterlockNextState = STATE_T0_REPLAY;
|
|
|
|
else if(WalkerInstrPageFaultRaw) InterlockNextState = STATE_T0_FAULT_REPLAY;
|
|
|
|
else InterlockNextState = STATE_T5_ITLB_MISS;
|
|
|
|
STATE_T0_FAULT_REPLAY: if(DCacheStall) InterlockNextState = STATE_T0_FAULT_REPLAY;
|
|
|
|
else InterlockNextState = STATE_T0_READY;
|
|
|
|
STATE_T7_DITLB_MISS: if(WalkerStorePageFaultM | WalkerLoadPageFaultM) InterlockNextState = STATE_T0_READY;
|
|
|
|
else if(DTLBWriteM) InterlockNextState = STATE_T5_ITLB_MISS;
|
|
|
|
else InterlockNextState = STATE_T7_DITLB_MISS;
|
|
|
|
default: InterlockNextState = STATE_T0_READY;
|
2021-12-19 19:55:57 +00:00
|
|
|
endcase
|
|
|
|
end // always_comb
|
2021-12-20 04:41:34 +00:00
|
|
|
|
2021-12-19 19:55:57 +00:00
|
|
|
// signal to CPU it needs to wait on HPTW.
|
2021-12-20 03:34:40 +00:00
|
|
|
/* -----\/----- EXCLUDED -----\/-----
|
|
|
|
// this code has a problem with imperas64mmu as it reads in an invalid uninitalized instruction. InterlockStall becomes x and it propagates
|
|
|
|
// everywhere. The case statement below implements the same logic but any x on the inputs will resolve to 0.
|
2021-12-28 00:12:59 +00:00
|
|
|
assign InterlockStall = (InterlockCurrState == STATE_T0_READY & (DTLBMissM | ITLBMissF)) |
|
|
|
|
(InterlockCurrState == STATE_T3_DTLB_MISS & ~WalkerPageFaultM) | (InterlockCurrState == STATE_T4_ITLB_MISS & ~WalkerInstrPageFaultRaw) |
|
|
|
|
(InterlockCurrState == STATE_T5_ITLB_MISS & ~WalkerInstrPageFaultRaw) | (InterlockCurrState == STATE_T7_DITLB_MISS & ~WalkerPageFaultM);
|
2021-12-19 23:53:13 +00:00
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
-----/\----- EXCLUDED -----/\----- */
|
2021-12-20 00:16:08 +00:00
|
|
|
|
|
|
|
always_comb begin
|
|
|
|
InterlockStall = 1'b0;
|
2021-12-28 00:12:59 +00:00
|
|
|
case(InterlockCurrState)
|
2021-12-20 00:16:08 +00:00
|
|
|
STATE_T0_READY: if(DTLBMissM | ITLBMissF) InterlockStall = 1'b1;
|
|
|
|
STATE_T3_DTLB_MISS: if (~WalkerPageFaultM) InterlockStall = 1'b1;
|
|
|
|
STATE_T4_ITLB_MISS: if (~WalkerInstrPageFaultRaw) InterlockStall = 1'b1;
|
2021-12-21 00:33:31 +00:00
|
|
|
STATE_T5_ITLB_MISS: InterlockStall = 1'b1;
|
|
|
|
//STATE_T0_FAULT_REPLAY: if (~WalkerInstrPageFaultF) InterlockStall = 1'b1;
|
2021-12-20 00:16:08 +00:00
|
|
|
STATE_T7_DITLB_MISS: if (~WalkerPageFaultM) InterlockStall = 1'b1;
|
|
|
|
default: InterlockStall = 1'b0;
|
|
|
|
endcase
|
|
|
|
end
|
2021-12-19 23:53:13 +00:00
|
|
|
|
2021-12-19 20:57:42 +00:00
|
|
|
|
2021-12-19 19:55:57 +00:00
|
|
|
// When replaying CPU memory request after PTW select the IEUAdrM for correct address.
|
2021-12-28 00:12:59 +00:00
|
|
|
assign SelReplayCPURequest = (InterlockNextState == STATE_T0_REPLAY) | (InterlockNextState == STATE_T0_FAULT_REPLAY);
|
|
|
|
assign SelHPTW = (InterlockCurrState == STATE_T3_DTLB_MISS) | (InterlockCurrState == STATE_T4_ITLB_MISS) |
|
|
|
|
(InterlockCurrState == STATE_T5_ITLB_MISS) | (InterlockCurrState == STATE_T7_DITLB_MISS);
|
|
|
|
assign IgnoreRequest = (InterlockCurrState == STATE_T0_READY & (ITLBMissF | DTLBMissM | ExceptionM | PendingInterruptM)) |
|
|
|
|
((InterlockCurrState == STATE_T0_REPLAY | InterlockCurrState == STATE_T0_FAULT_REPLAY)
|
2021-12-21 21:59:56 +00:00
|
|
|
& (ExceptionM | PendingInterruptM));
|
2021-12-19 19:55:57 +00:00
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
assign WalkerInstrPageFaultF = WalkerInstrPageFaultRaw | InterlockCurrState == STATE_T0_FAULT_REPLAY;
|
2021-12-19 19:55:57 +00:00
|
|
|
|
|
|
|
|
2021-12-19 20:00:30 +00:00
|
|
|
flopenrc #(`XLEN) AddressMReg(clk, reset, FlushM, ~StallM, IEUAdrE, IEUAdrM);
|
2021-12-15 20:10:45 +00:00
|
|
|
|
2021-12-14 20:46:29 +00:00
|
|
|
// *** add generate to conditionally create hptw, lsuArb, and mmu
|
|
|
|
// based on `MEM_VIRTMEM
|
2021-12-20 04:00:28 +00:00
|
|
|
hptw hptw(.clk, .reset, .SATP_REGW, .PCF, .IEUAdrM,
|
2021-12-20 03:34:40 +00:00
|
|
|
.ITLBMissF(ITLBMissF & ~PendingInterruptM),
|
|
|
|
.DTLBMissM(DTLBMissM & ~PendingInterruptM),
|
2021-12-20 04:00:28 +00:00
|
|
|
.MemRWM, .PTE, .PageType, .ITLBWriteF, .DTLBWriteM,
|
2021-12-20 03:34:40 +00:00
|
|
|
.HPTWReadPTE(ReadDataM),
|
2021-12-20 04:21:03 +00:00
|
|
|
.DCacheStall, .HPTWAdr, .HPTWRead, .HPTWSize, .AnyCPUReqM,
|
2021-12-20 03:34:40 +00:00
|
|
|
.WalkerInstrPageFaultF(WalkerInstrPageFaultRaw),
|
2021-12-20 04:00:28 +00:00
|
|
|
.WalkerLoadPageFaultM, .WalkerStorePageFaultM);
|
2021-07-17 18:48:44 +00:00
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
assign LSUStall = DCacheStall | InterlockStall | BusStall;
|
2021-07-04 18:49:38 +00:00
|
|
|
|
2021-07-16 17:22:13 +00:00
|
|
|
assign WalkerPageFaultM = WalkerStorePageFaultM | WalkerLoadPageFaultM;
|
2021-07-04 18:49:38 +00:00
|
|
|
|
2021-07-18 08:11:33 +00:00
|
|
|
// arbiter between IEU and hptw
|
2021-12-20 03:34:40 +00:00
|
|
|
|
|
|
|
// multiplex the outputs to LSU
|
2021-12-20 04:24:07 +00:00
|
|
|
assign MemRWMtoLRSC = SelHPTW ? {HPTWRead, 1'b0} : MemRWM;
|
2021-12-20 03:34:40 +00:00
|
|
|
|
2021-12-20 04:24:07 +00:00
|
|
|
mux2 #(3) sizemux(Funct3M, HPTWSize, SelHPTW, Funct3MtoDCache);
|
2021-12-20 03:34:40 +00:00
|
|
|
|
|
|
|
// this is for the d cache SRAM.
|
2021-12-20 04:00:28 +00:00
|
|
|
// turns out because we cannot pipeline hptw requests we don't need this register
|
2021-12-20 04:21:03 +00:00
|
|
|
//flop #(`PA_BITS) HPTWAdrMReg(clk, HPTWAdr, HPTWAdrM); // delay HPTWAdrM by a cycle
|
2021-12-20 03:34:40 +00:00
|
|
|
|
2021-12-20 04:24:07 +00:00
|
|
|
assign AtomicMtoDCache = SelHPTW ? 2'b00 : AtomicM;
|
2021-12-20 16:03:56 +00:00
|
|
|
assign IEUAdrExtM = {2'b00, IEUAdrM};
|
|
|
|
assign MemPAdrNoTranslate = SelHPTW ? HPTWAdr : IEUAdrExtM[`PA_BITS-1:0];
|
2021-12-20 04:24:07 +00:00
|
|
|
assign MemAdrE = SelHPTW ? HPTWAdr[11:0] : IEUAdrE[11:0];
|
|
|
|
assign CPUBusy = SelHPTW ? 1'b0 : StallW;
|
2021-12-20 03:34:40 +00:00
|
|
|
// always block interrupts when using the hardware page table walker.
|
2021-12-28 00:12:59 +00:00
|
|
|
assign CommittedM = SelHPTW ? 1'b1 : CommittedMfromDCache | CommittedMfromBus;
|
2021-12-20 03:34:40 +00:00
|
|
|
|
|
|
|
|
2021-12-20 04:24:07 +00:00
|
|
|
assign PendingInterruptMtoDCache = SelHPTW ? 1'b0 : PendingInterruptM;
|
2021-12-20 03:34:40 +00:00
|
|
|
|
2021-07-04 18:49:38 +00:00
|
|
|
|
2021-07-04 22:05:22 +00:00
|
|
|
mmu #(.TLB_ENTRIES(`DTLB_ENTRIES), .IMMU(0))
|
2021-12-08 08:15:30 +00:00
|
|
|
dmmu(.clk, .reset, .SATP_REGW, .STATUS_MXR, .STATUS_SUM, .STATUS_MPRV, .STATUS_MPP,
|
2021-12-20 04:24:07 +00:00
|
|
|
.PrivilegeModeW, .DisableTranslation(SelHPTW),
|
2021-12-19 22:12:31 +00:00
|
|
|
.PAdr(MemPAdrNoTranslate),
|
2021-12-19 20:00:30 +00:00
|
|
|
.VAdr(IEUAdrM),
|
2021-07-09 20:16:38 +00:00
|
|
|
.Size(Funct3MtoDCache[1:0]),
|
2021-12-20 04:47:48 +00:00
|
|
|
.PTE,
|
2021-07-17 06:31:23 +00:00
|
|
|
.PageTypeWriteVal(PageType),
|
2021-06-24 18:05:22 +00:00
|
|
|
.TLBWrite(DTLBWriteM),
|
|
|
|
.TLBFlush(DTLBFlushM),
|
|
|
|
.PhysicalAddress(MemPAdrM),
|
|
|
|
.TLBMiss(DTLBMissM),
|
2021-07-13 22:24:59 +00:00
|
|
|
.Cacheable(CacheableM),
|
2021-12-20 04:47:48 +00:00
|
|
|
.Idempotent(), .AtomicAllowed(),
|
2021-12-08 08:15:30 +00:00
|
|
|
.TLBPageFault(DTLBPageFaultM),
|
|
|
|
.InstrAccessFaultF(), .LoadAccessFaultM, .StoreAccessFaultM,
|
|
|
|
.AtomicAccessM(1'b0), .ExecuteAccessF(1'b0),
|
|
|
|
.WriteAccessM(MemRWMtoLRSC[0]), .ReadAccessM(MemRWMtoLRSC[1]),
|
|
|
|
.PMPCFG_ARRAY_REGW, .PMPADDR_ARRAY_REGW
|
|
|
|
); // *** the pma/pmp instruction access faults don't really matter here. is it possible to parameterize which outputs exist?
|
2021-06-23 05:41:00 +00:00
|
|
|
|
2021-07-18 01:58:49 +00:00
|
|
|
|
2021-12-14 20:46:29 +00:00
|
|
|
// Move generate from lrsc to outside this module.
|
2021-12-28 00:12:59 +00:00
|
|
|
assign MemReadM = MemRWMtoLRSC[1] & ~(ExceptionM | PendingInterruptMtoDCache) & ~DTLBMissM; // & ~NonBusTrapM & ~DTLBMissM & InterlockCurrState != STATE_STALLED;
|
2021-12-20 04:21:03 +00:00
|
|
|
lrsc lrsc(.clk, .reset, .FlushW, .CPUBusy, .MemReadM, .MemRWMtoLRSC, .AtomicMtoDCache, .MemPAdrM,
|
2021-10-23 18:41:20 +00:00
|
|
|
.SquashSCW, .MemRWMtoDCache);
|
2021-07-18 01:11:41 +00:00
|
|
|
|
2021-07-16 16:12:57 +00:00
|
|
|
// *** BUG, this is most likely wrong
|
2021-12-20 04:24:07 +00:00
|
|
|
assign CacheableMtoDCache = SelHPTW ? 1'b1 : CacheableM;
|
2021-07-16 16:12:57 +00:00
|
|
|
|
2021-07-13 22:24:59 +00:00
|
|
|
|
2021-06-23 05:41:00 +00:00
|
|
|
// Specify which type of page fault is occurring
|
2021-12-14 20:46:29 +00:00
|
|
|
// *** `MEM_VIRTMEM
|
2021-07-18 01:58:49 +00:00
|
|
|
assign DTLBLoadPageFaultM = DTLBPageFaultM & MemRWMtoLRSC[1];
|
|
|
|
assign DTLBStorePageFaultM = DTLBPageFaultM & MemRWMtoLRSC[0];
|
2021-06-23 05:41:00 +00:00
|
|
|
|
2021-06-24 18:05:22 +00:00
|
|
|
// Determine if an Unaligned access is taking place
|
2021-12-20 04:41:34 +00:00
|
|
|
// hptw guarantees alignment, only check inputs from IEU.
|
2021-06-24 18:05:22 +00:00
|
|
|
always_comb
|
2021-12-20 04:41:34 +00:00
|
|
|
case(Funct3M[1:0])
|
2021-12-20 03:34:40 +00:00
|
|
|
2'b00: DataMisalignedM = 0; // lb, sb, lbu
|
2021-12-20 04:41:34 +00:00
|
|
|
2'b01: DataMisalignedM = IEUAdrM[0]; // lh, sh, lhu
|
|
|
|
2'b10: DataMisalignedM = IEUAdrM[1] | IEUAdrM[0]; // lw, sw, flw, fsw, lwu
|
|
|
|
2'b11: DataMisalignedM = |IEUAdrM[2:0]; // ld, sd, fld, fsd
|
2021-06-24 18:05:22 +00:00
|
|
|
endcase
|
2021-06-23 05:41:00 +00:00
|
|
|
|
2021-07-09 22:14:54 +00:00
|
|
|
// Determine if address is valid
|
2021-12-20 03:34:40 +00:00
|
|
|
assign LoadMisalignedFaultM = DataMisalignedM & MemRWMtoLRSC[1];
|
|
|
|
assign StoreMisalignedFaultM = DataMisalignedM & MemRWMtoLRSC[0];
|
2021-07-09 20:16:38 +00:00
|
|
|
|
2021-12-14 20:46:29 +00:00
|
|
|
// conditional
|
2021-12-14 21:43:06 +00:00
|
|
|
// 1. ram // controlled by `MEM_DTIM
|
2021-12-14 20:46:29 +00:00
|
|
|
// 2. cache `MEM_DCACHE
|
|
|
|
// 3. wire pass-through
|
2021-12-19 22:12:31 +00:00
|
|
|
assign MemAdrE_RENAME = SelReplayCPURequest ? IEUAdrM[11:0] : MemAdrE[11:0];
|
2021-12-27 21:56:18 +00:00
|
|
|
|
2021-12-27 22:45:49 +00:00
|
|
|
localparam integer WORDSPERLINE = `DCACHE_BLOCKLENINBITS/`XLEN;
|
|
|
|
localparam integer LOGWPL = $clog2(WORDSPERLINE);
|
|
|
|
localparam integer BLOCKLEN = `DCACHE_BLOCKLENINBITS;
|
|
|
|
|
|
|
|
localparam integer FetchCountThreshold = WORDSPERLINE - 1;
|
|
|
|
localparam integer BLOCKBYTELEN = BLOCKLEN/8;
|
|
|
|
localparam integer OFFSETLEN = $clog2(BLOCKBYTELEN);
|
|
|
|
|
2021-12-27 21:56:18 +00:00
|
|
|
// temp
|
|
|
|
logic SelUncached;
|
2021-12-27 22:45:49 +00:00
|
|
|
logic FetchCountFlag;
|
|
|
|
|
2021-12-27 21:56:18 +00:00
|
|
|
logic [`XLEN-1:0] FinalAMOWriteDataM, FinalWriteDataM;
|
2021-12-27 22:45:49 +00:00
|
|
|
(* mark_debug = "true" *) logic [`XLEN-1:0] DC_HWDATA_FIXNAME;
|
2021-12-27 21:56:18 +00:00
|
|
|
logic SelFlush;
|
|
|
|
logic [`XLEN-1:0] ReadDataWordM;
|
2021-12-27 22:45:49 +00:00
|
|
|
logic [`DCACHE_BLOCKLENINBITS-1:0] DCacheMemWriteData;
|
2021-12-27 21:56:18 +00:00
|
|
|
|
|
|
|
// keep
|
|
|
|
logic [`XLEN-1:0] ReadDataWordMuxM;
|
2021-12-27 22:45:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
logic [LOGWPL-1:0] FetchCount, NextFetchCount;
|
|
|
|
logic [`PA_BITS-1:0] BasePAdrMaskedM;
|
|
|
|
logic [OFFSETLEN-1:0] BasePAdrOffsetM;
|
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
logic CntEn, PreCntEn;
|
2021-12-27 22:45:49 +00:00
|
|
|
logic CntReset;
|
|
|
|
logic [`PA_BITS-1:0] BasePAdrM;
|
|
|
|
logic [`XLEN-1:0] ReadDataBlockSetsM [(`DCACHE_BLOCKLENINBITS/`XLEN)-1:0];
|
2021-12-27 21:56:18 +00:00
|
|
|
|
2021-12-27 22:45:49 +00:00
|
|
|
|
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
logic DCWriteLine;
|
|
|
|
logic DCFetchLine;
|
|
|
|
logic BUSACK;
|
2021-12-19 22:12:31 +00:00
|
|
|
|
2021-12-20 04:47:48 +00:00
|
|
|
dcache dcache(.clk, .reset, .CPUBusy,
|
2021-12-20 03:34:40 +00:00
|
|
|
.MemRWM(MemRWMtoDCache),
|
|
|
|
.Funct3M(Funct3MtoDCache),
|
2021-12-20 04:47:48 +00:00
|
|
|
.Funct7M, .FlushDCacheM,
|
2021-12-20 03:34:40 +00:00
|
|
|
.AtomicM(AtomicMtoDCache),
|
|
|
|
.MemAdrE(MemAdrE_RENAME),
|
2021-12-20 04:47:48 +00:00
|
|
|
.MemPAdrM,
|
2021-12-20 03:34:40 +00:00
|
|
|
.VAdr(IEUAdrM[11:0]), // this will be removed once the dcache hptw interlock is removed.
|
2021-12-27 21:56:18 +00:00
|
|
|
.FinalWriteDataM, .ReadDataWordM, .DCacheStall,
|
2021-12-20 03:34:40 +00:00
|
|
|
.CommittedM(CommittedMfromDCache),
|
2021-12-20 04:47:48 +00:00
|
|
|
.DCacheMiss, .DCacheAccess, .ExceptionM, .IgnoreRequest,
|
2021-12-20 03:34:40 +00:00
|
|
|
.PendingInterruptM(PendingInterruptMtoDCache),
|
|
|
|
.CacheableM(CacheableMtoDCache),
|
|
|
|
|
2021-12-27 22:45:49 +00:00
|
|
|
.BasePAdrM,
|
|
|
|
.ReadDataBlockSetsM,
|
2021-12-27 21:56:18 +00:00
|
|
|
.SelFlush,
|
2021-12-27 22:45:49 +00:00
|
|
|
.DCacheMemWriteData,
|
2021-12-28 00:12:59 +00:00
|
|
|
.DCFetchLine,
|
|
|
|
.DCWriteLine,
|
|
|
|
.BUSACK,
|
2021-12-27 21:56:18 +00:00
|
|
|
|
2021-12-20 03:34:40 +00:00
|
|
|
// AHB connection
|
2021-12-28 00:12:59 +00:00
|
|
|
.AHBAck(1'b0),
|
2021-12-20 03:34:40 +00:00
|
|
|
.DCtoAHBSizeM
|
|
|
|
);
|
2021-06-23 05:41:00 +00:00
|
|
|
|
2021-12-27 21:56:18 +00:00
|
|
|
|
|
|
|
mux2 #(`XLEN) UnCachedDataMux(.d0(ReadDataWordM),
|
2021-12-27 22:45:49 +00:00
|
|
|
.d1(DCacheMemWriteData[`XLEN-1:0]),
|
2021-12-27 21:56:18 +00:00
|
|
|
.s(SelUncached),
|
|
|
|
.y(ReadDataWordMuxM));
|
|
|
|
|
|
|
|
// finally swr
|
|
|
|
subwordread subwordread(.ReadDataWordMuxM,
|
|
|
|
.MemPAdrM(MemPAdrM[2:0]),
|
|
|
|
.Funct3M(Funct3MtoDCache),
|
|
|
|
.ReadDataM);
|
|
|
|
|
|
|
|
generate
|
|
|
|
if (`A_SUPPORTED) begin
|
|
|
|
logic [`XLEN-1:0] AMOResult;
|
|
|
|
amoalu amoalu(.srca(ReadDataM), .srcb(WriteDataM), .funct(Funct7M), .width(Funct3MtoDCache[1:0]),
|
|
|
|
.result(AMOResult));
|
|
|
|
mux2 #(`XLEN) wdmux(WriteDataM, AMOResult, AtomicMtoDCache[1], FinalAMOWriteDataM);
|
|
|
|
end else
|
|
|
|
assign FinalAMOWriteDataM = WriteDataM;
|
|
|
|
endgenerate
|
|
|
|
|
|
|
|
subwordwrite subwordwrite(.HRDATA(ReadDataWordM),
|
|
|
|
.HADDRD(MemPAdrM[2:0]),
|
|
|
|
.HSIZED({Funct3MtoDCache[2], 1'b0, Funct3MtoDCache[1:0]}),
|
|
|
|
.HWDATAIN(FinalAMOWriteDataM),
|
|
|
|
.HWDATA(FinalWriteDataM));
|
|
|
|
|
|
|
|
assign DCtoAHBWriteData = CacheableMtoDCache | SelFlush ? DC_HWDATA_FIXNAME : WriteDataM;
|
2021-12-27 22:45:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Bus Side logic
|
|
|
|
// register the fetch data from the next level of memory.
|
|
|
|
// This register should be necessary for timing. There is no register in the uncore or
|
|
|
|
// ahblite controller between the memories and this cache.
|
|
|
|
|
|
|
|
genvar index;
|
|
|
|
generate
|
|
|
|
for (index = 0; index < WORDSPERLINE; index++) begin:fetchbuffer
|
|
|
|
flopen #(`XLEN) fb(.clk(clk),
|
|
|
|
.en(DCfromAHBAck & DCtoAHBReadM & (index == FetchCount)),
|
|
|
|
.d(DCfromAHBReadData),
|
|
|
|
.q(DCacheMemWriteData[(index+1)*`XLEN-1:index*`XLEN]));
|
|
|
|
end
|
|
|
|
endgenerate
|
|
|
|
|
|
|
|
|
|
|
|
// if not cacheable the offset bits needs to be sent to the EBU.
|
|
|
|
// if cacheable the offset bits are discarded. $ FSM will fetch the whole block.
|
|
|
|
assign BasePAdrOffsetM = CacheableM ? {{OFFSETLEN}{1'b0}} : BasePAdrM[OFFSETLEN-1:0];
|
|
|
|
assign BasePAdrMaskedM = {BasePAdrM[`PA_BITS-1:OFFSETLEN], BasePAdrOffsetM};
|
2021-12-27 21:56:18 +00:00
|
|
|
|
2021-12-27 22:45:49 +00:00
|
|
|
assign DCtoAHBPAdrM = ({{`PA_BITS-LOGWPL{1'b0}}, FetchCount} << $clog2(`XLEN/8)) + BasePAdrMaskedM;
|
|
|
|
|
|
|
|
assign DC_HWDATA_FIXNAME = ReadDataBlockSetsM[FetchCount];
|
|
|
|
|
|
|
|
assign FetchCountFlag = (FetchCount == FetchCountThreshold[LOGWPL-1:0]);
|
2021-12-28 00:12:59 +00:00
|
|
|
assign CntEn = PreCntEn & DCfromAHBAck;
|
|
|
|
|
2021-12-27 22:45:49 +00:00
|
|
|
flopenr #(LOGWPL)
|
|
|
|
FetchCountReg(.clk(clk),
|
|
|
|
.reset(reset | CntReset),
|
|
|
|
.en(CntEn),
|
|
|
|
.d(NextFetchCount),
|
|
|
|
.q(FetchCount));
|
|
|
|
|
|
|
|
assign NextFetchCount = FetchCount + 1'b1;
|
2021-12-27 21:56:18 +00:00
|
|
|
|
2021-12-28 00:12:59 +00:00
|
|
|
typedef enum {STATE_BUS_READY,
|
|
|
|
STATE_BUS_FETCH_WDV,
|
2021-12-28 17:29:16 +00:00
|
|
|
STATE_BUS_WRITE_WDV,
|
2021-12-28 00:12:59 +00:00
|
|
|
STATE_BUS_UNCACHED_WRITE,
|
|
|
|
STATE_BUS_UNCACHED_WRITE_DONE,
|
|
|
|
STATE_BUS_UNCACHED_READ,
|
|
|
|
STATE_BUS_UNCACHED_READ_DONE} busstatetype;
|
|
|
|
|
|
|
|
(* mark_debug = "true" *) busstatetype BusCurrState, BusNextState;
|
|
|
|
|
|
|
|
always_ff @(posedge clk)
|
|
|
|
if (reset) BusCurrState <= #1 STATE_BUS_READY;
|
|
|
|
else BusCurrState <= #1 BusNextState;
|
|
|
|
|
|
|
|
always_comb begin
|
|
|
|
BusNextState = STATE_BUS_READY;
|
|
|
|
CntReset = 1'b0;
|
|
|
|
BusStall = 1'b0;
|
|
|
|
PreCntEn = 1'b0;
|
|
|
|
DCtoAHBWriteM = 1'b0;
|
|
|
|
DCtoAHBReadM = 1'b0;
|
|
|
|
CommittedMfromBus = 1'b0;
|
|
|
|
BUSACK = 1'b0;
|
|
|
|
SelUncached = 1'b0;
|
|
|
|
|
|
|
|
case(BusCurrState)
|
|
|
|
STATE_BUS_READY: begin
|
2021-12-28 17:18:47 +00:00
|
|
|
if(IgnoreRequest) begin
|
|
|
|
BusNextState = STATE_BUS_READY;
|
|
|
|
end else
|
2021-12-28 00:12:59 +00:00
|
|
|
// uncache write
|
|
|
|
if(MemRWMtoDCache[0] & ~CacheableMtoDCache) begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_WRITE;
|
|
|
|
CntReset = 1'b1;
|
|
|
|
BusStall = 1'b1;
|
|
|
|
DCtoAHBWriteM = 1'b1;
|
|
|
|
end
|
|
|
|
// uncached read
|
|
|
|
else if(MemRWMtoDCache[1] & ~CacheableMtoDCache) begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_READ;
|
|
|
|
CntReset = 1'b1;
|
|
|
|
BusStall = 1'b1;
|
|
|
|
DCtoAHBReadM = 1'b1;
|
|
|
|
end
|
|
|
|
// D$ Fetch Line
|
|
|
|
else if(DCFetchLine) begin
|
|
|
|
BusNextState = STATE_BUS_FETCH_WDV;
|
|
|
|
CntReset = 1'b1;
|
|
|
|
BusStall = 1'b1;
|
|
|
|
end
|
|
|
|
// D$ Write Line
|
|
|
|
else if(DCWriteLine) begin
|
2021-12-28 17:29:16 +00:00
|
|
|
BusNextState = STATE_BUS_WRITE_WDV;
|
2021-12-28 00:12:59 +00:00
|
|
|
CntReset = 1'b1;
|
|
|
|
BusStall = 1'b1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
STATE_BUS_UNCACHED_WRITE : begin
|
|
|
|
BusStall = 1'b1;
|
|
|
|
DCtoAHBWriteM = 1'b1;
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
if(DCfromAHBAck) begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_WRITE_DONE;
|
|
|
|
end else begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_WRITE;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
STATE_BUS_UNCACHED_READ: begin
|
|
|
|
BusStall = 1'b1;
|
|
|
|
DCtoAHBReadM = 1'b1;
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
if(DCfromAHBAck) begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_READ_DONE;
|
|
|
|
end else begin
|
|
|
|
BusNextState = STATE_BUS_UNCACHED_READ;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
STATE_BUS_UNCACHED_WRITE_DONE: begin
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
BusNextState = STATE_BUS_READY;
|
|
|
|
end
|
|
|
|
|
|
|
|
STATE_BUS_UNCACHED_READ_DONE: begin
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
SelUncached = 1'b1;
|
|
|
|
end
|
|
|
|
|
|
|
|
STATE_BUS_FETCH_WDV: begin
|
|
|
|
BusStall = 1'b1;
|
|
|
|
PreCntEn = 1'b1;
|
|
|
|
DCtoAHBReadM = 1'b1;
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
|
|
|
|
if (FetchCountFlag & DCfromAHBAck) begin
|
2021-12-28 17:18:47 +00:00
|
|
|
BusNextState = STATE_BUS_READY;
|
|
|
|
BUSACK = 1'b1;
|
2021-12-28 00:12:59 +00:00
|
|
|
end else begin
|
|
|
|
BusNextState = STATE_BUS_FETCH_WDV;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-28 17:29:16 +00:00
|
|
|
STATE_BUS_WRITE_WDV: begin
|
2021-12-28 00:12:59 +00:00
|
|
|
BusStall = 1'b1;
|
|
|
|
PreCntEn = 1'b1;
|
|
|
|
DCtoAHBWriteM = 1'b1;
|
|
|
|
CommittedMfromBus = 1'b1;
|
|
|
|
if(FetchCountFlag & DCfromAHBAck) begin
|
2021-12-28 17:18:47 +00:00
|
|
|
BusNextState = STATE_BUS_READY;
|
|
|
|
BUSACK = 1'b1;
|
2021-12-28 00:12:59 +00:00
|
|
|
end else begin
|
2021-12-28 17:29:16 +00:00
|
|
|
BusNextState = STATE_BUS_WRITE_WDV;
|
2021-12-28 00:12:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
2021-06-23 05:41:00 +00:00
|
|
|
endmodule
|
|
|
|
|