1 /***
2 * Created by IntelliJ IDEA.
3 * User: gareth
4 * Date: 05-May-2005
5 * Time: 22:59:42
6 * To change this template use File | Settings | File Templates.
7 */
8 package com.tapina.robe.swi.clib;
9
10 import com.tapina.robe.runtime.ByteArrayUtils;
11 import com.tapina.robe.runtime.CPU;
12 import com.tapina.robe.runtime.Environment;
13 import com.tapina.robe.runtime.RawDataBlock;
14 import com.tapina.robe.swi.OS;
15 import com.tapina.robe.swi.clib.stdlib.RandomState;
16
17 import java.util.StringTokenizer;
18
19 public class CLibrary {
20 private final static CLibrary ourInstance = new CLibrary();
21 public static final int EOF = -1;
22
23 public static CLibrary getInstance() {
24 return ourInstance;
25 }
26
27 private CLibrary() {
28 }
29
30 public final Stub dummy(final int stubId) {
31 return new Stub() {
32 public void executeStub(Environment environment) {
33 log.warning("Attempt to call unknown clib shared library stub &" + Integer.toHexString(stubId));
34 throw new UnknownSharedLibraryCall(2, stubId);
35 }
36 };
37 }
38
39 public final Stub traphandler() {
40 return new Stub() {
41 public void executeStub(Environment environment) {
42 log.warning("traphandler() not implemented");
43 throw new UnknownSharedLibraryCall(2, 0);
44 }
45 };
46 }
47
48 public Stub initialise() {
49 return new Stub() {
50 public void executeStub(Environment environment) {
51 log.warning("initialise() not implemented");
52 }
53 };
54 }
55
56 public Stub xdudivide() {
57 return new Stub() {
58 public void executeStub(Environment environment) {
59 final int[] R = environment.getCpu().R;
60 final long numerator = ((long) R[1]) & 0xffffffffL;
61 final long denominator = ((long) R[0]) & 0xffffffffL;
62 final long result = numerator / denominator;
63 R[0] = (int) result & 0xffffffff;
64 }
65 };
66 }
67
68 public Stub xduremainder() {
69 return new Stub() {
70 public void executeStub(Environment environment) {
71 final int[] R = environment.getCpu().R;
72 final long numerator = ((long) R[1]) & 0xffffffffL;
73 final long denominator = ((long) R[0]) & 0xffffffffL;
74 final long result = numerator % denominator;
75 R[0] = (int) result & 0xffffffff;
76 }
77 };
78 }
79
80 public Stub xddivide() {
81 return new Stub() {
82 public void executeStub(Environment environment) {
83 final int[] R = environment.getCpu().R;
84 R[0] = R[1] / R[0];
85 }
86 };
87 }
88
89 public Stub xdremainder() {
90 return new Stub() {
91 public void executeStub(Environment environment) {
92 final int[] R = environment.getCpu().R;
93 R[0] = R[1] % R[0];
94 }
95 };
96 }
97
98 public Stub main() {
99 return new Stub() {
100 public void execute(Environment environment) {
101
102 final CPU cpu = environment.getCpu();
103 final int[] R = cpu.R;
104 cpu.setPC(R[1]);
105 final String commandLine = OS.getInstance().getEnv().getCommandLine();
106 if (commandLine.indexOf('\"') >= 0) {
107 log.warning("argv does not handle quoted arguments correctly for command line: " + commandLine);
108 }
109 StringTokenizer tokenizer = new StringTokenizer(commandLine, " ");
110 final int argc = tokenizer.countTokens();
111 RawDataBlock argv = environment.getMemoryMap().createSystemDataBlock((argc + 1) * 4);
112 for (int i = 0; tokenizer.hasMoreTokens(); i++) {
113 String token = tokenizer.nextToken();
114 RawDataBlock tokenData = environment.getMemoryMap().createSystemDataBlock(token);
115 ByteArrayUtils.putInt(argv.getBytes(), i * 4, tokenData.getAddress());
116 }
117
118 ByteArrayUtils.putInt(argv.getBytes(), argc * 4, 0);
119 cpu.R[0] = argc;
120 cpu.R[1] = argv.getAddress();
121 }
122
123 public void executeStub(Environment environment) {
124 }
125 };
126 }
127
128 public final RandomState random = new RandomState();
129
130 public Stub strtoul() {
131 return new Stub() {
132 public void executeStub(Environment environment) {
133
134
135
136 log.warning("Ignore call to strtoul()");
137 }
138 };
139 }
140
141 }