summaryrefslogtreecommitdiff
path: root/src/Bus.h
blob: d55ee4fb3fd161b664cd05c69026bc0b4507d744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
};