1: <?php
2:
3: namespace Csim\Model;
4:
5: 6: 7:
8: class Cache {
9:
10: private $sets;
11: private $state;
12: private $nextSetToMap = 0;
13:
14: 15: 16:
17: public function __construct($layout) {
18: for ($i = 0; $i < $layout->getAssociativity(); $i++) {
19: $this->sets[$i] = new \Csim\Model\Set($layout);
20: }
21: $this->state = new \Csim\Model\SimulationState($layout);
22: }
23:
24: 25: 26:
27: public function getState() {
28: return $this->state;
29: }
30:
31: 32: 33: 34: 35: 36:
37: public function simulate($instruction) {
38: for ($setNo = 0; $setNo < \count($this->sets); $setNo++) {
39: $location = $this->searchSet($setNo, $instruction);
40: if ($this->wasHit($location)) {
41: $this->state->addExecution(new \Csim\Model\Execution($instruction, true,
42: new \Csim\Model\CacheCell($setNo,
43: $location->getIndex(),
44: $location->getOffset())));
45: return $this->state;
46: }
47: }
48: $this->mapSet($instruction);
49: return $this->state;
50: }
51:
52: private function searchSet($setNo, $instruction) {
53: return $this->sets[$setNo]->addressLocation($instruction);
54: }
55:
56: private function wasHit($location) {
57: return !\is_null($location);
58: }
59:
60: private function mapSet($instruction) {
61: $this->nextSetToMap = ++$this->nextSetToMap % \count($this->sets);
62: $index = $this->sets[$this->nextSetToMap]->loadAddress($instruction->getAddress());
63: $this->state->addExecution(new \Csim\Model\Execution($instruction, false,
64: new \Csim\Model\CacheCell($this->nextSetToMap,
65: $index, 0)));
66: }
67:
68: }
69: