NMSIS-Core  Version 1.3.1
NMSIS-Core support for Nuclei processor-based devices
Core CSR Register Access

Functions to access the Core CSR Registers. More...

Macros

#define __RV_CSR_SWAP(csr, val)
 CSR operation Macro for csrrw instruction. More...
 
#define __RV_CSR_READ(csr)
 CSR operation Macro for csrr instruction. More...
 
#define __RV_CSR_WRITE(csr, val)
 CSR operation Macro for csrw instruction. More...
 
#define __RV_CSR_READ_SET(csr, val)
 CSR operation Macro for csrrs instruction. More...
 
#define __RV_CSR_SET(csr, val)
 CSR operation Macro for csrs instruction. More...
 
#define __RV_CSR_READ_CLEAR(csr, val)
 CSR operation Macro for csrrc instruction. More...
 
#define __RV_CSR_CLEAR(csr, val)
 CSR operation Macro for csrc instruction. More...
 

Functions

__STATIC_FORCEINLINE void __switch_mode (uint8_t mode, uintptr_t stack, void(*entry_point)(void))
 switch privilege from machine mode to others. More...
 
__STATIC_FORCEINLINE void __enable_irq (void)
 Enable IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_irq (void)
 Disable IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_ext_irq (void)
 Enable External IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_ext_irq (void)
 Disable External IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_timer_irq (void)
 Enable Timer IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_timer_irq (void)
 Disable Timer IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __enable_sw_irq (void)
 Enable software IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_sw_irq (void)
 Disable software IRQ Interrupts. More...
 
__STATIC_FORCEINLINE void __disable_core_irq (uint32_t irq)
 Disable Core IRQ Interrupt. More...
 
__STATIC_FORCEINLINE void __enable_core_irq (uint32_t irq)
 Enable Core IRQ Interrupt. More...
 
__STATIC_FORCEINLINE uint32_t __get_core_irq_pending (uint32_t irq)
 Get Core IRQ Interrupt Pending status. More...
 
__STATIC_FORCEINLINE void __clear_core_irq_pending (uint32_t irq)
 Clear Core IRQ Interrupt Pending status. More...
 
__STATIC_FORCEINLINE void __enable_irq_s (void)
 Enable IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_irq_s (void)
 Disable IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_ext_irq_s (void)
 Enable External IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_ext_irq_s (void)
 Disable External IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_timer_irq_s (void)
 Enable Timer IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_timer_irq_s (void)
 Disable Timer IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_sw_irq_s (void)
 Enable software IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_sw_irq_s (void)
 Disable software IRQ Interrupts in supervisor mode. More...
 
__STATIC_FORCEINLINE void __disable_core_irq_s (uint32_t irq)
 Disable Core IRQ Interrupt in supervisor mode. More...
 
__STATIC_FORCEINLINE void __enable_core_irq_s (uint32_t irq)
 Enable Core IRQ Interrupt in supervisor mode. More...
 
__STATIC_FORCEINLINE uint32_t __get_core_irq_pending_s (uint32_t irq)
 Get Core IRQ Interrupt Pending status in supervisor mode. More...
 
__STATIC_FORCEINLINE void __clear_core_irq_pending_s (uint32_t irq)
 Clear Core IRQ Interrupt Pending status in supervisor mode. More...
 
__STATIC_FORCEINLINE uint64_t __get_rv_cycle (void)
 Read whole 64 bits value of mcycle counter. More...
 
__STATIC_FORCEINLINE void __set_rv_cycle (uint64_t cycle)
 Set whole 64 bits value of mcycle counter. More...
 
__STATIC_FORCEINLINE uint64_t __get_rv_instret (void)
 Read whole 64 bits value of machine instruction-retired counter. More...
 
__STATIC_FORCEINLINE void __set_rv_instret (uint64_t instret)
 Set whole 64 bits value of machine instruction-retired counter. More...
 
__STATIC_FORCEINLINE uint64_t __get_rv_time (void)
 Read whole 64 bits value of real-time clock. More...
 
