1 /*
2     bug-2221.c
3 */
4 
5 #include <testfwk.h>
6 
7 #ifdef __SDCC_mcs51
8 #pragma nooverlay
9 #include <8052.h>
10 
11 //two functions that use the same registers (but should be in different banks)
12 
calculate(unsigned char v1,unsigned char v2)13 unsigned char calculate(unsigned char v1,unsigned char v2)
14 {
15 	unsigned char v3;
16 	TF2 = 1;			//trigger the interrupt
17 	v3 = v1 / 2;
18 	return v3 + v1 + v2;
19 }
20 
21 
calculateISR(unsigned char v1,unsigned char v2)22 unsigned char calculateISR(unsigned char v1,unsigned char v2) __using(1)
23 {
24 	unsigned char v3;
25 	TF2 = 0;			//clear the interrupt
26 	v3 = v1 / 2;
27 	return v3 + v1 + v2;
28 }
29 
30 
31 unsigned char r1;
32 unsigned char r2;
33 
T2_isr(void)34 void T2_isr(void) __interrupt(5) __using(1)
35 {
36 	r1 = calculateISR(4,40);
37 
38 	//sdcc eliminates mov psw,0x080
39 	//which is necessary to ensure that calculateISR uses registers from bank 1 to not
40 	//corrupt calculate in main loop
41 }
42 #endif
43 
testBug(void)44 void testBug(void)
45 {
46 #if 0 //defined(__SDCC_mcs51) && defined(__SDCC_STACK_AUTO) // TODO:remove STACK_AUTO CONDITION when division support routine becomes reentrant or sdcc optimizes out the call.
47 	TF2 = 0;				//clear timer 2 interrupt
48 	ET2 = 1;				//enable timer 2 interrupt
49 	EA = 1;					//enable interrupts
50 
51 	r2 = calculate(8,30);
52 	ASSERT(r1 == 46);
53 	ASSERT(r2 == 42);
54 #endif
55 }
56 
57