Skip to content

Commit

Permalink
Multipliers (#26)
Browse files Browse the repository at this point in the history
* Done classes for array multipliers

* Added tests

* Finished docs
  • Loading branch information
jamesjiang52 committed Nov 18, 2018
1 parent 8ace891 commit a058c83
Show file tree
Hide file tree
Showing 24 changed files with 780 additions and 5 deletions.
153 changes: 153 additions & 0 deletions bitwise/arithmetic/MULT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"""
The following classes are defined:
"""

from .. import wire
from . import gate
from . import ADD

Wire = wire.Wire
Bus4 = wire.Bus4
Bus8 = wire.Bus8
Bus16 = wire.Bus16


class Multiplier2:
"""Construct a new 2-bit unsigned multiplier.
Args:
a_1: An object of type Wire. The most significant bit of the
multiplicand.
a_2: An object of type Wire. The least significant bit of the
multiplicant.
b_1: An object of type Wire. The most significant bit of the
multiplier.
b_2: An object of type Wire. The least significant bit of the
multiplier.
product_bus: An object of type Bus4. The product. product_bus[0] and
product_bus[3] are the most and least significant bit,
respectively.
Raises:
TypeError: If product_bus is not a bus of width 4.
"""
def __init__(self, a_1, a_2, b_1, b_2, product_bus):
if len(product_bus) != 4:
raise TypeError(
"Expected bus of width 4, received bus of width {0}.".format(
len(product_bus)
)
)

and_1 = Wire()
and_3 = Wire()
and_4 = Wire()
cout_1 = Wire()
gnd = Wire()
gnd.value = 0

gate.ANDGate2(a_1, b_2, and_1)
gate.ANDGate2(a_2, b_2, product_bus[3])
gate.ANDGate2(a_1, b_1, and_3)
gate.ANDGate2(a_2, b_1, and_4)

ADD.FullAdder(gnd, and_1, and_4, cout_1, product_bus[2])
ADD.FullAdder(cout_1, gnd, and_3, product_bus[0], product_bus[1])


class Multiplier4:
"""Construct a new 4-bit unsigned multiplier.
Args:
a_bus: An object of type Bus4. The multiplicand. a_bus[0] and a_bus[3]
are the most and least significant bit, respectively.
b_bus: An object of type Bus4. The multiplier. b_bus[0] and b_bus[3]
are the most and least significant bit, respectively.
product_bus: An object of type Bus8. The product. product_bus[0] and
product_bus[7] are the most and least significant bit,
respectively.
Raises:
TypeError: If either a_bus or b_bus is not a bus of width 4, or if
product_bus is not a bus of width 8.
"""
def __init__(self, a_bus, b_bus, product_bus):
if len(a_bus) != 4:
raise TypeError(
"Expected bus of width 4, received bus of width {0}.".format(
len(a_bus)
)
)

if len(b_bus) != 4:
raise TypeError(
"Expected bus of width 4, received bus of width {0}.".format(
len(b_bus)
)
)

if len(product_bus) != 8:
raise TypeError(
"Expected bus of width 8, received bus of width {0}.".format(
len(product_bus)
)
)

and_3_0 = Wire()
and_2_0 = Wire()
and_1_0 = Wire()
and_3_1 = Wire()
and_2_1 = Wire()
and_1_1 = Wire()
and_0_1 = Wire()
and_3_2 = Wire()
and_2_2 = Wire()
and_1_2 = Wire()
and_0_2 = Wire()
and_3_3 = Wire()
and_2_3 = Wire()
and_1_3 = Wire()
and_0_3 = Wire()
gnd = Wire()
gnd.value = 0

cout_1 = Wire()
add_1_1 = Wire()
add_1_2 = Wire()
add_1_3 = Wire()
cout_2 = Wire()
add_2_1 = Wire()
add_2_2 = Wire()
add_2_3 = Wire()

gate.ANDGate2(a_bus[0], b_bus[3], and_3_0)
gate.ANDGate2(a_bus[1], b_bus[3], and_2_0)
gate.ANDGate2(a_bus[2], b_bus[3], and_1_0)
gate.ANDGate2(a_bus[3], b_bus[3], product_bus[7])
gate.ANDGate2(a_bus[0], b_bus[2], and_3_1)
gate.ANDGate2(a_bus[1], b_bus[2], and_2_1)
gate.ANDGate2(a_bus[2], b_bus[2], and_1_1)
gate.ANDGate2(a_bus[3], b_bus[2], and_0_1)
gate.ANDGate2(a_bus[0], b_bus[1], and_3_2)
gate.ANDGate2(a_bus[1], b_bus[1], and_2_2)
gate.ANDGate2(a_bus[2], b_bus[1], and_1_2)
gate.ANDGate2(a_bus[3], b_bus[1], and_0_2)
gate.ANDGate2(a_bus[0], b_bus[0], and_3_3)
gate.ANDGate2(a_bus[1], b_bus[0], and_2_3)
gate.ANDGate2(a_bus[2], b_bus[0], and_1_3)
gate.ANDGate2(a_bus[3], b_bus[0], and_0_3)

bus_1_1 = Bus4(and_3_1, and_2_1, and_1_1, and_0_1)
bus_1_2 = Bus4(gnd, and_3_0, and_2_0, and_1_0)
bus_1_out = Bus4(add_1_1, add_1_2, add_1_3, product_bus[6])
bus_2_1 = Bus4(and_3_2, and_2_2, and_1_2, and_0_2)
bus_2_2 = Bus4(cout_1, add_1_1, add_1_2, add_1_3)
bus_2_out = Bus4(add_2_1, add_2_2, add_2_3, product_bus[5])
bus_3_1 = Bus4(and_3_3, and_2_3, and_1_3, and_0_3)
bus_3_2 = Bus4(cout_2, add_2_1, add_2_2, add_2_3)
bus_3_out = Bus4(*product_bus[1:5])

ADD.Adder4(gnd, bus_1_1, bus_1_2, cout_1, bus_1_out)
ADD.Adder4(gnd, bus_2_1, bus_2_2, cout_2, bus_2_out)
ADD.Adder4(gnd, bus_3_1, bus_3_2, product_bus[0], bus_3_out)
1 change: 1 addition & 0 deletions bitwise/arithmetic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .ADD import *
from .ADD_SUB import *
from .MULT import *
Binary file modified docs/_build/doctrees/api.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/arithmetic.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/changelog.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
2 changes: 2 additions & 0 deletions docs/_build/html/_images/Multiplier2.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/_build/html/_images/Multiplier4.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/_build/html/_sources/api.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ API Documentation
* :ref:`AdderSubtractor16`
* :ref:`FullAdder`
* :ref:`HalfAdder`
* :ref:`Multiplier2`
* :ref:`Multiplier4`

:doc:`gate`
===========
Expand Down
82 changes: 82 additions & 0 deletions docs/_build/html/_sources/arithmetic.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,85 @@ Args:
* ``b``: An object of type ``Wire``. The second addend.
* ``carry_out``: An object of type ``Wire``. The carry-out of the adder.
* ``sum_``: An object of type ``Wire``. The sum of the two addends.


.. _Multiplier2:

Multiplier2
===========

Class ``bw.arithmetic.Multiplier2``
-----------------------------------

.. image:: images/schematics/arithmetic/Multiplier2.svg
:width: 600px

Defined in `bitwise/arithmetic/MULT.py <https://github.com/jamesjiang52/Bitwise/blob/master/bitwise/arithmetic/MULT.py>`_.

2-bit unsigned array `multiplier <https://en.wikipedia.org/wiki/Binary_multiplier>`_.

__init__
--------

::

__init__(
a_1,
a_2,
b_1,
b_2,
product_bus
)

Construct a new 2-bit unsigned multiplier.

Args:
~~~~~
* ``a_1``: An object of type ``Wire``. The most significant bit of the multiplicand.
* ``a_2``: An object of type ``Wire``. The least significant bit of the multiplicant.
* ``b_1``: An object of type ``Wire``. The most significant bit of the multiplier.
* ``b_2``: An object of type ``Wire``. The least significant bit of the multiplier.
* ``product_bus``: An object of type ``Bus4``. The product. ``product_bus[0]`` and ``product_bus[3]`` are the most and least significant bit, respectively.

Raises:
~~~~~~~
* ``TypeError``: If ``product_bus`` is not a bus of width 4.


.. _Multiplier4:

Multiplier4
===========

Class ``bw.arithmetic.Multiplier4``
-----------------------------------

.. image:: images/schematics/arithmetic/Multiplier4.svg
:width: 800px

Defined in `bitwise/arithmetic/MULT.py <https://github.com/jamesjiang52/Bitwise/blob/master/bitwise/arithmetic/MULT.py>`_.

4-bit unsigned array `multiplier <https://en.wikipedia.org/wiki/Binary_multiplier>`_.

__init__
--------

::

__init__(
a_bus,
b_bus,
product_bus
)

Construct a new 4-bit unsigned multiplier.

Args:
~~~~~
* ``a_bus``: An object of type ``Bus4``. The multiplicand. ``a_bus[0]`` and ``a_bus[3]`` are the most and least significant bit, respectively.
* ``b_bus``: An object of type ``Bus4``. The multiplier. ``b_bus[0]`` and ``b_bus[3]`` are the most and least significant bit, respectively.
* ``product_bus``: An object of type ``Bus8``. The product. ``product_bus[0]`` and ``product_bus[7]`` are the most and least significant bit, respectively.

Raises:
~~~~~~~
* ``TypeError``: If either ``a_bus`` or ``b_bus`` is not a bus of width 4, or if ``product_bus`` is not a bus of width 8.
7 changes: 7 additions & 0 deletions docs/_build/html/_sources/changelog.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Changelog
=========


Unreleased
==========
Added
-----
* 2- and 4-bit multipliers to arithmetic subpackage


v0.2 - 2018-11-08
=================

Expand Down
2 changes: 2 additions & 0 deletions docs/_build/html/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ <h2><a class="reference internal" href="arithmetic.html"><span class="doc">Arith
<li><a class="reference internal" href="arithmetic.html#addersubtractor16"><span class="std std-ref">AdderSubtractor16</span></a></li>
<li><a class="reference internal" href="arithmetic.html#fulladder"><span class="std std-ref">FullAdder</span></a></li>
<li><a class="reference internal" href="arithmetic.html#halfadder"><span class="std std-ref">HalfAdder</span></a></li>
<li><a class="reference internal" href="arithmetic.html#multiplier2"><span class="std std-ref">Multiplier2</span></a></li>
<li><a class="reference internal" href="arithmetic.html#multiplier4"><span class="std std-ref">Multiplier4</span></a></li>
</ul>
</div>
<div class="section" id="gate">
Expand Down

0 comments on commit a058c83

Please sign in to comment.