2023-12-03 00:27:07 -05:00
|
|
|
//
|
|
|
|
// Created by william on 12/2/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "../include/rom.h"
|
|
|
|
#include "ines.c"
|
2024-01-06 14:27:09 -05:00
|
|
|
#include "../include/system.h"
|
2023-12-03 00:27:07 -05:00
|
|
|
|
2024-01-06 14:27:09 -05:00
|
|
|
bool rom_load(char *path, System *system) {
|
2023-12-03 00:27:07 -05:00
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
if (!file) {
|
2023-12-23 16:35:23 -05:00
|
|
|
log_error("Failed to open ROM");
|
2024-01-06 14:27:09 -05:00
|
|
|
return false;
|
2023-12-03 00:27:07 -05:00
|
|
|
}
|
|
|
|
|
2023-12-23 16:35:23 -05:00
|
|
|
char header_buffer[ROM_HEADER_SIZE] = {0};
|
2024-01-06 14:27:09 -05:00
|
|
|
size_t read_size = fread(header_buffer, sizeof(char), ROM_HEADER_SIZE, file);
|
|
|
|
if (read_size < ROM_HEADER_SIZE) {
|
2023-12-23 16:35:23 -05:00
|
|
|
log_error("Failed to read ROM");
|
2024-01-06 14:27:09 -05:00
|
|
|
return false;
|
2023-12-03 00:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rom_is_ines(header_buffer)) {
|
2023-12-23 16:35:23 -05:00
|
|
|
log_error("Only iNes ROMs are supported");
|
2024-01-06 14:27:09 -05:00
|
|
|
return false;
|
2023-12-03 00:27:07 -05:00
|
|
|
}
|
|
|
|
|
2023-12-23 16:35:23 -05:00
|
|
|
log_info("Reading iNes 1.0 ROM at %s", path);
|
2024-01-06 14:27:09 -05:00
|
|
|
rom_ines_read(header_buffer, file, system);
|
2023-12-03 00:27:07 -05:00
|
|
|
|
|
|
|
if (fclose(file) != 0) {
|
2023-12-23 16:35:23 -05:00
|
|
|
log_error("Failed to close ROM file");
|
2024-01-06 14:27:09 -05:00
|
|
|
return false;
|
2023-12-03 00:27:07 -05:00
|
|
|
}
|
|
|
|
|
2024-01-06 14:27:09 -05:00
|
|
|
return true;
|
2023-12-03 00:27:07 -05:00
|
|
|
}
|