Skip to content

Commit

Permalink
[#8 #10] feature: implemented nop, loading files to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
laudominik committed Sep 19, 2023
1 parent ee2fcef commit dd9f2c6
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/basicComponents/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <cstdint>
#include <vector>
#include <fstream>
#include <iostream>

#include <utility/Widths.h>
#include <Register.h>
Expand All @@ -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<Byte>(
(std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>());
if(bytes.size() < memsize){
bytes.resize(memsize);
}
}

Memory(const std::vector<Byte> bytes) : bytes(bytes) {};

template<typename WIDTH_T>
Expand Down
31 changes: 31 additions & 0 deletions src/instructions/nop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <util/InstructionIfc.h>
#include <basicComponents/Memory.h>
#include <basicComponents/Cpu.h>
#include <util/register_code.h>

#include <iostream>


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<Nop>::isInstruction(Byte opcode){
return opcode == 0x90;
}
2 changes: 2 additions & 0 deletions src/instructions/util/InstructionsRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sub.h>
#include <push.h>
#include <mov.h>
#include <nop.h>

InstructionRegistry::InstructionRegistry() {
initializeInstructionModules();
Expand All @@ -13,6 +14,7 @@ void InstructionRegistry::initializeInstructionModules() {
addInstruction<Sub>();
addInstruction<Push>();
addInstruction<Mov>();
addInstruction<Nop>();
}

const std::vector<std::unique_ptr<InstructionIfcBuilder>>& InstructionRegistry::getInstructionRegistryVector() const {
Expand Down
3 changes: 3 additions & 0 deletions test/asm/noops.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nop
nop
nop
11 changes: 11 additions & 0 deletions test/binary_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dword>();
runner.cpu.cs.set<Word>(0);
runner.run(3);
EXPECT_EQ(eaxBefore, runner.cpu.eax.get<Dword>());
EXPECT_EQ(runner.memory.read<Byte>(0x0), 0x90);
EXPECT_EQ(runner.memory.read<Byte>(0x0), 0x90);
EXPECT_EQ(runner.memory.read<Byte>(0x0), 0x90);
}
1 change: 1 addition & 0 deletions test/data/compiled/example_hex_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abcdef
1 change: 1 addition & 0 deletions test/data/compiled/noops
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���
21 changes: 21 additions & 0 deletions test/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#include <gtest/gtest.h>
#include <filesystem>

#include <basicComponents/Memory.h>

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<Dword>(0x0, 0xB16B00B5);
Expand Down Expand Up @@ -33,3 +43,14 @@ TEST(MemoryTest, testRead){
EXPECT_EQ(memory.read<Dword>(0x0), 0x00DEFEC8);
EXPECT_EQ(memory.read<Dword>(testMemorySize), 0x00DEFEC8);
}

TEST(MemoryTest, testOpenFile){
TestableMemory memory("../../test/data/compiled/example_hex_file");
ASSERT_EQ(memory.size(), testMemorySize);
EXPECT_EQ(memory.read<Byte>(0x0), 'a');
EXPECT_EQ(memory.read<Byte>(0x1), 'b');
EXPECT_EQ(memory.read<Byte>(0x2), 'c');
EXPECT_EQ(memory.read<Byte>(0x3), 'd');
EXPECT_EQ(memory.read<Byte>(0x4), 'e');
EXPECT_EQ(memory.read<Byte>(0x5), 'f');
}
3 changes: 3 additions & 0 deletions test/util/BinaryRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit dd9f2c6

Please sign in to comment.