.include "lib/macros.xyw"
print-prompt JSRw
setup-handlers JSRw
HLT
.include "lib/putd.xyw"
.include "lib/puts.xyw"
.label setup-handlers
on-keypress terminal.on_keypress STW
on-timer clock.on_timer STW
$0100 clock.timer STW
RTS
.label on-timer
redraw-line JSRw
$0100 clock.timer STW
RTS
.label on-keypress
terminal.input LDB
DUP $0D EQU on-enter JCNw
DUP $0A EQU on-enter JCNw
DUP $7F EQU on-backspace JCNw
DUP $08 EQU on-backspace JCNw
DUP $20 LTH on-ignore JCNw
buf-len LDBw $30 EQU on-ignore JCNw
DUP
buf-len LDBw $00 SWP buf ADDw STBw
buf-len LDBw INC buf-len STBw
PUTC
RTS
.label on-ignore
POP RTS
.label on-enter
POP
NL
echo-prefix puts JSRw
print-buffer JSRw
NL
$00 buf-len STBw
print-prompt JSRw
RTS
.label on-backspace
POP
buf-len LDBw $00 EQU on-backspace.done JCNw
buf-len LDBw DEC buf-len STBw
$08 PUTC $20 PUTC $08 PUTC
.label on-backspace.done
RTS
.label print-prompt
$5B PUTC
clock.hour LDB ZEROPADx JSRw
$3A PUTC
clock.minute LDB ZEROPADx JSRw
$3A PUTC
clock.second LDB ZEROPADx JSRw
$5D PUTC SP $3E PUTC SP
RTS
.label redraw-line
$0D PUTC
print-prompt JSRw
print-buffer JSRw
$1B PUTC $5B PUTC $4B PUTC
RTS
.label print-buffer
buf POPxw
buf-len LDBw POPy
.label print-buffer.loop
PSHy $00 EQU print-buffer.done JCNw
LDBxw PUTC
INCxw DECy
print-buffer.loop JMPw
.label print-buffer.done
RTS
.label ZEROPADx
POPx PSHx $0A LTH zeropad.zero JCNw
.label zeropad.digit
PSHx putd JSRw
RTS
.label zeropad.zero
$30 PUTC
zeropad.digit JMPw
.label echo-prefix
.string "
.label buf-len
.byte $00
.label buf
.byte $00 $00 $00 $00 $00 $00 $00 $00
.byte $00 $00 $00 $00 $00 $00 $00 $00
.byte $00 $00 $00 $00 $00 $00 $00 $00
.byte $00 $00 $00 $00 $00 $00 $00 $00
.byte $00 $00 $00 $00 $00 $00 $00 $00
.byte $00 $00 $00 $00 $00 $00 $00 $00