uart, ioports, etc. Many more changes
[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 "simavr.h"
26
27 #define ARRAY_SIZE(_aa) (sizeof(_aa) / sizeof((_aa)[0]))
28
29 /*
30  * this 'structure' is a packed representation of an IO register 'bit'
31  * (or consecutive bits). This allows a way to set/get/clear them.
32  * gcc is happy passing these as register value, so you don't need to
33  * use a pointer when passing them along to functions.
34  */
35 typedef struct avr_regbit_t {
36         unsigned long reg : 8, bit : 3, mask : 8;
37 } avr_regbit_t;
38
39 /*
40  * These accessors are inlined and are used to perform the operations on
41  * avr_regbit_t definitions. This is the "official" way to access bits into registers
42  * The small footorint costs brings much better versatility for functions/bits that are
43  * not always defined in the same place on real AVR cores
44  */
45 /*
46  * set/get/clear io register bits in one operation
47  */
48 static inline uint8_t avr_regbit_set(avr_t * avr, avr_regbit_t rb)
49 {
50         uint8_t a = rb.reg;
51         if (!a)
52                 return 0;
53         uint8_t m = rb.mask << rb.bit;
54         avr_core_watch_write(avr, a, avr->data[a] | m);
55         return (avr->data[a] >> rb.bit) & rb.mask;
56 }
57
58 static inline uint8_t avr_regbit_setto(avr_t * avr, avr_regbit_t rb, uint8_t v)
59 {
60         uint8_t a = rb.reg;
61         if (!a)
62                 return 0;
63         uint8_t m = rb.mask << rb.bit;
64         avr_core_watch_write(avr, a, (avr->data[a] & ~(m)) | ((v << rb.bit) & m));
65         return (avr->data[a] >> rb.bit) & rb.mask;
66 }
67
68 static inline uint8_t avr_regbit_get(avr_t * avr, avr_regbit_t rb)
69 {
70         uint8_t a = rb.reg;
71         if (!a)
72                 return 0;
73         //uint8_t m = rb.mask << rb.bit;
74         return (avr->data[a] >> rb.bit) & rb.mask;
75 }
76
77 static inline uint8_t avr_regbit_clear(avr_t * avr, avr_regbit_t rb)
78 {
79         uint8_t a = (rb.reg);
80         uint8_t m = rb.mask << rb.bit;
81         avr_core_watch_write(avr, a, avr->data[a] & ~m);
82         return avr->data[a];
83 }
84
85
86 /*
87  * This reads the bits for an array of avr_regbit_t, make up a "byte" with them.
88  * This allows reading bits like CS0, CS1, CS2 etc even if they are not in the same
89  * physical IO register.
90  */
91 static inline uint8_t avr_regbit_get_array(avr_t * avr, avr_regbit_t *rb, int count)
92 {
93         uint8_t res = 0;
94
95         for (int i = 0; i < count; i++, rb++) if (rb->reg) {
96                 uint8_t a = (rb->reg);
97                 res |= ((avr->data[a] >> rb->bit) & rb->mask) << i;
98         }
99         return res;
100 }
101
102 #define AVR_IO_REGBIT(_io, _bit) { . reg = (_io), .bit = (_bit), .mask = 1 }
103 #define AVR_IO_REGBITS(_io, _bit, _mask) { . reg = (_io), .bit = (_bit), .mask = (_mask) }
104
105
106 #endif /* __SIM_REGBIT_H__ */