25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

257 satır
5.6KB

  1. /**
  2. * This code supplements instructions.org
  3. * Once you've gone through the instructions you can do
  4. * whatever you want with it.
  5. */
  6. package Ex0
  7. import chisel3._
  8. import chisel3.iotesters.PeekPokeTester
  9. import org.scalatest.{Matchers, FlatSpec}
  10. import TestUtils._
  11. // class MyVector() extends Module {
  12. // val io = IO(
  13. // new Bundle {
  14. // val idx = Input(UInt(32.W))
  15. // val out = Output(UInt(32.W))
  16. // }
  17. // )
  18. // val values = List(1, 2, 3, 4)
  19. // io.out := values(io.idx)
  20. // }
  21. // class MyVector() extends Module {
  22. // val io = IO(
  23. // new Bundle {
  24. // val idx = Input(UInt(32.W))
  25. // val out = Output(UInt(32.W))
  26. // }
  27. // )
  28. // // val values: List[Int] = List(1, 2, 3, 4)
  29. // val values = Vec(1, 2, 3, 4)
  30. // io.out := values(io.idx)
  31. // }
  32. class MyVector() extends Module {
  33. val io = IO(
  34. new Bundle {
  35. val idx = Input(UInt(32.W))
  36. val out = Output(UInt(32.W))
  37. }
  38. )
  39. val values = Vec(0.U, 1.U, 2.U, 3.U)
  40. io.out := values(io.idx)
  41. }
  42. class MyVector2() extends Module {
  43. val io = IO(
  44. new Bundle {
  45. val idx = Input(UInt(2.W))
  46. val out = Output(UInt(32.W))
  47. }
  48. )
  49. val values = Array(0.U, 1.U, 2.U, 3.U)
  50. val myWire = Wire(UInt(4.W))
  51. io.out := values(0)
  52. for(ii <- 0 until 4){
  53. when(io.idx === ii.U){
  54. io.out := values(ii)
  55. }
  56. }
  57. }
  58. class MyVecSpec extends FlatSpec with Matchers {
  59. behavior of "MyVec"
  60. it should "Output whatever idx points to" in {
  61. wrapTester(
  62. chisel3.iotesters.Driver(() => new MyVector2) { c =>
  63. new MyVecTester(c)
  64. } should be(true)
  65. )
  66. }
  67. }
  68. class MyVecTester(c: MyVector2) extends PeekPokeTester(c) {
  69. for(ii <- 0 until 4){
  70. poke(c.io.idx, ii)
  71. expect(c.io.out, ii)
  72. }
  73. }
  74. class Invalid() extends Module {
  75. val io = IO(new Bundle{})
  76. val myVec = Module(new MyVector)
  77. // Uncomment line below to make the circuit valid
  78. // myVec.io.idx := 0.U
  79. }
  80. /**
  81. * This goes a little beyond the example in exercise.org.
  82. * WrapTest is a simple wrapper that catches Unconnected wires
  83. * and prints them with a less scary stacktrace.
  84. * Additionally, we throw a RunTimeException instead of ??? for
  85. * similar reasons
  86. *
  87. */
  88. class InvalidSpec extends FlatSpec with Matchers {
  89. behavior of "Invalid"
  90. it should "Fail with a RefNotInitializedException" in {
  91. try {
  92. wrapTester(
  93. chisel3.iotesters.Driver(() => new Invalid) { c =>
  94. // Just a placeholder so it compiles
  95. throw new RuntimeException with scala.util.control.NoStackTrace
  96. } should be(true)
  97. )
  98. }
  99. catch {
  100. case e: RuntimeException => println("all good!")
  101. case e: Exception => throw e
  102. }
  103. }
  104. }
  105. class SimpleDelay() extends Module {
  106. val io = IO(
  107. new Bundle {
  108. val dataIn = Input(UInt(32.W))
  109. val dataOut = Output(UInt(32.W))
  110. }
  111. )
  112. val delayReg = RegInit(UInt(32.W), 0.U)
  113. delayReg := io.dataIn
  114. io.dataOut := delayReg
  115. }
  116. class DelaySpec extends FlatSpec with Matchers {
  117. behavior of "SimpleDelay"
  118. it should "Delay input by one timestep" in {
  119. wrapTester(
  120. chisel3.iotesters.Driver(() => new SimpleDelay) { c =>
  121. new DelayTester(c)
  122. } should be(true)
  123. )
  124. }
  125. }
  126. // class DelayTester(c: SimpleDelay) extends PeekPokeTester(c) {
  127. // for(ii <- 0 until 10){
  128. // val input = scala.util.Random.nextInt(10)
  129. // poke(c.io.dataIn, input)
  130. // expect(c.io.dataOut, input)
  131. // }
  132. // }
  133. class DelayTester(c: SimpleDelay) extends PeekPokeTester(c) {
  134. for(ii <- 0 until 10){
  135. val input = scala.util.Random.nextInt(10)
  136. poke(c.io.dataIn, input)
  137. step(1)
  138. expect(c.io.dataOut, input)
  139. }
  140. }
  141. class DPCsimulatorSpec extends FlatSpec with Matchers {
  142. case class DotProdCalculator(vectorLen: Int, timeStep: Int = 0, accumulator: Int = 0){
  143. def update(inputA: Int, inputB: Int): (Int, Boolean, DotProdCalculator) = {
  144. val product = inputA * inputB
  145. if(((timeStep + 1) % vectorLen) == 0)
  146. (accumulator + product, true, this.copy(timeStep = 0, accumulator = 0))
  147. else
  148. (accumulator + product, false, this.copy(timeStep = this.timeStep + 1, accumulator = accumulator + product))
  149. }
  150. }
  151. val myDPC = DotProdCalculator(4)
  152. val dpcStream = Stream.iterate((0, myDPC)){ case(ts, dpc) =>
  153. val a = scala.util.Random.nextInt(4)
  154. val b = scala.util.Random.nextInt(4)
  155. val (output, valid, nextDPC) = dpc.update(a, b)
  156. val validString = if(valid) "yes" else "no"
  157. println(s"at timestep $ts:")
  158. println(s"INPUTS:")
  159. println(s"inputA: $a, inputB: $b")
  160. println(s"OUTPUTS:")
  161. println(s"output: $output, valid: $validString\n\n")
  162. (ts + 1, nextDPC)
  163. }.take(20)
  164. behavior of "Dot product simulator"
  165. it should "Be shoehorned into a test" in {
  166. dpcStream.last
  167. }
  168. }
  169. class EvilPrintfSpec extends FlatSpec with Matchers {
  170. class CountTo3() extends Module {
  171. val io = IO(
  172. new Bundle {
  173. val dataOut = Output(UInt(32.W))
  174. val validOutput = Output(Bool())
  175. }
  176. )
  177. val count = RegInit(UInt(32.W), 0.U)
  178. io.dataOut := count
  179. printf(p"according to printf output is: ${io.dataOut}\n")
  180. when(count != 3.U){
  181. count := count + 1.U
  182. io.validOutput := false.B
  183. io.dataOut := 0.U
  184. }.otherwise{
  185. io.validOutput := true.B
  186. io.dataOut := 1.U
  187. }
  188. }
  189. class CountTo3Test(c: CountTo3) extends PeekPokeTester(c) {
  190. for(ii <- 0 until 5){
  191. println(s"\nIn cycle $ii the output of counter is: ${peek(c.io.dataOut)}")
  192. step(1)
  193. }
  194. }
  195. behavior of "EvilPrintf"
  196. it should "tell a lie and hurt you" in {
  197. wrapTester(
  198. chisel3.iotesters.Driver(() => new CountTo3) { c =>
  199. new CountTo3Test(c)
  200. } should be(true)
  201. )
  202. }
  203. }