From 9ea69c07b8d3c853da42a7ef2241f82eb8b6b602 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 14 Oct 2023 07:18:37 +0200 Subject: [PATCH] optimize word array reads with indexvar --- .../cpu6502/assignment/AssignmentAsmGen.kt | 11 ++--- docs/source/todo.rst | 8 +--- examples/test.p8 | 41 +++++++++++++------ 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index ce3743ebf..f2ed04c63 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -88,8 +88,8 @@ internal class AssignmentAsmGen(private val program: PtProgram, assignRegisterpairWord(assign.target, RegisterOrPair.AY) } else { asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y) - asmgen.out(" lda ${arrayVarName}_lsb,y | tax | lda ${arrayVarName}_msb,y | tay | txa") - assignRegisterpairWord(assign.target, RegisterOrPair.AY) + asmgen.out(" lda ${arrayVarName}_lsb,y | ldx ${arrayVarName}_msb,y") + assignRegisterpairWord(assign.target, RegisterOrPair.AX) } return } @@ -122,8 +122,8 @@ internal class AssignmentAsmGen(private val program: PtProgram, } in WordDatatypes -> { asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y) - asmgen.out(" lda $arrayVarName,y | tax | lda $arrayVarName+1,y | tay | txa") - assignRegisterpairWord(assign.target, RegisterOrPair.AY) + asmgen.out(" lda $arrayVarName,y | ldx $arrayVarName+1,y") + assignRegisterpairWord(assign.target, RegisterOrPair.AX) } DataType.FLOAT -> { asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.A) @@ -2209,7 +2209,8 @@ internal class AssignmentAsmGen(private val program: PtProgram, else program.memsizer.memorySize(arrayDt!!, constIndex) // add arrayIndexExpr * elementsize to the address of the array variable. } else { - TODO("address-of array element $sourceName with non-const index at ${target.position}") + val eltSize = program.memsizer.memorySize(ArrayToElementTypes.getValue(arrayDt!!)) + TODO("address-of array element $sourceName size $eltSize with non-const index at ${target.position}") } } else 0 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index cf9de7f04..31ed498a3 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,13 +1,7 @@ TODO ==== -- optimize word array assignment to not use tax/tay etc but simply lda+ldx: (note: with and without @split!) - uword pstep = w_array[index] -> - ldy p8_index - lda p8_w_array,y - ldx p8_w_array+1,y - sta p8_pstep - stx p8_pstep+1 +- fix TODO "address-of array element" in assignAddressOf() - gfx2: use vera auto in/decrement in the flood fill routine diff --git a/examples/test.p8 b/examples/test.p8 index 108a3a747..8d3b96799 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,18 +1,33 @@ +%import textio +%zeropage basicsafe + main { sub start() { - cx16.r1H = %00000001 ; enable auto-indent - cx16.r2L = 4 - cx16.r2H = 80 - cx16.r3L = 8 - cx16.r3H = 11<<4 | 7; background_color<<4 | text_color - cx16.r4 = 0 - cx16.r1L = 0 - cx16.rombank($d) - %asm {{ - ldx #2 - ldy #64 - jmp $c006 - }} + uword[] w_array = [1111,2222,3333,4444] + uword[] @split split_array = [1111,2222,3333,4444] + ubyte index = 2 + + uword value = 9999 + w_array[index] = value + split_array[index] = value + + uword pstep = w_array[index] + uword psteps = split_array[index] + ;; uword @zp ptr = &w_array[index] + + txt.print_uw(pstep) + txt.nl() + txt.print_uw(psteps) + txt.nl() +; txt.print_uw(peekw(ptr)) +; txt.nl() + + w_array[index] += 10 + split_array[index] += 10 + txt.print_uw(w_array[index]) + txt.nl() + txt.print_uw(split_array[index]) + txt.nl() } }