package inp.HAL;

import java.io.*;


public class HAL  {
  final static int MAXSLOTS =10;
  static { System.loadLibrary("JHAL");   }
  private int card=-1;

  public HAL() {  }
  
  public HAL(int baseAddr, int rangeLen, int INTnum) throws IOException {
    card = init(baseAddr, rangeLen, INTnum);
    if( card == -1 )  throw new IOException("Unable to lock hardware");
  }
  
  public void dispose() {
    release(card); 
  }
  
  private native int    init(int baseAddr, int rangeLen, int INTnum);  
  private native void   release(int card);

  public  native void    outByte(int addroffset, byte b);
  public  native void    outWord(int addroffset, short  word);  
  public  native byte    inByte (int addroffset);    
  public  native short   inWord (int addroffset); 
  private native int     waitInt(int card, long timeout);
  
  public synchronized void waitForInterrupt(long timeout) throws InterruptedException, IOException{
     int i=waitInt(card,timeout);
     if(i==0) return;
     if(i<0)  throw new InterruptedException();
     if(i>0)  throw new IOException("Interrupt wait timeout.");
  }
      
  
  
  public static void unloadAll(){
     HAL hal0 =new HAL();
     for(int i=0;i<MAXSLOTS;i++) hal0.release(i);
  }


  public static void main(String[] args) {
    
    try{
      HAL hal =new HAL(0x240,16,7);
      System.out.println(hal.card);
      HAL hal2 =new HAL(0x250,16,10);
      System.out.println(hal2.card);

      hal.release(hal.card);
      hal.release(hal2.card);      
    }catch(Exception e){
        e.printStackTrace();
    }
  }
  
}