import java.io.*;
import java.nio.*;
import java.util.*;

// A program to test the TM.java class, which allows you to talk to the
// tmumon program (and hence to the DE-3).
//	-- drg  September 2010

public class javatest
   {

public static void main(String args[]) {
   try {
      TM tm = new TM();
      System.out.println("javatest: open succeeded");
      System.out.println("javatest: design date: " + tm.getDesignDateString());
      System.out.println("javatest: design date again: " + tm.getDesignDateString());

      TMports tmports = new TMports();

      // XXX This should come from the ports file, but we can't do that on
      // the phone.
      tmports.describePort("input_data", TMports.WRITE, 32);
      tmports.describePort("result", TMports.READ, 32);

      int inputPort = tmports.open("input_data", "w");
      int resultPort = tmports.open("result", "r");

      int num_ints = 10;

      if(args.length > 0) {
         num_ints = Integer.valueOf(args[0]).intValue();
      }

      ByteBuffer data = ByteBuffer.allocate(num_ints * 4);
      data.order(ByteOrder.LITTLE_ENDIAN);
      int expected_result = 0;

      for(int i = 0; i < num_ints; i++) {
         data.putInt(i);
         expected_result += i;
      }

      tmports.write(inputPort, data.array(), 0, num_ints * 4);

      ByteBuffer resultBuffer = ByteBuffer.allocate(4);
      resultBuffer.order(ByteOrder.LITTLE_ENDIAN);

      tmports.read(resultPort, resultBuffer.array(), 0, 4);

      int result = resultBuffer.getInt();

      System.out.println("javatest: added from 0 to " + (num_ints - 1) + "; got " + result + "; should be " + expected_result);
   }
   catch(Exception e) {
      System.out.println("javatest: Can't talk to tmumon: " + e);
      System.exit(1);
   }
}


}
