memcopy.asm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ; ----------------------------------------------------------------
  2. ; This file is released under the GPL v3 License
  3. ; Copyleft (k) 2008
  4. ; by Jose Rodriguez-Rosa (a.k.a. Boriel) <http://www.boriel.com>
  5. ; Use this file as a template to develop your own library file
  6. ; ----------------------------------------------------------------
  7. ; Emulates both memmove and memcpy C routines
  8. ; Blocks will safely copies if they overlap
  9. ; HL => Start of source block
  10. ; DE => Start of destiny block
  11. ; BC => Block length
  12. ;
  13. __MEMCPY:
  14. ;- PROC
  15. ;- LOCAL __MEMCPY2
  16. lda z80_l ;- push hl
  17. pha
  18. lda z80_h
  19. pha
  20. ;- add hl,bc
  21. ora z80_a ;- or a
  22. ;- sbc hl,de ; checks if DE > HL + BC
  23. pla ;- pop hl ; recovers HL. If Carry set => DE > HL
  24. sta z80_h
  25. pla
  26. sta z80_l
  27. ;- jr c, __MEMCPY2
  28. ;- ; Now checks if DE <= HL
  29. ;- sbc hl,de
  30. ;- add hl,de
  31. ;- jr nc, __MEMCPY2
  32. ;- dec bc
  33. lda z80_l ;- add hl,bc
  34. clc
  35. adc z80_c
  36. sta z80_l
  37. lda z80_h
  38. adc z80_b
  39. sta z80_h
  40. lda z80_e ;- ex de,hl
  41. ldx z80_l
  42. stx z80_e
  43. sta z80_l
  44. lda z80_d
  45. ldx z80_h
  46. stx z80_d
  47. sta z80_h
  48. lda z80_l ;- add hl,bc
  49. clc
  50. adc z80_c
  51. sta z80_l
  52. lda z80_h
  53. adc z80_b
  54. sta z80_h
  55. lda z80_e ;- ex de,hl
  56. ldx z80_l
  57. stx z80_e
  58. sta z80_l
  59. lda z80_d
  60. ldx z80_h
  61. stx z80_d
  62. sta z80_h
  63. inc z80_c ;- inc bc ; HL and DE point to the last byte position
  64. bne *+4
  65. inc z80_b
  66. ;- lddr ; Copies from end to beginning
  67. rts ;- ret
  68. __MEMCPY2:
  69. ldy #$00 ;- ldir
  70. ldx z80_b
  71. beq ldir_last_page
  72. ldir_loop:
  73. lda (z80_hl),y
  74. sta (z80_de),y
  75. iny
  76. bne ldir_loop
  77. inc z80_h
  78. inc z80_d
  79. dex
  80. bne ldir_loop
  81. ldir_last_page:
  82. lda z80_c
  83. beq ldir_end
  84. ldir_last_page_loop:
  85. lda (z80_hl),y
  86. sta (z80_de),y
  87. iny
  88. cpy z80_c
  89. bne ldir_last_page_loop
  90. ldir_end:
  91. stx z80_c
  92. stx z80_b
  93. tya
  94. clc
  95. adc z80_l
  96. sta z80_l
  97. bcc *+4
  98. inc z80_h
  99. tya
  100. clc
  101. adc z80_e
  102. sta z80_e
  103. bcc *+4
  104. inc z80_d
  105. rts ;- ret
  106. ;- ENDP