sed は Verilog バスを別々のポートに分割します。

sed は Verilog バスを別々のポートに分割します。

Verilog Bus特定のコンテンツを別々の分割形式に変換するには、またはコマンドを使用したいと思います。sedawk

入力する

module test ( temp_bus[3:0], temp_B[1:0] )
    input [3:0] temp_bus;
    output [1:0] temp_B;
endmodule

出力

module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0], temp_B[1], temp_B[0])
   input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0];
   output temp_B[1], temp_B[0];
endmodule

Edit1:宣言が複数ある場合

module test ( temp_bus[3:0], temp_B[1:0] , temp_C[1:0] )
    input [3:0] temp_bus;
    output [1:0] temp_B , temp_c;
endmodule

結果は次のようになります output temp_B[1], temp_B[0], temp_C[1], temp_C[0] ;

カスほぼ最良の解決策が提供された。

ベストアンサー1

1つの方法は次のとおりですperl

(修正はサンプル入力をすべて処理します。また、内部のセミコロンが[]Markdown構文の強調表示と混同されないようです。)

#! /usr/bin/perl

use strict;

sub expand {
  my ($name,$start,$stop) = @_;
  my $step = ( $start < $stop ? 1 : -1);
  my @names=();

  my $i = $start;
  while ($i ne $stop + $step) {
    push @names, "$name\[$i\]";
    $i += $step;
  }
  return @names;
};

while(<>) {
  chomp;
  s/([(),;])/ $1/g;   # add a space before any commas, semi-colons, and
                      #  parentheses, so they get split into separate fields.

  my @l=();           # array to hold the output line as it's being built

  my @line = split ;  # split input line into fields, with 1-or-more
                      # whitespace characters (spaces or tabs) between each
                      # field.

  my $f=0;            # field counter

  while ($f < @line) {
    if ( $line[$f] =~ m/module/io ) {
        push @l,$line[$f++];
        while ($f < @line) {
            if ( $line[$f] =~ m/^(.*)\[(\d+):(\d+)\]$/o ) {
                # expand [n:n] on module line
                push @l, join(", ",expand($1,$2,$3));
            } else { 
                push @l, $line[$f]
            };
            $f++;
        };
    } elsif ($line[$f] =~ m/^(?:input|output)$/io) {
        # use sprintf() to indent first field to 10 chars wide.
        $line[$f] = sprintf("%10s",$line[$f]);
        push @l, $line[$f++];;

        my @exp = ();
        while ($f < @line) {
            if ( $line[$f] =~ m/^\[(\d+):(\d+)\]$/o ) {
                # extract and store [n:n] on input or output lines
                @exp=($1,$2);
            } elsif ( $line[$f] =~ m/^\w+$/io) {
                # expand "word" with [n:n] on input or output lines
                push @l,join(", ",expand($line[$f],@exp));
            } else {
                push @l, $line[$f];
            };
            $f++;
        };

    } else {
      # just append everything else to the output @l array
      push @l, $line[$f];
    };
    $f++;
  }
  print join(" ",@l),"\n";
}

出力:

$ ./jigar.pl ./jigar.txt 
module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] , temp_B[1], temp_B[0] ) 
     input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] ; 
    output temp_B[1], temp_B[0] ; 
endmodule 

2番目のサンプルの出力:

$ ./jigar2.pl jigar2.txt 
module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] , temp_B[1], temp_B[0] , temp_C[1], temp_C[0] )
     input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] ;
    output temp_B[1], temp_B[0] , temp_c[1], temp_c[0] ;
endmodule

おすすめ記事