summaryrefslogtreecommitdiff
path: root/src/Bus.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Bus.h')
-rw-r--r--src/Bus.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/Bus.h b/src/Bus.h
new file mode 100644
index 0000000..d55ee4f
--- /dev/null
+++ b/src/Bus.h
@@ -0,0 +1,65 @@
+#pragma once
+
+#include <iostream>
+#include <cstdint>
+#include <type_traits>
+#include <stdexcept>
+#include <cstring>
+#include <memory>
+
+#include "RAM.h"
+
+#include "Exceptions.h"
+
+class Bus {
+public:
+ Bus(std::shared_ptr<RAM> m_Bus);
+ ~Bus() = default;
+
+public:
+ template<typename T>
+ void WriteX(uint64_t address, T value)
+ {
+ static_assert(std::is_unsigned_v<T>, "T must be an unsigned int of any size smaller than 8 bytes!");
+
+ // std::cout << "Bus write: " << std::hex << address << std::endl;
+
+ switch(address)
+ {
+ case 0x00008000 ... 0x000FFFFF:
+ {
+ uint64_t offset = address - 0x00008000;
+ std::memcpy(&m_RAM->Data()[offset], &value, sizeof(T));
+ break;
+ }
+ default:
+ std::string exception = "Illegal access to: " + std::to_string(address);
+ throw CPUException(exception);
+ }
+ }
+
+ template<typename T>
+ T AccessX(uint64_t address)
+ {
+ static_assert(std::is_unsigned_v<T>, "T must be an unsigned int of any size smaller than 8 bytes!");
+
+ //std::cout << "Bus access: " << std::hex << address << std::endl;
+
+ switch(address)
+ {
+ case 0x00008000 ... 0x000FFFFF:
+ {
+ uint64_t offset = address - 0x00008000;
+ T value;
+ std::memcpy(&value, &m_RAM->Data()[offset], sizeof(T));
+ return value;
+ }
+ default:
+ std::string exception = "Illegal access to: " + std::to_string(address);
+ throw std::runtime_error(exception);
+ }
+ }
+
+private:
+ std::shared_ptr<RAM> m_RAM;
+};