2021-01-15 04:37:51 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
// alu.sv
|
|
|
|
//
|
2023-01-11 23:20:41 +00:00
|
|
|
// Written: David_Harris@hmc.edu, Sarah.Harris@unlv.edu
|
|
|
|
// Created: 9 January 2021
|
2021-01-15 04:37:51 +00:00
|
|
|
// Modified:
|
|
|
|
//
|
|
|
|
// 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) (
|
2021-12-14 19:15:47 +00:00
|
|
|
input logic [WIDTH-1:0] A, B,
|
2021-12-08 20:33:53 +00:00
|
|
|
input logic [2:0] ALUControl,
|
|
|
|
input logic [2:0] Funct3,
|
2021-12-14 19:15:47 +00:00
|
|
|
output logic [WIDTH-1:0] Result,
|
|
|
|
output logic [WIDTH-1:0] Sum);
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-12-18 17:27:25 +00:00
|
|
|
logic [WIDTH-1:0] CondInvB, Shift, SLT, SLTU, FullResult;
|
2021-12-14 19:15:47 +00:00
|
|
|
logic Carry, Neg;
|
|
|
|
logic LT, LTU;
|
2021-12-08 20:33:53 +00:00
|
|
|
logic W64, SubArith, ALUOp;
|
2022-04-17 16:49:51 +00:00
|
|
|
logic Asign, Bsign;
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-12-14 19:15:47 +00:00
|
|
|
// Extract control signals
|
|
|
|
// W64 indicates RV64 W-suffix instructions acting on lower 32-bit word
|
2022-12-24 15:18:35 +00:00
|
|
|
// SubArith indicates subtraction or arithmetic right shift
|
2021-12-14 19:15:47 +00:00
|
|
|
// ALUOp = 0 for address generation addition or 1 for regular ALU
|
2021-12-08 20:33:53 +00:00
|
|
|
assign {W64, SubArith, ALUOp} = ALUControl;
|
2021-12-14 19:15:47 +00:00
|
|
|
|
2021-01-15 04:37:51 +00:00
|
|
|
// addition
|
2021-12-14 19:15:47 +00:00
|
|
|
assign CondInvB = SubArith ? ~B : B;
|
|
|
|
assign {Carry, Sum} = A + CondInvB + {{(WIDTH-1){1'b0}}, SubArith};
|
2021-01-15 04:37:51 +00:00
|
|
|
|
2021-12-14 19:15:47 +00:00
|
|
|
// Shifts
|
2021-12-18 18:25:40 +00:00
|
|
|
shifter sh(.A, .Amt(B[`LOG_XLEN-1:0]), .Right(Funct3[2]), .Arith(SubArith), .W64, .Y(Shift));
|
|
|
|
|
2022-04-17 16:49:51 +00:00
|
|
|
// condition code flags based on subtract output Sum = A-B
|
2021-12-19 05:26:00 +00:00
|
|
|
// Overflow occurs when the numbers being subtracted have the opposite sign
|
|
|
|
// and the result has the opposite sign of A
|
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];
|
|
|
|
assign LT = Asign & ~Bsign | Asign & Neg | ~Bsign & Neg; // simplified from Overflow = Asign & Bsign & Asign & Neg; LT = Neg ^ Overflow
|
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
|
2021-01-15 04:37:51 +00:00
|
|
|
always_comb
|
2022-12-24 15:18:35 +00:00
|
|
|
if (~ALUOp) FullResult = Sum; // Always add for ALUOp = 0
|
|
|
|
else casez (Funct3) // Otherwise check Funct3
|
2021-12-18 17:27:25 +00:00
|
|
|
3'b000: FullResult = Sum; // add or sub
|
2021-12-18 18:08:52 +00:00
|
|
|
3'b?01: FullResult = Shift; // sll, sra, or srl
|
2021-12-18 17:27:25 +00:00
|
|
|
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
|
2021-12-14 19:15:47 +00:00
|
|
|
endcase
|
2021-12-18 17:27:25 +00:00
|
|
|
|
|
|
|
// support W-type RV64I ADDW/SUBW/ADDIW/Shifts that sign-extend 32-bit result to 64 bits
|
2022-01-05 14:35:25 +00:00
|
|
|
if (WIDTH==64) assign Result = W64 ? {{32{FullResult[31]}}, FullResult[31:0]} : FullResult;
|
|
|
|
else assign Result = FullResult;
|
2021-01-15 04:37:51 +00:00
|
|
|
endmodule
|
|
|
|
|