Introduction
Cache Basics Direct Mapped Cache Direct Mapped Example 1 Direct Mapped Example 2 Direct Mapped Problem 1 Direct Mapped Problem 2 Direct Mapped Problem 3 Direct Mapped Problem 4 Direct Mapped Comparators Direct Mapped Disadvantages Direct Mapped Locality Direct Mapped UVM Example Associative Mapped Cache Associative Mapped Problem 1 Associative Mapped Problem 2 Associative Mapped Problem 3 Associative Mapped Problem 4 Set Associative Mapped Cache Set Associative Mapped Comparators Set Associative Mapped Problem 1 Set Associative Mapped Problem 2 Set Associative Mapped Problem 3 Set Associative Mapped Problem 4 Set Associative Mapped Problem 5 Other Mapping Problem - Example 1 Cache Replacement Algorithms LRU Cache Replacement Algorithm FIFO Cache Replacement Algorithm MRU Cache Replacement Algorithm PLRU Cache Replacement Algorithm Round Robin Cache Replacement AlgorithmUVMArena
Direct Mapped Cache – Comparators
In a Direct Mapped Cache, the CPU generates a logical address which is translated into a physical address. This physical address is divided into three fields:
- Tag
- Line Index
- Offset
How the Cache Lookup Works
When the CPU accesses memory, the cache controller performs the following steps:
- The Line Index field is used to identify the cache line where the requested block may be located.
- The Tag from the physical address is compared with the tag stored in that cache line.
- If both tags match, it is a cache hit.
- If the tags do not match, it is a cache miss.
This comparison between the two tag values is performed using a hardware circuit called a comparator.
What is a Comparator?
A comparator is a digital circuit that compares two binary numbers and determines whether they are equal.
The comparator receives two binary inputs and produces an output:
- Output = 1 → both inputs are equal
- Output = 0 → inputs are different
Example of a 4-bit comparator:
Input B: 0011
Output: 1 (equal)
If the inputs were different, the output would be:
Input B: 0101
Output: 0 (not equal)
Why Comparators Are Needed
In cache memory, the comparator checks whether the requested memory block is currently stored in the cache.
Specifically, it compares:
- The tag from the physical address
- The tag stored in the selected cache line
If the comparator output is 1, the requested data is in the cache and we have a cache hit. Otherwise, it is a cache miss.
Number of Comparators in Direct Mapping
In a Direct Mapped Cache, each memory block can map to only one specific cache line.
Because the line index directly identifies the only possible location of the block, the system needs to check the tag for only one cache line.
Other mapping techniques (such as associative or set-associative mapping) may require multiple comparators because the block could be located in several different cache lines.
Size of the Comparator
The size of the comparator depends on the number of bits in the tag field.
Since the comparator compares two tags, its input width must match the tag size.
Example
- If the tag is 3 bits, we need a 3-bit comparator
- If the tag is 4 bits, we need a 4-bit comparator
- If the tag is 8 bits, we need an 8-bit comparator
This is because the comparator must check equality between two binary values of the same size.
Key Points
- A comparator is used to compare the tag from the physical address with the tag stored in the cache line.
- In Direct Mapping, the requested block can be located in only one cache line.
- Therefore, only one comparator is required.
- The size of the comparator equals the number of tag bits.