Microcontrollers: Lecture Notes No. 9
Microcontrollers: Lecture Notes No. 9
Microcontrollers: Lecture Notes No. 9
RAM: Volatile memory (if the microcontroller loses power, the contents of
RAM memory are lost)
It can be written to and read from during program execution.
It is typically used during system development to store a program.
Once development is complete, the completed program is stored in
nonvolatile memory such as Flash EEPROM.
During program execution, RAM is used to store global variables,
support dynamic memory allocation of variables, and to provide a
location for the stack.
EEPROM Used to permanently store and recall variables during program execution.
Useful for storing data that must be retained during a power failure but
might need to be changed periodically.
Examples: applications in which it store system parameters, electronic lock
combinations, automatic garage door electronic unlock sequences etc.
With these microcontrollers, each type of memory is accessed separately, usually through different addressing modes (e.g., direct
access, indirect access via an index register to the EEPROM memory etc.).
Figure below shows an example of this organizing and addressing approach, considering that there are 3 types of internal memory:
32KB Flash memory, 16KB EEPROM memory, 16KB of data memory (SRAM).
It is noticeable that each address space starts with address 0, which is why each type of memory addresses with its own
addressing mode.
In order to understand how the location addresses have been passed to the hex,
the encoding mode is binary in hexadecimal:
(b) Common addressing space, regardless of the type of memory
With these microcontrollers all address space is common, with each of the memory types
having specific access addresses.
Figure below presents an example of this mode of organization and addressing (here, the same
types of memory and the same dimensions used previously are considered).
For these microcontrollers, most of the time, space is reserved also for possible memory that
could be connected externally.
To keep a track:
of the memory locations in use and
of the type of memory present within the system,
a visual tool called a memory map is employed.
Also, the address map within the memory address space on Intel systems is split into two separate
sub-ranges:
a range of addresses that when decoded accesses the DRAM (physical memory), and
a range of addresses that are decoded to select the I/O devices – MMIO (Memory Mapped I/O).
The main memory is connected to the CPU via a bus – in fact two or three buses:
the data bus;
the address bus and, also,
a control bus, which carries timing pulses and the level to indicate writing or
reading.
Processor
k-bit Memory
address bus
MAR Up to addressable locations
n-bit
data bus Word length = n bits
MBR
Control lines
(R/)
Let’s remember!
The address bus does not have to be the same width as the data bus.
The number of conductors in the address bus (the width of the address bus) sets the
upper limit of memory locations that may be linearly addressed by the
microcontroller (the number of uniquely addressable memory locations).
Processor
k-bit Memory
address bus
MAR Up to addressable locations
n-bit
data bus Word length = n bits
MBR
Control lines
(R/)
Let’s remember!
Solution:
We’ll have to arrange them:
“8 high” – to cover the address space, and
“2 wide” to cover the data space.
A cache is:
a smaller memory,
faster memory,
closer to a processor core,
which acts as a high speed buffer between CPU and
main memory. The term "cache memory"
or "memory cache" or
It temporary stores very active data and actions during shortly "cache" without
processing: any specification, usually
instructions currently being executed or which may be is referred to a
executed within a short period of time and/or "hidden memory that
data that the CPU may frequently require for stores a subset of
manipulation. main memory content "
and specifically the
It is:
typically integrated directly with the CPU chip or "Instructions" of a
placed on a separate chip that has a separate bus interconnect program and the related
with the CPU. "Data" that must be
MEMORY SYSTEM – Cache memory
When the processor needs to read from or write to a location in main memory:
It first checks whether a copy of that data is in the L1 cache. If so (this is called a “cache hit”), the
processor immediately reads from or writes to the cache, which is much faster than reading from or writing
to main memory.
If it does not find it ("memory miss"), it will continue to search for that instruction in the main memory,
but it will simply take longer.
Memory address
from processor Main memory accessed if
address not in cache
Address not
Address found found in cache
Address location
MEMORY SYSTEM – Cache memory
Temporal locality
Data that have been used recently, have high likelihood of being used again.
A cache stores only a subset of MM data – the most recent-used MRU. Data read from MM are temporary stored in cache. If the processor requires
the same data, this is supplied by the cache. The cache is effective because short instruction loops and routines are a common program structure
and generally several operations are performed on the same data values and variables.
Spatial locality
If a data is referenced, is very likely that nearby data will be accessed soon.
Instructions and data are transferred from MM to the cache in fixed blocks (cache block), known as cache lines. Cache line size is in the range of 4 to
512 bytes so that more than one processing data (4/8 bytes) is stored in each cache entry. After a first MM access, all cache line data are available in
cache.
Most programs are highly sequential. Next instruction usually comes from the next memory location. Data is usually structured and data in these
structures normally are stored in contiguous memory locations (data strings, arrays, etc.).
Large lines size increase the spatial locality but increase also the number of invalidated data in case of line replacement (see Replacement policy).
(Note – for brevity, the term "data" often will be used instead of "cache line" or "cache block")
MEMORY SYSTEM – Cache memory
Write Policy
When the processor writes data to cache, it must at some point write that data to the backing store as well.
The timing of this write is controlled by what is known as the write policy. There are two basic writing
approaches:
Write-through - write is done synchronously both to the cache and to the backing store.
Write-back (also - initially, writing is done only to the cache; the write to the backing store is
called write-behind) postponed until the cache blocks containing the data are about to be
modified/replaced by new content
- this method was used in the Intel processor class starting with the 80486
processor.
The processor’s cache memory may have one of the following two organizations:
unified cache memory - same memory for both data and code, and
split cache for data and instructions - this cache organization is called modified Harvard architecture and
it is specific for the Intel's processors (IA-32 and IA-64), and not
only. These caches are shortly called "I-Cache" for Instruction cache
and "D-Cache" for Data cache.
a) b)
a) Example of Unified Cache Memory, and b) A simplified block diagram of a CPU with split L1 cache
MEMORY SYSTEM – Cache memory
Secondary memory
As 16-bit processors have become obsolete and replaced with 32-bit and 64-bit in general use,
reserving ranges of memory address space for I/O is less of a problem, as the memory
address space of the processor is usually much larger than the required space for all memory
and I/O devices in a system.
Therefore, it has become more frequently practical to take advantage of the benefits of
memory-mapped I/O.
However, even with address space being no longer a major concern, neither I/O mapping
method is universally superior to the other, and there will be cases where using port-
mapped I/O is still preferable.
Memory-mapped IO
I/O devices are mapped into the system memory map along with
RAM and ROM. To access a hardware device, simply read or write
to those “special” addresses using the normal memory access
instructions.
All I/O devices monitor independently the CPU address bus and
respond to any access of device-assigned address space, connecting
the data bus to a desirable device's hardware register.
Full I/O decoding involves checking every single line (i.e., all bits)
of the address bus (and the I/O R/W signal eventually) to determine
if a device is selected or not. With Full I/O decoding, each hardware
register is mapped to an unique I/O port address.
The advantage to this system: Less logic is needed to decode a discrete address and therefore
less cost to add hardware devices to a machine.
On the older PC compatible machines, only 10 bits of address space were decoded for I/O ports and so there were
only 1024 unique port locations.
Modern PC's decode all 16 address lines.
To read or write from a hardware device, special port I/O instructions are used.
From a software perspective, this is a slight disadvantage because more instructions are required to
accomplish the same task.
For instance, if we want to test one bit on a memory mapped port, there is a single instruction to test a bit
in memory, but for ports we must read the data into a register, then test the bit.
Memory-mapped IO Port-mapped IO
Same address bus to address memory Different address spaces for
and I/O devices. memory and I/O devices.
Access to the I/O devices using regular Uses a special class of CPU
instructions. instructions to access I/O devices.
x86 Intel microprocessors - IN and
Most widely used I/O method.
OUT instructions.