This commit is contained in:
2026-07-16 19:04:36 +10:00
parent c0da7748d2
commit 3bf340d9f2
3 changed files with 12 additions and 20 deletions

View File

@@ -9,6 +9,7 @@
#include <inttypes.h>
#include "../instruction.h"
#include "../util.h"
// Table of strings to instructions
struct _stringToInstructionOpcodeTable {char* str; InstructionOpcode opcode;} stringToInstructionOpcodeTable[] = {
@@ -212,27 +213,15 @@ void serializeProgramToFile(Program* program, const char* outputFileName) {
}
// Write magic header
size_t written = fwrite("mlc\0", 4, 1, fp);
if (written != 1) {
fprintf(stderr, "failed to write magic header into file %s\n", outputFileName);
fclose(fp);
exit(1);
}
ROMFileHeader header = {
.magic = "mlc\0",
.version = 1,
.numInstructions = program->len
};
// Write program version
uint8_t version = 1;
written = fwrite(&version, sizeof(uint8_t), 1, fp);
size_t written = fwrite(&header, sizeof(ROMFileHeader), 1, fp);
if (written != 1) {
fprintf(stderr, "failed to write version header into file %s\n", outputFileName);
fclose(fp);
exit(1);
}
// Write instruction count
uint32_t instCount = program->len;
written = fwrite(&instCount, sizeof(uint32_t), 1, fp);
if (written != 1) {
fprintf(stderr, "failed to write instruction count header into file %s\n", outputFileName);
fprintf(stderr, "failed to write file header into file %s\n", outputFileName);
fclose(fp);
exit(1);
}