I am new to Chisel HDL, and I found the Chisel HDL does provide fixed point respresentation. (I found this link: Fixed Point Arithmetic in Chisel HDL)
when I try it in the chisel hdl it actually doesn't work:
import Chisel._
class Toy extends Module {
val io = new Bundle {
val in0 = SFix(4, 12).asInput
val in1 = SFix(4, 12).asInput
val out = SFix(4, 16).asOutput
val oraw = Bits(OUTPUT, width=128)
}
val int_result = -io.in0 * (io.in0 + io.in1)
io.out := int_result
io.oraw := int_result.raw
}
class ToyTest(c: Toy) extends Tester(c) {
for (i <- 0 until 20) {
val i0 = 0.5
val i1 = 0.25
poke(c.io.in0, i0)
poke(c.io.in1, i1)
val res = -i0 * (i0+i1)
step(1)
expect(c.io.out, res)
}
}
object Toy {
def main(args: Array[String]): Unit = {
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Toy())) {
c => new ToyTest(c)
}
}
}
In my build.sbt file, I choose the latest release chisel by:
libraryDependencies += "edu.berkeley.cs" %% "chisel" % "latest.release"
According to Chisel code SFix seems to be deprecated, Fixed should be used instead. I modified your code to use it, but there is a problem with poke and expect. It seems that Fixed is not supported yet by poke and expect.
import Chisel._
class Toy extends Module {
val io = new Bundle {
val in0 = Fixed(INPUT, 4, 12)
val in1 = Fixed(INPUT, 4, 12)
val out = Fixed(OUTPUT, 8, 24)
val oraw = Bits(OUTPUT, width=128)
}
val int_result = -io.in0 * (io.in0 + io.in1)
io.out := int_result
io.oraw := int_result.asUInt()
}
class ToyTest(c: Toy) extends Tester(c) {
for (i <- 0 until 20) {
val i0 = Fixed(0.5, 4, 12)
val i1 = Fixed(0.25, 4, 12)
c.io.in0 := i0
c.io.in1 := i1
//poke(c.io.in0, i0)
//poke(c.io.in1, i1)
val res = -i0 * (i0+i1)
step(1)
//expect(c.io.out, res)
}
}
object Toy {
def main(args: Array[String]): Unit = {
val tutArgs = args.slice(1, args.length)
chiselMainTest(tutArgs, () => Module(new Toy())) {
c => new ToyTest(c)
}
}
}