diff --git a/src/emulator/defs.h b/src/emulator/defs.h new file mode 100644 index 0000000..62cd9e5 --- /dev/null +++ b/src/emulator/defs.h @@ -0,0 +1,26 @@ +/** + ******************************************************************************** + * @file defs.h + * @brief Defines global constants and types used in the emulator. + ******************************************************************************** + */ + +#ifndef DEFS_H +#define DEFS_H +#include "../global.h" +#include + +/************************************ + * MACROS AND CONSTANTS + ************************************/ +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +/************************************ + * TYPEDEFS + ************************************/ + +typedef uint8_t Byte; + + + #endif \ No newline at end of file diff --git a/src/emulator/objectloader.c b/src/emulator/objectloader.c new file mode 100644 index 0000000..533d146 --- /dev/null +++ b/src/emulator/objectloader.c @@ -0,0 +1,34 @@ +/** + ******************************************************************************** + * @file objectloader.c + * @brief Object file loader for the emulator + ******************************************************************************** + */ + +#include +#include +#include +#include "objectloader.h" +#include "defs.h" + +void loadObjectFile(const char *filename, Byte *memoryAddress) { + FILE *file = fopen(filename, "rb"); + + // Check if the file exists + if (file == NULL) { + fprintf(stderr, "Error: Could not open file %s\n", filename); + exit(EXIT_FAILURE); + } + + // Load the object file into memory (or as much as possible) + size_t bytesRead = fread(memoryAddress, MEMORY_SIZE, 1, file); + if (bytesRead == 0) { + if (feof(file)) + exit(EXIT_SUCCESS); + + fprintf(stderr, "Error: Could not read from file %s\n", filename); + exit(EXIT_FAILURE); + } + + fclose(file); +} \ No newline at end of file diff --git a/src/emulator/objectloader.h b/src/emulator/objectloader.h new file mode 100644 index 0000000..c08cfaa --- /dev/null +++ b/src/emulator/objectloader.h @@ -0,0 +1,15 @@ +/** + * @file objectloader.h + * @brief Object file loader for the emulator + */ + +#include +#include "defs.h" + +/** + * @brief Loads an object file into memory starting at memoryAddress. + * + * @param filename The name of the file to be read + * @param memoryAddress The memory address to load the object file into + */ +void loadObjectFile(const char *filename, Byte *memoryAddress); \ No newline at end of file