( font )

%+  { ADD } %-   { SUB }  %*  { MUL }  %/  { DIV }
%<  { LTH } %>   { GTH }  %=  { EQU }  %!  { NEQ }
%++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 }
%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }

%RTN { JMP2r }
%TOS { #00 SWP }
%BRK? { #01 JCN BRK }
%RTN? { #01 JCN RTN }

%DEBUG  { ;print-hex/byte JSR2 #0a .Console/write DEO }
%DEBUG2 { ;print-hex/short JSR2 #0a .Console/write DEO }

%HEX-CHAR { DUP #09 GTH #04 JCN #30 + RTN #57 + }

( devices )

|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g      $2 &b    $2 &debug  $1 &halt $1 ]
|10 @Console    [ &vector $2 &read     $1 &pad    $5 &write $1 &error  $1 ]
|20 @Screen  &vector $2 &width   $2 &height $2 &auto $1 &pad $1 &x      $2 &y      $2 &addr $2 &pixel $1 &sprite $1
|80 @Controller [ &vector $2 &button $1 &key    $1 ]
|90 @Mouse      [ &vector $2 &x        $2 &y      $2 &state $1 &wheel  $1 ]
|a0 @File    &vector $2 &success $2 &offset-hs $2 &offset-ls $2 &name $2 &length $2 &load $2 &save $2
|b0 @DateTime   [ &year   $2 &month    $1 &day    $1 &hour  $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ]

( variables )

|0000

@input $30
@second $1
@pointer
	&x  $2 &y  $2

( init )

|0100 ( -> )

	( theme ) 
	#e077 .System/r DEO2 
	#e00c .System/g DEO2 
	#e02a .System/b DEO2

	( vectors )
	;on-frame .Screen/vector DEO2
	;on-button .Controller/vector DEO2
	;on-mouse .Mouse/vector DEO2

	( theme support )
	;load-theme JSR2
	
BRK

@on-frame ( -> )

	.DateTime/second DEI .second LDZ ! BRK?
	.DateTime/second DEI .second STZ ;draw-time JSR2

BRK

@on-mouse ( -> )

	;pointer-icn .Screen/addr DEO2
	( clear last cursor )
	.pointer/x LDZ2 .Screen/x DEO2
	.pointer/y LDZ2 .Screen/y DEO2
	#40 .Screen/sprite DEO

	( record pointer positions )
	.Mouse/x DEI2 .pointer/x STZ2 
	.Mouse/y DEI2 .pointer/y STZ2

	( draw new cursor )
	.pointer/x LDZ2 .Screen/x DEO2
	.pointer/y LDZ2 .Screen/y DEO2
	#41 .Mouse/state DEI #01 = + .Screen/sprite DEO

BRK

@on-button ( -> )

	.Controller/key DEI BRK?

	.Controller/key DEI
	DUP #08 ! ,&no-backspace JCN
		;form/erase JSR2 POP BRK
		&no-backspace
	DUP #0d ! ,&no-enter JCN
		;form/validate JSR2 POP BRK
		&no-enter
	;form/append JSR2

BRK

@form ( -- )

	&append ( char -- )
		,&seek JSR STH2k NIP #30 < ,&can-append JCN
			POP POP2r RTN
			&can-append
		( save ) STH2kr STA
		( null ) #00 STH2r INC2 STA
		#07 ,&draw JSR
	RTN

	&validate ( -- )
		;input ;load-rom JSR2
		#00 ,&draw JSR
		( null ) #0000 ;input STA2
	RTN

	&erase ( -- )
		;input LDA2 #0000 !! ,&can-erase JCN
			RTN
			&can-erase
		#00 ,&draw JSR
		( remove ) #00 ,&seek JSR #0001 -- STA
		#07 ,&draw JSR
	RTN

	&seek ( -- end* )
		;input #0001 -- 
		&seek-loop 
			INC2 LDAk ,&seek-loop JCN
	RTN

	&draw ( color -- )
		STH
		#0038 #00c0 ;input STHkr ;draw-label JSR2
		STHr ;draw-cursor JSR2
	RTN

RTN

@redraw ( -- )

	#0018 #0020 #0040 #0040 ;logo-icn #03 ;draw-icn JSR2
	#0020 #0070 ;welcome-txt #01 ;draw-label JSR2
	;draw-time JSR2
	#0020 #00b0 ;prompt-txt #01 ;draw-label JSR2
	#0020 #00c0 ;do-txt #03 ;draw-label JSR2
	#03 ;form/draw JSR2

RTN

@draw-time ( -- )

	.DateTime/day DEI
		DUP #0f AND ;hex-char JSR2 ;&date-str #0009 ++ STA
		#04 SFT ;hex-char JSR2 ;&date-str #0008 ++ STA
	.DateTime/month DEI
		DUP #0f AND ;hex-char JSR2 ;&date-str #0006 ++ STA
		#04 SFT ;hex-char JSR2 ;&date-str #0005 ++ STA
	.DateTime/year DEI2
		DUP #0f AND ;hex-char JSR2 ;&date-str #0003 ++ STA
		#04 SFT ;hex-char JSR2 ;&date-str #0002 ++ STA
		DUP #0f AND ;hex-char JSR2 ;&date-str INC2 STA
		#04 SFT ;hex-char JSR2 ;&date-str STA

	.DateTime/second DEI
		DUP #0f AND ;hex-char JSR2 ;&time-str #0007 ++ STA
		#04 SFT ;hex-char JSR2 ;&time-str #0006 ++ STA
	.DateTime/minute DEI
		DUP #0f AND ;hex-char JSR2 ;&time-str #0004 ++ STA
		#04 SFT ;hex-char JSR2 ;&time-str #0003 ++ STA
	.DateTime/hour DEI
		DUP #0f AND ;hex-char JSR2 ;&time-str INC2 STA
		#04 SFT ;hex-char JSR2 ;&time-str STA

	#0020 #0080 ;&date-str #01 ;draw-label JSR2
	#0080 #0080 ;&time-str #02 ;draw-label JSR2

RTN
	&date-str "0000-00-00 $1
	&time-str "00:00:00 $1

@hex-char ( hex -- char )

	HEX-CHAR

RTN

@draw-cursor ( color -- )

	STH
	;cursor-icn .Screen/addr DEO2
	STHkr .Screen/sprite DEO
	.Screen/y DEI2 #0008 ++ .Screen/y DEO2
	STHr .Screen/sprite DEO

RTN

@draw-icn ( x* y* width* height* addr* color  -- )
	
	( load ) STH .Screen/addr DEO2 ,&height STR2 ,&width STR2 ,&y STR2 ,&x STR2
	,&height LDR2 #0000 
	&ver
		( save ) DUP2 ,&y LDR2 ADD2 .Screen/y DEO2
		,&width LDR2 #0000
		&hor
			( save ) DUP2 ,&x LDR2 ADD2 .Screen/x DEO2
			( draw ) STHkr .Screen/sprite DEO
			( incr ) .Screen/addr DEI2 #0008 ADD2 .Screen/addr DEO2
			#0008 ADD2 GTH2k ,&hor JCN
		POP2 POP2
		#0008 ADD2 GTH2k ,&ver JCN
	POP2 POP2
	POPr

RTN
	&x $2 &y $2 &width $2 &height $2

@draw-label ( x* y* addr* color -- )

	STH STH2
	.Screen/y DEO2
	.Screen/x DEO2
	STH2r
	&loop
		( top-addr ) LDAk #20 - TOS #0010 MUL2 ;font ++ .Screen/addr DEO2
		( top-draw ) STHkr .Screen/sprite DEO
		( bottom-addr ) .Screen/addr DEI2 #0008 ++ .Screen/addr DEO2
		( next-y ) .Screen/y DEI2 #0008 ++ .Screen/y DEO2
		( bottom-draw ) STHkr .Screen/sprite DEO
		( prev-y ) .Screen/y DEI2 #0008 -- .Screen/y DEO2
		( move ) .Screen/x DEI2 #0008 ++ .Screen/x DEO2
		INC2 LDAk ,&loop JCN
	POP2 POPr

RTN

( theme )

@theme-txt ".theme $1

@load-theme ( -- )

	;theme-txt .File/name DEO2 
	#0006 .File/length DEO2 
	#fffa .File/load DEO2

	.File/success DEI2 #0006 !! ,&ignore JCN
		#fffa LDA2 .System/r DEO2
		#fffc LDA2 .System/g DEO2
		#fffe LDA2 .System/b DEO2
		&ignore
	;redraw JSR2

RTN

( helpers )

@print-hex ( value* -- )
	
	&short ( value* -- )
		SWP ,&echo JSR 
	&byte ( value -- )
		,&echo JSR
	RTN

	&echo ( value -- )
	STHk #04 SFT ,&parse JSR .Console/write DEO
	STHr #0f AND ,&parse JSR .Console/write DEO
	RTN
	&parse ( value -- char )
		DUP #09 GTH ,&above JCN #30 + RTN &above #09 - #60 + RTN

RTN

include projects/library/load-rom.tal

@welcome-txt
	"Welcome 20 "to 20 "your 20 "UXN 20 "Ordinator $1
@today-txt
	"Today 20 "is 20 "0000-00-00 ", 20 "the 20 "time 20 "is 20 "00:00:00 $1
@prompt-txt
	"What 20 "shall 20 "we 20 "do? $1
@do-txt
	"$: $1

@error-txt
	"Sorry, 20 "ROM 20 "not 20 "found. $1

@pointer-icn
	80c0 e0f0 f8e0 1000
@cursor-icn
	ffff ffff ffff ffff

@logo-icn
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0003
	0000 0000 0000 40c1 0000 0000 0000 7cff
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 1f7f 0707 0707 0707
	c3c7 dcf0 e0e0 e0e0 ff1f 0707 0303 0303
	80c0 c0e0 e0e0 e0e0 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0707 0707 0707 0707
	e0e0 e0e0 e0e0 e0e0 0303 0303 0303 0303
	e0e0 e0e0 e0e0 e0e0 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0707 0707 0707 070f
	e0e0 e0e0 e0e0 e0f0 0303 0303 0303 0707
	e0e0 e0e0 e0e0 f0f8 0000 0000 0000 0000
	0000 0000 0000 0000 7f1f 0f07 0707 0707
	fce0 e0c0 c0c0 c0c0 7f0f 0707 0707 0707
	fef0 e0e0 e0e0 e0e0 3f00 0000 0000 0000
	fe00 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0707 0707 0707 0707
	c0c0 c0c0 c0c0 c0c0 0707 0707 0707 0707
	e0e0 e0e0 e0e0 e0e0 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0707 0707 0707 0303
	c0c0 c0c0 c0e0 e0f8 0707 0707 070f 3be3
	e0e0 e0e0 e0e0 e0fe 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0100 0000 0000 0000
	ffff 3e00 0000 0000 c383 0200 0000 0000
	f8c0 0000 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000


@font
	0000 0000 0000 0000 0000 0000 0000 0000
	0000 183c 3c3c 1818 1800 1818 0000 0000
	0066 6666 2400 0000 0000 0000 0000 0000
	0000 006c 6cfe 6c6c 6cfe 6c6c 0000 0000
	1818 7cc6 c2c0 7c06 0686 c67c 1818 0000 
	0000 0000 c2c6 0c18 3060 c686 0000 0000
	0000 386c 6c38 76dc cccc cc76 0000 0000
	0030 3030 6000 0000 0000 0000 0000 0000 
	0000 0c18 3030 3030 3030 180c 0000 0000
	0000 3018 0c0c 0c0c 0c0c 1830 0000 0000
	0000 0000 0066 3cff 3c66 0000 0000 0000
	0000 0000 0018 187e 1818 0000 0000 0000
	0000 0000 0000 0000 0018 1818 3000 0000 
	0000 0000 0000 00fe 0000 0000 0000 0000 
	0000 0000 0000 0000 0000 1818 0000 0000 
	0000 0000 0206 0c18 3060 c080 0000 0000
	0000 386c c6c6 d6d6 c6c6 6c38 0000 0000
	0000 1838 7818 1818 1818 187e 0000 0000
	0000 7cc6 060c 1830 60c0 c6fe 0000 0000
	0000 7cc6 0606 3c06 0606 c67c 0000 0000
	0000 0c1c 3c6c ccfe 0c0c 0c1e 0000 0000
	0000 fec0 c0c0 fc06 0606 c67c 0000 0000
	0000 3860 c0c0 fcc6 c6c6 c67c 0000 0000
	0000 fec6 0606 0c18 3030 3030 0000 0000
	0000 7cc6 c6c6 7cc6 c6c6 c67c 0000 0000
	0000 7cc6 c6c6 7e06 0606 0c78 0000 0000
	0000 0000 1818 0000 0018 1800 0000 0000
	0000 0000 1818 0000 0018 1830 0000 0000
	0000 0006 0c18 3060 3018 0c06 0000 0000
	0000 0000 007e 0000 7e00 0000 0000 0000
	0000 0060 3018 0c06 0c18 3060 0000 0000
	0000 7cc6 c60c 1818 1800 1818 0000 0000
	0000 007c c6c6 dede dedc c07c 0000 0000
	0000 1038 6cc6 c6fe c6c6 c6c6 0000 0000
	0000 fc66 6666 7c66 6666 66fc 0000 0000
	0000 3c66 c2c0 c0c0 c0c2 663c 0000 0000
	0000 f86c 6666 6666 6666 6cf8 0000 0000
	0000 fe66 6268 7868 6062 66fe 0000 0000
	0000 fe66 6268 7868 6060 60f0 0000 0000
	0000 3c66 c2c0 c0de c6c6 663a 0000 0000
	0000 c6c6 c6c6 fec6 c6c6 c6c6 0000 0000
	0000 3c18 1818 1818 1818 183c 0000 0000
	0000 1e0c 0c0c 0c0c cccc cc78 0000 0000
	0000 e666 666c 7878 6c66 66e6 0000 0000
	0000 f060 6060 6060 6062 66fe 0000 0000
	0000 c6ee fefe d6c6 c6c6 c6c6 0000 0000
	0000 c6e6 f6fe dece c6c6 c6c6 0000 0000
	0000 7cc6 c6c6 c6c6 c6c6 c67c 0000 0000
	0000 fc66 6666 7c60 6060 60f0 0000 0000
	0000 7cc6 c6c6 c6c6 c6d6 de7c 0c0e 0000
	0000 fc66 6666 7c6c 6666 66e6 0000 0000
	0000 7cc6 c660 380c 06c6 c67c 0000 0000
	0000 7e7e 5a18 1818 1818 183c 0000 0000
	0000 c6c6 c6c6 c6c6 c6c6 c67c 0000 0000
	0000 c6c6 c6c6 c6c6 c66c 3810 0000 0000
	0000 c6c6 c6c6 d6d6 d6fe ee6c 0000 0000
	0000 c6c6 6c7c 3838 7c6c c6c6 0000 0000
	0000 6666 6666 3c18 1818 183c 0000 0000
	0000 fec6 860c 1830 60c2 c6fe 0000 0000
	0000 3c30 3030 3030 3030 303c 0000 0000
	0000 0080 c0e0 7038 1c0e 0602 0000 0000
	0000 3c0c 0c0c 0c0c 0c0c 0c3c 0000 0000
	1038 6cc6 0000 0000 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 00ff 0000
	3030 1800 0000 0000 0000 0000 0000 0000
	0000 0000 0078 0c7c cccc cc76 0000 0000
	0000 e060 6078 6c66 6666 667c 0000 0000
	0000 0000 007c c6c0 c0c0 c67c 0000 0000
	0000 1c0c 0c3c 6ccc cccc cc76 0000 0000
	0000 0000 007c c6fe c0c0 c67c 0000 0000
	0000 386c 6460 f060 6060 60f0 0000 0000
	0000 0000 0076 cccc cccc cc7c 0ccc 7800
	0000 e060 606c 7666 6666 66e6 0000 0000
	0000 1818 0038 1818 1818 183c 0000 0000
	0000 0606 000e 0606 0606 0606 6666 3c00
	0000 e060 6066 6c78 786c 66e6 0000 0000
	0000 3818 1818 1818 1818 183c 0000 0000
	0000 0000 00ec fed6 d6d6 d6c6 0000 0000
	0000 0000 00dc 6666 6666 6666 0000 0000
	0000 0000 007c c6c6 c6c6 c67c 0000 0000
	0000 0000 00dc 6666 6666 667c 6060 f000
	0000 0000 0076 cccc cccc cc7c 0c0c 1e00
	0000 0000 00dc 7666 6060 60f0 0000 0000
	0000 0000 007c c660 380c c67c 0000 0000
	0000 1030 30fc 3030 3030 361c 0000 0000
	0000 0000 00cc cccc cccc cc76 0000 0000
	0000 0000 0066 6666 6666 3c18 0000 0000
	0000 0000 00c6 c6d6 d6d6 fe6c 0000 0000
	0000 0000 00c6 6c38 3838 6cc6 0000 0000
	0000 0000 00c6 c6c6 c6c6 c67e 060c f800
	0000 0000 00fe cc18 3060 c6fe 0000 0000
	0000 0e18 1818 7018 1818 180e 0000 0000
	0000 1818 1818 0018 1818 1818 0000 0000
	0000 7018 1818 0e18 1818 1870 0000 0000