62 lines
1.4 KiB
C
Executable File
62 lines
1.4 KiB
C
Executable File
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "a64instruction.h"
|
|
#include "emulator.h"
|
|
#include "fileio.h"
|
|
#include "global.h"
|
|
#include "print.h"
|
|
#include "decode.h"
|
|
#include "execute.h"
|
|
|
|
extern a64inst_instruction *decode(word w);
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Check the arguments
|
|
if (argc == 1) {
|
|
fprintf(stderr, "Error: An object file is required. Syntax: ./emulate <file_in> [<file_out>]");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
FILE *out = stdout;
|
|
if (argc > 2) {
|
|
out = fopen(argv[2], "w");
|
|
if (out == NULL) {
|
|
fprintf(stderr, "Error: Could not open file %s\n", argv[2]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
|
|
// Initialising the machine state
|
|
Machine state = {0};
|
|
state.memory = fileio_loadBin(argv[1], MEMORY_SIZE);
|
|
state.conditionCodes = (PState){0, 1, 0, 0};
|
|
state.pc = 0x0;
|
|
|
|
|
|
// Fetch-decode-execute cycle
|
|
word wrd;
|
|
a64inst_instruction *inst;
|
|
do {
|
|
|
|
// Step 1: Fetch instruction at PC's address
|
|
wrd = readWord(state.memory, state.pc);
|
|
|
|
// Step 2: Decode instruction to internal representation
|
|
inst = decode(wrd);
|
|
|
|
// Step 3: Update processor state to reflect executing the instruction, and increment PC
|
|
execute(&state, inst);
|
|
|
|
if (inst->type != a64inst_BRANCH)
|
|
state.pc += sizeof(word);
|
|
} while (inst->type != a64inst_HALT);
|
|
|
|
state.pc -= sizeof(word);
|
|
|
|
printState(&state, out);
|
|
free(state.memory);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|