L4 - Microcontroller Lecture
L4 - Microcontroller Lecture
Lecture 4
1
Architectural Inheritance
RISC Precedents- Historical Context
At the time the first ARM chip was designed, the only examples of RISC
architectures were:
1. Load-Store Architecture
• Only load and store instructions access memory
• All other operations work on registers
• Clean separation of memory and computation
2. Fixed-Length 32-bit Instructions
• Simplifies instruction decode
• Enables efficient pipelining
• Predictable instruction fetch
3. 3-Address Instruction Formats
• Two source operands, one destination
• All independently specified
• Maximum flexibility
3
Features Rejected: 1. Register Windows
Berkeley RISC Feature Not Adopted
Register Windows - A key Berkeley RISC feature that ARM designers chose
to reject.
4
Why ARM Rejected This:
ARM’s Approach
ARM uses banked registers for different processor modes instead of register
windows. This provides mode-specific context without the overhead of full
register windows.
5
Features Rejected: 2. Delayed Branches
6
Problem with Delayed Branches:
7
Wise Decision
In the long run this has turned out to be a good decision since it simplifies
re-implementing the architecture with a different pipeline.
8
Features Rejected:3. Single-Cycle Execution
Not All Instructions Single-Cycle
Although ARM executes most data processing instructions in a single clock
cycle, many other instructions take multiple clock cycles.
The Rationale:
• With single memory for both data and instructions
• Even a simple load or store instruction requires at least
two memory accesses
• One for the instruction
• One for the data
ARM’s Approach
Single cycle operation of all instructions is only possible with separate data
and instruction memories, which were considered too expensive for
intended ARM application areas.
9
Optimizing Memory Access
Design Philosophy
Instead of single-cycle execution of all instructions, ARM was designed to
use the minimum number of cycles required for memory accesses.
Strategy:
• Where this was greater than one, extra cycles used where
possible
• Support useful features like auto-indexing addressing modes
• Reduces total number of ARM instructions required
• Improves performance and code density
Example: Array Processing
Auto-indexing allows pointer increment during load/store
operation, eliminating separate increment instruction and
improving both speed and code size.
10
Simplicity - Core Design Principle
Overriding Concern
An overriding concern of the original ARM design team was the need to
keep the design simple.
Team Background:
• First ARM was an Acorn designers’ first serious VLSI
project
• Acorn designers had experience only of gate arrays
• Complexities up to around 2,000 gates
• Full-custom CMOS design medium approached with respect
11
Where Simplicity Shows
Hardware vs. Software
The simplicity of the ARM may be more apparent in hardware
organization and implementation than in the instruction set architecture.
Register Categories:
1. General-Purpose Registers: r0 - r14
2. Program Counter: r15 (PC)
3. Status Register: CPSR
Additional Registers
The remaining registers are used only for:
• System-level programming
• Handling exceptions (interrupts, etc.)
14
ARM Register Organization Diagram
FIQ Advantage
FIQ mode has 7 banked registers (r8-r14) enabling fast interrupt handling
without stack operations.
16
Complete ARM Register Set (Contd..)
3. Banked Registers in Other Modes:
Mode Banked SP (r13) Banked LR (r14)
Supervisor r13 svc (SP svc) r14 svc (LR svc)
Abort r13 abt (SP abt) r14 abt (LR abt)
IRQ r13 irq (SP irq) r14 irq (LR irq)
Undefined r13 und (SP und) r14 und (LR und)
4. Saved Program Status Registers (SPSR):
• SPSR svc: Supervisor mode SPSR
• SPSR abt: Abort mode SPSR
• SPSR irq: IRQ mode SPSR
• SPSR und: Undefined instruction mode SPSR
• SPSR fiq: FIQ mode SPSR
Total Count: 37 Registers
31 general-purpose + 1 PC + 1 CPSR + 5 SPSR = 37 registers 17
General-Purpose Registers (r0-r14)
Characteristics
All general-purpose registers are 32-bit wide and can hold:
• Data values
• Memory addresses
• No special hardware distinction between them
20
Stack Pointer (r13/SP)
Key Characteristics:
• Auto-updates with PUSH/POP operations
• Different SP for each processor mode
• Allows each mode to have separate stack
• Critical for exception handling
Banked SP Registers
• SP usr (User and System modes)
• SP fiq (Fast Interrupt mode)
• SP irq (Normal Interrupt mode)
• SP svc (Supervisor mode)
• SP abt (Abort mode)
• SP und (Undefined mode)
21
Link Register (r14/LR)
Operation:
• Automatically loaded by BL (Branch with Link) instruction
• BL copies (PC + 4) into LR before branching
• Return using: MOV PC, LR or BX LR
Banked LR:
• Like SP, each processor mode has its own LR
• Exception handlers can use LR without corrupting user’s
return address
• Critical for nested exception handling
Nested Calls
For nested function calls, LR must be saved to stack before
calling another function, as there is only one LR per mode.
22
Program Counter (r15/PC)
Purpose
The Program Counter (r15/PC) points to the current instruction address
being fetched.
Key Characteristics:
• Auto-increments by 4 for each 32-bit instruction
• Auto-increments by 2 for each 16-bit Thumb instruction
• Can be read like any other register
• Writing to PC changes program flow (branch)
Pipeline Effect - Critical! : Due to ARM’s 3-stage pipeline:
• Reading PC gives address of current instruction + 8 (ARM mode)
• Reading PC gives address of current instruction + 4 (Thumb mode)
• PC points to instruction being fetched, not executed
23
Current Program Status Register (CPSR)
Purpose
The CPSR stores condition codes and controls processor state and operating
mode.
24
CPSR Condition Flags - Detailed
Flag Description
N Negative: Set when result of operation is negative (bit 31 of
result = 1). Used for signed comparisons.
Z Zero: Set when result of operation is zero (all bits = 0). Used
for equality tests.
C Carry: Set when operation generates carry-out (unsigned
overflow) or borrow. Also set by shift operations.
V Overflow: Set when operation generates signed overflow (re-
sult doesn’t fit in sign bit representation).
Important Notes
• Not all instructions update flags
• Use ’S’ suffix to update flags: ADDS, SUBS, etc.
• CMP, CMN, TST, TEQ always update flags
• Conditional execution tests these flags 25
CPSR Control Bits
Bit Description
I IRQ Disable: When set (1), normal interrupts (IRQ)
are masked and will not be processed.
F FIQ Disable: When set (1), fast interrupts (FIQ) are
masked and will not be processed.
T Thumb State: When set (1), processor executes
Thumb (16-bit) instructions. When clear (0), executes
ARM (32-bit) instructions.
Mode[4:0] Processor Mode: 5 bits define current operating mode
(User, FIQ, IRQ, Supervisor, Abort, Undefined, Sys-
tem).
Privilege Levels Only privileged modes (not User mode) can
modify control bits directly. This protects system integrity.
26
Processor Modes
Mode Code Type Entry Condition
User 10000 Non-privileged Normal program execution
FIQ 10001 Exception Fast Interrupt Request
IRQ 10010 Exception Normal Interrupt Request
Supervisor 10011 Privileged Reset or Software Interrupt
(SWI)
Abort 10111 Exception Data or Prefetch Abort
Undefined 11011 Exception Undefined instruction
System 11111 Privileged Privileged user mode
Mode Categories
• User: Normal application code (non-privileged)
• System: Privileged tasks (OS) with user registers
• Exception Modes: FIQ, IRQ, Supervisor, Abort, Undefined
27
ARM Exceptions Overview
Exception Types
The ARM architecture supports a range of interrupts, traps and supervisor
calls, all grouped under the general heading of exceptions.
28
Saved Program Status Register (SPSR)
Purpose
SPSRs are used in ARM architecture to save the current program status
when switching to an exception mode.
- Preserve flags: Save flags (e.g., N, Z, C, V) and mode bits.
- Enable nesting: Allow nested exceptions without losing track of previous
state.
- Efficient context switch: Quickly save and restore processor state.
29
SPSR contd...
30
Banked Registers by Mode - Part 1
Register Banking Concept
Each processor mode has its own copy of certain registers, enabling fast
context switching without explicit save/restore operations.
Note on r8-r12
Only FIQ mode has banked versions of r8-r12. All other modes
share the same physical registers r8-r12 with User mode.
31
Banked Registers by Mode - Part 2
Register User System FIQ IRQ Svc Abt Und
Banking Benefits
• Fast context switching: No explicit save/restore of SP and LR needed
• Exception isolation: Each mode has separate stack (via banked SP)
• FIQ optimization: 7 banked registers (r8-r14) enable handling
without stack access
• SPSR preservation: Automatic CPSR save on exception entry
32
Memory System Organization
Address Space
An ARM system has memory state. Memory may be viewed as a linear array
of bytes numbered from zero up to 232 − 1.
Data Types:
• Byte: 8 bits
• Half-word: 16 bits
(aligned on even
boundaries)
• Word: 32 bits
(aligned on 4-byte
boundaries)
33
Memory Alignment - Part 1
Alignment Requirements
Data items must be aligned on their natural boundaries:
34
Memory Alignment - Part 2
Valid Addresses
• Word: 0x00, 0x04, 0x08, 0x0C, 0x10, ...
• Half-word: 0x00, 0x02, 0x04, 0x06, 0x08, ...
• Byte: 0x00, 0x01, 0x02, 0x03, 0x04, ...
Misaligned Access
Accessing misaligned data can cause exceptions or unpredictable
behavior depending on ARM implementation.
35
Memory dump from ARM
The Problem:
• A 32-bit word (0x12345678) occupies 4 bytes in memory
• But which byte goes in which address?
• Two different conventions exist!
Big-Endian Little-Endian
”Most significant byte first” ”Least significant byte first”
• Stores MSB at lowest address • Stores LSB at lowest address
• Human-readable order • Hardware-efficient
• Used by: Motorola, SPARC, • Used by: Intel x86, ARM
Network protocols (default)
37
Big-Endian vs Little-Endian - Visual Example
Storing 32-bit Value: 0x12345678
Memory Representation:
Big-Endian Little-Endian
(Most Significant Byte First) (Least Significant Byte First)
Address Address
0x1000 12 MSB 0x1000 78 LSB
0x1001 34 0x1001 56
0x1002 56 0x1002 34
0x1003 78 LSB 0x1003 12 MSB
38
ARM-Little endian: eg
Address Byte
0x00000000 → 10
0x00000001 → 10
0x00000002 → A0
0x00000003 → E3
Disassembly: 0x00000000 E3A01010 MOV R1,#0x00000010
MOV R1, #16
E3A0 → opcode for MOV immediate 1010 → immediate value
(16) + destination register
39
Endianness
Byte Ordering
ARM supports both little-endian and big-endian memory organizations.
Standard: Little-Endian
• Least significant byte at lowest address
• Most common ARM configuration
• Compatible with x86 systems
Configuration
Endianness can be configured in ARM
(implementation-dependent), but is typically set at power-up
and remains fixed.
40
Load-Store Architecture
Fundamental Principle
In common with most RISC processors, ARM employs a load-store
architecture.
42
ARM Instruction Categories - Contd..
3. Control Flow Instructions
• Normal execution uses consecutive memory addresses
• Control flow instructions cause execution to switch to
different address
• Either permanently (branch instructions)
• Or saving return address (branch and link instructions)
• Or trapping into system code (supervisor calls)
Summary
These three categories cover all ARM instruction operations:
processing data in registers, transferring data between memory
and registers, and controlling program flow.
43
Supervisor Mode
Protected Operating Mode
The ARM processor supports a protected supervisor mode.
Protection Mechanism:
• Ensures user code cannot gain supervisor privileges without
appropriate checks
• Ensures code is not attempting illegal operations
Privileged Operations:
• System-level functions accessed through specified supervisor
calls
• Generally include:
• Accesses to hardware peripheral registers
• Widely used operations such as character input and output
44
User vs. Supervisor Programming - Part 1
User-Level Programmers
User-level programmers are principally concerned with devising algorithms to
operate on data ’owned’ by their programs.
User-Level Concerns:
• Algorithm design and implementation
• Data structures and manipulation
• Rely on operating system for external interactions
System-Level Functions:
• Hardware peripheral access
• Interrupt handling
• Memory management
• Process scheduling
• Resource allocation
Supervisor Mode Characteristics
• Privileged execution
• Full hardware access
• Complete system control
• Responsible for system stability and security 46
ARM Instruction Set Features
Key Characteristics
All ARM instructions are 32 bits wide (except compressed 16-bit Thumb
instructions) and aligned on 4-byte boundaries in memory.
Core Features:
1. Load-Store Architecture
• Only LDR/STR access memory
• All data processing uses registers
2. 3-Address Data Processing Instructions
• Two source operand registers
• Result register
• All independently specified
• Example: ADD r0, r1, r2
3. Conditional Execution of Every Instruction
• 16 condition codes (EQ, NE, GT, etc.)
• Eliminates many branch instructions
• Improves pipeline efficiency 47
ARM Instruction Set Features (Contd...)
Advanced Features:
4. Powerful Load and Store Multiple Register
Instructions
• LDM/STM transfer up to 16 registers
• Efficient context save/restore
• Stack operations (PUSH/POP)
5. Single-Cycle Shift and ALU Operation
• Shift operation combined with ALU in single instruction
• Executes in single clock cycle
• Example: ADD r0, r1, r2, LSL #2
48
Instruction Set Features (Contd...)
Additional Features:
6. Open Instruction Set Extension
• Through coprocessor instruction set
• Adding new registers and data types
7. Very Dense 16-bit Compressed Representation
• Thumb architecture
• Approximately 65% of 32-bit code size
• Useful for memory-constrained systems
49
Code Density Advantage
Performance Trade-off
For the same embedded systems that most ARM processors are used in, this
code density advantage outweighs the small performance penalty incurred by
the decode complexity.
Benefits:
• Smaller memory footprint
• Reduced memory costs
• Better cache utilization
• Lower power consumption (fewer memory accesses)
Thumb Extension
Thumb code extends this advantage to give ARM better code
density than most CISC processors, while maintaining RISC-like
performance and efficiency.
50
I/O System
Memory-Mapped I/O
The ARM handles I/O (input/output) peripherals (such as disk controllers,
network interfaces, etc.) as memory-mapped devices with interrupt
support.
How It Works:
• Internal registers in devices appear as addressable locations
within ARM’s memory map
• May be read and written using same (load-store)
instructions as any other memory locations
Interrupt Handling:
• Peripherals attract processor’s attention by making
interrupt request
• Using either normal interrupt (IRQ)
• Or fast interrupt (FIQ) input
51
Interrupt Sources
Interrupt Inputs
Both interrupt inputs are level-sensitive and maskable.
Characteristics:
• Normally most interrupt sources share the IRQ input
• Just one or two time-critical sources connected to
higher-priority FIQ input
DMA Support
Some systems may include direct memory access (DMA)
hardware external to the processor to handle high-bandwidth
I/O traffic.
Exception Form:
• Interrupts are a form of exception
• Handled as outlined in exception handling mechanism 52
ARM Exceptions Overview
Exception Types
The ARM architecture supports a range of interrupts, traps and supervisor
calls, all grouped under the general heading of exceptions.
53
Vector Table
The instruction at the location the PC is forced to (vector address) will
usually contain a branch to the exception handler.
Pipeline Compensation
This adjustment is necessary because PC is always ahead of the executing
instruction by 8 bytes (ARM mode) or 4 bytes (Thumb mode) due to
pipelining.
55
Exception Entry Example
What Happens on IRQ
1. Current PC value copied to LR irq
2. CPSR copied to SPSR irq
3. CPSR mode bits set to IRQ mode (10010)
4. CPSR I bit set to disable further IRQs
5. PC loaded with 0x18 (IRQ vector address)
6. Instruction at 0x18 executed (typically: B irq handler)
7. IRQ handler executes:
• Saves registers to IRQ stack
• Determines interrupt source
• Services the interrupt
• Restores registers
• Returns: SUBS PC, LR, #4 (restores PC and CPSR)
56
FIQ - Fast Interrupt
Why FIQ is ”Fast”
FIQ has several features making it faster than IRQ:
1. Higher Priority
• FIQ vector at 0x1C (end of vector table)
• Handler can be placed directly at 0x1C
• No branch needed (saves cycles)
2. Additional Banked Registers
• FIQ mode has r8 fiq through r12 fiq
• Total of 7 banked registers (r8-r14)
• Often enough without saving to stack
• Faster entry and exit
3. Separate Disable Bit
• Can disable FIQ independently (F bit)
• FIQ can interrupt IRQ handler
• IRQ cannot interrupt FIQ handler
57
Key Takeaways - History
Summary 58
Key Takeaways - Programmer’s Model
• 37 Total Registers: 31 general-purpose, 1 PC, 1 CPSR, 5
SPSR
• 16 Visible in User Mode: r0-r14, PC, CPSR
• Banked Registers: Each mode has own SP, LR, SPSR
• CPSR: N, Z, C, V flags + I, F, T bits + Mode[4:0]
• 7 Processor Modes: User, System, FIQ, IRQ, Supervisor,
Abort, Undefined
• Load-Store Architecture: Only LDR/STR access memory
• Memory: Byte, half-word, word with alignment
requirements
• Exceptions: 8 vectors at 0x00-0x1C with mode switching
Summary 59
Key Takeaways - Architectural Features
Summary 60
Programming Model Best Practices
1. Follow Register Conventions
• r0-r3 for arguments/return values
• r4-r11 callee-saved (preserve in functions)
• r13 as stack pointer (don’t corrupt!)
2. Understand Pipeline Effects
• PC reads return address + 8
• Critical for PC-relative addressing
3. Use Banked Registers Effectively
• Each mode has separate SP, LR
• FIQ has extra banked r8-r12
4. Exception Handling
• Save context to mode-specific stack
• Adjust return PC for pipeline state
• Use SPSR to restore CPSR atomically
Summary 61