1 /** Test for checks of bitfields or bits in a byte.
3 varType: 0, 1, 2, 3, 4, 5, 6
4 bit: 0, 1, 2, 3, 4, 5, 6, 7
7 #pragma disable_warning 88
9 // Disable for ds390: bug #3211
10 // Absolute addressing has some issues for pdk. And if those are fixed, there might be a lack of memory, still.
11 // mcs51 creates invalid assembler. Don't know if that is a bug or just a bad choice for ABS_ADDR1 and ABS_ADDR2 below.
12 #if defined(__SDCC_ds390) || defined(__SDCC_pdk14) || defined(__SDCC_pdk15) || defined(__SDCC_mcs51)
23 #define VAR_TYPE ({varType})
24 #define BIT_TO_TEST {bit}
25 #define TYPE_IS_STRUCT ({struct})
30 // Use MSB to LSB order for hosts that use this order for bitfields
31 #if defined(PORT_HOST) && (defined(__ppc__) || defined(__PPC__) || defined(__sparc) || defined(__sparc64__))
32 unsigned int bit7
: 1;
33 unsigned int bit6
: 1;
34 unsigned int bit5
: 1;
35 unsigned int bit4
: 1;
36 unsigned int bit3
: 1;
37 unsigned int bit2
: 1;
38 unsigned int bit1
: 1;
39 unsigned int bit0
: 1;
41 // Use LSB to MSB order for SDCC and generic hosts (likely to be
42 // x86 or other little endian)
43 unsigned int bit0
: 1;
44 unsigned int bit1
: 1;
45 unsigned int bit2
: 1;
46 unsigned int bit3
: 1;
47 unsigned int bit4
: 1;
48 unsigned int bit5
: 1;
49 unsigned int bit6
: 1;
50 unsigned int bit7
: 1;
54 #define bitToTest__(b) bit ## b
55 #define bitToTest_(b) bitToTest__(b)
57 #define bitToTest bitToTest_(BIT_TO_TEST)
60 #define TYPE struct_8bits
65 #if defined(__SDCC_pic16)
66 #define ABS_ADDR1 0x0200
67 #define ABS_ADDR2 0x0204
68 #elif defined(__SDCC_pic14)
69 #define ABS_ADDR1 0x0100
70 #define ABS_ADDR2 0x0104
71 #elif defined(__SDCC_stm8)
72 #define ABS_ADDR1 0x1000
73 #define ABS_ADDR2 0x1004
74 #elif defined(__SDCC_f8)
75 #define ABS_ADDR1 0x3000
76 #define ABS_ADDR2 0x3004
78 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // TODO: Make test suitable for pdk
79 #if !defined(PORT_HOST) // Never do absolute address test with host
80 #define ABS_ADDR1 0xCA00
81 #define ABS_ADDR2 0xCA04
87 volatile TYPE volatileBits
;
90 #define volatileBits (*(volatile TYPE*)ABS_ADDR1)
96 #define volatileBits (*(volatile TYPE*)ABS_ADDR2)
101 #define VOLATILE_BITS_DEF volatile TYPE volatileBits
103 #define VOLATILE_BITS_DEF static volatile TYPE volatileBits
106 #define VOLATILE_BITS_DEF static volatile TYPE __at(ABS_ADDR1) volatileBits
112 #define VOLATILE_BITS_DEF static volatile TYPE __at(ABS_ADDR2) volatileBits
113 #define USE_ONLY_1_BYTE
118 #error "Unknown VAR_TYPE case"
121 #ifndef VOLATILE_BITS_DEF
122 #define VOLATILE_BITS_DEF
129 #define AS_UINT8(x) (*(uint8_t *)&x)
134 volatile uint8_t dummy
;
136 AS_UINT8(volatileBits
) = 1 << BIT_TO_TEST
;
137 ASSERT(AS_UINT8(volatileBits
) == (1 << BIT_TO_TEST
));
138 ASSERT(volatileBits
.bitToTest
);
140 if(volatileBits
.bitToTest
) dummy
= 1;
143 AS_UINT8(volatileBits
) = ~(1 << BIT_TO_TEST
);
144 ASSERT(AS_UINT8(volatileBits
) == (uint8_t)(~(1 << BIT_TO_TEST
)));
145 ASSERT(!volatileBits
.bitToTest
);
147 if(!volatileBits
.bitToTest
) dummy
= 1;
150 AS_UINT8(volatileBits
) = 0x00;
151 volatileBits
.bitToTest
= 1;
152 ASSERT(AS_UINT8(volatileBits
) == (1 << BIT_TO_TEST
));
154 AS_UINT8(volatileBits
) = 0xFF;
155 volatileBits
.bitToTest
= 1;
156 ASSERT(AS_UINT8(volatileBits
) == 0xFF);
158 AS_UINT8(volatileBits
) = 0x00;
159 volatileBits
.bitToTest
= 0;
160 ASSERT(AS_UINT8(volatileBits
) == 0x00);
162 AS_UINT8(volatileBits
) = 0xFF;
163 volatileBits
.bitToTest
= 0;
164 ASSERT(AS_UINT8(volatileBits
) == (uint8_t)(~(1 << BIT_TO_TEST
)));
166 AS_UINT8(volatileBits
) = 0x00;
167 volatileBits
.bitToTest
^= 1;
168 ASSERT(AS_UINT8(volatileBits
) == (1 << BIT_TO_TEST
));
170 AS_UINT8(volatileBits
) = 0xFF;
171 volatileBits
.bitToTest
^= 1;
172 ASSERT(AS_UINT8(volatileBits
) == (uint8_t)(~(1 << BIT_TO_TEST
)));
174 AS_UINT8(volatileBits
) = 0x00; // keep it to keep environment of all assert calls the same way
181 volatile uint8_t dummy
;
183 // Casts to int16_t are used because the code generator generates different code
184 // and as a result also different peephole rules are applied
186 volatileBits
= 1 << BIT_TO_TEST
;
187 ASSERT(volatileBits
& (1 << BIT_TO_TEST
));
188 ASSERT(volatileBits
& (int16_t)(1 << BIT_TO_TEST
));
190 if(volatileBits
& (int16_t)(1 << BIT_TO_TEST
)) dummy
= 1;
193 volatileBits
= ~(1 << BIT_TO_TEST
);
194 ASSERT(!(volatileBits
& (1 << BIT_TO_TEST
)));
195 ASSERT(!(volatileBits
& (int16_t)(1 << BIT_TO_TEST
)));
197 if(!(volatileBits
& (int16_t)(1 << BIT_TO_TEST
))) dummy
= 1;
201 volatileBits
|= 1 << BIT_TO_TEST
;
202 ASSERT(volatileBits
== (1 << BIT_TO_TEST
));
205 volatileBits
|= 1 << BIT_TO_TEST
;
206 ASSERT(volatileBits
== 0xFF);
209 volatileBits
&= (uint8_t)(~(1 << BIT_TO_TEST
));
210 ASSERT(volatileBits
== 0x00);
213 volatileBits
&= (uint8_t)(~(1 << BIT_TO_TEST
));
214 ASSERT(volatileBits
== (uint8_t)(~(1 << BIT_TO_TEST
)));
217 volatileBits
^= 1 << BIT_TO_TEST
;
218 ASSERT(volatileBits
== (1 << BIT_TO_TEST
));
221 volatileBits
^= 1 << BIT_TO_TEST
;
222 ASSERT(volatileBits
== (uint8_t)(~(1 << BIT_TO_TEST
)));
224 volatileBits
= 0x00; // keep it to keep environment of all assert calls the same way
231 testBitfieldsChecks(void)