delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/18/18:46:55

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How much memory installed?
Date: Mon, 18 May 1998 18:41:48 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 63
Message-ID: <3560B92C.4E88@cs.com>
References: <35606A30 DOT 279D AT bergen DOT mail DOT telia DOT com>
NNTP-Posting-Host: ppp117.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

John Kismul wrote:
> 
> How do I find out how much memory is available, NOT counting virtual
> memory ONLY physical memory.

It's a somewhat inexact process, but I can supply you with the code I use 
for that purpose in my DJVERIFY program (thanks to Eli Zaretskii for the 
original).  This code detects up to 64 MB of installed RAM and returns the
amount in bytes.  There are some caveats:

 - The amount reported is usually about 300K off due to ROM BIOS shadowing.
 - The value wraps around on machines with more than 64 MB of RAM.
 - Windows NT may report less memory than is actually installed.

Good luck!

/*
 * Routines to return the amount of physical memory installed on the host
 * computer.  Thanks to Eli Zaretskii for this code.
 */
static void usec_sleep( int usec )
{
    uclock_t start_time = uclock ();
    uclock_t end_time = start_time + UMAX(1, usec * UCLOCKS_PER_SEC / 1000000);
    
    while ( uclock () < end_time )
	;
}


static unsigned char read_cmos( int reg )
{
    unsigned char al = ( reg & 0xff ) | 0x80;   /* disable NMI */
    
    outportb( 0x70, al );
    usec_sleep( 2 );            /* delay for 2 microseconds */
    al = inportb( 0x71 );
    usec_sleep( 2 );
    outportb( 0x70, 0 );        /* enable NMI */
    
    return al;
}


int installed_memory_size( void )
{
    unsigned base_lo, base_hi, ext_lo, ext_hi;
    
    base_lo = read_cmos( 0x15 );
    base_hi = read_cmos( 0x16 );
    ext_lo  = read_cmos( 0x17 );
    ext_hi  = read_cmos( 0x18 );
    
    return ( (base_hi + ext_hi) << 8 ) + base_lo + ext_lo;
}

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Sex should be friendly.  Otherwise  |
|       aka Fighteer I       | stick to mechanical toys; it's more  |
|   mailto:fighteer AT cs DOT com   | sanitary."                           |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019