The xPSR (program status register) is one of the key registers in the ARM Cortex-M processor that contains status information about the processor state. It provides details on the current state of the application program being executed as well as the result of the previously executed instruction. The ‘x’ in xPSR stands for ‘extended’ as it is an extended version of the PSR used in older ARM processors.
Overview of xPSR
The xPSR register is 32-bits wide and contains the current state information of various processor controls like the instruction set state, interrupt disables, saturation flag as well as the condition flags. It is updated automatically by the processor after the execution of each instruction to reflect the new program state. The xPSR register can be read using the MRS instruction but cannot be directly written to using the MSR instruction. Its value needs to be modified using specific instructions like CPS, SETEND, CPSIE, etc.
Bits in the xPSR register
The xPSR register contains the following bit fields:
- N, Z, C, V bits (bits 31-28): These are the negative, zero, carry and overflow flags used for conditional execution. They are set or cleared automatically based on the result of arithmetic or logical instructions.
- Q bit (bit 27): The saturation flag. It is set if saturation occurs during execution of a Q-flagged instruction like QADD.
- ICI bits (bits 26-25): Reserved bits in Cortex-M processors. Used in other ARM architectures.
- T bit (bit 24): Thumb state bit. Indicates if the processor is executing in Arm or Thumb mode.
- IT bits (bits 15-10): IT block state bits for Thumb conditional execution.
- M, F bits (bits 4-0): Mode and FIQ mask bits that determine the current processor mode.
Key Uses of xPSR
Here are some of the main uses of the xPSR register in Cortex-M processors:
- Determining processor mode – The M and F bits in xPSR indicate if the processor is in Thread, Handler, or Privileged mode.
- Checking condition flags – The N, Z, C, V bits reflect the status flags which can be tested using conditional instructions.
- Identifying Thumb state – The T bit indicates if the processor is executing Thumb or ARM instructions.
- Detecting interrupts – Changes in xPSR mode bits can identify when exceptions and interrupts occur.
- Saving context – xPSR is part of the context saved during exception handling to preserve the state.
- Monitoring saturation – The Q bit shows if any saturation happened for Q-flagged arithmetic instructions.
Difference between xPSR and IPSR
The IPSR (Interrupt Program Status Register) is similar to the xPSR but has some differences:
- IPSR contains only the interrupt number bits and does not have the other xPSR fields like flags, or mode bits.
- IPSR is accessed using the MRS and MSR instructions but xPSR can only be read using MRS.
- IPSR is updated with the interrupt number during exception handling while xPSR contains the program status.
- xPSR is used to save context during exception return while IPSR is used to identify the pending interrupt.
Typical Operations using xPSR
Here are some typical operations involving the xPSR register on Cortex-M:
- Reading xPSR – Use MRS instruction like ‘MRS R0, xPSR’ to save xPSR into a general purpose register.
- Checking for zero – Test the Z flag using ‘ANDS R0, xPSR #ZFlagMask’ to see if last result was zero.
- Changing to handler mode – Write to the mode bits using ‘MSR CONTROL, #0x3’ to switch to handler mode.
- Identifying exceptions – Check the mode bits to see if an exception occurred when waking up from sleep.
- Context saving – Save xPSR along with other general registers during exception handling.
- Conditional execution – Use the IT instruction with condition flags to implement Thumb if-then blocks.
Tips for Using xPSR
Here are some tips for working with xPSR register:
- Use MRS and MSR instructions to read and modify xPSR as it cannot be accessed directly.
- Be careful when changing mode bits – it can trigger unintended exceptions.
- Save and restore xPSR during context save and restore operations.
- Check for specific flags like Z, C, V instead of whole xPSR when possible.
- Use xPSR flags for conditional execution within IF-THEN blocks.
- Monitor mode bits to detect processor state changes due to interrupts.
- Many debuggers can show xPSR contents during program trace and debugging.
Conclusion
The xPSR register is a key component of the ARM Cortex-M processor containing vital status information about the current program state. Understanding its various fields like the flags, mode bits, and exceptions allows developers to write efficient conditional code, monitor interrupts, save context during handling, and debug the application effectively. Referencing and manipulating the xPSR at the right points in the program flow is an important skill for mastering ARM Cortex-M application development.