编写top.v文件
module top(
input a,
input b,
output f
);
assign f = a ^ b;
endmodule
编写main.c文件
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Vtop.h"
#include "verilated.h"
#include "verilated_vcd_c.h"
int main(int argc, char** argv, char** env) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
Vtop* top = new Vtop{contextp};
VerilatedVcdC* tfp = new VerilatedVcdC;
contextp->traceEverOn(true);
top->trace(tfp, 0);
tfp->open("wave.vcd");
while (!contextp->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(contextp->time());
contextp->timeInc(1);
assert(top->f == a ^ b);
}
delete top;
tfp->close();
delete contextp;
return 0;
}
verilator -Wall top.v top_main.cpp --cc --trace --exe --build
--trace #显示波形
./obj_dir/Vtop
gtkwave wave.vcd