__STATIC_FORCEINLINE unsigned long __get_cluster_id (void)
 Get cluster id of current cluster. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_index (void)
 Get hart index of current cluster. More...
 
__STATIC_FORCEINLINE unsigned long __get_hart_id (void)
 Get hart id of current cluster. More...
 

Detailed Description

Functions to access the Core CSR Registers.

The following functions or macros provide access to Core CSR registers.

Macro Definition Documentation

◆ __RV_CSR_CLEAR

#define __RV_CSR_CLEAR (   csr,
  val 
)

CSR operation Macro for csrc instruction.

Set csr register to be csr_content & ~val

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrc instruction

Definition at line 606 of file core_feature_base.h.

◆ __RV_CSR_READ

#define __RV_CSR_READ (   csr)

CSR operation Macro for csrr instruction.

Read the content of csr register to __v and return it

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
Returns
the CSR register value

Definition at line 514 of file core_feature_base.h.

◆ __RV_CSR_READ_CLEAR

#define __RV_CSR_READ_CLEAR (   csr,
  val 
)

CSR operation Macro for csrrc instruction.

Read the content of csr register to __v, then set csr register to be __v & ~val, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrrc instruction
Returns
the CSR register value before written

Definition at line 588 of file core_feature_base.h.

◆ __RV_CSR_READ_SET

#define __RV_CSR_READ_SET (   csr,
  val 
)

CSR operation Macro for csrrs instruction.

Read the content of csr register to __v, then set csr register to be __v | val, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrrs instruction
Returns
the CSR register value before written

Definition at line 551 of file core_feature_base.h.

◆ __RV_CSR_SET

#define __RV_CSR_SET (   csr,
  val 
)

CSR operation Macro for csrs instruction.

Set csr register to be csr_content | val

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valMask value to be used wih csrs instruction

Definition at line 569 of file core_feature_base.h.

◆ __RV_CSR_SWAP

#define __RV_CSR_SWAP (   csr,
  val 
)

CSR operation Macro for csrrw instruction.

Read the content of csr register to __v, then write content of val into csr register, then return __v

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valvalue to store into the CSR register
Returns
the CSR register value before written

Definition at line 496 of file core_feature_base.h.

◆ __RV_CSR_WRITE

#define __RV_CSR_WRITE (   csr,
  val 
)

CSR operation Macro for csrw instruction.

Write the content of val to csr register

Parameters
csrCSR macro definition defined in Core CSR Registers, eg. CSR_MSTATUS
valvalue to store into the CSR register

Definition at line 532 of file core_feature_base.h.

Function Documentation

◆ __clear_core_irq_pending()

__STATIC_FORCEINLINE void __clear_core_irq_pending ( uint32_t  irq)

Clear Core IRQ Interrupt Pending status.

Clear Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 787 of file core_feature_base.h.

788 {
789  __RV_CSR_SET(CSR_MIP, 1 << irq);
790 }

References __RV_CSR_SET, and CSR_MIP.

◆ __clear_core_irq_pending_s()

__STATIC_FORCEINLINE void __clear_core_irq_pending_s ( uint32_t  irq)

Clear Core IRQ Interrupt Pending status in supervisor mode.

Clear Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 919 of file core_feature_base.h.

920 {
921  __RV_CSR_SET(CSR_SIP, 1 << irq);
922 }

References __RV_CSR_SET, and CSR_SIP.

◆ __disable_core_irq()

__STATIC_FORCEINLINE void __disable_core_irq ( uint32_t  irq)

Disable Core IRQ Interrupt.

Disable Core IRQ interrupt by clearing the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 754 of file core_feature_base.h.

755 {
756  __RV_CSR_CLEAR(CSR_MIE, 1 << irq);
757 }

References __RV_CSR_CLEAR, and CSR_MIE.

◆ __disable_core_irq_s()

__STATIC_FORCEINLINE void __disable_core_irq_s ( uint32_t  irq)

Disable Core IRQ Interrupt in supervisor mode.

Disable Core IRQ interrupt by clearing the irq bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 886 of file core_feature_base.h.

