Subversion Repositories NaviCtrl

Rev

Rev 378 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 378 Rev 380
1
#include "fifo.h"
1
#include "fifo.h"
2
#include "main.h"
2
#include "main.h"
3
#include "uart1.h"
3
#include "uart1.h"
4
 
4
 
5
u8 fifo_init (fifo_t* f, u8* buffer, const u16 size, u16 putvicsource, u16 getvicsource)
5
u8 fifo_init (fifo_t* f, u8* buffer, const u16 size, u16 putvicsource, u16 getvicsource)
6
{
6
{
7
        if(f == NULL) return(0);
7
        if(f == NULL) return(0);
8
        f->buffer = buffer;
8
        f->buffer = buffer;
9
        f->size = size;
9
        f->size = size;
10
        f->putvicsource = putvicsource;
10
        f->putvicsource = putvicsource;
11
        f->getvicsource = getvicsource;
11
        f->getvicsource = getvicsource;
12
        fifo_purge(f);
12
        fifo_purge(f);
13
        return(1);
13
        return(1);
14
}
14
}
15
 
15
 
16
u8 fifo_put (fifo_t *f, const u8 data)
16
u8 fifo_put(fifo_t *f, const u8 data)
17
{
17
{
18
        if(f->buffer == 0) return(0);
18
        if(f->buffer == 0) return(0);
19
        if (f->count >= f->size) return(0);     // return 0 in case of FIFO overflow.
19
        if (f->count >= f->size) return(0);     // return 0 in case of FIFO overflow.
20
        if(f->putvicsource != NO_ITLine) VIC_ITCmd(f->putvicsource, DISABLE);
20
        if(f->putvicsource != NO_ITLine) VIC_ITCmd(f->putvicsource, DISABLE);
21
        *(f->pwrite++) = data;      // copy data byte to buffer
21
        *(f->pwrite++) = data;      // copy data byte to buffer
22
        if(f->pwrite >= f->buffer + f->size) f->pwrite = f->buffer; // start at the begining after reaching the end 
22
        if(f->pwrite >= f->buffer + f->size) f->pwrite = f->buffer; // start at the begining after reaching the end 
23
        f->count++;
23
        f->count++;
24
        if(f->putvicsource != NO_ITLine) VIC_ITCmd(f->putvicsource, ENABLE);
24
        if(f->putvicsource != NO_ITLine) VIC_ITCmd(f->putvicsource, ENABLE);
25
        return(1);
25
        return(1);
26
}
26
}
27
 
27
 
28
u8 fifo_get (fifo_t *f, u8 *pdata)
28
u8 fifo_get (fifo_t *f, u8 *pdata)
29
{
29
{
30
        if(f->buffer == 0) return(0);
30
        if(f->buffer == 0) return(0);
31
        if(!f->count) return(0);
31
        if(!f->count) return(0);
32
        if(f->getvicsource != NO_ITLine) VIC_ITCmd(f->getvicsource, DISABLE);
32
        if(f->getvicsource != NO_ITLine) VIC_ITCmd(f->getvicsource, DISABLE);
33
        *pdata = *(f->pread++);
33
        *pdata = *(f->pread++);
34
        if(f->pread >= f->buffer + f->size) f->pread = f->buffer; // start at the begining after reaching the end 
34
        if(f->pread >= f->buffer + f->size) f->pread = f->buffer; // start at the begining after reaching the end 
35
        f->count--;
35
        f->count--;
36
        if(f->getvicsource != NO_ITLine) VIC_ITCmd(f->getvicsource, ENABLE);
36
        if(f->getvicsource != NO_ITLine) VIC_ITCmd(f->getvicsource, ENABLE);
37
        return(1);
37
        return(1);
38
}
38
}
39
 
39
 
40
u8 fifo_get_wait (fifo_t *f, u8 *pdata)
40
u8 fifo_get_wait (fifo_t *f, u8 *pdata)
41
{
41
{
42
        while (!f->count);
42
        while (!f->count);
43
        return fifo_get(f, pdata);
43
        return fifo_get(f, pdata);
44
}
44
}
45
 
45
 
46
void fifo_purge(fifo_t* f)
46
void fifo_purge(fifo_t* f)
47
{
47
{
48
        if((f == NULL)) return;
48
        if((f == NULL)) return;
49
        f->count = 0;
49
        f->count = 0;
50
        f->pread = f->buffer;
50
        f->pread = f->buffer;
51
        f->pwrite = f->buffer;
51
        f->pwrite = f->buffer;
52
        return;
52
        return;
53
}
53
}
54
 
54