Processors these days operate at mind-boggling speeds of several megahertz (MHz) and gigahertz (GHz). They are performing calculations at a very high speed so they must get the data required for those calculations as fast as possible. If processors read or write directly to storage memory, the process would get bogged down because storage memories like hard drives, flash memory, etc. are simply not fast enough. The potential of the processors is useless if their speed of operation is limited because the necessary data to perform the calculation is not available on time.
RAM helps us get rid of this problem by storing the information your computer is actively using so that it can be accessed quickly. RAM stands for Random Access Memory. It is a type of memory where data can be read and written at higher speeds. The more the RAM, the better is the performance. However, RAM is a volatile memory. This means that it loses the stored data when power is turned off.
RAM in Microcontrollers
The core elements of a microcontroller are processor, memory, and I/O peripherals. Its different types of memory and its peripherals (Timers, UART, SPI, I2C, USB, etc.) are all internally connected to the processor and are present inside the microcontroller chip. This reduces the overall cost and also minimizes the space needed on the circuit board compared to using multiple chips, an important factor in many embedded systems.
The primary factor deciding the performance of microcontrollers is the processor speed. But the second most important factor is the memory onboard. Talking about the classification of memories on microcontrollers, they are of three types:
- Flash or Program Memory
- Static Random Access Memory (SRAM)
- Electrically Erasable Programmable Read-Only Memory (EEPROM)
The program to be executed is stored in the flash or program memory, which is a type of Read-Only Memory (ROM). It’s a non-volatile memory. The execution of the program is done directly from flash, i.e, it is not loaded into the RAM.
EEPROM is used to store small bytes of data for the microcontroller, which can be electrically erased and reprogrammed byte-by-byte. It is a type of non-volatile memory, but with a higher number of write cycles as compared to flash memory.
SRAM is used to store the temporary variables, global variables, program stack, and heap sections while the program is executing. Unlike flash and EEPROM, it’s a volatile memory. The RAM in microcontrollers is small in size, usually in the size of kilobytes (KB).
We know that having more RAM increases the performance, but still microcontrollers come with a limited amount of RAM. This is because space is a crucial factor when developing embedded hardware. In the case of microcontrollers, SRAM takes up a lot of space, as much as six transistors per bit. Increasing the SRAM capacity will increase the consumed floor space of the chip of the microcontroller, leaving no room for other peripherals. However, as the tasks of microcontrollers are minuscule compared to those of desktop computers, the small size of SRAM is not a major issue, unless you are doing something which takes up a lot of it.
char message = "Arduino Uno went to space!";
Considering the above declaration, it takes up about 27 bytes of SRAM (one byte for each character and one for the ‘\0’ terminator). If we talk about Arduino Uno, the size of its SRAM is just 2KB. Arduino Mega, on the other hand, has 8KB of SRAM. As we can notice, there’s not much RAM in the Uno. Using it all up doesn’t take long if you have a lot of text to display. Using up all the SRAM may cause your program to behave unexpectedly. A solution to this problem is to store the strings in flash (program) memory instead of SRAM and keep the SRAM as free as possible for storing the data during runtime.
But why is RAM faster?
Random, Access, Memory. The answer to this question lies in the name itself. RAM functions a little differently than the other types of memory. It accesses the memory, randomly.
Let’s take an example: Assume that the graphic below represents a stack of books. If I ask you to pick out book #4 from it, you have two ways to do it:
- Remove the books numbered 1, 2, 3 one by one, and then get book number 4. This can be termed as Sequential Access.
- Or you can directly pick book number 4 from the stack, and get it out without having to remove the first three books, which is called Random Access.
The faster method out of the above two options is definitely the second option, which is Random Access. RAM functions in a similar way.
For simplicity, if our RAM has 1000 memory blocks, doing random access to memory block 389 takes the same time as accessing block 1. But doing sequential access to block 389 will be comparatively slower as you will have to pass 1, 2, 3 ... 387, 388, 389 to finally reach the requested memory block. This is why RAM is faster. It does random access to memory blocks instead of sequential access, and that is possible due to the internal structure of RAM.
When we look at the basic organization of RAM, there is an address decoding circuit and a 2-dimensional array of memory blocks which are interconnected to each other, just like a matrix.
Firstly, the address decoding circuitry receives the target address, which is a set of bits that correspond to a chain of switches that gets us to a specific memory cell. Then using a combination of multiplexer and demultiplexers it decodes the input address, finds which row and column of the memory block it has to traverse to reach the requested memory block. Of course, we’ve simplified the explanation to a great extent. Achieving this on the microprocessor level includes a significant amount of complexity.
It would be difficult for our microcontroller to meet the modern day speed-requirements if RAM didn’t exist onboard, because then the processor would need to fetch data from the flash memory, which is comparatively slower. It can be considered just as crucial as your processor or program memory. The more RAM your processor has access to, the easier its job becomes as there is more space and memory to work with.