在UVM中使用正则匹配
通常我们需要在验证环境中对路径path做正则或者对寄存器名做正则匹配来做针对的处理,如打印,检查或者屏蔽等操作,因此我们需要能在UVM环境中使用正则表达式进行字符串的搜索匹配。
恰好,UVM给我们提供了两个接口,分别是如下两个函数:
function int uvm_re_match(string re, string str);
function string uvm_glob_to_re(string glob);
其中uvm_glob_to_re函数输入一个你想匹配的字符串,然后返回给你正则表达式,这非常方便,连正则都不用自己写,你只需要调这个函数直接转换即可,有点类似python中的re.compile('str')。
而uvm_re_match函数则是输入你想匹配的正则表达式字符串re,以及你想匹配的目标字符串str,如果匹配到了,则返回0,否则返回1。
看个例子:
begin
int temp_int = 'd123;
string temp_str = "default_str";
string glob_str = "abc";
string re_str = "default_str";
string match_str = "top.abcdefg123";
// test 1
re_str = uvm_glob_to_re(glob_str);
temp_int = uvm_re_match(re_str,match_str);
`uvm_info("DEBUG",
$sformatf("temp_int: %0d,
glob_str: %s,
re_str: %s,
match_str: %s",
tmp_int,glob_str,re_str,match_str),
UVM_NONE)
// test 2
glob_str = "*abc*";
re_str = uvm_glob_to_re(glob_str);
temp_int = uvm_re_match(re_str,match_str);
`uvm_info("DEBUG",
$sformatf("temp_int: %0d,
glob_str: %s,
re_str: %s,
match_str: %s",
tmp_int,glob_str,re_str,match_str),
UVM_NONE)
// test 3
glob_str = "1234567";
re_str = uvm_glob_to_re(glob_str);
temp_int = uvm_re_match(re_str,match_str);
`uvm_info("DEBUG",
$sformatf("temp_int: %0d,
glob_str: %s,
re_str: %s,
match_str: %s",
tmp_int,glob_str,re_str,match_str),
UVM_NONE)
// test 4
glob_str = "*123";
re_str = uvm_glob_to_re(glob_str);
temp_int = uvm_re_match(re_str,match_str);
`uvm_info("DEBUG",
$sformatf("temp_int: %0d,
glob_str: %s,
re_str: %s,
match_str: %s",
tmp_int,glob_str,re_str,match_str),
UVM_NONE)
end
这里我们用了test 1~4来做了个小实验,仿真结果为:
UVM_INFO ... [DEBUG] temp_int: 1, glob_str: abc, re_str: /^abc$/, match_str: top.abcdefg123
UVM_INFO ... [DEBUG] temp_int: 0, glob_str: *abc*, re_str: /^.*abc.*$/, match_str: top.abcdefg123
UVM_INFO ... [DEBUG] temp_int: 1, glob_str: 1234567, re_str: /^1234567$/, match_str: top.abcdefg123
UVM_INFO ... [DEBUG] temp_int: 0, glob_str: *123, re_str: /^.*123$/, match_str: top.abcdefg123
需要注意的是匹配字符串abc以及abc生成正则表达式的区别,匹配的结果也是不一样的。
综上,使用上面这两个接口可以很轻松的实现在UVM中使用正则匹配搜索。
浙公网安备 33010602011771号