Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;----------------------------------------------------------------------------------------------------------------------
- ;; Memory routines
- ;;----------------------------------------------------------------------------------------------------------------------
- memcpy_dma:
- db %11000011 ; R6: Reset
- db %11000111 ; R6: Reset Port A timing
- db %11001011 ; R6: Reset Port B timing
- ; Register 0 set up
- db %01111101 ; R0: A -> B, transfer mode
- mc_dma_src: dw 0 ; Source address
- mc_dma_len: dw 0 ; Length
- ; Register 1 set up (Port A configuration)
- mc_dma_dir: db %01010100 ; R1: Port A config: increment, variable timing
- db 2 ; R1: Cycle length port A
- ; Register 2 set up (Port B configuration)
- mc_dma_dir2: db %01010000 ; R2: Port B config: address fixed, variable timing
- db 2
- ; Register 4 set up (Operation mode)
- db %10101101 ; R4: Continuous mode, set destination address
- mc_dma_dest: dw 0 ; Destination address
- ; Register 5 set up (Some control)
- db %10000010 ; R5: Stop at end of block; read active low
- ; Register 6 (Commands)
- db %11001111 ; R6: Load
- db %10000111 ; R6: Enable DMA
- memcpy_len EQU $-memcpy_dma
- memcpy:
- ; Input:
- ; HL = source address
- ; DE = destination address
- ; BC = number of bytes
- ;
- push af
- push bc
- push de
- push hl
- ld a,%01010100
- ld (mc_dma_dir),a ; Set direction
- ld a,%01010000
- ld (mc_dma_dir2),a
- transfer:
- ld (mc_dma_src),hl ; Set up the source address
- ld (mc_dma_len),bc ; Set up the length
- ld (mc_dma_dest),de ; Set up the destination address
- ld hl,memcpy_dma
- ld b,memcpy_len
- ld c,$6b
- otir ; Send DMA program
- pop hl
- pop de
- pop bc
- pop af
- ret
- memcpy_r:
- ; Reverse version of memcpy - it will copy the memory backwards.
- ;
- ; Input:
- ; HL = source address
- ; DE = destination address
- ; BC = number of bytes
- ;
- push af
- push bc
- push de
- push hl
- ; Prepare the proper HL & DE
- add hl,bc
- ex de,hl
- add hl,bc
- ex de,hl
- dec hl
- dec de
- ; Set up the DMA
- ld a,%01000100
- ld (mc_dma_dir),a
- ld a,%01000000
- ld (mc_dma_dir2),a
- jr transfer
- memmove:
- ; Input:
- ; HL = source address
- ; DE = destination address
- ; BC = number of bytes
- ;
- ; Output:
- ; CF = 0: normal copy, 1: reverse copy
- ;
- ; Destroys:
- ; AF
- ;
- call max
- jr nc,memcpy
- ex de,hl
- jr memcpy_r
- memfill:
- ; Input:
- ; HL = source address
- ; BC = number of bytes
- ; A = value to fill
- ;
- ; This is equivalent to memcpy(HL,HL+1,BC-1), when we load (HL) with A.
- ;
- push af
- push de
- ld (hl),a
- ld d,h
- ld e,l
- inc de
- dec bc
- call memcpy
- inc bc ; Restore BC
- pop de
- pop af
- ret
- ;;----------------------------------------------------------------------------------------------------------------------
- ;; max
- max:
- ; Input:
- ; HL = 1st value
- ; DE = 2nd value
- ; Output:
- ; HL = maximum value
- ; DE = minimum value
- ; CF = 1 if DE was maximum
- ;
- and a
- sbc hl,de
- jr c,.choose_2nd ; HL < DE? Choose DE!
- add hl,de ; Restore HL
- ret
- .choose_2nd add hl,de
- ex de,hl
- scf
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement