Search code examples
cachingassemblycpu-architecturecpu-cache

Direct mapped cache example


i am really confused on the topic Direct Mapped Cache i've been looking around for an example with a good explanation and it's making me more confused then ever.

For example: I have

  • 2048 byte memory
  • 64 byte big cache
  • 8 byte cache lines with direct mapped cache how do i determine the 'LINE' 'TAG' and "Byte offset'?


  • i believe that the total number of addressing bits is 11 bits because 2048 = 2^11

  • 2048/64 = 2^5 = 32 blocks (0 to 31) (5bits needed) (tag)

  • 64/8 = 8 = 2^3 = 3 bits for the index

  • 8 byte cache lines = 2^3 which means i need 3 bits for the byte offset

so the addres would be like this: 5 for the tag, 3 for the index and 3 for the byte offset

Do i have this figured out correctly?


Solution

  • Do i figured out correctly? YES

    Explanation

    1) Main memmory size is 2048 bytes = 211. So you need 11 bits to address a byte (If your word size is 1 byte) [word = smallest individual unit that will be accessed with the address]

    2) You can calculating tag bits in direct mapping by doing (main memmory size / cash size). But i will explain a little more about tag bits.

    Here the size of a cashe line( which is always same as size of a main memmory block) is 8 bytes. which is 23 bytes. So you need 3 bits to represent a byte within a cashe line. Now you have 8 bits (11 - 3) are remaining in the address.

    Now the total number of lines present in the cache is (cashe size / line size) = 26 / 23 = 23

    So, you have 3 bits to represent the line in which the your required byte is present.

    The number of remaining bits now are 5 (8 - 3).

    These 5 bits can be used to represent a tag. :)

    3) 3 bit for index. If you were trying to label the number of bits needed to represent a line as index. Yes you are right.

    4) 3 bits will be used to access a byte withing a cache line. (8 = 23)

    So,

    11 bits total address length = 5 tag bits + 3 bits to represent a line + 3 bits to represent a byte(word) withing a line

    Hope there is no confusion now.