Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;gol.asm
- define r16a $f0
- define r16al $f0
- define r16ah $f1
- define r16b $f2
- define r16bl $f2
- define r16bh $f3
- define r16c $f4
- define r16cl $f4
- define r16ch $f5
- define tmpA $f6
- define tmpX $f7
- define tmpY $f8
- define ccount $f9
- define tmp0 $fa
- define tmp1 $fb
- define tmp2 $fc
- define tmpf $fd
- define random $fe
- define key $ff
- *=$200
- screen:
- screen0:
- *=$300
- screen1:
- *=$400
- screen2:
- *=$500
- screen3:
- *=$fc00
- back_buffer:
- back_buffer0:
- *=$fd00
- back_buffer1:
- *=$fe00
- back_buffer2:
- *=$ff00
- back_buffer3:
- *=$600
- init:
- lda #0 ;clean up zp and stack
- tax ;^^
- init_loop_a: ;^^
- sta $00,x ;^^
- sta $100,x ;^^
- inx ;^^
- bne init_loop_a ;^^
- ldx #$ff ;reset registers, including the stack
- txs ;^^
- tax ;^^(A is currently 0)
- tay ;^^
- init_loop_b:
- ;fill in buffer with either white or black pixels randomly
- lda random
- and #1
- sta back_buffer0,x
- lda random
- and #1
- sta back_buffer1,x
- lda random
- and #1
- sta back_buffer2,x
- lda random
- and #1
- sta back_buffer3,x
- ;also turn the screen black
- lda #0
- sta screen0,x
- sta screen1,x
- sta screen2,x
- sta screen3,x
- inx
- bne init_loop_b ;loop will break once X wraps to 0
- main:
- jsr flip_screen
- jsr update_cells
- lda key ;if a key was pressed, jmp to init
- beq main_loop_end ;^^
- jmp init ;^^
- main_loop_end:
- jmp main
- ;r16a += A
- add8_16:
- clc
- adc r16al
- sta r16al
- bcc _add8_16_skip_increment
- inc r16ah
- _add8_16_skip_increment:
- rts
- ;r16a -= A
- sub8_16:
- sta tmp0
- lda r16al
- sec
- sbc tmp0
- sta r16al
- bcs _sub8_16_skip_decrement
- dec r16ah
- _sub8_16_skip_decrement:
- rts
- ;++r16a
- add1_16:
- inc r16al
- bne _add1_16_skip_hi_increment
- inc r16ah
- _add1_16_skip_hi_increment:
- rts
- ;++r16c
- add1_16c:
- inc r16cl
- bne _add1_16c_skip_hi_increment
- inc r16ch
- _add1_16c_skip_hi_increment:
- rts
- flip_screen:
- ldx #0
- _flip_screen_loop:
- lda back_buffer0,x
- sta screen0,x
- lda back_buffer1,x
- sta screen1,x
- lda back_buffer2,x
- sta screen2,x
- lda back_buffer3,x
- sta screen3,x
- inx
- bne _flip_screen_loop
- rts
- ;checks neighboring cells around "(r16a)"
- ;A should contain flags for which cells to check/skip
- ;bits 7,6,5,4,3,2,1,0 = NW,N,NE,W,E,SW,S,SE respectively
- ;cell count is returned into X
- count_neighboring_cells: ;r16a,r16b
- sty tmpY ;save state of Y
- tax ;save cell flags to X and tmpf
- sta tmpf ;^^
- lda #0 ;reset cell counter
- sta ccount ;^^
- tay ;set indirection index to 0
- lda r16al ;save state of r16a to r16b
- sta r16bl ;^^
- lda r16ah ;^^
- sta r16bh ;^^
- ;north west
- lda #33 ;-32 (up) -1 (left) for nw cell
- jsr sub8_16 ;^^
- txa ;skip nw cell if flag 7 ==0
- and #%10000000 ;^^
- beq _cnc_no_nw ;^^
- lda (r16a),y ;increment if nw cell !=0
- beq _cnc_no_nw ;^^
- inc ccount ;^^
- _cnc_no_nw: ;^^
- ;north
- iny ;+1 (right) for n cell
- txa ;skip n cell if flag 6 ==0
- and #%01000000 ;^^
- beq _cnc_no_n ;^^
- lda (r16a),y ;increment if n cell !=0
- beq _cnc_no_n ;^^
- inc ccount ;^^
- _cnc_no_n: ;^^
- ;north east
- iny ;+1 (right) for ne cell
- txa ;skip ne cell if flag 5 ==0
- and #%00100000 ;^^
- beq _cnc_no_ne ;^^
- lda (r16a),y ;increment if ne cell !=0
- beq _cnc_no_ne ;^^
- inc ccount ;^^
- _cnc_no_ne: ;^^
- ;west
- ldy #0 ;reset indirection index (-2 (left))
- lda #32 ;+32 (down) for w cell
- jsr add8_16 ;^^
- txa ;skip w cell if flag 4 ==0
- and #%00010000 ;^^
- beq _cnc_no_w ;^^
- lda (r16a),y ;increment if w cell !=0
- beq _cnc_no_w ;^^
- inc ccount ;^^
- _cnc_no_w: ;^^
- ;east
- ldy #2 ;+2 (right) for e cell
- txa ;skip e cell if flag 3 ==0
- and #%00001000 ;^^
- beq _cnc_no_e ;^^
- lda (r16a),y ;increment if e cell !=0
- beq _cnc_no_e ;^^
- inc ccount ;^^
- _cnc_no_e: ;^^
- ;south west
- ldy #0 ;reset indirection index (-2 (left))
- lda #32 ;+32 (down) for sw cell
- jsr add8_16 ;^^
- txa ;skip w cell if flag 2 ==0
- and #%00000100 ;^^
- beq _cnc_no_sw ;^^
- lda (r16a),y ;increment if sw cell !=0
- beq _cnc_no_sw ;^^
- inc ccount ;^^
- _cnc_no_sw: ;^^
- ;south
- iny ;+1 (right) for s cell
- txa ;skip s cell if flag 1 ==0
- and #%00000010 ;^^
- beq _cnc_no_s ;^^
- lda (r16a),y ;increment if s cell !=0
- beq _cnc_no_s ;^^
- inc ccount ;^^
- _cnc_no_s: ;^^
- ;south east
- iny ;+1 (right) for se cell
- txa ;skip se cell if flag 0 ==0
- and #%00000001 ;^^
- beq _cnc_no_se ;^^
- lda (r16a),y ;increment if se cell !=0
- beq _cnc_no_se ;^^
- inc ccount ;^^
- _cnc_no_se: ;^^
- ;wrap things up
- lda r16bl ;restore r16a's original value
- sta r16al ;^^
- lda r16bh ;^^
- sta r16ah ;^^
- ;ldy tmpY ;restore original value of Y
- txa ;restore cell flags
- ldx ccount ;return cell count into X
- rts
- ;assumes r16a and r16c are tied to the screen and back buffer,
- ;and that A=cell flags
- update_cell:
- jsr count_neighboring_cells
- cpx #2 ;if neighbors == 2,
- beq _update_cell_skip ;^^don't do anything to the cell
- cpx #3 ;else if neighbors != 3,
- bne _update_cell_off ;^^turn cell off
- ;_update_cell_on: ;else turn cell on
- lda #1 ;^^
- jmp _update_cell_set ;^^
- _update_cell_off: ;turn cell off
- lda #0 ;^^
- _update_cell_set: ;set cell based on the two prev. branches
- ldy #0 ;^^
- sta (r16c),y ;^^
- _update_cell_skip:
- ldy tmpY ;previously saved by count_neighboring_cells
- lda tmpf
- rts
- ;assumes back buffer was just copied to the screen (front buffer)
- update_cells:
- lda #30 ;set tmp2 to 30 (32 rows - start and end rows = 30)
- sta tmp2 ;^^(tmp2 used as a row counter here)
- lda #<screen ;set r16a to start of screen
- sta r16al ;^^
- lda #>screen ;^^
- sta r16ah ;^^
- lda #<back_buffer ;set r16c to start of back buffer
- sta r16cl ;^^
- lda #>back_buffer ;^^
- sta r16ch ;^^
- ;row 0
- lda #%00001011 ;e,s,se
- jsr update_cell ;update cell @ r16c
- jsr add1_16 ;increment r16a
- jsr add1_16c ;increment r16c
- lda #%00011111 ;w,e,sw,s,se
- ldy #30 ;32 - the start and end pixels = 30
- _uc_row0_loop:
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- dey ;will break once y reaches 0
- bne _uc_row0_loop ;^^
- lda #%00010110 ;w,sw,s
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- ;row 1 -> 30
- lda #%01101011 ;n,ne,e,s,sw
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- lda #%11111111 ;nw,n,ne,w,e,sw,s,se
- _uc_rows_loop:
- ldy #30
- _uc_cols_loop:
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- dey
- bne _uc_cols_loop
- dec tmp2
- bne _uc_rows_loop
- lda #%11010110 ;nw,n,w,sw,s
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- ;row 31
- lda #%01101000 ;n,ne,e
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- lda #%11111000 ;nw,n,ne,w,e
- ldy #30
- _uc_row31_loop:
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- dey
- bne _uc_row31_loop
- lda #%11010000 ;nw,n,w
- jsr update_cell
- jsr add1_16
- jsr add1_16c
- ;return
- rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement