dol: initial epiphany code generator (untested)
[jump.git] / dol / src / dol / visitor / epiphany / lib / ports.c
1 /* ports implementation */
2 #include <string.h>
3
4 #include "../shared.h"
5
6 extern shm_t shm;
7
8 int size_shm(void *hwbuf)
9 {
10         hwbuf_t *b = (hwbuf_t*)hwbuf;
11         return(b->size);
12 }
13
14 int level_shm(void *hwbuf)
15 {
16         hwbuf_t *b = (hwbuf_t*)hwbuf;
17
18         int level = (b->wp - b->rp) % b->size;
19         if(level < 0) level += b->size;
20
21         return(level);
22 }
23
24 int read_shm (void *hwbuf, void *buf, int len)
25 {
26         hwbuf_t *b = (hwbuf_t*)hwbuf;
27
28         /* block if necessary */
29         while(level_shm(hwbuf) < len);
30
31         /* copy data */
32         if(b->rp + len > b->size) {
33                 /* wrap-around -> split access */
34                 uint32_t amount = b->size - b->rp;
35                 memcpy(buf, &b->buf[b->rp], amount);
36                 memcpy(buf + amount, &b->buf[0], len - amount);
37         } else {
38                 memcpy(buf, &b->buf[b->rp], len);
39         }
40
41         /* update read pointer */
42         b->rp = (b->rp + len) % b->size;
43
44         return(len);
45 }
46
47 int write_shm(void *hwbuf, void *buf, int len)
48 {
49         hwbuf_t *b = (hwbuf_t*)hwbuf;
50
51         /* block if necessary */
52         while(b->size - level_shm(hwbuf) < len);
53
54         /* copy data */
55         if(b->wp + len > b->size) {
56                 /* wrap-around -> split access */
57                 uint32_t amount = b->size - b->wp;
58                 memcpy(&b->buf[b->wp], buf, amount);
59                 memcpy(&b->buf[0], buf + amount, len - amount);
60         } else {
61                 memcpy(&b->buf[b->wp], buf, len);
62         }
63
64         /* update write pointer */
65         b->wp = (b->wp + len) % b->size;
66
67         return(len);
68 }
69