From 7ebc9c79cf7c28eb0689b599fd51eac4ee44ece5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 10 Nov 2023 22:27:07 +0100 Subject: [PATCH] added string.append() cleanup redundant diskio prefixes --- compiler/res/prog8lib/cx16/diskio.p8 | 28 ++++++++++----------- compiler/res/prog8lib/diskio.p8 | 2 +- compiler/res/prog8lib/string.p8 | 33 ++++++++++++++++++++++--- compiler/res/prog8lib/virtual/string.p8 | 7 ++++++ docs/source/libraries.rst | 4 +++ examples/test.p8 | 30 +++++++++------------- 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/compiler/res/prog8lib/cx16/diskio.p8 b/compiler/res/prog8lib/cx16/diskio.p8 index 67f79bf32..fcc16183e 100644 --- a/compiler/res/prog8lib/cx16/diskio.p8 +++ b/compiler/res/prog8lib/cx16/diskio.p8 @@ -91,7 +91,7 @@ io_error: sub diskname() -> uword { ; returns disk label name or 0 if error cbm.SETNAM(3, "$") - cbm.SETLFS(READ_IO_CHANNEL, diskio.drivenumber, 0) + cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0) ubyte status = 1 void cbm.OPEN() ; open 12,8,0,"$=c" if_cs @@ -104,12 +104,12 @@ io_error: ; skip up to entry name } - cx16.r0 = &diskio.list_filename + cx16.r0 = &list_filename repeat { @(cx16.r0) = cbm.CHRIN() if @(cx16.r0)=='"' { @(cx16.r0) = ' ' - while @(cx16.r0)==' ' and cx16.r0>=&diskio.list_filename { + while @(cx16.r0)==' ' and cx16.r0>=&list_filename { @(cx16.r0) = 0 cx16.r0-- } @@ -124,7 +124,7 @@ io_error: cbm.CLOSE(READ_IO_CHANNEL) if status and status & $40 == 0 return 0 - return diskio.list_filename + return list_filename } ; internal variables for the iterative file lister / loader @@ -716,7 +716,7 @@ internal_vload: cx16.r12 = reversebuffer + MAX_PATH_LEN-1 @(cx16.r12)=0 cbm.SETNAM(3, "$=c") - cbm.SETLFS(READ_IO_CHANNEL, diskio.drivenumber, 0) + cbm.SETLFS(READ_IO_CHANNEL, drivenumber, 0) void cbm.OPEN() ; open 12,8,0,"$=c" if_cs goto io_error @@ -805,14 +805,14 @@ io_error: ; NOTE: f_seek_w() doesn't work reliably right now. I only manage to corrupt the fat32 filesystem on the sdcard with it... -; sub f_seek_w(uword pos_hiword, uword pos_loword) { -; ; -- seek in the output file opened with f_open_w, to the given 32-bits position -; diskio.f_seek.command[1] = WRITE_IO_CHANNEL ; f_open_w uses the write io channel -; diskio.f_seek.command[2] = lsb(pos_loword) -; diskio.f_seek.command[3] = msb(pos_loword) -; diskio.f_seek.command[4] = lsb(pos_hiword) -; diskio.f_seek.command[5] = msb(pos_hiword) -; goto diskio.f_seek.send_command -; } + sub f_seek_w(uword pos_hiword, uword pos_loword) { + ; -- seek in the output file opened with f_open_w, to the given 32-bits position + diskio.f_seek.command[1] = WRITE_IO_CHANNEL ; f_open_w uses the write io channel + diskio.f_seek.command[2] = lsb(pos_loword) + diskio.f_seek.command[3] = msb(pos_loword) + diskio.f_seek.command[4] = lsb(pos_hiword) + diskio.f_seek.command[5] = msb(pos_hiword) + goto diskio.f_seek.send_command + } } diff --git a/compiler/res/prog8lib/diskio.p8 b/compiler/res/prog8lib/diskio.p8 index b5eefc043..60ec44b5e 100644 --- a/compiler/res/prog8lib/diskio.p8 +++ b/compiler/res/prog8lib/diskio.p8 @@ -107,7 +107,7 @@ io_error: @(cx16.r0) = cbm.CHRIN() if @(cx16.r0)=='"' { @(cx16.r0) = ' ' - while @(cx16.r0)==' ' and cx16.r0>=&diskio.list_filename { + while @(cx16.r0)==' ' and cx16.r0>=&list_filename { @(cx16.r0) = 0 cx16.r0-- } diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index 419239b1f..59f49850b 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -146,11 +146,11 @@ _startloop dey iny bne - _notfound lda #0 - clc + clc rts -_found tya - sec - rts +_found tya + sec + rts }} } @@ -168,6 +168,31 @@ _found tya }} } + asmsub append(uword target @R0, uword suffix @R1) clobbers(Y) -> ubyte @A { + ; Append the suffix string to the target. (make sure the buffer is large enough!) + ; Returns the length of the resulting string. + %asm {{ + lda cx16.r0 + ldy cx16.r0+1 + jsr length + sty P8ZP_SCRATCH_B1 + tya + clc + adc cx16.r0 + sta P8ZP_SCRATCH_W1 + lda cx16.r0+1 + adc #0 + sta P8ZP_SCRATCH_W1+1 + lda cx16.r1 + ldy cx16.r1+1 + jsr prog8_lib.strcpy + tya + clc + adc P8ZP_SCRATCH_B1 + rts + }} + } + asmsub compare(uword string1 @R0, uword string2 @AY) clobbers(Y) -> byte @A { ; Compares two strings for sorting. ; Returns -1 (255), 0 or 1 depending on wether string1 sorts before, equal or after string2. diff --git a/compiler/res/prog8lib/virtual/string.p8 b/compiler/res/prog8lib/virtual/string.p8 index f913d33c3..0d1810b52 100644 --- a/compiler/res/prog8lib/virtual/string.p8 +++ b/compiler/res/prog8lib/virtual/string.p8 @@ -78,6 +78,13 @@ string { } } + sub append(str target, str suffix) -> ubyte { + ; Append the suffix string to the target. (make sure the buffer is large enough!) + ; Returns the length of the resulting string. + cx16.r0L = length(target) + return copy(suffix, target+cx16.r0L) + cx16.r0L + } + sub compare(str st1, str st2) -> byte { ; Compares two strings for sorting. ; Returns -1 (255), 0 or 1 depending on wether string1 sorts before, equal or after string2. diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index d3005d698..e54c470e1 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -236,6 +236,10 @@ Provides string manipulation routines. Often you don't have to call this explicitly and can just write ``string1 = string2`` but this function is useful if you're dealing with addresses for instance. +``append (string, suffix) -> ubyte length`` + Appends the suffix string to the other string (make sure the memory buffer is large enough!) + Returns the length of the combined string. + ``lower (string)`` Lowercases the PETSCII-string in place. diff --git a/examples/test.p8 b/examples/test.p8 index eaf27a469..c221e254c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,29 +1,21 @@ %import textio +%import string %zeropage basicsafe main { sub start() { - uword address = 1000 + str name = "irmen????????????" + name[5] = 0 - poke(1000, 99) - ubyte prev = pokemon(1000,123) - txt.print_ub(prev) - txt.nl() - prev = pokemon(1000,0) - txt.print_ub(prev) - txt.nl() - txt.print_ub(@(1000)) - txt.nl() + txt.print(name) txt.nl() + ubyte length = string.append(name, ".prg") - poke(address+3, 99) - prev = pokemon(address+3,123) - txt.print_ub(prev) - txt.nl() - prev = pokemon(address+3,0) - txt.print_ub(prev) - txt.nl() - txt.print_ub(@(address+3)) - txt.nl() + txt.print_ub(length) + txt.chrout('[') + for cx16.r0L in 0 to length-1 { + txt.chrout(name[cx16.r0L]) + } + txt.chrout(']') } }