/*
 * 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1986
 * LICENSED MATERIALS - PROPERTY OF IBM
 * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
 */
/* $Header:vm.c 12.0$ */
/* $ACIS:vm.c 12.0$ */
/* $Source: /ibm/acis/usr/src/ibm/aedtest/RCS/vm.c,v $ */

#ifndef lint
static char *rcsid = "$Header:vm.c 12.0$";
#endif

/* vm.c */
#include "vikst.h"

typedef struct cnode { INT16  lbit[4];
		       INT16  hbit[4];
		       INT16  unum;
		     } chiperr, *chipptr;

#ifdef BSD
extern int v_control;
extern INT32 ledstatus;
#endif

memtest(seg,length)
UINT16 seg,length;
{ UINT16  temp,j,k,offset = 0;
  char *c;
  char l,r;
  extern UINT16 zero;
  UINT16 tosend[4];
  INT16 i;
  extern FILE *out,*spool;
  tosend[0] = 0x0000;
  tosend[1] = 0x5555;
  tosend[2] = 0xaaaa;
  tosend[3] = 0xffff;
  vikstop;
  for (i=0; i<16; i++)
      { j = 1 << i;
#ifdef BSD
		ioctl(v_control,AED_LEDS,&ledstatus);
#endif
	vwrite(&j,1,seg);
	vread(&k,1,seg);
	if (j != k) error(j,k,0);
      }

#ifndef BSD
  p2m("%4x",0);
#endif
  for (i=0; i<=3; i++)
      { vikstop;
#ifdef BSD
		ioctl(v_control,AED_LEDS,&ledstatus);
#endif
	for (j=0; j<length; j+=2)  vwrite(&(tosend[i]),1,seg+j);
#ifndef BSD
	p2m("\b\b\b\b%4x",j);
#endif
	l = tosend[i];
	for (j=0; j<length; j+=2)
	    { vread(&temp,1,seg+j);
	      r = temp & 0xff;
	      if (l != r) error((unsigned) l,(unsigned) r,j);
	      r = (temp >> 8) & 0xff;
	      if (l != r) error((unsigned) l,(unsigned) r,j+1);
#ifndef BSD
	      if ((j%512)==0) p2m("\b\b\b\b%4x",j);
#endif
	    }
       }

  vikstop;
  for (j=0; j<length; j+=2)  vwrite(&j,1,seg+j);
#ifndef BSD
  p2m("\b\b\b\b%4x",0);
#endif
  for (j=0; j<length; j+=2)
      { vread(&temp,1,seg+j);
	l = temp & 0xff;   /* l is low end of temp */
	r = j	 & 0xff;   /* r is low end of j, which was written */
	if (l != r) error((unsigned) r,(unsigned) l,j);
	l = (temp >> 8) & 0xff;   /* l is low end of temp */
	r = (j	  >> 8) & 0xff;   /* r is low end of j, which was written */
	if (l != r) error((unsigned) r,(unsigned) l,j+1);
#ifndef BSD
	if ((j%512)==0) p2m("\b\b\b\b%4x",j);
#endif
      }
#ifndef BSD
  p1m("\b\b\b\b");
#endif
}

error(exp,got,where)
UINT16 exp,got,where;
{  extern chipptr errloc[4][2];
   UINT16 i,prob;

#ifdef BSD
   if (where & 1)
	where--;
   else
	where++;
#endif
   i = where %4;
   prob = exp ^ got;
   if (prob &	1) errloc[i][0]->lbit[0] += 1;
   if (prob &	2) errloc[i][0]->lbit[1] += 1;
   if (prob &	4) errloc[i][0]->lbit[2] += 1;
   if (prob &	8) errloc[i][0]->lbit[3] += 1;
   if (prob &  16) errloc[i][1]->hbit[0] += 1;
   if (prob &  32) errloc[i][1]->hbit[1] += 1;
   if (prob &  64) errloc[i][1]->hbit[2] += 1;
   if (prob & 128) errloc[i][1]->hbit[3] += 1;
}
