summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/psc/time/shared_memory.h
blob: b7bd00fc1da6f828a2cccd15a8a2965c4e51446a (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>

#include "common/common_types.h"
#include "core/hle/service/psc/time/common.h"

namespace Core {
class System;
}

namespace Kernel {
class KSharedMemory;
}

namespace Service::PSC::Time {

template <typename T>
struct LockFreeAtomicType {
    u32 m_counter;
    std::array<T, 2> m_value;
};

struct SharedMemoryStruct {
    LockFreeAtomicType<SteadyClockTimePoint> steady_time_points;
    LockFreeAtomicType<SystemClockContext> local_system_clock_contexts;
    LockFreeAtomicType<SystemClockContext> network_system_clock_contexts;
    LockFreeAtomicType<bool> automatic_corrections;
    LockFreeAtomicType<ContinuousAdjustmentTimePoint> continuous_adjustment_time_points;
    std::array<char, 0xEB8> pad0148;
};
static_assert(offsetof(SharedMemoryStruct, steady_time_points) == 0x0,
              "steady_time_points are in the wrong place!");
static_assert(offsetof(SharedMemoryStruct, local_system_clock_contexts) == 0x38,
              "local_system_clock_contexts are in the wrong place!");
static_assert(offsetof(SharedMemoryStruct, network_system_clock_contexts) == 0x80,
              "network_system_clock_contexts are in the wrong place!");
static_assert(offsetof(SharedMemoryStruct, automatic_corrections) == 0xC8,
              "automatic_corrections are in the wrong place!");
static_assert(offsetof(SharedMemoryStruct, continuous_adjustment_time_points) == 0xD0,
              "continuous_adjustment_time_points are in the wrong place!");
static_assert(sizeof(SharedMemoryStruct) == 0x1000,
              "Time's SharedMemoryStruct has the wrong size!");
static_assert(std::is_trivial_v<SharedMemoryStruct>);

class SharedMemory {
public:
    explicit SharedMemory(Core::System& system);

    Kernel::KSharedMemory& GetKSharedMemory() {
        return m_k_shared_memory;
    }

    void SetLocalSystemContext(const SystemClockContext& context);
    void SetNetworkSystemContext(const SystemClockContext& context);
    void SetSteadyClockTimePoint(ClockSourceId clock_source_id, s64 time_diff);
    void SetContinuousAdjustment(const ContinuousAdjustmentTimePoint& time_point);
    void SetAutomaticCorrection(bool automatic_correction);
    void UpdateBaseTime(s64 time);

private:
    Core::System& m_system;
    Kernel::KSharedMemory& m_k_shared_memory;
    SharedMemoryStruct* m_shared_memory_ptr;
};

} // namespace Service::PSC::Time