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