01090cd69c3fb15e81052074a423d24f5a4a092d
[bootcensus] / src / startup.s
1 # pcboot - bootable PC demo/game kernel
2 # Copyright (C) 2018-2019  John Tsiombikas <nuclear@member.fsf.org>
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY, without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17         .code32
18         .section .startup,"ax"
19
20         .extern _bss_start
21         .extern _bss_end
22         .extern pcboot_main
23         .extern wait_vsync
24         .extern kb_getkey
25
26         .equ STACKTOP,0x80000
27
28         # move the stack to the top of the conventional memory
29         cli
30         movl $STACKTOP, %esp
31         # push a 0 ret-addr to terminate gdb backtraces
32         pushl $0
33
34         # zero the BSS section
35         xor %eax, %eax
36         mov $_bss_start, %edi
37         mov $_bss_size, %ecx
38         cmp $0, %ecx
39         jz skip_bss_zero
40         shr $4, %ecx
41         rep stosl
42 skip_bss_zero:
43
44         call pcboot_main
45         # pcboot_main never returns
46 0:      cli
47         hlt
48         jmp 0b
49
50         # this is called once after memory init, to move the protected mode
51         # stack to the top of usable memory, to avoid interference from 16bit
52         # programs (as much as possible)
53         .global move_stack
54 move_stack:
55         # calculate the currently used lowest address of the stack (rounded
56         # down to 4-byte alignment), to see where to start copying
57         mov %esp, %esi
58         and $0xfffffffc, %esi
59         # calculate the size we need to copy
60         mov $STACKTOP, %ecx
61         sub %esi, %ecx
62         # load the destination address to edi
63         mov 4(%esp), %edi
64         sub %ecx, %edi
65         # size in longwords
66         shr $2, %ecx
67
68         # change esp to the new stack
69         mov $STACKTOP, %ecx
70         sub %esp, %ecx
71         mov 4(%esp), %eax
72         mov %eax, %esp
73         sub %ecx, %esp
74
75         rep movsd
76         ret
77
78         .global logohack
79 logohack:
80         pusha
81
82         # copy palette
83         mov $logo_pal, %esi
84         xor %cl, %cl
85
86 0:      xor %eax, %eax
87         mov $0x3c8, %dx
88         movb %cl, %al
89         outb %al, %dx
90         inc %dx
91         # red
92         movb (%esi), %al
93         inc %esi
94         shr $2, %al
95         outb %al, %dx
96         # green
97         movb (%esi), %al
98         inc %esi
99         shr $2, %al
100         outb %al, %dx
101         # blue
102         movb (%esi), %al
103         inc %esi
104         shr $2, %al
105         outb %al, %dx
106         add $1, %cl
107         jnc 0b
108
109         # copy pixels
110         mov $sintab, %ebp
111         mov $logo_pix, %esi
112 frameloop:
113         mov $0xa0000, %edi
114         movl $0, yval
115 yloop:
116         movl $0, xval
117 xloop:
118         # calc src scanline address -> ebx
119         xor %ecx, %ecx
120         mov yval, %ebx
121         shl $2, %ebx
122         add frameno, %ebx
123         and $0xff, %ebx
124         mov (%ebp, %ebx), %cl
125         shr $5, %ecx
126
127         mov yval, %eax
128         add %ecx, %eax
129         # bounds check
130         cmp $200, %eax
131         jl 0f
132         mov $199, %eax
133
134 0:      mov %eax, %ebx
135         shl $8, %eax
136         shl $6, %ebx
137         add %eax, %ebx
138
139         # calc src x offset -> eax
140         xor %ecx, %ecx
141         mov xval, %eax
142         shl $2, %eax
143         add frameno, %eax
144         and $0xff, %eax
145         mov (%ebp, %eax), %cl
146         shr $5, %ecx
147
148         mov xval, %eax
149         add %ecx, %eax
150         # bounds check
151         cmp $320, %eax
152         jl 0f
153         mov $319, %eax
154
155 0:      add %eax, %ebx
156         mov (%ebx, %esi), %al
157
158         mov %al, (%edi)
159         inc %edi
160
161         incl xval
162         cmpl $320, xval
163         jnz xloop
164
165         incl yval
166         cmpl $200, yval
167         jnz yloop
168
169         incl frameno
170
171         call wait_vsync
172
173         # check for escape keypress
174         call kb_getkey
175         cmp $27, %eax
176         jz 0f
177
178         jmp frameloop
179
180 0:      popa
181         ret
182
183 xval: .long 0
184 yval: .long 0
185 frameno: .long 0
186
187 logo_pal:
188         .incbin "logo.pal"
189
190         .align 16
191 logo_pix:
192         .incbin "logo.raw"
193
194 sintab:
195         .byte 127
196         .byte 130
197         .byte 133
198         .byte 136
199         .byte 139
200         .byte 143
201         .byte 146
202         .byte 149
203         .byte 152
204         .byte 155
205         .byte 158
206         .byte 161
207         .byte 164
208         .byte 167
209         .byte 170
210         .byte 173
211         .byte 176
212         .byte 179
213         .byte 182
214         .byte 184
215         .byte 187
216         .byte 190
217         .byte 193
218         .byte 195
219         .byte 198
220         .byte 200
221         .byte 203
222         .byte 205
223         .byte 208
224         .byte 210
225         .byte 213
226         .byte 215
227         .byte 217
228         .byte 219
229         .byte 221
230         .byte 224
231         .byte 226
232         .byte 228
233         .byte 229
234         .byte 231
235         .byte 233
236         .byte 235
237         .byte 236
238         .byte 238
239         .byte 239
240         .byte 241
241         .byte 242
242         .byte 244
243         .byte 245
244         .byte 246
245         .byte 247
246         .byte 248
247         .byte 249
248         .byte 250
249         .byte 251
250         .byte 251
251         .byte 252
252         .byte 253
253         .byte 253
254         .byte 254
255         .byte 254
256         .byte 254
257         .byte 254
258         .byte 254
259         .byte 255
260         .byte 254
261         .byte 254
262         .byte 254
263         .byte 254
264         .byte 254
265         .byte 253
266         .byte 253
267         .byte 252
268         .byte 251
269         .byte 251
270         .byte 250
271         .byte 249
272         .byte 248
273         .byte 247
274         .byte 246
275         .byte 245
276         .byte 244
277         .byte 242
278         .byte 241
279         .byte 239
280         .byte 238
281         .byte 236
282         .byte 235
283         .byte 233
284         .byte 231
285         .byte 229
286         .byte 228
287         .byte 226
288         .byte 224
289         .byte 221
290         .byte 219
291         .byte 217
292         .byte 215
293         .byte 213
294         .byte 210
295         .byte 208
296         .byte 205
297         .byte 203
298         .byte 200
299         .byte 198
300         .byte 195
301         .byte 193
302         .byte 190
303         .byte 187
304         .byte 184
305         .byte 182
306         .byte 179
307         .byte 176
308         .byte 173
309         .byte 170
310         .byte 167
311         .byte 164
312         .byte 161
313         .byte 158
314         .byte 155
315         .byte 152
316         .byte 149
317         .byte 146
318         .byte 143
319         .byte 139
320         .byte 136
321         .byte 133
322         .byte 130
323         .byte 127
324         .byte 124
325         .byte 121
326         .byte 118
327         .byte 115
328         .byte 111
329         .byte 108
330         .byte 105
331         .byte 102
332         .byte 99
333         .byte 96
334         .byte 93
335         .byte 90
336         .byte 87
337         .byte 84
338         .byte 81
339         .byte 78
340         .byte 75
341         .byte 72
342         .byte 70
343         .byte 67
344         .byte 64
345         .byte 61
346         .byte 59
347         .byte 56
348         .byte 54
349         .byte 51
350         .byte 49
351         .byte 46
352         .byte 44
353         .byte 41
354         .byte 39
355         .byte 37
356         .byte 35
357         .byte 33
358         .byte 30
359         .byte 28
360         .byte 26
361         .byte 25
362         .byte 23
363         .byte 21
364         .byte 19
365         .byte 18
366         .byte 16
367         .byte 15
368         .byte 13
369         .byte 12
370         .byte 10
371         .byte 9
372         .byte 8
373         .byte 7
374         .byte 6
375         .byte 5
376         .byte 4
377         .byte 3
378         .byte 3
379         .byte 2
380         .byte 1
381         .byte 1
382         .byte 0
383         .byte 0
384         .byte 0
385         .byte 0
386         .byte 0
387         .byte 0
388         .byte 0
389         .byte 0
390         .byte 0
391         .byte 0
392         .byte 0
393         .byte 1
394         .byte 1
395         .byte 2
396         .byte 3
397         .byte 3
398         .byte 4
399         .byte 5
400         .byte 6
401         .byte 7
402         .byte 8
403         .byte 9
404         .byte 10
405         .byte 12
406         .byte 13
407         .byte 15
408         .byte 16
409         .byte 18
410         .byte 19
411         .byte 21
412         .byte 23
413         .byte 25
414         .byte 26
415         .byte 28
416         .byte 30
417         .byte 33
418         .byte 35
419         .byte 37
420         .byte 39
421         .byte 41
422         .byte 44
423         .byte 46
424         .byte 49
425         .byte 51
426         .byte 54
427         .byte 56
428         .byte 59
429         .byte 61
430         .byte 64
431         .byte 67
432         .byte 70
433         .byte 72
434         .byte 75
435         .byte 78
436         .byte 81
437         .byte 84
438         .byte 87
439         .byte 90
440         .byte 93
441         .byte 96
442         .byte 99
443         .byte 102
444         .byte 105
445         .byte 108
446         .byte 111
447         .byte 115
448         .byte 118
449         .byte 121
450         .byte 124