.include "string.h65" .include "math.h65" Export str, strf, int8_to_hex_str .code ;******************************************************************************** ; @function Format a string ; @details ; If there is no value to be read, the Pz will be set ; Formats: ; - x: unsigned hex integer (1 byte) -> 2 chars ; - X: unsigned hex integer (2 byte) -> 4 chars TODO ; - u: unsigned decimal integer (1 byte) TODO ; - U: unsigned decimal integer (2 bytes) TODO ; @param ARG0-1: Format string address ; @param ARG2-3: Output string address ; @param ARG4+: Additional parameters ; @returns ; @modifies: A, X, Y ;******************************************************************************** out_idx := str::out_idx fmt_idx := str::fmt_idx .proc strf stz out_idx stz fmt_idx ldy #0 ; position in string ldx #0 ; index of format args @loop: ldy fmt_idx lda (ARG0),y beq @null cmp #'%' beq @percent @normal_char: ; store A in output string ldy out_idx sta (ARG2),y inc fmt_idx inc out_idx beq @out_overflow bra @loop @percent: ; check for format in next position iny sty fmt_idx lda (ARG0),y beq @null ; formats cmp #'x' beq @format_hex1 bra @normal_char @format_hex1: ; 1 byte hex -> 2 chars lda ARG4,x phx jsr int8_to_hex_str ldy out_idx sta (ARG2),y ; most sig digit iny beq @out_overflow txa sta (ARG2),y ; least sig digit iny beq @out_overflow sty out_idx plx inx ; 1 byte of args handeled ; bra @format_return @format_return: ; increment fmt_idx to swallow the formating char inc fmt_idx bra @loop @out_overflow: ; store 0 in last position ldy #$ff sty out_idx @store_null: lda #0 @null: ; store and return ldy out_idx sta (ARG2),y @rts: rts .endproc ;******************************************************************************** ; @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 int8_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"