From 2d19537ed327f0de13d680bae84a92f26541e008 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Tue, 9 Jun 2020 22:56:16 +0200 Subject: [PATCH] 6809: Fix writing through pointers --- .../m6809/M6809ExpressionCompiler.scala | 6 ++-- .../scala/millfork/test/PointerSuite.scala | 30 +++++++++---------- .../scala/millfork/test/emu/EmuM6809Run.scala | 4 ++- .../scala/millfork/test/emu/M6809Memory.scala | 2 ++ 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/scala/millfork/compiler/m6809/M6809ExpressionCompiler.scala b/src/main/scala/millfork/compiler/m6809/M6809ExpressionCompiler.scala index 0d3a2927..babed652 100644 --- a/src/main/scala/millfork/compiler/m6809/M6809ExpressionCompiler.scala +++ b/src/main/scala/millfork/compiler/m6809/M6809ExpressionCompiler.scala @@ -617,7 +617,7 @@ object M6809ExpressionCompiler extends AbstractExpressionCompiler[MLine] { val variable = ctx.env.get[Variable](name) List(MLine.variable(ctx, STB, variable)) case DerefExpression(inner, offset, _) => - compileToX(ctx, inner) :+ MLine.indexedX(STB, NumericConstant(offset, 2)) + stashBIfNeeded(ctx, compileToX(ctx, inner)) :+ MLine.indexedX(STB, NumericConstant(offset, 2)) case IndexedExpression(name, index) => ctx.env.getPointy(name) match { case p: ConstantPointy => @@ -639,7 +639,9 @@ object M6809ExpressionCompiler extends AbstractExpressionCompiler[MLine] { v.indexType.size match { case 1 | 2 => stashBIfNeeded(ctx, - compileToD(ctx, index) :+ MLine(LEAX, DAccumulatorIndexed(M6809Register.X, indirect = false), Constant.Zero)) :+ + MLine.absolute(LDX, v.addr) :: + (compileToD(ctx, index) :+ + MLine(LEAX, DAccumulatorIndexed(M6809Register.X, indirect = false), Constant.Zero))) :+ MLine.indexedX(STB, Constant.Zero) } } diff --git a/src/test/scala/millfork/test/PointerSuite.scala b/src/test/scala/millfork/test/PointerSuite.scala index 12a944ea..feeaf7ad 100644 --- a/src/test/scala/millfork/test/PointerSuite.scala +++ b/src/test/scala/millfork/test/PointerSuite.scala @@ -10,7 +10,7 @@ import org.scalatest.{AppendedClues, FunSuite, Matchers} class PointerSuite extends FunSuite with Matchers with AppendedClues { test("Pointers outside zeropage") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)( """ | pointer p @$c004 | array output[2] @$c000 @@ -25,7 +25,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Pointers on stack") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)( """ | array output[2] @$c000 | void main() { @@ -40,7 +40,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Typed byte-targeting pointers") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)( """ | enum e {} | array(e) output [3] @$c000 @@ -57,7 +57,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Typed word-targeting pointers") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Sixteen, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)( """ | word output @$c000 | void main() { @@ -130,7 +130,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Pointer optimization") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086, Cpu.Motorola6809)( """ | struct s { word a, byte b } | @@ -154,7 +154,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("nullptr") { - EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086)( + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086, Cpu.Motorola6809)( """ | void main() { | pointer.word pw @@ -171,7 +171,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { test("Complex pointers") { // TODO: optimize it when inlined - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086, Cpu.Motorola6809)( """ | array output[3] @$c000 | struct s { @@ -193,7 +193,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Indexing returned pointers") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8086, Cpu.Motorola6809)( """ | array output[10] @$c000 | pointer get() = output.addr @@ -211,7 +211,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Pointers and arrays") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Motorola6809)( """ | struct P { | word i @@ -233,7 +233,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Pointers and arrays to large elements") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Motorola6809)( """ | struct P { | word i @@ -255,7 +255,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Page crossing with arrays of large elements") { - EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80)( + EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Motorola6809)( """ | import zp_reg | struct P { @@ -287,7 +287,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Word pointers") { - EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080) ( + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Motorola6809) ( """ |pointer.word p |word output @$c000 @@ -304,7 +304,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Word pointers and byte-to-word promotions") { - EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080) ( + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Motorola6809) ( """ |pointer.word p |word output @$c000 @@ -324,7 +324,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Fast pointer indexing") { - EmuUnoptimizedCrossPlatformRun(Cpu.Mos) ( + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Motorola6809) ( """ |pointer.word p |array(word) input [6] @@ -341,7 +341,7 @@ class PointerSuite extends FunSuite with Matchers with AppendedClues { } test("Modifying a word via a pointer") { - EmuUnoptimizedCrossPlatformRun(/*Cpu.Mos, Cpu.Z80,*/ Cpu.Motorola6809)( + EmuUnoptimizedCrossPlatformRun(Cpu.Mos, Cpu.Z80, Cpu.Motorola6809)( """ |word output @$c000 |void main () { diff --git a/src/test/scala/millfork/test/emu/EmuM6809Run.scala b/src/test/scala/millfork/test/emu/EmuM6809Run.scala index dd184821..399d09b0 100644 --- a/src/test/scala/millfork/test/emu/EmuM6809Run.scala +++ b/src/test/scala/millfork/test/emu/EmuM6809Run.scala @@ -94,6 +94,8 @@ class EmuM6809Run(cpu: millfork.Cpu.Value, nodeOptimizations: List[NodeOptimizat CompilationFlag.SubroutineExtraction -> optimizeForSize, CompilationFlag.OptimizeForSize -> optimizeForSize, CompilationFlag.OptimizeForSpeed -> blastProcessing, +// CompilationFlag.SourceInAssembly -> true, +// CompilationFlag.LineNumbersInAssembly -> true, CompilationFlag.OptimizeForSonicSpeed -> blastProcessing // CompilationFlag.CheckIndexOutOfBounds -> true, ), None, 0, Map(), EmuPlatform.textCodecRepository, JobContext(log, new LabelGenerator)) @@ -141,7 +143,7 @@ class EmuM6809Run(cpu: millfork.Cpu.Value, nodeOptimizations: List[NodeOptimizat // compile - val env2 = new Environment(None, "", CpuFamily.M6502, options) + val env2 = new Environment(None, "", CpuFamily.M6809, options) env2.collectDeclarations(program, options) val assembler = new M6809Assembler(program, env2, platform) val output = assembler.assemble(callGraph, assemblyOptimizations, options, VeryLateM6809AssemblyOptimizations.All) diff --git a/src/test/scala/millfork/test/emu/M6809Memory.scala b/src/test/scala/millfork/test/emu/M6809Memory.scala index ba27cd5a..99274395 100644 --- a/src/test/scala/millfork/test/emu/M6809Memory.scala +++ b/src/test/scala/millfork/test/emu/M6809Memory.scala @@ -30,6 +30,8 @@ class M6809Memory(memoryBank: MemoryBank, resetVector: Int) extends MemorySegmen override def store(addr: Int, `val`: Int): Unit = { if (!memoryBank.writeable(addr)) { + val start = addr & 0xff00 + (0 until 0x100).grouped(16).map(range => (start + range.head).toHexString + range.map(i => memoryBank.output(start + i)).map(v => f" $v%02X").mkString("")).foreach(println) println(s"Accessing memory for write at $$${addr.toHexString}, writing $$${`val`.toHexString}") ??? }