887 {
888  __RV_CSR_CLEAR(CSR_SIE, 1 << irq);
889 }

References __RV_CSR_CLEAR, and CSR_SIE.

◆ __disable_ext_irq()

__STATIC_FORCEINLINE void __disable_ext_irq ( void  )

Disable External IRQ Interrupts.

Disables External IRQ interrupts by clearing the MEIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 699 of file core_feature_base.h.

700 {
702 }

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MEIE.

◆ __disable_ext_irq_s()

__STATIC_FORCEINLINE void __disable_ext_irq_s ( void  )

Disable External IRQ Interrupts in supervisor mode.

Disables External IRQ interrupts by clearing the SEIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 831 of file core_feature_base.h.

832 {
834 }

References __RV_CSR_CLEAR, CSR_SIE, and MIE_SEIE.

◆ __disable_irq()

__STATIC_FORCEINLINE void __disable_irq ( void  )

Disable IRQ Interrupts.

Disables IRQ interrupts by clearing the MIE-bit in the MSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 677 of file core_feature_base.h.

678 {
680 }

References __RV_CSR_CLEAR, CSR_MSTATUS, and MSTATUS_MIE.

◆ __disable_irq_s()

__STATIC_FORCEINLINE void __disable_irq_s ( void  )

Disable IRQ Interrupts in supervisor mode.

Disables IRQ interrupts by clearing the SIE-bit in the SSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 809 of file core_feature_base.h.

810 {
812 }

References __RV_CSR_CLEAR, CSR_SSTATUS, and SSTATUS_SIE.

◆ __disable_sw_irq()

__STATIC_FORCEINLINE void __disable_sw_irq ( void  )

Disable software IRQ Interrupts.

Disables software IRQ interrupts by clearing the MSIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 743 of file core_feature_base.h.

744 {
746 }

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MSIE.

◆ __disable_sw_irq_s()

__STATIC_FORCEINLINE void __disable_sw_irq_s ( void  )

Disable software IRQ Interrupts in supervisor mode.

Disables software IRQ interrupts by clearing the SSIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 875 of file core_feature_base.h.

876 {
878 }

References __RV_CSR_CLEAR, CSR_SIE, and MIE_SSIE.

◆ __disable_timer_irq()

__STATIC_FORCEINLINE void __disable_timer_irq ( void  )

Disable Timer IRQ Interrupts.

Disables Timer IRQ interrupts by clearing the MTIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 721 of file core_feature_base.h.

722 {
724 }

References __RV_CSR_CLEAR, CSR_MIE, and MIE_MTIE.

◆ __disable_timer_irq_s()

__STATIC_FORCEINLINE void __disable_timer_irq_s ( void  )

Disable Timer IRQ Interrupts in supervisor mode.

Disables Timer IRQ interrupts by clearing the STIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 853 of file core_feature_base.h.

854 {
856 }

References __RV_CSR_CLEAR, CSR_SIE, and MIE_STIE.

◆ __enable_core_irq()

__STATIC_FORCEINLINE void __enable_core_irq ( uint32_t  irq)

Enable Core IRQ Interrupt.

Enable Core IRQ interrupt by setting the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 765 of file core_feature_base.h.

766 {
767  __RV_CSR_SET(CSR_MIE, 1 << irq);
768 }

References __RV_CSR_SET, and CSR_MIE.

◆ __enable_core_irq_s()

__STATIC_FORCEINLINE void __enable_core_irq_s ( uint32_t  irq)

Enable Core IRQ Interrupt in supervisor mode.

Enable Core IRQ interrupt by setting the irq bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 897 of file core_feature_base.h.

898 {
899  __RV_CSR_SET(CSR_SIE, 1 << irq);
900 }

References __RV_CSR_SET, and CSR_SIE.

◆ __enable_ext_irq()

__STATIC_FORCEINLINE void __enable_ext_irq ( void  )

Enable External IRQ Interrupts.

Enables External IRQ interrupts by setting the MEIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 688 of file core_feature_base.h.

689 {
691 }

