Revert "hoc-clk: add live vdd2, live boost clock and basic pwm dimming"

This reverts commit 15b7df8ef1.
This commit is contained in:
souldbminersmwc
2025-11-09 16:14:52 -05:00
parent 22ec140738
commit 21a3f953d7
3804 changed files with 435 additions and 570162 deletions

View File

@@ -1,257 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time::impl::util {
namespace {
constexpr inline const int DaysPerMonth[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static_assert(std::accumulate(std::begin(DaysPerMonth), std::end(DaysPerMonth), 0) == 365);
constexpr inline const std::array<int, 12> SumDaysPerMonth = [] {
std::array<int, 12> days = {};
for (size_t i = 1; i < days.size(); ++i) {
days[i] = days[i - 1] + DaysPerMonth[i - 1];
}
return days;
}();
static_assert(SumDaysPerMonth[ 0] == 0);
static_assert(SumDaysPerMonth[11] + DaysPerMonth[11] == 365);
constexpr bool IsLeapYearImpl(int year) {
if ((year % 400) == 0) {
return true;
} else if ((year % 100) == 0) {
return false;
} else if ((year % 4) == 0) {
return true;
} else {
return false;
}
}
constexpr int DateToDaysImpl(int year, int month, int day) {
/* Lightly validate input. */
AMS_ASSERT(year > 0);
AMS_ASSERT(month > 0);
AMS_ASSERT(day > 0);
/* Adjust months within range. */
year += month / 12;
month %= 12;
if (month == 0) {
month = 12;
}
AMS_ASSERT(1 <= month && month <= 12);
/* Calculate days. */
int res = (year - 1) * 365;
res += (year / 4) - (year / 100) + (year / 400);
res += SumDaysPerMonth[month - 1] + day;
/* Subtract leap day, if it hasn't happened yet. */
if (month < 3 && IsLeapYearImpl(year)) {
res -= 1;
}
/* Subtract the current day. */
res -= 1;
return res;
}
constexpr void DaysToDateImpl(int *out_year, int *out_month, int *out_day, int days) {
/* Lightly validate input. */
AMS_ASSERT(days > 0);
/* Declare unit conversion factors. */
constexpr int DaysPerYear = 365;
constexpr int DaysPerFourYears = DaysPerYear * 4 + 1;
constexpr int DaysPerCentury = DaysPerFourYears * 25 - 1;
constexpr int DaysPerFourCenturies = DaysPerCentury * 4 + 1;
/* Adjust date. */
days -= 59;
days += 365;
/* Determine various units. */
int four_centuries = days / DaysPerFourCenturies;
int four_centuries_rem = days % DaysPerFourCenturies;
if (four_centuries_rem < 0) {
four_centuries_rem += DaysPerFourCenturies;
--four_centuries;
}
int centuries = four_centuries_rem / DaysPerCentury;
int centuries_rem = four_centuries_rem % DaysPerCentury;
int four_years = centuries_rem / DaysPerFourYears;
int four_years_rem = centuries_rem % DaysPerFourYears;
int years = four_years_rem / DaysPerYear;
int years_rem = four_years_rem % DaysPerYear;
/* Adjust into range. */
int year = 400 * four_centuries + 100 * centuries + 4 * four_years + years;
int month = (5 * years_rem + 2) / 153;
int day = years_rem - (153 * month + 2) / 5 + 1;
/* Adjust in case we fell into a pathological case. */
if (years == 4 || centuries == 4) {
month = 11;
day = 29;
year -= 1;
}
/* Adjust month. */
if (month <= 9) {
month += 3;
} else {
month -= 9;
year += 1;
}
/* Set output. */
if (out_year) {
*out_year = year;
}
if (out_month) {
*out_month = month;
}
if (out_day) {
*out_day = day;
}
}
constexpr inline int EpochDays = DateToDaysImpl(1970, 1, 1);
static_assert([]() -> bool {
int year{}, month{}, day{};
DaysToDateImpl(std::addressof(year), std::addressof(month), std::addressof(day), EpochDays);
return year == 1970 && month == 1 && day == 1;
}());
}
Result GetSpanBetween(s64 *out, const SteadyClockTimePoint &from, const SteadyClockTimePoint &to) {
AMS_ASSERT(out != nullptr);
R_UNLESS(out != nullptr, time::ResultInvalidPointer());
R_UNLESS(from.source_id == to.source_id, time::ResultNotComparable());
R_UNLESS(ams::util::TrySubtractWithoutOverflow(out, to.value, from.value), time::ResultOverflowed());
R_SUCCEED();
}
bool IsValidDate(int year, int month, int day) {
return 1 <= year && 1 <= month && month <= 12 && 1 <= day && day <= GetDaysInMonth(year, month);
}
bool IsLeapYear(int year) {
AMS_ASSERT(year > 0);
return IsLeapYearImpl(year);
}
int GetDaysInMonth(int year, int month) {
/* Check pre-conditions. */
AMS_ASSERT(year > 0);
AMS_ASSERT(1 <= month && month <= 12);
if (month == 2 && IsLeapYear(year)) {
return DaysPerMonth[month - 1] + 1;
} else {
return DaysPerMonth[month - 1];
}
}
int DateToDays(int year, int month, int day) {
return DateToDaysImpl(year, month, day);
}
void DaysToDate(int *out_year, int *out_month, int *out_day, int days) {
DaysToDateImpl(out_year, out_month, out_day, days);
}
CalendarTime ToCalendarTimeInUtc(const PosixTime &posix_time) {
constexpr s64 SecondsPerDay = TimeSpan::FromDays(1).GetSeconds();
constexpr s64 SecondsPerHour = TimeSpan::FromHours(1).GetSeconds();
constexpr s64 SecondsPerMinute = TimeSpan::FromMinutes(1).GetSeconds();
/* Get year/month/day. */
int year, month, day;
DaysToDate(std::addressof(year), std::addressof(month), std::addressof(day), static_cast<int>(posix_time.value / SecondsPerDay) + EpochDays);
/* Handle negative posix times. */
s64 posix_abs = posix_time.value >= 0 ? posix_time.value : -1 * posix_time.value;
s64 posix_rem = posix_abs % SecondsPerDay;
if (posix_time.value < 0) {
posix_rem *= -1;
}
/* Adjust remainder if negative. */
if (posix_rem < 0) {
if ((--day) <= 0) {
if ((--month) <= 0) {
--year;
month = 12;
}
day = time::impl::util::GetDaysInMonth(year, month);
}
posix_rem += SecondsPerDay;
}
const int hour = posix_rem / SecondsPerHour;
posix_rem %= SecondsPerHour;
const int minute = posix_rem / SecondsPerMinute;
posix_rem %= SecondsPerMinute;
const int second = posix_rem;
return CalendarTime {
.year = static_cast<s16>(year),
.month = static_cast<s8>(month),
.day = static_cast<s8>(day),
.hour = static_cast<s8>(hour),
.minute = static_cast<s8>(minute),
.second = static_cast<s8>(second),
};
}
PosixTime ToPosixTimeFromUtc(const CalendarTime &calendar_time) {
/* Validate pre-conditions. */
AMS_ASSERT(IsValidDate(calendar_time.year, calendar_time.month, calendar_time.day));
AMS_ASSERT(0 <= calendar_time.hour && calendar_time.hour <= 23);
AMS_ASSERT(0 <= calendar_time.minute && calendar_time.minute <= 59);
AMS_ASSERT(0 <= calendar_time.second && calendar_time.second <= 59);
/* Extract/convert fields. */
const s64 days = static_cast<s64>(time::impl::util::DateToDays(calendar_time.year, calendar_time.month, calendar_time.day)) - EpochDays;
const s64 hours = calendar_time.hour;
const s64 minutes = calendar_time.minute;
const s64 seconds = calendar_time.second;
return PosixTime { .value = ((((days * 24) + hours) * 60) + minutes) * 60 + seconds };
}
}

View File

@@ -1,122 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#if defined(ATMOSPHERE_OS_HORIZON)
extern "C" {
extern TimeServiceType __nx_time_service_type;
}
#endif
namespace ams::time {
namespace {
enum InitializeMode {
InitializeMode_None,
InitializeMode_Normal,
InitializeMode_Menu,
InitializeMode_System,
InitializeMode_Repair,
InitializeMode_SystemUser,
};
constinit u32 g_initialize_count = 0;
constinit InitializeMode g_initialize_mode = InitializeMode_None;
constinit os::SdkMutex g_initialize_mutex;
Result InitializeImpl(InitializeMode mode) {
std::scoped_lock lk(g_initialize_mutex);
if (g_initialize_count > 0) {
AMS_ABORT_UNLESS(mode == g_initialize_mode);
g_initialize_count++;
R_SUCCEED();
}
#if defined(ATMOSPHERE_OS_HORIZON)
switch (mode) {
case InitializeMode_Normal: __nx_time_service_type = ::TimeServiceType_User; break;
case InitializeMode_Menu: __nx_time_service_type = ::TimeServiceType_Menu; break;
case InitializeMode_System: __nx_time_service_type = ::TimeServiceType_System; break;
case InitializeMode_Repair: __nx_time_service_type = ::TimeServiceType_Repair; break;
case InitializeMode_SystemUser: __nx_time_service_type = ::TimeServiceType_SystemUser; break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
R_TRY(::timeInitialize());
#else
// TODO: Real AMS_ABORT("TODO");
#endif
g_initialize_count++;
g_initialize_mode = mode;
R_SUCCEED();
}
}
Result Initialize() {
R_RETURN(InitializeImpl(InitializeMode_Normal));
}
Result InitializeForSystem() {
R_RETURN(InitializeImpl(InitializeMode_System));
}
Result InitializeForSystemUser() {
if (hos::GetVersion() >= hos::Version_9_0_0) {
R_RETURN(InitializeImpl(InitializeMode_SystemUser));
} else {
R_RETURN(InitializeImpl(InitializeMode_Normal));
}
}
Result Finalize() {
std::scoped_lock lk(g_initialize_mutex);
if (g_initialize_count > 0) {
if ((--g_initialize_count) == 0) {
#if defined(ATMOSPHERE_OS_HORIZON)
::timeExit();
#else
// TODO: Real AMS_ABORT("TODO");
#endif
g_initialize_mode = InitializeMode_None;
}
}
R_SUCCEED();
}
bool IsInitialized() {
std::scoped_lock lk(g_initialize_mutex);
return g_initialize_count > 0;
}
bool IsValidDate(int year, int month, int day) {
return impl::util::IsValidDate(year, month, day);
}
Result GetElapsedSecondsBetween(s64 *out, const SteadyClockTimePoint &from, const SteadyClockTimePoint &to) {
R_RETURN(impl::util::GetSpanBetween(out, from, to));
}
}

View File

@@ -1,46 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time {
bool CalendarTime::IsValid() const {
return time::IsValidDate(this->year, this->month, this->day);
}
CalendarTime &CalendarTime::operator+=(const TimeSpan &ts) {
*this = ToCalendarTimeInUtc(ToPosixTimeFromUtc(*this) + ts);
return *this;
}
CalendarTime &CalendarTime::operator-=(const TimeSpan &ts) {
*this = ToCalendarTimeInUtc(ToPosixTimeFromUtc(*this) - ts);
return *this;
}
CalendarTime operator+(const CalendarTime &lhs, const TimeSpan &rhs) {
return ToCalendarTimeInUtc(ToPosixTimeFromUtc(lhs) + rhs);
}
CalendarTime operator-(const CalendarTime &lhs, const TimeSpan &rhs) {
return ToCalendarTimeInUtc(ToPosixTimeFromUtc(lhs) - rhs);
}
TimeSpan operator-(const CalendarTime &lhs, const CalendarTime &rhs) {
return ToPosixTimeFromUtc(lhs) - ToPosixTimeFromUtc(rhs);
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time {
Result StandardNetworkSystemClock::GetCurrentTime(PosixTime *out) {
#if defined(ATMOSPHERE_OS_HORIZON)
static_assert(sizeof(*out) == sizeof(u64));
R_RETURN(::timeGetCurrentTime(::TimeType_NetworkSystemClock, reinterpret_cast<u64 *>(out)));
#else
AMS_UNUSED(out);
AMS_ABORT("TODO");
#endif
}
StandardNetworkSystemClock::time_point StandardNetworkSystemClock::now() {
PosixTime posix_time = {};
if (R_FAILED(GetCurrentTime(std::addressof(posix_time)))) {
posix_time.value = 0;
}
return time_point(duration(posix_time.value));
}
std::time_t StandardNetworkSystemClock::to_time_t(const StandardNetworkSystemClock::time_point &t) {
return static_cast<std::time_t>(std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count());
}
StandardNetworkSystemClock::time_point StandardNetworkSystemClock::from_time_t(std::time_t t) {
return time_point(duration(t));
}
/* TODO: Result StandardNetworkSystemClock::GetSystemClockContext(SystemClockContext *out); */
}

View File

@@ -1,58 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time {
Result GetStandardSteadyClockCurrentTimePoint(SteadyClockTimePoint *out) {
#if defined(ATMOSPHERE_OS_HORIZON)
static_assert(sizeof(*out) == sizeof(::TimeSteadyClockTimePoint));
R_RETURN(::timeGetStandardSteadyClockTimePoint(reinterpret_cast<::TimeSteadyClockTimePoint *>(out)));
#else
AMS_UNUSED(out);
AMS_ABORT("TODO");
#endif
}
TimeSpan GetStandardSteadyClockInternalOffset() {
TimeSpanType offset;
#if defined(ATMOSPHERE_OS_HORIZON)
static_assert(sizeof(TimeSpanType) == sizeof(s64));
R_ABORT_UNLESS(::timeGetStandardSteadyClockInternalOffset(reinterpret_cast<s64 *>(std::addressof(offset))));
#else
AMS_UNUSED(offset);
AMS_ABORT("TODO");
#endif
return offset;
}
Result StandardSteadyClock::GetCurrentTimePoint(SteadyClockTimePoint *out) {
R_RETURN(GetStandardSteadyClockCurrentTimePoint(out));
}
StandardSteadyClock::time_point StandardSteadyClock::now() {
SteadyClockTimePoint steady_clock_time_point = {0, util::InvalidUuid};
if (R_FAILED(StandardSteadyClock::GetCurrentTimePoint(std::addressof(steady_clock_time_point)))) {
steady_clock_time_point.value = 0;
steady_clock_time_point.source_id = util::InvalidUuid;
}
return StandardSteadyClock::time_point(StandardSteadyClock::duration(steady_clock_time_point.value));
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time {
Result StandardUserSystemClock::GetCurrentTime(PosixTime *out) {
#if defined(ATMOSPHERE_OS_HORIZON)
static_assert(sizeof(*out) == sizeof(u64));
R_RETURN(::timeGetCurrentTime(::TimeType_UserSystemClock, reinterpret_cast<u64 *>(out)));
#else
AMS_UNUSED(out);
AMS_ABORT("TODO");
#endif
}
StandardUserSystemClock::time_point StandardUserSystemClock::now() {
PosixTime posix_time = {};
if (R_FAILED(GetCurrentTime(std::addressof(posix_time)))) {
posix_time.value = 0;
}
return time_point(duration(posix_time.value));
}
std::time_t StandardUserSystemClock::to_time_t(const StandardUserSystemClock::time_point &t) {
return static_cast<std::time_t>(std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count());
}
StandardUserSystemClock::time_point StandardUserSystemClock::from_time_t(std::time_t t) {
return time_point(duration(t));
}
/* TODO: Result StandardUserSystemClock::GetSystemClockContext(SystemClockContext *out); */
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::time {
CalendarTime ToCalendarTimeInUtc(const PosixTime &posix_time) {
return impl::util::ToCalendarTimeInUtc(posix_time);
}
PosixTime ToPosixTimeFromUtc(const CalendarTime &calendar_time) {
return impl::util::ToPosixTimeFromUtc(calendar_time);
}
}