uxn/projects/examples/demos/life.tal

280 lines
5.4 KiB
Tal
Raw Normal View History

2022-03-25 19:05:11 +00:00
( Game Of Life:
2021-05-06 03:16:27 +00:00
Any live cell with fewer than two live neighbours dies, as if by underpopulation.
2021-05-06 17:38:38 +00:00
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. )
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
|00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2
|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
2021-05-06 01:38:28 +00:00
|0000
2022-03-26 04:36:33 +00:00
@world &frame $1 &count $2
@anchor &x $2 &y $2 &x2 $2 &y2 $2
@pointer &x $2 &y $2
2021-05-06 01:38:28 +00:00
|0100 ( -> )
2022-02-09 04:04:18 +00:00
( theme )
#02cf .System/r DEO2
#02ff .System/g DEO2
2021-08-08 03:03:13 +00:00
#024f .System/b DEO2
2022-03-26 04:36:33 +00:00
( resize )
#00c0 .Screen/width DEO2
#00c0 .Screen/height DEO2
2021-05-06 01:38:28 +00:00
( vectors )
2022-03-26 04:36:33 +00:00
;on-frame .Screen/vector DEO2
;on-mouse .Mouse/vector DEO2
2021-05-06 17:38:38 +00:00
;on-control .Controller/vector DEO2
2021-05-06 01:38:28 +00:00
( glider )
#07 #03 ;set-cell JSR2
#07 #04 ;set-cell JSR2
#05 #04 ;set-cell JSR2
#07 #05 ;set-cell JSR2
#06 #05 ;set-cell JSR2
2022-03-26 04:36:33 +00:00
( center )
.Screen/width DEI2 #01 SFT2 #0040 SUB2
DUP2 .anchor/x STZ2
#007e ADD2 .anchor/x2 STZ2
.Screen/height DEI2 #01 SFT2 #0040 SUB2
DUP2 .anchor/y STZ2
#007e ADD2 .anchor/y2 STZ2
2021-05-06 17:38:38 +00:00
2021-05-06 01:38:28 +00:00
BRK
@on-frame ( -> )
2021-05-06 03:33:48 +00:00
2022-03-26 04:36:33 +00:00
.Mouse/state DEI #00 EQU #01 JCN [ BRK ]
#0000 .world/count STZ2
.world/frame LDZ INC
DUP .world/frame STZ
#03 AND #00 EQU #01 JCN [ BRK ]
;run JSR2
&paused
2021-05-06 03:16:27 +00:00
BRK
@on-mouse ( -> )
( clear last cursor )
2022-02-09 04:04:18 +00:00
;cursor .Screen/addr DEO2
.pointer/x LDZ2 .Screen/x DEO2
.pointer/y LDZ2 .Screen/y DEO2
#40 .Screen/sprite DEO
2021-05-06 03:16:27 +00:00
( record pointer positions )
2021-07-09 18:38:45 +00:00
.Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2
2022-02-09 04:04:18 +00:00
.Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2
2021-05-06 03:16:27 +00:00
( colorize on state )
2022-03-26 04:36:33 +00:00
#42 [ .Mouse/state DEI #00 NEQ ] ADD .Screen/sprite DEO
( on touch in rect )
.Mouse/state DEI #00 NEQ #01 JCN [ BRK ]
.Mouse/x DEI2 .Mouse/y DEI2 .anchor ;within-rect JSR2 JMP [ BRK ]
( paint )
.Mouse/x DEI2 .anchor/x LDZ2 SUB2 #01 SFT NIP
.Mouse/y DEI2 .anchor/y LDZ2 SUB2 #01 SFT NIP
;set-cell JSR2
( draw )
2021-05-06 03:33:48 +00:00
;draw-grid JSR2
2021-05-06 03:16:27 +00:00
BRK
2021-05-06 17:38:38 +00:00
@on-control ( -> )
2022-03-26 04:36:33 +00:00
( toggle play )
.Controller/key DEI #20 NEQ ,&no-toggle JCN
2021-08-08 03:03:13 +00:00
;on-frame
2022-03-26 04:36:33 +00:00
.Screen/vector DEI2 ;on-frame/paused EQU2 ,&swap JCN
POP2 ;on-frame/paused
2021-08-08 03:03:13 +00:00
&swap
.Screen/vector DEO2
&no-toggle
2022-03-26 04:36:33 +00:00
( clear on home )
.Controller/button DEI #08 NEQ ,&no-reset JCN
;bank1 #0400 ;mclr JSR2
2022-02-09 04:04:18 +00:00
&no-reset
2021-05-06 17:38:38 +00:00
BRK
2022-03-26 04:36:33 +00:00
@run ( -- )
2021-05-06 03:16:27 +00:00
2022-03-26 04:36:33 +00:00
( clear buffer )
;bank2 #1000 ;mclr JSR2
( run grid )
#4000
2021-05-06 01:38:28 +00:00
&ver
2022-03-26 04:36:33 +00:00
STHk
#4000
2021-05-06 01:38:28 +00:00
&hor
2022-03-26 04:36:33 +00:00
DUP STHkr ,run-cell JSR
2021-08-17 22:24:37 +00:00
INC GTHk ,&hor JCN
2021-05-13 01:28:45 +00:00
POP2
2022-03-26 04:36:33 +00:00
POPr
2021-08-17 22:24:37 +00:00
INC GTHk ,&ver JCN
2021-05-06 01:38:28 +00:00
POP2
2022-03-26 04:36:33 +00:00
( move buffer )
;bank2 ;bank1 #1000 ;mcpy JSR2
( draw )
;draw-grid JSR2
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
@run-cell ( x y -- )
2021-05-06 01:38:28 +00:00
( x y ) DUP2k
( neighbours ) ;get-neighbours JSR2
2022-03-26 04:36:33 +00:00
( state ) ROT ROT ;get-cell JSR2
#00 EQU ,&dead JCN
DUP #02 LTH ,&dies JCN
DUP #03 GTH ,&dies JCN
2022-05-31 20:25:41 +00:00
POP ;&save JSR2 JMP2r
2022-03-26 04:36:33 +00:00
&dies POP POP2 JMP2r
&dead
DUP #03 EQU ,&birth JCN POP POP2 JMP2r
2022-05-31 20:25:41 +00:00
&birth POP ;&save JSR2
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
&save ( x y -- )
STH2 #01 STH2r ,get-index JSR [ #1000 ADD2 ] STA
.world/count LDZ2 INC2 .world/count STZ2
JMP2r
@get-index ( x y -- index* )
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
( y ) #3f AND #00 SWP #60 SFT2
( x ) ROT #3f AND #00 SWP ADD2
;bank1 ADD2
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
@set-cell ( x y -- )
2021-11-26 22:29:20 +00:00
2022-03-26 04:36:33 +00:00
STH2 #01 STH2r ,get-index JSR STA
2021-11-26 22:29:20 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
2021-11-26 22:29:20 +00:00
2021-05-06 01:38:28 +00:00
@get-cell ( x y -- cell )
2022-03-26 04:36:33 +00:00
,get-index JSR LDA
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
2021-05-06 01:38:28 +00:00
@get-neighbours ( x y -- neighbours )
2022-03-26 04:36:33 +00:00
,&origin STR2
LITr 00
#0800
&loop
#00 OVR #10 SFT2 ;&mask ADD2 LDA2 [ LIT2 &origin $2 ]
ROT ADD STH ADD STHr ;get-cell JSR2 STH ADDr
INC GTHk ,&loop JCN
POP2
2021-05-06 01:38:28 +00:00
STHr
2022-03-26 04:36:33 +00:00
JMP2r
&mask ffff 00ff 01ff ff00 0100 ff01 0001 0101
2021-05-06 01:38:28 +00:00
2022-03-26 04:36:33 +00:00
@draw-grid ( -- )
( draw cell count )
.anchor/x LDZ2 .Screen/x DEO2
.anchor/y2 LDZ2 #0008 ADD2 .Screen/y DEO2
#01 .Screen/auto DEO
.world/count LDZ2 ;draw-short JSR2
#00 .Screen/auto DEO
#4000
&ver
#00 OVR #10 SFT2 .anchor/y LDZ2 ADD2 .Screen/y DEO2
STHk
#4000
&hor
#00 OVR #10 SFT2 .anchor/x LDZ2 ADD2 .Screen/x DEO2
DUP STHkr ;get-cell JSR2 INC .Screen/pixel DEO
INC GTHk ,&hor JCN
POP2
POPr
INC GTHk ,&ver JCN
POP2
2021-05-06 17:38:38 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
@draw-short ( short* -- )
SWP ,draw-byte JSR
2022-02-04 03:52:12 +00:00
2022-02-09 04:04:18 +00:00
@draw-byte ( byte color -- )
2022-02-04 03:52:12 +00:00
2022-03-26 04:36:33 +00:00
DUP #04 SFT ,draw-hex JSR #0f AND
2022-02-04 03:52:12 +00:00
2022-02-09 04:04:18 +00:00
@draw-hex ( char color -- )
2022-02-04 03:52:12 +00:00
2022-03-26 04:36:33 +00:00
#00 SWP #30 SFT2 ;font-hex ADD2 .Screen/addr DEO2
#03 .Screen/sprite DEO
2021-05-06 17:38:38 +00:00
2022-03-26 04:36:33 +00:00
JMP2r
@within-rect ( x* y* rect -- flag )
STH
( y < rect.y1 ) DUP2 STHkr INC INC LDZ2 LTH2 ,&skip JCN
2022-03-26 04:36:33 +00:00
( 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
JMP2r
&skip
POP2 POP2 POPr
#00
JMP2r
2021-05-06 17:38:38 +00:00
2022-02-09 04:04:18 +00:00
@mclr ( addr* len* -- )
2022-03-26 04:36:33 +00:00
OVR2 ADD2 SWP2
2022-02-09 04:04:18 +00:00
&loop
STH2k #00 STH2r STA
INC2 GTH2k ,&loop JCN
POP2 POP2
JMP2r
@mcpy ( src* dst* len* -- )
SWP2 STH2
2022-03-26 04:36:33 +00:00
OVR2 ADD2 SWP2
2022-02-09 04:04:18 +00:00
&loop
LDAk STH2kr STA INC2r
INC2 GTH2k ,&loop JCN
POP2 POP2
POP2r
JMP2r
@cursor
2021-05-13 01:28:45 +00:00
80c0 e0f0 f8e0 1000
@font-hex
2022-03-26 04:36:33 +00:00
7c82 8282 8282 7c00
3010 1010 1010 3800
7c82 027c 8080 fe00
7c82 021c 0282 7c00
2242 82fe 0202 0200
fe80 807c 0282 7c00
7c82 80fc 8282 7c00
fe82 0408 0810 1000
7c82 827c 8282 7c00
7c82 827e 0202 0200
7c82 82fe 8282 8200
fc82 82fc 8282 fc00
7c82 8080 8082 7c00
fc82 8282 8282 fc00
fe80 80f0 8080 fe00
fe80 80f0 8080 8000
@bank1 $1000 @bank2