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
|
|
|
//
|
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
|
|
|
|
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2022-12-28 03:57:10 +00:00
|
|
|
module mdu (
|
2021-03-30 19:25:07 +00:00
|
|
|
input logic clk, reset,
|
|
|
|
// Execute Stage interface
|
2021-11-17 18:53:17 +00:00
|
|
|
// 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
|
2021-10-02 13:38:02 +00:00
|
|
|
input logic [2:0] Funct3E, Funct3M,
|
2023-01-11 19:06:37 +00:00
|
|
|
input logic IntDivE, W64E,
|
2021-03-30 19:25:07 +00:00
|
|
|
// Writeback stage
|
2022-01-07 04:30:00 +00:00
|
|
|
output logic [`XLEN-1:0] MDUResultW,
|
2021-03-30 19:25:07 +00:00
|
|
|
// Divide Done
|
2021-04-02 11:27:37 +00:00
|
|
|
output logic DivBusyE,
|
2021-03-30 19:25:07 +00:00
|
|
|
// hazards
|
2022-12-15 19:00:54 +00:00
|
|
|
input logic StallM, StallW, FlushE, FlushM, FlushW
|
2021-03-30 19:25:07 +00:00
|
|
|
);
|
2021-02-16 03:27:35 +00:00
|
|
|
|
2022-01-07 04:30:00 +00:00
|
|
|
logic [`XLEN-1:0] MDUResultM;
|
2021-12-20 00:53:41 +00:00
|
|
|
logic [`XLEN-1:0] PrelimResultM;
|
|
|
|
logic [`XLEN-1:0] QuotM, RemM;
|
|
|
|
logic [`XLEN*2-1:0] ProdM;
|
2021-04-02 20:32:15 +00:00
|
|
|
|
2021-12-20 00:53:41 +00:00
|
|
|
logic DivSignedE;
|
|
|
|
logic W64M;
|
2021-10-02 13:19:25 +00:00
|
|
|
|
2021-12-20 00:53:41 +00:00
|
|
|
// Multiplier
|
|
|
|
mul mul(.clk, .reset, .StallM, .FlushM, .ForwardedSrcAE, .ForwardedSrcBE, .Funct3E, .ProdM);
|
2021-03-30 19:25:07 +00:00
|
|
|
|
2021-12-20 00:53:41 +00:00
|
|
|
// Divide
|
|
|
|
// Start a divide when a new division instruction is received and the divider isn't already busy or finishing
|
2022-12-15 01:03:13 +00:00
|
|
|
// When F extensions are supported, use the FPU divider instead
|
2022-12-15 14:37:55 +00:00
|
|
|
if (`IDIV_ON_FPU) begin
|
2022-12-15 01:03:13 +00:00
|
|
|
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,
|
2022-12-15 01:03:13 +00:00
|
|
|
.ForwardedSrcAE, .ForwardedSrcBE, .DivBusyE, .QuotM, .RemM);
|
|
|
|
end
|
2021-12-20 00:53:41 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2021-12-20 00:53:41 +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
|
2022-01-07 04:30:00 +00:00
|
|
|
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
|
2022-01-07 04:30:00 +00:00
|
|
|
assign MDUResultM = PrelimResultM;
|
2022-01-05 14:35:25 +00:00
|
|
|
end
|
2021-03-30 19:25:07 +00:00
|
|
|
|
2021-12-20 00:53:41 +00:00
|
|
|
// Writeback stage pipeline register
|
2022-01-07 04:30:00 +00:00
|
|
|
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
|
|
|
|