Blob


1 [cpu 286]
2 [bits 16]
4 %include "macros.inc"
6 section .text
7 extern floppy_init
8 global _entry
9 global putchar
10 global puts
11 global puthexb
12 global puthexw
13 global panic
14 _entry:
15 pop dx
16 mov byte [bootdrv], dl
18 ; clear screen
19 mov bx, 0xb800
20 mov es, bx
21 xor di, di
22 mov ax, (' ' | (0x07 << 8))
23 mov cx, (80 * 25 * 2)
24 rep stosw
26 ; get size of conventional memory (below 640K)
27 int 0x12
28 mov word [convmem], ax
30 ; get size of extended memory (above 1M)
31 mov ah, 0x88
32 int 0x15
33 jc .noxm
34 mov word [highmem], ax
36 .noxm:
37 ; NOTE: BIOS cannot be accessed from here
38 cli
40 ; disable keyboard
41 call .wait
42 mov al, 0xAD
43 out 0x64, al
45 ; read from input
46 call .wait
47 mov al, 0xD0
48 out 0x64, al
49 call .wait2
50 in al, 0x60
51 mov bl, al
53 ; write to output
54 call .wait
55 mov al,0xD1
56 out 0x64,al
57 call .wait
58 mov al, bl
59 or al, 2 ; enable A20 line
60 out 0x60, al
62 ; enable keyboard
63 call .wait
64 mov al, 0xAE
65 out 0x64, al
66 call .wait
67 jmp .setup
69 .wait:
70 in al,0x64
71 test al, 2
72 jnz .wait
73 ret
76 .wait2:
77 in al,0x64
78 test al,1
79 jz .wait2
80 ret
82 .setup:
83 lidt [idtr]
84 lgdt [gdtr]
85 smsw ax
86 or ax, 1
87 lmsw ax
88 jmp 0x08:.reloadcs
90 .reloadcs:
91 lea sp, [stack]
92 mov ax, 0x10
93 mov ds, ax
94 mov es, ax
95 mov ss, ax
97 out 0x80, al
99 ; initialize PIC
100 outb_slow 0x20, 0x11 ; Start the initialization routine (in cascae mode)
101 outb_slow 0xA0, 0x11
102 outb_slow 0x21, 0x20 ; Master PIC IRQ offset
103 outb_slow 0xA1, 0x28 ; Slave PIC IRQ offset
104 outb_slow 0x21, 4 ; Tell the master PIC that there is a slave PIC at IRQ2
105 outb_slow 0xA1, 2 ; Tell the slave PIC it's cascade identity
106 outb_slow 0x21, 1 ; Use the 8086-mode (and not the 8088 mode)
107 outb_slow 0xA1, 1
108 outb_slow 0x21, 0xfe ; Set the IRQ masks
109 outb_slow 0xA1, 0xff
111 ; initialize IDT
112 mov al, 0x20
113 mov ah, 0x86
114 lea dx, [i_timer]
115 call set_irq
117 lea bp, [hello]
118 call puts
120 lea bp, [convmsg]
121 call puts
122 mov ax, word [convmem]
123 call puthexw
124 lea bp, [kbmsg]
125 call puts
127 lea bp, [xmmsg]
128 call puts
129 mov ax, word [highmem]
130 call puthexw
131 lea bp, [kbmsg]
132 call puts
134 call floppy_init
136 sti
137 jmp $
139 i_timer:
140 pusha
141 mov al, '.'
142 call putchar
144 outb 0x20, 0x20
145 popa
146 iret
148 ; al - num
149 ; ah - attr
150 ; dx - offset
151 set_irq:
152 xor bh, bh
153 mov bl, al
154 shl bx, 3
155 lea bx, [idt + bx]
156 mov word [bx + 0], dx
157 mov word [bx + 2], 0x08
158 mov byte [bx + 4], 0x00
159 mov byte [bx + 5], ah
160 mov word [bx + 6], 0
161 ret
163 error:
164 cli
165 lea bp, [errstr]
166 call puts
167 hlt
168 jmp $
170 ; ax - value
171 puthexw:
172 push ax
173 mov al, ah
174 call puthexb
175 pop ax
176 jmp puthexb
178 ; al - value
179 puthexb:
180 push ax
181 shr al, 4
182 call puthexch
183 pop ax
184 and al, 0xf
185 jmp puthexch
187 ; al - hex digit
188 puthexch:
189 cmp al, 10
190 jae .hex
191 add al, '0'
192 jmp putchar
194 .hex:
195 add al, 'a' - 10
196 jmp putchar
198 ; al - char
199 writech:
200 mov bx, 0x18
201 mov es, bx
203 xor ch, ch
204 xor dh, dh
205 mov cl, byte [posx]
206 mov dl, byte [posy]
208 cmp al, 10 ; '\n'
209 je .nl
211 imul bx, dx, 80
212 add bx, cx
213 shl bx, 1
214 mov byte [es:bx], al
216 inc cl
217 cmp cl, 80
218 je .nl
219 mov byte [posx], cl
220 ret
222 .nl:
223 mov byte [posx], 0
224 inc dl
226 cmp dl, 25
227 je .scroll
228 mov byte [posy], dl
229 ret
231 .scroll:
232 push ds
233 mov ax, 0x18
234 mov ds, ax
236 cld
237 xor di, di
238 mov si, 160
239 mov cx, 80 * 24
240 rep movsw
242 pop ds
244 mov di, 80 * 24 * 2
245 mov cx, 80
246 mov ax, (' ' | (0x07 << 8))
247 rep stosw
248 mov byte [posy], 24
249 ret
251 ; al - ch
252 putchar:
253 call writech
254 jmp update_cursor
256 ; bp - str
257 puts:
258 mov al, byte [ds:bp]
259 inc bp
260 test al, al
261 jz update_cursor
263 call writech
264 jmp puts
266 update_cursor:
267 mov dx, 0x3D4
268 mov al, 0x0f
269 out dx, al
271 mov bx, word [posy]
272 imul bx, 80
273 add bx, word [posx]
275 inc dx ; 0x3D5
276 mov al, bl
277 out dx, al
279 dec dx ; 0x3D4
280 mov al, 0x0E
281 out dx, al
283 inc dx ; 0x3D5
284 mov al, bh
285 out dx, al
286 ret
288 ; bp - str
289 panic:
290 call puts
292 .halt:
293 cli
294 hlt
295 jmp .halt
297 section .rodata
298 hello:
299 db "Hello World", 10, 0
300 errstr:
301 db "Error", 10, 0
302 convmsg:
303 db "Conventional memory: 0x", 0
304 xmmsg:
305 db "Extended memory: 0x", 0
306 kbmsg:
307 db " KB", 10, 0
309 align 2
310 idtr:
311 dw idt.end - idt - 1
312 dd idt + 0x10000
314 align 2
315 gdtr:
316 dw gdt.end - gdt - 1
317 dd gdt + 0x10000
320 section .data
321 gdt:
322 dq 0
324 ; 0x08 - kernel code
325 dw 0xffff
326 dw 0x0000
327 db 0x01
328 db 0x9A
329 dw 0x0000
331 ; 0x10 - kernel data
332 dw 0xffff
333 dw 0x0000
334 db 0x01
335 db 0x92
336 dw 0x0000
338 ; 0x18 - video memory
339 dw 4000
340 dw 0x8000
341 db 0x0b
342 db 0x92
343 dw 0x0000
344 .end:
346 section .bss
347 posx:
348 resw 1
349 posy:
350 resw 1
351 bootdrv:
352 resb 1
353 convmem:
354 resw 1 ; in kilobytes
355 highmem:
356 resw 1 ; in kilobytes
357 idt:
358 resq 256
359 .end:
360 resb 512
361 stack: