#include <stdlib.h>
 
#include <stdio.h>
 
#include "wienvme_dll.h"
 
 
 
#ifdef _CVI_
 
#include <utility.h>
 
#endif
 
 
 
#ifdef LINUX
 
#include <dlfcn.h>
 
#define LoadLibrary(x) dlopen( (x), RTLD_NOW );
 
#define GetProcAddress(x,y) dlsym((x),(y))
 
static int  GetCurrentPlatform(void ) {
 
  return 0;
 
};
 
#define __stdcall
 
#define UCHAR unsigned char
 
#define HINSTANCE void *
 
#define  _VI_FUNC
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <stdint.h>
 
#include <string.h>
 
#endif
 
 
 
//----------- DEFINES -----------------------------------------------------
 
#define DEVICENAME_LINUX "/dev/pcivme_0"     // a device name 'template' for LINUX
 
#define DEVICENAME_NT "\\\\.\\PCIVME:\\VMEMMxx"     // a device name 'template' for WINNT
 
#define DEVICENAME_9X "\\\\.\\C:\\windows\\system\\VWIENVMED.vxd" // the same for WIN95/98
 
#define MODULE_NUMBER     1 // number of connected CC32 module
 
 
 
#define VMEinit (*vme_init)
 
static int VMEinit (const char*, unsigned short, unsigned char, int*);
 
#define VMEread (*vme_read)
 
static int VMEread (int, unsigned long, unsigned char, unsigned long, void*);
 
#define VMEwrite (*vme_write)
 
static int VMEwrite (int, unsigned long, unsigned char, unsigned long, void*);
 
#define VMEreset (*vme_reset)
 
static int VMEreset (int);
 
#define VMEclose (*vme_close)
 
static int VMEclose (int);
 
 
 
int hHandle24, hHandle32;
 
int VMEmodule;
 
 
 
static HINSTANCE DLLHandle;
 
void WIENVME_load (char* module_path) {
 
  /*
 
   int stat;
 
 
 
   if (module_path == NULL)
 
     VMEmodule = LoadExternalModule
 
       ("c:\\home\\CVI\\instr\\WIENVME_DLL\\pcivme_ni.lib");
 
    else
 
     VMEmodule = LoadExternalModule (module_path);
 
 
 
   vme_init = GetExternalModuleAddr (VMEmodule, "VMEinit", &stat);
 
   vme_read = GetExternalModuleAddr (VMEmodule, "VMEread", &stat);
 
   vme_write = GetExternalModuleAddr (VMEmodule, "VMEwrite", &stat);
 
   vme_reset = GetExternalModuleAddr (VMEmodule, "VMEreset", &stat);
 
   vme_close = GetExternalModuleAddr (VMEmodule, "VMEclose", &stat);
 
  */
 
  if (module_path == NULL) {
 
    DLLHandle = LoadLibrary("c:\\home\\CVI\\instr\\WIENVME_DLL\\pcivme_ni.dll");
 
  } else {
 
    DLLHandle = LoadLibrary(module_path);
 
  }
 
  if (!DLLHandle) {
 
    printf ("\n\nFailed to Open libxxusb.dll  \n");  
    return;
 
  }
 
 
 
  vme_init = (void *) GetProcAddress (DLLHandle , "VMEinit");
 
  vme_read = (void *) GetProcAddress (DLLHandle , "VMEread");
 
  vme_write =(void *) GetProcAddress (DLLHandle , "VMEwrite");
 
  vme_reset =(void *) GetProcAddress (DLLHandle , "VMEreset");
 
  vme_close =(void *) GetProcAddress (DLLHandle , "VMEclose");
 
 
 
  return;
 
}
 
 
 
int WIENVME_open (int* hHandle, unsigned char AddMod, char* device_name,
 
                  unsigned short module_number) {
 
  int result;
 
 
 
  /* open a path to a device. */
 
  if (device_name == NULL)
 
    switch (GetCurrentPlatform ()) {
 
      case 0:
 
        device_name = DEVICENAME_LINUX;
 
        break;
 
      case 2:
 
        device_name = DEVICENAME_9X;
 
        break;
 
      case 3:
 
        device_name = DEVICENAME_NT;
 
        break;
 
      default:
 
        break;
 
    }
 
  result = VMEinit(device_name, module_number, AddMod, hHandle);
 
  if (result) {
 
    printf("Can't open interface \"%s\" to VMEMM-module \"%d\"!\n", DEVICENAME_NT
, MODULE_NUMBER
);  
  }
 
  return(result);
 
}
 
 
 
int WIENVME_open24 (void) {
 
  return (WIENVME_open (&hHandle24, Std_NoPriv_Data, NULL, 1));
 
}
 
 
 
int WIENVME_open32 (void) {
 
  return (WIENVME_open (&hHandle32, Ext_NoPriv_Data, NULL, 1));
 
}
 
 
 
int WIENVME_start (char* module_path) {
 
  WIENVME_load(module_path);
 
  WIENVME_open24();
 
  WIENVME_open32();
 
 
 
  return 0;
 
}
 
 
 
void WIENVME_unload () {
 
  //UnloadExternalModule (VMEmodule);
 
  return;
 
}
 
 
 
