val path = System.getProperty("user.dir") + "/source/load-ivy.sc" interp.load.module(ammonite.ops.Path(java.nio.file.FileSystems.getDefault().getPath(path))) import chisel3._ import chisel3.util._ import chisel3.tester._ import chisel3.tester.RawTester.test println(List(1, 2, 3, 4).map(x => x + 1)) // explicit argument list in function println(List(1, 2, 3, 4).map(_ + 1)) // equivalent to the above, but implicit arguments println(List(1, 2, 3, 4).map(_.toString + "a")) // the output element type can be different from the input element type println(List((1, 5), (2, 6), (3, 7), (4, 8)).map { case (x, y) => x*y }) // this unpacks a tuple, note use of curly braces // Related: Scala has a syntax for constructing lists of sequential numbers println(0 to 10) // to is inclusive , the end point is part of the result println(0 until 10) // until is exclusive at the end, the end point is not part of the result // Those largely behave like lists, and can be useful for generating indices: val myList = List("a", "b", "c", "d") println((0 until 4).map(myList(_))) // Now you try: // Fill in the blanks (the ???) such that this doubles the elements of the input list. // This should return: List(2, 4, 6, 8) println(List(1, 2, 3, 4).map(???)) println(List(1, 2, 3, 4).zipWithIndex) // note indices start at zero println(List("a", "b", "c", "d").zipWithIndex) println(List(("a", "b"), ("c", "d"), ("e", "f"), ("g", "h")).zipWithIndex) // tuples nest println(List(1, 2, 3, 4).reduce((a, b) => a + b)) // returns the sum of all the elements println(List(1, 2, 3, 4).reduce(_ * _)) // returns the product of all the elements println(List(1, 2, 3, 4).map(_ + 1).reduce(_ + _)) // you can chain reduce onto the result of a map // Important note: reduce will fail with an empty list println(List[Int]().reduce(_ * _)) // Now you try: // Fill in the blanks (the ???) such that this returns the product of the double of the elements of the input list. // This should return: (1*2)*(2*2)*(3*2)*(4*2) = 384 println(List(1, 2, 3, 4).map(???).reduce(???)) println(List(1, 2, 3, 4).fold(0)(_ + _)) // equivalent to the sum using reduce println(List(1, 2, 3, 4).fold(1)(_ + _)) // like above, but accumulation starts at 1 println(List().fold(1)(_ + _)) // unlike reduce, does not fail on an empty input // Now you try: // Fill in the blanks (the ???) such that this returns the double the product of the elements of the input list. // This should return: 2*(1*2*3*4) = 48 // Note: unless empty list tolerance is needed, reduce is a much better fit here. println(List(1, 2, 3, 4).fold(???)(???)) class MyRoutingArbiter(numChannels: Int) extends Module { val io = IO(new Bundle { val in = Vec(numChannels, Flipped(Decoupled(UInt(8.W)))) val out = Decoupled(UInt(8.W)) } ) // YOUR CODE BELOW ??? } test(new MyRoutingArbiter(4)) { c => // verify that the computation is correct // Set input defaults for(i <- 0 until 4) { c.io.in(i).valid.poke(false.B) c.io.in(i).bits.poke(i.U) c.io.out.ready.poke(true.B) } c.io.out.valid.expect(false.B) // Check single input valid behavior with backpressure for (i <- 0 until 4) { c.io.in(i).valid.poke(true.B) c.io.out.valid.expect(true.B) c.io.out.bits.expect(i.U) c.io.out.ready.poke(false.B) c.io.in(i).ready.expect(false.B) c.io.out.ready.poke(true.B) c.io.in(i).valid.poke(false.B) } // Basic check of multiple input ready behavior with backpressure c.io.in(1).valid.poke(true.B) c.io.in(2).valid.poke(true.B) c.io.out.bits.expect(1.U) c.io.in(1).ready.expect(true.B) c.io.in(0).ready.expect(false.B) c.io.out.ready.poke(false.B) c.io.in(1).ready.expect(false.B) } println("SUCCESS!!") // Scala Code: if we get here, our tests passed!