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

wirehaze git hosting

fix initialize grid
[lifeboot.git] / lifeboot.asm
1 bits 16
2 org 0x7c00
3
4 %define ORG 0x7c00
5 %define VGA 0xb8000
6
7 %define COLS 80
8 %define ROWS 25
9
10 %define DEAD ' ' ; char to represent a dead cell
11 %define ALIVE '#' ; char to represent an alive cell
12 %define PRINT_COLOR 0x07 ; grey on black
13
14 %define GRID ORG + 512 ; current cell grid (80 * 25 bytes)
15 %define NEXT_GRID GRID + COLS * ROWS ; next cell grid (80 * 25 bytes)
16 %define XSS NEXT_GRID + COLS * ROWS ; xs() state
17
18 ; entry point -----------------------------------------------------------------
19
20 ; initialize segment registers
21 xor ax, ax
22 mov ds, ax
23 mov es, ax
24 mov ss, ax
25
26 ; set stack pointers
27 mov sp, ORG
28 mov bp, ORG
29
30 ; clear direction flag
31 cld
32
33 ; clear screen ----------------------------------------------------------------
34
35 ; disable cursor
36 mov ch, 0x3f
37 mov ah, 0x01
38 int 0x10
39
40 ; clear video memory
41 mov cx, COLS * ROWS
42 mov ax, VGA >> 4
43 mov es, ax
44 xor di, di
45 mov ax, (PRINT_COLOR << 8) | DEAD
46
47 rep stosw ; fill cx words at es:di with ax
48
49 ; setup -----------------------------------------------------------------------
50
51 setup:
52
53 ; initialize xss
54 mov ah, 0x00 ; get
55 int 0x1a ; system time
56 mov [XSS], dx ; cx:dx = number of clock ticks since midnight
57
58 ; initialize grid
59 mov cx, COLS * ROWS ; for each cell
60 xor ax, ax
61 mov es, ax
62 mov di, GRID ; es:di -> GRID
63
64 .initgrid: ; do {
65 call xs ; [XSS] = rand
66 mov al, DEAD ; al = DEAD (likely)
67
68 test [XSS], 0b11
69
70 jnz .initgrid_nz ; if ([XSS] % 4 == 0)
71 mov al, ALIVE ; al = ALIVE
72
73 .initgrid_nz:
74 stosb ; [es:(di++)] = al
75
76 loop .initgrid ; } while (--cx)
77
78 jmp halt
79
80 ; functions -------------------------------------------------------------------
81
82 ; xs() - xorshift pseudorandom number generator -------------------------------
83
84 ; output:
85 ; [XSS] = updated state
86
87 ; clobbers bx, dx
88
89 xs:
90
91 mov bx, [XSS]
92 mov dx, bx
93
94 shl dx, 1 ; dx = xss << 1
95 xor bx, dx ; bx = xss ^ (xss << 1)
96 mov dx, bx
97
98 shr dx, 3 ; dx = xss' >> 3
99 xor bx, dx ; bx = xss' ^ (xss' >> 3)
100 mov dx, bx
101
102 shl dx, 10 ; dx = xss'' << 10
103 xor bx, dx ; bx = xss'' ^ (xss'' << 10)
104 mov [XSS], bx
105
106 ret
107
108 ; errors ----------------------------------------------------------------------
109
110 hello:
111 mov si, str.hello
112
113 ; print the error message string in si and halt
114 ; note: we assume es = VGA_SEG and ds = 0
115 printerr:
116 xor di, di
117 mov ah, PRINT_COLOR
118
119 ; es:di = video memory
120 ; ds:si = error message
121 ; al = current char
122 ; ah = color attribute
123
124 write_char:
125 lodsb ; al = [ds:si], si += 1
126 or al, al ; on null terminator,
127 jz halt ; halt
128 stosw ; [es:di] = ax, di += 2
129 jmp write_char
130
131 halt:
132 cli ; disable interrupts
133 hlt
134 jmp halt
135
136 ; data ------------------------------------------------------------------------
137
138 str:
139 .hello:
140 db "lifeboot", 0
141
142 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
143 dw 0xaa55 ; mbr magic byte