Skip to content

Commit

Permalink
Use standard union for Div Mod (#693)
Browse files Browse the repository at this point in the history
* Cleanup ui.c div and mod

* Cleanup divRem in pauseMenu.c

* Add non matchings

* Consistent defines

* Restore comment
  • Loading branch information
KEKW555 authored Feb 17, 2024
1 parent 1a3f9e0 commit 8170c03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
20 changes: 4 additions & 16 deletions src/menu/pauseMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,26 +1232,14 @@ bool32 sub_080A5F24(void) {
return result;
}

typedef union {
struct {
s32 v1;
s32 v2;
} values;
u64 raw;
} DoubleReturnValue;

void sub_080A5F48(Item item, u32 offset) {
// this funcitons signature allows the div function to return a u64 (2x 32 bit registers)
// with the result in one register and the remainder in the other
typedef u64 DivRem(u32, u32);

s32 ammoCount;
s32 onesDigit;
s32 tensDigit;
void* dest;
u16* temp2;
u32 index;
DoubleReturnValue ret;
union SplitDWord divRem;

switch (item) {
case ITEM_BOTTLE1:
Expand Down Expand Up @@ -1281,9 +1269,9 @@ void sub_080A5F48(Item item, u32 offset) {
if (ammoCount < 0)
return;

ret.raw = ((DivRem*)Div)(ammoCount, 10); // by casting to DivRem, we can recover the remainder from the Div call
onesDigit = ret.values.v2;
tensDigit = ret.values.v1;
divRem = DivAndMod(ammoCount, 10);
onesDigit = divRem.HALF.HI;
tensDigit = divRem.HALF.LO;

if (tensDigit >= 10)
tensDigit = 9;
Expand Down
60 changes: 33 additions & 27 deletions src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,26 @@ void sub_0801C25C(void) {
}
}

void sub_0801C2F0(u32 param_1, u32 param_2) {
u32 uVar1;
register u32 rem asm("r1");
param_1 = param_1 * 0x20 + 0x6010000;
void sub_0801C2F0(u32 dest, u32 param_2) {
u32 tensDigit;
#ifdef NON_MATCHING
u32 onesDigit;
union SplitDWord divRem;
#else
FORCE_REGISTER(u32 onesDigit, r1);
FORCE_REGISTER(union SplitDWord divRem, r0);
#endif

uVar1 = Div(param_2, 10);
if (uVar1 > 9) {
uVar1 = 9;
}
dest = dest * 0x20 + 0x6010000;
divRem = DivAndMod(param_2, 10);
onesDigit = divRem.HALF_U.HI;
tensDigit = divRem.HALF_U.LO;

if (tensDigit >= 10)
tensDigit = 9;

DmaCopy32(3, (gUnk_085C4620 + uVar1 * 8), param_1, 0x8 * 4);
DmaCopy32(3, (gUnk_085C4620 + (rem + 10) * 8), param_1 + 0x20, 0x8 * 4);
DmaCopy32(3, gUnk_085C4620 + tensDigit * 0x8, dest, 0x8 * 4);
DmaCopy32(3, gUnk_085C4620 + (onesDigit + 10) * 0x8, dest + 0x20, 0x8 * 4);
}

void DrawUI(void) {
Expand Down Expand Up @@ -277,7 +285,11 @@ void RenderDigits(u32 iconVramIndex, u32 count, u32 isTextYellow, u32 digits) {
u32 digit;
vu32* ptr;
vu32* ptr2;
register u32 r1 asm("r1");
#ifdef NON_MATCHING
union SplitDWord divRem;
#else
FORCE_REGISTER(union SplitDWord divRem, r0);
#endif

puVar4 = RupeeKeyDigits;
if (isTextYellow == 0) {
Expand All @@ -287,13 +299,15 @@ void RenderDigits(u32 iconVramIndex, u32 count, u32 isTextYellow, u32 digits) {
iVar2 = iVar3 + 0x600c000;
switch (digits) {
case 3:
digit = Div(count, 100);
count = r1;
divRem = DivAndMod(count, 100);
digit = divRem.HALF_U.LO;
count = divRem.HALF_U.HI;
DmaCopy32(3, puVar4 + digit * 0x40, iVar2, 0x10 * 4);
iVar2 += 0x40;
case 2:
digit = Div(count, 10);
count = r1;
divRem = DivAndMod(count, 10);
digit = divRem.HALF_U.LO;
count = divRem.HALF_U.HI;
DmaCopy32(3, puVar4 + digit * 0x40, iVar2, 0x10 * 4);
iVar2 += 0x40;
}
Expand Down Expand Up @@ -440,21 +454,14 @@ void EraseChargeBar(void) {
}
}

typedef union {
u32 values[2];
u64 raw;
} returnValues;

typedef u64 DivRem(u32, u32);

void DrawChargeBar(void) {
bool32 tmp1;
u16* BufferPos;
returnValues ret;
// these names are almost certainly inaccurate
u32 chargeTime;
u32 chargeState;
u32 chargeFrame;
union SplitDWord divRem;

tmp1 = FALSE;
if (!(gHUD.hideFlags & HUD_HIDE_CHARGE_BAR))
Expand All @@ -476,10 +483,9 @@ void DrawChargeBar(void) {
gHUD.unk_6 = 1;
gHUD.unk_7 = chargeTime;

// this calls Div and returns the result in ret.values[0] and the remainder in ret.values[1]
ret.raw = ((DivRem*)&Div)(chargeTime, 4);
chargeState = ret.values[0];
chargeFrame = ret.values[1];
divRem = DivAndMod(chargeTime, 4);
chargeState = divRem.HALF_U.LO;
chargeFrame = divRem.HALF_U.HI;

BufferPos[0] = 0xf016;
BufferPos[11] = 0xf416;
Expand Down

0 comments on commit 8170c03

Please sign in to comment.