diff options
author | bd <bdunahu@operationnull.com> | 2025-03-30 12:33:25 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-03-30 12:33:25 -0400 |
commit | 6557e7e623140871968776429d241570002a65f5 (patch) | |
tree | 420c2aced8e008708ea951edf36608e0bc5d4e2c /src | |
parent | 8e56373a5436852fe9c7934e03d7b57493625003 (diff) |
Setting condition code register, overflow guard
Diffstat (limited to 'src')
-rw-r--r-- | src/sim/ex.cc | 88 | ||||
-rw-r--r-- | src/sim/instr.cc | 29 | ||||
-rw-r--r-- | src/sim/stage.cc | 12 |
3 files changed, 99 insertions, 30 deletions
diff --git a/src/sim/ex.cc b/src/sim/ex.cc index 3a1e92c..fbf460f 100644 --- a/src/sim/ex.cc +++ b/src/sim/ex.cc @@ -1,9 +1,95 @@ #include "ex.h" #include "accessor.h" +#include "definitions.h" #include "instrDTO.h" #include "response.h" #include "stage.h" +#include <unordered_map> -EX::EX(Stage *stage) : Stage(stage) { this->id = EXEC; } +// clang-format off +#define INIT_INSTRUCTION(mnemonic, body) \ + {mnemonic, [this](signed int &s1, signed int &s2) { \ + body; \ + }} +// clang-format on + +EX::EX(Stage *stage) : Stage(stage) +{ + this->id = EXEC; + instr_map = { + + /* R type instructions */ + INIT_INSTRUCTION( + ADD, + { + this->overflow_guard(s1, s2); + s1 = s1 + s2; + }), + + INIT_INSTRUCTION( + SUB, + { + this->overflow_guard(s1, -s2); + s1 = s1 - s2; + }), + + INIT_INSTRUCTION(MUL, {}), + INIT_INSTRUCTION(QUOT, {}), + INIT_INSTRUCTION(REM, {}), + INIT_INSTRUCTION(SFTR, {}), + INIT_INSTRUCTION(SFTL, {}), + INIT_INSTRUCTION(AND, {}), + INIT_INSTRUCTION(OR, {}), + INIT_INSTRUCTION(NOT, {}), + INIT_INSTRUCTION(XOR, {}), + INIT_INSTRUCTION(ADDV, {}), + INIT_INSTRUCTION(SUBV, {}), + INIT_INSTRUCTION(MULV, {}), + INIT_INSTRUCTION(DIVV, {}), + INIT_INSTRUCTION(CMP, {}), + INIT_INSTRUCTION(CEV, {}), + + /* I type instructions */ + INIT_INSTRUCTION(LOAD, {}), + INIT_INSTRUCTION(LOADV, {}), + INIT_INSTRUCTION(ADDI, {}), + INIT_INSTRUCTION(SUBI, {}), + INIT_INSTRUCTION(SFTRI, {}), + INIT_INSTRUCTION(SFTL, {}), + INIT_INSTRUCTION(ANDI, {}), + INIT_INSTRUCTION(ORI, {}), + INIT_INSTRUCTION(XORI, {}), + INIT_INSTRUCTION(STORE, {}), + INIT_INSTRUCTION(STOREV, {}), + + /* J type instructions */ + INIT_INSTRUCTION(JMP, {}), + INIT_INSTRUCTION(JRL, {}), + INIT_INSTRUCTION(JAL, {}), + INIT_INSTRUCTION(BEQ, {}), + INIT_INSTRUCTION(BGT, {}), + INIT_INSTRUCTION(BUF, {}), + INIT_INSTRUCTION(BOF, {}), + INIT_INSTRUCTION(PUSH, {}), + INIT_INSTRUCTION(POP, {}), + + /* NOP */ + INIT_INSTRUCTION(NOP, (void)s2; (void)s1;), + }; +} InstrDTO *EX::advance(Response p) { return nullptr; } + +void EX::overflow_guard(signed int a, signed int b) +{ + if (a >= 0 && b >= 0 && (a > MAX_INT - b)) { + this->set_condition(OF, true); + this->set_condition(UF, false); + } else if (a < 0 && b < 0 && (a < (-MAX_INT) - 1 - b)) { + this->set_condition(OF, false); + this->set_condition(UF, true); + } else { + this->set_condition(OF, false); + this->set_condition(UF, false); + } +} diff --git a/src/sim/instr.cc b/src/sim/instr.cc index 08edf5e..e614de5 100644 --- a/src/sim/instr.cc +++ b/src/sim/instr.cc @@ -3,36 +3,8 @@ #include <map> #include <unordered_map> -// clang-format off -#define INIT_INSTRUCTION(mnemonic, body) \ - {mnemonic, [](signed int &s1, signed int &s2, signed int &s3) { \ - body; \ - }} -// clang-format on - namespace instr { -// clang-format off -const std::unordered_map<Mnemonic, std::function<void(signed int &s1, signed int &s2, signed int &s3)>> - // clang-format on - instr_map = { - - /* R type instructions */ - // TODO these need to be WRAPPED with a function that sets overflow. - // future note to self, if these are more than 2 lines each, you're - // doing it wrong - INIT_INSTRUCTION(ADD, s3 = s1 + s2;), - INIT_INSTRUCTION(SUB, s3 = s1 - s2;), - - /* I type instructions */ - - /* J type instructions */ - - /* NOP */ - INIT_INSTRUCTION(NOP, (void)s3; (void)s2; (void)s1;), - -}; - const std::unordered_map<unsigned int, Mnemonic> mnemonic_map = { {0b0000100, ADD}, {0b0001000, SUB}, {0b0001100, MUL}, {0b0010000, QUOT}, {0b0010100, REM}, {0b0011000, SFTR}, @@ -48,6 +20,5 @@ const std::unordered_map<unsigned int, Mnemonic> mnemonic_map = { {0b0001010, JRL}, {0b0001110, JAL}, {0b0010010, BEQ}, {0b0010110, BGT}, {0b0011010, BUF}, {0b0011110, BOF}, {0b0100010, PUSH}, {0b0100110, POP}, - }; } // namespace instr diff --git a/src/sim/stage.cc b/src/sim/stage.cc index 62a7fd6..d8c882a 100644 --- a/src/sim/stage.cc +++ b/src/sim/stage.cc @@ -21,6 +21,18 @@ Storage *Stage::storage; bool Stage::is_pipelined; int Stage::clock_cycle; +void Stage::set_condition(CC c, bool v) +{ + if (v) + this->gprs[3] = this->gprs[3] & 1 << c; + else + this->gprs[3] = this->gprs[3] & ~(1 << c); +} + +bool Stage::get_condition(CC c) { + return (this->gprs[3] >> c) & 1; +} + signed int Stage::dereference_register(signed int v) { signed int r; |