int WIENVME_close (int hHandle) {
 
  int result;
 
 
 
  /* close the opened path */
 
  printf("hHandle %d\n",hHandle
);  
 
 
  result = VMEclose(hHandle);
 
  if (result) {
 
    printf("Can't close interface!\n");  
  }
 
  return (result);
 
}
 
 
 
int WIENVME_close24 () {
 
  return (WIENVME_close (hHandle24));
 
}
 
 
 
int WIENVME_close32 () {
 
  return (WIENVME_close (hHandle32));
 
}
 
 
 
int WIENVME_stop () {
 
  WIENVME_close24();
 
  WIENVME_close32();
 
  WIENVME_unload();
 
 
 
  return 0;
 
}
 
 
 
int WIENVME_reset () {
 
  int result;
 
 
 
  /* close the opened path */
 
  result = VMEreset(hHandle24);
 
  if (result) {
 
    printf("Can't reset interface!\n");  
  }
 
  return (result);
 
}
 
 
 
int WIENVME_read8 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D8 read */
 
  result = VMEread(hHandle, at, 1, n, buff);
 
  if (result) {
 
    printf("D8 read at 0x%lX failed!\n", at
);  
  }
 
//  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int WIENVME_read16 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D16 read */
 
  result = VMEread(hHandle, at, 2, n, buff);
 
  if (result) {
 
    printf("D16 read at 0x%lX failed!\n", at
);  
  }
 
//  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int WIENVME_read32 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D32 read */
 
  result = VMEread(hHandle, at, 4, n, buff);
 
  if (result) {
 
    printf("D32 read at 0x%lX failed!\n", at
);  
  }
 
  //printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int WIENVME_write8 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D8 write */
 
  result = VMEwrite(hHandle, at, 1, n, buff);
 
  if (result) {
 
    printf("D8 write at 0x%lX failed!\n", at
);  
  }
 
  return (result);
 
}
 
 
 
int WIENVME_write16 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D16 write */
 
  result = VMEwrite(hHandle, at, 2, n, buff);
 
  if (result) {
 
    printf("D16 write at 0x%lX failed!\n", at
);  
  }
 
  return (result);
 
}
 
 
 
int WIENVME_write32 (int hHandle, unsigned long n, unsigned long at, void* buff) {
 
  int result;
 
 
 
  /* D32 write */
 
  result = VMEwrite(hHandle, at, 4, n, buff);
 
  if (result) {
 
    printf("D32 write at 0x%lX failed!\n", at
);  
  }
 
  //printf("D32 write at 0x%X buff=0x%X\n", at,((int*) buff)[0]);
 
  return (result);
 
}
 
 
 
 
 
#ifndef VME_D32
 
 
 
#define VME_D8  0x1
 
#define VME_D16 0x2
 
#define VME_D32 0x4
 
#endif
 
 
 
short __stdcall WIENVME_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data) {
 
  int hHandle=0, nb=0;
 
  switch (AM) {
 
    case Ext_NoPriv_Data:
 
      hHandle = hHandle32;
 
      break;
 
    case Std_NoPriv_Data:
 
      hHandle = hHandle24;
 
      break;
 
    default :
 
      return 0;
 
  }
 
 
 
 
 
  switch (DW) {
 
    case VME_D16:
 
      nb=  WIENVME_read16 (hHandle, 1, (unsigned long) VME_Address, (void*) Data)  ;
 
      break;
 
    case VME_D32:
 
      nb=  WIENVME_read32 (hHandle, 1, (unsigned long) VME_Address, (void*) Data)  ;
 
      break;
 
 
 
    default:
 
      return 0;
 
  }
 
  return (short)nb;
 
}
 
 
 
short __stdcall WIENVME_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
 
  int hHandle=0, nb=0;
 
  switch (AM) {
 
    case Ext_NoPriv_Data:
 
      hHandle = hHandle32;
 
      break;
 
    case Std_NoPriv_Data:
 
      hHandle = hHandle24;
 
      break;
 
    default :
 
      return 0;
 
  }
 
 
 
 
 
  switch (DW) {
 
    case VME_D16:
 
      nb=  WIENVME_write16 (hHandle, 1, (unsigned long) VME_Address, (void*) &Data)  ;
 
      break;
 
    case VME_D32:
 
      nb=  WIENVME_write32 (hHandle, 1, (unsigned long) VME_Address, (void*) &Data)  ;
 
      break;
 
 
 
    default:
 
      return 0;
 
  }
 
  return (short)nb;
 
}
 
 
 
short __stdcall WIENVME_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
 
 
 
  return WIENVME_VME_W( AM, DW, VME_Address, Data);
 
}
 
short __stdcall WIENVME_VME_MWRST( void ) {
 
 
 
 
 
  return 0;
 
}
 
short __stdcall WIENVME_VME_MWEXEC( void ) {
 
 
 
 
 
  return 0;
 
}
 
 
 
short __stdcall WIENVME_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
 
 
 
 
 
  return WIENVME_VME_R( AM, DW, VME_Address, Data);
 
}
 
short __stdcall WIENVME_VME_MRRST( void ) {
 
 
 
 
 
  return 0;
 
}
 
short __stdcall WIENVME_VME_MREXEC(  uint32_t *Data  ) {
 
 
 
 
 
  return 0;
 
}