- 模块中函数的调用
module function(str1,str2);
input string str1,str2;
wire a;
assign a=strstr (str1,str2);
function strstr;//比较两字符串,看str1是否包含str2
input string str1;
input string str2;
integer len1,len2;
integer cnt;
strstr=0;
len1=str1.len();
len2=str2.len();
if(len2>len1)strstr=0;
else begin
for(cnt=0;cnt<=(len1-len2);cnt=cnt+1)
begin
if(str1.substr(cnt,cnt+len2-1)==str2)begin
strstr=1;
break;
end
end
end
endfunction
endmodule : tb_function
module tb_function ();
string str1;
string str2;
tb_function inst_tb_function (.str1(str1), .str2(str2));
initial begin
str1="uhdjnvjvfv";
str2="nvji";
end
endmodule
2.仿真中调用函数
module tb_function();
string str1,str2;
logic a;
initial begin
str1="uhdjnvjvfv";
str2="nvj";
a=strstr (str1,str2);
end
function strstr;
input string str1;
input string str2;
integer len1,len2;
integer cnt;
strstr=0;
len1=str1.len();
len2=str2.len();
if(len2>len1)strstr=0;
else begin
for(cnt=0;cnt<=(len1-len2);cnt=cnt+1)
begin
if(str1.substr(cnt,cnt+len2-1)==str2)begin
strstr=1;
break;
end
end
end
endfunction
endmodule : tb_function
上例中 a=strstr (str1,str2),逗号前代表的是function中的第一个输入,逗号后代表第二个输入,若有多个输入依次添加,顺序不能混淆。str1代表的是 “ str1="uhdjnvjvfv" ”,str2代表的是“ str2="nvj" ”。