Compare commits
2 Commits
f786235064
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 74159849c9 | |||
| 8f64b17777 |
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
13
src/main.c
13
src/main.c
@@ -36,8 +36,21 @@ 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);
|
||||||
|
|
||||||
|
printf("Emulation completed!\n");
|
||||||
|
printf("Registers:\n");
|
||||||
|
printf(" A: %d\n", emu->regs[REG_A]);
|
||||||
|
printf(" B: %d\n", emu->regs[REG_B]);
|
||||||
|
printf(" C: %d\n", emu->regs[REG_C]);
|
||||||
|
printf(" X: %d\n", emu->regs[REG_X]);
|
||||||
|
printf(" Y: %d\n", emu->regs[REG_Y]);
|
||||||
|
printf(" Z: %d\n", emu->regs[REG_Z]);
|
||||||
|
printf(" R: %d\n", emu->regs[REG_R]);
|
||||||
|
printf(" F: %d\n", emu->regs[REG_F]);
|
||||||
|
|
||||||
freeEmulator(emu);
|
freeEmulator(emu);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user