]> wirehaze git hosting - lifeboot.git/blob - lifeboot.asm

wirehaze git hosting

6d95c7cfee143213269e43a3da317f1abff75698
[lifeboot.git] / lifeboot.asm
1 bits 16
2 org 0x7c00
3
4 ; =============================================================================
5
6 ; settings --------------------------------------------------------------------
7
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
12
13 ; constants -------------------------------------------------------------------
14
15 %define COLS 80
16 %define ROWS 25
17 %define VGAPGSZ (COLS * ROWS * 2)
18
19 ; memory layout ---------------------------------------------------------------
20
21 %define ORG 0x7c00
22 %define VGA 0xb8000
23 %define DAT ORG + 512
24
25 ; static variables ------------------------------------------------------------
26
27 %define xss DAT ; xs() state (word)
28 %define currvgapg DAT + 2 ; current vga page near pointer (word)
29
30 ; =============================================================================
31
32 ; entry point -----------------------------------------------------------------
33
34 ; initialize segment registers
35 mov ax, VGA >> 4
36 mov es, ax
37
38 xor ax, ax
39 mov ds, ax
40 mov ss, ax
41
42 ; set stack pointers
43 mov bp, ORG
44 mov sp, bp
45
46 ; clear direction flag
47 cld
48
49 ; disable cursor
50 mov ch, 0x3f ; cursor start and options
51 mov ah, 0x01 ; set text-mode cursor shape
52 int 0x10 ; video services
53
54 ; initialize xss
55 mov ah, 0x00 ; get
56 int 0x1a ; system time
57 mov [xss], dx ; cx:dx = number of clock ticks since midnight
58
59 ; initialize currvgapg
60 mov word [currvgapg], VGAPGSZ
61
62 ; start simulation from random state ------------------------------------------
63
64 start:
65
66 call init_grid
67
68 ; apply game of life's rules to determine the next state ----------------------
69
70 next_state:
71
72 call flip_vga_page ; display current state
73 call delay ; sleep a little
74
75 call write_next_vga_page ; write the next state to the hidden page
76
77 jmp next_state
78
79 ; =============================================================================
80
81 ; functions -------------------------------------------------------------------
82
83 ; delay() - suspend program execution temporarily -----------------------------
84
85 ; clobbers ah, cx, dx
86
87 delay:
88
89 mov cx, WAIT_DELAY ; cx:dx = interval in microseconds
90 mov dx, 0
91
92 mov ah, 0x86
93 int 0x15 ; wait
94
95 ret
96
97 ; vsync_wait() - wait for display to enter the next VBlank cycle --------------
98
99 ; clobbers ax, dx
100
101 vsync_wait:
102
103 mov dx, 0x3da ; input status #1 register
104
105 .wait_on:
106 in al, dx
107 test al, 0x08 ; vertical retrace bit
108 jnz .wait_on
109
110 .wait_off:
111 in al, dx
112 test al, 0x08
113 jz .wait_off
114
115 ret
116
117 ; xs() - xorshift pseudorandom number generator -------------------------------
118
119 ; output:
120 ; [xss] = updated state
121
122 ; clobbers bx, dx
123
124 xs:
125
126 mov bx, [xss]
127 mov dx, bx
128
129 shl dx, 1 ; dx = xss << 1
130 xor bx, dx ; bx = xss ^ (xss << 1)
131 mov dx, bx
132
133 shr dx, 3 ; dx = xss' >> 3
134 xor bx, dx ; bx = xss' ^ (xss' >> 3)
135 mov dx, bx
136
137 shl dx, 10 ; dx = xss'' << 10
138 xor bx, dx ; bx = xss'' ^ (xss'' << 10)
139 mov [xss], bx
140
141 ret
142
143 ; init_grid() - initialize grid cells randomly --------------------------------
144
145 ; clobbers ax, bx, cx, dx, di
146
147 init_grid:
148
149 mov ah, PRINT_COLOR ; ah = color attribute
150 mov di, [currvgapg] ; es:di -> grid start
151 mov cx, COLS * ROWS ; for each cell
152
153 .write_cell: ; do {
154 call xs ; [xss] = rand
155 mov al, DEAD ; al = DEAD (likely)
156
157 test word [xss], 0b11
158
159 jnz .nz ; if ([xss] % 4 == 0)
160 mov al, ALIVE ; al = ALIVE
161
162 .nz:
163 stosw ; [es:di] = ax ; di += 2
164
165 loop .write_cell ; } while (--cx)
166
167 ret
168
169 ; flip_vga_page() - flip active display page ----------------------------------
170
171 ; clobbers ax, dx
172
173 flip_vga_page:
174
175 call vsync_wait
176
177 xor word [currvgapg], VGAPGSZ ; flip currvgapg
178 setnz al ; al = !(currvgapg == 0)
179
180 mov ah, 0x05 ; select active display page
181 int 0x10 ; video services
182
183 ret
184
185 ; alive_neighbours() ----------------------------------------------------------
186
187 ; input:
188 ; es:si -> current grid
189 ; cx -> cell index
190
191 ; output:
192 ; ax = # alive neighbours
193
194 ; clobbers ?
195
196 alive_neighbours:
197
198 ; todo
199
200 ret
201
202 ; write_next_cell_state() -----------------------------------------------------
203
204 ; input:
205 ; es:si -> current grid
206 ; es:di -> next grid
207 ; cx -> cell index
208
209 ; output:
210 ; [es:di + cx * 2] = updated cell state
211
212 ; clobbers ?
213
214 write_next_cell_state:
215
216 call alive_neighbours ; ax = # alive neighbours
217
218 mov bx, cx
219 shl bx, 1
220
221 mov dx, [es:si + bx] ; dl = cell current state
222
223 test dl, ALIVE ; if (ALIVE) {
224 jne 1f
225
226 test ax, 2 ; if (n < 2)
227 jl .dead ; return DEAD
228
229 test ax, 3 ; if (n > 3)
230 jg .dead ; return DEAD
231
232 jmp .alive ; return ALIVE
233
234 1: ; }
235
236 test ax, 3 ; if (n != 3)
237 jne .dead ; return DEAD
238
239 .alive: ; return ALIVE
240 mov dl, ALIVE
241 jmp .write
242
243 .dead:
244 mov dl, DEAD
245
246 .write:
247 mov [es:di + bx], dx
248
249 ret
250
251 ; write_next_vga_page() -------------------------------------------------------
252
253 ; clobbers di, si, cx
254
255 write_next_vga_page:
256
257 mov si, [currvgapg] ; es:si -> current page
258
259 mov di, VGAPGSZ
260 xor di, si ; es:di -> next page
261
262 xor cx, cx ; i = 0
263
264 .update_loop:
265 call write_next_cell_state
266
267 inc cx
268 test cx, COLS * ROWS
269 jl .update_loop ; for i in [0 .. COLS * ROWS - 1]
270
271 ret
272
273 ; halt() - stop program execution ---------------------------------------------
274
275 halt:
276
277 cli ; disable interrupts
278 hlt
279 jmp halt
280
281 ; =============================================================================
282
283 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
284 dw 0xaa55 ; mbr magic byte