![]() |
All About BATs - Printable Version +- Mario Kart Wii Gecko Codes, Cheats, & Hacks (https://mariokartwii.com) +-- Forum: Guides/Tutorials/How-To's (https://mariokartwii.com/forumdisplay.php?fid=45) +--- Forum: PowerPC Assembly (https://mariokartwii.com/forumdisplay.php?fid=50) +--- Thread: All About BATs (/showthread.php?tid=1963) |
All About BATs - Vega - 05-23-2022 All About BATs For advanced ASM coders/devs. This won't be useful for Wii Gecko Codes, but I've made this thread due to the Broadway Manual lacking detail on the BAT Registers and how to configure them. There are other PPC-based manuals that explain it, but said explanations are not all that "user-friendly". Chapter 1: Intro The BAT Registers are responsible for taking a Virtual Address and translating (converting) it to its Physical Address. As an Advanced ASM Coder/Dev, you should already know that mem1 and mem2 operate as Virtual Memory. Broadway needs to be 'told' how to convert any Virtual Memory Address to its Physical Address equivalent. BAT stands for Block Address Translation. There are 16 total BAT registers. There are 8 BATs available for areas of memory containing instructions (known as IBATs), and 8 BATs available for memory containing data (known as DBATs). Each BAT is 64 bits (double-word) in length and is split into upper 32 bit and lower 32 bit portions. Any area or region of memory that a BAT is responsible for is known as a Block. The BATs are mostly configured sometime shortly after the game is booted and a final BAT (DBAT3 for the Locked Cache) is configured after the bootstrap screen. From personal limited testing, it appears all Wii games setup the BATs in the same universal way. Here's a picture of the BATs while having the emulation randomly paused in MKWii at the Main Menu ![]() NOTE: Older Dolphin Dev Versions (such as 11xxx series and earlier) will not show proper values for the BATs! Use a newer version! Here's a list of SPR numbers for all the BATs. The list is formatted to directly be placed into an ASM Code assembler (i.e. PyiiASMH) Code: .set IBAT0U, 528 Chapter 2: BAT Structure Upper 32 bits Upper Portion:
BEPI: Block Effective Page Index. This is simply the beginning Virtual Address that you choose to use for the translation. If desired, you can set this to exactly match the Physical Address. The ending Virtual Address is determined by the memory block size in the BL field. BL: Block Length. This sets the amount (region/block) of Memory that the BAT will cover using BEPI as the starting address. Use the bit map below to determine the size. BL Bit Map:
Setting an invalid bit combo will corrupt the BAT and cause undefined behavior. If the exact size you want is not listed, then you need round up or split the sizes and use multiple BATs. Vs: Supervisor Valid Bit. If you are in Supervisor Mode (Machine State Register PR Bit Low), this bit will determine whether or not you are allowed to access that memory. Vp: User Valid Bit. If you are in User Mode (Machine State Register PR bit High), this bit will determine whether or not you are allowed to access that memory. Cheat Sheet for Upper Portion of BAT if using Vs & Vp as both set high. XXXXzzzz XXXX = Upper 16 bits of the starting Virtual Address (i.e. 0x8000 represents 0x80000000) zzzz = BL + Vs&Vp combo (whenever Vs and Vp are set high) zzzz value map (assumes Vs and Vp are set high)
Here's an example BAT Upper 32-bit Value using 8MB and 0x81000000 as the virtual start address; Vs and Vp both set high 0x810000FF Btw here's a handy memory size cheat sheet to help calculate the zzzz values. Take the end address minus the start address, then add 1. All calculations are in Hex obviously.. Example: (0x80FFFFFF - 0x80000000) + 1 = 0x01000000 (size of 16MB; use zzzz value of 01FF)
Chapter 3: BAT Structure Lower 32 bits Bits 0 - 14: BRPN Bits 15 - 24: Unused Bit 25: W Bit 26: I Bit 27: M Bit 28: G Bit 29: Unused Bits 30 & 31: PP BRPN: Block Register Page Number. This is the physical address that is used in conjuction to your Virtual Address, this must be a legit physical address. W: Write Through. If this bit is high, any store operations to cached memory are also written to physical memory, think of this like a dcbst instruction. If the bit is low, this is known as 'write-back'. In Write-Back mode, when store operations update cached memory, they do not instantly update physical memory. I: Cache-Inhibited. If this bit is high, no cache-ing of any kind shall occur for the specified block of memory. Blocks of memory that include access to something such as an I/O device should have the I bit set high. M: Memory coherence: When this bit is high, other devices or processors will be notified whenever the specified region of memory is accessed. Considering Broadway runs on 'its own' and doesn't need to notify other processors, M bit is set low on every BAT by every Wii menu, channel, game, etc. G: Guarded: This bit should be high if there are missing gaps in the specified memory block. This bit should also be set high for any memory that is not "well behaved". "Well behaved" simply means if the block of memory includes access to something such as an I/O device. I'm not sure on this but you may also need the G bit high if you had to roundup the BL bits. That would technically cause gaps in the memory block. Also G bit should be set high if you want to stop out-of-order operations (i.e loads, fetches, etc). Fyi, stores on Broadway are never done out-of-order in relation to each other. Finally, Store Gathering is automatically disabled (regardless of SGE bit value in HID0) whenever G bit is high. PP: Page Protection. Responsible for allowing access to the block of memory. Think of it like a firewall. See bit map below. PP bit map:
There is no available bit combo for Write only! If you are wanting a block of memory to allow full access in any 'situation', set both the Vs and Vp bits high in the upper portion of the BAT, and then set the PP bit combo to 10 for the lower portion. Here's an example of a BAT with its lower portion using a physical start address 0x01000000 with WIMG all low, and PP set to 10. We will use the upper portion example from Chapter 2 that used Virtual Start Address 0x81000000. The BAT will have Vs+Vp high with PP as 10 which means both user and supervisor have read+write access to the block of memory BAT value: 0x810000FF 01000002 Virtual Start Address: 0x81000000 Block Length: 8MB Vs and Vp high Physical Start Address: 0x01000000 WIMG all low PP = 10 (0x2); Read & Write IMPORTANT NOTEs: The IBATs can NEVER have the W and/or G bit set high. Doing so will corrupt the IBAT. Undefined behavior will occur. Also, regarding DBATs, whenever I bit is high, there is zero need to set W bit high. Having both W and I bit high (only applicable to DBATs) is considered as an invalid combo and should not be used. If you want to set any DBAT to simply be a representative of Physical Memory (but with the ability to use it Virtually), set I and G bits high, with W low. Chapter 4: Getting In & Out of Real Mode You must be in Real Mode when doing any invalidations/modifications to the BATs. Real Mode simply means you are executing in Physical Memory that is NOT part of an Exception. However, you do NOT need to be in Real Mode if you are simply copying BAT Register Data to the GPRs (maybe for something such as a Debug Report type code/source). Also, interrupts need to be disabled the entire time you are invalidating and/or modifying the BATs. Example Routine to get into Physical Mode~ Code: mfmsr r3 #Backup original MSR to some safe place if necessary Example Routine to get back into Virtual Mode~ Code: bl get_pc #Get Program Counter Alternatively instead of placing the address in srr0 and MSR into srr1, you could do something like this (do NOT use this for Exceptions btw)... Code: lis r0 #Will be your physical or virtual address **isync is needed to update the instruction context. If isync isn't used (remember Broadway is an out-of-order execution-type CPU), Broadway may fetch and execute instructions meant for virtual mode as real mode, and vice versa. The earlier two sources that use rfi do not need an isync because rfi, by design, is instruction synchronizing. Chapter 5: Invalidating BATs To modify a BAT, it MUST be invalidated first. Also when Broadway is powered on, the BATs are in an unknown state. All BATs should be marked invalid before configuring them (necessary for something such as writing a boot sequence code). To invalidate a BAT, both Vs and Vp bits of the Upper Portion must be set low. Super simple. The quickest way to invalidate a BAT is this... Code: li rX, 0 rX = Safe GPR to use for your code/source ZZZ = the SPR number of the BAT U = reminding you the write needs to be done the the UPPER portion Any modification/invalidation to an IBAT requires an isync instruction AFTERWARDS (Reference: PowerPC Microprocessor Family: The Programming Environments, table 2-22 (page 2-42)) Any modification/invalidation to a DBAT requires isync instructions BEFORE and AFTER (Reference: PowerPC Microprocessor Family: The Programming Environments, table 2-23 (page 2-43)) If you are invalidating a group of IBATS or DBATS, you do not need an isync after every single individual BAT invalidation. Example: Invalidate DBATS 2 thru 4~ Code: li r0, 0 Since IBAT invalidation doesn't require an isync beforehand, if you are writing a boot sequence code, you can invalidate all BATs simply like this... Code: li r0, 0 Chapter 6: Modifying BATs Correctly Assuming you have invalidated a BAT correctly, you can now modify it. The Lower 32-bits of the BAT MUST ALWAYS BE WRITTEN FIRST! Another rule is that you can only modify one BAT at a time. Example: Configure the 5th IBAT to have virtual address 0x80000000 represent physical address 0x00000000. 0x80000000 thru 0x80FFFFFF is 16MB in total size which is a perfect size match for setting the BL bits. Vs, Vp both High. PP = 10 (0x2). WIMG is all low. Code: #Pretend the IBAT was properly invalidated some time ago. Fyi no isync required beforehand since this is an IBAT One more example, set one BAT (DBAT2) to cover Virtual Uncached Mem2 (aka mem9). Mem9's full size is 64MB which is a perfect size match once again for the BL bits. Assume DBAT2 was properly invalidated beforehand. Code: #W = low Chapter 7: More Rules to Follow You cannot have multiple BATs (for the BAT same type; instruction or data), that do a translation to the same physical address. Example: You have IBAT0 translate 0x80000000 to physical 0x00000000 and IBAT6 translate 0x70000000 to physical 0x00000000. That's a no-go. Also, you cannot have memory blocks overlap from multiple BATs (for the same BAT type: instruction or data). Example: You have DBAT2 for 0x90000000 using block-length of 64MB (so this covers all of mem9 which is 0x90000000 thru 0x93FFFFFF. You then also have DBAT4 for 0x93000000 using block length of 16MB (so this covers just 0x93000000 thru 0x93FFFFFF). As a result memory addresses 0x93000000 thru 0x93FFFFFF are both part of DBAT2 and DBAT4. Whenever BATs are incorrectly invalidated and/or modified, the processor may enter into what is known as a Checkstop Condition. In simple terms, the processor halts without any Exception Routine being taken. Chapter 8: Hardware Register Notes By default (Broadway from a powered-on state), IBATs & DBATs 4 thru 7 are disabled. To enable these extra BATs, you need to set Bit 6 (SBE) of HID4 high. From personal limited testing, it appears all Wii applications that make modifications to HID4 will always set/keep this bit high All BAT M bits (for instruction fetching) can be overridden by setting bit 23 (IFEM) low on HID0. From personal limited testing, it appears all Wii applications set/keep this bit low. Please note that the I bit setting in a BAT will override the ICE, DCE, ILOCK, & DLOCK bits of HID0!!! (reference: Broadway User Manual pages 136 & 137) Chapter 9: Example Code of Messing Around with the BATs Here is a code I've clobbered together really quick that will run a couple of instructions in 0x50XXXXXX memory. This will most likely crash on most versions of Dolphin, because Dolphin is Dolphin. The code runs fine on real Hardware. If Dolphin does work for you then you will see (by stepping thru the code) that you will arrive at 0x50XXXXXX memory to execute two instructions. They set r4 to 0, then increment it by 1. In Dolphin's code view it will say something like.... "No RAM Contents Here". You won't be able to see the instructions in the Code View, but as you are stepping, you will see r4 get the proper updates in the Register tab/window. Code is hooked at old historical Shared Item Code Address (MKWii game ofc, credits to Guru for original Shared Item Code). Hit (or let a CPU) hit an item box for the code to execute. Btw to know that the code works on real Hardware, a Fib is set to be received from the Item Box when you (or the CPU) hits said item box. PAL C27BA164 0000001C 7D8000A6 5583045E 7C600124 48000005 7D6802A6 386B0020 5463007E 7C7A03A6 7C6000A6 54630732 7C7B03A6 4C000064 38000000 7C1083A6 4C00012C 38000002 3C605000 606301FF 7C1183A6 7C7083A6 4C00012C 7C6000A6 60630030 7C7B03A6 5563007E 64635000 38630064 7C7A03A6 4C000064 38800000 38840001 386B0088 5463013E 7C7A03A6 7C6000A6 54630732 7C7B03A6 4C000064 38000000 7C1083A6 4C00012C 38000002 3C608000 606301FF 7C1183A6 7C7083A6 4C00012C 7D9B03A6 5563013E 64638000 386300C4 7C7A03A6 4C000064 38600003 90770020 00000000 Code: #START ASSEMBLY Chapter 10: Example Source of Setting Up all BATs for Wii; Conclusion Here is a template you can use to invalidate then properly setup all BATs that the Wii needs to use. It assumes you are already in Real Mode and that r0 & r3 are safe to use. Code: #Invalidate all BATs The source uses certain specific BATs so it can be compatible with being 'injected' at any point in a Wii Game/Title or in a Boot Sequence. Keep in mind that Wii Games setup the Locked Cache BAT immediately *after* the Strap Screen, which will override the DBAT3 config of the above source if you inject it before that moment. But it doesn't really matter because the Locked Cache BAT (DBAT3) can be any size due to 2 reasons: The 16KB locked Cache size is covered by the bare minimum bat size config, and the size of this BAT can be anything due to the nature of how Locked Cache operates. Locked Cache is a very 'odd' feature of Broadway, such as it uses a fake Physical Address in the BAT. It's hard to explain why. Maybe one day, I'll make a tutorial about that to clear some confusion. RE: All About BATs - marito_yo - 11-07-2024 I am having issues with trying to get in and out of real mode I made a test code using what you gave in chapter 4, however when testing this in a real Wii it just seems to hang, but on dolphin is runs fine. the code doesn't do anything special, it just goes in, then goes out I find it odd it just hangs on Wii RE: All About BATs - Vega - 11-07-2024 Are you masking interrupts? If not, they should be masked. RE: All About BATs - marito_yo - 12-12-2024 (11-07-2024, 11:49 PM)Vega Wrote: Are you masking interrupts? If not, they should be masked. I fixed my issue, I forgot what I did but I think it might have something to do with isync? RE: All About BATs - Vega - 12-23-2024 Late reply, but did you omit some isyncs and this caused the hang? As an fyi isync does the following... 1. Waits for all currently executing instructions to complete 2. Purges all instructions that are sitting in the instruction queue 3. Refetches instructions into the instruction queue. This may not make sense unless you know how the instruction pipeline works and what it "looks" like via some sort of diagram (chapter 6 of Broadway Manual provides such a diagram) Because isync does a purge then refetch, you can use isync to enforce instruction context synchronization. This means if you change/alter the privilege or other attributes of how Instructions get executed, you can enforce this change immediately and in program order with isync. Without isync, these changes wont be immediate and won't be in order since Broadway executes instructions out of order. For modifying IBATs, you want these changes immediately or else you can cause a fault. If you are in Real Mode, and you've changed your IBATs to use mem80 (Virtual Address), then jump to Virtual Mode w/o isync, its a high chance the instructions in the program (that sit in Virtual addresses), were already fetched with the property/context of Real Mode. Now when said instructions get executed, BOOM an exception occurs. Placing an isync after modifying the IBATs before going to Virtual Mode will purge those faulty instructions and refetch them with the proper context My Cache tutorial explains how isync can be used to prevent bugs within Self Modifying Code - https://mariokartwii.com/showthread.php?tid=1958&pid=9227#pid9227 |