hex2rom.pl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/perl
  2. my @rom = ();
  3. die "missing start and end address" if(@ARGV < 2);
  4. $addrhi = 0;
  5. $baseaddr = hex($ARGV[0]);
  6. $endaddr = hex($ARGV[1]);
  7. $bits = 32;
  8. $bits = $ARGV[2] if(@ARGV > 2);
  9. $endian = 1;
  10. $inputfile_addr = $ARGV[3];
  11. print "\input: $inputfile_addr\n";
  12. $outputfile_addr =$ARGV[4];
  13. print "\output: $outputfile_addr\n";
  14. open(DATA,$inputfile_addr);
  15. while(<DATA>){
  16. $line++;
  17. $count = (hex substr($_, 1, 2));
  18. $addr = (hex substr($_, 3, 4));
  19. $type = (hex substr($_, 7, 2));
  20. if($type == 2 || $type == 4) {
  21. $addrhi = hex(substr($_, 9, 4)) << ($type == 2 ? 4 : 16);
  22. printf "type=%d, addr=%x, line=%d\n", $type, $addrhi, $line;
  23. }
  24. if($type == 0 && $addrhi >= $baseaddr && $addrhi <= $endaddr) {
  25. for($x=0; $x<$count; $x++) {
  26. $rom[$addrhi - $baseaddr + $addr + $x] = (hex substr($_, 9+2*$x, 2)) ;
  27. }
  28. }
  29. }
  30. #删除目标文件
  31. unlink($outputfile_addr);
  32. #创建目标文件
  33. $outputfile_addr =$ARGV[4];
  34. $outputfile_addr = '+>'.$outputfile_addr;
  35. print "$outputfile_addr\n";
  36. open(DATA1,$outputfile_addr) or die "des file open fail,$!";
  37. if($bits == 1) {
  38. for($x=0; $x<@rom; $x++) {
  39. printf ("%04x\n", $rom[$x]);
  40. }
  41. print "\n";
  42. } else {
  43. for($x=0; $x<@rom; $x+=$bits/8) {
  44. for($i = 0;$i < $bits/8;$i++) {
  45. $tmp = sprintf("%02x",($endian ? $rom[$x+$bits/8-1-$i] : $rom[$x+$i])),
  46. print DATA1 "$tmp";
  47. # printf "$tmp";
  48. }
  49. print DATA1 "\n";
  50. # printf "\n";
  51. }
  52. }
  53. $inputfile_addr = $ARGV[3];
  54. $outputfile_addr =$ARGV[4];
  55. close(inputfile_addr);
  56. close(outputfile_addr);