Conjunto.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package cache;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.Objects;
  10. /**
  11. *
  12. * @author Juninho Carlos
  13. */
  14. public class Conjunto {
  15. private Bloco[] blocosDoConjunto;
  16. private int RR;
  17. public LRU lru;
  18. public Conjunto() {
  19. this.RR = 0;
  20. this.lru = new LRU();
  21. this.blocosDoConjunto = new Bloco[4];
  22. for (int i = 0; i < 4; i++) {
  23. this.blocosDoConjunto[i] = new Bloco();
  24. }
  25. }
  26. public boolean isBlocoOcupado(int num_bloco) {
  27. return this.blocosDoConjunto[num_bloco].isValido();
  28. }
  29. public boolean arrayHasTheValue(ArrayList<Integer> v, Integer num) {
  30. for (Integer integer : v) {
  31. if (integer == num) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /**
  38. * Escreve as palavras com erro, de um bloco, na redundância.
  39. * @param req
  40. * @param redundancia
  41. * @return Retorna os endereços que foram sobrescritos na redundancia
  42. *
  43. */
  44. /**
  45. * Escreve as palavras com erro, de um bloco, na redundância.
  46. * @param req
  47. * @param redundancia
  48. * @param bloco
  49. * @return Retorna os endereços que foram sobrescritos na redundancia
  50. */
  51. public int getRR(){
  52. return this.RR;
  53. }
  54. public LRU getLRU() {
  55. return this.lru;
  56. }
  57. public void desalocaBloco(int numBloco){
  58. this.blocosDoConjunto[numBloco].setValidadeDoBloco(false);
  59. this.blocosDoConjunto[numBloco].setSujeira(false);
  60. }
  61. public void alocarBloco(Requisicao req, int numBloco) {
  62. this.blocosDoConjunto[numBloco].setSujeira(!req.read);
  63. this.blocosDoConjunto[numBloco].setValidadeDoBloco(true);
  64. this.blocosDoConjunto[numBloco].setLimitesDoBloco(req.limiteInf, req.limiteSup);
  65. }
  66. public Bloco getBloco(int numBloco){
  67. return this.blocosDoConjunto[numBloco];
  68. }
  69. public void atualizaRR() {
  70. this.RR++;
  71. if (this.RR == 4) {
  72. this.RR = 0;
  73. }
  74. }
  75. public boolean hasBlocoLivre() {
  76. for (int i = 0; i < 4; i++) {
  77. if (!this.blocosDoConjunto[i].isValido()) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. public Bloco[] getBlocos() {
  84. return this.blocosDoConjunto;
  85. }
  86. /*public int getBlocoIndexFromAddress(int a){
  87. }*/
  88. /**
  89. * Verifica se o bloco está nesse conjunto e atualiza o bit de sujeira do bloco se for um write
  90. *
  91. * @param req
  92. * @return
  93. */
  94. public boolean verificaHit(Requisicao req) {
  95. for (int i = 0; i < 4; i++) {
  96. //verifica o hit
  97. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  98. //atualiza a política LRU
  99. this.lru.atualizaLRU(i);
  100. if (!req.read) {//Se for uma escrita, atualiza para bloco sujo
  101. this.blocosDoConjunto[i].setSujeira(true);
  102. }
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. public int getBlocoFromReq(Requisicao req) {
  109. for (int i = 0; i < 4; i++) {
  110. //verifica o hit
  111. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  112. return i;
  113. }
  114. }
  115. return -1;
  116. }
  117. }