Fix incorrect code mapping logic

This commit is contained in:
CasualPokePlayer 2024-05-29 11:14:07 -07:00
parent 74c1a73fe8
commit befc3c90a1
2 changed files with 12 additions and 7 deletions

View File

@ -146,12 +146,12 @@ void Cpu::reset( void const* unmapped_page )
irq_time_ = LONG_MAX / 2 + 1; irq_time_ = LONG_MAX / 2 + 1;
end_time_ = LONG_MAX / 2 + 1; end_time_ = LONG_MAX / 2 + 1;
code_map [0] = low_mem; set_code_page(0, low_mem);
code_map [1] = low_mem - 1 * page_size; set_code_page(1, low_mem);
code_map [2] = low_mem - 2 * page_size; set_code_page(2, low_mem);
code_map [3] = low_mem - 3 * page_size; set_code_page(3, low_mem);
for ( int32_t i = 4; i < page_count + 1; i++ ) for ( int32_t i = 4; i < page_count + 1; i++ )
code_map [i] = (uint8_t*) unmapped_page; set_code_page(i, (uint8_t *) unmapped_page);
isCorrectExecution = true; isCorrectExecution = true;
} }

View File

@ -51,11 +51,16 @@ class Cpu
// Clear registers, unmap memory, and map code pages to unmapped_page. // Clear registers, unmap memory, and map code pages to unmapped_page.
void reset(void const *unmapped_page = 0); void reset(void const *unmapped_page = 0);
inline void set_code_page(int i, uint8_t const *p)
{
code_map[i] = p - (unsigned)i * page_size;
}
inline void map_code(nes_addr_t start, unsigned size, const void *data) inline void map_code(nes_addr_t start, unsigned size, const void *data)
{ {
unsigned first_page = start / page_size; unsigned first_page = start / page_size;
const uint8_t *newPtr = (uint8_t *)data - start; for (unsigned i = size / page_size; i--;)
for (unsigned i = size / page_size; i--;) code_map[first_page + i] = newPtr; set_code_page(first_page + i, (uint8_t *)data + i * page_size);
} }
// Access memory as the emulated CPU does. // Access memory as the emulated CPU does.