76 lines
1.5 KiB
C
76 lines
1.5 KiB
C
//
|
|
// Created by william on 12/2/23.
|
|
//
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
|
|
#include "../include/rom.h"
|
|
#include "ines.c"
|
|
|
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
|
|
|
Rom rom;
|
|
|
|
void rom_init() {
|
|
rom.header = NULL;
|
|
rom.prg_rom = NULL;
|
|
rom.chr_rom = NULL;
|
|
}
|
|
|
|
int rom_load(char *path) {
|
|
rom_init();
|
|
|
|
FILE *file = fopen(path, "r");
|
|
if (!file) {
|
|
log_error("Failed to open ROM");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
char header_buffer[ROM_HEADER_SIZE] = {0};
|
|
size_t read_size = fread(header_buffer, sizeof(char), ARRAY_SIZE(header_buffer), file);
|
|
if (read_size < ARRAY_SIZE(header_buffer)) {
|
|
log_error("Failed to read ROM");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (!rom_is_ines(header_buffer)) {
|
|
log_error("Only iNes ROMs are supported");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
log_info("Reading iNes 1.0 ROM at %s", path);
|
|
rom_ines_read(header_buffer, file, &rom);
|
|
|
|
if (fclose(file) != 0) {
|
|
log_error("Failed to close ROM file");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void rom_uninit() {
|
|
assert(rom.prg_rom != NULL);
|
|
assert(rom.chr_rom != NULL);
|
|
|
|
free(rom.prg_rom);
|
|
free(rom.chr_rom);
|
|
|
|
log_info("Cleared ROM data");
|
|
}
|
|
|
|
byte rom_prg_get_byte(address addr) {
|
|
assert(rom.prg_rom != NULL);
|
|
|
|
return rom.prg_rom[addr];
|
|
}
|
|
|
|
word rom_prg_get_word(address addr) {
|
|
assert(rom.prg_rom != NULL);
|
|
|
|
word word = rom.prg_rom[addr];
|
|
word += rom.prg_rom[addr + 1] << 8; // Little endian
|
|
return word;
|
|
} |