⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memtestlib.c

📁 hi everybody enjoy hi everybody enjoy hi everybody enjoy hi everybody enjoy hi everybody enjoy hi ev
💻 C
字号:
// 8051 port of the Memory Test Library
// written by Michael Barr

// Ported to the 8051 by the Embedded Systems Academy
// Version 1.00 Written by AA 7/31/00

#define NULL 0L
typedef unsigned char datum;

/*******************************************************************
 *
 * Function:    memTestDataBus()
 *
 * Description: Test the data bus wiring in a memory region by
 *              performing a walking 1's test at a fixed address
 *              within that region. The address (and hence the
 *              memory region) is selected by the caller.
 *
 * Notes:
 *
 * Returns:     0 if the test succeeds.
 *              A non-zero result is the first pattern that failed.
 *
 *******************************************************************/
datum memTestDataBus(volatile datum xdata * address)
{
  datum pattern;
  
  /*
   * Perform a walking 1's test at the given address
   */
  for (pattern = 1; pattern != 0; pattern <<= 1)
  {
    /*
     * Write the test pattern
     */
    *address = pattern;
    
    /*
     * Read it back (immediately is okay for this test).
     */
    if (*address != pattern)
    {
      return (pattern);
    }
  }
  
  return (0);
  
} /* memTestDataBus() */


/**********************************************************************
 *
 * Function:    memTestAddressBus()
 *
 * Description: Test the address bus wiring in a memory region by
 *              performing a walking 1's test on the relevant bits
 *              of the address and checking for aliasing.  The test
 *              will find single-bit address failures such as stuck
 *              -high, stuck-low, and shorted pins.  The base address
 *              and size of the region are selected by the caller.
 *
 * Notes:       For best results, the selected base address should
 *              have enough LSB 0's to guarantee single address bit
 *              changes.  For example, to test a 64-Kbyte region, 
 *              select a base address on a 64-Kbyte boundary.  Also, 
 *              select the region size as a power-of-two--if at all 
 *              possible.
 *
 * Returns:     NULL if the test succeeds.  
 *              A non-zero result is the first address at which an
 *              aliasing problem was uncovered.  By examining the
 *              contents of memory, it may be possible to gather
 *              additional information about the problem.
 *
 **********************************************************************/
datum xdata *memTestAddressBus(volatile datum xdata * baseAddress, unsigned long nBytes)
{
  unsigned long addressMask = (nBytes - 1);
  unsigned long offset;
  unsigned long testOffset;
  
  datum pattern     = (datum) 0xAAAAAAAA;
  datum antipattern = (datum) 0x55555555;
  
  /*
   * Write the default pattern at each of the power-of-two offsets.
   */
  for (offset = sizeof(datum); (offset & addressMask) != 0; offset <<= 1)
  {
    baseAddress[offset] = pattern;
  }
  
  /*
   * Check for address bits stuck high
   */
  testOffset = 0;
  baseAddress[testOffset] = antipattern;
  
  for (offset = sizeof(datum); (offset & addressMask) != 0; offset <<= 1)
  {
    if (baseAddress[offset] != pattern)
    {
      return ((datum xdata *) &baseAddress[offset]);
    }
  }
  
  baseAddress[testOffset] = pattern;
  
  /*
   * Check for address bits stuck low or shorted
   */
  for (testOffset = sizeof(datum); (testOffset & addressMask) != 0;
       testOffset <<= 1)
  {
    baseAddress[testOffset] = antipattern;
    
    for (offset = sizeof(datum); (offset & addressMask) != 0; offset <<= 1)
    {
      if ((baseAddress[offset] != pattern) && (offset != testOffset))
      {
        return ((datum xdata *) &baseAddress[testOffset]);
      }
    }
    
    baseAddress[testOffset] = pattern;
  }
  
  return (NULL);
  
} /* memTestAddressBus() */

/**********************************************************************
 *
 * Function:    memTestDevice()
 *
 * Description: Test the integrity of a physical memory device by
 *              performing an increment/decrement test over the
 *              entire region.  In the process every storage bit 
 *              in the device is tested as a zero and a one.  The
 *              base address and the size of the region are
 *              selected by the caller.
 *
 * Notes:       
 *
 * Returns:     NULL if the test succeeds.  Also, in that case, the
 *              entire memory region will be filled with zeros.
 *
 *              A non-zero result is the first address at which an
 *              incorrect value was read back.  By examining the
 *              contents of memory, it may be possible to gather
 *              additional information about the problem.
 *
 **********************************************************************/
datum xdata *memTestDevice(volatile datum xdata * baseAddress, unsigned long nBytes)	
{
  unsigned long offset;
  unsigned long nWords = nBytes / sizeof(datum);

  datum pattern;
  datum antipattern;

  /*
   * Fill memory with a known pattern.
   */
  for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++)
  {
    baseAddress[offset] = pattern;
  }

  /*
   * Check each location and invert it for the second pass.
   */
  for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++)
  {
    if (baseAddress[offset] != pattern)
    {
      return ((datum xdata *) &baseAddress[offset]);
    }

    antipattern = ~pattern;
    baseAddress[offset] = antipattern;
  }

  /*
   * Check each location for the inverted pattern and zero it.
   */
  for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++)
  {
    antipattern = ~pattern;
    if (baseAddress[offset] != antipattern)
    {
      return ((datum xdata *) &baseAddress[offset]);
    }

    baseAddress[offset] = 0;
  }

  return (NULL);

} /* memTestDevice() */

/**********************************************************************
 *
 * Function:    memTest()
 *
 * Description: Test an 8-k chunk of RAM.
 * 
 * Notes:       
 *
 * Returns:     0 on success.
 *              Otherwise -1 indicates failure.
 *
 **********************************************************************/
int memTest(void)
{
  #define BASE_ADDRESS  (volatile datum xdata *) 0x00000000
  #define NUM_BYTES     8 * 1024

  if ((memTestDataBus(BASE_ADDRESS) != 0) ||
      (memTestAddressBus(BASE_ADDRESS, NUM_BYTES) != NULL) ||
      (memTestDevice(BASE_ADDRESS, NUM_BYTES) != NULL))
  {
    return (-1);
  }
  else
  {
    return (0);
  }		
} /* memTest() */
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -