Perl Script to Indent Verilog Files

Here’s a perl script to indent verilog files.

Download here. vindent.pdf

Add your own list of keywords to indent any source code

For example, this script could be used to indent C/C++/Java source codes.

This script uses two spaces for each indent.  This could be changed as required.

#!/usr/bin/perl
# Filename    : vindent.pl
# Author      : A.G.Raja
# Website     : https://agraja.wordpress.com
#
# Usage       : ./vindent.pl verilogfile.v
#             : ./vindent.pl *.*v*
# Help        : ./vindent.pl

# Verilog File indenter

# Space for each indent
my $Space = ”  “;

# Block begin.  For indenting C/C++/Java program use “{”
my $Open = “begin”;
# Block end.  For indenting C/C++/Java program use “}”
my $Close = “end”;

# Block statements.
# Note: Each Open keyword must have corresponding Close keyword
my @Openkeywords = qw{ module program package interface class task function };
my @Closekeywords = qw{ endmodule endprogram endpackage endinterface endclass endtask endfunction };

sub indent {
my $indent = 0;
my($infile) = @_;
open(FILE, “$infile”) or die “Couldn’t open file \”$infile\”\n”;
open(TFILE, “>temp”) or die “Couldn’t create temp file\n”;

while($line = <FILE>) {
chomp $line;
$line =~ s/^\s*//;
print TFILE “$line \n”;
}
close TFILE;
close FILE;
open(TFILE, “<temp”);
open(FILE, “>$infile”);

while($line = <TFILE>) {
foreach $Closekey (@Closekeywords) {
if($line =~ /^$Closekey\s/) { $indent–; }
}
if (($line =~ /.*\s+$Close\s/ ) || ($line =~ /^$Close\s/))
{ $indent–; }
for($i=0;$i<$indent;$i++) { print FILE $Space; }
print FILE $line;
foreach $Openkey (@Openkeywords) {
if($line =~ /^$Openkey\s/) { $indent++; }
}
if (($line =~ /.*\s+$Open\s/ )|| ($line =~ /^$Open\s/))
{ $indent++; }
}
close FILE; close TFILE;
print “Indented File \”$infile\”\n”;
}

my $argnum;
if($ARGV[0] eq “”) {
print “Usage : vindent.pl verilogfile.v\n”;
print ”        vindent.pl *.v\n”;
print ”        vindent.pl *.*v*\n”;
print “Help  :\n”;
print ”        vindent.pl\n”;
}
foreach $argnum (0 .. $#ARGV) {
my $ifile = $ARGV[$argnum];
&indent($ifile);
}
unlink “temp”;
Download here. vindent.pdf

Advertisements

Perl script to display comments

Download here. c_help.pdf

 

#!/usr/bin/perl

# Filename: chelp.pl

# Author : A. G. Raja

# Website : agraja.wordpress.com

# License : GPL

# pass c file as argument to this script

# this script displays all the comments

use strict;

use warnings;

open(FILE,$ARGV[0]) or die “File not Found”;

my $line;

my $start=0;

while ( $line = <FILE> ) {

if($line =~ /\/\//)

{

$line=~ s/(.*)\/\/(.*)/$2/;

print $line;

}

if($line =~ /\/\*/)

{

$start=1;

$line=~ s/(.*)\/\*(.*)/$2/;

}

if($line =~ /\*\//)

{

$line=~ s/(.*)\*\//$1/;

print $line;

$start=0;

}

if($start) { print $line; }

}

close FILE;

 

Tips:

Use a shell script to redirect the output

of the above script to another program.

Example

#!/bin/bash

# Filename: help.sh

# Author : A.G.Raja

# Website : agraja.wordpress.com

# License : GPL

# rename chelp.pl to chelp

# Install chelp and add to path

chelp $1 | less

Demo:

 

Below is a C file

 

// Filename : demo.c

// Author : cfile.pl

// License : GPL

#include <stdio.h>

#define print(x) printf(“str=%s\n”,x);

int main(int argc, char **argv)

{

/* this is a multi-

line comment */

char *str=”File name:”;// this will print file name

print(argv[0])

return 0;

}

 

Type at the terminal:

[raja@agraja ]$ chelp demo.c

 

The following will be printed to stdout

 

Filename : demo.c

Author : cfile.pl

License : GPL

this is a multi-

line comment

this will print file name

Download here. c_help.pdf

C Find Replace Program

Download here. string_replace.pdf

// Filename : string_replace.c

// Author : A.G.Raja

// Website : agraja.wordpress.com

// License : GPL

#include <stdio.h>

 

 

char* string_replace(unsigned char *from, char *find, char *replace)

{

const char *p;

char *q, *to;

int i,d, match;

to=malloc(sizeof(char)*strlen(from)*strlen(replace)/strlen(find));

 

for(p = from, q = to; *p != ”; p++)

{

// find logic

for(d=0;d<strlen(find);d++)

{

if (*(p+d) == *(find+d)) match=1;

else match =0;

}

// replace logic

if (match)

{

for(i=0;i<strlen(replace);i++)

{

*(q+i)=*(replace+i);

}

q=q+i; p=p+d-1;

}

else // copy unchanged chars

{ *q=*p; q++; }

 

} *q = ”;

return to;

}

 

 

 

 

 

 

 

 

 

 

// Test code

int main()

{

unsigned char *str = “this is original text” ;

char f[]=”original”;

char r[]=”new”;

unsigned char *str2;

printf(“before:%s\n”,str);

str2=string_replace(str,f,r);

printf(“after:%s\n”,str2);

return 0;

}

 

Download here. string_replace.pdf

Tips:

See the following pages for char replace, trim spaces

char replace

trim spaces

 

C Find and Replace program

Download here. find_replace.pdf

// Filename : find_replace.c

// Author : A.G.Raja

// Website : agraja.wordpress.com

// License : GPL

#include <stdio.h>

void find_replace(char *to, unsigned char *from, char find, char replace)

{

const char *p; char *q;

for(p = from, q = to; *p != ”; p++)

{

if ( *p != find)

{ *q=*p; q++; }

else

{ *q=replace; q++; }

} *q = ”;

}

int main()

{

unsigned char *str = “this is original text” ;

unsigned char str2[strlen(str)];

printf(“before:%s\n”,str);

find_replace(str2,str,’s’,’x’);

printf(“after:%s\n”,str2);

return 0;

}

 

 

// This program can be used for single char only.

// string with more than one char is not supported

Download here. find_replace.pdf

Tips:

This function can be used to trim white spaces in file.

See this page for an example: Trim

See this page for string replace: string replace

 

 

 

 

C program to trim blank spaces

#include <stdio.h>

void copy(char *to, char *from);

int main(void)
{

char str[80];

copy (str,” this ia a test “);

printf(“without spaces:%s”,str);

return 0;

}

void copy(char *to, char *from)

{

const char *p; char *q;

for(p=from,q=to;*p != ‘ ‘; p++)

// space between quotes in the above statement
{

if (*p != ‘ ‘) { *q=*p; q++; }

}

*q = ”;

}

// Download here.

// trim.doc

Tips: This function can be modified to find and replace a char.

See this page for an example Find and Replace

See this page for string replace: string replace

System Verilog DPI Example

String passing example

Download as PDF document.

 systemverilogdpi.pdf

1) For illustration strings are used;
But strings can’t be used in module ports;
2) Use SystemVerilog “program” instead of “module” for testbenches.
See this: SystemVerilog program example

//  dpi_top.v
module top ();
import "DPI-C" context v2c_c=
task v2c_sv(input string a);

import "DPI-C" context c2v_c=
function string c2v_sv();

export "DPI-C" print_string_c =
function print_string_sv;

string v2c_string,c2v_string;

function void
print_string_sv(input string aaa);
$display("Exporting VerilogFunction:%s", aaa);
endfunction

initial
begin
v2c_string = "A v2c_string";
v2c_sv(v2c_string);
c2v_string = c2v_sv();
$display("c2v_string: %s n",
c2v_string);
$finish;
end
endmodule

// dpi.h
#include “svdpi.h”

#ifdef __cplusplus
extern “C” {
#endif

extern void print_string_c (const char *_a1);

#ifdef __cplusplus
}
#endif



//  dpi_main.c
#include <stdio.h>
#include "svdpi.h"
#include "dpi.h"
#include "veriuser.h"
void v2c_c(char* v2c_string)
{
io_printf("v2c_string: %sn",v2c_string);
print_string_c("string passed from C");
}
char* c2v_c(void)
{
char* c2v_string;
c2v_string="This is c2v_string";
//io_printf("c2v_string: %sn",
//c2v_string);
return c2v_string;
}
#####   shell script to run simulation
 ncverilog +sv dpi_top.v +elaborate +ncelabargs+-messages
 gcc -fPIC -shared -o dpi_main.so  dpi_main.c  -I/$CDS_INST_DIR/tools.lnx86/inca/include
 ncverilog +sv dpi_top.v +sv_lib=dpi_main.so +access+r +ncsimargs+"-sv_root ./"   
Download NCVerilog demo here.

 systemverilogdpi.pdf

 Here is a demo for running the same example 
using Modelsim under Windows. System Verilog DPI Modelsim Download Modelsim demo here. system-verilog-dpi-modelsim.pdf