在verilog中嵌入perl脚本

写了个可以处理嵌入到systemverilog文件中的perl的脚本.

# expand
./embaded_perl.pl -e -i test.sv [-nochk]
# clean
./embaded_perl.pl -c -i test.sv
// Perl sub: sub_name
// ...perl scripts print something to stdout...
// Perl sub end: sub_name
// generated contents
// Perl sub generation end: sub_name
#!/bin/perl
use strict;
use feature qw(switch);
no warnings "experimental::smartmatch";

use Data::Dumper;
use Tie::File;
use Getopt::Long;


# Perl sub: sub_name
# /*sub definition*/
# Perl sub end: sub_name
# /*generated contents*/
# Perl sub generation end: sub_name
#
my $debug_on = 0;

my $opt_infile;

my $opt_expand;
my $opt_nochk;

my $opt_clean;

GetOptions(
    "i=s"     => \$opt_infile,
    "e:s"     => \$opt_expand,
    "nochk:s" => \$opt_nochk,
    "c:s"     => \$opt_clean
) or die "Error in command line arguments\n";
&check_args;


my $begin_regex = '^\s*\/\/\s*Perl sub:\s*([a-zA-Z]\w*)\s*$';
my $end_regex = '^\s*\/\/\s*Perl sub end:\s*([a-zA-Z]\w*)\s*$';
my $end_generation_regex = '^\s*\/\/\s*Perl sub generation end:\s*([a-zA-Z]\w*)\s*$';

# the subs founded in the input file
my @subs;
my %subs_info;

my @contents;
tie @contents, 'Tie::File', $opt_infile or die "Fail to tie to [$opt_infile]\n";


if (defined $opt_expand) {
    &expand_all;
}

if (defined $opt_clean) {
    &clean_all;
}


sub traverse {
    my $state = 0;
    # 0: idle
    # 1: found begin
    # 2: found end

    my $current_sub_name = "";
    my $lineno = 0;
    for my $line (@contents) {
        $lineno++;
        given ($state) {
            when(1) { # found begin
                unless ($line =~ /$end_regex/) {
                    push @{$subs_info{$current_sub_name}->{sub_contents}}, $line;
                    next;
                }
                die "Expect sub end of [$current_sub_name]. Get sub end of [$1]\n"
                    if ($1 ne $current_sub_name);
                $state = 2;
                $subs_info{$current_sub_name}->{end_lineno} = $lineno;
                $subs_info{$current_sub_name}->{valid} = 1;
            }
            when(2) { # found end
                given($line) {
                    when(/$end_generation_regex/) {
                        die "Expect sub generation end of [$current_sub_name]. Get sub generation end of [$1]\n"
                            if ($1 ne $current_sub_name);
                        $subs_info{$current_sub_name}->{expanded} = 1;
                        $subs_info{$current_sub_name}->{generation_end_lineno} = $lineno;
                        $current_sub_name = "";
                        $state = 0;
                    }
                    when(/$begin_regex/) {
                        die "Repeat definition of [$1]\n" if (defined $subs_info{$1});
                        $current_sub_name = $1;
                        my $idx = scalar(@subs);
                        push @subs, $current_sub_name;
                        $subs_info{$current_sub_name} = {
                            idx          => $idx,
                            valid        => 0,
                            expanded     => 0,
                            begin_lineno => $lineno,
                            sub_contents => [],
                        };
                        $state = 1;
                    }
                }
            }
            default {
                if ($line =~ /$begin_regex/) {
                    die "Repeat definition of [$1]\n" if (defined $subs_info{$1});
                    $current_sub_name = $1;
                    my $idx = scalar(@subs);
                    push @subs, $current_sub_name;
                    $subs_info{$current_sub_name} = {
                        idx          => $idx,
                        valid        => 0,
                        expanded     => 0,
                        begin_lineno => $lineno,
                        sub_contents => [],
                    };
                    $state = 1;
                }
            }
        }
    } # end for

    if ($debug_on) {
        print Dumper(\@subs);
        print Dumper(\%subs_info);
    }
}

# expand
#------------------------------------------------------------------------------#
sub expand_all {
    &traverse;
    for (my $i = $#subs; $i >= 0; $i--) {
        my $name = $subs[$i];
        my $info = $subs_info{$name};
        if ($info->{valid} and not $info->{expanded}) {
            &expand($name, $info);
        }
    }
}

sub expand {
    my ($name, $info) = (shift, shift);
    my @tmp_script;
    for (@{$info->{sub_contents}}) {
        $_ =~ s/^\s*\/\/ ?//;
        push @tmp_script, $_;
    }
    open FH, ">tmp.$name.pl" or die "Fail to create [tmp.$name.pl]\n";
    print FH "#!/bin/perl\n";
    print FH "use strict;\n";
    print FH join("\n", @tmp_script);
    close FH;
    
    chmod 0700, "tmp.$name.pl";
    system("./tmp.$name.pl > tmp.$name.output");
    

    open FH, "<tmp.$name.output" or die "Fail to open [tmp.$name.output]\n";
    my @tmp_output = <FH>;
    close FH;
    push @tmp_output, ("\n", "// Perl sub generation end: $name", "\n");
    system("rm -rf tmp.$name.pl tmp.$name.output");

    if (defined $opt_nochk) {
        splice @contents, $info->{end_lineno}, 0, @tmp_output;
    } else { # default print the output for checking
        print @tmp_output, "\n";
    }
}

