URL
https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk
Subversion Repositories riscv_vhdl
[/] [riscv_vhdl/] [trunk/] [examples/] [boot/] [src/] [crt.S] - Rev 5
Compare with Previous | Blame | View Log
##! Register ABI Name Description Saver##! x1 ra Return address Caller##! x2 s0/fp Saved register/frame pointer Callee##! x14 sp Stack pointer Callee##! x15 tp Thread pointer Callee#include "encoding.h".text.align 4.globl reset_vector.globl _starttrap_table:#define BAD_TRAP_VECTOR 0.word bad_trap.word bad_trap # pmp_trap.word bad_trap # illegal_insn_trap.word bad_trap.word bad_trap # misaligned_load_trap.word bad_trap # pmp_trap.word bad_trap # misaligned_store_trap.word bad_trap # pmp_trap.word bad_trap.word bad_trap # mcall_trap.word bad_trap.word bad_trap.word bad_trap#define TRAP_FROM_MACHINE_MODE_VECTOR 13.word bad_trap # __trap_from_machine_mode.word bad_trap.word bad_trap##! Disabling the compressed code# .option norvcreset_vector:j _startbad_trap:j bad_trap_start:li x1, 0li x2, 0li x3, 0li x4, 0li x5, 0li x6, 0li x7, 0li x8, 0li x9, 0li x10,0li x11,0li x12,0li x13,0li x14,0li x15,0li x16,0li x17,0li x18,0li x19,0li x20,0li x21,0li x22,0li x23,0li x24,0li x25,0li x26,0li x27,0li x28,0li x29,0li x30,0li x31,0##! csrs (pseudo asm instruction) - set bit##! csrrs - atomic read and set bit##! csrc (pseudo asm instruction) - clear bit##! csrrc - atomic read and clear bitli t0, 0x00001800 # MPP[12:11] = 0x3 (Previous to machine mode)csrc mstatus, t0 # run tests in user mode = 0, by clearing bitsli t0, 0x00000008 # Enable irq in machine and user modes after execution of xRETcsrs mstatus, t0 # enable interrupts in user mode#li t0, MSTATUS_FS;#csrs mstatus, t0 # enable FPU#li t0, MSTATUS_XS;#csrs mstatus, t0 # enable accelerator##! init mtvec register (see https://github.com/riscv/riscv-test-env/blob/master/p/riscv_test.h)la t0, trap_entrycsrw mtvec, t0li t0, 0x00000800csrs mie, t0 # Enable External irq (ftom PLIC) for M mode#if 0##! see https://github.com/riscv/riscv-tests/benchmarks/commoncsrr t0, mstatusli t1, MSTATUS_XSand t1, t0, t1sw t1, have_vec, t2## if that didn't stick, we don't have a FPU, so don't initialize itli t1, MSTATUS_FSand t1, t0, t1beqz t1, 1f#endif# intialization when FPU enabled#ifdef REMOVETHIS__riscv_hard_floatfssr x0fmv.s.x f0, x0fmv.s.x f1, x0fmv.s.x f2, x0fmv.s.x f3, x0fmv.s.x f4, x0fmv.s.x f5, x0fmv.s.x f6, x0fmv.s.x f7, x0fmv.s.x f8, x0fmv.s.x f9, x0fmv.s.x f10,x0fmv.s.x f11,x0fmv.s.x f12,x0fmv.s.x f13,x0fmv.s.x f14,x0fmv.s.x f15,x0fmv.s.x f16,x0fmv.s.x f17,x0fmv.s.x f18,x0fmv.s.x f19,x0fmv.s.x f20,x0fmv.s.x f21,x0fmv.s.x f22,x0fmv.s.x f23,x0fmv.s.x f24,x0fmv.s.x f25,x0fmv.s.x f26,x0fmv.s.x f27,x0fmv.s.x f28,x0fmv.s.x f29,x0fmv.s.x f30,x0fmv.s.x f31,x0#endif##! initialize global pointer (no need in it)lui gp, 0x10000##! get core idcsrr a0, mhartid # for now, assume only 1 coreli a1, 11:bgeu a0, a1, 1b# Task stack pointer (tp) uses the same value as sp.#define STACK_512KB 19lui t0, 0x10000 # t0 = SRAM base address = 0x10000000 . lui = load upper immediate 20-bitsadd sp, zero, 1 # sp = 1sll sp, sp, STACK_512KB # sp = sp << 19 = 0x80000add sp, t0, sp # sp = sp + 0x10000000 = 0x10080000add tp, zero, sp # tp = sp + 0## Use tp register to save/restore registers context on task switchingaddi tp,tp,-256 # tp = tp - 256 = 0x1007ff00# 1 KB for the tap stack + TLS. Add signed extended 12-bits to registeraddi sp, sp, -1024 # sp = 0x1007fc00# copy image 64 KBjal _init##! jump to entry point in SRAM = 0x10000000##! 'meps' - Machine Exception Program Coutnerlui t0, 0x10000 # t0 = SRAM base address = 0x10000000 . lui = load upper immediate 20-bitscsrw mepc, t0##! @see riscv-priv-spec-1.7.pdf. 3.2.1##! After handling a trap, the ERET instruction is used to return to the privilege level at which the##! trap occurred. In addition to manipulating the privilege stack as described in Section 3.1.5, ERET##! sets the pc to the value stored in the Xepc register, where X is the privilege mode (S, H, or M) in##! which the ERET instruction was executed.mrettrap_entry:##! module CSRFile rises io_fatc signal that is cause of the 'ptw.invalidate'.fencecsrw mscratch, a0;_save_context(tp)## @brief Call function :## long handle_trap(long cause, long epc, long long regs[32])## a0 = argument 1: cause## a1 = argument 2: mepc## a2 = argument 3: pointer on stack## @return a0 New instruction pointer offsetcsrr a0, mcausecsrr a1, mepcsd a1,COOP_REG_TP(tp)mv a2, sp# !!! Cannot reset external pending bits only via IrqController (page 28)li t0, 0x00000800csrc mip, t0 #csrc pseudo asm instruction clear CSR bit.#[11] MEIP: machine pending external interruptjal handle_trap# tp-offset in the context array is used to save mepc value. An it may be# modified by isr handler during preemtive task switching.ld a1,COOP_REG_TP(tp)csrw mepc,a1_restore_context(tp)mret.section ".tdata.begin".globl _tdata_begin_tdata_begin:.section ".tdata.end".globl _tdata_end_tdata_end:.section ".tbss.end".globl _tbss_end_tbss_end:
