44deb390286a881d9d2808e7d012a0f38f33d515
[simavr] / simavr / sim / fifo_declare.h
1 /*
2         fifo_declare.h
3
4         Copyright 2008, 2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /* Licensed to Vidiactive by Michel Pollet under the terms of the VECL */
23
24 /*
25  * FIFO helpers, aka circular buffers
26  *
27  * these macros define accessories for FIFOs of any name, type and
28  * any (power of two) size
29  */
30
31 #ifndef __FIFO_DECLARE__
32 #define __FIFO_DECLARE__
33
34 /*
35         doing a :
36         DECLARE_FIFO(uint8_t, myfifo, 128);
37
38         will declare :
39         enum : myfifo_overflow_f
40         type : myfifo_t
41         functions:
42                 // write a byte into the fifo, return 1 if there was room, 0 if there wasn't
43                 int myfifo_write(myfifo_t *c, uint8_t b);
44                 // reads a byte from the fifo, return 0 if empty. Use myfifo_isempty() to check beforehand
45                 uint8_t myfifo_read(myfifo_t *c);
46                 int myfifo_isfull(myfifo_t *c);
47                 int myfifo_isempty(myfifo_t *c);
48                 // returns number of items to read now
49                 uint16_t myfifo_get_read_size(myfifo_t *c);
50                 // read item at offset o from read cursor, no cursor advance
51                 uint8_t myfifo_read_at(myfifo_t *c, uint16_t o);
52                 // write b at offset o compared to current write cursor, no cursor advance
53                 void myfifo_write_at(myfifo_t *c, uint16_t o, uint8_t b);
54
55         In your .c you need to 'implement' the fifo:
56         DEFINE_FIFO(uint8_t, myfifo)
57
58         To use the fifo, you must declare at least one :
59         myfifo_t fifo = FIFO_NULL;
60
61         while (!myfifo_isfull(&fifo))
62                 myfifo_write(&fifo, 0xaa);
63         ....
64         while (!myfifo_isempty(&fifo))
65                 b = myfifo_read(&fifo);
66  */
67
68 #include <stdint.h>
69
70 #if __AVR__
71 #define FIFO_CURSOR_TYPE        uint8_t
72 #define FIFO_BOOL_TYPE  char
73 #define FIFO_INLINE
74 #endif
75 #ifndef FIFO_CURSOR_TYPE
76 #define FIFO_CURSOR_TYPE        uint16_t
77 #endif
78 #ifndef FIFO_BOOL_TYPE
79 #define FIFO_BOOL_TYPE  int
80 #endif
81 #ifndef FIFO_INLINE
82 #define FIFO_INLINE     inline
83 #endif
84
85 #ifndef FIFO_ZERO_INIT
86 #define FIFO_ZERO_INIT {0}
87 #endif
88 #define FIFO_NULL { FIFO_ZERO_INIT, 0, 0, 0 }
89
90 #define DECLARE_FIFO(__type, __name, __size) \
91 enum { __name##_overflow_f = (1 << 0) }; \
92 enum { __name##_fifo_size = (__size) }; \
93 typedef struct __name##_t {                     \
94         __type          buffer[__name##_fifo_size];             \
95         volatile FIFO_CURSOR_TYPE       read;           \
96         volatile FIFO_CURSOR_TYPE       write;          \
97         volatile uint8_t        flags;          \
98 } __name##_t
99
100 #define DEFINE_FIFO(__type, __name) \
101 static FIFO_INLINE FIFO_BOOL_TYPE __name##_write(__name##_t * c, __type b)\
102 {\
103         FIFO_CURSOR_TYPE now = c->write;\
104         FIFO_CURSOR_TYPE next = (now + 1) & (__name##_fifo_size-1);\
105         if (c->read != next) {  \
106                 c->buffer[now] = b;\
107                 c->write = next;\
108                 return 1;\
109         }\
110         return 0;\
111 }\
112 static FIFO_INLINE FIFO_BOOL_TYPE __name##_isfull(__name##_t *c)\
113 {\
114         FIFO_CURSOR_TYPE next = (c->write + 1) & (__name##_fifo_size-1);\
115         return c->read == next;\
116 }\
117 static FIFO_INLINE FIFO_BOOL_TYPE __name##_isempty(__name##_t * c)\
118 {\
119         return c->read == c->write;\
120 }\
121 static FIFO_INLINE __type __name##_read(__name##_t * c)\
122 {\
123         __type res = FIFO_ZERO_INIT; \
124         if (c->read == c->write)\
125                 return res;\
126         FIFO_CURSOR_TYPE read = c->read;\
127         res = c->buffer[read];\
128         c->read = (read + 1) & (__name##_fifo_size-1);\
129         return res;\
130 }\
131 static FIFO_INLINE FIFO_CURSOR_TYPE __name##_get_read_size(__name##_t *c)\
132 {\
133         return c->write > c->read ? c->write - c->read : __name##_fifo_size - 1 - c->read + c->write;\
134 }\
135 static FIFO_INLINE void __name##_read_offset(__name##_t *c, FIFO_CURSOR_TYPE o)\
136 {\
137         c->read = (c->read + o) & (__name##_fifo_size-1);\
138 }\
139 static FIFO_INLINE __type __name##_read_at(__name##_t *c, FIFO_CURSOR_TYPE o)\
140 {\
141         return c->buffer[(c->read + o) & (__name##_fifo_size-1)];\
142 }\
143 static FIFO_INLINE void __name##_write_at(__name##_t *c, FIFO_CURSOR_TYPE o, __type b)\
144 {\
145         c->buffer[(c->write + o) & (__name##_fifo_size-1)] = b;\
146 }\
147 static FIFO_INLINE void __name##_write_offset(__name##_t *c, FIFO_CURSOR_TYPE o)\
148 {\
149         c->write = (c->write + o) & (__name##_fifo_size-1);\
150 }\
151 static FIFO_INLINE void __name##_reset(__name##_t *c)\
152 {\
153         c->read = c->write = c->flags = 0;\
154 }\
155 struct __name##_t
156
157 #endif