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. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-------------------------------------------------- Exercise 1 -------------------------------------------------- 1: CACHE 1: REF - BIN - TAG - INDEX - CACHE 1 - 0b00000001 - 0b00000 - 0b001 - MISS 134 - 0b10000110 - 0b10000 - 0b110 - MISS 212 - 0b11010100 - 0b11010 - 0b100 - MISS 1 - 0b00000001 - 0b00000 - 0b001 - HIT 135 - 0b10000111 - 0b10000 - 0b111 - MISS 213 - 0b11010101 - 0b11010 - 0b101 - MISS 162 - 0b10100010 - 0b10100 - 0b010 - MISS 161 - 0b10100001 - 0b10100 - 0b001 - MISS 2 - 0b00000010 - 0b00000 - 0b010 - MISS 44 - 0b00101100 - 0b00101 - 0b100 - MISS 41 - 0b00101001 - 0b00101 - 0b001 - MISS 221 - 0b11011101 - 0b11011 - 0b101 - MISS HITS: 1 MISSRATE: 92 % CACHE 2: REF - BIN - TAG - INDEX - CACHE 1 - 0b00000001 - 0b00000 - 0b00 - MISS 134 - 0b10000110 - 0b10000 - 0b11 - MISS 212 - 0b11010100 - 0b11010 - 0b10 - MISS 1 - 0b00000001 - 0b00000 - 0b00 - HIT 135 - 0b10000111 - 0b10000 - 0b11 - HIT 213 - 0b11010101 - 0b11010 - 0b10 - HIT 162 - 0b10100010 - 0b10100 - 0b01 - MISS 161 - 0b10100001 - 0b10100 - 0b00 - MISS 2 - 0b00000010 - 0b00000 - 0b01 - MISS 44 - 0b00101100 - 0b00101 - 0b10 - MISS 41 - 0b00101001 - 0b00101 - 0b00 - MISS 221 - 0b11011101 - 0b11011 - 0b10 - MISS HITS: 3 MISSRATE: 75 % CACHE 3: REF - BIN - TAG - INDEX - CACHE 1 - 0b00000001 - 0b00000 - 0b0 - MISS 134 - 0b10000110 - 0b10000 - 0b1 - MISS 212 - 0b11010100 - 0b11010 - 0b1 - MISS 1 - 0b00000001 - 0b00000 - 0b0 - HIT 135 - 0b10000111 - 0b10000 - 0b1 - MISS 213 - 0b11010101 - 0b11010 - 0b1 - MISS 162 - 0b10100010 - 0b10100 - 0b0 - MISS 161 - 0b10100001 - 0b10100 - 0b0 - HIT 2 - 0b00000010 - 0b00000 - 0b0 - MISS 44 - 0b00101100 - 0b00101 - 0b1 - MISS 41 - 0b00101001 - 0b00101 - 0b0 - MISS 221 - 0b11011101 - 0b11011 - 0b1 - MISS HITS: 2 MISSRATE: 83 % 2: CACHE 1: AMAT: 24.92 cycles CACHE 2: AMAT: 21.75 cycles CACHE 3: AMAT: 25.83 cycles -------------------------------------------------- Exercise 2 --------------------------------------------------