Skip to content

Commit 46ffa8b

Browse files
committed
Added Verilator testing
1 parent 5d87ef0 commit 46ffa8b

File tree

8 files changed

+307
-0
lines changed

8 files changed

+307
-0
lines changed

sim/MODULE.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ maven.install(
4646
],
4747
)
4848
use_repo(maven, "mvn")
49+
50+
bazel_dep(name = "rules_verilator", version = "5.034")
51+
single_version_override(
52+
module_name = "rules_verilator",
53+
registry = "https://raw.githubusercontent.com/Pinata-Consulting/ascenium-registry/683f93e670a04f8d3d47316c42d197d8ac095a00/",
54+
version = "5.034",
55+
)
56+
single_version_override(
57+
module_name = "verilator",
58+
registry = "https://raw.githubusercontent.com/Pinata-Consulting/ascenium-registry/683f93e670a04f8d3d47316c42d197d8ac095a00/",
59+
version = "5.034.ar.1",
60+
)
61+
single_version_override(
62+
module_name = "systemc",
63+
registry = "https://raw.githubusercontent.com/Pinata-Consulting/ascenium-registry/683f93e670a04f8d3d47316c42d197d8ac095a00/",
64+
version = "3.0.1",
65+
)
66+
register_toolchains(
67+
"@rules_verilator//verilator:verilator_toolchain",
68+
)

sim/make/vcs.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ $(vcs) $(vcs_post_synth) $(vcs_debug) $(vcs_post_synth_debug): export LDFLAGS :=
1919

2020
vcs_driver_deps := $(header) $(DRIVER_CC) $(DRIVER_H) $(midas_cc) $(midas_h)
2121

22+
# an example:
23+
# make -C .../midas/src/main/cc vcs \
24+
# PLATFORM=f1 \
25+
# DRIVER_NAME=SimpleCounterHarness \
26+
# GEN_FILE_BASENAME=FireSim-generated \
27+
# GEN_DIR=...sim/generated-src/f1/f1-examples-SimpleCounterHarness-NoConfig-F1Config \
28+
# DRIVER="...sim/src/main/cc/examples/simple_counter_top.cc" # could be multiple files
29+
# DESIGN_V=...sim/generated-src/f1/f1-examples-SimpleCounterHarness-NoConfig-F1Config/FireSim-generated.sv
2230
define make_vcs
2331
$(MAKE) -C $(simif_dir) $(1) \
2432
PLATFORM=$(PLATFORM) \
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
load("@rules_hdl//hdl:defs.bzl", "verilator_cc_library")
2+
3+
verilator_cc_library(
4+
name = "adder_verilator",
5+
module = ":adder", # should be the top level verilog file
6+
module_top = "emul",
7+
copts = [
8+
"-Wall",
9+
"-Imidas/src/main/cc",
10+
"-Igenerated-src/...",
11+
"-DVERILATOR",
12+
],
13+
vopts = [
14+
"--main",
15+
"--timing",
16+
"--cc",
17+
"--exe",
18+
"--timescale 1ps/1ps",
19+
"-O2",
20+
"-sv",
21+
"--vpi",
22+
"--output-split 10000",
23+
"--output-split-cfuncs 100",
24+
"-Wall",
25+
"-Wno-UNUSEDSIGNAL",
26+
"-Wno-DECLFILENAME",
27+
"-Wno-VARHIDDEN",
28+
"-Wno-UNDRIVEN",
29+
"TODO: LDFLAGS"
30+
"+define+CLOCK_PERIOD=1.0",
31+
"+define+RANDOMIZE_MEM_INIT",
32+
"+define+RANDOMIZE_REG_INIT",
33+
"+define+RANDOMIZE_GARBAGE_ASSIGN",
34+
"+define+RANDOMIZE_INVALID_ASSIGN",
35+
"+define+STOP_COND=!emul.reset",
36+
"+define+PRINTF_COND=!$emul.reset",
37+
"+define+RANDOM=32'b0",
38+
],
39+
)
40+
41+
cc_library(
42+
# probably has to include the header?
43+
)
44+
45+
# what about the driver class?
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_verilator//verilog:defs.bzl", "verilog_library")
2+
3+
verilog_library(
4+
name = "bufgce",
5+
srcs = ["BUFGCE.v"],
6+
)
7+
8+
verilog_library(
9+
name = "top",
10+
srcs = ["top.v"],
11+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
load("@rules_verilator//verilog:defs.bzl", "verilog_library")
2+
load("@rules_verilator//verilator:defs.bzl", "verilator_cc_library")
3+
4+
verilog_library(
5+
name = "counter",
6+
srcs = ["counter.v"],
7+
)
8+
9+
verilator_cc_library(
10+
name = "counter_cc",
11+
module = ":counter",
12+
module_top = "counter",
13+
)
14+
15+
cc_binary(
16+
name = "top",
17+
srcs = ["dut_counter.cpp"],
18+
deps = [":counter_cc"],
19+
)
20+
21+
verilog_library(
22+
name = "counter_tb",
23+
srcs = ["counter_tb.v"],
24+
deps = [":counter"],
25+
)
26+
27+
verilator_cc_library(
28+
name = "counter_tb_cc",
29+
module = ":counter_tb",
30+
module_top = "testbench",
31+
copts = [
32+
"-Wall",
33+
# "-DVERILATOR",
34+
"-fcoroutines",
35+
],
36+
vopts = [
37+
"--main",
38+
# "--exe",
39+
"--timing",
40+
"--cc",
41+
# "--timescale 1ps/1ps",
42+
"-O2",
43+
"-sv",
44+
# "--vpi",
45+
# "--output-split 10000",
46+
# "--output-split-cfuncs 100",
47+
"-Wall",
48+
"-Wno-UNUSEDSIGNAL",
49+
"-Wno-DECLFILENAME",
50+
"-Wno-VARHIDDEN",
51+
"-Wno-UNDRIVEN",
52+
# "+define+CLOCK_PERIOD=1.0",
53+
# "+define+RANDOMIZE_MEM_INIT",
54+
# "+define+RANDOMIZE_REG_INIT",
55+
# "+define+RANDOMIZE_GARBAGE_ASSIGN",
56+
# "+define+RANDOMIZE_INVALID_ASSIGN",
57+
# "+define+STOP_COND=!emul.reset",
58+
# "+define+PRINTF_COND=!emul.reset",
59+
# "+define+RANDOM=32\\'b0",
60+
],
61+
)
62+
63+
cc_binary(
64+
name = "counter_tb_top",
65+
deps = [":counter_tb_cc"],
66+
copts = [
67+
"-fcoroutines",
68+
],
69+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module counter(
2+
input clk,
3+
input reset,
4+
input enable,
5+
output [7:0] out
6+
);
7+
8+
(* keep *) reg [7:0] tmp;
9+
always @(*) begin
10+
tmp = counter_reg + 1;
11+
end
12+
13+
reg [7:0] counter_reg;
14+
always @(posedge clk) begin
15+
if (reset) begin
16+
counter_reg <= 0;
17+
end else if (enable) begin
18+
counter_reg <= tmp;
19+
end
20+
end
21+
22+
assign out = counter_reg;
23+
24+
endmodule
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module testbench;
2+
3+
// Inputs
4+
reg clk;
5+
reg reset;
6+
reg enable;
7+
8+
// Outputs
9+
wire [7:0] out;
10+
11+
// Instantiate the counter module
12+
counter uut (
13+
.clk(clk),
14+
.reset(reset),
15+
.enable(enable),
16+
.out(out)
17+
);
18+
19+
// Clock generation
20+
initial begin
21+
clk = 0;
22+
forever #5 clk = ~clk;
23+
end
24+
25+
// Stimulus
26+
initial begin
27+
// Initial values
28+
reset = 1;
29+
enable = 0;
30+
31+
// Apply reset
32+
#10;
33+
reset = 0;
34+
35+
// Enable the counter
36+
#10;
37+
enable = 1;
38+
39+
// Run for a while
40+
#100;
41+
42+
// Stop simulation
43+
$finish;
44+
end
45+
46+
// Monitor output
47+
initial begin
48+
$monitor("Time = %0t, clk = %b, reset = %b, enable = %b, out = %d", $time, clk, reset, enable, out);
49+
end
50+
51+
endmodule
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <cstdio>
2+
#include <stdlib.h>
3+
4+
#include "Vcounter.h"
5+
#include "verilated.h"
6+
#include "verilated_vcd_c.h"
7+
8+
vluint64_t global_time = 0;
9+
VerilatedVcdC *tfp = 0;
10+
11+
void do_terminate() {
12+
#ifdef VM_TRACE
13+
tfp->dump(global_time);
14+
tfp->close();
15+
#endif
16+
17+
printf("simulation finished at cycle %lld\n", (long long)global_time);
18+
19+
exit(0);
20+
}
21+
22+
void advance_clock(Vcounter *uut) {
23+
uut->clk ^= 1;
24+
uut->eval();
25+
#ifdef VM_TRACE
26+
tfp->dump(global_time);
27+
#endif
28+
uut->clk ^= 1;
29+
30+
global_time++;
31+
uut->eval();
32+
#ifdef VM_TRACE
33+
tfp->dump(global_time);
34+
#endif
35+
36+
global_time++;
37+
}
38+
39+
int main() {
40+
Vcounter top;
41+
#ifdef VM_TRACE
42+
// init trace dump
43+
Verilated::traceEverOn(true);
44+
tfp = new VerilatedVcdC;
45+
46+
top.trace(tfp, 99);
47+
tfp->open("output.vcd");
48+
#endif
49+
50+
top.clk = 1;
51+
top.enable = 0;
52+
top.reset = 1;
53+
advance_clock(&top);
54+
advance_clock(&top);
55+
advance_clock(&top);
56+
top.reset = 0;
57+
advance_clock(&top);
58+
59+
int counter = 0;
60+
61+
while (global_time < 100000) {
62+
bool enable = (rand() & 0xF) == 0;
63+
top.enable = enable;
64+
65+
if (enable)
66+
++counter;
67+
68+
advance_clock(&top);
69+
70+
if (top.out != counter) {
71+
fprintf(stderr, "ERROR: unexpected output of %d vs %d\n", top.out, counter);
72+
//do_terminate();
73+
}
74+
}
75+
76+
do_terminate();
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)