;
	MAGIC_NUMBER = 0x1BADB002     ; define the magic number constant
	FLAGS        = 0x7            ; multiboot flags
	CHECKSUM     = -MAGIC_NUMBER -FLAGS  ; calculate the checksum
                                    ; (magic number + checksum + flags should equal 0)
	MODE_TYPE = 0
	WIDTH =  800 ; preferred width 
	HEIGHT = 600  ; preferred height 
	DEPTH = 32    ; preferred bits per pixel BPP
	HEADER_ADDR = 0
	LOAD_ADDR = 0
	LOAD_END_ADDR = 0
	BSS_END_ADDR = 0
	ENTRY_ADDR = 0

struc db [data]
     {
       common
        . db data
        .size = $ - .
     }


format ELF

section '.multiboot'
	align 4                         ; the code must be 4 byte aligned
	dd MAGIC_NUMBER             	; write the magic number to the machine code,
	dd FLAGS                    	; the flags,
	dd CHECKSUM
	dd HEADER_ADDR
	dd LOAD_ADDR
	dd LOAD_END_ADDR
	dd BSS_END_ADDR
	dd ENTRY_ADDR
	dd MODE_TYPE
	dd WIDTH
	dd HEIGHT
	dd DEPTH


section '.text' executable
	public loader                    ; the entry symbol for ELF
	public reload_segs
	public _stop
	public set_handlers
	public wait_vsync
	
	extrn kernelSetup
	extrn kernelMain
	extrn kernelPrint
	extrn kernelPrintInt
	extrn kernelInterruptHandler

loader:
	; 
	push kernel_stack
    push eax ; multiboot_magic
    push ebx ; multiboot_structure
    call kernelSetup
	
	; userspace?
    mov esp, kernel_stack ; stack pointer offset
    call kernelMain

_stop:
	;push bye.size
    ;push bye
    ;call kernelPrint
    cli
    hlt
    jmp _stop


reload_segs:
	mov ax, 0x10
	mov ds, ax ; 0x10 - an offset into GDT for the third (kernel data segment) record.
	mov ss, ax
	mov es, ax
	mov fs, ax
	mov gs, ax
	jmp 0x08:flush_cs

flush_cs:
	ret


wait_vsync:
	mov dx, 0x3da
.0:	in al, dx
	and al, 0x8
	jnz .0
.1:	in al, dx
	and al, 0x8
	jz .1
	ret


; Interrupts
include 'interrupts.asm'

section '.data'
bye db 'Bye!',0
key: rb 1


section '.bss' writeable
.space rb 128*1024; # 128k
kernel_stack:


section '.note.GNU-stack'
