nesemu/rom/rom.c

49 lines
915 B
C

//
// Created by william on 12/2/23.
//
#include <stdio.h>
#include "../include/rom.h"
#include "ines.c"
#include "../include/system.h"
Rom rom;
Rom *rom_get() {
return &rom;
}
bool rom_load(char *path) {
FILE *file = fopen(path, "r");
if (!file) {
log_error("Failed to open ROM");
return false;
}
size_t read_size = fread(&rom.header, sizeof(byte), ROM_HEADER_SIZE, file);
if (read_size < ROM_HEADER_SIZE) {
log_error("Failed to read ROM");
return false;
}
if (!rom_is_ines(rom.header)) {
log_error("Only iNes ROMs are supported");
return false;
}
log_info("Reading iNes 1.0 ROM at %s", path);
rom_ines_read(&rom, file);
if (fclose(file) != 0) {
log_error("Failed to close ROM file");
return false;
}
return true;
}
void rom_unload() {
free(rom.prg_rom);
free(rom.chr_rom);
}