-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMU.cpp
More file actions
62 lines (56 loc) · 2.36 KB
/
MMU.cpp
File metadata and controls
62 lines (56 loc) · 2.36 KB
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
#include "MMU.h"
MMU::MMU(Cartridge &cartridge) : ram{}, cartridge(cartridge) {
SPDLOG_INFO("MMU created, but not initialized");
}
void MMU::PowerOn() {
SPDLOG_INFO("MMU setting power on state");
}
void MMU::Reset() {
SPDLOG_INFO("MMU resetting");
}
uint8_t MMU::Read(Addr address) {
if (address > 0x4020) {
auto value = cartridge.Read(address);
SPDLOG_TRACE("MMU read from cartridge address 0x{:04X} value 0x{:02X}", address, value);
return value;
}
auto value = GetAddRef(address);
SPDLOG_TRACE("MMU read from RAM address 0x{:04X} value 0x{:02X}", address, value);
return value;
}
void MMU::Write(Addr address, uint8_t value) {
if (address > 0x4020) {
SPDLOG_TRACE("MMU delegating write to cartridge address 0x{:04X} value 0x{:02X}", address, value);
cartridge.Write(address, value);
return;
}
SPDLOG_TRACE("MMU write to RAM address 0x{:04X} value 0x{:02X}", address, value);
GetAddRef(address) = value;
}
uint8_t& MMU::GetAddRef(Addr address) {
// Check for RAM and mirrors
if (address < 0x2000) {
auto effectiveAddress = address % 0x800;
SPDLOG_TRACE("MMU referencing RAM address 0x{:04X}, effective address 0x{:04X}", address, effectiveAddress);
return ram[effectiveAddress];
} else if (address >= 0x2000 && address < 0x4000) {
auto effectiveRegister = (address - 0x2000) % 8;
SPDLOG_TRACE("MMU referencing address 0x{:04X}, PPU register {}", address, effectiveRegister);
return ppuRegisters[(address - 0x2000) % 8];
} else if (address >= 0x4000 && address < 0x4018) {
auto effectiveRegister = (address - 0x4000) % 24;
SPDLOG_TRACE("MMU referencing address 0x{:04X} to APU or I/O register {}", address, effectiveRegister);
return apuRegisters[effectiveRegister];
} else if (address >= 0x4018 && address < 0x4020) {
auto effectiveRegister = (address - 0x4018) % 8;
SPDLOG_TRACE("MMU referencing to disabled APU or I/O register {}", address);
return disabledRegisters[effectiveRegister];
} else if (address >= 0x4020) {
VERIFY(false, "MMU trying to get reference to cartridge address 0x{:04X}", address);
LIBASSERT_UNREACHABLE;
} else {
VERIFY(false, "MMU GetAddRef Unreachable {}", address);
LIBASSERT_UNREACHABLE;
}
LIBASSERT_UNREACHABLE;
}