IC验证——perl脚本ccode_standard——c代码寄存器配置标准化

发布时间:2024年01月13日

目录

1 脚本名称

2 脚本路径

3 脚本参数说明

4 脚本操作说明

5 脚本代码


1 脚本名称

ccode_standard

2 脚本路径

/scripts/bin/ccode_standard

3 脚本参数说明

次序

参数名

说明

1

address

(./rfdig;.;..;./boot)

指定脚本执行路径(可以为脚本所在路径的任意相对路径)

help

脚本使用帮助,打印说明信息

2

all

运行脚本后,脚本会解析全部rbus.cfg,并对参数1指定的目录中的.c文件进行寄存器配置代码标准化修改

module_name_seca.cfg

运行脚本后,脚本会解析参数2指定的rbus.cfg,并对参数1指定的目录中的.c文件进行寄存器配置代码标准化修改

示例

命令:ccode_standard ?help

输出ccode_standard使用帮助信息

命令:ccode_standard ?./rfdig ?rfdig_seca.cfg

对当前路径下的rfdig文件夹及其子文件夹下所有的.c文件中,有关rfdig_seca.cfg中的寄存器配置代码进行标准化修改;

命令:ccode_standard ?. ?crg_seca.cfg

对当前路径下的所有文件夹及其子文件夹下的所有.c文件中,所有有关crg_seca.cfg中的寄存器配置代码进行标准化修改;

命令:ccode_standard ?. ?all

对当前路径下的所有文件夹及其子文件夹下的所有.c文件中,所有模块的寄存器配置代码进行标准化修改;

说明

配置文件module_name_seca.cfg是被验模块寄存器的详细配置文件;脚本索引该文件的具体路径为:

$PROJ_SRC/design/scripts/rbus_scripts/rbus_cfg/

使用该脚本前请先gen_rbus;

4 脚本操作说明

? ? ? ? 该脚本用于将如下所示的代码标准化,加速ST用例调整,该脚本可以反复多次调用,不会产生异常问题:

  1. 使用脚本前,首先gen_rbus,source,产生脚本需要解析的rbus.cfg文件,以及环境变量$ENV{PROJ_SRC};
  2. 根据脚本存放路径,指定脚本工作目录,如下例所示,脚本存放于case目录,我想修改rfdig文件夹下的.c文件,参考rfdig_seca.cfg;执行:ccode_standard? ./rfdig? rfdig_seca.cfg ?>? 1.log
  3. 如图所示,脚本只替换了有关rfdig的寄存器,iomux,crg等没有替换:

  4. 如下例所示,脚本存放于case目录,我想修改rfdig/rfdig_tc017文件夹下的.c文件,参考所有rbus.cfg文件;
  5. 执行:ccode_standard ?./rfdig/rfdig_m33_tc017? all? > ?1.log

  6. 如图所示,脚本不仅替换了有关rfdig的寄存器,iomux,crg等都有替换:

  7. ?

5 脚本代码

#! /usr/bin/perl -w

#==========================================================
# PERL MODULE 
#==========================================================

use Cwd;
use File::Path;
use File::Find;

#==========================================================
# PERL FUNCTION GetOptions (get command parameters)
#==========================================================
$g_work_dir  = $ARGV[0];
$g_seca_fl   = $ARGV[1];

#our $work_dir  = 'rfdig_m33_tc017';

#==========================================================
# SCRIPT MAIN  
#==========================================================

our @seca_fl_list;
our @seca_name;
our @seca_type;
our @seca_width;
our @seca_init_value;
our @seca_with_field;
our @seca_field_list_bgn;
our @seca_field_list_end;
our @seca_field_name;
our @seca_field_width;
our @seca_field_bgn_idx;
our @seca_field_end_idx;

our $ccode_standard_mark = 0;

Main();

