diff options
author | bd <bdunahu@operationnull.com> | 2025-03-30 19:42:34 -0400 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-03-30 19:42:34 -0400 |
commit | ebeb29d1b87c533c1e80e86ceed9ddc40e4d2cb2 (patch) | |
tree | 6fb10621af07438e01391f7cb734023e3aaf691a /tests | |
parent | 36dabe6183af98b2e3f6d0316436dc3affc3d986 (diff) |
Add tests for EX
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ex.cc | 425 | ||||
-rw-r--r-- | tests/id.cc | 64 | ||||
-rw-r--r-- | tests/if.cc | 12 |
3 files changed, 453 insertions, 48 deletions
diff --git a/tests/ex.cc b/tests/ex.cc new file mode 100644 index 0000000..d5920d2 --- /dev/null +++ b/tests/ex.cc @@ -0,0 +1,425 @@ +#include "ex.h" +#include "cache.h" +#include "controller.h" +#include "dram.h" +#include "dum.h" +#include "instr.h" +#include "instrDTO.h" +#include <catch2/catch_test_macros.hpp> + +class EXFixture +{ + public: + EXFixture() + { + this->dr = new Dram(3); + this->c = new Cache(this->dr, 1); + this->dum = new DUM(nullptr); + this->e = new EX(dum); + this->ct = new Controller(this->e, this->c, true); + }; + ~EXFixture() + { + delete this->ct; + delete this->c; + }; + InstrDTO *execute_instr(signed int s1, signed int s2, Mnemonic m) + { + InstrDTO *i = new InstrDTO(); + i->set_s1(s1); + i->set_s2(s2); + i->set_mnemonic(m); + this->dum->set_curr_instr(i); + + i = this->ct->advance(OK); + REQUIRE(i == nullptr); + i = this->ct->advance(OK); + REQUIRE(i != nullptr); + + return i; + } + Dram *dr; + Cache *c; + EX *e; + DUM *dum; + Controller *ct; +}; + +std::unordered_map<Mnemonic, signed int> results = { + {ADD, 42}, + {SUB, 83958}, +}; + +TEST_CASE_METHOD(EXFixture, "ADD within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = ADD; + s1 = 42000, s2 = -41958; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 42); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "ADD overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "ADD underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "SUB within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = SUB; + s1 = 200, s2 = 131; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 69); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "SUB overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SUB underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "MUL within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = MUL; + s1 = 200, s2 = 200; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 40000); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "MUL overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "MUL underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "QUOT within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = QUOT; + s1 = 2043, s2 = 40; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 51); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "QUOT overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "QUOT underflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "QUOT halt", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "REM within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = REM; + s1 = 2043, s2 = 40; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 3); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "REM overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "REM underflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "REM halt", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "SFTR within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = SFTR; + s1 = 1300, s2 = 6; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 20); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "SFTR overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SFTR underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "SFTL within bounds", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = SFTL; + s1 = 13, s2 = 6; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 832); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "SFTL overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SFTL underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "AND", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = AND; + s1 = 1234, s2 = 5678; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 1026); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +TEST_CASE_METHOD(EXFixture, "OR", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = OR; + s1 = 1234, s2 = 5678; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 5886); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +TEST_CASE_METHOD(EXFixture, "NOT", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = NOT; + s1 = 1234, s2 = -1; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == -1235); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +TEST_CASE_METHOD(EXFixture, "XOR", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = XOR; + s1 = 1234, s2 = 5678; + i = execute_instr(s1, s2, m); + + CHECK(i->get_s1() == 4860); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "ADDV within bounds", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "ADDV overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "ADDV underflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SUBV within bounds", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SUBV overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "SUBV underflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "MULV within bounds", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "MULV overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "MULV underflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "DIVV within bounds", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "DIVV overflow", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "DIVV underflow", "[ex]") +// { +// } + +TEST_CASE_METHOD(EXFixture, "CMP less", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = CMP; + s1 = -1, s2 = 0; + i = execute_instr(s1, s2, m); + + // should not be changed + CHECK(i->get_s1() == -1); + + CHECK(!ct->get_condition(EQ)); + CHECK(!ct->get_condition(GT)); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +TEST_CASE_METHOD(EXFixture, "CMP equal", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = CMP; + s1 = 20, s2 = 20; + i = execute_instr(s1, s2, m); + + // should not be changed + CHECK(i->get_s1() == 20); + + CHECK(ct->get_condition(EQ)); + CHECK(!ct->get_condition(GT)); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +TEST_CASE_METHOD(EXFixture, "CMP greater", "[ex]") +{ + signed int s1 = 0, s2 = 0; + Mnemonic m; + InstrDTO *i; + + m = CMP; + s1 = 21, s2 = 20; + i = execute_instr(s1, s2, m); + + // should not be changed + CHECK(i->get_s1() == 21); + + CHECK(!ct->get_condition(EQ)); + CHECK(ct->get_condition(GT)); + CHECK(!ct->get_condition(OF)); + CHECK(!ct->get_condition(UF)); + + delete i; +} + +// TEST_CASE_METHOD(EXFixture, "CEQ less", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "CEQ equal", "[ex]") +// { +// } + +// TEST_CASE_METHOD(EXFixture, "CEQ greater", "[ex]") +// { +// } diff --git a/tests/id.cc b/tests/id.cc index be50ed5..5270972 100644 --- a/tests/id.cc +++ b/tests/id.cc @@ -23,11 +23,18 @@ class IDFixture delete this->ct; delete this->c; }; - void prime_bits(signed int raw) + InstrDTO *decode_bits(signed int raw) { InstrDTO *i = new InstrDTO(); i->set_instr_bits(raw); this->dum->set_curr_instr(i); + + i = this->ct->advance(OK); + REQUIRE(i == nullptr); + i = this->ct->advance(OK); + REQUIRE(i != nullptr); + + return i; } signed int encode_R_type( signed int s3, @@ -79,12 +86,15 @@ class IDFixture TEST_CASE_METHOD(IDFixture, "Parse invalid type", "[id]") { - signed int s1 = 0, s2 = 0, s3 = 0; - Mnemonic m; + signed int t; + InstrDTO *i; - s1 = 0xFFFFFFFF; - this->d->get_instr_fields(s1, s2, s3, m); - CHECK(m == NOP); + t = this->encode_R_type(0b0, 0b1, 0b10, 0b11, 0b11); + i = this->decode_bits(t); + + CHECK(i->get_mnemonic() == NOP); + + delete i; } TEST_CASE_METHOD(IDFixture, "Parse arbitrary r-type # one", "[id]") @@ -93,12 +103,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary r-type # one", "[id]") InstrDTO *i; t = this->encode_R_type(0b0, 0b1, 0b10, 0b11, 0b0); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0x00000000); @@ -114,12 +119,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary r-type # two", "[id]") InstrDTO *i; t = this->encode_R_type(0b10000, 0b01000, 0b00100, 0b10, 0b0); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0x00000000); @@ -135,12 +135,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary i-type # one", "[id]") InstrDTO *i; t = this->encode_I_type(0xF, 0b1, 0b10, 0b0111, 0b1); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0x00000000); @@ -156,12 +151,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary i-type # two", "[id]") InstrDTO *i; t = this->encode_I_type(0xCC, 0b010, 0b101, 0b1011, 0b1); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0x00000000); @@ -177,12 +167,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary j-type # one", "[id]") InstrDTO *i; t = this->encode_J_type(0x3456, 0b10101, 0b0111, 0b10); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0x3456); @@ -197,12 +182,7 @@ TEST_CASE_METHOD(IDFixture, "Parse arbitrary j-type # two", "[id]") InstrDTO *i; t = this->encode_J_type(0xBBCCF, 0b10101, 0b0011, 0b10); - this->prime_bits(t); - - i = this->ct->advance(OK); - REQUIRE(i == nullptr); - i = this->ct->advance(OK); - REQUIRE(i != nullptr); + i = this->decode_bits(t); CHECK(i->get_s1() == 0x00000000); // registers are empty CHECK(i->get_s2() == 0xBBCCF); diff --git a/tests/if.cc b/tests/if.cc index 1f02cb0..4ebc47d 100644 --- a/tests/if.cc +++ b/tests/if.cc @@ -5,10 +5,10 @@ #include "instrDTO.h" #include <catch2/catch_test_macros.hpp> -class IFPipeFixture +class IFFixture { public: - IFPipeFixture() + IFFixture() { Dram *d; @@ -21,7 +21,7 @@ class IFPipeFixture this->f = new IF(nullptr); this->ct = new Controller(this->f, this->c, true); } - ~IFPipeFixture() + ~IFFixture() { delete this->ct; delete this->c; @@ -70,7 +70,7 @@ class IFPipeFixture Controller *ct; }; -TEST_CASE_METHOD(IFPipeFixture, "fetch returns single instuction", "[if_pipe]") +TEST_CASE_METHOD(IFFixture, "fetch returns single instuction", "[if_pipe]") { InstrDTO *i; int expected_cycles; @@ -83,7 +83,7 @@ TEST_CASE_METHOD(IFPipeFixture, "fetch returns single instuction", "[if_pipe]") delete i; } -TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]") +TEST_CASE_METHOD(IFFixture, "fetch returns two instuctions", "[if_pipe]") { InstrDTO *i; int expected_cycles; @@ -103,7 +103,7 @@ TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]") delete i; } -TEST_CASE_METHOD(IFPipeFixture, "fetch waits with old instruction", +TEST_CASE_METHOD(IFFixture, "fetch waits with old instruction", "[if_pipe]") { InstrDTO *i; |