Created
March 21, 2012 04:26
-
-
Save methane/2144346 to your computer and use it in GitHub Desktop.
Describing n-way cache.
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
| NWAY = 4 | |
| LINESIZE_BITS = 4 | |
| LINESIZE = 2 ** LINESIZE_BITS # 64 | |
| CACHESIZE = 4*1024 | |
| NUMLINES = CACHESIZE / LINESIZE | |
| LINES = [(None, None)] * NUMLINES | |
| NUM_SEGMENTS = NUMLINES / NWAY | |
| def cache_lookup(address): | |
| line_address = address >> LINESIZE_BITS | |
| segment = (line_address & 0xff) * NWAY | |
| segment_address = line_address >> 3 | |
| for i in xrange(8): | |
| if LINES[segment+i][0] == segment_address: | |
| return LINES[segment+i][1] | |
| return None | |
| import random | |
| def cache_fill(address, value): | |
| line_address = address >> LINESIZE_BITS | |
| segment = (line_address & 0xff) * NWAY | |
| segment_address = line_address >> 3 | |
| offs = random.randrange(0, 8) | |
| LINES[segment+offs] = (segment_address, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment