/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cache; import java.util.ArrayList; /** * * @author Juninho Carlos */ public class Conjunto { private Bloco[] blocosDoConjunto; private int RR; public LRU lru; private int qntdDeBlocos; private int qntdDeBlocosOcupados; public Conjunto() { this.RR = 0; this.lru = new LRU(); this.qntdDeBlocos = 4; this.qntdDeBlocosOcupados = 0; this.blocosDoConjunto = new Bloco[4]; for (int i = 0; i < 4; i++) { this.blocosDoConjunto[i] = new Bloco(); } } public boolean isBlocoOcupado(int num_bloco) { return this.blocosDoConjunto[num_bloco].isValido(); } public boolean hasBlocoSemErro(){ return this.qntdDeBlocos > 0; } public boolean arrayHasTheValue(ArrayList v, Integer num) { for (Integer integer : v) { if (integer == num) { return true; } } return false; } public int getRR(){ for(int i = 0; i < 4; i++){ if(this.blocosDoConjunto[this.RR].isCachavel()){ return this.RR; }else{ this.atualizaRR(); } } return -1; } public LRU getLRU() { return this.lru; } public void desalocaBloco(int numBloco){ this.blocosDoConjunto[numBloco].setValidadeDoBloco(false); this.blocosDoConjunto[numBloco].setSujeira(false); this.qntdDeBlocosOcupados--; } public void alocarBloco(Requisicao req, int numBloco) { this.blocosDoConjunto[numBloco].setSujeira(!req.read); this.blocosDoConjunto[numBloco].setValidadeDoBloco(true); this.blocosDoConjunto[numBloco].setLimitesDoBloco(req.limiteInf, req.limiteSup); this.qntdDeBlocosOcupados++; } public Bloco getBloco(int numBloco){ return this.blocosDoConjunto[numBloco]; } public void setarBlocoAsInvalid(int num){ this.blocosDoConjunto[num].setarComoInvalido(); this.qntdDeBlocos--; } public void atualizaRR() { this.RR++; if (this.RR == 4) { this.RR = 0; } } public boolean hasBlocoDisponivel() { return this.qntdDeBlocos > this.qntdDeBlocosOcupados; } public Bloco[] getBlocos() { return this.blocosDoConjunto; } /*public int getBlocoIndexFromAddress(int a){ }*/ /** * Verifica se o bloco está nesse conjunto e atualiza o bit de sujeira do bloco se for um write * * @param req * @return */ public boolean verificaHit(Requisicao req) { for (int i = 0; i < 4; i++) { //verifica o hit if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) { //atualiza a política LRU this.lru.atualizaLRU(i); if (!req.read) {//Se for uma escrita, atualiza para bloco sujo this.blocosDoConjunto[i].setSujeira(true); } return true; } } return false; } public int getBlocoFromReq(Requisicao req) { for (int i = 0; i < 4; i++) { //verifica o hit if (this.blocosDoConjunto[i].isValido() && this.blocosDoConjunto[i].verificaBloco(req.endereco)) { return i; } } return -1; } }