( logic )

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

%2*  { #10 SFT } %2/  { #01 SFT }
%4*  { #20 SFT } %4/  { #02 SFT }
%8*  { #30 SFT } %8/  { #03 SFT }
%10* { #40 SFT } %10/ { #04 SFT }
%20* { #50 SFT } %20/ { #05 SFT }

%2**  { #10 SFT2 } %2//  { #01 SFT2 }
%4**  { #20 SFT2 } %4//  { #02 SFT2 }
%8**  { #30 SFT2 } %8//  { #03 SFT2 }
%10** { #40 SFT2 } %10// { #04 SFT2 }
%20** { #50 SFT2 } %20// { #05 SFT2 }

%RELEASE-MOUSE { #0096 DEO }
%AUTO-X { #01 .Screen/auto DEO }
%AUTO-X-ADDR { #05 .Screen/auto DEO }
%AUTO-NONE { #00 .Screen/auto DEO }

%RTN { JMP2r }
%TOS { #00 SWP }

( 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 ]
|40 @Audio1     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
|50 @Audio2     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
|60 @Audio3     [ &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 ]
|c0 @DateTime   [ &year   $2 &month    $1 &day    $1 &hour  $1 &minute $1 &second $1 &dotw $1 &doty   $2 &isdst $1 ]

( variables )

|0000

@a-frame
	&x $2 &y $2 &x2 $2 &y2 $2
@b-frame
	&x $2 &y $2 &x2 $2 &y2 $2
@input
	&a $1 &b $1
@pointer
	&x $2 &y $2

( program )

|0100 ( -> )

	( theme ) 
	#0fe7 .System/r DEO2 
	#0fce .System/g DEO2 
	#0f2c .System/b DEO2

	;on-mouse   .Mouse/vector DEO2

	.Screen/width DEI2 2// 
		DUP2 #0040 -- .a-frame/x STZ2 DUP2 #0040 ++ .a-frame/x2 STZ2
		DUP2 #0040 -- .b-frame/x STZ2 #0040 ++ .b-frame/x2 STZ2

	.Screen/height DEI2 2// #0020 --
		DUP2 #0010 -- .a-frame/y STZ2 DUP2 #0000 ++ .a-frame/y2 STZ2
		DUP2 .b-frame/y STZ2 #0010 ++ .b-frame/y2 STZ2

	;redraw JSR2

BRK

@on-mouse ( -> )

	;draw-cursor JSR2 
	
	.Mouse/state DEI #00 ! JMP [ BRK ]

	.Mouse/x DEI2 .Mouse/y DEI2 .a-frame ;within-rect JSR2 
		;on-touch-a JCN2
	.Mouse/x DEI2 .Mouse/y DEI2 .b-frame ;within-rect JSR2 
		;on-touch-b JCN2

BRK

@on-touch-a ( -> )

	.Mouse/x DEI2 .a-frame/x LDZ2 -- 
	10// NIP #07 SWP - STH

	.input/a LDZ 
		#01 [ STHr #40 SFT ] SFT EOR 
		.input/a STZ

	;redraw JSR2
	RELEASE-MOUSE

BRK

@on-touch-b ( -> )

	.Mouse/x DEI2 .b-frame/x LDZ2 -- 
	10// NIP #07 SWP - STH

	.input/b LDZ 
		#01 [ STHr #40 SFT ] SFT EOR 
		.input/b STZ

	;redraw JSR2
	RELEASE-MOUSE

BRK

@redraw ( -- )

	.a-frame/x LDZ2  .Screen/x DEO2
	.a-frame/y LDZ2  .Screen/y DEO2
	.input/a LDZ #01 ;draw-byte JSR2

	.b-frame/x LDZ2 .Screen/x DEO2
	.b-frame/y LDZ2 .Screen/y DEO2
	.input/b LDZ #01 ;draw-byte JSR2

	.b-frame/x LDZ2 .Screen/x DEO2
	.Screen/y DEI2 #000d ++ .Screen/y DEO2
	.input LDZ2 AND #03 ;draw-byte JSR2

	.b-frame/x LDZ2 .Screen/x DEO2
	.Screen/y DEI2 #000d ++ .Screen/y DEO2
	.input LDZ2 ORA #03 ;draw-byte JSR2

	.b-frame/x LDZ2 .Screen/x DEO2
	.Screen/y DEI2 #000d ++ .Screen/y DEO2
	.input LDZ2 EOR #03 ;draw-byte JSR2

	( labels )
	AUTO-X-ADDR
	.b-frame/x LDZ2 #0020 -- .Screen/x DEO2
	;names-icn/and .Screen/addr DEO2
	.b-frame/y2 LDZ2 #0004 ++ .Screen/y DEO2
	,&draw-label JSR
	.b-frame/y2 LDZ2 #0014 ++ .Screen/y DEO2
	,&draw-label JSR
	.b-frame/y2 LDZ2 #0024 ++ .Screen/y DEO2
	,&draw-label JSR
	AUTO-NONE

RTN
	&draw-label
		.b-frame/x LDZ2 #0020 -- .Screen/x DEO2 
		#03 .Screen/sprite DEOk DEOk DEO
	RTN

@draw-byte ( value -- )

	STH STH
	#0800
	&loop
		DUP #07 SWP -
		STHkr SWP SFT #01 AND OVRr STHr ;draw-bit JSR2
		INC GTHk ,&loop JCN
	POP2
	.Screen/y DEI2 #0003 ++ .Screen/y DEO2
	.Screen/x DEI2 #0008 ++ .Screen/x DEO2
	STHr ;draw-hex JSR2
	POPr

RTN

@draw-bit ( value color -- )

	STH STH

	AUTO-X-ADDR
	;button-icns/off [ #00 STHkr 20* ++ ] .Screen/addr DEO2
	OVRr STHr .Screen/sprite DEO
	OVRr STHr .Screen/sprite DEO
	.Screen/y DEI2 #0008 ++ .Screen/y DEO2
	.Screen/x DEI2 #0010 -- .Screen/x DEO2
	OVRr STHr .Screen/sprite DEO
	OVRr STHr .Screen/sprite DEO
	AUTO-NONE
	.Screen/y DEI2 #0008 -- .Screen/y DEO2

	POPr POPr

RTN

@draw-hex ( value -- )

	AUTO-X
	DUP #04 SFT ,&draw JSR
	#0f AND ,&draw JSR
	AUTO-X

RTN
	&draw
		TOS 8** ;font-hex ++ .Screen/addr DEO2
		( draw ) #01 .Screen/sprite DEO
	RTN

@draw-cursor ( -- )
	
	( clear last cursor )
	;cursor .Screen/addr DEO2 
	.pointer/x LDZ2 .Screen/x DEO2 
	.pointer/y LDZ2 .Screen/y DEO2 
	#40 .Screen/sprite DEO
	( record pointer positions )
	.Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2 
	.Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2  
	( colorize on state )
	#42 [ .Mouse/state DEI #00 ! ] + .Screen/sprite DEO

RTN

@within-rect ( x* y* rect -- flag )
	
	STH
	( y < rect.y1 ) DUP2 STHkr #02 ADD LDZ2 LTH2 ,&skip JCN
	( y > rect.y2 ) DUP2 STHkr #06 ADD LDZ2 GTH2 ,&skip JCN
	SWP2
	( x < rect.x1 ) DUP2 STHkr LDZ2 LTH2 ,&skip JCN
	( x > rect.x2 ) DUP2 STHkr #04 ADD LDZ2 GTH2 ,&skip JCN
	POP2 POP2 POPr
	#01 
RTN
	&skip
	POP2 POP2 POPr
	#00

RTN

@cursor 
	80c0 e0f0 f8e0 1000 

@button-icns
	&off
	3f40 8080 8080 8080
	f804 0202 0202 0202
	8080 8080 8040 3f00
	0202 0202 0204 f800
	&on
	3f40 9fbf bfbf bfbf
	f804 f2fa fafa fafa
	bfbf bfbf 9f40 3f00
	fafa fafa f204 f800

@names-icn
	&and
	fc02 027e 8286 fa00
	bcc2 8282 8282 8200
	fc82 8282 8282 fc00
	&ora
	7c82 8282 8282 7c00
	fc82 82fc 8282 8200
	fc02 027e 8286 fa00
	&eor
	8282 4438 4482 8200
	7c82 8282 8282 7c00
	fc82 82fc 8282 8200

@font-hex ( 0-F )
	007c 8282 8282 827c 0030 1010 1010 1010
	007c 8202 7c80 80fe 007c 8202 1c02 827c
	000c 1424 4484 fe04 00fe 8080 7c02 827c
	007c 8280 fc82 827c 007c 8202 1e02 0202
	007c 8282 7c82 827c 007c 8282 7e02 827c
	007c 8202 7e82 827e 00fc 8282 fc82 82fc
	007c 8280 8080 827c 00fc 8282 8282 82fc
	007c 8280 f080 827c 007c 8280 f080 8080