Professional Documents
Culture Documents
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 - Getting Started.
Type definitions:
Like all machines the gamebo y uses data types o f different sizes, mainly bytes (8 bits) and wo rds (16 bits). The z8 0 -like instructio n set also uses signed bytes and signed wo rds. So to make understanding o f the co de easier I have typedef'd these to give the fo llo wing:
Gameboy Emulation:
Getting Started The Hardware Memory Control and Map ROM and RAM Banking The Timers Interupts LCD DMA Transfer Graphic Emulation Joypad Emulation Opcode Examples PDFmyURL.com
unsigned char BYTE ; char SIGNED_BYTE ; unsigned short WORD ; signed short SIGNED_WORD ;
I highly reco mmend yo u do the same as it makes understanding what value range a variable can sto re easier to understand and to track do wn any o verflo w erro rs.
bytes o f the cartride in memo ry and the rest is left in the cartridge (the emulato r will have to swap data in and o ut the cartride to memo ry as needed) Declaratio n o f cartridge memo ry:
Finished Product
BYTE m_CartridgeMemory[0x200000] ;
Lo ading a game into cartridge memo y: memset(m_CartridgeMemory,0,sizeof(m_CartridgeMemory)) ; FILE *in; in = fopen( "Tetris.gb", "rb" ); fread(m_CartridgeMemory, 1, 0x200000, in); fclose(in);
while (cyclesThisUpdate < MAXCYCLES) { int cycles = ExecuteNextOpcode( ) ; cyclesThisUpdate+=cycles ; UpdateTimers(cycles) ; UpdateGraphics(cycles) ; DoInterupts( ) ; } RenderScreen( ) ; }
So if the abo ve functio n is called 6 0 times a seco nd then RenderScreen is also called 6 0 times a seco nd which is the same as the gamebo y. The while lo o p ensures that the co rrect amo unt o f instructio ns is being executed this frame (assuming o f co urse that the functio n ExecuteNextOpco de is returning the co rrect clo ck cycles the next o pco de to o k to execute). Also the timers and graphics are being passed ho w many clo ck cycles the o pco de to o k so they can update at the same rate as the cpu. After every instructio n the interupts need to be checked and if needed then pro cessed.
PDFmyURL.com