Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Gate Level Examples: Full Adder

Verilog Examples

Runnable example from the Verilog Gate Level Examples tutorial (Full Adder)

Open in IDE
design.v
module fa ( input a, b, cin,
            output sum, cout);

  wire s1, net1, net2;

  xor (s1, a, b);         // s1 = a ^ b, shared by sum and cout
  and (net1, a, b);       // Carry generated by a and b

  xor (sum, s1, cin);     // sum = a ^ b ^ cin
  and (net2, s1, cin);    // Carry propagated from cin

  or (cout, net1, net2);

endmodule
testbench.v
module tb;
  reg a, b, cin;
  wire sum, cout;
  integer i;

  fa u0 ( .a(a), .b(b), .cin(cin),
          .sum(sum), .cout(cout));

  initial begin
    {a, b, cin} <= 0;

    $monitor ("T=%0t a=%0b b=%0b cin=%0b cout=%0b sum=%0b",
              $time, a, b, cin, cout, sum);

    for (i = 0; i < 10; i = i+1) begin
      #1  a <= $random;
          b <= $random;
          cin <= $random;
    end
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.