sub RegMem{
    @seca_name           = ();
    @seca_type           = ();
    @seca_width          = ();
    @seca_init_value     = ();
    @seca_with_field     = ();
    @seca_field_list_bgn = ();
    @seca_field_list_end = ();
    @seca_field_name     = ();
    @seca_field_width    = ();
    @seca_field_bgn_idx  = ();
    @seca_field_end_idx  = ();
    if (open(SECA_ID,"$g_seca_fl") or die "cannot open $g_seca_fl, no such file") {
        @seca_array = <SECA_ID>;
        my $i       = 0;
        my $j       = 0;
        my $rbus_ok = 0;
    
        foreach $seca_line (@seca_array) {
            $seca_line =~ s/[\n\r]*//g;
            if ($seca_line =~ m/^\s*field_(\S+)\s+\[(\S+)\]\s*$/) {
                if($rbus_ok eq 1) {
                    $seca_with_field[$i-1]     = 1 ;
                    $seca_field_list_bgn[$i-1] = $j;
                    $rbus_ok                   = 0 ;
                }
                $seca_field_list_end[$i-1] = $j;
                my $field_name_tmp   = "\L$1";
                my $field_name_size  = push(@seca_field_name ,$field_name_tmp);
                my $field_port_tmp   = $2;
                if ($field_port_tmp =~ m/(\d+):(\d+)/) {
                    push(@seca_field_bgn_idx,int($2));
                    push(@seca_field_end_idx,int($1));
                    push(@seca_field_width,(int($1)-int($2)+1));
                } else {
                    push(@seca_field_bgn_idx,int($field_port_tmp));
                    push(@seca_field_end_idx,int($field_port_tmp));
                    push(@seca_field_width,1);
                }
                #print ("seca_with_field[",$i-1,"]     = ",$seca_with_field[$i-1]     ,"\n");
                #print ("seca_field_list_bgn[",$i-1,"] = ",$seca_field_list_bgn[$i-1] ,"\n");
                #print ("seca_field_list_end[",$i-1,"] = ",$seca_field_list_end[$i-1] ,"\n");
                #print ("seca_field_name[",$j,"]       = ",$seca_field_name[$j]       ,"\n");
                #print ("seca_field_width[",$j,"]      = ",$seca_field_width[$j]      ,"\n");
                #print ("seca_field_bgn_idx[",$j,"]    = ",$seca_field_bgn_idx[$j]    ,"\n");
                #print ("seca_field_end_idx[",$j,"]    = ",$seca_field_end_idx[$j]    ,"\n");
                $j = $j + 1;
            }
            if ($seca_line =~ m/^\s*rbus_(\S+)\s+(\S+)\s+(\w+)\s+(\d+)('\w+)\s+(\S+)\s*$/) {
                #print ("before match string: ",$`,     "\n");
                #print ("match string       : ",$&,     "\n");
                #print ("after  match string: ",$',     "\n");
                #print ("seca_postfix       : ",$1,     "\n");
                #print ("seca_type          : ",$2,     "\n");
                #print ("seca_scope         : ",$3,     "\n");
                #print ("seca_width         : ",int($4),"\n");
                #print ("seca_init_value    : ",$5,     "\n");
                #print ("seca_prefix        : ",$6,     "\n");
                #print ("seca_name          : ","$6_$1","\n");
                $seca_name[$i]             = "\L$6_$1";
                $seca_type[$i]             = $2;
                $seca_width[$i]            = int($4);
                $seca_init_value[$i]       = $5;

                $seca_with_field[$i] = 0;
                $i = $i+1;
                $rbus_ok = 1;
            }
        }
    }
}

sub GenMacroDefine{
    #print "c file is              : $_[0]\n";
    #print "c macro_define file is : $_[1]\n";
    $ccode_standard_mark = 0;
    open(ORIEN_FILE,">>$_[1]");
        if (open(SOURCE_FILE,"$_[0]")) {
            @context = <SOURCE_FILE>;
            foreach $line (@context) {
                for ( $i = 0; $i < @seca_name; $i = $i +1) {
                    if ($seca_with_field[$i] eq 1) {
                        if ($line =~ m/^\s*(\w+)\[($seca_name[$i])\]\s*=\s*0x(\w+)\s*;.*$/) {
                            $ccode_standard_mark = 1;
                            print ORIEN_FILE "\n"."//"."$line";
                            my $seca_field_list_num  = $seca_field_list_end[$i] - $seca_field_list_bgn[$i] + 1;
                            for ($j = 0; $j < $seca_field_list_num; $j = $j+1) {
                                my $seca_field_width_tmp = $seca_field_width[$seca_field_list_bgn[$i]+$j];
                                my $seca_field_name_tmp  = "$seca_name[$i]"."_"."$seca_field_name[$seca_field_list_bgn[$i]+$j]";
                                my $seca_field_mask_tmp  = "$seca_field_name_tmp"."_mask ((1<<$seca_field_width[$seca_field_list_bgn[$i]+$j])-1)";
                                my $seca_field_shift_tmp = "$seca_field_name_tmp"."_shift $seca_field_bgn_idx[$seca_field_list_bgn[$i]+$j]";
                                my $seca_field_bit_tmp   = "$seca_field_name_tmp"."_bit (1<<$seca_field_bgn_idx[$seca_field_list_bgn[$i]+$j])";
                                if($seca_field_width_tmp eq 1){
                                    print ORIEN_FILE "#define $seca_field_bit_tmp\n";
                                } else {
                                    print ORIEN_FILE "#define $seca_field_mask_tmp\n";
                                    print ORIEN_FILE "#define $seca_field_shift_tmp\n";
                                }
                            }
                        }
                    }
                }
            }
            close SOURCE_FILE;
        }
    close ORIEN_FILE;
    system ("awk '!a[\$0]++' $_[1] > $_[1]_tmp \&\& mv -f $_[1]_tmp $_[1]");
}

sub AddMacroDefine{
    #print "old c file is : $_[0]\n";
    #print "new c file is : $_[1]\n";
    #print "add   file is : $_[2]\n";
    my $add_ready = 0;
    open(ORIEN_FILE,">$_[1]");
        if (open(SOURCE_FILE,"$_[0]")) {
            @context = <SOURCE_FILE>;
            foreach $line (@context) {
                if($line =~ m/^#include \"$_[2]\"$/) {

                } else {
                    print ORIEN_FILE "$line";
                }
                if ($add_ready eq 0) {
                    if ($line =~ m/^\s*\#include\s*\"\S+_define.c\"\s*$/) {
                        print ORIEN_FILE "\#include "."\"$_[2]\"\n";
                        $add_ready = 1;
                    }
                }
            }
            close SOURCE_FILE;
        }
    close ORIEN_FILE;
}

sub GenSubContent{
    #print "old c file is : $_[0]\n";
    #print "new c file is : $_[1]\n";
    open(ORIEN_FILE,">>$_[1]");
        if (open(SOURCE_FILE,"$_[0]")) {
            @context = <SOURCE_FILE>;
            foreach $line (@context) {
                my $line_match = 0;
                for ( $i = 0; $i < @seca_name; $i = $i + 1) {
                    if ($seca_with_field[$i] eq 1) {
                        if ($line =~ m/^(\s*)(\w+\[$seca_name[$i]\])\s*=\s*0x(\w+)\s*;.*$/) {
                            my $reg_value = hex($3);
                            if ($reg_value != 0) {
                                $line_match = 1;
                                print ORIEN_FILE "//"."$line";
                                my $seca_field_list_num = $seca_field_list_end[$i] - $seca_field_list_bgn[$i] + 1;
                                for ($j = 0; $j < $seca_field_list_num; $j = $j+1) {
                                    my $seca_field_name_tmp    = "$seca_name[$i]"."_"."$seca_field_name[$seca_field_list_bgn[$i]+$j]";
                                    my $seca_field_mask_tmp    = "$seca_field_name_tmp"."_mask";
                                    my $seca_field_value       = (($reg_value) >> ($seca_field_bgn_idx[$seca_field_list_bgn[$i]+$j])) & 
                                                                 ((1<<$seca_field_width[$seca_field_list_bgn[$i]+$j])-1);
                                    my $seca_field_width_tmp   = $seca_field_width[$seca_field_list_bgn[$i]+$j];
                                    my $seca_field_shift_tmp   = "$seca_field_name_tmp"."_shift";
                                    my $seca_field_bit_tmp     = "$seca_field_name_tmp"."_bit";
                                    #print ("seca_field_mask_value = ",((1<<$seca_field_width[$seca_field_list_bgn[$i]+$j])-1),";");
                                    #print ("reg_value = ",$reg_value,";");
                                    #print ("seca_field_value = $seca_field_value\n");
                                    if($j eq 0){
                                      if ($j eq ($seca_field_list_num - 1)) {
                                        if($seca_field_width_tmp eq 1) {
                                          printf ORIEN_FILE ("%s%-40s ((%-50s * 0x%-8x)    %-51s) ;\n","$1","$2 =","$seca_field_bit_tmp",$seca_field_value,"                     ");
                                        } else {
                                          printf ORIEN_FILE ("%s%-40s ((%-50s & 0x%-8x) << %-51s) ;\n","$1","$2 =","$seca_field_mask_tmp",$seca_field_value,"$seca_field_shift_tmp");
                                        }
                                      } else {
                                        if($seca_field_width_tmp eq 1) {
                                          printf ORIEN_FILE ("%s%-40s ((%-50s * 0x%-8x)    %-51s) |\n","$1","$2 =","$seca_field_bit_tmp",$seca_field_value,"                     ");
                                        } else {
                                          printf ORIEN_FILE ("%s%-40s ((%-50s & 0x%-8x) << %-51s) |\n","$1","$2 =","$seca_field_mask_tmp",$seca_field_value,"$seca_field_shift_tmp");
                                        }
                                      }
                                    } elsif($j eq ($seca_field_list_num - 1)) {                                                                                              
                                      if($seca_field_width_tmp eq 1) {
                                        printf ORIEN_FILE ("%s%-40s ((%-50s * 0x%-8x)    %-51s) ;\n","$1","    ","$seca_field_bit_tmp",$seca_field_value,"                     ");
                                      } else {
                                        printf ORIEN_FILE ("%s%-40s ((%-50s & 0x%-8x) << %-51s) ;\n","$1","    ","$seca_field_mask_tmp",$seca_field_value,"$seca_field_shift_tmp");
                                      }
                                    } else {                                                                                                                                 
                                      if($seca_field_width_tmp eq 1) {
                                        printf ORIEN_FILE ("%s%-40s ((%-50s * 0x%-8x)    %-51s) |\n","$1","    ","$seca_field_bit_tmp",$seca_field_value,"                     ");
                                      } else {
                                        printf ORIEN_FILE ("%s%-40s ((%-50s & 0x%-8x) << %-51s) |\n","$1","    ","$seca_field_mask_tmp",$seca_field_value,"$seca_field_shift_tmp");
                                      }
                                    }
                                }
                            }
                        }
                    }
                }
                if ($line_match eq 0) {
                    print ORIEN_FILE "$line";
                }
            }
            close SOURCE_FILE;
        }
    close ORIEN_FILE;
}

sub CopyFile{
    #print "source file name is $_[0]\n";
    #print "orien  file name is $_[1]\n";
    open(ORIEN_FILE,">$_[1]");
        if (open(SOURCE_FILE,"$_[0]")) {
            @context = <SOURCE_FILE>;
            foreach $line (@context) {
                print ORIEN_FILE "$line";
            }
            close SOURCE_FILE;
        }
    close ORIEN_FILE;
}

sub FindConfigFile{
    if (-f $File::Find::name) {
        if ($_ =~ m/^(\w+_sec[a-z]\.cfg)$/) {
            #print "find file is $1 \n";
            push(@seca_fl_list,$1); 
        }
    }
}

sub FindWanted{
    if (-f $File::Find::name) {
        if ($_ =~ m/(\S+)\.c$/) {
            print "process file: $File::Find::name \n";
            GenMacroDefine("$_","$1"."_macro_define.h");
            unlink "$1"."_macro_define.h";
            if($ccode_standard_mark eq 1) {
                unlink "$_".".tmp";
                GenSubContent("$_","$_".".tmp");
                #AddMacroDefine("$_".".tmp","$_","$1"."_macro_define.h");
                system ("mv -f $_.tmp $_");
                unlink "$_".".tmp";
            }
        }
    }
}

sub Main{
    my $cur_dir  = getcwd;
    my $proj_src = $ENV{PROJ_SRC};
    my $seca_dir = "$ENV{PROJ_SRC}/design/scripts/rbus_scripts/rbus_cfg/";
    if($g_seca_fl eq "all") {
        chdir($seca_dir) or die "$seca_dir was not found!!! please gen_rbus firstly.";
        find(\&FindConfigFile,"$seca_dir");
    } else {
        push(@seca_fl_list,$g_seca_fl);
    }
    #for ($i = 0; $i < @seca_fl_list; $i = $i+1) {
    #    print "seca_fl_list[$i] = $seca_fl_list[$i] \n";
    #}
    for ($z = 0; $z < @seca_fl_list; $z = $z + 1) {
        print "read config_file : $seca_fl_list[$z]\n";
        $g_seca_fl = "$seca_dir"."$seca_fl_list[$z]";
        RegMem();
        chdir($cur_dir) or die "$cur_dir does not exist!!!";
        find(\&FindWanted,"$cur_dir/$g_work_dir");
    }
}

文章来源:https://blog.csdn.net/ZhangShuHuai11/article/details/135571625
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。