summaryrefslogtreecommitdiff
path: root/src/sim/if.cc
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-03-29 17:11:17 -0400
committerbd <bdunahu@operationnull.com>2025-03-29 17:11:17 -0400
commitd21a1a9caa1f1791343a5376121936e552b1124c (patch)
treedf28c01f6d01603e5408bc1b1b111a66adafbff7 /src/sim/if.cc
parentac0ae7206491a42cdba70560b0db41cfc8c7f642 (diff)
Fetch stage properly holds objects until parent is ready
Diffstat (limited to 'src/sim/if.cc')
-rw-r--r--src/sim/if.cc30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/sim/if.cc b/src/sim/if.cc
index 099ff1c..de044f8 100644
--- a/src/sim/if.cc
+++ b/src/sim/if.cc
@@ -6,17 +6,31 @@
IF::IF(Stage *stage) : Stage(stage) { this->id = FETCH; }
-Response IF::advance(InstrDTO &i, Response p)
+Response IF::advance(InstrDTO &next_instr, Response p)
+{
+ this->fetch_with_buffer();
+ if (this->status == OK && p == OK) {
+ // mutual consent
+ ++this->pc;
+ this->curr_instr->set_time_of(this->id, this->clock_cycle);
+ next_instr = *this->curr_instr;
+ curr_instr = nullptr;
+ }
+ return this->status;
+}
+
+void IF::fetch_with_buffer()
{
Response r;
signed int bits;
- r = this->storage->read_word(this->id, this->pc, bits);
- if (r == OK) {
- ++this->pc;
- i.set_time_of(this->id, this->clock_cycle);
- i.set_instr_bits(bits);
+ if (this->curr_instr == nullptr) {
+ r = this->storage->read_word(this->id, this->pc, bits);
+ if (r == OK) {
+ this->status = r;
+ this->curr_instr = std::make_unique<InstrDTO>();
+ this->curr_instr->set_instr_bits(bits);
+ } else
+ this->status = STALLED;
}
-
- return r;
}