struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / critical.c
blobbe0d47401f98f728016ecd63dc1be9c318c9814d
1 /* Keyword "critical" tests.
2 */
3 #include <testfwk.h>
5 #if defined(__SDCC_mcs51)
6 #include <8052.h>
8 typedef union
10 unsigned int a;
11 struct
13 unsigned char b;
14 unsigned char c;
16 } big;
18 //must be at least 2 bytes big and volatile
19 volatile big global_var = { 0 };
20 int y;
22 unsigned int
23 get_global (void) __critical
25 return global_var.a;
27 #endif
29 #if defined(__SDCC_mcs51) || defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r2ka) || defined(__SDCC_r3ka) || defined(__SDCC_hc08) || defined(__SDCC_s08) || defined(__SDCC_mos6502) || defined(__SDCC_mos65c02) || defined(__SDCC_tlcs90) || defined(__SDCC_stm8)
30 // Check that param offsets are correctly adjusted for critial functions
31 long critical_function(long a, long b, long c) __critical
33 if (a > 10L)
34 return a + b + c;
35 else if (b < 10L)
36 return a - b - c;
37 else
38 return a + b - c;
40 inline long critical_function_inline(long a, long b, long c) __critical
42 if (a > 10L)
43 return a + b + c;
44 else if (b < 10L)
45 return a - b - c;
46 else
47 return a + b - c;
49 #endif
51 void
52 testCritical (void)
54 #if defined(__SDCC_mcs51)
55 big x;
56 unsigned char i;
58 //enable the interrupt and set it
59 ET2 = 1;
60 EA = 1;
61 TF2 = 1;
63 __critical x.a = global_var.a;
64 ASSERT (x.b == x.c);
66 x.a = get_global ();
67 ASSERT (x.b == x.c);
69 for (i = 10; i != 0; i--)
71 __critical x.a = global_var.a;
72 ASSERT (x.b == x.c);
74 x.a = get_global();
75 ASSERT (x.b == x.c);
77 //check the interrupt has run at all
78 ASSERT (x.a != 0);
80 __critical y = 0;
81 //check the interrupts are still enabled
82 ASSERT (EA);
83 #else
84 ASSERT (1);
85 #endif
87 #if defined(__SDCC_mcs51) || defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r2ka) || defined(__SDCC_r3ka) || defined(__SDCC_hc08) || defined(__SDCC_s08) || defined(__SDCC_mos6502) || defined(__SDCC_mos65c02) || defined(__SDCC_tlcs90) || defined(__SDCC_stm8)
88 ASSERT(critical_function(11, 1, 1) == 13);
89 ASSERT(critical_function(5, 1, 1) == 3);
90 ASSERT(critical_function(5, 11, 1) == 15);
91 ASSERT(critical_function_inline(11, 1, 1) == 13);
92 ASSERT(critical_function_inline(5, 1, 1) == 3);
93 ASSERT(critical_function_inline(5, 11, 1) == 15);
94 #endif
97 #if defined(__SDCC_mcs51)
98 void
99 T2_isr (void) __interrupt (5) __using (2)
101 //do not clear flag ET2 so it keeps interrupting !
102 global_var.b++;
103 if (global_var.b == 0)
104 global_var.b++;
105 global_var.c = global_var.b;
107 #endif