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
|
// See LICENSE for license details.
package chisel3.core
import scala.language.experimental.macros
import chisel3.internal._
import chisel3.internal.Builder.pushCommand
import chisel3.internal.firrtl._
import chisel3.internal.sourceinfo.{SourceInfo}
object Reg {
/** Creates a register without initialization (reset is ignored). Value does
* not change unless assigned to (using the := operator).
*
* @param t: data type for the register
*/
def apply[T <: Data](t: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
if (compileOptions.declaredTypeMustBeUnbound) {
Binding.checkUnbound(t, s"t ($t) must be unbound Type. Try using cloneType?")
}
val reg = t.chiselCloneType
val clock = Node(Builder.forcedClock)
Binding.bind(reg, RegBinder(Builder.forcedModule), "Error: t")
pushCommand(DefReg(sourceInfo, reg, clock))
reg
}
}
object RegNext {
/** Returns a register with the specified next and no reset initialization.
*
* Essentially a 1-cycle delayed version of the input signal.
*/
def apply[T <: Data](next: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
val model = (next match {
case next: Bits => next.cloneTypeWidth(Width())
case next => next.chiselCloneType
}).asInstanceOf[T]
val reg = Reg(model)
Binding.checkSynthesizable(next, s"'next' ($next)") // TODO: move into connect?
reg := next
reg
}
/** Returns a register with the specified next and reset initialization.
*
* Essentially a 1-cycle delayed version of the input signal.
*/
def apply[T <: Data](next: T, init: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
val model = (next match {
case next: Bits => next.cloneTypeWidth(Width())
case next => next.chiselCloneType
}).asInstanceOf[T]
val reg = RegInit(model, init) // TODO: this makes NO sense
Binding.checkSynthesizable(next, s"'next' ($next)") // TODO: move into connect?
reg := next
reg
}
}
object RegInit {
/** Returns a register pre-initialized (on reset) to the specified value.
* Register type is inferred from the initializer.
*/
def apply[T <: Data](init: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
val model = (init.litArg match {
// For e.g. Reg(init=UInt(0, k)), fix the Reg's width to k
case Some(lit) if lit.forcedWidth => init.chiselCloneType
case _ => init match {
case init: Bits => init.cloneTypeWidth(Width())
case init => init.chiselCloneType
}
}).asInstanceOf[T]
RegInit(model, init)
}
/** Creates a register given an explicit type and an initialization (reset) value.
*/
def apply[T <: Data](t: T, init: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
if (compileOptions.declaredTypeMustBeUnbound) {
Binding.checkUnbound(t, s"t ($t) must be unbound Type. Try using cloneType?")
}
val reg = t.chiselCloneType
val clock = Node(Builder.forcedClock)
val reset = Node(Builder.forcedReset)
Binding.bind(reg, RegBinder(Builder.forcedModule), "Error: t")
Binding.checkSynthesizable(init, s"'init' ($init)")
pushCommand(DefRegInit(sourceInfo, reg, clock, reset, init.ref))
reg
}
}
|