fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. #define TLB_SETS 16
  6. #define TLB_WAYS 4
  7. #define POBITS 12
  8.  
  9. typedef struct {
  10. size_t vpn;
  11. size_t ppn;
  12. int valid;
  13. int lru_counter;
  14. } TLBEntry;
  15.  
  16. TLBEntry tlb[TLB_SETS][TLB_WAYS];
  17.  
  18. void tlb_clear() {
  19. for (int i = 0; i < TLB_SETS; i++) {
  20. for (int j = 0; j < TLB_WAYS; j++) {
  21. tlb[i][j].valid = 0;
  22. tlb[i][j].lru_counter = 0;
  23. }
  24. }
  25. }
  26.  
  27. int tlb_peek(size_t va) {
  28. size_t vpn = va >> POBITS;
  29. int set = vpn % TLB_SETS;
  30.  
  31. for (int i = 0; i < TLB_WAYS; i++) {
  32. if (tlb[set][i].valid && tlb[set][i].vpn == vpn) {
  33. return tlb[set][i].lru_counter;
  34. }
  35. }
  36. return 0;
  37. }
  38.  
  39. void update_lru(int set, int way) {
  40. int used_counter = tlb[set][way].lru_counter;
  41. for (int i = 0; i < TLB_WAYS; i++) {
  42. if (tlb[set][i].lru_counter < used_counter) {
  43. tlb[set][i].lru_counter++;
  44. }
  45. }
  46. tlb[set][way].lru_counter = 1;
  47. }
  48.  
  49. size_t translate(size_t va) {
  50. if (va >= 0x12345000 && va < 0x12346000) {
  51. return 0x22345000 + (va - 0x12345000);
  52. } else if (va >= 0x23456000 && va < 0x23457000) {
  53. return 0x33456000 + (va - 0x23456000);
  54. } else {
  55. return (size_t)-1;
  56. }
  57. }
  58.  
  59. size_t tlb_translate(size_t va) {
  60. size_t vpn = va >> POBITS;
  61. int set = vpn % TLB_SETS;
  62.  
  63. for (int i = 0; i < TLB_WAYS; i++) {
  64. if (tlb[set][i].valid && tlb[set][i].vpn == vpn) {
  65. update_lru(set, i);
  66. return (tlb[set][i].ppn << POBITS) | (va & ((1 << POBITS) - 1));
  67. }
  68. }
  69.  
  70. size_t pa = translate(vpn << POBITS);
  71. if (pa == (size_t)-1) return (size_t)-1;
  72.  
  73. int lru_way = 0;
  74. for (int i = 1; i < TLB_WAYS; i++) {
  75. if (tlb[set][i].lru_counter > tlb[set][lru_way].lru_counter) {
  76. lru_way = i;
  77. }
  78. }
  79.  
  80. tlb[set][lru_way].vpn = vpn;
  81. tlb[set][lru_way].ppn = pa >> POBITS;
  82. tlb[set][lru_way].valid = 1;
  83. update_lru(set, lru_way);
  84.  
  85. return (tlb[set][lru_way].ppn << POBITS) | (va & ((1 << POBITS) - 1));
  86. }
  87.  
  88. int main() {
  89. tlb_clear();
  90.  
  91. printf("Translating VA: 0x12345678\n");
  92. size_t pa = tlb_translate(0x12345678);
  93. if (pa == (size_t)-1) {
  94. printf("Translation failed for VA: 0x12345678\n");
  95. } else {
  96. printf("Translated PA: 0x%zx\n", pa);
  97. }
  98.  
  99. printf("Translating VA: 0x23456789\n");
  100. pa = tlb_translate(0x23456789);
  101. if (pa == (size_t)-1) {
  102. printf("Translation failed for VA: 0x23456789\n");
  103. } else {
  104. printf("Translated PA: 0x%zx\n", pa);
  105. }
  106.  
  107. return 0;
  108. }
  109.  
Success #stdin #stdout 0s 5284KB
stdin
 
stdout
Translating VA: 0x12345678
Translated PA: 0x22345678
Translating VA: 0x23456789
Translated PA: 0x33456789