From dd9f2c6380ddf6a5c9df83a08932f059fca1ff6e Mon Sep 17 00:00:00 2001 From: Roagen7 Date: Tue, 19 Sep 2023 18:33:24 +0200 Subject: [PATCH] [#8 #10] feature: implemented nop, loading files to memory --- src/basicComponents/Memory.h | 16 ++++++++++ src/instructions/nop.h | 31 +++++++++++++++++++ .../util/InstructionsRegistry.cpp | 2 ++ test/asm/noops.asm | 3 ++ test/binary_runner.cpp | 11 +++++++ test/data/compiled/example_hex_file | 1 + test/data/compiled/noops | 1 + test/memory.cpp | 21 +++++++++++++ test/util/BinaryRunner.h | 3 ++ 9 files changed, 89 insertions(+) create mode 100644 src/instructions/nop.h create mode 100644 test/asm/noops.asm create mode 100644 test/data/compiled/example_hex_file create mode 100644 test/data/compiled/noops diff --git a/src/basicComponents/Memory.h b/src/basicComponents/Memory.h index aadda43..6614279 100644 --- a/src/basicComponents/Memory.h +++ b/src/basicComponents/Memory.h @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include @@ -21,6 +23,20 @@ class Memory { bytes.resize(size); } + Memory(const std::string& filename, std::size_t memsize){ + std::ifstream input(filename, std::ios::binary); + if(!input.is_open()){ + std::cerr << "can't read file " << filename << std::endl; + return; + } + bytes = std::vector( + (std::istreambuf_iterator(input)), + std::istreambuf_iterator()); + if(bytes.size() < memsize){ + bytes.resize(memsize); + } + } + Memory(const std::vector bytes) : bytes(bytes) {}; template diff --git a/src/instructions/nop.h b/src/instructions/nop.h new file mode 100644 index 0000000..779521e --- /dev/null +++ b/src/instructions/nop.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include + +#include + + +class Nop : public InstructionIfc { + using InstructionIfc::InstructionIfc; + + std::string mnemonic() const override { + return "NOP"; + } + void fetch() override { + + } + uint32_t size() const override { + return 1; + } + void callback() const override { + std::cout << mnemonic() << std::endl; + } +}; + +template<> +inline bool InstructionBuilder::isInstruction(Byte opcode){ + return opcode == 0x90; +} diff --git a/src/instructions/util/InstructionsRegistry.cpp b/src/instructions/util/InstructionsRegistry.cpp index 947a1d5..5ab918d 100644 --- a/src/instructions/util/InstructionsRegistry.cpp +++ b/src/instructions/util/InstructionsRegistry.cpp @@ -3,6 +3,7 @@ #include #include #include +#include InstructionRegistry::InstructionRegistry() { initializeInstructionModules(); @@ -13,6 +14,7 @@ void InstructionRegistry::initializeInstructionModules() { addInstruction(); addInstruction(); addInstruction(); + addInstruction(); } const std::vector>& InstructionRegistry::getInstructionRegistryVector() const { diff --git a/test/asm/noops.asm b/test/asm/noops.asm new file mode 100644 index 0000000..8e18b79 --- /dev/null +++ b/test/asm/noops.asm @@ -0,0 +1,3 @@ +nop +nop +nop \ No newline at end of file diff --git a/test/binary_runner.cpp b/test/binary_runner.cpp index 63b0de7..67dfc9f 100644 --- a/test/binary_runner.cpp +++ b/test/binary_runner.cpp @@ -12,3 +12,14 @@ TEST(BinaryRunnerTest, testBinaryRunner){ runner.run(numTicks); ASSERT_EQ(runner.cpu.getTickCounter(), numTicks); } + +TEST(BinaryRunnerTest ,testBinaryRunnerNoops){ + BinaryRunner runner("../../test/data/compiled/noops"); + auto eaxBefore = runner.cpu.eax.get(); + runner.cpu.cs.set(0); + runner.run(3); + EXPECT_EQ(eaxBefore, runner.cpu.eax.get()); + EXPECT_EQ(runner.memory.read(0x0), 0x90); + EXPECT_EQ(runner.memory.read(0x0), 0x90); + EXPECT_EQ(runner.memory.read(0x0), 0x90); +} \ No newline at end of file diff --git a/test/data/compiled/example_hex_file b/test/data/compiled/example_hex_file new file mode 100644 index 0000000..d96dc95 --- /dev/null +++ b/test/data/compiled/example_hex_file @@ -0,0 +1 @@ +abcdef \ No newline at end of file diff --git a/test/data/compiled/noops b/test/data/compiled/noops new file mode 100644 index 0000000..3356669 --- /dev/null +++ b/test/data/compiled/noops @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/memory.cpp b/test/memory.cpp index 487e230..590113b 100644 --- a/test/memory.cpp +++ b/test/memory.cpp @@ -1,9 +1,19 @@ #include +#include #include static constexpr auto testMemorySize = 1024; +class TestableMemory : public Memory { +public: + TestableMemory(const std::string& filename) : Memory(filename, testMemorySize){} + + size_t size(){ + return bytes.size(); + } +}; + TEST(MemoryTest, testWriteDword){ Memory memory(testMemorySize); memory.write(0x0, 0xB16B00B5); @@ -33,3 +43,14 @@ TEST(MemoryTest, testRead){ EXPECT_EQ(memory.read(0x0), 0x00DEFEC8); EXPECT_EQ(memory.read(testMemorySize), 0x00DEFEC8); } + +TEST(MemoryTest, testOpenFile){ + TestableMemory memory("../../test/data/compiled/example_hex_file"); + ASSERT_EQ(memory.size(), testMemorySize); + EXPECT_EQ(memory.read(0x0), 'a'); + EXPECT_EQ(memory.read(0x1), 'b'); + EXPECT_EQ(memory.read(0x2), 'c'); + EXPECT_EQ(memory.read(0x3), 'd'); + EXPECT_EQ(memory.read(0x4), 'e'); + EXPECT_EQ(memory.read(0x5), 'f'); +} diff --git a/test/util/BinaryRunner.h b/test/util/BinaryRunner.h index 6cfb9d2..17cabc8 100644 --- a/test/util/BinaryRunner.h +++ b/test/util/BinaryRunner.h @@ -19,6 +19,9 @@ class BinaryRunner { memory.write(addr++, bin); } } + + BinaryRunner(const std::string& filename): memory(filename, memorySize), cpu(memory){} + void run(const uint32_t& ticks){ for(auto i = 0u; i < ticks; i++){ cpu.tick();