Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created April 12, 2012 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theepicsnail/2364589 to your computer and use it in GitHub Desktop.
Save theepicsnail/2364589 to your computer and use it in GitHub Desktop.
Life
jsr init
jsr mainLoop
sub pc,1
;copy from 0x8200-0x83ff to 0x8000-0x81ff
:blit
set push,i
set i,0
:blitLoop
set [0x8000+i],[0x8200+i]
set [0x8001+i],[0x8201+i]
set [0x8002+i],[0x8202+i]
set [0x8003+i],[0x8203+i]
set [0x8004+i],[0x8204+i]
set [0x8005+i],[0x8205+i]
set [0x8006+i],[0x8206+i]
set [0x8007+i],[0x8207+i]
set [0x8008+i],[0x8208+i]
set [0x8009+i],[0x8209+i]
set [0x800a+i],[0x820a+i]
set [0x800b+i],[0x820b+i]
set [0x800c+i],[0x820c+i]
set [0x800d+i],[0x820d+i]
set [0x800e+i],[0x820e+i]
set [0x800f+i],[0x820f+i]
add i,0x10
ifn i,0x200
set pc,blitloop
set i,pop
set pc,pop
;Init
:init
set i,0
:initi
set j,[seed+i]
set [j],[alive]
add i,1
ifn [seed+i],0
set pc, initi
set pc,pop
;Main loop
:mainLoop
; jsr wait;slow down the program if needed
jsr blit
jsr step
ife [0x9000],0; Exit on first keystroke.
set pc,mainLoop
set pc,pop
; Calculate the next living cells based on the screen, store
; the results in the 0x8200-0x23ff block (to be blitted later)
:step
set push,i;we alter i, so store the old value
set i,0xffff
:loopi add i,1
jsr testAlive
set [0x8200+i],x
ife x,[alive]
jsr debugger
ifn i,0x200
set pc,loopi
;end loopi
set i,pop
set pc,pop
;end step
;testAlive
;in i - position to test
;out x - [dead] or [alive]
:testAlive
jsr countNeighbors
set x,[dead]
ife c,3
set x,[alive]
ife [0x8000+i],[dead]
set pc,endTestAlive
ife 2,c
set x,[alive]
:endTestAlive
set pc,pop
;in i - position to count
;out c - the number of living neighbors
:countNeighbors
set push,y
set push,x
set c,0;counter
set x,0x001F
and x,i; get the column
set y,0x01E0
and y,i; get the row
:topLeft
ife y,0
set pc,midLeft ; skip the row
ife x,0
set pc,topCenter
ife [0x7fdf+i],[alive]
add c,1
:topCenter
ife [0x7fe0+i],[alive]
add c,1
;topRight
ife x,0x1f
set pc,midLeft
ife [0x7fe1+i],[alive]
add c,1
:midLeft
ife x,0
set pc,midRight;no midCenter test
ife [0x7fff+i],[alive]
add c,1
:midRight
ife x,0x1f
set pc,botLeft
ife [0x8001+i],[alive]
add c,1
:botLeft
ife y,0xf
set pc,endCountNeighbors
ife x,0
set pc,botCenter
ife [0x801f+i],[alive]
add c,1
:botCenter
ife [0x8020+i],[alive]
add c,1
;botRight
ife [0x8021+i],[alive]
add c,1
:endCountNeighbors
set x,pop
set y,pop
set pc,pop
;end testAlive
;output
:alive dat 0x8231
:dead dat 0x0
;:seed dat 0x8242,0x8222,0x8202 ; 3x1 pulser
; dat 0x82c7,0x82c8,0x82e7,0x82e9,0x8307;glider
:seed dat 0x8288,0x8289,0x828a, 0x828c
dat 0x82a8
dat 0x82cb,0x82cc
dat 0x82e9,0x82ea, 0x82ec
dat 0x8308, 0x830a, 0x830c
dat 0; end of seed
:debugger
set x,x
set pc,pop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment