Verilog代碼自動縮排和對齊Perl指令碼,verilogperl

來源:互聯網
上載者:User

Verilog代碼自動縮排和對齊Perl指令碼,verilogperl

實驗室做FPGA開發時經常用到Verilog,代碼規範成為一個問題,於是乎寫了一個Perl指令碼對代碼進行正常化,主要是進行自動縮排和對齊。

代碼原理很簡單,主要是使用了Regex進行匹配和替換。

代碼如下,初學Perl,請讀者賜教:

###################################################### 代碼縮排對齊指令碼# 功能:對Verilog代碼進行自動縮排和對齊處理,#該版本目前還沒有對case語句進行處理# 更改:增加了對assign的縮排# 運行環境:perl# 運行方式:perl code_indent.pl# 作者:段聰# 日期:2014/9/16 22:16#####################################################my $filename = 'C:\Users\CvCv\Desktop\Perl_tmp\code.v';#輸入檔案名稱my $outfilename = 'C:\Users\CvCv\Desktop\Perl_tmp\out.v';#輸出檔案名#----------------------------------------------------------#測試檔案存在die "檔案$filename不存在!\n" unless -e $filename;#開啟檔案open(VFILE,"<",$filename) || die "開啟檔案失敗!\n";open(OFILE,">",$outfilename) || die "開啟檔案失敗!\n";#----------------------------------------------------------#變數定義$begin_cnt = 0;$if_cnt = 0;$autoindent_space = " "x4;#自動縮排的空格數(Tab寬度)$last_line = "";$assign_start = 0;#----------------------------------------------------------#讀取並處理檔案while(<VFILE>){$line = $_;if(/\s*\b(input|output|reg|wire)\b/i){$line =~ s/^\s+//;my @result = ($line =~ /\s*(input|output|reg|wire|(?:output\s+reg))\s*(\[\d+\:\d+\])?\s*((?!reg|wire)\w+)([,;]?)/ig);#print length($result[0]),"--@result\n";if($#result>0){$line  = $result[0]." "x(20-length($result[0]));$line .= $result[1]." "x(20-length($result[1]));$line .= $result[2]." "x(20-length($result[2]));$line .= $result[3]."\n";}$last_line = $_;}elsif(/(parameter)?\s*(\w+)\s*\=\s*(\d+\'[hbHB][0-9a-fA-F_]+)\s*([,;]?)/i){$line =~ s/^\s+//;my @result = ($line =~ /(parameter)?\s*(\w+)\s*\=\s*(\d+\'[hbHB][0-9a-fA-F_]+)\s*([,;]?)/ig);#print $#result."--@result\n";if($#result>0){$line  = $result[0]." "x(20-length($result[0]));$line .= $result[1]." "x(35-length($result[1]));$line .= "= ".$result[2]." "x(18-length($result[2]));$line .= $result[3]."\n";}$last_line = $_;}elsif(/\s*\b(module|endmodule|always|assign)\b/i){#匹配到module|always|assign$line =~ s/^\s+//;if(/\s*\bassign\b\s+\w/i){#匹配到assign$line =~ s/\s*\bassign\b\s+(\w)/assign  $1/i;$assign_start = 1;#print;}$last_line = $_;}elsif(/\bbegin\b/i){#匹配到begin$begin_cnt++;my $tmp_space = $autoindent_space x $begin_cnt;$line =~ s/^\s*/$tmp_space/;$last_line = $_;}elsif(/\bend\b/i){#匹配到endif($if_cnt>0){$if_cnt--;}my $tmp_space = $autoindent_space x $begin_cnt;$begin_cnt--;$line =~ s/^\s*/$tmp_space/;$last_line = $_;}elsif(/\bif\s*\(/i){#匹配到if$if_cnt++;my $tmp_space = $autoindent_space x ($begin_cnt>0?($begin_cnt+1):$begin_cnt);$line =~ s/^\s*/$tmp_space/;$last_line = $_;}elsif(/\belse\s*/i){#匹配到elsemy $tmp_space = $autoindent_space x ($begin_cnt>0?($begin_cnt+1):$begin_cnt);$line =~ s/^\s*/$tmp_space/;$last_line = $_;}elsif(/^\s*$/){#匹配到空行$line = "\n";}else{#print $if_cnt,"--",$_;if($assign_start){my $tmp_space = $autoindent_space x2;$line =~ s/^\s*/$tmp_space/;if(/".*;\s*$"/){$assign_start = 0;}}elsif($last_line =~ /(\bif\s*\()|(\belse\s*)/){$if_cnt--;my $tmp_space = $begin_cnt>0 ?  ($begin_cnt+2):$autoindent_space;$line =~ s/^\s*/$tmp_space/;}else{my $tmp_space = $begin_cnt>0 ? $autoindent_space x ($begin_cnt+1):"";$line =~ s/^\s*/$tmp_space/;}$last_line = $_;}$text.=$line;}#關閉檔案close VFILE;select OFILE;print $text;close OFILE;select STDOUT;print "處理完畢,檔案輸出到$outfilename\n";



Verilog綜合結果與perl指令碼

幫你回答3;
$a=' are you stupid? Yes you are';
@B=$a=~/you/g;
print scalar @B;
 
怎在verilog中調用perl

PLI。然後在C裡面調用perl。
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.