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