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
|
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
|
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
|
|
|
|
|
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
|
|
|
|
|
// may obtain a copy of the License at
|
2021-01-15 04:37:51 +00:00
|
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
|
// https://solderpad.org/licenses/SHL-2.1/
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, any work distributed under the
|
|
|
|
|
// License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
|
// and limitations under the License.
|
2022-01-07 12:58:40 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
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 (
|
2023-01-21 00:47:36 +00:00
|
|
|
|
input logic clk, reset,
|
|
|
|
|
input logic StallF, StallD, StallE, StallM, StallW,
|
|
|
|
|
input logic FlushD, FlushE, FlushM, FlushW,
|
|
|
|
|
output logic IFUStallF, // IFU stalsl pipeline during a multicycle operation
|
2023-01-20 17:51:10 +00:00
|
|
|
|
// Command from CPU
|
2023-01-21 00:47:36 +00:00
|
|
|
|
input logic InvalidateICacheM, // Clears all instruction cache valid bits
|
|
|
|
|
input logic CSRWriteFenceM, // CSR write or fence instruction, PCNextF = the next valid PC (typically PCE)
|
2022-01-04 04:23:04 +00:00
|
|
|
|
// Bus interface
|
2023-01-21 00:47:36 +00:00
|
|
|
|
output logic [`PA_BITS-1:0] IFUHADDR, // Bus address from IFU to EBU
|
|
|
|
|
input logic [`XLEN-1:0] HRDATA, // Bus read data from IFU to EBU
|
|
|
|
|
input logic IFUHREADY, // Bus ready from IFU to EBU
|
|
|
|
|
output logic IFUHWRITE, // Bus write operation from IFU to EBU
|
|
|
|
|
output logic [2:0] IFUHSIZE, // Bus operation size from IFU to EBU
|
|
|
|
|
output logic [2:0] IFUHBURST, // Bus burst from IFU to EBU
|
|
|
|
|
output logic [1:0] IFUHTRANS, // Bus transaction type from IFU to EBU
|
2023-01-20 17:51:10 +00:00
|
|
|
|
|
2023-01-21 00:47:36 +00:00
|
|
|
|
output logic [`XLEN-1:0] PCF, // Fetch stage instruction address
|
|
|
|
|
// Execute
|
2023-01-20 17:51:10 +00:00
|
|
|
|
output logic [`XLEN-1:0] PCLinkE, // The address following the branch instruction. (AKA Fall through address)
|
2023-01-21 00:47:36 +00:00
|
|
|
|
input logic PCSrcE, // Executation stage branch is taken
|
|
|
|
|
input logic [`XLEN-1:0] IEUAdrE, // The branch/jump target address
|
2023-01-20 17:51:10 +00:00
|
|
|
|
output logic [`XLEN-1:0] PCE, // Execution stage instruction address
|
|
|
|
|
output logic BPPredWrongE, // Prediction is wrong
|
2023-01-21 00:47:36 +00:00
|
|
|
|
// Mem
|
2023-01-20 17:51:10 +00:00
|
|
|
|
output logic CommittedF, // I$ or bus memory operation started, delay interrupts
|
2023-01-21 00:47:36 +00:00
|
|
|
|
input logic [`XLEN-1:0] UnalignedPCNextF, // The next PCF, but not aligned to 2 bytes.
|
2023-01-20 17:51:10 +00:00
|
|
|
|
output logic [`XLEN-1:0] PCNext2F, // Selected PC between branch prediction and next valid PC if CSRWriteFence
|
|
|
|
|
output logic [31:0] InstrD, // The decoded instruction in Decode stage
|
|
|
|
|
output logic [31:0] InstrM, // The decoded instruction in Memory stage
|
|
|
|
|
output logic [`XLEN-1:0] PCM, // Memory stage instruction address
|
|
|
|
|
// branch predictor
|
|
|
|
|
output logic [3:0] InstrClassM, // The valid instruction class. 1-hot encoded as jalr, ret, jr (not ret), j, br
|
|
|
|
|
output logic DirPredictionWrongM, // Prediction direction is wrong
|
|
|
|
|
output logic BTBPredPCWrongM, // Prediction target wrong
|
|
|
|
|
output logic RASPredPCWrongM, // RAS prediction is wrong
|
|
|
|
|
output logic PredictionInstrClassWrongM, // Class prediction is wrong
|
|
|
|
|
// Faults
|
|
|
|
|
input logic IllegalBaseInstrFaultD, // Illegal non-compressed instruction
|
|
|
|
|
output logic InstrPageFaultF, // Instruction page fault
|
|
|
|
|
output logic IllegalIEUInstrFaultD, // Illegal instruction including compressed
|
|
|
|
|
output logic InstrMisalignedFaultM, // Branch target not aligned to 4 bytes if no compressed allowed (2 bytes if allowed)
|
|
|
|
|
// mmu management
|
|
|
|
|
input logic [1:0] PrivilegeModeW, // Priviledge mode in Writeback stage
|
|
|
|
|
input logic [`XLEN-1:0] PTE, // Hardware page table walker (HPTW) writes Page table entry (PTE) to ITLB
|
|
|
|
|
input logic [1:0] PageType, // Hardware page table walker (HPTW) writes PageType to ITLB
|
|
|
|
|
input logic ITLBWriteF, // Writes PTE and PageType to ITLB
|
|
|
|
|
input logic [`XLEN-1:0] SATP_REGW, // Location of the root page table and page table configuration
|
|
|
|
|
input logic STATUS_MXR, // Status CSR: make executable page readable
|
|
|
|
|
input logic STATUS_SUM, // Status CSR: Supervisor access to user memory
|
|
|
|
|
input logic STATUS_MPRV, // Status CSR: modify machine privilege
|
|
|
|
|
input logic [1:0] STATUS_MPP, // Status CSR: previous machine privilege level
|
|
|
|
|
input logic sfencevmaM, // Virtual memory address fence, invalidate TLB entries
|
|
|
|
|
output logic ITLBMissF, // ITLB miss causes HPTW (hardware pagetable walker) walk
|
|
|
|
|
output logic InstrDAPageFaultF, // ITLB hit needs to update dirty or access bits
|
2023-01-20 18:09:21 +00:00
|
|
|
|
input var logic [7:0] PMPCFG_ARRAY_REGW[`PMP_ENTRIES-1:0], // PMP configuration from privileged unit
|
|
|
|
|
input var logic [`XLEN-1:0] PMPADDR_ARRAY_REGW[`PMP_ENTRIES-1:0], // PMP address from privileged unit
|
|
|
|
|
output logic InstrAccessFaultF, // Instruction access fault
|
|
|
|
|
output logic ICacheAccess, // Report I$ read to performance counters
|
|
|
|
|
output logic ICacheMiss // Report I$ miss to performance counters
|
2021-01-28 03:49:47 +00:00
|
|
|
|
);
|
2021-10-23 19:00:32 +00:00
|
|
|
|
|
2023-01-20 18:09:21 +00:00
|
|
|
|
localparam [31:0] nop = 32'h00000013; // instruction for NOP
|
|
|
|
|
|
2023-01-21 00:47:36 +00:00
|
|
|
|
logic [`XLEN-1:0] PCNextF; // Next PCF, selected from Branch predictor, Privilege, or PC+2/4
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic BranchMisalignedFaultE; // Branch target not aligned to 4 bytes if no compressed allowed (2 bytes if allowed)
|
|
|
|
|
logic [`XLEN-1:0] PCPlus2or4F; // PCF + 2 (CompressedF) or PCF + 4 (Non-compressed)
|
2023-01-20 18:29:25 +00:00
|
|
|
|
logic [`XLEN-1:0] PCNextFSpill; // Next PCF after possible + 2 to handle spill
|
|
|
|
|
logic [`XLEN-1:0] PCFSpill; // PCF with possible + 2 to handle spill
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic [`XLEN-1:0] PCLinkD; // PCF2or4F delayed 1 cycle. This is next PC after a control flow instruction (br or j)
|
|
|
|
|
logic [`XLEN-1:2] PCPlus4F; // PCPlus4F is always PCF + 4. Fancy way to compute PCPlus2or4F
|
|
|
|
|
logic [`XLEN-1:0] PCD; // Decode stage instruction address
|
|
|
|
|
logic [`XLEN-1:0] NextValidPCE; // The PC of the next valid instruction in the pipeline after csr write or fence
|
2023-01-21 00:47:36 +00:00
|
|
|
|
logic [`PA_BITS-1:0] PCPF; // Physical address after address translation
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic [`XLEN+1:0] PCFExt; //
|
2021-01-28 03:49:47 +00:00
|
|
|
|
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic [31:0] IROMInstrF; // Instruction from the IROM
|
|
|
|
|
logic [31:0] ICacheInstrF; // Instruction from the I$
|
|
|
|
|
logic [31:0] InstrRawF; // Instruction from the IROM, I$, or bus
|
|
|
|
|
logic CompressedF; // The fetched instruction is compressed
|
2023-01-21 00:47:36 +00:00
|
|
|
|
logic [31:0] PostSpillInstrRawF; // Fetch instruction after merge two halves of spill
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic [31:0] InstrRawD; // Non-decompressed instruction in the Decode stage
|
2021-07-06 18:43:53 +00:00
|
|
|
|
|
2023-01-20 18:09:21 +00:00
|
|
|
|
logic [1:0] IFURWF; // IFU alreays read IFURWF = 10
|
|
|
|
|
logic [31:0] InstrE; // Instruction in the Execution stage
|
|
|
|
|
logic [31:0] NextInstrD, NextInstrE; // Instruction into the next stage after possible stage flush
|
|
|
|
|
|
2022-01-04 04:23:04 +00:00
|
|
|
|
|
2023-01-26 00:51:09 +00:00
|
|
|
|
logic CacheableF; // PMA indicates instruction address is cacheable
|
2023-01-20 18:29:25 +00:00
|
|
|
|
logic SelNextSpillF; // In a spill, stall pipeline and gate local stallF
|
|
|
|
|
logic BusStall; // Bus interface busy with multicycle operation
|
|
|
|
|
logic ICacheStallF; // I$ busy with multicycle operation
|
|
|
|
|
logic IFUCacheBusStallD; // EIther I$ or bus busy with multicycle operation
|
|
|
|
|
logic GatedStallD; // StallD gated by selected next spill
|
2022-02-08 20:54:53 +00:00
|
|
|
|
// branch predictor signal
|
2023-01-20 18:29:25 +00:00
|
|
|
|
logic [`XLEN-1:0] PCNext1F; // Branch predictor next PCF
|
|
|
|
|
logic BusCommittedF; // Bus memory operation in flight, delay interrupts
|
|
|
|
|
logic CacheCommittedF; // I$ memory operation started, delay interrupts
|
|
|
|
|
logic SelIROM; // PMA indicates instruction address is in the IROM
|
2022-10-05 19:51:02 +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
|
|
|
|
|
2023-01-19 20:08:42 +00:00
|
|
|
|
if(`C_SUPPORTED) begin : Spill
|
|
|
|
|
spill #(`ICACHE) spill(.clk, .reset, .StallD, .FlushD, .PCF, .PCPlus4F, .PCNextF, .InstrRawF,
|
2023-01-19 01:25:54 +00:00
|
|
|
|
.InstrDAPageFaultF, .IFUCacheBusStallD, .ITLBMissF, .PCNextFSpill, .PCFSpill, .SelNextSpillF, .PostSpillInstrRawF, .CompressedF);
|
2023-01-19 20:08:42 +00:00
|
|
|
|
end else begin : NoSpill
|
2022-01-27 16:06:24 +00:00
|
|
|
|
assign PCNextFSpill = PCNextF;
|
|
|
|
|
assign PCFSpill = PCF;
|
2022-10-05 20:36:56 +00:00
|
|
|
|
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,
|
2022-08-28 18:50:50 +00:00
|
|
|
|
.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),
|
2023-01-07 13:59:56 +00:00
|
|
|
|
.Cacheable(CacheableF), .Idempotent(), .SelTIM(SelIROM),
|
2022-01-28 20:02:05 +00:00
|
|
|
|
.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;
|
2022-10-05 20:36:56 +00:00
|
|
|
|
assign SelIROM = '0;
|
2022-01-28 20:02:05 +00:00
|
|
|
|
end
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
2022-01-28 21:26:06 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Memory
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2023-01-12 00:33:08 +00:00
|
|
|
|
// CommittedM tells the CPU's privileged unit the current instruction
|
2022-09-28 22:39:51 +00:00
|
|
|
|
// in the memory stage is a memory operaton and that memory operation is either completed
|
|
|
|
|
// or is partially executed. Partially completed memory operations need to prevent an interrupts.
|
|
|
|
|
// There is not a clean way to restore back to a partial executed instruction. CommiteedM will
|
|
|
|
|
// delay the interrupt until the LSU is in a clean state.
|
|
|
|
|
assign CommittedF = CacheCommittedF | BusCommittedF;
|
2022-01-27 17:18:55 +00:00
|
|
|
|
|
2022-10-05 20:36:56 +00:00
|
|
|
|
logic IgnoreRequest;
|
2022-11-07 21:03:43 +00:00
|
|
|
|
assign IgnoreRequest = ITLBMissF | FlushD;
|
2021-12-30 20:23:05 +00:00
|
|
|
|
|
2022-08-28 03:31:09 +00:00
|
|
|
|
// The IROM uses untranslated addresses, so it is not compatible with virtual memory.
|
2023-01-19 00:44:30 +00:00
|
|
|
|
if (`IROM_SUPPORTED) begin : irom
|
|
|
|
|
logic IROMce;
|
|
|
|
|
assign IROMce = ~GatedStallD | reset;
|
2022-09-23 16:46:53 +00:00
|
|
|
|
assign IFURWF = 2'b10;
|
2023-01-19 00:44:30 +00:00
|
|
|
|
irom irom(.clk, .ce(IROMce), .Adr(PCNextFSpill[`XLEN-1:0]), .IROMInstrF);
|
2022-08-28 03:31:09 +00:00
|
|
|
|
end else begin
|
2022-09-23 16:46:53 +00:00
|
|
|
|
assign IFURWF = 2'b10;
|
2022-08-28 03:31:09 +00:00
|
|
|
|
end
|
2022-08-25 16:02:46 +00:00
|
|
|
|
if (`BUS) begin : bus
|
2022-11-09 23:52:50 +00:00
|
|
|
|
// **** must fix words per line vs beats per line as in lsu.
|
2022-08-24 17:35:15 +00:00
|
|
|
|
localparam integer WORDSPERLINE = `ICACHE ? `ICACHE_LINELENINBITS/`XLEN : 1;
|
|
|
|
|
localparam integer LOGBWPL = `ICACHE ? $clog2(WORDSPERLINE) : 1;
|
|
|
|
|
if(`ICACHE) begin : icache
|
2022-08-26 03:02:38 +00:00
|
|
|
|
localparam integer LINELEN = `ICACHE ? `ICACHE_LINELENINBITS : `XLEN;
|
2023-01-20 18:29:25 +00:00
|
|
|
|
localparam integer LLENPOVERAHBW = `LLEN / `AHBW; // Number of AHB beats in a LLEN word. AHBW cannot be larger than LLEN. (implementation limitation)
|
2022-08-26 03:02:38 +00:00
|
|
|
|
logic [LINELEN-1:0] FetchBuffer;
|
|
|
|
|
logic [`PA_BITS-1:0] ICacheBusAdr;
|
|
|
|
|
logic ICacheBusAck;
|
2022-11-01 20:23:24 +00:00
|
|
|
|
logic [1:0] CacheBusRW, BusRW, CacheRWF;
|
2022-08-31 16:21:02 +00:00
|
|
|
|
|
2022-11-07 21:03:43 +00:00
|
|
|
|
assign BusRW = ~ITLBMissF & ~CacheableF & ~SelIROM ? IFURWF : '0;
|
|
|
|
|
assign CacheRWF = ~ITLBMissF & CacheableF & ~SelIROM ? IFURWF : '0;
|
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-12-23 03:36:49 +00:00
|
|
|
|
icache(.clk, .reset, .FlushStage(FlushD), .Stall(GatedStallD),
|
2022-08-25 17:44:39 +00:00
|
|
|
|
.FetchBuffer, .CacheBusAck(ICacheBusAck),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.CacheBusAdr(ICacheBusAdr), .CacheStall(ICacheStallF),
|
2022-09-23 16:46:53 +00:00
|
|
|
|
.CacheBusRW,
|
2022-10-05 20:36:56 +00:00
|
|
|
|
.ReadDataWord(ICacheInstrF),
|
2022-10-19 20:08:23 +00:00
|
|
|
|
.SelHPTW('0),
|
2022-01-31 22:25:41 +00:00
|
|
|
|
.CacheMiss(ICacheMiss), .CacheAccess(ICacheAccess),
|
2022-11-09 23:52:50 +00:00
|
|
|
|
.ByteMask('0), .BeatCount('0), .SelBusBeat('0),
|
2022-11-14 03:36:12 +00:00
|
|
|
|
.CacheWriteData('0),
|
2022-11-01 20:23:24 +00:00
|
|
|
|
.CacheRW(CacheRWF),
|
|
|
|
|
.CacheAtomic('0), .FlushCache('0),
|
2022-01-28 20:27:11 +00:00
|
|
|
|
.NextAdr(PCNextFSpill[11:0]),
|
|
|
|
|
.PAdr(PCPF),
|
2022-09-28 22:39:51 +00:00
|
|
|
|
.CacheCommitted(CacheCommittedF), .InvalidateCache(InvalidateICacheM));
|
2023-01-18 22:58:03 +00:00
|
|
|
|
ahbcacheinterface #(WORDSPERLINE, LOGBWPL, LINELEN, LLENPOVERAHBW)
|
2022-08-31 19:12:19 +00:00
|
|
|
|
ahbcacheinterface(.HCLK(clk), .HRESETn(~reset),
|
2022-08-30 19:17:00 +00:00
|
|
|
|
.HRDATA,
|
2022-12-23 03:36:49 +00:00
|
|
|
|
.Flush(FlushD), .CacheBusRW, .HSIZE(IFUHSIZE), .HBURST(IFUHBURST), .HTRANS(IFUHTRANS), .HWSTRB(),
|
2022-08-30 19:17:00 +00:00
|
|
|
|
.Funct3(3'b010), .HADDR(IFUHADDR), .HREADY(IFUHREADY), .HWRITE(IFUHWRITE), .CacheBusAdr(ICacheBusAdr),
|
2022-11-11 20:30:32 +00:00
|
|
|
|
.BeatCount(), .Cacheable(CacheableF), .SelBusBeat(), .WriteDataM('0),
|
|
|
|
|
.CacheBusAck(ICacheBusAck), .HWDATA(), .CacheableOrFlushCacheM(1'b0), .CacheReadDataWordM('0),
|
2022-08-26 03:02:38 +00:00
|
|
|
|
.FetchBuffer, .PAdr(PCPF),
|
2022-12-21 22:12:55 +00:00
|
|
|
|
.BusRW, .Stall(GatedStallD),
|
2022-09-28 22:39:51 +00:00
|
|
|
|
.BusStall, .BusCommitted(BusCommittedF));
|
2022-08-26 03:02:38 +00:00
|
|
|
|
|
2022-10-05 20:36:56 +00:00
|
|
|
|
mux3 #(32) UnCachedDataMux(.d0(ICacheInstrF), .d1(FetchBuffer[32-1:0]), .d2(IROMInstrF),
|
2022-10-17 17:34:14 +00:00
|
|
|
|
.s({SelIROM, ~CacheableF}), .y(InstrRawF[31:0]));
|
2022-01-28 20:27:11 +00:00
|
|
|
|
end else begin : passthrough
|
2022-08-26 03:02:38 +00:00
|
|
|
|
assign IFUHADDR = PCPF;
|
2022-10-05 19:51:02 +00:00
|
|
|
|
logic [31:0] FetchBuffer;
|
2022-09-23 16:46:53 +00:00
|
|
|
|
logic [1:0] BusRW;
|
2022-11-07 21:03:43 +00:00
|
|
|
|
assign BusRW = ~ITLBMissF & ~SelIROM ? IFURWF : '0;
|
2022-09-18 01:30:01 +00:00
|
|
|
|
assign IFUHSIZE = 3'b010;
|
2022-08-26 03:02:38 +00:00
|
|
|
|
|
2022-12-23 03:36:49 +00:00
|
|
|
|
ahbinterface #(0) ahbinterface(.HCLK(clk), .Flush(FlushD), .HRESETn(~reset), .HREADY(IFUHREADY),
|
2022-08-31 19:52:06 +00:00
|
|
|
|
.HRDATA(HRDATA), .HTRANS(IFUHTRANS), .HWRITE(IFUHWRITE), .HWDATA(),
|
2022-09-23 16:46:53 +00:00
|
|
|
|
.HWSTRB(), .BusRW, .ByteMask(), .WriteData('0),
|
2022-12-21 22:12:55 +00:00
|
|
|
|
.Stall(GatedStallD), .BusStall, .BusCommitted(BusCommittedF), .FetchBuffer(FetchBuffer));
|
2022-08-29 18:01:24 +00:00
|
|
|
|
|
2022-12-06 16:37:45 +00:00
|
|
|
|
assign CacheCommittedF = '0;
|
2022-10-05 20:36:56 +00:00
|
|
|
|
if(`IROM_SUPPORTED) mux2 #(32) UnCachedDataMux2(FetchBuffer, IROMInstrF, SelIROM, InstrRawF);
|
|
|
|
|
else assign InstrRawF = FetchBuffer;
|
2022-08-26 03:02:38 +00:00
|
|
|
|
assign IFUHBURST = 3'b0;
|
2023-01-20 18:29:25 +00:00
|
|
|
|
assign {ICacheMiss, ICacheAccess, ICacheStallF} = '0;
|
2022-01-28 20:27:11 +00:00
|
|
|
|
end
|
2022-03-11 21:18:56 +00:00
|
|
|
|
end else begin : nobus // block: bus
|
2022-12-06 16:37:45 +00:00
|
|
|
|
assign {BusStall, CacheCommittedF} = '0;
|
2022-08-26 03:02:38 +00:00
|
|
|
|
assign {ICacheStallF, ICacheMiss, ICacheAccess} = '0;
|
2022-10-05 20:36:56 +00:00
|
|
|
|
assign InstrRawF = IROMInstrF;
|
2022-03-11 21:18:56 +00:00
|
|
|
|
end
|
2022-01-28 20:27:11 +00:00
|
|
|
|
|
2022-12-23 03:56:33 +00:00
|
|
|
|
assign IFUCacheBusStallD = ICacheStallF | BusStall;
|
2022-12-23 21:10:37 +00:00
|
|
|
|
assign IFUStallF = IFUCacheBusStallD | SelNextSpillF;
|
2022-12-21 22:12:55 +00:00
|
|
|
|
assign GatedStallD = StallD & ~SelNextSpillF;
|
2021-05-03 17:03:17 +00:00
|
|
|
|
|
2022-12-19 23:18:42 +00:00
|
|
|
|
flopenl #(32) AlignedInstrRawDFlop(clk, reset | FlushD, ~StallD, PostSpillInstrRawF, nop, InstrRawD);
|
2021-05-03 17:03:17 +00:00
|
|
|
|
|
2022-02-23 04:45:00 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// PCNextF logic
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-12-20 23:38:30 +00:00
|
|
|
|
if(`ZICSR_SUPPORTED | `ZIFENCEI_SUPPORTED)
|
2022-12-20 05:16:58 +00:00
|
|
|
|
mux2 #(`XLEN) pcmux2(.d0(PCNext1F), .d1(NextValidPCE), .s(CSRWriteFenceM),.y(PCNext2F));
|
2022-12-20 23:34:11 +00:00
|
|
|
|
else assign PCNext2F = PCNext1F;
|
2022-12-20 23:55:45 +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-12-20 18:58:54 +00:00
|
|
|
|
// pcadder
|
|
|
|
|
// add 2 or 4 to the PC, based on whether the instruction is 16 bits or 32
|
2022-12-21 15:18:30 +00:00
|
|
|
|
assign PCPlus4F = PCF[`XLEN-1:2] + 1; // add 4 to PC
|
2022-12-20 18:58:54 +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
|
2022-12-21 15:18:30 +00:00
|
|
|
|
// *** consider gating PCPlus4F to provide the reset.
|
2022-12-21 15:19:34 +00:00
|
|
|
|
/* -----\/----- EXCLUDED -----\/-----
|
2022-12-20 22:33:49 +00:00
|
|
|
|
assign PCPlus2or4F[0] = '0;
|
2022-12-21 14:35:43 +00:00
|
|
|
|
assign PCPlus2or4F[1] = ~reset & (CompressedF ^ PCF[1]);
|
2022-12-21 15:18:30 +00:00
|
|
|
|
assign PCPlus2or4F[`XLEN-1:2] = reset ? '0 : CompressedF & ~PCF[1] ? PCF[`XLEN-1:2] : PCPlus4F;
|
2022-12-21 15:19:34 +00:00
|
|
|
|
-----/\----- EXCLUDED -----/\----- */
|
2022-12-21 14:35:43 +00:00
|
|
|
|
/* -----\/----- EXCLUDED -----\/-----
|
|
|
|
|
assign PCPlus2or4F[1:0] = reset ? 2'b00 : CompressedF ? PCF[1] ? 2'b00 : 2'b10 : PCF[1:0];
|
|
|
|
|
-----/\----- EXCLUDED -----/\----- */
|
|
|
|
|
|
|
|
|
|
// *** There is actually a bug in the regression test. We fetched an address which returns data with
|
|
|
|
|
// an X. This version of the code does not die because if CompressedF is an X it just defaults to the last
|
|
|
|
|
// option. The above code would work, but propagates the x.
|
2022-12-20 18:58:54 +00:00
|
|
|
|
always_comb
|
|
|
|
|
if(reset) PCPlus2or4F = '0;
|
|
|
|
|
else if (CompressedF) // add 2
|
2022-12-21 15:18:30 +00:00
|
|
|
|
if (PCF[1]) PCPlus2or4F = {PCPlus4F, 2'b00};
|
2022-12-20 18:58:54 +00:00
|
|
|
|
else PCPlus2or4F = {PCF[`XLEN-1:2], 2'b10};
|
2022-12-21 15:18:30 +00:00
|
|
|
|
else PCPlus2or4F = {PCPlus4F, PCF[1:0]}; // add 4
|
2022-12-20 18:58:54 +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-01-05 16:25:08 +00:00
|
|
|
|
bpred bpred(.clk, .reset,
|
2023-01-05 20:04:09 +00:00
|
|
|
|
.StallF, .StallD, .StallE, .StallM, .StallW,
|
|
|
|
|
.FlushD, .FlushE, .FlushM, .FlushW,
|
2023-01-05 05:41:55 +00:00
|
|
|
|
.InstrD, .PCNextF, .PCPlus2or4F, .PCNext1F, .PCE, .PCM, .PCSrcE, .IEUAdrE, .PCF, .NextValidPCE,
|
2023-01-26 01:39:18 +00:00
|
|
|
|
.PCD, .PCLinkE, .InstrClassM, .BPPredWrongE, .PostSpillInstrRawF,
|
2023-01-05 23:19:27 +00:00
|
|
|
|
.DirPredictionWrongM, .BTBPredPCWrongM, .RASPredPCWrongM, .PredictionInstrClassWrongM);
|
2022-02-08 20:17:44 +00:00
|
|
|
|
|
2022-01-05 16:25:08 +00:00
|
|
|
|
end else begin : bpred
|
2022-12-20 05:16:58 +00:00
|
|
|
|
mux2 #(`XLEN) pcmux1(.d0(PCPlus2or4F), .d1(IEUAdrE), .s(PCSrcE), .y(PCNext1F));
|
2022-01-26 23:37:04 +00:00
|
|
|
|
assign BPPredWrongE = PCSrcE;
|
2023-01-05 23:19:27 +00:00
|
|
|
|
assign {InstrClassM, DirPredictionWrongM, BTBPredPCWrongM, RASPredPCWrongM, PredictionInstrClassWrongM} = '0;
|
2022-12-20 05:16:58 +00:00
|
|
|
|
assign NextValidPCE = PCE;
|
2022-01-05 16:25:08 +00:00
|
|
|
|
end
|
2021-02-18 04:19:17 +00:00
|
|
|
|
|
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
|
2023-01-07 13:51:47 +00:00
|
|
|
|
if (`C_SUPPORTED) begin
|
|
|
|
|
logic IllegalCompInstrD;
|
|
|
|
|
decompress decomp(.InstrRawD, .InstrD, .IllegalCompInstrD);
|
|
|
|
|
assign IllegalIEUInstrFaultD = IllegalBaseInstrFaultD | IllegalCompInstrD; // illegal if bad 32 or 16-bit instr
|
|
|
|
|
end else begin
|
|
|
|
|
assign InstrD = InstrRawD;
|
|
|
|
|
assign IllegalIEUInstrFaultD = IllegalBaseInstrFaultD;
|
|
|
|
|
end
|
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
|
2023-01-20 18:09:21 +00:00
|
|
|
|
// Cannot use flopenrc for Instr(E/M) as it resets to NOP not 0.
|
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
|