Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 522 lines (440 sloc) 8.416 kb
95189b3 Import
paul authored
1 #include <sys/stat.h>
2 #include <sys/time.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5
6 #include "wl_def.h"
7
8 #ifndef O_BINARY
9 #define O_BINARY 0
10 #endif
11
4d69700 ARM Integrator port.
paul authored
12 #ifdef INTEGRATOR
13 volatile unsigned long tcount;
14 static volatile int tlock;
15
16 void set_TimeCount(unsigned long t)
17 {
18 tlock = 1;
19 tcount = t;
20 tlock = 0;
21 }
22
23 unsigned long get_TimeCount(void)
24 {
25 return tcount;
26 }
27
28 #define pic ((volatile uint32_t *)0x14000000)
29 #define pit ((volatile uint32_t *)0x13000100)
30
31 static uint32_t irqstack[4];
32
33 static void __attribute__((interrupt("irq")))
34 irqhandler (void)
35 {
36 if (!tlock)
37 tcount++;
38 /* Clear interrupt. */
39 pit[3] = 1;
40 }
41
42 void TimerInit()
43 {
44 register uint32_t r1 asm("r1");
45 (*(volatile uint32_t *)0x18) = 0xe59ff018; /* ldr pc, 0x38 */
46 (*(volatile uint32_t *)0x38) = (uint32_t)irqhandler;
47 /* Enable IRQ 6. */
48 pic[2] = 1 << 6;
49 /* Setup timer. */
bc5d5b7 Remove C library dependencies.
paul authored
50 pit[0] = 1000000 / 65; /* 70Hz. */
4d69700 ARM Integrator port.
paul authored
51 pit[2] = 0xe2; /* Enable periodic 32-bit timer. */
52 /* Set the IRQ stack and unmask interrupts. */
53 r1 = (uint32_t)(irqstack + 4);
54 asm volatile ("mrs r0, cpsr\n\t"
55 "bic r0, r0, #0x0f\n\t"
56 "orr r0, r0, #0x02\n\t"
57 "msr cpsr_cxsf, r0\n\t"
58 "mov sp, r1\n\t"
59 "bic r0, r0, #0x8f\n\t"
60 "orr r0, r0, #0x03\n\t"
61 "msr cpsr_cxsf, r0"
62 ::"r"(r1): "r0");
63 }
64
4a6e324 Luminary board port.
paul authored
65 #elif defined (LUMINARY)
d6bf851 Strip out unused demo code.
paul authored
66 #include "luminary.h"
4a6e324 Luminary board port.
paul authored
67
68 volatile unsigned long tcount;
69 static volatile int tlock;
70
71 void set_TimeCount(unsigned long t)
72 {
73 tlock = 1;
74 tcount = t;
75 tlock = 0;
76 }
77
78 unsigned long get_TimeCount(void)
79 {
80 return tcount;
81 }
82
83 #define treg ((volatile int *)0x40030000)
84
85 void timer_isr()
86 {
87 /* Clear interrupt. */
88 treg[9] = 1;
89 if (!tlock)
90 tcount++;
91 }
92
93 void TimerInit()
94 {
95 /* Setup and enable timer. */
96 treg[0] = 0;
97 treg[1] = 2;
98 treg[10] = 50000000 / 70;
99 treg[6] = 1;
100 treg[3] = 1;
101
3b9fe12 Suspend CPU.
paul authored
102 /* Enable Timer and SSI interrupts. */
103 NVIC[0x40] |= (1 << 19) | (1 << 7);
4a6e324 Luminary board port.
paul authored
104 }
105
106 #else
95189b3 Import
paul authored
107 static struct timeval t0;
108 static unsigned long tc0;
109
110 void set_TimeCount(unsigned long t)
111 {
112 tc0 = t;
113
114 gettimeofday(&t0, NULL);
115 }
116
117 unsigned long get_TimeCount(void)
118 {
119 struct timeval t1;
120 long secs, usecs;
121 unsigned long tc;
122
123 gettimeofday(&t1, NULL);
124
125 secs = t1.tv_sec - t0.tv_sec;
126 usecs = t1.tv_usec - t0.tv_usec;
127
128 if (usecs < 0) {
129 usecs += 1000000;
130 secs--;
131 }
132
133 tc = tc0 + secs * 70 + (usecs * 70) / 1000000;
134
135 return tc;
136 }
4d69700 ARM Integrator port.
paul authored
137 #endif
95189b3 Import
paul authored
138
079a926 Add Embedded mode.
paul authored
139 unsigned long sleepuntil(unsigned long t)
140 {
141 unsigned long now;
142 while (1) {
143 now = get_TimeCount();
144 if (now >= t)
145 break;
4d69700 ARM Integrator port.
paul authored
146 #if defined(__linux__)
079a926 Add Embedded mode.
paul authored
147 usleep(10000);
4d69700 ARM Integrator port.
paul authored
148 #elif defined(__thumb2__)
149 asm volatile ("wfi");
150 #elif defined(__arm__)
151 asm volatile ("mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt");
46047c3 Embedded port.
paul authored
152 #endif
079a926 Add Embedded mode.
paul authored
153 }
154 return now;
155 }
156
a5ee03f Remove dead code.
paul authored
157 #ifndef EMBEDDED
0933d39 Reduce object memory use.
paul authored
158 long filelength(myint handle)
95189b3 Import
paul authored
159 {
bc5d5b7 Remove C library dependencies.
paul authored
160 #ifdef __linux__
95189b3 Import
paul authored
161 struct stat buf;
162
163 if (fstat(handle, &buf) == -1) {
164 perror("filelength");
165 exit(EXIT_FAILURE);
166 }
167
168 return buf.st_size;
bc5d5b7 Remove C library dependencies.
paul authored
169 #else
170 return 0;
171 #endif
95189b3 Import
paul authored
172 }
173
0933d39 Reduce object memory use.
paul authored
174 char *itoa(myint value, char *string, myint radix)
95189b3 Import
paul authored
175 {
bc5d5b7 Remove C library dependencies.
paul authored
176 return ltoa(value, string, radix);
95189b3 Import
paul authored
177 }
178
0933d39 Reduce object memory use.
paul authored
179 char *ltoa(long value, char *string, myint radix)
95189b3 Import
paul authored
180 {
bc5d5b7 Remove C library dependencies.
paul authored
181 if (value < 0) {
182 string[0] = '-';
183 ultoa(-value, string + 1, radix);
184 return string;
185 }
81c9506 Fix itoa.
paul authored
186 return ultoa(value, string, radix);
95189b3 Import
paul authored
187 }
188
0933d39 Reduce object memory use.
paul authored
189 char *ultoa(unsigned long value, char *string, myint radix)
95189b3 Import
paul authored
190 {
bc5d5b7 Remove C library dependencies.
paul authored
191 unsigned long tmp;
192 char *p;
193 if (value == 0) {
194 string[0] = '0';
195 string[1] = 0;
196 return string;
197 }
198 p = string;
199 for (tmp = value; tmp != 0; tmp = tmp / 10) {
200 p++;
201 }
202 *p = 0;
203 while (value != 0) {
204 p--;
205 *p = (value % 10) + '0';
206 value /= 10;
207 }
95189b3 Import
paul authored
208 return string;
209 }
210
211 /* from Dan Olson */
212 static void put_dos2ansi(byte attrib)
213 {
214 byte fore,back,blink=0,intens=0;
215
216 fore = attrib&15; // bits 0-3
217 back = attrib&112; // bits 4-6
218 blink = attrib&128; // bit 7
219
220 // Fix background, blink is either on or off.
221 back = back>>4;
222
223 // Fix foreground
224 if (fore > 7) {
225 intens = 1;
226 fore-=8;
227 }
228
229 // Convert fore/back
230 switch (fore) {
231 case 0: // BLACK
232 fore=30;
233 break;
234 case 1: // BLUE
235 fore=34;
236 break;
237 case 2: // GREEN
238 fore=32;
239 break;
240 case 3: // CYAN
241 fore=36;
242 break;
243 case 4: // RED
244 fore=31;
245 break;
246 case 5: // Magenta
247 fore=35;
248 break;
249 case 6: // BROWN(yellow)
250 fore=33;
251 break;
252 case 7: //GRAy
253 fore=37;
254 break;
255 }
256
257 switch (back) {
258 case 0: // BLACK
259 back=40;
260 break;
261 case 1: // BLUE
262 back=44;
263 break;
264 case 2: // GREEN
265 back=42;
266 break;
267 case 3: // CYAN
268 back=46;
269 break;
270 case 4: // RED
271 back=41;
272 break;
273 case 5: // Magenta
274 back=45;
275 break;
276 case 6: // BROWN(yellow)
277 back=43;
278 break;
279 case 7: //GRAy
280 back=47;
281 break;
282 }
283 }
284
0933d39 Reduce object memory use.
paul authored
285 void DisplayTextSplash(const byte *text, myint l)
95189b3 Import
paul authored
286 {
0933d39 Reduce object memory use.
paul authored
287 myint i, x;
95189b3 Import
paul authored
288
289 //printf("%02X %02X %02X %02X\n", text[0], text[1], text[2], text[3]);
290 text += 4;
291 //printf("%02X %02X %02X %02X\n", text[0], text[1], text[2], text[3]);
292 text += 2;
293
294 for (x = 0; x < l; x++) {
295 for (i = 0; i < 160; i += 2) {
296 put_dos2ansi(text[160*x+i+2]);
297 if (text[160*x+i+1] && text[160*x+i+1] != 160)
298 printf("%c", text[160*x+i+1]);
299 else
300 printf(" ");
301 }
302 printf("%c[m", 27);
303 printf("\n");
304 }
305 }
6f3263b Disable menu code.
paul authored
306 #endif
95189b3 Import
paul authored
307
308 /* ** */
309
310 uint16_t SwapInt16L(uint16_t i)
311 {
312 #if BYTE_ORDER == BIG_ENDIAN
313 return ((uint16_t)i >> 8) | ((uint16_t)i << 8);
314 #else
315 return i;
316 #endif
317 }
318
319 uint32_t SwapInt32L(uint32_t i)
320 {
321 #if BYTE_ORDER == BIG_ENDIAN
322 return ((uint32_t)(i & 0xFF000000) >> 24) |
323 ((uint32_t)(i & 0x00FF0000) >> 8) |
324 ((uint32_t)(i & 0x0000FF00) << 8) |
325 ((uint32_t)(i & 0x000000FF) << 24);
326 #else
327 return i;
328 #endif
329 }
330
331 /* ** */
332
89cb49b Really remove file IO code.
paul authored
333 #ifndef ENABLE_PRECOMPILE
0933d39 Reduce object memory use.
paul authored
334 myint OpenWrite(const char *fn)
95189b3 Import
paul authored
335 {
0933d39 Reduce object memory use.
paul authored
336 myint fp;
95189b3 Import
paul authored
337
338 fp = open(fn, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
339 return fp;
340 }
341
0933d39 Reduce object memory use.
paul authored
342 myint OpenWriteAppend(const char *fn)
95189b3 Import
paul authored
343 {
0933d39 Reduce object memory use.
paul authored
344 myint fp;
95189b3 Import
paul authored
345
346 fp = open(fn, O_CREAT|O_WRONLY|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
347 return fp;
348 }
349
0933d39 Reduce object memory use.
paul authored
350 void CloseWrite(myint fp)
95189b3 Import
paul authored
351 {
352 close(fp);
353 }
354
0933d39 Reduce object memory use.
paul authored
355 myint WriteSeek(myint fp, myint offset, myint whence)
95189b3 Import
paul authored
356 {
357 return lseek(fp, offset, whence);
358 }
359
0933d39 Reduce object memory use.
paul authored
360 myint WritePos(myint fp)
95189b3 Import
paul authored
361 {
362 return lseek(fp, 0, SEEK_CUR);
363 }
364
0933d39 Reduce object memory use.
paul authored
365 myint WriteInt8(myint fp, int8_t d)
95189b3 Import
paul authored
366 {
367 return write(fp, &d, 1);
368 }
369
0933d39 Reduce object memory use.
paul authored
370 myint WriteInt16(myint fp, int16_t d)
95189b3 Import
paul authored
371 {
372 int16_t b = SwapInt16L(d);
373
374 return write(fp, &b, 2) / 2;
375 }
376
0933d39 Reduce object memory use.
paul authored
377 myint WriteInt32(myint fp, int32_t d)
95189b3 Import
paul authored
378 {
379 int32_t b = SwapInt32L(d);
380
381 return write(fp, &b, 4) / 4;
382 }
383
0933d39 Reduce object memory use.
paul authored
384 myint WriteBytes(myint fp, const byte *d, myint len)
95189b3 Import
paul authored
385 {
386 return write(fp, d, len);
387 }
388
389
0933d39 Reduce object memory use.
paul authored
390 myint OpenRead(const char *fn)
95189b3 Import
paul authored
391 {
0933d39 Reduce object memory use.
paul authored
392 myint fp;
95189b3 Import
paul authored
393
394 fp = open(fn, O_RDONLY | O_BINARY);
395
396 return fp;
397 }
398
0933d39 Reduce object memory use.
paul authored
399 void CloseRead(myint fp)
95189b3 Import
paul authored
400 {
401 close(fp);
402 }
403
0933d39 Reduce object memory use.
paul authored
404 myint ReadSeek(myint fp, myint offset, myint whence)
95189b3 Import
paul authored
405 {
406 return lseek(fp, offset, whence);
407 }
408
0933d39 Reduce object memory use.
paul authored
409 myint ReadLength(myint fp)
95189b3 Import
paul authored
410 {
411 return filelength(fp);
412 }
413
0933d39 Reduce object memory use.
paul authored
414 int8_t ReadInt8(myint fp)
95189b3 Import
paul authored
415 {
416 byte d[1];
417
418 read(fp, d, 1);
419
420 return d[0];
421 }
422
0933d39 Reduce object memory use.
paul authored
423 int16_t ReadInt16(myint fp)
95189b3 Import
paul authored
424 {
425 byte d[2];
426
427 read(fp, d, 2);
428
429 return (d[0]) | (d[1] << 8);
430 }
431
0933d39 Reduce object memory use.
paul authored
432 int32_t ReadInt32(myint fp)
95189b3 Import
paul authored
433 {
434 byte d[4];
435
436 read(fp, d, 4);
437
438 return (d[0]) | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
439 }
440
0933d39 Reduce object memory use.
paul authored
441 myint ReadBytes(myint fp, byte *d, myint len)
95189b3 Import
paul authored
442 {
443 return read(fp, d, len);
444 }
f8d3b9d Remove unused code.
paul authored
445 #endif
e3f5e16 Avoid floating point.
paul authored
446
a87ab4a Fix atan2 implementation.
paul authored
447 myshort atan2fix(fixed y, fixed x)
e3f5e16 Avoid floating point.
paul authored
448 {
449 boolean negative;
450 long long quot;
451 fixed tang;
452 int offset;
453 int res;
454 if (x < 0) {
455 x = -x;
456 negative = true;
457 offset = 180;
458 } else {
459 negative = false;
460 offset = 0;
461 }
462 if (y < 0) {
463 y = -y;
464 negative = !negative;
465 if (negative)
466 offset = 360;
467 }
468 if (x == 0)
469 return negative ? 270 : 90;
470 if (y == 0)
471 return offset;
a87ab4a Fix atan2 implementation.
paul authored
472 quot = ((long long)y << 16) / x;
e3f5e16 Avoid floating point.
paul authored
473 tang = (fixed)quot;
474 if (quot != tang) {
475 /* Overflow. */
476 res = 90;
477 } else {
478 int low = 0;
479 int high = FINEANGLES / 4 - 1;
480
ff2772a Embed wall graphics.
paul authored
481 res = 0;
e3f5e16 Avoid floating point.
paul authored
482 while (low + 1 < high) {
483 res = (low + high) >> 1;
484 if (finetangent[res] < tang)
485 low = res;
a87ab4a Fix atan2 implementation.
paul authored
486 else
487 high = res;
e3f5e16 Avoid floating point.
paul authored
488 }
489 res = res / (FINEANGLES / ANGLES);
490 }
491 if (negative)
492 res = -res;
493 return res + offset;
494 }
495
ab44810 Out of line sin/cos implementation.
paul authored
496 fixed sinfix(int a)
497 {
498 if (a > ANGLES / 2) {
499 a -= ANGLES / 2;
500 if (a > ANGLES / 4)
501 a = (ANGLES / 2) - a;
502 if (a == ANGLES / 4)
503 return -GLOBAL1;
504 return -(fixed)sintable[a];
505 } else {
506 if (a > ANGLES / 4)
507 a = (ANGLES / 2) - a;
508 if (a == ANGLES / 4)
509 return GLOBAL1;
510 return (fixed)sintable[a];
511 }
512 }
513
514 fixed cosfix(int a)
515 {
516 a += ANGLES / 4;
517 if (a >= ANGLES)
518 a -= ANGLES;
519 return sinfix(a);
520 }
521
Something went wrong with that request. Please try again.