summaryrefslogtreecommitdiffstats
path: root/src/core/device_memory.cpp
blob: 1e418754631c954b218ddcce222ea1aa4db4e16e (plain) (blame)
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
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#ifdef _WIN32
#include <windows.h>
#endif

#include "common/assert.h"
#include "core/core.h"
#include "core/device_memory.h"
#include "core/memory.h"

namespace Core {

constexpr u64 DramSize{4ULL * 1024 * 1024 * 1024};

DeviceMemory::DeviceMemory(System& system) : system{system} {
#ifdef _WIN32
    base = static_cast<u8*>(
        VirtualAlloc(nullptr,                                    // System selects address
                     DramSize,                                   // Size of allocation
                     MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, // Allocate reserved pages
                     PAGE_READWRITE));                           // Protection = no access
#else
    physical_memory.resize(DramSize);
    base = physical_memory.data();
#endif
}

DeviceMemory::~DeviceMemory() {
#ifdef _WIN32
    ASSERT(VirtualFree(base, DramSize, MEM_RELEASE));
#endif
}

PAddr DeviceMemory::GetPhysicalAddr(VAddr addr) {
    u8* pointer{system.Memory().GetPointer(addr)};
    ASSERT(pointer);
    const uintptr_t offset{static_cast<uintptr_t>(pointer - GetPointer(DramMemoryMap::Base))};
    return DramMemoryMap::Base + offset;
}

u8* DeviceMemory::GetPointer(PAddr addr) {
    ASSERT(addr >= DramMemoryMap::Base);
    ASSERT(addr < DramMemoryMap::Base + DramSize);
    return base + (addr - DramMemoryMap::Base);
}

} // namespace Core