1
0
mirror of https://github.com/KarolS/millfork.git synced 2025-02-09 21:30:55 +00:00

Z80: Fix 16-bit operations

This commit is contained in:
Karol Stasiak 2018-08-03 13:02:18 +02:00
parent a8ab3b2c3f
commit 30aa62ceaf
2 changed files with 25 additions and 8 deletions

View File

@ -217,19 +217,24 @@ object ZBuiltIns {
} }
case (true, expr) => case (true, expr) =>
ctx.env.eval(expr) match { ctx.env.eval(expr) match {
case None => case Some(c) if hasConst && const.isProvablyGreaterOrEqualThan(c)=>
const = CompoundConstant(MathOperator.DecimalMinus, const, c).quickSimplify
case _ =>
if (result.isEmpty) { if (result.isEmpty) {
??? result += ZLine.ldImm16(ZRegister.HL, const)
} else if (ctx.options.flag(CompilationFlag.EmitExtended80Opcodes)) { const = NumericConstant(0, 2)
hasConst = false
}
if (ctx.options.flag(CompilationFlag.EmitExtended80Opcodes)) {
result += ZLine.ld8(ZRegister.E, ZRegister.L) result += ZLine.ld8(ZRegister.E, ZRegister.L)
result += ZLine.ld8(ZRegister.D, ZRegister.H) result += ZLine.ld8(ZRegister.D, ZRegister.H)
result ++= Z80ExpressionCompiler.stashDEIfChanged(ctx, Z80ExpressionCompiler.compileToHL(ctx, expr)) result ++= Z80ExpressionCompiler.stashDEIfChanged(ctx, Z80ExpressionCompiler.compileToHL(ctx, expr))
result += ZLine.ld8(ZRegister.A, ZRegister.E) result += ZLine.ld8(ZRegister.A, ZRegister.E)
result += ZLine.registers(SUB, ZRegister.A, ZRegister.L) result += ZLine.register(SUB, ZRegister.L)
result += ZLine.implied(DAA) result += ZLine.implied(DAA)
result += ZLine.ld8(ZRegister.L, ZRegister.A) result += ZLine.ld8(ZRegister.L, ZRegister.A)
result += ZLine.ld8(ZRegister.A, ZRegister.D) result += ZLine.ld8(ZRegister.A, ZRegister.D)
result += ZLine.registers(SBC, ZRegister.A, ZRegister.H) result += ZLine.register(SBC, ZRegister.H)
result += ZLine.implied(DAA) result += ZLine.implied(DAA)
result += ZLine.ld8(ZRegister.H, ZRegister.A) result += ZLine.ld8(ZRegister.H, ZRegister.A)
} else { } else {
@ -248,9 +253,6 @@ object ZBuiltIns {
result += ZLine.implied(DAA) result += ZLine.implied(DAA)
result += ZLine.ld8(ZRegister.H, ZRegister.A) result += ZLine.ld8(ZRegister.H, ZRegister.A)
} }
case Some(c) =>
hasConst = true
const = CompoundConstant(MathOperator.DecimalMinus, const, c).quickSimplify
} }
} }
} else { } else {

View File

@ -44,6 +44,21 @@ class WordMathSuite extends FunSuite with Matchers {
""".stripMargin)(_.readWord(0xc000) should equal(240)) """.stripMargin)(_.readWord(0xc000) should equal(240))
} }
test("Word subtraction 3") {
EmuCrossPlatformBenchmarkRun(Cpu.Cmos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)("""
| word output @$c000
| word a
| void main () {
| a = 640
| f()
| output = a - f()
| }
| word f() {
| return 400
| }
""".stripMargin)(_.readWord(0xc000) should equal(240))
}
test("Byte-to-word addition") { test("Byte-to-word addition") {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)(""" EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)("""
| word output @$c000 | word output @$c000