1 #include <stdio.h>
2 
3 #define get_cc() \
4 ({ \
5    char __cc; \
6    /* don't use IPM to better test spechelpers */ \
7    asm volatile( "brc 8,1f\n\t" \
8                  "brc 4,2f\n\t" \
9                  "brc 2,3f\n\t" \
10                  "brc 1,4f\n\t" \
11                  "mvi %0,4\n\t" \
12                  "j   0f\n\t" \
13                  "1:  mvi %0,0\n\t" \
14                  "j   0f\n\t" \
15                  "2:  mvi %0,1\n\t" \
16                  "j   0f\n\t" \
17                  "3:  mvi %0,2\n\t" \
18                  "j   0f\n\t" \
19                  "4:  mvi %0,3\n\t" \
20                  "j   0f\n\t" \
21                  "0:  /* nop */ brc 0,0\n\t" \
22                  : "=m" (__cc) : : "memory"); \
23 	__cc; \
24 })
25 
check_cc(int value,int cc)26 void check_cc(int value, int cc)
27 {
28    if (cc != value) {
29       printf("wrong cc: ");
30       if (value == 0) printf("expected 0  ");
31       if (value == 1) printf("expected 1  ");
32       if (value == 2) printf("expected 2  ");
33       if (value == 3) printf("expected 3  ");
34       if (cc == 0) printf("got 0");
35       if (cc == 1) printf("got 1");
36       if (cc == 2) printf("got 2");
37       if (cc == 3) printf("got 3");
38    } else {
39       printf("OK");
40    }
41    printf("\n");
42 }
43 
tm(void)44 void tm(void)
45 {
46    unsigned char v;
47 
48    /* test #1: value is zero */
49    v = 0;
50    asm volatile( "tm   %[v],15\n\t" : : [v] "R"(v) : "cc");
51    check_cc(0, get_cc());
52 
53    /* test #2: mask is zero */
54    v = 0xFF;
55    asm volatile( "tm   %[v],0\n\t" : : [v] "R"(v) : "cc");
56    check_cc(0, get_cc());
57 
58    /* test #3: selected bits are 0 */
59    v = 0x0F;
60    asm volatile( "tm   %[v],0xf0\n\t" : : [v] "R"(v) : "cc");
61    check_cc(0, get_cc());
62 
63    /* test #4: selected bits are all 1 */
64    v = 0xF0;
65    asm volatile( "tm   %[v],0x70\n\t" : : [v] "R"(v) : "cc");
66    check_cc(3, get_cc());
67 
68    /* test #5: selected bits are mixed */
69    v = 0x0F;
70    asm volatile( "tm   %[v],0x81\n\t" : : [v] "R"(v) : "cc");
71    check_cc(1, get_cc());
72 }
73 
main(void)74 int main(void)
75 {
76    tm();
77 
78    return 0;
79 }
80