4.cache Memory
4.cache Memory
ORGANIZATION
06/28/2024
INTRODUCTION
Every computer contains several types of memory devices
to store the instructions and data required for its operation.
The memory devices of a computer system are of four types
i) CPU registers
ii) cache memory
iii) main memory
iv) secondary memory
The main memory stores the programs and data that are in
active use. The main memory operates at very low speed as
compare to CPU . The cache memory serves as a buffer
between the main memory and the CPU so that the CPU
can operate near to its maximum speed.
BLOCK DIAGRAM OF A MEMORY
UNIT
m 2m ×w w
Address A RAM Data D
Output enable OE
Write enable WE
Chip select CS
The RAM has m-bit unidirectional address bus A and w-bit bidirectional data
bus D.
06/28/2024
06/28/2024
MEMORY ADDRESSES
0000000000 0 1000111111001010
0000000001 1 1000101011110001
…… …..
……
…… …..
..….
1111001101011010
1111111101 1021
1111111110 1022 1100110010101111
1111111111 1023 1110110010101100
The selection of a specific word inside memory is done by applying the k-bit
address to the address line.
A decoder accepts this address and opens the path needed to select the
word specified.
06/28/2024
MEMORY HIERARCHY
Decreasing cost per bit Co1 > Co2 > Co3 > Co4 >Co5 >Co6
Increasing capacity Ca1 < Ca2 < Ca3 < Ca4 < Ca5 <Ca6
06/28/2024
CACHE MEMORY
Cache memory serves as a buffer between the CPU and main memory. It is
intended to give faster memory speed and a larger memory capacity at less
price.
The (fig-4 b) shows the use of multiple levels of cache . The L2 cache is
slower and typically larger than L1 cache, and L3 cache is slower and
typically larger than the L2 cache.
Cache hit- when the CPU finds a requested data item in the cache, it is
called cache hit.
Cache miss-when the CPU does not find a data it needs in the cache, it is
called cache miss.
Hit ratio-It is the percentage of data found in the cache.
Access time-It is the total time taken to bring the required data form the
memory to CPU.
06/28/2024
In the figure -4(a) if
t1= time required to read from cache memory
t2= time required to read from main memory
h = is the hit ratio
Access time = h*t1 + (1-h)*(t1+t2)
In the figure-4(b) if
t1=time required to read the data from L1 cache
t2= time required to read the data from L2 cache
t3= time required to read the data from L3 cache
t4= time required to read the data from main memory
h1=hit ratio in L1 cache
h2=hit ratio in L2 cache
h3=hit ratio in L3 cache
Access time = h1t1 + h2(1-h1)(t1+t2) + h3(1-h1)(1-h2)(t1+t2+t3) +
(1-h1)(1-h2)(1-h3)(t1+t2+t3+t4)
06/28/2024
CACHE ORGANISATION-
Cache data
Hit memory
Cache tag
memory
The above fig shows the principal components of a cache . Memory words
are stored in a cache data memory and are grouped into small pages called
cache blocks or lines. The contents of the cache’s data memory are thus
copies of a set of main memory blocks. Each cache block is marked with its
block address referred to as tag.
06/28/2024
CACHE AND MAIN MEMORY
STRUCTURE
06/28/2024
MAPPING MEMORY TO CACHE
There are fewer cache lines than main memory blocks so an algorithm
is needed for mapping main memory blocks into cache lines.
06/28/2024
DIRECT MAPPING
In direct mapping is the simplest technique . It maps each blocks of main
memory into only one possible cache line . The mapping is expressed as
P=K module N
Where P=cache line number
K=main memory block number
N=number of lines in the cache
The Kth block of main memory has to be placed in (KmodN) th cache
position.
The physical address is divided into three field
physical address
1,m+1, 2m+1…2s-m+1
1
…
m-1, 2m-1,3m-1…2s-1
m-1
MM=512 words
00 011 1 0
01 CM=64 words
001 111 1 mod 4 1
9
10 110
11 001
29
N=4 blocks
30
31
M=32 blocks
TAG 000 001 010 011 100 101 110 111 Cache
Memory
Main Memory { 0, 4, 8, 12, 16, 20, 24, 28 } --- 0 00
Blocks
blocks { 1, 5, 9, 13, 17, 21, 25, 29}--- 1 01
{ 2, 6, 10, 14, 18, 22, 26, 30}--- 2 10
{3, 7, 11, 15, 19, 23, 27, 31}--- 3 11
1 1 0 0 1 0 0 0 0
0 1 1 0 0 0 1 1 1
Advantages-
1. simplest mapping technique.
2. Less number of tag bits and less tag comparator required.
Disadvantages-
1. It is a slowest mapping technique and requires more block movement.
06/28/2024
ASSOCIATIVE MAPPING
In associative mapping any block of main memory can be placed any where
in cache memory . In this case, the physical memory address can be divided
as a tag and a word field.
The tag field uniquely identifies a block of main memory. To determine
whether a block is in cache or not, each cache blocks are to be tested.
Hence the number of tag comparator is equal to number of cache blocks.
physical address
log M = s log P = w
06/28/2024
Address length = (s + w) bits
Number of addressable units = 2(s+w)
Advantages-
1. Each block of main memory can be mapped in to any block of cache
memory.
Disadvantages-
1. More tag bits are there and hence more TAG memory06/28/2024
required.
2. More comparator required and hence it is more costly.
SET ASSOCITIVE MAPPING
The set associative mapping is having the advantages of both direct mapping
and associative mapping.
Here the cache is divided into logical sets.
In 4-way set association each set is allocated with four cache blocks.
The physical address is divided into three fields.
1. word offset
2. set offset
3. tag information
physical address
TAG Set offset
Word offset w
06/28/2024
The cache consists of a number sets, each of which consists of a number of
lines. The relationships are
m=v*k
i = j modulo v
Where
i = cache set number
j = main memory block number
m = number of lines in the cache
v = number of sets
k = number of lines in each set
This is referred to as k-way set-associative mapping
06/28/2024
Explanation of Set associative memory
MM=512 words
0
0 0111 CM= 641words
sets
1001 111 0mod2 91
1101
1 0101
29
N=4 blocks
30
31
M=32 blocks
TAG
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
set0 1 3 5 7 9 11 13 15 17 19 21 23 25
27 29 31 set1
06/28/2024
Example- 1
The processor want to access the data of memory location (122)10 .
Find whether it is a hit or a miss.
Solution-
The set no ‘1’ does not contain the tag bit 0011. So it is a miss.
06/28/2024
Example-2
1 1 0 1 1 1 0 1 0
Advantages-
1. Number of TAG comparator required is equal to the number of blocks with in a set.
Disadvantages-
1. Complex in structure.
06/28/2024
UPDATION TECHNIQUE
The site effect of memory hierarchy is the data inconsistency . Same
information is available differently at different places.
Proper updation technique reduce this problem.
Write-back updation
Write-through updation
Write-back updation
Here the cache act like a buffer by receiving data from the processor and
writing data back to main memory whenever the system bus is available.
Advantage-
The processor is freed up to continue with other tasks while main memory is
updated at later time.
Disadvantage-
The cost and complexity of cache subsequently increase.
06/28/2024
Write through updation
The processor handles write to main memory instead of the cache.
The cache may update its contents as the data comes through from the
processor. The write operation does not end until the processor has
write the data back to main memory.
Advantage-
The cache does not have to be complex , which thus makes it less
expansive.
Disadvantage-
The processor must wait until the main memory accepts the data before
moving on to its next task
06/28/2024
CONCLUSION-
The goal of memory hierarchy is to obtain a cost per bit close to that of
the least expansive memory and access time close to that of the fastest
memory.
To reduce the speed difference between CPU and main memory cache
memory is used.
Various updation techniques are used to maintain the consistency of
data in memory.
06/28/2024
THANK YOU
06/28/2024