Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Created December 6, 2022 03:57
Show Gist options
  • Save mpilquist/6b75ae0c72c4d5b0da6d03bfd6355d29 to your computer and use it in GitHub Desktop.
Save mpilquist/6b75ae0c72c4d5b0da6d03bfd6355d29 to your computer and use it in GitHub Desktop.
LED flasher from Ben Eater's 6502 part 2 video
//> using scala "3.2.1"
//> using lib "org.scodec::scodec-bits:1.1.34"
//> using lib "co.fs2::fs2-io:3.4.0"
import scodec.bits.*
import cats.effect.IO
import cats.effect.unsafe.implicits.global
import fs2.{Stream, Chunk}
import fs2.io.file.{Files, Path}
def instructions(code: String): ByteVector =
Stream(code)
.through(fs2.text.lines)
.map(_.split("#").head)
.map(ByteVector.fromHex(_).get)
.compile.fold(ByteVector.empty)(_ ++ _)
val code = instructions("""
a9 ff # lda #$ff
8d 02 60 # sta $6002
a9 55 # lda #$55
8d 00 60 # sta $6000
a9 aa # lda #$aa
8d 00 60 # sta $6000
4c 05 80 # jmp $8005
""")
val rom = code ++ ByteVector.fill(32768 - code.size)(0xea)
val prom = rom.patch(0x7ffc, hex"0080")
Stream.chunk(Chunk.byteVector(prom)).through(Files[IO].writeAll(Path("rom.bin"))).compile.drain.unsafeRunSync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment