|
| 1 | +/* Copyright lowRISC contributors (OpenTitan project). */ |
| 2 | +/* Licensed under the Apache License, Version 2.0, see LICENSE for details. */ |
| 3 | +/* SPDX-License-Identifier: Apache-2.0 */ |
| 4 | + |
| 5 | +/** |
| 6 | +* Simple program to showcase how to use the MAI interface. |
| 7 | +*/ |
| 8 | + |
| 9 | +.section .text.start |
| 10 | +main: |
| 11 | + /* Load modulus */ |
| 12 | + li x2, 0 |
| 13 | + la x3, mod32 |
| 14 | + bn.lid x2, 0(x3) |
| 15 | + bn.wsrw MOD, w0 |
| 16 | + |
| 17 | + /* Configure MAI - select B2A */ |
| 18 | + /* B2A requires the value 0x1 in the field operation. This field is in bits[2:1] */ |
| 19 | + li x2, 0x2 |
| 20 | + csrrs x0, MAI_CTRL, x2 |
| 21 | + csrrs x3, MAI_STATUS, x0 /* Optional, just to populate the trace */ |
| 22 | + |
| 23 | + /* Write data into input WSRs */ |
| 24 | + bn.wsrw MAI_IN0_S0, w1 |
| 25 | + bn.wsrw MAI_IN0_S1, w2 |
| 26 | + |
| 27 | + /* Start conversion by writing the start bit */ |
| 28 | + li x2, 0x1 |
| 29 | + csrrs x3, MAI_STATUS, x0 /* Optional, just to populate the trace */ |
| 30 | + csrrs x0, MAI_CTRL, x2 |
| 31 | + /* Read the status register to populate the trace to see when the state changes. */ |
| 32 | + csrrs x3, MAI_STATUS, x0 |
| 33 | + csrrs x3, MAI_STATUS, x0 |
| 34 | + csrrs x3, MAI_STATUS, x0 |
| 35 | + csrrs x3, MAI_STATUS, x0 |
| 36 | + csrrs x3, MAI_STATUS, x0 |
| 37 | + csrrs x3, MAI_STATUS, x0 |
| 38 | + csrrs x3, MAI_STATUS, x0 |
| 39 | + csrrs x3, MAI_STATUS, x0 |
| 40 | + csrrs x3, MAI_STATUS, x0 |
| 41 | + csrrs x3, MAI_STATUS, x0 |
| 42 | + csrrs x3, MAI_STATUS, x0 |
| 43 | + csrrs x3, MAI_STATUS, x0 |
| 44 | + csrrs x3, MAI_STATUS, x0 |
| 45 | + csrrs x3, MAI_STATUS, x0 |
| 46 | + csrrs x3, MAI_STATUS, x0 |
| 47 | + csrrs x3, MAI_STATUS, x0 |
| 48 | + csrrs x3, MAI_STATUS, x0 |
| 49 | + csrrs x3, MAI_STATUS, x0 |
| 50 | + csrrs x3, MAI_STATUS, x0 |
| 51 | + csrrs x3, MAI_STATUS, x0 |
| 52 | + csrrs x3, MAI_STATUS, x0 |
| 53 | + /* Poll busy bit - use this in production code */ |
| 54 | +_poll_busy: |
| 55 | + csrrs x2, MAI_STATUS, x0 |
| 56 | + andi x2, x2, 0x1 |
| 57 | + bne x2, x0, _poll_busy |
| 58 | + |
| 59 | + /* Read results from output WSRs */ |
| 60 | + bn.wsrr w20, MAI_RES_S0 |
| 61 | + bn.wsrr w21, MAI_RES_S1 |
| 62 | + |
| 63 | + ecall |
| 64 | + |
| 65 | +.section .data |
| 66 | + |
| 67 | +/* |
| 68 | + mod32 = 8380417 |
| 69 | +*/ |
| 70 | +mod32: |
| 71 | + .word 0x007fe001 |
| 72 | + .word 0x00000000 |
| 73 | + .word 0x00000000 |
| 74 | + .word 0x00000000 |
| 75 | + .word 0x00000000 |
| 76 | + .word 0x00000000 |
| 77 | + .word 0x00000000 |
| 78 | + .word 0x00000000 |
0 commit comments