Arm/cachehits

From ASMBits

Consider the following cache:

  • Direct-mapped (i.e., 1-way set-associative)
  • 2B block size
  • 2S sets

Your function will be provided B, S, an array of tags (one for each cache block), and a list of addresses. Write a function that will determine how many of those addresses would hit in the above cache. This problem counts the number of hits assuming the cache state does not change (i.e., it's the same problem as nios/cachehit, but run multiple times on a list of addresses. You may wish to reuse your solution from that problem).

Assume every cache block in the cache is valid. The array of tags contains exactly 2S words, one for each set. Each entry in the array is a 32-bit word, but only the tag bits are valid (the lower bits that correspond to the set number and offset are undefined).

The address list contains an array of 32-bit addresses, and is terminated with -1. Do not process the -1 as if it were an address.

Your function should return the number of addresses in the address list that would hit in the cache.

int cachehits(unsigned int B, unsigned int S, unsigned int *cachetags, unsigned int *addrlist);

Expected solution length: Around 40 lines.

Sample Input

[See below]

Sample Output

r0=1

Write your solution here

x
 
1
.data
2
// This direct-mapped cache has 4 sets/blocks. Their tags are 0, 0x100, 0xc0, and 0x40
3
// for set 0, 1, 2, and 3, respectively.
4
CacheTags:
5
.word 0
6
.word 0x100
7
.word 0xc0
8
.word 0x40
9
10
AddressList:
11
.word 0x110 // This is hit (set 1)
12
.word 0x120 // This is a miss (set 2)
13
.word -1
14
15
.text
16
.global _start
17
_start:
18
    ldr sp, =0x4000000  // This problem will probably use the stack
19
    ldr r0, =4  // Cache has 2^4 byte blocks
20
    ldr r1, =2  // Cache has 2^2 sets
21
    ldr r2, =CacheTags
22
    ldr r3, =AddressList
23
    bl cachehits
24
25
cachehits:
26
    
27
    
Upload a source file...