|
| 1 | +#include "mapper.h" |
| 2 | +#include "data.h" |
| 3 | +#include "macros.h" |
| 4 | +#include "stdinc.h" |
| 5 | +#include <stddef.h> |
| 6 | +#include <stdlib.h> |
| 7 | + |
| 8 | +typedef struct { |
| 9 | + u8 (*read)(const Mapper *mapper, const u8 *rom, size_t rom_len, u16 addr); |
| 10 | + void (*write)(Mapper *mapper, u16 addr, u8 value); |
| 11 | + void (*destroy)(Mapper *mapper); |
| 12 | +} MapperInterface; |
| 13 | + |
| 14 | +struct Mapper { |
| 15 | + const MapperInterface *const vtable; |
| 16 | +}; |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + Mapper base; |
| 20 | +} NoMbcMapper; |
| 21 | + |
| 22 | +typedef struct { |
| 23 | + Mapper base; |
| 24 | + size_t rom_banks; |
| 25 | + size_t rom_bank_num; |
| 26 | + u8 banking_mode_sel; |
| 27 | +} Mbc1Mapper; |
| 28 | + |
| 29 | +static u8 NoMbcMapper_read([[maybe_unused]] const Mapper *const base, |
| 30 | + const u8 *const rom, const size_t rom_len, |
| 31 | + const u16 addr) |
| 32 | +{ |
| 33 | + if (addr < rom_len) |
| 34 | + return rom[addr]; |
| 35 | + |
| 36 | + return 0xFF; |
| 37 | +} |
| 38 | + |
| 39 | +static void NoMbcMapper_write([[maybe_unused]] Mapper *const base, |
| 40 | + [[maybe_unused]] const u16 addr, |
| 41 | + [[maybe_unused]] const u8 value) |
| 42 | +{ |
| 43 | +} |
| 44 | + |
| 45 | +static void NoMbcMapper_destroy([[maybe_unused]] Mapper *const base) |
| 46 | +{ |
| 47 | +} |
| 48 | + |
| 49 | +static u8 Mbc1Mapper_read(const Mapper *const base, const u8 *const rom, |
| 50 | + const size_t rom_len, const u16 addr) |
| 51 | +{ |
| 52 | + const Mbc1Mapper *const self = (const void *)base; |
| 53 | + |
| 54 | + if (self->banking_mode_sel == 1) |
| 55 | + log_info("read from $%04X, mode = %i", addr, self->banking_mode_sel); |
| 56 | + |
| 57 | + if (addr < 0x4000) // 0000-3FFF (ROM bank X0) |
| 58 | + return rom[addr]; |
| 59 | + |
| 60 | + if (addr < 0x8000) // 4000-7FFF (ROM bank 01-7F) |
| 61 | + return rom[(0x4000 * self->rom_bank_num) + addr - 0x4000]; |
| 62 | + |
| 63 | + return 0xFF; |
| 64 | +} |
| 65 | + |
| 66 | +static void Mbc1Mapper_write(Mapper *const base, const u16 addr, const u8 value) |
| 67 | +{ |
| 68 | + Mbc1Mapper *const self = (void *)base; |
| 69 | + |
| 70 | + if (addr >= 0x2000 && addr < 0x4000) { |
| 71 | + // 2000-3FFFF (ROM bank number) |
| 72 | + self->rom_bank_num = (value & 0x11111) % self->rom_banks; |
| 73 | + |
| 74 | + if (self->rom_bank_num == 0) |
| 75 | + self->rom_bank_num = 1; |
| 76 | + } else if (addr >= 0x6000 && addr < 0x8000) { |
| 77 | + // 6000-7FFF (banking mode select) |
| 78 | + self->banking_mode_sel = value & 1; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static void Mbc1Mapper_destroy([[maybe_unused]] Mapper *const base) |
| 83 | +{ |
| 84 | +} |
| 85 | + |
| 86 | +Mapper *Mapper_from_rom(const u8 *rom, const size_t rom_len) |
| 87 | +{ |
| 88 | + BAIL_IF(rom_len < 0x8000); |
| 89 | + |
| 90 | + const u8 ctype = rom[RomHeader_CartridgeType]; |
| 91 | + const u8 rom_size = rom[RomHeader_RomSize]; |
| 92 | + const u8 ram_size = rom[RomHeader_RamSize]; |
| 93 | + |
| 94 | + BAIL_IF(ram_size == 1, "invalid RAM size"); |
| 95 | + |
| 96 | + switch (ctype) { |
| 97 | + case CartridgeType_RomOnly: { |
| 98 | + static const MapperInterface vtable = { |
| 99 | + .read = NoMbcMapper_read, |
| 100 | + .write = NoMbcMapper_write, |
| 101 | + .destroy = NoMbcMapper_destroy, |
| 102 | + }; |
| 103 | + |
| 104 | + NoMbcMapper *const mapper = malloc(sizeof(*mapper)); |
| 105 | + BAIL_IF_NULL(mapper); |
| 106 | + |
| 107 | + const Mapper base = (Mapper){.vtable = &vtable}; |
| 108 | + memcpy(&mapper->base, &base, sizeof(base)); |
| 109 | + |
| 110 | + return &mapper->base; |
| 111 | + } |
| 112 | + case CartridgeType_Mbc1: { |
| 113 | + static const MapperInterface vtable = { |
| 114 | + .read = Mbc1Mapper_read, |
| 115 | + .write = Mbc1Mapper_write, |
| 116 | + .destroy = Mbc1Mapper_destroy, |
| 117 | + }; |
| 118 | + |
| 119 | + Mbc1Mapper *const mapper = malloc(sizeof(*mapper)); |
| 120 | + BAIL_IF_NULL(mapper); |
| 121 | + |
| 122 | + const Mapper base = (Mapper){.vtable = &vtable}; |
| 123 | + memcpy(&mapper->base, &base, sizeof(base)); |
| 124 | + |
| 125 | + mapper->banking_mode_sel = 0; |
| 126 | + mapper->rom_banks = rom_size; |
| 127 | + |
| 128 | + return &mapper->base; |
| 129 | + } |
| 130 | + default: |
| 131 | + BAIL("unimplemented mapper: $%02X", ctype); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +u8 Mapper_read(const Mapper *const self, const u8 *rom, const size_t rom_len, |
| 136 | + u16 addr) |
| 137 | +{ |
| 138 | + BAIL_IF(addr >= 0x8000 && (addr < 0xA000 || addr >= 0xC000), |
| 139 | + "unexpected mapper read (addr = $%04X)", addr); |
| 140 | + |
| 141 | + return self->vtable->read(self, rom, rom_len, addr); |
| 142 | +} |
| 143 | + |
| 144 | +void Mapper_write(Mapper *const self, const u16 addr, const u8 value) |
| 145 | +{ |
| 146 | + self->vtable->write(self, addr, value); |
| 147 | +} |
| 148 | + |
| 149 | +void Mapper_destroy(Mapper *const self) |
| 150 | +{ |
| 151 | + if (self != nullptr) { |
| 152 | + self->vtable->destroy(self); |
| 153 | + free(self); |
| 154 | + } |
| 155 | +} |
0 commit comments