Conjunto.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  9. *
  10. * @author Juninho Carlos
  11. */
  12. public class Conjunto {
  13. private Bloco[] blocosDoConjunto;
  14. private int RR;
  15. public LRU lru;
  16. private int qntdDeBlocos;
  17. private int qntdDeBlocosOcupados;
  18. public Conjunto() {
  19. this.RR = 0;
  20. this.lru = new LRU();
  21. this.qntdDeBlocos = 4;
  22. this.qntdDeBlocosOcupados = 0;
  23. this.blocosDoConjunto = new Bloco[4];
  24. for (int i = 0; i < 4; i++) {
  25. this.blocosDoConjunto[i] = new Bloco();
  26. }
  27. }
  28. public boolean isBlocoOcupado(int num_bloco) {
  29. return this.blocosDoConjunto[num_bloco].isValido();
  30. }
  31. public boolean hasBlocoSemErro(){
  32. return this.qntdDeBlocos > 0;
  33. }
  34. public boolean arrayHasTheValue(ArrayList<Integer> v, Integer num) {
  35. for (Integer integer : v) {
  36. if (integer == num) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. public int getRR(){
  43. for(int i = 0; i < 4; i++){
  44. if(this.blocosDoConjunto[this.RR].isCachavel()){
  45. return this.RR;
  46. }else{
  47. this.atualizaRR();
  48. }
  49. }
  50. return -1;
  51. }
  52. public LRU getLRU() {
  53. return this.lru;
  54. }
  55. public void desalocaBloco(int numBloco){
  56. this.blocosDoConjunto[numBloco].setValidadeDoBloco(false);
  57. this.blocosDoConjunto[numBloco].setSujeira(false);
  58. this.qntdDeBlocosOcupados--;
  59. }
  60. public void alocarBloco(Requisicao req, int numBloco) {
  61. this.blocosDoConjunto[numBloco].setSujeira(!req.read);
  62. this.blocosDoConjunto[numBloco].setValidadeDoBloco(true);
  63. this.blocosDoConjunto[numBloco].setLimitesDoBloco(req.limiteInf, req.limiteSup);
  64. this.qntdDeBlocosOcupados++;
  65. }
  66. public Bloco getBloco(int numBloco){
  67. return this.blocosDoConjunto[numBloco];
  68. }
  69. public void setarBlocoAsInvalid(int num){
  70. this.blocosDoConjunto[num].setarComoInvalido();
  71. this.qntdDeBlocos--;
  72. }
  73. public void atualizaRR() {
  74. this.RR++;
  75. if (this.RR == 4) {
  76. this.RR = 0;
  77. }
  78. }
  79. public boolean hasBlocoDisponivel() {
  80. return this.qntdDeBlocos > this.qntdDeBlocosOcupados;
  81. }
  82. public Bloco[] getBlocos() {
  83. return this.blocosDoConjunto;
  84. }
  85. /*public int getBlocoIndexFromAddress(int a){
  86. }*/
  87. /**
  88. * Verifica se o bloco está nesse conjunto e atualiza o bit de sujeira do bloco se for um write
  89. *
  90. * @param req
  91. * @return
  92. */
  93. public boolean verificaHit(Requisicao req) {
  94. for (int i = 0; i < 4; i++) {
  95. //verifica o hit
  96. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  97. //atualiza a política LRU
  98. this.lru.atualizaLRU(i);
  99. if (!req.read) {//Se for uma escrita, atualiza para bloco sujo
  100. this.blocosDoConjunto[i].setSujeira(true);
  101. }
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public int getBlocoFromReq(Requisicao req) {
  108. for (int i = 0; i < 4; i++) {
  109. //verifica o hit
  110. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  111. return i;
  112. }
  113. }
  114. return -1;
  115. }
  116. }