Skip to content

Commit

Permalink
Add Verilator testbench.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwise committed Dec 1, 2017
1 parent 4abb54e commit 24a3c08
Show file tree
Hide file tree
Showing 6 changed files with 1,098 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ DEFAULT_GCC := /home/utils/gcc-4.9.3/bin/g++
DEFAULT_PERL := /home/utils/perl-5.8.8/bin/perl
DEFAULT_JAVA := /home/utils/java/jdk1.8.0_131/bin/java
DEFAULT_SYSTEMC := /usr/local/systemc-2.3.0/
DEFAULT_VERILATOR := verilator
DEFAULT_CLANG := clang
DEFAULT_PROJ := nv_full

$(TREE_MAKE): Makefile
Expand Down Expand Up @@ -50,6 +52,12 @@ $(TREE_MAKE): Makefile
@echo " " >> $@
@echo "## systemc: needed for Cmodel build (optional) " >> $@
@read -p "Enter systemc path (Press ENTER to use: $(DEFAULT_SYSTEMC)):" opt_systemc; if [ "_$$opt_systemc" = "_" ]; then echo "SYSTEMC := $(DEFAULT_SYSTEMC)" >> $@; else echo "SYSTEMC := $$opt_systemc" >> $@; fi
@echo " " >> $@
@echo "## verilator: used to build testbench without VCS (optional)" >> $@
@read -p "OPTIONAL: Enter verilator path (Press ENTER to use: $(DEFAULT_VERILATOR)):" opt_verilator; if [ "_$$opt_verilator" = "_" ]; then echo "VERILATOR := $(DEFAULT_VERILATOR)" >> $@; else echo "VERILATOR := $$opt_verilator" >> $@; fi
@echo " " >> $@
@echo "## clang: used to build Verilated binaries (optional)" >> $@
@read -p "OPTIONAL: Enter clang path (Press ENTER to use: $(DEFAULT_CLANG)):" opt_clang; if [ "_$$opt_clang" = "_" ]; then echo "CLANG := $(DEFAULT_CLANG)" >> $@; else echo "CLANG := $$opt_clang" >> $@; fi
@echo
@echo "====================================================================="
@echo "$@ is created successfully, and you can edit $@ manually if necessary"
Expand Down
7 changes: 7 additions & 0 deletions tools/etc/build.config
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ vmod_nvdla_top:
- vmod_nvdla_retiming
- vmod_nvdla_car

verilator:
sandbox:
- verif/verilator
dependencies:
- vmod_nvdla_top

verif_sim:
optional: true
sandbox:
- verif/sim
dependencies:
Expand Down
34 changes: 34 additions & 0 deletions verif/verilator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Makefile
# Build system for Verilator testbench
# NVDLA Open Source Project
#
# Copyright (c) 2017 NVIDIA Corporation. Licensed under the NVDLA Open
# Hardware License. For more information, see the "LICENSE" file that came
# with this distribution.

DEPTH = ../..
include $(DEPTH)/tools/make/common.make

default: $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/VNV_nvdla

VERILATOR_PARAMS ?= --compiler clang --output-split 250000000
# Perhaps you need to trace?
# VERILATOR_PARAMS += --trace --trace-depth 7 --trace-max-array 8

# We end up hardcoding this, because the .f file applies only to nv_full,
# living in outdir, right now. Otherwise, you're on your own...
$(DEPTH)/outdir/nv_full/verilator/VNV_nvdla.mk: verilator.f ../../outdir/nv_full/vmod # and a lot of RTL...
$(VERILATOR) --cc --exe -f verilator.f --Mdir ../../outdir/nv_full/verilator/ nvdla.cpp --compiler clang --output-split 250000000

