From 28d945c8f1915969473177a7f4ae5e12483c78a4 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Wed, 7 Sep 2022 11:21:22 -0400 Subject: [PATCH] define load and store --- .../htmlism/nescant/ReadWriteLocation.scala | 3 +- .../com/htmlism/nescant/VolatileDevice.scala | 3 +- .../scala/com/htmlism/scratchpad/Load.scala | 8 ++++ .../com/htmlism/scratchpad/Register.scala | 38 +++++++++++++++++++ .../scala/com/htmlism/scratchpad/Store.scala | 4 ++ .../scala/com/htmlism/ShiftExtractor.scala | 3 +- .../com/htmlism/mos6502/dsl/Address.scala | 6 +-- .../mos6502/dsl/AsmDocumentContext.scala | 3 +- .../scala/com/htmlism/mos6502/dsl/Color.scala | 6 +-- .../com/htmlism/mos6502/dsl/Mapping.scala | 6 +-- .../com/htmlism/mos6502/dsl/Operand.scala | 6 +-- .../mos6502/dsl/ReadWriteLocation.scala | 6 +-- .../htmlism/mos6502/dsl/VolatileDevice.scala | 3 +- .../com/htmlism/mos6502/dsl/DslSpec.scala | 6 +-- .../mos6502/dsl/snake/AsciiValue.scala | 3 +- .../htmlism/mos6502/dsl/snake/Direction.scala | 3 +- 16 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala create mode 100644 scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala diff --git a/nescant/src/main/scala/com/htmlism/nescant/ReadWriteLocation.scala b/nescant/src/main/scala/com/htmlism/nescant/ReadWriteLocation.scala index 8ef442a..cd0ebb4 100644 --- a/nescant/src/main/scala/com/htmlism/nescant/ReadWriteLocation.scala +++ b/nescant/src/main/scala/com/htmlism/nescant/ReadWriteLocation.scala @@ -17,6 +17,5 @@ object ReadWriteLocation: new ByteSink[ReadWriteLocation[A]] {} implicit def operandForReadWriteLocation[A]: Operand[ReadWriteLocation[A]] = - new Operand[ReadWriteLocation[A]] { + new Operand[ReadWriteLocation[A]]: def encode(x: ReadWriteLocation[A]): String = "" - } diff --git a/nescant/src/main/scala/com/htmlism/nescant/VolatileDevice.scala b/nescant/src/main/scala/com/htmlism/nescant/VolatileDevice.scala index d8111e2..f72179a 100644 --- a/nescant/src/main/scala/com/htmlism/nescant/VolatileDevice.scala +++ b/nescant/src/main/scala/com/htmlism/nescant/VolatileDevice.scala @@ -18,6 +18,5 @@ object VolatileDevice: new ByteSource[VolatileDevice[A]] {} implicit def operandForVolatileDevice[A]: Operand[VolatileDevice[A]] = - new Operand[VolatileDevice[A]] { + new Operand[VolatileDevice[A]]: def encode(x: VolatileDevice[A]): String = "" - } diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala new file mode 100644 index 0000000..f854f78 --- /dev/null +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Load.scala @@ -0,0 +1,8 @@ +package com.htmlism.scratchpad + +trait Load[A]: + // from constant + def init: String + + // from register + def from: String diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala index 5f59d59..b79b388 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Register.scala @@ -13,6 +13,44 @@ object Register: given registerY: Register[Y] with def self: String = "Y" + object A: + given loadA: Load[A] with + def init: String = + "LDA" + + def from: String = + "LDA" + + given storeA: Store[A] with + def to: String = + "STA" + class A + + object X: + given loadX: Load[X] with + def init: String = + "LDX" + + def from: String = + "LDX" + + given storeX: Store[X] with + def to: String = + "STX" + class X + + object Y: + given loadY: Load[Y] with + def init: String = + "LDY" + + def from: String = + "LDY" + + given storeY: Store[Y] with + def to: String = + "STY" + class Y diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala new file mode 100644 index 0000000..0842eab --- /dev/null +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/Store.scala @@ -0,0 +1,4 @@ +package com.htmlism.scratchpad + +trait Store[A]: + def to: String diff --git a/src/main/scala/com/htmlism/ShiftExtractor.scala b/src/main/scala/com/htmlism/ShiftExtractor.scala index 00aed94..b720279 100644 --- a/src/main/scala/com/htmlism/ShiftExtractor.scala +++ b/src/main/scala/com/htmlism/ShiftExtractor.scala @@ -16,7 +16,7 @@ trait BitExtractor[A]: * A combinator for appending one extractor to another */ def >>[B](that: BitExtractor[B]): BitExtractor[(A, B)] = - new BitExtractor[(A, B)] { + new BitExtractor[(A, B)]: def length: Int = self.length + that.length def unapply(n: Int): Option[(A, B)] = @@ -25,7 +25,6 @@ trait BitExtractor[A]: shifted = n >> that.length a <- self.unapply(shifted) } yield (a, b) - } object AtomExtractor: @tailrec diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Address.scala b/src/main/scala/com/htmlism/mos6502/dsl/Address.scala index 6fb6cdd..2a20deb 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Address.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Address.scala @@ -5,7 +5,7 @@ sealed trait Address: object ZeroAddress: implicit val operandZero: Operand[ZeroAddress] = - new Operand[ZeroAddress] { + new Operand[ZeroAddress]: val operandType: OperandType = MemoryLocation @@ -14,7 +14,6 @@ object ZeroAddress: def toAddressLiteral(x: ZeroAddress): String = String.format("$%02x", x.n) - } implicit val definitionValueForZero: DefinitionValue[ZeroAddress] = operandZero.toAddressLiteral(_) @@ -23,7 +22,7 @@ case class ZeroAddress(n: Int) extends Address object GlobalAddress: implicit val operandGlobal: Operand[GlobalAddress] = - new Operand[GlobalAddress] { + new Operand[GlobalAddress]: val operandType: OperandType = MemoryLocation @@ -32,7 +31,6 @@ object GlobalAddress: def toAddressLiteral(x: GlobalAddress): String = String.format("$%04x", x.n) - } implicit val definitionValueForGlobal: DefinitionValue[GlobalAddress] = operandGlobal.toAddressLiteral(_) diff --git a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala index 570adc3..abb7625 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala @@ -84,10 +84,9 @@ case class Definition[A](name: String, x: A, comment: Option[String])(implicit e object Definition: implicit def namedResourceForDefinition[A]: NamedResource[Definition[A]] = - new NamedResource[Definition[A]] { + new NamedResource[Definition[A]]: def toDefinitions(x: Definition[A]): List[Definition[_]] = List(x) - } def apply[A: DefinitionValue](name: String, x: A): Definition[A] = Definition(name, x, None) diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala index 3e6af50..4385ced 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala @@ -6,7 +6,7 @@ sealed trait Color object Color: implicit val colorOperand: Operand[Color] = - new Operand[Color] { + new Operand[Color]: def toAddressLiteral(x: Color): String = "#" + x.toString.toLowerCase() @@ -15,10 +15,9 @@ object Color: def operandType: OperandType = ValueLiteral - } implicit val colorEnum: EnumAsm[Color] = - new EnumAsm[Color] { + new EnumAsm[Color]: def comment: String = "Colors" @@ -47,7 +46,6 @@ object Color: def comment(x: Color): String = x.toString - } case object Black extends Color case object White extends Color diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala b/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala index c1886bb..361fdcc 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala @@ -27,7 +27,7 @@ trait Mapping[A]: object Mapping: implicit def mappingForBitField[A](implicit ev: BitField[A]): Mapping[A] = - new Mapping[A] { + new Mapping[A]: private lazy val valueMap = ev.all .toList @@ -48,10 +48,9 @@ object Mapping: def comment(x: A): String = "" // TODO - } implicit def mappingForEnumAsm[A](implicit ev: EnumAsm[A]): Mapping[A] = - new Mapping[A] { + new Mapping[A]: private lazy val valueMap = ev.all .toList @@ -72,4 +71,3 @@ object Mapping: def comment(x: A): String = ev.comment(x) - } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala index 3329f0d..8228eb8 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala @@ -14,7 +14,7 @@ trait Operand[A]: object Operand: implicit val operandInt: Operand[Int] = - new Operand[Int] { + new Operand[Int]: val operandType: OperandType = ValueLiteral @@ -23,10 +23,9 @@ object Operand: def toAddressLiteral(x: Int): String = String.format("#$%02x", x) - } implicit def operandForMapping[A](implicit ev: Mapping[A]): Operand[A] = - new Operand[A] { + new Operand[A]: def toAddressLiteral(x: A): String = "#" + ev.label(x) @@ -35,4 +34,3 @@ object Operand: def operandType: OperandType = ValueLiteral - } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/ReadWriteLocation.scala b/src/main/scala/com/htmlism/mos6502/dsl/ReadWriteLocation.scala index b0394f1..7d1f6be 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/ReadWriteLocation.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/ReadWriteLocation.scala @@ -20,7 +20,7 @@ case class ReadWriteLocation[A: Operand](name: String, address: ZeroAddress): object ReadWriteLocation: implicit def operandForReadWriteLocation[A]: Operand[ReadWriteLocation[A]] = - new Operand[ReadWriteLocation[A]] { + new Operand[ReadWriteLocation[A]]: def toAddressLiteral(x: ReadWriteLocation[A]): String = x.name @@ -29,12 +29,10 @@ object ReadWriteLocation: def operandType: OperandType = MemoryLocation - } implicit def namedResourceForReadWriteLocation[A]: NamedResource[ReadWriteLocation[A]] = - new NamedResource[ReadWriteLocation[A]] { + new NamedResource[ReadWriteLocation[A]]: def toDefinitions(x: ReadWriteLocation[A]): List[Definition[ZeroAddress]] = List { Definition(x.name, x.address, "Read/write location for A values") } - } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/VolatileDevice.scala b/src/main/scala/com/htmlism/mos6502/dsl/VolatileDevice.scala index 74b28ff..69537d3 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/VolatileDevice.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/VolatileDevice.scala @@ -18,9 +18,8 @@ case class VolatileDevice[A](name: String, address: ZeroAddress): object VolatileDevice: implicit def namedResourceForVolatileDevice[A]: NamedResource[VolatileDevice[A]] = - new NamedResource[VolatileDevice[A]] { + new NamedResource[VolatileDevice[A]]: def toDefinitions(x: VolatileDevice[A]): List[Definition[ZeroAddress]] = List { Definition(x.name, x.address, "Volatile generator for A values") } - } diff --git a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala index fec9e5e..5a6ab89 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala @@ -95,7 +95,7 @@ case object Power extends Triforce object Triforce: implicit val enumTriforce: EnumAsm[Triforce] = - new EnumAsm[Triforce] { + new EnumAsm[Triforce]: def comment: String = "foo as enum" @@ -107,7 +107,6 @@ object Triforce: def comment(x: Triforce): String = x.toString - } sealed trait TestDirection @@ -118,7 +117,7 @@ case object Right extends TestDirection object TestDirection: implicit val bitFieldDirection: BitField[TestDirection] = - new BitField[TestDirection] { + new BitField[TestDirection]: def definitionGroupComment: String = "foo as bit field" @@ -127,4 +126,3 @@ object TestDirection: def label(x: TestDirection): String = x.toString.toLowerCase - } diff --git a/src/test/scala/com/htmlism/mos6502/dsl/snake/AsciiValue.scala b/src/test/scala/com/htmlism/mos6502/dsl/snake/AsciiValue.scala index d2b6311..f53b128 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/snake/AsciiValue.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/snake/AsciiValue.scala @@ -5,7 +5,7 @@ import cats.data.NonEmptyList object AsciiValue: implicit val asciiValueMapping: Mapping[AsciiValue] = - new Mapping[AsciiValue] { + new Mapping[AsciiValue]: def definitionGroupComment: String = "ASCII values of keys controlling the snake" @@ -24,7 +24,6 @@ object AsciiValue: def comment(x: AsciiValue): String = x.toString - } sealed trait AsciiValue diff --git a/src/test/scala/com/htmlism/mos6502/dsl/snake/Direction.scala b/src/test/scala/com/htmlism/mos6502/dsl/snake/Direction.scala index 3bbfc6c..701122f 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/snake/Direction.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/snake/Direction.scala @@ -5,7 +5,7 @@ import cats.data.NonEmptyList object Direction: implicit val directionBitField: BitField[Direction] = - new BitField[Direction] { + new BitField[Direction]: def definitionGroupComment: String = "Directions" @@ -14,7 +14,6 @@ object Direction: def label(x: Direction): String = "moving" + x.toString - } sealed trait Direction