SystemVerilog二维关联数组使用案例
摘要:二维的关联数组是一种非常重要的数据结构,在验证环境中用于保存一些特定的对象,用于检索和分析。本文会介绍一种二维关联数组的使用案例。关于SystemVerilog关联数组的基础知识,请参考以下博文:
一、二维关联数组的声明
在SystemVerilog中,二维关联数组的声明有两种方法:
1.1 方案一
- 先定义一个子hash
- 然后将子hash放入大的hash中
`timescale 1ns/1ps
module hdl_top();
typedef time cps_base_hash[string];
cps_base_hash task1_cps, task2_cps, task3_cpa;
cps_base_hash soc_cps_hash[string];
initial begin
task1_cps["begin_time"] = $time();
task1_cps["end_time"] = $time() + 'h1010;
task2_cps["begin_time"] = $time() + 'h1010;
task2_cps["end_time"] = $time() + 'h2020;
task3_cps["begin_time"] = $time() + 'h2020;
task3_cps["end_time"] = $time() + 'h3030;
soc_cps_hash["task1_name"] = task1_cps;
soc_cps_hash["task2_name"] = task2_cps;
soc_cps_hash["task3_name"] = task3_cps;
foreach(soc_cps_hash[key]) begin
$display("task_name = %s, begin_time = %0d, end_time = %0d", key, soc_cps_hash[key]["begin_time"], soc_cps_hash[key]["end_time"]);
end
foreach(soc_cps_hash[x, y]) begin
$display("First key is %s, second key is %s, value is %p", x, y, soc_cps_hash[x][y]);
end
end
endmodule
1.2 方案二
- 直接将子hash放入大hash中。
`timescale 1ns/1ps
module hdl_top();
time soc_cps_hash[string][string];
initial begin
soc_cps_hash["task1_name"]["begin_time"] = $time();
soc_cps_hash["task1_name"]["end_time"] = $time() + 'h1010;
soc_cps_hash["task2_name"]["begin_time"] = $time() + 'h1010;
soc_cps_hash["task2_name"]["end_time"] = $time() + 'h2020;
soc_cps_hash["task3_name"]["begin_time"] = $time() + 'h2020;
soc_cps_hash["task3_name"]["end_time"] = $time() + 'h3030;
foreach(soc_cps_hash[key]) begin
$display("task_name = %s, begin_time = %0d, end_time = %0d", key, soc_cps_hash[key]["begin_time"], soc_cps_hash[key]["end_time"]);
end
foreach(soc_cps_hash[x, y]) begin
$display("First key is %s, second key is %s, value is %p", x, y, soc_cps_hash[x][y]);
end
end
endmodule
二、在仿真日志中记录物理时间
在分析仿真效率的时候,怎么计算每个task执行了多少时间呢?这儿就涉及到计算2个时间差的问题。计算时间间隔,可以用Linux的时间戳(date +%s),将得到的数据写入文件,最后读回来作差即可。
Unix时间戳(英文为Unix epoch, Unix time, POSIXme 或 Unix timestamp)是从1970年1月1日00:00:00(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
function time get_physical_time();
time phy_time;
integer fptr;
$system("date +%s > soc_cps_time.csv");
fptr = $fopen("soc_cps_time.csv", "r");
$fscanf(fptr, "%0d", phy_time);
$fclose(fptr);
$system("rm -rf soc_cps_time.csv");
return phy_time;
endfunction : get_physical_time