Advertisement
Guest User

WILL

a guest
Oct 26th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. struct row { int valid; int tag; int timeStamp; };
  7. struct row *firstRow = NULL;
  8. struct row cache[5000];
  9. int maxCacheIndex;
  10. int testing_sequence[] = { 0, 4, 8, 12, 16, 20, 24,
  11. 28, 32, 36, 40, 44, 48, 52,
  12. 56, 60, 64, 68, 72, 76, 80,
  13. 0, 4, 8, 12, 16, 71, 3, 41,
  14. 81, 39, 38, 71, 15, 39, 11,
  15. 51, 57, 41 };
  16. int cacheNum = 0;
  17.  
  18. int testing_size = sizeof(testing_sequence)/sizeof(int);
  19.  
  20. int evictCacheLine(int k, int index) {
  21. int firstTime = cache[k*index].timeStamp;
  22. int indexTrack = k*index;
  23. int rowIdx = 0;
  24. for ( rowIdx = k*index; rowIdx < k*index + k; rowIdx++ ) {
  25. if ( cache[rowIdx].timeStamp < firstTime ) {
  26. firstTime = cache[rowIdx].timeStamp;
  27. indexTrack = rowIdx;
  28. }
  29. }
  30. }
  31.  
  32. int isHitOrMissForDirectMapped( int tag, int index );
  33. int isHitOrMissForSetAssoc( int k, int tag, int index );
  34.  
  35. int isHitOrMissForDirectMapped( int tag, int index ) {
  36. int isHit = 0;
  37. // For direct-mapped, index is the cache line number.
  38. if ( cache[index].valid && cache[index].tag == tag ) {
  39. isHit = true;
  40. }
  41. else {
  42. isHit = false;
  43. cache[index].valid = 1;
  44. cache[index].tag = tag;
  45. }
  46.  
  47. return isHit;
  48. }
  49.  
  50. int isHitOrMissForSetAssoc( int k, int tag, int index ) {
  51. int isHit = 0; // Initialize isHit to default value: false
  52. int rowIdx = 0;
  53. // For set associative, index is the set number.
  54. for ( rowIdx = k*index; rowIdx < k*index + k; rowIdx++ ) {
  55. if ( cache[rowIdx].valid && cache[rowIdx].tag == tag ) {
  56. isHit = true;
  57. break;
  58. }
  59. }
  60. // Now, isHit has value true if and only if we found a hit.
  61. if (isHit) {
  62. return 1; // return true
  63. }
  64. // Else search for cache line with valid field == 0 (false)
  65. for ( rowIdx = k*index; rowIdx < k*index + k; rowIdx++ ) {
  66. if ( cache[rowIdx].valid == 0 ) {
  67. cache[rowIdx].valid = 1;
  68. cache[rowIdx].timeStamp = cacheNum;
  69. cache[rowIdx].tag = tag;
  70. cacheNum++;
  71. break;
  72. }
  73. }
  74. int temp;
  75. // If we didn't find a cache line with valid field false, then evict cache line
  76. if (rowIdx >= k*index + k) { // if failed to find invalid cache line
  77. temp = evictCacheLine( k, index );
  78. cache[temp].timeStamp = cacheNum;
  79. cacheNum++;
  80. cache[temp].valid = 1;
  81. cache[temp].tag = tag;
  82. }
  83.  
  84. return isHit;
  85. }
  86.  
  87. int directMappedCache( int s, int l) {
  88. int x = 0;
  89. int tag, index, answer, offset;
  90. for (x = 0; x < testing_size; x++) {
  91. int address = testing_sequence[x];
  92. tag = address/s;
  93. index = address/l;
  94. offset = address % l;
  95. // We declared isHitOrMissForDirectMapped above. But we define it below.
  96. answer = isHitOrMissForDirectMapped( tag, index );
  97. if (answer)
  98. printf("%d: HIT (Tag/Set#/Offset: %d/%d/%d)\n", address, tag, index, offset);
  99. else
  100. printf("%d: MISS (Tag/Set#/Offset: %d/%d/%d)\n", address, tag, index, offset);
  101. }
  102. return 0; // The fnc main returns 0 for success. Anything else is error number.
  103. }
  104.  
  105. int setAssociateCache( int s, int l, int n ) {
  106. int x;
  107. int k = s/n;
  108. int numberOfSets = s/l/n;
  109. int tag, index, offset, answer;
  110. for (x = 0; x < testing_size; x++) {
  111. int address = testing_sequence[x];
  112. tag = address/k;
  113. index = (address/l) % numberOfSets;
  114. offset = address % l;
  115. // We declared isHitOrMissForDirectMapped above. But we define it below.
  116. answer = isHitOrMissForSetAssoc( k, tag, index );
  117. if (answer) {
  118. printf("%d: HIT (Tag/Set#/Offset: %d/%d/%d)\n", address, tag, index, offset);
  119. }
  120. else {
  121. printf("%d: MISS (Tag/Set#/Offset: %d/%d/%d)\n", address, tag, index, offset);
  122. }
  123. }
  124.  
  125. return 0; // The fnc main returns 0 for success. Anything else is error number.
  126. }
  127.  
  128. int main() {
  129. printf( "Test 1\n");
  130. directMappedCache( 128, 8 );
  131.  
  132. memset( cache, 0, 5000 );
  133. printf( "Test 2\n" );
  134. setAssociateCache( 64, 8, 2 );
  135.  
  136. memset( cache, 0, 5000 );
  137. printf( "Test 3\n" );
  138. directMappedCache( 128, 16 );
  139.  
  140. memset( cache, 0, 5000 );
  141. printf( "Test 4\n" );
  142. setAssociateCache( 64, 8, 8 );
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement