]>
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 + 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, 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
], 0
62 ; start simulation from random state ------------------------------------------
68 ; apply game of life's rules to determine the next state ----------------------
72 call write_next_vga_page
78 ; =============================================================================
80 ; functions -------------------------------------------------------------------
82 ; delay() - suspend program execution temporarily -----------------------------
88 mov cx, WAIT_DELAY
; cx:dx = interval in microseconds
96 ; wait_keypress() - wait for any keypress -------------------------------------
102 mov ah, 0x00 ; read key press
103 int 0x16 ; keyboard services
107 ; vsync_wait() - wait for display to enter the next VBlank cycle --------------
113 mov dx, 0x3da ; input status #1 register
117 test al, 0x08 ; vertical retrace bit
127 ; xs() - xorshift pseudorandom number generator -------------------------------
130 ; [xss] = updated state
139 shl dx, 1 ; dx = xss << 1
140 xor bx, dx ; bx = xss ^ (xss << 1)
143 shr dx, 3 ; dx = xss' >> 3
144 xor bx, dx ; bx = xss' ^ (xss' >> 3)
147 shl dx, 10 ; dx = xss'' << 10
148 xor bx, dx ; bx = xss'' ^ (xss'' << 10)
153 ; init_grid() - initialize grid cells randomly --------------------------------
155 ; clobbers ax, bx, cx, dx, di
159 mov ah, PRINT_COLOR
; ah = color attribute
160 mov di, [currvgapg
] ; es:di -> grid start
161 mov cx, COLS
* ROWS
; for each cell
164 call xs
; [xss] = rand
165 mov al, DEAD
; al = DEAD (likely)
167 test word [xss
], 0b11
169 jnz .nz
; if ([xss] % 4 == 0)
170 mov al, ALIVE
; al = ALIVE
173 stosw ; [es:di] = ax ; di += 2
175 loop .write_cell
; } while (--cx)
179 ; flip_vga_page() - flip active display page ----------------------------------
187 xor word [currvgapg
], VGAPGSZ
; flip currvgapg
188 setnz al ; al = !(currvgapg == 0)
190 mov di, [currvgapg
] ; es:di -> current page
202 mov ah, 0x05 ; select active display page
203 int 0x10 ; video services
207 ; alive_neighbours() ----------------------------------------------------------
210 ; es:si -> current grid
214 ; ax = # alive neighbours
216 ; clobbers ax, bx, dx
223 ; [bp - 3]: neighbours
227 mov ax, cx ; ax = idx
228 mov bl, COLS
; bl = COLS
229 div bl ; al = idx / COLS ; ah = idx % COLS
231 mov [bp - 1], al ; row = idx / COLS
232 mov [bp - 2], ah ; row = idx % COLS
233 mov byte [bp - 3], 0 ; neighbours = 0
235 mov byte [bp - 4], -1 ; i = -1
238 mov byte [bp - 5], -1 ; j = -1
243 jz .continue
; if (!i && !j) continue
246 add al, [bp - 4] ; al = row + i
249 jl .continue
; if (row + i < 0) continue
252 jge .continue
; if (row + i >= ROWS) continue
255 add ah, [bp - 5] ; ah = col + j
258 jl .continue
; if (col + j < 0) continue
261 jge .continue
; if (col + j >= COLS) continue
263 movzx bx, ah ; bx = col + j
266 mul ah ; ax = (row + i) * COLS
268 add bx, ax ; bx = cell index
269 shl bx, 1 ; bx = grid offset
271 mov dx, [es:si + bx] ; dl = cell state
273 cmp dl, ALIVE
; if (!ALIVE) continue
276 inc byte [bp - 3] ; neighbours++
281 jle .j
; } while (++j <= 1)
285 jle .i
; } while (++i <= 1)
287 movzx ax, byte [bp - 3] ; ax = # alive neighbours
292 ; write_next_cell_state() -----------------------------------------------------
295 ; es:si -> current grid
300 ; [es:di + cx * 2] = updated cell state
302 ; clobbers ax, bx, dx
304 write_next_cell_state:
306 call alive_neighbours
; ax = # alive neighbours
311 mov dx, [es:si + bx] ; dl = cell current state
313 cmp dl, ALIVE
; if (ALIVE) {
316 cmp ax, 2 ; if (n < 2)
317 jl .dead
; return DEAD
319 cmp ax, 3 ; if (n > 3)
320 jg .dead
; return DEAD
322 jmp .alive
; return ALIVE
326 cmp ax, 3 ; if (n != 3)
327 jne .dead
; return DEAD
329 .alive: ; return ALIVE
341 ; write_next_vga_page() -------------------------------------------------------
343 ; clobbers di, si, ax, bx, cx, dx
347 mov si, [currvgapg
] ; es:si -> current page
350 xor di, si ; es:di -> next page
355 call write_next_cell_state
359 jl .update_loop
; for i in [0 .. COLS * ROWS - 1]
363 ; halt() - stop program execution ---------------------------------------------
367 cli ; disable interrupts
371 ; =============================================================================
373 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
374 dw 0xaa55 ; mbr magic byte