c65gm/examples/script_library_demo/script_library_demo.c65

85 lines
2.1 KiB
Text

//-----------------------------------------------------------
// SCRIPT LIBRARY Demo
// Demonstrates reusable Starlark functions defined in
// SCRIPT LIBRARY blocks and called from SCRIPT blocks
//-----------------------------------------------------------
#INCLUDE <c64start.c65>
GOTO start
//-----------------------------------------------------------
// SCRIPT LIBRARY: Define reusable code generation functions
//-----------------------------------------------------------
SCRIPT LIBRARY
def emit_nops(count):
for i in range(count):
print(" nop")
def emit_delay(cycles):
# 4 cycles per iteration (2x nop)
for i in range(cycles // 4):
print(" nop")
print(" nop")
remainder = cycles % 4
if remainder >= 3:
print(" bit $ea")
remainder -= 3
# 2 cycles per nop
for i in range(remainder // 2):
print(" nop")
ENDSCRIPT
//-----------------------------------------------------------
// Second SCRIPT LIBRARY: Functions accumulate
//-----------------------------------------------------------
SCRIPT LIBRARY
def emit_border_flash(color1, color2):
print(" lda #%d" % color1)
print(" sta $d020")
print(" lda #%d" % color2)
print(" sta $d020")
def emit_load_store(value, addr):
print(" lda #%d" % value)
print(" sta %d" % addr)
ENDSCRIPT
//-----------------------------------------------------------
// Main code using library functions
//-----------------------------------------------------------
LABEL start
// Use emit_nops from first library
SCRIPT
print("; 5 nops from library")
emit_nops(5)
ENDSCRIPT
// Use emit_delay from first library
SCRIPT
print("; 10 cycle delay")
emit_delay(10)
ENDSCRIPT
// Use emit_border_flash from second library
SCRIPT
print("; border flash red/blue")
emit_border_flash(2, 6)
ENDSCRIPT
// Combine multiple library functions
SCRIPT
print("; combined: delay + flash + nops")
emit_delay(8)
emit_border_flash(0, 1)
emit_nops(3)
ENDSCRIPT
// Use emit_load_store
SCRIPT
print("; load/store to border")
emit_load_store(5, 0xd020)
ENDSCRIPT
SUBEND