12. Vector Bitwise Operators

Design a combinational module bitwise_ops_demo that demonstrates the use of vector bitwise operators.

Requirements

  • Module name: bitwise_ops_demo
  • Inputs:
    • A[3:0] (4-bit)
    • B[3:0] (4-bit)
  • Outputs:
    • AND_OUT[3:0] = bitwise AND of A and B
    • OR_OUT[3:0] = bitwise OR of A and B
    • XOR_OUT[3:0] = bitwise XOR of A and B
    • XNOR_OUT[3:0] = bitwise XNOR of A and B

Expected Behavior (example)

If A = 4'b1010 and B = 4'b1100:

OperationResult
AND1000
OR1110
XOR0110
XNOR1001

Testbench Code

`timescale 1ns/1ps

module tb_bitwise_ops_demo;
    // Inputs
    reg  [3:0] A, B;

    // DUT outputs
    wire [3:0] AND_OUT, OR_OUT, XOR_OUT, XNOR_OUT;

    // Expected outputs
    reg  [3:0] expected_and, expected_or, expected_xor, expected_xnor;

    // Consolidated mismatch for waveform/debug
    wire mismatch_and, mismatch_or, mismatch_xor, mismatch_xnor;
    assign mismatch_and  = (AND_OUT  !== expected_and);
    assign mismatch_or   = (OR_OUT   !== expected_or);
    assign mismatch_xor  = (XOR_OUT  !== expected_xor);
    assign mismatch_xnor = (XNOR_OUT !== expected_xnor);
    wire mismatch = mismatch_and | mismatch_or | mismatch_xor | mismatch_xnor;

    // Simple counters
    integer TOTAL_TEST_CASES = 0;
    integer TOTAL_PASSED_TEST_CASES = 0;
    integer TOTAL_FAILED_TEST_CASES = 0;

    // DUT
    bitwise_ops_demo dut (
        .A(A), .B(B),
        .AND_OUT(AND_OUT), .OR_OUT(OR_OUT),
        .XOR_OUT(XOR_OUT), .XNOR_OUT(XNOR_OUT)
    );

    // VCD: dump only inputs, outputs, expected, and mismatch signals
    initial begin
        $dumpfile("tb_bitwise_ops_demo.vcd");
        $dumpvars(0,
            tb_bitwise_ops_demo.A, tb_bitwise_ops_demo.B,                        // inputs
            tb_bitwise_ops_demo.AND_OUT, tb_bitwise_ops_demo.OR_OUT,             // outputs
            tb_bitwise_ops_demo.XOR_OUT, tb_bitwise_ops_demo.XNOR_OUT,
            tb_bitwise_ops_demo.expected_and, tb_bitwise_ops_demo.expected_or,   // expected
            tb_bitwise_ops_demo.expected_xor, tb_bitwise_ops_demo.expected_xnor,
            tb_bitwise_ops_demo.mismatch                                         // consolidated flag
        );
    end

    // One test transaction
    task run_one;
        input [3:0] tA, tB;
        begin
            A = tA; B = tB;
            expected_and  = tA & tB;
            expected_or   = tA | tB;
            expected_xor  = tA ^ tB;
            expected_xnor = ~(tA ^ tB);
            #1;
            TOTAL_TEST_CASES = TOTAL_TEST_CASES + 1;

            if (!mismatch) begin
                TOTAL_PASSED_TEST_CASES = TOTAL_PASSED_TEST_CASES + 1;
            end else begin
                TOTAL_FAILED_TEST_CASES = TOTAL_FAILED_TEST_CASES + 1;
                $display("FAILED: A=%b B=%b | AND=%b OR=%b XOR=%b XNOR=%b | EXP AND=%b OR=%b XOR=%b XNOR=%b",
                         A,B,AND_OUT,OR_OUT,XOR_OUT,XNOR_OUT,
                         expected_and,expected_or,expected_xor,expected_xnor);
            end
        end
    endtask

    integer i;
    initial begin
        // Directed cases
        run_one(4'b1010, 4'b1100); // example above
        run_one(4'hF, 4'h0);
        run_one(4'hA, 4'h5);
        run_one(4'h3, 4'hC);

        // Randomized cases (Verilog-2005 friendly; truncation gives 4-bit)
        for (i=0; i<20; i=i+1) begin
            run_one($random, $random);
        end

        // Summary
        $display("======================================");
        $display("TOTAL_TEST_CASES=%0d",        TOTAL_TEST_CASES);
        $display("TOTAL_PASSED_TEST_CASES=%0d", TOTAL_PASSED_TEST_CASES);
        $display("TOTAL_FAILED_TEST_CASES=%0d", TOTAL_FAILED_TEST_CASES);
        $display("ALL_TEST_CASES_PASSED=%s",
                 (TOTAL_FAILED_TEST_CASES==0) ? "true" : "false");
        $display("======================================");
        $finish;
    end
endmodule