From f4a3601d6e7f6b35c95e9a55d4134d7f571cf99a Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Wed, 11 Nov 2020 00:25:39 +0100 Subject: [PATCH] 8080: Fix word negation --- .../scala/millfork/compiler/z80/ZBuiltIns.scala | 16 +++++++++++++++- src/test/scala/millfork/test/WordMathSuite.scala | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/scala/millfork/compiler/z80/ZBuiltIns.scala b/src/main/scala/millfork/compiler/z80/ZBuiltIns.scala index 5f97647d..b316efae 100644 --- a/src/main/scala/millfork/compiler/z80/ZBuiltIns.scala +++ b/src/main/scala/millfork/compiler/z80/ZBuiltIns.scala @@ -279,7 +279,21 @@ object ZBuiltIns { ctx.env.eval(expr) match { case None => if (result.isEmpty) { - ??? + if (ctx.options.flag(CompilationFlag.EmitZ80Opcodes)) { + result ++= Z80ExpressionCompiler.compileToBC(ctx, expr) + result += ZLine.ldImm16(ZRegister.HL, 0) + result += ZLine.register(OR, ZRegister.A) + result += ZLine.registers(SBC_16, ZRegister.HL, ZRegister.BC) + } else { + result ++= Z80ExpressionCompiler.compileToHL(ctx, expr) + result += ZLine.register(DEC_16, ZRegister.HL) + result += ZLine.ld8(ZRegister.A, ZRegister.H) + result += ZLine.implied(CPL) + result += ZLine.ld8(ZRegister.H, ZRegister.A) + result += ZLine.ld8(ZRegister.A, ZRegister.L) + result += ZLine.implied(CPL) + result += ZLine.ld8(ZRegister.L, ZRegister.A) + } } else { if (ctx.options.flag(CompilationFlag.EmitZ80Opcodes)) { // TODO: optimize diff --git a/src/test/scala/millfork/test/WordMathSuite.scala b/src/test/scala/millfork/test/WordMathSuite.scala index 87a39463..50715be2 100644 --- a/src/test/scala/millfork/test/WordMathSuite.scala +++ b/src/test/scala/millfork/test/WordMathSuite.scala @@ -268,6 +268,20 @@ class WordMathSuite extends FunSuite with Matchers with AppendedClues { } } + test("Word addition 6") { + EmuCrossPlatformBenchmarkRun(Cpu.Sixteen, Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Intel8085, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)(""" + | byte output @$c000 + | void main () { + | signed16 p + | p = f() + | output = lo(-p) + | } + | noinline signed16 f() = 6 + """.stripMargin) { m => + m.readByte(0xc000) should equal((-6) & 0xff) + } + } + test("Word bit ops 2") { EmuCrossPlatformBenchmarkRun(Cpu.Sixteen, Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp, Cpu.Intel8086, Cpu.Motorola6809)(""" | word output @$c000