( launcher )

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

%8// { #03 SFT2 }
%10** { #40 SFT2 } %10// { #04 SFT2 }

%MIN2 { LTH2k JMP SWP2 POP2 }
%MOD { DIVk MUL SUB }
%DEC { #01 - }
%RTN { JMP2r }
%TOS { #00 SWP }
%SWP? { #01 JCN SWP } %SWP2? { #01 JCN SWP2 }
%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 }

%SEL-ENTRY { ;dir/entries #00 .browser/sel LDZ DUP2 ADD2 ADD2 }

( 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
|30 @Audio0     &vector $2 &position $2 &output $1 &pad    $3 &adsr   $2 &length $2 &addr $2 &volume $1 &pitch $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 &stat   $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
|b0 @DateTime   &year   $2 &month    $1 &day    $1 &hour   $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 

( variables )

|0000

@pointer
	&x  $2 &y  $2
@browser
	&x $2 &y $2 
	&sel $1 &last $1

( init )

|0100 ( -> )

	( theme ) 
	#f077 .System/r DEO2 
	#f00c .System/g DEO2 
	#f02a .System/b DEO2

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

	( setup synth )
	#0102 .Audio0/adsr DEO2
	;sin-pcm .Audio0/addr DEO2
	#0100 .Audio0/length DEO2
	#dd .Audio0/volume DEO ( TODO: turn ON )

	( determine max visible file length )
	.Screen/width DEI2 8// #00ff MIN2 NIP
	;draw-browser/clear-length STA

	( place )
	#0088 .browser/x STZ2
	#0010 .browser/y STZ2

	( draw mascot )
	#0010 #0010 #0060 #0060 ;mascot-icn #01 ;draw-icn JSR2

	;read-dir JSR2

	( theme support )
	;load-theme JSR2

	( force selection )
	#ff .browser/last STZ
	#00 ;select-file JSR2

BRK

@on-frame ( -> )

	( unused )

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

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

	( select choice )
	.Mouse/y DEI2 .browser/y LDZ2 --
		10// NIP ;select-file JSR2

	.Mouse/state DEI #00 = ,&no-click JCN
		.browser/sel LDZ ;run-file JSR2
		&no-click

BRK

@on-button ( -> )

	.Controller/button DEI
	DUP #10 ! ,&no-up JCN
		.browser/sel LDZ DEC ;select-file JSR2
		POP BRK
		&no-up
	DUP #20 ! ,&no-down JCN
		.browser/sel LDZ INC ;select-file JSR2
		POP BRK
		&no-down
	DUP #01 ! ,&no-a JCN
		.browser/sel LDZ ;run-file JSR2
		POP BRK
		&no-a
	POP
	.Controller/key DEI
	DUP #0d ! ,&no-enter JCN
		.browser/sel LDZ ;run-file JSR2
		POP
		&no-enter
	POP

BRK

@read-dir ( -- )

	;dir/path .File/name DEO2
	#1000 .File/length DEO2
	;dir/data .File/read DEO2

	( separate into lines )
	#00 ;dir/data STH2k
	&while
		LDAk #0a ! ,&no-lb JCN
		STAk
		STH2r ;add-entry JSR2
		INC2 STH2k ,&while JMP
		&no-lb INC2 LDAk ,&while JCN
	POP2r POP2 POP
RTN

@add-entry ( line* -- )
	DUP2 ;filter-entry JSR2 ,&ignored JCN

	( just add entry to end for now, FIXME sort entries )
	;dir/entries
		#00 ;dir/lines LDA DUP2 ADD2
		ADD2
	STA2
	;dir/lines LDAk INC ROT ROT STA
	#0000

	&ignored
	POP2
RTN

@filter-entry ( line* -- ignore-flag )
	POP2 #00
RTN

@select-file ( id -- )
	( clamp id to useful values )
	DUP #fc LTH ,&not-negative JCN
	DUP EOR
	&not-negative
	;dir/lines LDA DEC LTHk SWP? POP

	DUP .browser/last LDZ ! ,&has-changed JCN
		POP RTN
		&has-changed

	DUP ;scroll-browser JSR2

	.browser/x LDZ2 #0018 -- .browser/y LDZ2 [ .browser/sel LDZ TOS 10** ++ ] 
	#0010 #0010 
		;hand-icn #00 ;draw-icn JSR2

	DUP 
		.browser/sel STZ
	DUP
		.browser/last STZ
		#30 + .Audio0/pitch DEO
	;draw-browser JSR2

	.browser/x LDZ2 #0018 -- .browser/y LDZ2 [ .browser/sel LDZ TOS 10** ++ ] 
	#0010 #0010 
		;hand-icn #02 ;draw-icn JSR2

	#0010 #0010 #0060 #0060 
	;mascot-icn [ .browser/sel LDZ #03 AND TOS #0480 ** ++ ] #01 
		;draw-icn JSR2

RTN

( Scroll the browser to accomodate off-screen line items )
@scroll-browser ( id -- )
	( Check whether the selection moved up or down )
	.browser/last LDZ OVR SWP SUB #80 AND ,&negative JCN

	&positive ( The selection moved down )
	( Calculate the id currently at the bottom of the screen )
	.Screen/height DEI2 .browser/y LDZ2 #0010 ++ -- 10// NIP
	GTH ,&scroll-up JCN [ RTN ]
	&scroll-up
		.browser/y LDZ2k #0010 -- ROT STZ2
		RTN

	&negative ( The selection moved up )
	( Calculate the id currently at the top of the screen )
	( TODO make a macro constant for y initial pos )
	#0010 .browser/y LDZ2 -- 10// NIP

	LTH ,&scroll-down JCN [ RTN ]
	&scroll-down
		.browser/y LDZ2k #0010 ++ ROT STZ2 ( Decrement y by 0x10 )
		RTN

@run-file ( id -- )

	SEL-ENTRY LDA2
	#0005 ++ DUP2 ;print-string JSR2
	#0a .Console/write DEO

	DUP2 ;check-rom JSR2 ,&valid JCN
	POP2 RTN

	&valid
	;load-rom JSR2

RTN

@print-string ( addr* -- )

	&loop
		LDAk .Console/write DEO
		INC2 LDAk ,&loop JCN
	POP2

RTN

@redraw ( -- )

	( unused )

RTN

@draw-browser ( -- )

	.browser/x LDZ2 .Screen/x DEO2
	.browser/y LDZ2 .Screen/y DEO2
	SEL-ENTRY ;dir/entries
	&outer ( selected-entry* this-entry* )
		.Screen/y DEI2 #0010 LTH2 ,&inc JCN ( Don't draw anything in the first row )
		EQU2k #0c * #01 + STH
		LDA2k ORAk #00 = ,&end JCN
		,&clear JSR
		STHr ,&draw-one JSR
		.browser/x LDZ2 .Screen/x DEO2
		&inc .Screen/y DEI2 #0010 ++ .Screen/y DEO2
		INC2 INC2 ,&outer JMP
	&end
	POP2 POP2 POP2 POPr
RTN

	&draw-one ( line* color -- )
	STH
	&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
		&skip
		INC2 LDAk ,&loop JCN
	POP2 POPr
RTN

	&clear ( -- )
	.Screen/x DEI2k ( Stash the current x pos )

	( Clear the screen )
	( Clear length gets set in init )
	LIT2 &clear-length 00 00
	&clear-loop
		#00 .Screen/sprite DEO
		.Screen/y DEI2k #0008 ++ ROT DEO2
		#00 .Screen/sprite DEO
		.Screen/y DEI2k #0008 -- ROT DEO2
		.Screen/x DEI2k #0008 ++ ROT DEO2
		INC NEQk ,&clear-loop JCN
	POP2
	ROT DEO2
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 )

	DUP #09 GTH #04 JCN #30 + RTN #57 +

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/read 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

@pointer-icn
	80c0 e0f0 f8e0 1000
@hand-icn
	000c 0a0a 1224 4780
	0000 0000 0000 fc02
	8180 8080 8040 3f00
	fc20 c020 c020 c000

@sin-pcm
	8083 8689 8c8f 9295 989b 9ea1 a4a7 aaad
	b0b3 b6b9 bbbe c1c3 c6c9 cbce d0d2 d5d7
	d9db dee0 e2e4 e6e7 e9eb ecee f0f1 f2f4
	f5f6 f7f8 f9fa fbfb fcfd fdfe fefe fefe
	fffe fefe fefe fdfd fcfb fbfa f9f8 f7f6
	f5f4 f2f1 f0ee eceb e9e7 e6e4 e2e0 dedb
	d9d7 d5d2 d0ce cbc9 c6c3 c1be bbb9 b6b3
	b0ad aaa7 a4a1 9e9b 9895 928f 8c89 8683
	807d 7a77 7471 6e6b 6865 625f 5c59 5653
	504d 4a47 4542 3f3d 3a37 3532 302e 2b29
	2725 2220 1e1c 1a19 1715 1412 100f 0e0c
	0b0a 0908 0706 0505 0403 0302 0202 0202
	0102 0202 0202 0303 0405 0506 0708 090a
	0b0c 0e0f 1012 1415 1719 1a1c 1e20 2225
	2729 2b2e 3032 3537 3a3d 3f42 4547 4a4d
	5053 5659 5c5f 6265 686b 6e71 7477 7a7d

~projects/library/check-rom.tal
~projects/library/load-rom.tal

~projects/assets/mascot0cx0c.tal
~projects/assets/msx01x02.tal

@dir
	&path ". $1
	&lines $1
	&entries $100
	&data