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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// SPDX-License-Identifier: Apache-2.0
package chisel3.util.experimental
import chisel3._
import chisel3.experimental.{annotate, ChiselAnnotation, RunFirrtlTransform}
import firrtl.{annoSeqToSeq, seqToAnnoSeq}
import firrtl.annotations._
import firrtl.ir.{Module => _, _}
import firrtl.transforms.BlackBoxInlineAnno
import firrtl.Mappers._
import firrtl.{AnnotationSeq, CircuitForm, CircuitState, EmitCircuitAnnotation, LowForm, Transform, VerilogEmitter}
import scala.collection.mutable
/** This is the annotation created when using [[loadMemoryFromFile]], it records the memory, the load file
* and the format of the file.
* @param target memory to load
* @param fileName name of input file
* @param hexOrBinary use \$readmemh or \$readmemb, i.e. hex or binary text input, default is hex
*/
case class ChiselLoadMemoryAnnotation[T <: Data](
target: MemBase[T],
fileName: String,
hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex)
extends ChiselAnnotation
with RunFirrtlTransform {
if (fileName.isEmpty) {
throw new Exception(
s"""LoadMemory from file annotations file empty file name"""
)
}
def transformClass: Class[LoadMemoryTransform] = classOf[LoadMemoryTransform]
def toFirrtl: LoadMemoryAnnotation = {
val tx = target.toNamed.asInstanceOf[ComponentName]
LoadMemoryAnnotation(tx, fileName, hexOrBinary, Some(tx.name))
}
}
/** [[loadMemoryFromFile]] is an annotation generator that helps with loading a memory from a text file as a bind module. This relies on
* Verilator and Verilog's `\$readmemh` or `\$readmemb`. The [[https://github.com/freechipsproject/treadle Treadle
* backend]] can also recognize this annotation and load memory at run-time.
*
* This annotation, when the FIRRTL compiler runs, triggers the [[LoadMemoryTransform]]. That will add Verilog
* directives to enable the specified memories to be initialized from files.
*
* ==Example module==
*
* Consider a simple Module containing a memory:
* {{{
* import chisel3._
* class UsesMem(memoryDepth: Int, memoryType: Data) extends Module {
* val io = IO(new Bundle {
* val address = Input(UInt(memoryType.getWidth.W))
* val value = Output(memoryType)
* })
* val memory = Mem(memoryDepth, memoryType)
* io.value := memory(io.address)
* }
* }}}
*
* ==Above module with annotation==
*
* To load this memory from the file `/workspace/workdir/mem1.hex.txt` just add an import and annotate the memory:
* {{{
* import chisel3._
* import chisel3.util.experimental.loadMemoryFromFile // <<-- new import here
* class UsesMem(memoryDepth: Int, memoryType: Data) extends Module {
* val io = IO(new Bundle {
* val address = Input(UInt(memoryType.getWidth.W))
* val value = Output(memoryType)
* })
* val memory = Mem(memoryDepth, memoryType)
* io.value := memory(io.address)
* loadMemoryFromFile(memory, "/workspace/workdir/mem1.hex.txt") // <<-- Note the annotation here
* }
* }}}
*
* ==Example file format==
*
* A memory file should consist of ASCII text in either hex or binary format. The following example shows such a
* file formatted to use hex:
* {{{
* 0
* 7
* d
* 15
* }}}
*
* A binary file can be similarly constructed.
*
* @see
* [[https://github.com/freechipsproject/chisel3/tree/master/src/test/scala/chiselTests/LoadMemoryFromFileSpec.scala
* LoadMemoryFromFileSpec.scala]] in the test suite for additional examples.
* @see Chisel3 Wiki entry on
* [[https://github.com/freechipsproject/chisel3/wiki/Chisel-Memories#loading-memories-in-simulation "Loading Memories
* in Simulation"]]
*/
object loadMemoryFromFile {
/** Annotate a memory such that it can be initialized using a file
* @param memory the memory
* @param filename the file used for initialization
* @param hexOrBinary whether the file uses a hex or binary number representation
*/
def apply[T <: Data](
memory: MemBase[T],
fileName: String,
hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex
): Unit = {
annotate(ChiselLoadMemoryAnnotation(memory, fileName, hexOrBinary))
}
}
/** [[loadMemoryFromFileInline]] is an annotation generator that helps with loading a memory from a text file inlined in
* the Verilog module. This relies on Verilator and Verilog's `\$readmemh` or `\$readmemb`.
* The [[https://github.com/freechipsproject/treadle Treadlebackend]] can also recognize this annotation and load memory at run-time.
*
* This annotation, when the FIRRTL compiler runs, triggers the [[MemoryFileInlineAnnotation]] that will add Verilog
* directives inlined to the module enabling the specified memories to be initialized from files.
* The module supports both `hex` and `bin` files by passing the appropriate [[MemoryLoadFileType.FileType]] argument with
* [[MemoryLoadFileType.Hex]] or [[MemoryLoadFileType.Binary]]. Hex is the default.
*
* ==Example module==
*
* Consider a simple Module containing a memory:
* {{{
* import chisel3._
* class UsesMem(memoryDepth: Int, memoryType: Data) extends Module {
* val io = IO(new Bundle {
* val address = Input(UInt(memoryType.getWidth.W))
* val value = Output(memoryType)
* })
* val memory = Mem(memoryDepth, memoryType)
* io.value := memory(io.address)
* }
* }}}
*
* ==Above module with annotation==
*
* To load this memory from the file `/workspace/workdir/mem1.hex.txt` just add an import and annotate the memory:
* {{{
* import chisel3._
* import chisel3.util.experimental.loadMemoryFromFileInline // <<-- new import here
* class UsesMem(memoryDepth: Int, memoryType: Data) extends Module {
* val io = IO(new Bundle {
* val address = Input(UInt(memoryType.getWidth.W))
* val value = Output(memoryType)
* })
* val memory = Mem(memoryDepth, memoryType)
* io.value := memory(io.address)
* loadMemoryFromFileInline(memory, "/workspace/workdir/mem1.hex.txt") // <<-- Note the annotation here
* }
* }}}
*
* ==Example file format==
*
* A memory file should consist of ASCII text in either hex or binary format. The following example shows such a
* file formatted to use hex:
* {{{
* 0
* 7
* d
* 15
* }}}
*
* A binary file can be similarly constructed.
* Chisel does not validate the file format or existence. It is supposed to be in a path accessible by the synthesis
* tool together with the generated Verilog.
*
* @see Chisel3 Wiki entry on
* [[https://github.com/freechipsproject/chisel3/wiki/Chisel-Memories#loading-memories-in-simulation "Loading Memories
* in Simulation"]]
*/
object loadMemoryFromFileInline {
/** Annotate a memory such that it can be initialized inline using a file
* @param memory the memory
* @param fileName the file used for initialization
* @param hexOrBinary whether the file uses a hex or binary number representation
*/
def apply[T <: Data](
memory: MemBase[T],
fileName: String,
hexOrBinary: MemoryLoadFileType.FileType = MemoryLoadFileType.Hex
): Unit = {
annotate(new ChiselAnnotation {
override def toFirrtl = MemoryFileInlineAnnotation(memory.toTarget, fileName, hexOrBinary)
})
}
}
/** This transform only is activated if Verilog is being generated (determined by presence of the proper emit
* annotation) when activated it creates additional Verilog files that contain modules bound to the modules that
* contain an initializable memory
*
* Currently the only non-Verilog based simulation that can support loading memory from a file is treadle but it does
* not need this transform to do that.
*/
class LoadMemoryTransform extends Transform {
def inputForm: CircuitForm = LowForm
def outputForm: CircuitForm = LowForm
private var memoryCounter: Int = -1
private val bindModules: mutable.ArrayBuffer[BlackBoxInlineAnno] = new mutable.ArrayBuffer()
private val verilogEmitter: VerilogEmitter = new VerilogEmitter
/** run the pass
* @param circuit the circuit
* @param annotations all the annotations
* @return
*/
def run(circuit: Circuit, annotations: AnnotationSeq): Circuit = {
val groups = annotations.collect { case m: LoadMemoryAnnotation => m }
.groupBy(_.target.serialize)
val memoryAnnotations = groups.map {
case (key, annos) =>
if (annos.size > 1) {
throw new Exception(
s"Multiple (${annos.size} found for memory $key one LoadMemoryAnnotation is allowed per memory"
)
}
key -> annos.head
}
val modulesByName = circuit.modules.collect { case module: firrtl.ir.Module => module.name -> module }.toMap
/* Walk the module and for memories that are annotated with [[LoadMemoryAnnotation]]s generate the bindable modules for
* Verilog emission.
* @param myModule module being searched for memories
*/
def processModule(myModule: DefModule): DefModule = {
def makePath(componentName: String): String = {
circuit.main + "." + myModule.name + "." + componentName
}
def processMemory(name: String): Unit = {
val fullMemoryName = makePath(s"$name")
memoryAnnotations.get(fullMemoryName) match {
case Some(lma @ LoadMemoryAnnotation(ComponentName(componentName, moduleName), _, hexOrBinary, _)) =>
val writer = new java.io.StringWriter
val readmem = hexOrBinary match {
case MemoryLoadFileType.Binary => "$readmemb"
case MemoryLoadFileType.Hex => "$readmemh"
}
modulesByName.get(moduleName.name).foreach { module =>
val renderer = verilogEmitter.getRenderer(module, modulesByName)(writer)
val loadFileName = lma.getFileName
memoryCounter += 1
val bindsToName = s"BindsTo_${memoryCounter}_${moduleName.name}"
renderer.emitVerilogBind(
bindsToName,
s"""
|initial begin
| $readmem("$loadFileName", ${myModule.name}.$componentName);
|end
""".stripMargin
)
val inLineText = writer.toString + "\n" +
s"""bind ${myModule.name} $bindsToName ${bindsToName}_Inst(.*);"""
val blackBoxInline = BlackBoxInlineAnno(
moduleName,
moduleName.serialize + "." + componentName + ".v",
inLineText
)
bindModules += blackBoxInline
}
case _ =>
}
}
def processStatements(statement: Statement): Statement = {
statement match {
case m: DefMemory => processMemory(m.name)
case s => s.map(processStatements)
}
statement
}
myModule match {
case module: firrtl.ir.Module =>
processStatements(module.body)
case _ =>
}
myModule
}
circuit.map(processModule)
}
def execute(state: CircuitState): CircuitState = {
val isVerilog = state.annotations.exists {
case EmitCircuitAnnotation(emitter) =>
emitter == classOf[VerilogEmitter]
case _ =>
false
}
if (isVerilog) {
run(state.circuit, state.annotations)
state.copy(annotations = state.annotations ++ bindModules)
} else {
state
}
}
}
|