home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / misc / realmem.zip / REALMEM.C < prev    next >
C/C++ Source or Header  |  1994-01-30  |  4KB  |  143 lines

  1. unsigned char MEM48[6];
  2. unsigned char GDT[]={
  3. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    //GDT entry 0 (null segment)
  4. 0xFF,0xFF,0x00,0x00,0x00,0x92,0xCF,0xFF};   //GDT entry 1 (seg 0, limit 4GB)
  5. void INITCPU32(void)
  6. {
  7.     asm mov MEM48[0],16
  8.     asm mov eax,seg GDT
  9.     asm shl eax,4
  10.     asm mov bx,offset GDT
  11.     asm movzx ebx,bx
  12.     asm add eax,ebx
  13.     asm mov dword ptr MEM48[2],eax
  14.     asm lgdt pword ptr MEM48        //Load global descriptor table address
  15.  
  16.     asm mov bx,08h                  //Load bx to point to GDT entry 1
  17.     asm push ds
  18.     asm cli                         //Disable interrupts
  19.     asm mov eax,cr0                 //Switch to protected mode 
  20.     asm or eax,1
  21.     asm mov cr0,eax                  
  22.     asm jmp PROTECTION_ENABLED      //Clear executionpipe
  23.     PROTECTION_ENABLED:
  24.     asm mov gs,bx                   //Load segment shadow-registers
  25.     asm mov fs,bx                   //with GDT entry 1 (4GB segment limit)
  26.     asm mov es,bx
  27.     asm mov ds,bx                   
  28.     asm and al,0FEh                 //Switch back to real-mode without
  29.     asm mov cr0,eax                 //resetting the CPU
  30.     asm jmp PROTECTION_DISABLED     //Clear executionpipe
  31.     PROTECTION_DISABLED:
  32.     asm sti                         //Enable interrupts
  33.     asm pop ds
  34. }
  35.  
  36. unsigned long XMSDRIVERADDRESS;
  37.  
  38. unsigned int _virtual86(void)
  39. {
  40.     asm mov eax,cr0
  41.     asm and ax,1
  42. }
  43. #define XMScall(func,d)\
  44.     asm mov ah,func;\
  45.     asm mov dx,d;\
  46.     asm call dword ptr XMSDRIVERADDRESS;
  47. unsigned int XMSinit(void)
  48. {
  49.     asm mov ax,0x4300
  50.     asm int 0x2F
  51.     asm cmp al,0x80
  52.     asm je XMSOK1
  53.     return(1);
  54.     XMSOK1:
  55.     asm mov ax,0x4310
  56.     asm int 0x2F
  57.     asm mov ax,es
  58.     asm shl eax,16
  59.     asm mov ax,bx
  60.     asm mov XMSDRIVERADDRESS[0],bx
  61.     asm mov XMSDRIVERADDRESS[2],es
  62.     XMScall(0,0);
  63.     asm cmp ah,2
  64.     asm jae XMSOK2
  65.     return(1);
  66.     XMSOK2:
  67.     return(0);
  68. }
  69. unsigned int XMSlocal_enable_A20(void)
  70. {
  71.     XMScall(0x05,0);
  72. }
  73. unsigned int XMSqueryfree(void)
  74. {
  75.     XMScall(0x08,0);
  76.     asm mov ax,dx
  77. }
  78. unsigned int XMSalloc(unsigned int kbytes)
  79. {
  80.     XMScall(0x09,kbytes);
  81.     asm cmp ax,0x0001
  82.     asm je ALLOCOK
  83.     return(-1);
  84.     ALLOCOK:
  85.     asm mov ax,dx
  86. }
  87. unsigned int XMSfree(unsigned int handle)
  88. {
  89.     XMScall(0x0A,handle);
  90.     asm cmp ax,0x0001
  91.     asm je FREEOK
  92.     return(1);
  93.     FREEOK:
  94.     asm mov ax,bx
  95. }
  96. unsigned long XMSlock(unsigned int handle)
  97. {
  98.     XMScall(0x0C,handle);
  99.     asm mov ax,bx
  100. }
  101. void XMSunlock(unsigned int handle)
  102. {
  103.     XMScall(0x0D,handle);
  104. }
  105.  
  106. void main(void)
  107. {
  108.     unsigned int xmshandle;
  109.     unsigned long xmsaddress;
  110.     if(_virtual86()) {printf("Machine in virtual 8086 mode, remove QEMM and other such drivers.\n");exit(1);}
  111.     INITCPU32();
  112.     if(XMSinit()) {printf("XMS-driver not found or too old\n");exit(1);}
  113.     if(XMSqueryfree()<100) {printf("Not enough free XMS-memory\n");exit(1);}
  114.     XMSlocal_enable_A20();
  115.     xmshandle=XMSalloc(100);
  116.     xmsaddress=XMSlock(xmshandle);
  117.     printf("Writing 100KB to XMS\n");
  118.     asm xor ax,ax
  119.     asm mov es,ax
  120.     asm xor edi,edi
  121.     asm mov ebx,xmsaddress
  122.     XFILL:  asm mov dword ptr es:[ebx+edi],012345678h
  123.         asm add edi,4                   // Write 100000bytes to
  124.         asm cmp edi,100000              // extended memory
  125.         asm jb XFILL
  126.     asm xor edi,edi
  127.     printf("Checking the written data\n");
  128.     XCHK:   asm cmp dword ptr es:[ebx+edi],012345678h
  129.         asm jne READERR                 // Check, that data is
  130.         asm add edi,4                   // correct
  131.         asm cmp edi,100000
  132.         asm jb XCHK
  133.         asm jmp NOERRORS
  134.     READERR:
  135.     printf("Extended memory use failed\n");
  136.     asm jmp ERRORS
  137.     NOERRORS:
  138.     printf("Success\n");
  139.     ERRORS:
  140.     XMSunlock(xmshandle);
  141.     XMSfree(xmshandle);
  142. }
  143.