Anda di halaman 1dari 5

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 - Interupts.

Interupts:
As the chip8 system didnt have any interupts I'll explain what they are and what their purpo se is. An interupt is usually a hardware signal which tells the CPU that so mething special has happened. The CPU will check its settings to see if it can respo nd to that interupt. The way the CPU respo nds to the interupt is by saving its current pro gram co unter o nto the stack and then jumping to the interupt service ro utine address fo r the appro priate interupt. It will then carry o n servicing this interupts ro utine until it has finished where it will then relo ad the o ld pro gram co unter and carry o n executing the co de fro m where it was befo re it was interupted. Hence the name interupt. There are two special registers to do with the state o f interupt handling in the gamebo y. The first is the Interupt Enabled register (aka IE) lo cated at memo ry addres 0 xFFFF. This is written to by the game to enable and disable specific interupts. Fo r example so me event may happen that wo uld trigger an interupt like the timer o verflo ws, but this interupt wo uld o nly get serviced if its co rrespo nding bit is enabled in the Interupt Enabled Register (aka IE). If it is enabled then the interupt wo uld be serviced but if it is no t enabled the interupt wo uld sit pending until it beco mes enabled o r the game resets its request. The seco nd interupt register is the Interupt Request Register (aka IF) lo cated at memo ry address 0 xFF0 F. Using the timer interupt as an example again, whenever the timer o verflo ws it requests its interupt by setting its co rrespo nding bit in the Interupt Request Register where it will stay set until servicing o f the interupt begins o r the game resets it.

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

Finished Product

The final po int yo u need to kno w abo ut interupt handling is the master interupt enabled switch. This is no t part o f game memo ry and is just a bo o l that the game sets o n and o ff. When this bo o l is set to false no interupts will get serviced. Belo w is the criteria needed to handle an interupt:

1. When an event o ccurs that needs to trigger its interupt it must make an interupt request by setting its co rrespo nding bit in the Interupt Request Register (0 xFF0 F). 2. An interupt can o nly be serviced if the Interupt Master Enable switch is set to true 3. If the abo ve two co nditio ns are true and their is no o ther interupt with a higher prio rity awaiting to be serviced then it checks the Interupt Enabled Register(0 xFFFF) to see if its co rrespo nding interupt bit is set to 1 to allo w servicing o f this particular interupt.

Step 1 gets set to true and false by the EI and DI cpu instructio ns respectively. If either step 2 o r 3 is false then the interupt co ntinues to wait until bo th 2 and 3 are true and the game hasnt turned the interupt request o ff by resetting its co rrespo nding interupt bit the Interupt Request Register(0 xFF0 F).

Gameboy Interupts:
I have mentio ned a few times that an interupt has its co rrespo nding bit in the Interupt Enabled Register and Interupt Request Register. I have also mentio ned that there are 4 Gamebo y interupts we will be emulating. Belo w is the list o f interupts and their co rrespo nding bit in the interupt request register and interupt enabled register.

Bit 0 : V-Blank Interupt Bit 1: LCD Interupt Bit 2: Timer Interupt Bit 4: Jo ypad Interupt

This is also the prio rity listing o f the interupts. The lo wer the bit the higher prio rity that interupt has, so VBlank has the highest prio rity meaning if this interupt and ano ther interupt are bo th requested in the Interupt Request Register then V-Blank will be serviced first. No w fo r an explanatio n o f the interupts: V-BLANK: The gamebo y draws the display a scanline at a time. There are 144 scanlines o n the display and when it has drawn the last o ne it starts again fro m the beginning. The time it takes to sto p drawing scanline 144 and starting again at scanline 0 is the Vertical Blank perio d and this is when it requests the vblank interupt. This is the mo st impo rtant interupt to emulate co rrectly because during V-Blank the game can read fro m memo ry that was previo usly restricted, mainly video memo ry. As stated previo usly the
PDFmyURL.com

