Unreal Mode: Difference between revisions
[unchecked revision] | [unchecked revision] |
Content deleted Content added
m typo fix |
m Bot: Replace deprecated source tag with syntaxhighlight |
||
(33 intermediate revisions by 20 users not shown) | |||
Line 1:
'''Unreal mode'''
==Usage==
Unreal mode is usually recommended in the two following cases
*
*
==Implementation==
To do this, you need to set the descriptor cache's limits for your segment register(s) to any value higher than 64KiB (usually a full 4GiB (<tt>0xffffffff</tt>)).
In protected mode,
When
===Big Unreal Mode===
Finally, note that IP is unaffected by all this, so the code itself is still limited to 64k.▼
This won't touch CS. <br />
▲
<syntaxhighlight lang="asm">
; Assembly example
▲; nasmw boot.asm -o boot.bin
; partcopy boot.bin 0 200 -f0
start:
start: xor ax, ax ; make it zero▼
mov
mov
mov sp, 0x9c00 ; 2000h past code start,
; making the stack 7.5k in size
cli ; no
push ds ; save real mode
lgdt [gdtinfo] ; load gdt register
mov eax, cr0 ; switch to pmode by
or al,1 ; set pmode bit
mov cr0, eax
jmp 0x8:pmode
pmode:
mov bx, 0x08 ; select descriptor 1▼
mov
mov ds, bx ; 10h = 10000b
and al,0xFE ; back to realmode
mov cr0, eax ; by toggling bit again
jmp 0x0:unreal
unreal:
pop ds ; get back old segment
sti
mov bx, 0x0f01 ; attrib/char of smiley
mov eax, 0x0b8000 ; note 32 bit offset
mov word [ds:eax], bx
jmp $ ; loop forever
gdtinfo:
dw gdt_end - gdt - 1 ;last byte in table
dd gdt ;start of table
gdt: dd 0,0 ; entry 0 is always unused
flatdesc: db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0
gdt_end:
times 510-($-$$) db 0 ; fill sector w/ 0's
</syntaxhighlight>
===Huge Unreal Mode===
Huge Unreal Mode enables code over 64KiB. However, it is more difficult to implement as real mode interrupts do not automatically save the high 16 bits of EIP. Initialization is simple though, you just load a code segment with a 4GiB limit:
<syntaxhighlight lang="asm">
; Assembly example
; nasm boot.asm -o boot.bin
; partcopy boot.bin 0 200 -f0
ORG 0x7c00 ; add to offsets
▲start: xor ax, ax ; make it zero
... ; As before
mov cr0, eax
jmp 0x8:pmode
pmode:
mov ds, bx ; 10h = 10000b
and al,0xFE ; back to realmode
mov cr0, eax ; by toggling bit again
jmp 0x0:huge_unreal
huge_unreal:
... ;As before
gdtinfo:
dw gdt_end - gdt - 1 ;last byte in table
dd gdt ;start of table
gdt dd 0,0 ; entry 0 is always unused
flatcode db 0xff, 0xff, 0, 0, 0, 10011010b, 10001111b, 0
flatdata db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0
gdt_end:
times 510-($-$$) db 0 ; fill sector w/ 0's
dw 0xAA55 ; Required by some BIOSes
</syntaxhighlight>
WARNING: this may not work on some emulators or some hardware.
==Compiler Support==
===Smaller C===
The [[Smaller C]] compiler supports unreal mode. It produces [[MZ]] executables for unreal mode (can be loaded with [[BootProg]]).
The code and the stack are to be located below the 1MB mark and the stack size is limited by 64KB (IOW, there's nothing unusual about CS:(E)IP, SS:(E)SP, it's a natural setup for MZ executables in DOS). The DS and ES segment registers are set to 0, so C pointers can work as flat 32-bit physical addresses and address data or memory-mapped devices anywhere in the first 4GB of memory.
The startup code of these executables performs the necessary relocation (there are only custom relocations and no standard MZ relocations, which may simplify loading of the executables) and sets up unreal mode before passing control to the equivalent of ''main()''. See ''srclib/c0du.asm'' and other C/assembly code under ''srclib'' in the compiler source tree for how to write bits of assembly code for unreal mode (look for ''asm("inline asm code")'' under ''#ifdef __UNREAL__'').
You can try out unreal mode in DOS (e.g. in DOSBox, VirtualBox + FreeDOS) as the compiler fully supports the DOS + unreal mode combo in its C library. ''tests/vesalfb.c'' is a simple example of setting up a [[VESA]] graphics mode with the linear frame buffer enabled and drawing something on the screen in unreal mode.
For an example of an Unreal Mode [[bootloader]] implementation with Smaller C, look at [https://github.com/fysnet/FYSOS/tree/master/loader FYSOS].
[[Category:X86 CPU]]
[[Category:Operating Modes]]
[[Category:FAQ]]
|