Call a foreign-language function: Difference between revisions

Content added Content deleted
Line 25: Line 25:
(Technically, the 68000 isn't calling the function itself, since it doesn't understand Z80 code at all; rather, it's instructing the Z80 to call and execute the function on its behalf. But this is probably as close as we'll ever get.)
(Technically, the 68000 isn't calling the function itself, since it doesn't understand Z80 code at all; rather, it's instructing the Z80 to call and execute the function on its behalf. But this is probably as close as we'll ever get.)


'''Z80 Code:'''
<lang z80>
<lang z80>org &0000 ;execution resets here after the 68000 resets the Z80 and sends a bus request.
org &0000
jr start
jr start

org &0038 ;in IM 1 mode, we jump here for an IRQ. But this isn't being used for this example, so we'll just silently return.
reti


org &0060
org &0060
Line 35: Line 38:
LD SP,&2000
LD SP,&2000


main: ;hardware non-maskable interrupt (NMI) jumps here.
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.
ld a,(&1F00) ;we'll only allow the 68000 to alter the contents of this memory address.
or a
or a
jr z,main ;just keep looping until it's nonzero.
jr z,main ;just keep looping until it's nonzero.


;by counting the bytes each instruction takes, it can be proven that this label points to &006C.
smc: ;clobber by writing to &A0006D
call &0000 ;we'll overwrite the operand with whatever function we want to call.
;The call opcode takes 1 byte and the operand that follows takes two bytes.

smc:
call &0000 ;we'll overwrite the operand at &006D-&006E with whatever function we want to call.


done:
done: