diff --git a/util/string-conversion.s65 b/util/string-conversion.s65 new file mode 100644 index 0000000..71f006c --- /dev/null +++ b/util/string-conversion.s65 @@ -0,0 +1,135 @@ +.include "string.h65" +.include "math.h65" +Export str,hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str + +.code +; +; FROM STRING +;;******************************************************************************** +;; @function Convert a hex char into a binary number +;; @details +;; The char must be one of [0-9,a-f,A-F]. +;; All results are only valid if N == 0. +;; @param A: Char to convert +;; @returns C: 0 == success, 1 == invalid char +;; @returns A: The converted number +;;******************************************************************************** +.proc hex_char_to_uint8 + sec + sbc #'0' + bmi @invalid ; char was in [0, '0') + cmp #10 + bcc @rts ; A in [0, 10) + ; char higher than '9' + sbc #('A' - '0') + bmi @invalid ; char was in ('0', 'A') + cmp #7 + bcc @hex_char ; A in [0, 6] + ; char higher than 'F' + sbc #('a' - 'A') + bmi @invalid ; char was in ('F', 'a') + cmp #17 + bcc @hex_char ; A in [0, 6] + ; char was in ('f', $ff] +@invalid: + sec + rts +@hex_char: + ; carry is not set + adc #10 +@rts: + rts +.endproc + + +;;******************************************************************************** +;; @function Convert a number encoded as hexadecimal string to a binary number +;; @details +;; The string must only consist of [0-9,a-f,A-F]. +;; All results are only valid if C == 0. +;; @param ARG0-1: Address of string +;; @param A: Number of chars to convert +;; @param Y: Offset onto string so that first char = (ARG0) + Y +;; @returns C: 0 => success, 1 => invalid string +;; @returns A: 0 +;; @returns X: Size of the number in bytes +;; @returns Y: Offset onto string, one past the end of the number +;; @returns ARG2-...: Number in big endian +;; @modifies X,Y,A +;;******************************************************************************** +.proc hex_str_to_uint + ldx #0 + cmp #0 ; check if accumulator is zero + beq @rts_clc + bit #%00000001 ; check if accumulator is even + beq @loop ; even + ; not even + stz ARG2 + inc ; later subtract 2 + pha + bra @less_sig_char +@loop: + pha + ; more significant char + lda (ARG0),y ; load next char + iny + jsr hex_char_to_uint8 + bcs @invalid_pla + rol + rol + rol + rol + sta ARG2,x +@less_sig_char: + ; less significant char + lda (ARG0),y ; load next char + iny + jsr hex_char_to_uint8 + bcs @invalid_pla + ora ARG2,x + sta ARG2,x + inx + pla + dec + dec + bne @loop + rts +@invalid_pla: ; carry must be set + pla ; doesnt update the carry +@invalid: + rts +@rts_clc: + clc + rts +.endproc + + +; +; TO STRING +; + + +;;******************************************************************************** +;; @function Convert a 1 byte number into two hex characters +;; @param A: Number to convert +;; @returns A: Most significant digit +;; @returns X: Least significant digit +;; @modifies A,X,Y +;;******************************************************************************** +.proc uint8_to_hex_str + pha + and #%00001111 + tay + ldx HEX_CHARS_UPPER,y + pla + div A,16 + and #%00001111 + tay + lda HEX_CHARS_UPPER,y + rts +.endproc + + +.rodata +HEX_CHARS_UPPER: .byte "0123456789ABCDEF" +HEX_CHARS_LOWER: .byte "0123456789abcdef"