References __RV_CSR_SET, CSR_MIE, and MIE_MEIE.

◆ __enable_ext_irq_s()

__STATIC_FORCEINLINE void __enable_ext_irq_s ( void  )

Enable External IRQ Interrupts in supervisor mode.

Enables External IRQ interrupts by setting the SEIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 820 of file core_feature_base.h.

821 {
823 }

References __RV_CSR_SET, CSR_SIE, and MIE_SEIE.

◆ __enable_irq()

__STATIC_FORCEINLINE void __enable_irq ( void  )

Enable IRQ Interrupts.

Enables IRQ interrupts by setting the MIE-bit in the MSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 666 of file core_feature_base.h.

667 {
669 }

References __RV_CSR_SET, CSR_MSTATUS, and MSTATUS_MIE.

◆ __enable_irq_s()

__STATIC_FORCEINLINE void __enable_irq_s ( void  )

Enable IRQ Interrupts in supervisor mode.

Enables IRQ interrupts by setting the SIE-bit in the SSTATUS Register.

Remarks
Can only be executed in Privileged modes.

Definition at line 798 of file core_feature_base.h.

799 {
801 }

References __RV_CSR_SET, CSR_SSTATUS, and SSTATUS_SIE.

◆ __enable_sw_irq()

__STATIC_FORCEINLINE void __enable_sw_irq ( void  )

Enable software IRQ Interrupts.

Enables software IRQ interrupts by setting the MSIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 732 of file core_feature_base.h.

733 {
735 }

References __RV_CSR_SET, CSR_MIE, and MIE_MSIE.

◆ __enable_sw_irq_s()

__STATIC_FORCEINLINE void __enable_sw_irq_s ( void  )

Enable software IRQ Interrupts in supervisor mode.

Enables software IRQ interrupts by setting the SSIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 864 of file core_feature_base.h.

865 {
867 }

References __RV_CSR_SET, CSR_MIE, and MIE_SSIE.

◆ __enable_timer_irq()

__STATIC_FORCEINLINE void __enable_timer_irq ( void  )

Enable Timer IRQ Interrupts.

Enables Timer IRQ interrupts by setting the MTIE-bit in the MIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 710 of file core_feature_base.h.

711 {
713 }

References __RV_CSR_SET, CSR_MIE, and MIE_MTIE.

◆ __enable_timer_irq_s()

__STATIC_FORCEINLINE void __enable_timer_irq_s ( void  )

Enable Timer IRQ Interrupts in supervisor mode.

Enables Timer IRQ interrupts by setting the STIE-bit in the SIE Register.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 842 of file core_feature_base.h.

843 {
845 }

References __RV_CSR_SET, CSR_SIE, and MIE_STIE.

◆ __get_cluster_id()

__STATIC_FORCEINLINE unsigned long __get_cluster_id ( void  )

Get cluster id of current cluster.

This function will get cluster id of current cluster in a multiple cluster system

Returns
The cluster id of current cluster
Remarks
mhartid bit 15-8 is designed for cluster id in nuclei subsystem reference design
Attention
function is allowed in machine mode only

Definition at line 1047 of file core_feature_base.h.

1048 {
1049  unsigned long id;
1050 
1051  id = (__RV_CSR_READ(CSR_MHARTID) >> 8) & 0xFF;
1052  return id;
1053 }

References __RV_CSR_READ, and CSR_MHARTID.

◆ __get_core_irq_pending()

__STATIC_FORCEINLINE uint32_t __get_core_irq_pending ( uint32_t  irq)

Get Core IRQ Interrupt Pending status.

Get Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 776 of file core_feature_base.h.

777 {
778  return ((__RV_CSR_READ(CSR_MIP) >> irq) & 0x1);
779 }

References __RV_CSR_READ, and CSR_MIP.

◆ __get_core_irq_pending_s()

__STATIC_FORCEINLINE uint32_t __get_core_irq_pending_s ( uint32_t  irq)

Get Core IRQ Interrupt Pending status in supervisor mode.

Get Core IRQ interrupt pending status of irq bit.

