From 98e95b57071dc32b5b963772199d87c011dad0ed Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 13 Jan 2019 13:50:44 +0100 Subject: [PATCH] replaced str2ubyte --- compiler/res/prog8lib/c64utils.p8 | 115 +++++++++++++++++++----------- docs/docs.iml | 2 +- examples/test.p8 | 51 +++++++------ 3 files changed, 107 insertions(+), 61 deletions(-) diff --git a/compiler/res/prog8lib/c64utils.p8 b/compiler/res/prog8lib/c64utils.p8 index 3908ed4ad..b488057ce 100644 --- a/compiler/res/prog8lib/c64utils.p8 +++ b/compiler/res/prog8lib/c64utils.p8 @@ -190,32 +190,83 @@ asmsub uword2decimal (uword value @ AY) -> clobbers(A,X,Y) -> () { } +asmsub str2byte (str string @ AY) -> clobbers(Y) -> (byte @ A) { + %asm {{ + ; -- convert string (address in A/Y) to byte in A + ; doesn't use any kernal routines + sta c64.SCRATCH_ZPWORD1 + sty c64.SCRATCH_ZPWORD1+1 + ldy #0 + lda (c64.SCRATCH_ZPWORD1),y + cmp #'-' + beq + + jmp str2ubyte._enter ++ inc c64.SCRATCH_ZPWORD1 + bne + + inc c64.SCRATCH_ZPWORD1+1 ++ jsr str2ubyte._enter + eor #$ff + sec + adc #0 + rts + }} +} +asmsub str2ubyte (str string @ AY) -> clobbers(Y) -> (ubyte @ A) { + %asm {{ + ; -- convert string (address in A/Y) to ubyte in A + ; doesn't use any kernal routines + sta c64.SCRATCH_ZPWORD1 + sty c64.SCRATCH_ZPWORD1+1 +_enter jsr _numlen ; Y= slen + lda #0 + dey + bpl + + rts ++ lda (c64.SCRATCH_ZPWORD1),y + sec + sbc #'0' + dey + bpl + + rts ++ sta c64.SCRATCH_ZPREG ;result + lda (c64.SCRATCH_ZPWORD1),y + sec + sbc #'0' + asl a + sta c64.SCRATCH_ZPB1 + asl a + asl a + clc + adc c64.SCRATCH_ZPB1 + clc + adc c64.SCRATCH_ZPREG + dey + bpl + + rts ++ sta c64.SCRATCH_ZPREG + lda (c64.SCRATCH_ZPWORD1),y + tay + lda _hundreds-'0',y + clc + adc c64.SCRATCH_ZPREG + rts +_hundreds .byte 0, 100, 200 -; @todo this is python code for a str-to-ubyte function that doesn't use the basic rom: -;def str2ubyte(s, slen): -; hundreds_map = { -; 0: 0, -; 1: 100, -; 2: 200 -; } -; digitvalue = 0 -; result = 0 -; if slen==0: -; return digitvalue -; digitvalue = ord(s[slen-1])-48 -; slen -= 1 -; if slen==0: -; return digitvalue -; result = digitvalue -; digitvalue = 10 * (ord(s[slen-1])-48) -; result += digitvalue -; slen -= 1 -; if slen==0: -; return result -; digitvalue = hundreds_map[ord(s[slen-1])-48] -; result += digitvalue -; return result +_numlen + ;-- return the length of the numeric string at ZPWORD1, in Y + ldy #0 +- lda (c64.SCRATCH_ZPWORD1),y + cmp #'0' + bmi + + cmp #'9' + bpl + + iny + bne - ++ rts + + }} +} asmsub c64flt_FREADSTR (ubyte length @ A) -> clobbers(A,X,Y) -> () = $b7b5 ; @todo needed for (slow) str conversion below @@ -269,22 +320,6 @@ asmsub str2word(str string @ AY) -> clobbers() -> (word @ AY) { }} } -asmsub str2ubyte(str string @ AY) -> clobbers(Y) -> (ubyte @ A) { - %asm {{ - ;-- convert string (address in A/Y) to ubyte number in A - ; @todo don't use the (slow) kernel floating point conversion - jmp str2uword - }} -} - -asmsub str2byte(str string @ AY) -> clobbers(Y) -> (byte @ A) { - %asm {{ - ;-- convert string (address in A/Y) to byte number in A - ; @todo don't use the (slow) kernel floating point conversion - jmp str2word - }} -} - ; @todo string to 32 bit unsigned integer http://www.6502.org/source/strings/ascii-to-32bit.html diff --git a/docs/docs.iml b/docs/docs.iml index ad3c0a365..88ad54124 100644 --- a/docs/docs.iml +++ b/docs/docs.iml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/examples/test.p8 b/examples/test.p8 index 88d2c421d..9e19a6334 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,29 +5,40 @@ sub start() { - - inlinecall(1,2,3) - ubyte r = inlinesub(3,4,5) - c64scr.print_ub(r) + c64scr.print_ub(c64utils.str2ubyte("1")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2ubyte("12")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2ubyte("123")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2ubyte("1234")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2ubyte("12xyz")) c64.CHROUT('\n') - } - - sub inlinecall(byte b1, byte b2, byte b3) { - float f=3.1415 - c64scr.print("this is inlinecall!\n") - c64flt.print_f(f) - f*=2.0 - c64flt.print_f(f) c64.CHROUT('\n') - c64scr.print("end of inlinecall!\n") - } - sub inlinesub(ubyte b1, ubyte b2, ubyte b3) -> ubyte { - c64scr.print("this is inlinesub!\n") - ubyte qq = b1+b2 - qq += b3 - c64scr.print("end of inlinesub!\n") - return qq + c64scr.print_ub(c64utils.str2byte("1")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2byte("12")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2byte("123")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2byte("1234")) + c64.CHROUT('\n') + c64scr.print_ub(c64utils.str2ubyte("12xyz")) + c64.CHROUT('\n') + c64.CHROUT('\n') + + c64scr.print_b(c64utils.str2byte("-1")) + c64.CHROUT('\n') + c64scr.print_b(c64utils.str2byte("-12")) + c64.CHROUT('\n') + c64scr.print_b(c64utils.str2byte("-123")) + c64.CHROUT('\n') + c64scr.print_b(c64utils.str2byte("-1234")) + c64.CHROUT('\n') + c64scr.print_b(c64utils.str2byte("-12xyz")) + c64.CHROUT('\n') } }