struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bitfields-nibbles.c.in
blob6ea1d206ab7908cb0a68bd4853af37dfd3753ef7
1 /** Operations with 4bit-sized bitfields
2 operation: 0, 1, 2, 3, 4
3 pattern: 0, 1
4 varType: 0, 1, 2, 3, 4, 5, 6
5 */
7 // Uncomment this line to run the full test
8 //#define FULL_TEST
10 #pragma disable_warning 88 // cast of LITERAL value to 'generic' pointer
12 // Absolute addressing has some issues for pdk. And if those are fixed, there might be a lack of memory, still.
13 #if defined(__SDCC_pdk14) || defined(__SDCC_pdk15)
14 #define DUMMY_CASE
15 #endif
17 #include <testfwk.h>
18 #ifdef __sun__
19 #include <inttypes.h>
20 #else
21 #include <stdint.h>
22 #endif
24 #include <stdbool.h>
26 #define OPERATION ({operation})
27 #define PATTERN ({pattern})
28 #define VAR_TYPE ({varType})
30 #define OPERATION_ASSING 0
31 #define OPERATION_OR 1
32 #define OPERATION_AND 2
33 #define OPERATION_XOR 3
34 #define OPERATION_COM 4
36 typedef struct
38 unsigned int low : 4;
39 unsigned int high : 4;
40 } struct_nibbles;
42 #if defined(__SDCC_pic16)
43 #define ABS_ADDR1 0x0200
44 #define ABS_ADDR2 0x0204
45 #elif defined(__SDCC_pic14)
46 #define ABS_ADDR1 0x0100
47 #define ABS_ADDR2 0x0104
48 #elif defined(__SDCC_stm8)
49 #define ABS_ADDR1 0x1000
50 #define ABS_ADDR2 0x1004
51 #elif defined(__SDCC_f8)
52 #define ABS_ADDR1 0x3000
53 #define ABS_ADDR2 0x3004
54 #else
55 // TODO: Make test suitable for pdk
56 // mcs51 creates invalid assembler. Don't know if that is a bug or just a bad choice for ABS_ADDR1 and ABS_ADDR2 below.
57 // Disable for ds390: bug #3211
58 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) && !defined(__SDCC_mcs51) && !defined(__SDCC_ds390)
59 #if !defined(PORT_HOST) // Never do absolute address test with host
60 #define ABS_ADDR1 0xCA00
61 #define ABS_ADDR2 0xCA04
62 #endif
63 #endif
64 #endif
66 #if VAR_TYPE == 0
67 volatile struct_nibbles volatileBits;
68 #elif VAR_TYPE == 1
69 #ifdef ABS_ADDR1
70 #define volatileBits (*(volatile struct_nibbles*)ABS_ADDR1)
71 #else
72 #define DUMMY_CASE
73 #endif
74 #elif VAR_TYPE == 2
75 #ifdef ABS_ADDR2
76 #define volatileBits (*(volatile struct_nibbles*)ABS_ADDR2)
77 #else
78 #define DUMMY_CASE
79 #endif
80 #elif VAR_TYPE == 3
81 #define VOLATILE_BITS_DEF volatile struct_nibbles volatileBits
82 #elif VAR_TYPE == 4
83 #define VOLATILE_BITS_DEF static volatile struct_nibbles volatileBits
84 #elif VAR_TYPE == 5
85 #ifdef ABS_ADDR1
86 #define VOLATILE_BITS_DEF static volatile struct_nibbles __at(ABS_ADDR1) volatileBits
87 #else
88 #define DUMMY_CASE
89 #endif
90 #elif VAR_TYPE == 6
91 #ifdef ABS_ADDR2
92 #define VOLATILE_BITS_DEF static volatile struct_nibbles __at(ABS_ADDR2) volatileBits
93 #define USE_ONLY_1_BYTE
94 #else
95 #define DUMMY_CASE
96 #endif
97 #else
98 #error "Unknown VAR_TYPE case"
99 #endif
101 #ifndef VOLATILE_BITS_DEF
102 #define VOLATILE_BITS_DEF
103 #endif
106 #if PATTERN == 0
107 #define OTHER_FIELD_VALUE 0x00
108 #else
109 #define OTHER_FIELD_VALUE 0x0F
110 #if OPERATION == OPERATION_COM
111 #ifndef DUMMY_CASE
112 // No need to test case, because we have only one case for complement operation
113 #define DUMMY_CASE
114 #endif
115 #endif
116 #endif
118 #define OTHER_FIELD_high low
119 #define OTHER_FIELD_low high
121 #define OTHER_FIELD_(x) OTHER_FIELD_##x
122 #define OTHER_FIELD(x) OTHER_FIELD_(x)
125 #ifndef DUMMY_CASE
127 static bool success = false;
129 #if OPERATION == OPERATION_ASSING
130 #define OP
131 #define TEST(field, value) \
132 success = true; \
133 volatileBits.OTHER_FIELD(field) = OTHER_FIELD_VALUE; \
134 volatileBits.field = value; \
135 if((volatileBits.field != (value & 0x0F)) || (volatileBits.OTHER_FIELD(field) != OTHER_FIELD_VALUE)) success = false; \
136 ASSERT(success)
137 #elif OPERATION == OPERATION_OR
138 #define OP |
139 #elif OPERATION == OPERATION_AND
140 #define OP &
141 #elif OPERATION == OPERATION_XOR
142 #define OP ^
143 #elif OPERATION == OPERATION_COM
144 #define OP ~
145 #define TEST(valueLow, valueHigh) \
146 volatileBits.low = valueLow; \
147 volatileBits.high = valueHigh; \
149 volatileBits.low = ~volatileBits.low; \
150 ASSERT(volatileBits.low == ((~valueLow) & 0x0F)); \
151 ASSERT(volatileBits.high == (valueHigh & 0x0F)); \
153 volatileBits.high = ~volatileBits.high; \
154 ASSERT(volatileBits.low == ((~valueLow) & 0x0F)); \
155 ASSERT(volatileBits.high == ((~valueHigh) & 0x0F))
157 #else
158 #error "Unknown OPERATION case"
159 #endif
161 #ifndef TEST // GENERIC CASE FOR 'AND', 'OR' & 'XOR'
162 #define OP_EQUALS__(x) x##=
163 #define OP_EQUALS_(x) OP_EQUALS__(x)
164 #define OP_EQUALS OP_EQUALS_(OP)
166 #define TEST(field, value) \
167 success = true; \
168 volatileBits.field = 0x00; \
169 volatileBits.OTHER_FIELD(field) = OTHER_FIELD_VALUE; \
170 if((volatileBits.field != 0x00) || (volatileBits.OTHER_FIELD(field) != OTHER_FIELD_VALUE)) success = false; \
172 volatileBits.field OP_EQUALS value; \
173 if((volatileBits.field != ((value OP 0x00) & 0x0F)) || (volatileBits.OTHER_FIELD(field) != OTHER_FIELD_VALUE)) success = false; \
175 volatileBits.field = 0x0F; \
176 if((volatileBits.field != 0x0F) || (volatileBits.OTHER_FIELD(field) != OTHER_FIELD_VALUE)) success = false; \
178 volatileBits.field OP_EQUALS value; \
179 if((volatileBits.field != ((value OP 0x0F) & 0x0F)) || (volatileBits.OTHER_FIELD(field) != OTHER_FIELD_VALUE)) success = false; \
180 ASSERT(success)
181 #endif
183 #if OPERATION == OPERATION_COM
184 static void
185 nibble_com_test1(void)
187 VOLATILE_BITS_DEF;
188 TEST(0xFF, 0xFF);
189 TEST(0x00, 0x00);
190 TEST(0x0F, 0x0F);
191 TEST(0xF0, 0xF0);
194 static void
195 nibble_com_test2_1(void)
197 VOLATILE_BITS_DEF;
198 TEST(0x01, 0x01);
199 TEST(0x01, 0x02);
200 TEST(0x01, 0x04);
201 TEST(0x01, 0x08);
203 static void
204 nibble_com_test2_2(void)
206 VOLATILE_BITS_DEF;
207 TEST(0x02, 0x01);
208 TEST(0x02, 0x02);
209 TEST(0x02, 0x04);
210 TEST(0x02, 0x08);
212 static void
213 nibble_com_test2_3(void)
215 VOLATILE_BITS_DEF;
216 TEST(0x04, 0x01);
217 TEST(0x04, 0x02);
218 TEST(0x04, 0x04);
219 TEST(0x04, 0x08);
221 static void
222 nibble_com_test2_4(void)
224 VOLATILE_BITS_DEF;
225 TEST(0x08, 0x01);
226 TEST(0x08, 0x02);
227 TEST(0x08, 0x04);
228 TEST(0x08, 0x08);
231 static void
232 nibble_com_test3_1(void)
234 VOLATILE_BITS_DEF;
235 TEST(0x0E, 0x0E);
236 TEST(0x0E, 0x0D);
237 TEST(0x0E, 0x0B);
238 TEST(0x0E, 0x07);
240 static void
241 nibble_com_test3_2(void)
243 VOLATILE_BITS_DEF;
244 TEST(0x0D, 0x0E);
245 TEST(0x0D, 0x0D);
246 TEST(0x0D, 0x0B);
247 TEST(0x0D, 0x07);
249 static void
250 nibble_com_test3_3(void)
252 VOLATILE_BITS_DEF;
253 TEST(0x0B, 0x0E);
254 TEST(0x0B, 0x0D);
255 TEST(0x0B, 0x0B);
256 TEST(0x0B, 0x07);
258 static void
259 nibble_com_test3_4(void)
261 VOLATILE_BITS_DEF;
262 TEST(0x07, 0x0E);
263 TEST(0x07, 0x0D);
264 TEST(0x07, 0x0B);
265 TEST(0x07, 0x07);
268 static void
269 nibble_com_test4_1(void)
271 VOLATILE_BITS_DEF;
272 TEST(0x03, 0x03);
273 TEST(0x03, 0x06);
274 TEST(0x03, 0x0C);
276 static void
277 nibble_com_test4_2(void)
279 VOLATILE_BITS_DEF;
280 TEST(0x06, 0x03);
281 TEST(0x06, 0x06);
282 TEST(0x06, 0x0C);
284 static void
285 nibble_com_test4_3(void)
287 VOLATILE_BITS_DEF;
288 TEST(0x0C, 0x03);
289 TEST(0x0C, 0x06);
290 TEST(0x0C, 0x0C);
292 #else
293 static void
294 nibble_op_test_low1_1(void)
296 VOLATILE_BITS_DEF;
297 TEST(low, 0xFF);
298 TEST(low, 0x00);
300 static void
301 nibble_op_test_low1_2(void)
303 VOLATILE_BITS_DEF;
304 TEST(low, 0x0F);
305 TEST(low, 0xF0);
307 static void
308 nibble_op_test_low2_1(void)
310 VOLATILE_BITS_DEF;
311 TEST(low, 0x01);
312 TEST(low, 0x02);
314 #ifdef FULL_TEST
315 static void
316 nibble_op_test_low2_2(void)
318 VOLATILE_BITS_DEF;
319 TEST(low, 0x04);
320 TEST(low, 0x08);
322 #endif
323 static void
324 nibble_op_test_low3_1(void)
326 VOLATILE_BITS_DEF;
327 TEST(low, 0x0E);
328 TEST(low, 0x0D);
330 #ifdef FULL_TEST
331 static void
332 nibble_op_test_low3_2(void)
334 VOLATILE_BITS_DEF;
335 TEST(low, 0x0B);
336 TEST(low, 0x07);
338 #endif
339 static void
340 nibble_op_test_low4_1(void)
342 VOLATILE_BITS_DEF;
343 TEST(low, 0x10);
344 TEST(low, 0x20);
346 #ifdef FULL_TEST
347 static void
348 nibble_op_test_low4_2(void)
350 VOLATILE_BITS_DEF;
351 TEST(low, 0x40);
352 TEST(low, 0x80);
354 #endif
356 static void
357 nibble_op_test_high1_1(void)
359 VOLATILE_BITS_DEF;
360 TEST(high, 0xFF);
361 TEST(high, 0x00);
363 static void
364 nibble_op_test_high1_2(void)
366 VOLATILE_BITS_DEF;
367 TEST(high, 0x0F);
368 TEST(high, 0xF0);
370 static void
371 nibble_op_test_high2_1(void)
373 VOLATILE_BITS_DEF;
374 TEST(high, 0x01);
375 TEST(high, 0x02);
377 #ifdef FULL_TEST
378 static void
379 nibble_op_test_high2_2(void)
381 VOLATILE_BITS_DEF;
382 TEST(high, 0x04);
383 TEST(high, 0x08);
385 #endif
386 static void
387 nibble_op_test_high3_1(void)
389 VOLATILE_BITS_DEF;
390 TEST(high, 0x0E);
391 TEST(high, 0x0D);
393 #ifdef FULL_TEST
394 static void
395 nibble_op_test_high3_2(void)
397 VOLATILE_BITS_DEF;
398 TEST(high, 0x0B);
399 TEST(high, 0x07);
401 #endif
402 static void
403 nibble_op_test_high4_1(void)
405 VOLATILE_BITS_DEF;
406 TEST(high, 0x10);
407 TEST(high, 0x20);
409 #ifdef FULL_TEST
410 static void
411 nibble_op_test_high4_2(void)
413 VOLATILE_BITS_DEF;
414 TEST(high, 0x40);
415 TEST(high, 0x80);
417 #endif
418 #endif
420 #endif // DUMMY_CASE
422 static void
423 testBitfields(void)
425 #ifndef DUMMY_CASE
426 #if OPERATION == OPERATION_COM
427 nibble_com_test1();
429 nibble_com_test2_1();
430 nibble_com_test2_2();
431 nibble_com_test2_3();
432 nibble_com_test2_4();
434 nibble_com_test3_1();
435 nibble_com_test3_2();
436 nibble_com_test3_3();
437 nibble_com_test3_4();
439 nibble_com_test4_1();
440 nibble_com_test4_2();
441 nibble_com_test4_3();
442 #else
443 nibble_op_test_low1_1();
444 nibble_op_test_low1_2();
445 nibble_op_test_low2_1();
446 #ifdef FULL_TEST
447 nibble_op_test_low2_2();
448 #endif
449 nibble_op_test_low3_1();
450 #ifdef FULL_TEST
451 nibble_op_test_low3_2();
452 #endif
453 nibble_op_test_low4_1();
454 #ifdef FULL_TEST
455 nibble_op_test_low4_2();
456 #endif
458 nibble_op_test_high1_1();
459 nibble_op_test_high1_2();
460 nibble_op_test_high2_1();
461 #ifdef FULL_TEST
462 nibble_op_test_high2_2();
463 #endif
464 nibble_op_test_high3_1();
465 #ifdef FULL_TEST
466 nibble_op_test_high3_2();
467 #endif
468 nibble_op_test_high4_1();
469 #ifdef FULL_TEST
470 nibble_op_test_high4_2();
471 #endif
472 #endif
473 #endif