Remarks
Can only be executed in Privileged modes, available for plic interrupt mode.

Definition at line 908 of file core_feature_base.h.

909 {
910  return ((__RV_CSR_READ(CSR_SIP) >> irq) & 0x1);
911 }

References __RV_CSR_READ, and CSR_SIP.

◆ __get_hart_id()

__STATIC_FORCEINLINE unsigned long __get_hart_id ( void  )

Get hart id of current cluster.

This function will get hart id of current cluster in a multiple cluster system

Returns
The hart id of current cluster
Remarks
it will return full hartid not part of it for reference subsystem design, if your reference subsystem design has hartid offset, please define __HARTID_OFFSET in <Device>.h
Attention
function is allowed in machine mode only

Definition at line 1083 of file core_feature_base.h.

1084 {
1085  unsigned long id;
1086  id = __RV_CSR_READ(CSR_MHARTID);
1087  return id;
1088 }

References __RV_CSR_READ, and CSR_MHARTID.

◆ __get_hart_index()

__STATIC_FORCEINLINE unsigned long __get_hart_index ( void  )

Get hart index of current cluster.

This function will get hart index of current cluster in a multiple cluster system, hart index is hartid - hartid offset, for example if your hartid is 1, and offset is 1, then hart index is 0

Returns
The hart index of current cluster
Attention
function is allowed in machine mode only

Definition at line 1063 of file core_feature_base.h.

1064 {
1065  unsigned long id;
1066 #ifdef __HARTID_OFFSET
1067  id = __RV_CSR_READ(CSR_MHARTID) - __HARTID_OFFSET;
1068 #else
1069  id = __RV_CSR_READ(CSR_MHARTID);
1070 #endif
1071  return id;
1072 }

References __RV_CSR_READ, and CSR_MHARTID.

◆ __get_rv_cycle()

__STATIC_FORCEINLINE uint64_t __get_rv_cycle ( void  )

Read whole 64 bits value of mcycle counter.

This function will read the whole 64 bits of MCYCLE register

Returns
The whole 64 bits value of MCYCLE
Remarks
It will work for both RV32 and RV64 to get full 64bits value of MCYCLE

Definition at line 930 of file core_feature_base.h.

931 {
932 #if __RISCV_XLEN == 32
933  volatile uint32_t high0, low, high;
934  uint64_t full;
935 
936  high0 = __RV_CSR_READ(CSR_MCYCLEH);
937  low = __RV_CSR_READ(CSR_MCYCLE);
938  high = __RV_CSR_READ(CSR_MCYCLEH);
939  if (high0 != high) {
940  low = __RV_CSR_READ(CSR_MCYCLE);
941  }
942  full = (((uint64_t)high) << 32) | low;
943  return full;
944 #elif __RISCV_XLEN == 64
945  return (uint64_t)__RV_CSR_READ(CSR_MCYCLE);
946 #else // TODO Need cover for XLEN=128 case in future
947  return (uint64_t)__RV_CSR_READ(CSR_MCYCLE);
948 #endif
949 }

References __RV_CSR_READ, CSR_MCYCLE, and CSR_MCYCLEH.

Referenced by __get_hpm_counter().

◆ __get_rv_instret()

__STATIC_FORCEINLINE uint64_t __get_rv_instret ( void  )

Read whole 64 bits value of machine instruction-retired counter.

This function will read the whole 64 bits of MINSTRET register

Returns
The whole 64 bits value of MINSTRET
Remarks
It will work for both RV32 and RV64 to get full 64bits value of MINSTRET

Definition at line 974 of file core_feature_base.h.

975 {
976 #if __RISCV_XLEN == 32
977  volatile uint32_t high0, low, high;
978  uint64_t full;
979 
980  high0 = __RV_CSR_READ(CSR_MINSTRETH);
983  if (high0 != high) {
985  }
986  full = (((uint64_t)high) << 32) | low;
987  return full;
988 #elif __RISCV_XLEN == 64
989  return (uint64_t)__RV_CSR_READ(CSR_MINSTRET);
990 #else // TODO Need cover for XLEN=128 case in future
991  return (uint64_t)__RV_CSR_READ(CSR_MINSTRET);
992 #endif
993 }

References __RV_CSR_READ, CSR_MINSTRET, and CSR_MINSTRETH.

Referenced by __get_hpm_counter().

◆ __get_rv_time()

__STATIC_FORCEINLINE uint64_t __get_rv_time ( void  )

Read whole 64 bits value of real-time clock.

This function will read the whole 64 bits of TIME register

Returns
The whole 64 bits value of TIME CSR
Remarks
It will work for both RV32 and RV64 to get full 64bits value of TIME
Attention
only available when user mode available

Definition at line 1019 of file core_feature_base.h.

1020 {
1021 #if __RISCV_XLEN == 32
1022  volatile uint32_t high0, low, high;
1023  uint64_t full;
1024 
1025  high0 = __RV_CSR_READ(CSR_TIMEH);
1026  low = __RV_CSR_READ(CSR_TIME);
1027  high = __RV_CSR_READ(CSR_TIMEH);
1028  if (high0 != high) {
1029  low = __RV_CSR_READ(CSR_TIME);
1030  }
1031  full = (((uint64_t)high) << 32) | low;
1032  return full;
1033 #elif __RISCV_XLEN == 64
1034  return (uint64_t)__RV_CSR_READ(CSR_TIME);
1035 #else // TODO Need cover for XLEN=128 case in future
1036  return (uint64_t)__RV_CSR_READ(CSR_TIME);
1037 #endif
1038 }

References __RV_CSR_READ, CSR_TIME, and CSR_TIMEH.

◆ __set_rv_cycle()

__STATIC_FORCEINLINE void __set_rv_cycle ( uint64_t  cycle)

Set whole 64 bits value of mcycle counter.

This function will set the whole 64 bits of MCYCLE register

Remarks
It will work for both RV32 and RV64 to set full 64bits value of MCYCLE

Definition at line 956 of file core_feature_base.h.

957 {
958 #if __RISCV_XLEN == 32
959  __RV_CSR_WRITE(CSR_MCYCLE, 0); // prevent carry
960  __RV_CSR_WRITE(CSR_MCYCLEH, (uint32_t)(cycle >> 32));
961  __RV_CSR_WRITE(CSR_MCYCLE, (uint32_t)(cycle));
962 #elif __RISCV_XLEN == 64
963  __RV_CSR_WRITE(CSR_MCYCLE, cycle);
964 #else // TODO Need cover for XLEN=128 case in future
965 #endif
966 }

References __RV_CSR_WRITE, CSR_MCYCLE, and CSR_MCYCLEH.

◆ __set_rv_instret()

__STATIC_FORCEINLINE void __set_rv_instret ( uint64_t  instret)

Set whole 64 bits value of machine instruction-retired counter.

This function will set the whole 64 bits of MINSTRET register

Remarks
It will work for both RV32 and RV64 to set full 64bits value of MINSTRET

Definition at line 1000 of file core_feature_base.h.

1001 {
1002 #if __RISCV_XLEN == 32
1003  __RV_CSR_WRITE(CSR_MINSTRET, 0); // prevent carry
1004  __RV_CSR_WRITE(CSR_MINSTRETH, (uint32_t)(instret >> 32));
1005  __RV_CSR_WRITE(CSR_MINSTRET, (uint32_t)(instret));
1006 #elif __RISCV_XLEN == 64
1007  __RV_CSR_WRITE(CSR_MINSTRET, instret);
1008 #else // TODO Need cover for XLEN=128 case in future
1009 #endif
1010 }

References __RV_CSR_WRITE, CSR_MINSTRET, and CSR_MINSTRETH.

◆ __switch_mode()

__STATIC_FORCEINLINE void __switch_mode ( uint8_t  mode,
uintptr_t  stack,
void(*)(void)  entry_point 
)

switch privilege from machine mode to others.

Execute into entry_point in mode(supervisor or user) with given stack

