The Cortex-M0 processor features a reduced instruction set computer (RISC) architecture optimized for low-power operation. It has a 32-bit instruction set that provides a balance between code density and performance. Some key features of the Cortex-M0 instruction set include:
Data Processing Instructions
The Cortex-M0 supports common data processing instructions like addition, subtraction, logical operations, comparisons, and moves. These allow basic arithmetic, logical, and data movement operations on registers and immediate values. Some examples include:
- ADD – Add two registers or add register and immediate value
- AND – Bitwise AND of two registers
- CMP – Compare two registers or register against immediate
- MOV – Move between registers or load immediate value to register
Load/Store Instructions
Data can be loaded from memory into registers or stored from registers into memory using load and store instructions. Some load/store instructions supported:
- LDR – Load word or byte from memory
- STR – Store word or byte into memory
- LDM – Load multiple registers from memory
- STM – Store multiple registers to memory
Loads and stores use addresses composed from a base register and optional offset. Addressing modes like pre-indexed, post-indexed, and offset are supported.
Branch Instructions
Branch instructions alter program flow by changing the instruction fetch path. The Cortex-M0 supports both conditional and unconditional branches:
- B – Unconditional branch
- BX – Unconditional branch and exchange instruction set
- BEQ – Branch if equal
- BNE – Branch if not equal
- BCS – Branch if carry set
Software routines can be called using branch instructions. Link registers are provided to store return addresses.
Other Instructions
Some other instructions supported by the Cortex-M0 include:
- CBNZ/CBZ – Compare and branch if not zero/Compare and branch if zero
- PUSH/POP – Push/pop registers to/from stack
- BL/BLX – Call subroutines
- IT – If-Then instruction allows conditionally executing up to 4 instructions
- REV – Reverse byte order in register
- REV16 – Reverse byte order in 16-bit halfword
Instruction Encodings
Instructions are encoded as 32-bit values. Some features of the encoding:
- Highly regular encoding for most instructions
- Main opcode field identifies instruction class
- Additional operand fields encode operands
- Immediate operands directly encoded in instruction
- Register operands identified using 4-bit fields
- Memory access instructions encode addressing mode
The regular encodings maximize code density. PC-relative branches and branch with link (BL) instructions have special condensed encodings to save space.
Instruction Set Optimization
The Cortex-M0 instruction set is optimized for microcontroller applications in several ways:
- Emphasis on low-power operation
- High code density to minimize memory usage
- Small number of core registers to reduce chip complexity
- Load/store architecture minimizes power hungry register accesses
- Few instructions types reduces decode logic
- Short instructions maximize performance in single-cycle execution
- Limited addressing modes simplify address generation
- No support for unaligned memory access reduces complexity
The instruction set strikes a balance between performance, power efficiency, and minimizing implementation cost/complexity.
Control Instructions
The Cortex-M0 provides dedicated control instructions for managing the processor and interrupts:
- SEV – Send event signal to wake up processor
- WFE – Wait for event signal
- WFI – Wait for interrupt by suspending execution
- ISB – Instruction synchronization barrier
- MSR/MRS – Move special register to GP register or vice versa
Control instructions allow optimizing for low-power operation when waiting for events or I/O.
Memory Protection Unit
The Memory Protection Unit (MPU) provides instruction and data memory access control:
- Up to 8 memory regions can be configured
- Memory attributes like execute-never, privileged read-only set per region
- Background region provides default attributes
- Optional MPU support, can be disabled to reduce cost
The MPU prevents unauthorized or accidental access of memory regions for increased robustness.
Debug Support
The Cortex-M0 includes debug components for debugging software:
- Debug Access Port (DAP) for debug probe connection
- Breakpoints and watchpoints to halt execution
- Processor core registers visible in halted debug state
- Optional Embedded Trace Macrocell (ETM) for instruction trace
This debug support enables real-time inspection of software execution and facilitates identifying and fixing bugs.
Interrupt Handling
The Cortex-M0 has extensive support for fast, low-latency interrupt handling:
- Configurable Nested Vectored Interrupt Controller (NVIC)
- Up to 32 external interrupts with configurable priorities
- Low interrupt latency of 10s of cycles
- Automatically saved context on interrupt entry
- Tail-chaining of interrupts with no overhead
This interrupt handling architecture makes the Cortex-M0 well-suited for real-time embedded applications requiring fast response to external events.
Thumg2 Extensions
Some Cortex-M0 variants include the Thumb-2 instruction set extensions:
- 16-bit and 32-bit instruction support
- Additional 32-bit instructions like Load/Store Multiple
- 16-bit instruction backward compatibility
- Improved code density from 32-bit instruction encodings
The Thumb-2 extensions provide additional performance and efficiency while maintaining full compatibility with previous 16-bit Thumb code.