Advertisement
paul_nicholls

Untitled

Jan 18th, 2024
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. I'm converting the c routine below into mainly ASM code, can anyone see what I might have done wrong when translating it to ASM?
  2.  
  3. PLAIN C VERSION
  4. void plexSort() {
  5. char nxt_idx;
  6. char nxt_y;
  7. char s;
  8. char sortedIDX;
  9. char m;
  10.  
  11. for(char m: 0..PLEX_COUNT-2) {
  12. nxt_idx = PLEX_SORTED_IDX[m+1];
  13. nxt_y = PLEX_YPOS[nxt_idx];
  14.  
  15. sortedIDX = PLEX_SORTED_IDX[m];
  16.  
  17. if(nxt_y < PLEX_YPOS[sortedIDX]) {
  18. //Shift values until we encounter a value smaller than nxt_y
  19. s = m;
  20. while (true) {
  21. PLEX_SORTED_IDX[s+1] = PLEX_SORTED_IDX[s];
  22. s--;
  23. sortedIDX = PLEX_SORTED_IDX[s];
  24.  
  25. if (s == 0xff) {break;}
  26. if (nxt_y >= PLEX_YPOS[sortedIDX]) {break;}
  27. }
  28. // store the mark at the found position
  29. s++;
  30. PLEX_SORTED_IDX[s] = nxt_idx;
  31. }
  32. }
  33. //Prepare for showing the sprites
  34. plex_show_idx = 0;
  35. plex_sprite_idx = 0;
  36. plex_sprite_msb = 1;
  37. plex_sprite_mask = 0xff^plex_sprite_msb;
  38.  
  39. plexFreePrepare();
  40. }
  41. -------------------------------------------------------------------
  42. ASSEMBLY CODE VERSION...
  43.  
  44. void plexSort() {
  45. char nxt_idx;
  46. char nxt_y;
  47. char s;
  48. char sortedIDX;
  49. char m;
  50.  
  51. asm {
  52. lda #0
  53. sta m
  54. !forLoop:
  55. // nxt_idx = PLEX_SORTED_IDX[m+1];
  56. lda m
  57. clc
  58. adc #1
  59. tax
  60. lda PLEX_SORTED_IDX,x
  61. sta nxt_idx
  62.  
  63. // nxt_y = PLEX_YPOS[nxt_idx];
  64. tax
  65. lda PLEX_YPOS,x
  66. sta nxt_y
  67.  
  68. // sortedIDX = PLEX_SORTED_IDX[m];
  69. ldx m
  70. lda PLEX_SORTED_IDX,x
  71. sta sortedIDX
  72.  
  73. // if nxt_y >= PLEX_YPOS[sortedIDX]; if true, skip while loop
  74. tax
  75. lda nxt_y
  76. cmp PLEX_YPOS,x
  77. bcs !whileEnd+ // skip while loop
  78.  
  79. // Shift values until we encounter a value smaller than nxt_y
  80. // s = m;
  81. lda m
  82. sta s
  83.  
  84. !whileLoop:
  85. // PLEX_SORTED_IDX[s+1] = PLEX_SORTED_IDX[s];
  86. lda s
  87. tay
  88. clc
  89. adc #1
  90. tax
  91. lda PLEX_SORTED_IDX,y
  92. sta PLEX_SORTED_IDX,x
  93. // s--;
  94. dec s
  95.  
  96. // sortedIDX = PLEX_SORTED_IDX[s]
  97. ldx s
  98. lda PLEX_SORTED_IDX,x
  99. sta sortedIDX
  100.  
  101. // if (s == 0xff) {break;}
  102. txa
  103. cmp #$ff
  104. beq !whileEnd+
  105.  
  106. // if (nxt_y >= PLEX_YPOS[sortedIDX]) {break;}
  107. ldx sortedIDX
  108. lda nxt_y
  109. cmp PLEX_YPOS,x
  110. bcs !whileEnd+ // >= exit loop
  111. jmp !whileLoop-
  112. !whileEnd:
  113. // end of while loop
  114. // store the mark at the found position
  115. // s++;
  116. inc s
  117.  
  118. // PLEX_SORTED_IDX[s] = nxt_idx;
  119. ldx s
  120. lda nxt_idx
  121. sta PLEX_SORTED_IDX,x
  122.  
  123. // loop if m <= PLEX_COUNT - 2
  124. inc m
  125. lda m
  126. cmp #PLEX_COUNT-1
  127. beq !+
  128. // not finished for loop yet...
  129. jmp !forLoop-
  130. !:
  131. }
  132.  
  133. // Prepare for showing the sprites
  134. plex_show_idx = 0;
  135. plex_sprite_idx = 0;
  136. plex_sprite_msb = 1;
  137. plex_sprite_mask = 0xff^plex_sprite_msb;
  138.  
  139. plexFreePrepare();
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement