-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.c
More file actions
43 lines (38 loc) · 1.27 KB
/
bits.c
File metadata and controls
43 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "bits.h"
#include "cache.h"
int get_set(Cache *cache, address_type address) {
// TODO:
// Extract the set bits from a 32-bit address.
//
int bits_of_set = cache -> set_bits;
int bits_of_block = cache -> block_bits;
int L_bit = bits_of_set + bits_of_block - 1;
int R_bit = bits_of_block;
int mask = ((1<< (L_bit - R_bit + 1)) -1) <<R_bit;
int needed_set =(unsigned) ((address & mask))>> R_bit;
return needed_set;
}
int get_line(Cache *cache, address_type address) {
// TODO:
// Extract the tag bits from a 32-bit address.
//
int bits_of_set = cache -> set_bits;
int bits_of_block = cache -> block_bits;
int bits_of_tag = 32 - bits_of_set - bits_of_block;
int L_bit = bits_of_tag + bits_of_set + bits_of_block - 1;
int R_bit = bits_of_set + bits_of_block;
int mask = ((1<<(L_bit - R_bit + 1)) -1) << R_bit;
int needed_tag = (unsigned) (address & mask)>> R_bit;
return needed_tag;
}
int get_byte(Cache *cache, address_type address) {
// TODO
// Extract the block offset (byte index) bits from a 32-bit address.
//
int bits_of_block = cache -> block_bits;
int L_bit = bits_of_block - 1;
int R_bit = 0;
int mask = ((1<<(L_bit - R_bit + 1)) -1) <<R_bit;
int needed_byte = (unsigned) (address & mask)>> R_bit;
return needed_byte;
}