Skip to content

Commit

Permalink
Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed May 9, 2024
1 parent ccf5e78 commit eb3cb90
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 56 deletions.
71 changes: 40 additions & 31 deletions source/utils/DrawUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/utils/DrawUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
44 changes: 20 additions & 24 deletions source/utils/config/ConfigUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ 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;
}
auto regs = (uint32_t *) __OSPhysicalToEffectiveUncached(0xc200000);
return regs[index];
}

void __writeToReg(uint32_t index, uint32_t val) {
static void __WriteReg32(uint32_t index, uint32_t val) {
if (OSIsECOMode()) {
return;
}
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit eb3cb90

Please # to comment.