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-12 15:15:14 +00:00
|
|
|
// Documentation: RISC-V System on Chip Design Chapter 12 (Figure 12.21)
|
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2024-01-29 13:38:11 +00:00
|
|
|
// https://github.com/openhwgroup/cvw
|
2021-02-16 03:27:35 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-02-16 03:27:35 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-02-16 03:27:35 +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-02-16 03:27:35 +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-02-16 03:27:35 +00:00
|
|
|
|
2023-05-24 20:01:35 +00:00
|
|
|
module mdu import cvw::*; #(parameter cvw_t P) (
|
2023-06-12 20:54:54 +00:00
|
|
|
input logic clk, reset,
|
|
|
|
input logic StallM, StallW,
|
|
|
|
input logic FlushE, FlushM, FlushW,
|
2023-05-24 20:01:35 +00:00
|
|
|
input logic [P.XLEN-1:0] ForwardedSrcAE, ForwardedSrcBE, // inputs A and B from IEU forwarding mux output
|
2023-06-12 20:54:54 +00:00
|
|
|
input logic [2:0] Funct3E, Funct3M, // type of MDU operation
|
|
|
|
input logic IntDivE, W64E, // Integer division/remainder, and W-type instrutions
|
2023-06-15 19:17:23 +00:00
|
|
|
input logic MDUActiveE, // Mul/Div instruction being executed
|
2023-05-24 20:01:35 +00:00
|
|
|
output logic [P.XLEN-1:0] MDUResultW, // multiply/divide result
|
2023-06-12 20:54:54 +00:00
|
|
|
output logic DivBusyE // busy signal to stall pipeline in Execute stage
|
2023-01-12 15:15:14 +00:00
|
|
|
);
|
2021-04-02 20:32:15 +00:00
|
|
|
|
2023-05-24 20:01:35 +00:00
|
|
|
logic [P.XLEN*2-1:0] ProdM; // double-width product from mul
|
|
|
|
logic [P.XLEN-1:0] QuotM, RemM; // quotient and remainder from intdivrestoring
|
|
|
|
logic [P.XLEN-1:0] PrelimResultM; // selected result before W truncation
|
|
|
|
logic [P.XLEN-1:0] MDUResultM; // result after W truncation
|
2023-06-12 20:54:54 +00:00
|
|
|
logic W64M; // W-type instruction
|
2021-10-02 13:19:25 +00:00
|
|
|
|
2023-06-15 19:17:23 +00:00
|
|
|
logic [P.XLEN-1:0] AMDU, BMDU; // Gated inputs to MDU
|
|
|
|
|
|
|
|
// gate data inputs to MDU to only operate when MDU is active.
|
|
|
|
assign AMDU = ForwardedSrcAE & {P.XLEN{MDUActiveE}};
|
|
|
|
assign BMDU = ForwardedSrcBE & {P.XLEN{MDUActiveE}};
|
|
|
|
|
2023-03-24 22:32:25 +00:00
|
|
|
// Multiplier
|
2023-05-24 20:01:35 +00:00
|
|
|
mul #(P.XLEN) mul(.clk, .reset, .StallM, .FlushM, .ForwardedSrcAE, .ForwardedSrcBE, .Funct3E, .ProdM);
|
2021-03-30 19:25:07 +00:00
|
|
|
|
2023-03-24 22:32:25 +00:00
|
|
|
// Divider
|
|
|
|
// Start a divide when a new division instruction is received and the divider isn't already busy or finishing
|
|
|
|
// When IDIV_ON_FPU is set, use the FPU divider instead
|
|
|
|
// In ZMMUL, with M_SUPPORTED = 0, omit the divider
|
2023-11-18 04:25:24 +00:00
|
|
|
if ((P.IDIV_ON_FPU & P.F_SUPPORTED) || (!P.M_SUPPORTED)) begin:nodiv
|
2024-04-21 15:40:11 +00:00
|
|
|
assign QuotM = '0;
|
|
|
|
assign RemM = '0;
|
|
|
|
assign DivBusyE = 1'b0;
|
2023-03-24 22:32:25 +00:00
|
|
|
end else begin:div
|
2023-05-24 20:01:35 +00:00
|
|
|
div #(P) div(.clk, .reset, .StallM, .FlushE, .DivSignedE(~Funct3E[0]), .W64E, .IntDivE,
|
2023-03-24 22:32:25 +00:00
|
|
|
.ForwardedSrcAE, .ForwardedSrcBE, .DivBusyE, .QuotM, .RemM);
|
|
|
|
end
|
|
|
|
|
|
|
|
// Result multiplexer
|
|
|
|
// For ZMMUL, QuotM and RemM are tied to 0, so the mux automatically simplifies
|
|
|
|
always_comb
|
|
|
|
case (Funct3M)
|
2023-05-24 20:01:35 +00:00
|
|
|
3'b000: PrelimResultM = ProdM[P.XLEN-1:0]; // mul
|
2023-06-12 20:54:54 +00:00
|
|
|
3'b001: PrelimResultM = ProdM[P.XLEN*2-1:P.XLEN]; // mulh
|
|
|
|
3'b010: PrelimResultM = ProdM[P.XLEN*2-1:P.XLEN]; // mulhsu
|
|
|
|
3'b011: PrelimResultM = ProdM[P.XLEN*2-1:P.XLEN]; // mulhu
|
|
|
|
3'b100: PrelimResultM = QuotM; // div
|
|
|
|
3'b101: PrelimResultM = QuotM; // divu
|
|
|
|
3'b110: PrelimResultM = RemM; // rem
|
|
|
|
3'b111: PrelimResultM = RemM; // remu
|
2023-03-24 22:32:25 +00:00
|
|
|
endcase
|
2021-10-03 15:22:34 +00:00
|
|
|
|
2023-03-24 22:32:25 +00:00
|
|
|
// Handle sign extension for W-type instructions
|
|
|
|
flopenrc #(1) W64MReg(clk, reset, FlushM, ~StallM, W64E, W64M);
|
2023-05-24 20:01:35 +00:00
|
|
|
if (P.XLEN == 64) begin:resmux // RV64 has W-type instructions
|
2023-03-24 22:32:25 +00:00
|
|
|
assign MDUResultM = W64M ? {{32{PrelimResultM[31]}}, PrelimResultM[31:0]} : PrelimResultM;
|
|
|
|
end else begin:resmux // RV32 has no W-type instructions
|
|
|
|
assign MDUResultM = PrelimResultM;
|
|
|
|
end
|
2021-03-30 19:25:07 +00:00
|
|
|
|
2023-03-24 22:32:25 +00:00
|
|
|
// Writeback stage pipeline register
|
2023-05-24 20:01:35 +00:00
|
|
|
flopenrc #(P.XLEN) MDUResultWReg(clk, reset, FlushW, ~StallW, MDUResultM, MDUResultW);
|
2022-12-28 03:57:10 +00:00
|
|
|
endmodule // mdu
|