当前位置:网站首页>【深入浅出玩转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)。
连通性是使用一个简单并相互连接的模块来描述设计的能力,原理图设计工具就是连通性完美的支持工具。
时间性是表现设计状态演进的时间变化的能力,这个能力不同于衡量一个代码执行所用的时间。
并行性是描述同时发生相互独立的行为的能力。
边栏推荐
- Mulda: a multilingual data augmentation framework for low resource cross linguistic ner reading notes
- Network layer 2 and layer 3 forwarding
- Prime Ring Problem
- Study notes: original code, inverse code, complement code
- "Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section
- dataframe 修改某行某列位置的值
- 大咖观点+500强案例,软件团队应该这样提升研发效能
- 言语理解中心理解总结
- Record a failure caused by a custom redis distributed lock
- 两阶段提交和三阶段提交
猜你喜欢

聚势|海泰方圆亮相第五届数字中国建设峰会

言语理解-片段阅读的结构剖析练习

C language enumeration types and unions

记一次自定义 Redis 分布式锁导致的故障

Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
![[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

AUTOCAD——计算面积的方法

《分布式微服务电商》专题(一)-项目简介

C语言中的整型数据类型(你真的了解吗)

Practice sharing of monorepo based on yarn1.x
随机推荐
Pycharm automatically adds header comments when creating py files
B - Krypton Factor (dfs+ backtracking method)
"Yuanqi Cola" is not the end point, "China Cola" is
Detailed explanation of redis data structure, combined with books
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)
格式化JS代码,调试JS代码
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
Is it safe to open an account for stock speculation through the online account manager?
Shell summary (1)
Zero copy of network file transfer
2022 love analysis ― bank digitalization practice report
"Weilai Cup" 2022 Niuke summer multi school training camp 2 i.[let fat tension] matrix multiplication j.[link with arithmetic progression] linear regression
Mapbox GL JS active map refresh method
软件加群验证
MDK compilation process and arm compilation tool chain
MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
y77.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(八)
言语理解中心理解总结
Basic version of Google browser debugging tool (I)
Leetcode 537. 复数乘法(网友思路,自愧不如)