Add Object File Loader (objectloader)
This commit is contained in:
parent
e8880935ac
commit
32b4cab78e
26
src/emulator/defs.h
Normal file
26
src/emulator/defs.h
Normal 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
|
||||
34
src/emulator/objectloader.c
Normal file
34
src/emulator/objectloader.c
Normal 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);
|
||||
}
|
||||
15
src/emulator/objectloader.h
Normal file
15
src/emulator/objectloader.h
Normal 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);
|
||||
Loading…
Reference in New Issue
Block a user