conv_fastlz.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os
  2. import sys
  3. import time
  4. import shutil
  5. LEN = 2 ** 16
  6. huffman = False
  7. TARGET = os.getcwd()
  8. SOURCE = sys.argv[1]
  9. NAME = os.path.basename(sys.argv[1])
  10. COMPRESSED = NAME + ".fastlz"
  11. LOC = os.path.dirname(os.path.realpath(__file__))
  12. FASTLZ = os.path.join(LOC, "..", "tools", "fastlz", "pack")
  13. os.system("%s %s %s" % (FASTLZ, SOURCE, COMPRESSED))
  14. data = open(COMPRESSED).read()
  15. os.unlink(COMPRESSED)
  16. fastlz_size = len(data)
  17. cfile = open("/tmp/loader.c", "w")
  18. hfile = open("/tmp/loader.h", "w")
  19. parts = []
  20. cnt = len(data) / ((2 ** 15) - 1)
  21. r = len(data) - (cnt * ((2 ** 15) - 1))
  22. for i in range(0, cnt):
  23. parts.append(((2 ** 15) - 1))
  24. parts.append(r)
  25. hfile.write('''/*
  26. File: %s
  27. Time: %s
  28. */
  29. #ifndef __FIFO_H__
  30. #define __FIFO_H__
  31. #define LOADER_NAME "%s"
  32. #define LOADER_COMPRESS "FASTLZ"
  33. #define ROM_FASTLZ_SIZE %i
  34. #define ROM_BUFFER_CNT %i
  35. ''' % (
  36. os.path.basename(SOURCE),
  37. time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()),
  38. NAME,
  39. fastlz_size,
  40. len(parts)
  41. ))
  42. for idx, val in enumerate(parts):
  43. hfile.write('#define ROM_BUFFER_SIZE%02i %i\n' % (idx + 1, val))
  44. hfile.write('\n#endif\n')
  45. hfile.close()
  46. cfile.write('''/*
  47. File: %s
  48. Time: %s
  49. */
  50. #include <avr/pgmspace.h>
  51. #include <loader.h>
  52. ''')
  53. addr = 0
  54. for idx, val in enumerate(parts):
  55. cfile.write('''
  56. const char _rom%02i[ROM_BUFFER_SIZE%02i] PROGMEM = {
  57. ''' % (idx + 1, idx + 1))
  58. l = addr
  59. h = addr + parts[idx]
  60. addr += parts[idx]
  61. for idx, c in enumerate(data[l:h]):
  62. c = ord(c)
  63. if idx < len(data) - 1:
  64. cfile.write("0x%02x," % c)
  65. else:
  66. cfile.write("0x%02x" % c)
  67. if idx and idx % 16 == 0:
  68. cfile.write("\n")
  69. cfile.write('''
  70. };
  71. ''')
  72. cfile.write('PGM_VOID_P _rom[ROM_BUFFER_CNT]= {')
  73. for idx, val in enumerate(parts):
  74. if idx < len(parts) - 1:
  75. cfile.write('''&_rom%02i,''' % (idx + 1))
  76. else:
  77. cfile.write('''&_rom%02i''' % (idx + 1))
  78. cfile.write('''};
  79. ''')
  80. cfile.write('const int _rom_size[ROM_BUFFER_CNT] = {')
  81. for idx, val in enumerate(parts):
  82. if idx < len(parts) - 1:
  83. cfile.write('''%i,''' % (val))
  84. else:
  85. cfile.write('''%i''' % (val))
  86. cfile.write('''};
  87. ''')
  88. cfile.close()
  89. shutil.copy("/tmp/loader.c", os.path.join(TARGET, "loader.c"))
  90. shutil.copy("/tmp/loader.h", os.path.join(TARGET, "loader.h"))
  91. print "Copy loader.h and loader.c to %s" % TARGET