Add exercises

This commit is contained in:
Job Noorman 2022-10-13 11:52:00 +02:00
commit ac3cf23632
19 changed files with 21160 additions and 0 deletions

6
Exercises/.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
*.v
.idea/
project/
simWorkspace/
target/
tmp/

13
Exercises/build.sbt Normal file
View file

@ -0,0 +1,13 @@
name := "SpinalExercises"
version := "0.1"
scalaVersion := "2.11.12"
val spinalVersion = "1.7.3"
fork := true
libraryDependencies ++= Seq(
"com.github.spinalhdl" % "spinalhdl-core_2.11" % spinalVersion,
"com.github.spinalhdl" % "spinalhdl-lib_2.11" % spinalVersion,
compilerPlugin("com.github.spinalhdl" % "spinalhdl-idsl-plugin_2.11" % spinalVersion)
)

View file

@ -0,0 +1,16 @@
package examples
import spinal.core._
class Counter16 extends Component {
val enable = in(Bool)
val counter = out(UInt(16 bits))
val lsb = new Counter8
lsb.enable := enable
val msb = new Counter8
msb.enable := enable && (lsb.counter === 0xff);
counter := msb.counter @@ lsb.counter
}

View file

@ -0,0 +1,16 @@
package examples
import spinal.core._
class Counter8 extends Component {
val enable = in(Bool)
val counter = out(Reg(UInt(8 bits)).init(0))
def count(): Unit = {
enable := True
}
when (enable) {
counter := counter + 1
}
}

View file

@ -0,0 +1,30 @@
package examples
import spinal.core._
import spinal.lib._
class SpiBus(numSlaves: Int = 1) extends Bundle with IMasterSlave {
val sclk = Bool
val mosi = Bool
val miso = Bool
val ss = Bits(numSlaves bits)
override def asMaster(): Unit = {
out(sclk, mosi, ss)
in(ss)
}
}
class SpiMaster extends Component {
val bus = master(new SpiBus)
}
class SpiSlave extends Component {
val bus = slave(new SpiBus)
}
class Soc extends Component {
val spiMaster = new SpiMaster
val spiSlave = new SpiSlave
spiMaster.bus <> spiSlave.bus
}

View file

@ -0,0 +1,27 @@
package examples
import spinal.core._
import spinal.core.sim._
object CounterGen {
def main(args: Array[String]): Unit = {
SpinalVerilog(new Counter16)
}
}
object CounterSim {
def main(args: Array[String]) {
SimConfig.withWave.compile(new Counter16).doSim {dut =>
dut.enable #= true
dut.clockDomain.forkStimulus(10)
var tick = 0
while (tick < 256 * 256) {
dut.clockDomain.waitSampling()
tick += 1
}
}
}
}

View file

@ -0,0 +1,15 @@
package exercises
import spinal.core._
class Popcnt(width: BitCount = 4 bits, multiCycle: Boolean = false) extends Component {
// Replace these by the correct I/O ports
val start, ready = Bool
val value, count = UInt(42 bits)
if (!multiCycle) {
// Implement asynchronous logic here.
} else {
// Implement multi-cycle logic here.
}
}

View file

@ -0,0 +1,14 @@
package exercises
import spinal.core._
class ShiftReg extends Component {
def addReg(name: String, width: BitCount, delay: Int): (Bits, Bits) = {
(null, null)
}
def getReg(name: String): (Bits, Bits) = {
(null, null)
}
}

View file

@ -0,0 +1,55 @@
package exercises
import spinal.core._
import spinal.core.sim._
object PopcntSim {
def runSim(width: BitCount, multiCycle: Boolean, input: Int): Unit = {
SimConfig.withWave.compile(new Popcnt(width, multiCycle)).doSim {dut =>
dut.clockDomain.forkStimulus(10)
dut.value #= input
dut.start #= true
dut.clockDomain.waitSampling()
dut.start #= false
while (!dut.ready.toBoolean) {
dut.clockDomain.waitSampling()
}
dut.clockDomain.waitSampling()
println(s"popcnt($input) = ${dut.count.toBigInt}")
}
}
def main(args: Array[String]) {
runSim(4 bits, false, 15)
}
}
object ShiftRegSim {
def createShiftReg: ShiftReg = {
val sr = new ShiftReg
sr.addReg("foo", 8 bits, 3)
sr.addReg("bar", 16 bits, 5)
sr
}
def main(args: Array[String]) {
SimConfig.withWave.compile(createShiftReg).doSim {dut =>
dut.clockDomain.forkStimulus(10)
val (fooIn, fooOut) = dut.getReg("foo")
val (barIn, barOut) = dut.getReg("bar")
var tick = 0
while (tick < 10) {
fooIn #= tick
barIn #= 10 - tick
dut.clockDomain.waitSampling()
tick += 1
}
}
}
}