/***************************************************************************/
/* example_exception_scan.c
 * 
 * Build:  
 *  $COMMONCC -o example_exception_scan example_exception_scan.c $VMELIBS
 *
 * Simple example of who to scan the VME A32 address range from
 * 0x10000000 to 0xE0000000 using exception/bus error handling. 
 */ 
/***************************************************************************/

#include <stdio.h>
#include <sys/types.h>

#include "Vme.h"

#define VME_MAP_ADDRESS_FROM 0x40000000
#define VME_MAP_ADDRESS_TO   0xe4000000
#define VME_MAP_ADDRESS_OFF  0x01000000
#define VME_MAP_SIZE    1024*1024  /* 1 MB */ 
#define VME_MAP_AM      0x09       /* Extended VME address */

/***************************************************************************/
main (int argc, char *argv[])
/*---------------------------
*/
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
{
  u_long vme_address;
  int    cid, signal_nr;
  volatile int data;
  int    *vme_base;
/*.........................................................................*/

  printf("%s: VME A32 address scan\n", argv[0]);
  printf("  from 0x%x to 0x%x in steps of 0x%x (%dMB).\n",
	 VME_MAP_ADDRESS_FROM, VME_MAP_ADDRESS_TO,
	 VME_MAP_ADDRESS_OFF, VME_MAP_ADDRESS_OFF>>20);  
 
  /* Define the exception handling policy.
   */  
  VmeSetExceptionHandling(Vme_EXCEPTION_CHECK);

  for (vme_address  = VME_MAP_ADDRESS_FROM; 
       vme_address <= VME_MAP_ADDRESS_TO;
       vme_address += VME_MAP_ADDRESS_OFF)
    {
      /* Open a VME channel for Programmed I/O.
       */
      cid = VmeOpenChannel("Example1","pio");
      if (cid < 0 ) 
	exit(1);

      /* Map VME address space.
       */
      vme_base = (int *)VmeMapAddress(cid,
				      vme_address,
				      VME_MAP_SIZE,
				      VME_MAP_AM);
      if (vme_base == NULL)
	exit(1);
      
      /* Try to do a read access to this address.
       */
      Vme_D32READ( vme_base, &vme_base[0], data);
      if (signal_nr=VmeCheckException())
	printf("0x%08x: read failed, signal number = %d\n",
	       vme_address, signal_nr);
      else
	printf("0x%08x: 0x%x read\n", vme_address, data);
      
      /* Close Vme channel. Do not forget to call this function
       * before you exit otherwise you might get some trouble later.
       */
      (void)VmeCloseChannel(cid);
    }
}