; ### DISCRIPTION ############################################################
; ###
; ### Version list
; ### ver 1.0:
; ###   First we enable the A20-Gate for using more then 1MB RAM.
; ###   Sets up a simple IDT/GDT and jumps in protected mode and fills the
; ###   registers with pmode requeired values.
; ### ver 1.1:
; ###   Loads now the IDT and GDT via fs:di in HIMEM.
; ###
; ### written by Robert Hanke.
; ### Copyright (c) 2007
; ###
; ### DISCRIPTION END ########################################################





; ### MACRO SECTION ##########################################################

%include "../PRJ/SRC/includes/macros.asm"

; ### MACRO SECTION END ######################################################





; ### COMPILER SECTION #######################################################

[ORG 0x0500]
[BITS 16]
[CPU 386]

; ### COMPILER SECTION END ###################################################





; ### CODE SECTION ###########################################################

CODE_SECTION:
	mov	ax, 0x0500			; update registers
	mov	es, ax
	mov	ds, ax

	cli

;VESA:	mov	bx, 4118h			; set vesa-mode to ax
;	mov	ax, 4f01h			; 4f = Statusflag, 01 Vesa function
;	mov	di, Mode_Info			; Mode_info
;	int	10h

;	mov ax, 4f02h				; switch to vesa
;	int 10h

A20Gate:	in	al, 0x64			; first enable A20 gate
	test	al, 00000010b
	jnz	A20Gate				; loop till buffer is emty

	mov	al, 0xD0
	out	0x64, al			; send read order

wait_1:	in	al, 0x64
	test	al, 00000001b
	jz	wait_1				; wait till inst. is done

	in	al, 0x60
	or	al, 00000010b
	mov	bl, al				; set A20 and store in bx

wait_2:	in	al, 0x64
	test	al, 00000010b
	jnz	wait_2				; loop till buffer is emty

	mov	al, 0xD1
	out	0x64, al			; send write order

wait_3:	in	al, 0x64
	test	al, 00000010b
	jnz	wait_3				; loop till buffer is emty

	mov	al, bl
	out	0x60, al			; send A20 to controller

table:	mov	ax, 0xFFFF
	mov	fs, ax
	mov	di, 0x0010

	mov	[fs:di   ], word 0x00		; DESC_0
	mov	[fs:di+ 2], word 0x00
	mov	[fs:di+ 4], word 0x00
	mov	[fs:di+ 6], word 0x00

	mov	[fs:di+ 8], word 0xFFFF		; DESC_1 (CODE)
	mov	[fs:di+10], word 0x0000
	mov	[fs:di+12], byte 0x00
	mov	[fs:di+13], byte 0x9A
	mov	[fs:di+14], byte 0xCF
	mov	[fs:di+15], byte 0x00

	mov	[fs:di+16], word 0xFFFF		; DESC_2 (DATA)
	mov	[fs:di+18], word 0x0000
	mov	[fs:di+20], byte 0x00
	mov	[fs:di+21], byte 0x92
	mov	[fs:di+22], byte 0xCF
	mov	[fs:di+23], byte 0x00

	mov	eax, cs				; set ds = cs
	mov	ds, ax

	sub	esp, 0x06			; setting up GDT by stack-
	mov	[esp  ],  word 0xFFFF		; usage
	mov	[esp+2], dword 0x100000
	lgdt	[esp]				; load descriptor in GDT

	mov	[esp  ],  word 0xFFFF		; setting up IDT
	mov	[esp+2], dword 0x110000
	lidt	[esp]

	add	esp, 0x06			; reset stack

enterpm:	mov	eax, cr0			; set pmode bit to 1
	or	eax, 0000001b
	mov	cr0, eax

jump:	jmp	dword CODE_DESC:pmode		; far-jump in pode
						; jmp dword 0x08:ende

Mode_Info:
	ModeInfo_ModeAttributes		dw	1
	ModeInfo_WinAAttributes		db	1
	ModeInfo_WinBAttributes		db	1
	ModeInfo_WinGranularity	        dw	1
	ModeInfo_WinSize		dw	1
	ModeInfo_WinASegment		dw	1
	ModeInfo_WinBSegment		dw	1
	ModeInfo_WinFuncPtr		dd	1
	ModeInfo_BytesPerScanLine	dw	1
	ModeInfo_XResolution		dw	1
	ModeInfo_YResolution		dw	1
	ModeInfo_XCharSize		db	1
	ModeInfo_YCharSize		db	1
	ModeInfo_NumberOfPlanes		db	1
	ModeInfo_BitsPerPixel		db	1
	ModeInfo_NumberOfBanks		db	1
	ModeInfo_MemoryModel		db	1
	ModeInfo_BankSize		db	1
	ModeInfo_NumberOfImagePages	db	1
	ModeInfo_Reserved_page		db	1
	ModeInfo_RedMaskSize		db	1
	ModeInfo_RedMaskPos		db	1
	ModeInfo_GreenMaskSize		db	1
	ModeInfo_GreenMaskPos		db	1
	ModeInfo_BlueMaskSize		db	1
	ModeInfo_BlueMaskPos		db	1
	ModeInfo_ReservedMaskSize	db	1
	ModeInfo_ReservedMaskPos	db	1
	ModeInfo_DirectColorModeInfo	db	1
	ModeInfo_PhysBasePtr		dd	1
	ModeInfo_OffScreenMemOffset	dd	1
	ModeInfo_OffScreenMemSize	dw	1



	times	512-(ende-pmode)-($-$$) db 0x00	; alignes the end of
						; the file to a
						; 512Byte line

	[BITS 32]
pmode:	mov	ax, DATA_DESC
	mov	ds, ax
	mov	es, ax
	mov	ss, ax

	xor	ax, ax
	mov	fs, ax
	mov	gs, ax
ende:

; ### CODE SECTION END #######################################################