Cache Simulator in Java. Implementing Dirty Bit

Question
class DirectMappedCache extends Cache {

    private int logLineSize;    //log_2 of the number of bytes per cache line
    private int logNumLines;
    private TreeMap<String, CacheSlot> tmap; 
    private int HitCounter; 
    private int MissCounter; 

public DirectMappedCache(int logLineSize, int logNumLines) {

    //constructor that takes lengths of different fields
    this.logLineSize = logLineSize;
    this.logNumLines = logNumLines;
    this.HitCounter = 0; 
    this.MissCounter = 0; 
     tmap = new TreeMap<String, CacheSlot>(); 
    }


public boolean read(String addr) {

    System.out.println("Read from address " + addr + ": ");
    String tag = addr.substring(0,9); 
    String slotnumber = addr.substring(9,13);
    String offset = addr.substring(13,16); 


    if(tmap.containsKey(slotnumber)){
        CacheSlot temp = tmap.get(slotnumber); 
        if(temp.valid){
            if(tag.equals(temp.tag)){ 
                HitCounter++; 
                return true; 


            }

        }

    } 
        MissCounter++; 
        CacheSlot put = new CacheSlot(); 
        put.valid = true; 
        put.tag = tag; 
        tmap.put(slotnumber, put); 



    return false;
    }

 public boolean write(String addr) {

    System.out.print("Write to address  " + addr + ": ");
    String tag = addr.substring(0,9); 
    String slotnumber = addr.substring(9,13);
    String offset = addr.substring(13,16); 


    if(tmap.containsKey(slotnumber)){
        CacheSlot temp = tmap.get(slotnumber); 
        if(temp.valid){
            if(tag.equals(temp.tag)){ 
                HitCounter++; 
                return true; 

            }

        }

    } 
        MissCounter++; 
        CacheSlot put = new CacheSlot(); 
        put.valid = true; 
        put.tag = tag; 
        tmap.put(slotnumber, put); 

    return false;
    }

 public int numDirty(){

        for(String key: tmap.keySet()){ 
            CacheSlot c = tmap.get(key); 
            if(c.valid){ 
                dirtybit++; 
           }

class CacheSlot{ 

    boolean valid = false; 
    String tag = "";
    int dirtybit = 0; 




           }

        }
    }

So I need to count the dirty bit in the cache. I use a loop to iterate through the Tree Map but I can't tell it how to count it.


Show source
| java   | caching   | return   | return-value   2016-10-25 20:10 0 Answers

Answers ( 0 )

◀ Go back