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"
|
|
|
|
|
|
|
|
// *** Ross Thompson amo misalignment check?
|
2021-07-06 15:41:36 +00:00
|
|
|
module lsu
|
|
|
|
(
|
|
|
|
input logic clk, reset,
|
|
|
|
input logic StallM, FlushM, StallW, FlushW,
|
2021-07-09 20:16:38 +00:00
|
|
|
output logic LSUStall,
|
2021-07-06 15:41:36 +00:00
|
|
|
// Memory Stage
|
|
|
|
|
|
|
|
// connected to cpu (controls)
|
|
|
|
input logic [1:0] MemRWM,
|
|
|
|
input logic [2:0] Funct3M,
|
2021-07-09 20:16:38 +00:00
|
|
|
input logic [6:0] Funct7M,
|
2021-07-06 15:41:36 +00:00
|
|
|
input logic [1:0] AtomicM,
|
2021-07-14 20:00:33 +00:00
|
|
|
input logic ExceptionM,
|
|
|
|
input logic PendingInterruptM,
|
2021-09-15 17:14:00 +00:00
|
|
|
input logic FlushDCacheM,
|
2021-07-06 15:41:36 +00:00
|
|
|
output logic CommittedM,
|
|
|
|
output logic SquashSCW,
|
2021-07-20 03:12:20 +00:00
|
|
|
output logic DCacheMiss,
|
|
|
|
output logic DCacheAccess,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// address and write data
|
|
|
|
input logic [`XLEN-1:0] MemAdrM,
|
2021-07-09 20:16:38 +00:00
|
|
|
input logic [`XLEN-1:0] MemAdrE,
|
2021-07-06 15:41:36 +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
|
|
|
|
input logic [1:0] PrivilegeModeW,
|
|
|
|
input logic DTLBFlushM,
|
|
|
|
// faults
|
|
|
|
output logic DTLBLoadPageFaultM, DTLBStorePageFaultM,
|
|
|
|
output logic LoadMisalignedFaultM, LoadAccessFaultM,
|
|
|
|
// cpu hazard unit (trap)
|
|
|
|
output logic StoreMisalignedFaultM, StoreAccessFaultM,
|
|
|
|
|
|
|
|
// connect to ahb
|
2021-07-13 22:24:59 +00:00
|
|
|
output logic [`PA_BITS-1:0] DCtoAHBPAdrM,
|
2021-07-09 20:16:38 +00:00
|
|
|
output logic DCtoAHBReadM,
|
|
|
|
output logic DCtoAHBWriteM,
|
2021-07-13 22:24:59 +00:00
|
|
|
input logic DCfromAHBAck,
|
|
|
|
input logic [`XLEN-1:0] DCfromAHBReadData,
|
|
|
|
output logic [`XLEN-1:0] DCtoAHBWriteData,
|
|
|
|
output logic [2:0] DCtoAHBSizeM,
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
// mmu management
|
|
|
|
|
|
|
|
// page table walker
|
|
|
|
input logic [`XLEN-1:0] SATP_REGW, // from csr
|
|
|
|
input logic STATUS_MXR, STATUS_SUM, STATUS_MPRV,
|
|
|
|
input logic [1:0] STATUS_MPP,
|
|
|
|
|
|
|
|
input logic [`XLEN-1:0] PCF,
|
|
|
|
input logic ITLBMissF,
|
2021-07-17 19:01:01 +00:00
|
|
|
output logic [`XLEN-1:0] PTE,
|
2021-07-17 06:31:23 +00:00
|
|
|
output logic [1:0] PageType,
|
2021-07-06 15:41:36 +00:00
|
|
|
output logic ITLBWriteF,
|
|
|
|
output logic WalkerInstrPageFaultF,
|
|
|
|
output logic WalkerLoadPageFaultM,
|
|
|
|
output logic WalkerStorePageFaultM,
|
|
|
|
|
|
|
|
input var logic [7:0] PMPCFG_ARRAY_REGW[`PMP_ENTRIES-1:0],
|
2021-07-09 20:16:38 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
logic DTLBPageFaultM;
|
2021-10-23 19:10:13 +00:00
|
|
|
logic DataMisalignedM;
|
2021-07-06 15:41:36 +00:00
|
|
|
|
|
|
|
|
2021-12-17 20:40:25 +00:00
|
|
|
(* mark_debug = "true" *) logic [`PA_BITS-1:0] MemPAdrM; // from mmu to dcache
|
2021-07-15 16:55:20 +00:00
|
|
|
|
2021-07-06 15:41:36 +00:00
|
|
|
logic DTLBMissM;
|
|
|
|
logic DTLBWriteM;
|
|
|
|
logic HPTWStall;
|
2021-08-10 18:36:21 +00:00
|
|
|
logic [`PA_BITS-1:0] TranslationPAdr;
|
2021-07-06 15:41:36 +00:00
|
|
|
logic HPTWRead;
|
2021-07-08 23:03:52 +00:00
|
|
|
logic [1:0] MemRWMtoDCache;
|
2021-07-18 01:58:49 +00:00
|
|
|
logic [1:0] MemRWMtoLRSC;
|
2021-07-09 20:16:38 +00:00
|
|
|
logic [2:0] Funct3MtoDCache;
|
2021-07-08 23:03:52 +00:00
|
|
|
logic [1:0] AtomicMtoDCache;
|
2021-07-19 15:33:27 +00:00
|
|
|
logic [`PA_BITS-1:0] MemPAdrMtoDCache;
|
|
|
|
logic [11:0] MemAdrEtoDCache;
|
2021-07-08 23:03:52 +00:00
|
|
|
logic StallWtoDCache;
|
2021-08-10 18:36:21 +00:00
|
|
|
logic MemReadM;
|
2021-07-08 23:03:52 +00:00
|
|
|
logic DataMisalignedMfromDCache;
|
2021-07-06 15:41:36 +00:00
|
|
|
logic DisableTranslation; // used to stop intermediate PTE physical addresses being saved to TLB.
|
2021-07-09 20:16:38 +00:00
|
|
|
logic DCacheStall;
|
2021-07-04 18:49:38 +00:00
|
|
|
|
2021-07-13 22:24:59 +00:00
|
|
|
logic CacheableM;
|
2021-07-15 03:26:07 +00:00
|
|
|
logic CacheableMtoDCache;
|
|
|
|
logic SelPTW;
|
2021-07-04 18:49:38 +00:00
|
|
|
|
2021-07-14 21:18:09 +00:00
|
|
|
logic CommittedMfromDCache;
|
|
|
|
logic PendingInterruptMtoDCache;
|
2021-10-23 19:00:32 +00:00
|
|
|
// logic FlushWtoDCache;
|
2021-07-16 17:22:13 +00:00
|
|
|
logic WalkerPageFaultM;
|
2021-07-23 00:42:19 +00:00
|
|
|
|
2021-07-26 04:14:28 +00:00
|
|
|
logic AnyCPUReqM;
|
|
|
|
logic MemAfterIWalkDone;
|
|
|
|
|
|
|
|
assign AnyCPUReqM = (|MemRWM) | (|AtomicM);
|
2021-12-14 20:46:29 +00:00
|
|
|
|
|
|
|
// *** add generate to conditionally create hptw, lsuArb, and mmu
|
|
|
|
// based on `MEM_VIRTMEM
|
2021-07-26 04:14:28 +00:00
|
|
|
hptw hptw(.clk(clk),
|
2021-07-19 15:33:27 +00:00
|
|
|
.reset(reset),
|
|
|
|
.SATP_REGW(SATP_REGW),
|
|
|
|
.PCF(PCF),
|
|
|
|
.MemAdrM(MemAdrM),
|
2021-11-21 04:35:47 +00:00
|
|
|
.ITLBMissF(ITLBMissF & ~PendingInterruptM),
|
|
|
|
.DTLBMissM(DTLBMissM & ~PendingInterruptM),
|
2021-07-19 15:33:27 +00:00
|
|
|
.MemRWM(MemRWM),
|
|
|
|
.PTE(PTE),
|
|
|
|
.PageType,
|
|
|
|
.ITLBWriteF(ITLBWriteF),
|
|
|
|
.DTLBWriteM(DTLBWriteM),
|
2021-08-10 18:36:21 +00:00
|
|
|
.HPTWReadPTE(ReadDataM),
|
2021-12-17 20:38:25 +00:00
|
|
|
.DCacheStall(DCacheStall),
|
|
|
|
.TranslationPAdr,
|
2021-07-19 15:33:27 +00:00
|
|
|
.HPTWRead(HPTWRead),
|
|
|
|
.SelPTW(SelPTW),
|
2021-12-17 20:38:25 +00:00
|
|
|
.HPTWStall,
|
2021-07-26 04:14:28 +00:00
|
|
|
.AnyCPUReqM,
|
|
|
|
.MemAfterIWalkDone,
|
2021-07-19 15:33:27 +00:00
|
|
|
.WalkerInstrPageFaultF(WalkerInstrPageFaultF),
|
|
|
|
.WalkerLoadPageFaultM(WalkerLoadPageFaultM),
|
|
|
|
.WalkerStorePageFaultM(WalkerStorePageFaultM));
|
2021-07-17 18:48:44 +00:00
|
|
|
|
2021-12-17 20:38:25 +00:00
|
|
|
assign LSUStall = DCacheStall | HPTWStall;
|
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-07-04 18:49:38 +00:00
|
|
|
lsuArb arbiter(.clk(clk),
|
|
|
|
// HPTW connection
|
2021-07-16 16:12:57 +00:00
|
|
|
.SelPTW(SelPTW),
|
2021-07-04 18:49:38 +00:00
|
|
|
.HPTWRead(HPTWRead),
|
2021-07-19 15:33:27 +00:00
|
|
|
.TranslationPAdrE(TranslationPAdr),
|
2021-07-04 18:49:38 +00:00
|
|
|
// CPU connection
|
|
|
|
.MemRWM(MemRWM),
|
|
|
|
.Funct3M(Funct3M),
|
|
|
|
.AtomicM(AtomicM),
|
|
|
|
.MemAdrM(MemAdrM),
|
2021-10-24 20:04:20 +00:00
|
|
|
.MemAdrE(MemAdrE[11:0]),
|
2021-07-14 21:18:09 +00:00
|
|
|
.CommittedM(CommittedM),
|
|
|
|
.PendingInterruptM(PendingInterruptM),
|
2021-07-04 18:49:38 +00:00
|
|
|
.StallW(StallW),
|
|
|
|
.DataMisalignedM(DataMisalignedM),
|
2021-07-09 20:16:38 +00:00
|
|
|
// DCACHE
|
2021-07-04 18:49:38 +00:00
|
|
|
.DisableTranslation(DisableTranslation),
|
2021-07-18 01:58:49 +00:00
|
|
|
.MemRWMtoLRSC(MemRWMtoLRSC),
|
2021-07-09 20:16:38 +00:00
|
|
|
.Funct3MtoDCache(Funct3MtoDCache),
|
2021-07-08 23:03:52 +00:00
|
|
|
.AtomicMtoDCache(AtomicMtoDCache),
|
2021-07-19 15:33:27 +00:00
|
|
|
.MemPAdrMtoDCache(MemPAdrMtoDCache),
|
2021-07-15 03:26:07 +00:00
|
|
|
.MemAdrEtoDCache(MemAdrEtoDCache),
|
2021-07-08 23:03:52 +00:00
|
|
|
.StallWtoDCache(StallWtoDCache),
|
|
|
|
.DataMisalignedMfromDCache(DataMisalignedMfromDCache),
|
2021-07-14 21:18:09 +00:00
|
|
|
.CommittedMfromDCache(CommittedMfromDCache),
|
|
|
|
.PendingInterruptMtoDCache(PendingInterruptMtoDCache),
|
2021-07-16 16:12:57 +00:00
|
|
|
.DCacheStall(DCacheStall));
|
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,
|
|
|
|
.PrivilegeModeW, .DisableTranslation(DisableTranslation),
|
|
|
|
.PAdr(MemPAdrMtoDCache),
|
2021-07-19 15:33:27 +00:00
|
|
|
.VAdr(MemAdrM),
|
2021-07-09 20:16:38 +00:00
|
|
|
.Size(Funct3MtoDCache[1:0]),
|
2021-07-17 19:01:01 +00:00
|
|
|
.PTE(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),
|
|
|
|
.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
|
|
|
|
//.AtomicAccessM(AtomicMaskedM[1]),
|
|
|
|
); // *** 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-07-18 01:58:49 +00:00
|
|
|
assign MemReadM = MemRWMtoLRSC[1] & ~(ExceptionM | PendingInterruptMtoDCache) & ~DTLBMissM; // & ~NonBusTrapM & ~DTLBMissM & CurrState != STATE_STALLED;
|
|
|
|
lrsc lrsc(.clk, .reset, .FlushW, .StallWtoDCache, .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-07-15 03:26:07 +00:00
|
|
|
assign CacheableMtoDCache = SelPTW ? 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
|
|
|
|
always_comb
|
2021-07-09 20:16:38 +00:00
|
|
|
case(Funct3MtoDCache[1:0])
|
2021-07-08 23:03:52 +00:00
|
|
|
2'b00: DataMisalignedMfromDCache = 0; // lb, sb, lbu
|
2021-07-19 15:33:27 +00:00
|
|
|
2'b01: DataMisalignedMfromDCache = MemPAdrMtoDCache[0]; // lh, sh, lhu
|
|
|
|
2'b10: DataMisalignedMfromDCache = MemPAdrMtoDCache[1] | MemPAdrMtoDCache[0]; // lw, sw, flw, fsw, lwu
|
|
|
|
2'b11: DataMisalignedMfromDCache = |MemPAdrMtoDCache[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-07-18 01:58:49 +00:00
|
|
|
assign LoadMisalignedFaultM = DataMisalignedMfromDCache & MemRWMtoLRSC[1];
|
|
|
|
assign StoreMisalignedFaultM = DataMisalignedMfromDCache & 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-07-09 20:16:38 +00:00
|
|
|
dcache dcache(.clk(clk),
|
|
|
|
.reset(reset),
|
2021-07-22 19:51:14 +00:00
|
|
|
.StallWtoDCache(StallWtoDCache),
|
2021-07-09 20:16:38 +00:00
|
|
|
.MemRWM(MemRWMtoDCache),
|
|
|
|
.Funct3M(Funct3MtoDCache),
|
2021-09-16 23:32:29 +00:00
|
|
|
.Funct7M(Funct7M),
|
|
|
|
.FlushDCacheM,
|
2021-07-09 20:16:38 +00:00
|
|
|
.AtomicM(AtomicMtoDCache),
|
2021-07-15 03:26:07 +00:00
|
|
|
.MemAdrE(MemAdrEtoDCache),
|
2021-07-09 20:16:38 +00:00
|
|
|
.MemPAdrM(MemPAdrM),
|
2021-07-21 19:55:09 +00:00
|
|
|
.VAdr(MemAdrM[11:0]),
|
2021-07-09 20:16:38 +00:00
|
|
|
.WriteDataM(WriteDataM),
|
2021-07-22 19:51:14 +00:00
|
|
|
.ReadDataM(ReadDataM),
|
2021-07-09 20:16:38 +00:00
|
|
|
.DCacheStall(DCacheStall),
|
2021-07-14 21:18:09 +00:00
|
|
|
.CommittedM(CommittedMfromDCache),
|
2021-07-20 03:12:20 +00:00
|
|
|
.DCacheMiss,
|
|
|
|
.DCacheAccess,
|
2021-07-14 20:00:33 +00:00
|
|
|
.ExceptionM(ExceptionM),
|
2021-07-18 01:58:49 +00:00
|
|
|
.PendingInterruptM(PendingInterruptMtoDCache),
|
2021-07-09 20:16:38 +00:00
|
|
|
.DTLBMissM(DTLBMissM),
|
2021-07-15 03:26:07 +00:00
|
|
|
.CacheableM(CacheableMtoDCache),
|
2021-07-14 22:23:28 +00:00
|
|
|
.DTLBWriteM(DTLBWriteM),
|
2021-07-26 04:14:28 +00:00
|
|
|
.ITLBWriteF(ITLBWriteF),
|
|
|
|
.ITLBMissF,
|
|
|
|
.MemAfterIWalkDone,
|
2021-07-16 16:12:57 +00:00
|
|
|
.SelPTW(SelPTW),
|
2021-07-16 17:22:13 +00:00
|
|
|
.WalkerPageFaultM(WalkerPageFaultM),
|
2021-07-26 04:14:28 +00:00
|
|
|
.WalkerInstrPageFaultF(WalkerInstrPageFaultF),
|
2021-07-09 20:16:38 +00:00
|
|
|
|
|
|
|
// AHB connection
|
|
|
|
.AHBPAdr(DCtoAHBPAdrM),
|
|
|
|
.AHBRead(DCtoAHBReadM),
|
|
|
|
.AHBWrite(DCtoAHBWriteM),
|
|
|
|
.AHBAck(DCfromAHBAck),
|
|
|
|
.HWDATA(DCtoAHBWriteData),
|
2021-09-17 18:03:04 +00:00
|
|
|
.HRDATA(DCfromAHBReadData),
|
|
|
|
.DCtoAHBSizeM
|
2021-07-09 20:16:38 +00:00
|
|
|
);
|
2021-06-23 05:41:00 +00:00
|
|
|
|
|
|
|
endmodule
|
|
|
|
|