Advertisement
sandeshMC

Set Associative Cache

Apr 12th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SetAssociativeCache {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.println("Enter the size of main memory : ");
  9.         int m = sc.nextInt();
  10.         System.out.println("Enter the number of words in each block: ");
  11.         int w = sc.nextInt();
  12.         System.out.println("Enter the size of cache : ");
  13.         int c = sc.nextInt();
  14.         int n = (int) ((int) Math.log(m / w) / Math.log(2));
  15.         System.out.println("Number of bits in main memory address : "
  16.                 + Math.ceil((Math.log(m) / Math.log(2))) + " bits");
  17.         System.out
  18.                 .println("Number of bits required to address main memory blocks (S) : "
  19.                         + n);
  20.         int setsInCacheMemory = (m / (2 * c));
  21.         System.out.println("Number of sets in cache memory : "
  22.                 + setsInCacheMemory);
  23.         int set = (int) Math.log(setsInCacheMemory);
  24.         System.out.println("SET : " + set);
  25.         int tag = n - set;
  26.         System.out.println("TAG : " + tag);
  27.         int word = (int) (Math.log(w) / Math.log(2));
  28.         System.out.println("WORD : " + word);
  29.         for (int i = 0; i < 3; i++) {
  30.             System.out.print(" _______");
  31.         }
  32.         System.out.println();
  33.         for (int i = 0; i < 3; i++)
  34.             System.out.print("|       ");
  35.         System.out.println("|");
  36.         System.out.print("|   " + tag + "   ");
  37.         System.out.print("|   " + set + "   ");
  38.         System.out.print("|   " + word + "   ");
  39.         System.out.println("|");
  40.  
  41.         for (int i = 0; i < 3; i++) {
  42.             System.out.print("|_______");
  43.         }
  44.         System.out.println("|");
  45.         System.out.print("   tag     set     word");
  46.  
  47.     }
  48.  
  49. }
  50.  
  51. /*         
  52. Enter the size of main memory :
  53. 256
  54. Enter the number of words in each block:
  55. 4
  56. Enter the size of cache :
  57. 32
  58. Number of bits in main memory address : 8.0 bits
  59. Number of bits required to address main memory blocks (S) : 5
  60. Number of sets in cache memory : 4
  61. SET : 1
  62. TAG : 4
  63. WORD : 2
  64.  _______ _______ _______
  65. |       |       |       |
  66. |   4   |   1   |   2   |
  67. |_______|_______|_______|
  68.    tag     set     word
  69. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement