]>
wirehaze git hosting - lifeboot.git/blob - lifeboot.asm
e44a671b9c8e4644491e1422377419354509cad0
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 WAIT_DELAY
02h ; 0.131072 seconds
13 ; constants -------------------------------------------------------------------
17 %
define VGAPGSZ
(COLS
* ROWS
* 2 + 96) ; why 96 ??????
19 ; memory layout ---------------------------------------------------------------
25 ; static variables ------------------------------------------------------------
27 %
define xss DAT
; xs() state (word)
28 %
define currvgapg DAT
+ 2 ; current vga page near pointer (word)
30 ; =============================================================================
32 ; entry point -----------------------------------------------------------------
34 ; initialize segment registers
46 ; clear direction flag
50 mov ch, 3fh
; cursor start and options
51 mov ah, 01h ; set text-mode cursor shape
52 int 10h
; video services
62 je set_xss_seed
; if seed is 0, xs wont work properly
64 mov [xss
], dx ; cx:dx = number of clock ticks since midnight
66 ; initialize currvgapg
67 mov word [currvgapg
], 0
69 ; start simulation from random state ------------------------------------------
75 ; apply game of life's rules to determine the next state ----------------------
80 call write_next_vga_page
86 ; =============================================================================
88 ; functions -------------------------------------------------------------------
90 ; delay() - suspend program execution temporarily -----------------------------
96 mov cx, WAIT_DELAY
; cx:dx = interval in microseconds
104 ; wait_keypress() - wait for any keypress -------------------------------------
110 mov ah, 00h ; read key press
111 int 16h
; keyboard services
115 ; vsync_wait() - wait for display to enter the next VBlank cycle --------------
121 mov dx, 3dah
; input status #1 register
125 test al, 08h ; vertical retrace bit
135 ; xs() - xorshift pseudorandom number generator -------------------------------
138 ; [xss] = updated state
147 shl dx, 1 ; dx = xss << 1
148 xor bx, dx ; bx = xss ^ (xss << 1)
151 shr dx, 3 ; dx = xss' >> 3
152 xor bx, dx ; bx = xss' ^ (xss' >> 3)
155 shl dx, 10 ; dx = xss'' << 10
156 xor bx, dx ; bx = xss'' ^ (xss'' << 10)
161 ; init_grid() - initialize grid cells randomly --------------------------------
163 ; clobbers ax, bx, cx, dx, di
167 mov ah, PRINT_COLOR
; ah = color attribute
168 mov di, [currvgapg
] ; es:di -> grid start
169 mov cx, COLS
* ROWS
; for each cell
172 call xs
; [xss] = rand
173 mov al, DEAD
; al = DEAD (likely)
175 test word [xss
], 0b11
177 jnz .nz
; if ([xss] % 4 == 0)
178 mov al, ALIVE
; al = ALIVE
181 stosw ; [es:di] = ax ; di += 2
183 loop .write_cell
; } while (--cx)
187 ; flip_vga_page() - flip active display page ----------------------------------
195 xor word [currvgapg
], VGAPGSZ
; flip currvgapg
196 setnz al ; al = !(currvgapg == 0)
198 mov ah, 05h ; select active display page
199 int 10h
; video services
203 ; alive_neighbours() ----------------------------------------------------------
206 ; es:si -> current grid
210 ; ax = # alive neighbours
212 ; clobbers ax, bx, dx
219 ; [bp - 3]: neighbours
223 mov ax, cx ; ax = idx
224 mov bl, COLS
; bl = COLS
225 div bl ; al = idx / COLS ; ah = idx % COLS
227 mov [bp - 1], al ; row = idx / COLS
228 mov [bp - 2], ah ; row = idx % COLS
229 mov byte [bp - 3], 0 ; neighbours = 0
231 mov byte [bp - 4], -1 ; i = -1
234 mov byte [bp - 5], -1 ; j = -1
239 jz .continue
; if (!i && !j) continue
242 add al, [bp - 4] ; al = row + i
245 jl .continue
; if (row + i < 0) continue
248 jge .continue
; if (row + i >= ROWS) continue
251 add ah, [bp - 5] ; ah = col + j
254 jl .continue
; if (col + j < 0) continue
257 jge .continue
; if (col + j >= COLS) continue
259 movzx bx, ah ; bx = col + j
262 mul ah ; ax = (row + i) * COLS
264 add bx, ax ; bx = cell index
265 shl bx, 1 ; bx = grid offset
267 mov dx, [es:si + bx] ; dl = cell state
269 cmp dl, ALIVE
; if (!ALIVE) continue
272 inc byte [bp - 3] ; neighbours++
277 jle .j
; } while (++j <= 1)
281 jle .i
; } while (++i <= 1)
283 movzx ax, byte [bp - 3] ; ax = # alive neighbours
288 ; write_next_cell_state() -----------------------------------------------------
291 ; es:si -> current grid
296 ; [es:di + cx * 2] = updated cell state
298 ; clobbers ax, bx, dx
300 write_next_cell_state:
302 call alive_neighbours
; ax = # alive neighbours
307 mov dx, [es:si + bx] ; dl = cell current state
309 cmp dl, ALIVE
; if (ALIVE) {
312 cmp ax, 2 ; if (n < 2)
313 jl .dead
; return DEAD
315 cmp ax, 3 ; if (n > 3)
316 jg .dead
; return DEAD
318 jmp .alive
; return ALIVE
322 cmp ax, 3 ; if (n != 3)
323 jne .dead
; return DEAD
325 .alive: ; return ALIVE
337 ; write_next_vga_page() -------------------------------------------------------
339 ; clobbers di, si, ax, bx, cx, dx
343 mov si, [currvgapg
] ; es:si -> current page
346 xor di, si ; es:di -> next page
351 call write_next_cell_state
355 jl .update_loop
; for i in [0 .. COLS * ROWS - 1]
359 ; halt() - stop program execution ---------------------------------------------
363 cli ; disable interrupts
367 ; =============================================================================
369 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
370 dw 0aa55h ; mbr magic byte