Skip to content

Instantly share code, notes, and snippets.

@ulrikdamm
Created April 7, 2012 09:32
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 ulrikdamm/2326824 to your computer and use it in GitHub Desktop.
Save ulrikdamm/2326824 to your computer and use it in GitHub Desktop.
Game of Life for DCPU
; My implementation of Conways' Game of Life for Notch's DCPU.
; Since screen sizes vary, please test it with this online emulator: http://mappum.github.com/DCPU-16/
; It takes about 80k cycles to do a screen update, so have patience :)
; Made by Ulrik F. Damm
set pc, main
:fillboard ; (a: from board, b: to board, x: width, y: height)
set i, 0
set j, 0
:rowloop
ife i, x
set pc, fillboardend
set j, 0
jsr colloop
add i, 1
set pc, rowloop
:colloop
ife j, y
set pc, pop
jsr pixelop
add j, 1
set pc, colloop
:pixelop
set push, b
jsr fillboardcount
set b, pop
; j * x + i + a
set c, 0
set c, j
mul c, x
add c, i
add c, a
ife [c], 1
set pc, isalive
set pc, isdead
:isalive
ife z, 2
set pc, live
ife z, 3
set pc, live
set pc, die
:isdead
ife z, 3
set pc, live
set pc, die
:die
; j * x + i + b
set c, 0
set c, j
mul c, x
add c, i
add c, b
set [c], 0
set pc, pixelopend
:live
; j * x + i + b
set c, 0
set c, j
mul c, x
add c, i
add c, b
set [c], 1
:pixelopend
set pc, pop
:fillboardend
set pc, pop
:fillboardcount ; (a: from board, x: width, y: height, i: x, j: y -> z: count)
;allocating variable space
set push, 0
set push, 0
set c, sp
; c + 0: current pixel, c + 1: result
; j * x + i + a
set [0+c], j
mul [0+c], x
add [0+c], i
add [0+c], a
:left
; left: i == 0? z += (x-1): z -= 1
set z, [0+c]
ife i, 0 ; at left edge
set pc, leftedge
sub z, 1 ; if i > 0
set pc, leftset
:leftedge
add z, x ; if i == 0
sub z, 1 ; x - 1
:leftset
jsr fillboardcountcheckz
set push, z
; left top: j == 0? z += (y-1) * x: z -= x
ife j, 0
set pc, lefttopedge
sub z, x
set pc, lefttopset
:lefttopedge
set push, c
set c, y
sub c, 1
mul c, x
add z, c
set c, pop
:lefttopset
jsr fillboardcountcheckz
set z, pop
set push, z
; left bottom: j == (y-1)? z -= (y-1) * x: z += x
set push, c
set c, y
sub c, 1
ife j, c
set pc, leftbotedge
add z, x
set pc, leftbotset
:leftbotedge
set push, c
set c, y
sub c, 1
mul c, x
sub z, c
set c, pop
:leftbotset
set c, pop
jsr fillboardcountcheckz
set z, pop
:right
; left: i == (x-1)? z -= (x-1): z += 1
set z, [0+c]
set push, c
set c, x
sub c, 1
ife i, c ; at right edge
set pc, rightedge
add z, 1 ; no
set pc, rightset
:rightedge
sub z, c ; yes
:rightset
set c, pop
jsr fillboardcountcheckz
set push, z
; right top: j == 0? z += (y-1) * x: z -= x
ife j, 0
set pc, righttopedge
sub z, x
set pc, righttopset
:righttopedge
set push, c
set c, y
sub c, 1
mul c, x
add z, c
set c, pop
:righttopset
jsr fillboardcountcheckz
set z, pop
set push, z
; left bottom: j == (y-1)? z -= (y-1) * x: z += x
set push, c
set c, y
sub c, 1
ife j, c
set pc, rightbotedge
add z, x
set pc, rightbotset
:rightbotedge
set push, c
set c, y
sub c, 1
mul c, x
sub z, c
set c, pop
:rightbotset
set c, pop
jsr fillboardcountcheckz
set z, pop
:middle
set z, [0+c]
; top: j == 0? z += (y-1) * x: z -= x
ife j, 0
set pc, topedge
sub z, x
set pc, topset
:topedge
set push, c
set c, y
sub c, 1
mul c, x
add z, c
set c, pop
:topset
jsr fillboardcountcheckz
set z, [0+c]
; bottom: j == (y-1)? z -= (y-1) * x: z += x
set push, c
set c, y
sub c, 1
ife j, c
set pc, botedge
add z, x
set pc, botset
:botedge
set push, c
set c, y
sub c, 1
mul c, x
sub z, c
set c, pop
:botset
set c, pop
jsr fillboardcountcheckz
set z, [1+c]
set 0, pop
set 0, pop
set pc, pop
:fillboardcountcheckz
ife [z], 1
add [1+c], 1
set pc, pop
:main
set a, heapstart
set b, heapstart
add b, 372
set [0+a], 1
set [1+a], 1
set [2+a], 1
set [31+a], 1
set [33+a], 1
set [62+a], 1
set [63+a], 1
set [64+a], 1
:start
set x, 0
:loop
set y, a
add y, x
set z, [y]
add z, 0x30
set y, 0x8000
add y, x
set [y], z
add x, 1
ife x, 372
set pc, fill
set pc, loop
:fill
set x, 31
set y, 12
jsr fillboard
set c, a
set a, b
set b, c
set pc, start
:end
set pc, end
:heapstart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment