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