Last active
March 30, 2020 10:18
-
-
Save nicklasfrahm/6963ebe7455bd85f26830c426d34cc8d to your computer and use it in GitHub Desktop.
HSCOD - Exercise - Cache Design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const spacer = '--------------------------------------------------'; | |
const decToBin = (dec, paddingCount = 8) => | |
dec.toString(2).padStart(paddingCount, '0'); | |
console.log(`${spacer}\nExercise 1\n${spacer}`); | |
{ | |
const addressSize = 8; | |
const addressReferences = [3, 180, 43, 2, 191, 88, 190, 2, 88]; | |
const indexes = {}; | |
console.log('REF - BIN - TAG - INDEX - CACHE'); | |
addressReferences.forEach(ref => { | |
const decStr = ref.toString().padStart(3, ' '); | |
const refStr = decToBin(ref, addressSize); | |
const index = decToBin(ref % 16, 4); | |
const tag = decToBin((ref & ~16) >> 4, 4); | |
const hit = indexes[index] === tag ? 'HIT' : 'MISS'; | |
indexes[index] = tag; | |
console.log(`${decStr} - 0b${refStr} - 0b${tag} - 0b${index} - ${hit}`); | |
}); | |
} | |
console.log(`${spacer}\nExercise 2\n${spacer}`); | |
{ | |
const addressSize = 32; | |
const offsetLength = 5 - 0 + 1; | |
const indexLenth = 11 - 6 + 1; | |
const tagLength = 31 - 12 + 1; | |
const addressReferences = [ | |
0, | |
4, | |
16, | |
132, | |
232, | |
160, | |
1024, | |
30, | |
140, | |
3100, | |
180, | |
2180 | |
]; | |
const indexes = {}; | |
let hits = 0; | |
console.log(`ASSUMPTION: BLOCK OFFSET AND 8 BIT WORD SIZE`); | |
console.log(`A: CACHE LINE SIZE: ${2 ** offsetLength} WORDS`); | |
console.log(`B: CACHE BLOCK COUNT: ${2 ** indexLenth} BLOCKS`); | |
console.log( | |
'C: REF - BIN - TAG - INDEX - OFFSET - CACHE - START - END' | |
); | |
addressReferences.forEach(ref => { | |
const decStr = ref.toString().padStart(7, ' '); | |
const refStr = decToBin(ref, addressSize); | |
const offset = decToBin(ref % 64, offsetLength); | |
const index = decToBin((ref & (63 << 6)) >> 6, indexLenth); | |
const tag = decToBin((ref & (~63 << 6)) >> 6, tagLength); | |
const dataStart = ref & ~63; | |
const dataEnd = dataStart + 63; | |
const dataStartStr = dataStart.toString().padStart(5, ' '); | |
const dataEndStr = dataEnd.toString().padStart(5, ' '); | |
const hit = indexes[index] && indexes[index].tag === tag ? 'HIT ' : 'MISS'; | |
if (hit.trim() === 'HIT') { | |
++hits; | |
} | |
indexes[index] = { | |
tag, | |
offset, | |
dataStart, | |
dataEnd | |
}; | |
console.log( | |
`${decStr} - 0b${refStr} - 0b${tag} - 0b${index} - 0b${offset} - ${hit} - ${dataStartStr} - ${dataEndStr}` | |
); | |
}); | |
console.log(` TOTAL HITS: ${hits}`); | |
console.log( | |
` HITRATE: ${Math.round((hits / addressReferences.length) * 100)}%` | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result is: