#!/usr/bin/perl ############################################################################## ## Author # Avi Mittal ## Description # This script generates a template testbench for any vhdl file ############################################################################## ## Date # Who # Version # Details ############################################################################## ## 24/04/08 # Avi # 1 # Initial Revision ############################################################################## use File::Find; if($#ARGV < 0) { print "ERROR: insufficient fields\n"; print "Usage:\nlinux_prompt> vhdlgrep.pl \n"; exit; } if(!(-f @ARGV[0])) { print "ERROR! Input file @ARGV[0] does not exists. . . Exiting\n"; exit; } $ff = "$ARGV[0]"; @ffs = split(/\//, $ff); $out_file_name = pop(@ffs); $found_entity = 0; $in_port_scaler = zero; $out_port_scaler = zero; $clk_name = clk_viterbi; if($#ARGV == 1) { $clk_name = @ARGV[1]; } #f#print "file name minus path = $out_file_name,array = @ffs,orig argv0 = $ff\n"; $line_number = 0; open(infile0,"<$ff") || die "Couldnft open infile0. $ff\n "; #system("awk f/module/,/endmodule/f @ARGV[0]>@ARGV[0].module") while ($line0 = ) { chomp($line0); $line_number ++; $line0 =~ s/^/ /g; #add a space at start $line0 =~ s/$/ /g; #add a space at end $line0 =~ s/\-\-.*//g; #remove comments $line0 =~ s/;/ ; /g; #add space $line0 =~ s/\(/ \( /g; #add space $line0 =~ s/\)/ \) /g; #add space $line0 =~ s/,/ , /g; #add space $line0 =~ s/:/ : /g; #add space $line0 =~ s/: =/:=/g; #remove space $line0 =~ s// > /g; #add space $line0 =~ s/= >/=>/g; #remove space $line0 =~ s/\t+/ /g; #No tabs wanted $line0 =~ s/\s+/ /g; #Only single spaces wanted if(!($line0 =~ /\S/)) #Skip empty lines { next; } if($line0 =~ /( entity )/i) { $found_entity = $found_entity + 1; #$entity_name = $entity_name." ".$3; #print "Entity found = $entity_name\n"; } if($line0 =~ / end /i) { $found_end = 1; #print "End found\n"; $port_line = $port_line." ".$line0; last; } $port_line = $port_line." ".$line0; #print "$port_line\n"; #print "line_number = $line_number\n"; } #while ($line0 = ) #print "$port_line\n,found_entity = $found_entity,found_end = $found_end\n"; if($found_entity == 0) { print "ERROR! No 'entity' declaration found in the given file\n"; exit; } if(!($port_line =~ /(.*)(entity)(\s+)(\w+)(\s+)(is )/i)) { print "ERROR! No 'entity' declaration found in the given file\n"; exit; } else { $entity_name = $4; print "Name of Entity found = $entity_name\n"; } if($found_entity == 1 && $found_end == 1) { if($port_line =~ /(.*)( port )(.*)( end )/i) { #print "$3\n"; $port_scaler = $3; @port_list = split(/;/,$port_scaler); } }# if($found_entity == 1 && $found_end == 1) if($port_line =~ /(.*)( generic )(.*)( port )/i) { push(@generic_line,"$2\n$3\n"); #print "@generic_line"; } $port_line =~ s/library/xlibx/i; #first library is changed to xlibx #so that all libs may be grepped by #the following statement if($port_line =~ /(.*)( xlibx )(.*)( entity )/i) { $libs = $3; $libs =~ s/;/;\n/g; push(@library_line,"LIBRARY $libs"); #print "@library_line\n"; } push(@comp_list,"component $entity_name\n"); push(@comp_list,"PORT \( \n"); push(@map_list,"$entity_name\_u1 : $entity_name\n"); push(@map_list,"PORT MAP\( \n"); $current_port = 0; #print "total number of ports = $#port_list\n"; foreach $port (@port_list) { $current_port ++; $sc = ";"; $co = ","; if($current_port == $#port_list) { $sc = ""; $co = ""; } #print "current_port = $current_port\n"; #print "Each port = $port\n"; $port =~ s/#//g; if(!($port =~ /\(.*\)/)) #if the line does not contain both braces i.e open and closde (), then remove them please { $port =~ s/\(//g; $port =~ s/\)//g; } if($port =~ /(.*)(:)(\s+)(\w+)(\s+)(.*)/) { $port_name = $1; $port_direction = $4; $port_type = $6; $port_direction =~ tr/A-Z/a-z/; $port_name =~ s/(\s+)//g; push(@comp_list, "$port_name : $port_direction $port_type $sc\n"); push(@signal_list, "signal $port_name : $port_type ;\n"); @sub_ports = split(/,/,$port_name); foreach $sub_port (@sub_ports) { push(@map_list,"$sub_port => $sub_port $co\n"); } }# if($port =~ /(.*)(:)(\s+)(\w+)(\s+)(.*)/) }# foreach $port (@port_list) open(out,">$entity_name\_tb.vhd") || die "Could not open mapfile for $entity_name\_tb.vhd\n" ; push(@comp_list,"\);\n"); push(@comp_list,"END COMPONENT ;\n"); push(@map_list,"\);\n"); print out "\-\-Template testbench generated by genTemplateTb.pl for $entity_name\n"; print out "@library_line"; print out "\n"; print out "\n"; print out "ENTITY $entity_name\_tb is \n"; print out "@generic_line"; print out "END $entity_name\_tb ;\n"; print out "\n"; print out "ARCHITECTURE tb of $entity_name\_tb is \n"; print out "\n"; print out @signal_list; print out "\n"; print out @comp_list; print out "\n"; print out "BEGIN\n"; print out @map_list; print out "END TB; \n"