Skip to content

Commit 898949c

Browse files
committed
init
0 parents  commit 898949c

File tree

8 files changed

+251
-0
lines changed

8 files changed

+251
-0
lines changed

.github/workflows/push.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
on: [pull_request]
2+
jobs:
3+
checks:
4+
runs-on: ubuntu-latest
5+
container: archlinux/archlinux:latest
6+
steps:
7+
- name: Install dependencies
8+
run: pacman -Syu --noconfirm make mill z3
9+
- uses: actions/checkout@v2
10+
- name: Check Format
11+
run: make checkformat
12+
- name: Test
13+
run: make test

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# mill
2+
out/
3+
.bsp/
4+
.idea/
5+
.idea_modules/
6+
test_run_dir/

.scalafmt.conf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version = 2.6.4
2+
3+
maxColumn = 120
4+
align = most
5+
continuationIndent.defnSite = 2
6+
assumeStandardLibraryStripMargin = true
7+
docstrings = ScalaDoc
8+
lineEndings = preserve
9+
includeCurlyBraceInSelectChains = false
10+
danglingParentheses = true
11+
12+
align.tokens.add = [
13+
{
14+
code = ":"
15+
},
16+
{
17+
code = ":="
18+
},
19+
{
20+
code = "="
21+
}
22+
23+
]
24+
25+
newlines.alwaysBeforeCurlyBraceLambdaParams = false
26+
newlines.alwaysBeforeMultilineDef = false
27+
newlines.implicitParamListModifierForce = [before]
28+
29+
verticalMultiline.atDefnSite = true
30+
31+
optIn.annotationNewlines = true
32+
33+
rewrite.rules = [SortImports, PreferCurlyFors, AvoidInfix]

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
compile:
2+
mill -i __.compile
3+
4+
bsp:
5+
mill -i mill.bsp.BSP/install
6+
7+
clean:
8+
git clean -fd
9+
10+
cleanAll:
11+
git clean -fdx
12+
13+
reformat:
14+
mill -i __.reformat
15+
16+
checkformat:
17+
mill -i __.checkFormat
18+
19+
test:
20+
mill -i __.test

build.sc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// import Mill dependency
2+
import mill._
3+
import mill.scalalib._
4+
import mill.scalalib.scalafmt.ScalafmtModule
5+
import mill.scalalib.TestModule.Utest
6+
// support BSP
7+
import mill.bsp._
8+
9+
object playground extends ScalaModule with ScalafmtModule { m =>
10+
override def scalaVersion = "2.12.13"
11+
override def scalacOptions = Seq(
12+
"-Xsource:2.11",
13+
"-language:reflectiveCalls",
14+
"-deprecation",
15+
"-feature",
16+
"-Xcheckinit",
17+
// Enables autoclonetype2 in 3.4.x (on by default in 3.5)
18+
"-P:chiselplugin:useBundlePlugin"
19+
)
20+
override def ivyDeps = Agg(
21+
ivy"edu.berkeley.cs::chisel3:3.4.3",
22+
)
23+
override def scalacPluginIvyDeps = Agg(
24+
ivy"edu.berkeley.cs:::chisel3-plugin:3.4.3",
25+
ivy"org.scalamacros:::paradise:2.1.1"
26+
)
27+
object test extends Tests with Utest {
28+
override def ivyDeps = m.ivyDeps() ++ Agg(
29+
ivy"com.lihaoyi::utest:0.7.10",
30+
ivy"edu.berkeley.cs::chiseltest:0.3.3",
31+
)
32+
}
33+
}

