Include imm16's in the word count (fix the infinite loop bug yay!)

This commit is contained in:
2026-07-17 12:48:04 +10:00
parent f786235064
commit 8f64b17777
4 changed files with 9 additions and 3 deletions

BIN
add.o

Binary file not shown.

View File

@@ -248,11 +248,16 @@ void serializeProgramToFile(Program* program, const char* outputFileName) {
exit(1); exit(1);
} }
size_t totalWords = 0;
for (size_t i = 0; i < program->len; i++) {
totalWords += program->at[i].hasImm16 ? 2 : 1;
}
// Write magic header // Write magic header
ROMFileHeader header = { ROMFileHeader header = {
.magic = "mlc\0", .magic = "mlc\0",
.version = 1, .version = 1,
.numInstructions = program->len .numInstructions = totalWords
}; };
size_t written = fwrite(&header, sizeof(ROMFileHeader), 1, fp); size_t written = fwrite(&header, sizeof(ROMFileHeader), 1, fp);

View File

@@ -30,7 +30,7 @@ void addInstructionToProgram(Program* program, Instruction inst) {
} }
if (program->len + 1 >= program->capacity) { if (program->len + 1 >= program->capacity) {
Instruction* tmp = malloc(sizeof(Instruction) * program->capacity * 2); Instruction* tmp = realloc(program->at, sizeof(Instruction) * program->capacity * 2);
if (tmp == NULL) { if (tmp == NULL) {
fprintf(stderr, "malloc failed while expanding program\n"); fprintf(stderr, "malloc failed while expanding program\n");
exit(1); exit(1);
@@ -50,7 +50,7 @@ void addLabelToProgram(Program* program, const char* name, uint8_t position) {
} }
if (program->labels.len + 1 >= program->labels.capacity) { if (program->labels.len + 1 >= program->labels.capacity) {
Label* tmp = malloc(sizeof(Label) * program->labels.capacity * 2); Label* tmp = realloc(program->labels.at, sizeof(Label) * program->labels.capacity * 2);
if (tmp == NULL) { if (tmp == NULL) {
fprintf(stderr, "malloc failed while expanding program labels\n"); fprintf(stderr, "malloc failed while expanding program labels\n");
exit(1); exit(1);

View File

@@ -36,6 +36,7 @@ int main(int argc, char** argv) {
} }
memcpy(emu->mem->data + MEM_PROGRAM_START, rom->instructions, sizeof(SerializedInst) * rom->header.numInstructions); memcpy(emu->mem->data + MEM_PROGRAM_START, rom->instructions, sizeof(SerializedInst) * rom->header.numInstructions);
emu->pc = MEM_PROGRAM_START;
startEmulator(emu); startEmulator(emu);
freeEmulator(emu); freeEmulator(emu);