Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

200 lignes
4.6KB

  1. class Invalid() extends Module {
  2. val io = IO(new Bundle{})
  3. val myVec = Module(new MyVector)
  4. // Uncomment line below to make the circuit valid
  5. // myVec.io.idx := 0.U
  6. }
  7. /**
  8. * This goes a little beyond the example in exercise.org.
  9. * WrapTest is a simple wrapper that catches Unconnected wires
  10. * and prints them with a less scary stacktrace.
  11. * Additionally, we throw a RunTimeException instead of ??? for
  12. * similar reasons
  13. *
  14. */
  15. class InvalidSpec extends FlatSpec with Matchers {
  16. behavior of "Invalid"
  17. it should "Fail with a RefNotInitializedException" in {
  18. try {
  19. wrapTester(
  20. chisel3.iotesters.Driver(() => new Invalid) { c =>
  21. // Just a placeholder so it compiles
  22. throw new RuntimeException with scala.util.control.NoStackTrace
  23. } should be(true)
  24. )
  25. }
  26. catch {
  27. case e: RuntimeException => println("all good!")
  28. case e: Exception => throw e
  29. }
  30. }
  31. }
  32. class SimpleDelay() extends Module {
  33. val io = IO(
  34. new Bundle {
  35. val dataIn = Input(UInt(32.W))
  36. val dataOut = Output(UInt(32.W))
  37. }
  38. )
  39. val delayReg = RegInit(UInt(32.W), 0.U)
  40. delayReg := io.dataIn
  41. io.dataOut := delayReg
  42. }
  43. class DelaySpec extends FlatSpec with Matchers {
  44. behavior of "SimpleDelay"
  45. it should "Delay input by one timestep" in {
  46. wrapTester(
  47. chisel3.iotesters.Driver(() => new SimpleDelay) { c =>
  48. new DelayTester(c)
  49. } should be(true)
  50. )
  51. }
  52. }
  53. // class DelayTester(c: SimpleDelay) extends PeekPokeTester(c) {
  54. // for(ii <- 0 until 10){
  55. // val input = scala.util.Random.nextInt(10)
  56. // poke(c.io.dataIn, input)
  57. // expect(c.io.dataOut, input)
  58. // }
  59. // }
  60. class DelayTester(c: SimpleDelay) extends PeekPokeTester(c) {
  61. for(ii <- 0 until 10){
  62. val input = scala.util.Random.nextInt(10)
  63. poke(c.io.dataIn, input)
  64. step(1)
  65. expect(c.io.dataOut, input)
  66. }
  67. }
  68. class DPCsimulatorSpec extends FlatSpec with Matchers {
  69. case class DotProdCalculator(vectorLen: Int, timeStep: Int = 0, accumulator: Int = 0){
  70. def update(inputA: Int, inputB: Int): (Int, Boolean, DotProdCalculator) = {
  71. val product = inputA * inputB
  72. if(((timeStep + 1) % vectorLen) == 0)
  73. (accumulator + product, true, this.copy(timeStep = 0, accumulator = 0))
  74. else
  75. (accumulator + product, false, this.copy(timeStep = this.timeStep + 1, accumulator = accumulator + product))
  76. }
  77. }
  78. val myDPC = DotProdCalculator(4)
  79. val dpcStream = Stream.iterate((0, myDPC)){ case(ts, dpc) =>
  80. val a = scala.util.Random.nextInt(4)
  81. val b = scala.util.Random.nextInt(4)
  82. val (output, valid, nextDPC) = dpc.update(a, b)
  83. val validString = if(valid) "yes" else "no"
  84. println(s"at timestep $ts:")
  85. println(s"INPUTS:")
  86. println(s"inputA: $a, inputB: $b")
  87. println(s"OUTPUTS:")
  88. println(s"output: $output, valid: $validString\n\n")
  89. (ts + 1, nextDPC)
  90. }.take(20)
  91. behavior of "Dot product simulator"
  92. it should "Be shoehorned into a test" in {
  93. dpcStream.last
  94. }
  95. }
  96. class EvilPrintfSpec extends FlatSpec with Matchers {
  97. class CountTo3() extends Module {
  98. val io = IO(
  99. new Bundle {
  100. val dataOut = Output(UInt(32.W))
  101. val validOutput = Output(Bool())
  102. }
  103. )
  104. val count = RegInit(UInt(32.W), 0.U)
  105. io.dataOut := count
  106. printf(p"according to printf output is: ${io.dataOut}\n")
  107. when(count != 3.U){
  108. count := count + 1.U
  109. io.validOutput := false.B
  110. io.dataOut := 0.U
  111. }.otherwise{
  112. io.validOutput := true.B
  113. io.dataOut := 1.U
  114. }
  115. }
  116. class CountTo3Test(c: CountTo3) extends PeekPokeTester(c) {
  117. for(ii <- 0 until 5){
  118. println(s"\nIn cycle $ii the output of counter is: ${peek(c.io.dataOut)}")
  119. step(1)
  120. }
  121. }
  122. behavior of "EvilPrintf"
  123. it should "tell a lie and hurt you" in {
  124. wrapTester(
  125. chisel3.iotesters.Driver(() => new CountTo3) { c =>
  126. new CountTo3Test(c)
  127. } should be(true)
  128. )
  129. }
  130. }
  131. class PrintfExampleSpec extends FlatSpec with Matchers {
  132. class PrintfExample() extends Module {
  133. val io = IO(new Bundle{})
  134. val counter = RegInit(0.U(8.W))
  135. counter := counter + 1.U
  136. printf("Counter is %d\n", counter)
  137. when(counter % 2.U === 0.U){
  138. printf("Counter is even\n")
  139. }
  140. }
  141. class PrintfTest(c: PrintfExample) extends PeekPokeTester(c) {
  142. for(ii <- 0 until 5){
  143. println(s"At cycle 0:")
  144. step(1)
  145. }
  146. }
  147. behavior of "Printf Example"
  148. it should "print" in {
  149. wrapTester(
  150. chisel3.iotesters.Driver(() => new PrintfExample) { c =>
  151. new PrintfTest(c)
  152. } should be(true)
  153. )
  154. }
  155. }