Memory mapping & mirroring

Im writing a emulator to the NES just for the fun of it and ive hit a wall.
Memory locations 0x2000-0x2007 are mirrored every 8 bytes from 0x2008 to 0x4000. So location 0x2008 would point back to 0x2000. How would i be able to program this? Ive already handled the other parts of memory that mirror things, i just cant seem to get this one down.
Thanks in advance
This is really simple actually; just mod the address by 8.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int addr;
unsigned char value;
unsigned char memory[/*something*/];

//...

//write
if(addr >= 0x2000 && addr <= 0x4000)
{
    int base = 0x2000;
    int offset = addr % 8;
    memory[base + offset] = value;
}

//...

//read
if(addr >= 0x2000 && addr <= 0x4000)
{
   int base = 0x2000;
   int offset = addr % 8;
   value = memory[base + offset];
}
Sweet.I know this was easy I just didn't know how to go about it. Thank you :D
Turns out its even easier than that. Just & the memory address with 0x2007. Just in case someone googles this :p
Topic archived. No new replies allowed.