]>
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
202 ; write_next_cell_state() -----------------------------------------------------
205 ; es:si -> current grid
210 ; [es:di + cx * 2] = updated cell state
214 write_next_cell_state:
216 call alive_neighbours
; ax = # alive neighbours
221 mov dx, [es:si + bx] ; dl = cell current state
223 test dl, ALIVE
; if (ALIVE) {
226 test ax, 2 ; if (n < 2)
227 jl .dead
; return DEAD
229 test ax, 3 ; if (n > 3)
230 jg .dead
; return DEAD
232 jmp .alive
; return ALIVE
236 test ax, 3 ; if (n != 3)
237 jne .dead
; return DEAD
239 .alive: ; return ALIVE
251 ; write_next_vga_page() -------------------------------------------------------
253 ; clobbers di, si, cx
257 mov si, [currvgapg
] ; es:si -> current page
260 xor di, si ; es:di -> next page
265 call write_next_cell_state
269 jl .update_loop
; for i in [0 .. COLS * ROWS - 1]
273 ; halt() - stop program execution ---------------------------------------------
277 cli ; disable interrupts
281 ; =============================================================================
283 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
284 dw 0xaa55 ; mbr magic byte