X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Fvisitor%2Fepiphany%2Flib%2Fports.c;fp=dol%2Fsrc%2Fdol%2Fvisitor%2Fepiphany%2Flib%2Fports.c;h=240474b6ab96ac854b2e89fb1f008335c2304394;hb=2858109405a90a4d9df149b10444678d42e41c59;hp=0000000000000000000000000000000000000000;hpb=cbc1f1265fd129d066da10e72e819d7d20f5360a;p=jump.git diff --git a/dol/src/dol/visitor/epiphany/lib/ports.c b/dol/src/dol/visitor/epiphany/lib/ports.c new file mode 100644 index 0000000..240474b --- /dev/null +++ b/dol/src/dol/visitor/epiphany/lib/ports.c @@ -0,0 +1,69 @@ +/* ports implementation */ +#include + +#include "../shared.h" + +extern shm_t shm; + +int size_shm(void *hwbuf) +{ + hwbuf_t *b = (hwbuf_t*)hwbuf; + return(b->size); +} + +int level_shm(void *hwbuf) +{ + hwbuf_t *b = (hwbuf_t*)hwbuf; + + int level = (b->wp - b->rp) % b->size; + if(level < 0) level += b->size; + + return(level); +} + +int read_shm (void *hwbuf, void *buf, int len) +{ + hwbuf_t *b = (hwbuf_t*)hwbuf; + + /* block if necessary */ + while(level_shm(hwbuf) < len); + + /* copy data */ + if(b->rp + len > b->size) { + /* wrap-around -> split access */ + uint32_t amount = b->size - b->rp; + memcpy(buf, &b->buf[b->rp], amount); + memcpy(buf + amount, &b->buf[0], len - amount); + } else { + memcpy(buf, &b->buf[b->rp], len); + } + + /* update read pointer */ + b->rp = (b->rp + len) % b->size; + + return(len); +} + +int write_shm(void *hwbuf, void *buf, int len) +{ + hwbuf_t *b = (hwbuf_t*)hwbuf; + + /* block if necessary */ + while(b->size - level_shm(hwbuf) < len); + + /* copy data */ + if(b->wp + len > b->size) { + /* wrap-around -> split access */ + uint32_t amount = b->size - b->wp; + memcpy(&b->buf[b->wp], buf, amount); + memcpy(&b->buf[0], buf + amount, len - amount); + } else { + memcpy(&b->buf[b->wp], buf, len); + } + + /* update write pointer */ + b->wp = (b->wp + len) % b->size; + + return(len); +} +