如何在浮子上使用凿子dsptools

时间:2018-09-23 18:54:20

标签: scala fixed-point chisel

我需要将Float32转换为Chisel FixedPoint,执行一些计算,然后将FixedPoint转换回Float32。

例如,我需要以下内容:

val a = 3.1F
val b = 2.2F
val res = a * b // REPL returns res: Float 6.82

现在,我这样做:

import chisel3.experimental.FixedPoint

val fp_tpe = FixedPoint(6.W, 2.BP)
val a_fix = a.Something (fp_tpe) // convert a to FixPoint
val b_fix = b.Something (fp_tpe) // convert b to FixPoint
val res_fix = a_fix * b_fix
val res0 = res_fix.Something (fp_tpe) // convert back to Float

因此,我希望增量在的范围内,例如

val eps = 1e-4
assert ( abs(res - res0) < eps, "The error is too big")

谁可以为上面的伪代码提供Chisel3 FixedPoint类的工作示例?

3 个答案:

答案 0 :(得分:2)

看看下面的代码:

import chisel3._
import chisel3.core.FixedPoint
import dsptools._


class FPMultiplier extends Module {
  val io = IO(new Bundle {
    val a = Input(FixedPoint(6.W, binaryPoint = 2.BP))
    val b = Input(FixedPoint(6.W, binaryPoint = 2.BP))
    val c = Output(FixedPoint(12.W, binaryPoint = 4.BP))
  })

  io.c := io.a * io.b
}

class FPMultiplierTester(c: FPMultiplier) extends DspTester(c) {
  //
  // This will PASS, there is sufficient precision to model the inputs
  //
  poke(c.io.a, 3.25)
  poke(c.io.b, 2.5)

  step(1)
  expect(c.io.c, 8.125)

  //
  // This will FAIL, there is not sufficient precision to model the inputs
  // But this is only caught on output, this is likely the right approach
  // because you can't really pass in wrong precision data in hardware.
  //
  poke(c.io.a, 3.1)
  poke(c.io.b, 2.2)

  step(1)
  expect(c.io.c, 6.82)
}


object FPMultiplierMain {
  def main(args: Array[String]): Unit = {
    iotesters.Driver.execute(Array("-fiv"), () => new FPMultiplier) { c =>
      new FPMultiplierTester(c)
    }
  }
}

我还建议您查看ParameterizedAdder中的dsptools,这使您有一种如何编写传递不同类型的硬件模块的感觉。通常,您从DspReals开始,确认模型,然后开始使用FixedPoint大小进行实验/计算,以所需的精度返回结果。

答案 1 :(得分:1)

为了让他人受益,我提供了@Chick的改进解决方案,并用具有可变DSP容限的更抽象的Scala进行了重写。

package my_pkg

import chisel3._
import chisel3.core.{FixedPoint => FP}

import dsptools.{DspTester, DspTesterOptions, DspTesterOptionsManager}

class FPGenericIO (inType:FP, outType:FP) extends Bundle {
  val a = Input(inType)
  val b = Input(inType)
  val c = Output(outType)
}

class FPMul (inType:FP, outType:FP) extends Module {
  val io  = IO(new FPGenericIO(inType, outType))
  io.c := io.a * io.b
}

class FPMulTester(c: FPMul) extends DspTester(c) {

  val uut = c.io

  // This will PASS, there is sufficient precision to model the inputs
  poke(uut.a, 3.25)
  poke(uut.b, 2.5)

  step(1)
  expect(uut.c, 3.25*2.5)

  // This will FAIL, if you won't increase tolerance, which is eps = 0.0 by default
  poke(uut.a, 3.1)
  poke(uut.b, 2.2)

  step(1)
  expect(uut.c, 3.1*2.2)
}


object FPUMain extends App {

  val fpInType  = FP(8.W, 4.BP)
  val fpOutType = FP(12.W, 6.BP)

// Update default DspTester options and increase tolerance
  val opts = new DspTesterOptionsManager {

    dspTesterOptions = DspTesterOptions(
      fixTolLSBs = 2,
      genVerilogTb = false,
      isVerbose = true
    )
  }

  dsptools.Driver.execute (() => new FPMul(fpInType, fpOutType), opts) {
    c => new FPMulTester(c)
  }
}

答案 2 :(得分:0)

这是我最终的DSP乘法器实现,该实现应同时支持FixedPoint和DspComplex数字。 @ChickMarkley,如何更新此类以实现复杂乘法?

package my_pkg

import chisel3._

import dsptools.numbers.{Ring,DspComplex}
import dsptools.numbers.implicits._
import dsptools.{DspContext}

import chisel3.core.{FixedPoint => FP}
import dsptools.{DspTester, DspTesterOptions, DspTesterOptionsManager}


class FPGenericIO[A <: Data:Ring, B <: Data:Ring] (inType:A, outType:B) extends Bundle {
  val a = Input(inType.cloneType)
  val b = Input(inType.cloneType)
  val c = Output(outType.cloneType)

  override def cloneType = (new FPGenericIO(inType, outType)).asInstanceOf[this.type]

}

class FPMul[A <: Data:Ring, B <: Data:Ring] (inType:A, outType:B) extends Module {

  val io  = IO(new FPGenericIO(inType, outType))

  DspContext.withNumMulPipes(3) {
    io.c := io.a * io.b
  }
}

class FPMulTester[A <: Data:Ring, B <: Data:Ring](c: FPMul[A,B]) extends DspTester(c) {

  val uut = c.io

  //
  // This will PASS, there is sufficient precision to model the inputs
  //
  poke(uut.a, 3.25)
  poke(uut.b, 2.5)

  step(1)
  expect(uut.c, 3.25*2.5)

  //
  // This will FAIL, there is not sufficient precision to model the inputs
  // But this is only caught on output, this is likely the right approach
  // because you can't really pass in wrong precision data in hardware.
  //
  poke(uut.a, 3.1)
  poke(uut.b, 2.2)

  step(1)
  expect(uut.c, 3.1*2.2)
}


object FPUMain extends App {

  val fpInType  = FP(8.W, 4.BP)
  val fpOutType = FP(12.W, 6.BP)
  //val comp = DspComplex[Double] // How to declare a complex DSP type ?

  val opts = new DspTesterOptionsManager {

    dspTesterOptions = DspTesterOptions(
      fixTolLSBs = 0,
      genVerilogTb = false,
      isVerbose = true
    )
  }

  dsptools.Driver.execute (() => new FPMul(fpInType, fpOutType), opts) {
  //dsptools.Driver.execute (() => new FPMul(comp, comp), opts) { // <-- this won't compile
    c => new FPMulTester(c)
  }
}