5c63dfef38b27f0a33a9f238761686725e4e3a81
[bootcard] / bootcard.asm
1 ; ---- boot me! ----
2 ; nasm -f bin -o bootcard.img bootcard.asm
3 ; cat bootcard.img >/dev/<usbstick>
4 ; reboot
5
6         org 7c00h
7         bits 16
8
9 data_start      equ 7e00h
10 nticks          equ data_start
11 tmoffs          equ nticks + 4
12 muscur          equ tmoffs + 4
13 data_end        equ muscur + 4
14
15 backbuf_seg     equ 1000h
16
17 OSC_FREQ        equ 1193182
18 PIT_DATA0       equ 40h
19 PIT_CMD         equ 43h
20 PIT_CMD_CHAN0   equ 00h
21 PIT_CMD_HILO    equ 30h
22 PIT_CMD_SQWAVE  equ 06h
23 KB_CTRL         equ 61h
24
25 %define DIV_ROUND(a, b) ((a) / (b) + ((a) % (b)) / ((b) / 2))
26
27 %macro setcursor 2
28         mov dl, %1
29         mov dh, %2
30         xor bx, bx
31         mov ah, 2
32         int 10h
33 %endmacro
34 %macro spkon 0
35         in al, KB_CTRL
36         or al, 3
37         out KB_CTRL, al
38 %endmacro
39 %macro spkoff 0
40         in al, KB_CTRL
41         and al, 0fch
42         out KB_CTRL, al
43 %endmacro
44 %macro settimer 2
45         mov al, (PIT_CMD_CHAN0 + (%1 << 6)) | PIT_CMD_HILO | PIT_CMD_SQWAVE
46         out PIT_CMD, al
47         mov ax, %2
48         out PIT_DATA0 + %1, al
49         mov al, ah
50         out PIT_DATA0 + %1, al
51 %endmacro
52
53 start:  xor eax, eax
54         mov ds, ax
55         mov es, ax
56         mov ss, ax
57         mov sp, 7c00h
58
59         mov di, data_start
60         mov cx, (data_end - data_start) / 2
61         rep stosw
62
63         mov word [32], timer_intr
64         mov [34], ax
65
66         settimer 0, DIV_ROUND(OSC_FREQ, 200)
67
68         mov ax, 13h
69         int 10h
70         push backbuf_seg
71         pop es
72
73         ; setup palette
74         mov cx, 128
75         mov dx, 3c8h
76         mov al, 16
77         out dx, al
78         inc dx
79 .cmapsetup:
80         mov al, cl
81         shr al, 2
82         out dx, al
83         neg al
84         and al, 03fh
85         shr al, 1
86         mov ah, al
87         shr al, 2
88         out dx, al
89         mov al, ah
90         out dx, al
91         dec cx
92         jnz .cmapsetup
93         sti
94
95 mainloop:
96         call drawbg
97
98         mov dx, 3dah
99 .invb:  in al, dx
100         and al, 8
101         jnz mainloop
102 .novb:  in al, dx
103         and al, 8
104         jz .novb
105
106         push ds
107         push es
108         push es
109         pop ds
110         push 0a000h
111         pop es
112         xor di, di
113         xor si, si
114         mov cx, 32000
115         rep movsw
116         pop es
117         pop ds
118
119         setcursor 10, 12
120         mov si, str1
121         call textout
122         setcursor 12, 13
123         mov si, str2
124         call textout
125
126         jmp mainloop
127
128 drawbg:
129         mov bx, 200
130         xor di, di
131 .fillgrad:
132         mov ax, bx
133         add ax, 16
134         mov ah, al
135         mov cx, 320
136         rep stosw
137         dec bx
138         jnz .fillgrad
139         ret
140
141
142 textout:
143         mov al, [si]
144         and al, al
145         jz .done
146         mov ah, 0eh
147         mov bx, 0fh
148         int 10h
149         inc si
150         jmp textout
151 .done:  ret
152
153 timer_intr:
154         pusha
155         mov ax, [nticks]
156         inc ax
157         mov [nticks], ax
158
159         sub ax, [tmoffs]
160 .pmus:  mov bx, [muscur]
161         shl bx, 2
162         mov cx, [music + bx]    ; event time
163         cmp cx, 0ffffh
164         jz .loop
165         cmp ax, cx
166         jb .eoi
167
168         inc dword [muscur]
169         mov ax, [music + 2 + bx] ; event counter reload
170         test ax, ax
171         jz .off
172         mov bx, ax
173         settimer 2, bx
174         spkon
175         jmp .eoi
176
177 .off:   spkoff
178
179 .eoi:   mov al, 20h
180         out 20h, al     ; EOI
181         popa
182         iret
183
184 .loop:  neg cx
185         mov [muscur], cx
186         mov ax, [nticks]
187         mov [tmoffs], ax
188         jmp .pmus
189         
190
191 str1:   db 'message message blah',0
192 str2:   db 'Michael & Athina',0
193
194 G2      equ 24351/2
195 C3      equ 18243/2
196 D3      equ 16252/2
197 B2      equ 19328/2
198 F3      equ 13666/2
199 E3      equ 14479/2
200
201 %define TM(x)   (40 + (x) * 4)
202
203 music:  dw 0, 0
204         dw TM(0),       G2
205         dw TM(40),      C3
206         dw TM(70),      C3
207
208         dw TM(80),      C3
209         dw TM(140),     0
210
211         dw TM(160),     G2
212         dw TM(200),     D3
213         dw TM(230),     B2
214
215         dw TM(240),     C3
216         dw TM(300),     0
217
218         dw TM(320),     G2
219         dw TM(360),     C3
220         dw TM(390),     F3
221
222         dw TM(400),     F3
223         dw TM(440),     E3
224         dw TM(470),     D3
225
226         dw TM(480),     C3
227         dw TM(520),     B2
228         dw TM(550),     C3
229
230         dw TM(560),     D3
231         dw TM(640),     0
232
233         dw TM(680),     0
234         dw 0ffffh, 0
235
236         times 446-($-$$) db 0
237         dd 00212080h
238         dd 0820280ch
239         dd 00000800h
240         dd 0001f800h
241         times 510-($-$$) db 0
242         dw 0aa55h
243
244 ; vi:ft=nasm ts=8 sts=8 sw=8: