6502-OS/util/string.h65

81 lines
2.0 KiB
Plaintext
Raw Normal View History

2023-12-08 22:56:35 +01:00
;********************************************************************************
; @module string
; @type utility
; @details
; String utility
;********************************************************************************
.ifndef INCLUDE_STRING
INCLUDE_STRING = 1
.include "system/system.h65"
.scope str
2023-12-16 02:41:19 +01:00
Import str, strf
Import str, hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str
2023-12-08 22:56:35 +01:00
.macro _StrfStoreArg arg
.if (.not .blank(arg))
.if .match(arg, x)
stx ARG4 + @N_ARGS
.elseif .match(arg, y)
sty ARG4 + @N_ARGS
.else
lda arg
sta ARG4 + @N_ARGS
.endif
@N_ARGS .set (@N_ARGS + 1)
.endif
.endmacro
;********************************************************************************
; @function Macro for passing arguments to strf
2023-12-21 17:27:54 +01:00
; @param fmt: Format string address or string literal
2023-12-08 22:56:35 +01:00
; @param out: Output string address
; @param x0-x9: Additional parameters
2023-12-21 17:27:54 +01:00
; @warning Addresses as additional paramters must be passed like this `#<addr,#>addr`
2023-12-08 22:56:35 +01:00
; @modifies: A, X, Y
; @see strf
;********************************************************************************
.macro Strf fmt,out,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
@N_ARGS .set 0 ; @ so that it doesnt break cheap labels
2023-12-21 17:27:54 +01:00
.if .match(fmt, "") ; fmt is a string literal -> store in rodata
.pushseg
.rodata
: .asciiz fmt
.popseg
lda #<:-
sta ARG0
lda #>:-
sta ARG1
.else ; message is not a string
lda #<fmt
sta ARG0
lda #>fmt
sta ARG1
.endif
2023-12-08 22:56:35 +01:00
lda #<out
sta ARG2
lda #>out
sta ARG3
_StrfStoreArg x0
_StrfStoreArg x1
_StrfStoreArg x2
_StrfStoreArg x3
_StrfStoreArg x4
_StrfStoreArg x5
_StrfStoreArg x6
_StrfStoreArg x7
2023-12-21 17:27:54 +01:00
_StrfStoreArg x8
_StrfStoreArg x9
2023-12-08 22:56:35 +01:00
jsr str::strf
2023-12-21 17:27:54 +01:00
; .out .sprintf("info: Strf: called with %d arguments", @N_ARGS)
2023-12-08 22:56:35 +01:00
.endmacro
; TODO allocate zp memory
fmt_idx = $30
out_idx = $31
.endscope
.endif ; guard