1 | /* |
---|
2 | * C4x simulator IO interface routines based on code provided |
---|
3 | * by Herman ten Brugge <Haj.Ten.Brugge@net.HCC.nl> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | |
---|
10 | #define SIM_OPEN (0xf0) |
---|
11 | #define SIM_CLOSE (0xf1) |
---|
12 | #define SIM_READ (0xf2) |
---|
13 | #define SIM_WRITE (0xf3) |
---|
14 | #define SIM_LSEEK (0xf4) |
---|
15 | #define SIM_UNLINK (0xf5) |
---|
16 | #define SIM_GETENV (0xf6) |
---|
17 | #define SIM_RENAME (0xf7) |
---|
18 | #define SIM_GETTIME (0xf8) |
---|
19 | #define SIM_GETCLOCK (0xf9) |
---|
20 | |
---|
21 | typedef union _io { |
---|
22 | struct _open { |
---|
23 | int fd : 16; |
---|
24 | int flags : 16; |
---|
25 | } open; |
---|
26 | struct _openr { |
---|
27 | int result : 16; |
---|
28 | } openr; |
---|
29 | struct _close { |
---|
30 | int fd : 16; |
---|
31 | } close; |
---|
32 | struct _closer { |
---|
33 | int result : 16; |
---|
34 | } closer; |
---|
35 | struct _read { |
---|
36 | int fd : 16; |
---|
37 | int count : 16; |
---|
38 | } read; |
---|
39 | struct _readr { |
---|
40 | int result : 16; |
---|
41 | } readr; |
---|
42 | struct _write { |
---|
43 | int fd : 16; |
---|
44 | int count : 16; |
---|
45 | } write; |
---|
46 | struct _writer { |
---|
47 | int result : 16; |
---|
48 | } writer; |
---|
49 | struct _lseek { |
---|
50 | int fd : 16; |
---|
51 | int offsetlow : 16; |
---|
52 | int offsethigh : 16; |
---|
53 | int orgin : 16; |
---|
54 | } lseek; |
---|
55 | struct _lseekr { |
---|
56 | int result; |
---|
57 | } lseekr; |
---|
58 | struct _unlinkr { |
---|
59 | int result : 16; |
---|
60 | } unlinkr; |
---|
61 | struct _renamer { |
---|
62 | int result : 16; |
---|
63 | } renamer; |
---|
64 | struct _getenvr { |
---|
65 | int result : 16; |
---|
66 | } getenvr; |
---|
67 | struct _gettimer { |
---|
68 | int result; |
---|
69 | } gettimer; |
---|
70 | struct _getclockr { |
---|
71 | int result; |
---|
72 | } getclockr; |
---|
73 | struct _common { |
---|
74 | int word1; |
---|
75 | int word2; |
---|
76 | } common; |
---|
77 | } io; |
---|
78 | |
---|
79 | static void to_sim(int command, io *param, char *data, int length); |
---|
80 | static void call_sim(void); |
---|
81 | static void from_sim(io *param, char *data); |
---|
82 | |
---|
83 | void sim_exit(void) |
---|
84 | { |
---|
85 | __asm__(" .global C$$EXIT"); |
---|
86 | __asm__("C$$EXIT: nop"); |
---|
87 | __asm__("nop"); |
---|
88 | } |
---|
89 | |
---|
90 | int sim_open(const char *path, unsigned flags, int fno) |
---|
91 | { |
---|
92 | io param; |
---|
93 | |
---|
94 | param.open.fd = fno; |
---|
95 | param.open.flags = flags; |
---|
96 | to_sim(SIM_OPEN,¶m,(char *)path,strlen(path)+1); |
---|
97 | call_sim(); |
---|
98 | from_sim(¶m, NULL); |
---|
99 | return param.openr.result; |
---|
100 | } |
---|
101 | |
---|
102 | int sim_close(int fno) |
---|
103 | { |
---|
104 | io param; |
---|
105 | |
---|
106 | param.close.fd = fno; |
---|
107 | to_sim(SIM_CLOSE,¶m,NULL,0); |
---|
108 | call_sim(); |
---|
109 | from_sim(¶m, NULL); |
---|
110 | return param.closer.result; |
---|
111 | } |
---|
112 | |
---|
113 | int sim_read(int fno, char *buf, unsigned count) |
---|
114 | { |
---|
115 | io param; |
---|
116 | |
---|
117 | param.read.fd = fno; |
---|
118 | param.read.count = count; |
---|
119 | to_sim(SIM_READ,¶m,NULL,0); |
---|
120 | call_sim(); |
---|
121 | from_sim(¶m, buf); |
---|
122 | return param.readr.result; |
---|
123 | } |
---|
124 | |
---|
125 | int sim_write(int fno, const char *buf, unsigned count) |
---|
126 | { |
---|
127 | io param; |
---|
128 | |
---|
129 | param.write.fd = fno; |
---|
130 | param.write.count = count; |
---|
131 | to_sim(SIM_WRITE,¶m,(char *)buf,count); |
---|
132 | call_sim(); |
---|
133 | from_sim(¶m, NULL); |
---|
134 | return param.writer.result; |
---|
135 | } |
---|
136 | |
---|
137 | fpos_t sim_lseek(int fno, fpos_t offset, int origin) |
---|
138 | { |
---|
139 | io param; |
---|
140 | |
---|
141 | param.lseek.fd = fno; |
---|
142 | param.lseek.offsetlow = offset & 0xffff; |
---|
143 | param.lseek.offsethigh = offset >> 16; |
---|
144 | to_sim(SIM_LSEEK,¶m,NULL,0); |
---|
145 | call_sim(); |
---|
146 | from_sim(¶m, NULL); |
---|
147 | return param.lseekr.result; |
---|
148 | } |
---|
149 | |
---|
150 | int sim_unlink(const char *path) |
---|
151 | { |
---|
152 | io param; |
---|
153 | |
---|
154 | to_sim(SIM_UNLINK,NULL,(char *)path,strlen(path)+1); |
---|
155 | call_sim(); |
---|
156 | from_sim(¶m, NULL); |
---|
157 | return param.unlinkr.result; |
---|
158 | } |
---|
159 | |
---|
160 | int sim_rename(const char *old, const char *new) |
---|
161 | { |
---|
162 | int l; |
---|
163 | static char combined[200]; |
---|
164 | io param; |
---|
165 | |
---|
166 | strcpy(combined,old); |
---|
167 | l = strlen(old)+1; |
---|
168 | strcpy(combined+l,new); |
---|
169 | l += strlen(new) + 1; |
---|
170 | to_sim(SIM_RENAME,NULL,combined,l); |
---|
171 | call_sim(); |
---|
172 | from_sim(¶m, NULL); |
---|
173 | return param.renamer.result; |
---|
174 | } |
---|
175 | |
---|
176 | char *sim_getenv(const char *str) |
---|
177 | { |
---|
178 | io param; |
---|
179 | static char result[200]; |
---|
180 | |
---|
181 | to_sim(SIM_GETENV,NULL,(char *)str,strlen(str)+1); |
---|
182 | call_sim(); |
---|
183 | from_sim(¶m, result); |
---|
184 | return param.getenvr.result ? result : NULL; |
---|
185 | } |
---|
186 | |
---|
187 | int sim_gettime(void) |
---|
188 | { |
---|
189 | io param; |
---|
190 | |
---|
191 | to_sim(SIM_GETTIME,NULL,NULL,0); |
---|
192 | call_sim(); |
---|
193 | from_sim(¶m, NULL); |
---|
194 | return param.gettimer.result; |
---|
195 | } |
---|
196 | |
---|
197 | int sim_getclock(void) |
---|
198 | { |
---|
199 | io param; |
---|
200 | |
---|
201 | to_sim(SIM_GETCLOCK,NULL,NULL,0); |
---|
202 | call_sim(); |
---|
203 | from_sim(¶m, NULL); |
---|
204 | return param.getclockr.result; |
---|
205 | } |
---|
206 | |
---|
207 | int _CIOBUF_[BUFSIZ+32]; |
---|
208 | |
---|
209 | static void to_sim(int command, io *param, char *data, int length) |
---|
210 | { |
---|
211 | int i; |
---|
212 | int n; |
---|
213 | int v; |
---|
214 | int *ip = &_CIOBUF_[0]; |
---|
215 | |
---|
216 | *ip++ = length; |
---|
217 | *ip++ = command; |
---|
218 | if (param) { |
---|
219 | *ip++ = param->common.word1; |
---|
220 | *ip++ = param->common.word2; |
---|
221 | } |
---|
222 | else { |
---|
223 | *ip++ = 0; |
---|
224 | *ip++ = 0; |
---|
225 | } |
---|
226 | n = length & ~3; |
---|
227 | for (i = 0 ; i < n ; i += 4) { |
---|
228 | v = *data++ & 0xff; |
---|
229 | v |= (*data++ & 0xff) << 8; |
---|
230 | v |= (*data++ & 0xff) << 16; |
---|
231 | v |= (*data++ & 0xff) << 24; |
---|
232 | *ip++ = v; |
---|
233 | } |
---|
234 | v = 0; |
---|
235 | for ( ; i < length ; i++) { |
---|
236 | v |= (*data++ & 0xff) << ((i & 3) << 3); |
---|
237 | } |
---|
238 | *ip = v; |
---|
239 | } |
---|
240 | |
---|
241 | static void call_sim(void) |
---|
242 | { |
---|
243 | __asm__(" .global C$$IO$$"); |
---|
244 | __asm__("C$$IO$$: nop"); |
---|
245 | } |
---|
246 | |
---|
247 | static void from_sim(io *param, char *data) |
---|
248 | { |
---|
249 | int i; |
---|
250 | int l; |
---|
251 | int n; |
---|
252 | int v; |
---|
253 | int *ip = &_CIOBUF_[0]; |
---|
254 | |
---|
255 | l = *ip++; |
---|
256 | param->common.word1 = *ip++; |
---|
257 | param->common.word2 = *ip++; |
---|
258 | if (data != NULL) { |
---|
259 | n = l & ~3; |
---|
260 | for (i = 0 ; i < n ; i += 4) { |
---|
261 | v = *ip++; |
---|
262 | *data++ = v & 0xff; |
---|
263 | *data++ = (v >> 8) & 0xff; |
---|
264 | *data++ = (v >> 16) & 0xff; |
---|
265 | *data++ = (v >> 24) & 0xff; |
---|
266 | } |
---|
267 | v = *ip; |
---|
268 | for (; i < l ; i++) { |
---|
269 | *data++ = v >> ((i & 3) << 3); |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | #if 0 |
---|
275 | #include <fcntl.h> |
---|
276 | sim_io_test() |
---|
277 | { |
---|
278 | sim_write(1, "howdy\n", 6); |
---|
279 | } |
---|
280 | #endif |
---|
281 | |
---|
282 | /* |
---|
283 | * Debug junk |
---|
284 | */ |
---|
285 | #if 0 |
---|
286 | void printk_wrapper(void) |
---|
287 | { |
---|
288 | __asm__(" .global _printf"); |
---|
289 | __asm__("_printf: bu _printk"); |
---|
290 | } |
---|
291 | #endif |
---|
292 | |
---|
293 | #if 1 |
---|
294 | #ifdef _HAVE_STDC |
---|
295 | #include <stdarg.h> |
---|
296 | #else |
---|
297 | #include <varargs.h> |
---|
298 | #endif |
---|
299 | |
---|
300 | int __svfscanf( |
---|
301 | register FILE *fp, |
---|
302 | char const *fmt0, |
---|
303 | va_list ap |
---|
304 | ) |
---|
305 | { |
---|
306 | return 0; |
---|
307 | } |
---|
308 | #endif |
---|