Core CSR Register Access

Click Nuclei Core CSR to learn about Core CSR in Nuclei ISA Spec.

group NMSIS_Core_CSR_Register_Access

Functions to access the Core CSR Registers.

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

Defines

__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
Returns

the CSR register value before written

__RV_CSR_READ(csr)

CSR operation Macro for csrr instruction.

Read the content of csr register to __v and return it

Parameters
Returns

the CSR register value

__RV_CSR_WRITE(csr, val)

CSR operation Macro for csrw instruction.

Write the content of val to csr register

Parameters
__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
Returns

the CSR register value before written

__RV_CSR_SET(csr, val)

CSR operation Macro for csrs instruction.

Set csr register to be csr_content | val

Parameters
__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
Returns

the CSR register value before written

__RV_CSR_CLEAR(csr, val)

CSR operation Macro for csrc instruction.

Set csr register to be csr_content & ~val

Parameters

Functions

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

switch privilege from machine mode to others.

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

Parameters
  • mode – privilege mode

  • stack – predefined stack, size should set enough

  • entry_point – a function pointer to execute

__STATIC_FORCEINLINE void __enable_irq (void)

Enable IRQ Interrupts.

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

Remark

Can only be executed in Privileged modes.

__STATIC_FORCEINLINE void __disable_irq (void)

Disable IRQ Interrupts.

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

Remark

Can only be executed in Privileged modes.

__STATIC_FORCEINLINE void __enable_ext_irq (void)

Enable External IRQ Interrupts.

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

Remark

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

__STATIC_FORCEINLINE void __disable_ext_irq (void)

Disable External IRQ Interrupts.

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

Remark

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

__STATIC_FORCEINLINE void __enable_timer_irq (void)

Enable Timer IRQ Interrupts.

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

Remark

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

__STATIC_FORCEINLINE void __disable_timer_irq (void)

Disable Timer IRQ Interrupts.

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

Remark

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

__STATIC_FORCEINLINE void __enable_sw_irq (void)

Enable software IRQ Interrupts.

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

Remark

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

__STATIC_FORCEINLINE void __disable_sw_irq (void)

Disable software IRQ Interrupts.

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

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

Can only be executed in Privileged modes.

__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.

Remark

Can only be executed in Privileged modes.

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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.

Remark

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

__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

Remark

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

Returns

The whole 64 bits value of MCYCLE

__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

Remark

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

__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

Remark

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

Returns

The whole 64 bits value of MINSTRET

__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

Remark

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

__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

Remark

It will work for both RV32 and RV64 to get full 64bits value of TIME

Attention

only available when user mode available

Returns

The whole 64 bits value of TIME CSR

__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

Remark

mhartid bit 15-8 is designed for cluster id in nuclei subsystem reference design

Attention

function is allowed in machine mode only

Returns

The cluster id of current cluster

__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

Attention

function is allowed in machine mode only

Returns

The hart index of current cluster

__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

Remark

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

Returns

The hart id of current cluster