浏览代码

Fixed a few massive bugs that have been sitting around for far too long.
* Output file format is now correct. Finally.
* Checksums are no longer getting screwed up by the wrong return type.
* Checksum calculation is now being done as per the documentation rather than the ridiculous thing I had before.

Matthew Iselin 13 年之前
父节点
当前提交
e43d5dd367
共有 2 个文件被更改,包括 9 次插入9 次删除
  1. 8 8
      src/tibasic.cpp
  2. 1 1
      src/tibasic.h

+ 8 - 8
src/tibasic.cpp

@@ -28,11 +28,10 @@ using namespace std;
 
 /// \todo More error handling.
 
-unsigned char Compiler::doChecksum(size_t sum)
+unsigned short Compiler::doChecksum(size_t sum)
 {
-    // Take the bottom six bits, and then squash into an 8-bit byte
-    sum %= 0xFFFF;
-    return (sum & 0xFF) + ((sum >> 8) & 0xFF);
+    // Bottom 16 bits of the sum.
+    return (unsigned short) (sum & 0xFFFF);
 }
 
 size_t Compiler::sumBytes(const char *data, size_t len)
@@ -114,15 +113,15 @@ bool Compiler::compile(string inFile, string outFile)
     struct ProgramHeader phdr; struct VariableEntry ventry;
     memset(&phdr, 0, sizeof(ProgramHeader));
     memset(&ventry, 0, sizeof(VariableEntry));
-
-    phdr.datalen = sizeof(VariableEntry) + outputSize;
+    
+    phdr.datalen = sizeof(VariableEntry) + outputSize + sizeof(outputSize);
     strcpy(phdr.sig, "**TI83F*");
     phdr.extsig[0] = 0x1A; phdr.extsig[1] = 0x0A; phdr.extsig[2] = 0;
     strcpy(phdr.comment, "Generated by the TI-BASIC Compiler.");
 
     /// \todo Magic numbers!
     ventry.start = 0x0D;
-    ventry.length1 = ventry.length2 = outputSize;
+    ventry.length1 = ventry.length2 = outputSize + sizeof(outputSize);
     ventry.type = 0x05;
 
     // Convoluted magic to get the filename. Minus the extension. :)
@@ -140,8 +139,8 @@ bool Compiler::compile(string inFile, string outFile)
     // Begin writing to file.
     FILE *out = fopen(outFile.c_str(), "wb");
     fwrite(&phdr, sizeof(phdr), 1, out);
-    fwrite(&outputSize, 2, 1, out);
     fwrite(&ventry, sizeof(ventry), 1, out);
+    fwrite(&outputSize, 2, 1, out);
 
     // Sum of all bytes for checksum purposes.
     size_t sum = 0;
@@ -155,6 +154,7 @@ bool Compiler::compile(string inFile, string outFile)
     }
 
     // Perform a checksum and write to file.
+    sum += outputSize;
     sum += sumBytes(reinterpret_cast<const char*>(&ventry), sizeof(ventry));
     unsigned short checksum = doChecksum(sum);
     fwrite(&checksum, 2, 1, out);

+ 1 - 1
src/tibasic.h

@@ -93,7 +93,7 @@ class Compiler
 
         /// Perform a checksum over a region of data.
         size_t sumBytes(const char *data, size_t len);
-        unsigned char doChecksum(size_t sum);
+        unsigned short doChecksum(size_t sum);
 
 #ifdef _MSC_VER
 #pragma pack(push, 1)