Parameters
modeprivilege mode
stackpredefined stack, size should set enough
entry_pointa function pointer to execute

Definition at line 638 of file core_feature_base.h.

639 {
640  unsigned long val = 0;
641 
642  /* Set MPP to the requested privilege mode */
643  val = __RV_CSR_READ(CSR_MSTATUS);
644  val = __RV_INSERT_FIELD(val, MSTATUS_MPP, mode);
645 
646  /* Set previous MIE disabled */
647  val = __RV_INSERT_FIELD(val, MSTATUS_MPIE, 0);
648 
650 
651  /* Set the entry point in MEPC */
652  __RV_CSR_WRITE(CSR_MEPC, (unsigned long)entry_point);
653 
654  /* Set the register file */
655  __ASM volatile("mv sp, %0" ::"r"(stack));
656 
657  __ASM volatile("mret");
658 }

References __ASM, __RV_CSR_READ, __RV_CSR_WRITE, CSR_MEPC, CSR_MSTATUS, MSTATUS_MPIE, and MSTATUS_MPP.

CSR_SSTATUS
#define CSR_SSTATUS
Definition: riscv_encoding.h:531
SSTATUS_SIE
#define SSTATUS_SIE
Definition: riscv_encoding.h:79
CSR_MHARTID
#define CSR_MHARTID
Definition: riscv_encoding.h:773
MIE_MTIE
#define MIE_MTIE
Definition: riscv_encoding.h:166
__RV_CSR_CLEAR
#define __RV_CSR_CLEAR(csr, val)
CSR operation Macro for csrc instruction.
Definition: core_feature_base.h:606
CSR_MCYCLEH
#define CSR_MCYCLEH
Definition: riscv_encoding.h:851
MIE_MEIE
#define MIE_MEIE
Definition: riscv_encoding.h:169
__RV_CSR_WRITE
#define __RV_CSR_WRITE(csr, val)
CSR operation Macro for csrw instruction.
Definition: core_feature_base.h:532
CSR_TIME
#define CSR_TIME
Definition: riscv_encoding.h:497
CSR_MINSTRET
#define CSR_MINSTRET
Definition: riscv_encoding.h:711
CSR_MIE
#define CSR_MIE
Definition: riscv_encoding.h:600
MSTATUS_MPIE
#define MSTATUS_MPIE
Definition: riscv_encoding.h:44
MSTATUS_MPP
#define MSTATUS_MPP
Definition: riscv_encoding.h:47
CSR_TIMEH
#define CSR_TIMEH
Definition: riscv_encoding.h:784
CSR_SIE
#define CSR_SIE
Definition: riscv_encoding.h:534
CSR_SIP
#define CSR_SIP
Definition: riscv_encoding.h:547
MSTATUS_MIE
#define MSTATUS_MIE
Definition: riscv_encoding.h:40
CSR_MINSTRETH
#define CSR_MINSTRETH
Definition: riscv_encoding.h:852
MIE_STIE
#define MIE_STIE
Definition: riscv_encoding.h:164
CSR_MCYCLE
#define CSR_MCYCLE
Definition: riscv_encoding.h:710
MIE_SEIE
#define MIE_SEIE
Definition: riscv_encoding.h:167
__RV_CSR_READ
#define __RV_CSR_READ(csr)
CSR operation Macro for csrr instruction.
Definition: core_feature_base.h:514
CSR_MIP
#define CSR_MIP
Definition: riscv_encoding.h:614
MIE_SSIE
#define MIE_SSIE
Definition: riscv_encoding.h:161
__RV_CSR_SET
#define __RV_CSR_SET(csr, val)
CSR operation Macro for csrs instruction.
Definition: core_feature_base.h:569
CSR_MSTATUS
#define CSR_MSTATUS
Definition: riscv_encoding.h:596
__ASM
#define __ASM
Pass information from the compiler to the assembler.
Definition: nmsis_gcc.h:55
MIE_MSIE
#define MIE_MSIE
Definition: riscv_encoding.h:163
CSR_MEPC
#define CSR_MEPC
Definition: riscv_encoding.h:610