|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
用Perl脚本实现HEX文件转coe文件,在Linux下面运行正确,但是在windows下却无法正确
运行,我是在windows安装了ActivePerl_5.16.2.3010812913。
(之前用了两个小工具,hex2bin和bin2coe,然后还需要把生成的coe文件高低字节互换,再
去掉高两位,觉得很麻烦,就直接下了脚本一步到位,coe是用于FPGA测试用的程序文本)
代码如下,初学Perl,请教高手为何在windows下无法正确运行?谢谢!
#####################################################
#!/usr/bin/perl
my $address_old=0;
my @data_list;
my $hex_file = glob '.HEX';
open HEX,'<',"$hex_file";
(my $coe_file = $hex_file) =~ s/.HEX/.coe/;
open COE,'>',"$coe_file";
print COE "MEMORY_INITIALIZATION_RADIX=2;\n";
print COE "MEMORY_INITIALIZATION_VECTOR=\n";
while(<HEX>){
chomp;
if(/\A[0-9A-Z]{2})([0-9A-Z]{4})00([0-9A-Z]*)([0-9A-Z]{2})\Z/){
my @data_temp = split //,"$3";
my $instr_num = hex($1)/2;
my $address_new = hex($2)/2;
for(my $j=address_old;$j<$address_new;$j++){
push (@data_list,"0"x 14);
}
for(my $i=0;$i<$instr_num;$i++){
my $m1 = sprintf "%04b",hex(shift(@data_temp));
my $m0 = sprintf "%04b",hex(shift(@data_temp));
my $n1 = sprintf "%02b",hex(shift(@data_temp));
my $n0 = sprintf "%04b",hex(shift(@data_temp));
my $data = join "",$n1,$n0,$m1,$m0;
push (@data_list,$data);
}
$address_old = $address_new + $instr_num;
}
}
my $data_list = @data_list;
for(my $x=0;$x<$data_list;$x++){
if($x==@data_list-1){
print COE "$data_list[$x]",";\n";
}else{
print COE "$data_list[$x]",",\n";
}
}
close HEX;
close COE;
exit; |
|