/*Copyright 2019-2021 T-Head Semiconductor Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#************************************************************
#*            Target file generated by rangen               *
#************************************************************
#*                                                          *
#************************************************************

#*************Following is the generated instructions*****************
.macro SETMEXP EXP_CODE, HANDLER_BEGIN,HANDLER_END
  sd   t0, -24(sp)   #address cann't be changed 
  sd   t1, -32(sp)   #it relates to crt0.s

  la   t1, vector_table
  addi t0,x0,\EXP_CODE
  slli t0,t0,0x3
  add  t1,t1,t0 
  
  la   t0, \HANDLER_BEGIN
  sd   t0, 0(t1) 

  ld   t1, -32(sp)
  ld   t0, -24(sp)
  j    \HANDLER_END 

  ld   a4, -16(sp)
.endm

.macro MMU_PTW_4K VPN, PPN, FLAG, THEADFLAG
  #backup regs 
  addi	x2, x2, -88
  sd	x9, 0(x2)
  sd	x10, 8(x2)
  sd	x11, 16(x2)
  sd	x12, 24(x2)
  sd	x13, 32(x2)
  sd	x14, 40(x2)
  sd	x15, 48(x2)
  sd	x16, 56(x2)
  sd	x17, 64(x2)
  sd	x18, 72(x2)
  sd	x19, 80(x2)
   
  # get PPN from satp in x9
  csrr  x9, satp
  li    x10, 0xfffffffffff
  and   x9, x9, x10
  
  # get VPN2 in x10
  li    x10, \VPN
  li    x11, 0x7fc0000
  and   x10,x10,x11
  srli  x10, x10, 18    
   
  # cfig first-level page
  # level-1 page, entry addr:{ppn,VPN2,3'b0} in x15
  slli  x14, x9, 12
  slli  x11, x10, 3
  add   x15, x14, x11
  # write pte into level-1 page
  # level-2 base addr in x12
  addi  x12, x10, 1
  add   x12, x12, x9
  slli  x14, x12, 10
  li    x13, 0xc1
  or    x13, x13, x14
  sd    x13, 0(x15)
  
  # cfig level-2 page
  # get VPN1 in x16
  li    x11, \VPN
  li    x13, 0x3fe00
  and   x16, x11, x13
  srli  x16, x16, 9  
  # level-2 page, entry addr:{pte.ppn,VPN1,3'b0} in x17
  slli  x13, x12, 12
  slli  x17, x16, 3
  add   x17, x17, x13
  # write pte into level-2 page
  # level-3 base addr in x18: PPN+2+2^9+{vpn2,vpn1}
  li    x11, 0x200
  addi  x11, x11, 2
  add   x11, x11, x9  
  li    x10, \VPN
  srli  x10, x10, 9
  add   x18, x11, x10
  slli  x19, x18, 10 
  li    x13, 0xc1
  or    x19, x19, x13
  sd    x19, 0(x17)
  
  # cfig level-3 page
  # get VPN0 in x12
  li    x11, \VPN
  li    x12, 0x1ff
  and   x12, x12, x11
  # get level-3 page addr x17
  slli  x18, x18, 12
  slli  x12, x12, 3
  add   x17, x18, x12
  # write pte into level-3 page
  li    x11, \PPN
  li    x12, \THEADFLAG
  li    x13, \FLAG
  slli  x11, x11, 10
  slli  x12, x12, 59
  or    x11, x11, x12
  or    x11, x11, x13
  sd    x11, 0(x17)
   
  #restore regs
  ld	x9, 0(x2)
  ld	x10, 8(x2)
  ld	x11, 16(x2)
  ld	x12, 24(x2)
  ld	x13, 32(x2)
  ld	x14, 40(x2)
  ld	x15, 48(x2)
  ld	x16, 56(x2)
  ld	x17, 64(x2)
  ld	x18, 72(x2)
  ld	x19, 80(x2)
  addi	x2, x2, 88
  # fence 
  fence
.endm
	

.text
.align 6
.global main
main:
      csrr	x10, mhartid
      bnez	x10, TEST_WFI 
      nop
.global MMU_CFG
  csrr  x9,satp
  li    x10,0xfffffffffffffff
  and   x9,x9,x10
  li    x10,8
  slli  x10,x10,60
  or    x9,x9,x10
  csrw	satp, x9
  MMU_PTW_4K 0x0,0x0,0xff,0xf

      .option norvc
      dcache.ciall
.global DATA_CACHE_DIS
DATA_CACHE_DIS:
      csrci mhcr,0x2
      SETMEXP 2 ILL_BEG ILL_END
.global ILL_BEG
ILL_BEG:
      la x3,ILL_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x4,11111111
      csrrci x1,mtval,0x0
      bne x1,x4,TEST_FAIL
      li x3,0x2
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global ILL_END
ILL_END:
      SETMEXP 3 EBREAK_BEG EBREAK_END
.global EBREAK_BEG
EBREAK_BEG:
      la x3,EBREAK_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      csrrci x1,mtval,0x0
      li x3,0x3
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global EBREAK_END
EBREAK_END:
      SETMEXP 4 MISALIGN_BEG MISALIGN_END
.global MISALIGN_BEG
MISALIGN_BEG:
      la x3,MISALIGN_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x6fff
      csrrci x1,mtval,0x0
      bne x1,x3,TEST_FAIL
      li x3,0x4
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global MISALIGN_END
MISALIGN_END:
      SETMEXP 5 LOAD_ACCERR_BEG LOAD_ACCERR_END
.global LOAD_ACCERR_BEG
LOAD_ACCERR_BEG:
      la x3,LOAD_ACCERR_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x0
      csrrci x1,mtval,0x0
      bne x1,x3,TEST_FAIL
      li x3,0x5
      csrrc x1,mcause,x0
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global LOAD_ACCERR_END
LOAD_ACCERR_END:
      SETMEXP 6 STORE_MISALIGN_BEG STORE_MISALIGN_END
.global STORE_MISALIGN_BEG
STORE_MISALIGN_BEG:
      la x3,STORE_MISALIGN_EXPT
      csrrc x1,mepc,x0
      li x3,0x6
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global STORE_MISALIGN_END
STORE_MISALIGN_END:
      SETMEXP 7 STORE_ACCERR_BEG STORE_ACCERR_END
.global STORE_ACCERR_BEG
STORE_ACCERR_BEG:
      la x3,STORE_ACCERR_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x7
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global STORE_ACCERR_END
STORE_ACCERR_END:
      SETMEXP 11 MECALL_BEG MECALL_END
.global MECALL_BEG
MECALL_BEG:
      la x3,MECALL_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      csrrci x1,mtval,0x0
      bne x1,x0,TEST_FAIL
      li x3,0xb
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global MECALL_END
MECALL_END:
      SETMEXP 1 INST_SCCERR_BEG INST_SCCERR_END
.global INST_SCCERR_BEG
INST_SCCERR_BEG:
      li x3,0x600000010
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x600000010
      csrrci x1,mtval,0x0
      li x3,0x1
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      la x1,AFTER_INST_ACCERR_EXPT
      csrrw x3,mepc,x1
      mret
      jalr x0,x1,0x0
.global INST_SCCERR_END
INST_SCCERR_END:
      SETMEXP 9 SECALL_BEG SECALL_END
.global SECALL_BEG
SECALL_BEG:
      la x3,SECALL_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x9
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global SECALL_END
SECALL_END:
      SETMEXP 12 INS_PAGEFAULT_BEG INS_PAGEFAULT_END
.global INS_PAGEFAULT_BEG
INS_PAGEFAULT_BEG:
      li x3,0xfff00000
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0xc
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      la x1,AFTER_INST_PAGEFAULT_EXPT
      csrrw x0, mepc,x1
      mret
.global INS_PAGEFAULT_END
INS_PAGEFAULT_END:
      SETMEXP 13 LOAD_PAGEFAULT_BEG LOAD_PAGEFAULT_END
.global LOAD_PAGEFAULT_BEG
LOAD_PAGEFAULT_BEG:
      la x3,LOAD_PAGEFAULT_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0xfff00000
      csrrci x1,mtval,0x0
      bne x1,x3,TEST_FAIL
      li x3,0xd
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global LOAD_PAGEFAULT_END
LOAD_PAGEFAULT_END:
      SETMEXP 15 STORE_PAGEFAULT_BEG STORE_PAGEFAULT_END
.global STORE_PAGEFAULT_BEG
STORE_PAGEFAULT_BEG:
      la x3,STORE_PAGEFAULT_EXPT
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0xfff00000
      csrrci x1,mtval,0x0
      bne x1,x3,TEST_FAIL
      li x3,0xf
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global STORE_PAGEFAULT_END
STORE_PAGEFAULT_END:
      SETMEXP 8 UECALL_BEG UECALL_END
.global UECALL_BEG
UECALL_BEG:
      la x3,UROUTINE
      csrrc x1,mepc,x0
      bne x1,x3,TEST_FAIL
      li x3,0x8
      csrrc x1,mcause,x0
      bne x1,x3,TEST_FAIL
      csrrs x1, mepc,x0
      addi x1,x1,0x4
      csrrw x0, mepc,x1
      mret
.global UECALL_END
UECALL_END:
.global WRITE_DELEG
WRITE_DELEG:
      li x1,0x6000
      csrrc x0,mstatus,x1
.global MCSR_INITIAL
MCSR_INITIAL:
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1,0x800000000000000f
      csrrw x0,mcause,x1
      li x1,0xffffffffffffffff
      csrrw x0,mepc,x1
      li x1,0x800000000000000f
      csrrw x0,scause,x1
      li x1,0xffffffffffffffff
      csrrw x0,sepc,x1
.global ILL_EXPT
ILL_EXPT:
      .long 11111111
.global EBREAK
EBREAK:
      li x1,0x800
      csrrw x0,mstatus,x1
.global EBREAK_EXPT
EBREAK_EXPT:
      ebreak
.global MISALIGN
MISALIGN:
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1,0x6fff
.global MISALIGN_EXPT
MISALIGN_EXPT:
      lw x3,0x0(x1)
.global LOAD_ACCERR
LOAD_ACCERR:
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1, 0xbfffffff
      csrw pmpaddr0, x1
      li x1, 0xc007ffff
      csrw pmpaddr1, x1
      li x1, 0x1000001ff
      csrw pmpaddr2, x1
      li x1, 0x147ffffff
      csrw pmpaddr3, x1
      li x1, 0x1800001ff
      csrw pmpaddr4, x1
      li x1, 0x1807fffff
      csrw pmpaddr5, x1
      li x1, 0x18981898180f
      csrw pmpcfg0, x1
      li x1,0x600000010
.global LOAD_ACCERR_EXPT
LOAD_ACCERR_EXPT:
      lw x3,0x0(x1)
.global STORE_MISALIGN
STORE_MISALIGN:
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1,0x6fff
.global STORE_MISALIGN_EXPT
STORE_MISALIGN_EXPT:
      sw x3,0x0(x1)
.global STORE_ACCERR
STORE_ACCERR:
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1,0x600000010
.global STORE_ACCERR_EXPT
STORE_ACCERR_EXPT:
      sw x3,0x0(x1)
      li x1,0x800
      csrrw x0,mstatus,x1
.global MECALL_EXPT
MECALL_EXPT:
      ecall
      li x1,0x800
      csrrw x0,mstatus,x1
      li x1,0x600000010
.global INST_ACCERR_EXPT
INST_ACCERR_EXPT:
      jalr x0,x1,0x0
.global AFTER_INST_ACCERR_EXPT
AFTER_INST_ACCERR_EXPT:
      nop
      la x1,SROUTINE
      csrw mepc,x1
      li x1,0xc0800
      csrrs x0,mstatus,x1
      li x1,0x1000
      csrrc x0,mstatus,x1
      mret
.global SROUTINE
SROUTINE:
.global SECALL_EXPT
SECALL_EXPT:
      ecall
.global INST_PAGEFAULT
INST_PAGEFAULT:
      li x1,0xfff00000
.global INST_PAGEFAULT_EXPT
INST_PAGEFAULT_EXPT:
      jalr x0,x1,0x0
.global AFTER_INST_PAGEFAULT_EXPT
AFTER_INST_PAGEFAULT_EXPT:
      nop
.global LOAD_PAGEFAULT
LOAD_PAGEFAULT:
      li x1,0xfff00000
.global LOAD_PAGEFAULT_EXPT
LOAD_PAGEFAULT_EXPT:
      lw x3,0x0(x1)
.global STORE_PAGEFAULT
STORE_PAGEFAULT:
      li x1,0xfff00000
.global STORE_PAGEFAULT_EXPT
STORE_PAGEFAULT_EXPT:
      sw x3,0x0(x1)
      la x1,UROUTINE
      csrw sepc,x1
      li x1,0x100
      csrrc x3,sstatus,x1
      sret
.global UROUTINE
UROUTINE:
      ecall
.global TEST_PASS
TEST_PASS:
     la   x1, __exit
     jr   x1
.global TEST_FAIL
TEST_FAIL:
    la   x1, __fail 
    jr   x1
.global TEST_WFI
TEST_WFI:
  wfi
