Bit shifting - cache simulator

This is a discussion on Bit shifting - cache simulator within the C++ Programming forums, part of the General Programming Boards category; I'm attempting to make a cache simulator in C++. But I need to access individual bits in an integer to ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Bit shifting - cache simulator

    I'm attempting to make a cache simulator in C++. But I need to access individual bits in an integer to figure out where in my "cache" the writing actually gets done. I'm pretty new to bit shifting. Say I'm trying to access the the bits of the int 5, which are its "address". I'm simulating a direct mapped cache. I need to find its tag, the set it goes into, and which line. How do I use bit shifting to access the bits to aquire the tag, the index bits, offset bits, block number...all these pieces in order to actually find where I store it in the cache. Thank you!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,291
    If the data is in a single integer, you just need to do a right shift followed by an and operation. If it's two 32 bit integers, and you need to shift right "n" bits, you shift the "upper" word left (32 - n) bits then "and" off the unwanted bits, shift the lower word right "n" bits, "and" off unwanted bits, then "or" the result together. You could also use a structure with bit fields and let the compiler do the work for you. If this is a 2 way or more associative cache, you'll also need to implement a hashing algorithm to emulate the associative (content addressable) memory.
    Last edited by rcgldr; 04-18-2013 at 09:18 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    I need to break the bits up into 3 sections: tag, set index, and block index. I think I can figure out the set and block index sizes based on the values passed in. The tag bits are just the remaining ones. And I'm hard coding values such as cache size (C) - 1024, number of physical address bits (m) - 32, block size (B) - 2, number of lines per set (E) - 1 (again, directly mapped cache). How would this look?

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by rcgldr View Post
    If the data is in a single integer, you just need to do a right shift followed by an and operation. If it's two 32 bit integers, and you need to shift right "n" bits, you shift the "upper" word left (32 - n) bits then "and" off the unwanted bits, shift the lower word right "n" bits, "and" off unwanted bits, then "or" the result together. You could also use a structure with bit fields and let the compiler do the work for you. If this is a 2 way or more associative cache, you'll also need to implement a hashing algorithm to emulate the associative (content addressable) memory.


    For now I'm just attempting a directly mapped cache

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    I'm actually passing ints to a function which takes in unsigned longs so I can handle up to 64 bits

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a cache simulator
    By vip3r in forum C Programming
    Replies: 3
    Last Post: 10-18-2011, 05:03 AM
  2. D-Cache/I Cache Simulator
    By husslela2 in forum C Programming
    Replies: 7
    Last Post: 04-27-2010, 08:41 AM
  3. Cache Simulator
    By husslela2 in forum C Programming
    Replies: 1
    Last Post: 04-12-2010, 08:56 PM
  4. Difference between ARP Cache and DNS cache?
    By namasteall2000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-26-2009, 08:49 AM
  5. need help with cache simulator!
    By dtogers123 in forum C Programming
    Replies: 3
    Last Post: 04-30-2008, 06:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21