Cache Simulator in C

You’ve got two problems. Firstly, Scott Wales is correct about your hex2bin() function – you have a 'x' where you mean '4'.

Secondly, you are not correctly counting a cache miss when you hit an invalid cache slot. You can simply handle “invalid” with exactly the same code path you use for a miss:

    if ((newCache[totalset].valid == 1) && (strcmp(newCache[totalset].tag, tbits) == 0))
    {
        /* Hit */
        if (readwrite == 'W')
        {
            cacheHit++;
            write++;
        }
        if (readwrite == 'R')
            cacheHit++;
    }
    else
    {
        /* Miss (cache entry invalid, or wrong tag) */
        if (readwrite == 'R')
        {
            cacheMiss++;
            read++;
        }
        if (readwrite == 'W')
        {
            cacheMiss++;
            read++;
            write++;
        }
        newCache[totalset].valid = 1;
        strcpy(newCache[totalset].tag, tbits);
    }

Leave a Comment