check_short.c 2.19 KB
 /* The purpose of this program is to detect short circuit between bits. This */
 /* test guarantees that any two bits can have different values. */

#include <ultra64.h>
#include <assert.h>
#include "main.h"

extern	errorReport1(u32, u32, u32);
extern 	errorReport2(u32, u32, u32, u32, u32);

u32 checkShort(u32 *begadr, u32 *endadr)
{
  u32 mask;
  u32 *ptr;
  u8 loop_cnt, i;
  u32 error_cnt=0;
  u32 value, xor;
  u32 pattern1=0x0;
  u32 pattern2=~(pattern1);
  u32 bitpattern[5]=
  {
    0xaaaaaaaa,			/* 10101010_10101010_10101010_10101010 */
    0xcccccccc,			/* 11001100_11001100_11001100_11001100 */
    0xf0f0f0f0,			/* 11110000_11110000_11110000_11110000 */
    0xff00ff00,			/* 11111111_00000000_11111111_00000000 */
    0xffff0000			/* 11111111_11111111_00000000_00000000 */
    };
  u32 reverse=0x0;
  
  /* First, we must know how long do we have to do the test in order not to do */
  /* unnecessary tests.*/

  mask=0x80000000;
  xor=(u32)begadr ^ ((u32)endadr - 1);
  for (loop_cnt=32; loop_cnt>0; loop_cnt--)
  {
    if (xor & mask)
      break;
    mask >>= 1;
  }
#ifdef DEBUG
  SOAKPRINTF("loop_cnt = %d\n", loop_cnt);
#endif
  
  do
  {
    for (i=0; i<5; i++)		/* 32bit(1 word) = 2 ^ 5 */
    {
#ifdef DEBUG
      SOAKPRINTF("mask = 2 ^ %d\n", i);
#endif
      
      for (ptr=begadr; ptr<endadr; ptr++)
	*ptr = bitpattern[i] ^ reverse;
      for (ptr=begadr; ptr<endadr; ptr++)
	if (*ptr != (bitpattern[i] ^ reverse))
	{
	  error_cnt++;
	  
#ifdef DEBUG
	  errorReport1((u32)ptr, bitpattern[i] ^ reverse, *ptr);
#endif	
	}
    }
    
    mask=0x4;
    for (i=2; i<loop_cnt; i++)	/* Since i=0 (and 1) in this part is exactly the same */
				/* as i=3 (resp. 4) in the previous part, we start    */
				/* with i=2.*/
    {
#ifdef DEBUG
      SOAKPRINTF("mask = %x\n", mask);
#endif
      for (ptr=begadr; ptr<endadr; ptr++)
	*ptr = ((u32)ptr & mask)? pattern2^reverse : pattern1^reverse;
      for (ptr=begadr; ptr<endadr; ptr++)
      {
	value = ((u32)ptr & mask)? pattern2^reverse : pattern1^reverse;
	if (*ptr != value)
	{
	  error_cnt++;
	  
#ifdef DEBUG
	  errorReport1((u32)ptr, value, *ptr);
#endif
	  
	}	
      }
      mask <<= 1;
    }
    reverse = ~reverse;
  } while (reverse);
  
  return error_cnt;
}