Advertisement
Cthutu

DMA-based memory copy/fill routines.

Mar 17th, 2020
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;----------------------------------------------------------------------------------------------------------------------
  2. ;; Memory routines
  3. ;;----------------------------------------------------------------------------------------------------------------------
  4.  
  5. memcpy_dma:
  6.                 db      %11000011       ; R6: Reset
  7.                 db      %11000111       ; R6: Reset Port A timing
  8.                 db      %11001011       ; R6: Reset Port B timing
  9.  
  10.                 ; Register 0 set up
  11.                 db      %01111101       ; R0: A -> B, transfer mode
  12. mc_dma_src:     dw      0               ; Source address
  13. mc_dma_len:     dw      0               ; Length
  14.  
  15.                 ; Register 1 set up (Port A configuration)
  16. mc_dma_dir:     db      %01010100       ; R1: Port A config: increment, variable timing
  17.                 db      2               ; R1: Cycle length port A
  18.  
  19.                 ; Register 2 set up (Port B configuration)
  20. mc_dma_dir2:    db      %01010000       ; R2: Port B config: address fixed, variable timing
  21.                 db      2
  22.  
  23.                 ; Register 4 set up (Operation mode)
  24.                 db      %10101101       ; R4: Continuous mode, set destination address
  25. mc_dma_dest:    dw      0               ; Destination address
  26.  
  27.                 ; Register 5 set up (Some control)
  28.                 db      %10000010       ; R5: Stop at end of block; read active low
  29.  
  30.                 ; Register 6 (Commands)
  31.                 db      %11001111       ; R6: Load
  32.                 db      %10000111       ; R6: Enable DMA
  33.  
  34.  
  35. memcpy_len      EQU     $-memcpy_dma
  36.  
  37. memcpy:
  38.         ; Input:
  39.         ;       HL = source address
  40.         ;       DE = destination address
  41.         ;       BC = number of bytes
  42.         ;
  43.                 push    af
  44.                 push    bc
  45.                 push    de
  46.                 push    hl
  47.                 ld      a,%01010100
  48.                 ld      (mc_dma_dir),a          ; Set direction
  49.                 ld      a,%01010000
  50.                 ld      (mc_dma_dir2),a
  51.  
  52. transfer:
  53.                 ld      (mc_dma_src),hl         ; Set up the source address
  54.                 ld      (mc_dma_len),bc         ; Set up the length
  55.                 ld      (mc_dma_dest),de        ; Set up the destination address
  56.                 ld      hl,memcpy_dma
  57.                 ld      b,memcpy_len
  58.                 ld      c,$6b
  59.                 otir                            ; Send DMA program
  60.                 pop     hl
  61.                 pop     de
  62.                 pop     bc
  63.                 pop     af
  64.                 ret
  65.  
  66. memcpy_r:
  67.         ; Reverse version of memcpy - it will copy the memory backwards.
  68.         ;
  69.         ; Input:
  70.         ;       HL = source address
  71.         ;       DE = destination address
  72.         ;       BC = number of bytes
  73.         ;
  74.                 push    af
  75.                 push    bc
  76.                 push    de
  77.                 push    hl
  78.  
  79.                 ; Prepare the proper HL & DE
  80.                 add     hl,bc
  81.                 ex      de,hl
  82.                 add     hl,bc
  83.                 ex      de,hl
  84.                 dec     hl
  85.                 dec     de
  86.  
  87.                 ; Set up the DMA
  88.                 ld      a,%01000100
  89.                 ld      (mc_dma_dir),a
  90.                 ld      a,%01000000
  91.                 ld      (mc_dma_dir2),a
  92.                 jr      transfer
  93.  
  94. memmove:
  95.         ; Input:
  96.         ;       HL = source address
  97.         ;       DE = destination address
  98.         ;       BC = number of bytes
  99.         ;
  100.         ; Output:
  101.         ;       CF = 0: normal copy, 1: reverse copy
  102.         ;
  103.         ; Destroys:
  104.         ;       AF
  105.         ;
  106.                 call    max
  107.                 jr      nc,memcpy
  108.                 ex      de,hl
  109.                 jr      memcpy_r
  110.  
  111. memfill:
  112.         ; Input:
  113.         ;       HL = source address
  114.         ;       BC = number of bytes
  115.         ;       A = value to fill
  116.         ;
  117.         ; This is equivalent to memcpy(HL,HL+1,BC-1), when we load (HL) with A.
  118.         ;
  119.                 push    af
  120.                 push    de
  121.                 ld      (hl),a
  122.                 ld      d,h
  123.                 ld      e,l
  124.                 inc     de
  125.                 dec     bc
  126.                 call    memcpy
  127.                 inc     bc              ; Restore BC
  128.                 pop     de
  129.                 pop     af
  130.                 ret
  131.  
  132. ;;----------------------------------------------------------------------------------------------------------------------
  133. ;; max
  134.  
  135. max:
  136.         ; Input:
  137.         ;       HL = 1st value
  138.         ;       DE = 2nd value
  139.         ; Output:
  140.         ;       HL = maximum value
  141.         ;       DE = minimum value
  142.         ;       CF = 1 if DE was maximum
  143.         ;
  144.                 and     a
  145.                 sbc     hl,de
  146.                 jr      c,.choose_2nd   ; HL < DE?  Choose DE!
  147.                 add     hl,de           ; Restore HL
  148.                 ret
  149. .choose_2nd     add     hl,de
  150.                 ex      de,hl
  151.                 scf
  152.                 ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement