Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/migen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f5ab734bdf24
Choose a base ref
...
head repository: m-labs/migen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fd88b9b8a393
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Sep 17, 2015

  1. doc: Constant

    sbourdeauducq committed Sep 17, 2015

    Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    0b9c672 View commit details
  2. add unittests for Constant

    jordens authored and sbourdeauducq committed Sep 17, 2015
    Copy the full SHA
    74c9159 View commit details
  3. Copy the full SHA
    fd88b9b View commit details
Showing with 35 additions and 3 deletions.
  1. +7 −3 doc/fhdl.rst
  2. +28 −0 migen/test/test_constant.py
10 changes: 7 additions & 3 deletions doc/fhdl.rst
Original file line number Diff line number Diff line change
@@ -13,13 +13,17 @@ FHDL is made of several elements, which are briefly explained below. They all ca
Expressions
***********

Integers and booleans
=====================
Constants
=========

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.

Python integers and booleans can appear in FHDL expressions to represent constant values in a circuit. ``True`` and ``False`` are interpreted as 1 and 0, respectively.
``True`` and ``False`` are interpreted as 1 and 0, respectively.

Negative integers are explicitly supported. As with MyHDL [countin]_, arithmetic operations return the natural results.

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])``.

.. [countin] http://www.jandecaluwe.com/hdldesign/counting.html
Signal
28 changes: 28 additions & 0 deletions migen/test/test_constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest

from migen import *
from migen.test.support import SimCase


class ConstantCase(SimCase, unittest.TestCase):
class TestBench(Module):
def __init__(self):
self.sigs = [
(Signal(3), Constant(0), 0),
(Signal(3), Constant(5), 5),
(Signal(3), Constant(1, 2), 1),
(Signal(3), Constant(-1, 7), 7),
(Signal(3), Constant(0b10101)[:3], 0b101),
(Signal(3), Constant(0b10101)[1:4], 0b10),
]
self.comb += [a.eq(b) for a, b, c in self.sigs]

def test_comparisons(self):
def gen():
for s, l, v in self.tb.sigs:
s = yield s
self.assertEqual(
s, int(v),
"got {}, want {} from literal {}".format(
s, v, l))
self.run_with(gen())