OpenCores
URL https://opencores.org/ocsvn/s6soc/s6soc/trunk

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [zipos/] [syspipe.c] - Diff between revs 22 and 27

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 22 Rev 27
Line 49... Line 49...
 
 
#ifndef NULL
#ifndef NULL
#define NULL    (void *)0
#define NULL    (void *)0
#endif
#endif
 
 
static  void    clear_syspipe(SYSPIPE *p) {
 
        p->m_head  = 0;
 
        p->m_tail  = 0;
 
        p->m_error = 0;
 
 
 
        for(int i=0; i<=(int)p->m_mask; i++)
 
                p->m_buf[i] = 0;
 
 
 
        if ((p->m_rdtask)&&(p->m_rdtask != INTERRUPT_READ_TASK)) {
 
                p->m_rdtask->context[1] = p->m_nread;
 
                if (p->m_nread == 0)
 
                        p->m_rdtask->errno = -EFAULT;
 
                p->m_rdtask->state = SCHED_READY;
 
        } else if (p->m_wrtask) {
 
                p->m_wrtask->context[1] = p->m_nwritten;
 
                if (p->m_nwritten == 0)
 
                        p->m_wrtask->errno = -EFAULT;
 
                p->m_wrtask->state = SCHED_READY;
 
        }
 
 
 
        if (p->m_rdtask != INTERRUPT_READ_TASK)
 
                p->m_rdtask   = 0;
 
        p->m_wrtask   = 0;
 
        p->m_nread    = 0;
 
        p->m_nwritten = 0;
 
}
 
 
 
void    kpush_syspipe(SYSPIPE *pipe, int val) {
void    kpush_syspipe(SYSPIPE *pipe, int val) {
        int     tst = (pipe->m_head+1)&pipe->m_mask;
        int     tst = (pipe->m_head+1)&pipe->m_mask;
        if (tst != pipe->m_tail) {
        if (tst != pipe->m_tail) {
                pipe->m_buf[pipe->m_head] = val;
                pipe->m_buf[pipe->m_head] = val;
                pipe->m_head = tst;             // Increment the head pointer
                pipe->m_head = tst;             // Increment the head pointer
                if ((pipe->m_rdtask)&&(pipe->m_rdtask != INTERRUPT_READ_TASK))
                if ((pipe->m_rdtask)&&(pipe->m_rdtask != INTERRUPT_READ_TASK))
                        pipe->m_rdtask->state = SCHED_READY;
                        pipe->m_rdtask->state = SCHED_READY;
        } else pipe->m_error = 1;
        } else pipe->m_error = 1;
}
}
 
 
void    txchr(char v) {
extern  void    pipe_panic(SYSPIPE *p);
        volatile IOSPACE        *sys = (IOSPACE *)IOADDR;
 
        if (v < 10)
 
                return;
 
        v &= 0x0ff;
 
        sys->io_pic = INT_UARTTX;
 
        while((sys->io_pic&INT_UARTTX)==0)
 
                ;
 
        sys->io_uart = v;
 
}
 
 
 
void    txstr(const char *str) {
 
        const char *ptr = str;
 
        while(*ptr) {
 
                txchr(*ptr++);
 
        }
 
}
 
 
 
void    txhex(int num) {
 
        for(int ds=28; ds>=0; ds-=4) {
 
                int     ch;
 
                ch = (num>>ds)&0x0f;
 
                if (ch >= 10)
 
                        ch = 'A'+ch-10;
 
                else
 
                        ch += '0';
 
                txchr(ch);
 
        } txstr("\r\n");
 
}
 
 
 
void    pipe_panic(SYSPIPE *pipe) {
 
        extern void     kpanic(void);
 
        volatile IOSPACE        *sys = (IOSPACE *)IOADDR;
 
 
 
        sys->io_spio = 0x0fa;
 
 
 
        txstr("SYSPIPE PANIC!\r\n");
 
        txstr("ADDR: "); txhex((int)pipe);
 
        txstr("MASK: "); txhex(pipe->m_mask);
 
        txstr("HEAD: "); txhex(pipe->m_head);
 
        txstr("TAIL: "); txhex(pipe->m_tail);
 
        kpanic();
 
}
 
 
 
int     kpop_syspipe(SYSPIPE *pipe, int *vl) {
int     kpop_syspipe(SYSPIPE *pipe, int *vl) {
        if (pipe->m_head != pipe->m_tail) {
        if (pipe->m_head != pipe->m_tail) {
                *vl = pipe->m_buf[pipe->m_tail];
                *vl = pipe->m_buf[pipe->m_tail];
                pipe->m_tail++;
                pipe->m_tail = (pipe->m_tail+1)&pipe->m_mask;
                if ((unsigned)pipe->m_tail > pipe->m_mask)
 
                        pipe->m_tail = 0;
 
                if (pipe->m_wrtask)
                if (pipe->m_wrtask)
                        pipe->m_wrtask->state = SCHED_READY;
                        pipe->m_wrtask->state = SCHED_READY;
                return 0;
                return 0;
        } return 1; // Error condition
 
}
}
 
        return 1; // Error condition
SYSPIPE *new_syspipe(const unsigned int len) {
 
        unsigned        msk;
 
 
 
        for(msk=2; msk<len; msk<<=1)
 
                ;
 
        SYSPIPE *pipe = sys_malloc(sizeof(SYSPIPE)-1+msk);
 
        pipe->m_mask = msk-1;
 
        pipe->m_rdtask = pipe->m_wrtask = 0;
 
        clear_syspipe(pipe);
 
        return pipe;
 
}
}
 
 
int     len_syspipe(SYSPIPE *p) {
/* Returns how many values are in the pipe
        return (p->m_head-p->m_tail) & p->m_mask;
 */
}
/* Of course ... if it's not used, why include it?
 
int     len_syspipe(SYSPIPE *p) {
 
        return (p->m_head-p->m_tail) & p->m_mask;
 
} */
 
/* Returns how many empty spaces are in the pipe
 
 */
int     num_avail_syspipe(SYSPIPE *p) {
int     num_avail_syspipe(SYSPIPE *p) {
        return (p->m_mask + p->m_tail-p->m_head) & p->m_mask;
        // if (head+1 == tail)  PIPE is full
 
        //      (mask+tail-tail+1)=mask+1 &mask = 0
 
        // if (head == tail) PIPE is empty
 
        //      (mask+tail-tail)=mask & mask = mask
 
        // if (head == tail+2) PIPE has two within it
 
        //      (mask+tail-tail-2)=mask-2 & mask = mask-2
 
        //
 
        return (p->m_tail-p->m_head-1) & p->m_mask;
}
}
 
 
// This will be called from a user context.
// This will be called from a user context.
// Another task may write to the pipe during this call.  If the pipe becomes
// Another task may write to the pipe during this call.  If the pipe becomes
// full, that task will block.
// full, that task will block.
Line 191... Line 120...
                                for(int i=0; i<ln1; i++)
                                for(int i=0; i<ln1; i++)
                                        *dst++ = *src++;
                                        *dst++ = *src++;
 
 
                                p->m_nread += ln1;
                                p->m_nread += ln1;
                                nleft -= ln1;
                                nleft -= ln1;
                                p->m_tail += ln1;
 
                                if ((unsigned)p->m_tail > p->m_mask)
                                int nt = p->m_tail+ln1;
                                        p->m_tail = 0;
                                if ((unsigned)nt > p->m_mask)
 
                                        nt = 0;
 
                                p->m_tail = nt;
                        }
                        }
 
 
                        // nleft is either zero, or tail
                        // nleft is either zero, or tail
                        if (nleft & -2)
                        if (nleft & -2)
                                exit(nleft);
                                exit(nleft);
Line 217... Line 148...
                        for(int i=0; i<ln1; i++)
                        for(int i=0; i<ln1; i++)
                                *dst++ = *src++;
                                *dst++ = *src++;
 
 
                        p->m_nread += ln1;
                        p->m_nread += ln1;
                        nleft -= ln1;
                        nleft -= ln1;
                        p->m_tail += ln1;
                        int nt = p->m_tail+ln1; // nt = new tail value
                        if (p->m_tail == (int)p->m_mask+1)
                        if ((unsigned)nt > p->m_mask)
                                p->m_tail = 0;
                                nt = 0;
 
                        p->m_tail = nt;
 
 
                        if (nleft & -2)
                        if (nleft & -2)
                                exit(nleft);
                                exit(nleft);
                        else if (p->m_nread & -2)
                        else if (p->m_nread & -2)
                                exit(p->m_nread);
                                exit(p->m_nread);
Line 309... Line 241...
        // We have accomplished our read
        // We have accomplished our read
        //
        //
        return len;
        return len;
}
}
 
 
static int      uwrite_syspipe(TASKP tsk __attribute__((__unused__)), SYSPIPE *p, int *src, int len) {
static int      uwrite_syspipe(TASKP tsk __attribute__((__unused__)),
 
                SYSPIPE *p, int *src, int len) {
        int nleft = len;
        int nleft = len;
 
 
        // The kernel guarantees, before we come into here, that we have a 
        // The kernel guarantees, before we come into here, that we have a 
        // valid write request.  
        // valid write request.  
        do {
        do {
Line 325... Line 258...
                // If there is a read task blocked, the pipe must be empty
                // If there is a read task blocked, the pipe must be empty
                TASKP rdtask = ((volatile SYSPIPE *)p)->m_rdtask;
                TASKP rdtask = ((volatile SYSPIPE *)p)->m_rdtask;
                if (rdtask == INTERRUPT_READ_TASK) {
                if (rdtask == INTERRUPT_READ_TASK) {
                        // We need to copy everything to the buffer
                        // We need to copy everything to the buffer
                } else if (rdtask) {
                } else if (rdtask) {
// #warning "The previous code should have worked"
 
                // if (((unsigned)rdtask+1) & -2)
 
                        int ln = nleft;
                        int ln = nleft;
                        if (ln > p->m_rdtask->context[4])
                        if (ln > p->m_rdtask->context[4])
                                ln = p->m_rdtask->context[4];
                                ln = p->m_rdtask->context[4];
                        int *dst = (int *)p->m_rdtask->context[3];
                        int *dst = (int *)p->m_rdtask->context[3];
                        for(int i=0; i<ln; i++)
                        for(int i=0; i<ln; i++)
Line 369... Line 300...
                        if (nleft == 0)
                        if (nleft == 0)
                                break;
                                break;
                }
                }
 
 
                // Copy whatever we have into the pipe's buffer
                // Copy whatever we have into the pipe's buffer
                if ((nleft <= num_avail_syspipe(p))||(rdtask == INTERRUPT_READ_TASK)) {
                int     navail = num_avail_syspipe(p);
 
                if ((nleft <= navail)
 
                        ||((rdtask == INTERRUPT_READ_TASK)&&(navail>0))) {
                        // Either there is no immediate reader task, or
                        // Either there is no immediate reader task, or
                        // the reader has been exhausted, but we've go
                        // the reader has been exhausted, but we've go
                        // more to write.
                        // more to write.
                        //
                        //
                        // Note that we no longer need to check what
                        // Note that we no longer need to check what
                        // will fit into the pipe.  We know the entire
                        // will fit into the pipe.  We know the entire
                        // rest of our buffer will fit.
                        // rest of our buffer will fit.
 
 
                        { // Write into the first half of the pipe
                        { // Write into the first half of the pipe
 
                        // Be careful not to change head until all is written
 
                        // so that it remains consistent under interrupt
 
                        // conditions.
                                int ln = p->m_mask+1-p->m_head;
                                int ln = p->m_mask+1-p->m_head;
                                int *dst = &p->m_buf[p->m_head];
                                int *dst = &p->m_buf[p->m_head];
                                if (ln > nleft) ln = nleft;
                                if (ln > nleft) ln = nleft;
 
                                if (ln > navail) ln = navail;
 
 
                                for(int i=0; i<ln; i++)
                                for(int i=0; i<ln; i++)
                                        *dst++ = *src++;
                                        *dst++ = *src++;
 
 
                                p->m_head += ln;
                                p->m_head = (p->m_head+ln)&p->m_mask;
                                nleft -= ln;
                                nleft -= ln;
                                p->m_nwritten += ln;
                                p->m_nwritten += ln;
                                if (p->m_head > (int)p->m_mask)
                                navail -= ln;
                                        p->m_head = 0;
 
                        }
                        }
 
 
                        // Write into the rest of the pipe
                        /*
                        if (nleft > 0) {
                        // Write into the rest of the pipe
                                int ln = num_avail_syspipe(p);
                        if ((0 == p->m_head)&&(nleft>0)&&(navail>0)) {
                                if (nleft < ln)
                                int ln = navail;
                                        ln = nleft;
                                if (nleft < ln)
                                int *dst = &p->m_buf[p->m_head];
                                        ln = nleft;
 
                                int *dst = &p->m_buf[p->m_head];
 
 
                                for(int i=0; i<ln; i++)
                                for(int i=0; i<ln; i++)
                                        *dst++ = *src++;
                                        *dst++ = *src++;
 
 
                                p->m_head += ln;
                                p->m_head += ln;
                                p->m_nwritten += ln;
                                p->m_nwritten += ln;
                                nleft -= ln;
                                nleft -= ln;
                        }
                        }*/
                }
                }
 
 
                if (nleft > 0) {
                if ((nleft > 0)&&(navail == 0)) {
                        if (rdtask == INTERRUPT_READ_TASK) {
                        if (rdtask == INTERRUPT_READ_TASK) {
                                DISABLE_INTS();
                                DISABLE_INTS();
                                if (num_avail_syspipe(p)==0)
                                if (0==num_avail_syspipe(p))
                                        wait(0,-1);
                                        wait(0,-1);
                                else ENABLE_INTS();
                                else ENABLE_INTS();
                        } else {
                        } else {
                                DISABLE_INTS();
                                DISABLE_INTS();
                                if (!((volatile SYSPIPE *)p)->m_rdtask)
                                if (!((volatile SYSPIPE *)p)->m_rdtask)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.