Building a direct mapped cache with circuits

In order to construct a direct mapped cache, we must understand the various mappings:

 

Building a direct mapped cache with circuits

Because the cached unit = 4 bytes, we identify a "cacheable" memory unit with (mem addr)/4:

Recall: (mem addr)/4 = remove the last 2 bits from a mem addr
E.g.: addr = 0000..00001100, addr/4 = 0000..000011

Building a direct mapped cache with circuits

The mappings are easier to see when address/4 is expressed in binary:

Recall that the mapping depends on the cache size --- I used an 8 slots cache as example only !!

Building a direct mapped cache with circuits

The block number is equal to (address/4) / cacheSize:

In the 8 slots cache, we divide by 8 and this is the same as removing 3 bits from (address/4)

Building a direct mapped cache with circuits

The slot number is equal to (address/4) % cacheSize:

E.g.: the slot 2 (= 010) can cache data from addresses 8,9,10,11, 40,41,42,43, etc

Building a direct mapped cache with circuits - circuit

The direct-mapped cache can be constructed as follows:

I will explain the design in a piecemeal fashion next....

Building a direct mapped cache with circuits - circuit

The inputs and outputs of direct-mapped cache circuit are:

Inputs: address from CPU (read request);     outputs: cache hit (0/1) and data if cache hit=1

Building a direct mapped cache with circuits - circuit

We first "compute" address/4 by omitting the last 2 address signals:

Recalls that the value address/4 comprises of 2 parts:   BlockNumOffset

Building a direct mapped cache with circuits - circuit

In this figure, I have assign the offset and block number with a different color:

The offset will determine which cache slot contains the data !!

Building a direct mapped cache with circuits - circuit

How the offset value determines the cache slot:

When offset = 000, the data will be stored in the slot 000

Building a direct mapped cache with circuits - circuit

How the offset value determines the cache slot:

When offset = 001, the data will be stored in the slot 001 - and so on !!!

Building a direct mapped cache with circuits - circuit

When offset=000, we must use the block number is slot 000 to determine cache hit:

If the 2 block numbers are equal, we have a cache hit (if valid =1)

Building a direct mapped cache with circuits - circuit

When offset=001, we must use the block number is slot 001 to determine cache hit:

If the 2 block numbers are equal, we have a cache hit (if valid =1)

Building a direct mapped cache with circuits - circuit

We can achieve this by using multiplexors to select the correct block number:

The output of the MUX is the block number in the "offset" slot in the cache

Building a direct mapped cache with circuits - circuit

We check whether the 2 block numbers are equal:

To have a cache hit, the entry that store the data must be valid

Building a direct mapped cache with circuits - circuit

We first select the valid bit from the slot that stores the data:

 

Building a direct mapped cache with circuits - circuit

... and make sure that both condiions for cache hit are satisfied:

The Check Hit output =   1 when there is a cache hit and = 0 otherwise

Building a direct mapped cache with circuits - circuit

The data stored in the "offset" slot entry is output as follows:

The cache will return the data output to the CPU only when: Cache Hit=1 !!

Strength and weakness of the direct mapped cache
 

  • Strength: cheap

      • The Direct-Mapped cache uses only uses 1 compare circuit !!!

        (It also has 3 multiplexors, but this is much less circuitry than 16 million compare circuits)

  • Weakness: inflexible

      • Data from a memory location can only be stored in one specific slot in the direct mapped cache

We will look at the set-associative cache next that combine the 2 caching techniques that we have studied