1; Conway\'s Game of Life
2; http://rosettacode.org/wiki/Conway\'s_Game_of_Life
3; Submitted by Anonymous
4
5randfill:   stx $01          ;$200 for indirect
6            ldx #$02         ;addressing
7            stx $02
8randloop:   lda $fe          ;generate random
9            and #$01         ;pixels on the
10            sta ($01),Y      ;screen
11            jsr inc0103
12            cmp #$00
13            bne randloop
14            lda $02
15            cmp #$06
16            bne randloop
17
18
19clearmem:   lda #$df         ;set $07df-$0a20
20            sta $01          ;to $#00
21            lda #$07
22            sta $02
23clearbyte:  lda #$00
24            sta ($01),Y
25            jsr inc0103
26            cmp #$20
27            bne clearbyte
28            lda $02
29            cmp #$0a
30            bne clearbyte
31
32
33starttick:
34copyscreen: lda #$00         ;set up source
35            sta $01          ;pointer at
36            sta $03          ;$01/$02 and
37            lda #$02         ;dest pointer
38            sta $02          ;at $03/$04
39            lda #$08
40            sta $04
41            ldy #$00
42copybyte:   lda ($01),Y      ;copy pixel to
43            sta ($03),Y      ;back buffer
44            jsr inc0103      ;increment pointers
45            cmp #$00         ;check to see
46            bne copybyte     ;if we\'re at $600
47            lda $02          ;if so, we\'ve
48            cmp #$06         ;copied the
49            bne copybyte     ;entire screen
50
51
52conway:     lda #$df         ;apply conway rules
53            sta $01          ;reset the pointer
54            sta $03          ;to $#01df/$#07df
55            lda #$01         ;($200 - $21)
56            sta $02          ;($800 - $21)
57            lda #$07
58            sta $04
59onecell:    lda #$00         ;process one cell
60            ldy #$01         ;upper cell
61            clc
62            adc ($03),Y
63            ldy #$41         ;lower cell
64            clc
65            adc ($03),Y
66chkleft:    tax              ;check to see
67            lda $01          ;if we\'re at the
68            and #$1f         ;left edge
69            tay
70            txa
71            cpy #$1f
72            beq rightcells
73leftcells:  ldy #$00         ;upper-left cell
74            clc
75            adc ($03),Y
76            ldy #$20         ;left cell
77            clc
78            adc ($03),Y
79            ldy #$40         ;lower-left cell
80            clc
81            adc ($03),Y
82chkright:   tax              ;check to see
83            lda $01          ;if we\'re at the
84            and #$1f         ;right edge
85            tay
86            txa
87            cpy #$1e
88            beq evaluate
89rightcells: ldy #$02         ;upper-right cell
90            clc
91            adc ($03),Y
92            ldy #$22         ;right cell
93            clc
94            adc ($03),Y
95            ldy #$42         ;lower-right cell
96            clc
97            adc ($03),Y
98evaluate:   ldx #$01         ;evaluate total
99            ldy #$21         ;for current cell
100            cmp #$03         ;3 = alive
101            beq storex
102            ldx #$00
103            cmp #$02         ;2 = alive if
104            bne storex       ;c = alive
105            lda ($03),Y
106            and #$01
107            tax
108storex:     txa              ;store to screen
109            sta ($01),Y
110            jsr inc0103      ;move to next cell
111conwayloop: cmp #$e0         ;if not last cell,
112            bne onecell      ;process next cell
113            lda $02
114            cmp #$05
115            bne onecell
116            jmp starttick    ;run next tick
117
118
119inc0103:    lda $01          ;increment $01
120            cmp #$ff         ;and $03 as 16-bit
121            bne onlyinc01    ;pointers
122            inc $02
123            inc $04
124onlyinc01:  inc $01
125            lda $01
126            sta $03
127            rts
128