diff options
| author | Dragorn421 <Dragorn421@users.noreply.github.com> | 2024-11-08 02:27:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-07 17:27:19 -0800 |
| commit | fcc5cf828ded5d362b2e63a8868374a50df405ed (patch) | |
| tree | 530d7d6d8690599593f6588b04d10441e364b5c7 /src/boot/O2 | |
| parent | 9cd9099a0431b369be39f111bf57051f544bead3 (diff) | |
libu64 (#1705)
* libu64
* libu64 -O2
* Add libu64.md
Diffstat (limited to 'src/boot/O2')
| -rw-r--r-- | src/boot/O2/debug.c | 11 | ||||
| -rw-r--r-- | src/boot/O2/gfxprint.c | 255 | ||||
| -rw-r--r-- | src/boot/O2/loadfragment.c | 287 | ||||
| -rw-r--r-- | src/boot/O2/loadfragment2.c | 209 | ||||
| -rw-r--r-- | src/boot/O2/mtxuty-cvt.c | 23 | ||||
| -rw-r--r-- | src/boot/O2/padsetup.c | 35 | ||||
| -rw-r--r-- | src/boot/O2/padutils.c | 94 | ||||
| -rw-r--r-- | src/boot/O2/rcp_utils.c | 23 | ||||
| -rw-r--r-- | src/boot/O2/stackcheck.c | 122 | ||||
| -rw-r--r-- | src/boot/O2/system_heap.c | 123 |
10 files changed, 0 insertions, 1182 deletions
diff --git a/src/boot/O2/debug.c b/src/boot/O2/debug.c deleted file mode 100644 index 3933b585e..000000000 --- a/src/boot/O2/debug.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "global.h" -#include "fault.h" - -void _dbg_hungup(const char* file, int lineNum) { - osGetThreadId(NULL); - Fault_AddHungupAndCrash(file, lineNum); -} - -void Reset(void) { - Fault_AddHungupAndCrash("Reset", 0); -} diff --git a/src/boot/O2/gfxprint.c b/src/boot/O2/gfxprint.c deleted file mode 100644 index c4294177c..000000000 --- a/src/boot/O2/gfxprint.c +++ /dev/null @@ -1,255 +0,0 @@ -#include "gfxprint.h" -#include "attributes.h" - -#define GFXP_FLAG_HIRAGANA (1 << 0) -#define GFXP_FLAG_RAINBOW (1 << 1) -#define GFXP_FLAG_SHADOW (1 << 2) -#define GFXP_FLAG_UPDATE (1 << 3) -#define GFXP_FLAG_ENLARGE (1 << 6) -#define GFXP_FLAG_OPEN (1 << 7) - -u64 sGfxPrintFontTLUT[] = { -#include "assets/boot/gfxprint/gfx_print_font_tlut.rgba16.inc.c" -}; - -u64 sGfxPrintRainbowTLUT[] = { -#include "assets/boot/gfxprint/gfx_print_rainbow_tlut.rgba16.inc.c" -}; - -u8 sGfxPrintRainbowFont[] = { -#include "assets/boot/gfxprint/sGfxPrintRainbowFont.bin.inc.c" -}; - -u8 sGfxPrintFont[] = { -#include "assets/boot/gfxprint/sGfxPrintFont.bin.inc.c" -}; - -void GfxPrint_Setup(GfxPrint* this) { - s32 width = 16; - s32 height = 256; - s32 i; - - gDPPipeSync(this->dList++); - gDPSetOtherMode(this->dList++, - G_AD_DISABLE | G_CD_DISABLE | G_CK_NONE | G_TC_FILT | G_TF_BILERP | G_TT_IA16 | G_TL_TILE | - G_TD_CLAMP | G_TP_NONE | G_CYC_1CYCLE | G_PM_NPRIMITIVE, - G_AC_NONE | G_ZS_PRIM | G_RM_XLU_SURF | G_RM_XLU_SURF2); - gDPSetCombineMode(this->dList++, G_CC_DECALRGBA, G_CC_DECALRGBA); - gDPLoadTextureBlock_4b(this->dList++, sGfxPrintFont, G_IM_FMT_CI, width, height, 0, G_TX_NOMIRROR | G_TX_WRAP, - G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD); - gDPLoadTLUT(this->dList++, 64, 256, sGfxPrintFontTLUT); - - for (i = 1; i < 4; i++) { - gDPSetTile(this->dList++, G_IM_FMT_CI, G_IM_SIZ_4b, 1, 0, i * 2, i, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, - G_TX_NOLOD, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOLOD); - gDPSetTileSize(this->dList++, i * 2, 0, 0, 60, 1020); - } - - gDPSetColor(this->dList++, G_SETPRIMCOLOR, this->color.rgba); - - gDPLoadMultiTile_4b(this->dList++, sGfxPrintRainbowFont, 0, 1, G_IM_FMT_CI, 2, 8, 0, 0, 1, 7, 4, - G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, 1, 3, G_TX_NOLOD, G_TX_NOLOD); - - gDPLoadTLUT(this->dList++, 16, 320, sGfxPrintRainbowTLUT); - - for (i = 1; i < 4; i++) { - gDPSetTile(this->dList++, G_IM_FMT_CI, G_IM_SIZ_4b, 1, 0, i * 2 + 1, 4, G_TX_NOMIRROR | G_TX_WRAP, 3, - G_TX_NOLOD, G_TX_NOMIRROR | G_TX_WRAP, 1, G_TX_NOLOD); - gDPSetTileSize(this->dList++, i * 2 + 1, 0, 0, 4, 28); - } -} - -void GfxPrint_SetColor(GfxPrint* this, u32 r, u32 g, u32 b, u32 a) { - this->color.r = r; - this->color.g = g; - this->color.b = b; - this->color.a = a; - gDPPipeSync(this->dList++); - gDPSetColor(this->dList++, G_SETPRIMCOLOR, this->color.rgba); -} - -void GfxPrint_SetPosPx(GfxPrint* this, s32 x, s32 y) { - this->posX = this->baseX + (x << 2); - this->posY = this->baseY + (y << 2); -} - -void GfxPrint_SetPos(GfxPrint* this, s32 x, s32 y) { - GfxPrint_SetPosPx(this, x << 3, y << 3); -} - -void GfxPrint_SetBasePosPx(GfxPrint* this, s32 x, s32 y) { - this->baseX = x << 2; - this->baseY = y << 2; -} - -void GfxPrint_PrintCharImpl(GfxPrint* this, u8 c) { - u32 tile = (c & 0xFF) * 2; - u16 s = c & 4; - u16 t = c >> 3; - - if (this->flags & GFXP_FLAG_UPDATE) { - this->flags &= ~GFXP_FLAG_UPDATE; - - gDPPipeSync(this->dList++); - if (this->flags & GFXP_FLAG_RAINBOW) { - gDPSetTextureLUT(this->dList++, G_TT_RGBA16); - gDPSetCycleType(this->dList++, G_CYC_2CYCLE); - gDPSetRenderMode(this->dList++, G_RM_PASS, G_RM_XLU_SURF2); - gDPSetCombineMode(this->dList++, G_CC_INTERFERENCE, G_CC_PASS2); - } else { - gDPSetTextureLUT(this->dList++, G_TT_IA16); - gDPSetCycleType(this->dList++, G_CYC_1CYCLE); - gDPSetRenderMode(this->dList++, G_RM_XLU_SURF, G_RM_XLU_SURF2); - gDPSetCombineMode(this->dList++, G_CC_MODULATEIDECALA_PRIM, G_CC_MODULATEIDECALA_PRIM); - } - } - - if (this->flags & GFXP_FLAG_SHADOW) { - gDPSetColor(this->dList++, G_SETPRIMCOLOR, 0); - - gSPTextureRectangle(this->dList++, this->posX + 4, this->posY + 4, this->posX + 4 + 32, this->posY + 4 + 32, - tile, s << 6, t << 8, 1 << 10, 1 << 10); - - gDPSetColor(this->dList++, G_SETPRIMCOLOR, this->color.rgba); - } - - gSPTextureRectangle(this->dList++, this->posX, this->posY, this->posX + 32, this->posY + 32, tile, s << 6, t << 8, - 1 << 10, 1 << 10); - - this->posX += 32; -} - -void GfxPrint_PrintChar(GfxPrint* this, u8 c) { - if (c == ' ') { - this->posX += 32; - } else if (c > ' ' && c < 0x7F) { - GfxPrint_PrintCharImpl(this, c); - } else if (c >= 0xA0 && c < 0xE0) { - if (this->flags & GFXP_FLAG_HIRAGANA) { - if (c < 0xC0) { - c -= 0x20; - } else { - c += 0x20; - } - } - GfxPrint_PrintCharImpl(this, c); - } else { - switch (c) { - case '\0': - break; - - case '\n': - this->posY += 32; - FALLTHROUGH; - case '\r': - this->posX = this->baseX; - break; - - case '\t': - do { - GfxPrint_PrintCharImpl(this, ' '); - } while ((this->posX - this->baseX) % 256); - break; - - case GFXP_HIRAGANA_CHAR: - this->flags |= GFXP_FLAG_HIRAGANA; - break; - - case GFXP_KATAKANA_CHAR: - this->flags &= ~GFXP_FLAG_HIRAGANA; - break; - - case GFXP_RAINBOW_ON_CHAR: - this->flags |= GFXP_FLAG_RAINBOW; - this->flags |= GFXP_FLAG_UPDATE; - break; - - case GFXP_RAINBOW_OFF_CHAR: - this->flags &= ~GFXP_FLAG_RAINBOW; - this->flags |= GFXP_FLAG_UPDATE; - break; - - case GFXP_UNUSED_CHAR: - default: - break; - } - } -} - -void GfxPrint_PrintStringWithSize(GfxPrint* this, const void* buffer, size_t charSize, size_t charCount) { - const char* str = (const char*)buffer; - size_t count = charSize * charCount; - - while (count != 0) { - GfxPrint_PrintChar(this, *str++); - count--; - } -} - -void GfxPrint_PrintString(GfxPrint* this, const char* str) { - while (*str != '\0') { - GfxPrint_PrintChar(this, *str++); - } -} - -void* GfxPrint_Callback(void* arg, const char* str, size_t size) { - GfxPrint* this = arg; - - GfxPrint_PrintStringWithSize(this, str, sizeof(char), size); - - return this; -} - -void GfxPrint_Init(GfxPrint* this) { - this->flags &= ~GFXP_FLAG_OPEN; - - this->callback = GfxPrint_Callback; - this->dList = NULL; - this->posX = 0; - this->posY = 0; - this->baseX = 0; - this->baseY = 0; - this->color.rgba = 0; - - this->flags &= ~GFXP_FLAG_HIRAGANA; - this->flags &= ~GFXP_FLAG_RAINBOW; - this->flags |= GFXP_FLAG_SHADOW; - this->flags |= GFXP_FLAG_UPDATE; -} - -void GfxPrint_Destroy(GfxPrint* this) { -} - -void GfxPrint_Open(GfxPrint* this, Gfx* dList) { - if (!(this->flags & GFXP_FLAG_OPEN)) { - this->flags |= GFXP_FLAG_OPEN; - this->dList = dList; - GfxPrint_Setup(this); - } -} - -Gfx* GfxPrint_Close(GfxPrint* this) { - Gfx* ret; - - this->flags &= ~GFXP_FLAG_OPEN; - ret = this->dList; - this->dList = NULL; - - return ret; -} - -s32 GfxPrint_VPrintf(GfxPrint* this, const char* fmt, va_list args) { - return vaprintf(&this->callback, fmt, args); -} - -s32 GfxPrint_Printf(GfxPrint* this, const char* fmt, ...) { - s32 ret; - va_list args; - va_start(args, fmt); - - ret = GfxPrint_VPrintf(this, fmt, args); - - va_end(args); - - return ret; -} diff --git a/src/boot/O2/loadfragment.c b/src/boot/O2/loadfragment.c deleted file mode 100644 index 7fbcfb411..000000000 --- a/src/boot/O2/loadfragment.c +++ /dev/null @@ -1,287 +0,0 @@ -/** - * @file loadfragment.c - * - * Functions used to process and relocate dynamically loadable code segments (overlays). - * - * @note: - * These are completly unused in favor of the fragment overlay functions in `loadfragment2.c`. - * - * The main difference between them seems to be the lack of vramEnd arguments here. - * Instead they are calculated on the fly. - */ - -#include "global.h" -#include "libc64/malloc.h" -#include "loadfragment.h" - -s32 gFragmentLogSeverity = 2; - -// Extract MIPS register rs from an instruction word -#define MIPS_REG_RS(insn) (((insn) >> 0x15) & 0x1F) - -// Extract MIPS register rt from an instruction word -#define MIPS_REG_RT(insn) (((insn) >> 0x10) & 0x1F) - -// Extract MIPS jump target from an instruction word -#define MIPS_JUMP_TARGET(insn) (((insn)&0x03FFFFFF) << 2) - -/** - * Performs runtime relocation of overlay files, loadable code segments. - * - * Overlays are expected to be loadable anywhere in direct-mapped cached (KSEG0) memory, with some appropriate - * alignment requirements; memory addresses in such code must be updated once loaded to execute properly. - * When compiled, overlays are given 'fake' KSEG0 RAM addresses larger than the total possible available main memory - * (>= 0x80800000), such addresses are referred to as Virtual RAM (VRAM) to distinguish them. When loading the overlay, - * the relocation table produced at compile time is consulted to determine where and how to update these VRAM addresses - * to correct RAM addresses based on the location the overlay was loaded at, enabling the code to execute at this - * address as if it were compiled to run at this address. - * - * Each relocation is represented by a packed 32-bit value, formatted in the following way: - * - [31:30] 2-bit section id, taking values from the `RelocSectionId` enum. - * - [29:24] 6-bit relocation type describing which relocation operation should be performed. Same as ELF32 MIPS. - * - [23: 0] 24-bit section-relative offset indicating where in the section to apply this relocation. - * - * @param allocatedRamAddr Memory address the binary was loaded at. - * @param ovlRelocs Overlay relocation section containing overlay section layout and runtime relocations. - * @param vramStart Virtual RAM address that the overlay was compiled at. - */ -void Fragment_Relocate(void* allocatedRamAddr, OverlayRelocationSection* ovlRelocs, void* vramStart) { - u32 sections[RELOC_SECTION_MAX]; - u32* relocDataP; - u32 reloc; - uintptr_t relocatedAddress; - u32 i; - u32* luiInstRef; - u32 isLoNeg; - u32* regValP; - //! MIPS ELF relocation does not generally require tracking register values, so at first glance it appears this - //! register tracking was an unnecessary complication. However there is a bug in the IDO compiler that can cause - //! relocations to be emitted in the wrong order under rare circumstances when the compiler attempts to reuse a - //! previous HI16 relocation for a different LO16 relocation as an optimization. This register tracking is likely - //! a workaround to prevent improper matching of unrelated HI16 and LO16 relocations that would otherwise arise - //! due to the incorrect ordering. - u32* luiRefs[32]; - u32 luiVals[32]; - uintptr_t allocu32 = (uintptr_t)allocatedRamAddr; - uintptr_t vramu32 = (uintptr_t)vramStart; - - if (gFragmentLogSeverity >= 3) { - // "DoRelocation(%08x, %08x, %08x)\n" - } - - sections[RELOC_SECTION_NULL] = 0; - sections[RELOC_SECTION_TEXT] = allocu32; - sections[RELOC_SECTION_DATA] = allocu32 + ovlRelocs->textSize; - sections[RELOC_SECTION_RODATA] = sections[RELOC_SECTION_DATA] + ovlRelocs->dataSize; - - for (i = 0; i < ovlRelocs->numRelocations; i++) { - // This will always resolve to a 32-bit aligned address as each section - // containing code or pointers must be aligned to at least 4 bytes and the - // MIPS ABI defines the offset of both 16-bit and 32-bit relocations to be - // the start of the 32-bit word containing the target. - reloc = ovlRelocs->relocations[i]; - relocDataP = (u32*)(sections[RELOC_SECTION(reloc)] + RELOC_OFFSET(reloc)); - - switch (RELOC_TYPE_MASK(reloc)) { - case R_MIPS_32 << RELOC_TYPE_SHIFT: - // Handles 32-bit address relocation, used for things such as jump tables and pointers in data. - // Just relocate the full address - - // Check address is valid for relocation - if ((*relocDataP & 0x0F000000) == 0) { - *relocDataP = *relocDataP - vramu32 + allocu32; - } else if (gFragmentLogSeverity >= 3) { - // Segment pointer 32 %08x - // "セグメントポインタ32です %08x\n" - } - break; - - case R_MIPS_26 << RELOC_TYPE_SHIFT: - // Handles 26-bit address relocation, used for jumps and jals. - // Extract the address from the target field of the J-type MIPS instruction. - // Relocate the address and update the instruction. - - if (1) { - *relocDataP = - (*relocDataP & 0xFC000000) | - (((PHYS_TO_K0(MIPS_JUMP_TARGET(*relocDataP)) - vramu32 + allocu32) & 0x0FFFFFFF) >> 2); - } else if (gFragmentLogSeverity >= 3) { - // Segment pointer 26 %08x - // "セグメントポインタ26です %08x\n" - } - break; - - case R_MIPS_HI16 << RELOC_TYPE_SHIFT: - // Handles relocation for a hi/lo pair, part 1. - // Store the reference to the LUI instruction (hi) using the `rt` register of the instruction. - // This will be updated later in the `R_MIPS_LO16` section. - - luiRefs[(*relocDataP >> 0x10) & 0x1F] = relocDataP; - luiVals[(*relocDataP >> 0x10) & 0x1F] = *relocDataP; - break; - - case R_MIPS_LO16 << RELOC_TYPE_SHIFT: - // Handles relocation for a hi/lo pair, part 2. - // Grab the stored LUI (hi) from the `R_MIPS_HI16` section using the `rs` register of the instruction. - // The full address is calculated, relocated, and then used to update both the LUI and lo instructions. - // If the lo part is negative, add 1 to the LUI value. - // Note: The lo instruction is assumed to have a signed immediate. - - luiInstRef = luiRefs[(*relocDataP >> 0x15) & 0x1F]; - regValP = &luiVals[(*relocDataP >> 0x15) & 0x1F]; - - // Check address is valid for relocation - if ((((*luiInstRef << 0x10) + (s16)*relocDataP) & 0x0F000000) == 0) { - relocatedAddress = ((*regValP << 0x10) + (s16)*relocDataP) - vramu32 + allocu32; - isLoNeg = (relocatedAddress & 0x8000) ? 1 : 0; - *luiInstRef = (*luiInstRef & 0xFFFF0000) | (((relocatedAddress >> 0x10) & 0xFFFF) + isLoNeg); - *relocDataP = (*relocDataP & 0xFFFF0000) | (relocatedAddress & 0xFFFF); - } else if (gFragmentLogSeverity >= 3) { - // Segment pointer 16 %08x %08x %08x - // "セグメントポインタ16です %08x %08x %08x" - } - break; - } - } -} - -size_t Fragment_Load(uintptr_t vromStart, uintptr_t vromEnd, void* vramStart, void* allocatedRamAddr, - size_t allocatedBytes) { - size_t size = vromEnd - vromStart; - uintptr_t end; - s32 pad; - OverlayRelocationSection* ovlRelocs; - - if (gFragmentLogSeverity >= 3) { - // Starting loading dynamic link function - // "\nダイナミックリンクファンクションのロードを開始します\n" - } - if (gFragmentLogSeverity >= 3) { - // DMA transfer TEXT, DATA, RODATA+rel (%08x-%08x) - // "TEXT,DATA,RODATA+relをDMA転送します(%08x-%08x)\n" - } - - end = (uintptr_t)allocatedRamAddr + size; - DmaMgr_RequestSync(allocatedRamAddr, vromStart, size); - - ovlRelocs = (OverlayRelocationSection*)(end - ((s32*)end)[-1]); - - if (gFragmentLogSeverity >= 3) { - // "TEXT(%08x), DATA(%08x), RODATA(%08x), BSS(%08x)\n" - } - - if (allocatedBytes < ovlRelocs->bssSize + size) { - if (gFragmentLogSeverity >= 3) { - // ramSize is too small (ramSize=%08x, NeedRamSize=%08x) - // "ramSizeが小さすぎます(ramSize=%08x, NeedRamSize=%08x)\n" - } - return 0; - } - - allocatedBytes = ovlRelocs->bssSize + size; - - if (gFragmentLogSeverity >= 3) { - // I will relocate - // "リロケーションします\n" - } - - Fragment_Relocate(allocatedRamAddr, ovlRelocs, vramStart); - - if (ovlRelocs->bssSize != 0) { - if (gFragmentLogSeverity >= 3) { - // Clear BSS area (%08x-%08x) - // "BSS領域をクリアします(%08x-%08x)\n" - } - bzero((void*)end, ovlRelocs->bssSize); - } - - osWritebackDCache(allocatedRamAddr, allocatedBytes); - osInvalICache(allocatedRamAddr, allocatedBytes); - - if (gFragmentLogSeverity >= 3) { - // Finish loading the dynamic link function - // "ダイナミックリンクファンクションのロードを終了します\n\n" - } - - return allocatedBytes; -} - -void* Fragment_AllocateAndLoad(uintptr_t vromStart, uintptr_t vromEnd, void* vramStart) { - size_t size = vromEnd - vromStart; - uintptr_t end; - void* allocatedRamAddr; - uintptr_t ovlOffset; - OverlayRelocationSection* ovlRelocs; - size_t allocatedBytes; - - if (gFragmentLogSeverity >= 3) { - // Start loading dynamic link function - // "\nダイナミックリンクファンクションのロードを開始します\n" - - // "LoadFragment(%08x, %08x, %08x)\n" - } - - allocatedRamAddr = malloc_r(size); - end = (uintptr_t)allocatedRamAddr + size; - - if (gFragmentLogSeverity >= 3) { - // DMA transfer TEXT, DATA, RODATA+rel (%08x-%08x) - // "TEXT,DATA,RODATA+relをDMA転送します(%08x-%08x)\n" - } - - DmaMgr_RequestSync(allocatedRamAddr, vromStart, size); - - if (gFragmentLogSeverity >= 3) { - // "TEXT(%08x), DATA(%08x), RODATA(%08x), BSS(%08x)\n" - } - - ovlOffset = end - sizeof(s32); - ovlRelocs = (OverlayRelocationSection*)(end - ((s32*)end)[-1]); - - //! FAKE: - if (1) {} - - allocatedBytes = ovlRelocs->bssSize + size; - - allocatedRamAddr = realloc(allocatedRamAddr, allocatedBytes); - - if (gFragmentLogSeverity >= 3) { - // No reallocation. - // "リアロケーションしません。\n" - } - - if (allocatedRamAddr == NULL) { - if (gFragmentLogSeverity >= 3) { - // Reallocation failed. . - // "リアロケーションに失敗しました。" - } - return allocatedRamAddr; - } - - end = (uintptr_t)allocatedRamAddr + size; - ovlRelocs = (OverlayRelocationSection*)(end - *(uintptr_t*)ovlOffset); - - if (gFragmentLogSeverity >= 3) { - // I will relocate - // "リロケーションします\n" - } - - Fragment_Relocate(allocatedRamAddr, ovlRelocs, vramStart); - - if (ovlRelocs->bssSize != 0) { - if (gFragmentLogSeverity >= 3) { - // Clear BSS area (%08x-%08x) - // "BSS領域をクリアします(%08x-%08x)\n" - } - bzero((void*)end, ovlRelocs->bssSize); - } - - osInvalICache(allocatedRamAddr, allocatedBytes); - - if (gFragmentLogSeverity >= 3) { - // Finish loading the dynamic link function - // "ダイナミックリンクファンクションのロードを終了します\n\n" - } - - return allocatedRamAddr; -} diff --git a/src/boot/O2/loadfragment2.c b/src/boot/O2/loadfragment2.c deleted file mode 100644 index b8dacf82d..000000000 --- a/src/boot/O2/loadfragment2.c +++ /dev/null @@ -1,209 +0,0 @@ -/** - * @file loadfragment2.c - * - * Functions used to process and relocate dynamically loadable code segments (overlays). - * - * @note: - * These are for specific fragment overlays with the .ovl file extension - */ -#include "global.h" -#include "libc64/malloc.h" -#include "loadfragment.h" - -s32 gOverlayLogSeverity = 2; - -// Extract MIPS register rs from an instruction word -#define MIPS_REG_RS(insn) (((insn) >> 0x15) & 0x1F) - -// Extract MIPS register rt from an instruction word -#define MIPS_REG_RT(insn) (((insn) >> 0x10) & 0x1F) - -// Extract MIPS jump target from an instruction word -#define MIPS_JUMP_TARGET(insn) (((insn)&0x03FFFFFF) << 2) - -/** - * Performs runtime relocation of overlay files, loadable code segments. - * - * Overlays are expected to be loadable anywhere in direct-mapped cached (KSEG0) memory, with some appropriate - * alignment requirements; memory addresses in such code must be updated once loaded to execute properly. - * When compiled, overlays are given 'fake' KSEG0 RAM addresses larger than the total possible available main memory - * (>= 0x80800000), such addresses are referred to as Virtual RAM (VRAM) to distinguish them. When loading the overlay, - * the relocation table produced at compile time is consulted to determine where and how to update these VRAM addresses - * to correct RAM addresses based on the location the overlay was loaded at, enabling the code to execute at this - * address as if it were compiled to run at this address. - * - * Each relocation is represented by a packed 32-bit value, formatted in the following way: - * - [31:30] 2-bit section id, taking values from the `RelocSectionId` enum. - * - [29:24] 6-bit relocation type describing which relocation operation should be performed. Same as ELF32 MIPS. - * - [23: 0] 24-bit section-relative offset indicating where in the section to apply this relocation. - * - * @param allocatedRamAddress Memory address the binary was loaded at. - * @param ovlRelocs Overlay relocation section containing overlay section layout and runtime relocations. - * @param vramStart Virtual RAM address that the overlay was compiled at. - */ -void Overlay_Relocate(void* allocatedRamAddr, OverlayRelocationSection* ovlRelocs, void* vramStart) { - u32 sections[RELOC_SECTION_MAX]; - u32* relocDataP; - u32 reloc; - uintptr_t relocatedAddress; - u32 i; - u32* luiInstRef; - u32 isLoNeg; - u32* regValP; - //! MIPS ELF relocation does not generally require tracking register values, so at first glance it appears this - //! register tracking was an unnecessary complication. However there is a bug in the IDO compiler that can cause - //! relocations to be emitted in the wrong order under rare circumstances when the compiler attempts to reuse a - //! previous HI16 relocation for a different LO16 relocation as an optimization. This register tracking is likely - //! a workaround to prevent improper matching of unrelated HI16 and LO16 relocations that would otherwise arise - //! due to the incorrect ordering. - u32* luiRefs[32]; - u32 luiVals[32]; - uintptr_t allocu32 = (uintptr_t)allocatedRamAddr; - uintptr_t vramu32 = (uintptr_t)vramStart; - - if (gOverlayLogSeverity >= 3) { - // "DoRelocation(%08x, %08x, %08x)\n" - } - - sections[RELOC_SECTION_NULL] = 0; - sections[RELOC_SECTION_TEXT] = allocu32; - sections[RELOC_SECTION_DATA] = allocu32 + ovlRelocs->textSize; - sections[RELOC_SECTION_RODATA] = sections[RELOC_SECTION_DATA] + ovlRelocs->dataSize; - - for (i = 0; i < ovlRelocs->numRelocations; i++) { - // This will always resolve to a 32-bit aligned address as each section - // containing code or pointers must be aligned to at least 4 bytes and the - // MIPS ABI defines the offset of both 16-bit and 32-bit relocations to be - // the start of the 32-bit word containing the target. - reloc = ovlRelocs->relocations[i]; - relocDataP = (u32*)(sections[RELOC_SECTION(reloc)] + RELOC_OFFSET(reloc)); - - switch (RELOC_TYPE_MASK(reloc)) { - case R_MIPS_32 << RELOC_TYPE_SHIFT: - // Handles 32-bit address relocation, used for things such as jump tables and pointers in data. - // Just relocate the full address - - // Check address is valid for relocation - if ((*relocDataP & 0x0F000000) == 0) { - *relocDataP = *relocDataP - vramu32 + allocu32; - } else if (gOverlayLogSeverity >= 3) { - // Segment pointer 32 %08x - // "セグメントポインタ32です %08x\n" - } - break; - - case R_MIPS_26 << RELOC_TYPE_SHIFT: - // Handles 26-bit address relocation, used for jumps and jals. - // Extract the address from the target field of the J-type MIPS instruction. - // Relocate the address and update the instruction. - - if (1) { - *relocDataP = - (*relocDataP & 0xFC000000) | - (((PHYS_TO_K0(MIPS_JUMP_TARGET(*relocDataP)) - vramu32 + allocu32) & 0x0FFFFFFF) >> 2); - } else if (gOverlayLogSeverity >= 3) { - // Segment pointer 26 %08x - // "セグメントポインタ26です %08x\n" - } - break; - - case R_MIPS_HI16 << RELOC_TYPE_SHIFT: - // Handles relocation for a hi/lo pair, part 1. - // Store the reference to the LUI instruction (hi) using the `rt` register of the instruction. - // This will be updated later in the `R_MIPS_LO16` section. - - luiRefs[(*relocDataP >> 0x10) & 0x1F] = relocDataP; - luiVals[(*relocDataP >> 0x10) & 0x1F] = *relocDataP; - break; - - case R_MIPS_LO16 << RELOC_TYPE_SHIFT: - // Handles relocation for a hi/lo pair, part 2. - // Grab the stored LUI (hi) from the `R_MIPS_HI16` section using the `rs` register of the instruction. - // The full address is calculated, relocated, and then used to update both the LUI and lo instructions. - // If the lo part is negative, add 1 to the LUI value. - // Note: The lo instruction is assumed to have a signed immediate. - - luiInstRef = luiRefs[(*relocDataP >> 0x15) & 0x1F]; - regValP = &luiVals[(*relocDataP >> 0x15) & 0x1F]; - - // Check address is valid for relocation - if ((((*luiInstRef << 0x10) + (s16)*relocDataP) & 0x0F000000) == 0) { - relocatedAddress = ((*regValP << 0x10) + (s16)*relocDataP) - vramu32 + allocu32; - isLoNeg = (relocatedAddress & 0x8000) ? 1 : 0; - *luiInstRef = (*luiInstRef & 0xFFFF0000) | (((relocatedAddress >> 0x10) & 0xFFFF) + isLoNeg); - *relocDataP = (*relocDataP & 0xFFFF0000) | (relocatedAddress & 0xFFFF); - } else if (gOverlayLogSeverity >= 3) { - // Segment pointer 16 %08x %08x %08x - // "セグメントポインタ16です %08x %08x %08x" - } - break; - } - } -} - -size_t Overlay_Load(uintptr_t vromStart, uintptr_t vromEnd, void* vramStart, void* vramEnd, void* allocatedRamAddr) { - s32 pad[2]; - s32 size = vromEnd - vromStart; - uintptr_t end; - OverlayRelocationSection* ovlRelocs; - - if (gOverlayLogSeverity >= 3) { - // Start loading dynamic link function - // "\nダイナミックリンクファンクションのロードを開始します\n" - } - - size = vromEnd - vromStart; - end = (uintptr_t)allocatedRamAddr + size; - - if (gOverlayLogSeverity >= 3) { - // DMA transfer TEXT, DATA, RODATA+rel (%08x-%08x) - // "TEXT,DATA,RODATA+relをDMA転送します(%08x-%08x)\n" - } - - DmaMgr_RequestSync(allocatedRamAddr, vromStart, size); - - // The overlay file is expected to contain a 32-bit offset from the end of the file to the start of the - // relocation section. - ovlRelocs = (OverlayRelocationSection*)(end - ((s32*)end)[-1]); - - if (gOverlayLogSeverity >= 3) { - // "TEXT(%08x), DATA(%08x), RODATA(%08x), BSS(%08x)\n" - } - - if (gOverlayLogSeverity >= 3) { - // I will relocate - // "リロケーションします\n" - } - - Overlay_Relocate(allocatedRamAddr, ovlRelocs, vramStart); - - if (ovlRelocs->bssSize != 0) { - if (gOverlayLogSeverity >= 3) { - // Clear BSS area (%08x-%08x) - // "BSS領域をクリアします(%08x-%08x)\n" - } - bzero((void*)end, ovlRelocs->bssSize); - } - - size = (uintptr_t)vramEnd - (uintptr_t)vramStart; - - osWritebackDCache(allocatedRamAddr, size); - osInvalICache(allocatedRamAddr, size); - - if (gOverlayLogSeverity >= 3) { - // Finish loading the dynamic link function - // "ダイナミックリンクファンクションのロードを終了します\n\n" - } - - return size; -} - -void* Overlay_AllocateAndLoad(uintptr_t vromStart, uintptr_t vromEnd, void* vramStart, void* vramEnd) { - void* allocatedRamAddr = malloc_r((uintptr_t)vramEnd - (uintptr_t)vramStart); - - if (allocatedRamAddr != NULL) { - Overlay_Load(vromStart, vromEnd, vramStart, vramEnd, allocatedRamAddr); - } - - return allocatedRamAddr; -} diff --git a/src/boot/O2/mtxuty-cvt.c b/src/boot/O2/mtxuty-cvt.c deleted file mode 100644 index f96de806f..000000000 --- a/src/boot/O2/mtxuty-cvt.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "global.h" - -void MtxConv_F2L(Mtx* mtx, MtxF* mf) { - s32 i; - s32 j; - - for (i = 0; i < 4; i++) { - for (j = 0; j < 4; j++) { - s32 value = (mf->mf[i][j] * 0x10000); - struct { - u16 intPart[4][4]; - u16 fracPart[4][4]; - }* mu = (void*)mtx; - - mu->intPart[i][j] = value >> 16; - mu->fracPart[i][j] = value; - } - } -} - -void MtxConv_L2F(MtxF* mtx, Mtx* mf) { - guMtxL2F(mtx->mf, mf); -} diff --git a/src/boot/O2/padsetup.c b/src/boot/O2/padsetup.c deleted file mode 100644 index ebbb5d828..000000000 --- a/src/boot/O2/padsetup.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "global.h" - -s32 PadSetup_Init(OSMesgQueue* mq, u8* outMask, OSContStatus* status) { - s32 ret; - s32 i; - - *outMask = 0xFF; - ret = osContInit(mq, outMask, status); - if (ret != 0) { - return ret; - } - if (*outMask == 0xFF) { - if (osContStartQuery(mq) != 0) { - return 1; - } - osRecvMesg(mq, NULL, OS_MESG_BLOCK); - osContGetQuery(status); - - *outMask = 0; - - for (i = 0; i < MAXCONTROLLERS; i++) { - switch (status[i].errno) { - case 0: - if (status[i].type == CONT_TYPE_NORMAL) { - *outMask |= 1 << i; - } - break; - - default: - break; - } - } - } - return 0; -} diff --git a/src/boot/O2/padutils.c b/src/boot/O2/padutils.c deleted file mode 100644 index 8460bc007..000000000 --- a/src/boot/O2/padutils.c +++ /dev/null @@ -1,94 +0,0 @@ -#include "padutils.h" - -#include "PR/os_libc.h" - -void PadUtils_Init(Input* input) { - bzero(input, sizeof(Input)); -} - -void PadUtils_Destroy(void) { -} - -void PadUtils_ResetPressRel(Input* input) { - input->press.button = 0; - input->rel.button = 0; -} - -u32 PadUtils_CheckCurExact(Input* input, u16 value) { - return value == input->cur.button; -} - -u32 PadUtils_CheckCur(Input* input, u16 key) { - return key == (input->cur.button & key); -} - -u32 PadUtils_CheckPressed(Input* input, u16 key) { - return key == (input->press.button & key); -} - -u32 PadUtils_CheckReleased(Input* input, u16 key) { - return key == (input->rel.button & key); -} - -u16 PadUtils_GetCurButton(Input* input) { - return input->cur.button; -} - -u16 PadUtils_GetPressButton(Input* input) { - return input->press.button; -} - -s8 PadUtils_GetCurX(Input* input) { - return input->cur.stick_x; -} - -s8 PadUtils_GetCurY(Input* input) { - return input->cur.stick_y; -} - -void PadUtils_SetRelXY(Input* input, s32 x, s32 y) { - input->rel.stick_x = x; - input->rel.stick_y = y; -} - -s8 PadUtils_GetRelXImpl(Input* input) { - return input->rel.stick_x; -} - -s8 PadUtils_GetRelYImpl(Input* input) { - return input->rel.stick_y; -} - -s8 PadUtils_GetRelX(Input* input) { - return PadUtils_GetRelXImpl(input); -} - -s8 PadUtils_GetRelY(Input* input) { - return PadUtils_GetRelYImpl(input); -} - -void PadUtils_UpdateRelXY(Input* input) { - s32 curX = PadUtils_GetCurX(input); - s32 curY = PadUtils_GetCurY(input); - s32 relX; - s32 relY; - - if (curX > 7) { - relX = (curX < 0x43) ? curX - 7 : 0x43 - 7; - } else if (curX < -7) { - relX = (curX > -0x43) ? curX + 7 : -0x43 + 7; - } else { - relX = 0; - } - - if (curY > 7) { - relY = (curY < 0x43) ? curY - 7 : 0x43 - 7; - - } else if (curY < -7) { - relY = (curY > -0x43) ? curY + 7 : -0x43 + 7; - } else { - relY = 0; - } - - PadUtils_SetRelXY(input, relX, relY); -} diff --git a/src/boot/O2/rcp_utils.c b/src/boot/O2/rcp_utils.c deleted file mode 100644 index f587fad21..000000000 --- a/src/boot/O2/rcp_utils.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "ultra64.h" - -void RcpUtils_PrintRegisterStatus(void) { - u32 spStatus = __osSpGetStatus(); - u32 dpStatus = osDpGetStatus(); - - if (spStatus) { - // stubbed debug prints - } - - if (dpStatus) { - // stubbed debug prints - } -} - -void RcpUtils_Reset(void) { - RcpUtils_PrintRegisterStatus(); - // Flush the RDP pipeline and freeze clock counter - osDpSetStatus(DPC_SET_FREEZE | DPC_SET_FLUSH); - // Halt the RSP, disable interrupt on break and set "task done" signal - __osSpSetStatus(SP_SET_HALT | SP_SET_TASKDONE | SP_CLR_INTR_BREAK); - RcpUtils_PrintRegisterStatus(); -} diff --git a/src/boot/O2/stackcheck.c b/src/boot/O2/stackcheck.c deleted file mode 100644 index d08dbfcb5..000000000 --- a/src/boot/O2/stackcheck.c +++ /dev/null @@ -1,122 +0,0 @@ -#include "stackcheck.h" -#include "stdbool.h" -#include "stdint.h" - -StackEntry* sStackInfoListStart = NULL; -StackEntry* sStackInfoListEnd = NULL; - -void StackCheck_Init(StackEntry* entry, void* stackBottom, void* stackTop, u32 initValue, s32 minSpace, - const char* name) { - if (entry == NULL) { - sStackInfoListStart = NULL; - } else { - StackEntry* iter; - - entry->head = stackBottom; - entry->tail = stackTop; - entry->initValue = initValue; - entry->minSpace = minSpace; - entry->name = name; - iter = sStackInfoListStart; - while (iter) { - if (iter == entry) { - return; - } - iter = iter->next; - } - - entry->prev = sStackInfoListEnd; - entry->next = NULL; - - if (sStackInfoListEnd) { - sStackInfoListEnd->next = entry; - } - - sStackInfoListEnd = entry; - if (sStackInfoListStart == NULL) { - sStackInfoListStart = entry; - } - - if (entry->minSpace != -1) { - u32* addr = entry->head; - - while (addr < (u32*)entry->tail) { - *addr++ = entry->initValue; - } - } - } -} - -void StackCheck_Cleanup(StackEntry* entry) { - u32 inconsistency = false; - - if (entry->prev == NULL) { - if (entry == sStackInfoListStart) { - sStackInfoListStart = entry->next; - } else { - inconsistency = true; - } - } else { - entry->prev->next = entry->next; - } - - if (!entry->next) { - if (entry == sStackInfoListEnd) { - sStackInfoListEnd = entry->prev; - } else { - inconsistency = true; - } - } - - if (inconsistency) {} -} - -StackStatus StackCheck_GetState(StackEntry* entry) { - u32* last; - size_t used; - size_t free; - StackStatus status; - - for (last = entry->head; last < (u32*)entry->tail; last++) { - if (entry->initValue != *last) { - break; - } - } - - used = (uintptr_t)entry->tail - (uintptr_t)last; - free = (uintptr_t)last - (uintptr_t)entry->head; - - if (free == 0) { - status = STACK_STATUS_OVERFLOW; - } else if ((free < (size_t)entry->minSpace) && (entry->minSpace != -1)) { - status = STACK_STATUS_WARNING; - } else { - status = STACK_STATUS_OK; - } - - return status; -} - -u32 StackCheck_CheckAll(void) { - u32 ret = 0; - StackEntry* iter = sStackInfoListStart; - - while (iter != NULL) { - StackStatus state = StackCheck_GetState(iter); - - if (state != STACK_STATUS_OK) { - ret = 1; - } - iter = iter->next; - } - - return ret; -} - -u32 StackCheck_Check(StackEntry* entry) { - if (entry == NULL) { - return StackCheck_CheckAll(); - } else { - return StackCheck_GetState(entry); - } -} diff --git a/src/boot/O2/system_heap.c b/src/boot/O2/system_heap.c deleted file mode 100644 index 7ad21b6ae..000000000 --- a/src/boot/O2/system_heap.c +++ /dev/null @@ -1,123 +0,0 @@ -/** - * @file system_heap.c - * - * @note: - * Only SystemHeap_Init() is used, and is essentially just a wrapper for MallocInit(). - * - */ -#include "global.h" -#include "libc64/malloc.h" - -typedef void (*BlockFunc)(uintptr_t); -typedef void (*BlockFunc1)(uintptr_t, u32); -typedef void (*BlockFunc8)(uintptr_t, u32, u32, u32, u32, u32, u32, u32, u32); - -typedef struct InitFunc { - /* 0x0 */ uintptr_t nextOffset; - /* 0x4 */ void (*func)(void); -} InitFunc; // size = 0x8 - -void* sInitFuncs = NULL; - -char sNew[] = ""; - -UNK_TYPE1 D_80097508[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x80, 0x00, 0x00, - 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, -}; - -void* SystemHeap_Malloc(size_t size) { - if (size == 0) { - size = 1; - } - - return __osMalloc(&malloc_arena, size); -} - -void SystemHeap_Free(void* ptr) { - if (ptr != NULL) { - __osFree(&malloc_arena, ptr); - } -} - -void SystemHeap_RunBlockFunc(void* blk, size_t nBlk, size_t blkSize, BlockFunc blockFunc) { - uintptr_t pos = (uintptr_t)blk; - - for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) { - blockFunc(pos); - } -} - -void SystemHeap_RunBlockFunc1(void* blk, size_t nBlk, size_t blkSize, BlockFunc1 blockFunc) { - uintptr_t pos = (uintptr_t)blk; - - for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) { - blockFunc(pos, 2); - } -} - -void* SystemHeap_RunBlockFunc8(void* blk, size_t nBlk, size_t blkSize, BlockFunc8 blockFunc) { - if (blk == NULL) { - blk = SystemHeap_Malloc(nBlk * blkSize); - } - - if ((blk != NULL) && (blockFunc != NULL)) { - uintptr_t pos = (uintptr_t)blk; - - for (; pos < (uintptr_t)blk + (nBlk * blkSize); pos += (blkSize & ~0)) { - blockFunc(pos, 0, 0, 0, 0, 0, 0, 0, 0); - } - } - - return blk; -} - -void SystemHeap_RunBlockFunc1Reverse(void* blk, size_t nBlk, size_t blkSize, BlockFunc1 blockFunc, s32 shouldFree) { - uintptr_t pos; - uintptr_t start; - size_t maskedBlkSize; - - if (blk == NULL) { - return; - } - - if (blockFunc != NULL) { - start = (uintptr_t)blk; - maskedBlkSize = (blkSize & ~0); - pos = (uintptr_t)start + (nBlk * blkSize); - - while (pos > start) { - pos -= maskedBlkSize; - blockFunc(pos, 2); - } - } - - if (shouldFree) { - SystemHeap_Free(blk); - } -} - -void SystemHeap_RunInits(void) { - InitFunc* initFunc = (InitFunc*)&sInitFuncs; - u32 nextOffset = initFunc->nextOffset; - InitFunc* prev = NULL; - - while (nextOffset != 0) { - initFunc = (InitFunc*)((uintptr_t)initFunc + nextOffset); - - if (initFunc->func != NULL) { - (*initFunc->func)(); - } - - nextOffset = initFunc->nextOffset; - initFunc->nextOffset = (uintptr_t)prev; - prev = initFunc; - } - - sInitFuncs = prev; -} - -void SystemHeap_Init(void* start, size_t size) { - MallocInit(start, size); - SystemHeap_RunInits(); -} |
