6502-opcodes/src/main/scala/com/htmlism/mos6502/dsl/DslDemo.scala

165 lines
3.5 KiB
Scala
Raw Normal View History

2020-08-13 04:16:30 +00:00
package com.htmlism.mos6502.dsl
import scala.collection.mutable.ListBuffer
2020-08-15 06:04:21 +00:00
2020-08-14 20:54:29 +00:00
import cats.implicits._
2020-08-15 06:04:21 +00:00
2020-08-15 04:41:18 +00:00
import com.htmlism.mos6502.model._
2020-08-13 04:16:30 +00:00
object DslDemo extends App {
val cpu =
new CPU
import registers.{A, X}
// address demonstration
2020-08-13 04:21:15 +00:00
withAssemblyContext { implicit ctx =>
2020-08-13 04:16:30 +00:00
val payloadLocation =
0x01.z
cpu.A = 0x40
A.add(payloadLocation)
}
// a becomes others
2020-08-13 04:21:15 +00:00
withAssemblyContext { implicit ctx =>
2020-08-13 04:16:30 +00:00
cpu.A = cpu.X
cpu.A = cpu.Y
}
// demonstrate first example
2020-08-13 04:21:15 +00:00
withAssemblyContext { implicit ctx =>
2020-08-15 07:52:11 +00:00
cpu.A = 0xc0
2020-08-13 04:16:30 +00:00
cpu.X = cpu.A
X.incr
A.add(0xc4)
}
2020-08-13 04:21:15 +00:00
def withAssemblyContext(f: AssemblyContext => Unit): Unit = {
val ctx: AssemblyContext =
new AssemblyContext
f(ctx)
2020-08-13 04:16:30 +00:00
ctx.printOut()
println()
println()
}
}
object registers {
sealed trait Register
sealed trait DestinationA
2020-08-16 15:33:49 +00:00
sealed trait IndexRegister
2020-08-13 04:16:30 +00:00
case object A extends Register {
2020-08-14 20:54:29 +00:00
def add[A](x: A)(implicit ctx: AssemblyContext, ev: Operand[A]): Unit =
2020-08-15 03:06:09 +00:00
ev.operandType match {
case ValueLiteral =>
ctx.push(ADC, x, s"add LITERAL to a")
case MemoryLocation =>
ctx.push(ADC, x, s"add ADDR to a")
}
2020-08-13 04:16:30 +00:00
}
2020-08-16 15:33:49 +00:00
case object X extends Register with DestinationA with IndexRegister {
2020-08-14 20:54:29 +00:00
def incr(implicit ctx: AssemblyContext): Unit =
ctx.push(INX, "incr x")
2020-08-16 15:33:49 +00:00
def upTo(s: String, start: Int, stop: Int)(f: AssemblyContext => Unit)(implicit ctx: AssemblyContext): Unit = {
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(CPX, stop)
ctx.branch(BNE, s)
}
2020-08-13 04:16:30 +00:00
}
2020-08-16 15:33:49 +00:00
case object Y extends Register with DestinationA with IndexRegister
2020-08-13 04:16:30 +00:00
}
class CPU {
def A: registers.A.type =
registers.A
2020-08-14 21:13:55 +00:00
def A_=[A](x: A)(implicit ctx: AssemblyContext, ev: Operand[A]): Unit =
2020-08-15 03:22:13 +00:00
ctx.push(LDA, x, "set A to " + ev.toShow(x))
2020-08-13 04:42:21 +00:00
2020-08-13 04:16:30 +00:00
def A_=(reg: registers.DestinationA)(implicit ctx: AssemblyContext): Unit =
2020-08-14 20:54:29 +00:00
reg match {
case registers.X =>
ctx.push(TXA)
case registers.Y =>
ctx.push(TYA)
}
2020-08-13 04:16:30 +00:00
def X: registers.X.type =
registers.X
2020-08-14 20:54:29 +00:00
def X_=(reg: registers.A.type)(implicit ctx: AssemblyContext): Unit =
ctx.push(TAX, s"set x to register $reg")
2020-08-13 04:16:30 +00:00
def Y: registers.Y.type =
registers.Y
def Y_=(reg: registers.A.type)(implicit ctx: AssemblyContext): Unit =
2020-08-14 20:54:29 +00:00
ctx.push(TAY, s"set x to register $reg")
2020-08-13 04:16:30 +00:00
}
class AssemblyContext {
2020-08-15 05:27:13 +00:00
private val xs: ListBuffer[Statement] =
2020-08-13 04:16:30 +00:00
ListBuffer()
2020-08-14 20:54:29 +00:00
def push(instruction: Instruction): Unit =
xs.append(UnaryInstruction(instruction, None))
def push(instruction: Instruction, s: String): Unit =
xs.append(UnaryInstruction(instruction, s.some))
2020-08-13 04:30:00 +00:00
2020-08-16 15:33:49 +00:00
def label(s: String): Unit =
2020-08-16 06:17:04 +00:00
xs.append(Label(s))
2020-08-15 04:49:07 +00:00
def push[A: Operand](instruction: Instruction, x: A): Unit =
2020-08-14 20:54:29 +00:00
xs.append(InstructionWithOperand(instruction, x, None))
2020-08-13 04:30:00 +00:00
2020-08-15 04:49:07 +00:00
def push[A: Operand](instruction: Instruction, x: A, s: String): Unit =
2020-08-14 20:54:29 +00:00
xs.append(InstructionWithOperand(instruction, x: A, s.some))
2020-08-13 04:16:30 +00:00
2020-08-16 15:33:49 +00:00
def branch(instruction: Instruction, label: String): Unit =
xs.append(BranchingInstruction(instruction, label))
2020-08-13 04:30:00 +00:00
def printOut(): Unit = {
2020-08-15 04:49:07 +00:00
xs.map(_.toAsm)
2020-08-14 21:08:59 +00:00
.foreach(println)
2020-08-13 04:30:00 +00:00
}
2020-08-15 05:27:13 +00:00
def triplets: List[(String, Option[String], Option[String])] =
2020-08-15 06:04:21 +00:00
xs.map(_.toTriplet).toList
2020-08-16 04:58:25 +00:00
def toFragment: AsmFragment =
AsmFragment(xs.toList)
2020-08-14 20:54:29 +00:00
}