6502-OS/util/string.s65

81 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

2023-12-08 22:56:35 +01:00
.include "string.h65"
2023-12-16 02:41:19 +01:00
Export str, strf
2023-12-08 00:13:43 +01:00
.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
;********************************************************************************
2023-12-08 22:56:35 +01:00
out_idx := str::out_idx
fmt_idx := str::fmt_idx
2023-12-08 00:13:43 +01:00
.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
2023-12-16 02:41:19 +01:00
jsr str::uint8_to_hex_str
2023-12-08 00:13:43 +01:00
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