gamebo y has a vertical refresh rate o f 6 0 frames per seco nd meaning that if Step 2 and Step 3 o f the abo ve steps is always true then there sho uld be 6 0 V-Blank interupts a seco nd. Yo u will want to mo nito r this to make sure yo u are accurately emulating V-Blank. LCD: There are many reaso ns why the LCD wo uld request an interupt and these will be lo o ked at in mo re detail in the next chapter called LCD. The main info yo u need to kno w abo ut the LCD interupt is it lets the game kno w what state the LCD is in because depending what state its in certain memo ry regio ns beco me restricted. The game can also set up a co nincidence variable which means "let me kno w when yo u're active scanline is X". One o f the things that stumped me with the LCD interupt is that it can request an LCD interupt whenever V-Blank happens. I didnt understand why there were two v-blank interupts, the main o ne which was discussed abo ve and the o ne nested into the LCD interupt. What yo u need to remember is that during V-Blank if the game is allo wing it there will be two V-Blank interupts requested. The first is the main V-Blank interupt and the seco nd is the LCD interupt. Ho wever the first vblank interupt will have the higher prio rity. T IMER: This interupt has been discussed previo usly in the timers sectio n. Basically the gamebo y timer co unts up at a dynamic frequency and when it gets to value 255 and is abo ut to o verflo w it lets the game kno w by requesting the timer interupt. J OYPAD: The jo ypad will be discussed later o n in the chapter Jo ypad. This interupt is requested whenever o ne o f the butto ns go es fro m the unpressed state to the pressed state.

Requesting an Interupt:
Thro ugho ut the gamebo y part o f this site yo u will see me use the functio n RequestInterupt. I call this whenever an event happens that needs to request an interupt and I pass it the interupt identifier bit (see table abo ve) o f the interupt I wish to request. This is ho w the implementatio n o f this functio n lo o ks:

void Emulator::RequestInterupt(int id) { BYTE req = ReadMemory(0xFF0F) ; req = BitSet(req,id) ; WriteMemory(0xFF0F,id) ; }

Checking the Interupts:


No w we kno w under what circumstances and interupt is requested we must also kno w ho w to handle it. If yo u lo o k back to the Getting Started chapter yo u will see the main emulato r update lo o p. No tice ho w after ever o pco de there is a functio n called Do Interupts? This functio n wo rks by firstly checking if the master interupt switch is set to true. If it is then it checks to see if there are any pending interupts in the Interupt Request Flag. If there is then it checks all the interupts in their prio rity o rder to see if it being requested. If it
PDFmyURL.com

is being requested it checks to see if this particular interupt is enabled in the Interupt Enabled register and if so it services it, if no t it carries o n checking the lo wer prio rity interupts.

void Emulator::DoInterupts( ) { if (m_InteruptMaster == true) { BYTE req = ReadMemory(0xFF0F) ; BYTE enabled = ReadMemory(0xFFFF) ; if (req > 0) { for (int i = 0 ; i < 5; i++) { if (TestBit(req,i) == true) { if (TestBit(enabled,i)) ServiceInterupt(i) ; } } } } }

Servicing the Interupts:


No w we have detected that an interupt is to be serviced what happens then? Firstly the master interupt enabled switch gets set to false and its co rrespo nding bit in the Interupt Request Register is no w unset. Each interupt has a specific interupt service ro utine in game memo ry which the pro gram co unter gets set to and co ntinues executio n fro m there. When the interupt has finished servicing the pro gram co unter gets resto red to where it currently was and game exectio n co ntinues. The fo llo wing is the lo catio n o f the Interupt Service Ro utine fo r each o f the 4 interupts.

V-Blank: 0 x40 LCD: 0 x48 TIMER: 0 x50 JOYPAD: 0 x6 0


PDFmyURL.com

With this new kno wledge we can write the ServiceInterupt functio n like so : void Emulator::ServiceInterupt(int interupt) { m_InteruptMaster = false BYTE req = ReadMemory(0xFF0F) ; req = BitReset(req,interupt) ; WriteMemory(0xFF0F,req) ; /// we must save the current execution address by pushing it onto the stack PushWordOntoStack(m_ProgramCounter); switch (interupt) { case 0: m_ProgramCounter = 0x40 ; break ; case 1: m_ProgramCounter = 0x48 ; break ; case 2: m_ProgramCounter = 0x50 ; break ; case 4: m_ProgramCounter = 0x60 ; break ; } }

Nested Interupts:
I put this sectio n in here because it is impo rtant to kno w abo ut but it is no t so mething yo u need to emulate. It is po ssible that during the servicing o f o ne o f the interupts that the interupt re-enables the master interupt switch. By do ing this the current interupt can get interupted itself by ano ther interupt and will carry o n finishing its o wn interupt when the new interupt finishes servicing. If yo u have emulated yo ur interupts the same way I have then this functio nality will auto matically wo rk but it is useful to kno w incase yo u lo o k in yo ur debug lo gs and see it is servicing o ne interupt befo re it finishes servicing ano ther.

Copyright 2008 codeslinger.co.uk

PDFmyURL.com

Anda mungkin juga menyukai