ttasm2exec.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/perl -W
  2. use strict;
  3. # Trivial program to convert a TI-68k assembly file (.??z) to a TI-BASIC Exec string.
  4. # Copyright (C) 2009 Lionel Debroux.
  5. package main;
  6. my $length;
  7. my $filecontents;
  8. my $unpackedfilecontents;
  9. ( ($#ARGV == 0) # A single argument...
  10. && (($ARGV[0] =~ m#.89z$#) || ($ARGV[0] =~ m#.9xz$#) || ($ARGV[0] =~ m#.v2z$#)) # ... which ends with {89z,9xz,v2z} and ...
  11. && (-f $ARGV[0]) # ... which is a path representing an actual file.
  12. )
  13. or die "Usage: ttasm2exec.pl <infile.{89z,9xz,v2z}>\n";
  14. open(INFILE, $ARGV[0]) or die "Can't open $ARGV[0]: $!";
  15. read(INFILE, $filecontents, -s INFILE);
  16. close(INFILE);
  17. if ((index ($filecontents, "**TI89**") != 0) && (index ($filecontents, "**TI92P*") != 0)) {
  18. die "$ARGV[0] doesn't seem to be a file suitable for TI-68k calculators";
  19. }
  20. do {
  21. use bytes;
  22. # Get file size in bytes.
  23. $length = length($filecontents);
  24. print "Input file is $length bytes long (of which 91 bytes will be stripped).\n";
  25. if ($length <= 93) { # 86 bytes for the header, 2 size bytes, 2 bytes for the empty relocation, 1 byte for the tag and 2 bytes for the trailing checksum.
  26. die "$ARGV[0] is too short to be a valid ASM file";
  27. }
  28. $length -= 90;
  29. $filecontents = substr($filecontents, 88, $length);
  30. $length *= 2; # Number of hex digits is twice that of bytes.
  31. $unpackedfilecontents = unpack ("H[$length]", $filecontents);
  32. if (rindex($unpackedfilecontents, "f3") != $length - 2) {
  33. warn "$ARGV[0] doesn't seem to be an assembly file, the Exec string that will be generated is unlikely to work !";
  34. }
  35. $length = length($unpackedfilecontents);
  36. $unpackedfilecontents = substr($unpackedfilecontents, 0, $length - 2);
  37. print "Exec \"$unpackedfilecontents\"\n";
  38. } while (0);