Skip to content

Commit

Permalink
add:支持INES格式rom
Browse files Browse the repository at this point in the history
  • Loading branch information
Dozingfiretruck committed Jun 30, 2024
1 parent 80d2939 commit 38c7335
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
3 changes: 2 additions & 1 deletion inc/nes_rom.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
struct nes;
typedef struct nes nes_t;

/* INES: https://www.nesdev.org/wiki/INES */
/* NES 2.0: https://wiki.nesdev.org/w/index.php/NES_2.0 */
#define TRAINER_SIZE (0x200)
#define PRG_ROM_UNIT_SIZE (0x4000)
Expand All @@ -49,7 +50,7 @@ typedef struct {
};
struct {
uint8_t console_type:2; /* D0-1 Console type 0: Nintendo Entertainment System/Family Computer 1: Nintendo Vs. System 2: Nintendo Playchoice 10 3: Extended Console Type */
uint8_t identifier2:2; /* D2-3 NES 2.0 identifier */
uint8_t identifier:2; /* D2-3 NES 2.0 identifier */
uint8_t mapper_number_m:4; /* D4-7 Mapper Number D4..D7 */
};
struct {
Expand Down
32 changes: 28 additions & 4 deletions src/nes_rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ nes_t* nes_load_file(const char* file_path ){
nes_header_info_t nes_header_info = {0};
nes_t* nes = NULL;

FILE* nes_file = nes_fopen(file_path, "rb");
if (!nes_file){
void* nes_file = nes_fopen(file_path, "rb");
if (nes_file == NULL){
nes_printf("nes_load_file: failed to open file %s\n", file_path);
goto error;
}
nes = (nes_t *)nes_malloc(sizeof(nes_t));
Expand All @@ -62,7 +63,24 @@ nes_t* nes_load_file(const char* file_path ){
}
nes->nes_rom.prg_rom_size = ((nes_header_info.prg_rom_size_m << 8) & 0xF00) | nes_header_info.prg_rom_size_l;
nes->nes_rom.chr_rom_size = ((nes_header_info.prg_rom_size_m << 8) & 0xF00) | nes_header_info.chr_rom_size_l;
nes->nes_rom.mapper_number = ((nes_header_info.mapper_number_h << 8) & 0xF00) | ((nes_header_info.mapper_number_m << 4) & 0xF0) | (nes_header_info.mapper_number_l & 0x0F);
if (nes_header_info.identifier==1){
uint8_t idx = 4;
uint8_t* nes_header = (uint8_t*)&nes_header_info;
nes->nes_rom.mapper_number = nes_header_info.mapper_number_l & 0x0F;
for (idx = 4; idx < 8 && nes_header[8+idx] == 0; ++idx);
if (idx==8){
nes->nes_rom.mapper_number |= ((nes_header_info.mapper_number_m << 4) & 0xF0);
}
nes->nes_rom.prg_rom_size = nes_header_info.prg_rom_size_l;
nes->nes_rom.chr_rom_size = nes_header_info.chr_rom_size_l;
}else if (nes_header_info.identifier==2){
nes->nes_rom.mapper_number = ((nes_header_info.mapper_number_h << 8) & 0xF00) | ((nes_header_info.mapper_number_m << 4) & 0xF0) | (nes_header_info.mapper_number_l & 0x0F);
nes->nes_rom.prg_rom_size = ((nes_header_info.prg_rom_size_m << 8) & 0xF00) | nes_header_info.prg_rom_size_l;
nes->nes_rom.chr_rom_size = ((nes_header_info.prg_rom_size_m << 8) & 0xF00) | nes_header_info.chr_rom_size_l;
}else{
nes_printf("nes_load_file: unsupported rom format\n");
goto error;
}
nes->nes_rom.mirroring_type = (nes_header_info.mirroring);
nes->nes_rom.four_screen = (nes_header_info.four_screen);
nes->nes_rom.save_ram = (nes_header_info.save);
Expand All @@ -83,6 +101,10 @@ nes_t* nes_load_file(const char* file_path ){
goto error;
}
}

nes_printf("nes_load_rom: prg_rom_size: %d, chr_rom_size: %d, mapper_number: %d, mirroring_type: %d, four_screen: %d, save_ram: %d\n",
nes->nes_rom.prg_rom_size, nes->nes_rom.chr_rom_size, nes->nes_rom.mapper_number, nes->nes_rom.mirroring_type,
nes->nes_rom.four_screen, nes->nes_rom.save_ram);
}else{
goto error;
}
Expand Down Expand Up @@ -148,7 +170,9 @@ nes_t* nes_load_rom(const uint8_t* nes_rom){
nes->nes_rom.mirroring_type = (nes_header_info->mirroring);
nes->nes_rom.four_screen = (nes_header_info->four_screen);
nes->nes_rom.save_ram = (nes_header_info->save);

nes_printf("nes_load_rom: prg_rom_size: %d, chr_rom_size: %d, mapper_number: %d, mirroring_type: %d, four_screen: %d, save_ram: %d\n",
nes->nes_rom.prg_rom_size, nes->nes_rom.chr_rom_size, nes->nes_rom.mapper_number, nes->nes_rom.mirroring_type,
nes->nes_rom.four_screen, nes->nes_rom.save_ram);
nes->nes_rom.prg_rom = nes_bin;
nes_bin += PRG_ROM_UNIT_SIZE * nes->nes_rom.prg_rom_size;

Expand Down

0 comments on commit 38c7335

Please sign in to comment.