From 69c82d90a862d9418d8f12f164eccdd1459ed455 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Mon, 30 Dec 2019 14:28:37 +0100 Subject: [PATCH] 6502: Allow assigning between two statically allocated pointers; fixes #25 --- .../compiler/mos/MosExpressionCompiler.scala | 17 +++++++++-- .../scala/millfork/test/ForLoopSuite.scala | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala index 657ab8a4..a0ea64cb 100644 --- a/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala +++ b/src/main/scala/millfork/compiler/mos/MosExpressionCompiler.scala @@ -2238,9 +2238,22 @@ object MosExpressionCompiler extends AbstractExpressionCompiler[AssemblyLine] { AssemblyLine.indexedY(STA, reg, 2)) } } - case _ => ??? + case (MemoryAddressConstant(thT: MemoryVariable), MemoryAddressConstant(thS: MemoryVariable)) if thT.name != thS.name => + prepare ++ prepareSource ++ (0 until targetType.size).flatMap { i => + if (i >= sourceType.size) List( + if (i == 0) AssemblyLine.immediate(LDY, sourceOffset) else AssemblyLine.implied(INY), + AssemblyLine.immediate(LDA, 0), + AssemblyLine.indexedY(STA, thT)) + else List( + if (i == 0) AssemblyLine.immediate(LDY, sourceOffset) else AssemblyLine.implied(INY), + AssemblyLine.indexedY(LDA, thS), + AssemblyLine.indexedY(STA, thT)) + } + case _ => + ??? } - case _ => ??? + case _ => + ??? } case _ => (targetType.size, am) match { diff --git a/src/test/scala/millfork/test/ForLoopSuite.scala b/src/test/scala/millfork/test/ForLoopSuite.scala index 13ba46d1..2270bb49 100644 --- a/src/test/scala/millfork/test/ForLoopSuite.scala +++ b/src/test/scala/millfork/test/ForLoopSuite.scala @@ -478,4 +478,33 @@ class ForLoopSuite extends FunSuite with Matchers { |} """.stripMargin).readByte(0xc000) should equal(45) } + + test("Some pointers in loops") { + val code = + """ + |struct Sprite { + | byte y + |} + | + |array(Sprite) sprites [20] + |Sprite test + | + |void main() { + | byte i + | test = Sprite(1) + | pointer.Sprite test_pointer + | test_pointer = test.pointer + | + | sprites[0] = Sprite(5) + | pointer.Sprite current_sprite + | current_sprite = pointer.Sprite(sprites.pointer) + | for i,0,until,20 { + | current_sprite->y = test_pointer->y + | } + |} + |""".stripMargin + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Motorola6809) (code) { m => + // OK + } + } }