insertdelmem.z80 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;These routines were made by Zeda Thomas, feel free to use them!
  2. ;
  3. ;These utility routines are for resizing data within an edit buffer.
  4. ;The routines intended for use are:
  5. ; delmem
  6. ; insertmem
  7. ;
  8. ;These require the following variables:
  9. ; (buf_end) points to the end of the data in the buffer
  10. ; (buf_top) points to the end of the edit buffer itself
  11. ;
  12. ;Both routines take a pointer in HL, and a signed number of bytes in BC.
  13. ;For example, if you try to insert -3 bytes at HL, 3 bytes will be removed.
  14. ;Useful for when you just want to resize but don't know if you'll be inserting
  15. ;or removing.
  16. ;
  17. ;Also, though these routines are tested, there might be bugs. Since this messes
  18. ;with potentially large portions of RAM, if something does go wrong, there will
  19. ;be data loss. I made efforts to detect errors and exit with the carry flag set
  20. ;if an error was detected, but just know that I'm not perfect. (yet?)
  21. ;
  22. ;Credit me if you want; I'm not your mom (unless I am). But realistically, you
  23. ;will want to redirect potential bug reports to me, unless you feel like you can
  24. ;fix it. But if you can, I'll want you to redirect the fixes to me, please!
  25. insertmem_negate:
  26. xor a
  27. sub c
  28. ld c,a
  29. sbc a,a
  30. sub b
  31. ld b,a
  32. jr insertmem_pos
  33. insertmem:
  34. ;Input:
  35. ; HL points to where the mem needs to be inserted
  36. ; BC is the number of bytes to insert, signed
  37. ;Outputs:
  38. ; carry flag set if it failed
  39. ; carry flag is reset on success
  40. ; If BC was negative, bytes deleted, else inserted
  41. ; preserves HL
  42. ;Destroys:
  43. ; A,DE
  44. ; BC is 0 on success, else destroyed
  45. ld a,b
  46. add a,a
  47. jr c,delmem_negate
  48. insertmem_pos:
  49. or c
  50. ret z
  51. push hl
  52. ld h,b
  53. ld l,c
  54. ld de,(buf_end)
  55. add hl,de
  56. push de
  57. ex de,hl
  58. ld hl,(buf_top)
  59. or a
  60. sbc hl,de
  61. jr c,insertmem_fail
  62. pop hl
  63. ld (buf_end),de
  64. pop bc
  65. push hl
  66. or a
  67. sbc hl,bc
  68. ld b,h
  69. ld c,l
  70. pop hl
  71. ret z
  72. dec hl
  73. dec de
  74. lddr
  75. ex de,hl
  76. ret
  77. delmem_negate:
  78. xor a
  79. sub c
  80. ld c,a
  81. sbc a,a
  82. sub b
  83. ld b,a
  84. jr delmem_pos
  85. delmem:
  86. ;Input:
  87. ; HL points to where the mem needs to be removed
  88. ; BC is the number of bytes to remove, signed
  89. ;Outputs:
  90. ; carry flag set if it failed
  91. ; carry flag is reset on success
  92. ; If BC was negative, bytes inserted, else deleted
  93. ; preserves HL
  94. ;Destroys:
  95. ; A,DE
  96. ; BC is 0 on success, else destroyed
  97. ld a,b
  98. add a,a
  99. jr c,delmem_negate
  100. delmem_pos:
  101. or c
  102. ret z
  103. ;save HL to return
  104. push hl
  105. ;need to move (buf_end)-(HL+BC) bytes from HL+BC to HL
  106. ld d,h
  107. ld e,l
  108. add hl,bc
  109. ex de,hl
  110. ;need to move (buf_end)-DE bytes from DE to HL
  111. push hl
  112. ld hl,(buf_end)
  113. or a
  114. sbc hl,de
  115. jr c,delmem_fail ;don't want to delete more bytes than are left in the buffer!
  116. ld b,h
  117. ld c,l
  118. pop hl
  119. ex de,hl
  120. ;need to move BC bytes from HL to DE
  121. jr z,deledmem ;don't need to move any bytes
  122. ldir
  123. deledmem:
  124. ld (buf_end),de ;update buf_end
  125. delmem_fail:
  126. insertmem_fail:
  127. ;restore HL
  128. pop hl
  129. ret