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.

194 lines
3.5KB

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