Skip to content

Instantly share code, notes, and snippets.

@methane
Created March 21, 2012 04:26
Show Gist options
  • Select an option

  • Save methane/2144346 to your computer and use it in GitHub Desktop.

Select an option

Save methane/2144346 to your computer and use it in GitHub Desktop.
Describing n-way cache.
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