summaryrefslogtreecommitdiff
path: root/tests/if.cc
diff options
context:
space:
mode:
authorSiddarth Suresh <155843085+SiddarthSuresh98@users.noreply.github.com>2025-03-26 22:05:46 -0400
committerGitHub <noreply@github.com>2025-03-26 22:05:46 -0400
commit7abc8926670c1701db8011cacc9c5e2e2ca95be8 (patch)
treefd3263d2754d662fdad6d69851f14a84f44db4d1 /tests/if.cc
parent9eeea1ab8bf4eb17e5da46d57a6c1d455a0a262e (diff)
parent8d37d15ebd1221e3b1698abb3b051d9d0c044c93 (diff)
Merge pull request #36 from bdunahu/bdunahu
Add fetch stage implementation, tests, program loading, DTO object Base classes plus base tests for Instruction Fetch stage with a default program
Diffstat (limited to 'tests/if.cc')
-rw-r--r--tests/if.cc100
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/if.cc b/tests/if.cc
new file mode 100644
index 0000000..5c1b645
--- /dev/null
+++ b/tests/if.cc
@@ -0,0 +1,100 @@
+#include "if.h"
+#include "cache.h"
+#include "controller.h"
+#include "dram.h"
+#include "instrDTO.h"
+#include <catch2/catch_test_macros.hpp>
+
+class IFPipeFixture
+{
+ public:
+ IFPipeFixture()
+ {
+ Dram *d;
+
+ d = new Dram(this->m_delay);
+ // 0xC00 is a nop
+ p = {0xC000, 0xC001, 0xC002, 0xC003};
+ d->load(p);
+
+ this->c = new Cache(d, this->c_delay);
+ this->f = new IF(nullptr);
+ this->ct = new Controller(this->f, this->c, true);
+ }
+ ~IFPipeFixture()
+ {
+ delete this->ct;
+ delete this->c;
+ };
+
+ /**
+ * Fetch a clean line not in cache.
+ */
+ void fetch_through(InstrDTO &instr)
+ {
+ int i;
+ Response r;
+
+ for (i = 0; i < this->m_delay + 1; ++i) {
+ r = this->ct->advance(instr);
+ // check response
+ CHECK(r == BLOCKED);
+ }
+ this->fetch_cache(instr);
+ }
+
+ /**
+ * Fetch a line in cache.
+ */
+ void fetch_cache(InstrDTO &instr)
+ {
+ int i;
+ Response r;
+
+ for (i = 0; i < this->c_delay; ++i) {
+ r = this->ct->advance(instr);
+ // check response
+ CHECK(r == WAIT);
+ }
+ r = this->ct->advance(instr);
+ // check response
+ CHECK(r == OK);
+ }
+
+ int m_delay = 3;
+ int c_delay = 1;
+ std::vector<signed int> p;
+ Cache *c;
+ IF *f;
+ Controller *ct;
+};
+
+TEST_CASE_METHOD(IFPipeFixture, "fetch returns single instuction", "[if_pipe]")
+{
+ InstrDTO instr;
+ int expected_cycles;
+
+ expected_cycles = this->m_delay + this->c_delay + 2;
+ this->fetch_through(instr);
+
+ CHECK(instr.get_if_cycle() == expected_cycles);
+ REQUIRE(instr.get_instr_bits() == this->p[0]);
+}
+
+TEST_CASE_METHOD(IFPipeFixture, "fetch returns two instuctions", "[if_pipe]")
+{
+ InstrDTO instr;
+ int expected_cycles;
+
+ expected_cycles = this->m_delay + this->c_delay + 2;
+ this->fetch_through(instr);
+
+ CHECK(instr.get_if_cycle() == 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);
+ REQUIRE(instr.get_instr_bits() == this->p[1]);
+}