1.首先创建一个文件夹,名为 verilator-demo
bash
mkdir verilator-demo
2.接着设计一个电路,名为 ledmodule.v
v
module ledmodule(
output reg led,
input clk,
input rst
);
reg [2:0] counter = 3'd0;
always @(posedge clk) begin
counter <= (counter + 1'd1);
if(counter >= 3'd2) begin
led <= 1'd1;
end else begin
led <= 1'd0;
end
if(rst) begin
counter <= 3'd0;
led <= 1'd0;
end
end
endmodule
3.然后编写测试床,这是 cpp 文件,如下:
cpp
#include <stdlib.h>
#include <iostream>
#include <verilated.h>
#include <verilated_vcd_c.h>
#include "obj_dir/Vledmodule.h"
#define MAX_TIME 60
vluint64_t sim_time = 0;
int main(int argc, char **argv, char **env) {
Vledmodule *dut = new Vledmodule;
Verilated::traceEverOn(true);
VerilatedVcdC *m_trace = new VerilatedVcdC;
dut->trace(m_trace, 5);
m_trace->open("waveform.vcd");
while(sim_time < MAX_TIME) {
dut->clk ^= 1;
dut->rst = 0;
dut->eval();
m_trace->dump(sim_time);
sim_time++;
}
m_trace->close();
delete dut;
exit(EXIT_SUCCESS);
}
4接着运行如下命令,生成可执行文件,运行可执行文件产生波形
bash
verilator --cc ledmodule.v
verilator -Wall --trace --exe --build -cc ledmodule.cpp ledmodule.v
./obj_dir/Vledmodule
5.使用 gtkwave 打开波形
bash
gtkwave waveform.vcd
6.最后得到的波形如下图