Skip to content

Instantly share code, notes, and snippets.

@nihirash
Created March 27, 2023 10:15
Show Gist options
  • Select an option

  • Save nihirash/f3b93db13c9a8042de2dd27185f9bb37 to your computer and use it in GitHub Desktop.

Select an option

Save nihirash/f3b93db13c9a8042de2dd27185f9bb37 to your computer and use it in GitHub Desktop.
Threaded code interpreter for Z80
repeat_proc:
getWord
ld (.count + 1), de
pop de
push de
call Interpreter.push
.count
ld de, 0
call Interpreter.push
next
endloop_proc:
call Interpreter.pop
dec de
ld a, d : or e : jr z, .ended
call Interpreter.push
pop hl
call Interpreter.peek2
push de
next
.ended
drop 1
next
vsync_proc:
halt
next
exit_proc:
ret
include "macro.asm"
module Interpreter
stack_len = 20
; Execute code after call
enter:
pop hl
.inHL
ld e, (hl)
inc hl
ld d, (hl)
inc hl
push hl
ex hl, de
jp (hl)
; DE -> dataStack
push:
ld hl, (dataSP)
ld (hl), e
inc hl : ld (hl), d
inc hl
ld (dataSP), hl
ret
; dataStack -> DE
pop:
ld hl, (dataSP)
dec hl : ld d, (hl)
dec hl : ld e, (hl)
ld (dataSP), hl
ret
; dataStack prev value -> DE but value keeps in stack
peek2:
ld hl, (dataSP)
dec hl : dec hl: dec hl
ld d, (hl)
dec hl : ld e, (hl)
ret
dataSP dw dataStack
endmodule
macro WithoutArgs
dw withoutArgs
endm
macro PutText txt
dw putText
db 16-((.e-.s)/2)
.s
db txt
.e
db 0
endm
macro drop n
ld hl, (Interpreter.dataSP)
ld de, -(n * 2)
add hl, de
ld (Interpreter.dataSP), hl
endm
; DE - loaded word
macro getWord
pop hl
ld e, (hl)
inc hl : ld d, (hl)
inc hl
push hl
endm
; A - loaded byte
macro getByte
pop hl
ld a, (hl) : inc hl
push hl
endm
macro next
jp Interpreter.enter
endm
macro nextZ
jp z, Interpreter.enter
endm
macro loop_block count
dw repeat_proc, count
endm
macro end_loop
dw endloop_proc
endm
macro vsync
dw vsync_proc
endm
macro exit
dw exit_proc
endm
macro goto addr
exit
ld hl, addr
jp Interpreter.enter.inHL
endm
macro GoSub addr
exit
ld hl, addr
call Interpreter.enter.inHL
call Interpreter.enter
endm
macro return
exit
ret
endm
;; ....code....
call Interpreter.enter
demo:
PutText "Some text :-)"
loop_block 8
GoSub rayRun
AddRay
end_loop
exit
rst 0
doubleRun:
GoSub rayRun
rayRun:
loop_block 40
loop_block 4
vsync
ProcessingRays
end_loop
ProcessRunningMan
end_loop
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment