Skip to content

Commit 31bf5ab

Browse files
committed
Merge pull request #500 from Roxxik/getting-unreal
Getting unreal
2 parents d2cf9f8 + c20af16 commit 31bf5ab

15 files changed

+899
-686
lines changed
Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use16
2-
3-
org 0x7C00
1+
ORG 0x7C00
2+
SECTION .text
3+
USE16
44

55
boot: ; dl comes with disk
66
; initialize segment registers
77
xor ax, ax
88
mov ds, ax
99
mov es, ax
1010
mov ss, ax
11+
1112
; initialize stack
1213
mov sp, 0x7C00
1314

@@ -22,9 +23,9 @@ boot: ; dl comes with disk
2223
call print_num
2324
call print_line
2425

25-
mov ax, (fs_header - boot)/512
26+
mov ax, (fs_header - boot) / 512
2627
mov bx, fs_header
27-
mov cx, (kernel_file.end - fs_header)/512
28+
mov cx, (startup_end - fs_header) / 512
2829
xor dx, dx
2930
call load
3031

@@ -34,6 +35,17 @@ boot: ; dl comes with disk
3435

3536
jmp startup
3637

38+
; load some sectors from disk to a buffer in memory
39+
; buffer has to be below 1MiB
40+
; IN
41+
; ax: start sector
42+
; bx: offset of buffer
43+
; cx: number of sectors (512 Bytes each)
44+
; dx: segment of buffer
45+
; CLOBBER
46+
; ax, bx, cx, dx, si
47+
; TODO rewrite to (eventually) move larger parts at once
48+
; if that is done increase buffer_size_sectors in startup-common to that (max 0x80000 - startup_end)
3749
load:
3850
cmp cx, 64
3951
jbe .good_size
@@ -43,7 +55,7 @@ load:
4355
call load
4456
popa
4557
add ax, 64
46-
add dx, 64*512/16
58+
add dx, 64 * 512 / 16
4759
sub cx, 64
4860

4961
jmp load
@@ -86,59 +98,16 @@ load:
8698
jc error
8799
ret
88100

89-
print_char:
90-
mov ah, 0x0e
91-
int 0x10
92-
ret
93-
94-
print_num:
95-
mov cx, 4
96-
.loop:
97-
mov al, bh
98-
shr al, 4
99-
and al, 0xF
100-
101-
cmp al, 0xA
102-
jb .below_a
103-
104-
add al, 'A' - '0' - 0xA
105-
.below_a:
106-
add al, '0'
107-
108-
push cx
109-
push bx
110-
call print_char
111-
pop bx
112-
pop cx
113-
114-
shl bx, 4
115-
loop .loop
116-
117-
ret
118-
119-
print_line:
120-
mov si, line
121-
call print
122-
ret
123-
124-
print:
125-
.loop:
126-
lodsb
127-
or al, al
128-
jz .done
129-
call print_char
130-
jmp .loop
131-
.done:
132-
ret
133-
134101
error:
135-
mov si, errored
136-
call print
137-
call print_line
102+
mov si, errored
103+
call print
104+
call print_line
138105
.halt:
139-
cli
140-
hlt
141-
jmp .halt
106+
cli
107+
hlt
108+
jmp .halt
109+
110+
%include "asm/print16.asm"
142111

143112
name: db "Redox Loader",0
144113
loading: db "Loading",0
@@ -149,13 +118,13 @@ line: db 13,10,0
149118
disk: db 0
150119

151120
DAPACK:
152-
db 0x10
153-
db 0
154-
.count: dw 0 ; int 13 resets this to # of blocks actually read/written
155-
.buf: dw 0 ; memory buffer destination address (0:7c00)
156-
.seg: dw 0 ; in memory page zero
157-
.addr: dd 0 ; put the lba to read in this spot
158-
dd 0 ; more storage bytes only for big lba's ( > 4 bytes )
121+
db 0x10
122+
db 0
123+
.count: dw 0 ; int 13 resets this to # of blocks actually read/written
124+
.buf: dw 0 ; memory buffer destination address (0:7c00)
125+
.seg: dw 0 ; in memory page zero
126+
.addr: dd 0 ; put the lba to read in this spot
127+
dd 0 ; more storage bytes only for big lba's ( > 4 bytes )
159128

160129
times 510-($-$$) db 0
161130
db 0x55

kernel/asm/descriptor_flags.inc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
attrib:
2+
.present equ 1 << 7
3+
.ring1 equ 1 << 5
4+
.ring2 equ 1 << 6
5+
.ring3 equ 1 << 5 | 1 << 6
6+
.user equ 1 << 4
7+
;user
8+
.code equ 1 << 3
9+
; code
10+
.conforming equ 1 << 2
11+
.readable equ 1 << 1
12+
; data
13+
.expand_down equ 1 << 2
14+
.writable equ 1 << 1
15+
.accessed equ 1 << 0
16+
;system
17+
; legacy
18+
.tssAvailabe16 equ 0x1
19+
.ldt equ 0x2
20+
.tssBusy16 equ 0x3
21+
.call16 equ 0x4
22+
.task equ 0x5
23+
.interrupt16 equ 0x6
24+
.trap16 equ 0x7
25+
.tssAvailabe32 equ 0x9
26+
.tssBusy32 equ 0xB
27+
.call32 equ 0xC
28+
.interrupt32 equ 0xE
29+
.trap32 equ 0xF
30+
; long mode
31+
.ldt32 equ 0x2
32+
.tssAvailabe64 equ 0x9
33+
.tssBusy64 equ 0xB
34+
.call64 equ 0xC
35+
.interrupt64 equ 0xE
36+
.trap64 equ 0xF
37+
38+
flags:
39+
.granularity equ 1 << 7
40+
.available equ 1 << 4
41+
;user
42+
.default_operand_size equ 1 << 6
43+
; code
44+
.long_mode equ 1 << 5
45+
; data
46+
.reserved equ 1 << 5

kernel/asm/gdt_entry.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struc GDTEntry
2+
.limitl resw 1
3+
.basel resw 1
4+
.basem resb 1
5+
.attribute resb 1
6+
.flags__limith resb 1
7+
.baseh resb 1
8+
endstruc

kernel/asm/initialize.asm

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,81 @@
11
SECTION .text
2-
[BITS 16]
2+
USE16
3+
34
initialize:
45
.fpu: ;enable fpu
5-
mov eax, cr4
6-
or eax, 0x200
7-
mov cr4, eax
8-
mov eax, 0xB7F
9-
push eax
10-
fldcw [esp]
11-
pop eax
12-
ret
6+
mov eax, cr4
7+
or eax, 0x200
8+
mov cr4, eax
9+
mov eax, 0xB7F
10+
push eax
11+
fldcw [esp]
12+
pop eax
13+
ret
1314

1415
.sse: ;enable sse
15-
mov eax, cr0
16-
and al, 11111011b
17-
or al, 00000010b
18-
mov cr0, eax
19-
mov eax, cr4
20-
or ax, 0000011000000000b
21-
mov cr4, eax
22-
ret
16+
mov eax, cr0
17+
and al, 11111011b
18+
or al, 00000010b
19+
mov cr0, eax
20+
mov eax, cr4
21+
or ax, 0000011000000000b
22+
mov cr4, eax
23+
ret
2324

