Skip to content

Commit

Permalink
test: optimize testbench for Verilator
Browse files Browse the repository at this point in the history
  • Loading branch information
phsauter committed Sep 27, 2024
1 parent b129d5c commit d897c00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MORTY ?= morty
SVASE ?= svase
SV2V ?= sv2v
PYTHON3 ?= python3
VERILATOR ?= verilator
VERILATOR ?= /foss/tools/bin/verilator
VSIM ?= vsim
REGGEN ?= $(PYTHON3) $(shell $(BENDER) path register_interface)/vendor/lowrisc_opentitan/util/regtool.py

Expand Down Expand Up @@ -89,9 +89,11 @@ verilator/croc.f: Bender.lock Bender.yml
$(BENDER) script verilator -t rtl -t verilator -DSYNTHESIS -DVERILATOR > $@

## Simulate RTL using Verilator
verilator: verilator/croc.f $(SW)
cd verilator; $(VERILATOR) $(VERILATOR_ARGS) --top tb_croc_soc -f croc.f
cd verilator; ./obj_dir/Vtb_croc_soc
verilator/obj_dir/Vtb_croc_soc: verilator/croc.f $(SW)
cd verilator; $(VERILATOR) $(VERILATOR_ARGS) -CFLAGS "-O0" --top tb_croc_soc -f croc.f

verilator: verilator/obj_dir/Vtb_croc_soc
cd verilator; obj_dir/Vtb_croc_soc

.PHONY: verilator vsim vsim-yosys verilator-yosys

Expand Down
23 changes: 18 additions & 5 deletions rtl/tb_croc_soc.sv
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Authors:
// - Philippe Sauter <[email protected]>

//`define TRACE_WAVE

module tb_croc_soc #(
parameter time ClkPeriod = 100ns,
parameter time ClkPeriodJtag = 100ns,
Expand Down Expand Up @@ -180,6 +182,7 @@ module tb_croc_soc #(
bit [31:0] data;
bit [7:0] byte_data;
int byte_count;
dm::sbcs_t sbcs = dm::sbcs_t'{sbautoincrement: 1'b1, sbaccess: 2, default: '0};

file = $fopen(filename, "r");
if (file == 0) begin
Expand All @@ -188,6 +191,9 @@ module tb_croc_soc #(
end
end

$display("@%t | [JTAG] Loading binary from %s", $time, filename);
jtag_dbg.write_dmi(dm::SBCS, sbcs);

// line by line
while (!$feof(file)) begin
if ($fgets(line, file) == 0) begin
Expand All @@ -200,6 +206,8 @@ module tb_croc_soc #(
if (status != 1) begin
$fatal(1, "Error: Incorrect address line format in file %s", filename);
end
$display("@%t | [JTAG] Writing to memory @%08x ", $time, addr);
jtag_dbg.write_dmi(dm::SBAddress0, addr);
continue;
end

Expand All @@ -214,21 +222,22 @@ module tb_croc_soc #(
end

// Shift in the byte to the correct position in the data word
data = {data[23:0], byte_data}; // Combine bytes into a 32-bit word
data = {byte_data, data[31:8]}; // Combine bytes into a 32-bit word
byte_count++;

// remove the byte from the line (2 numbers + 1 space)
line = line.substr(3, line.len()-1);

// write a complete word via jtag
if (byte_count == 4) begin
jtag_write_reg32(addr, data, 1'b0);
jtag_write(dm::SBData0, data);
addr += 4;
data = 32'h0;
byte_count = 0;
end
end
end
jtag_dbg.write_dmi(dm::SBCS, JtagInitSbcs);
$fclose(file);
endtask

Expand Down Expand Up @@ -279,8 +288,10 @@ module tb_croc_soc #(
initial begin
$timeformat(-9, 0, "ns", 12); // 1: scale (ns=-9), 2: decimals, 3: suffix, 4: print-field width
// configure VCD dump
// $dumpfile("croc.vcd");
// $dumpvars(1,i_croc_soc);
`ifdef TRACE_WAVE
$dumpfile("croc.vcd");
$dumpvars(1,i_croc_soc);
`endif

uart_rx_i = 1'b0;
irq0_i = 1'b0;
Expand All @@ -305,7 +316,9 @@ module tb_croc_soc #(

// finish simulation
repeat(50) @(posedge clk);
// $dumpflush;
`ifdef TRACE
$dumpflush;
`endif
$finish();
end

Expand Down

0 comments on commit d897c00

Please sign in to comment.