]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_GCC/freedom-metal/src/trap.S
Base project to replace existing Freedom Studio project using latest Freedom Studio...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_GCC / freedom-metal / src / trap.S
1 /* Copyright 2019 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #define METAL_MSTATUS_MIE_SHIFT 8
5 #define METAL_MSTATUS_MPP_M     3
6 #define METAL_MSTATUS_MPP_SHIFT 11
7
8 #define METAL_MTVEC_MODE_MASK   3
9
10 /* void _metal_trap(int ecode)
11  *
12  * Trigger a machine-mode trap with exception code ecode
13  */
14 .global _metal_trap
15 .type _metal_trap, @function
16
17 _metal_trap:
18
19     /* Store the instruction which called _metal_trap in mepc */
20     addi t0, ra, -1
21     csrw mepc, t0
22
23     /* Set mcause to the desired exception code */
24     csrw mcause, a0
25
26     /* Read mstatus */
27     csrr t0, mstatus
28
29     /* Set MIE=0 */
30     li t1, -1
31     xori t1, t1, 8 /*_RB_ METAL_MSTATUS_MIE_SHIFT*/
32     and t0, t0, t1
33
34     /* Set MPP=M */
35     li t1, 3 /*_RB_ METAL_MSTATUS_MPP_M*/
36     slli t1, t1, 11 /*_RB_METAL_MSTATUS_MPP_SHIFT*/
37     or t0, t0, t1
38
39     /* Write mstatus */
40     csrw mstatus, t0
41
42     /* Read mtvec */
43     csrr t0, mtvec
44
45     /*
46      * Mask the mtvec MODE bits
47      * Exceptions always jump to mtvec.BASE regradless of the vectoring mode.
48      */
49     andi t0, t0, 3 /*_RB_METAL_MTVEC_MODE_MASK*/
50
51     /* Jump to mtvec */
52     jr t0
53