1: <?php
2:
3: namespace Csim\Model;
4:
5: /**
6: * Holds sizes of cache parts.
7: */
8: class CacheLayout {
9:
10: const LOG_BASE = 2;
11: const ADDRESS_SIZE = 32;
12:
13: private $block_size;
14: private $block_count;
15: private $associativity;
16:
17: /**
18: * Constructs a new instance with the specified associativity, block count and block size.
19: */
20: public function __construct($associativity, $block_count, $block_size) {
21: $this->associativity = $associativity;
22: $this->block_count = $block_count;
23: $this->block_size = $block_size;
24: }
25:
26: /**
27: * Returns the number of bits needed to represent a block in a set.
28: *
29: * @return integer
30: */
31: public function getIndexBits() {
32: return \log($this->block_size, self::LOG_BASE);
33: }
34:
35: /**
36: * Returns the number of bits needed to represent an index in a block..
37: *
38: * @return integer
39: */
40: public function getOffsetBits() {
41: return \log($this->block_size, self::LOG_BASE);
42: }
43:
44: /**
45: * Returns the number of bits needed to represent the address of a set.
46: *
47: * @return integer
48: */
49: public function getTagBits() {
50: return self::ADDRESS_SIZE - $this->getIndexBits() - $this->getOffsetBits();
51: }
52:
53: /**
54: * Returns the number of sets in the cache.
55: *
56: * @return integer
57: */
58: public function getAssociativity() {
59: return $this->associativity;
60: }
61:
62: /**
63: * Returns the number of blocks in a set.
64: *
65: * @return integer
66: */
67: public function getBlockCount() {
68: return $this->block_count;
69: }
70:
71: /**
72: * Returns the number of words in a block..
73: *
74: * @return integer
75: */
76: public function getBlockSize() {
77: return $this->block_size;
78: }
79:
80: /**
81: * Returns an integer with all bits in the tag part of an address set to one and all
82: * other bits set to zero.
83: *
84: * @return integer
85: */
86: public function getTagBitMask() {
87: return $this->createBitMask($this->getTagBits(), $this->getIndexBits() + $this->getOffsetBits());
88: }
89:
90: /**
91: * Returns an integer with all bits in the block index part of an address set to one and all
92: * other bits set to zero.
93: *
94: * @return integer
95: */
96: public function getIndexBitMask() {
97: return $this->createBitMask($this->getIndexBits(), $this->getOffsetBits());
98: }
99:
100: /**
101: * Returns an integer with all bits in the block offset part of an address set to one and all
102: * other bits set to zero.
103: *
104: * @return integer
105: */
106: public function getOffsetBitMask() {
107: return $this->createBitMask($this->getOffsetBits(), 0);
108: }
109:
110: private function createBitMask($noOfOnes, $noOfTrailingZeros) {
111: $mask = 0;
112: for ($i = 0; $i < $noOfOnes - 1; $i++) {
113: $mask = $mask | 1;
114: $mask = $mask << 1;
115: }
116: $mask = $mask | 1;
117: $mask = $mask << $noOfTrailingZeros;
118: return $mask;
119: }
120:
121: }
122: