mirror of
https://github.com/mcanlas/6502-opcodes.git
synced 2025-01-08 09:31:07 +00:00
increment range spec
This commit is contained in:
parent
2db25f5982
commit
53214fad91
@ -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)
|
||||
}
|
||||
|
11
src/main/scala/com/htmlism/mos6502/dsl/RangeSpec.scala
Normal file
11
src/main/scala/com/htmlism/mos6502/dsl/RangeSpec.scala
Normal 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
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user