Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
template: wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed May 10, 2024
1 parent 0b508c3 commit 1543996
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 0 deletions.
30 changes: 30 additions & 0 deletions template/.github/workflows/dependency-graph.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: update dependency graph

on:
push:
branches:
- main

permissions:
contents: write

jobs:
dependency-graph:
runs-on: ubuntu-latest
steps:
- name: Checkout chryse
uses: actions/checkout@v4
with:
repository: kivikakk/chryse
- name: Set up JDK 22
uses: actions/setup-java@v4
with:
java-version: '22'
distribution: 'temurin'
cache: 'sbt'
- name: Build and install chryse locally
run: sbt publishLocal

- name: Checkout this
uses: actions/checkout@v4
- uses: scalacenter/sbt-dependency-submission@v2
40 changes: 40 additions & 0 deletions template/.github/workflows/synth-ice40.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: iCE40 synthesis

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout chryse
uses: actions/checkout@v4
with:
repository: kivikakk/chryse
- name: Set up JDK 22
uses: actions/setup-java@v4
with:
java-version: '22'
distribution: 'temurin'
cache: 'sbt'
- name: Build and install chryse locally
run: sbt publishLocal

- name: Install OSS CAD Suite
uses: YosysHQ/setup-oss-cad-suite@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- run: yosys --version

- name: Checkout this
uses: actions/checkout@v4
- name: Elaborate
run: sbt run
- name: Synthesise
run: make ice40
32 changes: 32 additions & 0 deletions template/.github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Unit tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout chryse
uses: actions/checkout@v4
with:
repository: kivikakk/chryse
- name: Set up JDK 22
uses: actions/setup-java@v4
with:
java-version: '22'
distribution: 'temurin'
cache: 'sbt'
- name: Build and install chryse locally
run: sbt publishLocal

- name: Checkout this
uses: actions/checkout@v4
- name: Run tests
run: sbt test
18 changes: 18 additions & 0 deletions template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
build/

*.asc
*.bin
*.vcd
*.json
*.rpt
*.tim
*.sv

target/
test_run_dir/

.metals/
.bloop/
metals.sbt

.DS_Store
47 changes: 47 additions & 0 deletions template/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -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"]
41 changes: 41 additions & 0 deletions template/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
BASENAME = Top
BUILD_DIR = build
ARTIFACT_PREFIX = $(BUILD_DIR)/$(BASENAME)

SCALA_SRCS = $(shell find src/main/scala)

.PHONY: ice40-prog clean

all:
@echo "Targets:"
@echo " make ice40"
@echo " make ice40-prog"
@echo " make clean"

