From eaa87e9fcd90c00d6261cbdb854efb7a09467f1d Mon Sep 17 00:00:00 2001 From: bd Date: Thu, 27 Mar 2025 20:30:18 -0400 Subject: Instr, InstrDTO gets/sets, other structures required for decode --- src/sim/if.cc | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/sim/if.cc') diff --git a/src/sim/if.cc b/src/sim/if.cc index deed8e1..f5a2bb9 100644 --- a/src/sim/if.cc +++ b/src/sim/if.cc @@ -2,6 +2,9 @@ #include "accessor.h" #include "instrDTO.h" #include "response.h" +#include "stage.h" + +IF::IF(Stage *stage) : Stage(stage) { this->id = FETCH; } Response IF::advance(InstrDTO &i) { -- cgit v1.2.3 From 043c2566c112f0d913c52aa80c9fcc4da5fb11ae Mon Sep 17 00:00:00 2001 From: bd Date: Thu, 27 Mar 2025 20:47:44 -0400 Subject: Use an unordered map to record pipe stage history on instructions --- inc/instrDTO.h | 22 ++++++++-------------- src/sim/if.cc | 2 +- src/sim/instrDTO.cc | 11 +++-------- tests/if.cc | 6 +++--- 4 files changed, 15 insertions(+), 26 deletions(-) (limited to 'src/sim/if.cc') diff --git a/inc/instrDTO.h b/inc/instrDTO.h index 7f2c688..2a6ab1f 100644 --- a/inc/instrDTO.h +++ b/inc/instrDTO.h @@ -2,6 +2,8 @@ #define INSTRDTO_H #include #include +#include +#include "accessor.h" class InstrDTO { @@ -13,9 +15,9 @@ class InstrDTO ~InstrDTO() = default; /** - * @return if_cycle + * @return hist entry for Accessor */ - int get_if_cycle(); + int get_time_of(Accessor); /** * @return id_cycle */ @@ -42,13 +44,9 @@ class InstrDTO std::string get_oper_name(); /** - * @param if_cycle + * @param set hist key */ - void set_if_cycle(int); - /** - * @param id_cycle - */ - void set_id_cycle(int); + void set_time_of(Accessor, int); /** * @param instr_bits */ @@ -68,13 +66,9 @@ class InstrDTO private: /** - * The clock cycle this instruction finished being fetched. - */ - int if_cycle; - /** - * The clock cycle this instruction finished being identified. + * The clock cycle each stage finished an operation. */ - int id_cycle; + std::unordered_map hist; /** * The raw bits encoding the instruction. */ diff --git a/src/sim/if.cc b/src/sim/if.cc index f5a2bb9..559ad2e 100644 --- a/src/sim/if.cc +++ b/src/sim/if.cc @@ -14,7 +14,7 @@ Response IF::advance(InstrDTO &i) r = this->storage->read_word(this->id, this->pc, bits); if (r == OK) { ++this->pc; - i.set_if_cycle(this->clock_cycle); + i.set_time_of(this->id, this->clock_cycle); i.set_instr_bits(bits); } diff --git a/src/sim/instrDTO.cc b/src/sim/instrDTO.cc index 4e24d4e..7864eb4 100644 --- a/src/sim/instrDTO.cc +++ b/src/sim/instrDTO.cc @@ -1,18 +1,15 @@ #include "instrDTO.h" +#include "accessor.h" InstrDTO::InstrDTO() { - this->if_cycle = 0; - this->id_cycle = 0; this->instr_bits = 0; this->s1 = 0; this->s2 = 0; this->s3 = 0; } -int InstrDTO::get_if_cycle() { return this->if_cycle; } - -int InstrDTO::get_id_cycle() { return this->id_cycle; } +int InstrDTO::get_time_of(Accessor a) { return this->hist[a]; } signed int InstrDTO::get_instr_bits() { return this->instr_bits; } @@ -22,9 +19,7 @@ signed int InstrDTO::get_s2() { return this->s2; } signed int InstrDTO::get_s3() { return this->s3; } -void InstrDTO::set_if_cycle(int cycle) { this->if_cycle = cycle; } - -void InstrDTO::set_id_cycle(int cycle) { this->id_cycle = cycle; } +void InstrDTO::set_time_of(Accessor a, int i) { this->hist[a] = i; } void InstrDTO::set_instr_bits(signed int instr) { this->instr_bits = instr; } diff --git a/tests/if.cc b/tests/if.cc index 5c1b645..3e68ac4 100644 --- a/tests/if.cc +++ b/tests/if.cc @@ -77,7 +77,7 @@ TEST_CASE_METHOD(IFPipeFixture, "fetch returns single instuction", "[if_pipe]") expected_cycles = this->m_delay + this->c_delay + 2; this->fetch_through(instr); - CHECK(instr.get_if_cycle() == expected_cycles); + CHECK(instr.get_time_of(FETCH) == expected_cycles); REQUIRE(instr.get_instr_bits() == this->p[0]); } @@ -89,12 +89,12 @@ TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]") expected_cycles = this->m_delay + this->c_delay + 2; this->fetch_through(instr); - CHECK(instr.get_if_cycle() == expected_cycles); + CHECK(instr.get_time_of(FETCH) == expected_cycles); REQUIRE(instr.get_instr_bits() == this->p[0]); expected_cycles += this->c_delay + 1; this->fetch_cache(instr); - CHECK(instr.get_if_cycle() == expected_cycles); + CHECK(instr.get_time_of(FETCH) == expected_cycles); REQUIRE(instr.get_instr_bits() == this->p[1]); } -- cgit v1.2.3 From ac0ae7206491a42cdba70560b0db41cfc8c7f642 Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 29 Mar 2025 12:58:14 -0400 Subject: Add parameter to Stage::advance so status can transfer down the pipe --- inc/controller.h | 2 +- inc/ex.h | 2 +- inc/id.h | 2 +- inc/if.h | 2 +- inc/mm.h | 2 +- inc/stage.h | 6 ++++-- inc/wb.h | 2 +- src/sim/controller.cc | 6 +++--- src/sim/ex.cc | 2 +- src/sim/id.cc | 2 +- src/sim/if.cc | 2 +- src/sim/mm.cc | 2 +- src/sim/wb.cc | 2 +- tests/if.cc | 6 +++--- 14 files changed, 21 insertions(+), 19 deletions(-) (limited to 'src/sim/if.cc') diff --git a/inc/controller.h b/inc/controller.h index 4b102ce..ac83a78 100644 --- a/inc/controller.h +++ b/inc/controller.h @@ -36,7 +36,7 @@ class Controller : public Stage * @return the pc. */ int get_pc(); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; }; #endif /* CONTROLLER_H_INCLUDED */ diff --git a/inc/ex.h b/inc/ex.h index fef411a..c7b7663 100644 --- a/inc/ex.h +++ b/inc/ex.h @@ -14,7 +14,7 @@ class EX : public Stage */ EX(Stage *next); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; }; #endif /* EX_H_INCLUDED */ diff --git a/inc/id.h b/inc/id.h index 2411402..95dde37 100644 --- a/inc/id.h +++ b/inc/id.h @@ -15,7 +15,7 @@ class ID : public Stage */ ID(Stage *next); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; /** * Parse an instruction into a type, opcode, and fields. If the type is diff --git a/inc/if.h b/inc/if.h index 7906c22..a6869f7 100644 --- a/inc/if.h +++ b/inc/if.h @@ -15,7 +15,7 @@ class IF : public Stage */ IF(Stage *next); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; }; #endif /* IF_H_INCLUDED */ diff --git a/inc/mm.h b/inc/mm.h index 90ebe5a..edee589 100644 --- a/inc/mm.h +++ b/inc/mm.h @@ -14,7 +14,7 @@ class MM : public Stage */ MM(Stage *next); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; }; #endif /* MM_H_INCLUDED */ diff --git a/inc/stage.h b/inc/stage.h index 0348263..761b9f6 100644 --- a/inc/stage.h +++ b/inc/stage.h @@ -22,10 +22,12 @@ class Stage * Advances this stage by a single clock cycle. * @param a DTO object containing various information about an instruction * moving through the pipeline. - * @return a response, indicating whether this pipeline stage is stalled, + * @param a response, indicating whether or not the parent pipe stage is + * busy. + * @return a response, indicating whether this pipeline stage is stalling, * busy, or done. */ - virtual Response advance(InstrDTO &i) = 0; + virtual Response advance(InstrDTO &i, Response p) = 0; protected: /** diff --git a/inc/wb.h b/inc/wb.h index b8f5d8f..c1a6a32 100644 --- a/inc/wb.h +++ b/inc/wb.h @@ -14,7 +14,7 @@ class WB : public Stage */ WB(Stage *next); - Response advance(InstrDTO &i) override; + Response advance(InstrDTO &i, Response p) override; }; #endif /* WB_H_INCLUDED */ diff --git a/src/sim/controller.cc b/src/sim/controller.cc index 45aa9a0..833d900 100644 --- a/src/sim/controller.cc +++ b/src/sim/controller.cc @@ -19,7 +19,7 @@ void Controller::run_for(int number) InstrDTO instr; int i; for (i = 0; i < number; ++i) { - this->advance(instr); + this->advance(instr, OK); } } @@ -29,11 +29,11 @@ std::array Controller::get_gprs() { return this->gprs; } int Controller::get_pc() { return this->pc; } -Response Controller::advance(InstrDTO &i) +Response Controller::advance(InstrDTO &i, Response p) { Response r; - r = this->next->advance(i); + r = this->next->advance(i, p); ++this->clock_cycle; return r; } diff --git a/src/sim/ex.cc b/src/sim/ex.cc index 46f5417..c9c2116 100644 --- a/src/sim/ex.cc +++ b/src/sim/ex.cc @@ -6,4 +6,4 @@ EX::EX(Stage *stage) : Stage(stage) { this->id = EXEC; } -Response EX::advance(InstrDTO &i) { return OK; } +Response EX::advance(InstrDTO &i, Response p) { return OK; } diff --git a/src/sim/id.cc b/src/sim/id.cc index e9c48df..70fab9a 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -8,7 +8,7 @@ ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } -Response ID::advance(InstrDTO &i) +Response ID::advance(InstrDTO &i, Response p) { Response r; signed int s1, s2, s3; diff --git a/src/sim/if.cc b/src/sim/if.cc index 559ad2e..099ff1c 100644 --- a/src/sim/if.cc +++ b/src/sim/if.cc @@ -6,7 +6,7 @@ IF::IF(Stage *stage) : Stage(stage) { this->id = FETCH; } -Response IF::advance(InstrDTO &i) +Response IF::advance(InstrDTO &i, Response p) { Response r; signed int bits; diff --git a/src/sim/mm.cc b/src/sim/mm.cc index c5357f9..93c5b87 100644 --- a/src/sim/mm.cc +++ b/src/sim/mm.cc @@ -6,7 +6,7 @@ MM::MM(Stage *stage) : Stage(stage) { this->id = MEM; } -Response MM::advance(InstrDTO &i) +Response MM::advance(InstrDTO &i, Response p) { return OK; } diff --git a/src/sim/wb.cc b/src/sim/wb.cc index 218ed9a..13ab66a 100644 --- a/src/sim/wb.cc +++ b/src/sim/wb.cc @@ -6,4 +6,4 @@ WB::WB(Stage *stage) : Stage(stage) { this->id = WRITE; } -Response WB::advance(InstrDTO &i) { return OK; } +Response WB::advance(InstrDTO &i, Response p) { return OK; } diff --git a/tests/if.cc b/tests/if.cc index 3e68ac4..3be3305 100644 --- a/tests/if.cc +++ b/tests/if.cc @@ -36,7 +36,7 @@ class IFPipeFixture Response r; for (i = 0; i < this->m_delay + 1; ++i) { - r = this->ct->advance(instr); + r = this->ct->advance(instr, OK); // check response CHECK(r == BLOCKED); } @@ -52,11 +52,11 @@ class IFPipeFixture Response r; for (i = 0; i < this->c_delay; ++i) { - r = this->ct->advance(instr); + r = this->ct->advance(instr, OK); // check response CHECK(r == WAIT); } - r = this->ct->advance(instr); + r = this->ct->advance(instr, OK); // check response CHECK(r == OK); } -- cgit v1.2.3 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 --- inc/ex.h | 2 +- inc/id.h | 2 +- inc/if.h | 12 +++++++++++- inc/mm.h | 2 +- inc/stage.h | 26 +++++++++++++++++--------- inc/wb.h | 2 +- src/sim/controller.cc | 4 ++-- src/sim/ex.cc | 2 +- src/sim/id.cc | 11 ++++++----- src/sim/if.cc | 30 ++++++++++++++++++++++-------- src/sim/instrDTO.cc | 1 + src/sim/mm.cc | 2 +- src/sim/stage.cc | 11 +++++++++-- src/sim/wb.cc | 2 +- tests/if.cc | 34 ++++++++++++++++++++++++++++++++-- 15 files changed, 107 insertions(+), 36 deletions(-) (limited to 'src/sim/if.cc') diff --git a/inc/ex.h b/inc/ex.h index c7b7663..e67980d 100644 --- a/inc/ex.h +++ b/inc/ex.h @@ -14,7 +14,7 @@ class EX : public Stage */ EX(Stage *next); - Response advance(InstrDTO &i, Response p) override; + Response advance(InstrDTO &next_instr, Response p) override; }; #endif /* EX_H_INCLUDED */ diff --git a/inc/id.h b/inc/id.h index 95dde37..34e1a47 100644 --- a/inc/id.h +++ b/inc/id.h @@ -15,7 +15,7 @@ class ID : public Stage */ ID(Stage *next); - Response advance(InstrDTO &i, Response p) override; + Response advance(InstrDTO &next_instr, Response p) override; /** * Parse an instruction into a type, opcode, and fields. If the type is diff --git a/inc/if.h b/inc/if.h index a6869f7..9f84e51 100644 --- a/inc/if.h +++ b/inc/if.h @@ -15,7 +15,17 @@ class IF : public Stage */ IF(Stage *next); - Response advance(InstrDTO &i, Response p) override; + Response advance(InstrDTO &next_instr, Response p) override; + + private: + /** + * Performs a fetch only if a current fetch is not pending. Pending means + * that a fetch has completed successfully, but the caller stage in the + * pipeline is not ready to receive it. In this case, `curr_instr` is not + * the nullptr. + * @return STALLED if we are waiting on the storage devices, OK otherwise. + */ + void fetch_with_buffer(); }; #endif /* IF_H_INCLUDED */ diff --git a/inc/mm.h b/inc/mm.h index edee589..0d2ce82 100644 --- a/inc/mm.h +++ b/inc/mm.h @@ -14,7 +14,7 @@ class MM : public Stage */ MM(Stage *next); - Response advance(InstrDTO &i, Response p) override; + Response advance(InstrDTO &next_instr, Response p) override; }; #endif /* MM_H_INCLUDED */ diff --git a/inc/stage.h b/inc/stage.h index 761b9f6..ff4455b 100644 --- a/inc/stage.h +++ b/inc/stage.h @@ -7,6 +7,7 @@ #include "storage.h" #include #include +#include class Stage { @@ -17,17 +18,16 @@ class Stage * @return A newly allocated stage object. */ Stage(Stage *next); - virtual ~Stage() = default; + virtual ~Stage(); /** * Advances this stage by a single clock cycle. - * @param a DTO object containing various information about an instruction - * moving through the pipeline. + * @param a DTO object containing the next instruction to be processed. * @param a response, indicating whether or not the parent pipe stage is - * busy. + * ready to accept a new instruction object next cycle. * @return a response, indicating whether this pipeline stage is stalling, * busy, or done. */ - virtual Response advance(InstrDTO &i, Response p) = 0; + virtual Response advance(InstrDTO &next_instr, Response p) = 0; protected: /** @@ -56,10 +56,6 @@ class Stage * The address of the currently executing instruction. */ static unsigned int pc; - /** - * A pointer to the next stage in the pipeline. - */ - Stage *next; /** * A pointer to the top-level storage device. */ @@ -72,6 +68,18 @@ class Stage * The current clock cycle. */ static int clock_cycle; + /** + * A pointer to the next stage in the pipeline. + */ + Stage *next; + /** + * A pointer to the current instruction this stage is processing. + */ + std::unique_ptr curr_instr; + /** + * The current status of this stage. + */ + Response status; private: /** diff --git a/inc/wb.h b/inc/wb.h index c1a6a32..9a708a6 100644 --- a/inc/wb.h +++ b/inc/wb.h @@ -14,7 +14,7 @@ class WB : public Stage */ WB(Stage *next); - Response advance(InstrDTO &i, Response p) override; + Response advance(InstrDTO &next_instr, Response p) override; }; #endif /* WB_H_INCLUDED */ diff --git a/src/sim/controller.cc b/src/sim/controller.cc index 833d900..17937eb 100644 --- a/src/sim/controller.cc +++ b/src/sim/controller.cc @@ -29,11 +29,11 @@ std::array Controller::get_gprs() { return this->gprs; } int Controller::get_pc() { return this->pc; } -Response Controller::advance(InstrDTO &i, Response p) +Response Controller::advance(InstrDTO &next_instr, Response p) { Response r; - r = this->next->advance(i, p); + r = this->next->advance(next_instr, p); ++this->clock_cycle; return r; } diff --git a/src/sim/ex.cc b/src/sim/ex.cc index c9c2116..5b561f8 100644 --- a/src/sim/ex.cc +++ b/src/sim/ex.cc @@ -6,4 +6,4 @@ EX::EX(Stage *stage) : Stage(stage) { this->id = EXEC; } -Response EX::advance(InstrDTO &i, Response p) { return OK; } +Response EX::advance(InstrDTO &next_instr, Response p) { return OK; } diff --git a/src/sim/id.cc b/src/sim/id.cc index 70fab9a..83a8751 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -8,15 +8,16 @@ ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } -Response ID::advance(InstrDTO &i, Response p) +Response ID::advance(InstrDTO &next_instr, Response p) { Response r; - signed int s1, s2, s3; - Mnemonic m; + r = OK; + // signed int s1, s2, s3; + // Mnemonic m; - s1 = i.get_instr_bits(); + // s1 = next_instr.get_instr_bits(); - get_instr_fields(s1, s2, s3, m); + // get_instr_fields(s1, s2, s3, m); return r; } 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; } diff --git a/src/sim/instrDTO.cc b/src/sim/instrDTO.cc index 7418033..5a7fe3b 100644 --- a/src/sim/instrDTO.cc +++ b/src/sim/instrDTO.cc @@ -7,6 +7,7 @@ InstrDTO::InstrDTO() this->s1 = 0; this->s2 = 0; this->s3 = 0; + this->mnemonic = NOP; } int InstrDTO::get_time_of(Accessor a) { return this->hist[a]; } diff --git a/src/sim/mm.cc b/src/sim/mm.cc index 93c5b87..f394420 100644 --- a/src/sim/mm.cc +++ b/src/sim/mm.cc @@ -6,7 +6,7 @@ MM::MM(Stage *stage) : Stage(stage) { this->id = MEM; } -Response MM::advance(InstrDTO &i, Response p) +Response MM::advance(InstrDTO &next_instr, Response p) { return OK; } diff --git a/src/sim/stage.cc b/src/sim/stage.cc index 8d9dfc0..48ee494 100644 --- a/src/sim/stage.cc +++ b/src/sim/stage.cc @@ -5,7 +5,14 @@ static Logger *global_log = Logger::getInstance(); -Stage::Stage(Stage *next) { this->next = next; } +Stage::Stage(Stage *next) +{ + this->next = next; + this->curr_instr = nullptr; + this->status = OK; +} + +Stage::~Stage() { delete this->next; }; std::array Stage::gprs; std::array Stage::vrs; @@ -19,7 +26,7 @@ Response Stage::check_out(unsigned int &v) { Response r; if (this->is_checked_out(v)) - r = STALLED; + r = BLOCKED; else { r = OK; v = this->check_out_register(v); diff --git a/src/sim/wb.cc b/src/sim/wb.cc index 13ab66a..bdea65a 100644 --- a/src/sim/wb.cc +++ b/src/sim/wb.cc @@ -6,4 +6,4 @@ WB::WB(Stage *stage) : Stage(stage) { this->id = WRITE; } -Response WB::advance(InstrDTO &i, Response p) { return OK; } +Response WB::advance(InstrDTO &next_instr, Response p) { return OK; } diff --git a/tests/if.cc b/tests/if.cc index 3be3305..bb25afa 100644 --- a/tests/if.cc +++ b/tests/if.cc @@ -38,7 +38,7 @@ class IFPipeFixture for (i = 0; i < this->m_delay + 1; ++i) { r = this->ct->advance(instr, OK); // check response - CHECK(r == BLOCKED); + CHECK(r == STALLED); } this->fetch_cache(instr); } @@ -54,7 +54,7 @@ class IFPipeFixture for (i = 0; i < this->c_delay; ++i) { r = this->ct->advance(instr, OK); // check response - CHECK(r == WAIT); + CHECK(r == STALLED); } r = this->ct->advance(instr, OK); // check response @@ -98,3 +98,33 @@ TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]") CHECK(instr.get_time_of(FETCH) == expected_cycles); REQUIRE(instr.get_instr_bits() == this->p[1]); } + +TEST_CASE_METHOD(IFPipeFixture, "fetch waits with old instruction", "[if_pipe]") +{ + Response r; + InstrDTO instr; + int i, expected_cycles, fetch_cycles; + + fetch_cycles = this->m_delay + this->c_delay + 2; + expected_cycles = this->m_delay + (this->c_delay * 2) + 1; + + for (i = 0; i < this->m_delay + 1; ++i) { + r = this->ct->advance(instr, BLOCKED); + // check response + CHECK(r == STALLED); + } + for (i = 0; i < this->c_delay; ++i) { + r = this->ct->advance(instr, BLOCKED); + // check response + CHECK(r == STALLED); + } + for (i = 0; i < expected_cycles - fetch_cycles; ++i) { + r = this->ct->advance(instr, BLOCKED); + // check response + CHECK(r == OK); + } + + r = this->ct->advance(instr, OK); + CHECK(instr.get_time_of(FETCH) == expected_cycles); + REQUIRE(instr.get_instr_bits() == this->p[0]); +} -- cgit v1.2.3 From 4055b56b5d06e0188d764380d67e40b5b07359ac Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 29 Mar 2025 20:49:32 -0400 Subject: Add advance logic for decode --- inc/id.h | 21 ++++++---- inc/if.h | 2 +- src/sim/id.cc | 130 ++++++++++++++++++++++++++++++++++------------------------ src/sim/if.cc | 4 +- 4 files changed, 91 insertions(+), 66 deletions(-) (limited to 'src/sim/if.cc') diff --git a/inc/id.h b/inc/id.h index f8c167d..49637ce 100644 --- a/inc/id.h +++ b/inc/id.h @@ -17,6 +17,10 @@ class ID : public Stage Response advance(InstrDTO &next_instr, Response p) override; + /* The following methods are made public so that they may be tested, and are + * not to be called from outside classes during standard execution. + */ + /** * Parse an instruction into a type, opcode, and fields. If the type is * invalid, only the type field will be set. @@ -33,11 +37,6 @@ class ID : public Stage */ void get_instr_fields( signed int &s1, signed int &s2, signed int &s3, Mnemonic &m); - - /* The following methods are made public so that they may be tested, and are - * not to be called from outside classes during standard execution. - */ - /** * Facilitates register checkout and data hazard management. * It does this by checking that the register passed in is not currently @@ -57,11 +56,15 @@ class ID : public Stage void write_guard(signed int &r); private: - // TODO write me /** - * Helper for `` - * Attempts to parse and dereference instruction arguments. If a desc - + * Decodes `curr_instr` and sets status to BLOCKED if a data hazard occurs. + */ + void advance_helper(); + /** + * Helper for `get_instr_fields` + * Attempts to parse and dereference instruction arguments. Uses read and + * write guards to prevent RAW conflicts. + * * @param the resulting first field. To call this function properly, this * field must contain the section of the instruction to be parsed. * @param the resulting second field. diff --git a/inc/if.h b/inc/if.h index 9f84e51..3fafe53 100644 --- a/inc/if.h +++ b/inc/if.h @@ -25,7 +25,7 @@ class IF : public Stage * the nullptr. * @return STALLED if we are waiting on the storage devices, OK otherwise. */ - void fetch_with_buffer(); + void advance_helper(); }; #endif /* IF_H_INCLUDED */ diff --git a/src/sim/id.cc b/src/sim/id.cc index c04b31f..68ac555 100644 --- a/src/sim/id.cc +++ b/src/sim/id.cc @@ -5,68 +5,26 @@ #include "logger.h" #include "response.h" #include "stage.h" -#include ID::ID(Stage *stage) : Stage(stage) { this->id = DCDE; } Response ID::advance(InstrDTO &next_instr, Response p) { - Response r; - signed int s1, s2, s3; - Mnemonic m; - - s1 = next_instr.get_instr_bits(); - - get_instr_fields(s1, s2, s3, m); - return r; -} - -void ID::decode_R_type(signed int &s1, signed int &s2, signed int &s3) -{ - unsigned int s0b, s1b, s2b; - Response r1, r2; - - s0b = REG_SIZE; - s1b = s0b + REG_SIZE; - s2b = s1b + REG_SIZE; - s3 = GET_MID_BITS(s1, s1b, s2b); - s2 = GET_MID_BITS(s1, s0b, s1b); - s1 = GET_LS_BITS(s1, s0b); - - r1 = this->read_guard(s1); - r2 = this->read_guard(s2); - this->write_guard(s3); - - this->status = std::max(r1, r2); -} -void ID::decode_I_type(signed int &s1, signed int &s2, signed int &s3) -{ - unsigned int s0b, s1b, s2b; - - s0b = REG_SIZE; - s1b = s0b + REG_SIZE; - s2b = WORD_SPEC; - s3 = GET_MID_BITS(s1, s1b, s2b); - s2 = GET_MID_BITS(s1, s0b, s1b); - s1 = GET_LS_BITS(s1, s0b); - - this->status = this->read_guard(s1); - this->write_guard(s2); -} - -void ID::decode_J_type(signed int &s1, signed int &s2) -{ - unsigned int s0b, s1b; - - s0b = REG_SIZE; - s1b = WORD_SPEC; - s2 = GET_MID_BITS(s1, s0b, s1b); - s1 = GET_LS_BITS(s1, s0b); + Response n; + + this->advance_helper(); + if (this->status == OK && p == OK) { + // mutual consent + this->curr_instr->set_time_of(this->id, this->clock_cycle); + next_instr = *this->curr_instr; + curr_instr = nullptr; + } - this->status = this->read_guard(*&s1); + n = (p != OK || this->status != OK) ? BLOCKED : OK; + // the power of consent + n = this->next->advance(next_instr, n); } -// TODO this function is ugly void ID::get_instr_fields( signed int &s1, signed int &s2, signed int &s3, Mnemonic &m) { @@ -119,3 +77,67 @@ void ID::write_guard(signed int &v) this->checked_out.push_back(v); v = this->dereference_register(v); } + +void ID::advance_helper() +{ + signed int s1, s2, s3; + Mnemonic m; + + // it may be good to ensure we are not doing + // work that has already been done + if (this->curr_instr) { + s1 = curr_instr->get_instr_bits(); + get_instr_fields(s1, s2, s3, m); + if (this->status == OK) { + curr_instr->set_s1(s1); + curr_instr->set_s2(s2); + curr_instr->set_s3(s3); + curr_instr->set_mnemonic(m); + } + } +} + +void ID::decode_R_type(signed int &s1, signed int &s2, signed int &s3) +{ + unsigned int s0b, s1b, s2b; + Response r1, r2; + + s0b = REG_SIZE; + s1b = s0b + REG_SIZE; + s2b = s1b + REG_SIZE; + s3 = GET_MID_BITS(s1, s1b, s2b); + s2 = GET_MID_BITS(s1, s0b, s1b); + s1 = GET_LS_BITS(s1, s0b); + + r1 = this->read_guard(s1); + r2 = this->read_guard(s2); + this->write_guard(s3); + + this->status = (r1 == BLOCKED || r2 == BLOCKED) ? BLOCKED : OK; +} +void ID::decode_I_type(signed int &s1, signed int &s2, signed int &s3) +{ + unsigned int s0b, s1b, s2b; + + s0b = REG_SIZE; + s1b = s0b + REG_SIZE; + s2b = WORD_SPEC; + s3 = GET_MID_BITS(s1, s1b, s2b); + s2 = GET_MID_BITS(s1, s0b, s1b); + s1 = GET_LS_BITS(s1, s0b); + + this->status = this->read_guard(s1); + this->write_guard(s2); +} + +void ID::decode_J_type(signed int &s1, signed int &s2) +{ + unsigned int s0b, s1b; + + s0b = REG_SIZE; + s1b = WORD_SPEC; + s2 = GET_MID_BITS(s1, s0b, s1b); + s1 = GET_LS_BITS(s1, s0b); + + this->status = this->read_guard(*&s1); +} diff --git a/src/sim/if.cc b/src/sim/if.cc index de044f8..7d3291b 100644 --- a/src/sim/if.cc +++ b/src/sim/if.cc @@ -8,7 +8,7 @@ IF::IF(Stage *stage) : Stage(stage) { this->id = FETCH; } Response IF::advance(InstrDTO &next_instr, Response p) { - this->fetch_with_buffer(); + this->advance_helper(); if (this->status == OK && p == OK) { // mutual consent ++this->pc; @@ -19,7 +19,7 @@ Response IF::advance(InstrDTO &next_instr, Response p) return this->status; } -void IF::fetch_with_buffer() +void IF::advance_helper() { Response r; signed int bits; -- cgit v1.2.3