small arrange

git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/twl_wrapsdk/trunk@245 4ee2a332-4b2b-5046-8439-1ba90f034370
This commit is contained in:
yutaka 2007-08-23 00:57:36 +00:00
parent 61cd321aa6
commit 4044247f5c
2 changed files with 180 additions and 189 deletions

View File

@ -227,124 +227,125 @@ $comp_target =~ s/\r?\n/\r\n/g;
my @functions = ({name => "", data => "", comment => "", declare => ""}); my @functions = ({name => "", data => "", comment => "", declare => ""});
# 定義ファイルデータベース # 定義ファイルデータベース
my %regmap; # map register/regmap name to the address/mask my %regmap; # map register/regmap name to the address/mask
# 定義ファイルデータベースの生成 # 定義ファイルデータベースの生成
sub regmap_init { sub regmap_init {
my %mcu; my %mcu;
open IN, $_[0] or die; open IN, $_[0] or die;
# search space # search space
while(<IN>) { while(<IN>) {
if (/^\[ADDR_SPACE\]/) { if (/^\[ADDR_SPACE\]/) {
while (<IN>) { while (<IN>) {
last if (/^\[END\]/); last if /^\[END\]/;
if (/^([\w\d]+)\s*=\s*\{MCU, (\d+),/) { # only MCU is supported if (/^([\w\d]+)\s*=\s*\{MCU, (\d+),/) { # only MCU is supported
$mcu{$1} = $2; $mcu{$1} = $2;
} }
} }
} }
if (/^\[REGISTERS\]/) { if (/^\[REGISTERS\]/) {
my $lastreg = ""; my $lastreg = "";
while (<IN>) { while (<IN>) {
last if (/\[END\]/); last if /\[END\]/;
if (/(^[\w\d]+)\s*=\s*\{(\w+?),\s*([\w\d]+?),/) { # address entry if (/(^[\w\d]+)\s*=\s*\{(\w+?),\s*([\w\d]+?),/) { # address entry
if ($mcu{$3}) { # MCU if ($mcu{$3}) { # MCU
$regmap{$1}{0} = sprintf("0xA%1X%02X", $mcu{$3}, hex($2)); $regmap{$1}{0} = sprintf("0xA%1X%02X", $mcu{$3}, hex($2));
} }
$lastreg = $1; $lastreg = $1;
} }
elsif (/^\s*\{([\w\d]+?),\s*(\w+?),/) { # bitfield entry elsif (/^\s*\{([\w\d]+?),\s*(\w+?),/) { # bitfield entry
$regmap{$lastreg}{$1} = $2; $regmap{$lastreg}{$1} = $2;
} }
} }
} }
} }
close IN; close IN;
# use Data::Dumper; # use Data::Dumper;
# print Dumper(\%mcu, \%regmap); # print Dumper(\%mcu, \%regmap);
} }
# API名の整形 # API名の整形
sub name_conv { sub name_conv {
$_ = $_[0]; $_ = $_[0];
s/\<o\>/Option/g; s/\s*\<o\>\s*//g;
s/\%/Percent/g; # s/\<o\>/Option/g;
s/\-/Minus/g; s/\%/Percent/g;
s/\+/Plus/g; s/\-/Minus/g;
s/\>/To/g; s/\+/Plus/g;
s/[\s\.\:\=\*\/\(\)]+/_/g; s/\>/To/g;
return $_; s/[\s\.\:\=\*\/\(\)]+/_/g;
return $_;
} }
# 各種コマンドのC言語への変換 # 各種コマンドのC言語への変換
sub func_conv { sub func_conv {
my($key, $value, $comment) = @_; my($key, $value, $comment) = @_;
my @v = split /\s*\,\s*/, $value; # split value my @v = split /\s*\,\s*/, $value; # split value
$comment = " " . $comment if ($comment); # insert spaces $comment = " " . $comment if $comment; # insert spaces
if ($key eq "LOAD") { if ($key eq "LOAD") {
return sprintf($call_format, name_conv($value), $comment); return sprintf($call_format, name_conv($value), $comment);
} }
elsif ($key eq "REG") { # address, value elsif ($key eq "REG") { # address, value
return sprintf($reg_format, $v[0], $v[1], $comment); return sprintf($reg_format, $v[0], $v[1], $comment);
} }
elsif ($key eq "BITFIELD") { # address, mask, set/clear elsif ($key eq "BITFIELD") { # address, mask, set/clear
if ($v[2]) { if ($v[2]) {
return sprintf($set_format, $v[0], $v[1], $comment); return sprintf($set_format, $v[0], $v[1], $comment);
} else { } else {
return sprintf($clear_format, $v[0], $v[1], $comment); return sprintf($clear_format, $v[0], $v[1], $comment);
} }
} }
elsif ($key eq "DELAY") { # msec elsif ($key eq "DELAY") { # msec
return sprintf($delay_format, $v[0], $comment); return sprintf($delay_format, $v[0], $comment);
} }
elsif ($key eq "POLL_REG") { # address, mask, condition, delay, timeout elsif ($key eq "POLL_REG") { # address, mask, condition, delay, timeout
$v[3] =~ s/DELAY\s*=\s*//; $v[3] =~ s/DELAY\s*=\s*//;
$v[4] =~ s/TIMEOUT\s*=\s*//; $v[4] =~ s/TIMEOUT\s*=\s*//;
${$functions[$#functions]}{declare} = " int timeout;\r\n"; ${$functions[$#functions]}{declare} = " int timeout;\r\n";
return sprintf($pollreg_format, $v[0], @v[1..4], $comment); return sprintf($pollreg_format, $v[0], @v[1..4], $comment);
} }
elsif ($key eq "VAR8") { # page, address, value elsif ($key eq "VAR8") { # page, address, value
return sprintf($mcu_format, sprintf("0xA%1X%02X", $v[0], hex($v[1])), $v[2], $comment); return sprintf($mcu_format, sprintf("0xA%1X%02X", $v[0], hex($v[1])), $v[2], $comment);
} }
elsif ($key eq "VAR") { # page, address, value elsif ($key eq "VAR") { # page, address, value
return sprintf($mcu_format, sprintf("0x2%1X%02X", $v[0], hex($v[1])), $v[2], $comment); return sprintf($mcu_format, sprintf("0x2%1X%02X", $v[0], hex($v[1])), $v[2], $comment);
} }
elsif ($key eq "FIELD_WR") { # name, value OR name, mask, set/clear elsif ($key eq "FIELD_WR") { # name, value OR name, mask, set/clear
if ($v[2] eq "") { if ($v[2] eq "") {
return sprintf($mcu_format, $regmap{$v[0]}{0}, $v[1], $comment); return sprintf($mcu_format, $regmap{$v[0]}{0}, $v[1], $comment);
} }
if ($v[2]) { if ($v[2]) {
return sprintf($fieldset_format, $regmap{$v[0]}{0}, $regmap{$v[0]}{$v[1]}, $comment); return sprintf($fieldset_format, $regmap{$v[0]}{0}, $regmap{$v[0]}{$v[1]}, $comment);
} else { } else {
return sprintf($fieldclear_format, $regmap{$v[0]}{0}, $regmap{$v[0]}{$v[1]}, $comment); return sprintf($fieldclear_format, $regmap{$v[0]}{0}, $regmap{$v[0]}{$v[1]}, $comment);
} }
} }
elsif ($key eq "POLL_FIELD") { # name, condition, delay, timeout elsif ($key eq "POLL_FIELD") { # name, condition, delay, timeout
$v[2] =~ s/DELAY\s*=\s*//; $v[2] =~ s/DELAY\s*=\s*//;
$v[3] =~ s/TIMEOUT\s*=\s*//; $v[3] =~ s/TIMEOUT\s*=\s*//;
${$functions[$#functions]}{declare} = " int timeout;\r\n"; ${$functions[$#functions]}{declare} = " int timeout;\r\n";
return sprintf($pollfield_format, $regmap{$v[0]}{0}, @v[1..3], $comment); return sprintf($pollfield_format, $regmap{$v[0]}{0}, @v[1..3], $comment);
} }
return "// IGNORED: " . $key . "=" . $value . $comment . "\r\n"; # 未対応コマンド return "// IGNORED: " . $key . "=" . $value . $comment . "\r\n"; # 未対応コマンド
} }
# 後処理 (上位API化処理) # 後処理 (上位API化処理)
sub comp_func { sub comp_func {
$_[0] =~ s/$comp_target/sprintf($mcu_format, $1, $3, $2 . $4)/eg; $_[0] =~ s/$comp_target/sprintf($mcu_format, $1, $3, $2 . $4)/eg;
return $_[0]; return $_[0];
} }
#ここからメイン #ここからメイン
# 引数チェック # 引数チェック
die "USAGE: $0 INFILE [OUTFILE]\n" if ($#ARGV != 1 and $#ARGV != 0); die "USAGE: $0 INFILE [OUTFILE]\n" if $#ARGV != 1 and $#ARGV != 0;
# 各種初期化 # 各種初期化
regmap_init($register_sdat); regmap_init($register_sdat);
my $infile = $ARGV[0]; my $infile = $ARGV[0];
my $outfile = $ARGV[1]; my $outfile = $ARGV[1];
($outfile = $infile) =~ s/\.ini$/.autogen.c/ unless ($outfile); ($outfile = $infile) =~ s/\.ini$/.autogen.c/ unless $outfile;
# 入出力ファイルのオープン (両方オープンしておく) # 入出力ファイルのオープン (両方オープンしておく)
open IN, $infile or die "Cannot open the input file!\n"; open IN, $infile or die "Cannot open the input file!\n";
@ -352,28 +353,28 @@ open OUT, ">", $outfile or die "Cannot open the output file!\n";
# 入力処理 # 入力処理
while (<IN>) { while (<IN>) {
my $comment = ""; my $comment = "";
s/[\r\n]+$//; # delete \r and/or \n s/[\r\n]+$//; # delete \r and/or \n
if (s/(\;|\/\/)(.*)//) { # コメント抽出 if (s/(\;|\/\/)(.*)//) { # コメント抽出
$comment = $1 . $2; $comment = $1 . $2;
} }
if (/\s*\[(.+)\]/) { # API検出 if (/\s*\[(.+)\]/) { # API検出
push @functions, {name => name_conv($1), data => "", comment => "", declare => ""}; push @functions, {name => name_conv($1), data => "", comment => "", declare => ""};
} }
elsif (/\s*(.+?)\s*\=\s*(.+?)\s*$/) { # コマンド検出 elsif (/\s*(.+?)\s*\=\s*(.+?)\s*$/) { # コマンド検出
# コマンドの手前にコメントがあった場合は、dataに移動させる # コマンドの手前にコメントがあった場合は、dataに移動させる
${$functions[$#functions]}{data} .= ${$functions[$#functions]}{comment}; ${$functions[$#functions]}{data} .= ${$functions[$#functions]}{comment};
${$functions[$#functions]}{comment} = ""; ${$functions[$#functions]}{comment} = "";
# コマンドをC言語に変換してdataに追加 # コマンドをC言語に変換してdataに追加
${$functions[$#functions]}{data} .= func_conv($1, $2, $comment); ${$functions[$#functions]}{data} .= func_conv($1, $2, $comment);
} }
elsif (/\S+/) { # 未知入力行検出 (コメント扱い (エラーにすべきかも)) elsif (/\S+/) { # 未知入力行検出 (コメント扱い (エラーにすべきかも))
warn "UNKNOWN STATEMENT: <<", $_, ">>\n"; warn "UNKNOWN STATEMENT: <<", $_, ">>\n";
${$functions[$#functions]}{comment} .= "// " . $_ . $comment . "\r\n"; ${$functions[$#functions]}{comment} .= "// " . $_ . $comment . "\r\n";
} }
elsif ($comment) { # コメントのみ elsif ($comment) { # コメントのみ
${$functions[$#functions]}{comment} .= "// " . $comment . "\r\n"; ${$functions[$#functions]}{comment} .= "// " . $comment . "\r\n";
} }
} }
# 入力処理終了 # 入力処理終了
close IN; close IN;
@ -382,25 +383,25 @@ close IN;
#print Dumper(\@functions); #print Dumper(\@functions);
# 出力処理 # 出力処理
$outfile =~ s/^.*[\\\/]//; # get basename to print $outfile =~ s/^.*[\\\/]//; # get basename to print
printf OUT $file_head_format, $outfile; # ファイルヘッダ出力 printf OUT $file_head_format, $outfile; # ファイルヘッダ出力
foreach my $func ( @functions ) { # 検出済みAPIの宣言 foreach my $func ( @functions ) { # 検出済みAPIの宣言
printf OUT $declare_format, $$func{name} if ($$func{name}); printf OUT $declare_format, $$func{name} if $$func{name};
} }
printf OUT "\r\n"; printf OUT "\r\n";
foreach my $func ( @functions ) { # API本体の出力 foreach my $func ( @functions ) { # API本体の出力
if ($$func{name}) { # 最初のAPIより前の場合を除く if ($$func{name}) { # 最初のAPIより前の場合を除く
printf OUT $func_head_format, $$func{name}; # API名開き括弧の出力 printf OUT $func_head_format, $$func{name}; # API名開き括弧の出力
print OUT "#pragma unused(camera)\r\n" unless ($$func{data} =~ /camera/); # warning防止 print OUT "#pragma unused(camera)\r\n" unless $$func{data} =~ /\bcamera\b/; # warning防止
print OUT $$func{declare}, "\r\n" if ($$func{declare}); # 変数宣言 (if any) print OUT $$func{declare}, "\r\n" if $$func{declare}; # 変数宣言 (if any)
} }
print OUT comp_func($$func{data}); # 本体の後処理+出力 print OUT comp_func($$func{data}); # 本体の後処理+出力
if ($$func{name}) { # 最初のAPIより前の場合を除く if ($$func{name}) { # 最初のAPIより前の場合を除く
printf OUT $func_foot_format ; # 閉じ括弧出力 printf OUT $func_foot_format ; # 閉じ括弧出力
} }
print OUT $$func{comment}; # 最後のコメントの出力 (たいてい次のAPIのためのもの) print OUT $$func{comment}; # 最後のコメントの出力 (たいてい次のAPIのためのもの)
} }
printf OUT $file_foot_format; # ファイルフッタ出力 printf OUT $file_foot_format; # ファイルフッタ出力
# 出力処理終了 # 出力処理終了
close OUT; close OUT;

View File

@ -71,47 +71,44 @@ $multi_foot_format =~ s/\r?\n/\r\n/g;
my $row_nums = 8; my $row_nums = 8;
sub sprint_command { sub sprint_command {
my($addr, @value) = @_; my($addr, @value) = @_;
if (@value == 1) { # シングルライトは別枠 return sprintf($single_format, $addr, $value[0]) if @value == 1; # シングルライトは別枠
return sprintf($single_format, $addr, $value[0]); my $result = $multi_head_format; # これ以降はバーストライト
} for (my $i = 0; $i < @value; $i++) {
my $result = $multi_head_format; # これ以降はバーストライト if ($i % $row_nums == 0) {
for (my $i = 0; $i < @value; $i++) $result .= " " ;
{ } else {
if (($i % $row_nums) == 0) { $result .= " " ;
$result .= " " ; }
} else { $result .= sprintf("0x%02X,", $value[$i]);
$result .= " " ; if ($i % $row_nums == $row_nums - 1) {
} $result .= "\r\n";
$result .= sprintf("0x%02X,", $value[$i]); }
if (($i % $row_nums) == ($row_nums - 1) ) { }
$result .= "\r\n"; if (@value % $row_nums != 0) {
} $result .= "\r\n";
} }
if ((@value % $row_nums) != 0) { $result .= sprintf($multi_foot_format, $addr, scalar(@value));
$result .= "\r\n"; return $result;
}
$result .= sprintf($multi_foot_format, $addr, scalar(@value));
return $result;
} }
#ここからメイン
# #
# データはいったんキャッシュして、連続アドレスをバーストライトに書き換える # データはいったんキャッシュして、連続アドレスをバーストライトに書き換える
# #
my $comment; # コメントキャッシュ my $comment; # コメントキャッシュ
my @cache; # キャッシュデータ my @cache; # キャッシュデータ
my $start = -1; # キャッシュの先頭アドレス my $start = -1; # キャッシュの先頭アドレス
my @output; # 出力データ my @output; # 出力データ
#ここからメイン
# 引数チェック # 引数チェック
die "USAGE: $0 INFILE [OUTFILE]\n" if ($#ARGV != 1 and $#ARGV != 0); die "USAGE: $0 INFILE [OUTFILE]\n" if $#ARGV != 1 and $#ARGV != 0;
# 各種初期化 # 各種初期化
my $infile = $ARGV[0]; my $infile = $ARGV[0];
my $outfile = $ARGV[1]; my $outfile = $ARGV[1];
($outfile = $infile) =~ s/\.dat$/.autogen.c/ unless ($outfile); ($outfile = $infile) =~ s/\.dat$/.autogen.c/ unless $outfile;
# 入出力ファイルのオープン (両方オープンしておく) # 入出力ファイルのオープン (両方オープンしておく)
open IN, $infile or die "Cannot open the input file!\n"; open IN, $infile or die "Cannot open the input file!\n";
@ -119,42 +116,35 @@ open OUT, ">", $outfile or die "Cannot open the output file!\n";
# 入力処理 # 入力処理
while (<IN>) { while (<IN>) {
s/[\r\n]+$//; # delete \r and/or \n s/[\r\n]+$//; # delete \r and/or \n
s|\#|// |g; # change comment sign s|\#|// |g; # change comment sign
if (s|(//.*)||) { # コメント抽出 $comment .= " $1\r\n" if s|(//.*)||; # コメント抽出(独立行として出力予定)
$comment .= " $1\r\n"; # 独立行として出力予定
}
if (/\s*([\w\d]{2})\s+([\w\d]{2})/) { # データ抽出 if (/\s*([\w\d]{2})\s+([\w\d]{2})/) { # データ抽出
my ($addr, $value) = (hex($1), hex($2)); my ($addr, $value) = (hex($1), hex($2));
if ($addr != $start + @cache) # アドレスが連続していないなら if ($addr != $start + @cache) { # アドレスが連続していないなら
{ # 直前までを出力 push @output, sprint_command($start, @cache) if (@cache); # 直前までを出力
push @output, sprint_command($start, @cache) if (@cache); @cache = ($value); # 最新の値だけのエントリにする
@cache = ($value); # 最新の値だけのエントリにする $start = $addr;
$start = $addr; } else { # アドレスが連続しているなら
} else { # アドレスが連続しているなら push @cache, $value; # 値を追記
push @cache, $value; # 値を追記 }
} push @output, $comment; # このタイミングでコメント出力
push @output, $comment; # このタイミングでコメント出力 $comment = "";
$comment = ""; }
} elsif (/\S/) { # 未知入力行検出 (エラーにすべきかも)
elsif (/\S/) { # 未知入力行検出 (エラーにすべきかも) warn "UNKNOWN STATEMENT: <<", $_, ">>\n";
warn "UNKNOWN STATEMENT: <<", $_, ">>\n"; }
}
} }
# 最終行処理 # 最終行処理
push @output, sprint_command($start, @cache) if (@cache); push @output, sprint_command($start, @cache) if @cache;
# 入力処理終了 # 入力処理終了
close IN; close IN;
for (my $i = 0; $i < @output; $i++) {
$output[$i] =~ s/\r?\n/\r\n/g;
}
# 出力処理 # 出力処理
$outfile =~ s/^.*[\\\/]//; # get basename to print $outfile =~ s/^.*[\\\/]//; # get basename to print
printf OUT $file_head_format, $outfile; # ファイルヘッダ出力 printf OUT $file_head_format, $outfile; # ファイルヘッダ出力
print OUT @output; print OUT @output;
printf OUT $file_foot_format; # ファイルフッタ出力 printf OUT $file_foot_format; # ファイルフッタ出力
# 出力処理終了 # 出力処理終了
close OUT; close OUT;