forked from SingularityKChen/dl_accelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemCtrlSpecTest.scala
174 lines (168 loc) · 7.16 KB
/
MemCtrlSpecTest.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package dla.tests.diplomatictest
import chisel3._
import chisel3.tester._
import chisel3.tester.experimental.TestOptionBuilder._
import chisel3.util._
import chiseltest.internal.WriteVcdAnnotation
import dla.cluster.{ClusterConfig, ClusterSRAMConfig}
import dla.diplomatic.{EyerissIDMapGenerator, EyerissMemCtrlModule, EyerissMemCtrlParameters}
import org.scalatest._
import scala.util.Random
object MemCtrlDriver {
def pokeRespSourceId(reqList: List[Int], respList: List[Int], freeIO: DecoupledIO[UInt], theClock: Clock): List[Int] = {
var newRespList: List[Int] = Nil
theClock.step((new Random).nextInt(15))
if (reqList.isEmpty) {
println(s"[Info] reqList is empty now, reqList = $reqList")
} else {
val diffIdList = reqList.diff(respList)
if (diffIdList.isEmpty) {
println(Console.RED + s"[Error] there is no difference between reqList and respList" + Console.RESET)
} else {
val respId = diffIdList((new Random).nextInt(diffIdList.length))
freeIO.valid.poke(true.B)
freeIO.bits.poke(respId.U)
println(s"[free@${respList.length}] the response source id is $respId")
newRespList = respList:::List(respId)
}
}
theClock.step()
freeIO.valid.poke(false.B)
newRespList
}
}
object MemCtrlMonitor {
def peekReqSourceId(reqList: List[Int], allocIO: DecoupledIO[UInt], theClock: Clock): List[Int] = {
var newReqList: List[Int] = Nil
val reqNum = reqList.length
theClock.step((new Random).nextInt(10))
allocIO.ready.poke(true.B)
if (allocIO.valid.peek().litToBoolean) {
val reqId = allocIO.bits.peek().litValue().toInt
println(s"[alloc@$reqNum] the require source Id is $reqId")
newReqList = reqList:::List(reqId)
} else {
println(Console.RED + s"[Error] alloc is not valid @$reqNum" + Console.RESET)
}
theClock.step()
allocIO.ready.poke(false.B)
newReqList
}
}
class MemCtrlSpecTest extends FlatSpec with ChiselScalatestTester
with Matchers with ClusterConfig with ClusterSRAMConfig {
private val getSourceNum = inActRouterNum + weightRouterNum
private val driver = MemCtrlDriver
private val monitor = MemCtrlMonitor
private val decoderSequencer = DecoderSequencer
behavior of "test the spec of diplomatic memory controller"
it should "work well on MemCtrlModule" in {
implicit val p: EyerissMemCtrlParameters =
EyerissMemCtrlParameters(addressBits = 32, // TODO: check
inActSizeBits = 10, weightSizeBits = 10, pSumSizeBits = log2Ceil(pSumSRAMSize), // TODO: check
inActIds = inActRouterNum, weightIds = weightRouterNum, pSumIds = pSumRouterNum)
test(new EyerissMemCtrlModule()(p)).withAnnotations(Seq(WriteVcdAnnotation)) { theMemCtrl =>
val theTopIO = theMemCtrl.io
val theClock = theMemCtrl.clock
var inActReqFormerList: List[Int] = Nil
var inActRespFormerList: List[Int] = Nil
var inActReqLaterList: List[Int] = Nil
var inActRespLaterList: List[Int] = Nil
var weightReqList: List[Int] = Nil
var weightRespList: List[Int] = Nil
theMemCtrl.reset.poke(true.B)
theClock.step()
theMemCtrl.reset.poke(false.B)
println("----------------- test begin -----------------")
println("------------ generate id source --------------")
theTopIO.inActIO.startAdr.poke(decoderSequencer.inActAdr.hex.U)
theTopIO.inActIO.reqSize.poke(decoderSequencer.reqSize.inAct.U)
theTopIO.weightIO.startAdr.poke(decoderSequencer.weightAdr.hex.U)
theTopIO.weightIO.reqSize.poke(decoderSequencer.reqSize.weight.U)
theClock.step()
/** load GLB, req for inAct*/
fork {
while (inActReqFormerList.length < inActRouterNum) {
inActReqFormerList = monitor.peekReqSourceId(inActReqFormerList, theTopIO.inActIO.sourceAlloc, theClock)
}
println("------------ reqLater now --------------")
while (inActReqLaterList.length < inActRouterNum) {
inActReqLaterList = monitor.peekReqSourceId(inActReqLaterList, theTopIO.inActIO.sourceAlloc, theClock)
}
} .fork.withRegion(Monitor) {
fork {
while (inActRespFormerList.length < inActRouterNum) {
inActRespFormerList = driver.pokeRespSourceId(inActReqFormerList,
inActRespFormerList, theTopIO.inActIO.sourceFree, theClock)
}
while (inActRespLaterList.length < inActRouterNum) {
inActRespLaterList = driver.pokeRespSourceId(inActReqFormerList,
inActRespLaterList, theTopIO.inActIO.sourceFree, theClock)
}
} .fork {
while (inActRespLaterList.length < inActRouterNum) {
if (theTopIO.inActIO.sourceAlloc.ready.peek().litToBoolean &&
theTopIO.inActIO.sourceAlloc.valid.peek().litToBoolean)
{
println(s"inAct address = ${theTopIO.inActIO.address.peek().litValue()}")
theClock.step()
} else {
theClock.step()
}
}
}.join()
}.joinAndStep(theClock)
/** load pe, req for weight*/
theClock.step((new Random).nextInt(10))
fork {
while (weightReqList.length < weightRouterNum) {
weightReqList = monitor.peekReqSourceId(weightReqList, theTopIO.weightIO.sourceAlloc, theClock)
}
} .fork.withRegion(Monitor) {
fork {
while (weightRespList.length < weightRouterNum) {
weightRespList = driver.pokeRespSourceId(weightReqList, weightRespList, theTopIO.weightIO.sourceFree, theClock)
}
} .fork {
while (weightRespList.length < weightRouterNum) {
if (theTopIO.weightIO.sourceAlloc.ready.peek().litToBoolean &&
theTopIO.weightIO.sourceAlloc.valid.peek().litToBoolean)
{
println(s"weight address = ${theTopIO.weightIO.address.peek().litValue()}")
theClock.step()
} else {
theClock.step()
}
}
}.join()
}.joinAndStep(theClock)
}
}
it should "work well on EyerissIDMapGenerator" in {
test (new EyerissIDMapGenerator(getSourceNum)) { theIdMap =>
val theTopIO = theIdMap.io
val theClock = theIdMap.clock
val theAllocIO = theTopIO.alloc
val theFreeIO = theTopIO.free
var reqList: List[Int] = Nil
var respList: List[Int] = Nil
theIdMap.reset.poke(true.B)
theClock.step()
theIdMap.reset.poke(false.B)
println("----------------- test begin -----------------")
println("------------ generate id source --------------")
fork {
while (reqList.length < getSourceNum) {
reqList = monitor.peekReqSourceId(reqList, theAllocIO, theClock)
}
println(Console.YELLOW + "[Info] all sources have send requirements" + Console.RESET)
theAllocIO.valid.expect(false.B, "valid should be false as all have send req")
} .fork.withRegion(Monitor) {
while (!theTopIO.finish.peek().litToBoolean) {
respList = driver.pokeRespSourceId(reqList, respList, theFreeIO, theClock)
}
} .joinAndStep(theClock)
println(Console.GREEN + "[Success] all sources have send requirements and received response" + Console.RESET)
}
}
}