dragonbane

WiiVC Memory Setup

Mar 11th, 2021 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. NOTE:
  2. MEM1 has to be lower on NTSC because of CIC chip emulation occuring only in the Fishing Pond which allocates extra RAM (83936 bytes)!
  3.  
  4. Strategy:
  5. -Game code ends at 0x8025F0B8
  6. r13 = 0x802647C0
  7. r2 (rtoc) = 0x80265E40
  8. r1 (SP) = 0x8026F0B8
  9.  
  10. MEM1:
  11. -Move MEM1 ArenaLo from 0x802710C0 to 0x802E10C0 (+0x70000)
  12. -Keep ArenaHi as is (0x81800000)
  13. -App entry point should be: 0x80272000 (securely past game code, small mem regions and the main thread stack)
  14. -MEM1 Heap Size: 0xD000
  15. -MEM1 Heap should use OSGetMEM1ArenaLo() - HEAP_SIZE to find the start point for the heap dynamically (ensure it ends before ArenaLo starts)
  16. -This gives us 401.6 KB of workable space for code and data in MEM1
  17.  
  18. NTSC: 8008a080
  19. ; lis r3, 0x802e Use 802e10c0 as MEM1 ArenaLo instead of 802710c0
  20. u32 0x3c60802e
  21.  
  22. JP:
  23.  
  24.  
  25. MEM2:
  26. -Move MEM2 ArenaLo from 0x90000800 to 0x90400800 (+0x400000)
  27. -Keep ArenaHi as is (0x935E0000)
  28. -MEM2 Heap Size: 0x400000
  29. -MEM2 Heap should use OSGetMEM2ArenaLo() - HEAP_SIZE to find the start point for the heap dynamically (ensure it ends before ArenaLo starts)
  30. -This gives us 4194.30 KB of workable space for data in MEM2
  31.  
  32. NTSC: 8008a118
  33. ; lis r3, 0x9040 Use 90400000 as MEM2 ArenaLo instead of 90000800
  34. u32 0x3c609040
  35.  
  36. ; ori r3, r3, 0x800 Use 90400800 as MEM2 ArenaLo instead of 90000800
  37. u32 0x60630800
  38.  
  39. JP:
  40.  
  41.  
  42. For dynamic memory:
  43. -MEMInitAllocatorForFrmHeap has less overhead and fragmentation risk. Could use one set of news for temp list, delete entire space again after init. Then allocate from the bottom for the permanent watch list and change new to only allocate from the top after that. Each processingThread loop then allocates everything from the top and uses MEMFreeToFrmHeap with mode 1 (MEM_FRMHEAP_FREE_HEAD) once at the end of each cycle to clear the entire space for the next cycle without touching the permanent watch list (also skips a bunch of deletes). On reset can clear everything with mode 3
  44. -Values (valuePtrs) when reading or task creation happens should be allocated from MEM2 and properly freed (so the host can read the 32 KB SRAM for example)
  45. -When managing heaps, avoid the MEM_HEAP_OPT_THREAD_SAFE flag as we only have a single point of access (not shared between threads)
  46.  
  47. Safe for frame heap:
  48. MemoryAccessor
  49. WriteCondition
  50. QueueTaskGeneric (need to watch for queueTaskPtr if QueueTaskWrite)
  51. TaskEntry
  52.  
  53. Unsafe for frame heap:
  54. QueueTaskWrite (vector)
  55. WatchEntry (vector)
  56. WatchEntryUpdate (can be cached ; includes all children)
  57. CallbackValueWatchEntry (can be cached ; includes all children)
  58. TaskResult socket (can be cached ; includes all children)
  59. TaskResult watcher (can be cached ; includes all children)
  60. TaskResultRead (can be cached ; includes all children)
  61.  
  62.  
  63.  
  64.  
  65. App Flow:
  66. -OSInit creates the default arenas. 0x802710C0 - 0x81800000 (MEM1) and 0x90000800 - 0x935E0000 (MEM2)
  67. -setupMainAppHeap moves both ArenaLo to the end (blocking the entire space)
  68. -A custom allocator and free function manages both spaces as a giant unified heap:
  69.  
  70. bool customMalloc(int *outPtr, u32 size);
  71. void customFree(int *ptr);
  72. (code see: https://github.com/zeldaret/oot-vc/blob/master/src/virtual_console/xlHeap.c)
  73.  
  74. customMalloc decides whether MEM1 or MEM2 is used depending on if the size is >= 40000000 ((size >> 30) & 1). True is MEM2, otherwise MEM1 is used. Max size is 16.7 MB (0x1000000) and the function automatically aligns the size to blocks of 32
  75.  
  76. -The system default heap plays no role
  77. -OSAllocFromHeap is rigged to use customMalloc instead. The size is automatically added to 0x70000000 to force MEM2 usage
  78. -OSFreeToHeap is rigged to use customFree instead
  79.  
  80. -Sub heaps can be created and managed by allocating space from customMalloc and then letting the extended Memory lib handle it (MEMCreateExpHeapEx, MEMInitAllocatorForExpHeap, MEMAllocFromAllocator, MEMFreeToAllocator)
  81. -Sub heaps are especially good practice from threads and used there so the main heap manager doesn't have to constantly lock/unlock a mutex and force other systems to wait
  82.  
  83.  
  84. Memory Details:
  85.  
  86. MEM1: (Original NTSC)
  87. Normal: 0x80000000 - 0x817FFFFF (game code ends at 0x8025F0B8)
  88. Lo: 0x802710C0 (later moved to 0x81800000 by setupMainAppHeap)
  89. Hi: 0x81800000
  90. createAppHeap is called with: 0x817FFFFC, 0x00553A2A, 0x0011EBB1
  91.  
  92. With 4 MB N64 RAM:
  93. manageHeap function is called with:
  94. 802710C0 (ArenaLo)
  95. 802710d8
  96. 802b10f8
  97. 809b31d4
  98. 80af9034
  99. 80df9054
  100. 80efd074
  101. 80f43594
  102. 81364138
  103. 8137646c
  104. 813805ac
  105. 81386c74
  106.  
  107. Allocated after boot:
  108. 81386cdc (loading a file)
  109. 81386d20 (home menu)
  110. 81387d40 (home menu)
  111.  
  112. Game only allocates up to 0x81388000 at most (18 MB). 0x81388000 - 0x81800000 should be free. 0x478000 (4685824) = 4.68 MB
  113.  
  114.  
  115. With 8 MB N64 RAM:
  116. manageHeap function is called with:
  117. 802710C0 (ArenaLo)
  118. 802710d8
  119. 802b10f8
  120. 809f2458
  121. 80efd074
  122. 80f43654
  123. 80f66138
  124. 81764138
  125. 81786a08
  126. 81786c74
  127.  
  128. Allocated after boot:
  129. 81786cdc (loading a file)
  130. 81786d20 (home menu)
  131. 81787d40 (home menu)
  132.  
  133. Game only allocates up to 0x81788000 at most. 0x81788000 - 0x81800000 should be free. 0x78000 (491520) = 491 KB = 491 KB space total (use 20 KB of that for an Exp heap for new/malloc allocations)
  134.  
  135. Allocator test: Max safe allocation is 0x79300 (0x79400 fails) (496 KB)
  136.  
  137.  
  138. MEM2: (Original NTSC)
  139. Normal: 0x90000000 - 0x93FFFFFF (upper 12-16MB used by system, game leaves 10.6 MB free)
  140. Lo: 0x90000800 (after boot: 0x933E0000, set by setupMainAppHeap)
  141. Hi: 0x935E0000
  142.  
  143. createAppHeap is called with: 0x933DFFFC, 0x250D7E
  144. manageHeap function is called with:
  145. 0x90000800 (ArenaLo)
  146. 90096820
  147. 9012c840
  148. 9014c860
  149. 9016c9a0
  150. 92a9ca00
  151.  
  152. Allocator test: Max safe allocation is 0x8F0000 (0x708F0000) (0x900000 (0x70900000) fails) (9371648 bytes = 9.37 MB)
  153.  
  154.  
  155. MEM2: (With 32 MB ROM patch instead of 43 MB)
  156. manageHeap function is called with:
  157. 0x90000800 (ArenaLo)
  158. 90096820
  159. 9012c840
  160. 9014c860
  161. 9016c9a0
  162. 92a9ca00
  163.  
  164. Allocator test: Max safe allocation is 0x1440000 (0x71440000) (0x1450000 (0x71450000) fails) (21233664 bytes = 21.23 MB)
Add Comment
Please, Sign In to add comment