nesemu/rom/rom.c

49 lines
915 B
C
Raw Permalink Normal View History

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-05-23 22:44:52 -04:00
Rom rom;
Rom *rom_get() {
return &rom;
}
2024-05-06 20:23:44 -04:00
bool rom_load(char *path) {
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
}
2024-05-23 23:52:04 -04:00
size_t read_size = fread(&rom.header, sizeof(byte), ROM_HEADER_SIZE, file);
2024-01-06 14:27:09 -05:00
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
}
2024-05-23 23:52:04 -04:00
if (!rom_is_ines(rom.header)) {
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-05-23 22:44:52 -04:00
rom_ines_read(&rom, file);
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;
2024-05-23 22:44:52 -04:00
}
void rom_unload() {
free(rom.prg_rom);
free(rom.chr_rom);
2023-12-03 00:27:07 -05:00
}