Created
March 30, 2020 10:19
-
-
Save nicklasfrahm/7f241ecf6ffc56d10825814035ef5208 to your computer and use it in GitHub Desktop.
HSCOD - Exercise - Cache Design
This file contains hidden or 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'); | |
const sliceBits = (dec, start, end) => { | |
const positiveMask = 2 ** (end + 1) - 1; | |
const negativeMask = 2 ** start - 1; | |
return (dec & (positiveMask - negativeMask)) >> start; | |
}; | |
const round = (num, digits = 2) => | |
+(Math.round(num + `e+${digits}`) + `e-${digits}`); | |
console.log(`${spacer}\nExercise 1\n${spacer}`); | |
{ | |
const addressSize = 8; | |
const addressReferences = [ | |
1, | |
134, | |
212, | |
1, | |
135, | |
213, | |
162, | |
161, | |
2, | |
44, | |
41, | |
221 | |
]; | |
const hits = [0, 0, 0]; | |
const amats = [0, 0, 0]; | |
const missRates = [0, 0, 0]; | |
const missRate = hitCount => 1 - hitCount / addressReferences.length; | |
const amat = (accessTime, missRate, missPenalty) => | |
accessTime + missRate * missPenalty; | |
console.log('1: CACHE 1: REF - BIN - TAG - INDEX - CACHE'); | |
{ | |
const indexes = {}; | |
addressReferences.forEach(ref => { | |
const decStr = ref.toString().padStart(15, ' '); | |
const refStr = decToBin(ref, addressSize); | |
const index = decToBin(sliceBits(ref, 0, 2), 3); | |
const tag = decToBin(sliceBits(ref, 3, 7), 5); | |
const hit = indexes[index] === tag ? 'HIT' : 'MISS'; | |
if (hit.trim() == 'HIT') { | |
++hits[0]; | |
} | |
indexes[index] = tag; | |
console.log(`${decStr} - 0b${refStr} - 0b${tag} - 0b${index} - ${hit}`); | |
}); | |
console.log(` HITS: ${hits[0]}`); | |
missRates[0] = missRate(hits[0]); | |
console.log(` MISSRATE: ${round(missRates[0] * 100, 0)} %`); | |
} | |
console.log(' CACHE 2: REF - BIN - TAG - INDEX - CACHE'); | |
{ | |
const indexes = {}; | |
addressReferences.forEach(ref => { | |
const decStr = ref.toString().padStart(15, ' '); | |
const refStr = decToBin(ref, addressSize); | |
const index = decToBin(sliceBits(ref, 1, 2), 2); | |
const tag = decToBin(sliceBits(ref, 3, 7), 5); | |
const hit = indexes[index] === tag ? 'HIT' : 'MISS'; | |
if (hit.trim() == 'HIT') { | |
++hits[1]; | |
} | |
indexes[index] = tag; | |
console.log(`${decStr} - 0b${refStr} - 0b${tag} - 0b${index} - ${hit}`); | |
}); | |
console.log(` HITS: ${hits[1]}`); | |
missRates[1] = missRate(hits[1]); | |
console.log(` MISSRATE: ${round(missRates[1] * 100, 0)} %`); | |
} | |
console.log(' CACHE 3: REF - BIN - TAG - INDEX - CACHE'); | |
{ | |
const indexes = {}; | |
addressReferences.forEach(ref => { | |
const decStr = ref.toString().padStart(15, ' '); | |
const refStr = decToBin(ref, addressSize); | |
const index = decToBin(sliceBits(ref, 2, 2), 1); | |
const tag = decToBin(sliceBits(ref, 3, 7), 5); | |
const hit = indexes[index] === tag ? 'HIT' : 'MISS'; | |
if (hit.trim() == 'HIT') { | |
++hits[2]; | |
} | |
indexes[index] = tag; | |
console.log( | |
`${decStr} - 0b${refStr} - 0b${tag} - 0b${index} - ${hit}` | |
); | |
}); | |
console.log(` HITS: ${hits[2]}`); | |
missRates[2] = missRate(hits[2]); | |
console.log(` MISSRATE: ${round(missRates[2] * 100, 0)} %`); | |
} | |
amats[0] = amat(2, missRates[0], 25); | |
console.log(`2: CACHE 1: AMAT: ${round(amats[0], 2)} cycles`); | |
amats[1] = amat(3, missRates[1], 25); | |
console.log(` CACHE 2: AMAT: ${round(amats[1], 2)} cycles`); | |
amats[2] = amat(5, missRates[2], 25); | |
console.log(` CACHE 3: AMAT: ${round(amats[2], 2)} cycles`); | |
} | |
console.log(`${spacer}\nExercise 2\n${spacer}`); | |
{ | |
// ToDo. | |
} |
Author
nicklasfrahm
commented
Mar 30, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment