import migen in litex/gen
[litex.git] / litex / gen / doc / fhdl.rst
1 The FHDL domain-specific language
2 #################################
3
4 The Fragmented Hardware Description Language (FHDL) is the basis of Migen. It consists of a formal system to describe signals, and combinatorial and synchronous statements operating on them. The formal system itself is low level and close to the synthesizable subset of Verilog, and we then rely on Python algorithms to build complex structures by combining FHDL elements.
5 The FHDL module also contains a back-end to produce synthesizable Verilog, and some structure analysis and manipulation functionality.
6
7 FHDL differs from MyHDL [myhdl]_ in fundamental ways. MyHDL follows the event-driven paradigm of traditional HDLs (see :ref:`background`) while FHDL separates the code into combinatorial statements, synchronous statements, and reset values. In MyHDL, the logic is described directly in the Python AST. The converter to Verilog or VHDL then examines the Python AST and recognizes a subset of Python that it translates into V*HDL statements. This seriously impedes the capability of MyHDL to generate logic procedurally. With FHDL, you manipulate a custom AST from Python, and you can more easily design algorithms that operate on it.
8
9 .. [myhdl] http://www.myhdl.org
10
11 FHDL is made of several elements, which are briefly explained below. They all can be imported directly from the ``migen`` module.
12
13 Expressions
14 ***********
15
16 Constants
17 =========
18
19 The ``Constant`` object represents a constant, HDL-literal integer. It behaves like specifying integers and booleans but also supports slicing and can have a bit width or signedness different from what is implied by the value it represents.
20
21 ``True`` and ``False`` are interpreted as 1 and 0, respectively.
22
23 Negative integers are explicitly supported. As with MyHDL [countin]_, arithmetic operations return the natural results.
24
25 To lighten the syntax, assignments and operators automatically wrap Python integers and booleans into ``Constant``. Additionally, ``Constant`` is aliased to ``C``. The following are valid Migen statements: ``a.eq(0)``, ``a.eq(a + 1)``, ``a.eq(C(42)[0:1])``.
26
27 .. [countin] http://www.jandecaluwe.com/hdldesign/counting.html
28
29 Signal
30 ======
31
32 The signal object represents a value that is expected to change in the circuit. It does exactly what Verilog's "wire" and "reg" and VHDL's "signal" do.
33
34 The main point of the signal object is that it is identified by its Python ID (as returned by the :py:func:`id` function), and nothing else. It is the responsibility of the V*HDL back-end to establish an injective mapping between Python IDs and the V*HDL namespace. It should perform name mangling to ensure this. The consequence of this is that signal objects can safely become members of arbitrary Python classes, or be passed as parameters to functions or methods that generate logic involving them.
35
36 The properties of a signal object are:
37
38 * An integer or a (integer, boolean) pair that defines the number of bits and whether the bit of higher index of the signal is a sign bit (i.e. the signal is signed). The defaults are one bit and unsigned. Alternatively, the ``min`` and ``max`` parameters can be specified to define the range of the signal and determine its bit width and signedness. As with Python ranges, ``min`` is inclusive and defaults to 0, ``max`` is exclusive and defaults to 2.
39 * A name, used as a hint for the V*HDL back-end name mangler.
40 * The signal's reset value. It must be an integer, and defaults to 0. When the signal's value is modified with a synchronous statement, the reset value is the initialization value of the associated register. When the signal is assigned to in a conditional combinatorial statement (``If`` or ``Case``), the reset value is the value that the signal has when no condition that causes the signal to be driven is verified. This enforces the absence of latches in designs. If the signal is permanently driven using a combinatorial statement, the reset value has no effect.
41
42 The sole purpose of the name property is to make the generated V*HDL code easier to understand and debug. From a purely functional point of view, it is perfectly OK to have several signals with the same name property. The back-end will generate a unique name for each object. If no name property is specified, Migen will analyze the code that created the signal object, and try to extract the variable or member name from there. For example, the following statements will create one or several signals named "bar": ::
43
44 bar = Signal()
45 self.bar = Signal()
46 self.baz.bar = Signal()
47 bar = [Signal() for x in range(42)]
48
49 In case of conflicts, Migen tries first to resolve the situation by prefixing the identifiers with names from the class and module hierarchy that created them. If the conflict persists (which can be the case if two signal objects are created with the same name in the same context), it will ultimately add number suffixes.
50
51 Operators
52 =========
53
54 Operators are represented by the ``_Operator`` object, which generally should not be used directly. Instead, most FHDL objects overload the usual Python logic and arithmetic operators, which allows a much lighter syntax to be used. For example, the expression: ::
55
56 a * b + c
57
58 is equivalent to::
59
60 _Operator("+", [_Operator("*", [a, b]), c])
61
62 Slices
63 ======
64
65 Likewise, slices are represented by the ``_Slice`` object, which often should not be used in favor of the Python slice operation [x:y]. Implicit indices using the forms [x], [x:] and [:y] are supported. Beware! Slices work like Python slices, not like VHDL or Verilog slices. The first bound is the index of the LSB and is inclusive. The second bound is the index of MSB and is exclusive. In V*HDL, bounds are MSB:LSB and both are inclusive.
66
67 Concatenations
68 ==============
69
70 Concatenations are done using the ``Cat`` object. To make the syntax lighter, its constructor takes a variable number of arguments, which are the signals to be concatenated together (you can use the Python "*" operator to pass a list instead).
71 To be consistent with slices, the first signal is connected to the bits with the lowest indices in the result. This is the opposite of the way the "{}" construct works in Verilog.
72
73 Replications
74 ============
75
76 The ``Replicate`` object represents the equivalent of {count{expression}} in Verilog.
77
78 Statements
79 **********
80
81 Assignment
82 ==========
83
84 Assignments are represented with the ``_Assign`` object. Since using it directly would result in a cluttered syntax, the preferred technique for assignments is to use the ``eq()`` method provided by objects that can have a value assigned to them. They are signals, and their combinations with the slice and concatenation operators.
85 As an example, the statement: ::
86
87 a[0].eq(b)
88
89 is equivalent to: ::
90
91 _Assign(_Slice(a, 0, 1), b)
92
93 If
94 ==
95
96 The ``If`` object takes a first parameter which must be an expression (combination of the ``Constant``, ``Signal``, ``_Operator``, ``_Slice``, etc. objects) representing the condition, then a variable number of parameters representing the statements (``_Assign``, ``If``, ``Case``, etc. objects) to be executed when the condition is verified.
97
98 The ``If`` object defines a ``Else()`` method, which when called defines the statements to be executed when the condition is not true. Those statements are passed as parameters to the variadic method.
99
100 For convenience, there is also a ``Elif()`` method.
101
102 Example: ::
103
104 If(tx_count16 == 0,
105 tx_bitcount.eq(tx_bitcount + 1),
106 If(tx_bitcount == 8,
107 self.tx.eq(1)
108 ).Elif(tx_bitcount == 9,
109 self.tx.eq(1),
110 tx_busy.eq(0)
111 ).Else(
112 self.tx.eq(tx_reg[0]),
113 tx_reg.eq(Cat(tx_reg[1:], 0))
114 )
115 )
116
117 Case
118 ====
119
120 The ``Case`` object constructor takes as first parameter the expression to be tested, and a dictionary whose keys are the values to be matched, and values the statements to be executed in the case of a match. The special value ``"default"`` can be used as match value, which means the statements should be executed whenever there is no other match.
121
122 Arrays
123 ======
124
125 The ``Array`` object represents lists of other objects that can be indexed by FHDL expressions. It is explicitly possible to:
126
127 * nest ``Array`` objects to create multidimensional tables.
128 * list any Python object in a ``Array`` as long as every expression appearing in a module ultimately evaluates to a ``Signal`` for all possible values of the indices. This allows the creation of lists of structured data.
129 * use expressions involving ``Array`` objects in both directions (assignment and reading).
130
131 For example, this creates a 4x4 matrix of 1-bit signals: ::
132
133 my_2d_array = Array(Array(Signal() for a in range(4)) for b in range(4))
134
135 You can then read the matrix with (``x`` and ``y`` being 2-bit signals): ::
136
137 out.eq(my_2d_array[x][y])
138
139 and write it with: ::
140
141 my_2d_array[x][y].eq(inp)
142
143 Since they have no direct equivalent in Verilog, ``Array`` objects are lowered into multiplexers and conditional statements before the actual conversion takes place. Such lowering happens automatically without any user intervention.
144
145 Specials
146 ********
147
148 Tri-state I/O
149 =============
150
151 A triplet (O, OE, I) of one-way signals defining a tri-state I/O port is represented by the ``TSTriple`` object. Such objects are only containers for signals that are intended to be later connected to a tri-state I/O buffer, and cannot be used as module specials. Such objects, however, should be kept in the design as long as possible as they allow the individual one-way signals to be manipulated in a non-ambiguous way.
152
153 The object that can be used in as a module special is ``Tristate``, and it behaves exactly like an instance of a tri-state I/O buffer that would be defined as follows: ::
154
155 Instance("Tristate",
156 io_target=target,
157 i_o=o,
158 i_oe=oe,
159 o_i=i
160 )
161
162 Signals ``target``, ``o`` and ``i`` can have any width, while ``oe`` is 1-bit wide. The ``target`` signal should go to a port and not be used elsewhere in the design. Like modern FPGA architectures, Migen does not support internal tri-states.
163
164 A ``Tristate`` object can be created from a ``TSTriple`` object by calling the ``get_tristate`` method.
165
166 By default, Migen emits technology-independent behavioral code for a tri-state buffer. If a specific code is needed, the tristate handler can be overriden using the appropriate parameter of the V*HDL conversion function.
167
168 Instances
169 =========
170
171 Instance objects represent the parametrized instantiation of a V*HDL module, and the connection of its ports to FHDL signals. They are useful in a number of cases:
172
173 * Reusing legacy or third-party V*HDL code.
174 * Using special FPGA features (DCM, ICAP, ...).
175 * Implementing logic that cannot be expressed with FHDL (e.g. latches).
176 * Breaking down a Migen system into multiple sub-systems.
177
178 The instance object constructor takes the type (i.e. name of the instantiated module) of the instance, then multiple parameters describing how to connect and parametrize the instance.
179
180 These parameters can be:
181
182 * ``Instance.Input``, ``Instance.Output`` or ``Instance.InOut`` to describe signal connections with the instance. The parameters are the name of the port at the instance, and the FHDL expression it should be connected to.
183 * ``Instance.Parameter`` sets a parameter (with a name and value) of the instance.
184 * ``Instance.ClockPort`` and ``Instance.ResetPort`` are used to connect clock and reset signals to the instance. The only mandatory parameter is the name of the port at the instance. Optionally, a clock domain name can be specified, and the ``invert`` option can be used to interface to those modules that require a 180-degree clock or a active-low reset.
185
186 Memories
187 ========
188
189 Memories (on-chip SRAM) are supported using a mechanism similar to instances.
190
191 A memory object has the following parameters:
192
193 * The width, which is the number of bits in each word.
194 * The depth, which represents the number of words in the memory.
195 * An optional list of integers used to initialize the memory.
196
197 To access the memory in hardware, ports can be obtained by calling the ``get_port`` method. A port always has an address signal ``a`` and a data read signal ``dat_r``. Other signals may be available depending on the port's configuration.
198
199 Options to ``get_port`` are:
200
201 * ``write_capable`` (default: ``False``): if the port can be used to write to the memory. This creates an additional ``we`` signal.
202 * ``async_read`` (default: ``False``): whether reads are asychronous (combinatorial) or synchronous (registered).
203 * ``has_re`` (default: ``False``): adds a read clock-enable signal ``re`` (ignored for asychronous ports).
204 * ``we_granularity`` (default: ``0``): if non-zero, writes of less than a memory word can occur. The width of the ``we`` signal is increased to act as a selection signal for the sub-words.
205 * ``mode`` (default: ``WRITE_FIRST``, ignored for aynchronous ports). It can be:
206
207 * ``READ_FIRST``: during a write, the previous value is read.
208 * ``WRITE_FIRST``: the written value is returned.
209 * ``NO_CHANGE``: the data read signal keeps its previous value on a write.
210
211 * ``clock_domain`` (default: ``"sys"``): the clock domain used for reading and writing from this port.
212
213 Migen generates behavioural V*HDL code that should be compatible with all simulators and, if the number of ports is <= 2, most FPGA synthesizers. If a specific code is needed, the memory handler can be overriden using the appropriate parameter of the V*HDL conversion function.
214
215 Inline synthesis directives
216 ===========================
217
218 Inline synthesis directives (pseudo-comments such as ``// synthesis attribute keep of clock_signal_name is true``) are supported using the ``SynthesisDirective`` object. Its constructor takes as parameters a string containing the body of the directive, and optional keyword parameters that are used to replace signal names similarly to the Python string method ``format``. The above example could be represented as follows: ::
219
220 SynthesisDirective("attribute keep of {clksig} is true", clksig=clock_domain.clk)
221
222 Modules
223 *******
224
225 Modules play the same role as Verilog modules and VHDL entities. Similarly, they are organized in a tree structure. A FHDL module is a Python object that derives from the ``Module`` class. This class defines special attributes to be used by derived classes to describe their logic. They are explained below.
226
227 Combinatorial statements
228 ========================
229
230 A combinatorial statement is a statement that is executed whenever one of its inputs changes.
231
232 Combinatorial statements are added to a module by using the ``comb`` special attribute. Like most module special attributes, it must be accessed using the ``+=`` incrementation operator, and either a single statement, a tuple of statements or a list of statements can appear on the right hand side.
233
234 For example, the module below implements a OR gate: ::
235
236 class ORGate(Module):
237 def __init__(self):
238 self.a = Signal()
239 self.b = Signal()
240 self.x = Signal()
241
242 ###
243
244 self.comb += x.eq(a | b)
245
246 To improve code readability, it is recommended to place the interface of the module at the beginning of the ``__init__`` function, and separate it from the implementation using three hash signs.
247
248 Synchronous statements
249 ======================
250
251 A synchronous statements is a statement that is executed at each edge of some clock signal.
252
253 They are added to a module by using the ``sync`` special attribute, which has the same properties as the ``comb`` attribute.
254
255 The ``sync`` special attribute also has sub-attributes that correspond to abstract clock domains. For example, to add a statement to the clock domain named ``foo``, one would write ``self.sync.foo += statement``. The default clock domain is ``sys`` and writing ``self.sync += statement`` is equivalent to writing ``self.sync.sys += statement``.
256
257 Submodules and specials
258 =======================
259
260 Submodules and specials can be added by using the ``submodules`` and ``specials`` attributes respectively. This can be done in two ways:
261
262 #. anonymously, by using the ``+=`` operator on the special attribute directly, e.g. ``self.submodules += some_other_module``. Like with the ``comb`` and ``sync`` attributes, a single module/special or a tuple or list can be specified.
263 #. by naming the submodule/special using a subattribute of the ``submodules`` or ``specials`` attribute, e.g. ``self.submodules.foo = module_foo``. The submodule/special is then accessible as an attribute of the object, e.g. ``self.foo`` (and not ``self.submodules.foo``). Only one submodule/special can be added at a time using this form.
264
265 Clock domains
266 =============
267
268 Specifying the implementation of a clock domain is done using the ``ClockDomain`` object. It contains the name of the clock domain, a clock signal that can be driven like any other signal in the design (for example, using a PLL instance), and optionally a reset signal. Clock domains without a reset signal are reset using e.g. ``initial`` statements in Verilog, which in many FPGA families initalize the registers during configuration.
269
270 The name can be omitted if it can be extracted from the variable name. When using this automatic naming feature, prefixes ``_``, ``cd_`` and ``_cd_`` are removed.
271
272 Clock domains are then added to a module using the ``clock_domains`` special attribute, which behaves exactly like ``submodules`` and ``specials``.
273
274 Summary of special attributes
275 =============================
276
277 .. table::
278
279 +--------------------------------------------+--------------------------------------------------------------+
280 | Syntax | Action |
281 +============================================+==============================================================+
282 | self.comb += stmt | Add combinatorial statement to current module. |
283 +--------------------------------------------+--------------------------------------------------------------+
284 | self.comb += stmtA, stmtB | Add combinatorial statements A and B to current module. |
285 | | |
286 | self.comb += [stmtA, stmtB] | |
287 +--------------------------------------------+--------------------------------------------------------------+
288 | self.sync += stmt | Add synchronous statement to current module, in default |
289 | | clock domain sys. |
290 +--------------------------------------------+--------------------------------------------------------------+
291 | self.sync.foo += stmt | Add synchronous statement to current module, in clock domain |
292 | | foo. |
293 +--------------------------------------------+--------------------------------------------------------------+
294 | self.sync.foo += stmtA, stmtB | Add synchronous statements A and B to current module, in |
295 | | clock domain foo. |
296 | self.sync.foo += [stmtA, stmtB] | |
297 +--------------------------------------------+--------------------------------------------------------------+
298 | self.submodules += mod | Add anonymous submodule to current module. |
299 +--------------------------------------------+--------------------------------------------------------------+
300 | self.submodules += modA, modB | Add anonymous submodules A and B to current module. |
301 | | |
302 | self.submodules += [modA, modB] | |
303 +--------------------------------------------+--------------------------------------------------------------+
304 | self.submodules.bar = mod | Add submodule named bar to current module. The submodule can |
305 | | then be accessed using self.bar. |
306 +--------------------------------------------+--------------------------------------------------------------+
307 | self.specials += spe | Add anonymous special to current module. |
308 +--------------------------------------------+--------------------------------------------------------------+
309 | self.specials += speA, speB | Add anonymous specials A and B to current module. |
310 | | |
311 | self.specials += [speA, speB] | |
312 +--------------------------------------------+--------------------------------------------------------------+
313 | self.specials.bar = spe | Add special named bar to current module. The special can |
314 | | then be accessed using self.bar. |
315 +--------------------------------------------+--------------------------------------------------------------+
316 | self.clock_domains += cd | Add clock domain to current module. |
317 +--------------------------------------------+--------------------------------------------------------------+
318 | self.clock_domains += cdA, cdB | Add clock domains A and B to current module. |
319 | | |
320 | self.clock_domains += [cdA, cdB] | |
321 +--------------------------------------------+--------------------------------------------------------------+
322 | self.clock_domains.pix = ClockDomain() | Create and add clock domain pix to current module. The clock |
323 | | domain name is pix in all cases. It can be accessed using |
324 | self.clock_domains._pix = ClockDomain() | self.pix, self._pix, self.cd_pix and self._cd_pix, |
325 | | respectively. |
326 | self.clock_domains.cd_pix = ClockDomain() | |
327 | | |
328 | self.clock_domains._cd_pix = ClockDomain() | |
329 +--------------------------------------------+--------------------------------------------------------------+
330
331 Clock domain management
332 =======================
333
334 When a module has named submodules that define one or several clock domains with the same name, those clock domain names are prefixed with the name of each submodule plus an underscore.
335
336 An example use case of this feature is a system with two independent video outputs. Each video output module is made of a clock generator module that defines a clock domain ``pix`` and drives the clock signal, plus a driver module that has synchronous statements and other elements in clock domain ``pix``. The designer of the video output module can simply use the clock domain name ``pix`` in that module. In the top-level system module, the video output submodules are named ``video0`` and ``video1``. Migen then automatically renames the ``pix`` clock domain of each module to ``video0_pix`` and ``video1_pix``. Note that happens only because the clock domain is defined (using ClockDomain objects), not simply referenced (using e.g. synchronous statements) in the video output modules.
337
338 Clock domain name overlap is an error condition when any of the submodules that defines the clock domains is anonymous.
339
340 Finalization mechanism
341 ======================
342
343 Sometimes, it is desirable that some of a module logic be created only after the user has finished manipulating that module. For example, the FSM module supports that states be defined dynamically, and the width of the state signal can be known only after all states have been added. One solution is to declare the final number of states in the FSM constructor, but this is not user-friendly. A better solution is to automatically create the state signal just before the FSM module is converted to V*HDL. Migen supports this using the so-called finalization mechanism.
344
345 Modules can overload a ``do_finalize`` method that can create logic and is called using the algorithm below:
346
347 #. Finalization of the current module begins.
348 #. If the module has already been finalized (e.g. manually), the procedure stops here.
349 #. Submodules of the current module are recursively finalized.
350 #. ``do_finalize`` is called for the current module.
351 #. Any new submodules created by the current module's ``do_finalize`` are recursively finalized.
352
353 Finalization is automatically invoked at V*HDL conversion and at simulation. It can be manually invoked for any module by calling its ``finalize`` method.
354
355 The clock domain management mechanism explained above happens during finalization.
356
357 Conversion for synthesis
358 ************************
359
360 Any FHDL module can be converted into synthesizable Verilog HDL. This is accomplished by using the ``convert`` function in the ``verilog`` module.
361
362 The ``migen.build`` component provides scripts to interface third-party FPGA tools (from Xilinx, Altera and Lattice) to Migen, and a database of boards for the easy deployment of designs.