ARM Teaching Material
ARM Teaching Material
ARM Teaching Material
ARM1176JZF-S ARM1026EJ-S
V4 SC100 ARM720T
1994
1996
1998
2000
2002
2004
2006 time
Processor Modes
User : unprivileged mode under which most tasks run FIQ : entered when a high priority (fast) interrupt is raised IRQ : entered when a low priority (normal) interrupt is raised Supervisor : entered on reset and when a Software Interrupt instruction is executed Abort : used to handle memory access violations Undef : used to handle undefined instructions System : privileged mode using the same registers as user mode
3
FIQ
r8 r9 r10 r11 r12 r13 (sp) r14 (lr)
IRQ
SVC
Undef
Abort
spsr
spsr
spsr
spsr
spsr
Exception Handling
When an exception occurs, the ARM: Copies CPSR into SPSR_<mode> Sets appropriate CPSR bits 0x1C Change to ARM state 0x18 Change to exception mode 0x14 Disable interrupts (if appropriate) 0x10 Stores the return address in LR_<mode> 0x0C 0x08 Sets PC to vector address 0x04 To return, exception handler needs to:0x00 Restore CPSR from SPSR_<mode> Restore PC from LR_<mode> This can only be done in ARM state.
Reset
Vector Table
Vector table can be at 0xFFFF0000 on ARM720T and on ARM9/10 family devices
N Z C V Q f
n s
e x
I F T c
mode
T Bit
Architecture xT only T = 0: Processor in ARM state T = 1: Processor in Thumb state
039v12
All instructions are 32 bits wide All instructions must be word aligned Therefore the pc value is stored in bits [31:2] with bits [1:0] undefined (as instruction
cannot be halfword or byte aligned)
All instructions are 16 bits wide All instructions must be halfword aligned Therefore the pc value is stored in bits [31:1] with bit [0] undefined (as instruction
cannot be byte aligned)
All instructions are 8 bits wide Processor performs a word access to read 4 instructions at once
039v12 7
ARM instructions can be made to execute conditionally by postfixing them with the appropriate condition code field. This improves code density and performance by reducing the number of forward branch instructions. CMP r3,#0 CMP r3,#0 BEQ skip ADDNE r0,r1,r2 ADD r0,r1,r2 skip By default, data processing instructions do not affect the condition code flags but the flags can be optionally set by using S. CMP does not need S. loop decrement r1 and set flags SUBS r1,r1,#1 BNE loop if Z flag clear then branch
Condition Codes
The possible condition codes are listed below
Note AL is the default and does not need to be specified
Suffix
EQ NE CS/HS CC/LO MI PL VS VC HI LS GE LT GT LE AL
Description Equal Not equal Unsigned higher or same Unsigned lower Minus Positive or Zero Overflow No overflow Unsigned higher Unsigned lower or same Greater or equal Less than Greater than Less than or equal Always
Flags tested Z=1 Z=0 C=1 C=0 N=1 N=0 V=1 V=0 C=1 & Z=0 C=0 or Z=1 N=V N!=V Z=0 & N=V Z=1 or N=!V
10
Comparisons set flags only - they do not specify Rd Data movement does not specify Rn Second operand is sent to the ALU via barrel shifter.
11
Barrel Shifter
ALU
Result
2. Implement an ABS (absolute value) function for a registered value using only two instructions. 3. Multiply a number by 35, guaranteeing that it executes in 2 core clock cycles.
13
r9,r8,r8,LSL #2 r10,r9,r9,LSL #3
14
Immediate constants
No ARM instruction can contain a 32 bit immediate constant All ARM instructions are fixed as 32 bits long The data processing instruction format has 12 bits available for operand2
11 rot 8 7 immed_8 0
Quick Quiz:
x2
Shifter ROR
4 bit rotate value (0-15) is multiplied by two to give range 030 in steps of 2 8-bits rotated right by an even number of bit positions
15
Rule to remember is
16
STR Word STRB Byte STRH Halfword Signed byte load Signed halfword load
Syntax:
LDR{<cond>}{<size>} Rd, <address> STR{<cond>}{<size>} Rd, <address>
e.g. LDREQB
17
Address accessed
Address accessed by LDR/STR is specified by a base register with an offset
An unsigned 12-bit immediate value (i.e. 0 - 4095 bytes) LDR r0, [r1, #8] A register, optionally shifted by an immediate value LDR r0, [r1, r2] LDR r0, [r1, r2, LSL#2]
This can be either added or subtracted from the base register: LDR r0, [r1, #-8] LDR r0, [r1, -r2, LSL#2] For halfword and signed halfword / byte, offset can be:
A register (unshifted)
Choice of pre-indexed or post-indexed addressing Choice of whether to update the base pointer (pre-indexed only) LDR r0, [r1, #-8]!
18
Load/Store Exercise
Assume an array of 25 words. A compiler associates y with r1. Assume that the base address for the array is located in r2. Translate this C statement/assignment using just three instructions:
array[10] = array[5] + y;
19
array[10] = array[5] + y;
LDR
; r3 = array[5]
; r3 = array[5] + y ; array[5] + y =
20
Increasing Address
r0
21
; r0 = r1 * r2 ; r0 = (r1 * r2) + r3
64-bit multiply instructions offer both signed and unsigned versions For these instruction there are 2 destination registers
[U|S]MULL r4, r5, r2, r3 ; r5:r4 = r2 * r3 [U|S]MLAL r4, r5, r2, r3 ; r5:r4 = (r2 * r3) + r5:r4
Most ARM cores do not offer integer divide instructions Division operations will be performed by C library routines or inline shifts
22
Branch instructions
B{<cond>} label Branch : Branch with Link : BL{<cond>} subroutine_label
31
28 27
25 24 23
Cond
1 0 1 L
Offset
Link bit
Condition field
The processor core shifts the offset field left by 2 positions, sign-extends it
and adds it to the PC 32 Mbyte range How to perform longer branches?
23
Register Usage
Register
Arguments into function Result(s) from function otherwise corruptible (Additional parameters
passed on stack)
r0 r1 r2 r3
The compiler has a set of rules known as a Procedure Call Standard that determine how to pass parameters to a function (see AAPCS) CPSR flags may be corrupted by function call. Assembler code which links with compiled code must follow the AAPCS at external interfaces The AAPCS is part of the new ABI for the ARM Architecture
- Stack base - Stack limit if software stack checking selected
r12
r13/sp r14/lr r15/pc
- SP should always be 8-byte (2 word) aligned - R14 can be used as a temporary once value stacked
24
Stores return address in LR Returning implemented by restoring the PC from LR For non-leaf functions, LR will have to be stacked
func1
: : BL func1 : :
func2
: : : :
STMFD sp!,{regs,lr}
: BL func2 : LDMFD sp!,{regs,pc}
:
MOV pc, lr
039v12
25
PSR access
31 28 27 24 23 19 16 15 10 9 8 7 6 5 4 0
N Z C V Q de f
J s
GE[3:0] IT cond_abc E A I F T x c
mode
MRS and MSR allow contents of CPSR / SPSR to be transferred to / from a general purpose register or take an immediate value MSR allows the whole status register, or just parts of it to be updated Interrupts can be enable/disabled and modes changed, by writing to the CPSR Typically a read/modify/write strategy should be used: MRS r0,CPSR BIC r0,r0,#0x80 MSR CPSR_c,r0 ; read CPSR into r0 ; clear bit 7 to enable IRQ ; write modified value to c byte only
In User Mode, all bits can be read but only the condition flags (_f) can be modified
26
Agenda
Introduction to ARM Ltd
27
ARM7TDMI
Instruction Fetch ThumbARM decompress ARM decode Reg Select Reg Read Shift ALU Reg Write
FETCH
DECODE
EXECUTE
ARM9TDMI
Instruction Fetch ARM or Thumb Inst Decode Reg Reg Decode Read Shift + ALU Memory Access Reg Write
FETCH
DECODE
EXECUTE
MEMORY
WRITE
28
Multiply
FETCH
ISSUE
DECODE
EXECUTE
MEMORY
WRITE
ARM11
Shift ALU Saturate
Fetch 1
Fetch 2
Decode
Issue
MAC 1
Write back
Address
29
Agenda
Introduction to ARM Ltd
30
16 bit RAM
32 bit RAM
Interrupt Controller
nIRQ nFIQ
Peripherals
I/O
8 bit ROM
ARM Core
039v12
31
AHB
039v12
32
AHB Structure
Arbiter
HADDR HWDATA HRDATA HRDATA
Master #1
HADDR HWDATA
Slave #1
Address/Control
Master #2
Write Data Read Data
Slave #2
Slave #3
039v12
33