Add Object File Loader (objectloader)

This commit is contained in:
sBubshait 2024-05-29 13:27:03 +01:00
parent e8880935ac
commit 32b4cab78e
3 changed files with 75 additions and 0 deletions

26
src/emulator/defs.h Normal file
View File

@ -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 <stdint.h>
/************************************
* MACROS AND CONSTANTS
************************************/
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
/************************************
* TYPEDEFS
************************************/
typedef uint8_t Byte;
#endif

View File

@ -0,0 +1,34 @@
/**
********************************************************************************
* @file objectloader.c
* @brief Object file loader for the emulator
********************************************************************************
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#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);
}

View File

@ -0,0 +1,15 @@
/**
* @file objectloader.h
* @brief Object file loader for the emulator
*/
#include <stdbool.h>
#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);