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

wirehaze git hosting

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 screen ----------------------------------------------------------------
31
32 ; disable cursor
33 mov ch, 0x3f
34 mov ah, 0x01
35 int 0x10
36
37 ; clear video memory
38 mov cx, COLS * ROWS
39 mov ax, VGA >> 4
40 mov es, ax
41 xor di, di
42 mov ax, (PRINT_COLOR << 8) | DEAD
43
44 rep stosw ; fill cx words at es:di with ax
45
46 ; setup -----------------------------------------------------------------------
47
48 setup:
49
50 ; initialize xss
51 mov ah, 0x00 ; get
52 int 0x1a ; system time
53 mov [XSS], dx ; cx:dx = number of clock ticks since midnight
54
55 ; initialize grid
56 mov di, GRID
57 mov cx, COLS * ROWS
58
59 .initgrid: ; do {
60 call xs ; ax = rand
61 mov dl, DEAD ; dl = DEAD (likely)
62
63 test ax, 0b11
64
65 jnz .initgrid_nz ; if (ax % 4 == 0)
66 mov dl, ALIVE ; dl = ALIVE
67
68 .initgrid_nz:
69 mov [di + cx], dl ; grid[cx] = dl
70
71 loop .initgrid ; } while (--cx)
72
73 jmp halt
74
75 ; functions -------------------------------------------------------------------
76
77 ; xs() - xorshift pseudorandom number generator -------------------------------
78
79 ; output:
80 ; ax = updated state ([XSS])
81
82 ; clobbers ax, dx
83
84 xs:
85
86 mov ax, [XSS]
87 mov dx, ax
88
89 shl dx, 1 ; dx = xss << 1
90 xor ax, dx ; ax = xss ^ (xss << 1)
91 mov dx, ax
92
93 shr dx, 3 ; dx = xss' >> 3
94 xor ax, dx ; ax = xss' ^ (xss' >> 3)
95 mov dx, ax
96
97 shl dx, 10 ; dx = xss'' << 10
98 xor ax, dx ; ax = xss'' ^ (xss'' << 10)
99 mov [XSS], ax
100
101 ret
102
103 ; errors ----------------------------------------------------------------------
104
105 hello:
106 mov si, str.hello
107
108 ; print the error message string in si and halt
109 ; note: we assume es = VGA_SEG and ds = 0
110 printerr:
111 xor di, di
112 mov ah, PRINT_COLOR
113
114 ; es:di = video memory
115 ; ds:si = error message
116 ; al = current char
117 ; ah = color attribute
118
119 write_char:
120 lodsb ; al = [ds:si], si += 1
121 or al, al ; on null terminator,
122 jz halt ; halt
123 stosw ; [es:di] = ax, di += 2
124 jmp write_char
125
126 halt:
127 cli ; disable interrupts
128 hlt
129 jmp halt
130
131 ; data ------------------------------------------------------------------------
132
133 str:
134 .hello:
135 db "lifeboot", 0
136
137 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
138 dw 0xaa55 ; mbr magic byte