misc: Update NO_COLOR define switch
[simavr] / simavr / sim / sim_regbit.h
1 /*
2         sim_regbit.h
3
4         Copyright 2008, 2009 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 #ifndef __SIM_REGBIT_H__
23 #define __SIM_REGBIT_H__
24
25 #include "sim_avr.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #define ARRAY_SIZE(_aa) (sizeof(_aa) / sizeof((_aa)[0]))
32
33
34 /*
35  * These accessors are inlined and are used to perform the operations on
36  * avr_regbit_t definitions. This is the "official" way to access bits into registers
37  * The small footprint costs brings much better versatility for functions/bits that are
38  * not always defined in the same place on real AVR cores
39  */
40 /*
41  * set/get/clear io register bits in one operation
42  */
43 static inline uint8_t avr_regbit_set(avr_t * avr, avr_regbit_t rb)
44 {
45         uint8_t a = rb.reg;
46         if (!a)
47                 return 0;
48         uint8_t m = rb.mask << rb.bit;
49         avr_core_watch_write(avr, a, avr->data[a] | m);
50         return (avr->data[a] >> rb.bit) & rb.mask;
51 }
52
53 static inline uint8_t avr_regbit_setto(avr_t * avr, avr_regbit_t rb, uint8_t v)
54 {
55         uint8_t a = rb.reg;
56         if (!a)
57                 return 0;
58         uint8_t m = rb.mask << rb.bit;
59         avr_core_watch_write(avr, a, (avr->data[a] & ~(m)) | ((v << rb.bit) & m));
60         return (avr->data[a] >> rb.bit) & rb.mask;
61 }
62
63 /*
64  * Set the 'raw' bits, if 'v' is the unshifted value of the bits
65  */
66 static inline uint8_t avr_regbit_setto_raw(avr_t * avr, avr_regbit_t rb, uint8_t v)
67 {
68         uint8_t a = rb.reg;
69         if (!a)
70                 return 0;
71         uint8_t m = rb.mask << rb.bit;
72         avr_core_watch_write(avr, a, (avr->data[a] & ~(m)) | ((v) & m));
73         return (avr->data[a]) & (rb.mask << rb.bit);
74 }
75
76 static inline uint8_t avr_regbit_get(avr_t * avr, avr_regbit_t rb)
77 {
78         uint8_t a = rb.reg;
79         if (!a)
80                 return 0;
81         //uint8_t m = rb.mask << rb.bit;
82         return (avr->data[a] >> rb.bit) & rb.mask;
83 }
84
85 /*
86  * Return the bit(s) 'in position' instead of zero based
87  */
88 static inline uint8_t avr_regbit_get_raw(avr_t * avr, avr_regbit_t rb)
89 {
90         uint8_t a = rb.reg;
91         if (!a)
92                 return 0;
93         //uint8_t m = rb.mask << rb.bit;
94         return (avr->data[a]) & (rb.mask << rb.bit);
95 }
96
97 static inline uint8_t avr_regbit_clear(avr_t * avr, avr_regbit_t rb)
98 {
99         uint8_t a = (rb.reg);
100         uint8_t m = rb.mask << rb.bit;
101         avr_core_watch_write(avr, a, avr->data[a] & ~m);
102         return avr->data[a];
103 }
104
105
106 /*
107  * This reads the bits for an array of avr_regbit_t, make up a "byte" with them.
108  * This allows reading bits like CS0, CS1, CS2 etc even if they are not in the same
109  * physical IO register.
110  */
111 static inline uint8_t avr_regbit_get_array(avr_t * avr, avr_regbit_t *rb, int count)
112 {
113         uint8_t res = 0;
114
115         for (int i = 0; i < count; i++, rb++) if (rb->reg) {
116                 uint8_t a = (rb->reg);
117                 res |= ((avr->data[a] >> rb->bit) & rb->mask) << i;
118         }
119         return res;
120 }
121
122 #define AVR_IO_REGBIT(_io, _bit) { . reg = (_io), .bit = (_bit), .mask = 1 }
123 #define AVR_IO_REGBITS(_io, _bit, _mask) { . reg = (_io), .bit = (_bit), .mask = (_mask) }
124
125 #ifdef __cplusplus
126 };
127 #endif
128
129 #endif /* __SIM_REGBIT_H__ */