Call a foreign-language function: Difference between revisions

(Added Hare)
Line 17:
*   [[Use another language to call a function]]
<br><br>
 
=={{header|68000 Assembly}}==
{{works with|Sega Genesis}}
 
The Genesis uses a Z80 coprocessor to interface with its sound hardware. The Z80 executes its code from RAM, so before starting this, you have to <code>memcpy</code> the compiled program code from the Genesis cartridge ROM to the shared RAM area at $A00000. Since this task is about calling a function, we'll show the z80 code necessary to do that. Thanks to [https://www.chibiakumas.com/68000/platform2.php#LessonP20 this tutorial] for guidance on how this all works.
 
<lang z80>
org &0000
jr start
 
org &0060
start:
DI
IM 1
LD SP,&2000
 
main: ;hardware non-maskable interrupt (NMI) jumps here.
ld a,(&1F00) ;we'll only allow the 68000 to alter the contents of this memory address.
or a
jr z,main ;just keep looping until it's nonzero.
 
smc: ;clobber by writing to &A0006D
call &0000 ;we'll overwrite the operand with whatever function we want to call.
 
done:
jp done ;loop until next reset
 
ExampleFunction: ;ADDR: &0072($A00072)
ret ;for simplicity this does nothing but in reality you'd have it do something sound-related here.</lang>
 
Here's the 68000 code that will get the Z80 to call this function:
 
<lang 68000devpac>Z80_Call:
MOVE.W #$100,$A11100 ;write: z80 reset
.wait:
BTST #8,$A11100 ;read: check bit 8 to see if the z80 is busy
BNE .wait ;loop until not busy
 
 
;now we write the function address
;z80 is little-endian so we need to reverse the byte order.
;also 68000 cannot safely write words at odd addresses so we need to write as bytes.
 
MOVE.B #$72,$A0006D
MOVE.B #$00,$A0006E ;this changes the "call &0000" above to "call ExampleFunction"
 
MOVE.B #$FF,$A01F01 ;unlock the semaphore
MOVE.W #0,$A11100 ;Z80 Bus Request - after this write, the Z80 will start executing code.</lang>
 
=={{header|8th}}==
1,489

edits