From eb3cb908a781b3bff0e64404e9672a4552bc5679 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 10 May 2024 00:08:47 +0200 Subject: [PATCH] Simplify implementation --- source/utils/DrawUtils.cpp | 71 ++++++++++++++++------------- source/utils/DrawUtils.h | 2 +- source/utils/config/ConfigUtils.cpp | 44 ++++++++---------- 3 files changed, 61 insertions(+), 56 deletions(-) diff --git a/source/utils/DrawUtils.cpp b/source/utils/DrawUtils.cpp index ff436ae..e7cd9e0 100644 --- a/source/utils/DrawUtils.cpp +++ b/source/utils/DrawUtils.cpp @@ -29,37 +29,46 @@ static Color font_col(0xFFFFFFFF); #define __SetDCPitchReg ((void (*)(uint32_t, uint32_t))(0x101C400 + 0x1e714)) -void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_, uint32_t drcSize_, uint32_t tv_width, uint32_t tv_height) { - DrawUtils::tvBuffer = (uint8_t *) tvBuffer_; - DrawUtils::tvSize = tvSize_; - DrawUtils::drcBuffer = (uint8_t *) drcBuffer_; - DrawUtils::drcSize = drcSize_; - DrawUtils::usedTVWidth = tv_width; - - __SetDCPitchReg(SCREEN_TV, tv_width); - switch ((GX2TVRenderMode) gStoredTVBuffer.mode) { - case GX2_TV_RENDER_MODE_DISABLED: - break; - case GX2_TV_RENDER_MODE_STANDARD_480P: - DrawUtils::usedTVWidth = 640; - __SetDCPitchReg(SCREEN_TV, 640); - DrawUtils::usedTVScale = 0.75f; - break; - case GX2_TV_RENDER_MODE_WIDE_480P: - DrawUtils::usedTVWidth = 896; - __SetDCPitchReg(SCREEN_TV, 896); - DrawUtils::usedTVScale = 1.0f; - break; - case GX2_TV_RENDER_MODE_WIDE_720P: - DrawUtils::usedTVWidth = 1280; - __SetDCPitchReg(SCREEN_TV, 1280); - DrawUtils::usedTVScale = 0.75f; - break; - case GX2_TV_RENDER_MODE_WIDE_1080P: - DrawUtils::usedTVWidth = 1920; - __SetDCPitchReg(SCREEN_TV, 1920); - DrawUtils::usedTVScale = 1.125f; - break; +extern "C" uint32_t __OSPhysicalToEffectiveUncached(uint32_t); + +static uint32_t __ReadReg32(uint32_t index) { + if (OSIsECOMode()) { + return 0; + } + auto regs = (uint32_t *) __OSPhysicalToEffectiveUncached(0xc200000); + return regs[index]; +} + + +void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_, uint32_t drcSize_) { + DrawUtils::tvBuffer = (uint8_t *) tvBuffer_; + DrawUtils::tvSize = tvSize_; + DrawUtils::drcBuffer = (uint8_t *) drcBuffer_; + DrawUtils::drcSize = drcSize_; + + auto tv_width = __ReadReg32(0x184d + SCREEN_TV * 0x200); + + if (tv_width == 640) { + DrawUtils::usedTVWidth = 640; + __SetDCPitchReg(SCREEN_TV, 640); + DrawUtils::usedTVScale = 0.75f; + } else if (tv_width == 854) { + DrawUtils::usedTVWidth = 896; + __SetDCPitchReg(SCREEN_TV, 896); + DrawUtils::usedTVScale = 1.0f; + } else if (tv_width == 1280) { + DrawUtils::usedTVWidth = 1280; + __SetDCPitchReg(SCREEN_TV, 1280); + DrawUtils::usedTVScale = 0.75f; + } else if (tv_width == 1920) { + DrawUtils::usedTVWidth = 1920; + __SetDCPitchReg(SCREEN_TV, 1920); + DrawUtils::usedTVScale = 1.125f; + } else { + DrawUtils::usedTVWidth = tv_width; + __SetDCPitchReg(SCREEN_TV, tv_width); + DrawUtils::usedTVScale = 1.0f; + DEBUG_FUNCTION_LINE_WARN("Unknown tv width detected, config menu might not show properly"); } } diff --git a/source/utils/DrawUtils.h b/source/utils/DrawUtils.h index 597f6a2..229c748 100644 --- a/source/utils/DrawUtils.h +++ b/source/utils/DrawUtils.h @@ -30,7 +30,7 @@ union Color { class DrawUtils { public: - static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize, uint32_t tv_width, uint32_t tv_height); + static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize); static void beginDraw(); diff --git a/source/utils/config/ConfigUtils.cpp b/source/utils/config/ConfigUtils.cpp index c1d25c7..efaefa2 100644 --- a/source/utils/config/ConfigUtils.cpp +++ b/source/utils/config/ConfigUtils.cpp @@ -228,7 +228,7 @@ void ConfigUtils::displayMenu() { extern "C" uint32_t __OSPhysicalToEffectiveUncached(uint32_t); -uint32_t __readFromReg(uint32_t index) { +static uint32_t __ReadReg32(uint32_t index) { if (OSIsECOMode()) { return 0; } @@ -236,7 +236,7 @@ uint32_t __readFromReg(uint32_t index) { return regs[index]; } -void __writeToReg(uint32_t index, uint32_t val) { +static void __WriteReg32(uint32_t index, uint32_t val) { if (OSIsECOMode()) { return; } @@ -252,23 +252,19 @@ void ConfigUtils::openConfigMenu() { gOnlyAcceptFromThread = OSGetCurrentThread(); bool wasHomeButtonMenuEnabled = OSIsHomeButtonMenuEnabled(); - auto tv_width = __readFromReg(0x184d + SCREEN_TV * 0x200); - auto tv_height = __readFromReg(0x184e + SCREEN_TV * 0x200); - - // Save TV DC reg values OSScreenInit is overwriting // render reg - auto tvRender1 = __readFromReg(0x1841 + SCREEN_TV * 0x200); - auto tvRender2 = __readFromReg(0x1840 + SCREEN_TV * 0x200); + auto tvRender1 = __ReadReg32(0x1841 + SCREEN_TV * 0x200); + auto tvRender2 = __ReadReg32(0x1840 + SCREEN_TV * 0x200); // pitch - auto tvPitch1 = __readFromReg(0x1848 + SCREEN_TV * 0x200); - auto tvPitch2 = __readFromReg(0x1866 + SCREEN_TV * 0x200); + auto tvPitch1 = __ReadReg32(0x1848 + SCREEN_TV * 0x200); + auto tvPitch2 = __ReadReg32(0x1866 + SCREEN_TV * 0x200); // render reg - auto drcRender1 = __readFromReg(0x1841 + SCREEN_DRC * 0x200); - auto drcRender2 = __readFromReg(0x1840 + SCREEN_DRC * 0x200); + auto drcRender1 = __ReadReg32(0x1841 + SCREEN_DRC * 0x200); + auto drcRender2 = __ReadReg32(0x1840 + SCREEN_DRC * 0x200); // pitch - auto drcPitch1 = __readFromReg(0x1848 + SCREEN_DRC * 0x200); - auto drcPitch2 = __readFromReg(0x1866 + SCREEN_DRC * 0x200); + auto drcPitch1 = __ReadReg32(0x1848 + SCREEN_DRC * 0x200); + auto drcPitch2 = __ReadReg32(0x1866 + SCREEN_DRC * 0x200); OSScreenInit(); @@ -324,7 +320,7 @@ void ConfigUtils::openConfigMenu() { OSScreenEnableEx(SCREEN_TV, 1); OSScreenEnableEx(SCREEN_DRC, 1); - DrawUtils::initBuffers(screenbuffer0, screen_buf0_size, screenbuffer1, screen_buf1_size, tv_width, tv_height); + DrawUtils::initBuffers(screenbuffer0, screen_buf0_size, screenbuffer1, screen_buf1_size); if (!DrawUtils::initFont()) { DEBUG_FUNCTION_LINE_ERR("Failed to init Font"); goto error_exit; @@ -352,15 +348,15 @@ void ConfigUtils::openConfigMenu() { error_exit: // Restore DC regs - __writeToReg(0x1841 + SCREEN_TV * 0x200, tvRender1); - __writeToReg(0x1840 + SCREEN_TV * 0x200, tvRender2); - __writeToReg(0x1848 + SCREEN_TV * 0x200, tvPitch1); - __writeToReg(0x1866 + SCREEN_TV * 0x200, tvPitch2); - - __writeToReg(0x1841 + SCREEN_DRC * 0x200, drcRender1); - __writeToReg(0x1840 + SCREEN_DRC * 0x200, drcRender2); - __writeToReg(0x1848 + SCREEN_DRC * 0x200, drcPitch1); - __writeToReg(0x1866 + SCREEN_DRC * 0x200, drcPitch2); + __WriteReg32(0x1841 + SCREEN_TV * 0x200, tvRender1); + __WriteReg32(0x1840 + SCREEN_TV * 0x200, tvRender2); + __WriteReg32(0x1848 + SCREEN_TV * 0x200, tvPitch1); + __WriteReg32(0x1866 + SCREEN_TV * 0x200, tvPitch2); + + __WriteReg32(0x1841 + SCREEN_DRC * 0x200, drcRender1); + __WriteReg32(0x1840 + SCREEN_DRC * 0x200, drcRender2); + __WriteReg32(0x1848 + SCREEN_DRC * 0x200, drcPitch1); + __WriteReg32(0x1866 + SCREEN_DRC * 0x200, drcPitch2); if (!skipScreen0Free && screenbuffer0) {