From ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Thu, 4 Jan 2024 20:37:43 -0600 Subject: hid_core: Move hid to it's own subproject --- src/hid_core/resources/ring_lifo.h | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/hid_core/resources/ring_lifo.h (limited to 'src/hid_core/resources/ring_lifo.h') diff --git a/src/hid_core/resources/ring_lifo.h b/src/hid_core/resources/ring_lifo.h new file mode 100644 index 000000000..0816784e0 --- /dev/null +++ b/src/hid_core/resources/ring_lifo.h @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +#include "common/common_types.h" + +namespace Service::HID { + +template +struct AtomicStorage { + s64 sampling_number; + State state; +}; + +template +struct Lifo { + s64 timestamp{}; + s64 total_buffer_count = static_cast(max_buffer_size); + s64 buffer_tail{}; + s64 buffer_count{}; + std::array, max_buffer_size> entries{}; + + const AtomicStorage& ReadCurrentEntry() const { + return entries[buffer_tail]; + } + + const AtomicStorage& ReadPreviousEntry() const { + return entries[GetPreviousEntryIndex()]; + } + + std::size_t GetPreviousEntryIndex() const { + return static_cast((buffer_tail + max_buffer_size - 1) % max_buffer_size); + } + + std::size_t GetNextEntryIndex() const { + return static_cast((buffer_tail + 1) % max_buffer_size); + } + + void WriteNextEntry(const State& new_state) { + if (buffer_count < static_cast(max_buffer_size) - 1) { + buffer_count++; + } + buffer_tail = GetNextEntryIndex(); + const auto& previous_entry = ReadPreviousEntry(); + entries[buffer_tail].sampling_number = previous_entry.sampling_number + 1; + entries[buffer_tail].state = new_state; + } +}; + +} // namespace Service::HID -- cgit v1.2.3