2021-01-15 04:37:51 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// alu.sv
|
|
|
|
//
|
2023-03-04 05:52:34 +00:00
|
|
|
// Written: David_Harris@hmc.edu, Sarah.Harris@unlv.edu, kekim@hmc.edu
|
2023-01-11 23:20:41 +00:00
|
|
|
// Created: 9 January 2021
|
2023-03-04 05:52:34 +00:00
|
|
|
// Modified: 3 March 2023
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
|
|
|
// Purpose: RISC-V Arithmetic/Logic Unit
|
2023-01-12 00:52:46 +00:00
|
|
|
//
|
|
|
|
// Documentation: RISC-V System on Chip Design Chapter 4 (Figure 4.4)
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-11 23:15:08 +00:00
|
|
|
// A component of the CORE-V-WALLY configurable RISC-V project.
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
|
2021-01-15 04:37:51 +00:00
|
|
|
//
|
2023-01-10 19:35:20 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
|
2021-01-15 04:37:51 +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-01-15 04:37:51 +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-01-15 04:37:51 +00:00
|
|
|
|
2021-01-23 15:48:12 +00:00
|
|
|
`include "wally-config.vh"
|
2021-01-15 04:37:51 +00:00
|
|
|
|
|
|
|
module alu #(parameter WIDTH=32) (
|
2023-03-05 06:44:03 +00:00
|
|
|
input logic [WIDTH-1:0] A, B, // Operands
|
|
|
|
input logic [2:0] ALUControl, // With Funct3, indicates operation to perform
|
|
|
|
input logic [2:0] ALUSelect, // ALU mux select signal
|
2023-03-05 07:19:31 +00:00
|
|
|
input logic [1:0] BSelect, // One-Hot encoding of if it's a ZBA_ZBB_ZBC_ZBS instruction
|
2023-03-05 06:44:03 +00:00
|
|
|
input logic [2:0] ZBBSelect, // ZBB mux select signal
|
|
|
|
input logic [2:0] Funct3, // With ALUControl, indicates operation to perform NOTE: Change signal name to ALUSelect
|
|
|
|
input logic [1:0] CompFlags, // Comparator flags
|
|
|
|
input logic [2:0] BALUControl, // ALU Control signals for B instructions in Execute Stage
|
2023-03-06 14:19:01 +00:00
|
|
|
output logic [WIDTH-1:0] Result, // ALU result
|
2023-03-05 06:44:03 +00:00
|
|
|
output logic [WIDTH-1:0] Sum); // Sum of operands
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2023-02-12 05:22:33 +00:00
|
|
|
// CondInvB = ~B when subtracting, B otherwise. Shift = shift result. SLT/U = result of a slt/u instruction.
|
2023-01-17 14:02:26 +00:00
|
|
|
// FullResult = ALU result before adjusting for a RV64 w-suffix instruction.
|
2023-03-06 14:19:01 +00:00
|
|
|
logic [WIDTH-1:0] CondInvB,CondMaskInvB, Shift, SLT, SLTU, FullResult,ALUResult; // Intermediate Signals
|
|
|
|
logic [WIDTH-1:0] ZBCResult, ZBBResult; // Result of ZBB, ZBC
|
2023-02-18 04:15:12 +00:00
|
|
|
logic [WIDTH-1:0] MaskB; // BitMask of B
|
|
|
|
logic [WIDTH-1:0] CondMaskB; // Result of B mask select mux
|
|
|
|
logic [WIDTH-1:0] CondShiftA; // Result of A shifted select mux
|
|
|
|
logic [WIDTH-1:0] CondZextA; // Result of Zero Extend A select mux
|
2023-03-05 22:06:24 +00:00
|
|
|
logic [WIDTH-1:0] RevA; // Bit-reversed A
|
2023-02-17 16:21:55 +00:00
|
|
|
logic Carry, Neg; // Flags: carry out, negative
|
|
|
|
logic LT, LTU; // Less than, Less than unsigned
|
|
|
|
logic W64; // RV64 W-type instruction
|
|
|
|
logic SubArith; // Performing subtraction or arithmetic right shift
|
|
|
|
logic ALUOp; // 0 for address generation addition or 1 for regular ALU ops
|
|
|
|
logic Asign, Bsign; // Sign bits of A, B
|
2023-02-25 01:09:56 +00:00
|
|
|
logic [WIDTH:0] shA; // XLEN+1 bit input source to shifter
|
2023-02-25 01:33:47 +00:00
|
|
|
logic [WIDTH-1:0] rotA; // XLEN bit input source to shifter
|
2023-02-28 19:41:40 +00:00
|
|
|
logic [1:0] shASelect; // select signal for shifter source generation mux
|
2023-03-05 06:44:03 +00:00
|
|
|
logic Rotate; // Indicates if it is Rotate instruction
|
|
|
|
logic Mask; // Indicates if it is ZBS instruction
|
|
|
|
logic PreShift; // Inidicates if it is sh1add, sh2add, sh3add instruction
|
2023-03-05 22:06:24 +00:00
|
|
|
logic [1:0] PreShiftAmt; // Amount to Pre-Shift A
|
2023-02-28 19:41:40 +00:00
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2023-03-02 23:28:43 +00:00
|
|
|
// Extract control signals from ALUControl.
|
|
|
|
assign {W64, SubArith, ALUOp} = ALUControl;
|
|
|
|
|
2023-03-05 06:44:03 +00:00
|
|
|
// Extract control signals from bitmanip ALUControl.
|
|
|
|
assign {Rotate, Mask, PreShift} = BALUControl;
|
|
|
|
|
2023-03-02 23:28:43 +00:00
|
|
|
// Pack control signals into shifter select
|
|
|
|
assign shASelect = {W64,SubArith};
|
2023-02-17 16:21:55 +00:00
|
|
|
|
2023-03-05 22:06:24 +00:00
|
|
|
assign PreShiftAmt = Funct3[2:1] & {2{PreShift}};
|
|
|
|
|
2023-02-17 17:51:49 +00:00
|
|
|
if (`ZBS_SUPPORTED) begin: zbsdec
|
|
|
|
decoder #($clog2(WIDTH)) maskgen (B[$clog2(WIDTH)-1:0], MaskB);
|
2023-03-05 06:44:03 +00:00
|
|
|
assign CondMaskB = (Mask) ? MaskB : B;
|
2023-02-17 17:51:49 +00:00
|
|
|
end else assign CondMaskB = B;
|
2023-02-17 16:21:55 +00:00
|
|
|
|
2023-02-25 01:09:56 +00:00
|
|
|
// Sign/Zero extend mux
|
|
|
|
if (WIDTH == 64) begin // rv64 must handle word s/z extensions
|
|
|
|
always_comb
|
2023-02-28 19:41:40 +00:00
|
|
|
case (shASelect)
|
2023-03-05 22:06:24 +00:00
|
|
|
2'b00: shA = {{1'b0}, A}; // zero-extend double-word (srl)
|
|
|
|
2'b01: shA = {A[63], A}; // sign-extend double-word (sra)
|
|
|
|
2'b10: shA = {{33'b0}, A[31:0]}; // zero-extend word (add.uw, shadd.uw, srlw)
|
|
|
|
2'b11: shA = {{33{A[31]}}, A[31:0]}; //sign extend-word (sraw)
|
2023-02-25 01:09:56 +00:00
|
|
|
endcase
|
|
|
|
end else assign shA = (SubArith) ? {A[31], A} : {{1'b0},A}; // rv32 does need to handle s/z extensions
|
|
|
|
|
|
|
|
// shifter rotate source select mux
|
|
|
|
if (`ZBB_SUPPORTED) begin
|
|
|
|
if (WIDTH == 64) assign rotA = (W64) ? {A[31:0], A[31:0]} : A;
|
2023-02-25 01:14:12 +00:00
|
|
|
else assign rotA = A;
|
2023-02-25 01:09:56 +00:00
|
|
|
end else assign rotA = A;
|
2023-02-24 20:09:34 +00:00
|
|
|
|
2023-02-18 04:15:12 +00:00
|
|
|
if (`ZBA_SUPPORTED) begin: zbamuxes
|
2023-03-05 07:07:06 +00:00
|
|
|
// Pre-Shift
|
2023-03-05 22:06:24 +00:00
|
|
|
assign CondShiftA = shA[WIDTH-1:0] << (PreShiftAmt);
|
2023-02-18 04:15:12 +00:00
|
|
|
end else assign CondShiftA = A;
|
|
|
|
|
2023-01-17 14:02:26 +00:00
|
|
|
// Addition
|
2023-03-05 22:06:24 +00:00
|
|
|
assign CondMaskInvB = SubArith ? ~CondMaskB : CondMaskB;
|
|
|
|
assign CondInvB = SubArith ? ~B : B;
|
2023-02-18 04:15:12 +00:00
|
|
|
assign {Carry, Sum} = CondShiftA + CondInvB + {{(WIDTH-1){1'b0}}, SubArith};
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2023-03-02 19:45:32 +00:00
|
|
|
// Shifts (configurable for rotation)
|
|
|
|
shifter sh(.shA(shA), .rotA(rotA), .Amt(B[`LOG_XLEN-1:0]), .Right(Funct3[2]), .W64(W64), .Y(Shift), .Rotate(Rotate));
|
2021-12-18 18:25:40 +00:00
|
|
|
|
2023-01-17 14:02:26 +00:00
|
|
|
// Condition code flags are based on subtraction output Sum = A-B.
|
2021-12-19 05:26:00 +00:00
|
|
|
// Overflow occurs when the numbers being subtracted have the opposite sign
|
2023-01-17 14:02:26 +00:00
|
|
|
// and the result has the opposite sign of A.
|
|
|
|
// LT is simplified from Overflow = Asign & Bsign & Asign & Neg; LT = Neg ^ Overflow
|
2021-12-14 19:15:47 +00:00
|
|
|
assign Neg = Sum[WIDTH-1];
|
2022-04-17 16:49:51 +00:00
|
|
|
assign Asign = A[WIDTH-1];
|
|
|
|
assign Bsign = B[WIDTH-1];
|
2023-01-17 14:02:26 +00:00
|
|
|
assign LT = Asign & ~Bsign | Asign & Neg | ~Bsign & Neg;
|
2021-12-14 19:15:47 +00:00
|
|
|
assign LTU = ~Carry;
|
2021-12-08 20:33:53 +00:00
|
|
|
|
2021-12-14 19:15:47 +00:00
|
|
|
// SLT
|
|
|
|
assign SLT = {{(WIDTH-1){1'b0}}, LT};
|
|
|
|
assign SLTU = {{(WIDTH-1){1'b0}}, LTU};
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-12-14 19:15:47 +00:00
|
|
|
// Select appropriate ALU Result
|
2023-02-21 19:52:05 +00:00
|
|
|
if (`ZBS_SUPPORTED | `ZBB_SUPPORTED) begin
|
2023-02-17 17:51:49 +00:00
|
|
|
always_comb
|
|
|
|
if (~ALUOp) FullResult = Sum; // Always add for ALUOp = 0 (address generation)
|
|
|
|
else casez (ALUSelect) // Otherwise check Funct3 NOTE: change signal name to ALUSelect
|
|
|
|
3'b000: FullResult = Sum; // add or sub
|
|
|
|
3'b001: FullResult = Shift; // sll, sra, or srl
|
|
|
|
3'b010: FullResult = SLT; // slt
|
|
|
|
3'b011: FullResult = SLTU; // sltu
|
2023-03-05 22:06:24 +00:00
|
|
|
3'b100: FullResult = A ^ CondMaskInvB; // xor, xnor, binv
|
|
|
|
3'b110: FullResult = A | CondMaskInvB; // or, orn, bset
|
|
|
|
3'b111: FullResult = A & CondMaskInvB; // and, bclr
|
2023-02-17 19:02:07 +00:00
|
|
|
3'b101: FullResult = {{(WIDTH-1){1'b0}},{|(A & CondMaskB)}};// bext
|
2023-02-17 17:51:49 +00:00
|
|
|
endcase
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
always_comb
|
|
|
|
if (~ALUOp) FullResult = Sum; // Always add for ALUOp = 0 (address generation)
|
|
|
|
else casez (ALUSelect) // Otherwise check Funct3 NOTE: change signal name to ALUSelect
|
|
|
|
3'b000: FullResult = Sum; // add or sub
|
|
|
|
3'b?01: FullResult = Shift; // sll, sra, or srl
|
|
|
|
3'b010: FullResult = SLT; // slt
|
|
|
|
3'b011: FullResult = SLTU; // sltu
|
|
|
|
3'b100: FullResult = A ^ B; // xor
|
|
|
|
3'b110: FullResult = A | B; // or
|
|
|
|
3'b111: FullResult = A & B; // and
|
|
|
|
endcase
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-12-18 17:27:25 +00:00
|
|
|
|
2023-01-17 14:02:26 +00:00
|
|
|
// Support RV64I W-type addw/subw/addiw/shifts that discard upper 32 bits and sign-extend 32-bit result to 64 bits
|
2023-03-06 14:19:01 +00:00
|
|
|
if (WIDTH == 64) assign ALUResult = W64 ? {{32{FullResult[31]}}, FullResult[31:0]} : FullResult;
|
|
|
|
else assign ALUResult = FullResult;
|
2023-02-15 17:13:10 +00:00
|
|
|
|
2023-03-05 22:06:24 +00:00
|
|
|
if (`ZBC_SUPPORTED | `ZBB_SUPPORTED) begin: bitreverse
|
|
|
|
bitreverse #(WIDTH) brA(.a(A), .b(RevA));
|
|
|
|
end
|
2023-02-16 01:42:32 +00:00
|
|
|
//NOTE: This looks good and can be merged.
|
2023-02-16 01:39:37 +00:00
|
|
|
if (`ZBC_SUPPORTED) begin: zbc
|
2023-03-05 22:06:24 +00:00
|
|
|
zbc #(WIDTH) ZBC(.A(A), .RevA(RevA), .B(B), .Funct3(Funct3), .ZBCResult(ZBCResult));
|
2023-02-16 01:39:37 +00:00
|
|
|
end else assign ZBCResult = 0;
|
2023-02-19 03:44:14 +00:00
|
|
|
|
|
|
|
if (`ZBB_SUPPORTED) begin: zbb
|
2023-03-06 14:19:01 +00:00
|
|
|
zbb #(WIDTH) ZBB(.A(A), .RevA(RevA), .B(B), .ALUResult(ALUResult), .W64(W64), .lt(CompFlags[0]), .ZBBSelect(ZBBSelect), .ZBBResult(ZBBResult));
|
2023-02-19 03:44:14 +00:00
|
|
|
end else assign ZBBResult = 0;
|
2023-02-16 01:39:37 +00:00
|
|
|
|
2023-02-18 04:15:12 +00:00
|
|
|
// Final Result B instruction select mux
|
2023-02-21 19:52:05 +00:00
|
|
|
if (`ZBC_SUPPORTED | `ZBS_SUPPORTED | `ZBA_SUPPORTED | `ZBB_SUPPORTED) begin : zbdecoder
|
2023-02-15 17:35:07 +00:00
|
|
|
always_comb
|
2023-02-17 17:51:49 +00:00
|
|
|
case (BSelect)
|
2023-03-05 07:19:31 +00:00
|
|
|
// 00: ALU, 01: ZBA/ZBS, 10: ZBB, 11: ZBC
|
2023-03-06 14:19:01 +00:00
|
|
|
2'b00: Result = ALUResult;
|
|
|
|
2'b01: Result = FullResult; // NOTE: We don't use ALUResult because ZBA/ZBS instructions don't sign extend the MSB of the right-hand word.
|
|
|
|
2'b10: Result = ZBBResult;
|
|
|
|
2'b11: Result = ZBCResult;
|
2023-02-15 17:35:07 +00:00
|
|
|
endcase
|
2023-03-06 14:19:01 +00:00
|
|
|
end else assign Result = ALUResult;
|
2023-02-12 05:22:33 +00:00
|
|
|
endmodule
|