]>
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
0x07 ; grey on black
11 %
define WAIT_DELAY
0x02 ; 0.131072 seconds
13 ; constants -------------------------------------------------------------------
17 %
define VGAPGSZ
(COLS
* ROWS
* 2)
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, 0x3f ; cursor start and options
51 mov ah, 0x01 ; set text-mode cursor shape
52 int 0x10 ; video services
56 int 0x1a ; system time
57 mov [xss
], dx ; cx:dx = number of clock ticks since midnight
59 ; initialize currvgapg
60 mov word [currvgapg
], VGAPGSZ
62 ; start simulation from random state ------------------------------------------
68 ; apply game of life's rules to determine the next state ----------------------
72 call flip_vga_page
; display current state
73 call delay
; sleep a little
75 call write_next_vga_page
; write the next state to the hidden page
79 ; =============================================================================
81 ; functions -------------------------------------------------------------------
83 ; delay() - suspend program execution temporarily -----------------------------
89 mov cx, WAIT_DELAY
; cx:dx = interval in microseconds
97 ; vsync_wait() - wait for display to enter the next VBlank cycle --------------
103 mov dx, 0x3da ; input status #1 register
107 test al, 0x08 ; 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 ----------------------------------
177 xor word [currvgapg
], VGAPGSZ
; flip currvgapg
178 setnz al ; al = !(currvgapg == 0)
180 mov ah, 0x05 ; select active display page
181 int 0x10 ; video services
185 ; alive_neighbours() ----------------------------------------------------------
188 ; es:si -> current grid
192 ; ax = # alive neighbours
194 ; clobbers ax, bx, dx
201 ; [bp - 3]: neighbours
205 mov ax, cx ; ax = idx
206 mov bl, COLS
; bl = COLS
207 div bl ; al = idx / COLS ; ah = idx % COLS
209 mov [bp - 1], al ; row = idx / COLS
210 mov [bp - 2], ah ; row = idx % COLS
211 mov byte [bp - 3], 0 ; neighbours = 0
213 mov byte [bp - 4], -1 ; i = -1
216 mov byte [bp - 5], -1 ; j = -1
221 jz .continue
; if (!i && !j) continue
224 add al, [bp - 4] ; al = row + i
227 jl .continue
; if (row + i < 0) continue
230 jge .continue
; if (row + i >= ROWS) continue
233 add ah, [bp - 5] ; ah = col + j
236 jl .continue
; if (col + j < 0) continue
239 jge .continue
; if (col + j >= COLS) continue
241 movzx bx, ah ; bx = col + j
244 mul ah ; ax = (row + i) * COLS
246 add bx, ax ; bx = cell index
247 shl bx, 1 ; bx = grid offset
249 mov dx, [es:si + bx] ; dl = cell state
251 test dl, ALIVE
; if (!ALIVE) continue
254 inc byte [bp - 3] ; neighbours++
259 jle .j
; } while (++j <= 1)
263 jle .i
; } while (++i <= 1)
265 movzx ax, byte [bp - 3] ; ax = # alive neighbours
270 ; write_next_cell_state() -----------------------------------------------------
273 ; es:si -> current grid
278 ; [es:di + cx * 2] = updated cell state
280 ; clobbers ax, bx, dx
282 write_next_cell_state:
284 call alive_neighbours
; ax = # alive neighbours
289 mov dx, [es:si + bx] ; dl = cell current state
291 test dl, ALIVE
; if (ALIVE) {
294 test ax, 2 ; if (n < 2)
295 jl .dead
; return DEAD
297 test ax, 3 ; if (n > 3)
298 jg .dead
; return DEAD
300 jmp .alive
; return ALIVE
304 test ax, 3 ; if (n != 3)
305 jne .dead
; return DEAD
307 .alive: ; return ALIVE
319 ; write_next_vga_page() -------------------------------------------------------
321 ; clobbers di, si, ax, bx, cx, dx
325 mov si, [currvgapg
] ; es:si -> current page
328 xor di, si ; es:di -> next page
333 call write_next_cell_state
337 jl .update_loop
; for i in [0 .. COLS * ROWS - 1]
341 ; halt() - stop program execution ---------------------------------------------
345 cli ; disable interrupts
349 ; =============================================================================
351 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
352 dw 0xaa55 ; mbr magic byte