hex2mif.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/perl
  2. # 2005 - David Grant.
  3. # Take an ihex input from STDIN, and write a .mif file to STDOUT
  4. # This script could probably be implemented with something like:
  5. # $#!@_%^$@%$@%$_!^$@#^@%$#@_%$@^&!%$_!%!%&$*(#^#@%^)
  6. # But I perfer the somewhat readable version.
  7. # Flow from within the Nios2 SDK Shell:
  8. # nios2-elf-as file.asm -o file.o
  9. # nios2-elf-objcopy file.o --target ihex file.hex
  10. # cat file.hex | perl hex2mif.pl > file.mif
  11. sub conv {
  12. my ($in) = @_;
  13. # Changes endianness
  14. # $out = substr($in,6,2).substr($in,4,2).substr($in,2,2).substr($in,0,2);
  15. $out = $in;
  16. return hex $out;
  17. }
  18. my @code = ();
  19. $hiaddr = 0;
  20. while (<STDIN>) {
  21. $l = $_;
  22. $count = (hex substr($l, 1, 2)) / 4;
  23. $addr = (hex substr($l, 3, 4)) / 4;
  24. $type = (hex substr($l, 7, 2));
  25. last if $type eq 1;
  26. next if $type eq 5; # ignore record type 5
  27. if ($type eq 4) { # upper 16 bits of address, topmost 2 bits are bogus
  28. $hiaddr = ((hex substr($l, 9, 4)) & 0x3fff) << 16;
  29. next;
  30. }
  31. if ($type eq 0) { # actual data
  32. $l = substr($l, 9, $count*8);
  33. for($x=0; $x<$count; $x++) {
  34. $sstr = substr($l, 8*$x, 8);
  35. #chomp(chomp($sstr));
  36. #$sstr =~ s/\R//;
  37. $sstr = $sstr.'00000000';
  38. $sstr = substr($sstr, 0, 8);
  39. $code[$hiaddr + $addr + $x] = conv($sstr);
  40. }
  41. next;
  42. }
  43. printf("Unknown Intel hex record: %s\n", $l);
  44. }
  45. for($x=0; $x<@code; $x++) {
  46. printf("%08x\n", $code[$x]);
  47. }