0%

初识GTKWAVE

编写testbench生成仿真文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// 包含顶层模块的头文件
#include "Vtop.h"
#include "verilated.h"
// 生成vcd格式的仿真文件所需头文件
#include "verilated_vcd_c.h"

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext; // 创建一个上下文对象
VerilatedVcdC *tfp = new VerilatedVcdC; //创建一个VCD对象指针
contextp->commandArgs(argc, argv);
Vtop* top = new Vtop{contextp}; // 在heap上构造一个对象 {}列表初始化
contextp->traceEverOn(true); // 打开上下文追踪
top->trace(tfp, 0);
tfp->open("wave.vcd"); //保存波形文件的位置
// 若上下文标记未完成,则一直循环
while (!contextp->gotFinish()) {
int a = rand() & 1;
int b = rand() & 1;
// 对top module的input进行复制
top->a = a;
top->b = b;
// 更新output
top->eval();
printf("a = %d, b = %d, f = %d\n", a, b, top->f);
assert(top->f == (a ^ b));
// 写入wave.vcd
tfp->dump(contextp->time());
// 增加上下文的时间
contextp->timeInc(1);
if (contextp->time() > 3) {
contextp->gotFinish(true); //当上下文的时间超过3时标记完成
}
}
// 释放heap上面的对象
delete top;
delete contextp;
tfp->close();
return 0;
}

tips: 若要生成VCD文件

  1. 打开上下文中的traceOn
  2. 将输入输出写进VCD对象中
  3. 通过VCD对象写入文件
  4. 编译时请加上-trace