Higher-order functions: Difference between revisions

Content added Content deleted
Line 4,247: Line 4,247:
second function called
second function called
</pre>
</pre>


=={{header|Z80 Assembly}}==
Higher-order functions are often used for IRQ handlers which don't have a lot of time to figure out what to do. (I'll admit this is a bit of a stretch since typically the function that receives the IRQ handler as a parameter just calls it and does nothing else.)

Typically, the IRQ handler will jump to RAM, and before the program is in a state where the IRQ conditions will be met (such as a video game during a level transition), the function will be passed (sometimes even by value!) to the IRQ handler. On the Game Boy and the ZX Spectrum, RSTs are in ROM and thus cannot be changed at runtime, so there's not much of a choice.

In fact, it is <b>mandatory</b> to pass the IRQ handler by value on the Game Boy if your game uses hardware sprites, as during direct memory access the CPU loses the ability to access the cartridge, including code execution! Therefore, the code that initiates direct memory access must be copied to RAM from the cartridge ROM and executed from RAM. Since interrupt vectors are in ROM, the vBlank interrupt vector will immediately jump to RAM. Interrupts are disabled immediately after powering on, so we've got all the time we need to copy the interrupt handler to RAM. Once we enable IRQs, the code we copied must remain there or else the game will crash.

<lang z80>org &0040 ;Game Boy's vblank IRQ goes here.
;This is not part of the standard Z80 vector table - interrupts work differently on the Game Boy.
jp &ff80

;more code goes here

;during setup, we'll CALL SetupDMA before enabling the vblank IRQ.


SetupDMA:
ld bc,DMACopyEnd-DMACopy ;how many bytes to copy
ld hl,DMACopy ;pointer to source
ld de,&ff80 ;pointer to destination
z_ldir ;macro for LDIR which the game boy doesn't have.
ret

DMACopy: ;must be run from &ff80
push af
ld a,>GBSpriteCache ;high byte of wherever we're storing our object attribute memory
gb_out <dma ;start the transfer - DMA auto-copies 256 bytes from xx00-xxFF where xx = >GBSpriteCache
ld a,&28 ;delay - this ensures the DMA is done before we exit. Adjust to your liking.

;game boy doesn't have in/out commands, rather all its I/O ports are at &FFxx so there are special commands just for accessing them faster
;gb_out is a macro that inlines the bytecode, since not all assemblers auto-convert LD (&FFxx),a.

DMACopyWait:
dec a
jr nz,DMACopyWait
pop af
reti
DMACopyEnd:</lang>