Browse code

(life.tal) General optimizations

neauoire authored on 26/03/2022 04:36:33
Showing 1 changed files
... ...
@@ -4,56 +4,18 @@
4 4
 	Any live cell with more than three live neighbours dies, as if by overpopulation.
5 5
 	Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. )
6 6
 
7
-%+  { ADD } %-   { SUB }
8
-%<  { LTH } %>   { GTH }  %=  { EQU } %!   { NEQ }
9
-%++ { ADD2 } %-- { SUB2 }
10
-%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }
11
-
12
-%2/  { #01 SFT }
13
-%8/  { #03 SFT }
14
-%2//  { #01 SFT2 } %8//  { #03 SFT2 }
15
-%2**  { #10 SFT2 } %8**  { #30 SFT2 }
16
-%40** { #60 SFT2 }
17
-%8MOD { #07 AND } %2MOD { #01 AND }
18
-
19
-%TOS  { #00 SWP }
20
-%RTN  { JMP2r }
21
-%SFL  { #40 SFT SFT }
22
-
23
-%WIDTH { #40 }
24
-%HEIGHT { #40 }
25
-%LENGTH { #0200 }
26
-%WIDTH-MOD { #3f AND }
27
-%HEIGHT-MOD { #3f AND }
28
-%IN-RANGE { INCk SWP SUB2 GTH }
29
-
30
-%BANK1 { #8000 } %BANK2 { #a000 }
31
-
32
-%GET-ITERATORS { SWP2k POP NIP }
33
-%GET-ITER { OVR2 NIP OVR SWP }
34
-
35
-%AUTO-NONE   { #00 .Screen/auto DEO }
36
-%AUTO-X      { #01 .Screen/auto DEO }
37
-
38
-( devices )
39
-
40
-|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]
41
-|10 @Console    [ &vector $2 &read $1 &pad    $5 &write $1 &error  $1 ]
42
-|20 @Screen     &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
43
-|30 @Audio0     [ &vector $2 &position $2 &output $1 &pad   $3 &adsr   $2 &length $2 &addr $2 &volume $1 &pitch $1 ]
44
-|80 @Controller [ &vector $2 &button   $1 &key    $1 ]
45
-|90 @Mouse      [ &vector $2 &x        $2 &y      $2 &state $1 &wheel $1 ]
46
-
47
-( variables )
7
+|00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2
8
+|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1
9
+|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
10
+|30 @Audio0 &vector $2 &position $2 &output $1 &pad $3 &adsr $2 &length $2 &addr $2 &volume $1 &pitch $1
11
+|80 @Controller &vector $2 &button $1 &key $1
12
+|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &wheel $1
48 13
 
49 14
 |0000
50 15
 
51
-@world   [ &frame $1 &count $2 ]
52
-@anchor  [ &x $2 &y $2 ]
53
-@pointer [ &x $2 &y $2 ]
54
-@rle     [ &x $1 &y $1 &n $1 ]
55
-
56
-( program )
16
+@world &frame $1 &count $2
17
+@anchor &x $2 &y $2 &x2 $2 &y2 $2
18
+@pointer &x $2 &y $2
57 19
 
58 20
 |0100 ( -> )
59 21
 
... ...
@@ -61,94 +23,41 @@
61 23
 	#02cf .System/r DEO2
62 24
 	#02ff .System/g DEO2
63 25
 	#024f .System/b DEO2
64
-
26
+	( resize )
27
+	#00c0 .Screen/width DEO2
28
+	#00c0 .Screen/height DEO2
65 29
 	( vectors )
66
-	;on-input   .Console/vector DEO2
67
-	;on-frame   .Screen/vector DEO2
68
-	;on-mouse   .Mouse/vector DEO2
30
+	;on-frame .Screen/vector DEO2
31
+	;on-mouse .Mouse/vector DEO2
69 32
 	;on-control .Controller/vector DEO2
70
-
71 33
 	( glider )
72 34
 	#07 #03 ;set-cell JSR2
73 35
 	#07 #04 ;set-cell JSR2
74 36
 	#05 #04 ;set-cell JSR2
75 37
 	#07 #05 ;set-cell JSR2
76 38
 	#06 #05 ;set-cell JSR2
77
-
78
-	.Screen/width DEI2 2// WIDTH TOS -- .anchor/x STZ2
79
-	.Screen/height DEI2 2// HEIGHT TOS -- .anchor/y STZ2
80
-
81
-BRK
82
-
83
-@on-frame-paused ( -> )
39
+	( center )
40
+	.Screen/width DEI2 #01 SFT2 #0040 SUB2
41
+		DUP2 .anchor/x STZ2
42
+		#007e ADD2 .anchor/x2 STZ2
43
+	.Screen/height DEI2 #01 SFT2 #0040 SUB2
44
+		DUP2 .anchor/y STZ2
45
+		#007e ADD2 .anchor/y2 STZ2
84 46
 
85 47
 BRK
86 48
 
87 49
 @on-frame ( -> )
88 50
 	
89
-	.Mouse/state DEI #00 = #01 JCN [ BRK ]
90
-
91
-	( incr frame ) .world/frame LDZ INC [ DUP ] .world/frame STZ
92
-	( reset count ) #0000 .world/count STZ2
93
-
94
-	#03 AND #00 = #01 JCN [ BRK ]
95
-
96
-	( clear buffer )
97
-	BANK2 LENGTH ;mclr JSR2
98
-
99
-	( run grid )
100
-	#00 HEIGHT
101
-	&ver
102
-		#00 WIDTH
103
-		&hor
104
-			GET-ITERATORS
105
-			( x y ) DUP2
106
-			( neighbours ) DUP2 ;get-neighbours JSR2
107
-			( state ) ROT ROT ;get-cell JSR2
108
-			,run-cell JSR
109
-			SWP INC SWP
110
-			LTHk ,&hor JCN
111
-		POP2
112
-		SWP INC SWP
113
-		LTHk ,&ver JCN
114
-	POP2
115
-
116
-	( move buffer )
117
-	BANK2 BANK1 LENGTH ;mcpy JSR2
118
-
119
-	;draw-grid JSR2
51
+	.Mouse/state DEI #00 EQU #01 JCN [ BRK ]
52
+	#0000 .world/count STZ2
53
+	.world/frame LDZ INC
54
+		DUP .world/frame STZ
55
+		#03 AND #00 EQU #01 JCN [ BRK ]
56
+	;run JSR2
57
+	&paused
120 58
 
121 59
 BRK
122 60
 
123
-@run-cell ( x y neighbours state -- )
124
-	
125
-	#00 = ,&dead JCN
126
-	&alive
127
-		DUP #02 < ,&dies JCN
128
-		DUP #03 > ,&dies JCN
129
-		&lives POP ,save-cell JSR RTN
130
-		&dies POP POP2 RTN
131
-	&dead
132
-		DUP #03 = ,&birth JCN POP POP2 RTN
133
-		&birth POP ,save-cell JSR RTN
134
-
135
-RTN
136
-
137
-@save-cell ( x y -- )
138
-	
139
-	( get index )
140
-	HEIGHT-MOD SWP WIDTH-MOD SWP
141
-	TOS 8** ROT 8/ TOS ++ [ BANK2 ++ ]
142
-	( incr count )
143
-	.world/count LDZ2 INC2 .world/count STZ2
144
-	( save in buffer )
145
-	STH2
146
-	DUP2 POP 8MOD #01 SWP SFL
147
-	LDAkr STHr SWP ORA
148
-	STH2r STA
149
-
150
-RTN
151
-
152 61
 @on-mouse ( -> )
153 62
 	
154 63
 	( clear last cursor )
... ...
@@ -156,138 +65,177 @@ RTN
156 65
 	.pointer/x LDZ2 .Screen/x DEO2
157 66
 	.pointer/y LDZ2 .Screen/y DEO2
158 67
 	#40 .Screen/sprite DEO
159
-
160 68
 	( record pointer positions )
161 69
 	.Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2
162 70
 	.Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2
163
-
164 71
 	( colorize on state )
165
-	#42 [ .Mouse/state DEI #00 ! ] + .Screen/sprite DEO
166
-
167
-	.Mouse/state DEI #00 ! #01 JCN [ BRK ]
168
-
169
-	.Mouse/x DEI2 DUP2 .anchor/x LDZ2 >> ROT ROT .anchor/x LDZ2 WIDTH DUP ADD TOS ++ INC2 << #0101 ==
170
-	.Mouse/y DEI2 DUP2 .anchor/y LDZ2 >> ROT ROT .anchor/y LDZ2 HEIGHT DUP ADD TOS ++ << #0101 ==
171
-	#0101 == #01 JCN [ BRK ]
172
-
173
-	.Mouse/x DEI2 .anchor/x LDZ2 SUB2 2/ NIP
174
-	.Mouse/y DEI2 .anchor/y LDZ2 SUB2 2/ NIP
175
-	;set-cell JSR2
176
-	
72
+	#42 [ .Mouse/state DEI #00 NEQ ] ADD .Screen/sprite DEO
73
+	( on touch in rect )
74
+	.Mouse/state DEI #00 NEQ #01 JCN [ BRK ]
75
+	.Mouse/x DEI2 .Mouse/y DEI2 .anchor ;within-rect JSR2 JMP [ BRK ]
76
+	( paint )
77
+	.Mouse/x DEI2 .anchor/x LDZ2 SUB2 #01 SFT NIP
78
+	.Mouse/y DEI2 .anchor/y LDZ2 SUB2 #01 SFT NIP
79
+		;set-cell JSR2
80
+	( draw )
177 81
 	;draw-grid JSR2
178 82
 
179 83
 BRK
180 84
 
181 85
 @on-control ( -> )
182 86
 
183
-	.Controller/key DEI #20 ! ,&no-toggle JCN
87
+	( toggle play )
88
+	.Controller/key DEI #20 NEQ ,&no-toggle JCN
184 89
 		;on-frame
185
-		.Screen/vector DEI2 ;on-frame-paused == ,&swap JCN
186
-			POP2 ;on-frame-paused
90
+		.Screen/vector DEI2 ;on-frame/paused EQU2 ,&swap JCN
91
+			POP2 ;on-frame/paused
187 92
 			&swap
188 93
 		.Screen/vector DEO2
189 94
 		&no-toggle
190
-
191
-	.Controller/button DEI #08 ! ,&no-reset JCN
192
-		BANK1 #1000 ;mclr JSR2
193
-		BANK2 #1000 ;mclr JSR2
95
+	( clear on home )
96
+	.Controller/button DEI #08 NEQ ,&no-reset JCN
97
+		;bank1 #0400 ;mclr JSR2
194 98
 		&no-reset
195 99
 
196 100
 BRK
197 101
 
198
-@draw-grid ( -- )
199
-	
200
-	( draw cell count )
201
-	.anchor/x LDZ2 .Screen/x DEO2
202
-	.anchor/y LDZ2 HEIGHT DUP ADD TOS ++ .Screen/y DEO2
203
-	AUTO-X
204
-	.world/count LDZ2 #03 ;draw-short JSR2
205
-	AUTO-NONE
102
+@run ( -- )
206 103
 
207
-	HEIGHT #00
104
+	( clear buffer )
105
+	;bank2 #1000 ;mclr JSR2
106
+	( run grid )
107
+	#4000
208 108
 	&ver
209
-		DUP TOS 2** .anchor/y LDZ2 ++ .Screen/y DEO2
210
-		WIDTH #00
109
+		STHk
110
+		#4000
211 111
 		&hor
212
-			DUP TOS 2** .anchor/x LDZ2 ++ .Screen/x DEO2
213
-			GET-ITER ,get-cell JSR INC .Screen/pixel DEO
112
+			DUP STHkr ,run-cell JSR
214 113
 			INC GTHk ,&hor JCN
215 114
 		POP2
115
+		POPr
216 116
 		INC GTHk ,&ver JCN
217 117
 	POP2
118
+	( move buffer )
119
+	;bank2 ;bank1 #1000 ;mcpy JSR2
120
+	( draw )
121
+	;draw-grid JSR2
218 122
 
219
-RTN
123
+JMP2r
220 124
 
221
-@get-index ( x y -- index* )
222
-	
223
-	HEIGHT-MOD SWP WIDTH-MOD SWP
224
-	TOS 8** ROT 8/ TOS ++ [ BANK1 ++ ]
125
+@run-cell ( x y -- )
225 126
 
226
-RTN
127
+	( x y ) DUP2
128
+	( neighbours ) DUP2 ;get-neighbours JSR2
129
+	( state ) ROT ROT ;get-cell JSR2
130
+	#00 EQU ,&dead JCN
131
+		DUP #02 LTH ,&dies JCN
132
+		DUP #03 GTH ,&dies JCN
133
+		POP ,&save JSR JMP2r
134
+		&dies POP POP2 JMP2r
135
+	&dead
136
+		DUP #03 EQU ,&birth JCN POP POP2 JMP2r
137
+		&birth POP ,&save JSR JMP2r
227 138
 
228
-@set-cell ( x y -- )
139
+JMP2r
140
+	&save ( x y -- ) 
141
+		STH2 #01 STH2r ,get-index JSR [ #1000 ADD2 ] STA
142
+		.world/count LDZ2 INC2 .world/count STZ2
143
+	JMP2r
144
+
145
+@get-index ( x y -- index* )
229 146
 	
230
-	DUP2 ,get-index JSR STH2
231
-	POP 8MOD #01 SWP SFL
232
-	LDAkr STHr SWP ORA
233
-	STH2r STA
147
+	( y ) #3f AND #00 SWP #60 SFT2 
148
+	( x ) ROT #3f AND #00 SWP ADD2
149
+		;bank1 ADD2
234 150
 
235
-RTN
151
+JMP2r
236 152
 
237
-@unset-cell ( x y -- )
153
+@set-cell ( x y -- )
238 154
 	
239
-	DUP2 ,get-index JSR STH2
240
-	POP 8MOD #01 SWP SFL #ff EOR
241
-	LDAkr STHr SWP AND
242
-	STH2r STA
155
+	STH2 #01 STH2r ,get-index JSR STA
243 156
 
244
-RTN
157
+JMP2r
245 158
 
246 159
 @get-cell ( x y -- cell )
247 160
 	
248
-	DUP2 ,get-index JSR LDA
249
-	NIP SWP
250
-	8MOD
251
-	SFT 2MOD
161
+	,get-index JSR LDA
252 162
 
253
-RTN
163
+JMP2r
254 164
 
255 165
 @get-neighbours ( x y -- neighbours )
256 166
 	
257
-	( -1,-1 ) DUP2 #01 - [ SWP #01 - SWP ] ,get-cell JSR STH
258
-	(  0,-1 ) DUP2 #01 -      ,get-cell JSR STH ADDr
259
-	( +1,-1 ) DUP2 #01 - [ SWP INC SWP ] ,get-cell JSR STH ADDr
260
-	( -1, 0 ) DUP2       [ SWP #01 - SWP ] ,get-cell JSR STH ADDr
261
-	( +1, 0 ) DUP2       [ SWP INC SWP ] ,get-cell JSR STH ADDr
262
-	( -1,+1 ) DUP2 INC [ SWP #01 - SWP ] ,get-cell JSR STH ADDr
263
-	(  0,+1 ) DUP2 INC      ,get-cell JSR STH ADDr
264
-	( +1,+1 )      INC [ SWP INC SWP ] ,get-cell JSR STH ADDr
167
+	,&origin STR2
168
+	LITr 00
169
+	#0800
170
+	&loop
171
+		#00 OVR #10 SFT2 ;&mask ADD2 LDA2 [ LIT2 &origin $2 ]
172
+		ROT ADD STH ADD STHr ;get-cell JSR2 STH ADDr
173
+		INC GTHk ,&loop JCN
174
+	POP2
265 175
 	STHr
266 176
 
267
-RTN
177
+JMP2r
178
+	&mask ffff 00ff 01ff ff00 0100 ff01 0001 0101
268 179
 
269
-@draw-short ( short* color -- )
180
+@draw-grid ( -- )
181
+	
182
+	( draw cell count )
183
+	.anchor/x LDZ2 .Screen/x DEO2
184
+	.anchor/y2 LDZ2 #0008 ADD2 .Screen/y DEO2
185
+	#01 .Screen/auto DEO
186
+	.world/count LDZ2 ;draw-short JSR2
187
+	#00 .Screen/auto DEO
188
+	#4000
189
+	&ver
190
+		#00 OVR #10 SFT2 .anchor/y LDZ2 ADD2 .Screen/y DEO2
191
+		STHk
192
+		#4000
193
+		&hor
194
+			#00 OVR #10 SFT2 .anchor/x LDZ2 ADD2 .Screen/x DEO2
195
+			DUP STHkr ;get-cell JSR2 INC .Screen/pixel DEO
196
+			INC GTHk ,&hor JCN
197
+		POP2
198
+		POPr
199
+		INC GTHk ,&ver JCN
200
+	POP2
270 201
 
271
-	STH
272
-	SWP STHkr ,draw-byte JSR
273
-	STHr
202
+JMP2r
203
+
204
+@draw-short ( short* -- )
205
+
206
+	SWP ,draw-byte JSR
274 207
 
275 208
 @draw-byte ( byte color -- )
276 209
 
277
-	STH
278
-	DUP #04 SFT STHkr ,draw-hex JSR #0f AND
279
-	STHr
210
+	DUP #04 SFT ,draw-hex JSR #0f AND
280 211
 
281 212
 @draw-hex ( char color -- )
282 213
 
283
-	SWP TOS 8** ;font-hex ++ .Screen/addr DEO2
284
-	.Screen/sprite DEO
214
+	#00 SWP #30 SFT2 ;font-hex ADD2 .Screen/addr DEO2
215
+	#03 .Screen/sprite DEO
285 216
 
286
-RTN
217
+JMP2r
218
+
219
+@within-rect ( x* y* rect -- flag )
220
+	
221
+	STH
222
+	( y < rect.y1 ) DUP2 STHkr #02 ADD LDZ2 LTH2 ,&skip JCN
223
+	( y > rect.y2 ) DUP2 STHkr #06 ADD LDZ2 GTH2 ,&skip JCN
224
+	SWP2
225
+	( x < rect.x1 ) DUP2 STHkr LDZ2 LTH2 ,&skip JCN
226
+	( x > rect.x2 ) DUP2 STHkr #04 ADD LDZ2 GTH2 ,&skip JCN
227
+	POP2 POP2 POPr
228
+	#01
229
+JMP2r
230
+	&skip
231
+	POP2 POP2 POPr
232
+	#00
233
+
234
+JMP2r
287 235
 
288 236
 @mclr ( addr* len* -- )
289 237
 
290
-	OVR2 ++ SWP2
238
+	OVR2 ADD2 SWP2
291 239
 	&loop
292 240
 		STH2k #00 STH2r STA
293 241
 		INC2 GTH2k ,&loop JCN
... ...
@@ -298,7 +246,7 @@ JMP2r
298 246
 @mcpy ( src* dst* len* -- )
299 247
 
300 248
 	SWP2 STH2
301
-	OVR2 ++ SWP2
249
+	OVR2 ADD2 SWP2
302 250
 	&loop
303 251
 		LDAk STH2kr STA INC2r
304 252
 		INC2 GTH2k ,&loop JCN
... ...
@@ -307,77 +255,25 @@ JMP2r
307 255
 
308 256
 JMP2r
309 257
 
310
-( input )
311
-
312
-@on-input ( -> )
313
-	,&main JSR
314
-	BRK
315
-
316
-	&main
317
-	.Console/read DEI #20 GTH JMP JMP2r ( ignore whitespace )
318
-	.Console/read DEI LIT 'b EQU ,unset-run JCN
319
-	.Console/read DEI LIT 'o EQU ,set-run JCN
320
-	.Console/read DEI LIT '$ EQU ,input-eol JCN
321
-	.Console/read DEI LIT '! EQU ,input-eop JCN
322
-	LIT2 '0 '9 .Console/read DEI IN-RANGE ,input-number JCN
323
-	;on-ignore-until-eol .Console/vector DEO2
324
-	JMP2r
325
-
326
-@unset-run ( -- )
327
-	;unset-cell ,run JMP ( tail call )
328
-
329
-@set-run ( -- )
330
-	;set-cell ( fall through )
331
-
332
-@run ( cell-fn* -- )
333
-	STH2
334
-	;on-frame-paused .Screen/vector DEO2
335
-	.rle/n LDZk #00 ROT STZ
336
-	DUP #00 NEQ JMP INC
337
-	&loop ( count / cell-fn* )
338
-		DUP #00 EQU ,&end JCN
339
-		.rle/x LDZ .rle/y LDZ STH2kr JSR2
340
-		.rle/x LDZk INC SWP STZ
341
-		#01 SUB
342
-		,&loop JMP
343
-	&end
344
-	POP POP2r
345
-	JMP2r
346
-
347
-@input-number ( -- )
348
-	.rle/n LDZk #0a MUL
349
-		.Console/read DEI LIT '0 SUB
350
-		ADD SWP STZ
351
-	JMP2r
352
-
353
-@input-eol ( -- )
354
-	WIDTH .rle/x LDZ SUB .rle/n STZ
355
-	,unset-run JSR
356
-	#00 .rle/x STZ
357
-	.rle/y LDZk INC SWP STZ
358
-	JMP2r
359
-
360
-@input-eop ( -- )
361
-	,input-eol JSR
362
-	HEIGHT .rle/y LDZ GTH ,input-eop JCN
363
-	;on-frame .Screen/vector DEO2
364
-	#00 .rle/y STZ
365
-	BRK
366
-
367
-@on-ignore-until-eol ( -> )
368
-	.Console/read DEI #0a EQU JMP BRK
369
-	;on-input .Console/vector DEO2
370
-	BRK
371
-
372 258
 @cursor
373 259
 	80c0 e0f0 f8e0 1000
374 260
 
375 261
 @font-hex
376
-	007c 8282 8282 827c 0030 1010 1010 1010
377
-	007c 8202 7c80 80fe 007c 8202 1c02 827c
378
-	000c 1424 4484 fe04 00fe 8080 7c02 827c
379
-	007c 8280 fc82 827c 007c 8202 1e02 0202
380
-	007c 8282 7c82 827c 007c 8282 7e02 827c
381
-	007c 8202 7e82 827e 00fc 8282 fc82 82fc
382
-	007c 8280 8080 827c 00fc 8282 8282 82fc
383
-	007c 8280 f080 827c 007c 8280 f080 8080
262
+	7c82 8282 8282 7c00
263
+	3010 1010 1010 3800
264
+	7c82 027c 8080 fe00
265
+	7c82 021c 0282 7c00
266
+	2242 82fe 0202 0200
267
+	fe80 807c 0282 7c00
268
+	7c82 80fc 8282 7c00
269
+	fe82 0408 0810 1000
270
+	7c82 827c 8282 7c00
271
+	7c82 827e 0202 0200
272
+	7c82 82fe 8282 8200
273
+	fc82 82fc 8282 fc00
274
+	7c82 8080 8082 7c00
275
+	fc82 8282 8282 fc00
276
+	fe80 80f0 8080 fe00
277
+	fe80 80f0 8080 8000
278
+
279
+@bank1 $1000 @bank2