You are on page 1of 3

codeslinger.co.

uk
H o mB e a s Zi u c k sM o e g a D r M i a v e s /t G e re n S e y s Cs i ht s ie Bpm l 8 o Gamebo y g

codeslinger.co.uk
Gamebo y - Memo ry Co ntro l and Map.

Memory Map:
Taken fro m no cash-s pando cs we have the fo llo wing memo ry map:

Gameboy Emulation:
Getting Started The Hardware Memory Control and Map ROM and RAM Banking The Timers Interupts LCD DMA Transfer Graphic Emulation Joypad Emulation

0 0 0 0 -3FFF 16 KB ROM Bank 0 0 (in cartridge, fixed at bank 0 0 ) 40 0 0 -7FFF 16 KB ROM Bank 0 1..NN (in cartridge, switchable bank number) 8 0 0 0 -9 FFF 8 KB Video RAM (VRAM) (switchable bank 0 -1 in CGB Mo de) A0 0 0 -BFFF 8 KB External RAM (in cartridge, switchable bank, if any) C0 0 0 -CFFF 4KB Wo rk RAM Bank 0 (WRAM) D0 0 0 -DFFF 4KB Wo rk RAM Bank 1 (WRAM) (switchable bank 1-7 in CGB Mo de) E0 0 0 -FDFF Same as C0 0 0 -DDFF (ECHO) (typically no t used) FE0 0 -FE9 F Sprite Attribute Table (OAM) FEA0 -FEFF No t Usable FF0 0 -FF7F I/O Po rts FF8 0 -FFFE High RAM (HRAM) FFFF Interrupt Enable Register

As explained earlier the internal memo ry o nly has ro o m fo r 0 x8 0 0 0 (0 x0 0 0 0 - 0 x7FFF) o f the game memo ry. Ho wever mo st games are bigger in size than 0 x8 0 0 0 which is why memo ry banking is needed.

Opcode Examples PDFmyURL.com

The first 0 x40 0 0 bytes is where the first 0 x40 0 0 (0 0 0 0 -0 x3FFF) (Ro m bank 0 ) bytes o f the game memo ry is placed. The seco nd 0 x40 0 0 (0 x40 0 0 - 0 x7FFF) is also fo r the game memo ry ho wever this area is the ro m bank area so depending o n the current state o f the game this memo ry area will co ntain different game memo ry banks. Banking is discussed in much mo re detail in the Ro m and Ram Banking sectio n. Memo ry area 0 xA0 0 0 -0 xBFFF is also fo r banking but this time it's ram banking. Once again I'll discuss this is mo re detail in the next sectio n. The ECHO memo ry regio n (0 xE0 0 0 -0 xFDFF) is quite different because any data written here is also written in the equivelent ram memo ry regio n 0 xC0 0 0 0 xDDFF. Hence why it is called echo . Yo u maybe wo ndering if writing to RAM memo ry also writes its data to the ECHO memo ry. The answer is I do nt kno w. I'd imagine that it did ho wever whenever I emulated this it never wo rked co rrectly so I to o k it o ut and have never had any pro blems. The HRAM is where the stack sto res its data. The stack po inter starts at 0 xFFFE and wo rks its way do wn in memo ry whenever so mething is pushed o nto the stack. I will discuss the rest o f the regio ns as needed in the fo llo wing sectio ns.

Finished Product

Memory Control:
No w we have a breakdo wn o f the internal memo ry it beco mes quite o bvio us that read and write access to the memo ry needs to be well co ntro lled. Fo r instance the first 0 x8 0 0 0 bytes are read o nly so no thing sho uld ever get written there. Also anything that gets written to ECHO memo ry needs to be reflected in wo rk RAM. Also when reading fro m o ne o f the banks it is impo rtant that it gets read fro m the co rrect bank. The best way to co ntro l read and write access to the memo ry is creating a ReadMemo ry functio n and a WriteMemo ry functio n. Whenever yo u read and write to memo ry yo u MUST use these two functio ns, this way yo u will be certain yo ur memo ry access will be co ntro lled co rrectly. The o nly exceptio n to this is when yo u initialize Ro m memo ry which was sho wn o n the previo us sectio n Hardware. Belo w is an example o f ho w the WriteMemo ry will help yo u co ntro l memo ry access:

void Emulator::WriteMemory(WORD address, BYTE data) { // dont allow any writing to the read only memory if ( address < 0x8000 ) { } // writing to ECHO ram also writes in RAM else if ( ( address >= 0xE000 ) && (address < 0xFE00) ) { m_Rom[address] = data ; WriteMemory(address-0x2000, data) ; }

PDFmyURL.com

// this area is restricted else if ( ( address >= 0xFEA0 ) && (address < 0xFEFF) ) { } // no control needed over this area so write to memory else { m_Rom[address] = data ; } }

This is a very simplified versio n o f the WriteMemo ry functio n but it sho uld give yo u a basic idea o f the impo rtance o f it. This functio n will expand thro ugho ut the o ther sectio ns. The ReadMemo ry is discussed in mo re detail in the next sectio n Ro m and Ram Banking

Copyright 2008 codeslinger.co.uk

PDFmyURL.com

You might also like