文章目录
本文所用的题目代码来源于Perl在ASIC中的应用合集,为了方便记录学习
实例一:用Perl脚本统计芯片综合或PR网表中的cell类型及每种cell的使用的个数
#!/usr/bin/perl -w
use strict;
my %cells = ();
open(FIN,"<netlist.txt") or die "can not open file";
while (<FIN>) {
if ($_ =~ /(\w+)\s(\w+)\s\(\./ ) {
if (exists $cells{"$1"}){
$cells{"$1"} += 1;
} else {
$cells{"$1"} = 1;
}
}
}
close(FIN);
foreach my $celltype (sort(keys %cells)) {
print "$celltype: $cells{$celltype}\n";
}
实例二:利用Perl语言实现打印出杨辉三角
#!/usr/bin/perl -w
use strict;
my @tmp ;
my @tria;
push @tria, 1;
my $num = 1;
while (1) {
if ($num >=10) {last;}
push @tmp,1;
for (my $i = 0; $i < $num - 1; $i = $i + 1) {
if ($num > 1){
push @tmp, $tria[$i] + $tria[$i+1];}
}
push @tmp, 1;
@tria = @tmp;
print "@tria\n";
$num++;
@tmp = ();
}
实例三:用Perl语言写一个回归测试的脚本程序
从文件读取testcase列表,逐个调用仿真函数run_sim()进行仿真,并把每个testcase仿真的结果PASS/FAIL写入到汇总报告。(假设run_sim()的输入参数就是testcase名,返回值0表示PASS,返回值1表示FAIL
#!/usr/bin/perl -w
use strict;
open(FIN,"<testcase.txt") or die "can not open file";
my $cur_case = <FIN>;
my $FOUT;
open($FOUT,">log.txt") or die "1";
while(<FIN>) {
chomp($cur_case); #remove \n
my $log = rum_sum($cur_case);
if ($log == 1) { myPrint($FOUT,"$cur_case: PASSED!!\n");}
elsif ($log == 0) {myPrint($FOUT,"$cur_case: FAIED!!\n");}
}
close(FIN);
close($FOUT);
sub rum_sum {
return(0);
}
sub myPrint {
my $fh = shift;
for(@_) {
print $fh $_;# write into file
print $_; #print to screen
}
}
实例四:用函数递归解决下面这个问题:
有一只猴子吃一堆果子,已知每天吃昨天剩下的果子的一半再多一个,直到第10天才吃完。问原来这堆多少个?
#!/usr/bin/perl -w
use strict;
my $log = eatpeach(10,0);
sub eatpeach {
my $day = shift;
my $today = shift;
my $yesterday = 2*($today + 1);
$day--;
print "left:$yesterday day:$day\n";
if ($day == 0) {return $day,$yesterday;}
elsif ($day > 0) { return eatpeach($day,$yesterday);}
}
实例五: 用正则表达式把门级网表拆分成多个仅包含单个module的文件
#/usr/bin/perl -w
use strict;
#read in ori. netlist
#open(FIN, "<netlist.v");
#my $file = "";
#while(<FIN>){
#$file .= $_; #similar to +=
#}
#close(FIN);
my $file = `type netlist.v`;
#split by "module ... endmodule"
my @mod = ();
@mod = ($file =~ m/(module.*?endmodule)/gs);
#print into file
for my $m (@mod){
if($m =~ m/module\s+(\w+)\s+/){
open(FOUT, ">$1.v");
print FOUT "$m\n";
close(FOUT);
}
}