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