本版首Po, 查了一下好像沒有相關討論, 請大大們鞭小力一點QQ
網誌好讀版: http://tinyurl.com/y677eu2f
最近從同事那裡聽到這個小技巧, 分享給大家
Tip: 建議盡量使用structure來存取Register,可以獲得以下好處
1. 讓compiler對base address計算做最佳化 (with -O1),讓程式更有效率
2. 易寫、易讀、易懂!
正文開始!
讓compiler對base address計算做最佳化 (with -O1),讓程式更有效率
基本概念: Placing C variables at specific addresses to access memory-mapped
peripherals
The ARM compiler will normally use a ‘base register’ plus the immediate
offset field available in the load/store instruction to compile struct member
or specific array element access.
In the ARM instruction set, LDR/STR word/byte instructions have a 4KB range,
but LDRH/STRH instructions have a smaller immediate offset of 256 bytes.
Equivalent 16-bit Thumb instructions are much more restricted - LDR/STR have
a range of 32 words, LDRH/STRH have a range of 32 halfwords and LDRB/STRB
have a range of 32 bytes. However, 32-bit Thumb instructions offer a
significant improvement. Hence, it is important to group related peripheral
registers near to each other if possible. The compiler will generally do a
good job of minimising the number of instructions required to access the
array elements or structure members by using base registers.
以上大意上是說ARM compiler原本就會使用base register加上offset來
對struct member與array element來做存取,所以如果我們將一組連續位置的register用
struct或array來定義,就可以也套用上述的base register存取方式。
直接看例子比較快,如果我們直接用下面這樣的方法去寫A/B/C
#define REG_BASE_ADDR (0x10000000FFFFF00)
#define REG_A (REG_BASE_ADDR + 0x8)
#define REG_B (REG_BASE_ADDR + 0x10)
#define REG_C (REG_BASE_ADDR + 0x18)
#define READ_REG(reg, val) val = *((volatile unsigned long *) (reg))
#define WRITE_REG(reg, val) *((volatile unsigned long *) (reg)) = val
void foo(unsigned long a_val, unsigned long b_val, unsigned long c_val){
WRITE_REG(REG_A, a_val);
WRITE_REG(REG_B, b_val);
WRITE_REG(REG_C, c_val);
}
從Compiler Explorer(ARM64 GCC 8.2 -O2)測試的assembly結果如下
(https://godbolt.org/z/3MRiMJ)
可以看到需要分別計算A/B/C register的base address(Line2~10)才能寫值。
foo:
mov x5, 65288
mov x4, 65296
movk x5, 0xfff, lsl 16
movk x4, 0xfff, lsl 16
movk x5, 0x100, lsl 48
mov x3, 65304
movk x4, 0x100, lsl 48
movk x3, 0xfff, lsl 16
movk x3, 0x100, lsl 48
str x0, [x5]
str x1, [x4]
str x2, [x3]
ret
而如果改成下面的寫法,利用structure來存取 (https://godbolt.org/z/g-eJmz)
#define REG_BASE_ADDR (0x10000000FFFFF00)
typedef struct
{
unsigned long BASE;
unsigned long REG_A;
unsigned long REG_B;
unsigned long REG_C;
} my_register;
#define READ_REG(reg, val) do{ \
volatile my_register *base = (my_register *) REG_BASE_ADDR; \
val = base->reg; \
} while(0)
#define WRITE_REG(reg, val) do{ \
volatile my_register *base = (my_register *) REG_BASE_ADDR; \
base->reg = val; \
} while(0)
void foo(unsigned long a_val, unsigned long b_val, unsigned long c_val){
WRITE_REG(REG_A, a_val);
WRITE_REG(REG_B, b_val);
WRITE_REG(REG_C, c_val);
}
產生的Assembly如下,可以看到只需要去計算Base Address一次,並存在base register
x3,接著直接透過offset去讀A/B/C,整整少了一半的指令數!對於斤斤計較MCPS的Hard
Real Time Context來說可是有天壤之別!
foo:
mov x3, 268435200
movk x3, 0x100, lsl 48
str x0, [x3, 8]
str x1, [x3, 16]
str x2, [x3, 24]
ret
易寫、易讀、易懂!
再來看第二個優點,這點對我來說跟甚至比程式效率還重要,而這其實也是Structure本
身最大的用途:用對工程師最友善的方式來描述資料
例如Register A的Spec如下 (little-endian)
Bit | 0 1 2 3 4 5 6 7 8