public
Last active

Implementation of Conway's Game of Life for DCPU.

  • Download Gist
life.dasm
DCPU-16 ASM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
;
; Implementation of Conway's Game of Life.
;
; http://en.wikipedia.org/wiki/Conway's_Game_of_Life
; Uses 32x12 screen at 0x8000 and game field array at 0x1000.
;
; The field is self-closed in every direction
; but it has bugs, i hope i'll fix them some day.
; I prefer to run and debug it at http://mappum.github.com/DCPU-16/
; thanks to Mappum for this great tool.
;
; There is no delay in the game, it's runs as fast as it can.
; But you can uncomment STEP BRK.
;
; Game screen with glider on it: http://i.imgur.com/EZfPg.png
;
 
;set filed map elements to 0, but thay are already 0 so it's in commentary
;SET I, 16
;:LOOPSTARTX
; SUB I, 1
; SET J, 32
; :LOOPSTARTY
; SUB J, 1
; SET A, I
; MUL A, 32
; ADD A, J
; ADD A, 0x1000
; SET [A], 0
; IFN J, 0
; SET PC, LOOPSTARTY
; IFN I, 0
; SET PC, LOOPSTARTX
 
;set some start shapes, like a glider
SET A, 0x1000
ADD A, 0xC8
SET [A], 1
ADD A, 1
SET [A], 1
ADD A, 1
SET [A], 1
ADD A, 32
SET [A], 1
ADD A, 31
SET [A], 1
;infinity main loop starts
:MAINLOOP
 
;print field on screen
SET I, 12
:PRINTLOOPX
SUB I, 1
SET J, 32
:PRINTLOOPY
SUB J, 1
SET A, I
MUL A, 32
ADD A, J
ADD A, 0x1000
SET X, 0x7000
ADD X, A
IFE [A], 2
SET [A], 1
IFE [A], 3
SET [A], 0
IFE [A], 1
SET [X], 48
IFE [A], 0
SET [X], 46
IFN J, 0
SET PC, PRINTLOOPY
IFN I, 0
SET PC, PRINTLOOPX
 
;count changed cells
SET I, 12
:LOOPX
SUB I, 1
SET J, 32
:LOOPY
SUB J, 1
SET A, I
MUL A, 32
ADD A, J
ADD A, 0x1000
JSR COUNT
IFE [A], 1
SET PC, ALIVE
IFE [A], 3
SET PC, ALIVE
IFE C, 3
SET [A], 2
SET PC, NEXT
:ALIVE
IFE C, 3
SET PC, NEXT
IFE C, 2
SET PC, NEXT
SET [A], 3
:NEXT
IFN J, 0
SET PC, LOOPY
IFN I, 0
SET PC, LOOPX
; step mode BRK, if BRK is supported in your emulator:
; uncomment it to play game frame by frame
;BRK
;infinity main loop loops
SET PC, MAINLOOP
 
 
:COUNT
;C will be count of cells around
SET C, 0
 
;left top
SET X, 33
JSR SUBX
 
;center top
SET X, 32
JSR SUBX
 
;right top
SET X, 31
JSR SUBX
 
;left left
JSR LEFT
 
;right right
JSR RIGHT
 
;left bottom
SET X, 31
JSR ADDX
 
;center bottom
SET X, 32
JSR ADDX
 
;right bottom
SET X, 33
JSR ADDX
 
SET PC, POP
 
 
 
:SUBX
SET B, A
SUB B, X
IFN O, 1
SET PC, MARK1
SET B, A
SUB X, B
SET Y, 384
SUB Y, X
SET B, Y
:MARK1
IFE [B], 1
ADD C, 1
IFE [B], 3
ADD C, 1
SET PC, POP
 
 
 
:ADDX
SET B, A
ADD B, X
IFG B, 0x1180
SUB B, 384
IFE [B], 1
ADD C, 1
IFE [B], 3
ADD C, 1
SET PC, POP
 
 
 
:LEFT
SET B, A
IFE J, 0
ADD B, 32
SUB B, 1
IFE [B], 1
ADD C, 1
IFE [B], 3
ADD C, 1
SET PC, POP
 
 
 
:RIGHT
SET B, A
IFE I, 31
SUB B, 32
ADD B, 1
IFE [B], 1
ADD C, 1
IFE [B], 3
ADD C, 1
SET PC, POP

In :RIGHT, the line "SUB B, 31" should be "SUB B, 32", I believe.

On line 15 it should be "glider" rather than "plane"

sry, english is not mine first language.
btw, it was 3 months ago, how did you get here?

No need to apologize, English is hard to learn.

I remember seeing this on Reddit (must have been 3 months ago now) and I thought this was a nifty program. I saved the link so I could put this on my ship in 0x10c when it came out. So, I was going through so of my bookmarks and I happened upon this link. The "plane" thing was one of the things I noticed.

PS: Damn, it is line 13 not 15. Sorry!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.