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

wirehaze git hosting

lifeboot.asm: add xorshift
[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 INIT_RATIO 4 ; dead / alive
13 %define PRINT_COLOR 0x07 ; grey on black
14
15 %define GRID ORG + 512 ; current cell grid (80 * 25 bytes)
16 %define NEXT_GRID GRID + COLS * ROWS ; next cell grid (80 * 25 bytes)
17 %define XSS NEXT_GRID + COLS * ROWS ; xs() state
18
19 ; entry point -----------------------------------------------------------------
20
21 ; initialize segment registers
22 xor ax, ax
23 mov ds, ax
24 mov es, ax
25 mov ss, ax
26
27 ; set stack pointers
28 mov sp, ORG
29 mov bp, ORG
30
31 ; clear screen ----------------------------------------------------------------
32
33 ; disable cursor
34 mov ch, 0x3f
35 mov ah, 0x01
36 int 0x10
37
38 ; clear video memory
39 mov cx, COLS * ROWS
40 mov ax, VGA >> 4
41 mov es, ax
42 xor di, di
43 mov ax, (PRINT_COLOR << 8) | DEAD
44
45 rep stosw ; fill cx words at es:di with ax
46
47 ; setup -----------------------------------------------------------------------
48
49 setup:
50
51 ; initialize xss
52 mov ah, 0x00 ; get
53 int 0x1a ; system time
54 mov [XSS], dx ; cx:dx = number of clock ticks since midnight
55
56 ; functions -------------------------------------------------------------------
57
58 ; xs() - xorshift pseudorandom number generator -------------------------------
59
60 ; output:
61 ; ax = updated state ([XSS])
62
63 xs:
64
65 mov ax, [XSS]
66 mov dx, ax
67
68 shl dx, 1 ; dx = xss << 1
69 xor ax, dx ; ax = xss ^ (xss << 1)
70 mov dx, ax
71
72 shr dx, 3 ; dx = xss' >> 3
73 xor ax, dx ; ax = xss' ^ (xss' >> 3)
74 mov dx, ax
75
76 shl dx, 10 ; dx = xss'' << 10
77 xor ax, dx ; ax = xss'' ^ (xss'' << 10)
78 mov [XSS], ax
79
80 ret
81
82 ; errors ----------------------------------------------------------------------
83
84 hello:
85 mov si, str.hello
86
87 ; print the error message string in si and halt
88 ; note: we assume es = VGA_SEG and ds = 0
89 printerr:
90 xor di, di
91 mov ah, PRINT_COLOR
92
93 ; es:di = video memory
94 ; ds:si = error message
95 ; al = current char
96 ; ah = color attribute
97
98 write_char:
99 lodsb ; al = [ds:si], si += 1
100 or al, al ; on null terminator,
101 jz halt ; halt
102 stosw ; [es:di] = ax, di += 2
103 jmp write_char
104
105 halt:
106 cli ; disable interrupts
107 hlt
108 jmp halt
109
110 ; data ------------------------------------------------------------------------
111
112 str:
113 .hello:
114 db "lifeboot", 0
115
116 times 510 - ($ - $$) db 0 ; fill remaining bytes with zeroes
117 dw 0xaa55 ; mbr magic byte