2021-01-15 04:37:51 +00:00
|
|
|
|
///////////////////////////////////////////
|
2021-01-28 03:49:47 +00:00
|
|
|
|
// ifu.sv
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
|
|
|
|
// Written: David_Harris@hmc.edu 9 January 2021
|
2021-03-30 19:25:07 +00:00
|
|
|
|
// Modified:
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2021-01-28 03:49:47 +00:00
|
|
|
|
// Purpose: Instrunction Fetch Unit
|
|
|
|
|
// PC, branch prediction, instruction cache
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
|
// 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:
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or
|
|
|
|
|
// substantial portions of the Software.
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2022-01-07 12:58:40 +00:00
|
|
|
|
// 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.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-01-15 04:37:51 +00:00
|
|
|
|
|
2021-01-23 15:48:12 +00:00
|
|
|
|
`include "wally-config.vh"
|
2021-01-15 04:37:51 +00:00
|
|
|
|
|
2021-01-28 03:49:47 +00:00
|
|
|
|
module ifu (
|
2022-01-04 04:23:04 +00:00
|
|
|
|
input logic clk, reset,
|
2022-05-12 15:26:08 +00:00
|
|
|
|
input logic StallF, StallD, StallE, StallM,
|
|
|
|
|
input logic FlushF, FlushD, FlushE, FlushM,
|
2022-01-04 04:23:04 +00:00
|
|
|
|
// Bus interface
|
2022-01-07 04:30:00 +00:00
|
|
|
|
(* mark_debug = "true" *) input logic [`XLEN-1:0] IFUBusHRDATA,
|
|
|
|
|
(* mark_debug = "true" *) input logic IFUBusAck,
|
2022-06-08 22:03:15 +00:00
|
|
|
|
(* mark_debug = "true" *) input logic IFUBusInit,
|
2022-01-07 04:30:00 +00:00
|
|
|
|
(* mark_debug = "true" *) output logic [`PA_BITS-1:0] IFUBusAdr,
|
|
|
|
|
(* mark_debug = "true" *) output logic IFUBusRead,
|
|
|
|
|
(* mark_debug = "true" *) output logic IFUStallF,
|
2022-05-26 01:02:50 +00:00
|
|
|
|
(* mark_debug = "true" *) output logic [2:0] IFUBurstType,
|
2022-06-07 11:22:53 +00:00
|
|
|
|
(* mark_debug = "true" *) output logic [1:0] IFUTransType,
|
2022-06-08 22:03:15 +00:00
|
|
|
|
(* mark_debug = "true" *) output logic IFUTransComplete,
|
2022-01-04 04:23:04 +00:00
|
|
|
|
(* mark_debug = "true" *) output logic [`XLEN-1:0] PCF,
|
|
|
|
|
// Execute
|
|
|
|
|
output logic [`XLEN-1:0] PCLinkE,
|
|
|
|
|
input logic PCSrcE,
|
|
|
|
|
input logic [`XLEN-1:0] IEUAdrE,
|
|
|
|
|
output logic [`XLEN-1:0] PCE,
|
|
|
|
|
output logic BPPredWrongE,
|
|
|
|
|
// Mem
|
|
|
|
|
input logic RetM, TrapM,
|
|
|
|
|
input logic [`XLEN-1:0] PrivilegedNextPCM,
|
|
|
|
|
input logic InvalidateICacheM,
|
|
|
|
|
output logic [31:0] InstrD, InstrM,
|
|
|
|
|
output logic [`XLEN-1:0] PCM,
|
|
|
|
|
// branch predictor
|
|
|
|
|
output logic [4:0] InstrClassM,
|
|
|
|
|
output logic BPPredDirWrongM,
|
|
|
|
|
output logic BTBPredPCWrongM,
|
|
|
|
|
output logic RASPredPCWrongM,
|
|
|
|
|
output logic BPPredClassNonCFIWrongM,
|
|
|
|
|
// Faults
|
|
|
|
|
input logic IllegalBaseInstrFaultD,
|
2022-01-27 23:11:27 +00:00
|
|
|
|
output logic InstrPageFaultF,
|
2022-01-04 04:23:04 +00:00
|
|
|
|
output logic IllegalIEUInstrFaultD,
|
|
|
|
|
output logic InstrMisalignedFaultM,
|
|
|
|
|
// mmu management
|
|
|
|
|
input logic [1:0] PrivilegeModeW,
|
|
|
|
|
input logic [`XLEN-1:0] PTE,
|
|
|
|
|
input logic [1:0] PageType,
|
|
|
|
|
input logic [`XLEN-1:0] SATP_REGW,
|
|
|
|
|
input logic STATUS_MXR, STATUS_SUM, STATUS_MPRV,
|
|
|
|
|
input logic [1:0] STATUS_MPP,
|
2022-06-02 14:18:55 +00:00
|
|
|
|
input logic ITLBWriteF, sfencevmaM,
|
2022-02-17 05:37:36 +00:00
|
|
|
|
output logic ITLBMissF, InstrDAPageFaultF,
|
2022-01-04 04:23:04 +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],
|
2022-01-10 04:56:56 +00:00
|
|
|
|
output logic InstrAccessFaultF,
|
|
|
|
|
output logic ICacheAccess,
|
|
|
|
|
output logic ICacheMiss
|
2021-01-28 03:49:47 +00:00
|
|
|
|
);
|
2022-02-22 23:28:26 +00:00
|
|
|
|
localparam CACHE_ENABLED = `IMEM == `MEM_CACHE;
|
|
|
|
|
(* mark_debug = "true" *) logic [`XLEN-1:0] PCCorrectE, UnalignedPCNextF, PCNextF;
|
2022-01-28 19:19:24 +00:00
|
|
|
|
logic BranchMisalignedFaultE;
|
2021-10-27 19:43:55 +00:00
|
|
|
|
logic PrivilegedChangePCM;
|
|
|
|
|
logic IllegalCompInstrD;
|
|
|
|
|
logic [`XLEN-1:0] PCPlus2or4F, PCLinkD;
|
|
|
|
|
logic [`XLEN-3:0] PCPlusUpperF;
|
|
|
|
|
logic CompressedF;
|
2022-01-28 21:26:06 +00:00
|
|
|
|
logic [31:0] InstrRawD, InstrRawF;
|
2022-02-05 02:42:53 +00:00
|
|
|
|
logic [31:0] FinalInstrRawF;
|
2022-01-28 21:26:06 +00:00
|
|
|
|
|
2021-10-27 19:43:55 +00:00
|
|
|
|
logic [31:0] InstrE;
|
|
|
|
|
logic [`XLEN-1:0] PCD;
|
2021-10-23 19:00:32 +00:00
|
|
|
|
|
2022-01-04 04:23:04 +00:00
|
|
|
|
localparam [31:0] nop = 32'h00000013; // instruction for NOP
|
2022-02-12 06:25:12 +00:00
|
|
|
|
logic [31:0] NextInstrD, NextInstrE;
|
2021-01-28 03:49:47 +00:00
|
|
|
|
|
2022-01-04 04:23:04 +00:00
|
|
|
|
logic [`XLEN-1:0] PCBPWrongInvalidate;
|
2021-07-06 18:43:53 +00:00
|
|
|
|
|
2022-01-03 23:00:50 +00:00
|
|
|
|
(* mark_debug = "true" *) logic [`PA_BITS-1:0] PCPF; // used to either truncate or expand PCPF and PCNextF into `PA_BITS width.
|
2021-10-27 19:43:55 +00:00
|
|
|
|
logic [`XLEN+1:0] PCFExt;
|
2022-01-04 04:23:04 +00:00
|
|
|
|
|
2021-12-30 15:18:16 +00:00
|
|
|
|
logic CacheableF;
|
2022-01-27 04:33:26 +00:00
|
|
|
|
logic [`XLEN-1:0] PCNextFSpill;
|
|
|
|
|
logic [`XLEN-1:0] PCFSpill;
|
2022-01-27 16:41:57 +00:00
|
|
|
|
logic SelNextSpillF;
|
2022-01-04 00:10:15 +00:00
|
|
|
|
logic ICacheFetchLine;
|
|
|
|
|
logic BusStall;
|
2022-01-27 16:41:57 +00:00
|
|
|
|
logic ICacheStallF, IFUCacheBusStallF;
|
2022-01-04 00:10:15 +00:00
|
|
|
|
logic CPUBusy;
|
2022-01-19 20:05:14 +00:00
|
|
|
|
(* mark_debug = "true" *) logic [31:0] PostSpillInstrRawF;
|
2022-02-08 20:54:53 +00:00
|
|
|
|
// branch predictor signal
|
|
|
|
|
logic [`XLEN-1:0] PCNext1F, PCNext2F, PCNext0F;
|
2022-01-04 00:10:15 +00:00
|
|
|
|
|
2022-01-31 19:16:23 +00:00
|
|
|
|
assign PCFExt = {2'b00, PCFSpill};
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2022-02-23 04:45:00 +00:00
|
|
|
|
// Spill Support
|
2022-01-31 19:16:23 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
2022-01-14 17:13:06 +00:00
|
|
|
|
if(`C_SUPPORTED) begin : SpillSupport
|
|
|
|
|
|
2022-03-08 22:38:48 +00:00
|
|
|
|
spillsupport #(CACHE_ENABLED) spillsupport(.clk, .reset, .StallF, .PCF, .PCPlusUpperF, .PCNextF, .InstrRawF,
|
2022-02-23 16:16:12 +00:00
|
|
|
|
.InstrDAPageFaultF, .IFUCacheBusStallF, .ITLBMissF, .PCNextFSpill, .PCFSpill,
|
|
|
|
|
.SelNextSpillF, .PostSpillInstrRawF, .CompressedF);
|
2022-01-31 19:16:23 +00:00
|
|
|
|
end else begin : NoSpillSupport
|
2022-01-27 16:06:24 +00:00
|
|
|
|
assign PCNextFSpill = PCNextF;
|
|
|
|
|
assign PCFSpill = PCF;
|
|
|
|
|
assign PostSpillInstrRawF = InstrRawF;
|
2022-01-28 19:40:02 +00:00
|
|
|
|
assign {SelNextSpillF, CompressedF} = 0;
|
2022-01-14 17:13:06 +00:00
|
|
|
|
end
|
|
|
|
|
|
2022-01-28 20:02:05 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Memory management
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-06-04 15:59:14 +00:00
|
|
|
|
|
2022-01-28 20:02:05 +00:00
|
|
|
|
if(`ZICSR_SUPPORTED == 1) begin : immu
|
2022-06-02 14:18:55 +00:00
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
// sfence.vma causes TLB flushes
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
// sets ITLBFlush to pulse for one cycle of the sfence.vma instruction
|
|
|
|
|
// In this instr we want to flush the tlb and then do a pagetable walk to update the itlb and continue the program.
|
|
|
|
|
// But we're still in the stalled sfence instruction, so if itlbflushf == sfencevmaM, tlbflush would never drop and
|
|
|
|
|
// the tlbwrite would never take place after the pagetable walk. by adding in ~StallMQ, we are able to drop itlbflush
|
|
|
|
|
// after a cycle AND pulse it for another cycle on any further back-to-back sfences.
|
|
|
|
|
logic StallMQ, TLBFlush;
|
|
|
|
|
flopr #(1) StallMReg(.clk, .reset, .d(StallM), .q(StallMQ));
|
|
|
|
|
assign TLBFlush = sfencevmaM & ~StallMQ;
|
|
|
|
|
|
2022-01-28 20:02:05 +00:00
|
|
|
|
mmu #(.TLB_ENTRIES(`ITLB_ENTRIES), .IMMU(1))
|
|
|
|
|
immu(.clk, .reset, .SATP_REGW, .STATUS_MXR, .STATUS_SUM, .STATUS_MPRV, .STATUS_MPP,
|
|
|
|
|
.PrivilegeModeW, .DisableTranslation(1'b0),
|
2022-03-25 04:47:28 +00:00
|
|
|
|
.VAdr(PCFExt),
|
2022-01-28 20:02:05 +00:00
|
|
|
|
.Size(2'b10),
|
|
|
|
|
.PTE(PTE),
|
|
|
|
|
.PageTypeWriteVal(PageType),
|
|
|
|
|
.TLBWrite(ITLBWriteF),
|
2022-06-02 14:18:55 +00:00
|
|
|
|
.TLBFlush,
|
2022-01-28 20:02:05 +00:00
|
|
|
|
.PhysicalAddress(PCPF),
|
|
|
|
|
.TLBMiss(ITLBMissF),
|
|
|
|
|
.Cacheable(CacheableF), .Idempotent(), .AtomicAllowed(),
|
|
|
|
|
.InstrAccessFaultF, .LoadAccessFaultM(), .StoreAmoAccessFaultM(),
|
|
|
|
|
.InstrPageFaultF, .LoadPageFaultM(), .StoreAmoPageFaultM(),
|
|
|
|
|
.LoadMisalignedFaultM(), .StoreAmoMisalignedFaultM(),
|
2022-02-17 05:37:36 +00:00
|
|
|
|
.DAPageFault(InstrDAPageFaultF),
|
2022-01-28 20:02:05 +00:00
|
|
|
|
.AtomicAccessM(1'b0),.ExecuteAccessF(1'b1), .WriteAccessM(1'b0), .ReadAccessM(1'b0),
|
|
|
|
|
.PMPCFG_ARRAY_REGW, .PMPADDR_ARRAY_REGW);
|
|
|
|
|
|
|
|
|
|
end else begin
|
2022-02-23 16:16:12 +00:00
|
|
|
|
assign {ITLBMissF, InstrAccessFaultF, InstrPageFaultF, InstrDAPageFaultF} = '0;
|
2022-02-06 01:22:40 +00:00
|
|
|
|
assign PCPF = PCFExt[`PA_BITS-1:0];
|
2022-01-28 20:02:05 +00:00
|
|
|
|
assign CacheableF = '1;
|
|
|
|
|
end
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
2022-01-28 21:26:06 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Memory
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
2022-01-31 22:25:41 +00:00
|
|
|
|
logic [`XLEN-1:0] AllInstrRawF;
|
|
|
|
|
assign InstrRawF = AllInstrRawF[31:0];
|
2021-12-30 20:23:05 +00:00
|
|
|
|
|
2022-02-05 04:16:18 +00:00
|
|
|
|
if (`IMEM == `MEM_TIM) begin : irom // *** fix up dtim taking PA_BITS rather than XLEN, *** IEUAdr is a bad name. Probably use a ROM rather than DTIM
|
2022-07-06 22:39:35 +00:00
|
|
|
|
dtim irom(.clk, .reset, .CPUBusy, .LSURWM(2'b10), .IEUAdrM({{(`XLEN-32){1'b0}}, PCPF[31:0]}), .IEUAdrE(PCNextFSpill),
|
2022-03-11 00:44:50 +00:00
|
|
|
|
.TrapM(1'b0), .FinalWriteDataM(), .ByteMaskM('0),
|
2022-07-06 22:39:35 +00:00
|
|
|
|
.ReadDataWordM({{(`XLEN-32){1'b0}}, FinalInstrRawF}), .BusStall, .LSUBusWrite(), .LSUBusRead(IFUBusRead),
|
2022-03-12 06:46:11 +00:00
|
|
|
|
.BusCommittedM(), .DCacheStallM(ICacheStallF), .Cacheable(CacheableF),
|
2022-01-31 19:48:14 +00:00
|
|
|
|
.DCacheCommittedM(), .DCacheMiss(ICacheMiss), .DCacheAccess(ICacheAccess));
|
|
|
|
|
|
2022-03-11 21:18:56 +00:00
|
|
|
|
end
|
|
|
|
|
if (`IBUS) begin : bus
|
2022-02-22 23:28:26 +00:00
|
|
|
|
localparam integer WORDSPERLINE = (CACHE_ENABLED) ? `ICACHE_LINELENINBITS/`XLEN : 1;
|
|
|
|
|
localparam integer LINELEN = (CACHE_ENABLED) ? `ICACHE_LINELENINBITS : `XLEN;
|
2022-08-02 02:06:36 +00:00
|
|
|
|
localparam integer LOGBWPL = (`DMEM == `MEM_CACHE) ? $clog2(WORDSPERLINE) : 1;
|
2022-08-17 21:12:04 +00:00
|
|
|
|
logic [LINELEN-1:0] ILSUBusBuffer;
|
2022-01-31 19:48:14 +00:00
|
|
|
|
logic [`PA_BITS-1:0] ICacheBusAdr;
|
2022-01-31 22:25:41 +00:00
|
|
|
|
logic ICacheBusAck;
|
2022-02-16 21:43:03 +00:00
|
|
|
|
logic SelUncachedAdr;
|
2022-02-05 02:42:53 +00:00
|
|
|
|
|
2022-08-02 02:06:36 +00:00
|
|
|
|
busdp #(WORDSPERLINE, LINELEN, LOGBWPL, CACHE_ENABLED)
|
2022-01-31 22:25:41 +00:00
|
|
|
|
busdp(.clk, .reset,
|
2022-08-17 21:09:20 +00:00
|
|
|
|
.LSUBusHRDATA(IFUBusHRDATA), .LSUBusAck(IFUBusAck), .LSUBusInit(IFUBusInit), .LSUBusWrite(), .SelLSUBusWord(),
|
2022-06-08 22:03:15 +00:00
|
|
|
|
.LSUBusRead(IFUBusRead), .LSUBusSize(), .LSUBurstType(IFUBurstType), .LSUTransType(IFUTransType), .LSUTransComplete(IFUTransComplete),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.LSUFunct3M(3'b010), .LSUBusAdr(IFUBusAdr), .DCacheBusAdr(ICacheBusAdr),
|
2022-02-16 21:43:03 +00:00
|
|
|
|
.WordCount(),
|
2022-02-05 04:52:51 +00:00
|
|
|
|
.DCacheFetchLine(ICacheFetchLine),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.DCacheWriteLine(1'b0), .DCacheBusAck(ICacheBusAck),
|
2022-08-17 21:12:04 +00:00
|
|
|
|
.DLSUBusBuffer(ILSUBusBuffer), .LSUPAdrM(PCPF),
|
2022-03-08 22:58:26 +00:00
|
|
|
|
.SelUncachedAdr,
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.IgnoreRequest(ITLBMissF), .LSURWM(2'b10), .CPUBusy, .CacheableM(CacheableF),
|
|
|
|
|
.BusStall, .BusCommittedM());
|
2022-02-05 02:42:53 +00:00
|
|
|
|
|
2022-08-17 21:12:04 +00:00
|
|
|
|
mux2 #(32) UnCachedDataMux(.d0(FinalInstrRawF), .d1(ILSUBusBuffer[32-1:0]),
|
2022-02-16 21:43:03 +00:00
|
|
|
|
.s(SelUncachedAdr), .y(AllInstrRawF[31:0]));
|
|
|
|
|
|
|
|
|
|
|
2022-02-22 23:28:26 +00:00
|
|
|
|
if(CACHE_ENABLED) begin : icache
|
2022-01-28 20:27:11 +00:00
|
|
|
|
cache #(.LINELEN(`ICACHE_LINELENINBITS),
|
|
|
|
|
.NUMLINES(`ICACHE_WAYSIZEINBYTES*8/`ICACHE_LINELENINBITS),
|
2022-08-02 02:06:36 +00:00
|
|
|
|
.NUMWAYS(`ICACHE_NUMWAYS), .LOGBWPL(LOGBWPL), .WORDLEN(32), .MUXINTERVAL(16), .DCACHE(0))
|
2022-04-12 22:56:23 +00:00
|
|
|
|
icache(.clk, .reset, .CPUBusy, .IgnoreRequestTLB(ITLBMissF), .TrapM(TrapM), .IgnoreRequestTrapM('0),
|
2022-08-17 21:12:04 +00:00
|
|
|
|
.LSUBusBuffer(ILSUBusBuffer), .CacheBusAck(ICacheBusAck),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.CacheBusAdr(ICacheBusAdr), .CacheStall(ICacheStallF),
|
2022-08-02 02:22:48 +00:00
|
|
|
|
.CacheFetchLine(ICacheFetchLine),
|
2022-03-11 17:03:36 +00:00
|
|
|
|
.CacheWriteLine(), .ReadDataWord(FinalInstrRawF),
|
|
|
|
|
.Cacheable(CacheableF),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.CacheMiss(ICacheMiss), .CacheAccess(ICacheAccess),
|
2022-08-17 21:09:20 +00:00
|
|
|
|
.ByteMask('0), .WordCount('0), .SelLSUBusWord('0),
|
2022-01-28 20:27:11 +00:00
|
|
|
|
.FinalWriteData('0),
|
2022-03-08 22:34:02 +00:00
|
|
|
|
.RW(2'b10),
|
2022-02-01 20:32:27 +00:00
|
|
|
|
.Atomic('0), .FlushCache('0),
|
2022-01-28 20:27:11 +00:00
|
|
|
|
.NextAdr(PCNextFSpill[11:0]),
|
|
|
|
|
.PAdr(PCPF),
|
2022-07-23 04:36:27 +00:00
|
|
|
|
.CacheCommitted(), .InvalidateCache(InvalidateICacheM));
|
2022-01-28 20:27:11 +00:00
|
|
|
|
|
|
|
|
|
end else begin : passthrough
|
2022-01-31 22:25:41 +00:00
|
|
|
|
assign {ICacheFetchLine, ICacheBusAdr, ICacheStallF, FinalInstrRawF} = '0;
|
|
|
|
|
assign ICacheAccess = CacheableF; assign ICacheMiss = CacheableF;
|
2022-01-28 20:27:11 +00:00
|
|
|
|
end
|
2022-03-11 21:18:56 +00:00
|
|
|
|
end else begin : nobus // block: bus
|
|
|
|
|
assign AllInstrRawF = FinalInstrRawF;
|
|
|
|
|
end
|
2022-01-28 20:27:11 +00:00
|
|
|
|
|
2022-01-27 16:41:57 +00:00
|
|
|
|
assign IFUCacheBusStallF = ICacheStallF | BusStall;
|
|
|
|
|
assign IFUStallF = IFUCacheBusStallF | SelNextSpillF;
|
|
|
|
|
assign CPUBusy = StallF & ~SelNextSpillF;
|
2021-05-03 17:03:17 +00:00
|
|
|
|
|
2022-05-05 02:03:44 +00:00
|
|
|
|
flopenl #(32) AlignedInstrRawDFlop(clk, reset, ~StallD, FlushD ? nop : PostSpillInstrRawF, nop, InstrRawD);
|
2021-05-03 17:03:17 +00:00
|
|
|
|
|
2022-02-23 04:45:00 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// PCNextF logic
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
|
assign PrivilegedChangePCM = RetM | TrapM;
|
|
|
|
|
|
2022-02-08 20:54:53 +00:00
|
|
|
|
mux2 #(`XLEN) pcmux1(.d0(PCNext0F), .d1(PCCorrectE), .s(BPPredWrongE), .y(PCNext1F));
|
2022-02-23 04:45:00 +00:00
|
|
|
|
if(CACHE_ENABLED)
|
|
|
|
|
mux2 #(`XLEN) pcmux2(.d0(PCNext1F), .d1(PCBPWrongInvalidate), .s(InvalidateICacheM),
|
|
|
|
|
.y(PCNext2F));
|
|
|
|
|
else assign PCNext2F = PCNext1F;
|
|
|
|
|
if(`ZICSR_SUPPORTED)
|
|
|
|
|
mux2 #(`XLEN) pcmux3(.d0(PCNext2F), .d1(PrivilegedNextPCM), .s(PrivilegedChangePCM),
|
|
|
|
|
.y(UnalignedPCNextF));
|
|
|
|
|
else assign UnalignedPCNextF = PCNext2F;
|
2021-12-21 17:29:28 +00:00
|
|
|
|
|
2021-01-23 15:48:12 +00:00
|
|
|
|
assign PCNextF = {UnalignedPCNextF[`XLEN-1:1], 1'b0}; // hart-SPEC p. 21 about 16-bit alignment
|
2022-01-14 17:19:12 +00:00
|
|
|
|
flopenl #(`XLEN) pcreg(clk, reset, ~StallF, PCNextF, `RESET_VECTOR, PCF);
|
2021-01-19 01:16:53 +00:00
|
|
|
|
|
2022-02-23 04:45:00 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Branch and Jump Predictor
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-01-14 17:19:12 +00:00
|
|
|
|
if (`BPRED_ENABLED) begin : bpred
|
2022-02-08 20:17:44 +00:00
|
|
|
|
logic BPPredWrongM;
|
|
|
|
|
logic SelBPPredF;
|
2022-02-08 20:54:53 +00:00
|
|
|
|
logic [`XLEN-1:0] BPPredPCF;
|
2022-01-05 16:25:08 +00:00
|
|
|
|
bpred bpred(.clk, .reset,
|
2022-02-01 20:32:27 +00:00
|
|
|
|
.StallF, .StallD, .StallE, .StallM,
|
|
|
|
|
.FlushF, .FlushD, .FlushE, .FlushM,
|
|
|
|
|
.InstrD, .PCNextF, .BPPredPCF, .SelBPPredF, .PCE, .PCSrcE, .IEUAdrE,
|
|
|
|
|
.PCD, .PCLinkE, .InstrClassM, .BPPredWrongE, .BPPredWrongM,
|
|
|
|
|
.BPPredDirWrongM, .BTBPredPCWrongM, .RASPredPCWrongM, .BPPredClassNonCFIWrongM);
|
2022-02-08 20:17:44 +00:00
|
|
|
|
|
|
|
|
|
mux2 #(`XLEN) pcmux0(.d0(PCPlus2or4F), .d1(BPPredPCF), .s(SelBPPredF), .y(PCNext0F));
|
|
|
|
|
// Mux only required on instruction class miss prediction.
|
|
|
|
|
mux2 #(`XLEN) pcmuxBPWrongInvalidateFlush(.d0(PCE), .d1(PCF),
|
|
|
|
|
.s(BPPredWrongM), .y(PCBPWrongInvalidate));
|
2022-02-08 20:54:53 +00:00
|
|
|
|
mux2 #(`XLEN) pccorrectemux(.d0(PCLinkE), .d1(IEUAdrE), .s(PCSrcE), .y(PCCorrectE));
|
2022-02-01 20:32:27 +00:00
|
|
|
|
|
2022-01-05 16:25:08 +00:00
|
|
|
|
end else begin : bpred
|
2022-01-26 23:37:04 +00:00
|
|
|
|
assign BPPredWrongE = PCSrcE;
|
2022-02-01 20:32:27 +00:00
|
|
|
|
assign {BPPredDirWrongM, BTBPredPCWrongM, RASPredPCWrongM, BPPredClassNonCFIWrongM} = '0;
|
2022-02-08 20:17:44 +00:00
|
|
|
|
assign PCNext0F = PCPlus2or4F;
|
2022-02-08 20:54:53 +00:00
|
|
|
|
assign PCCorrectE = IEUAdrE;
|
|
|
|
|
assign PCBPWrongInvalidate = PCE;
|
2022-01-05 16:25:08 +00:00
|
|
|
|
end
|
2021-02-18 04:19:17 +00:00
|
|
|
|
|
2021-01-19 01:16:53 +00:00
|
|
|
|
// pcadder
|
|
|
|
|
// add 2 or 4 to the PC, based on whether the instruction is 16 bits or 32
|
2021-01-23 15:48:12 +00:00
|
|
|
|
assign PCPlusUpperF = PCF[`XLEN-1:2] + 1; // add 4 to PC
|
2022-01-27 16:06:24 +00:00
|
|
|
|
// choose PC+2 or PC+4 based on CompressedF, which arrives later.
|
|
|
|
|
// Speeds up critical path as compared to selecting adder input based on CompressedF
|
2021-01-19 01:16:53 +00:00
|
|
|
|
always_comb
|
|
|
|
|
if (CompressedF) // add 2
|
|
|
|
|
if (PCF[1]) PCPlus2or4F = {PCPlusUpperF, 2'b00};
|
2021-01-23 15:48:12 +00:00
|
|
|
|
else PCPlus2or4F = {PCF[`XLEN-1:2], 2'b10};
|
2021-01-19 01:16:53 +00:00
|
|
|
|
else PCPlus2or4F = {PCPlusUpperF, PCF[1:0]}; // add 4
|
2021-01-15 04:37:51 +00:00
|
|
|
|
|
2022-02-23 04:45:00 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Decode stage pipeline register and compressed instruction decoding.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-01-28 03:49:47 +00:00
|
|
|
|
// Decode stage pipeline register and logic
|
|
|
|
|
flopenrc #(`XLEN) PCDReg(clk, reset, FlushD, ~StallD, PCF, PCD);
|
|
|
|
|
|
2021-01-28 05:22:05 +00:00
|
|
|
|
// expand 16-bit compressed instructions to 32 bits
|
2021-12-02 18:32:35 +00:00
|
|
|
|
decompress decomp(.InstrRawD, .InstrD, .IllegalCompInstrD);
|
2021-01-28 03:49:47 +00:00
|
|
|
|
assign IllegalIEUInstrFaultD = IllegalBaseInstrFaultD | IllegalCompInstrD; // illegal if bad 32 or 16-bit instr
|
2021-03-04 15:23:35 +00:00
|
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
|
// Misaligned PC logic
|
2022-01-28 19:19:24 +00:00
|
|
|
|
// Instruction address misalignement only from br/jal(r) instructions.
|
2021-12-30 20:23:05 +00:00
|
|
|
|
// instruction address misalignment is generated by the target of control flow instructions, not
|
|
|
|
|
// the fetch itself.
|
2022-01-28 19:19:24 +00:00
|
|
|
|
// xret and Traps both cannot produce instruction misaligned.
|
|
|
|
|
// xret: mepc is an MXLEN-bit read/write register formatted as shown in Figure 3.21.
|
|
|
|
|
// The low bit of mepc (mepc[0]) is always zero. On implementations that support
|
|
|
|
|
// only IALIGN=32, the two low bits (mepc[1:0]) are always zero.
|
|
|
|
|
// Spec 3.1.14
|
|
|
|
|
// Traps: Can’t happen. The bottom two bits of MTVEC are ignored so the trap always is to a multiple of 4. See 3.1.7 of the privileged spec.
|
|
|
|
|
assign BranchMisalignedFaultE = (IEUAdrE[1] & ~`C_SUPPORTED) & PCSrcE;
|
|
|
|
|
flopenr #(1) InstrMisalginedReg(clk, reset, ~StallM, BranchMisalignedFaultE, InstrMisalignedFaultM);
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
|
|
|
|
// Instruction and PC/PCLink pipeline registers
|
2022-02-12 06:25:12 +00:00
|
|
|
|
mux2 #(32) FlushInstrEMux(InstrD, nop, FlushE, NextInstrD);
|
|
|
|
|
mux2 #(32) FlushInstrMMux(InstrE, nop, FlushM, NextInstrE);
|
|
|
|
|
flopenr #(32) InstrEReg(clk, reset, ~StallE, NextInstrD, InstrE);
|
|
|
|
|
flopenr #(32) InstrMReg(clk, reset, ~StallM, NextInstrE, InstrM);
|
2021-02-08 04:21:55 +00:00
|
|
|
|
flopenr #(`XLEN) PCEReg(clk, reset, ~StallE, PCD, PCE);
|
|
|
|
|
flopenr #(`XLEN) PCMReg(clk, reset, ~StallM, PCE, PCM);
|
|
|
|
|
flopenr #(`XLEN) PCPDReg(clk, reset, ~StallD, PCPlus2or4F, PCLinkD);
|
|
|
|
|
flopenr #(`XLEN) PCPEReg(clk, reset, ~StallE, PCLinkD, PCLinkE);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
endmodule
|