Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor

Showing 2 changed files with 143 additions and 26 deletions. Show diff stats Hide diff stats

  1. 2  doc/bus.rst
  2. 167  doc/fhdl.rst
2  doc/bus.rst
Source Rendered
@@ -52,6 +52,8 @@ The lowest-level description of a register is provided by the ``CSR`` class, whi
52 52
 * ``re``, which is the strobe signal for ``r``. It is active for one cycle, after or during a write from the bus. ``r`` is only valid when ``re`` is high.
53 53
 * ``w``, which must provide at all times the value to be read from the bus.
54 54
 
  55
+Names of CSRs can be omitted if they can be extracted from the variable name. When using this automatic naming feature, prefixes ``_``, ``r_`` and ``_r_`` are removed.
  56
+
55 57
 Compound CSRs (which are transformed into ``CSR`` plus additional logic for implementation) provide additional features optimized for common applications.
56 58
 
57 59
 The ``CSRStatus`` class is meant to be used as a status register that is read-only from the CPU. The user design is expected to drive its ``status`` signal. The advantage of using ``CSRStatus`` instead of using ``CSR`` and driving ``w`` is that the width of ``CSRStatus`` can be arbitrary. Status registers larger than the bus word width are automatically broken down into several ``CSR`` registers to span several addresses. Be careful that the atomicity of reads is not guaranteed.
167  doc/fhdl.rst
Source Rendered
... ...
@@ -1,16 +1,14 @@
1 1
 The FHDL layer
2 2
 ##############
3 3
 
4  
-The Fragmented Hardware Description Language (FHDL) is the lowest layer 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 and encapsulating them in "fragments".
5  
-The FHDL module also contains a back-end to produce synthesizable Verilog, and some structure analysis and manipulation functionality. A VHDL back-end [vhdlbe]_ is in development.
6  
-
7  
-.. [vhdlbe] https://github.com/peteut/migen
  4
+The Fragmented Hardware Description Language (FHDL) is the lowest layer 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.
8 6
 
9 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.
10 8
 
11 9
 .. [myhdl] http://www.myhdl.org
12 10
 
13  
-FHDL is made of several elements, which are briefly explained below.
  11
+FHDL is made of several elements, which are briefly explained below. They all can be imported from the ``migen.fhdl.std`` module.
14 12
 
15 13
 Expressions
16 14
 ***********
@@ -26,7 +24,7 @@ Negative integers are explicitly supported. As with MyHDL [countin]_, arithmetic
26 24
 
27 25
 Signal
28 26
 ======
29  
-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" and "variable" do.
  27
+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.
30 28
 
31 29
 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.
32 30
 
@@ -34,7 +32,6 @@ The properties of a signal object are:
34 32
 
35 33
 * 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.
36 34
 * A name, used as a hint for the V*HDL back-end name mangler.
37  
-* A boolean "variable". If true, the signal will behave like a VHDL variable, or a Verilog reg that uses blocking assignment. This parameter only has an effect when the signal's value is modified in a synchronous statement.
38 35
 * 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.
39 36
   
40 37
 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": ::
