increment range spec

This commit is contained in:
Mark Canlas 2020-08-21 04:04:35 -04:00
parent 2db25f5982
commit 53214fad91
4 changed files with 31 additions and 15 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}