[ORG 7C00h]

[BITS 16]

	cli
	call enable_a20

	xor ax, ax
	mov ds, ax

	lgdt [gdt_desc]

	mov eax, cr0
	or eax, 1
	mov cr0, eax

	jmp 08h:clear_pipe


[BITS 32]

clear_pipe:
	mov ax, 10h
	mov ds, ax
	mov ss, ax

	mov eax, [ds:video]
	;mov eax, 0B8000h			; if this line is uncommented, then X is displayed on the screen
	mov byte [ds:eax], 'X'

	hlt


;; WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW


[BITS 16]

enable_a20:
	mov ax, 0FFFFh
	mov ds, ax

	mov ax, 0AA55h
	cmp ax, [7E0Eh]
	jne a20_enabled

	mov ax, 055AAh
	cmp ax, [7E0Eh]
	jne a20_enabled

	in al, 92h
	or al, 2
	out 92h, al

a20_enabled:
	ret


; WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
	
[BITS 32]

gdt:					; Address for the GDT

gdt_null:				; Null Segment
	dd 0
	dd 0

gdt_code:				; Code segment, read/execute, nonconforming
	dw 0FFFFh
	dw 0
	db 0
	db 10011010b
	db 11001111b
	db 0

gdt_data:				; Data segment, read/write, expand down
	dw 0FFFFh
	dw 0
	db 0
	db 10010010b
	db 11001111b
	db 0

gdt_end:				; Used to calculate the size of the GDT

gdt_desc:				; The GDT descriptor
	dw gdt_end - gdt - 1		; Limit (size)
	dd gdt				; Address of the GDT


video dw 0B8000h

times 510-($-$$) db 0
dw 0AA55h
