From 10135c61e23f2bba6794f4f08d3c7f54190ce534 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 8 May 2024 22:09:37 +0300 Subject: [PATCH] init. --- .github/workflows/dependency-graph.yml | 16 +++++++ .github/workflows/unit-tests.yml | 24 ++++++++++ .gitignore | 8 ++++ .scalafmt.conf | 47 +++++++++++++++++++ build.sbt | 22 +++++++++ project/build.properties | 1 + project/plugins.sbt | 1 + src/main/scala/ee/hrzn/chryse/HasIO.scala | 11 +++++ .../ee/hrzn/chryse/platform/GenericTop.scala | 21 +++++++++ .../ee/hrzn/chryse/platform/Platform.scala | 17 +++++++ .../platform/cxxrtl/CXXRTLPlatform.scala | 16 +++++++ .../chryse/platform/ice40/ICE40Platform.scala | 15 ++++++ .../hrzn/chryse/platform/ice40/ICE40Top.scala | 43 +++++++++++++++++ src/main/scala/ee/hrzn/chryse/sb/SB_GB.scala | 9 ++++ 14 files changed, 251 insertions(+) create mode 100644 .github/workflows/dependency-graph.yml create mode 100644 .github/workflows/unit-tests.yml create mode 100644 .gitignore create mode 100644 .scalafmt.conf create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 src/main/scala/ee/hrzn/chryse/HasIO.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/GenericTop.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/Platform.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/cxxrtl/CXXRTLPlatform.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Platform.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Top.scala create mode 100644 src/main/scala/ee/hrzn/chryse/sb/SB_GB.scala diff --git a/.github/workflows/dependency-graph.yml b/.github/workflows/dependency-graph.yml new file mode 100644 index 0000000..25a90cc --- /dev/null +++ b/.github/workflows/dependency-graph.yml @@ -0,0 +1,16 @@ +name: Update Dependency Graph +on: + push: + branches: + - main + +permissions: + contents: write + +jobs: + dependency-graph: + name: Update Dependency Graph + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: scalacenter/sbt-dependency-submission@v2 diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..b43698d --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,24 @@ +name: Unit tests + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 22 + uses: actions/setup-java@v4 + with: + java-version: '22' + distribution: 'temurin' + cache: 'sbt' + - name: Run tests + run: sbt test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96cf104 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +target/ +test_run_dir/ + +.metals/ +.bloop/ +metals.sbt + +.DS_Store diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..a72825b --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,47 @@ +version = "3.7.15" +runner.dialect = scala213 + +maxColumn = 80 + +newlines.avoidForSimpleOverflow = [slc, tooLong] + +assumeStandardLibraryStripMargin = true + +align.preset = most +align.multiline = false +align.tokens."+" = [ + { code = ":=", owner = "Term.ApplyInfix" }, + { code = "+=", owner = "Term.ApplyInfix" }, + { code = "++=", owner = "Term.ApplyInfix" }, + { code = "--=", owner = "Term.ApplyInfix" }, + { code = "-=", owner = "Term.ApplyInfix" }, +] + +binPack.literalArgumentLists = true +binPack.literalsIncludeSimpleExpr = true +binPack.literalsExclude = [ "Term.Name" ] + +# do I care about this? +docstrings.style = "SpaceAsterisk" +docstrings.wrap = yes +docstrings.oneline = fold + +rewrite.rules = [ + AvoidInfix, # do I want this? + RedundantBraces, + RedundantParens, + SortModifiers, + PreferCurlyFors, + Imports, +] + +rewrite.redundantBraces.maxBreaks = 1 +rewrite.redundantBraces.stringInterpolation = true + +rewrite.trailingCommas.style = always + +# unsure. +# includeCurlyBraceInSelectChains = false + +project.includePaths = ["glob:**.scala", "glob:**.sbt", "glob:**.sc", "glob:**.md"] +project.excludePaths = ["glob:**metals.sbt"] \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..bd82bc0 --- /dev/null +++ b/build.sbt @@ -0,0 +1,22 @@ +ThisBuild / scalaVersion := "2.13.12" +ThisBuild / version := "0.1.0-SNAPSHOT" +ThisBuild / organization := "ee.hrzn" + +val chiselVersion = "6.3.0" + +lazy val root = (project in file(".")) + .settings( + name := "chryse", + libraryDependencies ++= Seq( + "org.chipsalliance" %% "chisel" % chiselVersion, + "org.scalatest" %% "scalatest" % "3.2.18" % "test", + "edu.berkeley.cs" %% "chiseltest" % "6.0.0", + ), + scalacOptions ++= Seq( + "-language:reflectiveCalls", "-deprecation", "-feature", "-Xcheckinit", + "-Ymacro-annotations", + ), + addCompilerPlugin( + "org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full, + ), + ) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..b19d4e1 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.9.7 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..5708f81 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn diff --git a/src/main/scala/ee/hrzn/chryse/HasIO.scala b/src/main/scala/ee/hrzn/chryse/HasIO.scala new file mode 100644 index 0000000..f99c297 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/HasIO.scala @@ -0,0 +1,11 @@ +package ee.hrzn.chryse + +import chisel3._ + +// This looks a lot like the FixedIO*Module stuff. + +trait HasIO[ContainedIO <: Data] extends RawModule { + def createIo(): ContainedIO + + val io = IO(createIo()) +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/GenericTop.scala b/src/main/scala/ee/hrzn/chryse/platform/GenericTop.scala new file mode 100644 index 0000000..0722014 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/GenericTop.scala @@ -0,0 +1,21 @@ +package ee.hrzn.chryse.platform + +import chisel3._ +import ee.hrzn.chryse.HasIO +import ee.hrzn.chryse.platform.Platform + +class GenericTop[Top <: HasIO[_ <: Data]](genTop: => Top)(implicit + platform: Platform, +) extends Module { + override def desiredName = "top" + + private val top = Module(genTop) + private val io = IO(top.createIo()) + io :<>= top.io.as[Data] +} + +object GenericTop { + def apply[Top <: HasIO[_ <: Data]](genTop: => Top)(implicit + platform: Platform, + ) = new GenericTop(genTop) +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/Platform.scala b/src/main/scala/ee/hrzn/chryse/platform/Platform.scala new file mode 100644 index 0000000..99150f5 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/Platform.scala @@ -0,0 +1,17 @@ +package ee.hrzn.chryse.platform + +import chisel3._ +import ee.hrzn.chryse.HasIO + +// Similar to chisel3.choice.ModuleChoice. + +trait Platform { + val id: String + val clockHz: Int +} + +trait ElaboratablePlatform extends Platform { + def apply[Top <: HasIO[_ <: Data]](top: => Top)(implicit + platform: Platform, + ): RawModule +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/cxxrtl/CXXRTLPlatform.scala b/src/main/scala/ee/hrzn/chryse/platform/cxxrtl/CXXRTLPlatform.scala new file mode 100644 index 0000000..c4e496f --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/cxxrtl/CXXRTLPlatform.scala @@ -0,0 +1,16 @@ +package ee.hrzn.chryse.platform.cxxrtl + +import chisel3._ +import ee.hrzn.chryse.HasIO +import ee.hrzn.chryse.platform.ElaboratablePlatform +import ee.hrzn.chryse.platform.GenericTop +import ee.hrzn.chryse.platform.Platform + +case object CXXRTLPlatform extends ElaboratablePlatform { + val id = "cxxrtl" + val clockHz = 3_000_000 + + override def apply[Top <: HasIO[_ <: Data]](top: => Top)(implicit + platform: Platform, + ) = GenericTop(top) +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Platform.scala b/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Platform.scala new file mode 100644 index 0000000..b60ed01 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Platform.scala @@ -0,0 +1,15 @@ +package ee.hrzn.chryse.platform.ice40 + +import chisel3._ +import ee.hrzn.chryse.HasIO +import ee.hrzn.chryse.platform.ElaboratablePlatform +import ee.hrzn.chryse.platform.Platform + +case object ICE40Platform extends ElaboratablePlatform { + val id = "ice40" + val clockHz = 12_000_000 + + override def apply[Top <: HasIO[_ <: Data]](top: => Top)(implicit + platform: Platform, + ) = ICE40Top(top) +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Top.scala b/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Top.scala new file mode 100644 index 0000000..ffb8970 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/ice40/ICE40Top.scala @@ -0,0 +1,43 @@ +package ee.hrzn.chryse.platform.ice40 + +import chisel3._ +import chisel3.util._ +import ee.hrzn.chryse.HasIO +import ee.hrzn.chryse.platform.Platform +import ee.hrzn.chryse.sb.SB_GB + +class ICE40Top[Top <: HasIO[_ <: Data]](genTop: => Top)(implicit + platform: Platform, +) extends RawModule { + override def desiredName = "top" + + private val clki = IO(Input(Clock())) + + private val clk_gb = Module(new SB_GB) + clk_gb.USER_SIGNAL_TO_GLOBAL_BUFFER := clki + private val clk = clk_gb.GLOBAL_BUFFER_OUTPUT + + private val timerLimit = (15e-6 * platform.clockHz).toInt + private val resetTimerReg = + withClock(clk)(Reg(UInt(unsignedBitLength(timerLimit).W))) + private val reset = Wire(Bool()) + + when(resetTimerReg === timerLimit.U) { + reset := false.B + }.otherwise { + reset := true.B + resetTimerReg := resetTimerReg + 1.U + } + private val io_ubtn = IO(Input(Bool())) + + private val top = + withClockAndReset(clk, reset | ~io_ubtn)(Module(genTop)) + private val io = IO(top.createIo()) + io :<>= top.io.as[Data] +} + +object ICE40Top { + def apply[Top <: HasIO[_ <: Data]](genTop: => Top)(implicit + platform: Platform, + ) = new ICE40Top(genTop) +} diff --git a/src/main/scala/ee/hrzn/chryse/sb/SB_GB.scala b/src/main/scala/ee/hrzn/chryse/sb/SB_GB.scala new file mode 100644 index 0000000..a752e78 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/sb/SB_GB.scala @@ -0,0 +1,9 @@ +package ee.hrzn.chryse.sb + +import chisel3._ +import chisel3.experimental.ExtModule + +class SB_GB extends ExtModule { + val USER_SIGNAL_TO_GLOBAL_BUFFER = IO(Input(Clock())) + val GLOBAL_BUFFER_OUTPUT = IO(Output(Clock())) +}