Half Adder (Structural Design) Verilog

Half Adder (Structural Design)


  • Structural Design can be defined at gate and transistor level (using built-in-primitives) and at functional level or combination of these three styles.
  • For gate level modelling, predefined logic gates (andnandornorxorxnorbufnot) are used via their instantiation.
    • syntax: <predefined_gate> <instance_name> (list_of_ports);
    • e.g. and G1 (f, a, b, c);
    • syntax: <predefined_gate> <instance_name>[N:0] (list_of_ports);
    • or G[1:0] (f, a, b, c);  It makes two instances of 3-Input OR gate with name G1 and G0.
  • For transistor level modelling, predefined switches (tranrtran, nmospmosrnmosrpmoscmosrcmosrtranif0rtranif1tranif0tranif1) are used via their instantiation.


Design Code (Structural Modelling)
_________________________________________________________________________________

module Half_Adder(output Sum, Cout, input a, b);
  xor G1 (Sum, a, b);  // instantiation of predefined and gate
  and G2 (Cout, a, b); // instantiation of predefined xor gate
endmodule
_________________________________________________________________________________

Test Bench
_________________________________________________________________________________
module HA_test;
  reg a,b;
  wire Sum, Cout;
  Half_Adder HA1 (Sum, Cout, a, b);
  initial
    begin
      $display("a b \t Sum, Carry");
      $monitor("%b %b \t  %b     %b", a,b,Sum,Cout);
      a=1'b0; b=1'b0;
      #5 a=1'b0; b=1'b1;
      #5 a=1'b1; b=1'b0;
      #5 a=1'b1; b=1'b1;
      #5 $finish;
    end
endmodule
_________________________________________________________________________________

Output
______________________________
a b Sum, Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
______________________________


Design Code 2
_________________________________________________________________________________

module Half_Adder(output Sum, Cout, input a, b);
  xor #2 G1 (Sum, a, b);  // with Delay (assume delay of two NAND gate)
  and #3 G2 (Cout, a, b); // assume delay of 3 NAND gate
endmodule
_________________________________________________________________________________

Test Bench 2
_________________________________________________________________________________
module HA_test;
  reg a,b;
  wire Sum, Cout;
  Half_Adder HA1 (Sum, Cout, a, b);
  initial
    begin
      $display("\t\t Time \t a b \t Carry Sum");
      $monitor($time,"\t %b %b \t  %b     %b", a,b,Cout,Sum);
      a=1'b0; b=1'b0;
      #5 a=1'b0; b=1'b1;
      #5 a=1'b1; b=1'b0;
      #5 a=1'b1; b=1'b1;
      #5 $finish;
    end
endmodule
_________________________________________________________________________________


Output
______________________________________________________
                              Time a b Carry Sum
0 0 0 z z
2 0 0 0 z
3 0 0 0 0
5 0 1 0 0
8 0 1 0 1
10 1 0 0 1
15 1 1 0 1
17 1 1 1 1
18 1 1 1 0
_______________________________________________________
default value of  wire (net) is 'z'

  • A delay can be specified with a predefined gates.
    • e.g. and #2 G1 (Sum, a, b);
  • Similarly signal strength can be defined with predefined gates.
    • e.g. and (strong1, weak0) (Sum, a, b);
    • e.g. and (strong1, weak0) #2 (Sum, a, b);
  • Sum is changed after a delay of 2 units.
  • Carry is changed after a delay of 3 units.



Truth Table of Predefined logic gates

  • x is don't care
  • z is high impedance state

and G1 (f, a, b);
b f
0 0 0
0 1 0
1 0 0
1 1 1
0 x,z 0
1,x,z x,z x





or G1 (f, a, b);
b f
0 0 0
0 1 1
1 0 1
1 1 1
1 x,z 1
0,x,z x,z x





xor G1 (f, a, b);
b f
0 0 0
0 1 1
1 0 1
1 1 0
0,1,x,z x,z x


anand G1 (f, a, b);
b f
0 0 1
0 1 1
1 0 1
1 1 0
0 x,z 1
1,x,z x,z x


nor G1 (f, a, b);
b f
0 0 1
0 1 0
1 0 0
1 1 0
1 x,z 0
0,x,z x,z x



xnor G1 (f, a, b);
bf
001
010
100
111
0,1,x,zx,z
x




Comments

Popular posts from this blog

16-to-1 multiplexer (16X1 MUX) Verilog

8 bit Adder Verilog

Carry Lookahead 4-bit Adder Verilog