| 1 |
2 |
ayersg |
`timescale 1ns / 1ps
|
| 2 |
|
|
/*
|
| 3 |
|
|
* File : Processor.v
|
| 4 |
|
|
* Project : University of Utah, XUM Project MIPS32 core
|
| 5 |
|
|
* Creator(s) : Grant Ayers (ayers@cs.utah.edu)
|
| 6 |
|
|
*
|
| 7 |
|
|
* Modification History:
|
| 8 |
|
|
* Rev Date Initials Description of Change
|
| 9 |
|
|
* 1.0 23-Jul-2011 GEA Initial design.
|
| 10 |
|
|
* 2.0 26-May-2012 GEA Release version with CP0.
|
| 11 |
|
|
*
|
| 12 |
|
|
* Standards/Formatting:
|
| 13 |
|
|
* Verilog 2001, 4 soft tab, wide column.
|
| 14 |
|
|
*
|
| 15 |
|
|
* Description:
|
| 16 |
|
|
* The top-level MIPS32 Processor. This file is mostly the instantiation
|
| 17 |
|
|
* and wiring of the building blocks of the processor according to the
|
| 18 |
|
|
* hardware design diagram. It contains very little logic itself.
|
| 19 |
|
|
*/
|
| 20 |
|
|
module Processor(
|
| 21 |
|
|
input clock,
|
| 22 |
|
|
input reset,
|
| 23 |
|
|
input [4:0] Interrupts, // 5 general-purpose hardware interrupts
|
| 24 |
|
|
input NMI, // Non-maskable interrupt
|
| 25 |
|
|
// Data Memory Interface
|
| 26 |
|
|
input [31:0] DataMem_In,
|
| 27 |
|
|
input DataMem_Ready,
|
| 28 |
|
|
output DataMem_Read,
|
| 29 |
|
|
output [3:0] DataMem_Write, // 4-bit Write, one for each byte in word.
|
| 30 |
|
|
output [29:0] DataMem_Address, // Addresses are words, not bytes.
|
| 31 |
|
|
output [31:0] DataMem_Out,
|
| 32 |
|
|
// Instruction Memory Interface
|
| 33 |
|
|
input [31:0] InstMem_In,
|
| 34 |
|
|
output [29:0] InstMem_Address, // Addresses are words, not bytes.
|
| 35 |
|
|
input InstMem_Ready,
|
| 36 |
|
|
output InstMem_Read,
|
| 37 |
|
|
output [7:0] IP // Pending interrupts (diagnostic)
|
| 38 |
|
|
);
|
| 39 |
|
|
|
| 40 |
|
|
`include "MIPS_Parameters.v"
|
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
|
/*** MIPS Instruction and Components (ID Stage) ***/
|
| 44 |
|
|
wire [31:0] Instruction;
|
| 45 |
|
|
wire [5:0] OpCode = Instruction[31:26];
|
| 46 |
|
|
wire [4:0] Rs = Instruction[25:21];
|
| 47 |
|
|
wire [4:0] Rt = Instruction[20:16];
|
| 48 |
|
|
wire [4:0] Rd = Instruction[15:11];
|
| 49 |
|
|
wire [5:0] Funct = Instruction[5:0];
|
| 50 |
|
|
wire [15:0] Immediate = Instruction[15:0];
|
| 51 |
|
|
wire [25:0] JumpAddress = Instruction[25:0];
|
| 52 |
|
|
wire [2:0] Cp0_Sel = Instruction[2:0];
|
| 53 |
|
|
|
| 54 |
|
|
/*** IF (Instruction Fetch) Signals ***/
|
| 55 |
|
|
wire IF_Stall, IF_Flush;
|
| 56 |
|
|
wire IF_EXC_AdIF;
|
| 57 |
|
|
wire IF_Exception_Stall;
|
| 58 |
|
|
wire IF_Exception_Flush;
|
| 59 |
|
|
wire IF_IsBDS;
|
| 60 |
|
|
wire [31:0] IF_PCAdd4, IF_PC_PreExc, IF_PCIn, IF_PCOut, IF_Instruction;
|
| 61 |
|
|
|
| 62 |
|
|
/*** ID (Instruction Decode) Signals ***/
|
| 63 |
|
|
wire ID_Stall;
|
| 64 |
|
|
wire [1:0] ID_PCSrc;
|
| 65 |
|
|
wire [1:0] ID_RsFwdSel, ID_RtFwdSel;
|
| 66 |
|
|
wire ID_Link, ID_Movn, ID_Movz;
|
| 67 |
|
|
wire ID_SignExtend;
|
| 68 |
|
|
wire ID_LLSC;
|
| 69 |
|
|
wire ID_RegDst, ID_ALUSrcImm, ID_MemWrite, ID_MemRead, ID_MemByte, ID_MemHalf, ID_MemSignExtend, ID_RegWrite, ID_MemtoReg;
|
| 70 |
|
|
wire [4:0] ID_ALUOp;
|
| 71 |
|
|
wire ID_Mfc0, ID_Mtc0, ID_Eret;
|
| 72 |
|
|
wire ID_NextIsDelay;
|
| 73 |
|
|
wire ID_CanErr, ID_ID_CanErr, ID_EX_CanErr, ID_M_CanErr;
|
| 74 |
|
|
wire ID_KernelMode;
|
| 75 |
|
|
wire ID_ReverseEndian;
|
| 76 |
|
|
wire ID_Trap, ID_TrapCond;
|
| 77 |
|
|
wire ID_EXC_Sys, ID_EXC_Bp, ID_EXC_RI;
|
| 78 |
|
|
wire ID_Exception_Stall;
|
| 79 |
|
|
wire ID_Exception_Flush;
|
| 80 |
|
|
wire ID_PCSrc_Exc;
|
| 81 |
|
|
wire [31:0] ID_ExceptionPC;
|
| 82 |
|
|
wire ID_CP1, ID_CP2, ID_CP3;
|
| 83 |
|
|
wire [31:0] ID_PCAdd4;
|
| 84 |
|
|
wire [31:0] ID_ReadData1_RF, ID_ReadData1_End;
|
| 85 |
|
|
wire [31:0] ID_ReadData2_RF, ID_ReadData2_End;
|
| 86 |
|
|
wire [31:0] CP0_RegOut;
|
| 87 |
|
|
wire ID_CmpEQ, ID_CmpGZ, ID_CmpLZ, ID_CmpGEZ, ID_CmpLEZ;
|
| 88 |
|
|
wire [29:0] ID_SignExtImm = (ID_SignExtend & Immediate[15]) ? {14'h3FFF, Immediate} : {14'h0000, Immediate};
|
| 89 |
|
|
wire [31:0] ID_ImmLeftShift2 = {ID_SignExtImm[29:0], 2'b00};
|
| 90 |
|
|
wire [31:0] ID_JumpAddress = {ID_PCAdd4[31:28], JumpAddress[25:0], 2'b00};
|
| 91 |
|
|
wire [31:0] ID_BranchAddress;
|
| 92 |
|
|
wire [31:0] ID_RestartPC;
|
| 93 |
|
|
wire ID_IsBDS;
|
| 94 |
|
|
wire ID_Left, ID_Right;
|
| 95 |
|
|
wire ID_IsFlushed;
|
| 96 |
|
|
|
| 97 |
|
|
/*** EX (Execute) Signals ***/
|
| 98 |
|
|
wire EX_Stall;
|
| 99 |
|
|
wire [1:0] EX_RsFwdSel, EX_RtFwdSel;
|
| 100 |
|
|
wire EX_Link;
|
| 101 |
|
|
wire [1:0] EX_LinkRegDst;
|
| 102 |
|
|
wire EX_ALUSrcImm;
|
| 103 |
|
|
wire [4:0] EX_ALUOp;
|
| 104 |
|
|
wire EX_Movn, EX_Movz;
|
| 105 |
|
|
wire EX_LLSC;
|
| 106 |
|
|
wire EX_MemRead, EX_MemWrite, EX_MemByte, EX_MemHalf, EX_MemSignExtend, EX_RegWrite, EX_MemtoReg;
|
| 107 |
|
|
wire [4:0] EX_Rs, EX_Rt;
|
| 108 |
|
|
wire EX_WantRsByEX, EX_NeedRsByEX, EX_WantRtByEX, EX_NeedRtByEX;
|
| 109 |
|
|
wire EX_Trap, EX_TrapCond;
|
| 110 |
|
|
wire EX_CanErr, EX_EX_CanErr, EX_M_CanErr;
|
| 111 |
|
|
wire EX_KernelMode;
|
| 112 |
|
|
wire EX_ReverseEndian;
|
| 113 |
|
|
wire EX_Exception_Stall;
|
| 114 |
|
|
wire EX_Exception_Flush;
|
| 115 |
|
|
wire [31:0] EX_ReadData1_PR, EX_ReadData1_Fwd, EX_ReadData2_PR, EX_ReadData2_Fwd, EX_ReadData2_Imm;
|
| 116 |
|
|
wire [31:0] EX_SignExtImm;
|
| 117 |
|
|
wire [4:0] EX_Rd, EX_RtRd, EX_Shamt;
|
| 118 |
|
|
wire [31:0] EX_ALUResult;
|
| 119 |
|
|
wire EX_BZero;
|
| 120 |
|
|
wire EX_EXC_Ov;
|
| 121 |
|
|
wire [31:0] EX_RestartPC;
|
| 122 |
|
|
wire EX_IsBDS;
|
| 123 |
|
|
wire EX_Left, EX_Right;
|
| 124 |
|
|
|
| 125 |
|
|
/*** MEM (Memory) Signals ***/
|
| 126 |
|
|
wire M_Stall, M_Stall_Controller;
|
| 127 |
|
|
wire M_LLSC;
|
| 128 |
|
|
wire M_MemRead, M_MemWrite, M_MemByte, M_MemHalf, M_MemSignExtend;
|
| 129 |
|
|
wire M_RegWrite, M_MemtoReg;
|
| 130 |
|
|
wire M_WriteDataFwdSel;
|
| 131 |
|
|
wire M_EXC_AdEL, M_EXC_AdES;
|
| 132 |
|
|
wire M_M_CanErr;
|
| 133 |
|
|
wire M_KernelMode;
|
| 134 |
|
|
wire M_ReverseEndian;
|
| 135 |
|
|
wire M_Trap, M_TrapCond;
|
| 136 |
|
|
wire M_EXC_Tr;
|
| 137 |
|
|
wire M_Exception_Flush;
|
| 138 |
|
|
wire [31:0] M_ALUResult, M_ReadData2_PR;
|
| 139 |
|
|
wire [4:0] M_RtRd;
|
| 140 |
|
|
wire [31:0] M_MemReadData;
|
| 141 |
|
|
wire [31:0] M_RestartPC;
|
| 142 |
|
|
wire M_IsBDS;
|
| 143 |
|
|
wire [31:0] M_WriteData_Pre;
|
| 144 |
|
|
wire M_Left, M_Right;
|
| 145 |
|
|
wire M_Exception_Stall;
|
| 146 |
|
|
|
| 147 |
|
|
/*** WB (Writeback) Signals ***/
|
| 148 |
|
|
wire WB_Stall, WB_RegWrite;
|
| 149 |
|
|
wire [31:0] WB_ReadData, WB_ALUResult;
|
| 150 |
|
|
wire [4:0] WB_RtRd;
|
| 151 |
|
|
wire [31:0] WB_WriteData;
|
| 152 |
|
|
|
| 153 |
|
|
/*** Other Signals ***/
|
| 154 |
|
|
wire [7:0] ID_DP_Hazards, HAZ_DP_Hazards;
|
| 155 |
|
|
|
| 156 |
|
|
/*** Assignments ***/
|
| 157 |
|
|
assign IF_Instruction = (IF_Stall) ? 32'h00000000 : InstMem_In;
|
| 158 |
|
|
assign IF_IsBDS = ID_NextIsDelay;
|
| 159 |
|
|
assign HAZ_DP_Hazards = {ID_DP_Hazards[7:4], EX_WantRsByEX, EX_NeedRsByEX, EX_WantRtByEX, EX_NeedRtByEX};
|
| 160 |
|
|
assign IF_EXC_AdIF = IF_PCOut[1] | IF_PCOut[0];
|
| 161 |
|
|
assign ID_CanErr = ID_ID_CanErr | ID_EX_CanErr | ID_M_CanErr;
|
| 162 |
|
|
assign EX_CanErr = EX_EX_CanErr | EX_M_CanErr;
|
| 163 |
|
|
assign M_CanErr = M_M_CanErr;
|
| 164 |
|
|
|
| 165 |
|
|
// External Memory Interface
|
| 166 |
|
|
reg IRead, IReadMask;
|
| 167 |
|
|
assign InstMem_Address = IF_PCOut[31:2];
|
| 168 |
|
|
assign DataMem_Address = M_ALUResult[31:2];
|
| 169 |
|
|
always @(posedge clock) begin
|
| 170 |
|
|
IRead <= (reset) ? 1 : ~InstMem_Ready;
|
| 171 |
|
|
IReadMask <= (reset) ? 0 : ((IRead & InstMem_Ready) ? 1 : ((~IF_Stall) ? 0 : IReadMask));
|
| 172 |
|
|
end
|
| 173 |
|
|
assign InstMem_Read = IRead & ~IReadMask;
|
| 174 |
|
|
|
| 175 |
|
|
|
| 176 |
|
|
/*** Datapath Controller ***/
|
| 177 |
|
|
Control Controller (
|
| 178 |
|
|
.ID_Stall (ID_Stall),
|
| 179 |
|
|
.OpCode (OpCode),
|
| 180 |
|
|
.Funct (Funct),
|
| 181 |
|
|
.Rs (Rs),
|
| 182 |
|
|
.Rt (Rt),
|
| 183 |
|
|
.Cmp_EQ (ID_CmpEQ),
|
| 184 |
|
|
.Cmp_GZ (ID_CmpGZ),
|
| 185 |
|
|
.Cmp_GEZ (ID_CmpGEZ),
|
| 186 |
|
|
.Cmp_LZ (ID_CmpLZ),
|
| 187 |
|
|
.Cmp_LEZ (ID_CmpLEZ),
|
| 188 |
|
|
.IF_Flush (IF_Flush),
|
| 189 |
|
|
.DP_Hazards (ID_DP_Hazards),
|
| 190 |
|
|
.PCSrc (ID_PCSrc),
|
| 191 |
|
|
.SignExtend (ID_SignExtend),
|
| 192 |
|
|
.Link (ID_Link),
|
| 193 |
|
|
.Movn (ID_Movn),
|
| 194 |
|
|
.Movz (ID_Movz),
|
| 195 |
|
|
.Mfc0 (ID_Mfc0),
|
| 196 |
|
|
.Mtc0 (ID_Mtc0),
|
| 197 |
|
|
.CP1 (ID_CP1),
|
| 198 |
|
|
.CP2 (ID_CP2),
|
| 199 |
|
|
.CP3 (ID_CP3),
|
| 200 |
|
|
.Eret (ID_Eret),
|
| 201 |
|
|
.Trap (ID_Trap),
|
| 202 |
|
|
.TrapCond (ID_TrapCond),
|
| 203 |
|
|
.EXC_Sys (ID_EXC_Sys),
|
| 204 |
|
|
.EXC_Bp (ID_EXC_Bp),
|
| 205 |
|
|
.EXC_RI (ID_EXC_RI),
|
| 206 |
|
|
.ID_CanErr (ID_ID_CanErr),
|
| 207 |
|
|
.EX_CanErr (ID_EX_CanErr),
|
| 208 |
|
|
.M_CanErr (ID_M_CanErr),
|
| 209 |
|
|
.NextIsDelay (ID_NextIsDelay),
|
| 210 |
|
|
.RegDst (ID_RegDst),
|
| 211 |
|
|
.ALUSrcImm (ID_ALUSrcImm),
|
| 212 |
|
|
.ALUOp (ID_ALUOp),
|
| 213 |
|
|
.LLSC (ID_LLSC),
|
| 214 |
|
|
.MemWrite (ID_MemWrite),
|
| 215 |
|
|
.MemRead (ID_MemRead),
|
| 216 |
|
|
.MemByte (ID_MemByte),
|
| 217 |
|
|
.MemHalf (ID_MemHalf),
|
| 218 |
|
|
.MemSignExtend (ID_MemSignExtend),
|
| 219 |
|
|
.Left (ID_Left),
|
| 220 |
|
|
.Right (ID_Right),
|
| 221 |
|
|
.RegWrite (ID_RegWrite),
|
| 222 |
|
|
.MemtoReg (ID_MemtoReg)
|
| 223 |
|
|
);
|
| 224 |
|
|
|
| 225 |
|
|
/*** Hazard and Forward Control Unit ***/
|
| 226 |
|
|
Hazard_Detection HazardControl (
|
| 227 |
|
|
.DP_Hazards (HAZ_DP_Hazards),
|
| 228 |
|
|
.ID_Rs (Rs),
|
| 229 |
|
|
.ID_Rt (Rt),
|
| 230 |
|
|
.EX_Rs (EX_Rs),
|
| 231 |
|
|
.EX_Rt (EX_Rt),
|
| 232 |
|
|
.EX_RtRd (EX_RtRd),
|
| 233 |
|
|
.MEM_RtRd (M_RtRd),
|
| 234 |
|
|
.WB_RtRd (WB_RtRd),
|
| 235 |
|
|
.ID_Link (ID_Link),
|
| 236 |
|
|
.EX_Link (EX_Link),
|
| 237 |
|
|
.EX_RegWrite (EX_RegWrite),
|
| 238 |
|
|
.MEM_RegWrite (M_RegWrite),
|
| 239 |
|
|
.WB_RegWrite (WB_RegWrite),
|
| 240 |
|
|
.MEM_MemRead (M_MemRead),
|
| 241 |
|
|
.MEM_MemWrite (M_MemWrite),
|
| 242 |
|
|
.InstMem_Read (InstMem_Read),
|
| 243 |
|
|
.InstMem_Ready (InstMem_Ready),
|
| 244 |
|
|
.Mfc0 (ID_Mfc0),
|
| 245 |
|
|
.IF_Exception_Stall (IF_Exception_Stall),
|
| 246 |
|
|
.ID_Exception_Stall (ID_Exception_Stall),
|
| 247 |
|
|
.EX_Exception_Stall (EX_Exception_Stall),
|
| 248 |
|
|
.M_Stall_Controller (M_Stall_Controller),
|
| 249 |
|
|
.IF_Stall (IF_Stall),
|
| 250 |
|
|
.ID_Stall (ID_Stall),
|
| 251 |
|
|
.EX_Stall (EX_Stall),
|
| 252 |
|
|
.M_Stall (M_Stall),
|
| 253 |
|
|
.WB_Stall (WB_Stall),
|
| 254 |
|
|
.ID_RsFwdSel (ID_RsFwdSel),
|
| 255 |
|
|
.ID_RtFwdSel (ID_RtFwdSel),
|
| 256 |
|
|
.EX_RsFwdSel (EX_RsFwdSel),
|
| 257 |
|
|
.EX_RtFwdSel (EX_RtFwdSel),
|
| 258 |
|
|
.M_WriteDataFwdSel (M_WriteDataFwdSel)
|
| 259 |
|
|
);
|
| 260 |
|
|
|
| 261 |
|
|
/*** Coprocessor 0: Exceptions and Interrupts ***/
|
| 262 |
|
|
CPZero CP0 (
|
| 263 |
|
|
.clock (clock),
|
| 264 |
|
|
.Mfc0 (ID_Mfc0),
|
| 265 |
|
|
.Mtc0 (ID_Mtc0),
|
| 266 |
|
|
.IF_Stall (IF_Stall),
|
| 267 |
|
|
.ID_Stall (ID_Stall),
|
| 268 |
|
|
.COP1 (ID_CP1),
|
| 269 |
|
|
.COP2 (ID_CP2),
|
| 270 |
|
|
.COP3 (ID_CP3),
|
| 271 |
|
|
.ERET (ID_Eret),
|
| 272 |
|
|
.Rd (Rd),
|
| 273 |
|
|
.Sel (Cp0_Sel),
|
| 274 |
|
|
.Reg_In (ID_ReadData2_End),
|
| 275 |
|
|
.Reg_Out (CP0_RegOut),
|
| 276 |
|
|
.KernelMode (ID_KernelMode),
|
| 277 |
|
|
.ReverseEndian (ID_ReverseEndian),
|
| 278 |
|
|
.Int (Interrupts),
|
| 279 |
|
|
.reset (reset),
|
| 280 |
|
|
.EXC_NMI (NMI),
|
| 281 |
|
|
.EXC_AdIF (IF_EXC_AdIF),
|
| 282 |
|
|
.EXC_AdEL (M_EXC_AdEL),
|
| 283 |
|
|
.EXC_AdES (M_EXC_AdES),
|
| 284 |
|
|
.EXC_Ov (EX_EXC_Ov),
|
| 285 |
|
|
.EXC_Tr (M_EXC_Tr),
|
| 286 |
|
|
.EXC_Sys (ID_EXC_Sys),
|
| 287 |
|
|
.EXC_Bp (ID_EXC_Bp),
|
| 288 |
|
|
.EXC_RI (ID_EXC_RI),
|
| 289 |
|
|
.ID_RestartPC (ID_RestartPC),
|
| 290 |
|
|
.EX_RestartPC (EX_RestartPC),
|
| 291 |
|
|
.M_RestartPC (M_RestartPC),
|
| 292 |
|
|
.ID_IsFlushed (ID_IsFlushed),
|
| 293 |
|
|
.IF_IsBD (IF_IsBDS),
|
| 294 |
|
|
.ID_IsBD (ID_IsBDS),
|
| 295 |
|
|
.EX_IsBD (EX_IsBDS),
|
| 296 |
|
|
.M_IsBD (M_IsBDS),
|
| 297 |
|
|
.BadAddr_M (M_ALUResult),
|
| 298 |
|
|
.BadAddr_IF (IF_PCOut),
|
| 299 |
|
|
.ID_CanErr (ID_CanErr),
|
| 300 |
|
|
.EX_CanErr (EX_CanErr),
|
| 301 |
|
|
.M_CanErr (M_CanErr),
|
| 302 |
|
|
.IF_Exception_Stall (IF_Exception_Stall),
|
| 303 |
|
|
.ID_Exception_Stall (ID_Exception_Stall),
|
| 304 |
|
|
.EX_Exception_Stall (EX_Exception_Stall),
|
| 305 |
|
|
.M_Exception_Stall (M_Exception_Stall),
|
| 306 |
|
|
.IF_Exception_Flush (IF_Exception_Flush),
|
| 307 |
|
|
.ID_Exception_Flush (ID_Exception_Flush),
|
| 308 |
|
|
.EX_Exception_Flush (EX_Exception_Flush),
|
| 309 |
|
|
.M_Exception_Flush (M_Exception_Flush),
|
| 310 |
|
|
.Exc_PC_Sel (ID_PCSrc_Exc),
|
| 311 |
|
|
.Exc_PC_Out (ID_ExceptionPC),
|
| 312 |
|
|
.IP (IP)
|
| 313 |
|
|
);
|
| 314 |
|
|
|
| 315 |
|
|
/*** PC Source Non-Exception Mux ***/
|
| 316 |
|
|
Mux4 #(.WIDTH(32)) PCSrcStd_Mux (
|
| 317 |
|
|
.sel (ID_PCSrc),
|
| 318 |
|
|
.in0 (IF_PCAdd4),
|
| 319 |
|
|
.in1 (ID_JumpAddress),
|
| 320 |
|
|
.in2 (ID_BranchAddress),
|
| 321 |
|
|
.in3 (ID_ReadData1_End),
|
| 322 |
|
|
.out (IF_PC_PreExc)
|
| 323 |
|
|
);
|
| 324 |
|
|
|
| 325 |
|
|
/*** PC Source Exception Mux ***/
|
| 326 |
|
|
Mux2 #(.WIDTH(32)) PCSrcExc_Mux (
|
| 327 |
|
|
.sel (ID_PCSrc_Exc),
|
| 328 |
|
|
.in0 (IF_PC_PreExc),
|
| 329 |
|
|
.in1 (ID_ExceptionPC),
|
| 330 |
|
|
.out (IF_PCIn)
|
| 331 |
|
|
);
|
| 332 |
|
|
|
| 333 |
|
|
/*** Program Counter (MIPS spec is 0xBFC00000 starting address) ***/
|
| 334 |
|
|
Register #(.WIDTH(32), .INIT(EXC_Vector_Base_Reset)) PC (
|
| 335 |
|
|
.clock (clock),
|
| 336 |
|
|
.reset (reset),
|
| 337 |
|
|
//.enable (~IF_Stall), // XXX verify. HERE. Was 1 but on stall latches PC+4, ad nauseum.
|
| 338 |
|
|
.enable (~(IF_Stall | ID_Stall)),
|
| 339 |
|
|
.D (IF_PCIn),
|
| 340 |
|
|
.Q (IF_PCOut)
|
| 341 |
|
|
);
|
| 342 |
|
|
|
| 343 |
|
|
/*** PC +4 Adder ***/
|
| 344 |
|
|
Add PC_Add4 (
|
| 345 |
|
|
.A (IF_PCOut),
|
| 346 |
|
|
.B (32'h00000004),
|
| 347 |
|
|
.C (IF_PCAdd4)
|
| 348 |
|
|
);
|
| 349 |
|
|
|
| 350 |
|
|
/*** Instruction Fetch -> Instruction Decode Stage Register ***/
|
| 351 |
|
|
IFID_Stage IFID (
|
| 352 |
|
|
.clock (clock),
|
| 353 |
|
|
.reset (reset),
|
| 354 |
|
|
.IF_Flush (IF_Exception_Flush | IF_Flush),
|
| 355 |
|
|
.IF_Stall (IF_Stall),
|
| 356 |
|
|
.ID_Stall (ID_Stall),
|
| 357 |
|
|
.IF_Instruction (IF_Instruction),
|
| 358 |
|
|
.IF_PCAdd4 (IF_PCAdd4),
|
| 359 |
|
|
.IF_PC (IF_PCOut),
|
| 360 |
|
|
.IF_IsBDS (IF_IsBDS),
|
| 361 |
|
|
.ID_Instruction (Instruction),
|
| 362 |
|
|
.ID_PCAdd4 (ID_PCAdd4),
|
| 363 |
|
|
.ID_RestartPC (ID_RestartPC),
|
| 364 |
|
|
.ID_IsBDS (ID_IsBDS),
|
| 365 |
|
|
.ID_IsFlushed (ID_IsFlushed)
|
| 366 |
|
|
);
|
| 367 |
|
|
|
| 368 |
|
|
/*** Register File ***/
|
| 369 |
|
|
RegisterFile RegisterFile (
|
| 370 |
|
|
.clock (clock),
|
| 371 |
|
|
.reset (reset),
|
| 372 |
|
|
.ReadReg1 (Rs),
|
| 373 |
|
|
.ReadReg2 (Rt),
|
| 374 |
|
|
.WriteReg (WB_RtRd),
|
| 375 |
|
|
.WriteData (WB_WriteData),
|
| 376 |
|
|
.RegWrite (WB_RegWrite),
|
| 377 |
|
|
.ReadData1 (ID_ReadData1_RF),
|
| 378 |
|
|
.ReadData2 (ID_ReadData2_RF)
|
| 379 |
|
|
);
|
| 380 |
|
|
|
| 381 |
|
|
/*** ID Rs Forwarding/Link Mux ***/
|
| 382 |
|
|
Mux4 #(.WIDTH(32)) IDRsFwd_Mux (
|
| 383 |
|
|
.sel (ID_RsFwdSel),
|
| 384 |
|
|
.in0 (ID_ReadData1_RF),
|
| 385 |
|
|
.in1 (M_ALUResult),
|
| 386 |
|
|
.in2 (WB_WriteData),
|
| 387 |
|
|
.in3 (ID_PCAdd4),
|
| 388 |
|
|
.out (ID_ReadData1_End)
|
| 389 |
|
|
);
|
| 390 |
|
|
|
| 391 |
|
|
/*** ID Rt Forwarding/CP0 Mfc0 Mux ***/
|
| 392 |
|
|
Mux4 #(.WIDTH(32)) IDRtFwd_Mux (
|
| 393 |
|
|
.sel (ID_RtFwdSel),
|
| 394 |
|
|
.in0 (ID_ReadData2_RF),
|
| 395 |
|
|
.in1 (M_ALUResult),
|
| 396 |
|
|
.in2 (WB_WriteData),
|
| 397 |
|
|
.in3 (CP0_RegOut),
|
| 398 |
|
|
.out (ID_ReadData2_End)
|
| 399 |
|
|
);
|
| 400 |
|
|
|
| 401 |
|
|
/*** Condition Compare Unit ***/
|
| 402 |
|
|
Compare Compare (
|
| 403 |
|
|
.A (ID_ReadData1_End),
|
| 404 |
|
|
.B (ID_ReadData2_End),
|
| 405 |
|
|
.EQ (ID_CmpEQ),
|
| 406 |
|
|
.GZ (ID_CmpGZ),
|
| 407 |
|
|
.LZ (ID_CmpLZ),
|
| 408 |
|
|
.GEZ (ID_CmpGEZ),
|
| 409 |
|
|
.LEZ (ID_CmpLEZ)
|
| 410 |
|
|
);
|
| 411 |
|
|
|
| 412 |
|
|
/*** Branch Address Adder ***/
|
| 413 |
|
|
Add BranchAddress_Add (
|
| 414 |
|
|
.A (ID_PCAdd4),
|
| 415 |
|
|
.B (ID_ImmLeftShift2),
|
| 416 |
|
|
.C (ID_BranchAddress)
|
| 417 |
|
|
);
|
| 418 |
|
|
|
| 419 |
|
|
/*** Instruction Decode -> Execute Pipeline Stage ***/
|
| 420 |
|
|
IDEX_Stage IDEX (
|
| 421 |
|
|
.clock (clock),
|
| 422 |
|
|
.reset (reset),
|
| 423 |
|
|
.ID_Flush (ID_Exception_Flush),
|
| 424 |
|
|
.ID_Stall (ID_Stall),
|
| 425 |
|
|
.EX_Stall (EX_Stall),
|
| 426 |
|
|
.ID_Link (ID_Link),
|
| 427 |
|
|
.ID_RegDst (ID_RegDst),
|
| 428 |
|
|
.ID_ALUSrcImm (ID_ALUSrcImm),
|
| 429 |
|
|
.ID_ALUOp (ID_ALUOp),
|
| 430 |
|
|
.ID_Movn (ID_Movn),
|
| 431 |
|
|
.ID_Movz (ID_Movz),
|
| 432 |
|
|
.ID_LLSC (ID_LLSC),
|
| 433 |
|
|
.ID_MemRead (ID_MemRead),
|
| 434 |
|
|
.ID_MemWrite (ID_MemWrite),
|
| 435 |
|
|
.ID_MemByte (ID_MemByte),
|
| 436 |
|
|
.ID_MemHalf (ID_MemHalf),
|
| 437 |
|
|
.ID_MemSignExtend (ID_MemSignExtend),
|
| 438 |
|
|
.ID_Left (ID_Left),
|
| 439 |
|
|
.ID_Right (ID_Right),
|
| 440 |
|
|
.ID_RegWrite (ID_RegWrite),
|
| 441 |
|
|
.ID_MemtoReg (ID_MemtoReg),
|
| 442 |
|
|
.ID_ReverseEndian (ID_ReverseEndian),
|
| 443 |
|
|
.ID_Rs (Rs),
|
| 444 |
|
|
.ID_Rt (Rt),
|
| 445 |
|
|
.ID_WantRsByEX (ID_DP_Hazards[3]),
|
| 446 |
|
|
.ID_NeedRsByEX (ID_DP_Hazards[2]),
|
| 447 |
|
|
.ID_WantRtByEX (ID_DP_Hazards[1]),
|
| 448 |
|
|
.ID_NeedRtByEX (ID_DP_Hazards[0]),
|
| 449 |
|
|
.ID_KernelMode (ID_KernelMode),
|
| 450 |
|
|
.ID_RestartPC (ID_RestartPC),
|
| 451 |
|
|
.ID_IsBDS (ID_IsBDS),
|
| 452 |
|
|
.ID_Trap (ID_Trap),
|
| 453 |
|
|
.ID_TrapCond (ID_TrapCond),
|
| 454 |
|
|
.ID_EX_CanErr (ID_EX_CanErr),
|
| 455 |
|
|
.ID_M_CanErr (ID_M_CanErr),
|
| 456 |
|
|
.ID_ReadData1 (ID_ReadData1_End),
|
| 457 |
|
|
.ID_ReadData2 (ID_ReadData2_End),
|
| 458 |
|
|
.ID_SignExtImm (ID_SignExtImm[16:0]),
|
| 459 |
|
|
.EX_Link (EX_Link),
|
| 460 |
|
|
.EX_LinkRegDst (EX_LinkRegDst),
|
| 461 |
|
|
.EX_ALUSrcImm (EX_ALUSrcImm),
|
| 462 |
|
|
.EX_ALUOp (EX_ALUOp),
|
| 463 |
|
|
.EX_Movn (EX_Movn),
|
| 464 |
|
|
.EX_Movz (EX_Movz),
|
| 465 |
|
|
.EX_LLSC (EX_LLSC),
|
| 466 |
|
|
.EX_MemRead (EX_MemRead),
|
| 467 |
|
|
.EX_MemWrite (EX_MemWrite),
|
| 468 |
|
|
.EX_MemByte (EX_MemByte),
|
| 469 |
|
|
.EX_MemHalf (EX_MemHalf),
|
| 470 |
|
|
.EX_MemSignExtend (EX_MemSignExtend),
|
| 471 |
|
|
.EX_Left (EX_Left),
|
| 472 |
|
|
.EX_Right (EX_Right),
|
| 473 |
|
|
.EX_RegWrite (EX_RegWrite),
|
| 474 |
|
|
.EX_MemtoReg (EX_MemtoReg),
|
| 475 |
|
|
.EX_ReverseEndian (EX_ReverseEndian),
|
| 476 |
|
|
.EX_Rs (EX_Rs),
|
| 477 |
|
|
.EX_Rt (EX_Rt),
|
| 478 |
|
|
.EX_WantRsByEX (EX_WantRsByEX),
|
| 479 |
|
|
.EX_NeedRsByEX (EX_NeedRsByEX),
|
| 480 |
|
|
.EX_WantRtByEX (EX_WantRtByEX),
|
| 481 |
|
|
.EX_NeedRtByEX (EX_NeedRtByEX),
|
| 482 |
|
|
.EX_KernelMode (EX_KernelMode),
|
| 483 |
|
|
.EX_RestartPC (EX_RestartPC),
|
| 484 |
|
|
.EX_IsBDS (EX_IsBDS),
|
| 485 |
|
|
.EX_Trap (EX_Trap),
|
| 486 |
|
|
.EX_TrapCond (EX_TrapCond),
|
| 487 |
|
|
.EX_EX_CanErr (EX_EX_CanErr),
|
| 488 |
|
|
.EX_M_CanErr (EX_M_CanErr),
|
| 489 |
|
|
.EX_ReadData1 (EX_ReadData1_PR),
|
| 490 |
|
|
.EX_ReadData2 (EX_ReadData2_PR),
|
| 491 |
|
|
.EX_SignExtImm (EX_SignExtImm),
|
| 492 |
|
|
.EX_Rd (EX_Rd),
|
| 493 |
|
|
.EX_Shamt (EX_Shamt)
|
| 494 |
|
|
);
|
| 495 |
|
|
|
| 496 |
|
|
/*** EX Rs Forwarding Mux ***/
|
| 497 |
|
|
Mux4 #(.WIDTH(32)) EXRsFwd_Mux (
|
| 498 |
|
|
.sel (EX_RsFwdSel),
|
| 499 |
|
|
.in0 (EX_ReadData1_PR),
|
| 500 |
|
|
.in1 (M_ALUResult),
|
| 501 |
|
|
.in2 (WB_WriteData),
|
| 502 |
|
|
.in3 (32'hxxxxxxxx),
|
| 503 |
|
|
.out (EX_ReadData1_Fwd)
|
| 504 |
|
|
);
|
| 505 |
|
|
|
| 506 |
|
|
/*** EX Rt Forwarding / Link Mux ***/
|
| 507 |
|
|
Mux4 #(.WIDTH(32)) EXRtFwdLnk_Mux (
|
| 508 |
|
|
.sel (EX_RtFwdSel),
|
| 509 |
|
|
.in0 (EX_ReadData2_PR),
|
| 510 |
|
|
.in1 (M_ALUResult),
|
| 511 |
|
|
.in2 (WB_WriteData),
|
| 512 |
|
|
.in3 (32'h00000004),
|
| 513 |
|
|
.out (EX_ReadData2_Fwd)
|
| 514 |
|
|
);
|
| 515 |
|
|
|
| 516 |
|
|
/*** EX ALU Immediate Mux ***/
|
| 517 |
|
|
Mux2 #(.WIDTH(32)) EXALUImm_Mux (
|
| 518 |
|
|
.sel (EX_ALUSrcImm),
|
| 519 |
|
|
.in0 (EX_ReadData2_Fwd),
|
| 520 |
|
|
.in1 (EX_SignExtImm),
|
| 521 |
|
|
.out (EX_ReadData2_Imm)
|
| 522 |
|
|
);
|
| 523 |
|
|
|
| 524 |
|
|
/*** EX RtRd / Link Mux ***/
|
| 525 |
|
|
Mux4 #(.WIDTH(5)) EXRtRdLnk_Mux (
|
| 526 |
|
|
.sel (EX_LinkRegDst),
|
| 527 |
|
|
.in0 (EX_Rt),
|
| 528 |
|
|
.in1 (EX_Rd),
|
| 529 |
|
|
.in2 (5'b11111),
|
| 530 |
|
|
.in3 (5'bxxxxx),
|
| 531 |
|
|
.out (EX_RtRd)
|
| 532 |
|
|
);
|
| 533 |
|
|
|
| 534 |
|
|
/*** Arithmetic Logic Unit ***/
|
| 535 |
|
|
ALU ALU (
|
| 536 |
|
|
.clock (clock),
|
| 537 |
|
|
.reset (reset),
|
| 538 |
|
|
.EX_Stall (EX_Stall),
|
| 539 |
|
|
.EX_Flush (EX_Exception_Flush),
|
| 540 |
|
|
.A (EX_ReadData1_Fwd),
|
| 541 |
|
|
.B (EX_ReadData2_Imm),
|
| 542 |
|
|
.Operation (EX_ALUOp),
|
| 543 |
|
|
.Shamt (EX_Shamt),
|
| 544 |
|
|
.Result (EX_ALUResult),
|
| 545 |
|
|
.BZero (EX_BZero),
|
| 546 |
|
|
.EXC_Ov (EX_EXC_Ov)
|
| 547 |
|
|
);
|
| 548 |
|
|
|
| 549 |
|
|
/*** Execute -> Memory Pipeline Stage ***/
|
| 550 |
|
|
EXMEM_Stage EXMEM (
|
| 551 |
|
|
.clock (clock),
|
| 552 |
|
|
.reset (reset),
|
| 553 |
|
|
.EX_Flush (EX_Exception_Flush),
|
| 554 |
|
|
.EX_Stall (EX_Stall),
|
| 555 |
|
|
.M_Stall (M_Stall),
|
| 556 |
|
|
.EX_Movn (EX_Movn),
|
| 557 |
|
|
.EX_Movz (EX_Movz),
|
| 558 |
|
|
.EX_BZero (EX_BZero),
|
| 559 |
|
|
.EX_RegWrite (EX_RegWrite),
|
| 560 |
|
|
.EX_MemtoReg (EX_MemtoReg),
|
| 561 |
|
|
.EX_ReverseEndian (EX_ReverseEndian),
|
| 562 |
|
|
.EX_LLSC (EX_LLSC),
|
| 563 |
|
|
.EX_MemRead (EX_MemRead),
|
| 564 |
|
|
.EX_MemWrite (EX_MemWrite),
|
| 565 |
|
|
.EX_MemByte (EX_MemByte),
|
| 566 |
|
|
.EX_MemHalf (EX_MemHalf),
|
| 567 |
|
|
.EX_MemSignExtend (EX_MemSignExtend),
|
| 568 |
|
|
.EX_Left (EX_Left),
|
| 569 |
|
|
.EX_Right (EX_Right),
|
| 570 |
|
|
.EX_KernelMode (EX_KernelMode),
|
| 571 |
|
|
.EX_RestartPC (EX_RestartPC),
|
| 572 |
|
|
.EX_IsBDS (EX_IsBDS),
|
| 573 |
|
|
.EX_Trap (EX_Trap),
|
| 574 |
|
|
.EX_TrapCond (EX_TrapCond),
|
| 575 |
|
|
.EX_M_CanErr (EX_M_CanErr),
|
| 576 |
|
|
.EX_ALU_Result (EX_ALUResult),
|
| 577 |
|
|
.EX_ReadData2 (EX_ReadData2_Fwd),
|
| 578 |
|
|
.EX_RtRd (EX_RtRd),
|
| 579 |
|
|
.M_RegWrite (M_RegWrite),
|
| 580 |
|
|
.M_MemtoReg (M_MemtoReg),
|
| 581 |
|
|
.M_ReverseEndian (M_ReverseEndian),
|
| 582 |
|
|
.M_LLSC (M_LLSC),
|
| 583 |
|
|
.M_MemRead (M_MemRead),
|
| 584 |
|
|
.M_MemWrite (M_MemWrite),
|
| 585 |
|
|
.M_MemByte (M_MemByte),
|
| 586 |
|
|
.M_MemHalf (M_MemHalf),
|
| 587 |
|
|
.M_MemSignExtend (M_MemSignExtend),
|
| 588 |
|
|
.M_Left (M_Left),
|
| 589 |
|
|
.M_Right (M_Right),
|
| 590 |
|
|
.M_KernelMode (M_KernelMode),
|
| 591 |
|
|
.M_RestartPC (M_RestartPC),
|
| 592 |
|
|
.M_IsBDS (M_IsBDS),
|
| 593 |
|
|
.M_Trap (M_Trap),
|
| 594 |
|
|
.M_TrapCond (M_TrapCond),
|
| 595 |
|
|
.M_M_CanErr (M_M_CanErr),
|
| 596 |
|
|
.M_ALU_Result (M_ALUResult),
|
| 597 |
|
|
.M_ReadData2 (M_ReadData2_PR),
|
| 598 |
|
|
.M_RtRd (M_RtRd)
|
| 599 |
|
|
);
|
| 600 |
|
|
|
| 601 |
|
|
/*** Trap Detection Unit ***/
|
| 602 |
|
|
TrapDetect TrapDetect (
|
| 603 |
|
|
.Trap (M_Trap),
|
| 604 |
|
|
.TrapCond (M_TrapCond),
|
| 605 |
|
|
.ALUResult (M_ALUResult),
|
| 606 |
|
|
.EXC_Tr (M_EXC_Tr)
|
| 607 |
|
|
);
|
| 608 |
|
|
|
| 609 |
|
|
/*** MEM Write Data Mux ***/
|
| 610 |
|
|
Mux2 #(.WIDTH(32)) MWriteData_Mux (
|
| 611 |
|
|
.sel (M_WriteDataFwdSel),
|
| 612 |
|
|
.in0 (M_ReadData2_PR),
|
| 613 |
|
|
.in1 (WB_WriteData),
|
| 614 |
|
|
.out (M_WriteData_Pre)
|
| 615 |
|
|
);
|
| 616 |
|
|
|
| 617 |
|
|
/*** Data Memory Controller ***/
|
| 618 |
|
|
MemControl DataMem_Controller (
|
| 619 |
|
|
.clock (clock),
|
| 620 |
|
|
.reset (reset),
|
| 621 |
|
|
.DataIn (M_WriteData_Pre),
|
| 622 |
|
|
.Address (M_ALUResult),
|
| 623 |
|
|
.MReadData (DataMem_In),
|
| 624 |
|
|
.MemRead (M_MemRead),
|
| 625 |
|
|
.MemWrite (M_MemWrite),
|
| 626 |
|
|
.DataMem_Ready (DataMem_Ready),
|
| 627 |
|
|
.Byte (M_MemByte),
|
| 628 |
|
|
.Half (M_MemHalf),
|
| 629 |
|
|
.SignExtend (M_MemSignExtend),
|
| 630 |
|
|
.KernelMode (M_KernelMode),
|
| 631 |
|
|
.ReverseEndian (M_ReverseEndian),
|
| 632 |
|
|
.LLSC (M_LLSC),
|
| 633 |
|
|
.ERET (ID_Eret),
|
| 634 |
|
|
.Left (M_Left),
|
| 635 |
|
|
.Right (M_Right),
|
| 636 |
|
|
.M_Exception_Stall (M_Exception_Stall),
|
| 637 |
|
|
|
| 638 |
|
|
.IF_Stall (IF_Stall),
|
| 639 |
|
|
|
| 640 |
|
|
.DataOut (M_MemReadData),
|
| 641 |
|
|
.MWriteData (DataMem_Out),
|
| 642 |
|
|
.WriteEnable (DataMem_Write),
|
| 643 |
|
|
.ReadEnable (DataMem_Read),
|
| 644 |
|
|
.M_Stall (M_Stall_Controller),
|
| 645 |
|
|
.EXC_AdEL (M_EXC_AdEL),
|
| 646 |
|
|
.EXC_AdES (M_EXC_AdES)
|
| 647 |
|
|
);
|
| 648 |
|
|
|
| 649 |
|
|
/*** Memory -> Writeback Pipeline Stage ***/
|
| 650 |
|
|
MEMWB_Stage MEMWB (
|
| 651 |
|
|
.clock (clock),
|
| 652 |
|
|
.reset (reset),
|
| 653 |
|
|
.M_Flush (M_Exception_Flush),
|
| 654 |
|
|
.M_Stall (M_Stall),
|
| 655 |
|
|
.WB_Stall (WB_Stall),
|
| 656 |
|
|
.M_RegWrite (M_RegWrite),
|
| 657 |
|
|
.M_MemtoReg (M_MemtoReg),
|
| 658 |
|
|
.M_ReadData (M_MemReadData),
|
| 659 |
|
|
.M_ALU_Result (M_ALUResult),
|
| 660 |
|
|
.M_RtRd (M_RtRd),
|
| 661 |
|
|
.WB_RegWrite (WB_RegWrite),
|
| 662 |
|
|
.WB_MemtoReg (WB_MemtoReg),
|
| 663 |
|
|
.WB_ReadData (WB_ReadData),
|
| 664 |
|
|
.WB_ALU_Result (WB_ALUResult),
|
| 665 |
|
|
.WB_RtRd (WB_RtRd)
|
| 666 |
|
|
);
|
| 667 |
|
|
|
| 668 |
|
|
/*** WB MemtoReg Mux ***/
|
| 669 |
|
|
Mux2 #(.WIDTH(32)) WBMemtoReg_Mux (
|
| 670 |
|
|
.sel (WB_MemtoReg),
|
| 671 |
|
|
.in0 (WB_ALUResult),
|
| 672 |
|
|
.in1 (WB_ReadData),
|
| 673 |
|
|
.out (WB_WriteData)
|
| 674 |
|
|
);
|
| 675 |
|
|
|
| 676 |
|
|
endmodule
|