Wednesday, August 26, 2020

Verilog Code for Comparator

 Design Code:

module compare1(

    input a,b,

    output e,g,l

    );

  assign e=(a & b)|((~a) & (~b));

  assign g= (a & (~b));

  assign l= ((~a) & b);

    endmodule

Testbench Code: 

module tb;

  reg a1,b1;

  wire e1,g1,l1;

    compare1 tess(a1,b1,e1,g1,l1);

    initial begin

  $dumpfile("dump.vcd");

  $dumpvars(1);

      $display("input & outputs");

    a1=1'b0;

    b1=1'b0;

    #1

    $display("a=%b, b=%b : e=%b, g=%b, l=%b,",a1,b1,e1,g1,l1);

   a1=1'b0;

    b1=1'b1;

    #1

    $display("a=%b, b=%b : e=%b, g=%b, l=%b,",a1,b1,e1,g1,l1);

    a1=1'b1;

    b1=1'b0;

    #1

    $display("a=%b, b=%b : e=%b, g=%b, l=%b,",a1,b1,e1,g1,l1);

   a1=1'b1;

    b1=1'b1;

    #1

    $display("a=%b, b=%b : e=%b, g=%b, l=%b,",a1,b1,e1,g1,l1);

    end

endmodule

Note: You can test this code  in virtual mode also at https://www.edaplayground.com/  free of cost by login with the college mail/ office mail.

There you need to paste 1. Source code in design side (R.H.S)

                                       2. Test Bench code in test side (L.H.S)

Also, simple settings to observe the outputs in edaplayground.com are

--> Testbench + Design should be selected with Systemverilog/ verilog

--> Tools & Simulators can be Icarus Verilog 0.9.7 or any one there, which supports.

--> In Tools & Simulators, Open EPWave after run is to be marked to observe wave forms.

You can observe outputs in Log window below

Outputs For this example:

input & outputs
a=0, b=0 : e=1, g=0, l=0,
a=0, b=1 : e=0, g=0, l=1,
a=1, b=0 : e=0, g=1, l=0,
a=1, b=1 : e=1, g=0, l=0,

Waveforms :



No comments:

Post a Comment

Verilog Code for Full Adder

    Design Code: module fa1(a,b,c,s,cy);     input a,b,c;     output s,cy;   assign s = a ^ b ^ c;     assign cy = (a & b) | (b & c)...