; Exported Function
[GLOBAL _set_vesa]

[SECTION .vesa]
; Implementation of function

; set_vesa: C-Prototyp: void set_vesa ();

[BITS 32]

_set_vesa:
	
	sgdt	[saved_gdt]
	sidt	[saved_idt]

	mov	[saved_esp], esp
	
	lgdt  [gdtr]			       ; Load the GDT descriptor

;Comments from http://download.intel.com/design/PentiumII/manuals/24319202.pdf
;1. Disable interrupts. A CLI instruction disables maskable hardware interrupts. NMI
;interrupts can be disabled with external circuitry.
	cli
    
;2. Transfer program control to a readable segment that has a limit of 64 KBytes (FFFFH).
;This operation loads the CS register with the segment limit required in real-address mode.
	jmp 	RM_Code_Sel:pmode_16bit

;3. Load segment registers SS, DS, ES, FS, and GS with a selector for a descriptor containing
;the following values, which are appropriate for real-address mode:
;— Limit = 64 KBytes (0FFFFH)
;— Byte granular (G = 0)
;— Expand up (E = 0)
;— Writable (W = 1)
;— Present (P = 1)
;— Base = any value
;The segment registers must be loaded with nonnull segment selectors or the segment
;registers will be unusable in real-address mode. Note that if the segment registers are not
;reloaded, execution continues using the descriptor attributes loaded during protected
;mode.
[BITS 16]	 
pmode_16bit:
	
	mov 	ax,RM_Data_Sel
	mov 	ss,ax
	mov	ds,ax

;4. Execute an LIDT instruction to point to a real-address mode interrupt table that is within
;the 1-MByte real-address mode address range.
	;lidt 	[ridtr]

;5. Clear the PE flag in the CR0 register to switch to real-address mode.	
  	
	mov 	eax, cr0
  	and  	al,0xfe
	mov	cr0, eax

;6. Execute a far JMP instruction to jump to a real-address mode program. This operation
;flushes the instruction queue and loads the appropriate base and access rights values in the
;CS register.
 	mov   sp,0xFFFE

   mov   bx,[RealModeCS]
	push  bx
	lea   bx,[do_rm]
	push  bx

	retf

;7. Load the SS, DS, ES, FS, and GS registers as needed by the real-address mode code. If any
;of the registers are not going to be used in real-address mode, write 0s to them.
do_rm:

	mov	ax,cs
	mov 	ds,ax
	mov 	ss,ax
	nop
	mov 	es,ax
	mov 	fs,ax
	mov 	gs,ax

; point to real-mode IDTR
	lidt 	[ridtr]

;8. Execute the STI instruction to enable maskable hardware interrupts and perform the
;necessary hardware operation to enable NMI interrupts.
	sti

;We are in Real Mode, let's set 1024 x 768 x (16/32) or 800 x 600 x (16/32) or
;go back to CGA and hope for VGA

;Get VBE Controller Information - 512 byte
	mov	ax,0x700		;put struct to 0x7000
	mov	es,ax
	mov	di,0x0		;Put to 0x7000 with offset 0x0
	mov	eax,'VBE2'	;tell VESA-BIOS that we want VBE 2 rather than VBE 1
	mov	[es:di],eax
	mov	ax,0x4F00	;Function to get VBE Controller Information
	int	0x10
	cmp	al,0x4F		;Is Function supported?
	jne	continue_cga
	cmp	ah,0x00		;successful?
	jne	continue_cga

	mov	ax,0x800		;position for VBE Mode Information struct
	mov	es,ax
	mov	ax,0x4F01	;function call for VBE Mode Information
	mov	cx,[0x8200]	;getting VBE Mode number
	mov	di,0x0	
	int	0x10
	cmp	al,0x4F
	jne	continue_cga
	cmp	ah,0x00
	jne	continue_cga
	
	;jmp	continue_cga
