From b114559a8dfd8ef914ea846d90660e52291f305f Mon Sep 17 00:00:00 2001 From: citrusbolt Date: Mon, 19 Feb 2024 15:15:47 -0700 Subject: [PATCH] Document `gflib` and `libmagb` other than `save.c` --- payload/include/gflib/bg.h | 14 ++++++------- payload/include/gflib/init.h | 4 ++-- payload/include/gflib/sprite.h | 12 +++++------ payload/include/gflib/text.h | 2 +- payload/include/libpmagb/berry.h | 2 +- payload/src/gflib/bg.c | 35 ++++++++++++++++---------------- payload/src/gflib/init.c | 14 +++++++++---- payload/src/gflib/keys.c | 6 ++++-- payload/src/gflib/sound.c | 27 ++++++++++++------------ payload/src/gflib/text.c | 14 +++++++++---- payload/src/libpmagb/agb_rom.c | 11 ++++++++++ payload/src/libpmagb/berry.c | 21 ++++++++++--------- 12 files changed, 95 insertions(+), 67 deletions(-) diff --git a/payload/include/gflib/bg.h b/payload/include/gflib/bg.h index 1d22b9b..e6bf82b 100644 --- a/payload/include/gflib/bg.h +++ b/payload/include/gflib/bg.h @@ -9,12 +9,12 @@ extern bool8 gBgTilemapBufferTransferScheduled[]; void ResetGpuBuffers(void); void DoGpuUpdateAndTilemapTransfers(void); -void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, const u16 * src); -void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 * dest); -void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int height, int paletteNum); -void SetBgTilemapBufferTileAt(int bgNum, int x, int y, u16 tileNum); -void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 tileNum); -void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width, int height, int destLeft, int destTop); -void SetBgPos(int bgNum, s32 x, s32 y); +void CopyToBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, const u16 * src); +void CopyFromBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 * dest); +void SetBgTilemapBufferPaletteRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u32 paletteNum); +void SetBgTilemapBufferTileAt(u32 bgNum, u32 x, u32 y, u16 tileNum); +void FillBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 tileNum); +void CopyRectWithinBgTilemapBuffer(u32 bgNum, u32 srcLeft, u32 srcTop, s32 width, u32 height, u32 destLeft, u32 destTop); +void SetBgPos(u32 bgNum, s32 x, s32 y); #endif //GFLIB_BG_H diff --git a/payload/include/gflib/init.h b/payload/include/gflib/init.h index c8f1fc6..bba995d 100644 --- a/payload/include/gflib/init.h +++ b/payload/include/gflib/init.h @@ -9,9 +9,9 @@ void AgbMain(void); extern IntrFunc gIntrTable[14u]; -void SetIntrFunc(int i, IntrFunc func); +void SetIntrFunc(u32 i, IntrFunc func); void SetVBlankCallback(IntrFunc cb); u32 GetFrameTotal(void); -void DelayFrames(u32 a0); +void DelayFrames(u32 frames); #endif //GFLIB_INIT_H diff --git a/payload/include/gflib/sprite.h b/payload/include/gflib/sprite.h index 58355e5..b1cf8de 100644 --- a/payload/include/gflib/sprite.h +++ b/payload/include/gflib/sprite.h @@ -54,11 +54,11 @@ void ResetSprites(void); void UpdateSprites(void); void DoOamBufferTransfer(void); struct Sprite * AddSprite(s32 x, s32 y, const struct Subsprites * subsprites); -void MoveSpriteToHead(struct Sprite * a0); -void SetSpritePos(struct Sprite * r0, s32 r1, s32 r2); -void AddSpritePos(struct Sprite * r0, s32 r1, s32 r2); -void SetSpritePaletteNum(struct Sprite * r0, s32 r1); -void SetSpriteTileOffset(struct Sprite * r0, s32 r1); -void SetSpriteInvisible(struct Sprite * r0, s32 r1); +void MoveSpriteToHead(struct Sprite * sprite); +void SetSpritePos(struct Sprite * sprite, s32 x, s32 y); +void AddSpritePos(struct Sprite * sprite, s32 x, s32 y); +void SetSpritePaletteNum(struct Sprite * sprite, s32 paletteNum); +void SetSpriteTileOffset(struct Sprite * sprite, s32 tileOffset); +void SetSpriteInvisible(struct Sprite * sprite, s32 invisible); #endif //GFLIB_SPRITE_H diff --git a/payload/include/gflib/text.h b/payload/include/gflib/text.h index 76e1859..d5a8f55 100644 --- a/payload/include/gflib/text.h +++ b/payload/include/gflib/text.h @@ -34,7 +34,7 @@ void DrawGlyphOnWindow_NoShadow(struct Window *window, u32 glyphId, u32 fgColor) void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColor, u32 shadowColor); struct Window *AddWindow(u32 windowId, const struct Window *template); void GenerateFontHalfrowLookupTable(u32 * buffer); -void ClearWindowCharBuffer(struct Window * window, u32 a1); +void ClearWindowCharBuffer(struct Window * window, u32 tile); void FillWindowCharBufferRect(struct Window * window, s32 x, s32 y, s32 width, s32 height, u32 fillValue); void SetTextColor(struct Window * window, u8 fg, u8 shadow); void TextWindowNextLine(struct Window * window); diff --git a/payload/include/libpmagb/berry.h b/payload/include/libpmagb/berry.h index 3ddbe4f..450a8c2 100644 --- a/payload/include/libpmagb/berry.h +++ b/payload/include/libpmagb/berry.h @@ -5,6 +5,6 @@ u32 GetEnigmaBerryChecksum(struct EnigmaBerry * enigmaBerry); bool32 IsEnigmaBerryValid(void); const struct Berry *GetBerryInfo(u8 berry); const u8 *ItemId_GetName(u16 itemId); -void CopyItemName(u32 itemId, u8 *string, const u8 * berry_str); +void CopyItemName(u32 itemId, u8 *string, const u8 * berryString); #endif //PMAGB_BERRY_H diff --git a/payload/src/gflib/bg.c b/payload/src/gflib/bg.c index b1ded6d..21e3d29 100644 --- a/payload/src/gflib/bg.c +++ b/payload/src/gflib/bg.c @@ -1,9 +1,9 @@ #include "global.h" #include "gflib/bg.h" -u16 gBgHofsBuffer[4]; -u16 gBgVofsBuffer[4]; -bool8 gBgTilemapBufferTransferScheduled[4]; +u16 gBgHofsBuffer[NUM_BACKGROUNDS]; +u16 gBgVofsBuffer[NUM_BACKGROUNDS]; +bool8 gBgTilemapBufferTransferScheduled[NUM_BACKGROUNDS]; u32 filler1; u32 filler2; u32 filler3; @@ -13,8 +13,9 @@ void DoGpuUpdateAndTilemapTransfers(void) s32 i; u16 bgCnt; void * screenBase; - size_t size; + u32 size; s32 r2; + REG_BG0HOFS = gBgHofsBuffer[0]; REG_BG1HOFS = gBgHofsBuffer[1]; REG_BG2HOFS = gBgHofsBuffer[2]; @@ -23,7 +24,7 @@ void DoGpuUpdateAndTilemapTransfers(void) REG_BG1VOFS = gBgVofsBuffer[1]; REG_BG2VOFS = gBgVofsBuffer[2]; REG_BG3VOFS = gBgVofsBuffer[3]; - for (i = 0; i < 4; i++) + for (i = 0; i < NUM_BACKGROUNDS; i++) { if (gBgTilemapBufferTransferScheduled[i]) { @@ -47,9 +48,9 @@ void ResetGpuBuffers(void) gBgTilemapBufferTransferScheduled[0] = 0; } -void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, const u16 * src) +void CopyToBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, const u16 * src) { - int x; + s32 x; u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1)); while (height) @@ -63,9 +64,9 @@ void CopyToBgTilemapBufferRect(int bgNum, int left, int top, int width, int heig } } -void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 * dest) +void CopyFromBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 * dest) { - int x; + s32 x; u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1)); while (height) @@ -79,9 +80,9 @@ void CopyFromBgTilemapBufferRect(int bgNum, int left, int top, int width, int he } } -void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int height, int paletteNum) +void SetBgTilemapBufferPaletteRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u32 paletteNum) { - int x; + s32 x; u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1)); while (height) @@ -96,15 +97,15 @@ void SetBgTilemapBufferPaletteRect(int bgNum, int left, int top, int width, int } } -void SetBgTilemapBufferTileAt(int bgNum, int x, int y, u16 tileNum) +void SetBgTilemapBufferTileAt(u32 bgNum, u32 x, u32 y, u16 tileNum) { u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (y << 6) + (x << 1)); *ptr = tileNum; } -void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height, u16 tileNum) +void FillBgTilemapBufferRect(u32 bgNum, u32 left, u32 top, s32 width, u32 height, u16 tileNum) { - int x; + s32 x; u16 * ptr = (void *)(gBGTilemapBuffers + (bgNum << 12) + (top << 6) + (left << 1)); while (height) @@ -118,9 +119,9 @@ void FillBgTilemapBufferRect(int bgNum, int left, int top, int width, int height } } -void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width, int height, int destLeft, int destTop) +void CopyRectWithinBgTilemapBuffer(u32 bgNum, u32 srcLeft, u32 srcTop, s32 width, u32 height, u32 destLeft, u32 destTop) { - int x; + s32 x; const u16 * srcPtr; u16 * destPtr; @@ -139,7 +140,7 @@ void CopyRectWithinBgTilemapBuffer(int bgNum, int srcLeft, int srcTop, int width } } -void SetBgPos(int bgNum, s32 x, s32 y) +void SetBgPos(u32 bgNum, s32 x, s32 y) { gBgHofsBuffer[bgNum] = x; gBgVofsBuffer[bgNum] = y; diff --git a/payload/src/gflib/init.c b/payload/src/gflib/init.c index 5514d64..082ad96 100644 --- a/payload/src/gflib/init.c +++ b/payload/src/gflib/init.c @@ -37,9 +37,9 @@ u32 GetFrameTotal(void) return gVBlankCounter; } -void DelayFrames(u32 a0) +void DelayFrames(u32 frames) { - for (; a0 != 0; a0--) + for (; frames != 0; frames--) { UpdateSprites(); VBlankIntrWait(); @@ -51,17 +51,20 @@ void DelayFrames(u32 a0) void InitIntr(void) { - int i; + u32 i; + gVBlankCallback = NULL; + for (i = 0; i < 14u; i++) gIntrTable[i] = IntrDummy; + if (VBlankIntr != NULL) gIntrTable[1] = VBlankIntr; else gIntrTable[1] = IntrDummy; } -void SetIntrFunc(int i, IntrFunc func) +void SetIntrFunc(u32 i, IntrFunc func) { if (func != NULL) gIntrTable[i] = func; @@ -81,12 +84,15 @@ void VBlankIntr(void) { if (EnableSoundVSync(0)) m4aSoundVSync(); + DoOamBufferTransfer(); DoGpuUpdateAndTilemapTransfers(); gVBlankCounter++; INTR_CHECK = INTR_FLAG_VBLANK; + if (gVBlankCallback != NULL) gVBlankCallback(); + if (EnableSoundVSync(1)) m4aSoundMain(); } diff --git a/payload/src/gflib/keys.c b/payload/src/gflib/keys.c index 1871d2e..f2102c4 100644 --- a/payload/src/gflib/keys.c +++ b/payload/src/gflib/keys.c @@ -12,9 +12,10 @@ static u16 sKeyRepeatTimer; void ReadKeys(void) { u32 keyInput; - u16 * prevKeys_p = &gHeldKeys; - u16 newKeys = (keyInput = REG_KEYINPUT ^ KEYS_MASK) & ~*prevKeys_p; + u16 * prevKeys = &gHeldKeys; + u16 newKeys = (keyInput = REG_KEYINPUT ^ KEYS_MASK) & ~*prevKeys; gNewKeys = newKeys; + if (gHeldKeys != keyInput) { gNewAndRepeatedKeys = keyInput; @@ -29,6 +30,7 @@ void ReadKeys(void) { gNewAndRepeatedKeys = 0; } + gHeldKeys = keyInput; } diff --git a/payload/src/gflib/sound.c b/payload/src/gflib/sound.c index 4871cd0..5da2b48 100644 --- a/payload/src/gflib/sound.c +++ b/payload/src/gflib/sound.c @@ -1,27 +1,28 @@ #include "global.h" #include "gflib/sound.h" -static u8 gUnknown_02021368; -static bool8 isSoundVsync; +static u8 sIsSoundVSyncPaused; +static bool8 sIsSoundVsync; void InitSound(void) { - gUnknown_02021368 = 0; - isSoundVsync = 0; + sIsSoundVSyncPaused = 0; + sIsSoundVsync = FALSE; m4aSoundInit(); } void PauseSoundVSync(void) { - gUnknown_02021368 = 1; - isSoundVsync = 0; + sIsSoundVSyncPaused = 1; + sIsSoundVsync = FALSE; } -bool32 EnableSoundVSync(u32 a0) +bool32 EnableSoundVSync(u32 enable) { - if (a0 && gUnknown_02021368) - isSoundVsync = 1; - return isSoundVsync; + if (enable && sIsSoundVSyncPaused) + sIsSoundVsync = TRUE; + + return sIsSoundVsync; } void PlaySE(u16 song) @@ -54,8 +55,8 @@ bool8 sub_020093AC(void) void FadeOutSE(u16 song, u16 _speed) { struct MusicPlayerInfo * mPlayInfo; - u16 speed = _speed >> 4; + if (speed == 0) speed = 1; @@ -89,12 +90,12 @@ void SetSEPitch(u16 song, s16 pitch) void SoundVSyncOff(void) { - isSoundVsync = 0; + sIsSoundVsync = FALSE; m4aSoundVSyncOff(); } void SoundVSyncOn(void) { m4aSoundVSyncOn(); - isSoundVsync = 1; + sIsSoundVsync = TRUE; } diff --git a/payload/src/gflib/text.c b/payload/src/gflib/text.c index d9b523a..e391d75 100644 --- a/payload/src/gflib/text.c +++ b/payload/src/gflib/text.c @@ -11,9 +11,12 @@ static inline void *GetAddr(struct Window *window) { void *vaddr = window->vramCharBase + (window->currentY / 8) * window->width * 32; u32 x = window->currentX; + vaddr += 32 * (x / 8); // Needed to match. - x++;x--; + x++; + x--; + return vaddr; } @@ -30,6 +33,7 @@ void DrawGlyphOnWindow_NoShadow(struct Window * window, u32 glyphId, u32 fgColor xpixel = 8 - (window->currentX & 7); glyphData = window->glyphGfx + glyphId * window->glyphSize; curpos--; + for (i = 0; i < window->glyphSize; i++) { if (i != 8) @@ -66,6 +70,7 @@ void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColo xpixel = 8 - (window->currentX & 7); glyphData = window->glyphGfx + (glyphId * window->glyphSize * 2); + for (i = 0; i < window->glyphSize; i++) { vaddr = window->vramCharBase + ((window->currentY + i) >> 3) * window->width * 32 + (32 * (window->currentX >> 3)); @@ -101,9 +106,9 @@ void DrawGlyphOnWindow_WithShadow(struct Window *window, u32 glyphId, u32 fgColo static inline void TextWindowFillTileBufferForText_Inline(struct Window *win) { s32 i, j; - u16 *addr = (void *) IWRAM_START + (win->bg << 12); u16 blockVal = win->baseBlock; + for (i = win->top; i < win->top + win->height; i++) { for (j = win->left; j < win->left + win->width; j++) @@ -153,6 +158,7 @@ void GenerateFontHalfrowLookupTable(u32 *ptr) s32 i; sFontHalfrowLookupTablePtr = ptr; + for (i = 0; i <= 0xFF; i++) { u32 r1 = 0; @@ -178,9 +184,9 @@ void GenerateFontHalfrowLookupTable(u32 *ptr) } } -void ClearWindowCharBuffer(struct Window *window, u32 a1) +void ClearWindowCharBuffer(struct Window *window, u32 fillValue) { - CpuFill16(a1, window->vramCharBase, (window->width * window->height) * 32); + CpuFill16(fillValue, window->vramCharBase, (window->width * window->height) * 32); TextWindowSetXY(window, 0, 0); } diff --git a/payload/src/libpmagb/agb_rom.c b/payload/src/libpmagb/agb_rom.c index ab543a4..54a7117 100644 --- a/payload/src/libpmagb/agb_rom.c +++ b/payload/src/libpmagb/agb_rom.c @@ -36,8 +36,10 @@ static inline void SeedSaveRng(void) { sum += firstSector[i]; } + gLastSaveBlockPtr = gSaveBlockLoadRegions + 4 * (sum & 0xFF); gSaveRngValue = sum; + for (i = 0; i < (sum & 0xFF); i++) { SaveRandom(); @@ -47,7 +49,9 @@ static inline void SeedSaveRng(void) static inline void * UpdateSaveBlockPtr(u32 size) { void * ret = gLastSaveBlockPtr; + gLastSaveBlockPtr = (gLastSaveBlockPtr + size) + ((SaveRandom() & 15) * 4); + return ret; } @@ -60,6 +64,7 @@ static inline void SetSaveBlock1Ptr(void) { void ** ptr = (void**)&gSaveBlock1Ptr; u32 size = gAgbPmRomParams->saveBlock1Size; + SetSaveBlockPtr(ptr, size); } @@ -67,6 +72,7 @@ static inline void SetSaveBlock1BakPtr(void) { void ** ptr = (void**)&gSaveBlock1BakPtr; u32 size = gAgbPmRomParams->saveBlock1Size; + SetSaveBlockPtr(ptr, size); } @@ -74,6 +80,7 @@ static inline void SetSaveBlock2Ptr(void) { void ** ptr = (void**)&gSaveBlock2Ptr; u32 size = gAgbPmRomParams->saveBlock2Size; + SetSaveBlockPtr(ptr, size); } @@ -81,12 +88,14 @@ static inline void SetPokemonStoragePtr(void) { void ** ptr = (void**)&gPokemonStoragePtr; u32 size = sizeof(struct PokemonStorage); + SetSaveBlockPtr(ptr, size); } void SaveBlocksInit(void) { SeedSaveRng(); + switch (SaveRandom() & 3) { case 0: @@ -114,6 +123,7 @@ void SaveBlocksInit(void) SetSaveBlock1Ptr(); break; } + gPlayerPartyPtr = gSaveBlock1Ptr + gAgbPmRomParams->partyOffset; gPlayerPartyBakPtr = gSaveBlock1BakPtr + gAgbPmRomParams->partyOffset; gPlayerPartyCountPtr = gSaveBlock1Ptr + gAgbPmRomParams->partyCountOffset; @@ -130,6 +140,7 @@ void SaveBlocksInit(void) u16 SaveRandom(void) { gSaveRngValue = gSaveRngValue * 2061 + 7; + return gSaveRngValue; } diff --git a/payload/src/libpmagb/berry.c b/payload/src/libpmagb/berry.c index 616ebb7..cc65fe3 100644 --- a/payload/src/libpmagb/berry.c +++ b/payload/src/libpmagb/berry.c @@ -27,30 +27,30 @@ struct Item u32 GetEnigmaBerryChecksum(struct EnigmaBerry * enigmaBerry) { - u32 i, csum; + u32 i, checksum; u8 * dest; // the description pointers could be pointing to anywhere in memory. we do not want these // pointers to factor into the checksum as it will produce a different result every time: so // back the pointers up and set them to null so the checksum is safe to calculate. - const u8 * desc1bak = gEnigmaBerryPtr->berry.description1; - const u8 * desc2bak = gEnigmaBerryPtr->berry.description2; + const u8 * desc1Bak = gEnigmaBerryPtr->berry.description1; + const u8 * desc2Bak = gEnigmaBerryPtr->berry.description2; gEnigmaBerryPtr->berry.description1 = NULL; gEnigmaBerryPtr->berry.description2 = NULL; dest = (u8 *)enigmaBerry; - csum = 0; + checksum = 0; for (i = 0; i < gAgbPmRomParams->enigmaBerrySize - 4; i++) { - csum += dest[i]; + checksum += dest[i]; } // the checksum is calculated: the descriptions are safe to restore now. - gEnigmaBerryPtr->berry.description1 = desc1bak; - gEnigmaBerryPtr->berry.description2 = desc2bak; + gEnigmaBerryPtr->berry.description1 = desc1Bak; + gEnigmaBerryPtr->berry.description2 = desc2Bak; - return csum; + return checksum; } // due to e-reader scans being particularly volatile to failure, it is a requirement to check for @@ -102,13 +102,14 @@ const u8 *ItemId_GetName(u16 itemId) return items[SanitizeItemId(itemId)].name; } -void CopyItemName(u32 itemId, u8 *string, const u8 * berry_str) +void CopyItemName(u32 itemId, u8 *string, const u8 * berryString) { u16 itemId_ = itemId; + if (itemId_ == ITEM_ENIGMA_BERRY) { StringCopy(string, GetBerryInfo(ITEM_TO_BERRY(ITEM_ENIGMA_BERRY))->name); - StringAppend(string, berry_str); + StringAppend(string, berryString); } else {