CBL_TwosCompToSignMag

CBL_TwosCompToSignMag : A combinational logic converting a two’s complement to a sign-magnitude representation

This model describes a combinational logic converting a two’s complement to a sign-magnitude. For an N-bit binary number in[N-1:0], the sign-magnitude representation uses the MSB in[N-1] as the sign bit and the rest in[N-2:0] as the magnitude. Note that -2^(N-1) in two’s complement representation will be converted to a zero in sign-magnitude representation.

module CBL_TwosCompToSignMag #(
    parameter width = 4             // bit width
)(
    input [width-1:0] in,           // input bits
    output [width-1:0] out          // output bits
);

assign out = (in[width-1]) ? ~{1'b0, in[width-2:0]} + 1 : in;

endmodule

Input/Output Terminals

Name I/O Type Description
in [width-1:0] input wire input bits
out [width-1:0] output wire output bits

Parameters

Name Type Default Description
width integer 4 bit width

List of Testbenches

tb_check : A testbench for checking the basic functionality of a combinational logic

tb_check : A testbench for checking the basic functionality of a combinational logic

This testbench checks the basic functionality of a combinational logic by enumerating all the possible input values and listing its corresponding outputs.

module tb_check ();
parameter in_width = 4;
parameter out_width = in_width;

reg [in_width-1:0] in;
wire [out_width-1:0] out;

CBL_TwosCompToSignMag #(.width(in_width)) DUT (.in, .out);

initial begin
    in = 0;

    repeat ((1 << in_width) - 1) begin
        #(1ns);
        in = in + 1;
    end
end

always @(out) begin
    $display("IN: %b --> OUT: %b", in, out);
end

endmodule