cvw/pipelined/src/ieu/comparator.sv

50 lines
2.0 KiB
Systemverilog
Raw Normal View History

2021-12-08 20:33:53 +00:00
///////////////////////////////////////////
// comparator.sv
//
2023-01-17 14:02:26 +00:00
// Written: David_Harris@hmc.edu, Sarah.Harris@unlv.edu
// Created: 8 December 2021
2021-12-08 20:33:53 +00:00
// Modified:
//
// Purpose: Branch comparison
//
2023-01-12 12:35:44 +00:00
// Documentation: RISC-V System on Chip Design Chapter 4 (Figure 4.7)
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-12-08 20:33:53 +00:00
//
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-12-08 20:33:53 +00:00
//
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-12-08 20:33:53 +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-12-08 20:33:53 +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-12-08 20:33:53 +00:00
`include "wally-config.vh"
2022-08-22 08:28:28 +00:00
// This comparator is best
module comparator_dc_flip #(parameter WIDTH=64) (
2023-01-17 14:02:26 +00:00
input logic [WIDTH-1:0] a, b, // Operands
input logic sgnd, // Signed operands
output logic [1:0] flags); // Output flags: {eq, lt}
2022-08-22 08:28:28 +00:00
2023-01-17 14:02:26 +00:00
logic eq, lt; // Flags: equal (eq), less than (lt)
logic [WIDTH-1:0] af, bf; // Operands with msb flipped (inverted) when signed
2022-08-22 08:28:28 +00:00
// For signed numbers, flip most significant bit
assign af = {a[WIDTH-1] ^ sgnd, a[WIDTH-2:0]};
assign bf = {b[WIDTH-1] ^ sgnd, b[WIDTH-2:0]};
2023-01-17 14:02:26 +00:00
// Behavioral description gives best results
assign eq = (a == b); // eq = 1 when operands are equal, 0 otherwise
assign lt = (af < bf); // lt = 1 when a less than b (taking signed operands into account)
2022-08-22 08:28:28 +00:00
assign flags = {eq, lt};
endmodule