当前位置:网站首页>【深入浅出玩转FPGA学习11----Testbench书写技巧1】
【深入浅出玩转FPGA学习11----Testbench书写技巧1】
2022-07-26 01:42:00 【周猿猿】
封装有用的子程序
在C语言中,有经验的软件工程师一定会为经常使用的一段代码写一个子程序,然后通过不同的参数对其进行调用。这样做可以达到代码复用的效果,减少了不必要的重复劳动,也使得代码相对简捷一些。在Testbench中也可以使用task进行代码的封装,它能够和C语言的子函数一样被灵活调用。
下面的例子给出一段很实用的封装子程序,也许在每个工程的测试脚本中都可以排上用场。
// 封装一些做测试时有用的报告显示
//包括任务error, warning, fatal, terminate
module print_task();
//显示warning报告,同时包含显示当前时间和警告内容(由用户输入)
task warning;
input [80*8:1] msg;
begin
$write("WARNING at %t: %s", $time,msg);
end
endtask
//显示error 报告,同时包含显示当前时间和错误内容(由用户输入)
task error;
input [80*8:1] msg;
begin
$write("-ERROR- at %t: %s", $time, msg);
end
endtask
//显示fatal报告,同时包含显示当前时间和致命内容(由用户输入)
task fatal;
input [80*8:1] msg;
begin
$write(" *FATAL* at %t: %s", $time, msg);
terminate;
end
endtask
//显示warning 报告,同时包含显示当前时间和结束信息(该任务生成)
task terminate;
begin
$write("Simulation completed\n");
$ finish;
end
endtask
endmodule
在使用封装子程序时,如下代码所示:
//使用print_task.v,后面就可以调用其封装好的task了
print_task print();
...
initial begin
if (...) print.error("Unexpected response\n"); //调用error任务
...
print.terminate; //调用terminate任务
end
...
endmodule
关于变量的定义
在编写Testbench时,关于变量的定义常犯的错误就是将一个定义好的全局变量应用到了两个不同的always块中(如EX1C),那么由于这两个always块独立并行的工作机制,很可能会导致意想不到的后果。
EX1C:
interger i;
always begin
for(i=0; i<32; i=i+1) begin
end
end
always begin
for(i=0; i<32;i=i+1) begin
...
end
end
实际上,在Verilog中(编写Testbench时),如果在begin…end 之间定义了always的块名,那么可以如EX1C一样申明变量。这样两个always块里的变量i就互不相关,也就不会产生不可预料的结果了。
EX2C:
always begin
integer i;
for(i=0; i<32; i=i+1) begin
...
end
end
always
begin: block_2
integer i;
for(i=0; i<32;i=i+1) begin
...
end
end
除此以外,在Verilog中的function和task也支持类似上面的局部变量定义。
HDL的并行性
为什么C不能取代Verilog和VHDL作为硬件描述语言?因为C缺少了硬件描述最基本的3个思想:连通性(connectivity)、时间性(Time)和并行性(Concurrency)。
连通性是使用一个简单并相互连接的模块来描述设计的能力,原理图设计工具就是连通性完美的支持工具。
时间性是表现设计状态演进的时间变化的能力,这个能力不同于衡量一个代码执行所用的时间。
并行性是描述同时发生相互独立的行为的能力。
边栏推荐
- TV software burning
- AUTOCAD——计算面积的方法
- Is it safe to buy funds in stock accounts? Professional answers
- [ickim 2022] the Fourth International Conference on knowledge and information management
- The e-commerce project is written in the resume. How to answer it during the interview
- 3059. Sculpture (jzoj)
- Introduction to API testing
- MDK compilation process and arm compilation tool chain
- 元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))
- The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
猜你喜欢

聚势|海泰方圆亮相第五届数字中国建设峰会
![[data mining] differences, advantages and disadvantages between generative model and discriminant model](/img/a4/3dba2ba9fa0fe3062f17aea2bfae47.png)
[data mining] differences, advantages and disadvantages between generative model and discriminant model

图像批处理高斯滤波降噪+峰值信噪比计算

销量连连夺冠,五菱的成功秘诀只有低价吗?

Leetcode537. Complex multiplication (yes, solved)

8. Learn Mysql to create data tables

3、 Pinda general permission system__ pd-tools-swagger2

The e-commerce project is written in the resume. How to answer it during the interview

EasyRecovery15下载量高的恢复率高的数据恢复软件

Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes
随机推荐
ABC find 4-cycle (pigeon nest theorem)
The gym closes 8000 stores a year. How does the studio that bucks the trend and makes profits open?
“蔚来杯“2022牛客暑期多校训练营2 G.[Link with Monotonic Subsequence] 分块构造
Is it safe to buy funds on e fund? Professional answers
leetcode/只出现一次的数字
Codisvsrediscluster: which cluster scheme should I choose?
y77.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(八)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
Server available resources query script
EasyRecovery15下载量高的恢复率高的数据恢复软件
Advanced C language (I) dynamic memory allocation
How uxdb works on multiple processors
Qtreewidget dotted line setting
B - Krypton Factor (dfs+ backtracking method)
Understand Linglong platform unified access service from simple to deep Monet
01. MySQL transaction isolation level and concurrent database access
Ideal Path(UVA - 1599)
Shell exercises
AUTOCAD——计算面积的方法
Basic version of Google browser debugging tool (I)