123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- * 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;
-
-
-
- public Conjunto() {
- this.RR = 0;
- this.lru = new LRU();
-
- this.blocosDoConjunto = new Bloco[4];
- for (int i = 0; i < 4; i++) {
- this.blocosDoConjunto[i] = new Bloco();
- }
- }
-
- public boolean hasBlocoLivre() {
- for (int i = 0; i < 4; i++) {
- if (!this.blocosDoConjunto[i].isValido()) {
- return true;
- }
- }
- return false;
- }
-
- public boolean usaRR(){
- for(int i = 0; i < 4; i++){
- if (this.blocosDoConjunto[i].isFirstWrite()) {
- return true;
- }
- }
- return false;
- }
-
- public boolean isBlocoOcupado(int num_bloco) {
- return this.blocosDoConjunto[num_bloco].isValido();
- }
-
-
- public boolean arrayHasTheValue(ArrayList<Integer> v, Integer num) {
- for (Integer integer : v) {
- if (integer == num) {
- return true;
- }
- }
- return false;
- }
- public int getRR(){
- return this.RR;
- }
-
-
- public LRU getLRU() {
- return this.lru;
- }
-
- public void desalocaBloco(int numBloco){
- this.blocosDoConjunto[numBloco].setValidadeDoBloco(false);
- this.blocosDoConjunto[numBloco].setSujeira(false);
- }
-
- 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);
- }
-
- public Bloco getBloco(int numBloco){
- return this.blocosDoConjunto[numBloco];
- }
-
- public void setarBlocoAsInvalid(int num){
- this.blocosDoConjunto[num].setarComoInvalido();
- }
-
- public void atualizaRR() {
- this.RR++;
- if (this.RR == 4) {
- this.RR = 0;
- }
- }
- 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;
- }
- }
|