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
|
|
|
|
// Modified:
|
|
|
|
//
|
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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
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 (
|
2021-02-18 04:19:17 +00:00
|
|
|
input logic clk, reset,
|
|
|
|
input logic StallF, StallD, FlushF, FlushD, FlushE, FlushM, FlushW,
|
2021-02-02 04:44:41 +00:00
|
|
|
// Fetch
|
2021-02-18 04:19:17 +00:00
|
|
|
input logic [31:0] InstrF,
|
2021-02-02 04:44:41 +00:00
|
|
|
output logic [`XLEN-1:0] PCF,
|
2021-02-02 20:09:24 +00:00
|
|
|
output logic [`XLEN-1:0] InstrPAdrF,
|
|
|
|
// Decode
|
2021-02-18 04:19:17 +00:00
|
|
|
output logic InstrStall,
|
2021-02-02 04:44:41 +00:00
|
|
|
// Execute
|
2021-02-27 02:12:27 +00:00
|
|
|
output logic [`XLEN-1:0] PCLinkE,
|
2021-02-18 04:19:17 +00:00
|
|
|
input logic PCSrcE,
|
|
|
|
input logic [`XLEN-1:0] PCTargetE,
|
|
|
|
output logic [`XLEN-1:0] PCE,
|
2021-02-27 02:12:27 +00:00
|
|
|
output logic BPPredWrongE,
|
2021-02-02 04:44:41 +00:00
|
|
|
// Mem
|
2021-02-18 04:19:17 +00:00
|
|
|
input logic RetM, TrapM,
|
|
|
|
input logic [`XLEN-1:0] PrivilegedNextPCM,
|
|
|
|
output logic [31:0] InstrD, InstrM,
|
2021-02-02 04:44:41 +00:00
|
|
|
output logic [`XLEN-1:0] PCM,
|
|
|
|
// Writeback
|
2021-01-28 05:22:05 +00:00
|
|
|
output logic [`XLEN-1:0] PCLinkW,
|
2021-02-02 04:44:41 +00:00
|
|
|
// Faults
|
2021-02-18 04:19:17 +00:00
|
|
|
input logic IllegalBaseInstrFaultD,
|
|
|
|
output logic IllegalIEUInstrFaultD,
|
|
|
|
output logic InstrMisalignedFaultM,
|
2021-01-28 03:49:47 +00:00
|
|
|
output logic [`XLEN-1:0] InstrMisalignedAdrM
|
|
|
|
);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-01-28 03:49:47 +00:00
|
|
|
logic [`XLEN-1:0] UnalignedPCNextF, PCNextF;
|
2021-01-15 04:37:51 +00:00
|
|
|
logic misaligned, BranchMisalignedFaultE, BranchMisalignedFaultM, TrapMisalignedFaultM;
|
|
|
|
logic StallExceptResolveBranchesF, PrivilegedChangePCM;
|
2021-01-28 03:49:47 +00:00
|
|
|
logic IllegalCompInstrD;
|
2021-02-27 02:12:27 +00:00
|
|
|
logic [`XLEN-1:0] PCPlusUpperF, PCPlus2or4F, PCD, PCW, PCLinkD, PCLinkM;
|
2021-01-19 01:16:53 +00:00
|
|
|
logic CompressedF;
|
2021-01-28 03:49:47 +00:00
|
|
|
logic [31:0] InstrRawD, InstrE;
|
|
|
|
logic [31:0] nop = 32'h00000013; // instruction for NOP
|
|
|
|
|
2021-02-18 04:19:17 +00:00
|
|
|
// branch predictor signals
|
|
|
|
logic SelBPPredF;
|
|
|
|
logic [`XLEN-1:0] BPPredPCF, PCCorrectE, PCNext0F, PCNext1F;
|
|
|
|
|
|
|
|
|
2021-01-28 03:49:47 +00:00
|
|
|
// *** put memory interface on here, InstrF becomes output
|
2021-02-02 20:09:24 +00:00
|
|
|
assign InstrStall = 0; // ***
|
|
|
|
assign InstrPAdrF = PCF; // *** no MMU
|
2021-01-15 04:37:51 +00:00
|
|
|
|
|
|
|
assign PrivilegedChangePCM = RetM | TrapM;
|
|
|
|
|
2021-02-19 22:36:51 +00:00
|
|
|
assign StallExceptResolveBranchesF = StallF & ~(SelBPPredF | BPPredWrongE | PrivilegedChangePCM);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-02-18 04:19:17 +00:00
|
|
|
//mux3 #(`XLEN) pcmux(PCPlus2or4F, PCCorrectE, PrivilegedNextPCM, {PrivilegedChangePCM, BPPredWrongE}, UnalignedPCNextF);
|
|
|
|
mux2 #(`XLEN) pcmux0(.d0(PCPlus2or4F),
|
|
|
|
.d1(BPPredPCF),
|
|
|
|
.s(SelBPPredF),
|
|
|
|
.y(PCNext0F));
|
|
|
|
|
|
|
|
mux2 #(`XLEN) pcmux1(.d0(PCNext0F),
|
|
|
|
.d1(PCCorrectE),
|
|
|
|
.s(BPPredWrongE),
|
|
|
|
.y(PCNext1F));
|
|
|
|
|
|
|
|
mux2 #(`XLEN) pcmux2(.d0(PCNext1F),
|
|
|
|
.d1(PrivilegedNextPCM),
|
|
|
|
.s(PrivilegedChangePCM),
|
|
|
|
.y(UnalignedPCNextF));
|
|
|
|
|
2021-01-23 15:48:12 +00:00
|
|
|
assign PCNextF = {UnalignedPCNextF[`XLEN-1:1], 1'b0}; // hart-SPEC p. 21 about 16-bit alignment
|
2021-01-25 20:57:36 +00:00
|
|
|
flopenl #(`XLEN) pcreg(clk, reset, ~StallExceptResolveBranchesF, PCNextF, `RESET_VECTOR, PCF);
|
2021-01-19 01:16:53 +00:00
|
|
|
|
2021-02-18 04:19:17 +00:00
|
|
|
// branch and jump predictor
|
|
|
|
// I am making the port connection explicit for now as I want to see them and they will be changing.
|
|
|
|
bpred bpred(.clk(clk),
|
|
|
|
.reset(reset),
|
|
|
|
.StallF(StallF),
|
|
|
|
.StallD(StallD),
|
|
|
|
.StallE(1'b0), // *** may need this eventually
|
|
|
|
.FlushF(FlushF),
|
|
|
|
.FlushD(FlushD),
|
|
|
|
.FlushE(FlushE),
|
|
|
|
.PCNextF(PCNextF),
|
|
|
|
.BPPredPCF(BPPredPCF),
|
|
|
|
.SelBPPredF(SelBPPredF),
|
|
|
|
.InstrF(InstrF), // *** this is flushed internally. The logic is redundant with some out here.
|
|
|
|
// Also I believe this port will be removed.
|
|
|
|
.PCE(PCE),
|
|
|
|
.PCSrcE(PCSrcE),
|
|
|
|
.PCTargetE(PCTargetE),
|
|
|
|
.PCD(PCD),
|
|
|
|
.PCLinkE(PCLinkE),
|
|
|
|
.BPPredWrongE(BPPredWrongE));
|
|
|
|
// The true correct target is PCTargetE if PCSrcE is 1 else it is the fall through PCLinkE.
|
|
|
|
assign PCCorrectE = PCSrcE ? PCTargetE : PCLinkE;
|
|
|
|
|
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
|
|
|
|
assign CompressedF = (InstrF[1:0] != 2'b11); // is it a 16-bit compressed instruction?
|
2021-01-23 15:48:12 +00:00
|
|
|
assign PCPlusUpperF = PCF[`XLEN-1:2] + 1; // add 4 to PC
|
2021-01-19 01:16:53 +00:00
|
|
|
|
|
|
|
// choose PC+2 or PC+4
|
|
|
|
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
|
|
|
|
2021-01-28 03:49:47 +00:00
|
|
|
|
|
|
|
// Decode stage pipeline register and logic
|
|
|
|
flopenl #(32) InstrDReg(clk, reset, ~StallD, (FlushD ? nop : InstrF), nop, InstrRawD);
|
|
|
|
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
|
|
|
|
decompress decomp(.*);
|
2021-01-28 03:49:47 +00:00
|
|
|
assign IllegalIEUInstrFaultD = IllegalBaseInstrFaultD | IllegalCompInstrD; // illegal if bad 32 or 16-bit instr
|
|
|
|
// *** combine these with others in better way, including M, F
|
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
// Misaligned PC logic
|
|
|
|
|
|
|
|
generate
|
|
|
|
if (`C_SUPPORTED) // C supports compressed instructions on halfword boundaries
|
|
|
|
assign misaligned = PCNextF[0];
|
|
|
|
else // instructions must be on word boundaries
|
|
|
|
assign misaligned = |PCNextF[1:0];
|
|
|
|
endgenerate
|
|
|
|
|
|
|
|
// pipeline misaligned faults to M stage
|
|
|
|
assign BranchMisalignedFaultE = misaligned & PCSrcE; // E-stage (Branch/Jump) misaligned
|
|
|
|
flopr #(1) InstrMisalginedReg(clk, reset, BranchMisalignedFaultE, BranchMisalignedFaultM);
|
2021-01-23 15:48:12 +00:00
|
|
|
flopr #(`XLEN) InstrMisalignedAdrReg(clk, reset, PCNextF, InstrMisalignedAdrM);
|
2021-01-15 04:37:51 +00:00
|
|
|
assign TrapMisalignedFaultM = misaligned & PrivilegedChangePCM;
|
|
|
|
assign InstrMisalignedFaultM = BranchMisalignedFaultM; // | TrapMisalignedFaultM; *** put this back in without causing a cyclic path
|
|
|
|
|
2021-01-28 03:49:47 +00:00
|
|
|
flopr #(32) InstrEReg(clk, reset, FlushE ? nop : InstrD, InstrE);
|
|
|
|
flopr #(32) InstrMReg(clk, reset, FlushM ? nop : InstrE, InstrM);
|
2021-01-28 05:22:05 +00:00
|
|
|
flopr #(`XLEN) PCEReg(clk, reset, PCD, PCE);
|
|
|
|
flopr #(`XLEN) PCMReg(clk, reset, PCE, PCM);
|
|
|
|
flopr #(`XLEN) PCWReg(clk, reset, PCM, PCW); // *** probably not needed; delete later
|
|
|
|
|
2021-01-29 02:40:48 +00:00
|
|
|
// seems like there should be a lower-cost way of doing this PC+2 or PC+4 for JAL.
|
|
|
|
// either have ALU compute PC+2/4 and feed into ALUResult input of ResultMux or
|
|
|
|
// have dedicated adder in Mem stage based on PCM + 2 or 4
|
|
|
|
// *** redo this
|
2021-01-28 05:22:05 +00:00
|
|
|
flopr #(`XLEN) PCPDReg(clk, reset, PCPlus2or4F, PCLinkD);
|
|
|
|
flopr #(`XLEN) PCPEReg(clk, reset, PCLinkD, PCLinkE);
|
|
|
|
flopr #(`XLEN) PCPMReg(clk, reset, PCLinkE, PCLinkM);
|
|
|
|
flopr #(`XLEN) PCPWReg(clk, reset, PCLinkM, PCLinkW);
|
2021-01-28 03:49:47 +00:00
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
endmodule
|
|
|
|
|