Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
4.6KB

  1. ; core
  2. ;
  3. ; Routines used by pretty much all parts. You will want to include it first
  4. ; in your glue file.
  5. ; *** CONSTS ***
  6. ASCII_CR .equ 0x0d
  7. ASCII_LF .equ 0x0a
  8. ; *** CODE ***
  9. ; add the value of A into DE
  10. addDE:
  11. add a, e
  12. jr nc, .end ; no carry? skip inc
  13. inc d
  14. .end:
  15. ld e, a
  16. ret
  17. ; copy (DE) into DE, little endian style (addresses in z80 are always have
  18. ; their LSB before their MSB)
  19. intoDE:
  20. push af
  21. ld a, (de)
  22. inc de
  23. ex af, af'
  24. ld a, (de)
  25. ld d, a
  26. ex af, af'
  27. ld e, a
  28. pop af
  29. ret
  30. ; add the value of A into HL
  31. addHL:
  32. add a, l
  33. jr nc, .end ; no carry? skip inc
  34. inc h
  35. .end:
  36. ld l, a
  37. ret
  38. ; Write the contents of HL in (DE)
  39. writeHLinDE:
  40. push af
  41. ld a, l
  42. ld (de), a
  43. inc de
  44. ld a, h
  45. ld (de), a
  46. pop af
  47. ret
  48. ; jump to the location pointed to by IX. This allows us to call IX instead of
  49. ; just jumping it. We use IX because we never use this for arguments.
  50. callIX:
  51. jp (ix)
  52. ret
  53. ; Increase HL until the memory address it points to is null for a maximum of
  54. ; 0xff bytes. Returns the new HL value as well as the number of bytes iterated
  55. ; in A.
  56. findnull:
  57. push bc
  58. ld a, 0xff
  59. ld b, a
  60. .loop: ld a, (hl)
  61. cp 0
  62. jr z, .end
  63. inc hl
  64. djnz .loop
  65. .end:
  66. ; We ran 0xff-B loops. That's the result that goes in A.
  67. ld a, 0xff
  68. sub a, b
  69. pop bc
  70. ret
  71. ; Format the lower nibble of A into a hex char and stores the result in A.
  72. fmtHex:
  73. and a, 0xf
  74. cp 10
  75. jr nc, .alpha ; if >= 10, we have alpha
  76. add a, '0'
  77. ret
  78. .alpha:
  79. add a, 'A'-10
  80. ret
  81. ; Formats value in A into a string hex pair. Stores it in the memory location
  82. ; that HL points to. Does *not* add a null char at the end.
  83. fmtHexPair:
  84. push af
  85. ; let's start with the rightmost char
  86. inc hl
  87. call fmtHex
  88. ld (hl), a
  89. ; and now with the leftmost
  90. dec hl
  91. pop af
  92. push af
  93. and a, 0xf0
  94. rra \ rra \ rra \ rra
  95. call fmtHex
  96. ld (hl), a
  97. pop af
  98. ret
  99. ; Parse the hex char at A and extract it's 0-15 numerical value. Put the result
  100. ; in A.
  101. ;
  102. ; On success, the carry flag is reset. On error, it is set.
  103. parseHex:
  104. ; First, let's see if we have an easy 0-9 case
  105. cp '0'
  106. jr c, .error ; if < '0', we have a problem
  107. cp '9'+1
  108. jr nc, .alpha ; if >= '9'+1, we might have alpha
  109. ; We are in the 0-9 range
  110. sub a, '0' ; C is clear
  111. ret
  112. .alpha:
  113. call upcase
  114. cp 'A'
  115. jr c, .error ; if < 'A', we have a problem
  116. cp 'F'+1
  117. jr nc, .error ; if >= 'F', we have a problem
  118. ; We have alpha.
  119. sub a, 'A'-10 ; C is clear
  120. ret
  121. .error:
  122. scf
  123. ret
  124. ; Parses 2 characters of the string pointed to by HL and returns the numerical
  125. ; value in A. If the second character is a "special" character (<0x21) we don't
  126. ; error out: the result will be the one from the first char only.
  127. ; HL is set to point to the last char of the pair.
  128. ;
  129. ; On success, the carry flag is reset. On error, it is set.
  130. parseHexPair:
  131. push bc
  132. ld a, (hl)
  133. call parseHex
  134. jr c, .end ; error? goto end, keeping the C flag on
  135. rla \ rla \ rla \ rla ; let's push this in MSB
  136. ld b, a
  137. inc hl
  138. ld a, (hl)
  139. cp 0x21
  140. jr c, .single ; special char? single digit
  141. call parseHex
  142. jr c, .end ; error?
  143. or b ; join left-shifted + new. we're done!
  144. ; C flag was set on parseHex and is necessarily clear at this point
  145. jr .end
  146. .single:
  147. ; If we have a single digit, our result is already stored in B, but
  148. ; we have to right-shift it back.
  149. ld a, b
  150. and a, 0xf0
  151. rra \ rra \ rra \ rra
  152. dec hl
  153. .end:
  154. pop bc
  155. ret
  156. ; print null-terminated string pointed to by HL
  157. printstr:
  158. push af
  159. push hl
  160. .loop:
  161. ld a, (hl) ; load character to send
  162. or a ; is it zero?
  163. jr z, .end ; if yes, we're finished
  164. call aciaPutC
  165. inc hl
  166. jr .loop
  167. .end:
  168. pop hl
  169. pop af
  170. ret
  171. ; print A characters from string that HL points to
  172. printnstr:
  173. push bc
  174. push hl
  175. ld b, a
  176. .loop:
  177. ld a, (hl) ; load character to send
  178. call aciaPutC
  179. inc hl
  180. djnz .loop
  181. .end:
  182. pop hl
  183. pop bc
  184. ret
  185. ; Compares strings pointed to by HL and DE up to A count of characters. If
  186. ; equal, Z is set. If not equal, Z is reset.
  187. strncmp:
  188. push bc
  189. push hl
  190. push de
  191. ld b, a
  192. .loop:
  193. ld a, (de)
  194. cp (hl)
  195. jr nz, .end ; not equal? break early. NZ is carried out
  196. ; to the called
  197. cp 0 ; If our chars are null, stop the cmp
  198. jr z, .end ; The positive result will be carried to the
  199. ; caller
  200. inc hl
  201. inc de
  202. djnz .loop
  203. ; We went through all chars with success, but our current Z flag is
  204. ; unset because of the cp 0. Let's do a dummy CP to set the Z flag.
  205. cp a
  206. .end:
  207. pop de
  208. pop hl
  209. pop bc
  210. ; Because we don't call anything else than CP that modify the Z flag,
  211. ; our Z value will be that of the last cp (reset if we broke the loop
  212. ; early, set otherwise)
  213. ret
  214. ; Transforms the character in A, if it's in the a-z range, into its upcase
  215. ; version.
  216. upcase:
  217. cp 'a'
  218. ret c ; A < 'a'. nothing to do
  219. cp 'z'+1
  220. ret nc ; A >= 'z'+1. nothing to do
  221. ; 'a' - 'A' == 0x20
  222. sub 0x20
  223. ret