playground/src/DecoupledGCD.scala

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import chisel3._
2+
import chisel3.util.Decoupled
3+
4+
class GcdInputBundle(val w: Int) extends Bundle {
5+
val value1 = UInt(w.W)
6+
val value2 = UInt(w.W)
7+
}
8+
9+
class GcdOutputBundle(val w: Int) extends Bundle {
10+
val value1 = UInt(w.W)
11+
val value2 = UInt(w.W)
12+
val gcd = UInt(w.W)
13+
}
14+
15+
/**
16+
* Compute Gcd using subtraction method.
17+
* Subtracts the smaller from the larger until register y is zero.
18+
* value input register x is then the Gcd.
19+
* Unless first input is zero then the Gcd is y.
20+
* Can handle stalls on the producer or consumer side
21+
*/
22+
class DecoupledGcd(width: Int) extends MultiIOModule {
23+
val input = IO(Flipped(Decoupled(new GcdInputBundle(width))))
24+
val output = IO(Decoupled(new GcdOutputBundle(width)))
25+
26+
val xInitial = Reg(UInt())
27+
val yInitial = Reg(UInt())
28+
val x = Reg(UInt())
29+
val y = Reg(UInt())
30+
val busy = RegInit(false.B)
31+
val resultValid = RegInit(false.B)
32+
33+
input.ready := !busy
34+
output.valid := resultValid
35+
output.bits := DontCare
36+
37+
when(busy) {
38+
when(x > y) {
39+
x := x - y
40+
}.otherwise {
41+
y := y - x
42+
}
43+
when(x === 0.U || y === 0.U) {
44+
when(x === 0.U) {
45+
output.bits.gcd := y
46+
}.otherwise {
47+
output.bits.gcd := x
48+
}
49+
50+
output.bits.value1 := xInitial
51+
output.bits.value2 := yInitial
52+
resultValid := true.B
53+
54+
when(output.ready && resultValid) {
55+
busy := false.B
56+
resultValid := false.B
57+
}
58+
}
59+
}.otherwise {
60+
when(input.valid) {
61+
val bundle = input.deq()
62+
x := bundle.value1
63+
y := bundle.value2
64+
xInitial := bundle.value1
65+
yInitial := bundle.value2
66+
busy := true.B
67+
}
68+
}
69+
}

playground/src/GCD.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import chisel3._
2+
3+
/**
4+
* Compute GCD using subtraction method.
5+
* Subtracts the smaller from the larger until register y is zero.
6+
* value in register x is then the GCD
7+
*/
8+
class GCD extends Module {
9+
val io = IO(new Bundle {
10+
val value1 = Input(UInt(16.W))
11+
val value2 = Input(UInt(16.W))
12+
val loadingValues = Input(Bool())
13+
val outputGCD = Output(UInt(16.W))
14+
val outputValid = Output(Bool())
15+
})
16+
17+
val x = Reg(UInt())
18+
val y = Reg(UInt())
19+
20+
when(x > y) { x := x - y }.otherwise { y := y - x }
21+
22+
when(io.loadingValues) {
23+
x := io.value1
24+
y := io.value2
25+
}
26+
27+
io.outputGCD := x
28+
io.outputValid := y === 0.U
29+
}

playground/test/src/GCDSpec.scala

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import chisel3._
2+
import chisel3.tester._
3+
import chisel3.experimental.BundleLiterals._
4+
5+
import utest._
6+
7+
/**
8+
* This is a trivial example of how to run this Specification
9+
* From within sbt use:
10+
* {{{
11+
* testOnly gcd.GcdDecoupledTester
12+
* }}}
13+
* From a terminal shell use:
14+
* {{{
15+
* sbt 'testOnly gcd.GcdDecoupledTester'
16+
* }}}
17+
*/
18+
object GCDSpec extends ChiselUtestTester {
19+
val tests = Tests {
20+
test("GCD") {
21+
testCircuit(new DecoupledGcd(16)) {
22+
dut =>
23+
dut.input.initSource()
24+
dut.input.setSourceClock(dut.clock)
25+
dut.output.initSink()
26+
dut.output.setSinkClock(dut.clock)
27+
val testValues = for {x <- 0 to 10; y <- 0 to 10} yield (x, y)
28+
val inputSeq = testValues.map { case (x, y) => (new GcdInputBundle(16)).Lit(_.value1 -> x.U, _.value2 -> y.U) }
29+
val resultSeq = testValues.map { case (x, y) =>
30+
(new GcdOutputBundle(16)).Lit(_.value1 -> x.U, _.value2 -> y.U, _.gcd -> BigInt(x).gcd(BigInt(y)).U)
31+
}
32+
fork {
33+
// push inputs into the calculator, stall for 11 cycles one third of the way
34+
val (seq1, seq2) = inputSeq.splitAt(resultSeq.length / 3)
35+
dut.input.enqueueSeq(seq1)
36+
dut.clock.step(11)
37+
dut.input.enqueueSeq(seq2)
38+
}.fork {
39+
// retrieve computations from the calculator, stall for 10 cycles one half of the way
40+
val (seq1, seq2) = resultSeq.splitAt(resultSeq.length / 2)
41+
dut.output.expectDequeueSeq(seq1)
42+
dut.clock.step(10)
43+
dut.output.expectDequeueSeq(seq2)
44+
}.join()
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)