( Orca 
	
	TODO
		- Synthax highlight
		- B operating doesn't loop around
		- Detect capitalization
		- Comments
		- Scale selection
		- Load/Save
		- Copy/Paste
		- Copy/paste(chorded)
)

%RTN { JMP2r }
%8+ { #0008 ADD2 }
%8* { #0008 MUL2 } %8/ { #0008 DIV2 }
%MOD { DUP2 DIV MUL SUB }

%GRID-CELLS { #2000 }
%GRID-LOCKS { #3000 }
%GRID-TYPES { #4000 }

%GET-OFFSET {
	#00 SWP #00 ~grid.width MUL2 ROT #00 SWP ADD2
} ( x y -- offset* )
%GET-INDEX { 
	GET-OFFSET GRID-CELLS ADD2 
} ( x y -- index* )
%SET-CELL { 
	ROT ROT GET-INDEX POK2 
} ( x y char -- )
%GET-CELL { 
	GET-INDEX PEK2 
} ( x y -- char )

( variables )

;timer { byte 1 frame 1 speed 1 }
;grid { width 1 height 1 }
;selection { x1 1 y1 1 x2 1 y2 1 }
;cursor { x 2 y 2 }

( devices )

|0100 ;System { vector 2 pad 6 r 2 g 2 b 2 }
|0110 ;Console { pad 8 char 1 byte 1 short 2 }
|0120 ;Screen { vector 2 width 2 height 2 pad 2 x 2 y 2 addr 2 color 1 }
|0140 ;Controller { vector 2 button 1 key 1 }
|0160 ;Mouse  { vector 2 x 2 y 2 state 1 chord 1 }

|0200

	( theme ) #08f3 =System.r #08fc =System.g #08f9 =System.b
	( vectors ) ,on-button =Controller.vector
	( vectors ) ,on-mouse =Mouse.vector
	( vectors ) ,on-frame =Screen.vector

	( find size )
	~Screen.width 8/ SWP POP =grid.width
	~Screen.height 8/ SWP POP #02 SUB =grid.height

	( fill grid with dots )

	,start JSR2
	,redraw JSR2

BRK

@on-frame

	~timer #01 ADD DUP =timer

	( skip ) #08 EQU ^$tick JNZ BRK $tick

	~timer.frame #01 ADD =timer.frame

	,run JSR2

	#00 =timer

BRK

@on-button
	
	~Controller.key #00 EQU ^$no-key JNZ
		~selection.x1 ~selection.y1 ~Controller.key SET-CELL
		~Controller.key =Console.byte
		,redraw JSR2
	$no-key

	( arrows )
	~Controller.button #f0 AND
		DUP #04 SFT #01 AND #01 NEQ ^$no-up JNZ 
			~selection.y1 #00 EQU ^$no-up JNZ
			~selection.y1 #01 SUB =selection.y1 
			~selection.y2 #01 SUB =selection.y2 $no-up
		DUP #05 SFT #01 AND #01 NEQ ^$no-down JNZ 
			~selection.y1 ~grid.height #01 SUB EQU ^$no-down JNZ
			~selection.y1 #01 ADD =selection.y1 
			~selection.y2 #01 ADD =selection.y2 $no-down
		DUP #06 SFT #01 AND #01 NEQ ^$no-left JNZ 
			~selection.x1 #00 EQU ^$no-left JNZ
			~selection.x1 #01 SUB =selection.x1 
			~selection.x2 #01 SUB =selection.x2 $no-left
		DUP #07 SFT #01 AND #01 NEQ ^$no-right JNZ 
			~selection.x1 ~grid.width #01 SUB EQU ^$no-right JNZ
			~selection.x1 #01 ADD =selection.x1 
			~selection.x2 #01 ADD =selection.x2 $no-right
	POP

	~Controller.button #04 NEQ ^$no-backspace JNZ
		~Controller.button =Console.byte
		~selection.x1 ~selection.y1 #2e SET-CELL ( put . char )
	$no-backspace

	,redraw JSR2

BRK

@on-mouse
	
	~Mouse.state #00 EQU ^$no-touch JNZ
		~Mouse.x 8/ SWP POP =selection.x1
		~Mouse.y 8/ SWP POP =selection.y1
		,redraw JSR2
	$no-touch

	( clear last cursor )
	~cursor.x =Screen.x
	~cursor.y =Screen.y
	,blank_icn =Screen.addr
	#30 =Screen.color

	( record cursor positions )
	~Mouse.x =cursor.x 
	~Mouse.y =cursor.y

	( draw new cursor )
	~cursor.x =Screen.x
	~cursor.y =Screen.y
	,cursor_icn =Screen.addr
	#32 ~Mouse.state #01 EQU ADD =Screen.color
	
BRK

@start ( -- )
	
	#00 ~grid.height
	$ver
		#00 ~grid.width
		$hor
			( get x,y ) SWP2 OVR STH SWP2 OVR STHr 
			#2e SET-CELL
			( incr )    SWP #01 ADD SWP
			DUP2 LTH ^$hor JNZ
		POP2
		( incr ) SWP #01 ADD SWP
		DUP2 LTH ^$ver JNZ
	POP2

	#9a =timer.speed

RTN

@is-selected ( x y -- flag )
	
	~selection.x1 ~selection.y1 EQU2

RTN

@set-lock ( x y flag -- )
	
	ROT ROT GET-OFFSET GRID-LOCKS ADD2 POK2

RTN

@get-lock ( x y -- flag )
	
	GET-OFFSET GRID-LOCKS ADD2 PEK2

RTN

@get-cell-value ( char -- value )
	
	#00 SWP ,values ADD2 PEK2

RTN

@get-value-char ( value -- char )
	
	#24 MOD #00 SWP ,b36clc ADD2 PEK2
	
RTN

@get-value ( x y -- value )
	
	GET-CELL #20 SUB ,get-cell-value JSR2

RTN

@get-cell-sprite ( x y -- addr )
	
	DUP2 GET-CELL 
	( if character is dot )
	DUP #2e NEQ ^$no-bar JNZ
		( check if x,y is grid )
		POP
		DUP2 #08 MOD #00 EQU SWP #08 MOD #00 EQU #0101 NEQ2 ^$no-marker8 JNZ POP2 ,marker8_icn RTN $no-marker8
		DUP2 #02 MOD #00 EQU SWP #02 MOD #00 EQU #0101 NEQ2 ^$no-marker4 JNZ POP2 ,marker4_icn RTN $no-marker4
		POP2 ,font RTN
	$no-bar
	STH POP2 STHr
	#20 SUB #00 SWP #0008 MUL2 ,font ADD2

RTN

( operators )

@op-a ( x y char -- )

	POP
	( get left ) DUP2 SWP #01 SUB SWP ,get-value JSR2 STH
	( get right ) DUP2 SWP #01 ADD SWP ,get-value JSR2 STH
	( incr y ) #01 ADD 
	( get result ) ADDr STHr
	,get-value-char JSR2 
	SET-CELL

RTN

@op-b ( x y char -- )
	
	POP
	( get left ) DUP2 SWP #01 SUB SWP ,get-value JSR2 STH
	( get right ) DUP2 SWP #01 ADD SWP ,get-value JSR2 STH
	( incr y ) #01 ADD 
	( get result ) SUBr STHr 
	,get-value-char JSR2 
	SET-CELL

RTN

@op-c ( x y char -- )
	
	POP
	#01 ADD
	#30 ~timer.frame #08 MOD ADD SET-CELL

RTN

@op-d ( x y char -- )

	POP POP2
	
RTN

@op-e ( x y char -- )

	POP POP2
	
RTN

@op-f ( x y char -- )

	POP POP2
	
RTN

@op-g ( x y char -- )

	POP POP2
	
RTN

@op-h ( x y char -- )

	POP POP2
	
RTN

@op-i ( x y char -- )

	POP POP2
	
RTN

@op-j ( x y char -- )

	POP POP2
	
RTN

@op-k ( x y char -- )

	POP POP2
	
RTN

@op-l ( x y char -- )

	POP POP2
	
RTN

@op-m ( x y char -- )

	POP POP2
	
RTN

@op-n ( x y char -- )
	
	STH
	( limit )
	DUP #00 NEQ ^$not-edge JNZ
		#2a SET-CELL POP STHr RTN 
	$not-edge
	( collide )
	DUP2 #01 SUB GET-CELL #2e EQU ^$not-collide JNZ
		#2a SET-CELL POP STHr RTN
	$not-collide
	( move )
	DUP2 STHr
	SWP #01 SUB SWP SET-CELL	
	#2e SET-CELL
	
RTN

@op-o ( x y char -- )

	POP POP2
	
RTN

@op-p ( x y char -- )

	POP POP2
	
RTN

@op-q ( x y char -- )

	POP POP2
	
RTN

@op-r ( x y char -- )

	POP POP2
	
RTN

@op-s ( x y char -- )
	
	STH 
	( clear ) DUP2 #2e SET-CELL
	( move ) #01 ADD DUP2 #01 ,set-lock JSR2
	STHr SET-CELL
	
RTN

@op-t ( x y char -- )

	POP POP2
	
RTN

@op-u ( x y char -- )

	POP POP2
	
RTN

@op-v ( x y char -- )

	POP POP2
	
RTN

@op-w ( x y char -- )

	STH
	( limit )
	OVR #00 NEQ ^$not-edge JNZ
		#2a SET-CELL POP STHr RTN 
	$not-edge
	( collide )
	DUP2 SWP #01 SUB SWP GET-CELL #2e EQU ^$not-collide JNZ
		#2a SET-CELL POP STHr RTN
	$not-collide
	( move )
	DUP2 
	SWP #01 SUB SWP STHr SET-CELL	
	#2e SET-CELL
	
RTN

@op-x ( x y char -- )

	POP POP2
	
RTN

@op-y ( x y char -- )

	POP POP2
	
RTN

@op-z ( x y char -- )

	POP POP2
	
RTN

@op-bang ( x y char -- )

	POP
	#2e SET-CELL

RTN

@run-char ( x y char -- )
	
	( skip dot )
	DUP #2e NEQ ^$not-dot JNZ
		POP POP2 RTN
	$not-dot

	( skip locked )
	ROT ROT DUP2 ,get-lock JSR2 #00 EQU ^$not-locked JNZ
		POP POP2 RTN 
	$not-locked
	ROT

	( A ) DUP #41 EQU ,op-a JNZ2
	( B ) DUP #42 EQU ,op-b JNZ2
	( C ) DUP #43 EQU ,op-c JNZ2
	( D ) DUP #44 EQU ,op-d JNZ2
	( E ) DUP #45 EQU ,op-e JNZ2
	( F ) DUP #46 EQU ,op-f JNZ2
	( G ) DUP #47 EQU ,op-g JNZ2
	( H ) DUP #48 EQU ,op-h JNZ2
	( I ) DUP #49 EQU ,op-i JNZ2
	( J ) DUP #4a EQU ,op-j JNZ2
	( K ) DUP #4b EQU ,op-k JNZ2
	( L ) DUP #4c EQU ,op-l JNZ2
	( M ) DUP #4d EQU ,op-m JNZ2
	( N ) DUP #4e EQU ,op-n JNZ2 ( done. )
	( O ) DUP #4f EQU ,op-o JNZ2
	( P ) DUP #50 EQU ,op-p JNZ2
	( Q ) DUP #51 EQU ,op-q JNZ2
	( R ) DUP #52 EQU ,op-r JNZ2
	( S ) DUP #53 EQU ,op-s JNZ2
	( T ) DUP #54 EQU ,op-t JNZ2
	( U ) DUP #55 EQU ,op-u JNZ2
	( V ) DUP #56 EQU ,op-v JNZ2
	( W ) DUP #57 EQU ,op-w JNZ2 ( done. )
	( X ) DUP #58 EQU ,op-x JNZ2
	( Y ) DUP #59 EQU ,op-y JNZ2
	( Z ) DUP #5a EQU ,op-z JNZ2
	( * ) DUP #2a EQU ,op-bang JNZ2
	POP POP2

RTN

@init ( -- )
	
	#00 ~grid.height
	$ver
		#00 ~grid.width
		$hor
			( get x,y ) SWP2 OVR STH SWP2 OVR STHr 
			( unlock ) #00 ,set-lock JSR2
			( incr ) SWP #01 ADD SWP
			DUP2 LTH ^$hor JNZ
		POP2
		( incr ) SWP #01 ADD SWP
		DUP2 LTH ^$ver JNZ
	POP2

RTN

@run ( -- )
	
	,init JSR2

	#00 ~grid.height
	$ver
		#00 ~grid.width
		$hor
			( get x,y ) SWP2 OVR STH SWP2 OVR STHr 
			DUP2 GET-CELL ,run-char JSR2
			( incr )    SWP #01 ADD SWP
			DUP2 LTH ^$hor JNZ
		POP2
		( incr ) SWP #01 ADD SWP
		DUP2 LTH ^$ver JNZ
	POP2
	,redraw JSR2

RTN

@draw-interface ( -- )
	
	~Screen.height #0008 SUB2 =Screen.y

	( Positionx )
	#0000 =Screen.x
	~selection.x1 
		DUP #04 SFT ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color
	#0008 =Screen.x
		#0f AND ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color

	( Positiony )
	#0010 =Screen.x
	~selection.y1 
		DUP #04 SFT ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color
	#0018 =Screen.x
		#0f AND ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color

	#0020 =Screen.x
	,position_icn =Screen.addr
	#23 =Screen.color

	( Frame )
	#0030 =Screen.x
	~timer.frame 
		DUP #04 SFT ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color
	#0038 =Screen.x
		#0f AND ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color

	#0040 =Screen.x
	,beat_icn =Screen.addr
	#21 ~timer.frame #08 MOD #00 EQU #02 MUL ADD =Screen.color

	( Speed )
	#0050 =Screen.x
	~timer.speed 
		DUP #04 SFT ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color
	#0058 =Screen.x
		#0f AND ,get-value-char JSR2 #20 SUB #00 SWP #0008 MUL2 ,font ADD2 =Screen.addr
	#22 =Screen.color

	( TODO: Signal VU )

	( File )
	~Screen.width #0028 SUB2 =Screen.x
	~Screen.x 8+ =Screen.x ,eye_icns =Screen.addr #21 =Screen.color
	~Screen.x 8+ =Screen.x ,filestate_icn =Screen.addr #21 =Screen.color
	~Screen.x 8+ =Screen.x ,load_icn =Screen.addr #21 =Screen.color
	~Screen.x 8+ =Screen.x ,save_icn =Screen.addr #21 =Screen.color

RTN

@redraw ( -- )
	
	#00 ~grid.height
	$ver
		( pos-y ) OVR #00 SWP #0008 MUL2 =Screen.y
		#00 ~grid.width
		$hor
			( pos-x )   OVR #00 SWP #0008 MUL2 =Screen.x 
			( get x,y ) SWP2 OVR STH SWP2 OVR STHr 
			( sprite )  DUP2 ,get-cell-sprite JSR2 =Screen.addr
			( draw )    ,is-selected JSR2 #0d MUL #21 ADD =Screen.color
			( incr )    SWP #01 ADD SWP
			DUP2 LTH ^$hor JNZ
		POP2
		( incr ) SWP #01 ADD SWP
		DUP2 LTH ^$ver JNZ
	POP2

	,draw-interface JSR2

RTN

( char to b36 )

@values [
	
	00 00 00 00 00 00 00 00 
	00 00 00 00 00 00 00 00 

	00 01 02 03 04 05 06 07
	08 09 00 00 00 00 00 00

	00 0a 0b 0c 0d 0e 0f 10 
	11 12 13 14 15 16 17 18
	19 1a 1b 1c 1d 1e 1f 20
	21 22 23 00 00 00 00 00

	00 0a 0b 0c 0d 0e 0f 10 
	11 12 13 14 15 16 17 18
	19 1a 1b 1c 1d 1e 1f 20
	21 22 23 00 00 00 00 00

]

( b36 to char-lc )

@b36clc [
	30 31 32 33 34 35 36 37
	38 39 61 62 63 64 65 66
	67 68 69 6a 6b 6c 6d 6e
	6f 70 71 72 73 74 75 76
	77 78 79 7a
]

@cursor_icn     [ 80c0 e0f0 f8e0 1000 ]
@blank_icn      [ 0000 0000 0000 0000 ]
@position_icn   [ 0066 4200 0042 6600 ]
@beat_icn       [ 0000 1038 7c38 1000 ]
@marker8_icn    [ 0000 2400 0024 0000 ]
@marker4_icn    [ 0000 0000 0000 1000 ]
@filestate_icn  [ 1054 28c6 2854 1000 ]
@load_icn       [ feaa d6aa d4aa f400 ]
@save_icn       [ fe82 8282 848a f400 ]

@eye_icns
	[ 0038 4492 2810 0000 ] ( open )
	[ 0000 0082 4438 0000 ] ( closed )


@font ( specter8-frag font ) 
[
	0000 0000 0000 0000 0008 0808 0800 0800
	0014 1400 0000 0000 0024 7e24 247e 2400
	0008 1e28 1c0a 3c08 0000 2204 0810 2200
	0030 4832 4c44 3a00 0008 1000 0000 0000
	0004 0808 0808 0400 0020 1010 1010 2000
	005a 2442 4224 5a00 0000 0808 3e08 0800
	0000 0000 0000 0810 0000 0000 3e00 0000
	0000 0000 0000 0800 0000 0204 0810 2000
	003c 464a 5262 3c00 0018 0808 0808 1c00
	003c 4202 3c40 7e00 003c 421c 0242 3c00
	000c 1424 447e 0400 007e 407c 0242 3c00
	003c 407c 4242 3c00 007e 0204 0810 1000
	003c 423c 4242 3c00 003c 4242 3e02 3c00
	0000 0010 0000 1000 0000 1000 0010 1020
	0000 0810 2010 0800 0000 003e 003e 0000
	0000 1008 0408 1000 003c 420c 1000 1000
	003c 4232 4a42 3c00 003c 4242 7e42 4200
	007c 427c 4242 7c00 003c 4240 4042 3c00
	007c 4242 4242 7c00 007e 4078 4040 7e00
	007e 4078 4040 4000 003c 4240 4642 3c00
	0042 427e 4242 4200 001c 0808 0808 1c00
	007e 0202 0242 3c00 0042 4478 4442 4200
	0040 4040 4040 7e00 0042 665a 4242 4200
	0042 6252 4a46 4200 003c 4242 4242 3c00
	007c 4242 7c40 4000 003c 4242 4244 3a00
	007c 4242 7c44 4200 003e 403c 0242 3c00
	007e 0808 0808 1000 0042 4242 4244 3a00
	0042 4242 4224 1800 0042 4242 5a66 4200
	0042 423c 4242 4200 0042 423e 0242 3c00
	007e 020c 3040 7e00 000c 0808 0808 0c00
	0040 2010 0804 0200 0030 1010 1010 3000
	0008 1400 0000 0000 0000 0000 0000 7e00
	0008 0400 0000 0000 0000 3c02 3e42 3a00
	0040 407c 4242 7c00 0000 3c42 4042 3c00
	0002 023e 4242 3e00 0000 3c42 7e40 3e00
	0000 3e40 7840 4000 0000 3c42 3e02 3c00
	0040 405c 6242 4200 0008 0018 0808 0400
	0008 0018 0808 4830 0040 4244 7844 4200
	0010 1010 1010 0c00 0000 6c52 5252 5200
	0000 5c62 4242 4200 0000 3c42 4242 3c00
	0000 7c42 427c 4040 0000 3e42 423e 0202
	0000 5c62 4040 4000 0000 3e40 3c02 7c00
	0008 7e08 0808 1000 0000 4242 4244 3a00
	0000 4242 4224 1800 0000 5252 5252 2e00
	0000 4224 1824 4200 0000 4242 3e02 7c00
	0000 7e02 3c40 7e00 000c 0810 1008 0c00
	0008 0808 0808 0800 0030 1008 0810 3000
	0000 0032 4c00 0000 3c42 99a1 a199 423c
]

@data [ ]