struct line { char block[16]; unsigned tag; bool valid; } line cache[4]; bool isInCache(unsigned address) { // address 10987654321098765432109876[54][3210] // block offset // set index // the rest of the address unsigned setIndex = (address>>4)&3; unsigned blockOffset = address&0xf; unsigned tag = address >> 6; line inQuestion = cache[setIndex]; return tag == line.tag; } bool readByte(unsigned address) { // address 10987654321098765432109876[54][3210] // block offset // set index // the rest of the address unsigned setIndex = (address>>4)&3; unsigned blockOffset = address&0xf; unsigned tag = address >> 6; line inQuestion = cache[setIndex]; if (tag == inQuestion.tag && inQuestion.valid) { return inQuestion.block[blockOffset]; } else { char read[16] = lineFromMemory(address); cache[setIndex].tag = tag; cache[setIndex].block = read; // not valid C cache[setIndex].valid = 1; return inQuestion.block[blockOffset]; } }