more uniform code for array indexing (all using scaled offset now)

This commit is contained in:
Irmen de Jong 2020-08-23 20:25:00 +02:00
parent 93b2ff2e52
commit b9ca1c2e2c
3 changed files with 43 additions and 101 deletions

View File

@ -1,5 +1,6 @@
; --- low level floating point assembly routines for the C64 ; --- low level floating point assembly routines for the C64
ub2float .proc ub2float .proc
; -- convert ubyte in SCRATCH_ZPB1 to float at address A/Y ; -- convert ubyte in SCRATCH_ZPB1 to float at address A/Y
; clobbers A, Y ; clobbers A, Y
@ -154,17 +155,6 @@ func_rndf .proc
_rndf_rnum5 .byte 0,0,0,0,0 _rndf_rnum5 .byte 0,0,0,0,0
.pend .pend
push_float_from_indexed_var .proc
; -- push the float from the array at A/Y with index on stack, onto the stack.
sta c64.SCRATCH_ZPWORD1
sty c64.SCRATCH_ZPWORD1+1
jsr prog8_lib.pop_index_times_5
jsr prog8_lib.add_a_to_zpword
lda c64.SCRATCH_ZPWORD1
ldy c64.SCRATCH_ZPWORD1+1
jmp push_float
.pend
pop_float .proc pop_float .proc
; ---- pops mflpt5 from stack to memory A/Y ; ---- pops mflpt5 from stack to memory A/Y
; (frees 3 stack positions = 6 bytes of which 1 is padding) ; (frees 3 stack positions = 6 bytes of which 1 is padding)
@ -269,42 +259,6 @@ dec_var_f .proc
rts rts
.pend .pend
inc_indexed_var_f .proc
; -- add 1 to float in array pointed to by A/Y, at index X
pha
txa
sta c64.SCRATCH_ZPB1
asl a
asl a
clc
adc c64.SCRATCH_ZPB1
sta c64.SCRATCH_ZPB1
pla
clc
adc c64.SCRATCH_ZPB1
bcc +
iny
+ jmp inc_var_f
.pend
dec_indexed_var_f .proc
; -- subtract 1 to float in array pointed to by A/Y, at index X
pha
txa
sta c64.SCRATCH_ZPB1
asl a
asl a
clc
adc c64.SCRATCH_ZPB1
sta c64.SCRATCH_ZPB1
pla
clc
adc c64.SCRATCH_ZPB1
bcc +
iny
+ jmp dec_var_f
.pend
pop_2_floats_f2_in_fac1 .proc pop_2_floats_f2_in_fac1 .proc
; -- pop 2 floats from stack, load the second one in FAC1 as well ; -- pop 2 floats from stack, load the second one in FAC1 as well

View File

@ -82,9 +82,28 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
throw AssemblyError("weird array type") throw AssemblyError("weird array type")
} }
} else { } else {
// TODO rewrite to use Scaled when (elementDt) {
loadUnscaledArrayIndexIntoA(value) in ByteDatatypes -> {
readAndPushArrayvalueWithUnscaledIndexA(elementDt, arrayVarName) asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | dex")
}
in WordDatatypes -> {
asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.Y)
asmgen.out(" lda $arrayVarName,y | sta $ESTACK_LO_HEX,x | lda $arrayVarName+1,y | sta $ESTACK_HI_HEX,x | dex")
}
DataType.FLOAT -> {
asmgen.loadScaledArrayIndexIntoRegister(value, elementDt, CpuRegister.A)
asmgen.out("""
ldy #>$arrayVarName
clc
adc #<$arrayVarName
bcc +
iny
+ jsr c64flt.push_float""")
}
else ->
throw AssemblyError("weird array elt type")
}
} }
assignStackValue(assign.target) assignStackValue(assign.target)
} }
@ -637,40 +656,4 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
throw AssemblyError("weird array type") throw AssemblyError("weird array type")
} }
} }
private fun loadUnscaledArrayIndexIntoA(expr: ArrayIndexedExpression) {
// TODO this is only used once, remove this when that is rewritten using scaled
when (val index = expr.arrayspec.index) {
is NumericLiteralValue -> throw AssemblyError("constant array index should be optimized earlier")
is IdentifierReference -> {
val indexName = asmgen.asmIdentifierName(index)
asmgen.out(" lda $indexName")
}
else -> {
asmgen.translateExpression(index)
asmgen.out(" inx | lda $ESTACK_LO_HEX,x")
}
}
}
private fun readAndPushArrayvalueWithUnscaledIndexA(elementDt: DataType, asmVarname: String) {
// TODO this is only used once, remove this when that is rewritten using scaled
when (elementDt) {
in ByteDatatypes ->
asmgen.out(" tay | lda $asmVarname,y | sta $ESTACK_LO_HEX,x | dex")
in WordDatatypes ->
asmgen.out(" asl a | tay | lda $asmVarname,y | sta $ESTACK_LO_HEX,x | lda $asmVarname+1,y | sta $ESTACK_HI_HEX,x | dex")
DataType.FLOAT ->
// index * 5 is done in the subroutine that's called
asmgen.out("""
sta $ESTACK_LO_HEX,x
dex
lda #<$asmVarname
ldy #>$asmVarname
jsr c64flt.push_float_from_indexed_var
""")
else ->
throw AssemblyError("weird array elt type")
}
}
} }

View File

@ -11,25 +11,30 @@ main {
uword[] array=[1111 ,2222,3333] uword[] array=[1111 ,2222,3333]
float[] farr = [1.111, 2.222, 3.333] float[] farr = [1.111, 2.222, 3.333]
float ff
ubyte i = 1 ubyte i = 1
c64scr.print_ub(arr1[1]) farr[2] = farr[i]
c64.CHROUT('\n') c64flt.print_f(farr[2])
arr1 [i] ++
c64scr.print_ub(arr1[1])
c64.CHROUT('\n') c64.CHROUT('\n')
c64scr.print_uw(array[1]) ; c64scr.print_ub(arr1[1])
c64.CHROUT('\n') ; c64.CHROUT('\n')
array[i] ++ ; arr1 [i] ++
c64scr.print_uw(array[1]) ; c64scr.print_ub(arr1[1])
c64.CHROUT('\n') ; c64.CHROUT('\n')
;
c64flt.print_f(farr[1]) ; c64scr.print_uw(array[1])
c64.CHROUT('\n') ; c64.CHROUT('\n')
farr[i] ++ ; array[i] ++
c64flt.print_f(farr[1]) ; c64scr.print_uw(array[1])
c64.CHROUT('\n') ; c64.CHROUT('\n')
;
; c64flt.print_f(farr[1])
; c64.CHROUT('\n')
; farr[i] ++
; c64flt.print_f(farr[1])
; c64.CHROUT('\n')
} }
} }