From 01ad5b6f308124d78624282b20a4689d0223a160 Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Thu, 23 Jan 2020 19:28:40 +0100
Subject: [PATCH] Creating the base of the Memory Bus
---
CMakeLists.txt | 2 +-
sources/Memory/IMemory.cpp | 25 +++++++++++++++++++++
sources/Memory/IMemory.hpp | 28 ++++++++++++++++++++++++
sources/Memory/MemoryBus.cpp | 42 ++++++++++++++++++++++++++++++++++++
sources/Memory/MemoryBus.hpp | 26 ++++++++++++++++++++++
5 files changed, 122 insertions(+), 1 deletion(-)
create mode 100644 sources/Memory/IMemory.cpp
create mode 100644 sources/Memory/IMemory.hpp
create mode 100644 sources/Memory/MemoryBus.cpp
create mode 100644 sources/Memory/MemoryBus.hpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bda854b..453fc87 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,4 +15,4 @@ target_link_libraries(unit_tests criterion -lgcov)
target_compile_options(unit_tests PUBLIC -fprofile-arcs -ftest-coverage)
# make app
-add_executable(ComSquare main.cpp sources/hello.cpp)
+add_executable(ComSquare main.cpp sources/hello.cpp sources/Memory/MemoryBus.cpp sources/Memory/MemoryBus.hpp sources/Memory/IMemory.hpp sources/Memory/IMemory.cpp)
diff --git a/sources/Memory/IMemory.cpp b/sources/Memory/IMemory.cpp
new file mode 100644
index 0000000..58816aa
--- /dev/null
+++ b/sources/Memory/IMemory.cpp
@@ -0,0 +1,25 @@
+//
+// Created by anonymus-raccoon on 1/23/20.
+//
+
+#include "IMemory.hpp"
+#include
+
+namespace ComSquare
+{
+ void IMemory::setMemoryRegion(uint32_t start, uint32_t end)
+ {
+ this->_start = start;
+ this->_end = end;
+ }
+
+ bool IMemory::hasMemorydAt(uint32_t addr)
+ {
+ return this->_start <= addr && addr <= this->_end;
+ }
+
+ uint32_t IMemory::getStart()
+ {
+ return this->_start;
+ }
+}
\ No newline at end of file
diff --git a/sources/Memory/IMemory.hpp b/sources/Memory/IMemory.hpp
new file mode 100644
index 0000000..39a1b45
--- /dev/null
+++ b/sources/Memory/IMemory.hpp
@@ -0,0 +1,28 @@
+//
+// Created by anonymus-raccoon on 1/23/20.
+//
+
+#ifndef COMSQUARE_IMEMORY_HPP
+#define COMSQUARE_IMEMORY_HPP
+
+
+#include
+#include
+
+namespace ComSquare
+{
+ class IMemory {
+ private:
+ uint32_t _start;
+ uint32_t _end;
+ public:
+ virtual uint8_t read(uint32_t addr) = 0;
+ virtual void write(uint32_t addr, uint8_t data) = 0;
+ void setMemoryRegion(uint32_t start, uint32_t end);
+ bool hasMemorydAt(uint32_t addr);
+ uint32_t getStart();
+ };
+};
+
+
+#endif //COMSQUARE_IMEMORY_HPP
diff --git a/sources/Memory/MemoryBus.cpp b/sources/Memory/MemoryBus.cpp
new file mode 100644
index 0000000..46ca3a2
--- /dev/null
+++ b/sources/Memory/MemoryBus.cpp
@@ -0,0 +1,42 @@
+//
+// Created by anonymus-raccoon on 1/23/20.
+//
+
+#include
+#include
+#include "MemoryBus.hpp"
+
+namespace ComSquare
+{
+ IMemory *MemoryBus::getAccessor(uint32_t addr)
+ {
+ return std::find_if(this->_memoryAccessors.begin(), this->_memoryAccessors.end(), [addr](IMemory *accessor)
+ {
+ return accessor->hasMemorydAt(addr);
+ }).base();
+ }
+
+ uint8_t MemoryBus::read(uint32_t addr)
+ {
+ IMemory *handler = this->getAccessor(addr);
+
+ if (!handler) {
+ std::cout << "Unknown memory accessor for address " << std::hex << addr << ". Using open bus." << std::endl;
+ return this->_openbus;
+ }
+ uint8_t data = handler->read(addr - handler->getStart());
+ this->_openbus = data;
+ return data;
+ }
+
+ void MemoryBus::write(uint32_t addr, uint8_t data)
+ {
+ IMemory *handler = this->getAccessor(addr);
+
+ if (!handler) {
+ std::cout << "Unknown memory accessor for address " << std::hex << addr << ". Warning, it was a write." << std::endl;
+ return;
+ }
+ handler->write(addr - handler->getStart(), data);
+ }
+}
\ No newline at end of file
diff --git a/sources/Memory/MemoryBus.hpp b/sources/Memory/MemoryBus.hpp
new file mode 100644
index 0000000..f480839
--- /dev/null
+++ b/sources/Memory/MemoryBus.hpp
@@ -0,0 +1,26 @@
+//
+// Created by anonymus-raccoon on 1/23/20.
+//
+
+#ifndef COMSQUARE_MEMORYBUS_HPP
+#define COMSQUARE_MEMORYBUS_HPP
+
+#include
+#include
+#include "IMemory.hpp"
+
+namespace ComSquare
+{
+ class MemoryBus {
+ private:
+ std::vector _memoryAccessors;
+ IMemory * getAccessor(uint32_t addr);
+ uint8_t _openbus;
+ public:
+ uint8_t read(uint32_t addr);
+ void write(uint32_t addr, uint8_t data);
+ };
+}
+
+
+#endif //COMSQUARE_MEMORYBUS_HPP