$(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/VNV_nvdla: $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/VNV_nvdla.mk nvdla.cpp
rm -f $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/nvdla.cpp
cp nvdla.cpp $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/nvdla.cpp
make -C $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator -f VNV_nvdla.mk CC=$(CLANG) CXX=$(CLANG)++ VM_PARALLEL_BUILDS=1

$(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/test/%: $(DEPTH)/verif/traces/traceplayer/%
rm -rf $@
mkdir -p $@
$(PERL) input_txn_to_verilator.pl $< $@/trace.bin

run: $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/test/$(TEST) $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/VNV_nvdla
cd $(DEPTH)/$(OUTDIR)/$(PROJECT)/verilator/test/$(TEST) && ../../VNV_nvdla trace.bin
170 changes: 170 additions & 0 deletions verif/verilator/input_txn_to_verilator.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#!/usr/bin/env perl

use strict;

# Configurable parameters
my $read_reg_poll_retries = 50000;
my $test_dir = '.';
$test_dir = $ARGV[0];

# Creating a hash for the above - command - hex val
my %command_hash = (
'write_reg' => '00',
'read_reg' => '01',
'write_mem' => '02',
'read_mem' => '03',
'load_mem' => '04',
'dump_mem' => '05',
'wait' => '06',
);


my $input_file = "$test_dir/input.txn";
my $output_file = $ARGV[1];

my $inf;
my $ouf;

my $load_file_counter = 0;
my $dump_file_counter = 0;
print "converting $input_file to $output_file\n";
open ($inf, "<", $input_file) || die "Cannot open $input_file";
open ($ouf, ">:raw", $output_file) || die "Cannot open $output_file";

# SW Reset sequence from master_seq.sv
#write_reg("write_reg 0x27000200 0x00000000", 0);
#write_reg("write_reg 0x27000200 0xffffffff", 0);
#write_reg("write_reg 0x27000200 0x00000000", 0);
#write_reg("write_reg 0x27000200 0xffffffff", 1);
#write_reg("write_reg 0x27000200 0xc0012020", 1);

#my @sw_reset_seq = (
# "write_reg 0x27000200 0x00000000",
# "write_reg 0x27000200 0xffffffff",
# "write_reg 0x27000200 0x00000000",
# "write_reg 0x27000200 0xffffffff",
# "write_reg 0x27000200 0xc0012020"
#);
#
#my $cmd;
#foreach $cmd (@sw_reset_seq) {
## Right now sw_reset_seq only has write_reg
# my @values = split (' ', $cmd);
# my $hex_string = $command_hash{$values[0]};
#
# my $address = $values[1];
# my $data = $values[2];
#
# $address =~ m/0x(.*)/;
# my $hex_addr = $1;
# $data =~ m/0x(.*)/;
# my $hex_data = $1;
#
# $hex_string = $hex_string.$hex_addr.$hex_data;
#
# # Pad extra 0's to maintain cmd length of 128 bits
# $hex_string = $hex_string."00000000000000";
# print $ouf "$hex_string\n";
#}

while(<$inf>)
{
my $input_line = $_;
$input_line =~ s/#.*//;
my @values = split (' ', $input_line);
#print "\n" . $input_line . "\n";

my $size = scalar (split(' ', $input_line));
#print "\nSIZE: ". $size . "\n";

if($size != 0) {

if ($values[0] =~ /wait/) {
print $ouf pack("C", 1);
}
elsif($values[0] =~ /write_reg/) {
if($size != 3) { die "\nERROR: in write_reg command: $input_line\n" };

# For CSB, top 16 bits are misc, lower 16 are addr
my $address = $values[1];
my $data = $values[2];

print $ouf pack("CLL", 2, hex($address), hex($data));

} elsif ($values[0] =~ /read_reg/) {
if($size != 4) { die "\n ERROR: in read_reg command: $input_line\n" };

my $address;
my $bitmask;
my $cmp_mode;
my $exp_data;
my $poll_attempts = $read_reg_poll_retries;

$address = $values[1];
$bitmask = $values[2];
$exp_data = $values[3];

print $ouf pack("CLLL", 3, hex($address), hex($bitmask), hex($exp_data));
} elsif ($values[0] =~ /dump_mem/) {
my $addr = $values[1];
my $offset = $values[2];
my $mem_out = $values[3];

print $ouf pack("CLLL", 4, hex($addr), hex($offset), length($mem_out)) . $mem_out;
} elsif ($values[0] =~ /load_mem/) {
my $addr = $values[1];
my $offset = $values[2];
my $mem_in = $values[3];
my $raw_file;
my $mem_out;
my $minf, my $mouf;

print $ouf pack("CLL", 5, hex($addr), hex($offset));
open $minf, "<". "$test_dir/$mem_in";

print "File $load_file_counter $addr $offset\n";

my $totbytes = 0;

if ($mem_in =~ m/\.dat$/) {
while(<$minf>)
{
chomp;
my $line = $_;
next if ($line !~ m/^ ?0x/);
$line =~ s/ ?0x//g;
#assuming 32 bytes per line. 1st byte on line goes to addr0
# So we split into groups of 8 nums (4 bytes) then reverse each byte
my @bytes = ($line =~ m/([0-9a-z]{2})/g);
die "Can't parse $line into 32 groups of 2 hex numbers. Got ".scalar(@bytes)." groups" if (scalar(@bytes) != 32);
foreach my $byte (@bytes) {
print $ouf pack("C", hex($byte));
}
$totbytes += 32;
if ($totbytes == hex($offset)) {
print "okay, that's all the bytes I expected -- I'm stopping here\n";
last;
}
}
die "expected ".hex($offset)." bytes, got $totbytes bytes" if hex($offset) != $totbytes;
}
else {
die "only .dat files supported in this tool so far";
}

close $minf;
}
#print " HexString: $hex_string" . "\n";
#my @split_hex = ($hex_string =~ m/../g);
#foreach(@split_hex) { print $ouf "$_\n";}
}

}

print $ouf pack("C", 0xFF);

close $inf;
close $ouf;

1;

Loading

0 comments on commit 24a3c08

Please sign in to comment.