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

And/Or/Xor with N Inputs

Verilog Gate Level Simulation

These gates can have more than 2 inputs

Open in IDE
Related tutorial:
design.v
module gates (	input a, b, c, d,
				output x, y, z);

  and (x, a, b, c, d); 	// x is the output, a, b, c, d are inputs
  or  (y, a, b, c, d);	// y is the output, a, b, c, d are inputs
  nor (z, a, b, c, d); 	// z is the output, a, b, c, d are inputs
endmodule
testbench.v
module tb;
	reg a, b, c, d;
	wire x, y, z;
	integer i;

  gates u0 ( .a(a), .b(b), .c(c), .d(d), .x(x), .y(y), .z(z));

	initial begin
      {a, b, c, d} = 0;

      $monitor ("[T=%0t a=%0b b=%0b c=%0b d=%0b x=%0b y=%0b x=%0b", $time, a, b, c, d, x, y, z);

		for (i = 0; i < 10; i = i+1) begin
			#1 	a <= $random;
				b <= $random;
          		c <= $random;
          		d <= $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.