需要此语法的帮助:“#define LEDs (char *) 0x0003010”

发布于 2024-09-05 05:17:26 字数 497 浏览 8 评论 0原文

我正在对 Altera 的 Nios II 软核处理器进行编程,下面是其中一个教程中的代码,我设法通过在硬件(DE2 板)上测试代码来使其工作,但是,我无法理解代码。

#define Switches (volatile char *) 0x0003000
#define LEDs (char *) 0x0003010
void main()
{ while (1)
*LEDs = *Switches;
}

我对#define的了解是,它要么用于定义一个常量,要么用于定义一个,但是

  1. 为什么在上面的代码中, #define 中是否存在类似于 (char *) 0x0003010 的转换?
  2. 为什么SwitchesLED这两个常量像变量而不是常量?

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

  1. why in the above code, there are casting like, (char *) 0x0003010, in #define?
  2. why the 2 constants, Switches and LEDs act like a variable instead of a constant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秉烛思 2024-09-12 05:17:26

1) 为什么在上面的代码中,#define 中有类似 (char *) 0x0003010 的转换?

预处理器宏是文本替换。因此,代码重复将

while (1) {
  *(char *) 0x0003010 = *(volatile char *) 0x0003000
}

映射在 0x3000 的输入(开关)的内容分配给映射在 0x3010 的输出(led)。

2) 为什么 2 个常量、开关和 LED 的作用就像变量而不是常量?

请注意,这些是指针。因此它们总是指向同一个位置(恰好是几个内存映射 IO 引脚或类似的东西),但不能保证这些常量位置的内容是不变的,并且< code>* 出现在每个预处理器符号之前的是指针取消引用运算符。

1) why in the above code, there are casting like, (char *) 0x0003010, in #define?

Preprocessor macros are textual replacements. So the code comes out as

while (1) {
  *(char *) 0x0003010 = *(volatile char *) 0x0003000
}

which repeated assigns the contents of the input (switch) mapped at 0x3000 to the output (led) mapped at 0x3010.

2) why the 2 constants, Switches and LEDs act like a variable instead of a constant?

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.

淑女气质 2024-09-12 05:17:26

看来,SwitchesLED 代表了到实际输入(在 Switches 的情况下)和输出(在LED)。

所以你的答案包括:

  1. 输入开关的字节被内存映射到地址 0x0003000。为了将其作为字节访问,您需要告诉编译器地址 0x0003000 处的任何内容都是 char (事实上,您告诉它该地址处的值是 volatile char,这样编译器就不会优化该地址处的值可能随时更改的事实)。

  2. 它们常量,但它们是常量指针。也就是说,地址是常量,但这些地址中包含的值不是常量。

发生的情况是,每个时钟周期(左右),从内存地址 0x0003000 读取的任何内容都会写入地址 0x0003010。这给人一种开关立即切换 LED 的错觉。

It appears that Switches and LEDs represent the memory-mapping to the actual input (in the case of Switches) and output (in the case of LEDs).

So your answers include:

  1. 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 a volatile char, so that the compiler doesn't optimize away the fact that the value at that address may change at any time).

  2. 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.

墨落画卷 2024-09-12 05:17:26

在 C 语言中,宏是简单的替换。

每次编译器在代码中看到 LED 时,都会将其替换为 (char *) 0x0003010

所以你的代码实际上与此相同:

void main()
{
    while (1)
        *(char *) 0x0003010 = *(volatile char *) 0x0003000;
}

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:

void main()
{
    while (1)
        *(char *) 0x0003010 = *(volatile char *) 0x0003000;
}
乖不如嘢 2024-09-12 05:17:26

如果没有 #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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文