需要此语法的帮助:“#define LEDs (char *) 0x0003010”
我正在对 Altera 的 Nios II 软核处理器进行编程,下面是其中一个教程中的代码,我设法通过在硬件(DE2 板)上测试代码来使其工作,但是,我无法理解代码。
#define Switches (volatile char *) 0x0003000
#define LEDs (char *) 0x0003010
void main()
{ while (1)
*LEDs = *Switches;
}
我对#define
的了解是,它要么用于定义一个常量,要么用于定义一个宏,但是
- 为什么在上面的代码中,
#define
中是否存在类似于(char *) 0x0003010
的转换? - 为什么
Switches
和LED
这两个常量像变量而不是常量?
I'm doing programming of a softcore processor, Nios II from Altera, below is the code in one of the tutorial, I manage to get the code working by testing it on the hardware (DE2 board), however, I could not understand the code.
#define Switches (volatile char *) 0x0003000
#define LEDs (char *) 0x0003010
void main()
{ while (1)
*LEDs = *Switches;
}
What I know about #define
is that, it is either used to define a constant, or a macro, but
- why in the above code, there are casting like,
(char *) 0x0003010
, in#define
? - why the 2 constants,
Switches
andLEDs
act like a variable instead of a constant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
预处理器宏是文本替换。因此,代码重复将
映射在 0x3000 的输入(开关)的内容分配给映射在 0x3010 的输出(led)。
请注意,这些是指针。因此它们总是指向同一个位置(恰好是几个内存映射 IO 引脚或类似的东西),但不能保证这些常量位置的内容是不变的,并且< code>* 出现在每个预处理器符号之前的是指针取消引用运算符。
Preprocessor macros are textual replacements. So the code comes out as
which repeated assigns the contents of the input (switch) mapped at 0x3000 to the output (led) mapped at 0x3010.
Note that those are pointer. So they always point to the same place (which happens to be a couple of memory mapped IO pins or something similar), but there is no guarantee the the contents of those constant locations are constant, and the
*
appearing before each preprocessor symbol is the pointer de-reference operator.看来,
Switches
和LED
代表了到实际输入(在Switches
的情况下)和输出(在LED
)。所以你的答案包括:
输入开关的字节被内存映射到地址 0x0003000。为了将其作为字节访问,您需要告诉编译器地址 0x0003000 处的任何内容都是
char
(事实上,您告诉它该地址处的值是volatile char
,这样编译器就不会优化该地址处的值可能随时更改的事实)。它们是常量,但它们是常量指针。也就是说,地址是常量,但这些地址中包含的值不是常量。
发生的情况是,每个时钟周期(左右),从内存地址 0x0003000 读取的任何内容都会写入地址 0x0003010。这给人一种开关立即切换 LED 的错觉。
It appears that
Switches
andLEDs
represent the memory-mapping to the actual input (in the case ofSwitches
) and output (in the case ofLEDs
).So your answers include:
The byte for the input switches is memory-mapped to address 0x0003000. In order to access that as a byte, you need to tell the compiler that whatever is at address 0x0003000 is a
char
(in fact, you tell it that the value at that address is avolatile char
, so that the compiler doesn't optimize away the fact that the value at that address may change at any time).They are constants, but they are constant pointers. That is to say, the address is constant, but the values contained at those addresses are not constant.
What happens is that every clock cycle (or so), whatever is read from memory address 0x0003000 is then written to address 0x0003010. This gives the illusion that the switches instantly toggle the LEDs.
在 C 语言中,宏是简单的替换。
每次编译器在代码中看到
LED
时,都会将其替换为(char *) 0x0003010
。所以你的代码实际上与此相同:
In C, macros are simple substitutions.
Every time the compiler sees
LEDs
in your code it will replace it with(char *) 0x0003010
.So your code is effectively the same as this:
如果没有 #defines 中的类型转换,它们就不会被视为 char* 和 volatile char*。 *开关处的内容被复制到*LED 中。
Without the type casting at the #defines, they wouldn't be treated as char* and volatile char*. What is at *Switches is copied into *LEDs.