Advertisement
marc_max

Untitled

Feb 12th, 2021
2,486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;hl=source offset
  2. ;de=target offset
  3. ;bc=length
  4. SECTION "Mem copy during HBLANK", ROM0[$1c39]
  5. ;this subroutine can copy to VRAM safely at any time
  6. mem_copy_safe: 
  7.     ldh     a, [rLCDC]
  8.     and     LCDCF_ON
  9.     jr      nz, .mem_copy_safe_start
  10.     jp      mem_copy ;if LCD is off we can copy at any time, call the basic mem_copy
  11.  
  12. ;check how many bytes we are trying to copy
  13. .mem_copy_safe_start:
  14.     ld      a, b
  15.     or      a
  16.     jr      nz, .start_copying_three_bytes ;there are at least 256 bytes to copy, start copying three bytes
  17.    
  18.     ld      a,c
  19.     cp      3
  20.     jr      c, .check_if_2_or_1_bytes_remaining ;if c<3, there are only 2 or 1 bytes
  21.    
  22.    
  23. .start_copying_three_bytes:
  24.     call    copy_three_bytes_during_hblank
  25.     jr      z, .copy_finished ;if bc%3==0, then there are no more bytes to copy, return (this could be replaced by ret z???)
  26.  
  27. .check_if_2_or_1_bytes_remaining:
  28.     ld      a, c
  29.     cp      2
  30.     jr      nz, .start_copying_last_byte ;there is only a byte to copy
  31.    
  32.     call    copy_two_bytes_during_hblank
  33.     jr      .copy_finished ;the last 2 bytes were copied (this could be replaced by ret???)
  34.  
  35.  
  36.  
  37.  
  38. .start_copying_last_byte:
  39.     call    copy_last_byte_during_hblank
  40.  
  41. .copy_finished:
  42.     ret
  43.  
  44.  
  45.  
  46.  
  47. copy_three_bytes_during_hblank:
  48.     ld      a,b
  49.     or      a
  50.     jr      nz, _1c66 ;there are at least 256 bytes to copy, start copying
  51.  
  52.     ld      a, c
  53.     cp      a, $03
  54.     ret     c    ;there are less than 3 bytes to copy, return
  55.  
  56. _1c66:
  57.     push    bc
  58.     ld      c, [hl]
  59.     inc     hl
  60.     ld      b, [hl]
  61.     inc     hl
  62.     ld      a, [hli]
  63.     push    hl
  64.     ld      l, e
  65.     ld      h, d
  66.     ld      d, a
  67.    
  68.     ;c, b and d=three bytes to copy
  69. .wait_for_non_vblank:
  70.     ldh     a, [rLY]
  71.     cp      a, 142
  72.     jr      nc, .wait_for_non_vblank
  73.    
  74.     di
  75. .wait_for_hblank1:
  76.     ld      a,[rSTAT]
  77.     and     STATF_LCD
  78.     jr      z, .wait_for_hblank1 ;do not continue if we are halfway hblank (as there wouldn't be enough time for 3 bytes)
  79.    
  80. .wait_for_hblank2:
  81.     ldh     a,[rSTAT]
  82.     and     STATF_LCD
  83.     jr      nz, .wait_for_hblank2 ;do not continue if we aren't in hblank yet
  84.    
  85.     ld      [hl],c
  86.     inc     hl
  87.     ld      [hl],b
  88.     inc     hl
  89.     ld      [hl],d
  90.     ei
  91.     inc     hl
  92.     ld      e,l
  93.     ld      d,h
  94.    
  95.     pop     hl
  96.     pop     bc
  97.     dec     bc
  98.     dec     bc
  99.     dec     bc
  100.     ld      a,b
  101.     or      c
  102.    
  103.     jr      nz, copy_three_bytes_during_hblank
  104.     ret
  105.  
  106.  
  107. copy_two_bytes_during_hblank:
  108.     ld      a,b
  109.     or      a
  110.     jr      nz, _1c9e ;there are at least 256 bytes to copy, start copying (however, it shouldn't never get here at this point, so might not be needed?)
  111.  
  112.     ld      a,c
  113.     cp      a,$02
  114.     ret     c ;there are less than 2 bytes to copy, return
  115.  
  116. _1c9e:
  117.     push    bc
  118.     ld      c,[hl]
  119.     inc     hl
  120.     ld      b,[hl]
  121.     inc     hl
  122.     push    hl
  123.     ld      l,e
  124.     ld      h,d
  125.  
  126.     ;c and b=two bytes to copy
  127. .wait_for_non_vblank:
  128.     ldh     a, [rLY]
  129.     cp      a, 142
  130.     jr      nc, .wait_for_non_vblank
  131.    
  132.     di
  133. .wait_for_hblank1:
  134.     ld      a,[rSTAT]
  135.     and     STATF_LCD
  136.     jr      z, .wait_for_hblank1 ;do not continue if either OAM or VRAM (vblank) are not in use
  137.    
  138. .wait_for_hblank2:
  139.     ldh     a,[rSTAT]
  140.     and     STATF_LCD
  141.     jr      nz, .wait_for_hblank2 ;do not continue if we aren't in hblank yet
  142.    
  143.     ld      [hl], c
  144.     inc     hl
  145.     ld      [hl], b
  146.     ei
  147.  
  148.     inc     hl
  149.     ld      e,l
  150.     ld      d,h
  151.     pop     hl
  152.     pop     bc
  153.     dec     bc
  154.     dec     bc
  155.     ld      a,b
  156.     or      c
  157.     jr      nz,copy_two_bytes_during_hblank
  158.     ret
  159.  
  160.  
  161. copy_last_byte_during_hblank:
  162.     ld      a,b
  163.     or      a
  164.     jr      nz,_1cd1 ;there are at least 256 bytes to copy, start copying (however, it shouldn't never get here at this point, so might not be needed?)
  165.    
  166.     ld      a,c
  167.     cp      a,$01
  168.     ret     c ;there are no bytes to copy, return
  169. _1cd1:
  170.     push    bc
  171.     ld      c,[hl]
  172.     inc     hl
  173.     push    hl 
  174.     ld      l,e
  175.     ld      h,d
  176.    
  177.     ;c=single byte to copy
  178. .wait_for_non_vblank:
  179.     ldh     a, [rLY]
  180.     cp      a, 142
  181.     jr      nc, .wait_for_non_vblank
  182.    
  183.     di
  184. .wait_for_hblank1:
  185.     ld      a,[rSTAT]
  186.     and     STATF_LCD
  187.     jr      z, .wait_for_hblank1 ;do not continue if either OAM or VRAM (vblank) are not in use
  188.    
  189. .wait_for_hblank2:
  190.     ldh     a,[rSTAT]
  191.     and     STATF_LCD
  192.     jr      nz, .wait_for_hblank2 ;do not continue if we aren't in hblank yet
  193.    
  194.     ld      [hl],c
  195.     ei
  196.     inc     hl
  197.     ld      e,l
  198.     ld      d,h
  199.     pop     hl
  200.     pop     bc
  201.     dec     bc
  202.  
  203.     ld      a,b
  204.     or      c
  205.     jr      nz, copy_last_byte_during_hblank
  206.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement