summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartmists <mail@martmists.com>2020-04-26 08:54:18 +0200
committerGitHub <noreply@github.com>2020-04-26 08:54:18 +0200
commit08b8520c12dcbd6fa0d19770c512122df2303ed9 (patch)
tree74960adfb31cddcf30675a6b08e33443c9cbc98f
parentbbd690eb994cb95f6b9c2235996278fe586f8de2 (diff)
parente028229b82c0e20aa9a8c0a88eece2a41f4a0fd4 (diff)
Merge pull request #24 from red031000/os_lib
few cleanups to os_alloc.c
-rw-r--r--include/nitro/consts.h25
-rw-r--r--include/nitro/mmap.h18
-rw-r--r--include/nitro/os.h1
-rw-r--r--include/nitro/os_alloc.c145
-rw-r--r--include/nitro/os_protectionRegion.c17
-rw-r--r--include/nitro/os_protectionRegion.h47
-rw-r--r--undefined_syms.txt3
7 files changed, 231 insertions, 25 deletions
diff --git a/include/nitro/consts.h b/include/nitro/consts.h
index 3592500a..6fc71be2 100644
--- a/include/nitro/consts.h
+++ b/include/nitro/consts.h
@@ -13,6 +13,31 @@
#define HW_PSR_IRQ_DISABLE 0x80 // Disable IRQ
#define HW_PSR_IRQ_FIQ_DISABLE 0xc0 // Disable FIQ and IRQ
+#define HW_C6_PR_4KB 0x16
+#define HW_C6_PR_8KB 0x18
+#define HW_C6_PR_16KB 0x1a
+#define HW_C6_PR_32KB 0x1c
+#define HW_C6_PR_64KB 0x1e
+#define HW_C6_PR_128KB 0x20
+#define HW_C6_PR_256KB 0x22
+#define HW_C6_PR_512KB 0x24
+#define HW_C6_PR_1MB 0x26
+#define HW_C6_PR_2MB 0x28
+#define HW_C6_PR_4MB 0x2a
+#define HW_C6_PR_8MB 0x2c
+#define HW_C6_PR_16MB 0x2e
+#define HW_C6_PR_32MB 0x30
+#define HW_C6_PR_64MB 0x32
+#define HW_C6_PR_128MB 0x34
+#define HW_C6_PR_256MB 0x36
+#define HW_C6_PR_512MB 0x38
+#define HW_C6_PR_1GB 0x3a
+#define HW_C6_PR_2GB 0x3c
+#define HW_C6_PR_4GB 0x3e
+
+#define OS_CONSOLE_SIZE_MASK 0x00000003
+#define OS_CONSOLE_SIZE_4MB 0x00000001
+
#define OSi_GetArenaInfo() (*(OSArenaInfo*)HW_ARENA_INFO_BUF)
#define OSi_TRUNC(n, a) (((u32) (n)) & ~((a) - 1))
#define OSi_ROUND(n, a) (((u32) (n) + (a) - 1) & ~((a) - 1))
diff --git a/include/nitro/mmap.h b/include/nitro/mmap.h
index 4c781d5f..cbf54451 100644
--- a/include/nitro/mmap.h
+++ b/include/nitro/mmap.h
@@ -4,9 +4,27 @@
#define HW_MAIN_MEM 0x02000000
#define HW_MAIN_MEM_SIZE 0x00400000
#define HW_MAIN_MEM_EX_SIZE 0x00800000
+#define HW_MAIN_MEM_MAIN_SIZE 0x003E0000
+#define HW_MAIN_MEM_SHARED_SIZE 0x00001000
+#define HW_MAIN_MEM_DEBUGGER_OFFSET 0x00700000
+
+#define HW_ITCM 0x01FF8000
+#define HW_ITCM_SIZE 0x00008000
+
+#define HW_WRAM 0x037F8000
+
+#define HW_CARD_ROM_HEADER_SIZE 0x00000160
+#define HW_DOWNLOAD_PARAMETER_SIZE 0x00000020
#define HW_ARENA_INFO_BUF (HW_MAIN_MEM + 0x007ffda0) // Arena data structure
#define HW_ROM_HEADER_BUF (HW_MAIN_MEM + 0x007ffe00) // ROM registration area data buffer
+#define HW_RED_RESERVED (HW_MAIN_MEM + 0x007ff800) // Some kind of reserved data for shared memory
+#define HW_MAIN_MEM_MAIN_END (HW_MAIN_MEM + HW_MAIN_MEM_MAIN_SIZE)
+#define OSi_MAIN_ARENA_HI_DEFAULT (HW_MAIN_MEM_MAIN_END)
+#define OSi_MAINEX_ARENA_HI_DEFAULT (HW_MAIN_MEM + HW_MAIN_MEM_DEBUGGER_OFFSET)
+#define HW_ITCM_ARENA_HI_DEFAULT (HW_ITCM + HW_ITCM_SIZE)
+#define HW_SHARED_ARENA_HI_DEFAULT (HW_RED_RESERVED - HW_CARD_ROM_HEADER_SIZE - HW_DOWNLOAD_PARAMETER_SIZE)
+#define OSi_WRAM_MAIN_ARENA_HI_DEFAULT (HW_WRAM)
#endif \ No newline at end of file
diff --git a/include/nitro/os.h b/include/nitro/os.h
index f1422647..b8843a73 100644
--- a/include/nitro/os.h
+++ b/include/nitro/os.h
@@ -7,6 +7,7 @@
#include "types.h"
#include "consts.h"
+#include "os_protectionRegion.h"
#include "os_alloc.h"
#include "os_system.h"
diff --git a/include/nitro/os_alloc.c b/include/nitro/os_alloc.c
index 3115d106..b6749f27 100644
--- a/include/nitro/os_alloc.c
+++ b/include/nitro/os_alloc.c
@@ -5,17 +5,13 @@
#include "os_alloc.h"
#include "consts.h"
#include "os_system.h"
+#include "os_protectionRegion.h"
-extern BOOL OSi_MainExArenaEnabled; // TODO: located at 0x021d36f4
+extern BOOL OSi_MainExArenaEnabled;
extern BOOL OSi_Initialized; // TODO: located at 0x021d36f0
extern u32 OS_GetConsoleType();
-extern u32 OS_CONSOLE_SIZE_MASK;
-extern u32 OS_CONSOLE_SIZE_4MB;
-extern Cell* DLExtract(Cell* list, Cell* cell);
extern Cell* DLInsert(Cell* list, Cell* cell);
extern Cell* DLAddFront(Cell* list, Cell* cell);
-extern void OS_SetProtectionRegion1(u32 param);
-extern void OS_SetProtectionRegion2(u32 param);
void* OSiHeapInfo[OS_ARENA_MAX] = {
NULL,
@@ -30,6 +26,36 @@ void* OSiHeapInfo[OS_ARENA_MAX] = {
};
#ifdef MATCH_ASM
+asm static Cell* DLExtract(Cell* list, Cell* cell)
+{
+ ldr r3, [r1, #0x4]
+ cmp r3, #0x0
+ ldrne r2, [r1, #0x0]
+ strne r2, [r3, #0x0]
+ ldr r2, [r1, #0x0]
+ cmp r2, #0x0
+ ldreq r0, [r1, #0x4]
+ ldrne r1, [r1, #0x4]
+ strne r1, [r2, #0x4]
+ bx lr
+}
+#else
+static Cell* DLExtract(Cell* list, Cell* cell)
+{
+ if (cell->next) {
+ cell->prev = cell->next->prev;
+ }
+ if (cell->prev) {
+ cell->prev->next = cell->next;
+ return list;
+ }
+ else {
+ return cell->next;
+ }
+}
+#endif
+
+#ifdef MATCH_ASM
asm void* OS_AllocFromArenaHi(OSArenaId id, u32 size, u32 align) {
stmdb sp!,{ r4-r6, lr }
mov r4,r0
@@ -198,27 +224,105 @@ void* OS_GetInitArenaLo(OSArenaId id) {
#ifdef MATCH_ASM
asm void* OS_GetInitArenaHi(OSArenaId id) {
- // TODO: idk how to do switch case stuff properly in asm
+ stmdb sp!, {lr}
+ sub sp, sp, #0x4
+ cmp r0, #0x6
+ addls pc, pc, r0, lsl #0x2
+ b _020CC508
+_020CC41C:
+ b _020CC438
+ b _020CC508
+ b _020CC448
+ b _020CC488
+ b _020CC498
+ b _020CC4E8
+ b _020CC4F8
+_020CC438:
+ add sp, sp, #0x4
+ ldr r0, =OSi_MAIN_ARENA_HI_DEFAULT
+ ldmfd sp!, {lr}
+ bx lr
+_020CC448:
+ ldr r0, =OSi_MainExArenaEnabled
+ ldr r0, [r0]
+ cmp r0, #0x0
+ beq _020CC468
+ bl OS_GetConsoleType
+ and r0, r0, #0x3
+ cmp r0, #0x1
+ bne _020CC478
+_020CC468:
+ add sp, sp, #0x4
+ mov r0, #0x0
+ ldmfd sp!, {lr}
+ bx lr
+_020CC478:
+ add sp, sp, #0x4
+ mov r0, #OSi_MAINEX_ARENA_HI_DEFAULT
+ ldmfd sp!, {lr}
+ bx lr
+_020CC488:
+ add sp, sp, #0x4
+ mov r0, #HW_ITCM_ARENA_HI_DEFAULT
+ ldmfd sp!, {lr}
+ bx lr
+_020CC498:
+ ldr r0, =0x027E0000
+ ldr r1, =0x00000000
+ ldr r2, =0x00000400
+ add r3, r0, #0x3f80
+ cmp r1, #0x0
+ sub r2, r3, r2
+ bne _020CC4CC
+ ldr r1, =0x027E0080
+ add sp, sp, #0x4
+ cmp r0, r1
+ movcc r0, r1
+ ldmfd sp!, {lr}
+ bx lr
+_020CC4CC:
+ cmp r1, #0x0
+ ldrlt r0, =0x027E0080
+ add sp, sp, #0x4
+ sublt r0, r0, r1
+ subge r0, r2, r1
+ ldmfd sp!, {lr}
+ bx lr
+_020CC4E8:
+ add sp, sp, #0x4
+ ldr r0, =HW_SHARED_ARENA_HI_DEFAULT
+ ldmfd sp!, {lr}
+ bx lr
+_020CC4F8:
+ add sp, sp, #0x4
+ ldr r0, =OSi_WRAM_MAIN_ARENA_HI_DEFAULT;
+ ldmfd sp!, {lr}
+ bx lr
+_020CC508:
+ mov r0, #0x0
+ add sp, sp, #0x4
+ ldmia sp!, {lr}
+ bx lr
}
#else
void* OS_GetInitArenaHi(OSArenaId id) {
switch (id) {
case OS_ARENA_MAIN:
- return (void *)0x023e0000;
+ return (void *)OSi_MAIN_ARENA_HI_DEFAULT;
case OS_ARENA_MAINEX:
if (!OSi_MainExArenaEnabled || (OS_GetConsoleType() & OS_CONSOLE_SIZE_MASK) == OS_CONSOLE_SIZE_4MB) {
return (void *)0;
} else {
- return (void *)0x02700000;
+ return (void *)OSi_MAINEX_ARENA_HI_DEFAULT;
}
case OS_ARENA_ITCM:
- return (void *)0x02000000;
+ return (void *)HW_ITCM_ARENA_HI_DEFAULT;
case OS_ARENA_DTCM:
- return (void *)0x027e0080;
+ return (void *)0x027e0080; //todo pretty sure this is incorrect, no constant and doesn't match
case OS_ARENA_SHARED:
- return (void *)0x027ff680;
+ return (void *)HW_SHARED_ARENA_HI_DEFAULT;
case OS_ARENA_WRAM_MAIN:
- return (void *)0x037f8000;
+ return (void *)OSi_WRAM_MAIN_ARENA_HI_DEFAULT;
default:
return NULL;
}
@@ -287,19 +391,14 @@ _020CC5B8:
bx lr
}
#else
-void OS_InitArenaEx() {
- void* uVar1;
+void OS_InitArenaEx() { //todo figure out what compiler settings will get this to match
+ OS_SetArenaHi(2, OS_GetInitArenaHi(OS_ARENA_MAINEX));
+ OS_SetArenaLo(2, OS_GetInitArenaLo(OS_ARENA_MAINEX));
- uVar1 = OS_GetInitArenaHi(OS_ARENA_MAINEX);
- OS_SetArenaHi(2,uVar1);
- uVar1 = OS_GetInitArenaLo(OS_ARENA_MAINEX);
- OS_SetArenaLo(2,uVar1);
if (!OSi_MainExArenaEnabled || (OS_GetConsoleType() & OS_CONSOLE_SIZE_MASK) == OS_CONSOLE_SIZE_4MB) {
- return;
+ OS_SetProtectionRegion(1, HW_MAIN_MEM, 4MB);
+ OS_SetProtectionRegion(2, HW_MAIN_MEM_MAIN_END, 128KB);
}
- // TODO:
- // OS_SetProtectionRegion1(&UNK_0200002b);
- // OS_SetProtectionRegion2(0x023e0021);
}
#endif
diff --git a/include/nitro/os_protectionRegion.c b/include/nitro/os_protectionRegion.c
new file mode 100644
index 00000000..b70f9056
--- /dev/null
+++ b/include/nitro/os_protectionRegion.c
@@ -0,0 +1,17 @@
+//
+// Created by red031000 on 2020-04-24.
+//
+
+#include "os_protectionRegion.h"
+
+asm void OS_SetProtectionRegion1(u32 param)
+{
+ mcr p15, 0x0, r0, c6, c1, 0x0
+ bx lr
+}
+
+asm void OS_SetProtectionRegion2(u32 param)
+{
+ mcr p15, 0x0, r0, c6, c2, 0x0
+ bx lr
+} \ No newline at end of file
diff --git a/include/nitro/os_protectionRegion.h b/include/nitro/os_protectionRegion.h
new file mode 100644
index 00000000..7b212934
--- /dev/null
+++ b/include/nitro/os_protectionRegion.h
@@ -0,0 +1,47 @@
+//
+// Created by red031000 on 2020-04-24.
+//
+
+#ifndef POKEDIAMOND_OS_PROTECTIONREGION_H
+#define POKEDIAMOND_OS_PROTECTIONREGION_H
+
+#include "types.h"
+#include "consts.h"
+
+void OS_SetProtectionRegion1(u32 param);
+void OS_SetProtectionRegion2(u32 param);
+
+typedef enum
+{
+ OSi_PR_BASE_MASK_4KB = 0xfffff000,
+ OSi_PR_BASE_MASK_8KB = 0xffffe000,
+ OSi_PR_BASE_MASK_16KB = 0xffffc000,
+ OSi_PR_BASE_MASK_32KB = 0xffff8000,
+ OSi_PR_BASE_MASK_64KB = 0xffff0000,
+ OSi_PR_BASE_MASK_128KB = 0xfffe0000,
+ OSi_PR_BASE_MASK_256KB = 0xfffc0000,
+ OSi_PR_BASE_MASK_512KB = 0xfff80000,
+ OSi_PR_BASE_MASK_1MB = 0xfff00000,
+ OSi_PR_BASE_MASK_2MB = 0xffe00000,
+ OSi_PR_BASE_MASK_4MB = 0xffc00000,
+ OSi_PR_BASE_MASK_8MB = 0xff800000,
+ OSi_PR_BASE_MASK_16MB = 0xff000000,
+ OSi_PR_BASE_MASK_32MB = 0xfe000000,
+ OSi_PR_BASE_MASK_64MB = 0xfc000000,
+ OSi_PR_BASE_MASK_128MB = 0xf8000000,
+ OSi_PR_BASE_MASK_256MB = 0xf0000000,
+ OSi_PR_BASE_MASK_512MB = 0xe0000000,
+ OSi_PR_BASE_MASK_1GB = 0xc0000000,
+ OSi_PR_BASE_MASK_2GB = 0x80000000,
+ OSi_PR_BASE_MASK_4GB = 0x00000000
+} OSiProtectionRegionBaseMask;
+
+static inline u32 OSi_CalcPRParam(u32 address, u32 size, OSiProtectionRegionBaseMask mask) {
+ return ((address & mask) | size);
+}
+
+#define OS_SetProtectionRegion(regionNo, address, sizeStr) \
+ OS_SetProtectionRegion##regionNo(OSi_CalcPRParam(address, HW_C6_PR_##sizeStr, OSi_PR_BASE_MASK_##sizeStr) \
+ | 1)
+
+#endif //POKEDIAMOND_OS_PROTECTIONREGION_H
diff --git a/undefined_syms.txt b/undefined_syms.txt
index e6f5b4cf..a6954272 100644
--- a/undefined_syms.txt
+++ b/undefined_syms.txt
@@ -290,8 +290,6 @@ FUN_020C9418 = 0x020C9418;
FUN_020C9480 = 0x020C9480;
FUN_020C94E8 = 0x020C94E8;
FUN_020C9550 = 0x020C9550;
-OS_SetProtectionRegion1 = 0x020CC9EC;
-OS_SetProtectionRegion2 = 0x020CC9F4;
OS_SpinWait = 0x020CD380;
FUN_020D5150 = 0x020D5150;
FUN_020D5168 = 0x020D5168;
@@ -377,6 +375,7 @@ gBacklightTop_2 = 0x02106FA0;
gUnknown2106FB8 = 0x02106FB8;
gUnknown21C48B8 = 0x021C48B8;
gUnk021C4918 = 0x021C4918;
+OSi_MainExArenaEnabled = 0x021D36F4;
FUN_021D74E0 = 0x021D74E0;
FUN_021D76AC = 0x021D76AC;
gUnk021D76C8 = 0x021D76C8;