clean:
-rm build/*

$(BASENAME)-%.sv: $(SCALA_SRCS)
sbt run

ice40: $(ARTIFACT_PREFIX).bin

ice40-prog: $(ARTIFACT_PREFIX).bin
iceprog $<

$(ARTIFACT_PREFIX).bin: $(ARTIFACT_PREFIX).asc
icepack $< $@

$(ARTIFACT_PREFIX).asc: $(ARTIFACT_PREFIX).json $(BASENAME)-ice40.pcf
nextpnr-ice40 -q --log $(ARTIFACT_PREFIX).tim \
--up5k --package sg48 \
--json $(ARTIFACT_PREFIX).json \
--pcf $(BASENAME)-ice40.pcf \
--asc $@

$(ARTIFACT_PREFIX).json: $(BASENAME)-ice40.sv
@mkdir -p $(BUILD_DIR)
yosys -q -g -l $(ARTIFACT_PREFIX).rpt \
-p 'read_verilog -sv $<' \
-p 'synth_ice40 -top top' \
-p 'write_json $@'
7 changes: 7 additions & 0 deletions template/Top-ice40.pcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set_io clki 35
set_frequency clki 12.0

set_io io_ubtn 10

set_io io_ledr 11
set_io io_ledg 37
44 changes: 44 additions & 0 deletions template/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import scala.sys.process._

ThisBuild / scalaVersion := "2.13.12"
ThisBuild / version := "0.1.0"
ThisBuild / organization := "com.example"

val chiselVersion = "6.3.0"

lazy val root = (project in file("."))
.settings(
name := "newproject",
libraryDependencies ++= Seq(
"org.chipsalliance" %% "chisel" % chiselVersion,
"org.scalatest" %% "scalatest" % "3.2.18" % "test",
"edu.berkeley.cs" %% "chiseltest" % "6.0.0",
"ee.hrzn" %% "chryse" % "0.1.0-SNAPSHOT",
),
scalacOptions ++= Seq(
"-language:reflectiveCalls", "-deprecation", "-feature", "-Xcheckinit",
"-Ymacro-annotations",
),
addCompilerPlugin(
"org.chipsalliance" % "chisel-plugin" % chiselVersion cross CrossVersion.full,
),
)

// TODO: we want to move these into chryse itself, to be imported into projects that use it.
// Same for cxxsim stuff.
lazy val ice40 = inputKey[Unit]("Elaborate and synthesise for ice40")
ice40 := {
(Compile / run).evaluated
if (("make ice40" !) != 0) {
throw new IllegalStateException("ice40 failed to synthesise")
}
}

lazy val ice40prog =
inputKey[Unit]("Elaborate, synthesise for ice40, and program the board")
ice40prog := {
(Compile / run).evaluated
if (("make ice40-prog" !) != 0) {
throw new IllegalStateException("ice40 failed to synthesise")
}
}
1 change: 1 addition & 0 deletions template/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 1.9.7
1 change: 1 addition & 0 deletions template/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logLevel := Level.Warn
54 changes: 54 additions & 0 deletions template/src/main/scala/com/example/newproject/Top.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.newproject

import _root_.circt.stage.ChiselStage
import chisel3._
import chisel3.util._
import ee.hrzn.chryse.HasIO
import ee.hrzn.chryse.platform.ElaboratablePlatform
import ee.hrzn.chryse.platform.Platform
import ee.hrzn.chryse.platform.ice40.ICE40Platform

import java.io.PrintWriter

class TopIO extends Bundle {
val ledr = Output(Bool())
val ledg = Output(Bool())
}

class Top(implicit platform: Platform) extends Module with HasIO[TopIO] {
def createIo() = new TopIO

private val ledReg = RegInit(true.B)
io.ledr := ledReg
val timerReg = RegInit(
((platform.clockHz / 4) - 1)
.U(unsignedBitLength((platform.clockHz / 2) - 1).W),
)
when(timerReg === 0.U) {
ledReg := ~ledReg
timerReg := ((platform.clockHz / 2) - 1).U
}.otherwise {
timerReg := timerReg - 1.U
}

io.ledg := false.B
}

object Top extends App {
def apply()(implicit platform: ElaboratablePlatform) =
platform(new Top)

implicit private val platform: ElaboratablePlatform = ICE40Platform
private val firtoolOpts = Array(
"--lowering-options=disallowLocalVariables",
"-disable-all-randomization",
"-strip-debug-info",
)
val verilog =
ChiselStage.emitSystemVerilog(Top(), firtoolOpts = firtoolOpts)
new PrintWriter(s"Top-${platform.id}.sv", "utf-8") {
try
write(verilog)
finally close()
}
}
28 changes: 28 additions & 0 deletions template/src/test/scala/com/example/newproject/TopSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.newproject

import chisel3._
import chiseltest._
import chiseltest.simulator.WriteVcdAnnotation
import ee.hrzn.chryse.platform.Platform
import org.scalatest.flatspec.AnyFlatSpec

class TopSpec extends AnyFlatSpec with ChiselScalatestTester {
behavior.of("Top")

implicit private val platform: Platform = new Platform {
val id = "topspec"
val clockHz = 8
}

it should "blink the light" in {
test(new Top()).withAnnotations(Seq(WriteVcdAnnotation)) { c =>
// ledg is always false (on).
// ledr starts true (off) for 1/4 duty, then toggles evenly at 1/2 duty.
for { ledr <- Seq(1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1) } {
c.io.ledg.expect(false.B)
c.io.ledr.expect((ledr == 1).B)
c.clock.step()
}
}
}
}

0 comments on commit 1543996

Please sign in to comment.