Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

136 rindas
2.9KB

  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 Examples
  7. import Ex0._
  8. import chisel3._
  9. import chisel3.iotesters.PeekPokeTester
  10. import org.scalatest.{Matchers, FlatSpec}
  11. import TestUtils._
  12. class MyIncrementTest extends FlatSpec with Matchers {
  13. class MyIncrement(val incrementBy: Int) extends Module {
  14. val io = IO(
  15. new Bundle {
  16. val dataIn = Input(UInt(32.W))
  17. val dataOut = Output(UInt(32.W))
  18. }
  19. )
  20. io.dataOut := io.dataIn + incrementBy.U
  21. }
  22. class TheTestRunner(c: MyIncrement) extends PeekPokeTester(c) {
  23. for(ii <- 0 until 5){
  24. poke(c.io.dataIn, ii)
  25. val o = peek(c.io.dataOut)
  26. println(s"At cycle $ii the output of myIncrement was $o")
  27. expect(c.io.dataOut, ii+c.incrementBy)
  28. }
  29. }
  30. behavior of "my increment"
  31. it should "increment its input by 3" in {
  32. chisel3.iotesters.Driver(() => new MyIncrement(3)) { c =>
  33. new TheTestRunner(c)
  34. } should be(true)
  35. }
  36. }
  37. class MyIncrementManyTest extends FlatSpec with Matchers {
  38. class MyIncrement(val incrementBy: Int) extends Module {
  39. val io = IO(
  40. new Bundle {
  41. val dataIn = Input(UInt(32.W))
  42. val dataOut = Output(UInt(32.W))
  43. }
  44. )
  45. io.dataOut := io.dataIn + incrementBy.U
  46. }
  47. class MyIncrementN(val incrementBy: Int, val numIncrementors: Int) extends Module {
  48. val io = IO(
  49. new Bundle {
  50. val dataIn = Input(UInt(32.W))
  51. val dataOut = Output(UInt(32.W))
  52. }
  53. )
  54. val incrementors = Array.fill(numIncrementors){ Module(new MyIncrement(incrementBy)) }
  55. for(ii <- 1 until numIncrementors){
  56. incrementors(ii).io.dataIn := incrementors(ii - 1).io.dataOut
  57. }
  58. incrementors(0).io.dataIn := io.dataIn
  59. io.dataOut := incrementors.last.io.dataOut
  60. }
  61. class TheTestRunner(c: MyIncrementN) extends PeekPokeTester(c) {
  62. for(ii <- 0 until 5){
  63. poke(c.io.dataIn, ii)
  64. val o = peek(c.io.dataOut)
  65. println(s"At cycle $ii the output of myIncrement was $o")
  66. expect(c.io.dataOut, ii+(c.incrementBy*c.numIncrementors))
  67. }
  68. }
  69. behavior of "my incrementN"
  70. it should "increment its input by 3*4" in {
  71. chisel3.iotesters.Driver(() => new MyIncrementN(4, 3)) { c =>
  72. new TheTestRunner(c)
  73. } should be(true)
  74. }
  75. }
  76. class ChiselConditional() extends Module {
  77. val io = IO(
  78. new Bundle {
  79. val a = Input(UInt(32.W))
  80. val b = Input(UInt(32.W))
  81. val opSel = Input(Bool())
  82. val out = Output(UInt(32.W))
  83. }
  84. )
  85. when(io.opSel){
  86. io.out := io.a + io.b
  87. }.otherwise{
  88. io.out := io.a - io.b
  89. }
  90. }
  91. class ScalaConditional(opSel: Boolean) extends Module {
  92. val io = IO(
  93. new Bundle {
  94. val a = Input(UInt(32.W))
  95. val b = Input(UInt(32.W))
  96. val out = Output(UInt(32.W))
  97. }
  98. )
  99. if(opSel){
  100. io.out := io.a + io.b
  101. } else {
  102. io.out := io.a - io.b
  103. }
  104. }