package com.htmlism import cats.implicits._ import java.io.PrintWriter object MatchOpcodes { def paddedBinary(n: Int, width: Int) = String.format(s"%${width}s", Integer.toBinaryString(n)).replace(" ", "0") def main(args: Array[String]): Unit = write(args(0))(doStuff) def generatedOpcodes: Map[Int, (Instruction, AddressingMode)] = (0 to 255) .map(n => n -> toOpcode(n)) .collect { case (n, Some(x)) => (n, x) } .toMap // format: off def injectedOpcodesRelative: Map[Int, (Instruction, AddressingMode)] = Map( 0x10 -> BPL, 0x30 -> BMI, 0x50 -> BVC, 0x70 -> BVS, 0x90 -> BCC, 0xB0 -> BCS, 0xD0 -> BNE, 0xF0 -> BEQ, ).view.mapValues(x => x -> Relative).toMap def injectedOpcodesImplied: Map[Int, (Instruction, AddressingMode)] = Map( 0x00 -> BRK, 0x20 -> JSR, 0x40 -> RTI, 0x60 -> RTS, 0x08 -> PHP, 0x28 -> PLP, 0x48 -> PHA, 0x68 -> PLA, 0x88 -> DEY, 0xA8 -> TAY, 0xC8 -> INY, 0xE8 -> INX, 0x18 -> CLC, 0x38 -> SEC, 0x58 -> CLI, 0x78 -> SEI, 0x98 -> TYA, 0xB8 -> CLV, 0xD8 -> CLD, 0xF8 -> SED, 0x8A -> TXA, 0x9A -> TXS, 0xAA -> TAX, 0xBA -> TSX, 0xCA -> DEX, 0xEA -> NOP ).view.mapValues(x => x -> Implied).toMap // format: on def doStuff(out: PrintWriter): Unit = { val lookup = generatedOpcodes ++ injectedOpcodesImplied ++ injectedOpcodesRelative out.print("
") val fancyColumns = for { c <- 0 to 3 b <- 0 to 7 } yield b * 4 + c for (f <- fancyColumns) out.print(s" | ${paddedBinary(f, 8)} | ") out.print("|
---|---|---|
${paddedBinary(r, 3)} | ") for (f <- fancyColumns) { val fullInt = r + f lookup.get(fullInt) match { case Some((ints, mode)) => val hex = f"$fullInt%2X" out.print( s"""$ints $mode $hex | """
)
case None =>
out.print(s"UNDEF | ") } } out.print("
") for (c <- wideColumns) out.print(s" | ${paddedBinary(c, 5)} | ") out.print("|
---|---|---|
${paddedBinary(r, 3)} | ") for (c <- wideColumns) { val fullInt = (r << 5) + c lookup.get(fullInt) match { case Some((ints, mode)) => val hex = f"$fullInt%2X" out.print( s"""$ints $mode $hex | """
)
case None =>
out.print(s"UNDEF | ") } } out.print("
") val columns = for { y <- 0 to 1 x <- 0 to 1 z <- 0 to 1 } yield (z << 2) + (x << 1) + y val rows = 0 to 7 for (c <- columns) out.print(s" | ${paddedBinary(c, 3)} | ") out.print("|
---|---|---|
${paddedBinary(r, 3)} | ") for (c <- columns) { val fullInt = (r << (3 + 2)) + (c << 2) + n lookup.get(fullInt) match { case Some((ints, mode)) => val hex = f"$fullInt%2X" out.print( s"""$ints $mode $hex | """
)
case None =>
out.print(s"UNDEF | ") } } out.print("