19 constexpr uint32_t kLoRomHeaderLocation = 0x7FC0;
20 constexpr uint32_t kMinRomSizeForHeader = 0x7FD9;
22 if (rom_data.size() < kMinRomSizeForHeader) {
23 LOG_DEBUG(
"Memory",
"ROM too small for header access: %zu bytes (need at least %u bytes)",
24 rom_data.size(), kMinRomSizeForHeader);
26 rom_size_ =
static_cast<uint32_t
>(rom_data.size());
28 LOG_DEBUG(
"Memory",
"Using fallback: ROM size=%u bytes, SRAM size=%u bytes",
31 auto location = kLoRomHeaderLocation;
32 uint8_t rom_size_shift = rom_data[location + 0x17];
33 uint8_t sram_size_shift = rom_data[location + 0x18];
36 if (rom_size_shift > 15) {
37 LOG_DEBUG(
"Memory",
"Invalid ROM size shift: %u, using fallback", rom_size_shift);
38 rom_size_ =
static_cast<uint32_t
>(rom_data.size());
43 if (sram_size_shift > 7) {
44 LOG_DEBUG(
"Memory",
"Invalid SRAM size shift: %u, using default", sram_size_shift);
53 const size_t copy_size = std::min<size_t>(
rom_size_, rom_data.size());
54 std::copy(rom_data.begin(), rom_data.begin() + copy_size,
rom_.begin());
57 std::fill(
ram_.begin(),
ram_.end(), 0);
60 "LoROM initialized: ROM size=$%06X (%zuKB) SRAM size=$%04X",
64 if (rom_data.size() >= 0x7FFE) {
65 LOG_DEBUG(
"Memory",
"Reset vector at ROM offset $7FFC-$7FFD = $%02X%02X",
66 rom_data[0x7FFD], rom_data[0x7FFC]);
68 LOG_DEBUG(
"Memory",
"ROM too small to read reset vector (size: %zu bytes)",
147 if ((bank & 0x7f) < 0x40 && adr >= 0x6000 && adr < 0x8000 && sram_size_ > 0) {
149 return ram_[(((bank & 0x3f) << 13) | (adr & 0x1fff)) & (
sram_size_ - 1)];
151 bool secondHalf = bank < 0x80;
153 if (adr >= 0x8000 || bank >= 0x40) {
155 return rom_[(((bank & 0x3f) << 16) | (secondHalf ? 0x400000 : 0) | adr) &