Coroutines in Z80 assembly language


	  .bss

stack1:	  .space  stksiz
stack2:	  .space  stksiz
coswap:	  .space  2

	  .text

Initialization code:
	  ld	  sp,stack1+stksiz
	  ld	  hl,task2
	  ld	  (stack2+stksiz-2),hl
	  ld	  hl,stack2+stksiz-2
	  ld	  (coswap),hl
	  jp	  task1

swapco:	  ld	  hl,(coswap)
	  ld	  (coswap),sp
	  ld	  sp,hl
	  ret

# wait -- wait for N ticks, where N is number
# (in range 0 .. 127) in B reg.
#
wait:	  ld	  hl,ticks
	  ld	  a,(hl)
	  add	  a,b
wt1:	  cp	  (hl)
	  jp	  m,wt2
	  call	  swapco
	  jr	  wt1
wt2:	  ret

Tony Fisher / fisher@minster.york.ac.uk