|
@@ -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);
|