1: <?php
2:
3: namespace Csim\Model;
4:
5: /**
6: * Holds the current state of a simulation. Also keeps hit rate and a list of all
7: * executed instructions..
8: */
9: class SimulationState {
10:
11: private $layout;
12: private $history;
13: private $newExecutionAdded = false;
14:
15: /**
16: * Constructs a new instance representing a simulation of a cache with the specified layout.
17: */
18: public function __construct($layout) {
19: $this->layout = $layout;
20: }
21:
22: /**
23: * Returns the layout of the cache used in this simulation.
24: *
25: * @return \Csim\Model\CacheLayout
26: */
27: public function getLayout() {
28: return $this->layout;
29: }
30:
31: /**
32: * Adds the specified simulated instruction and its result to the execution history.
33: *
34: * @param \Csim\Model\Execution $execution The executed instruction.
35: */
36: public function addExecution(\Csim\Model\Execution $execution) {
37: $this->history[] = $execution;
38: $this->newExecutionAdded = true;
39: }
40:
41: /**
42: * Returns true if a new execution has been added since last time this method was called.
43: *
44: * @return boolean
45: */
46: public function hasNewExecution() {
47: $result = $this->newExecutionAdded;
48: $this->newExecutionAdded = false;
49: return $result;
50: }
51:
52: private function lastExecutionsIndex() {
53: return \count($this->history) - 1;
54: }
55:
56: private function lastExecution() {
57: return $this->history[$this->lastExecutionsIndex()];
58: }
59:
60: /**
61: * Returns true if last simulated instruction hit in the cache, false if not.
62: * @return boolean
63: */
64: public function lastWasHit() {
65: return $this->lastExecution()->isHit();
66: }
67:
68: /**
69: * Returns the location where the last instruction wrote/read.
70: *
71: * @return \Csim\Model\CacheLocation
72: */
73: public function getLastAccessLocation() {
74: return $this->lastExecution()->getLocation();
75: }
76:
77: /**
78: * Returns the tag that was mapped by the last write instruction.
79: *
80: * @return integer
81: */
82: public function getLastMappedTag() {
83: return $this->lastExecution()->getInstruction()->getAddress() & $this->layout->getTagBitMask();
84: }
85:
86: }
87: