1// Based on https://github.com/agra-uni-bremen/riscv-vp/blob/f6e95b370cb6eab7024f1be26a2b8a8ae2c916be/sw/simple-sensor/bootstrap.S2//3// Copyright © 2017-2018, Group of Computer Architecture, University of Bremen4//5// Permission is hereby granted, free of charge, to any person obtaining a6// copy of this software and associated documentation files (the7// "Software"), to deal in the Software without restriction, including8// without limitation the rights to use, copy, modify, merge, publish,9// distribute, sublicense, and/or sell copies of the Software, and to10// permit persons to whom the Software is furnished to do so, subject to11// the following conditions:12//13// The above copyright notice and this permission notice shall be included14// in all copies or substantial portions of the Software.15//16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS17// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.19// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY20// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,21// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE22// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2324.globl register_handler25.globl lvl0_handler2627lvl0_handler:28 # Store all register values on the stack29 addi sp, sp, -4 * 3230 sw x1, 0x0(sp)31 sw x4, 3 * 4(sp)32 sw x5, 4 * 4(sp)33 sw x6, 5 * 4(sp)34 sw x7, 6 * 4(sp)35 sw x10, 9 * 4(sp)36 sw x11, 10 * 4(sp)37 sw x12, 11 * 4(sp)38 sw x13, 12 * 4(sp)39 sw x14, 13 * 4(sp)40 sw x15, 14 * 4(sp)41 sw x16, 15 * 4(sp)42 sw x17, 16 * 4(sp)43 sw x28, 27 * 4(sp)44 sw x29, 28 * 4(sp)45 sw x30, 29 * 4(sp)46 sw x31, 30 * 4(sp)4748 # Jump to the level 1 trap handler written in Zig49 jal level1IRQHandler5051 # Load all register values from the stack and return52 lw x1, 0x0(sp)53 lw x4, 3 * 4(sp)54 lw x5, 4 * 4(sp)55 lw x6, 5 * 4(sp)56 lw x7, 6 * 4(sp)57 lw x10, 9 * 4(sp)58 lw x11, 10 * 4(sp)59 lw x12, 11 * 4(sp)60 lw x13, 12 * 4(sp)61 lw x14, 13 * 4(sp)62 lw x15, 14 * 4(sp)63 lw x16, 15 * 4(sp)64 lw x17, 16 * 4(sp)65 lw x28, 27 * 4(sp)66 lw x29, 28 * 4(sp)67 lw x30, 29 * 4(sp)68 lw x31, 30 * 4(sp)69 addi sp, sp, 4 * 3270 mret7172register_handler:73 # Use lvl0_handler as the trap handler74 la t0, lvl0_handler75 csrw mtvec, t07677 # Enable machine external interrupts (MIE bit)78 li t1, 0x80079 csrw mie, t18081 # Globally enable machine mode interrupts (MIE bit)82 li t1, 0x883 csrw mstatus, t18485 # Return86 ret