Conjunto.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public Conjunto() {
  17. this.RR = 0;
  18. this.lru = new LRU();
  19. this.blocosDoConjunto = new Bloco[4];
  20. for (int i = 0; i < 4; i++) {
  21. this.blocosDoConjunto[i] = new Bloco();
  22. }
  23. }
  24. public boolean hasBlocoLivre() {
  25. for (int i = 0; i < 4; i++) {
  26. if (!this.blocosDoConjunto[i].isValido()) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. public boolean usaRR(){
  33. for(int i = 0; i < 4; i++){
  34. if (this.blocosDoConjunto[i].isFirstWrite()) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. public boolean isBlocoOcupado(int num_bloco) {
  41. return this.blocosDoConjunto[num_bloco].isValido();
  42. }
  43. public boolean arrayHasTheValue(ArrayList<Integer> v, Integer num) {
  44. for (Integer integer : v) {
  45. if (integer == num) {
  46. return true;
  47. }
  48. }
  49. return false;
  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 setarBlocoAsInvalid(int num){
  70. this.blocosDoConjunto[num].setarComoInvalido();
  71. }
  72. public void atualizaRR() {
  73. this.RR++;
  74. if (this.RR == 4) {
  75. this.RR = 0;
  76. }
  77. }
  78. public Bloco[] getBlocos() {
  79. return this.blocosDoConjunto;
  80. }
  81. /*public int getBlocoIndexFromAddress(int a){
  82. }*/
  83. /**
  84. * Verifica se o bloco está nesse conjunto e atualiza o bit de sujeira do bloco se for um write
  85. *
  86. * @param req
  87. * @return
  88. */
  89. public boolean verificaHit(Requisicao req) {
  90. for (int i = 0; i < 4; i++) {
  91. //verifica o hit
  92. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  93. //atualiza a política LRU
  94. this.lru.atualizaLRU(i);
  95. if (!req.read) {//Se for uma escrita, atualiza para bloco sujo
  96. this.blocosDoConjunto[i].setSujeira(true);
  97. }
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. public int getBlocoFromReq(Requisicao req) {
  104. for (int i = 0; i < 4; i++) {
  105. //verifica o hit
  106. if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) {
  107. return i;
  108. }
  109. }
  110. return -1;
  111. }
  112. }