1 /** Operations with 1-bit sized bitfields
4 varType: 0, 1, 2, 3, 4, 5, 6
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 alack of memory, still.
11 // mcs51 fails tests. 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)
16 // These tests assume the bitfields are allocated in LSB to MSB order
17 // but some hosts allocate them in MSB to LSB order. Disable the
18 // tests on these hosts
19 #if defined(PORT_HOST) && (defined(__ppc__) || defined(__PPC__) || defined(__sparc) || defined(__sparc64__))
30 #define PRE_BITS ({preBits})
31 #define PATTERN ({pattern})
32 #define VAR_TYPE ({varType})
37 unsigned int preBits
: PRE_BITS
;
39 unsigned int bit0
: 1;
40 unsigned int bit1
: 1;
41 unsigned int bit2
: 1;
42 unsigned int bit3
: 1;
43 unsigned int bit4
: 1;
44 unsigned int bit5
: 1;
45 unsigned int bit6
: 1;
46 unsigned int bit7
: 1;
48 unsigned int postBits
: 8;
54 unsigned int preBits
: PRE_BITS
;
56 unsigned int bits
: 8;
57 unsigned int postBits
: 8;
61 #define bitToTest__(b) bit ## b
62 #define bitToTest_(b) bitToTest__(b)
63 #define bitToTest bitToTest_(BIT_TO_TEST)
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 struct_8bits volatileBits
;
90 #define volatileBits (*(volatile struct_8bits*)ABS_ADDR1)
96 #define volatileBits (*(volatile struct_8bits*)ABS_ADDR2)
101 #define VOLATILE_BITS_DEF volatile struct_8bits volatileBits
103 #define VOLATILE_BITS_DEF static volatile struct_8bits volatileBits
106 #define VOLATILE_BITS_DEF static volatile struct_8bits __at(ABS_ADDR1) volatileBits
112 #define VOLATILE_BITS_DEF static volatile struct_8bits __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
126 #define PRE_BIT_VALUE 0
127 #define POST_BIT_VALUE 0
129 #define PRE_BIT_VALUE ((1UL << PRE_BITS) - 1)
130 #define POST_BIT_VALUE 0xFF
140 // Return type of this function is uint16_t to trigger some peephole rules for STM8
142 bits_check_value(const struct_8bits
* const var
, const uint8_t value
)
145 if(var
->preBits
!= PRE_BIT_VALUE
) return 0;
147 if(var
->postBits
!= POST_BIT_VALUE
) return 0;
149 if(((struct_8bits_joined
*)var
)->bits
!= value
) return 0;
154 bits_set_value(struct_8bits
* const var
, const uint8_t value
)
157 var
->preBits
= PRE_BIT_VALUE
;
159 var
->postBits
= POST_BIT_VALUE
;
161 ((struct_8bits_joined
*)var
)->bits
= value
;
171 #define BIT_TO_TEST 0
173 bits_set_value(&volatileBits
, 0x00);
174 volatileBits
.bitToTest
= 1;
176 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
178 bits_set_value(&volatileBits
, 0xFF);
179 volatileBits
.bitToTest
= 0;
181 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
183 bits_set_value(&volatileBits
, 0x00);
184 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
186 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
188 bits_set_value(&volatileBits
, 0xFF);
189 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
191 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
193 bits_set_value(&volatileBits
, 0x00);
194 volatileBits
.bitToTest
^= 1;
196 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
198 bits_set_value(&volatileBits
, 0xFF);
199 volatileBits
.bitToTest
^= 1;
201 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
210 #define BIT_TO_TEST 1
212 bits_set_value(&volatileBits
, 0x00);
213 volatileBits
.bitToTest
= 1;
215 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
217 bits_set_value(&volatileBits
, 0xFF);
218 volatileBits
.bitToTest
= 0;
220 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
222 bits_set_value(&volatileBits
, 0x00);
223 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
225 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
227 bits_set_value(&volatileBits
, 0xFF);
228 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
230 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
232 bits_set_value(&volatileBits
, 0x00);
233 volatileBits
.bitToTest
^= 1;
235 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
237 bits_set_value(&volatileBits
, 0xFF);
238 volatileBits
.bitToTest
^= 1;
240 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
249 #define BIT_TO_TEST 2
251 bits_set_value(&volatileBits
, 0x00);
252 volatileBits
.bitToTest
= 1;
254 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
256 bits_set_value(&volatileBits
, 0xFF);
257 volatileBits
.bitToTest
= 0;
259 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
261 bits_set_value(&volatileBits
, 0x00);
262 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
264 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
266 bits_set_value(&volatileBits
, 0xFF);
267 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
269 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
271 bits_set_value(&volatileBits
, 0x00);
272 volatileBits
.bitToTest
^= 1;
274 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
276 bits_set_value(&volatileBits
, 0xFF);
277 volatileBits
.bitToTest
^= 1;
279 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
288 #define BIT_TO_TEST 3
290 bits_set_value(&volatileBits
, 0x00);
291 volatileBits
.bitToTest
= 1;
293 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
295 bits_set_value(&volatileBits
, 0xFF);
296 volatileBits
.bitToTest
= 0;
298 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
300 bits_set_value(&volatileBits
, 0x00);
301 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
303 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
305 bits_set_value(&volatileBits
, 0xFF);
306 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
308 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
310 bits_set_value(&volatileBits
, 0x00);
311 volatileBits
.bitToTest
^= 1;
313 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
315 bits_set_value(&volatileBits
, 0xFF);
316 volatileBits
.bitToTest
^= 1;
318 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
327 #define BIT_TO_TEST 4
329 bits_set_value(&volatileBits
, 0x00);
330 volatileBits
.bitToTest
= 1;
332 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
334 bits_set_value(&volatileBits
, 0xFF);
335 volatileBits
.bitToTest
= 0;
337 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
339 bits_set_value(&volatileBits
, 0x00);
340 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
342 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
344 bits_set_value(&volatileBits
, 0xFF);
345 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
347 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
349 bits_set_value(&volatileBits
, 0x00);
350 volatileBits
.bitToTest
^= 1;
352 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
354 bits_set_value(&volatileBits
, 0xFF);
355 volatileBits
.bitToTest
^= 1;
357 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
366 #define BIT_TO_TEST 5
368 bits_set_value(&volatileBits
, 0x00);
369 volatileBits
.bitToTest
= 1;
371 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
373 bits_set_value(&volatileBits
, 0xFF);
374 volatileBits
.bitToTest
= 0;
376 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
378 bits_set_value(&volatileBits
, 0x00);
379 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
381 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
383 bits_set_value(&volatileBits
, 0xFF);
384 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
386 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
388 bits_set_value(&volatileBits
, 0x00);
389 volatileBits
.bitToTest
^= 1;
391 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
393 bits_set_value(&volatileBits
, 0xFF);
394 volatileBits
.bitToTest
^= 1;
396 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
405 #define BIT_TO_TEST 6
407 bits_set_value(&volatileBits
, 0x00);
408 volatileBits
.bitToTest
= 1;
410 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
412 bits_set_value(&volatileBits
, 0xFF);
413 volatileBits
.bitToTest
= 0;
415 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
417 bits_set_value(&volatileBits
, 0x00);
418 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
420 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
422 bits_set_value(&volatileBits
, 0xFF);
423 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
425 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
427 bits_set_value(&volatileBits
, 0x00);
428 volatileBits
.bitToTest
^= 1;
430 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
432 bits_set_value(&volatileBits
, 0xFF);
433 volatileBits
.bitToTest
^= 1;
435 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
444 #define BIT_TO_TEST 7
446 bits_set_value(&volatileBits
, 0x00);
447 volatileBits
.bitToTest
= 1;
449 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
451 bits_set_value(&volatileBits
, 0xFF);
452 volatileBits
.bitToTest
= 0;
454 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
456 bits_set_value(&volatileBits
, 0x00);
457 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
459 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
461 bits_set_value(&volatileBits
, 0xFF);
462 volatileBits
.bitToTest
= ! volatileBits
.bitToTest
;
464 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));
466 bits_set_value(&volatileBits
, 0x00);
467 volatileBits
.bitToTest
^= 1;
469 ASSERT(bits_check_value(&volatileBits
, 1 << BIT_TO_TEST
));
471 bits_set_value(&volatileBits
, 0xFF);
472 volatileBits
.bitToTest
^= 1;
474 ASSERT(bits_check_value(&volatileBits
, ~(1 << BIT_TO_TEST
)));