;Set desired modes
	mov 	ax,0x4F02	;loading VBE mode-setting-function
	mov	bx,[0x8200]	;getting VBE Mode number
	or		bx,0x4000	;enable linear Buffering
	int	0x10			;go for it
	jmp 	activate_pm

continue_cga:

activate_pm:
	
;1. Disable interrupts. A CLI instruction disables maskable hardware interrupts.
;NMI interrupts can be disabled with external circuitry. (Software must guarantee
;that no exceptions or interrupts are generated during the mode switching
;operation.)
	cli

;2. Execute the LGDT instruction to load the GDTR register with the base
;address of the GDT.
	lgdt	[gdtr]

;3. Execute a MOV CR0 instruction that sets the PE flag (and optionally
;the PG flag) in control register CR0.
	mov	eax,cr0
	or		al,0x01
	mov	cr0,eax

;4. Immediately following the MOV CR0 instruction, execute a far JMP
;or far CALL instruction. (This operation is typically a far jump or call to
;the next instruction in the instruction stream.)
;
;The JMP or CALL instruction immediately after the MOV CR0 instruction 
;changes the flow of execution and serializes the processor. 
;
;If paging is enabled, the code for the MOV CR0 instruction and the JMP or 
;CALL instruction must come from a page that is identity mapped (that is, the 
;linear address before the jump is the same as the physical address 
;after paging and protected mode is enabled). The target instruction for the JMP or CALL
;instruction does not need to be identity mapped.
	jmp	PM_Code_Sel:do_pm

;5. If a local descriptor table is going to be used, execute the LLDT instruction
;to load the segment selector for the LDT in the
;LDTR register.

;6. Execute the LTR instruction to load the task register with a segment
;selector to the initial protected-mode task or to a writable area of memory that can be
;used to store TSS information on a task switch.
	do_pm:

;7. After entering protected mode, the segment registers conntinue to hold the contents
;they had in real-address mode. The JMP or CALL instruction in step 4 resets the CS register.
;Perform one of the following operations to update the contents of the remaining segment
;registers.  
;— Reload segment registers DS, SS, ES, FS, and GS. If the ES, FS, and/or
;GS registers are not going to be used, load them with a null selector.
;— Perform a JMP or CALL instruction to a new task, which automatically resets
;the values of the segment registers and branches to a new code segment.
	mov	ax,0x10
	mov	ds,ax
	mov	es,ax
	mov	fs,ax
	mov	gs,ax
	mov 	ss,ax
	mov	ds,ax

;8. Execute the LIDT instruction to load the IDTR register with the
;address and limit of the protected-mode IDT.

[BITS 32]
	
	mov	esp,[saved_esp]
	
	lidt	[saved_idt]
	lgdt	[saved_gdt]
	
;9. Execute the STI instruction to enable maskable hardware
;interrupts and perform the necessary hardware operation to enable NMI interrupts.
	ret

[SECTION .DATA]

;variables for "Real Mode <-> Protected Mode"-Switching

ridtr:	 
	dw 0xFFFF		; limit=0xFFFF
	dd 0				; base=0

RealModeCS:
	dw	0x0

gdtr:	
	dw gdt_end-1
	dd gdt

gdt:

	dw	0,0,0,0				;null desrciptor

PM_Code_Sel equ $-gdt
	dw	0xFFFF
	dw	0x0000
	dw	0x9A00
	dw	0x00CF

PM_Data_Sel	equ $-gdt
	dw	0xFFFF
	dw	0x0000
	dw	0x9200
	dw	0x00CF

RM_Code_Sel equ $-gdt
	dw	0xFFFF
	dw	0x0000
	dw	0x9A00
	dw	0x0000

RM_Data_Sel	equ $-gdt
	dw	0xFFFF
	dw 0x0000
	dw 0x9200
	dw 0x0000

gdt_end 

saved_gdt:
	dw	0
	dd	0

saved_idt:
	dw	0
	dd	0

saved_esp:
	dw	0

saved_gdtr:
	dw	0

