-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicMemorySearch.scala
43 lines (39 loc) · 1.16 KB
/
DynamicMemorySearch.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import chisel3._
import chisel3.util.log2Ceil
// Problem:
//
// This module should be able to write 'data' to
// internal memory at 'wrAddr' if 'isWr' is asserted.
//
// This module should perform sequential search of 'data'
// in internal memory if 'en' was asserted at least 1 clock cycle
//
// While searching 'done' should remain 0,
// 'done' should be asserted when search is complete
//
// If 'data' has been found 'target' should be updated to the
// address of the first occurrence
//
class DynamicMemorySearch(val n: Int, val w: Int) extends Module {
val io = IO(new Bundle {
val isWr = Input(Bool())
val wrAddr = Input(UInt(log2Ceil(n).W))
val data = Input(UInt(w.W))
val en = Input(Bool())
val target = Output(UInt(log2Ceil(n).W))
val done = Output(Bool())
})
val index = RegInit(0.U(log2Ceil(n).W))
val list = Mem(n, UInt(w.W))
val memVal = list(index)
val done = !io.en && ((memVal === io.data) || (index === (n-1).asUInt))
when (io.isWr) {
list(io.wrAddr) := io.data
} .elsewhen (io.en) {
index := 0.U
} .elsewhen (done === false.B) {
index := index + 1.U
}
io.done := done
io.target := index
}