Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 8bc825a

Browse files
committed
Add ShiftArray
Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
1 parent 092d7a7 commit 8bc825a

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 IBM
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package esp.examples
16+
17+
import chisel3._
18+
import chisel3.util.Valid
19+
import chisel3.experimental.withReset
20+
21+
class ShiftArrayIO[A <: Data](rows: Int, cols: Int, gen: A) extends Bundle {
22+
23+
val in = Flipped(Valid(Vec(rows, gen.cloneType)))
24+
val out = Valid(Vec(rows, Vec(cols, gen.cloneType)))
25+
26+
}
27+
28+
class ShiftArray[A <: Data](val rows: Int, val cols: Int, gen: A) extends Module {
29+
30+
val io = IO(new ShiftArrayIO(rows, cols, gen))
31+
32+
val regArray = Seq.fill(rows)(Seq.fill(cols)(Reg(gen)))
33+
val count = RegInit(0.U(cols.W))
34+
35+
/* Shift regArray left when the input fires */
36+
when (io.in.fire()) {
37+
count := count ## 1.U
38+
regArray
39+
.zip(io.in.bits)
40+
.foreach{ case (a, in) => a.foldLeft(in){ case (r, l) => l := r; l } }
41+
}
42+
43+
/* Route the regArray to the output */
44+
io.out.bits.flatten
45+
.zip(regArray.flatten)
46+
.foreach{ case (l, r) => l := r }
47+
48+
io.out.valid := count.toBools.last
49+
50+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2019 IBM
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package esptests.examples
16+
17+
import chisel3._
18+
import chisel3.iotesters.{ChiselFlatSpec, Driver, AdvTester}
19+
20+
import esp.examples.ShiftArray
21+
22+
class ShiftArrayTester[A <: Bits](dut: ShiftArray[A]) extends AdvTester(dut) {
23+
def init(): Unit = {
24+
Seq(dut.io.in.valid).map(p => wire_poke(p, false))
25+
}
26+
27+
def load(in: Seq[Int]): Unit = {
28+
require(in.size % dut.cols == 0)
29+
in
30+
.grouped(dut.cols)
31+
.foreach{ case a =>
32+
wire_poke(dut.io.in.valid, 1)
33+
a.zipWithIndex.map{ case (b, i) => wire_poke(dut.io.in.bits(i), b) }
34+
step(1)
35+
wire_poke(dut.io.in.valid, 0) }
36+
}
37+
38+
val input: Seq[Int] = 0 until dut.rows * dut.cols
39+
40+
reset(4)
41+
expect(dut.io.out.valid, false)
42+
43+
init()
44+
step(1)
45+
46+
load(0 until 9)
47+
48+
expect(dut.io.out.valid, true)
49+
println(peek(dut.io.out.bits).mkString(", "))
50+
51+
load(9 until 12)
52+
expect(dut.io.out.valid, true)
53+
println(peek(dut.io.out.bits).mkString(", "))
54+
55+
reset(1)
56+
expect(dut.io.out.valid, false)
57+
}
58+
59+
class ShiftArraySpec extends ChiselFlatSpec {
60+
61+
behavior of "ShiftArray"
62+
63+
it should "present a 3x3 array" in {
64+
Driver(() => new ShiftArray(3, 3, UInt(16.W)), "treadle") {
65+
dut => new ShiftArrayTester(dut)
66+
} should be (true)
67+
}
68+
69+
}

0 commit comments

Comments
 (0)