add file loading
This commit is contained in:
@@ -4,14 +4,10 @@ sources = [
|
|||||||
'src/main.c',
|
'src/main.c',
|
||||||
'src/emu.c',
|
'src/emu.c',
|
||||||
'src/memory.c',
|
'src/memory.c',
|
||||||
'src/instruction.c'
|
'src/instruction.c',
|
||||||
|
'src/util.c'
|
||||||
]
|
]
|
||||||
|
|
||||||
args = ['-Wall', '-Wextra', '-O3']
|
args = ['-Wall', '-Wextra', '-O3']
|
||||||
if get_option('buildtype') == 'debug'
|
|
||||||
args = ['-Wall', '-Wextra', '-O3', '-ggdb', '-fsanitize=address']
|
|
||||||
add_project_arguments('-fsanitize=address', language : 'c')
|
|
||||||
add_project_link_arguments('-fsanitize=address', language : 'c')
|
|
||||||
endif
|
|
||||||
|
|
||||||
executable('emu', sources, c_args: args)
|
executable('emu', sources, c_args: args)
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
#include "emu.h"
|
#include "emu.h"
|
||||||
|
|
||||||
inline Instruction fetchInst(Emulator* emu) {
|
inline Instruction fetchInst(Emulator* emu) {
|
||||||
return decodeInstruction(emu->mem, &emu->pc);
|
Instruction inst = decodeInstruction(emu->mem, &emu->pc);
|
||||||
|
printf("%s\n", instructionToCStr(inst));
|
||||||
|
return inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void updateFlagsRegister(Emulator* emu, int32_t value) {
|
inline void updateFlagsRegister(Emulator* emu, int32_t value) {
|
||||||
|
|||||||
15
src/main.c
15
src/main.c
@@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "emu.h"
|
#include "emu.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Emulator* emu = initEmulator(0x00010FFF);
|
Emulator* emu = initEmulator(0x00010FFF);
|
||||||
@@ -8,19 +9,19 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction program[] = {
|
RomFile* romFile = readProgramFromFile("../test.bin");
|
||||||
|
|
||||||
|
/*Instruction program[] = {
|
||||||
{INST_MVI, {REG_A, 0, 0}, 5, true},
|
{INST_MVI, {REG_A, 0, 0}, 5, true},
|
||||||
{INST_MVI, {REG_B, 0, 0}, 3, true},
|
{INST_MVI, {REG_B, 0, 0}, 3, true},
|
||||||
{INST_ADD, {REG_A, REG_B, 0}, 0, false},
|
{INST_ADD, {REG_A, REG_B, 0}, 0, false},
|
||||||
{INST_HLT, {}, 0, false}
|
{INST_HLT, {}, 0, false}
|
||||||
};
|
};*/
|
||||||
memoryLoadProgram(emu->mem, program, sizeof(program)/sizeof(program[0]));
|
printf("%d\n", romFile->header.numInstructions);
|
||||||
|
memoryLoadProgram(emu->mem, romFile->instructions, romFile->header.numInstructions);
|
||||||
|
|
||||||
printf("%s\n", instructionToCStr(decodeInstruction(emu->mem, &emu->pc)));
|
|
||||||
printf("%s\n", instructionToCStr(decodeInstruction(emu->mem, &emu->pc)));
|
|
||||||
printf("%s\n", instructionToCStr(decodeInstruction(emu->mem, &emu->pc)));
|
|
||||||
printf("%s\n", instructionToCStr(decodeInstruction(emu->mem, &emu->pc)));
|
|
||||||
|
|
||||||
|
//startEmulator(emu);
|
||||||
freeEmulator(emu);
|
freeEmulator(emu);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
45
src/util.c
Normal file
45
src/util.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
RomFile* readProgramFromFile(char* path) {
|
||||||
|
FILE* f = fopen(path, "rb");
|
||||||
|
if (!f) {
|
||||||
|
perror("failed to open file");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long length = ftell(f);
|
||||||
|
rewind(f);
|
||||||
|
|
||||||
|
char* buffer = malloc(length);
|
||||||
|
if (!buffer) {
|
||||||
|
fclose(f);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fread(buffer, length, 1, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
ROMFileHeader* header = (ROMFileHeader*)buffer;
|
||||||
|
Instruction* instructions = calloc(header->numInstructions, sizeof(SerializedInst));
|
||||||
|
if (!instructions) {
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
RomFile* romFile = malloc(sizeof(RomFile));
|
||||||
|
if (!romFile) {
|
||||||
|
free(buffer);
|
||||||
|
free(instructions);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
romFile->header = *header;
|
||||||
|
romFile->instructions = instructions;
|
||||||
|
|
||||||
|
char* cursor = buffer + sizeof(ROMFileHeader);
|
||||||
|
|
||||||
|
memcpy(instructions, cursor, sizeof(SerializedInst) * header->numInstructions);
|
||||||
|
|
||||||
|
return romFile;
|
||||||
|
}
|
||||||
20
src/util.h
Normal file
20
src/util.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "instruction.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// -- TYPES -- //
|
||||||
|
typedef struct {
|
||||||
|
char magic[4];
|
||||||
|
uint8_t version;
|
||||||
|
uint32_t numInstructions;
|
||||||
|
} ROMFileHeader;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ROMFileHeader header;
|
||||||
|
Instruction* instructions;
|
||||||
|
} RomFile;
|
||||||
|
|
||||||
|
// -- FUNCTIONS -- //
|
||||||
|
RomFile* readProgramFromFile(char* path);
|
||||||
Reference in New Issue
Block a user