Compare commits

..

No commits in common. "92f26b0cf294f25699772c06fedd76bed37405c4" and "8b8a94e2e73c04048c1b71ba8d4ea39ba4ad311f" have entirely different histories.

2 changed files with 18 additions and 101 deletions

View File

@ -136,6 +136,18 @@ fmt_digit = $32
rts rts
.endproc .endproc
; todo error handling
.proc hex_char_to_int
cmp #'A'
bge @char
sbc #'0'
rts
@char:
sbc #('A' - $A)
rts
.endproc
;******************************************************************************** ;********************************************************************************
; @function Convert any int into hex ; @function Convert any int into hex
; @param ARG2-3: Address of output string ; @param ARG2-3: Address of output string
@ -148,9 +160,9 @@ fmt_digit = $32
; @modifies X,Y,A ; @modifies X,Y,A
;******************************************************************************** ;********************************************************************************
.proc int_to_hex_str .proc int_to_hex_str
bit cmp #0
beq @rts ; check done
@loop: @loop:
beq @rts ; check done
; load next byte ; load next byte
pha pha
lda ARG4,x lda ARG4,x
@ -176,106 +188,11 @@ fmt_digit = $32
plx plx
pla pla
dec dec
bne @loop
@rts: @rts:
rts rts
.endproc .endproc
;********************************************************************************
; @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 N: 0 == success, 1 == invalid char
; @returns A: The converted number
;********************************************************************************
.proc hex_char_to_int
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:
lda #$ff ; sets N flag
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 N == 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 N: 0 => success, 1 => invalid string
; @returns A: 0
; @returns X: Size of the number in bytes
; @returns Y: Offset onto string, past the end of the number
; @modifies X,Y,A
;********************************************************************************
.proc hex_to_int
bit #%11111111 ; check if accumulator is zero
beq @rts
ldx #0
bit #%00000001 ; check if accumulator is even
beq @loop ; even
; not even
stz ARG2
pha
bra @less_sig_char
@loop:
pha
; more significant char
lda (ARG0),y ; load next char
iny
jsr hex_char_to_int
bmi @invalid
rol
rol
rol
rol
sta ARG2,x
@less_sig_char:
; less significant char
lda (ARG0),y ; load next char
iny
jsr hex_char_to_int
bmi @invalid
ora ARG2,x
sta ARG2,x
inx
pla
dec
bne @loop
@invalid:
@rts:
rts
.endproc
TEST_FMT: .asciiz "%x -> %x -> %x:)" TEST_FMT: .asciiz "%x -> %x -> %x:)"
TEST_OUT: .asciiz "TESTOUT" TEST_OUT: .asciiz "TESTOUT"
HEX_CHARS_UPPER: .byte "0123456789ABCDEF" HEX_CHARS_UPPER: .byte "0123456789ABCDEF"

View File

@ -27,10 +27,10 @@ INCLUDE_UTILITY = 1
;;******************************************************************************** ;;********************************************************************************
;; @macro Generate a unique label ;; @macro Generate a unique label
;;******************************************************************************** ;;********************************************************************************
.macro genlabel ;.macro genlabel
.ident(.sprintf("generated_label%04X", _n_genlabel)) ; .ident(.sprintf("generated_label%04X", _n_genlabel))
_n_genlabel .set _n_genlabel + 1 ; _n_genlabel .set _n_genlabel + 1
.endmacro ;.endmacro
;;******************************************************************************** ;;********************************************************************************
;; @macro Export labels with a prefix ;; @macro Export labels with a prefix