我什么时候使用 xdata? [英] When do I use xdata?

查看:20
本文介绍了我什么时候使用 xdata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是嵌入式系统编程的新手.我正在开发使用 8051 芯片组的设备.我在示例程序中注意到,在定义变量时,有时它们使用关键字 xdata.像这样……

I am new at embedded system programming. I am working on a device that uses an 8051 chipset. I have noticed in the sample programs that when defining variables, sometimes they use the keyword xdata. like this...

static unsigned char xdata PatternSize;

而其他时候 xdata 关键字被省略.

while other times the xdata keyword is omitted.

我的理解是 xdata 关键字指示编译器将该变量存储在外部、闪存、内存中.

My understanding is that the xdata keyword instructs the compiler that that variable is to be stored in external, flash, memory.

在什么情况下我应该使用 xdata 在外部存储变量?访问这些变量需要更长的时间,对吧?使用 xdata 存储的值在设备硬重置后不会保留吗?

In what cases should I store variables externally with xdata? Accessing those variables takes longer, right? Values stored using xdata do not remain after a hard reset of the device do they?

另外,我知道 static 关键字意味着该变量将在每次调用定义它的函数时保持不变.静态和 xdata 必须一起使用吗?

Also, I understand that the static keyword means that the variable will persist through every call to the function it is defined in. Do static and xdata have to be used together?

推荐答案

8051架构有3个独立的地址空间,核心RAM使用8位地址,所以最多可达256字节,XDATA是16位地址空间(64Kbytes) 具有读/写能力,程序空间是一个 16 位地址空间,具有执行和只读数据能力.由于其较小的地址范围和与内核的紧密耦合,寻址内核 RAM 在代码空间和访问周期方面更有效

The 8051 architecture has three separate address spaces, the core RAM uses an 8 bit address, so can be up to 256 bytes, XDATA is a 16bit address space (64Kbytes) with read/write capability, and the program space is a 16bit address space with execution and read-only data capability. Because of its small address range and close coupling to the core, addressing the core RAM is more efficient in terms of code space and access cycles

最初的 8051 内核具有片上微型 RAM(地址空间为 256 字节,但一些变体在实际内存中只有一半),而 XDATA 指的是片外数据存储器(与程序存储器相反).然而,大多数现代 8051 架构设备都具有片上 XDATA 和程序存储器.

The original 8051 core had tiny-on-chip RAM (an address space of 256 bytes but some variants had half that in actual memory), and XDATA referred to off-chip data memory (as opposed to program memory). However most modern 8051 architecture devices have on-chip XDATA and program memory.

因此,当性能至关重要时,您可能会使用核心内存,而将 XDATA 用于更大的内存对象.然而,在大多数情况下,编译器应该为您做出这个决定(查看您的编译器手册,它将详细描述如何分配内存).指令集使得在核心内存中实现堆栈变得高效,而静态和动态分配的数据通常在 XDATA 中更合理地分配.如果编译器有 XDATA 关键字,那么它将覆盖编译器的策略,并且应该只在编译器的策略以某种方式失败时使用,因为它会降低代码的可移植性.

So you might use the core memory when performance is critical and XDATA for larger memory objects. However the compiler should in most cases make this decision for you (check your compilr's manual, it will describe in detail how memory is allocated). The instruction set makes it efficient to implement the stack in core memory, whereas static and dynamically allocated data would usually be more sensibly allocated in XDATA. If the compiler has an XDATA keyword, then it will override the compiler's strategy, and should only be used when the compiler's strategy somehow fails since it will reduce the portability of the code.

[edit] 另请注意,核心存储器包括一个 32 字节的位寻址区域,位寻址指令使用该区域的 8 位地址直接访问各个位.该区域存在于 256 字节字节可寻址核心内存中,因此位和字节均可寻址[/edit]

[edit] Note also that the core memory includes a 32byte bit-addressable region, the bit-addressing instructions use an 8bit address into this region to access individual bits directly. The region exists within the 256byte byte addressable core memory, so is both bit and byte addressable[/edit]

这篇关于我什么时候使用 xdata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