hex2rom.pl 792 B

12345678910111213141516171819202122232425262728293031
  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. $endian = 1;
  9. while (<STDIN>) {
  10. $line++;
  11. $count = (hex substr($_, 1, 2));
  12. $addr = (hex substr($_, 3, 4));
  13. $type = (hex substr($_, 7, 2));
  14. if($type == 2 || $type == 4) {
  15. $addrhi = hex(substr($_, 9, 4)) << ($type == 2 ? 4 : 16);
  16. # printf "type=%d, addr=%x, line=%d\n", $type, $addrhi, $line;
  17. }
  18. if($type == 0 && $addrhi >= $baseaddr && $addrhi <= $endaddr) {
  19. for($x=0; $x<$count; $x++) {
  20. $rom[$addrhi - $baseaddr + $addr + $x] = (hex substr($_, 9+2*$x, 2)) ;
  21. }
  22. }
  23. }
  24. for($x=0; $x<@rom; $x+=$bits/8) {
  25. for($i = 0;$i < $bits/8;$i++) {
  26. printf ("%02x", $endian ? $rom[$x+$bits/8-1-$i] : $rom[$x+$i]);
  27. }
  28. print "\n";
  29. }