Cortex-M0+ CPU Core
Cortex-M0+ CPU Core
1
Overview
Cortex-M0+ Processor Core Registers
References
DDI0419C Architecture ARMv6-M Reference Manual
2
Microcontroller vs. Microprocessor
Both have a CPU core to execute
instructions
Microcontroller has peripherals for
embedded interfacing and control
Analog
Non-logic level
signals
Timing
Clock generators
Communications
point to point
network
Reliability
and safety
3
Cortex-M0+ Core
4
Architectures and Memory Speed
Load/Store Architecture
Developed to simplify CPU design and improve performance
Memory wall: CPUs keep getting faster than memory
Memory accesses slow down CPU, limit compiler optimizations
Change instruction set to make most instructions independent of memory
Data processing instructions can access registers only
1. Load data into the registers
2. Process the data
3. Store results back into memory
More effective when more registers are available
Register/Memory Architecture
Data processing instructions can access memory or registers
Memory wall is not very high at lower CPU speeds (e.g. under 50 MHz)
5
ARM Processor Core Registers
R0-R12 - General purpose, for data processing
SP - Stack pointer (R13)
Can refer to one of two SPs
Main Stack Pointer (MSP)
Process Stack Pointer (PSP)
Uses MSP initially, and in Handler mode
In Thread mode, can select either MSP or PSP
using SPSEL flag in CONTROL register.
LR - Link Register (R14)
Holds return address when called with Branch &
Link instruction (B&L)
PC - program counter (R15)
6
Operating Modes
Reset
Thread
Mode.
MSP or PSP.
Exception Starting
Processing Exception
Completed Processing
Handler
Mode
MSP
Which SP is active depends on operating mode, and SPSEL (CONTROL register bit 1)
SPSEL == 0: MSP
SPSEL == 1: PSP
7
ARM Program Status Register
8
ARM Processor Core Registers
PRIMASK - Exception mask register
Bit 0: PM Flag
Set to 1 to prevent activation of all exceptions with configurable priority
Access using CPS, MSR and MRS instructions
Use to prevent data race conditions with code needing atomicity
CONTROL
Bit 1: SPSEL flag
Selects SP when in thread mode: MSP (0) or PSP (1)
Bit 0: nPRIV flag
Defines whether thread mode is privileged (0) or unprivileged (1)
With OS environment,
Threads use PSP
OS and exception handlers (ISRs) use MSP
9
Memory Maps For Cortex M0+ and MCU
KL25Z128VLK4
0x2000_2FFF
SRAM_U (3/4)
16 KB SRAM
0x2000_0000
SRAM_L (1/4)
0x1FFF_F000
Some RAM is located in
Code segment, allowing
code to run from RAM
to allow flash
reprogramming or for
0x0001_FFFF better speed on faster
systems
128KB Flash
0x0000_0000
10
Endianness Memory
7 0
For a multi-byte value, in what
Address A B0 LSB
order are the bytes stored? Register
A+1 B1 31 24 23 16 15 8 7 0
A+2 B2 B3 B2 B1 B0
Little-Endian: Start with least- A+3 B3 MSB
significant byte
Memory
7 0
11
ARMv6-M Endianness
12
Different Instruction Sets for Different Design Spaces?
13
The Memory Wall
https://en.wikipedia.org/wiki/ARM_Cortex-M#Instruction_sets
16
Reference for ARM Instruction Set Architecture
17
Example Instruction Encoding: ADC (register)
18
Example Instruction Encoding: ADD (register)
19
Assembler Instruction Format
<operation> <operand1> <operand2> <operand3>
There may be fewer operands
First operand is typically destination (<Rd>)
Other operands are sources (<Rn>, <Rm>)
Examples
ADDS <Rd>, <Rn>, <Rm>
Add registers: <Rd> = <Rn> + <Rm>
AND <Rdn>, <Rm>
Bitwise and: <Rdn> = <Rdn> & <Rm>
CMP <Rn>, <Rm>
Compare: Set condition flags based on result of computing <Rn> - <Rm>
20
Where Can the Operands Be Located?
In a general-purpose register R
Destination: Rd
Source: Rm, Rn
Both source and destination: Rdn
Target: Rt
Source for shift amount: Rs
In memory
Only for load, store, push and pop instructions
21
Update Condition Codes in APSR?
22
AAPCS Register Use Conventions
23
Instruction Set Summary
Instruction Type Instructions
Move MOV
Load/Store LDR, LDRB, LDRH, LDRSH, LDRSB, LDM, STR, STRB,
STRH, STM
Add, Subtract, Multiply ADD, ADDS, ADCS, ADR, SUB, SUBS, SBCS, RSBS, MULS
Compare CMP, CMN
Logical ANDS, EORS, ORRS, BICS, MVNS, TST
Shift and Rotate LSLS, LSRS, ASRS, RORS
Stack PUSH, POP
Conditional branch B, BL, B{cond}, BX, BLX
Extend SXTH, SXTB, UXTH, UXTB
Reverse REV, REV16, REVSH
Processor State SVC, CPSID, CPSIE, SETEND, BKPT
No Operation NOP
Hint SEV, WFE, WFI, YIELD
Barriers DMB, DSB, ISB
24
Load and Store Register Instructions
Some load and store instructions can handle half-word (16 bits) and byte (8 bits)
Store just writes to half-word or byte
STRH, STRB
Loading a byte or half-word requires padding or extension: What do we put in the upper bits of the
register?
Example: How do we extend 0x80 into a full word?
Unsigned? Then 0x80 = 128, so zero-pad to extend to word 0x0000_0080 = 128
Signed? Then 0x80 = -128, so sign-extend to word 0xFFFF_FF80 = -128
26
In-Register Size Extension
Signed Unsigned
Byte SXTB UXTB
Half-word SXTH UXTH
27
Load/Store Multiple
LDM/LDMIA: load multiple registers starting from [base register], update base register afterwards
LDM <Rn>!,<registers>
LDM <Rn>,<registers>
STM/STMIA: store multiple registers starting at [base register], update base register after
STM <Rn>!, <registers>
28
Load Literal Value into Register
29
Move (Pseudo-)Instructions
Copy data from one register to another without
updating condition flags
MOV <Rd>, <Rm>
30
Stack Operations
Push some or all of registers (R0-R7, LR) to stack
PUSH {<registers>}
Decrements SP by 4 bytes for each register saved
Pushing LR saves return address
PUSH {r1, r2, LR}
Largest register number pushed first (to largest address)
31
Add Instructions
Add registers, update condition flags
ADDS <Rd>,<Rn>,<Rm>
Add registers
ADD <Rdn>,<Rm>
32
Add Instructions with Stack Pointer
Add SP and immediate value
ADD <Rd>,SP,#<imm8>
ADD SP,SP,#<imm7>
33
Address to Register Pseudo-Instruction
Add immediate value to PC, write result in register
ADR <Rd>,<label>
34
Subtract
Subtract immediate from register, update condition flags
SUBS <Rd>,<Rn>,#<imm3>
SUBS <Rdn>,#<imm8>
35
Multiply
Multiply source registers, save lower word of result in destination register, update condition flags
MULS <Rdm>, <Rn>, <Rdm>
<Rdm> = <Rdm> * <Rn>
Signed multiply
Note: upper word of result is truncated
36
Logical Operations
Bitwise AND registers, update condition flags
ANDS <Rdn>,<Rm>
Bitwise OR registers, update condition flags
ORRS <Rdn>,<Rm>
Bitwise Exclusive OR registers, update condition flags
EORS <Rdn>,<Rm>
Bitwise AND register and complement of second register, update condition flags
BICS <Rdn>,<Rm>
Move inverse of register value to destination, update condition flags
MVNS <Rd>,<Rm>
Update condition flags by ANDing two registers, discarding result
TST <Rn>, <Rm>
37
Compare
Compare - subtracts second value from first, discards result, updates APSR
CMP <Rn>,#<imm8>
CMP <Rn>,<Rm>
38
Shift and Rotate
Common features
All of these instructions update APSR condition flags
Shift/rotate amount (in number of bits) specified by last operand
Logical shift left - shifts in zeroes on right
LSLS <Rd>,<Rm>,#<imm5>
LSLS <Rdn>,<Rm>
Logical shift right - shifts in zeroes on left
LSRS <Rd>,<Rm>,#<imm5>
LSRS <Rdn>,<Rm>
Arithmetic shift right - shifts in copies of sign bit on left (to maintain arithmetic sign)
ASRS <Rd>,<Rm>,#<imm5>
Rotate right
RORS <Rdn>,<Rm>
39
Reversing Bytes
MSB LSB
REV16 - reverse bytes in both half-words
REV16 <Rd>,<Rm>
MSB LSB
REVSH - reverse bytes in low half-word
(signed) and sign-extend
REVSH <Rd>,<Rm> MSB LSB
MSB LSB
Sign extend
MSB LSB
40
Changing Program Flow - Branches
Unconditional Branches
B <label>
Target address must be within 2 KB of branch instruction (-2048 B to
+2046 B)
Conditional Branches
B<cond> <label>
<cond> is condition - see next page
B<cond> target address must be within of branch instruction
B target address must be within 256 B of branch instruction (-256 B to
+254 B)
41
Condition Codes
42
Changing Program Flow - Subroutines
Call Return
BL <label> - branch with link BX <Rd> branch and exchange
Call subroutine at <label> Branch to address specified by <Rd>
PC-relative, range limited to PC+/-16MB LSB of target address must be set to 1 to
Save return address in LR ensure continued execution in Thumb state
BLX <Rd> - branch with link and Supports full 4 GB address space
BX LR - Return from subroutine
exchange
Call subroutine at address in register Rd POP {PC}
(exchange Rd with PC)
Supports full 4GB address range
LSB of target address must be set to 1 to
ensure continued execution in Thumb state
Save return address in LR
43
Special Register Instructions
Move to Register from Special Register
MSR <Rd>, <spec_reg>
44
Other
No Operation - does nothing! Used for delays, or to align instruction to word address
NOP
Breakpoint - causes hard fault or debug halt - used to implement software breakpoints
BKPT #<imm8>
Wait for interrupt - Pause program, enter low-power state until a WFI wake-up event occurs (e.g. an
interrupt)
WFI
45