|
| 1 | +.. _fixedpoint: |
| 2 | + |
| 3 | +Fixed-Point Fields |
| 4 | +================== |
| 5 | + |
| 6 | +`Fixed-point <https://en.wikipedia.org/wiki/Fixed-point_arithmetic>`_ numbers |
| 7 | +can be used to efficiently represent real numbers using integers. Fixed-point |
| 8 | +numbers consist of some combination of integer bits and fractional bits. The |
| 9 | +number of integer/fractional bits is usually implicitly tracked (not stored) |
| 10 | +for each number, unlike for floating-point numbers. |
| 11 | + |
| 12 | +For this VHDL exporter, these properties only affect the signal type in |
| 13 | +the the ``hwif`` structs. There is no special handling in the internals of |
| 14 | +the regblock. |
| 15 | + |
| 16 | +Properties |
| 17 | +---------- |
| 18 | +Fields can be declared as fixed-point numbers using the following two properties: |
| 19 | + |
| 20 | +.. literalinclude:: ../../hdl-src/regblock_udps.rdl |
| 21 | + :lines: 46-54 |
| 22 | + |
| 23 | +The :ref:`is_signed<signed>` property can be used in conjunction with these |
| 24 | +properties to declare signed fixed-point fields. |
| 25 | + |
| 26 | +These UDP definitions, along with others supported by PeakRDL-regblock, can be |
| 27 | +enabled by compiling the following file along with your design: |
| 28 | +:download:`regblock_udps.rdl <../../hdl-src/regblock_udps.rdl>`. |
| 29 | + |
| 30 | +.. describe:: intwidth |
| 31 | + |
| 32 | + * The ``intwidth`` property defines the number of integer bits in the |
| 33 | + fixed-point representation (including the sign bit, if present). |
| 34 | + |
| 35 | +.. describe:: fracwidth |
| 36 | + |
| 37 | + * The ``fracwidth`` property defines the number of fractional bits in the |
| 38 | + fixed-point representation. |
| 39 | + |
| 40 | +Representable Numbers |
| 41 | +^^^^^^^^^^^^^^^^^^^^^ |
| 42 | + |
| 43 | +The range of representable real numbers is summarized in the table below. |
| 44 | + |
| 45 | +.. list-table:: Representable Numbers |
| 46 | + :header-rows: 1 |
| 47 | + |
| 48 | + * - Signedness |
| 49 | + - Minimum Value |
| 50 | + - Maximum Value |
| 51 | + - Step Size |
| 52 | + |
| 53 | + * - Unsigned |
| 54 | + - :math:`0` |
| 55 | + - :math:`2^{\mathrm{intwidth}} - 2^{-\mathrm{fracwidth}}` |
| 56 | + - :math:`2^{-\mathrm{fracwidth}}` |
| 57 | + |
| 58 | + * - Signed |
| 59 | + - :math:`-2^{\mathrm{intwidth}-1}` |
| 60 | + - :math:`2^{\mathrm{intwidth}-1} - 2^{-\mathrm{fracwidth}}` |
| 61 | + - :math:`2^{-\mathrm{fracwidth}}` |
| 62 | + |
| 63 | +VHDL Types |
| 64 | +^^^^^^^^^^ |
| 65 | + |
| 66 | +The standard VHDL-2008 types defined in ``ieee.fixed_pkg`` are used for |
| 67 | +fixed-point numbers. |
| 68 | + |
| 69 | +When either ``intwidth`` or ``fracwidth`` are defined for a field, that field's |
| 70 | +type in the generated VHDL ``hwif`` record is ``ufixed(intwidth-1 downto -fracwidth)``, |
| 71 | +or ``sfixed(intwidth-1 downto -fracwidth)`` if ``is_signed`` is true. The bit at |
| 72 | +index :math:`i` contributes a weight of :math:`2^i` to the real number represented. |
| 73 | + |
| 74 | +Other Rules |
| 75 | +^^^^^^^^^^^ |
| 76 | +* Only one of ``intwidth`` or ``fracwidth`` need be defined. The other is |
| 77 | + inferred from the field bit width. |
| 78 | +* The bit width of the field shall be equal to ``intwidth`` + ``fracwidth``. |
| 79 | +* If both ``intwidth`` and ``fracwidth`` are defined for a field, it is an |
| 80 | + error if their sum does not equal the bit width of the field. |
| 81 | +* Either ``fracwidth`` or ``intwidth`` can be a negative integer. Because |
| 82 | + SystemRDL does not have a signed integer type, the only way to achieve |
| 83 | + this is to define one of the widths as larger than the bit width of the |
| 84 | + component so that the other width is inferred as a negative number. |
| 85 | +* The properties defined above are mutually exclusive with the ``counter`` |
| 86 | + property. |
| 87 | +* The properties defined above are mutually exclusive with the ``encode`` |
| 88 | + property. |
| 89 | + |
| 90 | +Examples |
| 91 | +-------- |
| 92 | + |
| 93 | +A 12-bit signed fixed-point field with 4 integer bits and 8 fractional bits |
| 94 | +can be declared with |
| 95 | + |
| 96 | +.. code-block:: systemrdl |
| 97 | + :emphasize-lines: 3, 4 |
| 98 | +
|
| 99 | + field { |
| 100 | + sw=rw; hw=r; |
| 101 | + intwidth = 4; |
| 102 | + is_signed; |
| 103 | + } fixedpoint_num[11:0] = 0; |
| 104 | +
|
| 105 | +This field can represent values from -8.0 to 7.99609375 |
| 106 | +in steps of 0.00390625. |
0 commit comments