]>
wirehaze git hosting - lifeboot.git/blob - lifeboot.asm
4 ; =============================================================================
6 ; settings --------------------------------------------------------------------
8 %
define DEAD
' ' ; char to represent a dead cell
9 %
define ALIVE
'.' ; char to represent an alive cell
10 %
define PRINT_COLOR
07h ; grey on black
11 %
define ITER_LIM
500 ; reset the simulation after ITER_LIM iterations
13 ; constants -------------------------------------------------------------------
19 ; memory layout ---------------------------------------------------------------
25 ; static variables ------------------------------------------------------------
27 %
define xss DAT
; xs() state (word)
28 %
define currvgapg DAT
+ 2 ; current vga page near pointer (word)
29 %
define iter DAT
+ 4 ; current iteration # (word)
31 ; =============================================================================
33 ; entry point -----------------------------------------------------------------
35 ; initialize segment registers
47 ; clear direction flag
51 mov ch, 3fh
; cursor start and options
52 mov ah, 01h ; set text-mode cursor shape
53 int 10h
; video services
63 je set_xss_seed
; if seed is 0, xs wont work properly
65 mov [xss
], dx ; cx:dx = number of clock ticks since midnight
67 ; initialize currvgapg
68 mov word [currvgapg
], 0
70 ; start simulation from random state ------------------------------------------
77 ; apply game of life's rules to determine the next state ----------------------
82 call write_next_vga_page
88 cmp word [iter
], ITER_LIM
93 ; =============================================================================
95 ; functions -------------------------------------------------------------------
97 ; vsync_wait() - wait for display to enter the next VBlank cycle --------------
103 mov dx, 3dah
; input status #1 register
107 test al, 08h ; vertical retrace bit
117 ; xs() - xorshift pseudorandom number generator -------------------------------
120 ; [xss] = updated state
129 shl dx, 1 ; dx = xss << 1
130 xor bx, dx ; bx = xss ^ (xss << 1)
133 shr dx, 3 ; dx = xss' >> 3
134 xor bx, dx ; bx = xss' ^ (xss' >> 3)
137 shl dx, 10 ; dx = xss'' << 10
138 xor bx, dx ; bx = xss'' ^ (xss'' << 10)
143 ; init_grid() - initialize grid cells randomly --------------------------------
145 ; clobbers ax, bx, cx, dx, di
149 mov ah, PRINT_COLOR
; ah = color attribute
150 mov di, [currvgapg
] ; es:di -> grid start
151 mov cx, COLS
* ROWS
; for each cell
154 call xs
; [xss] = rand
155 mov al, DEAD
; al = DEAD (likely)
157 test word [xss
], 0b11
159 jnz .nz
; if ([xss] % 4 == 0)
160 mov al, ALIVE
; al = ALIVE
163 stosw ; [es:di] = ax ; di += 2
165 loop .write_cell
; } while (--cx)
169 ; flip_vga_page() - flip active display page ----------------------------------
175 xor word [currvgapg
], VGAPGSZ
; flip currvgapg
176 setnz al ; al = !(currvgapg == 0)
178 mov ah, 05h ; select active display page
179 int 10h
; video services
183 ; alive_neighbours() ----------------------------------------------------------
186 ; es:si -> current grid
190 ; ax = # alive neighbours
192 ; clobbers ax, bx, dx
199 ; [bp - 3]: neighbours
203 mov ax, cx ; ax = idx
204 mov bl, COLS
; bl = COLS
205 div bl ; al = idx / COLS ; ah = idx % COLS
207 mov [bp - 1], al ; row = idx / COLS
208 mov [bp - 2], ah ; row = idx % COLS
209 mov byte [bp - 3], 0 ; neighbours = 0
211 mov byte [bp - 4], -1 ; i = -1
214 mov byte [bp - 5], -1 ; j = -1
219 jz .continue
; if (!i && !j) continue
222 add al, [bp - 4] ; al = row + i
225 jl .continue
; if (row + i < 0) continue
228 jge .continue
; if (row + i >= ROWS) continue
231 add ah, [bp - 5] ; ah = col + j
234 jl .continue
; if (col + j < 0) continue
237 jge .continue
; if (col + j >= COLS) continue
239 movzx bx, ah ; bx = col + j
242 mul ah ; ax = (row + i) * COLS
244 add bx, ax ; bx = cell index
245 shl bx, 1 ; bx = grid offset
247 mov dx, [es:si + bx] ; dl = cell state
249 cmp dl, ALIVE
; if (!ALIVE) continue
252 inc byte [bp - 3] ; neighbours++
257 jle .j
; } while (++j <= 1)
261 jle .i
; } while (++i <= 1)
263 movzx ax, byte [bp - 3] ; ax = # alive neighbours
268 ; write_next_cell_state() -----------------------------------------------------
271 ; es:si -> current grid
276 ; [es:di + cx * 2] = updated cell state
278 ; clobbers ax, bx, dx
280 write_next_cell_state:
282 call alive_neighbours
; ax = # alive neighbours
287 mov dx, [es:si + bx] ; dl = cell current state
289 cmp dl, ALIVE
; if (ALIVE) {
292 cmp ax, 2 ; if (n < 2)
293 jl .dead
; return DEAD
295 cmp ax, 3 ; if (n > 3)
296 jg .dead
; return DEAD
298 jmp .alive
; return ALIVE
302 cmp ax, 3 ; if (n != 3)
303 jne .dead
; return DEAD
305 .alive: ; return ALIVE
317 ; write_next_vga_page() -------------------------------------------------------
319 ; clobbers di, si, ax, bx, cx, dx
323 mov si, [currvgapg
] ; es:si -> current page
326 xor di, si ; es:di -> next page
331 call write_next_cell_state
335 jl .update_loop
; for i in [0 .. COLS * ROWS - 1]
339 ; halt() - stop program execution ---------------------------------------------
343 cli ; disable interrupts
347 ; =============================================================================
349 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
350 dw 0aa55h ; mbr magic byte