From 8027941b705e219fd202b7c01d5a4a311670cbee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sat, 7 Apr 2018 21:13:42 +0200 Subject: Add initial implementatio of caches --- qtmips_machine/tests/testcache.cpp | 61 ++++++++++++++++++++++++++++++++++++++ qtmips_machine/tests/tests.pro | 3 +- qtmips_machine/tests/tst_machine.h | 3 ++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 qtmips_machine/tests/testcache.cpp (limited to 'qtmips_machine/tests') diff --git a/qtmips_machine/tests/testcache.cpp b/qtmips_machine/tests/testcache.cpp new file mode 100644 index 0000000..02e734e --- /dev/null +++ b/qtmips_machine/tests/testcache.cpp @@ -0,0 +1,61 @@ +#include "tst_machine.h" +#include "cache.h" + +using namespace machine; + +void MachineTests::cache_data() { + QTest::addColumn("cache_c"); + QTest::addColumn("hit"); + QTest::addColumn("miss"); + + MachineConfigCache cache_c; + cache_c.set_write_policy(MachineConfigCache::WP_TROUGH); + cache_c.set_enabled(true); + cache_c.set_sets(8); + cache_c.set_blocks(1); + cache_c.set_associativity(1); + QTest::newRow("Directly mapped") << cache_c \ + << (unsigned)3 \ + << (unsigned)7; + cache_c.set_sets(1); + cache_c.set_blocks(8); + QTest::newRow("Wide") << cache_c \ + << (unsigned)5 \ + << (unsigned)5; + cache_c.set_sets(4); + cache_c.set_blocks(4); + QTest::newRow("Square") << cache_c \ + << (unsigned)4 \ + << (unsigned)6; +} + +void MachineTests::cache() { + QFETCH(MachineConfigCache, cache_c); + QFETCH(unsigned, hit); + QFETCH(unsigned, miss); + + Memory m; + Cache cch(&m, &cache_c); + + // Test reads // + m.write_word(0x200, 0x24); + m.write_word(0x204, 0x66); + m.write_word(0x21c, 0x12); + m.write_word(0x300, 0x32); + for (int i = 0; i < 2; i++) { + QCOMPARE(cch.read_word(0x200), (std::uint32_t)0x24); + QCOMPARE(cch.read_word(0x204), (std::uint32_t)0x66); + QCOMPARE(cch.read_word(0x21c), (std::uint32_t)0x12); + QCOMPARE(cch.read_word(0x300), (std::uint32_t)0x32); + } + + // Test writes // + cch.write_word(0x700, 0x24); + QCOMPARE(m.read_word(0x700), (std::uint32_t)0x24); + cch.write_word(0x700, 0x23); + QCOMPARE(m.read_word(0x700), (std::uint32_t)0x23); + + // Verify counts + QCOMPARE(cch.hit(), hit); + QCOMPARE(cch.miss(), miss); +} diff --git a/qtmips_machine/tests/tests.pro b/qtmips_machine/tests/tests.pro index ffe75b7..751bf8a 100644 --- a/qtmips_machine/tests/tests.pro +++ b/qtmips_machine/tests/tests.pro @@ -21,7 +21,8 @@ SOURCES += tst_machine.cpp \ testprogramloader.cpp \ testinstruction.cpp \ testalu.cpp \ - testcore.cpp + testcore.cpp \ + testcache.cpp HEADERS += tst_machine.h diff --git a/qtmips_machine/tests/tst_machine.h b/qtmips_machine/tests/tst_machine.h index ad3112c..a0e8525 100644 --- a/qtmips_machine/tests/tst_machine.h +++ b/qtmips_machine/tests/tst_machine.h @@ -46,6 +46,9 @@ private Q_SLOTS: void singlecore_mem_data(); void pipecore_mem(); void pipecore_mem_data(); + // Cache + void cache_data(); + void cache(); }; #endif // TST_MACHINE_H -- cgit v1.2.3