diff --git a/src/main/scala/com/htmlism/mos6502/dsl/DslDemo.scala b/src/main/scala/com/htmlism/mos6502/dsl/DslDemo.scala index 5e267a0..0c08da3 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/DslDemo.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/DslDemo.scala @@ -74,26 +74,23 @@ object registers { def incr(implicit ctx: AssemblyContext): Unit = ctx.push(INX, "incr x") - def upTo(s: String, start: Int, stop: Int)(f: AssemblyContext => Unit)(implicit ctx: AssemblyContext): Unit = { + def loop(s: String, spec: RangeSpec)(f: AssemblyContext => Unit)(implicit ctx: AssemblyContext): Unit = { + val (start, stop, instruction) = + spec match { + case Incrementing(from, to) => + (from, to, INX) + + case Decrementing(from, to) => + (from, to, DEX) + } + ctx.push(LDX, start) label(s) f(ctx) - ctx.push(INX) - ctx.push(CPX, stop) - ctx.branch(BNE, s) - } - - def downTo(s: String, start: Int, stop: Int)(f: AssemblyContext => Unit)(implicit ctx: AssemblyContext): Unit = { - ctx.push(LDX, start) - - label(s) - - f(ctx) - - ctx.push(DEX) + ctx.push(instruction) ctx.push(CPX, stop) ctx.branch(BNE, s) } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/RangeSpec.scala b/src/main/scala/com/htmlism/mos6502/dsl/RangeSpec.scala new file mode 100644 index 0000000..ca95c1a --- /dev/null +++ b/src/main/scala/com/htmlism/mos6502/dsl/RangeSpec.scala @@ -0,0 +1,11 @@ +package com.htmlism.mos6502.dsl + +sealed trait RangeSpec { + def from: Int + + def to: Int +} + +case class Incrementing(from: Int, to: Int) extends RangeSpec + +case class Decrementing(from: Int, to: Int) extends RangeSpec \ No newline at end of file diff --git a/src/main/scala/com/htmlism/mos6502/dsl/package.scala b/src/main/scala/com/htmlism/mos6502/dsl/package.scala index 0116aed..47f24dd 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/package.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/package.scala @@ -17,4 +17,12 @@ package object dsl extends syntax.DefinitionGroupSyntax with syntax.AsmDocSyntax def addr: GlobalAddress = GlobalAddress(n) } + + implicit class RangeOps(from: Int) { + def upTo(to: Int): Incrementing = + Incrementing(from, to) + + def downTo(to: Int): Decrementing = + Decrementing(from, to) + } } diff --git a/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala b/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala index 72d1bca..634244a 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala @@ -55,7 +55,7 @@ class Easy6502Spec extends AnyFlatSpec with should.Matchers { val doc = asmDoc { implicit ctx => asm { implicit a => - registers.X.upTo("incrementing", 2, 5) { implicit a => + registers.X.loop("incrementing", 2 upTo 5) { implicit a => a.push(INY) } }