Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SetAssociativeCache {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the size of main memory : ");
- int m = sc.nextInt();
- System.out.println("Enter the number of words in each block: ");
- int w = sc.nextInt();
- System.out.println("Enter the size of cache : ");
- int c = sc.nextInt();
- int n = (int) ((int) Math.log(m / w) / Math.log(2));
- System.out.println("Number of bits in main memory address : "
- + Math.ceil((Math.log(m) / Math.log(2))) + " bits");
- System.out
- .println("Number of bits required to address main memory blocks (S) : "
- + n);
- int setsInCacheMemory = (m / (2 * c));
- System.out.println("Number of sets in cache memory : "
- + setsInCacheMemory);
- int set = (int) Math.log(setsInCacheMemory);
- System.out.println("SET : " + set);
- int tag = n - set;
- System.out.println("TAG : " + tag);
- int word = (int) (Math.log(w) / Math.log(2));
- System.out.println("WORD : " + word);
- for (int i = 0; i < 3; i++) {
- System.out.print(" _______");
- }
- System.out.println();
- for (int i = 0; i < 3; i++)
- System.out.print("| ");
- System.out.println("|");
- System.out.print("| " + tag + " ");
- System.out.print("| " + set + " ");
- System.out.print("| " + word + " ");
- System.out.println("|");
- for (int i = 0; i < 3; i++) {
- System.out.print("|_______");
- }
- System.out.println("|");
- System.out.print(" tag set word");
- }
- }
- /*
- Enter the size of main memory :
- 256
- Enter the number of words in each block:
- 4
- Enter the size of cache :
- 32
- Number of bits in main memory address : 8.0 bits
- Number of bits required to address main memory blocks (S) : 5
- Number of sets in cache memory : 4
- SET : 1
- TAG : 4
- WORD : 2
- _______ _______ _______
- | | | |
- | 4 | 1 | 2 |
- |_______|_______|_______|
- tag set word
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement