cb0d3bdf95a42ab85ac3b4d136667ddd59e5d2e4
[b43-tools.git] / assembler / test.asm
1 /* This is a bcm43xx microcode assembly example.
2  *
3  * In this example file, r0 and r1 are always input
4  * registers and r2 is output.
5  * For input we can always have constant values or (one) memory
6  * operand instead of the input registers shown here.
7  *
8  * Registers:
9  *      GPRs:                   r0 - r63
10  *      Offset Registers:       off0 - off5
11  *      SPRs:                   spr000
12  *
13  * To access memory, two methods can be used. Examples follow.
14  * Direct linear:
15  *      mov r0,[0xCA]
16  * Indirect through Offset Register (pointer):
17  *      mov r0,[0xCA,off0]
18  */
19
20
21 /* The target architecture. Supported versions are 5 and 15 */
22 %arch   5
23
24 /* Program entry point */
25 %start  testlabel
26
27 #define PSM_BRC         spr848
28
29 #define ECOND_MAC_ON    (0x20 | 4)
30
31 %assert ((((1))) == ((((2 - 1) & 0xFF))))
32 %assert ((1 == 2) || (1 == (0xFF & 1)))
33 %assert (1 != (~1))
34 %assert ((1 == (2 - 1)) && (2 == 2))
35
36 .text
37
38         /* Inline assertion inside of a complex immediate.
39          * The %assert() expression will always return zero. */
40         mov     (1 + (%assert(1 == ((1 + 2) - 2)))), r0
41
42 label:
43         /* ADD instructions */
44         add     r0,r1,r2        /* add */
45         add.    r0,r1,r2        /* add, set carry */
46         addc    r0,r1,r2        /* add with carry */
47         addc.   r0,r1,r2        /* add with carry, set carry */
48
49 testlabel:
50         /* SUB instructions */
51         sub     r0,r1,r2        /* sub */
52         sub.    r0,r1,r2        /* sub, set carry */
53         subc    r0,r1,r2        /* sub with carry */
54         subc.   r0,r1,r2        /* sub with carry, set carry */
55
56         sra     r0,r1,r2        /* arithmetic rightshift */
57
58         /* Logical instructions */
59         or      r0,r1,r2        /* bitwise OR */
60         and     r0,r1,r2        /* bitwise AND */
61         xor     r0,r1,r2        /* bitwise XOR */
62         sr      r0,r1,r2        /* rightshift */
63         sl      r0,r1,r2        /* leftshift */
64
65         srx     7,8,r0,r1,r2    /* eXtended right shift (two input regs) */
66
67         rl      r0,r1,r2        /* rotate left */
68         rr      r0,r1,r2        /* rotate right */
69         nand    r0,r1,r2        /* clear bits (notmask + and) */
70
71         orx     7,8,r0,r1,r2    /* eXtended OR */
72
73         /* Copy instruction. This is a virtual instruction
74          * translated to more lowlevel stuff like OR. */
75         mov     r0,r2           /* copy data */
76
77         /* Jumps */
78         jmp     label           /* unconditional jump */
79         jand    r0,r1,label     /* jump if binary AND */
80         jnand   r0,r1,label     /* jump if not binary AND */
81         js      r0,r1,label     /* jump if all bits set */
82         jns     r0,r1,label     /* jump if not all bits set */
83         je      r0,r1,label     /* jump if equal */
84         jne     r0,r1,label     /* jump if not equal */
85         jls     r0,r1,label     /* jump if less (signed) */
86         jges    r0,r1,label     /* jump if greater or equal (signed) */
87         jgs     r0,r1,label     /* jump if greater (signed) */
88         jles    r0,r1,label     /* jump if less or equal (signed) */
89         jl      r0,r1,label     /* jump if less */
90         jge     r0,r1,label     /* jump if greater or equal */
91         jg      r0,r1,label     /* jump if greater */
92         jle     r0,r1,label     /* jump if less or equal */
93
94         jzx     7,8,r0,r1,label /* Jump if zero after shift and mask */
95         jnzx    7,8,r0,r1,label /* Jump if nonzero after shift and mask */
96
97         /* jump on external conditions */
98         jext    ECOND_MAC_ON,label  /* jump if external condition is TRUE */
99         jnext   ECOND_MAC_ON,label  /* jump if external condition is FALSE */
100
101         /* Subroutines */
102         call    lr0,label       /* store PC in lr0, call func at label */
103         ret     lr0,lr1         /* store PC in lr0, return to lr1
104                                  * Both link registers can be the same
105                                  * and don't interfere. */
106
107         /* TKIP sbox lookup */
108         tkiph   r0,r2           /* Lookup high */
109         tkiphs  r0,r2           /* Lookup high, byteswap */
110         tkipl   r0,r2           /* Lookup low */
111         tkipls  r0,r2           /* Lookup low, byteswap */
112
113         nap                     /* sleep until event */
114
115         /* raw instruction */
116         @160    r0,r1,r2        /* equivalent to  or r0,r1,r2 */
117         @1C0    @C11, @C22, @BC3
118
119
120         /* Support for directional jumps.
121          * Directional jumps can be used to conveniently jump inside of
122          * functions without using function specific label prefixes. Note
123          * that this does not establish a sub-namespace, though. "loop"
124          * and "out" are still in the global namespace and can't be used
125          * anymore for absolute jumps (Assembler will warn about duplication).
126          */
127 function_a:
128         jl r0, r1, out+
129  loop:
130         nap
131         jmp loop-
132  out:
133         mov r0, r0
134         ret lr0, lr1
135
136 function_b:
137         jl r0, r1, out+
138  loop:
139         nap
140         jmp loop-
141  out:
142         mov r0, r0
143         ret lr0, lr1
144
145
146 /* The assembler has support for fancy assemble-time
147  * immediate constant expansion. This is called "complex immediates".
148  * Complex immediates are _always_ clamped by parentheses. There is no
149  * operator precedence. You must use parentheses to tell precedence.
150  */
151         mov     (2 + 3),r0
152         mov     (6 - 2),r0
153         mov     (2 * 3),r0
154         mov     (10 / 5),r0
155         mov     (1 | 2),r0
156         mov     (3 & 2),r0
157         mov     (3 ^ 2),r0
158         mov     (~1),r0
159         mov     (2 << 3),r0
160         mov     (8 >> 2),r0
161         mov     (1 << (0x3 + 2)),r0
162         mov     (1 + (2 + (3 + 4))),r0
163         mov     (4 >> (((((~5 | 0x21)))) | (~((10) & 2)))),r0
164
165
166 /* Some regression testing for the assembler follows */
167         mov     2,off0                  /* test memory stuff */
168         xor     0x124,r1,[0x0,off0]     /* test memory stuff */
169         xor     0x124,r0,[0x0]          /* test memory stuff */
170         mov     -34,r0                  /* negative dec numbers are supported */
171         or      r0,r1,@BC2              /* We also support single raw operands */
172         mov     0xEEEE,r0               /* MOV supports up to 16bit */
173         jand    0x3800,r0,label         /* This is emulated by jnzx */
174         jnand   0x3800,r0,label         /* This is emulated by jzx */
175         or      spr06c,0,spr06c         /* Can have one spr input and one spr output */
176         or      [0],0,[0]               /* Can have one mem input and one mem output */
177         mov     testlabel, r0           /* Can use label as immediate value */
178         mov r0,r1;mov r2, r3            /* ; does split instructions */
179         mov     [(1+1)],[(2+2),off0]    /* Can use complex immediates as memory offsets */
180         orx     (0 + 1), (1 * 2), 0, 0, r0 /* Allow complex immediates as M or S */
181
182
183 /* The .initvals section generates an "Initial Values" file
184  * with the name "foobar" in this example, which is uploaded
185  * by the kernel driver on load. This is useful for writing ucode
186  * specific values to the chip without bloating the small ucode
187  * memory space with this initialization stuff.
188  * Values are written in order they appear here.
189  */
190 .initvals(foobar)
191         mmio16  0x1234, 0xABC                   /* Write 0x1234 to MMIO register 0xABC */
192         mmio32  0x12345678, 0xABC               /* Write 0x12345678 to MMIO register 0xABC */
193         phy     0x1234, 0xABC                   /* Write 0x1234 to PHY register 0xABC */
194         radio   0x1234, 0xABC                   /* Write 0x1234 to RADIO register 0xABC */
195         shm16   0x1234, 0x0001, 0x0002          /* Write 0x1234 to SHM routing 0x0001, register 0x0002 */
196         shm32   0x12345678, 0x0001, 0x0002      /* Write 0x12345678 to SHM routing 0x0001, register 0x0002 */
197
198
199 // vim: syntax=b43 ts=8