From d21a1a9caa1f1791343a5376121936e552b1124c Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 29 Mar 2025 17:11:17 -0400 Subject: Fetch stage properly holds objects until parent is ready --- src/sim/if.cc | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/sim/if.cc') 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(); + this->curr_instr->set_instr_bits(bits); + } else + this->status = STALLED; } - - return r; } -- cgit v1.2.3