2023-12-16 02:46:52 +01:00
|
|
|
.include "string.h65"
|
|
|
|
.include "math.h65"
|
2023-12-27 16:56:14 +01:00
|
|
|
Export str,hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str, uint_to_hex_str
|
2023-12-16 02:46:52 +01:00
|
|
|
|
|
|
|
.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
|
|
|
|
|
|
|
|
|
2023-12-27 16:56:14 +01:00
|
|
|
;;********************************************************************************
|
|
|
|
;; @function Convert a 1 byte number into two hex characters
|
|
|
|
;; @param A: Size of the number in bytes
|
|
|
|
;; @param X: Offset onto ARG4, so that the number starts at `ARG4+x` (BE)
|
|
|
|
;; @param ARG2-3: Pointer to output string
|
|
|
|
;; @param Y: Offset onto the string in ARG2-3
|
|
|
|
;; @returns A: 0
|
|
|
|
;; @returns X: Offset onto ARG4, past the end of the number
|
|
|
|
;; @returns Y: Offset onto the the string in ARG2-3, past the end of the number
|
|
|
|
;; @returns N: Clear if success, else set
|
|
|
|
;; @modifies A,X,Y
|
|
|
|
;;********************************************************************************
|
|
|
|
.proc uint_to_hex_str
|
|
|
|
@loop:
|
|
|
|
cmp #0
|
|
|
|
beq @rts
|
|
|
|
pha
|
|
|
|
lda ARG4,x
|
|
|
|
phx
|
|
|
|
pha
|
|
|
|
and #%00001111
|
|
|
|
tax
|
|
|
|
lda HEX_CHARS_UPPER,x
|
|
|
|
sta (ARG2),y
|
|
|
|
pla
|
|
|
|
iny
|
|
|
|
beq @overflow2
|
|
|
|
div A,16
|
|
|
|
and #%00001111
|
|
|
|
tax
|
|
|
|
lda HEX_CHARS_UPPER,x
|
|
|
|
sta (ARG2),y
|
|
|
|
iny
|
|
|
|
beq @overflow2
|
|
|
|
plx
|
|
|
|
pla
|
|
|
|
inx
|
|
|
|
beq @overflow
|
|
|
|
bra @loop
|
|
|
|
@overflow2:
|
|
|
|
plx
|
|
|
|
pla
|
|
|
|
@overflow:
|
|
|
|
lda #$ff ; set n
|
|
|
|
@rts:
|
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
2023-12-16 02:46:52 +01:00
|
|
|
.rodata
|
|
|
|
HEX_CHARS_UPPER: .byte "0123456789ABCDEF"
|
|
|
|
HEX_CHARS_LOWER: .byte "0123456789abcdef"
|