User guide

22-68
SystemVerilog Design Constructs
Functions In Interfaces
If we define a user-defined task or function in the interface, we can
use the import keyword in the modport to make it accessible to
the module instances connected by the interface and that use the
modport. For example:
interface intf;
logic [7:0] send,receive;
function automatic logic parity(logic [7:0] data);
return (^data);
endfunction
modport sendmode (output send, input receive,
import function parity());
modport receivemode (input send, output receive,
import function parity());
endinterface
Using the keyword import and specifying whether it is a task or
function in the modport, enables modules connected by the interface
to use the function named parity. Using a function in an interface is
called using a method.
module sendmod (intf.sendmode intfa1,
input logic [7:0] in,
output logic [7:0] out,
output logic out_parity);
always @(intfa1.receive)
begin
out = intfa1.receive;
out_parity = intfa1.parity(intfa1.receive);
intfa1.send = in;
end
endmodule