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

Using Declarations And Directions

SystemVerilog Advanced Features

Functions declared with separate input / output port statements, including one that returns a value while also writing an output argument.

Open in IDE
testbench.sv
module tb;
	
 	initial begin
      int res, s;
      s = sum(5,9);
      $display ("s = %0d", sum(5,9));
      $display ("sum(5,9) = %0d", sum(5,9));
      $display ("mul(3,1) = %0d", mul(3,1,res));
      $display ("res = %0d", res);
    end
  
	// Function has an 8-bit return value and accepts two inputs
  	// and provides the result through its output port and return val
  	function bit [7:0] sum;
      input int x, y;
      output sum;
		sum = x + y;
	endfunction
  
  // Same as above but ports are given inline
  function byte mul (input int x, y, output int res);
    res = x*y + 1;
    return x * y;
  endfunction
endmodule

/* 
Simulation Log:
---------------
ncsim> run
s = 14
sum(5,9) = 14
mul(3,1) = 3
res = 4
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
*/
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.