Advertisement
Stranck

Merge Sort - Pokémon Yellow [ACE]

Apr 5th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Ace::
  2.  
  3. WRAM1:DA7F    21 00 DC    ld hl, DC00                    ;RESET:                                                             INITIALIZE
  4. WRAM1:DA82    AF          xor a                          ;this will set a to 0
  5. WRAM1:DA83    22          ldi (hl), a                    ;Reset base index to 0
  6. WRAM1:DA84    22          ldi (hl), a                    ;Reset first index to 0
  7. WRAM1:DA85    22          ldi (hl), a                    ;Reset second index to 0
  8. WRAM1:DA86    36 01       ld (hl), 01                    ;Reset merge size to 1
  9. WRAM1:DA88    0E 01       ld c, 01
  10.  
  11.     .loop                ;LOAD ITEMS
  12. WRAM1:DA8A    21 01 DC    ld hl, DC01
  13. WRAM1:DA8D    7E          ld a, (hl)                     ;get the first index
  14. WRAM1:DA8E    CD FA DA    call getAddressItem [$DAFA]    ;get the ptr of the first item
  15. WRAM1:DA91    28 14       jr z, .oob1 [$DAA7]            ;if it is out of bounds, check if also the other item it is in .oob1
  16. WRAM1:DA93    56          ld d, (hl)                     ;load first item index id into d
  17. WRAM1:DA94    23          inc hl
  18. WRAM1:DA95    5E          ld e, (hl)                     ;load first item quantity into e
  19.  
  20. WRAM1:DA96    CD 16 DB    call getAddress2 [$DB16]       ;load second item
  21. WRAM1:DA99    28 07       jr z, .secondbigger [$DAA2]    ;if it is out of bounds, directly paste the first item that cannot be oob (if it was we're not here)
  22. WRAM1:DA9B    46          ld b, (hl)                     ;load second item index id into b
  23. WRAM1:DA9C    23          inc hl
  24. WRAM1:DA9D    4E          ld c, (hl)                     ;load second item quantity into c
  25.  
  26. WRAM1:DA9E    79          ld a, c                        ;prepeare for comparing the quantity
  27. WRAM1:DA9F    BB          cp e                           ;compare the quantity
  28. WRAM1:DAA0    38 0D       jr c, .firstBigger [$DAAF]     ;if first > second go to first bigger AAAAAAAAAAAAAAA
  29.  
  30.     .secondBigger
  31. WRAM1:DAA2    2E 01       ld l, 01                       ;set first index to incrase
  32. WRAM1:DAA4    D5          push de                        ;push first item into stack
  33. WRAM1:DAA5    18 0B       jr .complete [$DAB2]
  34.  
  35.     .oob1
  36. WRAM1:DAA7    CD 16 DB    call getAddress2 [$DB16]       ;load second item for check if it is oob
  37. WRAM1:DAAA    28 10       jr z, .Next [$DABC]            ;if yes, jump to next array
  38. WRAM1:DAAC    46          ld b, (hl)                     ;load second item index id into b
  39. WRAM1:DAAD    23          inc hl
  40. WRAM1:DAAE    4E          ld c, (hl)                     ;load second item quantity into c
  41.     .firstBigger
  42. WRAM1:DAAF    2E 02       ld l, 02                       ;set second index to incrase
  43. WRAM1:DAB1    C5          push bc                        ;push second item into stack
  44.  
  45.     .complete
  46. WRAM1:DAB2    26 DC       ld h, DC                       ;get the address of the index of the selected item
  47. WRAM1:DAB4    34          inc (hl)                       ;increment this index
  48. WRAM1:DAB5    21 03 DC    ld hl, DC03
  49. WRAM1:DAB8    4E          ld c, (hl)                     ;reload the merge size into c
  50. WRAM1:DAB9    C3 8A DA    jp .loop [$DA8A]               ;jump again to the loop
  51.  
  52.     .next
  53. WRAM1:DABC    79          ld a, c                        ;put the merge size in a
  54. WRAM1:DABD    87          add a                          ;douplicate merge size
  55. WRAM1:DABE    21 1C D3    ld hl, wNumBagItems [$D31C]
  56. WRAM1:DAC1    46          ld b, (hl)                     ;put wNumBagItems in b, for easy use in .sizedone
  57. WRAM1:DAC2    38 11       jr c, .sizeDone [$DAD5]        ;if a > FF goto .sizeDone
  58. WRAM1:DAC4    21 02 DC    ld hl, DC02
  59. WRAM1:DAC7    36 00       ld (hl), 00                    ;reset second index
  60. WRAM1:DAC9    2B          dec hl
  61. WRAM1:DACA    36 00       ld (hl), 00                    ;reset first index
  62. WRAM1:DACC    2B          dec hl
  63. WRAM1:DACD    86          add (hl)                       ;add base index. At this point: a[base index] = 2(merge size) + base index
  64. WRAM1:DACE    38 05       jr c, .sizeDone [$DAD5]        ;if a > FF goto .sizeDone
  65. WRAM1:DAD0    77          ld (hl), a                     ;save the new base index in ram
  66. WRAM1:DAD1    B8          cp b                           ;check if the new base index is oob
  67. WRAM1:DAD2    DA 8A DA    jp c, .loop [$DA8A]            ;if no, jump again to .loop
  68.  
  69.     .sizeDone
  70. WRAM1:DAD5    21 00 DC    ld hl, DC00
  71. WRAM1:DAD8    36 00       ld (hl), 00                    ;reset the base index
  72. WRAM1:DADA    68          ld l, b
  73. WRAM1:DADB    26 00       ld h, 00                       ;put wNumBagItems into hl
  74. WRAM1:DADD    29          add hl, hl                     ;douplicate wNumBagItems
  75. WRAM1:DADE    11 1C D3    ld de, D31C                    ;'cuz we can't do directly add hl, d31e we save bc, put this no in hl, add bc to hl and then restore bc
  76. WRAM1:DAE1    19          add hl, de                     ;add base inventory quantity index, so it point to the last item quantity
  77. WRAM1:DAE2    78          ld a, b                        ;put wNumBagItems into a, for the loop
  78.     .pasteToRam
  79. WRAM1:DAE3    D1          pop de                         ;get the item from stack
  80. WRAM1:DAE4    73          ld (hl), e                     ;write the item quantity in ram
  81. WRAM1:DAE5    2B          dec hl
  82. WRAM1:DAE6    72          ld (hl), d                     ;write the item id in ram
  83. WRAM1:DAE7    2B          dec hl
  84. WRAM1:DAE8    3D          dec a
  85. WRAM1:DAE9    C2 E3 DA    jp nz, .pasteToRam [$DAE3]
  86.  
  87. WRAM1:DAEC    79          ld a, c
  88. WRAM1:DAED    87          add a                          ;douplicate the merge size
  89. WRAM1:DAEE    D8          ret c                          ;if a > FF return
  90. WRAM1:DAEF    4F          ld c, a                        ;put the new merge size in c
  91. WRAM1:DAF0    21 03 DC    ld hl, DC03
  92. WRAM1:DAF3    71          ld (hl), c                     ;save the new merge size in ram
  93. WRAM1:DAF4    78          ld a, b                        ;restore wNumBagItems
  94. WRAM1:DAF5    B9          cp c                           ;check if all merge size are done, by testing if it is more all inventory items
  95. WRAM1:DAF6    D2 8A DA    jp nc, .loop [$DA8A]           ;if wNumBagItems > merge size, jump to loop
  96. WRAM1:DAF9    C9          ret                            ;else return
  97.  
  98.  
  99.  
  100.     getAddressItem::    ;a = index, c = merge size
  101. WRAM1:DAFA    B9          cp c                           ;check if AI is out of merge array
  102. WRAM1:DAFB    30 17       jr nc, .oob [$DB14]            ;if yes, jump to .oob
  103.     .noCheck
  104. WRAM1:DAFD    21 00 DC    ld hl, DC00
  105. WRAM1:DB00    86          add (hl)                       ;index + base index = absolute index (aka AI)
  106. WRAM1:DB01    38 11       jr c, .oob [$DB14]             ;if result is more than FF, it's obviously out of bounds (also 'cuz max inventory slots are FF)
  107. WRAM1:DB03    21 1C D3    ld hl, wNumBagItems [$D31C]
  108. WRAM1:DB06    BE          cp (hl)                        ;check if AI is out of our inventory
  109. WRAM1:DB07    30 0B       jr nc, .oob [$DB14]            ;if yes, jump to .oob
  110. WRAM1:DB09    6F          ld l, a
  111. WRAM1:DB0A    26 00       ld h, 00                       ;put AI into lower byte of hl
  112. WRAM1:DB0C    29          add hl, hl                     ;AI *= 2
  113. WRAM1:DB0D    C5          push bc
  114. WRAM1:DB0E    01 1D D3    ld bc, D31D
  115. WRAM1:DB11    09          add hl, bc                     ;AI += Address of the index id of the first item in inventory
  116. WRAM1:DB12    C1          pop bc
  117. WRAM1:DB13    C9          ret                            ;We are returning: 2*(BASE INDEX + A ) + BASE INVENTORY ADDRESS
  118.     .oob
  119. WRAM1:DB14    BF          cp a
  120. WRAM1:DB15    C9          ret                            ;return a zero flag if it is oob
  121.  
  122.     getAddress2::
  123. WRAM1:DB16    21 02 DC    ld hl, DC02
  124. WRAM1:DB19    C5          push bc                        ;save b and c
  125. WRAM1:DB1A    79          ld a, c                        ;copy the merge size into a
  126. WRAM1:DB1B    41          ld b, c                        ;copy the merge size into b
  127. WRAM1:DB1C    87          add a                          ;douplicate the merge size to avoid false oob
  128. WRAM1:DB1D    38 08       jr c, .oob2 [$DB27]            ;if 2a is oob call getaddresItem without checking if a is oob
  129. WRAM1:DB1F    4F          ld c, a                        ;set the douplicate merge size into c
  130. WRAM1:DB20    78          ld a, b                        ;restore the normal merge size into a
  131. WRAM1:DB21    86          add (hl)                       ;add the current index (this is the absoulute merge index)
  132. WRAM1:DB22    CD FA DA    call getAddressItem [$DAFA]    ;get the ptr of the second item
  133. WRAM1:DB25    18 05       jr .subEnd [$DB2C]
  134.     .oob2
  135. WRAM1:DB27    78          ld a, b                        ;restore the normal merge size into a
  136. WRAM1:DB28    86          add (hl)                       ;add the current index (this is the absoulute merge index)
  137. WRAM1:DB29    CD FD DA    call getAddressItem.noCheck [$DAFD]
  138.     .subEnd
  139. WRAM1:DB2C    C1          pop bc                         ;restore b and c
  140. WRAM1:DB2D    C9          ret
  141.  
  142.  
  143. ;DC00: base index
  144. ;DC01: first index
  145. ;DC02: second index
  146. ;DC03: dimension (aka merge size)
  147.  
  148. ;Raw bytes for easy paste in gbc:
  149. 21 00 dc af 22 22 22 36 01 0e 01 21 01 dc 7e cd fa da 28 14 56 23 5e cd 16 db 28 07 46 23 4e 79 bb 38 0d 2e 01 d5 18 0b cd 16 db 28 10 46 23 4e 2e 02 c5 26 dc 34 21 03 dc 4e c3 8a da 79 87 21 1c d3 46 38 11 21 02 dc 36 00 2b 36 00 2b 86 38 05 77 b8 da 8a da 21 00 dc 36 00 68 26 00 29 11 1c d3 19 78 d1 73 2b 72 2b 3d c2 e3 da 79 87 d8 4f 21 03 dc 71 78 B9 d2 8a da c9 b9 30 17 21 00 dc 86 38 11 21 1c d3 be 30 0b 6f 26 00 29 c5 01 1d d3 09 c1 c9 bf c9 21 02 dc c5 79 41 87 38 08 4f 78 86 cd fa da 18 05 78 86 cd fd da c1 c9
  150. Ace:: [$DA7F]
  151. 21 00 dc af 22 22 22 36 01 0e 01
  152. .loop [$DA8A]
  153. 21 01 dc 7e cd fa da 28 14 56 23 5e cd 16 db 28 07 46 23 4e 79 bb 38 0d
  154. .secondBigger [$DAA2]
  155. 2e 01 d5 18 0b
  156. .oob1 [$DAA7]
  157. cd 16 db 28 10 46 23 4e
  158. .firstBigger [$DAAF]
  159. 2e 02 c5
  160. .complete [$DAB2]
  161. 26 dc 34 21 03 dc 4e c3 8a da
  162. .next [$DABC]
  163. 79 87 21 1c d3 46 38 11 21 02 dc 36 00 2b 36 00 2b 86 38 05 77 b8 da 8a da
  164. .sizeDone [$DAD5]
  165. 21 00 dc 36 00 68 26 00 29 11 1c d3 19 78
  166. .pasteToRam [$DAE3]
  167. d1 73 2b 72 2b 3d c2 e3 da
  168. [$DAEC]
  169. 79 87 d8 4f 21 03 dc 71 78 B9 d2 8a da c9
  170. getAddressItem:: [$DAFA]
  171. b9 30 17
  172. .noCheck [$DAFD]
  173. 21 00 dc 86 38 11 21 1c d3 be 30 0b 6f 26 00 29 c5 01 1d d3 09 c1 c9
  174. .oob [$DB14]
  175. bf c9
  176. getAddress2:: [$DB16]
  177. 21 02 dc c5 79 41 87 38 08 4f 78 86 cd fa da 18 05
  178. .oob2 [$DB27]
  179. 78 86 cd fd da
  180. .subEnd [$DB2C]
  181. c1 c9
  182.  
  183. ;Used for this video: https://youtu.be/XTprmo04FKY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement