From http://www.jwz.org/xscreensaver/xscreensaver-5.36.tar.gz
[xscreensaver] / hacks / images / m6502 / sflake.asm
1 ;; Jeremy English 01-December-2008
2 ;; Snowflakes
3
4 ;; Main loop Count
5 lda #7
6 sta $f
7
8 ;; pattern number
9 lda #0
10 sta $10
11
12 ;;Cells
13 lda #$00
14 sta $4
15 lda #$10
16 sta $5
17
18 ;;Tmp
19 lda #$00
20 sta $6
21 lda #$11
22 sta $7
23
24 ;;Init Cells Buffer
25 ;;------------------------------------------------------------
26 ldy #$ff
27 initCells:
28 lda #0
29 sta ($4),y
30 sta ($6),y
31 dey
32 bne initCells
33
34 ;;Set start position
35 ldy #115
36 lda #1
37 sta ($4),y
38    
39 ;;Setup offset
40 lda #15
41 sta $d
42 lda #16
43 sta $e
44
45 ;;Start of main loop
46 ;;------------------------------------------------------------
47 mainloop:
48
49 ;;init indent
50 ;;We want to indent every other line
51 lda #0
52 sta $a
53
54 lda #0
55 sta $9
56
57 ;; Display Cells
58 ;;------------------------------------------------------------
59 ;; 248 is the total number of cells
60 ldy #248
61 display:
62
63 lda #0
64 sta $8
65
66 lda $a
67 beq stop16
68 lda #15
69 sta $b
70 lda #1
71 sta $8
72 jmp toggle
73 stop16:
74 lda #16
75 sta $b
76 toggle:
77 lda $a
78 eor #1
79 sta $a
80
81 ;; Set the stop position
82 ldx $b
83 inner_display:
84 dex
85 txa
86 pha
87
88 dey
89 tya
90 pha
91 lda ($4),y
92
93 beq display_continue
94 ldx $8
95 ldy $9
96 lda #1
97 jsr paint
98 ldx $8
99 ldy $9
100 inx
101 jsr paint
102 ldx $8
103 ldy $9
104 iny
105 jsr paint
106 ldx $8
107 ldy $9
108 inx
109 iny
110 jsr paint
111 display_continue:
112 inc $8
113 inc $8
114
115 ;;Life Cycle
116 ;;------------------------------------------------------------
117 pla
118 tay
119 pha ;;Store y on the stack
120
121 tax
122 dey
123 lda ($4),y
124 iny
125 iny
126 clc
127 adc ($4),y
128 sta $c
129
130 txa
131 sec
132 sbc $d
133 tay
134 lda $c
135 clc
136 adc ($4),y
137 sta $c
138
139 txa
140 sec
141 sbc $e
142 tay
143 lda $c
144 clc
145 adc ($4),y
146 sta $c
147
148 txa
149 clc
150 adc $d
151 tay
152 lda $c
153 clc
154 adc ($4),y
155 sta $c
156
157 txa
158 clc
159 adc $e
160 tay
161 lda $c
162 clc
163 adc ($4),y
164 sta $c
165
166 pla
167 tay ;;Pull Y off of the stack
168
169 lda $c
170 and #1
171 beq dontset
172 sta ($6),y
173 dontset:
174    
175 pla
176 tax ;;Pull x off of the stack   
177 beq exit_inner_display
178 jmp inner_display
179 exit_inner_display:
180 inc $9
181 inc $9
182 tya
183 beq display_exit
184 jmp display
185 display_exit:
186
187
188 ;;Copy Temporary Buffer
189 ;;------------------------------------------------------------
190 ldy #248
191 copybuf:
192 dey
193 lda ($6),y
194 sta ($4),y
195 tya
196 bne copybuf
197
198 dec $f
199 lda $f
200 beq reset_main
201 jmp mainloop
202
203 ;;Reset main counter
204 ;;------------------------------------------------------------
205 reset_main:
206 lda #7
207 sta $f
208
209 lda #$ff ;;Delay Count
210 sta $11
211 delay:
212 ldy #$a0
213 inner_delay:
214 nop
215 dey
216 bne inner_delay
217 dec $11
218 lda $11
219 bne delay
220
221 ;; init buffer
222 ;; and clear screen
223 clrscr:
224 lda $fe
225 and $f
226 cmp #1
227 beq clrscr ;We don't want a white background
228 ldy #$00
229 ldx #$0
230 cs_loop: 
231 sta $200,x
232 sta $300,x
233 sta $400,x
234 sta $500,x
235 pha
236 lda #0
237 sta ($6),y
238 sta ($4),y
239 pla
240 inx
241 dey
242 bne cs_loop
243
244 ;; Setup new pattern
245 ;;------------------------------------------------------------
246 inc $10
247 lda $10
248 and #3
249 sta $10
250 cmp #0
251 beq pattern1
252 cmp #1
253 beq pattern2
254 cmp #2
255 beq pattern3
256 cmp #3
257 beq pattern4
258
259 pattern1:
260 ldy #114
261 lda #1
262 sta ($4),y
263 ldy #115
264 lda #1
265 sta ($4),y
266 ldy #116
267 lda #1
268 sta ($4),y
269 jmp mainloop
270
271 pattern2:
272 ldy #113
273 lda #1
274 sta ($4),y
275 ldy #118
276 lda #1
277 sta ($4),y
278 jmp mainloop
279
280 pattern3:
281 ldy #115
282 lda #1
283 sta ($4),y
284 jmp mainloop
285
286 pattern4:
287 ldy #102
288 lda #1
289 sta ($4),y
290 ldy #128
291 lda #1
292 sta ($4),y
293 jmp mainloop
294
295 ;;Paint subroutine
296 ;;------------------------------------------------------------
297 paint:
298    pha
299    lda yl,y
300    sta $2
301    lda yh,y
302    sta $3
303    txa
304    tay
305    pla
306    sta ($2),y
307    rts
308
309        ;; Y cord MSB   
310 yh:
311        dcb $02, $02, $02, $02, $02, $02, $02, $02
312        dcb $03, $03, $03, $03, $03, $03, $03, $03
313        dcb $04, $04, $04, $04, $04, $04, $04, $04
314        dcb $05, $05, $05, $05, $05, $05, $05, $05
315        ;; Y cord LSB
316 yl:
317        dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
318        dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
319        dcb $00, $20, $40, $60, $80, $a0, $c0, $e0
320        dcb $00, $20, $40, $60, $80, $a0, $c0, $e0