2425
;PIT Frequency
2526
;If using nanoseconds, to minimize drift, one should find a frequency as close to an integer nanosecond value in wavelength
26-
;Divider Hz Nanoseconds Properties
27-
;2685 444.38795779019242706393 2250286.00003631746492922946 Best For Context Switching
28-
;5370 222.19397889509621353196 4500572.00007263492985856020
29-
;21029 56.73981961418358774390 17624306.99991199998882825455
30-
;23714 50.31549576902532962244 19874592.99994831745375667118
31-
;26399 45.19798729749864262535 22124878.99998463491868476373
32-
;29084 41.02536331545408701233 24375165.00002095238361424615
33-
;31769 37.55804925136663623868 26625451.00005726984854313455
34-
;34454 34.63115071302799868423 28875737.00009358731347639618
35-
;50113 23.80982313305263437963 41999471.99993295237244784676
36-
;52798 22.59899364874932131267 44249757.99996926983737931766
37-
;55483 21.50535599492937776736 46500044.00000558730230583335 Lowest Drift
38-
;58168 20.51268165772704350616 48750330.00004190476724037528
39-
;60853 19.60760630809765610021 51000616.00007822223218031738
27+
;Divider Hz Nanoseconds Properties
28+
;2685 444.38795779019242706393 2250286.00003631746492922946 Best For Context Switching
29+
;5370 222.19397889509621353196 4500572.00007263492985856020
30+
;21029 56.73981961418358774390 17624306.99991199998882825455
31+
;23714 50.31549576902532962244 19874592.99994831745375667118
32+
;26399 45.19798729749864262535 22124878.99998463491868476373
33+
;29084 41.02536331545408701233 24375165.00002095238361424615
34+
;31769 37.55804925136663623868 26625451.00005726984854313455
35+
;34454 34.63115071302799868423 28875737.00009358731347639618
36+
;50113 23.80982313305263437963 41999471.99993295237244784676
37+
;52798 22.59899364874932131267 44249757.99996926983737931766
38+
;55483 21.50535599492937776736 46500044.00000558730230583335 Lowest Drift
39+
;58168 20.51268165772704350616 48750330.00004190476724037528
40+
;60853 19.60760630809765610021 51000616.00007822223218031738
4041

4142
.pit:
42-
;initialize the PIT
43-
mov ax, 2685 ;this is the divider for the PIT
44-
out 0x40, al
45-
rol ax, 8
46-
out 0x40, al
47-
;DISABLED ;enable rtc interrupt
48-
;mov al, 0xB
49-
;out 0x70, al
50-
;rol ax, 8
51-
;in al, 0x71
52-
;rol ax, 8
53-
;out 0x70, al
54-
;rol ax, 8
55-
;or al, 0x40
56-
;out 0x71, al
57-
ret
43+
;initialize the PIT
44+
mov ax, 2685 ;this is the divider for the PIT
45+
out 0x40, al
46+
rol ax, 8
47+
out 0x40, al
48+
;DISABLED ;enable rtc interrupt
49+
;mov al, 0xB
50+
;out 0x70, al
51+
;rol ax, 8
52+
;in al, 0x71
53+
;rol ax, 8
54+
;out 0x70, al
55+
;rol ax, 8
56+
;or al, 0x40
57+
;out 0x71, al
58+
ret
5859

59-
.pic: ;sets up IRQs at int 20-2F
60-
mov al, 0x11
61-
out 0x20, al
62-
out 0xA0, al
63-
mov al, 0x20 ;IRQ0 vector
64-
out 0x21, al
65-
mov al, 0x28 ;IRQ8 vector
66-
out 0xA1, al
67-
mov al, 4
68-
out 0x21, al
69-
mov al, 2
70-
out 0xA1, al
71-
mov al, 1
72-
out 0x21, al
73-
out 0xA1, al
74-
xor al, al ;no IRQ masks
75-
out 0x21, al
76-
out 0xA1, al
77-
mov al, 0x20 ;reset PIC's
78-
out 0xA0, al
79-
out 0x20, al
80-
ret
60+
.pic: ;sets up IRQs at int 20-2F
61+
mov al, 0x11
62+
out 0x20, al
63+
out 0xA0, al
64+
mov al, 0x20 ;IRQ0 vector
65+
out 0x21, al
66+
mov al, 0x28 ;IRQ8 vector
67+
out 0xA1, al
68+
mov al, 4
69+
out 0x21, al
70+
mov al, 2
71+
out 0xA1, al
72+
mov al, 1
73+
out 0x21, al
74+
out 0xA1, al
75+
xor al, al ;no IRQ masks
76+
out 0x21, al
77+
out 0xA1, al
78+
mov al, 0x20 ;reset PIC's
79+
out 0xA0, al
80+
out 0x20, al
81+
ret

0 commit comments

Comments
 (0)