vcs+Makefile实现简单的testbench
网络上找的文章,实现了一遍。
步骤如下:
1. 创建verilog代码, 包括8位加法器代码和testbench代码。
adder8.v
module adder8 ( input clk, input [7:0] a_i, input [7:0] b_i, output reg [8:0] c_o ); always @ (posedge clk) begin c_o <= a_i + b_i; end endmodule
adder8_tb.v
// TB_SEED is random seed `ifndef TB_SEED `define TB_SEED 0 `endif module adder8_tb(); wire [8:0] result; reg [7:0] input_0; reg [7:0] input_1; reg clk; // clk2 is delay of clk, is used to verify result wire #5 clk2; assign clk2 = clk; initial begin $fsdbDumpfile("adder8.fsdb"); $fsdbDumpvars(); $display("TB_SEED is %d", `TB_SEED); clk = 0; input_0 = 8'd0; input_1 = 8'd0; #10000 $display("All test PASS!"); $finish; end // system clk is 50MHz always begin #10 clk = ~clk; end //generate random input always @ (negedge clk) begin input_0 = $random() % 256; input_1 = $random() % 256; end //get verified output always @ (posedge clk2) begin if ((input_0 + input_1) != result) begin $display("Test failed for %x + %x = %x", input_0, input_1, result); $finish; end else begin $display("%x + %x = %x", input_0, input_1, result);end end //instantiate adder8 adder8 dut( .clk(clk), .a_i(input_0), .b_i(input_1), .c_o(result) ); endmodule
2.编写Makefile文件,
VCS= vcs -sverilog -timescale=1ns/1ns +vpi -l build.log -debug_access+all SIMV = ./simv -l simv.log ifndef TB_SEED TB_SEED = 1024 endif all: comp run comp: $(VCS) +define+TB_SEED=$(TB_SEED) +incdir+. \ adder8.v \ adder8_tb.v run: $(SIMV) +fsdbfile+top.fsdb dbg: verdi -f file.f -ssf top.fsdb & clean: rm -rf core csrc simv* vc_hdrs.h ucli.key urg* *.log *.fsdb novas.* verdiLog
注意:Makefile里面的空格排版位tab键。
file.f里面内容为:
adder8_tb.v
adder8.v
3.
编译项目: Make
清除项目: Make clean
查看波形:Make dbg
编译日志文件为:build .log
仿真日志文件为:simv.log
仿真结果为:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程