more xwt -> ibex cleanup
[nestedvm.git] / src / tests / CallTest.java
1 package tests;
2
3 import org.ibex.nestedvm.Runtime;
4 import java.io.*;
5
6 public class CallTest {
7     public static void main(String[] args) throws Exception {
8         int a1 = args.length > 0 ? Integer.parseInt(args[0]) : 0;
9         int a2 = args.length > 1 ? Integer.parseInt(args[1]) : 0;
10         int a3 = args.length > 2 ? Integer.parseInt(args[2]) : 0;
11         int a4 = args.length > 3 ? Integer.parseInt(args[3]) : 0;
12         int a5 = args.length > 4 ? Integer.parseInt(args[4]) : 0;
13         int a6 = args.length > 5 ? Integer.parseInt(args[5]) : 0;
14         
15         System.out.println("Version is: " + System.getProperty("os.version"));
16         Runtime rt;
17         if(a1 == 99) // yeah.. this is ugly
18             rt = new org.ibex.nestedvm.Interpreter("build/tests/Test.mips");
19         else
20                 //FIXME: Callback not subclass
21             rt = new Test() {
22                 protected int callJava(int a, int b, int c, int d) {
23                     switch(a) {
24                         case 1: return strdup("OS: " + System.getProperty("os.name"));
25                         case 2: return strdup(System.getProperty("os.version"));
26                         case 3: return strdup(new Date().toString());
27                         case 4: return allocFDEnt(new OutputStreamFD(new CustomOS()));
28                         case 5:
29                             System.out.println("In callJava() in Java"); 
30                             try { call("backinmips"); } catch(CallException e) { }
31                             System.out.println("Back in callJava() in Java");
32                             return 0;
33                         default: return super.callJava(a,b,c,d);
34                     }
35                 }
36             };
37         System.out.println("Runtime: " + rt);
38         
39         rt.start(new String[]{"Test","calltest"});
40         rt.execute();
41         
42         System.out.println("== Start of CallTest ==");
43         System.out.println("Back in java... calling callme()");
44         int ret = rt.call("callme",a1,a2,a3,a4,a5,a6);
45         System.out.println("callme returned: " + ret);
46         
47         int addr = rt.strdup("Hello, World from java");
48         rt.call("echo",addr,4);
49         rt.free(addr);
50         System.out.println("== End of CallTest ==");
51         
52         rt.execute();
53         System.exit(rt.exitStatus());
54     }
55     
56     private static class CustomOS extends OutputStream {
57         public CustomOS() { }
58         public void write(int b) {  byte[] a = new byte[1]; a[0] = (byte)(b&0xff); write(a,0,1); }
59         public void write(byte[] b, int off, int len) {
60             int len2 = len;
61             while(b[len2-1]=='\n') len2--;
62             System.out.println("This just in from MIPS: " + new String(b,off,len2));
63         }
64     }
65 }