Browse code

Added Game Of Life

neauoire authored on 06/05/2021 03:16:27
Showing 2 changed files
... ...
@@ -1,57 +1,38 @@
1
-( game of life )
1
+( game of life
2
+	Any live cell with fewer than two live neighbours dies, as if by underpopulation.
3
+    Any live cell with two or three live neighbours lives on to the next generation.
4
+    Any live cell with more than three live neighbours dies, as if by overpopulation.
5
+    Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. )
2 6
 
3 7
 %+  { ADD } %-   { SUB }  %*  { MUL } %/   { DIV }  
4 8
 %<  { LTH } %>   { GTH }  %=  { EQU } %!   { NEQ } 
5 9
 %++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 } 
6 10
 %<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }  
7 11
 
8
-%TOS { #00 SWP }
12
+%INCR { #01 + }  %DECR { #01 - }
13
+%TOS { #00 SWP } %TOB { SWP POP }
9 14
 %RTN { JMP2r }
10 15
 %MOD { DUP2 / * - }
11 16
 %SFL { #40 SFT SFT }
12 17
 
13
-%INCR { #01 + } %DECR { #01 - }
14
-
15
-%DEBUG { .Console/byte DEO #0a .Console/char DEO }
16
-%DEBUG2 { .Console/short DEO2 #0a .Console/char DEO }
18
+%WIDTH { #40 } %HEIGHT { #40 }
19
+%BANK1 { #8000 } %BANK2 { #a000 }
17 20
 
18 21
 %GET-SIZE { WIDTH TOS #0008 // HEIGHT TOS ** }
19 22
 
20
-(
21
-	The maximum grid is 256x256, 
22
-	each byte is 8 horizontal cells,
23
-	the memory is 32x256[8192 bytes long]
24
-
25
-	The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, 
26
-	each of which is in one of two possible states, live or dead,. Every cell interacts with its eight 
27
-	neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. 
28
-	At each step in time, the following transitions occur:
29
-
30
-    Any live cell with fewer than two live neighbours dies, as if by underpopulation.
31
-    Any live cell with two or three live neighbours lives on to the next generation.
32
-    Any live cell with more than three live neighbours dies, as if by overpopulation.
33
-    Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
34
-
35
-)
36
-
37
-%WIDTH { #40 } %HEIGHT { #40 }
38
-%BANK1 { #8000 }
39
-%BANK2 { #a000 }
40
-
41 23
 ( devices )
42 24
 
43 25
 |00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]
44
-|10 @Console    [ &pad    $8 &char     $1 &byte   $1 &short $2 &string $2 ]
45 26
 |20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y      $2 &addr  $2 &color $1 ]
46
-|80 @Controller [ &vector $2 &button   $1 &key    $1 ]
47 27
 |90 @Mouse      [ &vector $2 &x        $2 &y      $2 &state $1 &chord $1 ]
48 28
 
49 29
 ( variables )
50 30
 
51 31
 |0000
52 32
 
53
-@timer $2
33
+@timer $1
54 34
 @anchor [ &x $2 &y $2 ]
35
+@pointer [ &x $2 &y $2 ]
55 36
 
56 37
 ( program )
57 38
 
... ...
@@ -64,6 +45,7 @@
64 45
 
65 46
 	( vectors )
66 47
 	;on-frame   .Screen/vector DEO2
48
+	;on-mouse   .Mouse/vector DEO2
67 49
 
68 50
 	( glider )
69 51
 	#07 #03 ;set-cell JSR2
... ...
@@ -72,22 +54,22 @@
72 54
 	#07 #05 ;set-cell JSR2
73 55
 	#06 #05 ;set-cell JSR2
74 56
 
75
-	.Screen/width DEI2 #0002 // WIDTH #02 / TOS SUB2 .anchor/x POK2
76
-	.Screen/height DEI2 #0002 // HEIGHT #02 / TOS SUB2 .anchor/y POK2
57
+	.Screen/width DEI2 #0002 // WIDTH TOS -- .anchor/x POK2
58
+	.Screen/height DEI2 #0002 // HEIGHT TOS -- .anchor/y POK2
77 59
 
78 60
 BRK
79 61
 
80 62
 @on-frame ( -> )
81 63
 
82
-	.timer PEK #01 ADD [ DUP ] .timer POK
64
+	.timer PEK #01 + [ DUP ] .timer POK
83 65
 
84
-	#08 MOD #00 ! #01 JNZ [ BRK ] 
66
+	#10 MOD #00 ! #01 JNZ [ BRK ] 
85 67
 
86 68
 	( clear buffer )
87 69
 	BANK2 DUP2 GET-SIZE ++
88 70
 	&clear-loop
89 71
 		OVR2 #0000 SWP2 STA2
90
-		( incr ) SWP2 #0002 ADD2 SWP2
72
+		SWP2 #0002 ++ SWP2
91 73
 		OVR2 OVR2 !! ,&clear-loop JNZ
92 74
 	POP2 POP2
93 75
 
... ...
@@ -98,28 +80,68 @@ BRK
98 80
 	&copy-loop
99 81
 		OVR2 DUP2 LDA2 
100 82
 		SWP2 #2000 -- STA2
101
-		( incr ) SWP2 #0002 ADD2 SWP2
83
+		SWP2 #0002 ++ SWP2
102 84
 		OVR2 OVR2 !! ,&copy-loop JNZ
103 85
 	POP2 POP2
104 86
 
105
-	( draw )
87
+	;draw-grid JSR2
88
+
89
+BRK
90
+
91
+@on-mouse ( -> )
92
+	
93
+	( clear last cursor )
94
+	#fff8 .Screen/addr DEO2 
95
+	.pointer/x PEK2 .Screen/x DEO2 
96
+	.pointer/y PEK2 .Screen/y DEO2 
97
+	#30 .Screen/color DEO
98
+
99
+	( record pointer positions )
100
+	.Mouse/x DEI2 .pointer/x POK2 
101
+	.Mouse/y DEI2 .pointer/y POK2
102
+
103
+	( draw new cursor )
104
+	;cursor .Screen/addr DEO2 
105
+	.pointer/x PEK2 .Screen/x DEO2 
106
+	.pointer/y PEK2 .Screen/y DEO2 
107
+
108
+	( colorize on state )
109
+	#32 [ .Mouse/state DEI #00 ! ] + .Screen/color DEO
110
+
111
+	.Mouse/state DEI #00 ! #01 JNZ [ BRK ]
112
+
113
+	.Mouse/x DEI2 DUP2 .anchor/x PEK2 >> ROT ROT .anchor/x PEK2 WIDTH #02 * TOS ++ #0001 ++ << #0101 ==
114
+	.Mouse/y DEI2 DUP2 .anchor/y PEK2 >> ROT ROT .anchor/y PEK2 HEIGHT #02 * TOS ++ << #0101 ==
115
+	#0101 == ;on-touch JNZ2
116
+
117
+BRK
118
+
119
+@on-touch ( -> )
120
+
121
+	.Mouse/x DEI2 .anchor/x PEK2 SUB2 #02 / TOB
122
+	.Mouse/y DEI2 .anchor/y PEK2 SUB2 #02 / TOB
123
+	;set-cell JSR2
124
+
125
+BRK
126
+
127
+@draw-grid ( -- )
128
+
106 129
 	#00 HEIGHT
107 130
 	&ver
108
-		OVR TOS .anchor/y PEK2 ADD2 .Screen/y DEO2
131
+		OVR TOS #0002 ** .anchor/y PEK2 ++ .Screen/y DEO2
109 132
 		OVR STH
110 133
 		#00 WIDTH
111 134
 		&hor
112
-			OVR TOS .anchor/x PEK2 ADD2 .Screen/x DEO2
135
+			OVR TOS #0002 ** .anchor/x PEK2 ++ .Screen/x DEO2
113 136
 			OVR DUPr STHr ,get-cell JSR #01 + .Screen/color DEO
114
-			( incr ) SWP #01 + SWP
137
+			SWP #01 + SWP
115 138
 			DUP2 ! ,&hor JNZ
116
-		POP2
117
-		POPr
118
-		( incr ) SWP #01 + SWP
139
+		POP2 POPr
140
+		SWP #01 + SWP
119 141
 		DUP2 ! ,&ver JNZ
120 142
 	POP2
121 143
 
122
-BRK
144
+RTN
123 145
 
124 146
 @get-index ( x y -- index* )
125 147
 	
... ...
@@ -130,10 +152,10 @@ RTN
130 152
 
131 153
 @set-cell ( x y -- )
132 154
 	
133
-	DUP2 ,get-index JSR LDA STH
134
-	DUP2 POP #08 MOD #01 SWP SFL 
135
-	STHr SWP ORA STH
136
-	,get-index JSR STHr ROT ROT STA
155
+	DUP2 ,get-index JSR STH2
156
+	POP #08 MOD #01 SWP SFL 
157
+	DUP2r LDAr STHr SWP ORA
158
+	STH2r STA
137 159
 
138 160
 RTN
139 161
 
... ...
@@ -172,11 +194,10 @@ RTN
172 194
 			( neighbours ) DUP2r STH2r ,get-neighbours JSR
173 195
 			( state ) STH2r ;get-cell JSR2
174 196
 			,run-cell JSR
175
-			( incr ) SWP #01 + SWP
197
+			SWP #01 + SWP
176 198
 			DUP2 ! ,&hor JNZ
177
-		POP2
178
-		POPr
179
-		( incr ) SWP #01 + SWP
199
+		POP2 POPr
200
+		SWP #01 + SWP
180 201
 		DUP2 ! ,&ver JNZ
181 202
 	POP2
182 203
 
... ...
@@ -198,16 +219,16 @@ RTN
198 219
 
199 220
 @save-cell ( x y -- )
200 221
 	
201
-	,get-index-buffer JSR STH2
222
+	( get index )
223
+	HEIGHT MOD SWP WIDTH MOD SWP
224
+	WIDTH #08 / TOS ROT TOS ** ROT #08 / TOS ++ [ BANK2 ++ ]
225
+	( save in buffer )
226
+	STH2
202 227
 	DUP2 POP #08 MOD #01 SWP SFL 
203 228
 	DUP2r LDAr STHr SWP ORA
204 229
 	STH2r STA
205 230
 
206 231
 RTN
207 232
 
208
-@get-index-buffer ( x y -- index* )
209
-	
210
-	HEIGHT MOD SWP WIDTH MOD SWP
211
-	WIDTH #08 / TOS ROT TOS ** ROT #08 / TOS ++ [ BANK2 ++ ]
212
-
213
-RTN
233
+@cursor [ 
234
+	80c0 e0f0 f8e0 1000 ]
214 235
\ No newline at end of file
... ...
@@ -1,5 +1,13 @@
1 1
 ( a blank file )
2 2
 
3
+%+  { ADD } %-   { SUB }  %*  { MUL } %/   { DIV }  
4
+%<  { LTH } %>   { GTH }  %=  { EQU } %!   { NEQ } 
5
+%++ { ADD2 } %-- { SUB2 } %** { MUL2 } %// { DIV2 } 
6
+%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }  
7
+
8
+%DEBUG  { .Console/byte DEO #0a .Console/char DEO }
9
+%DEBUG2 { .Console/short DEO2 #0a .Console/char DEO }
10
+
3 11
 ( devices )
4 12
 
5 13
 |00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]