ADSP混合编程

伪操作和伪指令

Directives,即 伪操作,是汇编语言中的特殊指令助记符。主要作用是为完成汇编程序做各种准备。伪操作仅是在源程序进行汇编时由汇编程序处理,而不是在计算机运行期间由机器执行的指令。即,伪操作只在汇编时起作用,一旦汇编结束,其使命也就结束了。

Pseudo-Instruction,即 伪指令,是汇编语言程序里的特殊指令助记符,不是 真指令。伪指令在汇编时被替换成合适的机器指令(根据芯片架构而定,不同芯片架构之间指令可能不同),故其也只在汇编时其作用,不在机器运行期间由机器执行。

ADI汇编文件样式

.asm 文件具有以下样式,包含 预处理指令(主要是C/C++宏定义)、汇编伪操作类别(assembler direcitives)、数据块、代码块、条件编译预处理 和 汇编标签。

image-20210630170546778

混合编程

使用asm()

1
2
3
4
int img288; //定义C语言变量
asm("ax0 = 0x5C;");
asm("dm(img288_) = ax0;"); //用汇编语言赋值时,变量需要加后置下划线???
img288 = 0x5C; //直接用C语言赋值

以上C语言与汇编代码经过编译后:

1
2
3
4
ax0 = 0x5C;
dm(img288_) = ax0;
my1 = 92;
dm(img288_) = my1;

此处可否把 dm() 当成一个类似于指针的东西

使用汇编子程序

使用汇编子程序是C语言程序与汇编语言接口的另一种方法。用户定义的子程序放在单独的汇编文件中,或是做成二进制的库文件,并将子程序的定义用GLOBEL输出,汇编后就可以供C语言程序调用。下面是一个不需要参数的子程序的例子:

1
2
3
4
5
6
7
8
9
10
11
12
.MODULE/RAM_delay_;
.external del_cycle; //声明del_cycle是外部变量
.global delay; //声明delay是全局变量

delay_:
function_entry; //子程序开始标志,必不可少
ar = dm(del_cycle_);
cntr = ar;
do d_loop until ce;
d_loop:nop;
exit; //子程序结束标志,必不可少
.ENDMOD;

注意:以上代码源自参考链接4,实际上代码中的关键字相差可能较大,需要根据实际情况进行改动。

比如,21479系列芯片与ADAU1939芯片一同工作,其ADC采样代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
.section/pm seg_pmco;  //程序代码块,procedure memory section

_Receive_ADC_Samples: /*应该类似于 { */
.global _Receive_ADC_Samples; //定义自己为全局函数

r1 = -31;
r0 = dm(_rx1a_buf + Internal_ADC_L1);
f0 = float r0 by r1;
dm(_Left_Channel_In1) = r0;

r0 = dm(_rx1a_buf + Internal_ADC_R1);
f0 = float r0 by r1;
dm(_Right_Channel_In1) = r0;

r0 = dm(_rx1b_buf + Internal_ADC_L2);
f0 = float r0 by r1;
dm(_Left_Channel_In2) = r0;

r0 = dm(_rx1b_buf + Internal_ADC_R2);
f0 = float r0 by r1;
dm(_Right_Channel_In2) = r0;

r0 = DM(AD1939_audio_frame_timer);
r0 = r0 + 1;
DM(AD1939_audio_frame_timer) = r0;

leaf_exit; //程序结束标志,应该类似于return;吧
_Receive_ADC_Samples.end: /*应该类似于 }*/

Dual Memory Support Keywords (dm / pm)

This section describes cc21k language extension keywords to C and C++ that support the dual-memory space, modified Harvard architecture of the ADSP-21xxx processors. There are two keywords used to designate memory space: dm and pm. They can be used to specify the location of a static or global variable or to qualify a pointer declaration.

以下规则适用于两种内存关键字(dm/pm):

  • The memory space keyword (dm or pm) refers to the expression to the right of the keyword.
  • You can specify a memory space for each level of pointer. This corresponds to one memory space for each * in the declaration.
  • The compiler uses Data Memory (DM) as the default memory space for all variables. All undeclared spaces for data are Data Memory spaces.
  • The compiler always uses Program Memory (PM) as the memory space for functions. Function pointers always point to Program Memory.
  • You cannot assign memory spaces to automatic variables. All automatic variables reside on the stack, which is always in Data Memory.
  • Literal character strings always reside in Data Memory.

参考

  1. VisualDSP++ 5.0 C/C++ Compiler Manual for SHARC Processors
  2. ADSP-21160 SHARC DSP Instruction Set Reference
  3. 《嵌入式系统原理与应用设计》王光学,电子工业出版社
  4. 嵌入式C语言开发ADSP21XX系列