Software APIs
Macros | Functions
csr.h File Reference

(69c572b)

Ibex Control and Status Register (CSR) interface. More...

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include "sw/device/lib/base/csr_registers.h"
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/base/stdasm.h"

Go to the source code of this file.

Macros

#define CSR_FORCE_CONST_EXPR(x)   asm volatile("" ::"i"(x))
 Define the implementation macros. More...
 
#define CSR_READ_IMPL(csr, dest)
 
#define CSR_WRITE_IMPL(csr, val)
 
#define CSR_SET_BITS_IMPL(csr, mask)
 
#define CSR_CLEAR_BITS_IMPL(csr, mask)
 
#define CSR_READ(csr, dest)   CSR_READ_IMPL(csr, dest)
 Read the value of a CSR and place the result into the location pointed to by dest. More...
 
#define CSR_WRITE(csr, val)   CSR_WRITE_IMPL(csr, val)
 Write a value to a CSR. More...
 
#define CSR_SET_BITS(csr, mask)   CSR_SET_BITS_IMPL(csr, mask)
 Set masked bits in the CSR. More...
 
#define CSR_CLEAR_BITS(csr, mask)   CSR_CLEAR_BITS_IMPL(csr, mask)
 Clear masked bits in the CSR. More...
 

Functions

uint32_t mock_csr_read (uint32_t addr)
 
void mock_csr_write (uint32_t addr, uint32_t value)
 
void mock_csr_set_bits (uint32_t addr, uint32_t mask)
 
void mock_csr_clear_bits (uint32_t addr, uint32_t mask)
 

Detailed Description

Ibex Control and Status Register (CSR) interface.

A set of macros that provide both read and modify operations on Ibex CSRs. Compiling translation units that include this header file with -DMOCK_CSR will result in the CSR operations being replaced with a mocked implementation.

Definition in file csr.h.

Macro Definition Documentation

◆ CSR_CLEAR_BITS

#define CSR_CLEAR_BITS (   csr,
  mask 
)    CSR_CLEAR_BITS_IMPL(csr, mask)

Clear masked bits in the CSR.

Equivalent to:

csr &= ~mask

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
maskMask containing the bits to clear.

Definition at line 171 of file csr.h.

◆ CSR_CLEAR_BITS_IMPL

#define CSR_CLEAR_BITS_IMPL (   csr,
  mask 
)
Value:
do { \
static_assert(sizeof(mask) == sizeof(uint32_t), \
"mask must be a 4-byte value"); \
CSR_FORCE_CONST_EXPR(csr); \
mock_csr_clear_bits(csr, mask); \
} while (false)

Definition at line 83 of file csr.h.

◆ CSR_FORCE_CONST_EXPR

#define CSR_FORCE_CONST_EXPR (   x)    asm volatile("" ::"i"(x))

Define the implementation macros.

The implementation used depends on whether the CSR library is providing a real or a mocked interface. Macro to check that an argument is a constant expression at compile time.

The real implementations of CSR operations require the CSR address to be a constant expression. Using this macro allows the same constraint to be applied when using the mocked implementations.

Note: could also use a static assertion for this but an inline asm immediate constraint means the error for mocked and real implementation should be very similar.

Definition at line 49 of file csr.h.

◆ CSR_READ

#define CSR_READ (   csr,
  dest 
)    CSR_READ_IMPL(csr, dest)

Read the value of a CSR and place the result into the location pointed to by dest.

Equivalent to:

*dest = csr

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
[out]destPointer to a variable where the value of the named CSR will be written to.

Definition at line 135 of file csr.h.

◆ CSR_READ_IMPL

#define CSR_READ_IMPL (   csr,
  dest 
)
Value:
do { \
static_assert(sizeof(*dest) == sizeof(uint32_t), \
"dest must point to a 4-byte variable"); \
CSR_FORCE_CONST_EXPR(csr); \
*dest = mock_csr_read(csr); \
} while (false)

Definition at line 53 of file csr.h.

◆ CSR_SET_BITS

#define CSR_SET_BITS (   csr,
  mask 
)    CSR_SET_BITS_IMPL(csr, mask)

Set masked bits in the CSR.

Equivalent to:

csr |= mask

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
maskMask containing the bits to set.

Definition at line 159 of file csr.h.

◆ CSR_SET_BITS_IMPL

#define CSR_SET_BITS_IMPL (   csr,
  mask 
)
Value:
do { \
static_assert(sizeof(mask) == sizeof(uint32_t), \
"mask must be a 4-byte value"); \
CSR_FORCE_CONST_EXPR(csr); \
mock_csr_set_bits(csr, mask); \
} while (false)

Definition at line 73 of file csr.h.

◆ CSR_WRITE

#define CSR_WRITE (   csr,
  val 
)    CSR_WRITE_IMPL(csr, val)

Write a value to a CSR.

Equivalent to:

csr = val

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
valThe value to write to the named CSR.

Definition at line 147 of file csr.h.

◆ CSR_WRITE_IMPL

#define CSR_WRITE_IMPL (   csr,
  val 
)
Value:
do { \
static_assert(sizeof(val) == sizeof(uint32_t), \
"val must be a 4-byte value"); \
CSR_FORCE_CONST_EXPR(csr); \
mock_csr_write(csr, val); \
} while (false)

Definition at line 63 of file csr.h.