Mirror of CollapseOS
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

215 lines
3.8KB

  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. ; Increase HL until the memory address it points to is null for a maximum of
  18. ; 0xff bytes. Returns the new HL value as well as the number of bytes iterated
  19. ; in A.
  20. findnull:
  21. push bc
  22. ld a, 0xff
  23. ld b, a
  24. .loop: ld a, (hl)
  25. cp 0
  26. jr z, .end
  27. inc hl
  28. djnz .loop
  29. .end:
  30. ; We ran 0xff-B loops. That's the result that goes in A.
  31. ld a, 0xff
  32. sub a, b
  33. pop bc
  34. ret
  35. ; Format the lower nibble of A into a hex char and stores the result in A.
  36. fmtHex:
  37. and a, 0xf
  38. cp 10
  39. jr nc, .alpha ; if >= 10, we have alpha
  40. add a, '0'
  41. ret
  42. .alpha:
  43. add a, 'A'-10
  44. ret
  45. ; Formats value in A into a string hex pair. Stores it in the memory location
  46. ; that HL points to. Does *not* add a null char at the end.
  47. fmtHexPair:
  48. push af
  49. ; let's start with the rightmost char
  50. inc hl
  51. call fmtHex
  52. ld (hl), a
  53. ; and now with the leftmost
  54. dec hl
  55. pop af
  56. push af
  57. and a, 0xf0
  58. rra \ rra \ rra \ rra
  59. call fmtHex
  60. ld (hl), a
  61. pop af
  62. ret
  63. ; jump to the location pointed to by HL. This allows us to call HL instead of
  64. ; just jumping it.
  65. jumpHL:
  66. jp hl
  67. ret
  68. ; Parse the hex char at A and extract it's 0-15 numerical value. Put the result
  69. ; in A.
  70. ;
  71. ; On success, the carry flag is reset. On error, it is set.
  72. parseHex:
  73. ; First, let's see if we have an easy 0-9 case
  74. cp '0'
  75. jr c, .error ; if < '0', we have a problem
  76. cp '9'+1
  77. jr nc, .alpha ; if >= '9'+1, we might have alpha
  78. ; We are in the 0-9 range
  79. sub a, '0' ; C is clear
  80. ret
  81. .alpha:
  82. call upcase
  83. cp 'A'
  84. jr c, .error ; if < 'A', we have a problem
  85. cp 'F'+1
  86. jr nc, .error ; if >= 'F', we have a problem
  87. ; We have alpha.
  88. sub a, 'A'-10 ; C is clear
  89. ret
  90. .error:
  91. scf
  92. ret
  93. ; Parses 2 characters of the string pointed to by HL and returns the numerical
  94. ; value in A. If the second character is a "special" character (<0x21) we don't
  95. ; error out: the result will be the one from the first char only.
  96. ;
  97. ; On success, the carry flag is reset. On error, it is set.
  98. parseHexPair:
  99. push bc
  100. push hl
  101. ld a, (hl)
  102. call parseHex
  103. jr c, .end ; error? goto end, keeping the C flag on
  104. rla \ rla \ rla \ rla ; let's push this in MSB
  105. ld b, a
  106. inc hl
  107. ld a, (hl)
  108. cp 0x21
  109. jr c, .single ; special char? single digit
  110. call parseHex
  111. jr c, .end ; error?
  112. or b ; join left-shifted + new. we're done!
  113. ; C flag was set on parseHex and is necessarily clear at this point
  114. jr .end
  115. .single:
  116. ; If we have a single digit, our result is already stored in B, but
  117. ; we have to right-shift it back.
  118. ld a, b
  119. and a, 0xf0
  120. rra \ rra \ rra \ rra
  121. .end:
  122. pop hl
  123. pop bc
  124. ret
  125. ; print null-terminated string pointed to by HL
  126. printstr:
  127. push af
  128. push hl
  129. .loop:
  130. ld a, (hl) ; load character to send
  131. or a ; is it zero?
  132. jr z, .end ; if yes, we're finished
  133. call aciaPutC
  134. inc hl
  135. jr .loop
  136. .end:
  137. pop hl
  138. pop af
  139. ret
  140. ; print A characters from string that HL points to
  141. printnstr:
  142. push bc
  143. push hl
  144. ld b, a
  145. .loop:
  146. ld a, (hl) ; load character to send
  147. call aciaPutC
  148. inc hl
  149. djnz .loop
  150. .end:
  151. pop hl
  152. pop bc
  153. ret
  154. ; Compares strings pointed to by HL and DE up to A count of characters. If
  155. ; equal, Z is set. If not equal, Z is reset.
  156. strncmp:
  157. push bc
  158. push hl
  159. push de
  160. ld b, a
  161. .loop:
  162. ld a, (de)
  163. cp (hl)
  164. jr nz, .end ; not equal? break early
  165. inc hl
  166. inc de
  167. djnz .loop
  168. .end:
  169. pop de
  170. pop hl
  171. pop bc
  172. ; Because we don't call anything else than CP that modify the Z flag,
  173. ; our Z value will be that of the last cp (reset if we broke the loop
  174. ; early, set otherwise)
  175. ret
  176. ; Transforms the character in A, if it's in the a-z range, into its upcase
  177. ; version.
  178. upcase:
  179. cp 'a'
  180. ret c ; A < 'a'. nothing to do
  181. cp 'z'+1
  182. ret nc ; A >= 'z'+1. nothing to do
  183. ; 'a' - 'A' == 0x20
  184. sub 0x20
  185. ret