cvw/pipelined/src/mdu/mdu.sv

94 lines
3.3 KiB
Systemverilog
Raw Normal View History

2021-02-16 03:27:35 +00:00
///////////////////////////////////////////
2022-12-28 03:57:10 +00:00
// mdu.sv
2021-02-16 03:27:35 +00:00
//
// Written: David_Harris@hmc.edu 9 January 2021
// Modified:
//
// Purpose: M extension multiply and divide
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-02-16 03:27:35 +00:00
//
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-02-16 03:27:35 +00:00
//
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-02-16 03:27:35 +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-02-16 03:27:35 +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.
////////////////////////////////////////////////////////////////////////////////////////////////
2021-02-16 03:27:35 +00:00
`include "wally-config.vh"
2022-12-28 03:57:10 +00:00
module mdu (
input logic clk, reset,
// Execute Stage interface
// input logic [`XLEN-1:0] SrcAE, SrcBE,
input logic [`XLEN-1:0] ForwardedSrcAE, ForwardedSrcBE, // *** these are the src outputs before the mux choosing between them and PCE to put in srcA/B
input logic [2:0] Funct3E, Funct3M,
2023-01-11 19:06:37 +00:00
input logic IntDivE, W64E,
// Writeback stage
output logic [`XLEN-1:0] MDUResultW,
// Divide Done
output logic DivBusyE,
// hazards
input logic StallM, StallW, FlushE, FlushM, FlushW
);
2021-02-16 03:27:35 +00:00
logic [`XLEN-1:0] MDUResultM;
logic [`XLEN-1:0] PrelimResultM;
logic [`XLEN-1:0] QuotM, RemM;
logic [`XLEN*2-1:0] ProdM;
logic DivSignedE;
logic W64M;
2021-10-02 13:19:25 +00:00
// Multiplier
mul mul(.clk, .reset, .StallM, .FlushM, .ForwardedSrcAE, .ForwardedSrcBE, .Funct3E, .ProdM);
// Divide
// Start a divide when a new division instruction is received and the divider isn't already busy or finishing
// When F extensions are supported, use the FPU divider instead
if (`IDIV_ON_FPU) begin
assign QuotM = 0;
assign RemM = 0;
assign DivBusyE = 0;
end else begin
assign DivSignedE = ~Funct3E[0];
2023-01-11 19:06:37 +00:00
intdivrestoring div(.clk, .reset, .StallM, .FlushE, .DivSignedE, .W64E, .IntDivE,
.ForwardedSrcAE, .ForwardedSrcBE, .DivBusyE, .QuotM, .RemM);
end
// Result multiplexer
always_comb
case (Funct3M)
3'b000: PrelimResultM = ProdM[`XLEN-1:0];
3'b001: PrelimResultM = ProdM[`XLEN*2-1:`XLEN];
3'b010: PrelimResultM = ProdM[`XLEN*2-1:`XLEN];
3'b011: PrelimResultM = ProdM[`XLEN*2-1:`XLEN];
3'b100: PrelimResultM = QuotM;
3'b101: PrelimResultM = QuotM;
3'b110: PrelimResultM = RemM;
3'b111: PrelimResultM = RemM;
endcase
2021-10-03 15:22:34 +00:00
// Handle sign extension for W-type instructions
flopenrc #(1) W64MReg(clk, reset, FlushM, ~StallM, W64E, W64M);
2022-01-05 14:35:25 +00:00
if (`XLEN == 64) begin:resmux // RV64 has W-type instructions
assign MDUResultM = W64M ? {{32{PrelimResultM[31]}}, PrelimResultM[31:0]} : PrelimResultM;
2022-01-05 14:35:25 +00:00
end else begin:resmux // RV32 has no W-type instructions
assign MDUResultM = PrelimResultM;
2022-01-05 14:35:25 +00:00
end
// Writeback stage pipeline register
flopenrc #(`XLEN) MDUResultWReg(clk, reset, FlushW, ~StallW, MDUResultM, MDUResultW);
2022-12-28 03:57:10 +00:00
endmodule // mdu
2021-02-17 20:29:20 +00:00
2021-02-16 03:27:35 +00:00