Merge branch 'refs/heads/master' into structs6502

# Conflicts:
#	examples/test.p8
This commit is contained in:
Irmen de Jong
2025-08-07 21:25:07 +02:00
4 changed files with 66 additions and 220 deletions

View File

@@ -832,8 +832,18 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
}
// fall through method:
asmgen.assignByteOperandsToAAndVar(fcall.args[1], fcall.args[0], "P8ZP_SCRATCH_W1")
asmgen.out(" jsr prog8_lib.func_pokebool")
if(fcall.args[1].isSimple()) {
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.A)
} else {
asmgen.pushCpuStack(BaseDataType.UBYTE, fcall.args[1])
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
if(asmgen.isTargetCpu(CpuType.CPU65C02))
asmgen.out(" sta (P8ZP_SCRATCH_W1)")
else
asmgen.out(" ldy #0 | sta (P8ZP_SCRATCH_W1),y")
}
private fun funcPokeW(fcall: PtBuiltinFunctionCall) {
@@ -904,7 +914,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
private fun funcPeekBool(fcall: PtBuiltinFunctionCall, resultRegister: RegisterOrPair?) {
fun fallback() {
asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY)
asmgen.out(" jsr prog8_lib.func_peekbool")
asmgen.out(" jsr prog8_lib.func_peek")
}
when(val addrExpr = fcall.args[0]) {
is PtNumber -> {

View File

@@ -401,8 +401,17 @@ _loop_hi ldy _index_first
.pend
func_peek .proc
; -- read the byte value on the address in AY, into A
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
ldy #0
lda (P8ZP_SCRATCH_W1),y
rts
.pend
func_peekw .proc
; -- read the word value on the address in AY
; -- read the word value on the address in AY, into AY
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
ldy #0

View File

@@ -15,6 +15,7 @@ import prog8.code.core.RegisterOrPair
import prog8.code.core.isNumeric
import prog8.code.target.C64Target
import prog8.code.target.Cx16Target
import prog8.code.target.VMTarget
import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.compileText
@@ -136,24 +137,50 @@ main {
test("all peeks and pokes") {
val src="""
%import floats
%import textio
%option no_sysinit
%zeropage basicsafe
main {
uword[5] a
float f
sub start() {
pokebool(3000, true)
pokew(3100, 12345)
pokef(3200, 3.1415927)
poke(3300, 222)
pokebool(cx16.r0, true)
pokew(cx16.r0, 12345)
pokef(cx16.r0, 3.1415927)
poke(cx16.r0, 222)
pokebool(a[2], true)
pokew(a[2], 12345)
pokef(a[2], 3.1415927)
poke(a[2], 222)
pokebool(cx16.r0+cx16.r1, true)
pokew(cx16.r0+cx16.r1, 12345)
pokef(cx16.r0+cx16.r1, 3.1415927)
poke(cx16.r0+cx16.r1, 222)
txt.print_bool(peekbool(3000))
txt.print_uw(peekw(3100))
txt.print_f(peekf(3200))
txt.print_ub(peek(3300))
cx16.r0bL = peekbool(3000)
cx16.r1 = peekw(3100)
f = peekf(3200)
cx16.r2L = peek(3300)
cx16.r0bL = peekbool(cx16.r0)
cx16.r1 = peekw(cx16.r0)
f = peekf(cx16.r0)
cx16.r2L = peek(cx16.r0)
cx16.r0bL = peekbool(a[2])
cx16.r1 = peekw(a[2])
f = peekf(a[2])
cx16.r2L = peek(a[2])
cx16.r0bL = peekbool(cx16.r0+cx16.r1)
cx16.r1 = peekw(cx16.r0+cx16.r1)
f = peekf(cx16.r0+cx16.r1)
cx16.r2L = peek(cx16.r0+cx16.r1)
}
}"""
}
"""
compileText(VMTarget(), false, src, outputDir, writeAssembly = true) shouldNotBe null
compileText(Cx16Target(), false, src, outputDir, writeAssembly = true) shouldNotBe null
compileText(C64Target(), false, src, outputDir, writeAssembly = true) shouldNotBe null
}

View File

@@ -1,214 +1,14 @@
%import floats
main {
^^bool g_bp
^^word g_bw
^^float g_floats
uword[5] a
sub start() {
^^bool l_bp
^^word l_bw
^^float l_floats
pokebool(cx16.r0, false)
pokebool(a[2], false)
pokebool(cx16.r0+cx16.r1, false)
assign_pointers()
assign_values()
assign_inplace()
assign_deref()
assign_uwords()
assign_same_ptrs()
assign_different_ptrs()
sub assign_pointers() {
cx16.r0 = l_bp
cx16.r1 = l_bw
cx16.r2 = l_floats
cx16.r0 = g_bp
cx16.r1 = g_bw
cx16.r2 = g_floats
cx16.r0 = other.g_bp
cx16.r1 = other.g_bw
cx16.r2 = other.g_floats
cx16.r0 = other.func.l_bp
cx16.r1 = other.func.l_bw
cx16.r2 = other.func.l_floats
}
sub assign_deref() {
float f
bool b
word w
b = l_bp^^
w = l_bw^^
f = l_floats^^
b = g_bp^^
w = g_bw^^
f = g_floats^^
b = other.g_bp^^
w = other.g_bw^^
f = other.g_floats^^
b = other.func.l_bp^^
w = other.func.l_bw^^
f = other.func.l_floats^^
}
sub assign_values() {
l_bp^^ = true
l_bw^^ = -1234
l_floats^^ = 5.678
g_bp^^ = true
g_bw^^ = -1234
g_floats^^ = 5.678
other.g_bp^^ = true
other.g_bw^^ = -1234
other.g_floats^^ = 5.678
other.func.l_bp^^ = true
other.func.l_bw^^ = -1234
other.func.l_floats^^ = 5.678
}
sub assign_same_ptrs() {
l_bp = g_bp
l_bw = g_bw
l_floats = g_floats
g_bp = other.g_bp
g_bw = other.g_bw
g_floats = other.g_floats
other.g_bp = other.func.l_bp
other.g_bw = other.func.l_bw
other.g_floats = other.func.l_floats
}
sub assign_different_ptrs() {
l_bp = g_floats as ^^bool
l_bw = g_floats as ^^word
l_floats = g_bp as ^^float
other.g_bp = l_floats as ^^bool
other.g_bw = l_floats as ^^word
other.g_floats = l_bp as ^^float
}
sub assign_inplace() {
bool b
l_bp^^ = l_bp^^ xor b
l_bw^^ += -1234
l_floats^^ += 5.678
g_bp^^ = g_bp^^ xor b
g_bw^^ += -1234
g_floats^^ += 5.678
other.g_bp^^ = other.g_bp^^ xor b
other.g_bw^^ += -1234
other.g_floats^^ += 5.678
other.func.l_bp^^ = other.func.l_bp^^ xor b
other.func.l_bw^^ += -1234
other.func.l_floats^^ += 5.678
l_bw^^ /= 3
l_floats^^ /= 3.0
g_bw^^ /= 3
g_floats^^ /= 3.0
other.g_bw^^ /= 3
other.g_floats^^ /= 3.0
other.func.l_bw^^ /= 3
other.func.l_floats^^ /= 3.0
}
sub assign_uwords() {
l_bp = $9000
l_bw = $9000
l_floats = $9000
g_bp = $9000
g_bw = $9000
g_floats = $9000
other.g_bp = $9000
other.g_bw = $9000
other.g_floats = $9000
other.func.l_bp = $9000
other.func.l_bw = $9000
other.func.l_floats = $9000
}
cx16.r0bL = peekbool(cx16.r0)
cx16.r0bL = peekbool(a[2])
cx16.r0bL = peekbool(cx16.r0+cx16.r1)
}
}
other {
^^bool g_bp
^^word g_bw
^^float g_floats
sub func() {
^^bool l_bp
^^word l_bw
^^float l_floats
}
}
;%import floats
;%import textio
;%option no_sysinit
;%zeropage basicsafe
;
;
;main {
; struct Node {
; uword value
; bool flag
; ^^Node next
; }
;
; sub start() {
; ^^uword ptr = 3000
; ptr^^=9999
; txt.print_uw(peekw(3000))
; txt.spc()
; ptr^^ ++
; txt.print_uw(peekw(3000))
; txt.spc()
; ptr^^ += 123
; txt.print_uw(peekw(3000))
; txt.spc()
; ptr^^ -= 123
; txt.print_uw(peekw(3000))
; txt.spc()
; ptr^^ --
; txt.print_uw(peekw(3000))
; txt.nl()
;
; ptr^^ ^= $eeee
; ptr^^ = 1111
; ptr^^ *= 5
; cx16.r0 = 2
; ptr^^ *= cx16.r0
; uword @shared wvar = 3
; ptr^^ *= wvar
; wvar = cx16.r0 = 1111
; ptr^^ += cx16.r0
; ptr^^ += wvar
; txt.print_uw(peekw(3000))
; txt.nl()
; ptr^^ *= 4
; ptr^^ /= 4
; ptr^^ += 3
; ptr^^ -= 3
; ptr^^ *= 3
; ptr^^ /= 3
; ^^float fptr = 0
; fptr^^ += 3.0
; fptr^^ -= 3.0
; fptr^^ *= 3.0
; fptr^^ /= 3.0
;
;
;; ^^Node nptr = 30000
;; ^^Node nptr2 = Node()
;; ^^Node nptr3 = Node(9999, true, 12345)
;;
;; txt.print_bool(nptr2.flag)
;; txt.spc()
;; txt.print_bool(nptr3.flag)
;; txt.spc()
;; txt.print_uw(nptr2.next)
;; txt.spc()
;; txt.print_uw(nptr3.next)
; }
;}