@@ -206,35 +203,153 @@ Inline synthesis directives (pseudo-comments such as ``// synthesis attribute ke
206 203
 
207 204
   SynthesisDirective("attribute keep of {clksig} is true", clksig=clock_domain.clk)
208 205
 
209  
-Fragments
210  
-*********
211  
-A "fragment" is a unit of logic, which is composed of:
  206
+Modules
  207
+*******
  208
+
  209
+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.
  210
+
  211
+Combinatorial statements
  212
+========================
  213
+
  214
+A combinatorial statement is a statement that is executed whenever one of its inputs changes.
  215
+
  216
+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.
  217
+
  218
+For example, the module below implements a OR gate: ::
  219
+
  220
+  class ORGate(Module):
  221
+    def __init__(self):
  222
+      self.a = Signal()
  223
+      self.b = Signal()
  224
+      self.x = Signal()
  225
+
  226
+      ###
  227
+
  228
+      self.comb += x.eq(a | b)
  229
+
  230
+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.
  231
+
  232
+Synchronous statements
  233
+======================
212 234
 
213  
-* A list of combinatorial statements.
214  
-* A list of synchronous statements, or a clock domain name -> synchronous statements dictionary.
215  
-* A set of specials (memories, instances, etc.)
216  
-* A list of simulation functions (see :ref:`simulating`).
  235
+A synchronous statements is a statement that is executed at each edge of some clock signal.
217 236
 
218  
-Fragments can reference arbitrary signals, including signals that are referenced in other fragments. Fragments can be combined using the "+" operator, which returns a new fragment containing the concatenation of each matched pair of lists.
  237
+They are added to a module by using the ``sync`` special attribute, which has the same properties as the ``comb`` attribute.
219 238
 
220  
-Fragments can be passed to the back-end for conversion to Verilog.
  239
+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``.
221 240
 
222  
-Specials are using a set, not a list, to facilitate sharing of certain elements by different components of a design. For example, one component may use a static memory buffer that another component would map onto a bus, using another port to that memory. Both components should reference the memory object in their fragments, but that memory should appear only once in the final design. The Python ``set`` provides this functionality.
  241
+Submodules and specials
  242
+=======================
223 243
 
224  
-By convention, classes that generate logic implement a method called ``get_fragment``. When called, this method builds a new fragment implementing the desired functionality of the class, and returns it. This convention allows fragments to be built automatically by combining the fragments from all relevant objects in the local scope, by using the autofragment module.
  244
+Submodules and specials can be added by using the ``submodules`` and ``specials`` attributes respectively. This can be done in two ways:
  245
+
  246
+#. 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.
  247
+#. 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.
  248
+
  249
+Clock domains
  250
+=============
  251
+
  252
+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.
  253
+
  254
+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.
  255
+
  256
+Clock domains are then added to a module using the ``clock_domains`` special attribute, which behaves exactly like ``submodules`` and ``specials``.
  257
+
  258
+Summary of special attributes
  259
+=============================
  260
+
  261
+.. table:: Summary of special attributes
  262
+
  263
++--------------------------------------------+--------------------------------------------------------------+
  264
+| Syntax                                     | Action                                                       |
  265
++============================================+==============================================================+
  266
+| self.comb += stmt                          | Add combinatorial statement to current module.               |
  267
++--------------------------------------------+--------------------------------------------------------------+
  268
+| self.comb += stmtA, stmtB                  | Add combinatorial statements A and B to current module.      |
  269
+|                                            |                                                              |
  270
+| self.comb += [stmtA, stmtB]                |                                                              |
  271
++--------------------------------------------+--------------------------------------------------------------+
  272
+| self.sync += stmt                          | Add synchronous statement to current module, in default      |
  273
+|                                            | clock domain sys.                                            |
  274
++--------------------------------------------+--------------------------------------------------------------+
  275
+| self.sync.foo += stmt                      | Add synchronous statement to current module, in clock domain |
  276
+|                                            | foo.                                                         |
  277
++--------------------------------------------+--------------------------------------------------------------+
  278
+| self.sync.foo += stmtA, stmtB              | Add synchronous statements A and B to current module, in     |
  279
+|                                            | clock domain foo.                                            |
  280
+| self.sync.foo += [stmtA, stmtB]            |                                                              |
  281
++--------------------------------------------+--------------------------------------------------------------+
  282
+| self.submodules += mod                     | Add anonymous submodule to current module.                   |
  283
++--------------------------------------------+--------------------------------------------------------------+
  284
+| self.submodules += modA, modB              | Add anonymous submodules A and B to current module.          |
  285
+|                                            |                                                              |
  286
+| self.submodules += [modA, modB]            |                                                              |
  287
++--------------------------------------------+--------------------------------------------------------------+
  288
+| self.submodules.bar = mod                  | Add submodule named bar to current module. The submodule can |
  289
+|                                            | then be accessed using self.bar.                             |
  290
++--------------------------------------------+--------------------------------------------------------------+
  291
+| self.specials += spe                       | Add anonymous special to current module.                     |
  292
++--------------------------------------------+--------------------------------------------------------------+
  293
+| self.specials += speA, speB                | Add anonymous specials A and B to current module.            |
  294
+|                                            |                                                              |
  295
+| self.specials += [speA, speB]              |                                                              |
  296
++--------------------------------------------+--------------------------------------------------------------+
  297
+| self.specials.bar = spe                    | Add special named bar to current module. The special can     |
  298
+|                                            | then be accessed using self.bar.                             |
  299
++--------------------------------------------+--------------------------------------------------------------+
  300
+| self.clock_domains += cd                   | Add clock domain to current module.                          |
  301
++--------------------------------------------+--------------------------------------------------------------+
  302
+| self.clock_domains += cdA, cdB             | Add clock domains A and B to current module.                 |
  303
+|                                            |                                                              |
  304
+| self.clock_domains += [cdA, cdB]           |                                                              |
  305
++--------------------------------------------+--------------------------------------------------------------+
  306
+| self.clock_domains.pix = ClockDomain()     | Create and add clock domain pix to current module. The clock |
  307
+|                                            | domain name is pix in all cases. It can be accessed using    |
  308
+| self.clock_domains._pix = ClockDomain()    | self.pix, self._pix, self.cd_pix and self._cd_pix,           |
  309
+|                                            | respectively.                                                |
  310
+| self.clock_domains.cd_pix = ClockDomain()  |                                                              |
  311
+|                                            |                                                              |
  312
+| self.clock_domains._cd_pix = ClockDomain() |                                                              |
  313
++--------------------------------------------+--------------------------------------------------------------+
  314
+
  315
+Clock domain management
  316
+=======================
  317
+
  318
+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.
  319
+
  320
+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.
  321
+
  322
+Clock domain name overlap is an error condition when any of the submodules that defines the clock domains is anonymous.
  323
+
  324
+Finalization mechanism
  325
+======================
  326
+
  327
+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.
  328
+
  329
+Modules can overload a ``do_finalize`` method that can create logic and is called using the algorithm below:
  330
+
  331
+#. Finalization of the current module begins.
  332
+#. If the module has already been finalized (e.g. manually), the procedure stops here.
  333
+#. Submodules of the current module are recursively finalized.
  334
+#. ``do_finalize`` is called for the current module.
  335
+#. Any new submodules created by the current module's ``do_finalize`` are recursively finalized.
  336
+
  337
+Finalization is automatically invoked at V*HDL conversion and at simulation. It can be manually invoked for any module by calling its ``finalize`` method.
  338
+
  339
+The clock domain management mechanism explained above happens during finalization.
  340
+
  341
+Simulation
  342
+==========
  343
+
  344
+The ``do_simulation`` method of the ``Module`` class can be overloaded and will be executed at each clock cycle. See :ref:`simulating` for more information on using the simulator.
  345
+
  346
+Simulation of designs with several clock domains is not supported yet.
225 347
 
226 348
 Conversion for synthesis
227 349
 ************************
228 350
 
229  
-Any FHDL fragment (except, of course, its simulation functions) can be converted into synthesizable Verilog HDL. This is accomplished by using the ``convert`` function in the ``verilog`` module.
  351
+Any FHDL module (except, of course, its simulation functions) can be converted into synthesizable Verilog HDL. This is accomplished by using the ``convert`` function in the ``verilog`` module.
230 352
 
231 353
 Migen does not provide support for any specific synthesis tools or ASIC/FPGA technologies. Users must run themselves the generated code through the appropriate tool flow for hardware implementation.
232 354
 
233 355
 The Mibuild package, available separately from the Migen website, provides scripts to interface third-party FPGA tools to Migen and a database of boards for the easy deployment of designs.
234  
-
235  
-Multi-clock-domain designs
236  
-**************************
237  
-
238  
-A clock domain is identified by its name (a string). A design with multiple clock domains passes a dictionary instead of a list of synchronous statements in the ``Fragment`` constructor. Keys of that dictionary are the names of the clock domains, and the associated values are the statements that should be executed at each cycle of the clock in that domain.
239  
-
240  
-Mapping clock domain names to clock signals is done during conversion. The ``clock_domain`` parameter of the conversion function accepts a dictionary keyed by clock domain names that contains ``ClockDomain`` objects. ``ClockDomain`` objects are containers for a clock signal and a optional reset signal. Those signals can be driven like other FHDL signals.

No commit comments for this range

Something went wrong with that request. Please try again.