Verilator testbench for unit tests
This commit is contained in:
@@ -1,11 +1,30 @@
|
||||
all: testbench.iv
|
||||
TOP = VX_fifo_queue
|
||||
|
||||
testbench.iv: testbench.v
|
||||
iverilog testbench.v -o testbench.iv -I ../../rtl/
|
||||
PARAMS ?=
|
||||
|
||||
run: testbench.iv
|
||||
! vvp testbench.iv | grep 'ERROR' || false
|
||||
INCLUDE = -I../../rtl/ -I../../rtl/libs
|
||||
|
||||
SRCS = main.cpp
|
||||
|
||||
all: build
|
||||
|
||||
CF += -std=c++11 -fms-extensions -I../..
|
||||
VF += $(PARAMS)
|
||||
|
||||
VF += --language 1800-2009 --assert -Wall --trace
|
||||
VF += -Wno-DECLFILENAME
|
||||
VF += --x-initial unique
|
||||
VF += -exe $(SRCS) $(INCLUDE)
|
||||
VF += $(PARAMS)
|
||||
|
||||
gen:
|
||||
verilator $(VF) -cc $(TOP).v -CFLAGS '$(CF)' --exe $(SRCS)
|
||||
|
||||
build: gen
|
||||
(cd obj_dir && make -j -f V$(TOP).mk)
|
||||
|
||||
run: build
|
||||
(cd obj_dir && ./V$(TOP))
|
||||
|
||||
clean:
|
||||
rm testbench.iv
|
||||
|
||||
rm -rf obj_dir
|
||||
|
||||
58
hw/unit_tests/generic_queue/main.cpp
Normal file
58
hw/unit_tests/generic_queue/main.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include "vl_simulator.h"
|
||||
#include "VVX_fifo_queue.h"
|
||||
#include "VVX_fifo_queue__Syms.h"
|
||||
|
||||
static const uint32_t MAX_TICKS = 10000000;
|
||||
static const uint32_t NUM_ITERATIONS = 20000;
|
||||
|
||||
using Device = VVX_fifo_queue;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// Initialize Verilators variables
|
||||
Verilated::commandArgs(argc, argv);
|
||||
|
||||
vl_simulator<Device> sim;
|
||||
|
||||
auto start_time = std::chrono::system_clock::now();
|
||||
|
||||
// run simulation
|
||||
unit64_t ticks = sim.reset(0);
|
||||
|
||||
for (;;) {
|
||||
//sim->io_in_valid = (in_sample < num_iterations * FFT_SIZE);
|
||||
//sim->io_out_ready = (out_sample < num_iterations * FFT_SIZE);
|
||||
|
||||
// enqueue data
|
||||
//if (sim->io_in_valid && sim->io_in_ready) {
|
||||
//std::cout << "t" << std::dec << ticks << std::hex << " input: re=" << re << ", im=" << im << std::endl;
|
||||
//sim->io_in_data = sample;
|
||||
//}
|
||||
|
||||
// dequeue data
|
||||
//if (sim->io_out_valid && sim->io_out_ready) {
|
||||
//std::cout << "t" << std::dec << ticks << std::hex << " output: re=" << re << ", im=" << im << std::endl;
|
||||
//test_outputs[out_sample++] = sample;
|
||||
//}
|
||||
|
||||
// check for completion
|
||||
|
||||
|
||||
// advance clock
|
||||
ticks = sim.step(ticks, 2);
|
||||
}
|
||||
|
||||
auto end_time = std::chrono::system_clock::now();
|
||||
auto latency = end_time - start_time;
|
||||
std::cout << "Average elapsed time = "
|
||||
<< std::chrono::duration<double, std::milli>(latency).count()
|
||||
<< " ms" << std::endl;
|
||||
|
||||
std::cout << "Simulation run time: " << std::dec << ticks/2 << " cycles" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
`timescale 1ns/1ns
|
||||
`include "VX_fifo_queue.v"
|
||||
|
||||
`define check(x, y) if ((x == y) !== 1) if ((x == y) === 0) $error("x=%h, expected=%h", x, y); else $warning("x=%h, expected=%h", x, y)
|
||||
|
||||
module testbench();
|
||||
|
||||
reg clk;
|
||||
reg reset;
|
||||
reg[3:0] data_in;
|
||||
reg push;
|
||||
reg pop;
|
||||
wire[3:0] data_out;
|
||||
wire full;
|
||||
wire empty;
|
||||
|
||||
VX_fifo_queue #(
|
||||
.DATAW(4),
|
||||
.SIZE(4)
|
||||
) dut (
|
||||
.clk(clk),
|
||||
.reset(reset),
|
||||
.data_in(data_in),
|
||||
.push(push),
|
||||
.pop(pop),
|
||||
.data_out(data_out),
|
||||
.empty(empty),
|
||||
.full(full),
|
||||
`UNUSED_PIN (alm_empty),
|
||||
`UNUSED_PIN (alm_full),
|
||||
`UNUSED_VAR (size)
|
||||
);
|
||||
|
||||
always begin
|
||||
#1 clk = !clk;
|
||||
end
|
||||
|
||||
initial begin
|
||||
$monitor ("%d: clk=%b rst=%b push=%b, pop=%b, din=%h, empty=%b, full=%b, dout=%h",
|
||||
$time, clk, reset, push, pop, data_in, empty, full, data_out);
|
||||
#0 clk=0; reset=1; pop=0; push=0;
|
||||
#2 reset=0; data_in=4'ha; pop=0; push=1;
|
||||
#2 `check(full, 0); `check(data_out, 4'ha); `check(empty, 0);
|
||||
#0 data_in=4'hb;
|
||||
#2 `check(full, 0); `check(data_out, 4'ha); `check(empty, 0);
|
||||
#0 data_in=4'hc;
|
||||
#2 `check(full, 0); `check(data_out, 4'ha); `check(empty, 0);
|
||||
#0 data_in=4'hd;
|
||||
#2 `check(full, 1); `check(data_out, 4'ha); `check(empty, 0);
|
||||
#0 push=0; pop=1;
|
||||
#2 `check(full, 0); `check(data_out, 4'hb); `check(empty, 0);
|
||||
#2 `check(full, 0); `check(data_out, 4'hc); `check(empty, 0);
|
||||
#2 `check(full, 0); `check(data_out, 4'hd); `check(empty, 0);
|
||||
#2 `check(full, 0); `check(data_out, 4'ha); `check(empty, 1);
|
||||
#0 data_in=4'he; push=1; pop=0;
|
||||
#2 `check(full, 0); `check(data_out, 4'he); `check(empty, 0);
|
||||
#0 data_in=4'hf; pop=1;
|
||||
#2 `check(full, 0); `check(data_out, 4'hf); `check(empty, 0);
|
||||
#0 push=0;
|
||||
#2 `check(full, 0); `check(data_out, 4'hc); `check(empty, 1);
|
||||
#1 $finish;
|
||||
end
|
||||
|
||||
endmodule
|
||||
81
hw/unit_tests/generic_queue/vl_simulator.h
Normal file
81
hw/unit_tests/generic_queue/vl_simulator.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include "verilated.h"
|
||||
|
||||
#ifdef VM_TRACE
|
||||
#include <verilated_vcd_c.h> // Trace file format header
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
class vl_simulator {
|
||||
private:
|
||||
|
||||
T top_;
|
||||
#ifdef VM_TRACE
|
||||
VerilatedVcdC tfp_;
|
||||
#endif
|
||||
|
||||
public:
|
||||
|
||||
vl_simulator() {
|
||||
top_.clk = 0;
|
||||
top_.reset = 0;
|
||||
#ifdef VM_TRACE
|
||||
Verilated::traceEverOn(true);
|
||||
top_.trace(&tfp_, 99);
|
||||
tfp_.open("trace.vcd");
|
||||
#endif
|
||||
}
|
||||
|
||||
~vl_simulator() {
|
||||
#ifdef VM_TRACE
|
||||
tfp_.close();
|
||||
#endif
|
||||
top_.final();
|
||||
}
|
||||
|
||||
uint64_t reset(uint64_t ticks) {
|
||||
top_.reset = 1;
|
||||
ticks = this->step(ticks, 2);
|
||||
top_.reset = 0;
|
||||
return ticks;
|
||||
}
|
||||
|
||||
uint64_t step(uint64_t ticks, uint32_t count = 1) {
|
||||
while (count--) {
|
||||
top_.eval();
|
||||
#ifdef VM_TRACE
|
||||
tfp_.dump(ticks);
|
||||
#endif
|
||||
top_.clk = !top_.clk;
|
||||
++ticks;
|
||||
}
|
||||
return ticks;
|
||||
}
|
||||
|
||||
auto operator->() {
|
||||
return &top_;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
void vl_setw(uint32_t* sig, Args&&... args) {
|
||||
std::array<uint32_t, sizeof... (Args)> arr{static_cast<uint32_t>(std::forward<Args>(args))...};
|
||||
for (size_t i = 0; i < sizeof... (Args); ++i) {
|
||||
sig[i] = arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
int vl_cmpw(const uint32_t* sig, Args&&... args) {
|
||||
std::array<uint32_t, sizeof... (Args)> arr{static_cast<uint32_t>(std::forward<Args>(args))...};
|
||||
for (size_t i = 0; i < sizeof... (Args); ++i) {
|
||||
if (sig[i] < arr[i])
|
||||
return -1;
|
||||
if (sig[i] > arr[i])
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user