Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

119 wiersze
3.0KB

  1. package Ex0
  2. import chisel3._
  3. import chisel3.iotesters.PeekPokeTester
  4. import org.scalatest.{Matchers, FlatSpec}
  5. import TestUtils._
  6. class DotProdSpec extends FlatSpec with Matchers {
  7. import DotProdTests._
  8. val rand = new scala.util.Random(100)
  9. val elements = 7
  10. behavior of "DotProd"
  11. it should "Only signal valid output at end of calculation" in {
  12. wrapTester(
  13. chisel3.iotesters.Driver(() => new DotProd(elements)) { c =>
  14. new SignalsWhenDone(c)
  15. } should be(true)
  16. )
  17. }
  18. it should "Calculate the correct output" in {
  19. wrapTester(
  20. chisel3.iotesters.Driver(() => new DotProd(elements)) { c =>
  21. new CalculatesCorrectResult(c)
  22. } should be(true)
  23. )
  24. }
  25. it should "Calculate the correct output and signal when appropriate" in {
  26. wrapTester(
  27. chisel3.iotesters.Driver(() => new DotProd(elements)) { c =>
  28. new CalculatesCorrectResultAndSignals(c)
  29. } should be(true)
  30. )
  31. }
  32. }
  33. object DotProdTests {
  34. val rand = new scala.util.Random(100)
  35. class SignalsWhenDone(c: DotProd) extends PeekPokeTester(c) {
  36. for(ii <- 0 until c.elements - 1){
  37. expect(c.io.outputValid, false)
  38. step(1)
  39. }
  40. expect(c.io.outputValid, true)
  41. step(1)
  42. for(ii <- 0 until c.elements - 1){
  43. expect(c.io.outputValid, false)
  44. step(1)
  45. }
  46. expect(c.io.outputValid, true)
  47. step(1)
  48. }
  49. class CalculatesCorrectResult(c: DotProd) extends PeekPokeTester(c) {
  50. val inputsA = List.fill(c.elements)(rand.nextInt(10))
  51. val inputsB = List.fill(c.elements)(rand.nextInt(10))
  52. println("runnign dot prod calc with inputs:")
  53. println(inputsA.mkString("[", "] [", "]"))
  54. println(inputsB.mkString("[", "] [", "]"))
  55. val expectedOutput = (for ((a, b) <- inputsA zip inputsB) yield a * b) sum
  56. for(ii <- 0 until c.elements){
  57. poke(c.io.dataInA, inputsA(ii))
  58. poke(c.io.dataInB, inputsB(ii))
  59. if(ii == c.elements - 1)
  60. expect(c.io.dataOut, expectedOutput)
  61. step(1)
  62. }
  63. }
  64. class CalculatesCorrectResultAndSignals(c: DotProd) extends PeekPokeTester(c) {
  65. val inputsA = List.fill(c.elements)(rand.nextInt(10))
  66. val inputsB = List.fill(c.elements)(rand.nextInt(10))
  67. println("runnign dot prod calc with inputs:")
  68. println(inputsA.mkString("[", "] [", "]"))
  69. println(inputsB.mkString("[", "] [", "]"))
  70. val expectedOutput = (for ((a, b) <- inputsA zip inputsB) yield a * b) sum
  71. for(ii <- 0 until c.elements){
  72. poke(c.io.dataInA, inputsA(ii))
  73. poke(c.io.dataInB, inputsB(ii))
  74. if(ii == c.elements - 1){
  75. expect(c.io.dataOut, expectedOutput)
  76. expect(c.io.outputValid, true)
  77. }
  78. else
  79. expect(c.io.outputValid, false)
  80. step(1)
  81. }
  82. for(ii <- 0 until c.elements){
  83. poke(c.io.dataInA, inputsA(ii))
  84. poke(c.io.dataInB, inputsB(ii))
  85. if(ii == c.elements - 1){
  86. expect(c.io.dataOut, expectedOutput)
  87. expect(c.io.outputValid, true)
  88. }
  89. else
  90. expect(c.io.outputValid, false)
  91. step(1)
  92. }
  93. }
  94. }