verilator仿真生成.vcd波形文件 示例

1、模块准备

首先,我们先写好一个模块,如our_OnOff.v:

module our_OnOff(
    input a,
    input b,
    output f
);
  assign f = a ^ b;
endmodule

2、测试模块

main.c 如下:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Vour_OnOff.h"
#include "verilated_vcd_c.h"

vluint64_t main_time = 0;  //initial 仿真时间

double sc_time_stamp()
 {
     return main_time;
 }
 
int main(int argc,char **argv)
{
     Verilated::commandArgs(argc,argv);
     Verilated::traceEverOn(true); //导出vcd波形需要加此语句
 
     VerilatedVcdC* tfp = new VerilatedVcdC(); //导出vcd波形需要加此语句
 
     Vour_OnOff *top = new Vour_OnOff("top");
     top->trace(tfp, 0);
     tfp->open("wave.vcd"); //打开vcd
 
      while(!sc_time_stamp() < 20 && !Verilated::gotFinish())
     {                                                                                                                                                                                                            
     int a = rand() & 1;
     int b = rand() & 1;
     top->a = a;
     top->b = b;
     top->eval();
     printf("a = %d, b = %d, f = %d\n",a,b, top->f);
     tfp->dump(main_time); //dump wave
     main_time++; //推动仿真时间
     }
     top->final();
     tfp->close();
     delete top;
     return 0;
}

在一次循环中, 代码将会随机生成两个1比特信号, 用来驱动两个输入端口, 然后通过eval()函数更新电路的状态, 这样我们就可以读取输出端口的值并打印. 为了自动检查结果是否正确, 我们通过assert()语句对输出结果进行检查.

3、运行仿真

运行仿真分成三步:

a、生成目标文件夹

verilator -Wall --cc --exe --build main.cpp our_OnOff.v --trace

 b、编译

make -C obj_dir -f Vour_OnOff.mk Vour_OnOff

c、运行

./obj_dir/Vour_OnOff

可看到已经生成wave.vcd

 

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值