From 26fc5ff5e2661fdfa1d590a93d493b52ac1adcb6 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 15 Oct 2020 22:41:29 +0200 Subject: [PATCH] preparing conv.bin and hex string to number --- compiler/src/prog8/compiler/Main.kt | 2 +- .../assignment/AugmentableAssignmentAsmGen.kt | 24 +++-- examples/test.p8 | 102 +++++++++++++----- 3 files changed, 91 insertions(+), 37 deletions(-) diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 239f5ca10..bae223d0c 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -221,7 +221,7 @@ private fun writeAssembly(programAst: Program, errors: ErrorReporter, outputDir: programAst.processAstBeforeAsmGeneration(errors) errors.handle() - // printAst(programAst) + printAst(programAst) // TODO CompilationTarget.instance.machine.initializeZeropage(compilerOptions) val assembly = CompilationTarget.instance.asmGenerator( diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt index 19cd2a1e3..2a23a483f 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AugmentableAssignmentAsmGen.kt @@ -941,9 +941,13 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, bne -""") } } - "&" -> TODO("bitand (u)wordvar bytevar") - "^" -> TODO("bitxor (u)wordvar bytevar") - "|" -> TODO("bitor (u)wordvar bytevar") + "&" -> { + asmgen.out(" lda $otherName | and $name | sta $name") + if(dt in WordDatatypes) + asmgen.out(" lda #0 | sta $name+1") + } + "^" -> asmgen.out(" lda $otherName | eor $name | sta $name") + "|" -> asmgen.out(" lda $otherName | ora $name | sta $name") else -> throw AssemblyError("invalid operator for in-place modification $operator") } } @@ -1164,7 +1168,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, remainderWord() } "<<" -> { - asmgen.translateExpression(value) + asmgen.translateExpression(value) // TODO huh is this okay? wasn't this done above already? asmgen.out(""" inx ldy P8ESTACK_LO,x @@ -1176,7 +1180,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, +""") } ">>" -> { - asmgen.translateExpression(value) + asmgen.translateExpression(value) // TODO huh is this okay? wasn't this done above already? if(dt==DataType.UWORD) { asmgen.out(""" inx @@ -1201,9 +1205,13 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, +""") } } - "&" -> TODO("bitand (u)word (u)byte on stack") - "^" -> TODO("bitxor (u)word (u)byte on stack") - "|" -> TODO("bitor (u)word (u)byte on stack") + "&" -> { + asmgen.out(" lda P8ESTACK_LO+1,x | and $name | sta $name") + if(dt in WordDatatypes) + asmgen.out(" lda #0 | sta $name+1") + } + "^" -> asmgen.out(" lda P8ESTACK_LO+1,x | eor $name | sta $name") + "|" -> asmgen.out(" lda P8ESTACK_LO+1,x | ora $name | sta $name") else -> throw AssemblyError("invalid operator for in-place modification $operator") } } diff --git a/examples/test.p8 b/examples/test.p8 index b5ec166ee..409ede72d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,36 +6,38 @@ main { sub start() { - str num1 = "01234" - str num2 = @"01234" - str hex1 = "a04E" - str hex2 = "$a04E" - str hex3 = @"a04E" - str hex4 = @"$a04E" + str hex1 = "a4E" + str hex2 = "$a4E" + str hex3 = @"a4E" + str hex4 = @"$a4E" + str bin1 = "111111010010" + str bin2 = "%111111010010" -; txt.print(num1) -; txt.chrout('\n') -; txt.print(num2) -; txt.chrout('\n') -; txt.print(hex1) -; txt.chrout('\n') -; txt.print(hex2) -; txt.chrout('\n') -; txt.print(hex3) -; txt.chrout('\n') -; txt.print(hex4) -; txt.chrout('\n') + txt.print(hex1) + txt.chrout('=') + txt.print_uwhex(conv2.hex2uword(hex1), true) + txt.chrout('\n') + txt.print(hex2) + txt.chrout('=') + txt.print_uwhex(conv2.hex2uword(hex2), true) + txt.chrout('\n') + txt.print(hex3) + txt.chrout('=') + txt.print_uwhex(conv2.hex2uword(hex3), true) + txt.chrout('\n') + txt.print(hex4) + txt.chrout('=') + txt.print_uwhex(conv2.hex2uword(hex4), true) + txt.chrout('\n') + txt.print(bin1) + txt.chrout('=') + txt.print_uwbin(conv2.bin2uword(bin1), true) + txt.chrout('\n') + txt.print(bin2) + txt.chrout('=') + txt.print_uwbin(conv2.bin2uword(bin2), true) + txt.chrout('\n') - ubyte cc - for cc in 0 to len(hex3)-1 { - @($0410+cc) = hex3[cc] - txt.setchr(16+cc,2,hex3[cc]) - } - - for cc in 0 to len(hex4)-1 { - @($0420+cc) = hex4[cc] - txt.setchr(32+cc,2,hex4[cc]) - } testX() } @@ -55,3 +57,47 @@ _saveX .byte 0 }} } } + + +conv2 { + + sub hex2uword(uword strptr) -> uword { + uword result = 0 + while @(strptr) { + if @(strptr)!='$' { + ubyte add = 0 + if @(strptr) <= 6 or @(strptr) > '9' + add = 9 + result = (result << 4) | (add + @(strptr) & $0f) + } + strptr++ + } + return result + } + + asmsub bin2uword(uword strptr @AY) -> uword @AY { + %asm {{ + sta P8ZP_SCRATCH_W2 + sty P8ZP_SCRATCH_W2+1 + ldy #0 + sty P8ZP_SCRATCH_W1 + sty P8ZP_SCRATCH_W1+1 +_loop lda (P8ZP_SCRATCH_W2),y + beq _stop + cmp #'%' + beq + + asl P8ZP_SCRATCH_W1 + rol P8ZP_SCRATCH_W1+1 + and #1 + ora P8ZP_SCRATCH_W1 + sta P8ZP_SCRATCH_W1 ++ inc P8ZP_SCRATCH_W2 + bne _loop + inc P8ZP_SCRATCH_W2+1 + bne _loop +_stop lda P8ZP_SCRATCH_W1 + ldy P8ZP_SCRATCH_W1+1 + rts + }} + } +}