Get Combined Bit Value From Stored Pointer

From Final Fantasy Hacktics Wiki
Jump to navigation Jump to search
#   ROUTINE: GET COMBINED BIT VALUE FROM STORED POINTER
#       Gets the combined bit value based on a previously stored pointer.  The combined value can span several bytes.
#       The bits can also be read sequentially with repeated calls to this function.
#       Parameters:
#           r4 = (numBits) Number of bits to combine into the final value
#       Pseudo-Parameters: Set by Store Combined Bit Value Pointer
#           *0x8018e47c = (shouldResetReadBitIndex) Whether or not to reset the bit index to read from
#           *0x801ca650 = (storedPtr) The stored pointer to read bits from
#       Returns:
#           r2 = Combined bit value

8012b1ec: 10800028 beq r4,r0,0x8012b290             #   if (numBits == 0)
8012b1f0: 00001021 addu r2,r0,r0                    #       return 0;
8012b1f4: 3c028019 lui r2,0x8019
8012b1f8: 9042e47c lbu r2,-0x1b84(r2)               #   value = *0x8018e47c
8012b1fc: 00000000 nop
8012b200: 10400006 beq r2,r0,0x8012b21c             
8012b204: 00003821 addu r7,r0,r0                    #   combinedValue = 0
                                                    #   if (shouldResetReadBitIndex) {
8012b208: 34020007 ori r2,r0,0x0007
8012b20c: 3c018019 lui r1,0x8019
8012b210: ac22e480 sw r2,-0x1b80(r1)                #       invertedReadBitIndex = 7
8012b214: 3c018019 lui r1,0x8019
8012b218: a020e47c sb r0,-0x1b84(r1)                #       shouldResetReadBitIndex = false
                                                    #   }
8012b21c: 2484ffff addiu r4,r4,-0x0001              #   bitIndex = numBits - 1
8012b220: 2402ffff addiu r2,r0,-0x0001              #   -1
8012b224: 1082001a beq r4,r2,0x8012b290             #   if (bitIndex == -1)
8012b228: 00e01021 addu r2,r7,r0                    #       return combinedValue;
8012b22c: 340a0001 ori r10,r0,0x0001                #   1
8012b230: 34090007 ori r9,r0,0x0007                 #   7
8012b234: 2408ffff addiu r8,r0,-0x0001              #   -1
                                                    #   do {
8012b238: 3c06801d lui r6,0x801d
8012b23c: 8cc6a6f0 lw r6,-0x5910(r6)                #       storedPtr
8012b240: 3c038019 lui r3,0x8019
8012b244: 8c63e480 lw r3,-0x1b80(r3)                #       invertedReadBitIndex
8012b248: 90c20000 lbu r2,0x0000(r6)                #       storedValue = *storedPtr
8012b24c: 2465ffff addiu r5,r3,-0x0001              #       invertedReadBitIndex - 1
8012b250: 3c018019 lui r1,0x8019
8012b254: ac25e480 sw r5,-0x1b80(r1)                #       invertedReadBitIndex = invertedReadBitIndex - 1
8012b258: 00621007 srav r2,r2,r3                    #       storedValue >> invertedReadBitIndex
8012b25c: 30420001 andi r2,r2,0x0001                #       bitValue = (storedValue >> invertedReadBitIndex) & 0x01
8012b260: 10400002 beq r2,r0,0x8012b26c             #       if (bitValue != 0) {
8012b264: 008a1004 sllv r2,r10,r4                   #           1 << bitIndex
8012b268: 00e23825 or r7,r7,r2                      #           combinedValue = combinedValue | (1 << bitIndex)
                                                    #       }
8012b26c: 04a10006 bgez r5,0x8012b288               
8012b270: 2484ffff addiu r4,r4,-0x0001              #       bitIndex = bitIndex - 1
                                                    #       if (invertedReadBitIndex < 0) {
8012b274: 24c20001 addiu r2,r6,0x0001               #           storedPtr + 1
8012b278: 3c018019 lui r1,0x8019
8012b27c: ac29e480 sw r9,-0x1b80(r1)                #           invertedReadBitIndex = 7
8012b280: 3c01801d lui r1,0x801d
8012b284: ac22a6f0 sw r2,-0x5910(r1)                #           storedPtr = storedPtr + 1
                                                    #       }
8012b288: 1488ffeb bne r4,r8,0x8012b238             #   } while (bitIndex != -1);
8012b28c: 00e01021 addu r2,r7,r0                    #   return combinedValue;
8012b290: 03e00008 jr r31
8012b294: 00000000 nop