# clean
#------------------------------------------------------------------------------#
sub clean_all {
    &traverse;
    for (my $i = $#subs; $i >= 0; $i--) {
        my $name = $subs[$i];
        my $info = $subs_info{$name};
        if ($info->{valid} and $info->{expanded}) {
            &clean($name, $info);
        }
    }
}

sub clean {
    my ($name, $info) = (shift, shift);
    my $clean_length = $info->{generation_end_lineno}-$info->{end_lineno};
    splice @contents, $info->{end_lineno}, $clean_length;
}

# check_args
#------------------------------------------------------------------------------#
sub check_args {
    if (defined $opt_expand and defined $opt_clean) {
        die "Can't use -e and -c at the same time\n";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iscas2spice spice netlist generation tool -- version 2.2 by Jingye Xu @ VLSI Group, Dept. of ECE, UIC, June, 2008 This tool reads the ISCAS85 benchmark circuit "*.bench" file and translate the file into SPICE netlist using the given technology and the standard cell library. platform: linux x86 sytem Input: ISCAS85 benchmark circuit: *.bench; standard cell library: stdcells.sclb; standard cell models: stdcells.lib; interconnect paramaters: *.int; Output: SPICE netlist: out.sp The whole procedure of the tools can be divided into several steps: 1. Gate replacement: replace the gates that can't be found in the with the gates in the standard cell lib. (break.pl) Output: *.bench, *.bench.bak 2. Generate the GSRC files: generate the GSRC files for the fengshui placer. (gsrcgen.pl) Output: gsrcfile/iscas.* 3. Placement: using the fengshui placement tool to perform the component placement. (fs50) Output: gsrcfile/iscas_fs50.pl 4. Generate ISPD file: tanslate the placement results into ISPD98 format file that can be used as the input of the global router. (gsrc2ispd.pl) Output: gsrcfile/iscas.laby.txt 5. Perform the routing: use the labyrinth global router to perform the routing. (mazeRoute) Output: gsrcfile/output 6. Generate the SPICE netlist: use all the available information to generate the final SPICE netlist. (spicegen.pl) Output: out.sp Usage: iscas2spice.pl Iscas85BenchmarkFile [-C/L/N] options: -C :default value, use the RC model for interconnect -L :use the RLC model for interconnect -N :treat interconnect as short circuit wire This package used the fengshui placement tools and labyrinth global routing tools, for information regarding these two free tools, please vist: http://www.ece.ucsb.edu/~kastner/labyrinth/ http://vlsicad.cs.binghamton.edu/software.html For information regarding this software itself please visit: http://wave.ece.uic.edu/~iscas2spice Many thanks to my advisor Masud H. Chowdhury for his support!
### 回答1: Verilog-Perl是一个用于Verilog语言的开源工具,它结合了Perl编程语言的强大功能和Verilog的硬件描述语言特性,旨在提供一种快速、灵活和高效的方法来处理Verilog代码Verilog-Perl提供了丰富的功能,包括解析和生成Verilog代码、模块实例化、层次化遍历、参数化和条件化编译等。使用Verilog-Perl,开发人员可以轻松地从原始的Verilog代码提取信息、进行代码修改和生成新的Verilog文件,这对于设计验证、测试环境的自动化以及代码重用都非常有帮助。 与其他Verilog处理工具相比,Verilog-Perl的一个显著优势是它的灵活性。Perl编程语言具有强大的正则表达式和文本处理能力,这使得Verilog-Perl能够处理复杂的Verilog代码和设计结构。开发人员可以编写自定义的Perl脚本,利用Verilog-Perl提供的函数和模块,来实现各种Verilog代码处理任务。 另外,Verilog-Perl还支持与其他常用工具的集成。开发人员可以使用Verilog-Perl生成Verilog代码作为输入,再与仿真器、综合工具、形式验证工具等进行集成和协同工作。 总的来说,Verilog-Perl是一个强大的工具,可以帮助开发人员处理Verilog代码、加快设计验证和测试环境的开发进程,提高代码重用的效率。它的灵活性和与其他工具的集成能力使得它成为Verilog设计流程不可或缺的一部分。 ### 回答2: Verilog-Perl是一种用于编写和处理Verilog仿真测试环境的脚本语言。它结合了Verilog硬件描述语言和Perl脚本语言的功能,可用于自动化测试、仿真验证和设计分析等任务。 Verilog-Perl提供了一些用于读取和解析Verilog代码的函数和模块。通过这些函数和模块,用户可以方便地读取和操作Verilog代码的模块、端口、信号和语句等信息。这对于自动化测试和仿真验证非常有帮助,可以减少手动操作的工作量,提高工作效率。 此外,Verilog-Perl还提供了一些用于生成和修改Verilog代码的函数和模块。用户可以使用这些函数和模块来生成自定义的测试向量、修改模块的行为以及生成其他辅助代码。通过这些功能,用户可以快速创建和调整测试环境,以满足不同的验证需求。 Verilog-Perl还可以与其他常用的Perl模块和工具进行集成,扩展其功能和灵活性。用户可以使用Perl的正则表达式、文件处理和其他特性来处理Verilog代码和仿真结果,实现更复杂的验证任务。 总之,Verilog-Perl是一种功能强大的工具,可以帮助工程师简化Verilog代码的处理和测试环境的创建。它可以提高工作效率,减少出错的可能性,并促进验证的自动化和标准化。使用Verilog-Perl可以更高效地进行硬件设计和验证工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值