I'm wrapping up some product code on a modern 8051.
I actually surprised myself, because I ran out of memory in the data area.
I need to check with the manufacturer because supposedly this chip has 256 bytes of RAM in both the IRAM and XDATA. I have to dig through the device setup files from the manufacturer, because it isn't like an ARM where you specify the address ranges in KEIL directly.
I understand legacy 8051s had an external memory source in some cases, that you could access using the XDATA syntax. I understand the architecture issue to a degree since I've been building a mock 8 bit MCU in Verilog.
On a contemporary 8051, is it really that big of a performance to use XData area for variables?
Am I correct this is really a micro-optimization in terms of system gain? Like fractions of microseconds (µs) difference, or is it worse?
Wish I could edit -- anyway, I get that XDATA is some die level memory area created on the IC, so the overheads seem really low to access it. The bus level access may be sub-optimal on the die, but it's not a like separate IC going over copper traces....
Okay, so the DATA area is split between data / idata. IData is some indirect addressing area that is a bit slower than data. 128 bytes of data, and in my case 128 bytes of idata.
So ideally you'd use the idata next...
Okay, so the DATA area is split between data / idata.
Not really. They're not even separate areas. Idata is primarily not access method, not a memory region.
The 8051 is an ancient class of microcontrollers which, unlike more modern architectures, you really have to understand on the machine language level to a considerable level before you try to program it in C. Partly to avoid such confusion as you're suffering right now, partly to know why and how to use the special features of the C compilers to work around its quirks and limitations.
So your chip has 256 bytes of data memory. All of that is accessible by indirect addressing (IDATA). The stack uses part of that. The lower 128 bytes of it are, in addition, also addressable directly (DATA). The lowest 32 bytes of that are also used as the core's register banks. The next 32 bytes are also addressable bitwise (BDATA).
On top of that, almost any modern 8051 chip will have some amount of (originally external, but now internalized) XRAM memory, sometimes also referred to as "MOVX" memory after the only opcode that handles it. The C compilers uses this for XDATA. One 256-byte page of that is accessible by a paged/"near" addressing scheme (PDATA).
And yes, accessing XRAM is a good deal slower than IDATA, which in turn is slower than registers or DATA. Not because of caches or other such new-fangled trickery, but because it takes considerably more code to prepare any access to such a variable, and reading and processing that code takes time. It can be really illuminating to compile a C program using variables in the various memory spaces and then look at the machine code that the compiler had to create to work with them.
I definitely agree, the DATA / IDATA distinction is a bit odd looking at it from 2018! I had't even thought of IDATA largely because in the KEIL compiler outputs it showed the compiler failing at 128 bytes of memory usage, which is suspiciously 50% of the state 256 bytes of memory.... I didn't think I was close to being out of memory!
Maybe you can enable that in KEIL to show the IDATA when compiler something -- maybe it's the map file somewhere else....
You'd really have to drill down to MCU architecture and instruction set architecture to see why that was done way back when (if you really wanted too, I can't say I'm that interested). I am guessing they just kept bolting on new memory areas and kept the system back compatible with the original instruction set architecture as best they could. I don't really know the 8051 ISA to any real depth.
Here's a good start for the next person: www.circuitstoday.com/8051-addressing-modes
In your experience, have you ever profiled a system using XData to see what the performance hit is on a "modern" 8051?
I suppose I could wire up an experiment, but I can't say it would be even worth the time of few hours vs. asking on a forum.
I'm always trying to get the best performance out of these little cheap MCUs...
I am guessing they just kept bolting on new memory areas and kept the system back compatible with the original instruction set architecture as best they could. I don't really know the 8051 ISA to any real depth.
Nope. The original 8051 had all of these memory regions. There was then a variant (the 8052) that had double the amount of internal RAM (accessible only as stack or IDATA).
Back in the day I used to keep pushing for that 8052 instead of the 8051, and frequently got refused. (I was only a junior programmer in the 1980s and didn't have the clout to authorise it.)
Later on various manufacturers added multiple data pointers and extra interrupt priority levels and stuff, but the basic instruction set remained the same.
The one good thing with having such limited resources was having to know the architecture well in order to code efficiently. I sometimes think that this skill is now sorely lacking.
View all questions in Keil forum